blob: 87c49686a0997eeebda22eb76fe3987febbcec13 [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2010 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000030#if defined(V8_TARGET_ARCH_ARM)
31
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "bootstrapper.h"
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000033#include "code-stubs.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034#include "codegen-inl.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000035#include "compiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036#include "debug.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000037#include "ic-inl.h"
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000038#include "jsregexp.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000039#include "jump-target-inl.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000040#include "parser.h"
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000041#include "regexp-macro-assembler.h"
42#include "regexp-stack.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000043#include "register-allocator-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044#include "runtime.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000045#include "scopes.h"
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000046#include "stub-cache.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000047#include "virtual-frame-inl.h"
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000048#include "virtual-frame-arm-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000049
kasperl@chromium.org71affb52009-05-26 05:44:31 +000050namespace v8 {
51namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000053
ricow@chromium.org30ce4112010-05-31 10:38:25 +000054#define __ ACCESS_MASM(masm_)
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000055
ager@chromium.orge2902be2009-06-08 12:21:35 +000056// -------------------------------------------------------------------------
57// Platform-specific DeferredCode functions.
58
59void DeferredCode::SaveRegisters() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000060 // On ARM you either have a completely spilled frame or you
61 // handle it yourself, but at the moment there's no automation
62 // of registers and deferred code.
ager@chromium.orge2902be2009-06-08 12:21:35 +000063}
64
65
66void DeferredCode::RestoreRegisters() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000067}
68
69
70// -------------------------------------------------------------------------
71// Platform-specific RuntimeCallHelper functions.
72
73void VirtualFrameRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
74 frame_state_->frame()->AssertIsSpilled();
75}
76
77
78void VirtualFrameRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
79}
80
81
kasperl@chromium.orga5551262010-12-07 12:49:48 +000082void StubRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000083 masm->EnterInternalFrame();
84}
85
86
kasperl@chromium.orga5551262010-12-07 12:49:48 +000087void StubRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000088 masm->LeaveInternalFrame();
ager@chromium.orge2902be2009-06-08 12:21:35 +000089}
90
ager@chromium.org3bf7b912008-11-17 09:09:45 +000091
92// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000093// CodeGenState implementation.
94
ager@chromium.org7c537e22008-10-16 08:43:32 +000095CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000096 : owner_(owner),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +000097 previous_(owner->state()) {
98 owner->set_state(this);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000099}
100
101
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000102ConditionCodeGenState::ConditionCodeGenState(CodeGenerator* owner,
103 JumpTarget* true_target,
104 JumpTarget* false_target)
105 : CodeGenState(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000106 true_target_(true_target),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000107 false_target_(false_target) {
108 owner->set_state(this);
109}
110
111
112TypeInfoCodeGenState::TypeInfoCodeGenState(CodeGenerator* owner,
113 Slot* slot,
114 TypeInfo type_info)
115 : CodeGenState(owner),
116 slot_(slot) {
117 owner->set_state(this);
118 old_type_info_ = owner->set_type_info(slot, type_info);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000119}
120
121
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000122CodeGenState::~CodeGenState() {
123 ASSERT(owner_->state() == this);
124 owner_->set_state(previous_);
125}
126
127
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000128TypeInfoCodeGenState::~TypeInfoCodeGenState() {
129 owner()->set_type_info(slot_, old_type_info_);
130}
131
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000132// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000133// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000135int CodeGenerator::inlined_write_barrier_size_ = -1;
136
ager@chromium.org5c838252010-02-19 08:53:10 +0000137CodeGenerator::CodeGenerator(MacroAssembler* masm)
138 : deferred_(8),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000139 masm_(masm),
ager@chromium.org5c838252010-02-19 08:53:10 +0000140 info_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000141 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000142 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143 cc_reg_(al),
144 state_(NULL),
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000145 loop_nesting_(0),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000146 type_info_(NULL),
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000147 function_return_(JumpTarget::BIDIRECTIONAL),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000148 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149}
150
151
152// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000153// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000155// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156// cp: callee's context
157
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000158void CodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000159 // Record the position for debugging purposes.
ager@chromium.org5c838252010-02-19 08:53:10 +0000160 CodeForFunctionPosition(info->function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000161 Comment cmnt(masm_, "[ function compiled by virtual frame code generator");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162
163 // Initialize state.
ager@chromium.org5c838252010-02-19 08:53:10 +0000164 info_ = info;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000165
166 int slots = scope()->num_parameters() + scope()->num_stack_slots();
167 ScopedVector<TypeInfo> type_info_array(slots);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000168 for (int i = 0; i < slots; i++) {
169 type_info_array[i] = TypeInfo::Unknown();
170 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000171 type_info_ = &type_info_array;
172
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000173 ASSERT(allocator_ == NULL);
174 RegisterAllocator register_allocator(this);
175 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000176 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000177 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000178 cc_reg_ = al;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000179
180 // Adjust for function-level loop nesting.
181 ASSERT_EQ(0, loop_nesting_);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000182 loop_nesting_ = info->is_in_loop() ? 1 : 0;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000183
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000184 {
185 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000187 // Entry:
188 // Stack: receiver, arguments
189 // lr: return address
190 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000192 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000194 allocator_->Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000195
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196#ifdef DEBUG
197 if (strlen(FLAG_stop_at) > 0 &&
ager@chromium.org5c838252010-02-19 08:53:10 +0000198 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000199 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000200 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 }
202#endif
203
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000204 frame_->Enter();
205 // tos: code slot
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000206
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000207 // Allocate space for locals and initialize them. This also checks
208 // for stack overflow.
209 frame_->AllocateStackSlots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000210
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000211 frame_->AssertIsSpilled();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000212 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000213 if (heap_slots > 0) {
214 // Allocate local context.
215 // Get outer context and create a new context based on it.
216 __ ldr(r0, frame_->Function());
217 frame_->EmitPush(r0);
218 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
219 FastNewContextStub stub(heap_slots);
220 frame_->CallStub(&stub, 1);
221 } else {
222 frame_->CallRuntime(Runtime::kNewContext, 1);
223 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000224
225#ifdef DEBUG
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000226 JumpTarget verified_true;
227 __ cmp(r0, cp);
228 verified_true.Branch(eq);
229 __ stop("NewContext: r0 is expected to be the same as cp");
230 verified_true.Bind();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000231#endif
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000232 // Update context local.
233 __ str(cp, frame_->Context());
234 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000235
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000236 // TODO(1241774): Improve this code:
237 // 1) only needed if we have a context
238 // 2) no need to recompute context ptr every single time
239 // 3) don't copy parameter operand code from SlotOperand!
240 {
241 Comment cmnt2(masm_, "[ copy context parameters into .context");
242 // Note that iteration order is relevant here! If we have the same
243 // parameter twice (e.g., function (x, y, x)), and that parameter
244 // needs to be copied into the context, it must be the last argument
245 // passed to the parameter that needs to be copied. This is a rare
246 // case so we don't check for it, instead we rely on the copying
247 // order: such a parameter is copied repeatedly into the same
248 // context location and thus the last value is what is seen inside
249 // the function.
250 frame_->AssertIsSpilled();
251 for (int i = 0; i < scope()->num_parameters(); i++) {
252 Variable* par = scope()->parameter(i);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000253 Slot* slot = par->AsSlot();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000254 if (slot != NULL && slot->type() == Slot::CONTEXT) {
255 ASSERT(!scope()->is_global_scope()); // No params in global scope.
256 __ ldr(r1, frame_->ParameterAt(i));
257 // Loads r2 with context; used below in RecordWrite.
258 __ str(r1, SlotOperand(slot, r2));
259 // Load the offset into r3.
260 int slot_offset =
261 FixedArray::kHeaderSize + slot->index() * kPointerSize;
262 __ RecordWrite(r2, Operand(slot_offset), r3, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000263 }
264 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000265 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000266
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000267 // Store the arguments object. This must happen after context
268 // initialization because the arguments object may be stored in
269 // the context.
270 if (ArgumentsMode() != NO_ARGUMENTS_ALLOCATION) {
271 StoreArgumentsObject(true);
272 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000273
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000274 // Initialize ThisFunction reference if present.
275 if (scope()->is_function_scope() && scope()->function() != NULL) {
276 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000277 StoreToSlot(scope()->function()->AsSlot(), NOT_CONST_INIT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000278 }
279
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000280 // Initialize the function return target after the locals are set
281 // up, because it needs the expected frame height from the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000282 function_return_.SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000283 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000285 // Generate code to 'execute' declarations and initialize functions
286 // (source elements). In case of an illegal redeclaration we need to
287 // handle that instead of processing the declarations.
ager@chromium.org5c838252010-02-19 08:53:10 +0000288 if (scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000290 scope()->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 } else {
292 Comment cmnt(masm_, "[ declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000293 ProcessDeclarations(scope()->declarations());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000294 // Bail out if a stack-overflow exception occurred when processing
295 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000296 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 }
298
mads.s.ager31e71382008-08-13 09:32:07 +0000299 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000300 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000301 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000302 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303
304 // Compile the body of the function in a vanilla state. Don't
305 // bother compiling all the code if the scope has an illegal
306 // redeclaration.
ager@chromium.org5c838252010-02-19 08:53:10 +0000307 if (!scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308 Comment cmnt(masm_, "[ function body");
309#ifdef DEBUG
310 bool is_builtin = Bootstrapper::IsActive();
311 bool should_trace =
312 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000313 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000314 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000315 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000316 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317#endif
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000318 VisitStatements(info->function()->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320 }
321
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000322 // Handle the return from the function.
323 if (has_valid_frame()) {
324 // If there is a valid frame, control flow can fall off the end of
325 // the body. In that case there is an implicit return statement.
326 ASSERT(!function_return_is_shadowed_);
327 frame_->PrepareForReturn();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000328 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000329 if (function_return_.is_bound()) {
330 function_return_.Jump();
331 } else {
332 function_return_.Bind();
333 GenerateReturnSequence();
334 }
335 } else if (function_return_.is_linked()) {
336 // If the return target has dangling jumps to it, then we have not
337 // yet generated the return sequence. This can happen when (a)
338 // control does not flow off the end of the body so we did not
339 // compile an artificial return statement just above, and (b) there
340 // are return statements in the body but (c) they are all shadowed.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000341 function_return_.Bind();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000342 GenerateReturnSequence();
mads.s.ager31e71382008-08-13 09:32:07 +0000343 }
344
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000345 // Adjust for function-level loop nesting.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000346 ASSERT(loop_nesting_ == info->is_in_loop()? 1 : 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000347 loop_nesting_ = 0;
348
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 ASSERT(!has_cc());
351 ASSERT(state_ == NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000352 ASSERT(loop_nesting() == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000353 ASSERT(!function_return_is_shadowed_);
354 function_return_.Unuse();
355 DeleteFrame();
356
357 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000358 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000359 ProcessDeferred();
360 }
361
362 allocator_ = NULL;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000363 type_info_ = NULL;
364}
365
366
367int CodeGenerator::NumberOfSlot(Slot* slot) {
368 if (slot == NULL) return kInvalidSlotNumber;
369 switch (slot->type()) {
370 case Slot::PARAMETER:
371 return slot->index();
372 case Slot::LOCAL:
373 return slot->index() + scope()->num_parameters();
374 default:
375 break;
376 }
377 return kInvalidSlotNumber;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378}
379
380
ager@chromium.org7c537e22008-10-16 08:43:32 +0000381MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
382 // Currently, this assertion will fail if we try to assign to
383 // a constant variable that is constant because it is read-only
384 // (such as the variable referring to a named function expression).
385 // We need to implement assignments to read-only variables.
386 // Ideally, we should do this during AST generation (by converting
387 // such assignments into expression statements); however, in general
388 // we may not be able to make the decision until past AST generation,
389 // that is when the entire program is known.
390 ASSERT(slot != NULL);
391 int index = slot->index();
392 switch (slot->type()) {
393 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000394 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000395
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000396 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000397 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000398
399 case Slot::CONTEXT: {
400 // Follow the context chain if necessary.
401 ASSERT(!tmp.is(cp)); // do not overwrite context register
402 Register context = cp;
403 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000404 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000405 // Load the closure.
406 // (All contexts, even 'with' contexts, have a closure,
407 // and it is the same for all contexts inside a function.
408 // There is no need to go to the function context first.)
409 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
410 // Load the function context (which is the incoming, outer context).
411 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
412 context = tmp;
413 }
414 // We may have a 'with' context now. Get the function context.
415 // (In fact this mov may never be the needed, since the scope analysis
416 // may not permit a direct context access in this case and thus we are
417 // always at a function context. However it is safe to dereference be-
418 // cause the function context of a function context is itself. Before
419 // deleting this mov we should try to create a counter-example first,
420 // though...)
421 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
422 return ContextOperand(tmp, index);
423 }
424
425 default:
426 UNREACHABLE();
427 return MemOperand(r0, 0);
428 }
429}
430
431
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000432MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
433 Slot* slot,
434 Register tmp,
435 Register tmp2,
436 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000437 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000438 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000439
ager@chromium.org381abbb2009-02-25 13:23:22 +0000440 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
441 if (s->num_heap_slots() > 0) {
442 if (s->calls_eval()) {
443 // Check that extension is NULL.
444 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
445 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000446 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000447 }
448 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
449 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
450 context = tmp;
451 }
452 }
453 // Check that last extension is NULL.
454 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
455 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000456 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000457 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000458 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000459}
460
461
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000462// Loads a value on TOS. If it is a boolean value, the result may have been
463// (partially) translated into branches, or it may have set the condition
464// code register. If force_cc is set, the value is forced to set the
465// condition code register and no value is pushed. If the condition code
466// register was set, has_cc() is true and cc_reg_ contains the condition to
467// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000468void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000469 JumpTarget* true_target,
470 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000471 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000472 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000473 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000475 { ConditionCodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000476 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000477
478 // If we hit a stack overflow, we may not have actually visited
479 // the expression. In that case, we ensure that we have a
480 // valid-looking frame state because we will continue to generate
481 // code as we unwind the C++ stack.
482 //
483 // It's possible to have both a stack overflow and a valid frame
484 // state (eg, a subexpression overflowed, visiting it returned
485 // with a dummied frame state, and visiting this expression
486 // returned with a normal-looking state).
487 if (HasStackOverflow() &&
488 has_valid_frame() &&
489 !has_cc() &&
490 frame_->height() == original_height) {
491 true_target->Jump();
492 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000493 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000494 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000495 // Convert the TOS value to a boolean in the condition code register.
496 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000498 ASSERT(!force_cc || !has_valid_frame() || has_cc());
499 ASSERT(!has_valid_frame() ||
500 (has_cc() && frame_->height() == original_height) ||
501 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502}
503
504
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000505void CodeGenerator::Load(Expression* expr) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000506 // We generally assume that we are not in a spilled scope for most
507 // of the code generator. A failure to ensure this caused issue 815
508 // and this assert is designed to catch similar issues.
509 frame_->AssertIsNotSpilled();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000510#ifdef DEBUG
511 int original_height = frame_->height();
512#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000513 JumpTarget true_target;
514 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000515 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516
517 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000518 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000519 JumpTarget loaded;
520 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000521 materialize_true.Branch(cc_reg_);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000522 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000523 loaded.Jump();
524 materialize_true.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000525 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000526 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 cc_reg_ = al;
528 }
529
530 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000531 // We have at least one condition value that has been "translated"
532 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000533 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000534 if (frame_ != NULL) {
535 loaded.Jump(); // Don't lose the current TOS.
536 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000538 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000540 true_target.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000541 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000543 // If both "true" and "false" need to be loaded jump across the code for
544 // "false".
545 if (both) {
546 loaded.Jump();
547 }
548 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000550 false_target.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000551 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000553 // A value is loaded on all paths reaching this point.
554 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000556 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 ASSERT(!has_cc());
ager@chromium.orgac091b72010-05-05 07:34:42 +0000558 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559}
560
561
ager@chromium.org7c537e22008-10-16 08:43:32 +0000562void CodeGenerator::LoadGlobal() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000563 Register reg = frame_->GetTOSRegister();
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000564 __ ldr(reg, GlobalObjectOperand());
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000565 frame_->EmitPush(reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566}
567
568
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000569void CodeGenerator::LoadGlobalReceiver(Register scratch) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000570 Register reg = frame_->GetTOSRegister();
571 __ ldr(reg, ContextOperand(cp, Context::GLOBAL_INDEX));
572 __ ldr(reg,
573 FieldMemOperand(reg, GlobalObject::kGlobalReceiverOffset));
574 frame_->EmitPush(reg);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000575}
576
577
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000578ArgumentsAllocationMode CodeGenerator::ArgumentsMode() {
579 if (scope()->arguments() == NULL) return NO_ARGUMENTS_ALLOCATION;
580 ASSERT(scope()->arguments_shadow() != NULL);
581 // We don't want to do lazy arguments allocation for functions that
582 // have heap-allocated contexts, because it interfers with the
583 // uninitialized const tracking in the context objects.
584 return (scope()->num_heap_slots() > 0)
585 ? EAGER_ARGUMENTS_ALLOCATION
586 : LAZY_ARGUMENTS_ALLOCATION;
587}
588
589
590void CodeGenerator::StoreArgumentsObject(bool initial) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000591 ArgumentsAllocationMode mode = ArgumentsMode();
592 ASSERT(mode != NO_ARGUMENTS_ALLOCATION);
593
594 Comment cmnt(masm_, "[ store arguments object");
595 if (mode == LAZY_ARGUMENTS_ALLOCATION && initial) {
596 // When using lazy arguments allocation, we store the hole value
597 // as a sentinel indicating that the arguments object hasn't been
598 // allocated yet.
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000599 frame_->EmitPushRoot(Heap::kArgumentsMarkerRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000600 } else {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000601 frame_->SpillAll();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000602 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
603 __ ldr(r2, frame_->Function());
604 // The receiver is below the arguments, the return address, and the
605 // frame pointer on the stack.
606 const int kReceiverDisplacement = 2 + scope()->num_parameters();
607 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
608 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
609 frame_->Adjust(3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000610 __ Push(r2, r1, r0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000611 frame_->CallStub(&stub, 3);
612 frame_->EmitPush(r0);
613 }
614
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000615 Variable* arguments = scope()->arguments();
616 Variable* shadow = scope()->arguments_shadow();
617 ASSERT(arguments != NULL && arguments->AsSlot() != NULL);
618 ASSERT(shadow != NULL && shadow->AsSlot() != NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000619 JumpTarget done;
620 if (mode == LAZY_ARGUMENTS_ALLOCATION && !initial) {
621 // We have to skip storing into the arguments slot if it has
622 // already been written to. This can happen if the a function
623 // has a local variable named 'arguments'.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000624 LoadFromSlot(scope()->arguments()->AsSlot(), NOT_INSIDE_TYPEOF);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000625 Register arguments = frame_->PopToRegister();
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000626 __ LoadRoot(ip, Heap::kArgumentsMarkerRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000627 __ cmp(arguments, ip);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000628 done.Branch(ne);
629 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000630 StoreToSlot(arguments->AsSlot(), NOT_CONST_INIT);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000631 if (mode == LAZY_ARGUMENTS_ALLOCATION) done.Bind();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000632 StoreToSlot(shadow->AsSlot(), NOT_CONST_INIT);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000633}
634
635
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000636void CodeGenerator::LoadTypeofExpression(Expression* expr) {
637 // Special handling of identifiers as subexpressions of typeof.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000638 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000640 // For a global variable we build the property reference
641 // <global>.<variable> and perform a (regular non-contextual) property
642 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
644 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000645 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000646 Reference ref(this, &property);
ager@chromium.org357bf652010-04-12 11:30:10 +0000647 ref.GetValue();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000648 } else if (variable != NULL && variable->AsSlot() != NULL) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000649 // For a variable that rewrites to a slot, we signal it is the immediate
650 // subexpression of a typeof.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000651 LoadFromSlotCheckForArguments(variable->AsSlot(), INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000653 // Anything else can be handled normally.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000654 Load(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655 }
656}
657
658
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000659Reference::Reference(CodeGenerator* cgen,
660 Expression* expression,
661 bool persist_after_get)
662 : cgen_(cgen),
663 expression_(expression),
664 type_(ILLEGAL),
665 persist_after_get_(persist_after_get) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000666 // We generally assume that we are not in a spilled scope for most
667 // of the code generator. A failure to ensure this caused issue 815
668 // and this assert is designed to catch similar issues.
669 cgen->frame()->AssertIsNotSpilled();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670 cgen->LoadReference(this);
671}
672
673
674Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000675 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676}
677
678
ager@chromium.org7c537e22008-10-16 08:43:32 +0000679void CodeGenerator::LoadReference(Reference* ref) {
680 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681 Expression* e = ref->expression();
682 Property* property = e->AsProperty();
683 Variable* var = e->AsVariableProxy()->AsVariable();
684
685 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000686 // The expression is either a property or a variable proxy that rewrites
687 // to a property.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000688 Load(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000689 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690 ref->set_type(Reference::NAMED);
691 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000692 Load(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 ref->set_type(Reference::KEYED);
694 }
695 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000696 // The expression is a variable proxy that does not rewrite to a
697 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699 LoadGlobal();
700 ref->set_type(Reference::NAMED);
701 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000702 ASSERT(var->AsSlot() != NULL);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000703 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704 }
705 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000706 // Anything else is a runtime error.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000707 Load(e);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000708 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709 }
710}
711
712
ager@chromium.org7c537e22008-10-16 08:43:32 +0000713void CodeGenerator::UnloadReference(Reference* ref) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714 int size = ref->size();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000715 ref->set_unloaded();
ager@chromium.org357bf652010-04-12 11:30:10 +0000716 if (size == 0) return;
717
718 // Pop a reference from the stack while preserving TOS.
719 VirtualFrame::RegisterAllocationScope scope(this);
720 Comment cmnt(masm_, "[ UnloadReference");
721 if (size > 0) {
722 Register tos = frame_->PopToRegister();
723 frame_->Drop(size);
724 frame_->EmitPush(tos);
725 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726}
727
728
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
730// register to a boolean in the condition code register. The code
731// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000732void CodeGenerator::ToBoolean(JumpTarget* true_target,
733 JumpTarget* false_target) {
mads.s.ager31e71382008-08-13 09:32:07 +0000734 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735 // Only the condition code should be set.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000736 bool known_smi = frame_->KnownSmiAt(0);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000737 Register tos = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738
739 // Fast case checks
740
mads.s.ager31e71382008-08-13 09:32:07 +0000741 // Check if the value is 'false'.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000742 if (!known_smi) {
743 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
744 __ cmp(tos, ip);
745 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000747 // Check if the value is 'true'.
748 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
749 __ cmp(tos, ip);
750 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000752 // Check if the value is 'undefined'.
753 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
754 __ cmp(tos, ip);
755 false_target->Branch(eq);
756 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757
mads.s.ager31e71382008-08-13 09:32:07 +0000758 // Check if the value is a smi.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000759 __ cmp(tos, Operand(Smi::FromInt(0)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000761 if (!known_smi) {
762 false_target->Branch(eq);
763 __ tst(tos, Operand(kSmiTagMask));
764 true_target->Branch(eq);
765
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000766 // Slow case.
767 if (CpuFeatures::IsSupported(VFP3)) {
768 CpuFeatures::Scope scope(VFP3);
769 // Implements the slow case by using ToBooleanStub.
770 // The ToBooleanStub takes a single argument, and
771 // returns a non-zero value for true, or zero for false.
772 // Both the argument value and the return value use the
773 // register assigned to tos_
774 ToBooleanStub stub(tos);
775 frame_->CallStub(&stub, 0);
776 // Convert the result in "tos" to a condition code.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000777 __ cmp(tos, Operand(0, RelocInfo::NONE));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000778 } else {
779 // Implements slow case by calling the runtime.
780 frame_->EmitPush(tos);
781 frame_->CallRuntime(Runtime::kToBool, 1);
782 // Convert the result (r0) to a condition code.
783 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
784 __ cmp(r0, ip);
785 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000786 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787
788 cc_reg_ = ne;
789}
790
791
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000792void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000793 OverwriteMode overwrite_mode,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000794 GenerateInlineSmi inline_smi,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000795 int constant_rhs) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000796 // top of virtual frame: y
797 // 2nd elt. on virtual frame : x
798 // result : top of virtual frame
799
800 // Stub is entered with a call: 'return address' is in lr.
801 switch (op) {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000802 case Token::ADD:
803 case Token::SUB:
804 if (inline_smi) {
805 JumpTarget done;
806 Register rhs = frame_->PopToRegister();
807 Register lhs = frame_->PopToRegister(rhs);
808 Register scratch = VirtualFrame::scratch0();
809 __ orr(scratch, rhs, Operand(lhs));
810 // Check they are both small and positive.
811 __ tst(scratch, Operand(kSmiTagMask | 0xc0000000));
812 ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000813 STATIC_ASSERT(kSmiTag == 0);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000814 if (op == Token::ADD) {
815 __ add(r0, lhs, Operand(rhs), LeaveCC, eq);
816 } else {
817 __ sub(r0, lhs, Operand(rhs), LeaveCC, eq);
818 }
819 done.Branch(eq);
820 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
821 frame_->SpillAll();
822 frame_->CallStub(&stub, 0);
823 done.Bind();
824 frame_->EmitPush(r0);
825 break;
826 } else {
827 // Fall through!
828 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000829 case Token::BIT_OR:
830 case Token::BIT_AND:
831 case Token::BIT_XOR:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000832 if (inline_smi) {
833 bool rhs_is_smi = frame_->KnownSmiAt(0);
834 bool lhs_is_smi = frame_->KnownSmiAt(1);
835 Register rhs = frame_->PopToRegister();
836 Register lhs = frame_->PopToRegister(rhs);
837 Register smi_test_reg;
838 Condition cond;
839 if (!rhs_is_smi || !lhs_is_smi) {
840 if (rhs_is_smi) {
841 smi_test_reg = lhs;
842 } else if (lhs_is_smi) {
843 smi_test_reg = rhs;
844 } else {
845 smi_test_reg = VirtualFrame::scratch0();
846 __ orr(smi_test_reg, rhs, Operand(lhs));
847 }
848 // Check they are both Smis.
849 __ tst(smi_test_reg, Operand(kSmiTagMask));
850 cond = eq;
851 } else {
852 cond = al;
853 }
854 ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now.
855 if (op == Token::BIT_OR) {
856 __ orr(r0, lhs, Operand(rhs), LeaveCC, cond);
857 } else if (op == Token::BIT_AND) {
858 __ and_(r0, lhs, Operand(rhs), LeaveCC, cond);
859 } else {
860 ASSERT(op == Token::BIT_XOR);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000861 STATIC_ASSERT(kSmiTag == 0);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000862 __ eor(r0, lhs, Operand(rhs), LeaveCC, cond);
863 }
864 if (cond != al) {
865 JumpTarget done;
866 done.Branch(cond);
867 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
868 frame_->SpillAll();
869 frame_->CallStub(&stub, 0);
870 done.Bind();
871 }
872 frame_->EmitPush(r0);
873 break;
874 } else {
875 // Fall through!
876 }
877 case Token::MUL:
878 case Token::DIV:
879 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000880 case Token::SHL:
881 case Token::SHR:
882 case Token::SAR: {
ager@chromium.org357bf652010-04-12 11:30:10 +0000883 Register rhs = frame_->PopToRegister();
884 Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000885 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
886 frame_->SpillAll();
887 frame_->CallStub(&stub, 0);
ager@chromium.org357bf652010-04-12 11:30:10 +0000888 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889 break;
890 }
891
ager@chromium.org357bf652010-04-12 11:30:10 +0000892 case Token::COMMA: {
893 Register scratch = frame_->PopToRegister();
894 // Simply discard left value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000895 frame_->Drop();
ager@chromium.org357bf652010-04-12 11:30:10 +0000896 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 break;
ager@chromium.org357bf652010-04-12 11:30:10 +0000898 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899
900 default:
901 // Other cases should have been handled before this point.
902 UNREACHABLE();
903 break;
904 }
905}
906
907
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000908class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000909 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000910 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000911 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000912 bool reversed,
ager@chromium.org357bf652010-04-12 11:30:10 +0000913 OverwriteMode overwrite_mode,
914 Register tos)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000915 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000916 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000917 reversed_(reversed),
ager@chromium.org357bf652010-04-12 11:30:10 +0000918 overwrite_mode_(overwrite_mode),
919 tos_register_(tos) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000920 set_comment("[ DeferredInlinedSmiOperation");
921 }
922
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000923 virtual void Generate();
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000924 // This stub makes explicit calls to SaveRegisters(), RestoreRegisters() and
925 // Exit(). Currently on ARM SaveRegisters() and RestoreRegisters() are empty
926 // methods, it is the responsibility of the deferred code to save and restore
927 // registers.
928 virtual bool AutoSaveAndRestore() { return false; }
929
930 void JumpToNonSmiInput(Condition cond);
931 void JumpToAnswerOutOfRange(Condition cond);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000932
933 private:
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000934 void GenerateNonSmiInput();
935 void GenerateAnswerOutOfRange();
936 void WriteNonSmiAnswer(Register answer,
937 Register heap_number,
938 Register scratch);
939
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000940 Token::Value op_;
941 int value_;
942 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000943 OverwriteMode overwrite_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000944 Register tos_register_;
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000945 Label non_smi_input_;
946 Label answer_out_of_range_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000947};
948
949
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000950// For bit operations we try harder and handle the case where the input is not
951// a Smi but a 32bits integer without calling the generic stub.
952void DeferredInlineSmiOperation::JumpToNonSmiInput(Condition cond) {
953 ASSERT(Token::IsBitOp(op_));
954
955 __ b(cond, &non_smi_input_);
956}
957
958
959// For bit operations the result is always 32bits so we handle the case where
960// the result does not fit in a Smi without calling the generic stub.
961void DeferredInlineSmiOperation::JumpToAnswerOutOfRange(Condition cond) {
962 ASSERT(Token::IsBitOp(op_));
963
964 if ((op_ == Token::SHR) && !CpuFeatures::IsSupported(VFP3)) {
965 // >>> requires an unsigned to double conversion and the non VFP code
966 // does not support this conversion.
967 __ b(cond, entry_label());
968 } else {
969 __ b(cond, &answer_out_of_range_);
970 }
971}
972
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000973
974// On entry the non-constant side of the binary operation is in tos_register_
975// and the constant smi side is nowhere. The tos_register_ is not used by the
976// virtual frame. On exit the answer is in the tos_register_ and the virtual
977// frame is unchanged.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000978void DeferredInlineSmiOperation::Generate() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000979 VirtualFrame copied_frame(*frame_state()->frame());
980 copied_frame.SpillAll();
981
ager@chromium.org357bf652010-04-12 11:30:10 +0000982 Register lhs = r1;
983 Register rhs = r0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000984 switch (op_) {
985 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000986 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000987 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000988 __ sub(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000989 __ mov(r1, Operand(Smi::FromInt(value_)));
990 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000991 __ sub(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000992 __ mov(r0, Operand(Smi::FromInt(value_)));
993 }
994 break;
995 }
996
997 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000998 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000999 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001000 __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001001 __ mov(r1, Operand(Smi::FromInt(value_)));
1002 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00001003 __ add(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001004 __ mov(r0, Operand(Smi::FromInt(value_)));
1005 }
1006 break;
1007 }
1008
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001009 // For these operations there is no optimistic operation that needs to be
1010 // reverted.
1011 case Token::MUL:
1012 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001013 case Token::BIT_OR:
1014 case Token::BIT_XOR:
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001015 case Token::BIT_AND:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001016 case Token::SHL:
1017 case Token::SHR:
1018 case Token::SAR: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001019 if (tos_register_.is(r1)) {
1020 __ mov(r0, Operand(Smi::FromInt(value_)));
1021 } else {
1022 ASSERT(tos_register_.is(r0));
1023 __ mov(r1, Operand(Smi::FromInt(value_)));
1024 }
1025 if (reversed_ == tos_register_.is(r1)) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001026 lhs = r0;
1027 rhs = r1;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001028 }
1029 break;
1030 }
1031
1032 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +00001033 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001034 UNREACHABLE();
1035 break;
1036 }
1037
ager@chromium.org357bf652010-04-12 11:30:10 +00001038 GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001039 __ CallStub(&stub);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001040
ager@chromium.org357bf652010-04-12 11:30:10 +00001041 // The generic stub returns its value in r0, but that's not
1042 // necessarily what we want. We want whatever the inlined code
1043 // expected, which is that the answer is in the same register as
1044 // the operand was.
1045 __ Move(tos_register_, r0);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001046
1047 // The tos register was not in use for the virtual frame that we
1048 // came into this function with, so we can merge back to that frame
1049 // without trashing it.
1050 copied_frame.MergeTo(frame_state()->frame());
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001051
1052 Exit();
1053
1054 if (non_smi_input_.is_linked()) {
1055 GenerateNonSmiInput();
1056 }
1057
1058 if (answer_out_of_range_.is_linked()) {
1059 GenerateAnswerOutOfRange();
1060 }
1061}
1062
1063
1064// Convert and write the integer answer into heap_number.
1065void DeferredInlineSmiOperation::WriteNonSmiAnswer(Register answer,
1066 Register heap_number,
1067 Register scratch) {
1068 if (CpuFeatures::IsSupported(VFP3)) {
1069 CpuFeatures::Scope scope(VFP3);
1070 __ vmov(s0, answer);
1071 if (op_ == Token::SHR) {
1072 __ vcvt_f64_u32(d0, s0);
1073 } else {
1074 __ vcvt_f64_s32(d0, s0);
1075 }
1076 __ sub(scratch, heap_number, Operand(kHeapObjectTag));
1077 __ vstr(d0, scratch, HeapNumber::kValueOffset);
1078 } else {
1079 WriteInt32ToHeapNumberStub stub(answer, heap_number, scratch);
1080 __ CallStub(&stub);
1081 }
1082}
1083
1084
1085void DeferredInlineSmiOperation::GenerateNonSmiInput() {
1086 // We know the left hand side is not a Smi and the right hand side is an
1087 // immediate value (value_) which can be represented as a Smi. We only
1088 // handle bit operations.
1089 ASSERT(Token::IsBitOp(op_));
1090
1091 if (FLAG_debug_code) {
1092 __ Abort("Should not fall through!");
1093 }
1094
1095 __ bind(&non_smi_input_);
1096 if (FLAG_debug_code) {
1097 __ AbortIfSmi(tos_register_);
1098 }
1099
1100 // This routine uses the registers from r2 to r6. At the moment they are
1101 // not used by the register allocator, but when they are it should use
1102 // SpillAll and MergeTo like DeferredInlineSmiOperation::Generate() above.
1103
1104 Register heap_number_map = r7;
1105 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1106 __ ldr(r3, FieldMemOperand(tos_register_, HeapNumber::kMapOffset));
1107 __ cmp(r3, heap_number_map);
1108 // Not a number, fall back to the GenericBinaryOpStub.
1109 __ b(ne, entry_label());
1110
1111 Register int32 = r2;
1112 // Not a 32bits signed int, fall back to the GenericBinaryOpStub.
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001113 __ ConvertToInt32(tos_register_, int32, r4, r5, d0, entry_label());
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001114
1115 // tos_register_ (r0 or r1): Original heap number.
1116 // int32: signed 32bits int.
1117
1118 Label result_not_a_smi;
1119 int shift_value = value_ & 0x1f;
1120 switch (op_) {
1121 case Token::BIT_OR: __ orr(int32, int32, Operand(value_)); break;
1122 case Token::BIT_XOR: __ eor(int32, int32, Operand(value_)); break;
1123 case Token::BIT_AND: __ and_(int32, int32, Operand(value_)); break;
1124 case Token::SAR:
1125 ASSERT(!reversed_);
1126 if (shift_value != 0) {
1127 __ mov(int32, Operand(int32, ASR, shift_value));
1128 }
1129 break;
1130 case Token::SHR:
1131 ASSERT(!reversed_);
1132 if (shift_value != 0) {
1133 __ mov(int32, Operand(int32, LSR, shift_value), SetCC);
1134 } else {
1135 // SHR is special because it is required to produce a positive answer.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001136 __ cmp(int32, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001137 }
1138 if (CpuFeatures::IsSupported(VFP3)) {
1139 __ b(mi, &result_not_a_smi);
1140 } else {
1141 // Non VFP code cannot convert from unsigned to double, so fall back
1142 // to GenericBinaryOpStub.
1143 __ b(mi, entry_label());
1144 }
1145 break;
1146 case Token::SHL:
1147 ASSERT(!reversed_);
1148 if (shift_value != 0) {
1149 __ mov(int32, Operand(int32, LSL, shift_value));
1150 }
1151 break;
1152 default: UNREACHABLE();
1153 }
1154 // Check that the *signed* result fits in a smi. Not necessary for AND, SAR
1155 // if the shift if more than 0 or SHR if the shit is more than 1.
1156 if (!( (op_ == Token::AND) ||
1157 ((op_ == Token::SAR) && (shift_value > 0)) ||
1158 ((op_ == Token::SHR) && (shift_value > 1)))) {
1159 __ add(r3, int32, Operand(0x40000000), SetCC);
1160 __ b(mi, &result_not_a_smi);
1161 }
1162 __ mov(tos_register_, Operand(int32, LSL, kSmiTagSize));
1163 Exit();
1164
1165 if (result_not_a_smi.is_linked()) {
1166 __ bind(&result_not_a_smi);
1167 if (overwrite_mode_ != OVERWRITE_LEFT) {
1168 ASSERT((overwrite_mode_ == NO_OVERWRITE) ||
1169 (overwrite_mode_ == OVERWRITE_RIGHT));
1170 // If the allocation fails, fall back to the GenericBinaryOpStub.
1171 __ AllocateHeapNumber(r4, r5, r6, heap_number_map, entry_label());
1172 // Nothing can go wrong now, so overwrite tos.
1173 __ mov(tos_register_, Operand(r4));
1174 }
1175
1176 // int32: answer as signed 32bits integer.
1177 // tos_register_: Heap number to write the answer into.
1178 WriteNonSmiAnswer(int32, tos_register_, r3);
1179
1180 Exit();
1181 }
1182}
1183
1184
1185void DeferredInlineSmiOperation::GenerateAnswerOutOfRange() {
1186 // The input from a bitwise operation were Smis but the result cannot fit
lrn@chromium.org303ada72010-10-27 09:33:13 +00001187 // into a Smi, so we store it into a heap number. VirtualFrame::scratch0()
1188 // holds the untagged result to be converted. tos_register_ contains the
1189 // input. See the calls to JumpToAnswerOutOfRange to see how we got here.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001190 ASSERT(Token::IsBitOp(op_));
1191 ASSERT(!reversed_);
1192
lrn@chromium.org303ada72010-10-27 09:33:13 +00001193 Register untagged_result = VirtualFrame::scratch0();
1194
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001195 if (FLAG_debug_code) {
1196 __ Abort("Should not fall through!");
1197 }
1198
1199 __ bind(&answer_out_of_range_);
1200 if (((value_ & 0x1f) == 0) && (op_ == Token::SHR)) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001201 // >>> 0 is a special case where the untagged_result register is not set up
1202 // yet. We untag the input to get it.
1203 __ mov(untagged_result, Operand(tos_register_, ASR, kSmiTagSize));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001204 }
1205
1206 // This routine uses the registers from r2 to r6. At the moment they are
1207 // not used by the register allocator, but when they are it should use
1208 // SpillAll and MergeTo like DeferredInlineSmiOperation::Generate() above.
1209
1210 // Allocate the result heap number.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001211 Register heap_number_map = VirtualFrame::scratch1();
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001212 Register heap_number = r4;
1213 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1214 // If the allocation fails, fall back to the GenericBinaryOpStub.
1215 __ AllocateHeapNumber(heap_number, r5, r6, heap_number_map, entry_label());
lrn@chromium.org303ada72010-10-27 09:33:13 +00001216 WriteNonSmiAnswer(untagged_result, heap_number, r3);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001217 __ mov(tos_register_, Operand(heap_number));
1218
1219 Exit();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001220}
1221
1222
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001223static bool PopCountLessThanEqual2(unsigned int x) {
1224 x &= x - 1;
1225 return (x & (x - 1)) == 0;
1226}
1227
1228
1229// Returns the index of the lowest bit set.
1230static int BitPosition(unsigned x) {
1231 int bit_posn = 0;
1232 while ((x & 0xf) == 0) {
1233 bit_posn += 4;
1234 x >>= 4;
1235 }
1236 while ((x & 1) == 0) {
1237 bit_posn++;
1238 x >>= 1;
1239 }
1240 return bit_posn;
1241}
1242
1243
ricow@chromium.org65fae842010-08-25 15:26:24 +00001244// Can we multiply by x with max two shifts and an add.
1245// This answers yes to all integers from 2 to 10.
1246static bool IsEasyToMultiplyBy(int x) {
1247 if (x < 2) return false; // Avoid special cases.
1248 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
1249 if (IsPowerOf2(x)) return true; // Simple shift.
1250 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
1251 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
1252 return false;
1253}
1254
1255
1256// Can multiply by anything that IsEasyToMultiplyBy returns true for.
1257// Source and destination may be the same register. This routine does
1258// not set carry and overflow the way a mul instruction would.
1259static void InlineMultiplyByKnownInt(MacroAssembler* masm,
1260 Register source,
1261 Register destination,
1262 int known_int) {
1263 if (IsPowerOf2(known_int)) {
1264 masm->mov(destination, Operand(source, LSL, BitPosition(known_int)));
1265 } else if (PopCountLessThanEqual2(known_int)) {
1266 int first_bit = BitPosition(known_int);
1267 int second_bit = BitPosition(known_int ^ (1 << first_bit));
1268 masm->add(destination, source,
1269 Operand(source, LSL, second_bit - first_bit));
1270 if (first_bit != 0) {
1271 masm->mov(destination, Operand(destination, LSL, first_bit));
1272 }
1273 } else {
1274 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
1275 int the_bit = BitPosition(known_int + 1);
1276 masm->rsb(destination, source, Operand(source, LSL, the_bit));
1277 }
1278}
1279
1280
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001281void CodeGenerator::SmiOperation(Token::Value op,
1282 Handle<Object> value,
1283 bool reversed,
1284 OverwriteMode mode) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001285 int int_value = Smi::cast(*value)->value();
1286
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001287 bool both_sides_are_smi = frame_->KnownSmiAt(0);
1288
ager@chromium.org357bf652010-04-12 11:30:10 +00001289 bool something_to_inline;
1290 switch (op) {
1291 case Token::ADD:
1292 case Token::SUB:
1293 case Token::BIT_AND:
1294 case Token::BIT_OR:
1295 case Token::BIT_XOR: {
1296 something_to_inline = true;
1297 break;
1298 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001299 case Token::SHL: {
1300 something_to_inline = (both_sides_are_smi || !reversed);
1301 break;
1302 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001303 case Token::SHR:
1304 case Token::SAR: {
1305 if (reversed) {
1306 something_to_inline = false;
1307 } else {
1308 something_to_inline = true;
1309 }
1310 break;
1311 }
1312 case Token::MOD: {
1313 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1314 something_to_inline = false;
1315 } else {
1316 something_to_inline = true;
1317 }
1318 break;
1319 }
1320 case Token::MUL: {
1321 if (!IsEasyToMultiplyBy(int_value)) {
1322 something_to_inline = false;
1323 } else {
1324 something_to_inline = true;
1325 }
1326 break;
1327 }
1328 default: {
1329 something_to_inline = false;
1330 break;
1331 }
1332 }
1333
1334 if (!something_to_inline) {
1335 if (!reversed) {
1336 // Push the rhs onto the virtual frame by putting it in a TOS register.
1337 Register rhs = frame_->GetTOSRegister();
1338 __ mov(rhs, Operand(value));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001339 frame_->EmitPush(rhs, TypeInfo::Smi());
1340 GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, int_value);
ager@chromium.org357bf652010-04-12 11:30:10 +00001341 } else {
1342 // Pop the rhs, then push lhs and rhs in the right order. Only performs
1343 // at most one pop, the rest takes place in TOS registers.
1344 Register lhs = frame_->GetTOSRegister(); // Get reg for pushing.
1345 Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this.
1346 __ mov(lhs, Operand(value));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001347 frame_->EmitPush(lhs, TypeInfo::Smi());
1348 TypeInfo t = both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Unknown();
1349 frame_->EmitPush(rhs, t);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001350 GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI,
1351 GenericBinaryOpStub::kUnknownIntValue);
ager@chromium.org357bf652010-04-12 11:30:10 +00001352 }
1353 return;
1354 }
1355
1356 // We move the top of stack to a register (normally no move is invoved).
1357 Register tos = frame_->PopToRegister();
ager@chromium.org357bf652010-04-12 11:30:10 +00001358 switch (op) {
1359 case Token::ADD: {
1360 DeferredCode* deferred =
1361 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1362
1363 __ add(tos, tos, Operand(value), SetCC);
1364 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001365 if (!both_sides_are_smi) {
1366 __ tst(tos, Operand(kSmiTagMask));
1367 deferred->Branch(ne);
1368 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001369 deferred->BindExit();
1370 frame_->EmitPush(tos);
1371 break;
1372 }
1373
1374 case Token::SUB: {
1375 DeferredCode* deferred =
1376 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1377
1378 if (reversed) {
1379 __ rsb(tos, tos, Operand(value), SetCC);
1380 } else {
1381 __ sub(tos, tos, Operand(value), SetCC);
1382 }
1383 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001384 if (!both_sides_are_smi) {
1385 __ tst(tos, Operand(kSmiTagMask));
1386 deferred->Branch(ne);
1387 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001388 deferred->BindExit();
1389 frame_->EmitPush(tos);
1390 break;
1391 }
1392
1393
1394 case Token::BIT_OR:
1395 case Token::BIT_XOR:
1396 case Token::BIT_AND: {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001397 if (both_sides_are_smi) {
1398 switch (op) {
1399 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1400 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001401 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001402 default: UNREACHABLE();
1403 }
1404 frame_->EmitPush(tos, TypeInfo::Smi());
1405 } else {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001406 DeferredInlineSmiOperation* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001407 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1408 __ tst(tos, Operand(kSmiTagMask));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001409 deferred->JumpToNonSmiInput(ne);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001410 switch (op) {
1411 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1412 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001413 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001414 default: UNREACHABLE();
1415 }
1416 deferred->BindExit();
1417 TypeInfo result_type =
1418 (op == Token::BIT_AND) ? TypeInfo::Smi() : TypeInfo::Integer32();
1419 frame_->EmitPush(tos, result_type);
ager@chromium.org357bf652010-04-12 11:30:10 +00001420 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001421 break;
1422 }
1423
1424 case Token::SHL:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001425 if (reversed) {
1426 ASSERT(both_sides_are_smi);
1427 int max_shift = 0;
1428 int max_result = int_value == 0 ? 1 : int_value;
1429 while (Smi::IsValid(max_result << 1)) {
1430 max_shift++;
1431 max_result <<= 1;
1432 }
1433 DeferredCode* deferred =
1434 new DeferredInlineSmiOperation(op, int_value, true, mode, tos);
1435 // Mask off the last 5 bits of the shift operand (rhs). This is part
1436 // of the definition of shift in JS and we know we have a Smi so we
1437 // can safely do this. The masked version gets passed to the
1438 // deferred code, but that makes no difference.
1439 __ and_(tos, tos, Operand(Smi::FromInt(0x1f)));
1440 __ cmp(tos, Operand(Smi::FromInt(max_shift)));
1441 deferred->Branch(ge);
1442 Register scratch = VirtualFrame::scratch0();
1443 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Untag.
1444 __ mov(tos, Operand(Smi::FromInt(int_value))); // Load constant.
1445 __ mov(tos, Operand(tos, LSL, scratch)); // Shift constant.
1446 deferred->BindExit();
1447 TypeInfo result = TypeInfo::Integer32();
1448 frame_->EmitPush(tos, result);
1449 break;
1450 }
1451 // Fall through!
ager@chromium.org357bf652010-04-12 11:30:10 +00001452 case Token::SHR:
1453 case Token::SAR: {
1454 ASSERT(!reversed);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001455 int shift_value = int_value & 0x1f;
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001456 TypeInfo result = TypeInfo::Number();
1457
1458 if (op == Token::SHR) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001459 if (shift_value > 1) {
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001460 result = TypeInfo::Smi();
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001461 } else if (shift_value > 0) {
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001462 result = TypeInfo::Integer32();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001463 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001464 } else if (op == Token::SAR) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001465 if (shift_value > 0) {
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001466 result = TypeInfo::Smi();
1467 } else {
1468 result = TypeInfo::Integer32();
1469 }
1470 } else {
1471 ASSERT(op == Token::SHL);
1472 result = TypeInfo::Integer32();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001473 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001474
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001475 DeferredInlineSmiOperation* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001476 new DeferredInlineSmiOperation(op, shift_value, false, mode, tos);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001477 if (!both_sides_are_smi) {
1478 __ tst(tos, Operand(kSmiTagMask));
1479 deferred->JumpToNonSmiInput(ne);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001480 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001481 switch (op) {
1482 case Token::SHL: {
1483 if (shift_value != 0) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001484 Register untagged_result = VirtualFrame::scratch0();
1485 Register scratch = VirtualFrame::scratch1();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001486 int adjusted_shift = shift_value - kSmiTagSize;
1487 ASSERT(adjusted_shift >= 0);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001488
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001489 if (adjusted_shift != 0) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001490 __ mov(untagged_result, Operand(tos, LSL, adjusted_shift));
1491 } else {
1492 __ mov(untagged_result, Operand(tos));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001493 }
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001494 // Check that the *signed* result fits in a smi.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001495 __ add(scratch, untagged_result, Operand(0x40000000), SetCC);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001496 deferred->JumpToAnswerOutOfRange(mi);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001497 __ mov(tos, Operand(untagged_result, LSL, kSmiTagSize));
ager@chromium.org357bf652010-04-12 11:30:10 +00001498 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001499 break;
1500 }
1501 case Token::SHR: {
ager@chromium.org357bf652010-04-12 11:30:10 +00001502 if (shift_value != 0) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001503 Register untagged_result = VirtualFrame::scratch0();
1504 // Remove tag.
1505 __ mov(untagged_result, Operand(tos, ASR, kSmiTagSize));
1506 __ mov(untagged_result, Operand(untagged_result, LSR, shift_value));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001507 if (shift_value == 1) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001508 // Check that the *unsigned* result fits in a smi.
1509 // Neither of the two high-order bits can be set:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001510 // - 0x80000000: high bit would be lost when smi tagging
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001511 // - 0x40000000: this number would convert to negative when Smi
1512 // tagging.
1513 // These two cases can only happen with shifts by 0 or 1 when
1514 // handed a valid smi.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001515 __ tst(untagged_result, Operand(0xc0000000));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001516 deferred->JumpToAnswerOutOfRange(ne);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001517 }
lrn@chromium.org303ada72010-10-27 09:33:13 +00001518 __ mov(tos, Operand(untagged_result, LSL, kSmiTagSize));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001519 } else {
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001520 __ cmp(tos, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001521 deferred->JumpToAnswerOutOfRange(mi);
ager@chromium.org357bf652010-04-12 11:30:10 +00001522 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001523 break;
1524 }
1525 case Token::SAR: {
1526 if (shift_value != 0) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001527 // Do the shift and the tag removal in one operation. If the shift
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001528 // is 31 bits (the highest possible value) then we emit the
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001529 // instruction as a shift by 0 which in the ARM ISA means shift
1530 // arithmetically by 32.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001531 __ mov(tos, Operand(tos, ASR, (kSmiTagSize + shift_value) & 0x1f));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001532 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
ager@chromium.org357bf652010-04-12 11:30:10 +00001533 }
1534 break;
1535 }
1536 default: UNREACHABLE();
1537 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001538 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001539 frame_->EmitPush(tos, result);
ager@chromium.org357bf652010-04-12 11:30:10 +00001540 break;
1541 }
1542
1543 case Token::MOD: {
1544 ASSERT(!reversed);
1545 ASSERT(int_value >= 2);
1546 ASSERT(IsPowerOf2(int_value));
1547 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001548 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001549 unsigned mask = (0x80000000u | kSmiTagMask);
1550 __ tst(tos, Operand(mask));
1551 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1552 mask = (int_value << kSmiTagSize) - 1;
1553 __ and_(tos, tos, Operand(mask));
1554 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001555 // Mod of positive power of 2 Smi gives a Smi if the lhs is an integer.
1556 frame_->EmitPush(
1557 tos,
1558 both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Number());
ager@chromium.org357bf652010-04-12 11:30:10 +00001559 break;
1560 }
1561
1562 case Token::MUL: {
1563 ASSERT(IsEasyToMultiplyBy(int_value));
1564 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001565 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001566 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1567 max_smi_that_wont_overflow <<= kSmiTagSize;
1568 unsigned mask = 0x80000000u;
1569 while ((mask & max_smi_that_wont_overflow) == 0) {
1570 mask |= mask >> 1;
1571 }
1572 mask |= kSmiTagMask;
1573 // This does a single mask that checks for a too high value in a
1574 // conservative way and for a non-Smi. It also filters out negative
1575 // numbers, unfortunately, but since this code is inline we prefer
1576 // brevity to comprehensiveness.
1577 __ tst(tos, Operand(mask));
1578 deferred->Branch(ne);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001579 InlineMultiplyByKnownInt(masm_, tos, tos, int_value);
ager@chromium.org357bf652010-04-12 11:30:10 +00001580 deferred->BindExit();
1581 frame_->EmitPush(tos);
1582 break;
1583 }
1584
1585 default:
1586 UNREACHABLE();
1587 break;
1588 }
1589}
1590
1591
ager@chromium.org378b34e2011-01-28 08:04:38 +00001592void CodeGenerator::Comparison(Condition cond,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001593 Expression* left,
1594 Expression* right,
1595 bool strict) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001596 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001597
ager@chromium.org357bf652010-04-12 11:30:10 +00001598 if (left != NULL) Load(left);
1599 if (right != NULL) Load(right);
1600
mads.s.ager31e71382008-08-13 09:32:07 +00001601 // sp[0] : y
1602 // sp[1] : x
1603 // result : cc register
1604
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605 // Strict only makes sense for equality comparisons.
ager@chromium.org378b34e2011-01-28 08:04:38 +00001606 ASSERT(!strict || cond == eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607
ager@chromium.org357bf652010-04-12 11:30:10 +00001608 Register lhs;
1609 Register rhs;
1610
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001611 bool lhs_is_smi;
1612 bool rhs_is_smi;
1613
ager@chromium.org357bf652010-04-12 11:30:10 +00001614 // We load the top two stack positions into registers chosen by the virtual
1615 // frame. This should keep the register shuffling to a minimum.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001616 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
ager@chromium.org378b34e2011-01-28 08:04:38 +00001617 if (cond == gt || cond == le) {
1618 cond = ReverseCondition(cond);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001619 lhs_is_smi = frame_->KnownSmiAt(0);
1620 rhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001621 lhs = frame_->PopToRegister();
1622 rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001623 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001624 rhs_is_smi = frame_->KnownSmiAt(0);
1625 lhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001626 rhs = frame_->PopToRegister();
1627 lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001628 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001629
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001630 bool both_sides_are_smi = (lhs_is_smi && rhs_is_smi);
1631
ager@chromium.org357bf652010-04-12 11:30:10 +00001632 ASSERT(rhs.is(r0) || rhs.is(r1));
1633 ASSERT(lhs.is(r0) || lhs.is(r1));
1634
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001635 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001637 if (!both_sides_are_smi) {
1638 // Now we have the two sides in r0 and r1. We flush any other registers
1639 // because the stub doesn't know about register allocation.
1640 frame_->SpillAll();
1641 Register scratch = VirtualFrame::scratch0();
1642 Register smi_test_reg;
1643 if (lhs_is_smi) {
1644 smi_test_reg = rhs;
1645 } else if (rhs_is_smi) {
1646 smi_test_reg = lhs;
1647 } else {
1648 __ orr(scratch, lhs, Operand(rhs));
1649 smi_test_reg = scratch;
1650 }
1651 __ tst(smi_test_reg, Operand(kSmiTagMask));
1652 JumpTarget smi;
1653 smi.Branch(eq);
1654
1655 // Perform non-smi comparison by stub.
1656 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1657 // We call with 0 args because there are 0 on the stack.
ager@chromium.org378b34e2011-01-28 08:04:38 +00001658 CompareStub stub(cond, strict, NO_SMI_COMPARE_IN_STUB, lhs, rhs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001659 frame_->CallStub(&stub, 0);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001660 __ cmp(r0, Operand(0, RelocInfo::NONE));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001661 exit.Jump();
1662
1663 smi.Bind();
ager@chromium.org357bf652010-04-12 11:30:10 +00001664 }
1665
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001666 // Do smi comparisons by pointer comparison.
ager@chromium.org357bf652010-04-12 11:30:10 +00001667 __ cmp(lhs, Operand(rhs));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001668
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001669 exit.Bind();
ager@chromium.org378b34e2011-01-28 08:04:38 +00001670 cc_reg_ = cond;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671}
1672
1673
mads.s.ager31e71382008-08-13 09:32:07 +00001674// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001675void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001676 CallFunctionFlags flags,
1677 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001678 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001679 int arg_count = args->length();
1680 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001681 Load(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001682 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683
kasper.lund7276f142008-07-30 08:49:36 +00001684 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001685 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686
kasper.lund7276f142008-07-30 08:49:36 +00001687 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001688 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001689 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001690 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691
1692 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001693 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001694 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001695}
1696
1697
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001698void CodeGenerator::CallApplyLazy(Expression* applicand,
1699 Expression* receiver,
1700 VariableProxy* arguments,
1701 int position) {
1702 // An optimized implementation of expressions of the form
1703 // x.apply(y, arguments).
1704 // If the arguments object of the scope has not been allocated,
1705 // and x.apply is Function.prototype.apply, this optimization
1706 // just copies y and the arguments of the current function on the
1707 // stack, as receiver and arguments, and calls x.
1708 // In the implementation comments, we call x the applicand
1709 // and y the receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001710
1711 ASSERT(ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION);
1712 ASSERT(arguments->IsArguments());
1713
1714 // Load applicand.apply onto the stack. This will usually
1715 // give us a megamorphic load site. Not super, but it works.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001716 Load(applicand);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001717 Handle<String> name = Factory::LookupAsciiSymbol("apply");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001718 frame_->Dup();
ager@chromium.orgac091b72010-05-05 07:34:42 +00001719 frame_->CallLoadIC(name, RelocInfo::CODE_TARGET);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001720 frame_->EmitPush(r0);
1721
1722 // Load the receiver and the existing arguments object onto the
1723 // expression stack. Avoid allocating the arguments object here.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001724 Load(receiver);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001725 LoadFromSlot(scope()->arguments()->AsSlot(), NOT_INSIDE_TYPEOF);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001726
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001727 // At this point the top two stack elements are probably in registers
1728 // since they were just loaded. Ensure they are in regs and get the
1729 // regs.
1730 Register receiver_reg = frame_->Peek2();
1731 Register arguments_reg = frame_->Peek();
1732
1733 // From now on the frame is spilled.
1734 frame_->SpillAll();
1735
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001736 // Emit the source position information after having loaded the
1737 // receiver and the arguments.
1738 CodeForSourcePosition(position);
1739 // Contents of the stack at this point:
1740 // sp[0]: arguments object of the current function or the hole.
1741 // sp[1]: receiver
1742 // sp[2]: applicand.apply
1743 // sp[3]: applicand.
1744
1745 // Check if the arguments object has been lazily allocated
1746 // already. If so, just use that instead of copying the arguments
1747 // from the stack. This also deals with cases where a local variable
1748 // named 'arguments' has been introduced.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001749 JumpTarget slow;
1750 Label done;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001751 __ LoadRoot(ip, Heap::kArgumentsMarkerRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001752 __ cmp(ip, arguments_reg);
1753 slow.Branch(ne);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001754
1755 Label build_args;
1756 // Get rid of the arguments object probe.
1757 frame_->Drop();
1758 // Stack now has 3 elements on it.
1759 // Contents of stack at this point:
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001760 // sp[0]: receiver - in the receiver_reg register.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001761 // sp[1]: applicand.apply
1762 // sp[2]: applicand.
1763
1764 // Check that the receiver really is a JavaScript object.
ager@chromium.org378b34e2011-01-28 08:04:38 +00001765 __ JumpIfSmi(receiver_reg, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001766 // We allow all JSObjects including JSFunctions. As long as
1767 // JS_FUNCTION_TYPE is the last instance type and it is right
1768 // after LAST_JS_OBJECT_TYPE, we do not have to check the upper
1769 // bound.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00001770 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1771 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001772 __ CompareObjectType(receiver_reg, r2, r3, FIRST_JS_OBJECT_TYPE);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001773 __ b(lt, &build_args);
1774
1775 // Check that applicand.apply is Function.prototype.apply.
1776 __ ldr(r0, MemOperand(sp, kPointerSize));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001777 __ JumpIfSmi(r0, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001778 __ CompareObjectType(r0, r1, r2, JS_FUNCTION_TYPE);
1779 __ b(ne, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001780 Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply));
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001781 __ ldr(r1, FieldMemOperand(r0, JSFunction::kCodeEntryOffset));
1782 __ sub(r1, r1, Operand(Code::kHeaderSize - kHeapObjectTag));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001783 __ cmp(r1, Operand(apply_code));
1784 __ b(ne, &build_args);
1785
1786 // Check that applicand is a function.
1787 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001788 __ JumpIfSmi(r1, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001789 __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE);
1790 __ b(ne, &build_args);
1791
1792 // Copy the arguments to this function possibly from the
1793 // adaptor frame below it.
1794 Label invoke, adapted;
1795 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1796 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1797 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1798 __ b(eq, &adapted);
1799
1800 // No arguments adaptor frame. Copy fixed number of arguments.
1801 __ mov(r0, Operand(scope()->num_parameters()));
1802 for (int i = 0; i < scope()->num_parameters(); i++) {
1803 __ ldr(r2, frame_->ParameterAt(i));
1804 __ push(r2);
1805 }
1806 __ jmp(&invoke);
1807
1808 // Arguments adaptor frame present. Copy arguments from there, but
1809 // avoid copying too many arguments to avoid stack overflows.
1810 __ bind(&adapted);
1811 static const uint32_t kArgumentsLimit = 1 * KB;
1812 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1813 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
1814 __ mov(r3, r0);
1815 __ cmp(r0, Operand(kArgumentsLimit));
1816 __ b(gt, &build_args);
1817
1818 // Loop through the arguments pushing them onto the execution
1819 // stack. We don't inform the virtual frame of the push, so we don't
1820 // have to worry about getting rid of the elements from the virtual
1821 // frame.
1822 Label loop;
1823 // r3 is a small non-negative integer, due to the test above.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001824 __ cmp(r3, Operand(0, RelocInfo::NONE));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001825 __ b(eq, &invoke);
1826 // Compute the address of the first argument.
1827 __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2));
1828 __ add(r2, r2, Operand(kPointerSize));
1829 __ bind(&loop);
1830 // Post-decrement argument address by kPointerSize on each iteration.
1831 __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex));
1832 __ push(r4);
1833 __ sub(r3, r3, Operand(1), SetCC);
1834 __ b(gt, &loop);
1835
1836 // Invoke the function.
1837 __ bind(&invoke);
1838 ParameterCount actual(r0);
1839 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1840 // Drop applicand.apply and applicand from the stack, and push
1841 // the result of the function call, but leave the spilled frame
1842 // unchanged, with 3 elements, so it is correct when we compile the
1843 // slow-case code.
1844 __ add(sp, sp, Operand(2 * kPointerSize));
1845 __ push(r0);
1846 // Stack now has 1 element:
1847 // sp[0]: result
1848 __ jmp(&done);
1849
1850 // Slow-case: Allocate the arguments object since we know it isn't
1851 // there, and fall-through to the slow-case where we call
1852 // applicand.apply.
1853 __ bind(&build_args);
1854 // Stack now has 3 elements, because we have jumped from where:
1855 // sp[0]: receiver
1856 // sp[1]: applicand.apply
1857 // sp[2]: applicand.
1858 StoreArgumentsObject(false);
1859
1860 // Stack and frame now have 4 elements.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001861 slow.Bind();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001862
1863 // Generic computation of x.apply(y, args) with no special optimization.
1864 // Flip applicand.apply and applicand on the stack, so
1865 // applicand looks like the receiver of the applicand.apply call.
1866 // Then process it as a normal function call.
1867 __ ldr(r0, MemOperand(sp, 3 * kPointerSize));
1868 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001869 __ Strd(r0, r1, MemOperand(sp, 2 * kPointerSize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001870
1871 CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS);
1872 frame_->CallStub(&call_function, 3);
1873 // The function and its two arguments have been dropped.
1874 frame_->Drop(); // Drop the receiver as well.
1875 frame_->EmitPush(r0);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001876 frame_->SpillAll(); // A spilled frame is also jumping to label done.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001877 // Stack now has 1 element:
1878 // sp[0]: result
1879 __ bind(&done);
1880
1881 // Restore the context register after a call.
1882 __ ldr(cp, frame_->Context());
1883}
1884
1885
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001886void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887 ASSERT(has_cc());
ager@chromium.org378b34e2011-01-28 08:04:38 +00001888 Condition cond = if_true ? cc_reg_ : NegateCondition(cc_reg_);
1889 target->Branch(cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890 cc_reg_ = al;
1891}
1892
1893
ager@chromium.org7c537e22008-10-16 08:43:32 +00001894void CodeGenerator::CheckStack() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001895 frame_->SpillAll();
ager@chromium.org3811b432009-10-28 14:53:37 +00001896 Comment cmnt(masm_, "[ check stack");
1897 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
ager@chromium.org3811b432009-10-28 14:53:37 +00001898 masm_->cmp(sp, Operand(ip));
1899 StackCheckStub stub;
1900 // Call the stub if lower.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001901 masm_->mov(ip,
ager@chromium.org3811b432009-10-28 14:53:37 +00001902 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1903 RelocInfo::CODE_TARGET),
1904 LeaveCC,
1905 lo);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001906 masm_->Call(ip, lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907}
1908
1909
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001910void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1911#ifdef DEBUG
1912 int original_height = frame_->height();
1913#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001914 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001915 Visit(statements->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001916 }
1917 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1918}
1919
1920
ager@chromium.org7c537e22008-10-16 08:43:32 +00001921void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001922#ifdef DEBUG
1923 int original_height = frame_->height();
1924#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001925 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001926 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001927 node->break_target()->SetExpectedHeight();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001928 VisitStatements(node->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001929 if (node->break_target()->is_linked()) {
1930 node->break_target()->Bind();
1931 }
1932 node->break_target()->Unuse();
1933 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934}
1935
1936
ager@chromium.org7c537e22008-10-16 08:43:32 +00001937void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org3811b432009-10-28 14:53:37 +00001938 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001939 frame_->EmitPush(Operand(pairs));
1940 frame_->EmitPush(Operand(Smi::FromInt(is_eval() ? 1 : 0)));
1941
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001942 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001943 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944}
1945
1946
ager@chromium.org7c537e22008-10-16 08:43:32 +00001947void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001948#ifdef DEBUG
1949 int original_height = frame_->height();
1950#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001951 Comment cmnt(masm_, "[ Declaration");
1952 Variable* var = node->proxy()->var();
1953 ASSERT(var != NULL); // must have been resolved
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001954 Slot* slot = var->AsSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955
1956 // If it was not possible to allocate the variable at compile time,
1957 // we need to "declare" it at runtime to make sure it actually
1958 // exists in the local context.
1959 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1960 // Variables with a "LOOKUP" slot were introduced as non-locals
1961 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001962 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001964 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001965 frame_->EmitPush(Operand(var->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966 // Declaration nodes are always declared in only two modes.
1967 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1968 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001969 frame_->EmitPush(Operand(Smi::FromInt(attr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970 // Push initial value, if any.
1971 // Note: For variables we must not push an initial value (such as
1972 // 'undefined') because we may have a (legal) redeclaration and we
1973 // must not destroy the current value.
1974 if (node->mode() == Variable::CONST) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001975 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001976 } else if (node->fun() != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001977 Load(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978 } else {
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001979 frame_->EmitPush(Operand(0, RelocInfo::NONE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00001981
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001982 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001983 // Ignore the return value (declarations are statements).
ager@chromium.orgac091b72010-05-05 07:34:42 +00001984
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001985 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001986 return;
1987 }
1988
1989 ASSERT(!var->is_global());
1990
1991 // If we have a function or a constant, we need to initialize the variable.
1992 Expression* val = NULL;
1993 if (node->mode() == Variable::CONST) {
1994 val = new Literal(Factory::the_hole_value());
1995 } else {
1996 val = node->fun(); // NULL if we don't have a function
1997 }
1998
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001999
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 if (val != NULL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002001 WriteBarrierCharacter wb_info =
2002 val->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
2003 if (val->AsLiteral() != NULL) wb_info = NEVER_NEWSPACE;
ager@chromium.orgac091b72010-05-05 07:34:42 +00002004 // Set initial value.
2005 Reference target(this, node->proxy());
2006 Load(val);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002007 target.SetValue(NOT_CONST_INIT, wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002008
iposva@chromium.org245aa852009-02-10 00:49:54 +00002009 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002010 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002012 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013}
2014
2015
ager@chromium.org7c537e22008-10-16 08:43:32 +00002016void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002017#ifdef DEBUG
2018 int original_height = frame_->height();
2019#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002021 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 Expression* expression = node->expression();
2023 expression->MarkAsStatement();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002024 Load(expression);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002025 frame_->Drop();
2026 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027}
2028
2029
ager@chromium.org7c537e22008-10-16 08:43:32 +00002030void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002031#ifdef DEBUG
2032 int original_height = frame_->height();
2033#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002035 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002036 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002037 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038}
2039
2040
ager@chromium.org7c537e22008-10-16 08:43:32 +00002041void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002042#ifdef DEBUG
2043 int original_height = frame_->height();
2044#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002046 // Generate different code depending on which parts of the if statement
2047 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 bool has_then_stm = node->HasThenStatement();
2049 bool has_else_stm = node->HasElseStatement();
2050
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002051 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002053 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00002055 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002056 JumpTarget then;
2057 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002059 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002060 if (frame_ != NULL) {
2061 Branch(false, &else_);
2062 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002063 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002064 if (frame_ != NULL || then.is_linked()) {
2065 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002066 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002067 }
2068 if (frame_ != NULL) {
2069 exit.Jump();
2070 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002071 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002072 if (else_.is_linked()) {
2073 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002074 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002075 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076
2077 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00002078 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002080 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002082 LoadCondition(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002083 if (frame_ != NULL) {
2084 Branch(false, &exit);
2085 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002087 if (frame_ != NULL || then.is_linked()) {
2088 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002089 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002090 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091
2092 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00002093 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002095 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096 // if (!cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002097 LoadCondition(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002098 if (frame_ != NULL) {
2099 Branch(true, &exit);
2100 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002102 if (frame_ != NULL || else_.is_linked()) {
2103 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002104 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002105 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106
2107 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002108 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 ASSERT(!has_then_stm && !has_else_stm);
2110 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002111 LoadCondition(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002112 if (frame_ != NULL) {
2113 if (has_cc()) {
2114 cc_reg_ = al;
2115 } else {
2116 frame_->Drop();
2117 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118 }
2119 }
2120
2121 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002122 if (exit.is_linked()) {
2123 exit.Bind();
2124 }
2125 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126}
2127
2128
ager@chromium.org7c537e22008-10-16 08:43:32 +00002129void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002131 CodeForStatementPosition(node);
2132 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133}
2134
2135
ager@chromium.org7c537e22008-10-16 08:43:32 +00002136void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002138 CodeForStatementPosition(node);
2139 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140}
2141
2142
ager@chromium.org7c537e22008-10-16 08:43:32 +00002143void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002144 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00002145
ager@chromium.org4af710e2009-09-15 12:20:11 +00002146 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002147 Load(node->expression());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002148 frame_->PopToR0();
2149 frame_->PrepareForReturn();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002150 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002151 function_return_.Jump();
2152 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002153 // Pop the result from the frame and prepare the frame for
2154 // returning thus making it easier to merge.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002155 if (function_return_.is_bound()) {
2156 // If the function return label is already bound we reuse the
2157 // code by jumping to the return site.
2158 function_return_.Jump();
2159 } else {
2160 function_return_.Bind();
2161 GenerateReturnSequence();
2162 }
2163 }
2164}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002165
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002166
2167void CodeGenerator::GenerateReturnSequence() {
2168 if (FLAG_trace) {
2169 // Push the return value on the stack as the parameter.
2170 // Runtime::TraceExit returns the parameter as it is.
2171 frame_->EmitPush(r0);
2172 frame_->CallRuntime(Runtime::kTraceExit, 1);
2173 }
2174
2175#ifdef DEBUG
2176 // Add a label for checking the size of the code used for returning.
2177 Label check_exit_codesize;
2178 masm_->bind(&check_exit_codesize);
2179#endif
2180 // Make sure that the constant pool is not emitted inside of the return
2181 // sequence.
2182 { Assembler::BlockConstPoolScope block_const_pool(masm_);
2183 // Tear down the frame which will restore the caller's frame pointer and
2184 // the link register.
2185 frame_->Exit();
2186
2187 // Here we use masm_-> instead of the __ macro to avoid the code coverage
2188 // tool from instrumenting as we rely on the code size here.
2189 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
2190 masm_->add(sp, sp, Operand(sp_delta));
2191 masm_->Jump(lr);
2192 DeleteFrame();
2193
2194#ifdef DEBUG
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00002195 // Check that the size of the code used for returning is large enough
2196 // for the debugger's requirements.
2197 ASSERT(Assembler::kJSReturnSequenceInstructions <=
2198 masm_->InstructionsGeneratedSince(&check_exit_codesize));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002199#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002200 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201}
2202
2203
ager@chromium.org7c537e22008-10-16 08:43:32 +00002204void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002205#ifdef DEBUG
2206 int original_height = frame_->height();
2207#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002209 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002210 Load(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002211 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002212 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002213 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002214 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002215 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002216#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002217 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002218 __ cmp(r0, cp);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002219 verified_true.Branch(eq);
2220 __ stop("PushContext: r0 is expected to be the same as cp");
2221 verified_true.Bind();
2222#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002224 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002225 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226}
2227
2228
ager@chromium.org7c537e22008-10-16 08:43:32 +00002229void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002230#ifdef DEBUG
2231 int original_height = frame_->height();
2232#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002234 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 // Pop context.
2236 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
2237 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002238 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002239 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002240}
2241
2242
ager@chromium.org7c537e22008-10-16 08:43:32 +00002243void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002244#ifdef DEBUG
2245 int original_height = frame_->height();
2246#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002248 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002249 node->break_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002251 Load(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002252
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002253 JumpTarget next_test;
2254 JumpTarget fall_through;
2255 JumpTarget default_entry;
2256 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257 ZoneList<CaseClause*>* cases = node->cases();
2258 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002259 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260
2261 for (int i = 0; i < length; i++) {
2262 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002264 // Remember the default clause and compile it at the end.
2265 default_clause = clause;
2266 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 }
2268
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002269 Comment cmnt(masm_, "[ Case clause");
2270 // Compile the test.
2271 next_test.Bind();
2272 next_test.Unuse();
2273 // Duplicate TOS.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002274 frame_->Dup();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002275 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002276 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002277
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002278 // Before entering the body from the test, remove the switch value from
2279 // the stack.
2280 frame_->Drop();
2281
2282 // Label the body so that fall through is enabled.
2283 if (i > 0 && cases->at(i - 1)->is_default()) {
2284 default_exit.Bind();
2285 } else {
2286 fall_through.Bind();
2287 fall_through.Unuse();
2288 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002289 VisitStatements(clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002290
2291 // If control flow can fall through from the body, jump to the next body
2292 // or the end of the statement.
2293 if (frame_ != NULL) {
2294 if (i < length - 1 && cases->at(i + 1)->is_default()) {
2295 default_entry.Jump();
2296 } else {
2297 fall_through.Jump();
2298 }
2299 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 }
2301
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002302 // The final "test" removes the switch value.
2303 next_test.Bind();
2304 frame_->Drop();
2305
2306 // If there is a default clause, compile it.
2307 if (default_clause != NULL) {
2308 Comment cmnt(masm_, "[ Default clause");
2309 default_entry.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002310 VisitStatements(default_clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002311 // If control flow can fall out of the default and there is a case after
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002312 // it, jump to that case's body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002313 if (frame_ != NULL && default_exit.is_bound()) {
2314 default_exit.Jump();
2315 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002316 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002317
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002318 if (fall_through.is_linked()) {
2319 fall_through.Bind();
2320 }
2321
2322 if (node->break_target()->is_linked()) {
2323 node->break_target()->Bind();
2324 }
2325 node->break_target()->Unuse();
2326 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327}
2328
2329
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002330void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002331#ifdef DEBUG
2332 int original_height = frame_->height();
2333#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002334 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002335 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002336 node->break_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002337 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002338 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002339
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002340 // Label the top of the loop for the backward CFG edge. If the test
2341 // is always true we can use the continue target, and if the test is
2342 // always false there is no need.
2343 ConditionAnalysis info = AnalyzeCondition(node->cond());
2344 switch (info) {
2345 case ALWAYS_TRUE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002346 node->continue_target()->SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002347 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002348 break;
2349 case ALWAYS_FALSE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002350 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002351 break;
2352 case DONT_KNOW:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002353 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002354 body.Bind();
2355 break;
2356 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002357
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002358 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002359 Visit(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002360
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002361 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002362 switch (info) {
2363 case ALWAYS_TRUE:
2364 // If control can fall off the end of the body, jump back to the
2365 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002366 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002367 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002368 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002370 case ALWAYS_FALSE:
2371 // If we have a continue in the body, we only have to bind its
2372 // jump target.
2373 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002374 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002375 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002376 break;
2377 case DONT_KNOW:
2378 // We have to compile the test expression if it can be reached by
2379 // control flow falling out of the body or via continue.
2380 if (node->continue_target()->is_linked()) {
2381 node->continue_target()->Bind();
2382 }
2383 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002384 Comment cmnt(masm_, "[ DoWhileCondition");
2385 CodeForDoWhileConditionPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002386 LoadCondition(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002387 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002388 // A invalid frame here indicates that control did not
2389 // fall out of the test expression.
2390 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002391 }
2392 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002393 break;
2394 }
2395
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002396 if (node->break_target()->is_linked()) {
2397 node->break_target()->Bind();
2398 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002399 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002400 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2401}
2402
2403
2404void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
2405#ifdef DEBUG
2406 int original_height = frame_->height();
2407#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002408 Comment cmnt(masm_, "[ WhileStatement");
2409 CodeForStatementPosition(node);
2410
2411 // If the test is never true and has no side effects there is no need
2412 // to compile the test or body.
2413 ConditionAnalysis info = AnalyzeCondition(node->cond());
2414 if (info == ALWAYS_FALSE) return;
2415
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002416 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002417 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002418
2419 // Label the top of the loop with the continue target for the backward
2420 // CFG edge.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002421 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002422 node->continue_target()->Bind();
2423
2424 if (info == DONT_KNOW) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002425 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002426 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002427 if (has_valid_frame()) {
2428 // A NULL frame indicates that control did not fall out of the
2429 // test expression.
2430 Branch(false, node->break_target());
2431 }
2432 if (has_valid_frame() || body.is_linked()) {
2433 body.Bind();
2434 }
2435 }
2436
2437 if (has_valid_frame()) {
2438 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002439 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002440
2441 // If control flow can fall out of the body, jump back to the top.
2442 if (has_valid_frame()) {
2443 node->continue_target()->Jump();
2444 }
2445 }
2446 if (node->break_target()->is_linked()) {
2447 node->break_target()->Bind();
2448 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002449 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002450 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2451}
2452
2453
2454void CodeGenerator::VisitForStatement(ForStatement* node) {
2455#ifdef DEBUG
2456 int original_height = frame_->height();
2457#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002458 Comment cmnt(masm_, "[ ForStatement");
2459 CodeForStatementPosition(node);
2460 if (node->init() != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002461 Visit(node->init());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002462 }
2463
2464 // If the test is never true there is no need to compile the test or
2465 // body.
2466 ConditionAnalysis info = AnalyzeCondition(node->cond());
2467 if (info == ALWAYS_FALSE) return;
2468
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002469 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002470 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002471
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002472 // We know that the loop index is a smi if it is not modified in the
2473 // loop body and it is checked against a constant limit in the loop
2474 // condition. In this case, we reset the static type information of the
2475 // loop index to smi before compiling the body, the update expression, and
2476 // the bottom check of the loop condition.
2477 TypeInfoCodeGenState type_info_scope(this,
2478 node->is_fast_smi_loop() ?
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002479 node->loop_variable()->AsSlot() :
2480 NULL,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002481 TypeInfo::Smi());
2482
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002483 // If there is no update statement, label the top of the loop with the
2484 // continue target, otherwise with the loop target.
2485 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2486 if (node->next() == NULL) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002487 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002488 node->continue_target()->Bind();
2489 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002490 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002491 loop.Bind();
2492 }
2493
2494 // If the test is always true, there is no need to compile it.
2495 if (info == DONT_KNOW) {
2496 JumpTarget body;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002497 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002498 if (has_valid_frame()) {
2499 Branch(false, node->break_target());
2500 }
2501 if (has_valid_frame() || body.is_linked()) {
2502 body.Bind();
2503 }
2504 }
2505
2506 if (has_valid_frame()) {
2507 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002508 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002509
2510 if (node->next() == NULL) {
2511 // If there is no update statement and control flow can fall out
2512 // of the loop, jump directly to the continue label.
2513 if (has_valid_frame()) {
2514 node->continue_target()->Jump();
2515 }
2516 } else {
2517 // If there is an update statement and control flow can reach it
2518 // via falling out of the body of the loop or continuing, we
2519 // compile the update statement.
2520 if (node->continue_target()->is_linked()) {
2521 node->continue_target()->Bind();
2522 }
2523 if (has_valid_frame()) {
2524 // Record source position of the statement as this code which is
2525 // after the code for the body actually belongs to the loop
2526 // statement and not the body.
2527 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002528 Visit(node->next());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002529 loop.Jump();
2530 }
2531 }
2532 }
2533 if (node->break_target()->is_linked()) {
2534 node->break_target()->Bind();
2535 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002536 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002537 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538}
2539
2540
ager@chromium.org7c537e22008-10-16 08:43:32 +00002541void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002542#ifdef DEBUG
2543 int original_height = frame_->height();
2544#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002545 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002546 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002548 JumpTarget primitive;
2549 JumpTarget jsobject;
2550 JumpTarget fixed_array;
2551 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2552 JumpTarget end_del_check;
2553 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002554
2555 // Get the object to enumerate over (converted to JSObject).
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002556 Load(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002557
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002558 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002559 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2560 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002561 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002562 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2563 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002564 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002565 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2566 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002567 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568
2569 // Stack layout in body:
2570 // [iteration counter (Smi)]
2571 // [length of array]
2572 // [FixedArray]
2573 // [Map or 0]
2574 // [Object]
2575
2576 // Check if enumerable is already a JSObject
2577 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002578 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002579 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002580 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002581
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002582 primitive.Bind();
2583 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002584 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002585
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002586 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002588 // r0: value to be iterated over
2589 frame_->EmitPush(r0); // Push the object being iterated over.
2590
2591 // Check cache validity in generated code. This is a fast case for
2592 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2593 // guarantee cache validity, call the runtime system to check cache
2594 // validity or get the property names in a fixed array.
2595 JumpTarget call_runtime;
2596 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2597 JumpTarget check_prototype;
2598 JumpTarget use_cache;
2599 __ mov(r1, Operand(r0));
2600 loop.Bind();
2601 // Check that there are no elements.
2602 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2603 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2604 __ cmp(r2, r4);
2605 call_runtime.Branch(ne);
2606 // Check that instance descriptors are not empty so that we can
2607 // check for an enum cache. Leave the map in r3 for the subsequent
2608 // prototype load.
2609 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2610 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2611 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2612 __ cmp(r2, ip);
2613 call_runtime.Branch(eq);
2614 // Check that there in an enum cache in the non-empty instance
2615 // descriptors. This is the case if the next enumeration index
2616 // field does not contain a smi.
2617 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2618 __ tst(r2, Operand(kSmiTagMask));
2619 call_runtime.Branch(eq);
2620 // For all objects but the receiver, check that the cache is empty.
2621 // r4: empty fixed array root.
2622 __ cmp(r1, r0);
2623 check_prototype.Branch(eq);
2624 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2625 __ cmp(r2, r4);
2626 call_runtime.Branch(ne);
2627 check_prototype.Bind();
2628 // Load the prototype from the map and loop if non-null.
2629 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2630 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2631 __ cmp(r1, ip);
2632 loop.Branch(ne);
2633 // The enum cache is valid. Load the map of the object being
2634 // iterated over and use the cache for the iteration.
2635 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2636 use_cache.Jump();
2637
2638 call_runtime.Bind();
2639 // Call the runtime to get the property names for the object.
2640 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002641 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002643 // If we got a map from the runtime call, we can do a fast
2644 // modification check. Otherwise, we got a fixed array, and we have
2645 // to do a slow check.
2646 // r0: map or fixed array (result from call to
2647 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648 __ mov(r2, Operand(r0));
2649 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002650 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2651 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002652 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002654 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002656 // r0: map (either the result from a call to
2657 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2658 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002659 __ mov(r1, Operand(r0));
2660 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2661 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2662 __ ldr(r2,
2663 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2664
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002665 frame_->EmitPush(r0); // map
2666 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002667 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002668 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002669 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002670 frame_->EmitPush(r0);
2671 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002672
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002673 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002675 frame_->EmitPush(r1); // insert 0 in place of Map
2676 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677
2678 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002679 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002680 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002681 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002682 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002683
2684 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002685 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002686 // sp[0] : index
2687 // sp[1] : array/enum cache length
2688 // sp[2] : array or enum cache
2689 // sp[3] : 0 or map
2690 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002691 // Grab the current frame's height for the break and continue
2692 // targets only after all the state is pushed on the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002693 node->break_target()->SetExpectedHeight();
2694 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002696 // Load the current count to r0, load the length to r1.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002697 __ Ldrd(r0, r1, frame_->ElementAt(0));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002698 __ cmp(r0, r1); // compare to the array length
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002699 node->break_target()->Branch(hs);
2700
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002702 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2704 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2705
2706 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002707 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002708 // Check if this (still) matches the map of the enumerable.
2709 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002710 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002711 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2712 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002713 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714
2715 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002716 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2717 frame_->EmitPush(r0);
2718 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002719 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002720 __ mov(r3, Operand(r0), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721 // If the property has been removed while iterating, we just skip it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002722 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002724 end_del_check.Bind();
2725 // Store the entry in the 'each' expression and take another spin in the
2726 // loop. r3: i'th entry of the enum cache (or string there of)
2727 frame_->EmitPush(r3); // push entry
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002728 { VirtualFrame::RegisterAllocationScope scope(this);
2729 Reference each(this, node->each());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002730 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002731 if (each.size() > 0) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002732 // Loading a reference may leave the frame in an unspilled state.
2733 frame_->SpillAll(); // Sync stack to memory.
2734 // Get the value (under the reference on the stack) from memory.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002735 __ ldr(r0, frame_->ElementAt(each.size()));
2736 frame_->EmitPush(r0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002737 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002738 frame_->Drop(2); // The result of the set and the extra pushed value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002739 } else {
2740 // If the reference was to a slot we rely on the convenient property
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002741 // that it doesn't matter whether a value (eg, ebx pushed above) is
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002742 // right on top of or right underneath a zero-sized reference.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002743 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002744 frame_->Drop(1); // Drop the result of the set operation.
mads.s.ager31e71382008-08-13 09:32:07 +00002745 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002746 }
2747 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002748 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002749 CheckStack(); // TODO(1222600): ignore if body contains calls.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002750 { VirtualFrame::RegisterAllocationScope scope(this);
2751 Visit(node->body());
2752 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002753
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002754 // Next. Reestablish a spilled frame in case we are coming here via
2755 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002756 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002757 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002758 frame_->EmitPop(r0);
2759 __ add(r0, r0, Operand(Smi::FromInt(1)));
2760 frame_->EmitPush(r0);
2761 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002762
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002763 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2764 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002765 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002766 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767
2768 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002769 exit.Bind();
2770 node->continue_target()->Unuse();
2771 node->break_target()->Unuse();
2772 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002773}
2774
2775
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002776void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002777#ifdef DEBUG
2778 int original_height = frame_->height();
2779#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002780 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002781 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002782 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002783
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002784 JumpTarget try_block;
2785 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002786
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002787 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002788 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002789 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790
2791 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002792 Variable* catch_var = node->catch_var()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002793 ASSERT(catch_var != NULL && catch_var->AsSlot() != NULL);
2794 StoreToSlot(catch_var->AsSlot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002795
2796 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002797 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002798
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002799 { VirtualFrame::RegisterAllocationScope scope(this);
2800 VisitStatements(node->catch_block()->statements());
2801 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002802 if (frame_ != NULL) {
2803 exit.Jump();
2804 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002805
2806
2807 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002808 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002809
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002810 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2811 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002812
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002813 // Shadow the labels for all escapes from the try block, including
2814 // returns. During shadowing, the original label is hidden as the
2815 // LabelShadow and operations on the original actually affect the
2816 // shadowing label.
2817 //
2818 // We should probably try to unify the escaping labels and the return
2819 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002820 int nof_escapes = node->escaping_targets()->length();
2821 List<ShadowTarget*> shadows(1 + nof_escapes);
2822
2823 // Add the shadow target for the function return.
2824 static const int kReturnShadowIndex = 0;
2825 shadows.Add(new ShadowTarget(&function_return_));
2826 bool function_return_was_shadowed = function_return_is_shadowed_;
2827 function_return_is_shadowed_ = true;
2828 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2829
2830 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002831 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002832 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002833 }
2834
2835 // Generate code for the statements in the try block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002836 { VirtualFrame::RegisterAllocationScope scope(this);
2837 VisitStatements(node->try_block()->statements());
2838 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002839
2840 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002841 // After shadowing stops, the original labels are unshadowed and the
2842 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002843 bool has_unlinks = false;
2844 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002845 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002846 has_unlinks = has_unlinks || shadows[i]->is_linked();
2847 }
2848 function_return_is_shadowed_ = function_return_was_shadowed;
2849
2850 // Get an external reference to the handler address.
2851 ExternalReference handler_address(Top::k_handler_address);
2852
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002853 // If we can fall off the end of the try block, unlink from try chain.
2854 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002855 // The next handler address is on top of the frame. Unlink from
2856 // the handler list and drop the rest of this handler from the
2857 // frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002858 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002859 frame_->EmitPop(r1); // r0 can contain the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002860 __ mov(r3, Operand(handler_address));
2861 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002862 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002863 if (has_unlinks) {
2864 exit.Jump();
2865 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002866 }
2867
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002868 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002869 // jumped to. Deallocate each shadow target.
2870 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002871 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002872 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002873 shadows[i]->Bind();
2874 // Because we can be jumping here (to spilled code) from unspilled
2875 // code, we need to reestablish a spilled frame at this block.
2876 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002877
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002878 // Reload sp from the top handler, because some statements that we
2879 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002880 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002881 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002882 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002883
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002884 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002885 frame_->EmitPop(r1); // r0 can contain the return value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002886 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002887 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002888
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002889 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2890 frame_->PrepareForReturn();
2891 }
2892 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002893 }
2894 }
2895
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002896 exit.Bind();
2897 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002898}
2899
2900
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002901void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002902#ifdef DEBUG
2903 int original_height = frame_->height();
2904#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002905 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002906 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002907 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002908
2909 // State: Used to keep track of reason for entering the finally
2910 // block. Should probably be extended to hold information for
2911 // break/continue from within the try block.
2912 enum { FALLING, THROWING, JUMPING };
2913
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002914 JumpTarget try_block;
2915 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002916
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002917 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002919 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002920 // In case of thrown exceptions, this is where we continue.
2921 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002922 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002923
2924 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002925 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002926
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002927 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2928 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002929
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002930 // Shadow the labels for all escapes from the try block, including
2931 // returns. Shadowing hides the original label as the LabelShadow and
2932 // operations on the original actually affect the shadowing label.
2933 //
2934 // We should probably try to unify the escaping labels and the return
2935 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002936 int nof_escapes = node->escaping_targets()->length();
2937 List<ShadowTarget*> shadows(1 + nof_escapes);
2938
2939 // Add the shadow target for the function return.
2940 static const int kReturnShadowIndex = 0;
2941 shadows.Add(new ShadowTarget(&function_return_));
2942 bool function_return_was_shadowed = function_return_is_shadowed_;
2943 function_return_is_shadowed_ = true;
2944 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2945
2946 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002947 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002948 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002949 }
2950
2951 // Generate code for the statements in the try block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002952 { VirtualFrame::RegisterAllocationScope scope(this);
2953 VisitStatements(node->try_block()->statements());
2954 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002955
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002956 // Stop the introduced shadowing and count the number of required unlinks.
2957 // After shadowing stops, the original labels are unshadowed and the
2958 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002959 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002960 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961 shadows[i]->StopShadowing();
2962 if (shadows[i]->is_linked()) nof_unlinks++;
2963 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002964 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002965
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002966 // Get an external reference to the handler address.
2967 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002968
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002969 // If we can fall off the end of the try block, unlink from the try
2970 // chain and set the state on the frame to FALLING.
2971 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002972 // The next handler address is on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002973 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002974 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002975 __ mov(r3, Operand(handler_address));
2976 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002977 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002978
2979 // Fake a top of stack value (unneeded when FALLING) and set the
2980 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002981 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002982 frame_->EmitPush(r0);
2983 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2984 if (nof_unlinks > 0) {
2985 finally_block.Jump();
2986 }
2987 }
2988
2989 // Generate code to unlink and set the state for the (formerly)
2990 // shadowing targets that have been jumped to.
2991 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002992 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002993 // If we have come from the shadowed return, the return value is
2994 // in (a non-refcounted reference to) r0. We must preserve it
2995 // until it is pushed.
2996 //
2997 // Because we can be jumping here (to spilled code) from
2998 // unspilled code, we need to reestablish a spilled frame at
2999 // this block.
3000 shadows[i]->Bind();
3001 frame_->SpillAll();
3002
3003 // Reload sp from the top handler, because some statements that
3004 // we break from (eg, for...in) may have left stuff on the
3005 // stack.
3006 __ mov(r3, Operand(handler_address));
3007 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003008 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003009
3010 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003011 // handler address is currently on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00003012 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003013 frame_->EmitPop(r1);
3014 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003015 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003016
3017 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003018 // If this label shadowed the function return, materialize the
3019 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003020 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003021 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003022 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003023 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003024 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003025 }
3026 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003027 if (--nof_unlinks > 0) {
3028 // If this is not the last unlink block, jump around the next.
3029 finally_block.Jump();
3030 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003031 }
3032 }
3033
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003034 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003035 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003036
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003037 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003038 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003039
3040 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003041 // and the state - while evaluating the finally block.
3042 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003043 // Generate code for the statements in the finally block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003044 { VirtualFrame::RegisterAllocationScope scope(this);
3045 VisitStatements(node->finally_block()->statements());
3046 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003047
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003048 if (has_valid_frame()) {
3049 // Restore state and return value or faked TOS.
3050 frame_->EmitPop(r2);
3051 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003052 }
3053
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003054 // Generate code to jump to the right destination for all used
3055 // formerly shadowing targets. Deallocate each shadow target.
3056 for (int i = 0; i < shadows.length(); i++) {
3057 if (has_valid_frame() && shadows[i]->is_bound()) {
3058 JumpTarget* original = shadows[i]->other_target();
3059 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
3060 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003061 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003062 skip.Branch(ne);
3063 frame_->PrepareForReturn();
3064 original->Jump();
3065 skip.Bind();
3066 } else {
3067 original->Branch(eq);
3068 }
3069 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003070 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003071
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003072 if (has_valid_frame()) {
3073 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003074 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003075 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
3076 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003077
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003078 // Rethrow exception.
3079 frame_->EmitPush(r0);
3080 frame_->CallRuntime(Runtime::kReThrow, 1);
3081
3082 // Done.
3083 exit.Bind();
3084 }
3085 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003086}
3087
3088
ager@chromium.org7c537e22008-10-16 08:43:32 +00003089void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003090#ifdef DEBUG
3091 int original_height = frame_->height();
3092#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003093 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003094 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003095#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00003096 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003097#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003098 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003099 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003100}
3101
3102
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003103void CodeGenerator::InstantiateFunction(
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003104 Handle<SharedFunctionInfo> function_info,
3105 bool pretenure) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003106 // Use the fast case closure allocation code that allocates in new
3107 // space for nested functions that don't need literals cloning.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003108 if (scope()->is_function_scope() &&
3109 function_info->num_literals() == 0 &&
3110 !pretenure) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003111 FastNewClosureStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003112 frame_->EmitPush(Operand(function_info));
3113 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003114 frame_->CallStub(&stub, 1);
3115 frame_->EmitPush(r0);
3116 } else {
3117 // Create a new closure.
3118 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003119 frame_->EmitPush(Operand(function_info));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003120 frame_->EmitPush(Operand(pretenure
3121 ? Factory::true_value()
3122 : Factory::false_value()));
3123 frame_->CallRuntime(Runtime::kNewClosure, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003124 frame_->EmitPush(r0);
3125 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003126}
3127
3128
ager@chromium.org7c537e22008-10-16 08:43:32 +00003129void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003130#ifdef DEBUG
3131 int original_height = frame_->height();
3132#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003133 Comment cmnt(masm_, "[ FunctionLiteral");
3134
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003135 // Build the function info and instantiate it.
3136 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00003137 Compiler::BuildFunctionInfo(node, script());
3138 if (function_info.is_null()) {
3139 SetStackOverflow();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003140 ASSERT(frame_->height() == original_height);
3141 return;
3142 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003143 InstantiateFunction(function_info, node->pretenure());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003144 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003145}
3146
3147
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003148void CodeGenerator::VisitSharedFunctionInfoLiteral(
3149 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003150#ifdef DEBUG
3151 int original_height = frame_->height();
3152#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003153 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003154 InstantiateFunction(node->shared_function_info(), false);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003155 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003156}
3157
3158
ager@chromium.org7c537e22008-10-16 08:43:32 +00003159void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003160#ifdef DEBUG
3161 int original_height = frame_->height();
3162#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003163 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003164 JumpTarget then;
3165 JumpTarget else_;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003166 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003167 if (has_valid_frame()) {
3168 Branch(false, &else_);
3169 }
3170 if (has_valid_frame() || then.is_linked()) {
3171 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003172 Load(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003173 }
3174 if (else_.is_linked()) {
3175 JumpTarget exit;
3176 if (has_valid_frame()) exit.Jump();
3177 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003178 Load(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003179 if (exit.is_linked()) exit.Bind();
3180 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003181 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003182}
3183
3184
ager@chromium.org7c537e22008-10-16 08:43:32 +00003185void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
3186 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003187 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003188
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003189 // JumpTargets do not yet support merging frames so the frame must be
3190 // spilled when jumping to these targets.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003191 JumpTarget slow;
3192 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003193
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003194 // Generate fast case for loading from slots that correspond to
3195 // local/global variables or arguments unless they are shadowed by
3196 // eval-introduced bindings.
3197 EmitDynamicLoadFromSlotFastCase(slot,
3198 typeof_state,
3199 &slow,
3200 &done);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003201
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003202 slow.Bind();
3203 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003204 frame_->EmitPush(Operand(slot->var()->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003205
ager@chromium.org7c537e22008-10-16 08:43:32 +00003206 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003207 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003208 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003209 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003210 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00003211
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003212 done.Bind();
3213 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003214
3215 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00003216 Register scratch = VirtualFrame::scratch0();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003217 TypeInfo info = type_info(slot);
3218 frame_->EmitPush(SlotOperand(slot, scratch), info);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003219
ager@chromium.org7c537e22008-10-16 08:43:32 +00003220 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003221 // Const slots may contain 'the hole' value (the constant hasn't been
3222 // initialized yet) which needs to be converted into the 'undefined'
3223 // value.
3224 Comment cmnt(masm_, "[ Unhole const");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003225 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003226 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003227 __ cmp(tos, ip);
3228 __ LoadRoot(tos, Heap::kUndefinedValueRootIndex, eq);
3229 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003230 }
3231 }
3232}
3233
3234
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003235void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot,
3236 TypeofState state) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003237 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003238 LoadFromSlot(slot, state);
3239
3240 // Bail out quickly if we're not using lazy arguments allocation.
3241 if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return;
3242
3243 // ... or if the slot isn't a non-parameter arguments slot.
3244 if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return;
3245
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003246 // Load the loaded value from the stack into a register but leave it on the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003247 // stack.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003248 Register tos = frame_->Peek();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003249
3250 // If the loaded value is the sentinel that indicates that we
3251 // haven't loaded the arguments object yet, we need to do it now.
3252 JumpTarget exit;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003253 __ LoadRoot(ip, Heap::kArgumentsMarkerRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003254 __ cmp(tos, ip);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003255 exit.Branch(ne);
3256 frame_->Drop();
3257 StoreArgumentsObject(false);
3258 exit.Bind();
3259}
3260
3261
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003262void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
3263 ASSERT(slot != NULL);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003264 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003265 if (slot->type() == Slot::LOOKUP) {
3266 ASSERT(slot->var()->is_dynamic());
3267
3268 // For now, just do a runtime call.
3269 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003270 frame_->EmitPush(Operand(slot->var()->name()));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003271
3272 if (init_state == CONST_INIT) {
3273 // Same as the case for a normal store, but ignores attribute
3274 // (e.g. READ_ONLY) of context slot so that we can initialize
3275 // const properties (introduced via eval("const foo = (some
3276 // expr);")). Also, uses the current function context instead of
3277 // the top context.
3278 //
3279 // Note that we must declare the foo upon entry of eval(), via a
3280 // context slot declaration, but we cannot initialize it at the
3281 // same time, because the const declaration may be at the end of
3282 // the eval code (sigh...) and the const variable may have been
3283 // used before (where its value is 'undefined'). Thus, we can only
3284 // do the initialization when we actually encounter the expression
3285 // and when the expression operands are defined and valid, and
3286 // thus we need the split into 2 operations: declaration of the
3287 // context slot followed by initialization.
3288 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3289 } else {
3290 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
3291 }
3292 // Storing a variable must keep the (new) value on the expression
3293 // stack. This is necessary for compiling assignment expressions.
3294 frame_->EmitPush(r0);
3295
3296 } else {
3297 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00003298 Register scratch = VirtualFrame::scratch0();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003299 Register scratch2 = VirtualFrame::scratch1();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003300
ager@chromium.org357bf652010-04-12 11:30:10 +00003301 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003302 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00003303
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003304 if (init_state == CONST_INIT) {
3305 ASSERT(slot->var()->mode() == Variable::CONST);
3306 // Only the first const initialization must be executed (the slot
3307 // still contains 'the hole' value). When the assignment is
3308 // executed, the code is identical to a normal store (see below).
3309 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00003310 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003311 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00003312 __ cmp(scratch, ip);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003313 exit.Branch(ne);
3314 }
3315
3316 // We must execute the store. Storing a variable must keep the
3317 // (new) value on the stack. This is necessary for compiling
3318 // assignment expressions.
3319 //
3320 // Note: We will reach here even with slot->var()->mode() ==
3321 // Variable::CONST because of const declarations which will
3322 // initialize consts to 'the hole' value and by doing so, end up
3323 // calling this code. r2 may be loaded with context; used below in
3324 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00003325 Register tos = frame_->Peek();
3326 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003327 if (slot->type() == Slot::CONTEXT) {
3328 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00003329 __ tst(tos, Operand(kSmiTagMask));
3330 // We don't use tos any more after here.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003331 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00003332 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003333 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003334 // We need an extra register. Until we have a way to do that in the
3335 // virtual frame we will cheat and ask for a free TOS register.
3336 Register scratch3 = frame_->GetTOSRegister();
3337 __ RecordWrite(scratch, Operand(offset), scratch2, scratch3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003338 }
3339 // If we definitely did not jump over the assignment, we do not need
3340 // to bind the exit label. Doing so can defeat peephole
3341 // optimization.
3342 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3343 exit.Bind();
3344 }
3345 }
3346}
3347
3348
ager@chromium.org381abbb2009-02-25 13:23:22 +00003349void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
3350 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003351 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003352 // Check that no extension objects have been created by calls to
3353 // eval from the current scope to the global scope.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003354 Register tmp = frame_->scratch0();
3355 Register tmp2 = frame_->scratch1();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003356 Register context = cp;
3357 Scope* s = scope();
3358 while (s != NULL) {
3359 if (s->num_heap_slots() > 0) {
3360 if (s->calls_eval()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003361 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003362 // Check that extension is NULL.
3363 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
3364 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003365 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003366 }
3367 // Load next context in chain.
3368 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
3369 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3370 context = tmp;
3371 }
3372 // If no outer scope calls eval, we do not need to check more
3373 // context extensions.
3374 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
3375 s = s->outer_scope();
3376 }
3377
3378 if (s->is_eval_scope()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003379 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003380 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00003381 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003382 __ bind(&next);
3383 // Terminate at global context.
3384 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003385 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
3386 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003387 __ b(eq, &fast);
3388 // Check that extension is NULL.
3389 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
3390 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003391 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003392 // Load next context in chain.
3393 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
3394 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3395 __ b(&next);
3396 __ bind(&fast);
3397 }
3398
ager@chromium.org381abbb2009-02-25 13:23:22 +00003399 // Load the global object.
3400 LoadGlobal();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003401 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00003402 frame_->CallLoadIC(slot->var()->name(),
3403 typeof_state == INSIDE_TYPEOF
3404 ? RelocInfo::CODE_TARGET
3405 : RelocInfo::CODE_TARGET_CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003406}
3407
3408
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003409void CodeGenerator::EmitDynamicLoadFromSlotFastCase(Slot* slot,
3410 TypeofState typeof_state,
3411 JumpTarget* slow,
3412 JumpTarget* done) {
3413 // Generate fast-case code for variables that might be shadowed by
3414 // eval-introduced variables. Eval is used a lot without
3415 // introducing variables. In those cases, we do not want to
3416 // perform a runtime call for all variables in the scope
3417 // containing the eval.
3418 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
3419 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, slow);
3420 frame_->SpillAll();
3421 done->Jump();
3422
3423 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
3424 frame_->SpillAll();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003425 Slot* potential_slot = slot->var()->local_if_not_shadowed()->AsSlot();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003426 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
3427 if (potential_slot != NULL) {
3428 // Generate fast case for locals that rewrite to slots.
3429 __ ldr(r0,
3430 ContextSlotOperandCheckExtensions(potential_slot,
3431 r1,
3432 r2,
3433 slow));
3434 if (potential_slot->var()->mode() == Variable::CONST) {
3435 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3436 __ cmp(r0, ip);
3437 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
3438 }
3439 done->Jump();
3440 } else if (rewrite != NULL) {
3441 // Generate fast case for argument loads.
3442 Property* property = rewrite->AsProperty();
3443 if (property != NULL) {
3444 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
3445 Literal* key_literal = property->key()->AsLiteral();
3446 if (obj_proxy != NULL &&
3447 key_literal != NULL &&
3448 obj_proxy->IsArguments() &&
3449 key_literal->handle()->IsSmi()) {
3450 // Load arguments object if there are no eval-introduced
3451 // variables. Then load the argument from the arguments
3452 // object using keyed load.
3453 __ ldr(r0,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003454 ContextSlotOperandCheckExtensions(obj_proxy->var()->AsSlot(),
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003455 r1,
3456 r2,
3457 slow));
3458 frame_->EmitPush(r0);
3459 __ mov(r1, Operand(key_literal->handle()));
3460 frame_->EmitPush(r1);
3461 EmitKeyedLoad();
3462 done->Jump();
3463 }
3464 }
3465 }
3466 }
3467}
3468
3469
ager@chromium.org7c537e22008-10-16 08:43:32 +00003470void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003471#ifdef DEBUG
3472 int original_height = frame_->height();
3473#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003474 Comment cmnt(masm_, "[ Slot");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003475 LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003476 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003477}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003478
ager@chromium.org7c537e22008-10-16 08:43:32 +00003479
3480void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003481#ifdef DEBUG
3482 int original_height = frame_->height();
3483#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003484 Comment cmnt(masm_, "[ VariableProxy");
3485
3486 Variable* var = node->var();
3487 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003488 if (expr != NULL) {
3489 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003490 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00003491 ASSERT(var->is_global());
3492 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003493 ref.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003494 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003495 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003496}
3497
3498
ager@chromium.org7c537e22008-10-16 08:43:32 +00003499void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003500#ifdef DEBUG
3501 int original_height = frame_->height();
3502#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003503 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00003504 Register reg = frame_->GetTOSRegister();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003505 bool is_smi = node->handle()->IsSmi();
ager@chromium.org357bf652010-04-12 11:30:10 +00003506 __ mov(reg, Operand(node->handle()));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003507 frame_->EmitPush(reg, is_smi ? TypeInfo::Smi() : TypeInfo::Unknown());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003508 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003509}
3510
3511
ager@chromium.org7c537e22008-10-16 08:43:32 +00003512void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003513#ifdef DEBUG
3514 int original_height = frame_->height();
3515#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003516 Comment cmnt(masm_, "[ RexExp Literal");
3517
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003518 Register tmp = VirtualFrame::scratch0();
3519 // Free up a TOS register that can be used to push the literal.
3520 Register literal = frame_->GetTOSRegister();
3521
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003522 // Retrieve the literal array and check the allocated entry.
3523
3524 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003525 __ ldr(tmp, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003526
3527 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003528 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kLiteralsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003529
3530 // Load the literal at the ast saved index.
3531 int literal_offset =
3532 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003533 __ ldr(literal, FieldMemOperand(tmp, literal_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003534
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003535 JumpTarget materialized;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003536 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003537 __ cmp(literal, ip);
3538 // This branch locks the virtual frame at the done label to match the
3539 // one we have here, where the literal register is not on the stack and
3540 // nothing is spilled.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003541 materialized.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003542
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003543 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003544 // the literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003545 // literal array (0)
3546 frame_->EmitPush(tmp);
3547 // literal index (1)
3548 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3549 // RegExp pattern (2)
3550 frame_->EmitPush(Operand(node->pattern()));
3551 // RegExp flags (3)
3552 frame_->EmitPush(Operand(node->flags()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003553 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003554 __ Move(literal, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003555
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003556 materialized.Bind();
3557
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003558 frame_->EmitPush(literal);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003559 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
3560 frame_->EmitPush(Operand(Smi::FromInt(size)));
3561 frame_->CallRuntime(Runtime::kAllocateInNewSpace, 1);
3562 // TODO(lrn): Use AllocateInNewSpace macro with fallback to runtime.
3563 // r0 is newly allocated space.
3564
3565 // Reuse literal variable with (possibly) a new register, still holding
3566 // the materialized boilerplate.
3567 literal = frame_->PopToRegister(r0);
3568
3569 __ CopyFields(r0, literal, tmp.bit(), size / kPointerSize);
3570
3571 // Push the clone.
3572 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003573 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003574}
3575
3576
ager@chromium.org7c537e22008-10-16 08:43:32 +00003577void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003578#ifdef DEBUG
3579 int original_height = frame_->height();
3580#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003581 Comment cmnt(masm_, "[ ObjectLiteral");
3582
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003583 Register literal = frame_->GetTOSRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003584 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003585 __ ldr(literal, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003586 // Literal array.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003587 __ ldr(literal, FieldMemOperand(literal, JSFunction::kLiteralsOffset));
3588 frame_->EmitPush(literal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003589 // Literal index.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003590 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003591 // Constant properties.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003592 frame_->EmitPush(Operand(node->constant_properties()));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003593 // Should the object literal have fast elements?
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003594 frame_->EmitPush(Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003595 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003596 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003597 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003598 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003599 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003600 frame_->EmitPush(r0); // save the result
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003601
3602 // Mark all computed expressions that are bound to a key that
3603 // is shadowed by a later occurrence of the same key. For the
3604 // marked expressions, no store code is emitted.
3605 node->CalculateEmitStore();
3606
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003607 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003608 // At the start of each iteration, the top of stack contains
3609 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003610 ObjectLiteral::Property* property = node->properties()->at(i);
3611 Literal* key = property->key();
3612 Expression* value = property->value();
3613 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003614 case ObjectLiteral::Property::CONSTANT:
3615 break;
3616 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3617 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3618 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003619 case ObjectLiteral::Property::COMPUTED:
3620 if (key->handle()->IsSymbol()) {
3621 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003622 Load(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003623 if (property->emit_store()) {
3624 frame_->PopToR0();
3625 // Fetch the object literal.
3626 frame_->SpillAllButCopyTOSToR1();
3627 __ mov(r2, Operand(key->handle()));
3628 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3629 } else {
3630 frame_->Drop();
3631 }
ager@chromium.org5c838252010-02-19 08:53:10 +00003632 break;
3633 }
3634 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003635 case ObjectLiteral::Property::PROTOTYPE: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003636 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003637 Load(key);
3638 Load(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003639 if (property->emit_store()) {
3640 frame_->CallRuntime(Runtime::kSetProperty, 3);
3641 } else {
3642 frame_->Drop(3);
3643 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003644 break;
3645 }
3646 case ObjectLiteral::Property::SETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003647 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003648 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003649 frame_->EmitPush(Operand(Smi::FromInt(1)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003650 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003651 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003652 break;
3653 }
3654 case ObjectLiteral::Property::GETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003655 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003656 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003657 frame_->EmitPush(Operand(Smi::FromInt(0)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003658 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003659 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003660 break;
3661 }
3662 }
3663 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003664 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003665}
3666
3667
ager@chromium.org7c537e22008-10-16 08:43:32 +00003668void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003669#ifdef DEBUG
3670 int original_height = frame_->height();
3671#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003672 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003673
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003674 Register tos = frame_->GetTOSRegister();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003675 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003676 __ ldr(tos, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003677 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003678 __ ldr(tos, FieldMemOperand(tos, JSFunction::kLiteralsOffset));
3679 frame_->EmitPush(tos);
3680 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3681 frame_->EmitPush(Operand(node->constant_elements()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003682 int length = node->values()->length();
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003683 if (node->constant_elements()->map() == Heap::fixed_cow_array_map()) {
3684 FastCloneShallowArrayStub stub(
3685 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
3686 frame_->CallStub(&stub, 3);
3687 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
3688 } else if (node->depth() > 1) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003689 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003690 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003691 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003692 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003693 FastCloneShallowArrayStub stub(
3694 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
ager@chromium.org5c838252010-02-19 08:53:10 +00003695 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003696 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003697 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003698 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003699
3700 // Generate code to set the elements in the array that are not
3701 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003702 for (int i = 0; i < node->values()->length(); i++) {
3703 Expression* value = node->values()->at(i);
3704
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003705 // If value is a literal the property value is already set in the
3706 // boilerplate object.
3707 if (value->AsLiteral() != NULL) continue;
3708 // If value is a materialized literal the property value is already set
3709 // in the boilerplate object if it is simple.
3710 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003711
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003712 // The property must be set by generated code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003713 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003714 frame_->PopToR0();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003715 // Fetch the object literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003716 frame_->SpillAllButCopyTOSToR1();
3717
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003718 // Get the elements array.
3719 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003720
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003721 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003722 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003723 __ str(r0, FieldMemOperand(r1, offset));
3724
3725 // Update the write barrier for the array address.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003726 __ RecordWrite(r1, Operand(offset), r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003727 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003728 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003729}
3730
3731
ager@chromium.org32912102009-01-16 10:38:43 +00003732void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003733#ifdef DEBUG
3734 int original_height = frame_->height();
3735#endif
ager@chromium.org32912102009-01-16 10:38:43 +00003736 // Call runtime routine to allocate the catch extension object and
3737 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003738 Comment cmnt(masm_, "[ CatchExtensionObject");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003739 Load(node->key());
3740 Load(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003741 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3742 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003743 ASSERT_EQ(original_height + 1, frame_->height());
3744}
3745
3746
3747void CodeGenerator::EmitSlotAssignment(Assignment* node) {
3748#ifdef DEBUG
3749 int original_height = frame_->height();
3750#endif
3751 Comment cmnt(masm(), "[ Variable Assignment");
3752 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3753 ASSERT(var != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003754 Slot* slot = var->AsSlot();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003755 ASSERT(slot != NULL);
3756
3757 // Evaluate the right-hand side.
3758 if (node->is_compound()) {
3759 // For a compound assignment the right-hand side is a binary operation
3760 // between the current property value and the actual right-hand side.
3761 LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
3762
3763 // Perform the binary operation.
3764 Literal* literal = node->value()->AsLiteral();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003765 bool overwrite_value = node->value()->ResultOverwriteAllowed();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003766 if (literal != NULL && literal->handle()->IsSmi()) {
3767 SmiOperation(node->binary_op(),
3768 literal->handle(),
3769 false,
3770 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3771 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003772 GenerateInlineSmi inline_smi =
3773 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3774 if (literal != NULL) {
3775 ASSERT(!literal->handle()->IsSmi());
3776 inline_smi = DONT_GENERATE_INLINE_SMI;
3777 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003778 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003779 GenericBinaryOperation(node->binary_op(),
3780 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3781 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003782 }
3783 } else {
3784 Load(node->value());
3785 }
3786
3787 // Perform the assignment.
3788 if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) {
3789 CodeForSourcePosition(node->position());
3790 StoreToSlot(slot,
3791 node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT);
3792 }
3793 ASSERT_EQ(original_height + 1, frame_->height());
3794}
3795
3796
3797void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) {
3798#ifdef DEBUG
3799 int original_height = frame_->height();
3800#endif
3801 Comment cmnt(masm(), "[ Named Property Assignment");
3802 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3803 Property* prop = node->target()->AsProperty();
3804 ASSERT(var == NULL || (prop == NULL && var->is_global()));
3805
3806 // Initialize name and evaluate the receiver sub-expression if necessary. If
3807 // the receiver is trivial it is not placed on the stack at this point, but
3808 // loaded whenever actually needed.
3809 Handle<String> name;
3810 bool is_trivial_receiver = false;
3811 if (var != NULL) {
3812 name = var->name();
3813 } else {
3814 Literal* lit = prop->key()->AsLiteral();
3815 ASSERT_NOT_NULL(lit);
3816 name = Handle<String>::cast(lit->handle());
3817 // Do not materialize the receiver on the frame if it is trivial.
3818 is_trivial_receiver = prop->obj()->IsTrivial();
3819 if (!is_trivial_receiver) Load(prop->obj());
3820 }
3821
3822 // Change to slow case in the beginning of an initialization block to
3823 // avoid the quadratic behavior of repeatedly adding fast properties.
3824 if (node->starts_initialization_block()) {
3825 // Initialization block consists of assignments of the form expr.x = ..., so
3826 // this will never be an assignment to a variable, so there must be a
3827 // receiver object.
3828 ASSERT_EQ(NULL, var);
3829 if (is_trivial_receiver) {
3830 Load(prop->obj());
3831 } else {
3832 frame_->Dup();
3833 }
3834 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3835 }
3836
3837 // Change to fast case at the end of an initialization block. To prepare for
3838 // that add an extra copy of the receiver to the frame, so that it can be
3839 // converted back to fast case after the assignment.
3840 if (node->ends_initialization_block() && !is_trivial_receiver) {
3841 frame_->Dup();
3842 }
3843
3844 // Stack layout:
3845 // [tos] : receiver (only materialized if non-trivial)
3846 // [tos+1] : receiver if at the end of an initialization block
3847
3848 // Evaluate the right-hand side.
3849 if (node->is_compound()) {
3850 // For a compound assignment the right-hand side is a binary operation
3851 // between the current property value and the actual right-hand side.
3852 if (is_trivial_receiver) {
3853 Load(prop->obj());
3854 } else if (var != NULL) {
3855 LoadGlobal();
3856 } else {
3857 frame_->Dup();
3858 }
3859 EmitNamedLoad(name, var != NULL);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003860
3861 // Perform the binary operation.
3862 Literal* literal = node->value()->AsLiteral();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003863 bool overwrite_value = node->value()->ResultOverwriteAllowed();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003864 if (literal != NULL && literal->handle()->IsSmi()) {
3865 SmiOperation(node->binary_op(),
3866 literal->handle(),
3867 false,
3868 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3869 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003870 GenerateInlineSmi inline_smi =
3871 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3872 if (literal != NULL) {
3873 ASSERT(!literal->handle()->IsSmi());
3874 inline_smi = DONT_GENERATE_INLINE_SMI;
3875 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003876 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003877 GenericBinaryOperation(node->binary_op(),
3878 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3879 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003880 }
3881 } else {
3882 // For non-compound assignment just load the right-hand side.
3883 Load(node->value());
3884 }
3885
3886 // Stack layout:
3887 // [tos] : value
3888 // [tos+1] : receiver (only materialized if non-trivial)
3889 // [tos+2] : receiver if at the end of an initialization block
3890
3891 // Perform the assignment. It is safe to ignore constants here.
3892 ASSERT(var == NULL || var->mode() != Variable::CONST);
3893 ASSERT_NE(Token::INIT_CONST, node->op());
3894 if (is_trivial_receiver) {
3895 // Load the receiver and swap with the value.
3896 Load(prop->obj());
3897 Register t0 = frame_->PopToRegister();
3898 Register t1 = frame_->PopToRegister(t0);
3899 frame_->EmitPush(t0);
3900 frame_->EmitPush(t1);
3901 }
3902 CodeForSourcePosition(node->position());
3903 bool is_contextual = (var != NULL);
3904 EmitNamedStore(name, is_contextual);
3905 frame_->EmitPush(r0);
3906
3907 // Change to fast case at the end of an initialization block.
3908 if (node->ends_initialization_block()) {
3909 ASSERT_EQ(NULL, var);
3910 // The argument to the runtime call is the receiver.
3911 if (is_trivial_receiver) {
3912 Load(prop->obj());
3913 } else {
3914 // A copy of the receiver is below the value of the assignment. Swap
3915 // the receiver and the value of the assignment expression.
3916 Register t0 = frame_->PopToRegister();
3917 Register t1 = frame_->PopToRegister(t0);
3918 frame_->EmitPush(t0);
3919 frame_->EmitPush(t1);
3920 }
3921 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3922 }
3923
3924 // Stack layout:
3925 // [tos] : result
3926
3927 ASSERT_EQ(original_height + 1, frame_->height());
3928}
3929
3930
3931void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) {
3932#ifdef DEBUG
3933 int original_height = frame_->height();
3934#endif
3935 Comment cmnt(masm_, "[ Keyed Property Assignment");
3936 Property* prop = node->target()->AsProperty();
3937 ASSERT_NOT_NULL(prop);
3938
3939 // Evaluate the receiver subexpression.
3940 Load(prop->obj());
3941
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003942 WriteBarrierCharacter wb_info;
3943
ager@chromium.orgac091b72010-05-05 07:34:42 +00003944 // Change to slow case in the beginning of an initialization block to
3945 // avoid the quadratic behavior of repeatedly adding fast properties.
3946 if (node->starts_initialization_block()) {
3947 frame_->Dup();
3948 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3949 }
3950
3951 // Change to fast case at the end of an initialization block. To prepare for
3952 // that add an extra copy of the receiver to the frame, so that it can be
3953 // converted back to fast case after the assignment.
3954 if (node->ends_initialization_block()) {
3955 frame_->Dup();
3956 }
3957
3958 // Evaluate the key subexpression.
3959 Load(prop->key());
3960
3961 // Stack layout:
3962 // [tos] : key
3963 // [tos+1] : receiver
3964 // [tos+2] : receiver if at the end of an initialization block
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003965 //
ager@chromium.orgac091b72010-05-05 07:34:42 +00003966 // Evaluate the right-hand side.
3967 if (node->is_compound()) {
3968 // For a compound assignment the right-hand side is a binary operation
3969 // between the current property value and the actual right-hand side.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003970 // Duplicate receiver and key for loading the current property value.
3971 frame_->Dup2();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003972 EmitKeyedLoad();
3973 frame_->EmitPush(r0);
3974
3975 // Perform the binary operation.
3976 Literal* literal = node->value()->AsLiteral();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003977 bool overwrite_value = node->value()->ResultOverwriteAllowed();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003978 if (literal != NULL && literal->handle()->IsSmi()) {
3979 SmiOperation(node->binary_op(),
3980 literal->handle(),
3981 false,
3982 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3983 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003984 GenerateInlineSmi inline_smi =
3985 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3986 if (literal != NULL) {
3987 ASSERT(!literal->handle()->IsSmi());
3988 inline_smi = DONT_GENERATE_INLINE_SMI;
3989 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003990 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003991 GenericBinaryOperation(node->binary_op(),
3992 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3993 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003994 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003995 wb_info = node->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
ager@chromium.orgac091b72010-05-05 07:34:42 +00003996 } else {
3997 // For non-compound assignment just load the right-hand side.
3998 Load(node->value());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003999 wb_info = node->value()->AsLiteral() != NULL ?
4000 NEVER_NEWSPACE :
4001 (node->value()->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI);
ager@chromium.orgac091b72010-05-05 07:34:42 +00004002 }
4003
4004 // Stack layout:
4005 // [tos] : value
4006 // [tos+1] : key
4007 // [tos+2] : receiver
4008 // [tos+3] : receiver if at the end of an initialization block
4009
4010 // Perform the assignment. It is safe to ignore constants here.
4011 ASSERT(node->op() != Token::INIT_CONST);
4012 CodeForSourcePosition(node->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004013 EmitKeyedStore(prop->key()->type(), wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00004014 frame_->EmitPush(r0);
4015
4016 // Stack layout:
4017 // [tos] : result
4018 // [tos+1] : receiver if at the end of an initialization block
4019
4020 // Change to fast case at the end of an initialization block.
4021 if (node->ends_initialization_block()) {
4022 // The argument to the runtime call is the extra copy of the receiver,
4023 // which is below the value of the assignment. Swap the receiver and
4024 // the value of the assignment expression.
4025 Register t0 = frame_->PopToRegister();
4026 Register t1 = frame_->PopToRegister(t0);
4027 frame_->EmitPush(t1);
4028 frame_->EmitPush(t0);
4029 frame_->CallRuntime(Runtime::kToFastProperties, 1);
4030 }
4031
4032 // Stack layout:
4033 // [tos] : result
4034
4035 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org32912102009-01-16 10:38:43 +00004036}
4037
4038
ager@chromium.org7c537e22008-10-16 08:43:32 +00004039void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004040 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004041#ifdef DEBUG
4042 int original_height = frame_->height();
4043#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004044 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00004045
ager@chromium.orgac091b72010-05-05 07:34:42 +00004046 Variable* var = node->target()->AsVariableProxy()->AsVariable();
4047 Property* prop = node->target()->AsProperty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004048
ager@chromium.orgac091b72010-05-05 07:34:42 +00004049 if (var != NULL && !var->is_global()) {
4050 EmitSlotAssignment(node);
mads.s.ager31e71382008-08-13 09:32:07 +00004051
ager@chromium.orgac091b72010-05-05 07:34:42 +00004052 } else if ((prop != NULL && prop->key()->IsPropertyName()) ||
4053 (var != NULL && var->is_global())) {
4054 // Properties whose keys are property names and global variables are
4055 // treated as named property references. We do not need to consider
4056 // global 'this' because it is not a valid left-hand side.
4057 EmitNamedPropertyAssignment(node);
4058
4059 } else if (prop != NULL) {
4060 // Other properties (including rewritten parameters for a function that
4061 // uses arguments) are keyed property assignments.
4062 EmitKeyedPropertyAssignment(node);
4063
4064 } else {
4065 // Invalid left-hand side.
4066 Load(node->target());
4067 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
4068 // The runtime call doesn't actually return but the code generator will
4069 // still generate code and expects a certain frame height.
4070 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004071 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004072 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004073}
4074
4075
ager@chromium.org7c537e22008-10-16 08:43:32 +00004076void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004077#ifdef DEBUG
4078 int original_height = frame_->height();
4079#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004080 Comment cmnt(masm_, "[ Throw");
4081
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004082 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004083 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004084 frame_->CallRuntime(Runtime::kThrow, 1);
4085 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00004086 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004087}
4088
4089
ager@chromium.org7c537e22008-10-16 08:43:32 +00004090void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004091#ifdef DEBUG
4092 int original_height = frame_->height();
4093#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004094 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004095
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004096 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00004097 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004098 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004099 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004100}
4101
4102
ager@chromium.org7c537e22008-10-16 08:43:32 +00004103void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004104#ifdef DEBUG
4105 int original_height = frame_->height();
4106#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004107 Comment cmnt(masm_, "[ Call");
4108
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004109 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004110 ZoneList<Expression*>* args = node->arguments();
4111
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004112 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004113 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004114 Variable* var = function->AsVariableProxy()->AsVariable();
4115 Property* property = function->AsProperty();
4116
4117 // ------------------------------------------------------------------------
4118 // Fast-case: Use inline caching.
4119 // ---
4120 // According to ECMA-262, section 11.2.3, page 44, the function to call
4121 // must be resolved after the arguments have been evaluated. The IC code
4122 // automatically handles this by loading the arguments before the function
4123 // is resolved in cache misses (this also holds for megamorphic calls).
4124 // ------------------------------------------------------------------------
4125
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004126 if (var != NULL && var->is_possibly_eval()) {
4127 // ----------------------------------
4128 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
4129 // ----------------------------------
4130
4131 // In a call to eval, we first call %ResolvePossiblyDirectEval to
4132 // resolve the function we need to call and the receiver of the
4133 // call. Then we call the resolved function using the given
4134 // arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004135
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004136 // Prepare stack for call to resolved function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004137 Load(function);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004138
4139 // Allocate a frame slot for the receiver.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00004140 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004141
4142 // Load the arguments.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004143 int arg_count = args->length();
4144 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004145 Load(args->at(i));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004146 }
4147
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00004148 VirtualFrame::SpilledScope spilled_scope(frame_);
4149
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004150 // If we know that eval can only be shadowed by eval-introduced
4151 // variables we attempt to load the global eval function directly
4152 // in generated code. If we succeed, there is no need to perform a
4153 // context lookup in the runtime system.
4154 JumpTarget done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004155 if (var->AsSlot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
4156 ASSERT(var->AsSlot()->type() == Slot::LOOKUP);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004157 JumpTarget slow;
4158 // Prepare the stack for the call to
4159 // ResolvePossiblyDirectEvalNoLookup by pushing the loaded
4160 // function, the first argument to the eval call and the
4161 // receiver.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004162 LoadFromGlobalSlotCheckExtensions(var->AsSlot(),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004163 NOT_INSIDE_TYPEOF,
4164 &slow);
4165 frame_->EmitPush(r0);
4166 if (arg_count > 0) {
4167 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
4168 frame_->EmitPush(r1);
4169 } else {
4170 frame_->EmitPush(r2);
4171 }
4172 __ ldr(r1, frame_->Receiver());
4173 frame_->EmitPush(r1);
4174
ricow@chromium.org83aa5492011-02-07 12:42:56 +00004175 // Push the strict mode flag.
4176 frame_->EmitPush(Operand(Smi::FromInt(strict_mode_flag())));
4177
4178 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEvalNoLookup, 4);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004179
4180 done.Jump();
4181 slow.Bind();
4182 }
4183
4184 // Prepare the stack for the call to ResolvePossiblyDirectEval by
4185 // pushing the loaded function, the first argument to the eval
4186 // call and the receiver.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004187 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
4188 frame_->EmitPush(r1);
4189 if (arg_count > 0) {
4190 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
4191 frame_->EmitPush(r1);
4192 } else {
4193 frame_->EmitPush(r2);
4194 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004195 __ ldr(r1, frame_->Receiver());
4196 frame_->EmitPush(r1);
4197
ricow@chromium.org83aa5492011-02-07 12:42:56 +00004198 // Push the strict mode flag.
4199 frame_->EmitPush(Operand(Smi::FromInt(strict_mode_flag())));
4200
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004201 // Resolve the call.
ricow@chromium.org83aa5492011-02-07 12:42:56 +00004202 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 4);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004203
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004204 // If we generated fast-case code bind the jump-target where fast
4205 // and slow case merge.
4206 if (done.is_linked()) done.Bind();
4207
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004208 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004209 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004210 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
4211
4212 // Call the function.
4213 CodeForSourcePosition(node->position());
4214
4215 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004216 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004217 frame_->CallStub(&call_function, arg_count + 1);
4218
4219 __ ldr(cp, frame_->Context());
4220 // Remove the function from the stack.
4221 frame_->Drop();
4222 frame_->EmitPush(r0);
4223
4224 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004225 // ----------------------------------
4226 // JavaScript example: 'foo(1, 2, 3)' // foo is global
4227 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004228 // Pass the global object as the receiver and let the IC stub
4229 // patch the stack to use the global proxy as 'this' in the
4230 // invoked function.
4231 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004232
4233 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004234 int arg_count = args->length();
4235 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004236 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004237 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004238
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004239 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +00004240 // Setup the name register and call the IC initialization code.
4241 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004242 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004243 Handle<Code> stub = StubCache::ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004244 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004245 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
4246 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004247 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004248 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004249
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004250 } else if (var != NULL && var->AsSlot() != NULL &&
4251 var->AsSlot()->type() == Slot::LOOKUP) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004252 // ----------------------------------
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004253 // JavaScript examples:
4254 //
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004255 // with (obj) foo(1, 2, 3) // foo may be in obj.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004256 //
4257 // function f() {};
4258 // function g() {
4259 // eval(...);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004260 // f(); // f could be in extension object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004261 // }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004262 // ----------------------------------
4263
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004264 JumpTarget slow, done;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004265
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004266 // Generate fast case for loading functions from slots that
4267 // correspond to local/global variables or arguments unless they
4268 // are shadowed by eval-introduced bindings.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004269 EmitDynamicLoadFromSlotFastCase(var->AsSlot(),
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004270 NOT_INSIDE_TYPEOF,
4271 &slow,
4272 &done);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004273
4274 slow.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004275 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004276 frame_->EmitPush(cp);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004277 frame_->EmitPush(Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004278 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004279 // r0: slot value; r1: receiver
4280
4281 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004282 frame_->EmitPush(r0); // function
4283 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004284
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004285 // If fast case code has been generated, emit code to push the
4286 // function and receiver and have the slow path jump around this
4287 // code.
4288 if (done.is_linked()) {
4289 JumpTarget call;
4290 call.Jump();
4291 done.Bind();
4292 frame_->EmitPush(r0); // function
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004293 LoadGlobalReceiver(VirtualFrame::scratch0()); // receiver
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004294 call.Bind();
4295 }
4296
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004297 // Call the function. At this point, everything is spilled but the
4298 // function and receiver are in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004299 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004300 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004301
4302 } else if (property != NULL) {
4303 // Check if the key is a literal string.
4304 Literal* literal = property->key()->AsLiteral();
4305
4306 if (literal != NULL && literal->handle()->IsSymbol()) {
4307 // ------------------------------------------------------------------
4308 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
4309 // ------------------------------------------------------------------
4310
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004311 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004312
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004313 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
4314 name->IsEqualTo(CStrVector("apply")) &&
4315 args->length() == 2 &&
4316 args->at(1)->AsVariableProxy() != NULL &&
4317 args->at(1)->AsVariableProxy()->IsArguments()) {
4318 // Use the optimized Function.prototype.apply that avoids
4319 // allocating lazily allocated arguments objects.
4320 CallApplyLazy(property->obj(),
4321 args->at(0),
4322 args->at(1)->AsVariableProxy(),
4323 node->position());
4324
4325 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004326 Load(property->obj()); // Receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004327 // Load the arguments.
4328 int arg_count = args->length();
4329 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004330 Load(args->at(i));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004331 }
4332
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004333 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004334 // Set the name register and call the IC initialization code.
4335 __ mov(r2, Operand(name));
4336 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004337 Handle<Code> stub =
4338 StubCache::ComputeCallInitialize(arg_count, in_loop);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004339 CodeForSourcePosition(node->position());
4340 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4341 __ ldr(cp, frame_->Context());
4342 frame_->EmitPush(r0);
4343 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004344
4345 } else {
4346 // -------------------------------------------
4347 // JavaScript example: 'array[index](1, 2, 3)'
4348 // -------------------------------------------
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004349
4350 // Load the receiver and name of the function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004351 Load(property->obj());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004352 Load(property->key());
4353
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004354 if (property->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004355 EmitKeyedLoad();
4356 // Put the function below the receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004357 // Use the global receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004358 frame_->EmitPush(r0); // Function.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004359 LoadGlobalReceiver(VirtualFrame::scratch0());
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004360 // Call the function.
4361 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
4362 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004363 } else {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004364 // Swap the name of the function and the receiver on the stack to follow
4365 // the calling convention for call ICs.
4366 Register key = frame_->PopToRegister();
4367 Register receiver = frame_->PopToRegister(key);
4368 frame_->EmitPush(key);
4369 frame_->EmitPush(receiver);
4370
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004371 // Load the arguments.
4372 int arg_count = args->length();
4373 for (int i = 0; i < arg_count; i++) {
4374 Load(args->at(i));
4375 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004376
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004377 // Load the key into r2 and call the IC initialization code.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004378 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004379 Handle<Code> stub =
4380 StubCache::ComputeKeyedCallInitialize(arg_count, in_loop);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004381 CodeForSourcePosition(node->position());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004382 frame_->SpillAll();
4383 __ ldr(r2, frame_->ElementAt(arg_count + 1));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004384 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004385 frame_->Drop(); // Drop the key still on the stack.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004386 __ ldr(cp, frame_->Context());
4387 frame_->EmitPush(r0);
4388 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004389 }
4390
4391 } else {
4392 // ----------------------------------
4393 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
4394 // ----------------------------------
4395
4396 // Load the function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004397 Load(function);
4398
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004399 // Pass the global proxy as the receiver.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004400 LoadGlobalReceiver(VirtualFrame::scratch0());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004401
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004402 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004403 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004404 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004405 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004406 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004407}
4408
4409
ager@chromium.org7c537e22008-10-16 08:43:32 +00004410void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004411#ifdef DEBUG
4412 int original_height = frame_->height();
4413#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004414 Comment cmnt(masm_, "[ CallNew");
4415
4416 // According to ECMA-262, section 11.2.2, page 44, the function
4417 // expression in new calls must be evaluated before the
4418 // arguments. This is different from ordinary calls, where the
4419 // actual function to call is resolved after the arguments have been
4420 // evaluated.
4421
ricow@chromium.org65fae842010-08-25 15:26:24 +00004422 // Push constructor on the stack. If it's not a function it's used as
4423 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
4424 // ignored.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004425 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004426
4427 // Push the arguments ("left-to-right") on the stack.
4428 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004429 int arg_count = args->length();
4430 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004431 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004432 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004433
ricow@chromium.org65fae842010-08-25 15:26:24 +00004434 // Spill everything from here to simplify the implementation.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004435 VirtualFrame::SpilledScope spilled_scope(frame_);
4436
ricow@chromium.org65fae842010-08-25 15:26:24 +00004437 // Load the argument count into r0 and the function into r1 as per
4438 // calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004439 __ mov(r0, Operand(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004440 __ ldr(r1, frame_->ElementAt(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004441
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004442 // Call the construct call builtin that handles allocation and
4443 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004444 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004445 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004446 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004447 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004448
ager@chromium.orgac091b72010-05-05 07:34:42 +00004449 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004450}
4451
4452
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004453void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004454 Register scratch = VirtualFrame::scratch0();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004455 JumpTarget null, function, leave, non_function_constructor;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004456
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004457 // Load the object into register.
4458 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004459 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004460 Register tos = frame_->PopToRegister();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004461
4462 // If the object is a smi, we return null.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004463 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004464 null.Branch(eq);
4465
4466 // Check that the object is a JS object but take special care of JS
4467 // functions to make sure they have 'Function' as their class.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004468 __ CompareObjectType(tos, tos, scratch, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004469 null.Branch(lt);
4470
4471 // As long as JS_FUNCTION_TYPE is the last instance type and it is
4472 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
4473 // LAST_JS_OBJECT_TYPE.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00004474 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4475 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004476 __ cmp(scratch, Operand(JS_FUNCTION_TYPE));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004477 function.Branch(eq);
4478
4479 // Check if the constructor in the map is a function.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004480 __ ldr(tos, FieldMemOperand(tos, Map::kConstructorOffset));
4481 __ CompareObjectType(tos, scratch, scratch, JS_FUNCTION_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004482 non_function_constructor.Branch(ne);
4483
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004484 // The tos register now contains the constructor function. Grab the
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004485 // instance class name from there.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004486 __ ldr(tos, FieldMemOperand(tos, JSFunction::kSharedFunctionInfoOffset));
4487 __ ldr(tos,
4488 FieldMemOperand(tos, SharedFunctionInfo::kInstanceClassNameOffset));
4489 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004490 leave.Jump();
4491
4492 // Functions have class 'Function'.
4493 function.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004494 __ mov(tos, Operand(Factory::function_class_symbol()));
4495 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004496 leave.Jump();
4497
4498 // Objects with a non-function constructor have class 'Object'.
4499 non_function_constructor.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004500 __ mov(tos, Operand(Factory::Object_symbol()));
4501 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004502 leave.Jump();
4503
4504 // Non-JS objects have class null.
4505 null.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004506 __ LoadRoot(tos, Heap::kNullValueRootIndex);
4507 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004508
4509 // All done.
4510 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004511}
4512
4513
ager@chromium.org7c537e22008-10-16 08:43:32 +00004514void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004515 Register scratch = VirtualFrame::scratch0();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004516 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004517
4518 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004519 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004520 Register tos = frame_->PopToRegister(); // tos contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00004521 // if (object->IsSmi()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004522 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004523 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004524 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004525 __ CompareObjectType(tos, scratch, scratch, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004526 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004527 // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004528 __ ldr(tos, FieldMemOperand(tos, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004529 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004530 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004531}
4532
4533
ager@chromium.org7c537e22008-10-16 08:43:32 +00004534void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004535 Register scratch1 = VirtualFrame::scratch0();
4536 Register scratch2 = VirtualFrame::scratch1();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004537 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004538
4539 ASSERT(args->length() == 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004540 Load(args->at(0)); // Load the object.
4541 Load(args->at(1)); // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004542 Register value = frame_->PopToRegister();
4543 Register object = frame_->PopToRegister(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004544 // if (object->IsSmi()) return object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004545 __ tst(object, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004546 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004547 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004548 __ CompareObjectType(object, scratch1, scratch1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004549 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004550 // Store the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004551 __ str(value, FieldMemOperand(object, JSValue::kValueOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004552 // Update the write barrier.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004553 __ RecordWrite(object,
4554 Operand(JSValue::kValueOffset - kHeapObjectTag),
4555 scratch1,
4556 scratch2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004557 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004558 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004559 frame_->EmitPush(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004560}
4561
4562
ager@chromium.org7c537e22008-10-16 08:43:32 +00004563void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004564 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004565 Load(args->at(0));
4566 Register reg = frame_->PopToRegister();
4567 __ tst(reg, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004568 cc_reg_ = eq;
4569}
4570
4571
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004572void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
4573 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
4574 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004575#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004576 if (ShouldGenerateLog(args->at(0))) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004577 Load(args->at(1));
4578 Load(args->at(2));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004579 frame_->CallRuntime(Runtime::kLog, 2);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004580 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004581#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004582 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004583}
4584
4585
ager@chromium.org7c537e22008-10-16 08:43:32 +00004586void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004587 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004588 Load(args->at(0));
4589 Register reg = frame_->PopToRegister();
4590 __ tst(reg, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004591 cc_reg_ = eq;
4592}
4593
4594
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004595// Generates the Math.pow method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004596void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
4597 ASSERT(args->length() == 2);
4598 Load(args->at(0));
4599 Load(args->at(1));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004600
4601 if (!CpuFeatures::IsSupported(VFP3)) {
4602 frame_->CallRuntime(Runtime::kMath_pow, 2);
4603 frame_->EmitPush(r0);
4604 } else {
4605 CpuFeatures::Scope scope(VFP3);
4606 JumpTarget runtime, done;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004607 Label exponent_nonsmi, base_nonsmi, powi, not_minus_half, allocate_return;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004608
4609 Register scratch1 = VirtualFrame::scratch0();
4610 Register scratch2 = VirtualFrame::scratch1();
4611
4612 // Get base and exponent to registers.
4613 Register exponent = frame_->PopToRegister();
4614 Register base = frame_->PopToRegister(exponent);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004615 Register heap_number_map = no_reg;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004616
4617 // Set the frame for the runtime jump target. The code below jumps to the
4618 // jump target label so the frame needs to be established before that.
4619 ASSERT(runtime.entry_frame() == NULL);
4620 runtime.set_entry_frame(frame_);
4621
ager@chromium.org378b34e2011-01-28 08:04:38 +00004622 __ JumpIfNotSmi(exponent, &exponent_nonsmi);
4623 __ JumpIfNotSmi(base, &base_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004624
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004625 heap_number_map = r6;
4626 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4627
4628 // Exponent is a smi and base is a smi. Get the smi value into vfp register
4629 // d1.
4630 __ SmiToDoubleVFPRegister(base, d1, scratch1, s0);
4631 __ b(&powi);
4632
4633 __ bind(&base_nonsmi);
4634 // Exponent is smi and base is non smi. Get the double value from the base
4635 // into vfp register d1.
4636 __ ObjectToDoubleVFPRegister(base, d1,
4637 scratch1, scratch2, heap_number_map, s0,
4638 runtime.entry_label());
4639
4640 __ bind(&powi);
4641
4642 // Load 1.0 into d0.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004643 __ vmov(d0, 1.0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004644
4645 // Get the absolute untagged value of the exponent and use that for the
4646 // calculation.
4647 __ mov(scratch1, Operand(exponent, ASR, kSmiTagSize), SetCC);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00004648 // Negate if negative.
4649 __ rsb(scratch1, scratch1, Operand(0, RelocInfo::NONE), LeaveCC, mi);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004650 __ vmov(d2, d0, mi); // 1.0 needed in d2 later if exponent is negative.
4651
4652 // Run through all the bits in the exponent. The result is calculated in d0
4653 // and d1 holds base^(bit^2).
4654 Label more_bits;
4655 __ bind(&more_bits);
4656 __ mov(scratch1, Operand(scratch1, LSR, 1), SetCC);
4657 __ vmul(d0, d0, d1, cs); // Multiply with base^(bit^2) if bit is set.
4658 __ vmul(d1, d1, d1, ne); // Don't bother calculating next d1 if done.
4659 __ b(ne, &more_bits);
4660
4661 // If exponent is positive we are done.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00004662 __ cmp(exponent, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004663 __ b(ge, &allocate_return);
4664
4665 // If exponent is negative result is 1/result (d2 already holds 1.0 in that
4666 // case). However if d0 has reached infinity this will not provide the
4667 // correct result, so call runtime if that is the case.
4668 __ mov(scratch2, Operand(0x7FF00000));
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00004669 __ mov(scratch1, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004670 __ vmov(d1, scratch1, scratch2); // Load infinity into d1.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004671 __ VFPCompareAndSetFlags(d0, d1);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004672 runtime.Branch(eq); // d0 reached infinity.
4673 __ vdiv(d0, d2, d0);
4674 __ b(&allocate_return);
4675
4676 __ bind(&exponent_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004677 // Special handling of raising to the power of -0.5 and 0.5. First check
4678 // that the value is a heap number and that the lower bits (which for both
4679 // values are zero).
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004680 heap_number_map = r6;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004681 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4682 __ ldr(scratch1, FieldMemOperand(exponent, HeapObject::kMapOffset));
4683 __ ldr(scratch2, FieldMemOperand(exponent, HeapNumber::kMantissaOffset));
4684 __ cmp(scratch1, heap_number_map);
4685 runtime.Branch(ne);
4686 __ tst(scratch2, scratch2);
4687 runtime.Branch(ne);
4688
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004689 // Load the higher bits (which contains the floating point exponent).
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004690 __ ldr(scratch1, FieldMemOperand(exponent, HeapNumber::kExponentOffset));
4691
4692 // Compare exponent with -0.5.
4693 __ cmp(scratch1, Operand(0xbfe00000));
4694 __ b(ne, &not_minus_half);
4695
4696 // Get the double value from the base into vfp register d0.
4697 __ ObjectToDoubleVFPRegister(base, d0,
4698 scratch1, scratch2, heap_number_map, s0,
4699 runtime.entry_label(),
4700 AVOID_NANS_AND_INFINITIES);
4701
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00004702 // Convert -0 into +0 by adding +0.
4703 __ vmov(d2, 0.0);
4704 __ vadd(d0, d2, d0);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004705 // Load 1.0 into d2.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004706 __ vmov(d2, 1.0);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004707
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00004708 // Calculate the reciprocal of the square root.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004709 __ vsqrt(d0, d0);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00004710 __ vdiv(d0, d2, d0);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004711
4712 __ b(&allocate_return);
4713
4714 __ bind(&not_minus_half);
4715 // Compare exponent with 0.5.
4716 __ cmp(scratch1, Operand(0x3fe00000));
4717 runtime.Branch(ne);
4718
4719 // Get the double value from the base into vfp register d0.
4720 __ ObjectToDoubleVFPRegister(base, d0,
4721 scratch1, scratch2, heap_number_map, s0,
4722 runtime.entry_label(),
4723 AVOID_NANS_AND_INFINITIES);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00004724 // Convert -0 into +0 by adding +0.
4725 __ vmov(d2, 0.0);
4726 __ vadd(d0, d2, d0);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004727 __ vsqrt(d0, d0);
4728
4729 __ bind(&allocate_return);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004730 Register scratch3 = r5;
4731 __ AllocateHeapNumberWithValue(scratch3, d0, scratch1, scratch2,
4732 heap_number_map, runtime.entry_label());
4733 __ mov(base, scratch3);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004734 done.Jump();
4735
4736 runtime.Bind();
4737
4738 // Push back the arguments again for the runtime call.
4739 frame_->EmitPush(base);
4740 frame_->EmitPush(exponent);
4741 frame_->CallRuntime(Runtime::kMath_pow, 2);
4742 __ Move(base, r0);
4743
4744 done.Bind();
4745 frame_->EmitPush(base);
4746 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004747}
4748
4749
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004750// Generates the Math.sqrt method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004751void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
4752 ASSERT(args->length() == 1);
4753 Load(args->at(0));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004754
4755 if (!CpuFeatures::IsSupported(VFP3)) {
4756 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4757 frame_->EmitPush(r0);
4758 } else {
4759 CpuFeatures::Scope scope(VFP3);
4760 JumpTarget runtime, done;
4761
4762 Register scratch1 = VirtualFrame::scratch0();
4763 Register scratch2 = VirtualFrame::scratch1();
4764
4765 // Get the value from the frame.
4766 Register tos = frame_->PopToRegister();
4767
4768 // Set the frame for the runtime jump target. The code below jumps to the
4769 // jump target label so the frame needs to be established before that.
4770 ASSERT(runtime.entry_frame() == NULL);
4771 runtime.set_entry_frame(frame_);
4772
4773 Register heap_number_map = r6;
lrn@chromium.org303ada72010-10-27 09:33:13 +00004774 Register new_heap_number = r5;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004775 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4776
4777 // Get the double value from the heap number into vfp register d0.
4778 __ ObjectToDoubleVFPRegister(tos, d0,
4779 scratch1, scratch2, heap_number_map, s0,
4780 runtime.entry_label());
4781
4782 // Calculate the square root of d0 and place result in a heap number object.
4783 __ vsqrt(d0, d0);
lrn@chromium.org303ada72010-10-27 09:33:13 +00004784 __ AllocateHeapNumberWithValue(new_heap_number,
4785 d0,
4786 scratch1, scratch2,
4787 heap_number_map,
4788 runtime.entry_label());
4789 __ mov(tos, Operand(new_heap_number));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004790 done.Jump();
4791
4792 runtime.Bind();
4793 // Push back the argument again for the runtime call.
4794 frame_->EmitPush(tos);
4795 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4796 __ Move(tos, r0);
4797
4798 done.Bind();
4799 frame_->EmitPush(tos);
4800 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004801}
4802
4803
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004804class DeferredStringCharCodeAt : public DeferredCode {
4805 public:
4806 DeferredStringCharCodeAt(Register object,
4807 Register index,
4808 Register scratch,
4809 Register result)
4810 : result_(result),
4811 char_code_at_generator_(object,
4812 index,
4813 scratch,
4814 result,
4815 &need_conversion_,
4816 &need_conversion_,
4817 &index_out_of_range_,
4818 STRING_INDEX_IS_NUMBER) {}
4819
4820 StringCharCodeAtGenerator* fast_case_generator() {
4821 return &char_code_at_generator_;
4822 }
4823
4824 virtual void Generate() {
4825 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4826 char_code_at_generator_.GenerateSlow(masm(), call_helper);
4827
4828 __ bind(&need_conversion_);
4829 // Move the undefined value into the result register, which will
4830 // trigger conversion.
4831 __ LoadRoot(result_, Heap::kUndefinedValueRootIndex);
4832 __ jmp(exit_label());
4833
4834 __ bind(&index_out_of_range_);
4835 // When the index is out of range, the spec requires us to return
4836 // NaN.
4837 __ LoadRoot(result_, Heap::kNanValueRootIndex);
4838 __ jmp(exit_label());
4839 }
4840
4841 private:
4842 Register result_;
4843
4844 Label need_conversion_;
4845 Label index_out_of_range_;
4846
4847 StringCharCodeAtGenerator char_code_at_generator_;
4848};
4849
4850
4851// This generates code that performs a String.prototype.charCodeAt() call
4852// or returns a smi in order to trigger conversion.
4853void CodeGenerator::GenerateStringCharCodeAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004854 Comment(masm_, "[ GenerateStringCharCodeAt");
kasper.lund7276f142008-07-30 08:49:36 +00004855 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004856
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004857 Load(args->at(0));
4858 Load(args->at(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004859
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004860 Register index = frame_->PopToRegister();
4861 Register object = frame_->PopToRegister(index);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004862
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004863 // We need two extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004864 Register scratch = VirtualFrame::scratch0();
4865 Register result = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004866
4867 DeferredStringCharCodeAt* deferred =
4868 new DeferredStringCharCodeAt(object,
4869 index,
4870 scratch,
4871 result);
4872 deferred->fast_case_generator()->GenerateFast(masm_);
4873 deferred->BindExit();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004874 frame_->EmitPush(result);
kasper.lund7276f142008-07-30 08:49:36 +00004875}
4876
4877
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004878class DeferredStringCharFromCode : public DeferredCode {
4879 public:
4880 DeferredStringCharFromCode(Register code,
4881 Register result)
4882 : char_from_code_generator_(code, result) {}
4883
4884 StringCharFromCodeGenerator* fast_case_generator() {
4885 return &char_from_code_generator_;
4886 }
4887
4888 virtual void Generate() {
4889 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4890 char_from_code_generator_.GenerateSlow(masm(), call_helper);
4891 }
4892
4893 private:
4894 StringCharFromCodeGenerator char_from_code_generator_;
4895};
4896
4897
4898// Generates code for creating a one-char string from a char code.
4899void CodeGenerator::GenerateStringCharFromCode(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004900 Comment(masm_, "[ GenerateStringCharFromCode");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004901 ASSERT(args->length() == 1);
4902
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004903 Load(args->at(0));
4904
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004905 Register result = frame_->GetTOSRegister();
4906 Register code = frame_->PopToRegister(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004907
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004908 DeferredStringCharFromCode* deferred = new DeferredStringCharFromCode(
4909 code, result);
4910 deferred->fast_case_generator()->GenerateFast(masm_);
4911 deferred->BindExit();
4912 frame_->EmitPush(result);
4913}
4914
4915
4916class DeferredStringCharAt : public DeferredCode {
4917 public:
4918 DeferredStringCharAt(Register object,
4919 Register index,
4920 Register scratch1,
4921 Register scratch2,
4922 Register result)
4923 : result_(result),
4924 char_at_generator_(object,
4925 index,
4926 scratch1,
4927 scratch2,
4928 result,
4929 &need_conversion_,
4930 &need_conversion_,
4931 &index_out_of_range_,
4932 STRING_INDEX_IS_NUMBER) {}
4933
4934 StringCharAtGenerator* fast_case_generator() {
4935 return &char_at_generator_;
4936 }
4937
4938 virtual void Generate() {
4939 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4940 char_at_generator_.GenerateSlow(masm(), call_helper);
4941
4942 __ bind(&need_conversion_);
4943 // Move smi zero into the result register, which will trigger
4944 // conversion.
4945 __ mov(result_, Operand(Smi::FromInt(0)));
4946 __ jmp(exit_label());
4947
4948 __ bind(&index_out_of_range_);
4949 // When the index is out of range, the spec requires us to return
4950 // the empty string.
4951 __ LoadRoot(result_, Heap::kEmptyStringRootIndex);
4952 __ jmp(exit_label());
4953 }
4954
4955 private:
4956 Register result_;
4957
4958 Label need_conversion_;
4959 Label index_out_of_range_;
4960
4961 StringCharAtGenerator char_at_generator_;
4962};
4963
4964
4965// This generates code that performs a String.prototype.charAt() call
4966// or returns a smi in order to trigger conversion.
4967void CodeGenerator::GenerateStringCharAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004968 Comment(masm_, "[ GenerateStringCharAt");
4969 ASSERT(args->length() == 2);
4970
4971 Load(args->at(0));
4972 Load(args->at(1));
4973
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004974 Register index = frame_->PopToRegister();
4975 Register object = frame_->PopToRegister(index);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004976
4977 // We need three extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004978 Register scratch1 = VirtualFrame::scratch0();
4979 Register scratch2 = VirtualFrame::scratch1();
4980 // Use r6 without notifying the virtual frame.
4981 Register result = r6;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004982
4983 DeferredStringCharAt* deferred =
4984 new DeferredStringCharAt(object,
4985 index,
4986 scratch1,
4987 scratch2,
4988 result);
4989 deferred->fast_case_generator()->GenerateFast(masm_);
4990 deferred->BindExit();
ager@chromium.orgac091b72010-05-05 07:34:42 +00004991 frame_->EmitPush(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004992}
4993
4994
ager@chromium.org7c537e22008-10-16 08:43:32 +00004995void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004996 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004997 Load(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004998 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004999 // We need the CC bits to come out as not_equal in the case where the
5000 // object is a smi. This can't be done with the usual test opcode so
5001 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005002 Register possible_array = frame_->PopToRegister();
5003 Register scratch = VirtualFrame::scratch0();
5004 __ and_(scratch, possible_array, Operand(kSmiTagMask));
5005 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005006 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005007 // It is a heap object - get the map. Check if the object is a JS array.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005008 __ CompareObjectType(possible_array, scratch, scratch, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005009 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005010 cc_reg_ = eq;
5011}
5012
5013
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00005014void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00005015 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005016 Load(args->at(0));
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00005017 JumpTarget answer;
5018 // We need the CC bits to come out as not_equal in the case where the
5019 // object is a smi. This can't be done with the usual test opcode so
5020 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005021 Register possible_regexp = frame_->PopToRegister();
5022 Register scratch = VirtualFrame::scratch0();
5023 __ and_(scratch, possible_regexp, Operand(kSmiTagMask));
5024 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00005025 answer.Branch(ne);
5026 // It is a heap object - get the map. Check if the object is a regexp.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005027 __ CompareObjectType(possible_regexp, scratch, scratch, JS_REGEXP_TYPE);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00005028 answer.Bind();
5029 cc_reg_ = eq;
5030}
5031
5032
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005033void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
5034 // This generates a fast version of:
5035 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005036 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005037 Load(args->at(0));
5038 Register possible_object = frame_->PopToRegister();
5039 __ tst(possible_object, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005040 false_target()->Branch(eq);
5041
5042 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005043 __ cmp(possible_object, ip);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005044 true_target()->Branch(eq);
5045
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005046 Register map_reg = VirtualFrame::scratch0();
5047 __ ldr(map_reg, FieldMemOperand(possible_object, HeapObject::kMapOffset));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005048 // Undetectable objects behave like undefined when tested with typeof.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005049 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kBitFieldOffset));
5050 __ tst(possible_object, Operand(1 << Map::kIsUndetectable));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005051 false_target()->Branch(ne);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005052
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005053 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
5054 __ cmp(possible_object, Operand(FIRST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005055 false_target()->Branch(lt);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005056 __ cmp(possible_object, Operand(LAST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005057 cc_reg_ = le;
5058}
5059
5060
ricow@chromium.org4980dff2010-07-19 08:33:45 +00005061void CodeGenerator::GenerateIsSpecObject(ZoneList<Expression*>* args) {
5062 // This generates a fast version of:
5063 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp' ||
5064 // typeof(arg) == function).
5065 // It includes undetectable objects (as opposed to IsObject).
5066 ASSERT(args->length() == 1);
5067 Load(args->at(0));
5068 Register value = frame_->PopToRegister();
5069 __ tst(value, Operand(kSmiTagMask));
5070 false_target()->Branch(eq);
5071 // Check that this is an object.
5072 __ ldr(value, FieldMemOperand(value, HeapObject::kMapOffset));
5073 __ ldrb(value, FieldMemOperand(value, Map::kInstanceTypeOffset));
5074 __ cmp(value, Operand(FIRST_JS_OBJECT_TYPE));
5075 cc_reg_ = ge;
5076}
5077
5078
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00005079// Deferred code to check whether the String JavaScript object is safe for using
5080// default value of. This code is called after the bit caching this information
5081// in the map has been checked with the map for the object in the map_result_
5082// register. On return the register map_result_ contains 1 for true and 0 for
5083// false.
5084class DeferredIsStringWrapperSafeForDefaultValueOf : public DeferredCode {
5085 public:
5086 DeferredIsStringWrapperSafeForDefaultValueOf(Register object,
5087 Register map_result,
5088 Register scratch1,
5089 Register scratch2)
5090 : object_(object),
5091 map_result_(map_result),
5092 scratch1_(scratch1),
5093 scratch2_(scratch2) { }
5094
5095 virtual void Generate() {
5096 Label false_result;
5097
5098 // Check that map is loaded as expected.
5099 if (FLAG_debug_code) {
5100 __ ldr(ip, FieldMemOperand(object_, HeapObject::kMapOffset));
5101 __ cmp(map_result_, ip);
5102 __ Assert(eq, "Map not in expected register");
5103 }
5104
5105 // Check for fast case object. Generate false result for slow case object.
5106 __ ldr(scratch1_, FieldMemOperand(object_, JSObject::kPropertiesOffset));
5107 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
5108 __ LoadRoot(ip, Heap::kHashTableMapRootIndex);
5109 __ cmp(scratch1_, ip);
5110 __ b(eq, &false_result);
5111
5112 // Look for valueOf symbol in the descriptor array, and indicate false if
5113 // found. The type is not checked, so if it is a transition it is a false
5114 // negative.
5115 __ ldr(map_result_,
5116 FieldMemOperand(map_result_, Map::kInstanceDescriptorsOffset));
5117 __ ldr(scratch2_, FieldMemOperand(map_result_, FixedArray::kLengthOffset));
5118 // map_result_: descriptor array
5119 // scratch2_: length of descriptor array
5120 // Calculate the end of the descriptor array.
5121 STATIC_ASSERT(kSmiTag == 0);
5122 STATIC_ASSERT(kSmiTagSize == 1);
5123 STATIC_ASSERT(kPointerSize == 4);
5124 __ add(scratch1_,
5125 map_result_,
5126 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5127 __ add(scratch1_,
5128 scratch1_,
5129 Operand(scratch2_, LSL, kPointerSizeLog2 - kSmiTagSize));
5130
5131 // Calculate location of the first key name.
5132 __ add(map_result_,
5133 map_result_,
5134 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
5135 DescriptorArray::kFirstIndex * kPointerSize));
5136 // Loop through all the keys in the descriptor array. If one of these is the
5137 // symbol valueOf the result is false.
5138 Label entry, loop;
5139 // The use of ip to store the valueOf symbol asumes that it is not otherwise
5140 // used in the loop below.
5141 __ mov(ip, Operand(Factory::value_of_symbol()));
5142 __ jmp(&entry);
5143 __ bind(&loop);
5144 __ ldr(scratch2_, MemOperand(map_result_, 0));
5145 __ cmp(scratch2_, ip);
5146 __ b(eq, &false_result);
5147 __ add(map_result_, map_result_, Operand(kPointerSize));
5148 __ bind(&entry);
5149 __ cmp(map_result_, Operand(scratch1_));
5150 __ b(ne, &loop);
5151
5152 // Reload map as register map_result_ was used as temporary above.
5153 __ ldr(map_result_, FieldMemOperand(object_, HeapObject::kMapOffset));
5154
5155 // If a valueOf property is not found on the object check that it's
5156 // prototype is the un-modified String prototype. If not result is false.
5157 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kPrototypeOffset));
5158 __ tst(scratch1_, Operand(kSmiTagMask));
5159 __ b(eq, &false_result);
5160 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
5161 __ ldr(scratch2_,
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005162 ContextOperand(cp, Context::GLOBAL_INDEX));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00005163 __ ldr(scratch2_,
5164 FieldMemOperand(scratch2_, GlobalObject::kGlobalContextOffset));
5165 __ ldr(scratch2_,
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005166 ContextOperand(
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00005167 scratch2_, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
5168 __ cmp(scratch1_, scratch2_);
5169 __ b(ne, &false_result);
5170
5171 // Set the bit in the map to indicate that it has been checked safe for
5172 // default valueOf and set true result.
5173 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
5174 __ orr(scratch1_,
5175 scratch1_,
5176 Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
5177 __ str(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
5178 __ mov(map_result_, Operand(1));
5179 __ jmp(exit_label());
5180 __ bind(&false_result);
5181 // Set false result.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00005182 __ mov(map_result_, Operand(0, RelocInfo::NONE));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00005183 }
5184
5185 private:
5186 Register object_;
5187 Register map_result_;
5188 Register scratch1_;
5189 Register scratch2_;
5190};
5191
5192
5193void CodeGenerator::GenerateIsStringWrapperSafeForDefaultValueOf(
5194 ZoneList<Expression*>* args) {
5195 ASSERT(args->length() == 1);
5196 Load(args->at(0));
5197 Register obj = frame_->PopToRegister(); // Pop the string wrapper.
5198 if (FLAG_debug_code) {
5199 __ AbortIfSmi(obj);
5200 }
5201
5202 // Check whether this map has already been checked to be safe for default
5203 // valueOf.
5204 Register map_result = VirtualFrame::scratch0();
5205 __ ldr(map_result, FieldMemOperand(obj, HeapObject::kMapOffset));
5206 __ ldrb(ip, FieldMemOperand(map_result, Map::kBitField2Offset));
5207 __ tst(ip, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
5208 true_target()->Branch(ne);
5209
5210 // We need an additional two scratch registers for the deferred code.
5211 Register scratch1 = VirtualFrame::scratch1();
5212 // Use r6 without notifying the virtual frame.
5213 Register scratch2 = r6;
5214
5215 DeferredIsStringWrapperSafeForDefaultValueOf* deferred =
5216 new DeferredIsStringWrapperSafeForDefaultValueOf(
5217 obj, map_result, scratch1, scratch2);
5218 deferred->Branch(eq);
5219 deferred->BindExit();
5220 __ tst(map_result, Operand(map_result));
5221 cc_reg_ = ne;
5222}
5223
5224
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005225void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
5226 // This generates a fast version of:
5227 // (%_ClassOf(arg) === 'Function')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005228 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005229 Load(args->at(0));
5230 Register possible_function = frame_->PopToRegister();
5231 __ tst(possible_function, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005232 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005233 Register map_reg = VirtualFrame::scratch0();
5234 Register scratch = VirtualFrame::scratch1();
5235 __ CompareObjectType(possible_function, map_reg, scratch, JS_FUNCTION_TYPE);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005236 cc_reg_ = eq;
5237}
5238
5239
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005240void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005241 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005242 Load(args->at(0));
5243 Register possible_undetectable = frame_->PopToRegister();
5244 __ tst(possible_undetectable, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005245 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005246 Register scratch = VirtualFrame::scratch0();
5247 __ ldr(scratch,
5248 FieldMemOperand(possible_undetectable, HeapObject::kMapOffset));
5249 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5250 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005251 cc_reg_ = ne;
5252}
5253
5254
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005255void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005256 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005257
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005258 Register scratch0 = VirtualFrame::scratch0();
5259 Register scratch1 = VirtualFrame::scratch1();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005260 // Get the frame pointer for the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005261 __ ldr(scratch0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005262
5263 // Skip the arguments adaptor frame if it exists.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005264 __ ldr(scratch1,
5265 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5266 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5267 __ ldr(scratch0,
5268 MemOperand(scratch0, StandardFrameConstants::kCallerFPOffset), eq);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005269
5270 // Check the marker in the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005271 __ ldr(scratch1,
5272 MemOperand(scratch0, StandardFrameConstants::kMarkerOffset));
5273 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005274 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005275}
5276
5277
ager@chromium.org7c537e22008-10-16 08:43:32 +00005278void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005279 ASSERT(args->length() == 0);
5280
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005281 Register tos = frame_->GetTOSRegister();
5282 Register scratch0 = VirtualFrame::scratch0();
5283 Register scratch1 = VirtualFrame::scratch1();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005284
lrn@chromium.org25156de2010-04-06 13:10:27 +00005285 // Check if the calling frame is an arguments adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005286 __ ldr(scratch0,
5287 MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5288 __ ldr(scratch1,
5289 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5290 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5291
5292 // Get the number of formal parameters.
5293 __ mov(tos, Operand(Smi::FromInt(scope()->num_parameters())), LeaveCC, ne);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005294
5295 // Arguments adaptor case: Read the arguments length from the
5296 // adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005297 __ ldr(tos,
5298 MemOperand(scratch0, ArgumentsAdaptorFrameConstants::kLengthOffset),
5299 eq);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005300
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005301 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005302}
5303
5304
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00005305void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005306 ASSERT(args->length() == 1);
5307
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00005308 // Satisfy contract with ArgumentsAccessStub:
5309 // Load the key into r1 and the formal parameters count into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005310 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005311 frame_->PopToR1();
5312 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005313 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005314
5315 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005316 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005317 frame_->CallStub(&stub, 0);
5318 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005319}
5320
5321
ager@chromium.org357bf652010-04-12 11:30:10 +00005322void CodeGenerator::GenerateRandomHeapNumber(
5323 ZoneList<Expression*>* args) {
5324 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005325 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00005326
5327 Label slow_allocate_heapnumber;
5328 Label heapnumber_allocated;
5329
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005330 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
5331 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00005332 __ jmp(&heapnumber_allocated);
5333
5334 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00005335 // Allocate a heap number.
5336 __ CallRuntime(Runtime::kNumberAlloc, 0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005337 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00005338
5339 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005340
5341 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
5342 // by computing:
5343 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
5344 if (CpuFeatures::IsSupported(VFP3)) {
5345 __ PrepareCallCFunction(0, r1);
5346 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
5347
5348 CpuFeatures::Scope scope(VFP3);
5349 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
5350 // Create this constant using mov/orr to avoid PC relative load.
5351 __ mov(r1, Operand(0x41000000));
5352 __ orr(r1, r1, Operand(0x300000));
5353 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
5354 __ vmov(d7, r0, r1);
5355 // Move 0x4130000000000000 to VFP.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00005356 __ mov(r0, Operand(0, RelocInfo::NONE));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005357 __ vmov(d8, r0, r1);
5358 // Subtract and store the result in the heap number.
5359 __ vsub(d7, d7, d8);
5360 __ sub(r0, r4, Operand(kHeapObjectTag));
5361 __ vstr(d7, r0, HeapNumber::kValueOffset);
5362 frame_->EmitPush(r4);
5363 } else {
5364 __ mov(r0, Operand(r4));
5365 __ PrepareCallCFunction(1, r1);
5366 __ CallCFunction(
5367 ExternalReference::fill_heap_number_with_random_function(), 1);
5368 frame_->EmitPush(r0);
5369 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005370}
5371
5372
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005373void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
5374 ASSERT_EQ(2, args->length());
5375
5376 Load(args->at(0));
5377 Load(args->at(1));
5378
ager@chromium.org5c838252010-02-19 08:53:10 +00005379 StringAddStub stub(NO_STRING_ADD_FLAGS);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005380 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005381 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005382 frame_->EmitPush(r0);
5383}
5384
5385
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005386void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
5387 ASSERT_EQ(3, args->length());
5388
5389 Load(args->at(0));
5390 Load(args->at(1));
5391 Load(args->at(2));
5392
ager@chromium.org5c838252010-02-19 08:53:10 +00005393 SubStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005394 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005395 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005396 frame_->EmitPush(r0);
5397}
5398
5399
5400void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
5401 ASSERT_EQ(2, args->length());
5402
5403 Load(args->at(0));
5404 Load(args->at(1));
5405
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005406 StringCompareStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005407 frame_->SpillAll();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005408 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005409 frame_->EmitPush(r0);
5410}
5411
5412
5413void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
5414 ASSERT_EQ(4, args->length());
5415
5416 Load(args->at(0));
5417 Load(args->at(1));
5418 Load(args->at(2));
5419 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005420 RegExpExecStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005421 frame_->SpillAll();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005422 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005423 frame_->EmitPush(r0);
5424}
5425
5426
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005427void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005428 ASSERT_EQ(3, args->length());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005429
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005430 Load(args->at(0)); // Size of array, smi.
5431 Load(args->at(1)); // "index" property value.
5432 Load(args->at(2)); // "input" property value.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005433 RegExpConstructResultStub stub;
5434 frame_->SpillAll();
5435 frame_->CallStub(&stub, 3);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005436 frame_->EmitPush(r0);
5437}
5438
5439
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005440class DeferredSearchCache: public DeferredCode {
5441 public:
5442 DeferredSearchCache(Register dst, Register cache, Register key)
5443 : dst_(dst), cache_(cache), key_(key) {
5444 set_comment("[ DeferredSearchCache");
5445 }
5446
5447 virtual void Generate();
5448
5449 private:
5450 Register dst_, cache_, key_;
5451};
5452
5453
5454void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005455 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005456 __ CallRuntime(Runtime::kGetFromCache, 2);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005457 __ Move(dst_, r0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005458}
5459
5460
5461void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
5462 ASSERT_EQ(2, args->length());
5463
5464 ASSERT_NE(NULL, args->at(0)->AsLiteral());
5465 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
5466
5467 Handle<FixedArray> jsfunction_result_caches(
5468 Top::global_context()->jsfunction_result_caches());
5469 if (jsfunction_result_caches->length() <= cache_id) {
5470 __ Abort("Attempt to use undefined cache.");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005471 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005472 return;
5473 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005474
5475 Load(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005476
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005477 frame_->PopToR1();
5478 frame_->SpillAll();
5479 Register key = r1; // Just poped to r1
5480 Register result = r0; // Free, as frame has just been spilled.
5481 Register scratch1 = VirtualFrame::scratch0();
5482 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005483
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005484 __ ldr(scratch1, ContextOperand(cp, Context::GLOBAL_INDEX));
5485 __ ldr(scratch1,
5486 FieldMemOperand(scratch1, GlobalObject::kGlobalContextOffset));
5487 __ ldr(scratch1,
5488 ContextOperand(scratch1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
5489 __ ldr(scratch1,
5490 FieldMemOperand(scratch1, FixedArray::OffsetOfElementAt(cache_id)));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005491
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005492 DeferredSearchCache* deferred =
5493 new DeferredSearchCache(result, scratch1, key);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005494
5495 const int kFingerOffset =
5496 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00005497 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005498 __ ldr(result, FieldMemOperand(scratch1, kFingerOffset));
5499 // result now holds finger offset as a smi.
5500 __ add(scratch2, scratch1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5501 // scratch2 now points to the start of fixed array elements.
5502 __ ldr(result,
5503 MemOperand(
5504 scratch2, result, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
5505 // Note side effect of PreIndex: scratch2 now points to the key of the pair.
5506 __ cmp(key, result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005507 deferred->Branch(ne);
5508
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005509 __ ldr(result, MemOperand(scratch2, kPointerSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005510
5511 deferred->BindExit();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005512 frame_->EmitPush(result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005513}
5514
5515
ager@chromium.org5c838252010-02-19 08:53:10 +00005516void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
5517 ASSERT_EQ(args->length(), 1);
5518
5519 // Load the argument on the stack and jump to the runtime.
5520 Load(args->at(0));
5521
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005522 NumberToStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005523 frame_->SpillAll();
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005524 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005525 frame_->EmitPush(r0);
5526}
5527
5528
ager@chromium.orgac091b72010-05-05 07:34:42 +00005529class DeferredSwapElements: public DeferredCode {
5530 public:
5531 DeferredSwapElements(Register object, Register index1, Register index2)
5532 : object_(object), index1_(index1), index2_(index2) {
5533 set_comment("[ DeferredSwapElements");
5534 }
5535
5536 virtual void Generate();
5537
5538 private:
5539 Register object_, index1_, index2_;
5540};
5541
5542
5543void DeferredSwapElements::Generate() {
5544 __ push(object_);
5545 __ push(index1_);
5546 __ push(index2_);
5547 __ CallRuntime(Runtime::kSwapElements, 3);
5548}
5549
5550
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005551void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
5552 Comment cmnt(masm_, "[ GenerateSwapElements");
5553
5554 ASSERT_EQ(3, args->length());
5555
5556 Load(args->at(0));
5557 Load(args->at(1));
5558 Load(args->at(2));
5559
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005560 VirtualFrame::SpilledScope spilled_scope(frame_);
5561
ager@chromium.orgac091b72010-05-05 07:34:42 +00005562 Register index2 = r2;
5563 Register index1 = r1;
5564 Register object = r0;
5565 Register tmp1 = r3;
5566 Register tmp2 = r4;
5567
5568 frame_->EmitPop(index2);
5569 frame_->EmitPop(index1);
5570 frame_->EmitPop(object);
5571
5572 DeferredSwapElements* deferred =
5573 new DeferredSwapElements(object, index1, index2);
5574
5575 // Fetch the map and check if array is in fast case.
5576 // Check that object doesn't require security checks and
5577 // has no indexed interceptor.
5578 __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE);
5579 deferred->Branch(lt);
5580 __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset));
5581 __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
ager@chromium.org378b34e2011-01-28 08:04:38 +00005582 deferred->Branch(ne);
ager@chromium.orgac091b72010-05-05 07:34:42 +00005583
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00005584 // Check the object's elements are in fast case and writable.
ager@chromium.orgac091b72010-05-05 07:34:42 +00005585 __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset));
5586 __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset));
5587 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5588 __ cmp(tmp2, ip);
5589 deferred->Branch(ne);
5590
5591 // Smi-tagging is equivalent to multiplying by 2.
5592 STATIC_ASSERT(kSmiTag == 0);
5593 STATIC_ASSERT(kSmiTagSize == 1);
5594
5595 // Check that both indices are smis.
5596 __ mov(tmp2, index1);
5597 __ orr(tmp2, tmp2, index2);
5598 __ tst(tmp2, Operand(kSmiTagMask));
ager@chromium.org378b34e2011-01-28 08:04:38 +00005599 deferred->Branch(ne);
ager@chromium.orgac091b72010-05-05 07:34:42 +00005600
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005601 // Check that both indices are valid.
5602 __ ldr(tmp2, FieldMemOperand(object, JSArray::kLengthOffset));
5603 __ cmp(tmp2, index1);
5604 __ cmp(tmp2, index2, hi);
5605 deferred->Branch(ls);
5606
ager@chromium.orgac091b72010-05-05 07:34:42 +00005607 // Bring the offsets into the fixed array in tmp1 into index1 and
5608 // index2.
5609 __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5610 __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize));
5611 __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize));
5612
5613 // Swap elements.
5614 Register tmp3 = object;
5615 object = no_reg;
5616 __ ldr(tmp3, MemOperand(tmp1, index1));
5617 __ ldr(tmp2, MemOperand(tmp1, index2));
5618 __ str(tmp3, MemOperand(tmp1, index2));
5619 __ str(tmp2, MemOperand(tmp1, index1));
5620
5621 Label done;
5622 __ InNewSpace(tmp1, tmp2, eq, &done);
5623 // Possible optimization: do a check that both values are Smis
5624 // (or them and test against Smi mask.)
5625
5626 __ mov(tmp2, tmp1);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00005627 __ add(index1, index1, tmp1);
5628 __ add(index2, index2, tmp1);
5629 __ RecordWriteHelper(tmp1, index1, tmp3);
5630 __ RecordWriteHelper(tmp2, index2, tmp3);
ager@chromium.orgac091b72010-05-05 07:34:42 +00005631 __ bind(&done);
5632
5633 deferred->BindExit();
5634 __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex);
5635 frame_->EmitPush(tmp1);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005636}
5637
5638
ager@chromium.org357bf652010-04-12 11:30:10 +00005639void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
5640 Comment cmnt(masm_, "[ GenerateCallFunction");
5641
5642 ASSERT(args->length() >= 2);
5643
5644 int n_args = args->length() - 2; // for receiver and function.
5645 Load(args->at(0)); // receiver
5646 for (int i = 0; i < n_args; i++) {
5647 Load(args->at(i + 1));
5648 }
5649 Load(args->at(n_args + 1)); // function
5650 frame_->CallJSFunction(n_args);
5651 frame_->EmitPush(r0);
5652}
5653
5654
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005655void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
5656 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005657 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005658 if (CpuFeatures::IsSupported(VFP3)) {
5659 TranscendentalCacheStub stub(TranscendentalCache::SIN);
5660 frame_->SpillAllButCopyTOSToR0();
5661 frame_->CallStub(&stub, 1);
5662 } else {
5663 frame_->CallRuntime(Runtime::kMath_sin, 1);
5664 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005665 frame_->EmitPush(r0);
5666}
5667
5668
5669void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
5670 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005671 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005672 if (CpuFeatures::IsSupported(VFP3)) {
5673 TranscendentalCacheStub stub(TranscendentalCache::COS);
5674 frame_->SpillAllButCopyTOSToR0();
5675 frame_->CallStub(&stub, 1);
5676 } else {
5677 frame_->CallRuntime(Runtime::kMath_cos, 1);
5678 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005679 frame_->EmitPush(r0);
5680}
5681
5682
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005683void CodeGenerator::GenerateMathLog(ZoneList<Expression*>* args) {
5684 ASSERT_EQ(args->length(), 1);
5685 Load(args->at(0));
5686 if (CpuFeatures::IsSupported(VFP3)) {
5687 TranscendentalCacheStub stub(TranscendentalCache::LOG);
5688 frame_->SpillAllButCopyTOSToR0();
5689 frame_->CallStub(&stub, 1);
5690 } else {
5691 frame_->CallRuntime(Runtime::kMath_log, 1);
5692 }
5693 frame_->EmitPush(r0);
5694}
5695
5696
ager@chromium.org7c537e22008-10-16 08:43:32 +00005697void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005698 ASSERT(args->length() == 2);
5699
5700 // Load the two objects into registers and perform the comparison.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005701 Load(args->at(0));
5702 Load(args->at(1));
5703 Register lhs = frame_->PopToRegister();
5704 Register rhs = frame_->PopToRegister(lhs);
5705 __ cmp(lhs, rhs);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005706 cc_reg_ = eq;
5707}
5708
5709
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005710void CodeGenerator::GenerateIsRegExpEquivalent(ZoneList<Expression*>* args) {
5711 ASSERT(args->length() == 2);
5712
5713 // Load the two objects into registers and perform the comparison.
5714 Load(args->at(0));
5715 Load(args->at(1));
5716 Register right = frame_->PopToRegister();
5717 Register left = frame_->PopToRegister(right);
5718 Register tmp = frame_->scratch0();
5719 Register tmp2 = frame_->scratch1();
5720
5721 // Jumps to done must have the eq flag set if the test is successful
5722 // and clear if the test has failed.
5723 Label done;
5724
5725 // Fail if either is a non-HeapObject.
5726 __ cmp(left, Operand(right));
5727 __ b(eq, &done);
5728 __ and_(tmp, left, Operand(right));
5729 __ eor(tmp, tmp, Operand(kSmiTagMask));
5730 __ tst(tmp, Operand(kSmiTagMask));
5731 __ b(ne, &done);
5732 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
5733 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
5734 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
5735 __ b(ne, &done);
5736 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
5737 __ cmp(tmp, Operand(tmp2));
5738 __ b(ne, &done);
5739 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
5740 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
5741 __ cmp(tmp, tmp2);
5742 __ bind(&done);
5743 cc_reg_ = eq;
5744}
5745
5746
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00005747void CodeGenerator::GenerateHasCachedArrayIndex(ZoneList<Expression*>* args) {
5748 ASSERT(args->length() == 1);
5749 Load(args->at(0));
5750 Register value = frame_->PopToRegister();
5751 Register tmp = frame_->scratch0();
5752 __ ldr(tmp, FieldMemOperand(value, String::kHashFieldOffset));
5753 __ tst(tmp, Operand(String::kContainsCachedArrayIndexMask));
5754 cc_reg_ = eq;
5755}
5756
5757
5758void CodeGenerator::GenerateGetCachedArrayIndex(ZoneList<Expression*>* args) {
5759 ASSERT(args->length() == 1);
5760 Load(args->at(0));
5761 Register value = frame_->PopToRegister();
5762
5763 __ ldr(value, FieldMemOperand(value, String::kHashFieldOffset));
5764 __ IndexFromHash(value, value);
5765 frame_->EmitPush(value);
5766}
5767
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005768
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00005769void CodeGenerator::GenerateFastAsciiArrayJoin(ZoneList<Expression*>* args) {
5770 ASSERT(args->length() == 2);
5771 Load(args->at(0));
5772 Register value = frame_->PopToRegister();
5773 __ LoadRoot(value, Heap::kUndefinedValueRootIndex);
5774 frame_->EmitPush(value);
5775}
5776
5777
ager@chromium.org7c537e22008-10-16 08:43:32 +00005778void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005779#ifdef DEBUG
5780 int original_height = frame_->height();
5781#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005782 if (CheckForInlineRuntimeCall(node)) {
5783 ASSERT((has_cc() && frame_->height() == original_height) ||
5784 (!has_cc() && frame_->height() == original_height + 1));
5785 return;
5786 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005787
5788 ZoneList<Expression*>* args = node->arguments();
5789 Comment cmnt(masm_, "[ CallRuntime");
5790 Runtime::Function* function = node->function();
5791
ager@chromium.org41826e72009-03-30 13:30:57 +00005792 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00005793 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00005794 // Push the builtins object found in the current global object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005795 Register scratch = VirtualFrame::scratch0();
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005796 __ ldr(scratch, GlobalObjectOperand());
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005797 Register builtins = frame_->GetTOSRegister();
5798 __ ldr(builtins, FieldMemOperand(scratch, GlobalObject::kBuiltinsOffset));
5799 frame_->EmitPush(builtins);
ager@chromium.org41826e72009-03-30 13:30:57 +00005800 }
mads.s.ager31e71382008-08-13 09:32:07 +00005801
ager@chromium.org41826e72009-03-30 13:30:57 +00005802 // Push the arguments ("left-to-right").
5803 int arg_count = args->length();
5804 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005805 Load(args->at(i));
ager@chromium.org41826e72009-03-30 13:30:57 +00005806 }
mads.s.ager31e71382008-08-13 09:32:07 +00005807
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005808 VirtualFrame::SpilledScope spilled_scope(frame_);
5809
ager@chromium.org41826e72009-03-30 13:30:57 +00005810 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005811 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00005812 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005813 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005814 Handle<Code> stub = StubCache::ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005815 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005816 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005817 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00005818 } else {
5819 // Call the C runtime function.
5820 frame_->CallRuntime(function, arg_count);
5821 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005822 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00005823 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005824}
5825
5826
ager@chromium.org7c537e22008-10-16 08:43:32 +00005827void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005828#ifdef DEBUG
5829 int original_height = frame_->height();
5830#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005831 Comment cmnt(masm_, "[ UnaryOperation");
5832
5833 Token::Value op = node->op();
5834
5835 if (op == Token::NOT) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005836 LoadCondition(node->expression(), false_target(), true_target(), true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005837 // LoadCondition may (and usually does) leave a test and branch to
5838 // be emitted by the caller. In that case, negate the condition.
5839 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005840
5841 } else if (op == Token::DELETE) {
5842 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00005843 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005844 if (property != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005845 Load(property->obj());
5846 Load(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005847 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005848 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005849
mads.s.ager31e71382008-08-13 09:32:07 +00005850 } else if (variable != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005851 Slot* slot = variable->AsSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005852 if (variable->is_global()) {
5853 LoadGlobal();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005854 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005855 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005856 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005857
5858 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
ager@chromium.org0ee099b2011-01-25 14:06:47 +00005859 // Delete from the context holding the named variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005860 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005861 frame_->EmitPush(Operand(variable->name()));
ager@chromium.org0ee099b2011-01-25 14:06:47 +00005862 frame_->CallRuntime(Runtime::kDeleteContextSlot, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005863 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005864
mads.s.ager31e71382008-08-13 09:32:07 +00005865 } else {
5866 // Default: Result of deleting non-global, not dynamically
5867 // introduced variables is false.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005868 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00005869 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005870
5871 } else {
5872 // Default: Result of deleting expressions is true.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005873 Load(node->expression()); // may have side-effects
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005874 frame_->Drop();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005875 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005876 }
5877
5878 } else if (op == Token::TYPEOF) {
5879 // Special case for loading the typeof expression; see comment on
5880 // LoadTypeofExpression().
5881 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005882 frame_->CallRuntime(Runtime::kTypeof, 1);
5883 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005884
5885 } else {
ricow@chromium.org65fae842010-08-25 15:26:24 +00005886 bool can_overwrite = node->expression()->ResultOverwriteAllowed();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005887 UnaryOverwriteMode overwrite =
5888 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
5889
5890 bool no_negative_zero = node->expression()->no_negative_zero();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005891 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005892 switch (op) {
5893 case Token::NOT:
5894 case Token::DELETE:
5895 case Token::TYPEOF:
5896 UNREACHABLE(); // handled above
5897 break;
5898
5899 case Token::SUB: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005900 frame_->PopToR0();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005901 GenericUnaryOpStub stub(
5902 Token::SUB,
5903 overwrite,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005904 NO_UNARY_FLAGS,
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005905 no_negative_zero ? kIgnoreNegativeZero : kStrictNegativeZero);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005906 frame_->CallStub(&stub, 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005907 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005908 break;
5909 }
5910
5911 case Token::BIT_NOT: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005912 Register tos = frame_->PopToRegister();
5913 JumpTarget not_smi_label;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005914 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005915 // Smi check.
5916 __ tst(tos, Operand(kSmiTagMask));
5917 not_smi_label.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005918
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005919 __ mvn(tos, Operand(tos));
5920 __ bic(tos, tos, Operand(kSmiTagMask)); // Bit-clear inverted smi-tag.
5921 frame_->EmitPush(tos);
5922 // The fast case is the first to jump to the continue label, so it gets
5923 // to decide the virtual frame layout.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005924 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005925
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005926 not_smi_label.Bind();
5927 frame_->SpillAll();
5928 __ Move(r0, tos);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005929 GenericUnaryOpStub stub(Token::BIT_NOT,
5930 overwrite,
5931 NO_UNARY_SMI_CODE_IN_STUB);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005932 frame_->CallStub(&stub, 0);
5933 frame_->EmitPush(r0);
5934
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005935 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005936 break;
5937 }
5938
5939 case Token::VOID:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005940 frame_->Drop();
5941 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005942 break;
5943
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005944 case Token::ADD: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005945 Register tos = frame_->Peek();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005946 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005947 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005948 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005949 continue_label.Branch(eq);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005950
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005951 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005952 frame_->EmitPush(r0);
5953
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005954 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005955 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005956 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005957 default:
5958 UNREACHABLE();
5959 }
5960 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005961 ASSERT(!has_valid_frame() ||
5962 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005963 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005964}
5965
5966
ager@chromium.orgbeb25712010-11-29 08:02:25 +00005967class DeferredCountOperation: public DeferredCode {
5968 public:
5969 DeferredCountOperation(Register value,
5970 bool is_increment,
5971 bool is_postfix,
5972 int target_size)
5973 : value_(value),
5974 is_increment_(is_increment),
5975 is_postfix_(is_postfix),
5976 target_size_(target_size) {}
5977
5978 virtual void Generate() {
5979 VirtualFrame copied_frame(*frame_state()->frame());
5980
5981 Label slow;
5982 // Check for smi operand.
5983 __ tst(value_, Operand(kSmiTagMask));
5984 __ b(ne, &slow);
5985
5986 // Revert optimistic increment/decrement.
5987 if (is_increment_) {
5988 __ sub(value_, value_, Operand(Smi::FromInt(1)));
5989 } else {
5990 __ add(value_, value_, Operand(Smi::FromInt(1)));
5991 }
5992
5993 // Slow case: Convert to number. At this point the
5994 // value to be incremented is in the value register..
5995 __ bind(&slow);
5996
5997 // Convert the operand to a number.
5998 copied_frame.EmitPush(value_);
5999
6000 copied_frame.InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
6001
6002 if (is_postfix_) {
6003 // Postfix: store to result (on the stack).
6004 __ str(r0, MemOperand(sp, target_size_ * kPointerSize));
6005 }
6006
6007 copied_frame.EmitPush(r0);
6008 copied_frame.EmitPush(Operand(Smi::FromInt(1)));
6009
6010 if (is_increment_) {
6011 copied_frame.CallRuntime(Runtime::kNumberAdd, 2);
6012 } else {
6013 copied_frame.CallRuntime(Runtime::kNumberSub, 2);
6014 }
6015
6016 __ Move(value_, r0);
6017
6018 copied_frame.MergeTo(frame_state()->frame());
6019 }
6020
6021 private:
6022 Register value_;
6023 bool is_increment_;
6024 bool is_postfix_;
6025 int target_size_;
6026};
6027
6028
ager@chromium.org7c537e22008-10-16 08:43:32 +00006029void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006030#ifdef DEBUG
6031 int original_height = frame_->height();
6032#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006033 Comment cmnt(masm_, "[ CountOperation");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006034 VirtualFrame::RegisterAllocationScope scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006035
6036 bool is_postfix = node->is_postfix();
6037 bool is_increment = node->op() == Token::INC;
6038
6039 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
6040 bool is_const = (var != NULL && var->mode() == Variable::CONST);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006041 bool is_slot = (var != NULL && var->mode() == Variable::VAR);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006042
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006043 if (!is_const && is_slot && type_info(var->AsSlot()).IsSmi()) {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006044 // The type info declares that this variable is always a Smi. That
6045 // means it is a Smi both before and after the increment/decrement.
6046 // Lets make use of that to make a very minimal count.
6047 Reference target(this, node->expression(), !is_const);
6048 ASSERT(!target.is_illegal());
6049 target.GetValue(); // Pushes the value.
6050 Register value = frame_->PopToRegister();
6051 if (is_postfix) frame_->EmitPush(value);
6052 if (is_increment) {
6053 __ add(value, value, Operand(Smi::FromInt(1)));
6054 } else {
6055 __ sub(value, value, Operand(Smi::FromInt(1)));
6056 }
6057 frame_->EmitPush(value);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006058 target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006059 if (is_postfix) frame_->Pop();
6060 ASSERT_EQ(original_height + 1, frame_->height());
6061 return;
6062 }
6063
6064 // If it's a postfix expression and its result is not ignored and the
6065 // reference is non-trivial, then push a placeholder on the stack now
6066 // to hold the result of the expression.
6067 bool placeholder_pushed = false;
6068 if (!is_slot && is_postfix) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006069 frame_->EmitPush(Operand(Smi::FromInt(0)));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006070 placeholder_pushed = true;
mads.s.ager31e71382008-08-13 09:32:07 +00006071 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006072
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006073 // A constant reference is not saved to, so a constant reference is not a
6074 // compound assignment reference.
6075 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006076 if (target.is_illegal()) {
6077 // Spoof the virtual frame to have the expected height (one higher
6078 // than on entry).
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006079 if (!placeholder_pushed) frame_->EmitPush(Operand(Smi::FromInt(0)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00006080 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006081 return;
6082 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006083
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006084 // This pushes 0, 1 or 2 words on the object to be used later when updating
6085 // the target. It also pushes the current value of the target.
ager@chromium.org357bf652010-04-12 11:30:10 +00006086 target.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006087
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006088 bool value_is_known_smi = frame_->KnownSmiAt(0);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006089 Register value = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006090
6091 // Postfix: Store the old value as the result.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006092 if (placeholder_pushed) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006093 frame_->SetElementAt(value, target.size());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006094 } else if (is_postfix) {
6095 frame_->EmitPush(value);
6096 __ mov(VirtualFrame::scratch0(), value);
6097 value = VirtualFrame::scratch0();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006098 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006099
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006100 // We can't use any type information here since the virtual frame from the
6101 // deferred code may have lost information and we can't merge a virtual
6102 // frame with less specific type knowledge to a virtual frame with more
6103 // specific knowledge that has already used that specific knowledge to
6104 // generate code.
6105 frame_->ForgetTypeInfo();
6106
6107 // The constructor here will capture the current virtual frame and use it to
6108 // merge to after the deferred code has run. No virtual frame changes are
6109 // allowed from here until the 'BindExit' below.
6110 DeferredCode* deferred =
6111 new DeferredCountOperation(value,
6112 is_increment,
6113 is_postfix,
6114 target.size());
6115 if (!value_is_known_smi) {
6116 // Check for smi operand.
6117 __ tst(value, Operand(kSmiTagMask));
6118
6119 deferred->Branch(ne);
6120 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006121
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006122 // Perform optimistic increment/decrement.
6123 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006124 __ add(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006125 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006126 __ sub(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006127 }
6128
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006129 // If increment/decrement overflows, go to deferred code.
6130 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006131
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006132 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006133
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006134 // Store the new value in the target if not const.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006135 // At this point the answer is in the value register.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006136 frame_->EmitPush(value);
6137 // Set the target with the result, leaving the result on
6138 // top of the stack. Removes the target from the stack if
6139 // it has a non-zero size.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006140 if (!is_const) target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006141 }
6142
6143 // Postfix: Discard the new value and use the old.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006144 if (is_postfix) frame_->Pop();
ager@chromium.orgac091b72010-05-05 07:34:42 +00006145 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006146}
6147
6148
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006149void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006150 // According to ECMA-262 section 11.11, page 58, the binary logical
6151 // operators must yield the result of one of the two expressions
6152 // before any ToBoolean() conversions. This means that the value
6153 // produced by a && or || operator is not necessarily a boolean.
6154
6155 // NOTE: If the left hand side produces a materialized value (not in
6156 // the CC register), we force the right hand side to do the
6157 // same. This is necessary because we may have to branch to the exit
6158 // after evaluating the left hand side (due to the shortcut
6159 // semantics), but the compiler must (statically) know if the result
6160 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006161 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006162 JumpTarget is_true;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006163 LoadCondition(node->left(), &is_true, false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006164 if (has_valid_frame() && !has_cc()) {
6165 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006166 JumpTarget pop_and_continue;
6167 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006168
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006169 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006170 // Avoid popping the result if it converts to 'false' using the
6171 // standard ToBoolean() conversion as described in ECMA-262,
6172 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00006173 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006174 Branch(false, &exit);
6175
6176 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006177 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006178 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006179
6180 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006181 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006182 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006183
6184 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006185 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006186 } else if (has_cc() || is_true.is_linked()) {
6187 // The left-hand side is either (a) partially compiled to
6188 // control flow with a final branch left to emit or (b) fully
6189 // compiled to control flow and possibly true.
6190 if (has_cc()) {
6191 Branch(false, false_target());
6192 }
6193 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006194 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006195 } else {
6196 // Nothing to do.
6197 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006198 }
6199
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006200 } else {
6201 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006202 JumpTarget is_false;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006203 LoadCondition(node->left(), true_target(), &is_false, false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006204 if (has_valid_frame() && !has_cc()) {
6205 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006206 JumpTarget pop_and_continue;
6207 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006208
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006209 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006210 // Avoid popping the result if it converts to 'true' using the
6211 // standard ToBoolean() conversion as described in ECMA-262,
6212 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00006213 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006214 Branch(true, &exit);
6215
6216 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006217 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006218 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006219
6220 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006221 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006222 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006223
6224 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006225 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006226 } else if (has_cc() || is_false.is_linked()) {
6227 // The left-hand side is either (a) partially compiled to
6228 // control flow with a final branch left to emit or (b) fully
6229 // compiled to control flow and possibly false.
6230 if (has_cc()) {
6231 Branch(true, true_target());
6232 }
6233 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006234 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006235 } else {
6236 // Nothing to do.
6237 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006238 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006239 }
6240}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006241
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006242
6243void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
6244#ifdef DEBUG
6245 int original_height = frame_->height();
6246#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006247 Comment cmnt(masm_, "[ BinaryOperation");
6248
6249 if (node->op() == Token::AND || node->op() == Token::OR) {
6250 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006251 } else {
6252 // Optimize for the case where (at least) one of the expressions
6253 // is a literal small integer.
6254 Literal* lliteral = node->left()->AsLiteral();
6255 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006256 // NOTE: The code below assumes that the slow cases (calls to runtime)
6257 // never return a constant/immutable object.
ricow@chromium.org65fae842010-08-25 15:26:24 +00006258 bool overwrite_left = node->left()->ResultOverwriteAllowed();
6259 bool overwrite_right = node->right()->ResultOverwriteAllowed();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006260
6261 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006262 VirtualFrame::RegisterAllocationScope scope(this);
6263 Load(node->left());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006264 if (frame_->KnownSmiAt(0)) overwrite_left = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006265 SmiOperation(node->op(),
6266 rliteral->handle(),
6267 false,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006268 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006269 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006270 VirtualFrame::RegisterAllocationScope scope(this);
6271 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006272 if (frame_->KnownSmiAt(0)) overwrite_right = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006273 SmiOperation(node->op(),
6274 lliteral->handle(),
6275 true,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006276 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006277 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006278 GenerateInlineSmi inline_smi =
6279 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
6280 if (lliteral != NULL) {
6281 ASSERT(!lliteral->handle()->IsSmi());
6282 inline_smi = DONT_GENERATE_INLINE_SMI;
6283 }
6284 if (rliteral != NULL) {
6285 ASSERT(!rliteral->handle()->IsSmi());
6286 inline_smi = DONT_GENERATE_INLINE_SMI;
6287 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006288 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006289 OverwriteMode overwrite_mode = NO_OVERWRITE;
6290 if (overwrite_left) {
6291 overwrite_mode = OVERWRITE_LEFT;
6292 } else if (overwrite_right) {
6293 overwrite_mode = OVERWRITE_RIGHT;
6294 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006295 Load(node->left());
6296 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006297 GenericBinaryOperation(node->op(), overwrite_mode, inline_smi);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006298 }
6299 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006300 ASSERT(!has_valid_frame() ||
6301 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006302 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006303}
6304
6305
ager@chromium.org7c537e22008-10-16 08:43:32 +00006306void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006307#ifdef DEBUG
6308 int original_height = frame_->height();
6309#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006310 frame_->EmitPush(MemOperand(frame_->Function()));
ager@chromium.orgac091b72010-05-05 07:34:42 +00006311 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006312}
6313
6314
ager@chromium.org7c537e22008-10-16 08:43:32 +00006315void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006316#ifdef DEBUG
6317 int original_height = frame_->height();
6318#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006319 Comment cmnt(masm_, "[ CompareOperation");
6320
ager@chromium.org357bf652010-04-12 11:30:10 +00006321 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
6322
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006323 // Get the expressions from the node.
6324 Expression* left = node->left();
6325 Expression* right = node->right();
6326 Token::Value op = node->op();
6327
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006328 // To make typeof testing for natives implemented in JavaScript really
6329 // efficient, we generate special code for expressions of the form:
6330 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006331 UnaryOperation* operation = left->AsUnaryOperation();
6332 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
6333 (operation != NULL && operation->op() == Token::TYPEOF) &&
6334 (right->AsLiteral() != NULL &&
6335 right->AsLiteral()->handle()->IsString())) {
6336 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
6337
ager@chromium.org357bf652010-04-12 11:30:10 +00006338 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006339 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00006340 Register tos = frame_->PopToRegister();
6341
ager@chromium.org357bf652010-04-12 11:30:10 +00006342 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006343
6344 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006345 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006346 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00006347 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006348 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006349 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006350 cc_reg_ = eq;
6351
6352 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006353 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006354 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006355
ager@chromium.org357bf652010-04-12 11:30:10 +00006356 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006357
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006358 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006359 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
6360 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
6361 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006362 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006363
ager@chromium.org357bf652010-04-12 11:30:10 +00006364 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
6365 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006366 cc_reg_ = lt;
6367
6368 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006369 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006370 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006371 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006372 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006373 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006374 cc_reg_ = eq;
6375
6376 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006377 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006378 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006379 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006380
ager@chromium.org357bf652010-04-12 11:30:10 +00006381 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006382 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006383
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006384 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006385 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
6386 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
6387 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
6388 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006389
6390 cc_reg_ = eq;
6391
6392 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006393 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006394 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00006395 Register map_reg = scratch;
6396 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006397 true_target()->Branch(eq);
6398 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00006399 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006400 cc_reg_ = eq;
6401
6402 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006403 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006404 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006405
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006406 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006407 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006408 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006409
ager@chromium.org357bf652010-04-12 11:30:10 +00006410 Register map_reg = scratch;
6411 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006412 false_target()->Branch(eq);
6413
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006414 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006415 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
6416 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6417 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006418 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006419
ager@chromium.org357bf652010-04-12 11:30:10 +00006420 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
6421 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006422 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00006423 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006424 cc_reg_ = le;
6425
6426 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006427 // Uncommon case: typeof testing against a string literal that is
6428 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006429 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006430 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006431 ASSERT(!has_valid_frame() ||
6432 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006433 return;
6434 }
6435
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006436 switch (op) {
6437 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006438 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006439 break;
6440
6441 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006442 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006443 break;
6444
6445 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006446 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006447 break;
6448
6449 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006450 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006451 break;
6452
6453 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006454 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006455 break;
6456
6457 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006458 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006459 break;
6460
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006461 case Token::IN: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006462 Load(left);
6463 Load(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006464 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006465 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006466 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006467 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006468
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006469 case Token::INSTANCEOF: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006470 Load(left);
6471 Load(right);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00006472 InstanceofStub stub(InstanceofStub::kNoFlags);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006473 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006474 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006475 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006476 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006477 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006478 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006479
6480 default:
6481 UNREACHABLE();
6482 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006483 ASSERT((has_cc() && frame_->height() == original_height) ||
6484 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006485}
6486
6487
ricow@chromium.org65fae842010-08-25 15:26:24 +00006488void CodeGenerator::VisitCompareToNull(CompareToNull* node) {
6489#ifdef DEBUG
6490 int original_height = frame_->height();
6491#endif
6492 Comment cmnt(masm_, "[ CompareToNull");
6493
6494 Load(node->expression());
6495 Register tos = frame_->PopToRegister();
6496 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6497 __ cmp(tos, ip);
6498
6499 // The 'null' value is only equal to 'undefined' if using non-strict
6500 // comparisons.
6501 if (!node->is_strict()) {
6502 true_target()->Branch(eq);
6503 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
6504 __ cmp(tos, Operand(ip));
6505 true_target()->Branch(eq);
6506
6507 __ tst(tos, Operand(kSmiTagMask));
6508 false_target()->Branch(eq);
6509
6510 // It can be an undetectable object.
6511 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
6512 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
6513 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6514 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
6515 }
6516
6517 cc_reg_ = eq;
6518 ASSERT(has_cc() && frame_->height() == original_height);
6519}
6520
6521
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006522class DeferredReferenceGetNamedValue: public DeferredCode {
6523 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006524 explicit DeferredReferenceGetNamedValue(Register receiver,
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006525 Handle<String> name,
6526 bool is_contextual)
6527 : receiver_(receiver),
6528 name_(name),
6529 is_contextual_(is_contextual),
6530 is_dont_delete_(false) {
6531 set_comment(is_contextual
6532 ? "[ DeferredReferenceGetNamedValue (contextual)"
6533 : "[ DeferredReferenceGetNamedValue");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006534 }
6535
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006536 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006537
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006538 void set_is_dont_delete(bool value) {
6539 ASSERT(is_contextual_);
6540 is_dont_delete_ = value;
6541 }
6542
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006543 private:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006544 Register receiver_;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006545 Handle<String> name_;
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006546 bool is_contextual_;
6547 bool is_dont_delete_;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006548};
6549
6550
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006551// Convention for this is that on entry the receiver is in a register that
6552// is not used by the stack. On exit the answer is found in that same
6553// register and the stack has the same height.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006554void DeferredReferenceGetNamedValue::Generate() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006555#ifdef DEBUG
6556 int expected_height = frame_state()->frame()->height();
6557#endif
6558 VirtualFrame copied_frame(*frame_state()->frame());
6559 copied_frame.SpillAll();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006560
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006561 Register scratch1 = VirtualFrame::scratch0();
6562 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006563 ASSERT(!receiver_.is(scratch1) && !receiver_.is(scratch2));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006564 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
6565 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
6566
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006567 // Ensure receiver in r0 and name in r2 to match load ic calling convention.
6568 __ Move(r0, receiver_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006569 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006570
6571 // The rest of the instructions in the deferred code must be together.
6572 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6573 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006574 RelocInfo::Mode mode = is_contextual_
6575 ? RelocInfo::CODE_TARGET_CONTEXT
6576 : RelocInfo::CODE_TARGET;
6577 __ Call(ic, mode);
6578 // We must mark the code just after the call with the correct marker.
6579 MacroAssembler::NopMarkerTypes code_marker;
6580 if (is_contextual_) {
6581 code_marker = is_dont_delete_
6582 ? MacroAssembler::PROPERTY_ACCESS_INLINED_CONTEXT_DONT_DELETE
6583 : MacroAssembler::PROPERTY_ACCESS_INLINED_CONTEXT;
6584 } else {
6585 code_marker = MacroAssembler::PROPERTY_ACCESS_INLINED;
6586 }
6587 __ MarkCode(code_marker);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006588
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006589 // At this point the answer is in r0. We move it to the expected register
6590 // if necessary.
6591 __ Move(receiver_, r0);
6592
6593 // Now go back to the frame that we entered with. This will not overwrite
6594 // the receiver register since that register was not in use when we came
6595 // in. The instructions emitted by this merge are skipped over by the
6596 // inline load patching mechanism when looking for the branch instruction
6597 // that tells it where the code to patch is.
6598 copied_frame.MergeTo(frame_state()->frame());
6599
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006600 // Block the constant pool for one more instruction after leaving this
6601 // constant pool block scope to include the branch instruction ending the
6602 // deferred code.
6603 __ BlockConstPoolFor(1);
6604 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006605 ASSERT_EQ(expected_height, frame_state()->frame()->height());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006606}
6607
6608
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006609class DeferredReferenceGetKeyedValue: public DeferredCode {
6610 public:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006611 DeferredReferenceGetKeyedValue(Register key, Register receiver)
6612 : key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006613 set_comment("[ DeferredReferenceGetKeyedValue");
6614 }
6615
6616 virtual void Generate();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006617
6618 private:
6619 Register key_;
6620 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006621};
6622
6623
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006624// Takes key and register in r0 and r1 or vice versa. Returns result
6625// in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006626void DeferredReferenceGetKeyedValue::Generate() {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006627 ASSERT((key_.is(r0) && receiver_.is(r1)) ||
6628 (key_.is(r1) && receiver_.is(r0)));
6629
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006630 VirtualFrame copied_frame(*frame_state()->frame());
6631 copied_frame.SpillAll();
6632
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006633 Register scratch1 = VirtualFrame::scratch0();
6634 Register scratch2 = VirtualFrame::scratch1();
6635 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
6636 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
6637
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006638 // Ensure key in r0 and receiver in r1 to match keyed load ic calling
6639 // convention.
6640 if (key_.is(r1)) {
6641 __ Swap(r0, r1, ip);
6642 }
6643
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006644 // The rest of the instructions in the deferred code must be together.
6645 { Assembler::BlockConstPoolScope block_const_pool(masm_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006646 // Call keyed load IC. It has the arguments key and receiver in r0 and r1.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006647 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
6648 __ Call(ic, RelocInfo::CODE_TARGET);
6649 // The call must be followed by a nop instruction to indicate that the
6650 // keyed load has been inlined.
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006651 __ MarkCode(MacroAssembler::PROPERTY_ACCESS_INLINED);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006652
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006653 // Now go back to the frame that we entered with. This will not overwrite
6654 // the receiver or key registers since they were not in use when we came
6655 // in. The instructions emitted by this merge are skipped over by the
6656 // inline load patching mechanism when looking for the branch instruction
6657 // that tells it where the code to patch is.
6658 copied_frame.MergeTo(frame_state()->frame());
6659
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006660 // Block the constant pool for one more instruction after leaving this
6661 // constant pool block scope to include the branch instruction ending the
6662 // deferred code.
6663 __ BlockConstPoolFor(1);
6664 }
6665}
6666
6667
6668class DeferredReferenceSetKeyedValue: public DeferredCode {
6669 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006670 DeferredReferenceSetKeyedValue(Register value,
6671 Register key,
6672 Register receiver)
6673 : value_(value), key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006674 set_comment("[ DeferredReferenceSetKeyedValue");
6675 }
6676
6677 virtual void Generate();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006678
6679 private:
6680 Register value_;
6681 Register key_;
6682 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006683};
6684
6685
6686void DeferredReferenceSetKeyedValue::Generate() {
6687 Register scratch1 = VirtualFrame::scratch0();
6688 Register scratch2 = VirtualFrame::scratch1();
6689 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
6690 __ IncrementCounter(
6691 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
6692
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006693 // Ensure value in r0, key in r1 and receiver in r2 to match keyed store ic
6694 // calling convention.
6695 if (value_.is(r1)) {
6696 __ Swap(r0, r1, ip);
6697 }
6698 ASSERT(receiver_.is(r2));
6699
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006700 // The rest of the instructions in the deferred code must be together.
6701 { Assembler::BlockConstPoolScope block_const_pool(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006702 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6703 // r1 and r2.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006704 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
6705 __ Call(ic, RelocInfo::CODE_TARGET);
6706 // The call must be followed by a nop instruction to indicate that the
6707 // keyed store has been inlined.
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006708 __ MarkCode(MacroAssembler::PROPERTY_ACCESS_INLINED);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006709
6710 // Block the constant pool for one more instruction after leaving this
6711 // constant pool block scope to include the branch instruction ending the
6712 // deferred code.
6713 __ BlockConstPoolFor(1);
6714 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006715}
6716
6717
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006718class DeferredReferenceSetNamedValue: public DeferredCode {
6719 public:
6720 DeferredReferenceSetNamedValue(Register value,
6721 Register receiver,
6722 Handle<String> name)
6723 : value_(value), receiver_(receiver), name_(name) {
6724 set_comment("[ DeferredReferenceSetNamedValue");
6725 }
6726
6727 virtual void Generate();
6728
6729 private:
6730 Register value_;
6731 Register receiver_;
6732 Handle<String> name_;
6733};
6734
6735
6736// Takes value in r0, receiver in r1 and returns the result (the
6737// value) in r0.
6738void DeferredReferenceSetNamedValue::Generate() {
6739 // Record the entry frame and spill.
6740 VirtualFrame copied_frame(*frame_state()->frame());
6741 copied_frame.SpillAll();
6742
6743 // Ensure value in r0, receiver in r1 to match store ic calling
6744 // convention.
6745 ASSERT(value_.is(r0) && receiver_.is(r1));
6746 __ mov(r2, Operand(name_));
6747
6748 // The rest of the instructions in the deferred code must be together.
6749 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6750 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6751 // r1 and r2.
6752 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
6753 __ Call(ic, RelocInfo::CODE_TARGET);
6754 // The call must be followed by a nop instruction to indicate that the
6755 // named store has been inlined.
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006756 __ MarkCode(MacroAssembler::PROPERTY_ACCESS_INLINED);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006757
6758 // Go back to the frame we entered with. The instructions
6759 // generated by this merge are skipped over by the inline store
6760 // patching mechanism when looking for the branch instruction that
6761 // tells it where the code to patch is.
6762 copied_frame.MergeTo(frame_state()->frame());
6763
6764 // Block the constant pool for one more instruction after leaving this
6765 // constant pool block scope to include the branch instruction ending the
6766 // deferred code.
6767 __ BlockConstPoolFor(1);
6768 }
6769}
6770
6771
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006772// Consumes the top of stack (the receiver) and pushes the result instead.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006773void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006774 bool contextual_load_in_builtin =
6775 is_contextual &&
6776 (Bootstrapper::IsActive() ||
6777 (!info_->closure().is_null() && info_->closure()->IsBuiltin()));
6778
6779 if (scope()->is_global_scope() ||
6780 loop_nesting() == 0 ||
6781 contextual_load_in_builtin) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006782 Comment cmnt(masm(), "[ Load from named Property");
6783 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00006784 frame_->CallLoadIC(name,
6785 is_contextual
6786 ? RelocInfo::CODE_TARGET_CONTEXT
6787 : RelocInfo::CODE_TARGET);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006788 frame_->EmitPush(r0); // Push answer.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006789 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006790 // Inline the in-object property case.
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006791 Comment cmnt(masm(), is_contextual
6792 ? "[ Inlined contextual property load"
6793 : "[ Inlined named property load");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006794
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006795 // Counter will be decremented in the deferred code. Placed here to avoid
6796 // having it in the instruction stream below where patching will occur.
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006797 if (is_contextual) {
6798 __ IncrementCounter(&Counters::named_load_global_inline, 1,
6799 frame_->scratch0(), frame_->scratch1());
6800 } else {
6801 __ IncrementCounter(&Counters::named_load_inline, 1,
6802 frame_->scratch0(), frame_->scratch1());
6803 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006804
6805 // The following instructions are the inlined load of an in-object property.
6806 // Parts of this code is patched, so the exact instructions generated needs
6807 // to be fixed. Therefore the instruction pool is blocked when generating
6808 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006809
6810 // Load the receiver from the stack.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006811 Register receiver = frame_->PopToRegister();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006812
6813 DeferredReferenceGetNamedValue* deferred =
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006814 new DeferredReferenceGetNamedValue(receiver, name, is_contextual);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006815
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006816 bool is_dont_delete = false;
6817 if (is_contextual) {
6818 if (!info_->closure().is_null()) {
6819 // When doing lazy compilation we can check if the global cell
6820 // already exists and use its "don't delete" status as a hint.
6821 AssertNoAllocation no_gc;
6822 v8::internal::GlobalObject* global_object =
6823 info_->closure()->context()->global();
6824 LookupResult lookup;
6825 global_object->LocalLookupRealNamedProperty(*name, &lookup);
6826 if (lookup.IsProperty() && lookup.type() == NORMAL) {
6827 ASSERT(lookup.holder() == global_object);
6828 ASSERT(global_object->property_dictionary()->ValueAt(
6829 lookup.GetDictionaryEntry())->IsJSGlobalPropertyCell());
6830 is_dont_delete = lookup.IsDontDelete();
6831 }
6832 }
6833 if (is_dont_delete) {
6834 __ IncrementCounter(&Counters::dont_delete_hint_hit, 1,
6835 frame_->scratch0(), frame_->scratch1());
6836 }
6837 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006838
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006839 { Assembler::BlockConstPoolScope block_const_pool(masm_);
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006840 if (!is_contextual) {
6841 // Check that the receiver is a heap object.
6842 __ tst(receiver, Operand(kSmiTagMask));
6843 deferred->Branch(eq);
6844 }
6845
6846 // Check for the_hole_value if necessary.
6847 // Below we rely on the number of instructions generated, and we can't
6848 // cope with the Check macro which does not generate a fixed number of
6849 // instructions.
6850 Label skip, check_the_hole, cont;
6851 if (FLAG_debug_code && is_contextual && is_dont_delete) {
6852 __ b(&skip);
6853 __ bind(&check_the_hole);
6854 __ Check(ne, "DontDelete cells can't contain the hole");
6855 __ b(&cont);
6856 __ bind(&skip);
6857 }
6858
6859#ifdef DEBUG
6860 int InlinedNamedLoadInstructions = 5;
6861 Label check_inlined_codesize;
6862 masm_->bind(&check_inlined_codesize);
6863#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006864
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006865 Register scratch = VirtualFrame::scratch0();
6866 Register scratch2 = VirtualFrame::scratch1();
6867
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006868 // Check the map. The null map used below is patched by the inline cache
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006869 // code. Therefore we can't use a LoadRoot call.
6870 __ ldr(scratch, FieldMemOperand(receiver, HeapObject::kMapOffset));
6871 __ mov(scratch2, Operand(Factory::null_value()));
6872 __ cmp(scratch, scratch2);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006873 deferred->Branch(ne);
6874
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006875 if (is_contextual) {
6876#ifdef DEBUG
6877 InlinedNamedLoadInstructions += 1;
6878#endif
6879 // Load the (initially invalid) cell and get its value.
6880 masm()->mov(receiver, Operand(Factory::null_value()));
6881 __ ldr(receiver,
6882 FieldMemOperand(receiver, JSGlobalPropertyCell::kValueOffset));
6883
6884 deferred->set_is_dont_delete(is_dont_delete);
6885
6886 if (!is_dont_delete) {
6887#ifdef DEBUG
6888 InlinedNamedLoadInstructions += 3;
6889#endif
6890 __ cmp(receiver, Operand(Factory::the_hole_value()));
6891 deferred->Branch(eq);
6892 } else if (FLAG_debug_code) {
6893#ifdef DEBUG
6894 InlinedNamedLoadInstructions += 3;
6895#endif
6896 __ cmp(receiver, Operand(Factory::the_hole_value()));
6897 __ b(&check_the_hole, eq);
6898 __ bind(&cont);
6899 }
6900 } else {
6901 // Initially use an invalid index. The index will be patched by the
6902 // inline cache code.
6903 __ ldr(receiver, MemOperand(receiver, 0));
6904 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006905
6906 // Make sure that the expected number of instructions are generated.
ager@chromium.orgbeb25712010-11-29 08:02:25 +00006907 // If the code before is updated, the offsets in ic-arm.cc
6908 // LoadIC::PatchInlinedContextualLoad and PatchInlinedLoad need
6909 // to be updated.
6910 ASSERT_EQ(InlinedNamedLoadInstructions,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006911 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006912 }
6913
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006914 deferred->BindExit();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006915 // At this point the receiver register has the result, either from the
6916 // deferred code or from the inlined code.
6917 frame_->EmitPush(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006918 }
6919}
6920
6921
ager@chromium.orgac091b72010-05-05 07:34:42 +00006922void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) {
6923#ifdef DEBUG
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006924 int expected_height = frame()->height() - (is_contextual ? 1 : 2);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006925#endif
ager@chromium.orgac091b72010-05-05 07:34:42 +00006926
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006927 Result result;
6928 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6929 frame()->CallStoreIC(name, is_contextual);
6930 } else {
6931 // Inline the in-object property case.
6932 JumpTarget slow, done;
6933
6934 // Get the value and receiver from the stack.
6935 frame()->PopToR0();
6936 Register value = r0;
6937 frame()->PopToR1();
6938 Register receiver = r1;
6939
6940 DeferredReferenceSetNamedValue* deferred =
6941 new DeferredReferenceSetNamedValue(value, receiver, name);
6942
6943 // Check that the receiver is a heap object.
6944 __ tst(receiver, Operand(kSmiTagMask));
6945 deferred->Branch(eq);
6946
6947 // The following instructions are the part of the inlined
6948 // in-object property store code which can be patched. Therefore
6949 // the exact number of instructions generated must be fixed, so
6950 // the constant pool is blocked while generating this code.
6951 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6952 Register scratch0 = VirtualFrame::scratch0();
6953 Register scratch1 = VirtualFrame::scratch1();
6954
6955 // Check the map. Initially use an invalid map to force a
6956 // failure. The map check will be patched in the runtime system.
6957 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
6958
6959#ifdef DEBUG
6960 Label check_inlined_codesize;
6961 masm_->bind(&check_inlined_codesize);
6962#endif
6963 __ mov(scratch0, Operand(Factory::null_value()));
6964 __ cmp(scratch0, scratch1);
6965 deferred->Branch(ne);
6966
6967 int offset = 0;
6968 __ str(value, MemOperand(receiver, offset));
6969
6970 // Update the write barrier and record its size. We do not use
6971 // the RecordWrite macro here because we want the offset
6972 // addition instruction first to make it easy to patch.
6973 Label record_write_start, record_write_done;
6974 __ bind(&record_write_start);
6975 // Add offset into the object.
6976 __ add(scratch0, receiver, Operand(offset));
6977 // Test that the object is not in the new space. We cannot set
6978 // region marks for new space pages.
6979 __ InNewSpace(receiver, scratch1, eq, &record_write_done);
6980 // Record the actual write.
6981 __ RecordWriteHelper(receiver, scratch0, scratch1);
6982 __ bind(&record_write_done);
6983 // Clobber all input registers when running with the debug-code flag
6984 // turned on to provoke errors.
6985 if (FLAG_debug_code) {
6986 __ mov(receiver, Operand(BitCast<int32_t>(kZapValue)));
6987 __ mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
6988 __ mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
6989 }
6990 // Check that this is the first inlined write barrier or that
6991 // this inlined write barrier has the same size as all the other
6992 // inlined write barriers.
6993 ASSERT((inlined_write_barrier_size_ == -1) ||
6994 (inlined_write_barrier_size_ ==
6995 masm()->InstructionsGeneratedSince(&record_write_start)));
6996 inlined_write_barrier_size_ =
6997 masm()->InstructionsGeneratedSince(&record_write_start);
6998
6999 // Make sure that the expected number of instructions are generated.
7000 ASSERT_EQ(GetInlinedNamedStoreInstructionsAfterPatch(),
7001 masm()->InstructionsGeneratedSince(&check_inlined_codesize));
7002 }
7003 deferred->BindExit();
7004 }
7005 ASSERT_EQ(expected_height, frame()->height());
ager@chromium.orgac091b72010-05-05 07:34:42 +00007006}
7007
7008
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007009void CodeGenerator::EmitKeyedLoad() {
7010 if (loop_nesting() == 0) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007011 Comment cmnt(masm_, "[ Load from keyed property");
7012 frame_->CallKeyedLoadIC();
7013 } else {
7014 // Inline the keyed load.
7015 Comment cmnt(masm_, "[ Inlined load from keyed property");
7016
7017 // Counter will be decremented in the deferred code. Placed here to avoid
7018 // having it in the instruction stream below where patching will occur.
7019 __ IncrementCounter(&Counters::keyed_load_inline, 1,
7020 frame_->scratch0(), frame_->scratch1());
7021
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00007022 // Load the key and receiver from the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00007023 bool key_is_known_smi = frame_->KnownSmiAt(0);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00007024 Register key = frame_->PopToRegister();
7025 Register receiver = frame_->PopToRegister(key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007026
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00007027 // The deferred code expects key and receiver in registers.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007028 DeferredReferenceGetKeyedValue* deferred =
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00007029 new DeferredReferenceGetKeyedValue(key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007030
7031 // Check that the receiver is a heap object.
7032 __ tst(receiver, Operand(kSmiTagMask));
7033 deferred->Branch(eq);
7034
7035 // The following instructions are the part of the inlined load keyed
7036 // property code which can be patched. Therefore the exact number of
7037 // instructions generated need to be fixed, so the constant pool is blocked
7038 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007039 { Assembler::BlockConstPoolScope block_const_pool(masm_);
7040 Register scratch1 = VirtualFrame::scratch0();
7041 Register scratch2 = VirtualFrame::scratch1();
7042 // Check the map. The null map used below is patched by the inline cache
7043 // code.
7044 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00007045
7046 // Check that the key is a smi.
7047 if (!key_is_known_smi) {
7048 __ tst(key, Operand(kSmiTagMask));
7049 deferred->Branch(ne);
7050 }
7051
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00007052#ifdef DEBUG
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00007053 Label check_inlined_codesize;
7054 masm_->bind(&check_inlined_codesize);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00007055#endif
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007056 __ mov(scratch2, Operand(Factory::null_value()));
7057 __ cmp(scratch1, scratch2);
7058 deferred->Branch(ne);
7059
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00007060 // Get the elements array from the receiver.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007061 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00007062 __ AssertFastElements(scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007063
7064 // Check that key is within bounds. Use unsigned comparison to handle
7065 // negative keys.
7066 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007067 __ cmp(scratch2, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007068 deferred->Branch(ls); // Unsigned less equal.
7069
7070 // Load and check that the result is not the hole (key is a smi).
7071 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
7072 __ add(scratch1,
7073 scratch1,
7074 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00007075 __ ldr(scratch1,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007076 MemOperand(scratch1, key, LSL,
7077 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00007078 __ cmp(scratch1, scratch2);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007079 deferred->Branch(eq);
7080
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00007081 __ mov(r0, scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007082 // Make sure that the expected number of instructions are generated.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00007083 ASSERT_EQ(GetInlinedKeyedLoadInstructionsAfterPatch(),
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007084 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
7085 }
7086
7087 deferred->BindExit();
7088 }
7089}
7090
7091
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007092void CodeGenerator::EmitKeyedStore(StaticType* key_type,
7093 WriteBarrierCharacter wb_info) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007094 // Generate inlined version of the keyed store if the code is in a loop
7095 // and the key is likely to be a smi.
7096 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
7097 // Inline the keyed store.
7098 Comment cmnt(masm_, "[ Inlined store to keyed property");
7099
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007100 Register scratch1 = VirtualFrame::scratch0();
7101 Register scratch2 = VirtualFrame::scratch1();
7102 Register scratch3 = r3;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007103
7104 // Counter will be decremented in the deferred code. Placed here to avoid
7105 // having it in the instruction stream below where patching will occur.
7106 __ IncrementCounter(&Counters::keyed_store_inline, 1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007107 scratch1, scratch2);
7108
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007109
7110
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007111 // Load the value, key and receiver from the stack.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007112 bool value_is_harmless = frame_->KnownSmiAt(0);
7113 if (wb_info == NEVER_NEWSPACE) value_is_harmless = true;
7114 bool key_is_smi = frame_->KnownSmiAt(1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007115 Register value = frame_->PopToRegister();
7116 Register key = frame_->PopToRegister(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00007117 VirtualFrame::SpilledScope spilled(frame_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007118 Register receiver = r2;
7119 frame_->EmitPop(receiver);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007120
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007121#ifdef DEBUG
7122 bool we_remembered_the_write_barrier = value_is_harmless;
7123#endif
7124
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007125 // The deferred code expects value, key and receiver in registers.
7126 DeferredReferenceSetKeyedValue* deferred =
7127 new DeferredReferenceSetKeyedValue(value, key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007128
7129 // Check that the value is a smi. As this inlined code does not set the
7130 // write barrier it is only possible to store smi values.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007131 if (!value_is_harmless) {
7132 // If the value is not likely to be a Smi then let's test the fixed array
7133 // for new space instead. See below.
7134 if (wb_info == LIKELY_SMI) {
7135 __ tst(value, Operand(kSmiTagMask));
7136 deferred->Branch(ne);
7137#ifdef DEBUG
7138 we_remembered_the_write_barrier = true;
7139#endif
7140 }
7141 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007142
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007143 if (!key_is_smi) {
7144 // Check that the key is a smi.
7145 __ tst(key, Operand(kSmiTagMask));
7146 deferred->Branch(ne);
7147 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007148
7149 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007150 __ tst(receiver, Operand(kSmiTagMask));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007151 deferred->Branch(eq);
7152
7153 // Check that the receiver is a JSArray.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007154 __ CompareObjectType(receiver, scratch1, scratch1, JS_ARRAY_TYPE);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007155 deferred->Branch(ne);
7156
7157 // Check that the key is within bounds. Both the key and the length of
7158 // the JSArray are smis. Use unsigned comparison to handle negative keys.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007159 __ ldr(scratch1, FieldMemOperand(receiver, JSArray::kLengthOffset));
7160 __ cmp(scratch1, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007161 deferred->Branch(ls); // Unsigned less equal.
7162
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007163 // Get the elements array from the receiver.
7164 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
7165 if (!value_is_harmless && wb_info != LIKELY_SMI) {
7166 Label ok;
7167 __ and_(scratch2, scratch1, Operand(ExternalReference::new_space_mask()));
7168 __ cmp(scratch2, Operand(ExternalReference::new_space_start()));
7169 __ tst(value, Operand(kSmiTagMask), ne);
7170 deferred->Branch(ne);
7171#ifdef DEBUG
7172 we_remembered_the_write_barrier = true;
7173#endif
7174 }
7175 // Check that the elements array is not a dictionary.
7176 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007177 // The following instructions are the part of the inlined store keyed
7178 // property code which can be patched. Therefore the exact number of
7179 // instructions generated need to be fixed, so the constant pool is blocked
7180 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007181 { Assembler::BlockConstPoolScope block_const_pool(masm_);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007182#ifdef DEBUG
7183 Label check_inlined_codesize;
7184 masm_->bind(&check_inlined_codesize);
7185#endif
7186
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007187 // Read the fixed array map from the constant pool (not from the root
7188 // array) so that the value can be patched. When debugging, we patch this
7189 // comparison to always fail so that we will hit the IC call in the
7190 // deferred code which will allow the debugger to break for fast case
7191 // stores.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007192 __ mov(scratch3, Operand(Factory::fixed_array_map()));
7193 __ cmp(scratch2, scratch3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007194 deferred->Branch(ne);
7195
7196 // Store the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007197 __ add(scratch1, scratch1,
7198 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
7199 __ str(value,
7200 MemOperand(scratch1, key, LSL,
7201 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007202
7203 // Make sure that the expected number of instructions are generated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007204 ASSERT_EQ(kInlinedKeyedStoreInstructionsAfterPatch,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007205 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
7206 }
7207
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007208 ASSERT(we_remembered_the_write_barrier);
7209
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007210 deferred->BindExit();
7211 } else {
7212 frame()->CallKeyedStoreIC();
7213 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007214}
7215
7216
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00007217#ifdef DEBUG
7218bool CodeGenerator::HasValidEntryRegisters() { return true; }
7219#endif
7220
7221
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007222#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007223#define __ ACCESS_MASM(masm)
7224
ager@chromium.org7c537e22008-10-16 08:43:32 +00007225Handle<String> Reference::GetName() {
7226 ASSERT(type_ == NAMED);
7227 Property* property = expression_->AsProperty();
7228 if (property == NULL) {
7229 // Global variable reference treated as a named property reference.
7230 VariableProxy* proxy = expression_->AsVariableProxy();
7231 ASSERT(proxy->AsVariable() != NULL);
7232 ASSERT(proxy->AsVariable()->is_global());
7233 return proxy->name();
7234 } else {
7235 Literal* raw_name = property->key()->AsLiteral();
7236 ASSERT(raw_name != NULL);
7237 return Handle<String>(String::cast(*raw_name->handle()));
7238 }
7239}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007240
ager@chromium.org7c537e22008-10-16 08:43:32 +00007241
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007242void Reference::DupIfPersist() {
7243 if (persist_after_get_) {
7244 switch (type_) {
7245 case KEYED:
7246 cgen_->frame()->Dup2();
7247 break;
7248 case NAMED:
7249 cgen_->frame()->Dup();
7250 // Fall through.
7251 case UNLOADED:
7252 case ILLEGAL:
7253 case SLOT:
7254 // Do nothing.
7255 ;
7256 }
7257 } else {
7258 set_unloaded();
7259 }
7260}
7261
7262
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007263void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00007264 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00007265 ASSERT(!is_illegal());
7266 ASSERT(!cgen_->has_cc());
7267 MacroAssembler* masm = cgen_->masm();
7268 Property* property = expression_->AsProperty();
7269 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00007270 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00007271 }
7272
7273 switch (type_) {
7274 case SLOT: {
7275 Comment cmnt(masm, "[ Load from Slot");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00007276 Slot* slot = expression_->AsVariableProxy()->AsVariable()->AsSlot();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007277 ASSERT(slot != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007278 DupIfPersist();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007279 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007280 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007281 }
7282
ager@chromium.org7c537e22008-10-16 08:43:32 +00007283 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00007284 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007285 bool is_global = var != NULL;
7286 ASSERT(!is_global || var->is_global());
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007287 Handle<String> name = GetName();
7288 DupIfPersist();
7289 cgen_->EmitNamedLoad(name, is_global);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007290 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007291 }
7292
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007293 case KEYED: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007294 ASSERT(property != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007295 DupIfPersist();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007296 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007297 cgen_->frame()->EmitPush(r0);
7298 break;
7299 }
7300
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007301 default:
7302 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007303 }
7304}
7305
7306
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007307void Reference::SetValue(InitState init_state, WriteBarrierCharacter wb_info) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00007308 ASSERT(!is_illegal());
7309 ASSERT(!cgen_->has_cc());
7310 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00007311 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007312 Property* property = expression_->AsProperty();
7313 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00007314 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007315 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007316
ager@chromium.org7c537e22008-10-16 08:43:32 +00007317 switch (type_) {
7318 case SLOT: {
7319 Comment cmnt(masm, "[ Store to Slot");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00007320 Slot* slot = expression_->AsVariableProxy()->AsVariable()->AsSlot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007321 cgen_->StoreToSlot(slot, init_state);
ager@chromium.orgac091b72010-05-05 07:34:42 +00007322 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007323 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007324 }
7325
ager@chromium.org7c537e22008-10-16 08:43:32 +00007326 case NAMED: {
7327 Comment cmnt(masm, "[ Store to named Property");
ager@chromium.orgac091b72010-05-05 07:34:42 +00007328 cgen_->EmitNamedStore(GetName(), false);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00007329 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00007330 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007331 break;
7332 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007333
ager@chromium.org7c537e22008-10-16 08:43:32 +00007334 case KEYED: {
7335 Comment cmnt(masm, "[ Store to keyed Property");
7336 Property* property = expression_->AsProperty();
7337 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00007338 cgen_->CodeForSourcePosition(property->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007339 cgen_->EmitKeyedStore(property->key()->type(), wb_info);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00007340 frame->EmitPush(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007341 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007342 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007343 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00007344
7345 default:
7346 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007347 }
7348}
7349
7350
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007351const char* GenericBinaryOpStub::GetName() {
7352 if (name_ != NULL) return name_;
7353 const int len = 100;
7354 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
7355 if (name_ == NULL) return "OOM";
7356 const char* op_name = Token::Name(op_);
7357 const char* overwrite_name;
7358 switch (mode_) {
7359 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
7360 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
7361 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
7362 default: overwrite_name = "UnknownOverwrite"; break;
7363 }
7364
7365 OS::SNPrintF(Vector<char>(name_, len),
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007366 "GenericBinaryOpStub_%s_%s%s_%s",
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007367 op_name,
7368 overwrite_name,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007369 specialized_on_rhs_ ? "_ConstantRhs" : "",
7370 BinaryOpIC::GetName(runtime_operands_type_));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007371 return name_;
7372}
7373
7374
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007375#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007376
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007377} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007378
7379#endif // V8_TARGET_ARCH_ARM