blob: 13eef0309c6b19c5ec034e3bf85afab927221fd9 [file] [log] [blame]
Leon Clarke4515c472010-02-03 11:58:03 +00001// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
Leon Clarkef7060e22010-06-03 12:02:55 +010030#if defined(V8_TARGET_ARCH_X64)
31
Leon Clarke4515c472010-02-03 11:58:03 +000032#include "codegen-inl.h"
33#include "fast-codegen.h"
Steve Block6ded16b2010-05-10 14:33:55 +010034#include "scopes.h"
Leon Clarke4515c472010-02-03 11:58:03 +000035
36namespace v8 {
37namespace internal {
38
39#define __ ACCESS_MASM(masm())
40
Andrei Popescu402d9372010-02-26 13:31:12 +000041Register FastCodeGenerator::accumulator0() { return rax; }
42Register FastCodeGenerator::accumulator1() { return rdx; }
43Register FastCodeGenerator::scratch0() { return rcx; }
44Register FastCodeGenerator::scratch1() { return rdi; }
45Register FastCodeGenerator::receiver_reg() { return rbx; }
46Register FastCodeGenerator::context_reg() { return rsi; }
47
48
49void FastCodeGenerator::EmitLoadReceiver() {
Leon Clarke4515c472010-02-03 11:58:03 +000050 // Offset 2 is due to return address and saved frame pointer.
Andrei Popescu31002712010-02-23 13:46:05 +000051 int index = 2 + scope()->num_parameters();
Andrei Popescu402d9372010-02-26 13:31:12 +000052 __ movq(receiver_reg(), Operand(rbp, index * kPointerSize));
Andrei Popescu31002712010-02-23 13:46:05 +000053}
54
55
56void FastCodeGenerator::EmitGlobalVariableLoad(Handle<Object> cell) {
Andrei Popescu402d9372010-02-26 13:31:12 +000057 ASSERT(!destination().is(no_reg));
Andrei Popescu31002712010-02-23 13:46:05 +000058 ASSERT(cell->IsJSGlobalPropertyCell());
Andrei Popescu402d9372010-02-26 13:31:12 +000059
60 __ Move(destination(), cell);
61 __ movq(destination(),
62 FieldOperand(destination(), JSGlobalPropertyCell::kValueOffset));
Andrei Popescu31002712010-02-23 13:46:05 +000063 if (FLAG_debug_code) {
Andrei Popescu402d9372010-02-26 13:31:12 +000064 __ Cmp(destination(), Factory::the_hole_value());
Andrei Popescu31002712010-02-23 13:46:05 +000065 __ Check(not_equal, "DontDelete cells can't contain the hole");
Leon Clarke4515c472010-02-03 11:58:03 +000066 }
Andrei Popescu402d9372010-02-26 13:31:12 +000067
68 // The loaded value is not known to be a smi.
69 clear_as_smi(destination());
Leon Clarke4515c472010-02-03 11:58:03 +000070}
71
72
73void FastCodeGenerator::EmitThisPropertyStore(Handle<String> name) {
74 LookupResult lookup;
Andrei Popescu31002712010-02-23 13:46:05 +000075 info()->receiver()->Lookup(*name, &lookup);
Leon Clarke4515c472010-02-03 11:58:03 +000076
Andrei Popescu31002712010-02-23 13:46:05 +000077 ASSERT(lookup.holder() == *info()->receiver());
Leon Clarke4515c472010-02-03 11:58:03 +000078 ASSERT(lookup.type() == FIELD);
Andrei Popescu31002712010-02-23 13:46:05 +000079 Handle<Map> map(Handle<HeapObject>::cast(info()->receiver())->map());
Leon Clarke4515c472010-02-03 11:58:03 +000080 int index = lookup.GetFieldIndex() - map->inobject_properties();
81 int offset = index * kPointerSize;
82
Andrei Popescu402d9372010-02-26 13:31:12 +000083 // We will emit the write barrier unless the stored value is statically
84 // known to be a smi.
85 bool needs_write_barrier = !is_smi(accumulator0());
86
87 // Perform the store. Negative offsets are inobject properties.
Leon Clarke4515c472010-02-03 11:58:03 +000088 if (offset < 0) {
89 offset += map->instance_size();
Andrei Popescu402d9372010-02-26 13:31:12 +000090 __ movq(FieldOperand(receiver_reg(), offset), accumulator0());
91 if (needs_write_barrier) {
92 // Preserve receiver from write barrier.
93 __ movq(scratch0(), receiver_reg());
94 }
Leon Clarke4515c472010-02-03 11:58:03 +000095 } else {
96 offset += FixedArray::kHeaderSize;
Andrei Popescu402d9372010-02-26 13:31:12 +000097 __ movq(scratch0(),
98 FieldOperand(receiver_reg(), JSObject::kPropertiesOffset));
99 __ movq(FieldOperand(scratch0(), offset), accumulator0());
Leon Clarke4515c472010-02-03 11:58:03 +0000100 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000101
102 if (needs_write_barrier) {
103 if (destination().is(no_reg)) {
104 // After RecordWrite accumulator0 is only accidently a smi, but it is
105 // already marked as not known to be one.
106 __ RecordWrite(scratch0(), offset, accumulator0(), scratch1());
107 } else {
108 // Copy the value to the other accumulator to preserve a copy from the
109 // write barrier. One of the accumulators is available as a scratch
110 // register. Neither is a smi.
111 __ movq(accumulator1(), accumulator0());
112 clear_as_smi(accumulator1());
113 Register value_scratch = other_accumulator(destination());
114 __ RecordWrite(scratch0(), offset, value_scratch, scratch1());
115 }
116 } else if (destination().is(accumulator1())) {
117 __ movq(accumulator1(), accumulator0());
118 // Is a smi because we do not need the write barrier.
119 set_as_smi(accumulator1());
120 }
121}
122
123
124void FastCodeGenerator::EmitThisPropertyLoad(Handle<String> name) {
125 ASSERT(!destination().is(no_reg));
126 LookupResult lookup;
127 info()->receiver()->Lookup(*name, &lookup);
128
129 ASSERT(lookup.holder() == *info()->receiver());
130 ASSERT(lookup.type() == FIELD);
131 Handle<Map> map(Handle<HeapObject>::cast(info()->receiver())->map());
132 int index = lookup.GetFieldIndex() - map->inobject_properties();
133 int offset = index * kPointerSize;
134
135 // Perform the load. Negative offsets are inobject properties.
136 if (offset < 0) {
137 offset += map->instance_size();
138 __ movq(destination(), FieldOperand(receiver_reg(), offset));
139 } else {
140 offset += FixedArray::kHeaderSize;
141 __ movq(scratch0(),
142 FieldOperand(receiver_reg(), JSObject::kPropertiesOffset));
143 __ movq(destination(), FieldOperand(scratch0(), offset));
144 }
145
146 // The loaded value is not known to be a smi.
147 clear_as_smi(destination());
148}
149
150
151void FastCodeGenerator::EmitBitOr() {
152 if (is_smi(accumulator0()) && is_smi(accumulator1())) {
153 // If both operands are known to be a smi then there is no need to check
154 // the operands or result.
155 if (destination().is(no_reg)) {
156 __ or_(accumulator1(), accumulator0());
157 } else {
158 // Leave the result in the destination register. Bitwise or is
159 // commutative.
160 __ or_(destination(), other_accumulator(destination()));
161 }
162 } else {
163 // Left is in accumulator1, right in accumulator0.
164 if (destination().is(accumulator0())) {
165 __ movq(scratch0(), accumulator0());
166 __ or_(destination(), accumulator1()); // Or is commutative.
167 Label* bailout =
168 info()->AddBailout(accumulator1(), scratch0()); // Left, right.
169 __ JumpIfNotSmi(destination(), bailout);
170 } else if (destination().is(accumulator1())) {
171 __ movq(scratch0(), accumulator1());
172 __ or_(destination(), accumulator0());
173 Label* bailout = info()->AddBailout(scratch0(), accumulator0());
174 __ JumpIfNotSmi(destination(), bailout);
175 } else {
176 ASSERT(destination().is(no_reg));
177 __ movq(scratch0(), accumulator1());
178 __ or_(scratch0(), accumulator0());
179 Label* bailout = info()->AddBailout(accumulator1(), accumulator0());
180 __ JumpIfNotSmi(scratch0(), bailout);
181 }
182 }
183
184 // If we didn't bailout, the result (in fact, both inputs too) is known to
185 // be a smi.
186 set_as_smi(accumulator0());
187 set_as_smi(accumulator1());
Leon Clarke4515c472010-02-03 11:58:03 +0000188}
189
190
Andrei Popescu31002712010-02-23 13:46:05 +0000191void FastCodeGenerator::Generate(CompilationInfo* compilation_info) {
Leon Clarke4515c472010-02-03 11:58:03 +0000192 ASSERT(info_ == NULL);
Andrei Popescu31002712010-02-23 13:46:05 +0000193 info_ = compilation_info;
Steve Block6ded16b2010-05-10 14:33:55 +0100194 Comment cmnt(masm_, "[ function compiled by fast code generator");
Leon Clarke4515c472010-02-03 11:58:03 +0000195
196 // Save the caller's frame pointer and set up our own.
197 Comment prologue_cmnt(masm(), ";; Prologue");
198 __ push(rbp);
199 __ movq(rbp, rsp);
200 __ push(rsi); // Context.
201 __ push(rdi); // Closure.
202 // Note that we keep a live register reference to esi (context) at this
203 // point.
204
Andrei Popescu402d9372010-02-26 13:31:12 +0000205 Label* bailout_to_beginning = info()->AddBailout();
206 // Receiver (this) is allocated to a fixed register.
207 if (info()->has_this_properties()) {
208 Comment cmnt(masm(), ";; MapCheck(this)");
209 if (FLAG_print_ir) {
210 PrintF("MapCheck(this)\n");
211 }
212 ASSERT(info()->has_receiver() && info()->receiver()->IsHeapObject());
213 Handle<HeapObject> object = Handle<HeapObject>::cast(info()->receiver());
214 Handle<Map> map(object->map());
215 EmitLoadReceiver();
216 __ CheckMap(receiver_reg(), map, bailout_to_beginning, false);
217 }
Leon Clarke4515c472010-02-03 11:58:03 +0000218
Andrei Popescu402d9372010-02-26 13:31:12 +0000219 // If there is a global variable access check if the global object is the
220 // same as at lazy-compilation time.
221 if (info()->has_globals()) {
222 Comment cmnt(masm(), ";; MapCheck(GLOBAL)");
223 if (FLAG_print_ir) {
224 PrintF("MapCheck(GLOBAL)\n");
225 }
226 ASSERT(info()->has_global_object());
227 Handle<Map> map(info()->global_object()->map());
228 __ movq(scratch0(), CodeGenerator::GlobalObject());
229 __ CheckMap(scratch0(), map, bailout_to_beginning, true);
230 }
Andrei Popescu31002712010-02-23 13:46:05 +0000231
232 VisitStatements(info()->function()->body());
Leon Clarke4515c472010-02-03 11:58:03 +0000233
234 Comment return_cmnt(masm(), ";; Return(<undefined>)");
Andrei Popescu402d9372010-02-26 13:31:12 +0000235 if (FLAG_print_ir) {
236 PrintF("Return(<undefined>)\n");
237 }
Leon Clarke4515c472010-02-03 11:58:03 +0000238 __ LoadRoot(rax, Heap::kUndefinedValueRootIndex);
Leon Clarke4515c472010-02-03 11:58:03 +0000239 __ movq(rsp, rbp);
240 __ pop(rbp);
Andrei Popescu31002712010-02-23 13:46:05 +0000241 __ ret((scope()->num_parameters() + 1) * kPointerSize);
Leon Clarke4515c472010-02-03 11:58:03 +0000242}
243
244
245#undef __
246
247
248} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +0100249
250#endif // V8_TARGET_ARCH_X64