blob: dd0520feed4c4ca9a9ef1cff9947e5334531b77d [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"
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000039#include "jump-target-light-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
82void ICRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
83 masm->EnterInternalFrame();
84}
85
86
87void ICRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
88 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);
168 type_info_ = &type_info_array;
169
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000170 ASSERT(allocator_ == NULL);
171 RegisterAllocator register_allocator(this);
172 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000173 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000174 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000175 cc_reg_ = al;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000176
177 // Adjust for function-level loop nesting.
178 ASSERT_EQ(0, loop_nesting_);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000179 loop_nesting_ = info->is_in_loop() ? 1 : 0;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000180
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000181 {
182 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000184 // Entry:
185 // Stack: receiver, arguments
186 // lr: return address
187 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000189 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000191 allocator_->Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000192
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193#ifdef DEBUG
194 if (strlen(FLAG_stop_at) > 0 &&
ager@chromium.org5c838252010-02-19 08:53:10 +0000195 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000196 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000197 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198 }
199#endif
200
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000201 frame_->Enter();
202 // tos: code slot
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000203
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000204 // Allocate space for locals and initialize them. This also checks
205 // for stack overflow.
206 frame_->AllocateStackSlots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000207
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000208 frame_->AssertIsSpilled();
209 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
210 if (heap_slots > 0) {
211 // Allocate local context.
212 // Get outer context and create a new context based on it.
213 __ ldr(r0, frame_->Function());
214 frame_->EmitPush(r0);
215 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
216 FastNewContextStub stub(heap_slots);
217 frame_->CallStub(&stub, 1);
218 } else {
219 frame_->CallRuntime(Runtime::kNewContext, 1);
220 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000221
222#ifdef DEBUG
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000223 JumpTarget verified_true;
224 __ cmp(r0, cp);
225 verified_true.Branch(eq);
226 __ stop("NewContext: r0 is expected to be the same as cp");
227 verified_true.Bind();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000228#endif
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000229 // Update context local.
230 __ str(cp, frame_->Context());
231 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000232
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000233 // TODO(1241774): Improve this code:
234 // 1) only needed if we have a context
235 // 2) no need to recompute context ptr every single time
236 // 3) don't copy parameter operand code from SlotOperand!
237 {
238 Comment cmnt2(masm_, "[ copy context parameters into .context");
239 // Note that iteration order is relevant here! If we have the same
240 // parameter twice (e.g., function (x, y, x)), and that parameter
241 // needs to be copied into the context, it must be the last argument
242 // passed to the parameter that needs to be copied. This is a rare
243 // case so we don't check for it, instead we rely on the copying
244 // order: such a parameter is copied repeatedly into the same
245 // context location and thus the last value is what is seen inside
246 // the function.
247 frame_->AssertIsSpilled();
248 for (int i = 0; i < scope()->num_parameters(); i++) {
249 Variable* par = scope()->parameter(i);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000250 Slot* slot = par->AsSlot();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000251 if (slot != NULL && slot->type() == Slot::CONTEXT) {
252 ASSERT(!scope()->is_global_scope()); // No params in global scope.
253 __ ldr(r1, frame_->ParameterAt(i));
254 // Loads r2 with context; used below in RecordWrite.
255 __ str(r1, SlotOperand(slot, r2));
256 // Load the offset into r3.
257 int slot_offset =
258 FixedArray::kHeaderSize + slot->index() * kPointerSize;
259 __ RecordWrite(r2, Operand(slot_offset), r3, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000260 }
261 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000262 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000263
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000264 // Store the arguments object. This must happen after context
265 // initialization because the arguments object may be stored in
266 // the context.
267 if (ArgumentsMode() != NO_ARGUMENTS_ALLOCATION) {
268 StoreArgumentsObject(true);
269 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000270
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000271 // Initialize ThisFunction reference if present.
272 if (scope()->is_function_scope() && scope()->function() != NULL) {
273 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000274 StoreToSlot(scope()->function()->AsSlot(), NOT_CONST_INIT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000275 }
276
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000277 // Initialize the function return target after the locals are set
278 // up, because it needs the expected frame height from the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000279 function_return_.SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000280 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000282 // Generate code to 'execute' declarations and initialize functions
283 // (source elements). In case of an illegal redeclaration we need to
284 // handle that instead of processing the declarations.
ager@chromium.org5c838252010-02-19 08:53:10 +0000285 if (scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000287 scope()->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 } else {
289 Comment cmnt(masm_, "[ declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000290 ProcessDeclarations(scope()->declarations());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000291 // Bail out if a stack-overflow exception occurred when processing
292 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000293 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294 }
295
mads.s.ager31e71382008-08-13 09:32:07 +0000296 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000297 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000298 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000299 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300
301 // Compile the body of the function in a vanilla state. Don't
302 // bother compiling all the code if the scope has an illegal
303 // redeclaration.
ager@chromium.org5c838252010-02-19 08:53:10 +0000304 if (!scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 Comment cmnt(masm_, "[ function body");
306#ifdef DEBUG
307 bool is_builtin = Bootstrapper::IsActive();
308 bool should_trace =
309 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000310 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000311 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000312 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000313 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314#endif
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000315 VisitStatements(info->function()->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317 }
318
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000319 // Handle the return from the function.
320 if (has_valid_frame()) {
321 // If there is a valid frame, control flow can fall off the end of
322 // the body. In that case there is an implicit return statement.
323 ASSERT(!function_return_is_shadowed_);
324 frame_->PrepareForReturn();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000325 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000326 if (function_return_.is_bound()) {
327 function_return_.Jump();
328 } else {
329 function_return_.Bind();
330 GenerateReturnSequence();
331 }
332 } else if (function_return_.is_linked()) {
333 // If the return target has dangling jumps to it, then we have not
334 // yet generated the return sequence. This can happen when (a)
335 // control does not flow off the end of the body so we did not
336 // compile an artificial return statement just above, and (b) there
337 // are return statements in the body but (c) they are all shadowed.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000338 function_return_.Bind();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000339 GenerateReturnSequence();
mads.s.ager31e71382008-08-13 09:32:07 +0000340 }
341
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000342 // Adjust for function-level loop nesting.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000343 ASSERT(loop_nesting_ == info->is_in_loop()? 1 : 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000344 loop_nesting_ = 0;
345
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347 ASSERT(!has_cc());
348 ASSERT(state_ == NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000349 ASSERT(loop_nesting() == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000350 ASSERT(!function_return_is_shadowed_);
351 function_return_.Unuse();
352 DeleteFrame();
353
354 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000355 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000356 ProcessDeferred();
357 }
358
359 allocator_ = NULL;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000360 type_info_ = NULL;
361}
362
363
364int CodeGenerator::NumberOfSlot(Slot* slot) {
365 if (slot == NULL) return kInvalidSlotNumber;
366 switch (slot->type()) {
367 case Slot::PARAMETER:
368 return slot->index();
369 case Slot::LOCAL:
370 return slot->index() + scope()->num_parameters();
371 default:
372 break;
373 }
374 return kInvalidSlotNumber;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375}
376
377
ager@chromium.org7c537e22008-10-16 08:43:32 +0000378MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
379 // Currently, this assertion will fail if we try to assign to
380 // a constant variable that is constant because it is read-only
381 // (such as the variable referring to a named function expression).
382 // We need to implement assignments to read-only variables.
383 // Ideally, we should do this during AST generation (by converting
384 // such assignments into expression statements); however, in general
385 // we may not be able to make the decision until past AST generation,
386 // that is when the entire program is known.
387 ASSERT(slot != NULL);
388 int index = slot->index();
389 switch (slot->type()) {
390 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000391 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000392
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000393 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000394 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000395
396 case Slot::CONTEXT: {
397 // Follow the context chain if necessary.
398 ASSERT(!tmp.is(cp)); // do not overwrite context register
399 Register context = cp;
400 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000401 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000402 // Load the closure.
403 // (All contexts, even 'with' contexts, have a closure,
404 // and it is the same for all contexts inside a function.
405 // There is no need to go to the function context first.)
406 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
407 // Load the function context (which is the incoming, outer context).
408 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
409 context = tmp;
410 }
411 // We may have a 'with' context now. Get the function context.
412 // (In fact this mov may never be the needed, since the scope analysis
413 // may not permit a direct context access in this case and thus we are
414 // always at a function context. However it is safe to dereference be-
415 // cause the function context of a function context is itself. Before
416 // deleting this mov we should try to create a counter-example first,
417 // though...)
418 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
419 return ContextOperand(tmp, index);
420 }
421
422 default:
423 UNREACHABLE();
424 return MemOperand(r0, 0);
425 }
426}
427
428
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000429MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
430 Slot* slot,
431 Register tmp,
432 Register tmp2,
433 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000434 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000435 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000436
ager@chromium.org381abbb2009-02-25 13:23:22 +0000437 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
438 if (s->num_heap_slots() > 0) {
439 if (s->calls_eval()) {
440 // Check that extension is NULL.
441 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
442 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000443 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000444 }
445 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
446 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
447 context = tmp;
448 }
449 }
450 // Check that last extension is NULL.
451 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
452 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000453 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000454 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000455 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000456}
457
458
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000459// Loads a value on TOS. If it is a boolean value, the result may have been
460// (partially) translated into branches, or it may have set the condition
461// code register. If force_cc is set, the value is forced to set the
462// condition code register and no value is pushed. If the condition code
463// register was set, has_cc() is true and cc_reg_ contains the condition to
464// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000465void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000466 JumpTarget* true_target,
467 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000468 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000469 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000470 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000472 { ConditionCodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000473 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000474
475 // If we hit a stack overflow, we may not have actually visited
476 // the expression. In that case, we ensure that we have a
477 // valid-looking frame state because we will continue to generate
478 // code as we unwind the C++ stack.
479 //
480 // It's possible to have both a stack overflow and a valid frame
481 // state (eg, a subexpression overflowed, visiting it returned
482 // with a dummied frame state, and visiting this expression
483 // returned with a normal-looking state).
484 if (HasStackOverflow() &&
485 has_valid_frame() &&
486 !has_cc() &&
487 frame_->height() == original_height) {
488 true_target->Jump();
489 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000490 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000491 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000492 // Convert the TOS value to a boolean in the condition code register.
493 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000495 ASSERT(!force_cc || !has_valid_frame() || has_cc());
496 ASSERT(!has_valid_frame() ||
497 (has_cc() && frame_->height() == original_height) ||
498 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499}
500
501
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000502void CodeGenerator::Load(Expression* expr) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000503 // We generally assume that we are not in a spilled scope for most
504 // of the code generator. A failure to ensure this caused issue 815
505 // and this assert is designed to catch similar issues.
506 frame_->AssertIsNotSpilled();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000507#ifdef DEBUG
508 int original_height = frame_->height();
509#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000510 JumpTarget true_target;
511 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000512 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513
514 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000515 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000516 JumpTarget loaded;
517 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000518 materialize_true.Branch(cc_reg_);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000519 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000520 loaded.Jump();
521 materialize_true.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000522 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000523 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 cc_reg_ = al;
525 }
526
527 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000528 // We have at least one condition value that has been "translated"
529 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000530 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000531 if (frame_ != NULL) {
532 loaded.Jump(); // Don't lose the current TOS.
533 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000535 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000537 true_target.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000538 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000540 // If both "true" and "false" need to be loaded jump across the code for
541 // "false".
542 if (both) {
543 loaded.Jump();
544 }
545 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000547 false_target.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000548 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000550 // A value is loaded on all paths reaching this point.
551 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000553 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554 ASSERT(!has_cc());
ager@chromium.orgac091b72010-05-05 07:34:42 +0000555 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556}
557
558
ager@chromium.org7c537e22008-10-16 08:43:32 +0000559void CodeGenerator::LoadGlobal() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000560 Register reg = frame_->GetTOSRegister();
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000561 __ ldr(reg, GlobalObjectOperand());
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000562 frame_->EmitPush(reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563}
564
565
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000566void CodeGenerator::LoadGlobalReceiver(Register scratch) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000567 Register reg = frame_->GetTOSRegister();
568 __ ldr(reg, ContextOperand(cp, Context::GLOBAL_INDEX));
569 __ ldr(reg,
570 FieldMemOperand(reg, GlobalObject::kGlobalReceiverOffset));
571 frame_->EmitPush(reg);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000572}
573
574
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000575ArgumentsAllocationMode CodeGenerator::ArgumentsMode() {
576 if (scope()->arguments() == NULL) return NO_ARGUMENTS_ALLOCATION;
577 ASSERT(scope()->arguments_shadow() != NULL);
578 // We don't want to do lazy arguments allocation for functions that
579 // have heap-allocated contexts, because it interfers with the
580 // uninitialized const tracking in the context objects.
581 return (scope()->num_heap_slots() > 0)
582 ? EAGER_ARGUMENTS_ALLOCATION
583 : LAZY_ARGUMENTS_ALLOCATION;
584}
585
586
587void CodeGenerator::StoreArgumentsObject(bool initial) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000588 ArgumentsAllocationMode mode = ArgumentsMode();
589 ASSERT(mode != NO_ARGUMENTS_ALLOCATION);
590
591 Comment cmnt(masm_, "[ store arguments object");
592 if (mode == LAZY_ARGUMENTS_ALLOCATION && initial) {
593 // When using lazy arguments allocation, we store the hole value
594 // as a sentinel indicating that the arguments object hasn't been
595 // allocated yet.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000596 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000597 } else {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000598 frame_->SpillAll();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000599 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
600 __ ldr(r2, frame_->Function());
601 // The receiver is below the arguments, the return address, and the
602 // frame pointer on the stack.
603 const int kReceiverDisplacement = 2 + scope()->num_parameters();
604 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
605 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
606 frame_->Adjust(3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000607 __ Push(r2, r1, r0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000608 frame_->CallStub(&stub, 3);
609 frame_->EmitPush(r0);
610 }
611
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000612 Variable* arguments = scope()->arguments();
613 Variable* shadow = scope()->arguments_shadow();
614 ASSERT(arguments != NULL && arguments->AsSlot() != NULL);
615 ASSERT(shadow != NULL && shadow->AsSlot() != NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000616 JumpTarget done;
617 if (mode == LAZY_ARGUMENTS_ALLOCATION && !initial) {
618 // We have to skip storing into the arguments slot if it has
619 // already been written to. This can happen if the a function
620 // has a local variable named 'arguments'.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000621 LoadFromSlot(scope()->arguments()->AsSlot(), NOT_INSIDE_TYPEOF);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000622 Register arguments = frame_->PopToRegister();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000623 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000624 __ cmp(arguments, ip);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000625 done.Branch(ne);
626 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000627 StoreToSlot(arguments->AsSlot(), NOT_CONST_INIT);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000628 if (mode == LAZY_ARGUMENTS_ALLOCATION) done.Bind();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000629 StoreToSlot(shadow->AsSlot(), NOT_CONST_INIT);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000630}
631
632
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000633void CodeGenerator::LoadTypeofExpression(Expression* expr) {
634 // Special handling of identifiers as subexpressions of typeof.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000635 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000637 // For a global variable we build the property reference
638 // <global>.<variable> and perform a (regular non-contextual) property
639 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
641 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000642 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000643 Reference ref(this, &property);
ager@chromium.org357bf652010-04-12 11:30:10 +0000644 ref.GetValue();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000645 } else if (variable != NULL && variable->AsSlot() != NULL) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000646 // For a variable that rewrites to a slot, we signal it is the immediate
647 // subexpression of a typeof.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000648 LoadFromSlotCheckForArguments(variable->AsSlot(), INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000650 // Anything else can be handled normally.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000651 Load(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 }
653}
654
655
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000656Reference::Reference(CodeGenerator* cgen,
657 Expression* expression,
658 bool persist_after_get)
659 : cgen_(cgen),
660 expression_(expression),
661 type_(ILLEGAL),
662 persist_after_get_(persist_after_get) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000663 // We generally assume that we are not in a spilled scope for most
664 // of the code generator. A failure to ensure this caused issue 815
665 // and this assert is designed to catch similar issues.
666 cgen->frame()->AssertIsNotSpilled();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667 cgen->LoadReference(this);
668}
669
670
671Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000672 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673}
674
675
ager@chromium.org7c537e22008-10-16 08:43:32 +0000676void CodeGenerator::LoadReference(Reference* ref) {
677 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678 Expression* e = ref->expression();
679 Property* property = e->AsProperty();
680 Variable* var = e->AsVariableProxy()->AsVariable();
681
682 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000683 // The expression is either a property or a variable proxy that rewrites
684 // to a property.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000685 Load(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000686 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687 ref->set_type(Reference::NAMED);
688 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000689 Load(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690 ref->set_type(Reference::KEYED);
691 }
692 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000693 // The expression is a variable proxy that does not rewrite to a
694 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696 LoadGlobal();
697 ref->set_type(Reference::NAMED);
698 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000699 ASSERT(var->AsSlot() != NULL);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000700 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701 }
702 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000703 // Anything else is a runtime error.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000704 Load(e);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000705 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706 }
707}
708
709
ager@chromium.org7c537e22008-10-16 08:43:32 +0000710void CodeGenerator::UnloadReference(Reference* ref) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711 int size = ref->size();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000712 ref->set_unloaded();
ager@chromium.org357bf652010-04-12 11:30:10 +0000713 if (size == 0) return;
714
715 // Pop a reference from the stack while preserving TOS.
716 VirtualFrame::RegisterAllocationScope scope(this);
717 Comment cmnt(masm_, "[ UnloadReference");
718 if (size > 0) {
719 Register tos = frame_->PopToRegister();
720 frame_->Drop(size);
721 frame_->EmitPush(tos);
722 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723}
724
725
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
727// register to a boolean in the condition code register. The code
728// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000729void CodeGenerator::ToBoolean(JumpTarget* true_target,
730 JumpTarget* false_target) {
mads.s.ager31e71382008-08-13 09:32:07 +0000731 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732 // Only the condition code should be set.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000733 bool known_smi = frame_->KnownSmiAt(0);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000734 Register tos = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735
736 // Fast case checks
737
mads.s.ager31e71382008-08-13 09:32:07 +0000738 // Check if the value is 'false'.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000739 if (!known_smi) {
740 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
741 __ cmp(tos, ip);
742 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000744 // Check if the value is 'true'.
745 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
746 __ cmp(tos, ip);
747 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000749 // Check if the value is 'undefined'.
750 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
751 __ cmp(tos, ip);
752 false_target->Branch(eq);
753 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754
mads.s.ager31e71382008-08-13 09:32:07 +0000755 // Check if the value is a smi.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000756 __ cmp(tos, Operand(Smi::FromInt(0)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000758 if (!known_smi) {
759 false_target->Branch(eq);
760 __ tst(tos, Operand(kSmiTagMask));
761 true_target->Branch(eq);
762
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000763 // Slow case.
764 if (CpuFeatures::IsSupported(VFP3)) {
765 CpuFeatures::Scope scope(VFP3);
766 // Implements the slow case by using ToBooleanStub.
767 // The ToBooleanStub takes a single argument, and
768 // returns a non-zero value for true, or zero for false.
769 // Both the argument value and the return value use the
770 // register assigned to tos_
771 ToBooleanStub stub(tos);
772 frame_->CallStub(&stub, 0);
773 // Convert the result in "tos" to a condition code.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000774 __ cmp(tos, Operand(0, RelocInfo::NONE));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000775 } else {
776 // Implements slow case by calling the runtime.
777 frame_->EmitPush(tos);
778 frame_->CallRuntime(Runtime::kToBool, 1);
779 // Convert the result (r0) to a condition code.
780 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
781 __ cmp(r0, ip);
782 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000783 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784
785 cc_reg_ = ne;
786}
787
788
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000789void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000790 OverwriteMode overwrite_mode,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000791 GenerateInlineSmi inline_smi,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000792 int constant_rhs) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000793 // top of virtual frame: y
794 // 2nd elt. on virtual frame : x
795 // result : top of virtual frame
796
797 // Stub is entered with a call: 'return address' is in lr.
798 switch (op) {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000799 case Token::ADD:
800 case Token::SUB:
801 if (inline_smi) {
802 JumpTarget done;
803 Register rhs = frame_->PopToRegister();
804 Register lhs = frame_->PopToRegister(rhs);
805 Register scratch = VirtualFrame::scratch0();
806 __ orr(scratch, rhs, Operand(lhs));
807 // Check they are both small and positive.
808 __ tst(scratch, Operand(kSmiTagMask | 0xc0000000));
809 ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000810 STATIC_ASSERT(kSmiTag == 0);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000811 if (op == Token::ADD) {
812 __ add(r0, lhs, Operand(rhs), LeaveCC, eq);
813 } else {
814 __ sub(r0, lhs, Operand(rhs), LeaveCC, eq);
815 }
816 done.Branch(eq);
817 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
818 frame_->SpillAll();
819 frame_->CallStub(&stub, 0);
820 done.Bind();
821 frame_->EmitPush(r0);
822 break;
823 } else {
824 // Fall through!
825 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000826 case Token::BIT_OR:
827 case Token::BIT_AND:
828 case Token::BIT_XOR:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000829 if (inline_smi) {
830 bool rhs_is_smi = frame_->KnownSmiAt(0);
831 bool lhs_is_smi = frame_->KnownSmiAt(1);
832 Register rhs = frame_->PopToRegister();
833 Register lhs = frame_->PopToRegister(rhs);
834 Register smi_test_reg;
835 Condition cond;
836 if (!rhs_is_smi || !lhs_is_smi) {
837 if (rhs_is_smi) {
838 smi_test_reg = lhs;
839 } else if (lhs_is_smi) {
840 smi_test_reg = rhs;
841 } else {
842 smi_test_reg = VirtualFrame::scratch0();
843 __ orr(smi_test_reg, rhs, Operand(lhs));
844 }
845 // Check they are both Smis.
846 __ tst(smi_test_reg, Operand(kSmiTagMask));
847 cond = eq;
848 } else {
849 cond = al;
850 }
851 ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now.
852 if (op == Token::BIT_OR) {
853 __ orr(r0, lhs, Operand(rhs), LeaveCC, cond);
854 } else if (op == Token::BIT_AND) {
855 __ and_(r0, lhs, Operand(rhs), LeaveCC, cond);
856 } else {
857 ASSERT(op == Token::BIT_XOR);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000858 STATIC_ASSERT(kSmiTag == 0);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000859 __ eor(r0, lhs, Operand(rhs), LeaveCC, cond);
860 }
861 if (cond != al) {
862 JumpTarget done;
863 done.Branch(cond);
864 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
865 frame_->SpillAll();
866 frame_->CallStub(&stub, 0);
867 done.Bind();
868 }
869 frame_->EmitPush(r0);
870 break;
871 } else {
872 // Fall through!
873 }
874 case Token::MUL:
875 case Token::DIV:
876 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000877 case Token::SHL:
878 case Token::SHR:
879 case Token::SAR: {
ager@chromium.org357bf652010-04-12 11:30:10 +0000880 Register rhs = frame_->PopToRegister();
881 Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000882 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
883 frame_->SpillAll();
884 frame_->CallStub(&stub, 0);
ager@chromium.org357bf652010-04-12 11:30:10 +0000885 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886 break;
887 }
888
ager@chromium.org357bf652010-04-12 11:30:10 +0000889 case Token::COMMA: {
890 Register scratch = frame_->PopToRegister();
891 // Simply discard left value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000892 frame_->Drop();
ager@chromium.org357bf652010-04-12 11:30:10 +0000893 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 break;
ager@chromium.org357bf652010-04-12 11:30:10 +0000895 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896
897 default:
898 // Other cases should have been handled before this point.
899 UNREACHABLE();
900 break;
901 }
902}
903
904
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000905class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000906 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000907 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000908 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000909 bool reversed,
ager@chromium.org357bf652010-04-12 11:30:10 +0000910 OverwriteMode overwrite_mode,
911 Register tos)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000912 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000913 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000914 reversed_(reversed),
ager@chromium.org357bf652010-04-12 11:30:10 +0000915 overwrite_mode_(overwrite_mode),
916 tos_register_(tos) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000917 set_comment("[ DeferredInlinedSmiOperation");
918 }
919
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000920 virtual void Generate();
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000921 // This stub makes explicit calls to SaveRegisters(), RestoreRegisters() and
922 // Exit(). Currently on ARM SaveRegisters() and RestoreRegisters() are empty
923 // methods, it is the responsibility of the deferred code to save and restore
924 // registers.
925 virtual bool AutoSaveAndRestore() { return false; }
926
927 void JumpToNonSmiInput(Condition cond);
928 void JumpToAnswerOutOfRange(Condition cond);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000929
930 private:
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000931 void GenerateNonSmiInput();
932 void GenerateAnswerOutOfRange();
933 void WriteNonSmiAnswer(Register answer,
934 Register heap_number,
935 Register scratch);
936
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000937 Token::Value op_;
938 int value_;
939 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000940 OverwriteMode overwrite_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000941 Register tos_register_;
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000942 Label non_smi_input_;
943 Label answer_out_of_range_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000944};
945
946
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000947// For bit operations we try harder and handle the case where the input is not
948// a Smi but a 32bits integer without calling the generic stub.
949void DeferredInlineSmiOperation::JumpToNonSmiInput(Condition cond) {
950 ASSERT(Token::IsBitOp(op_));
951
952 __ b(cond, &non_smi_input_);
953}
954
955
956// For bit operations the result is always 32bits so we handle the case where
957// the result does not fit in a Smi without calling the generic stub.
958void DeferredInlineSmiOperation::JumpToAnswerOutOfRange(Condition cond) {
959 ASSERT(Token::IsBitOp(op_));
960
961 if ((op_ == Token::SHR) && !CpuFeatures::IsSupported(VFP3)) {
962 // >>> requires an unsigned to double conversion and the non VFP code
963 // does not support this conversion.
964 __ b(cond, entry_label());
965 } else {
966 __ b(cond, &answer_out_of_range_);
967 }
968}
969
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000970
971// On entry the non-constant side of the binary operation is in tos_register_
972// and the constant smi side is nowhere. The tos_register_ is not used by the
973// virtual frame. On exit the answer is in the tos_register_ and the virtual
974// frame is unchanged.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000975void DeferredInlineSmiOperation::Generate() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000976 VirtualFrame copied_frame(*frame_state()->frame());
977 copied_frame.SpillAll();
978
ager@chromium.org357bf652010-04-12 11:30:10 +0000979 Register lhs = r1;
980 Register rhs = r0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000981 switch (op_) {
982 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000983 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000984 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000985 __ sub(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000986 __ mov(r1, Operand(Smi::FromInt(value_)));
987 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000988 __ sub(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000989 __ mov(r0, Operand(Smi::FromInt(value_)));
990 }
991 break;
992 }
993
994 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000995 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000996 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000997 __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000998 __ mov(r1, Operand(Smi::FromInt(value_)));
999 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00001000 __ add(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001001 __ mov(r0, Operand(Smi::FromInt(value_)));
1002 }
1003 break;
1004 }
1005
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001006 // For these operations there is no optimistic operation that needs to be
1007 // reverted.
1008 case Token::MUL:
1009 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001010 case Token::BIT_OR:
1011 case Token::BIT_XOR:
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001012 case Token::BIT_AND:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001013 case Token::SHL:
1014 case Token::SHR:
1015 case Token::SAR: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001016 if (tos_register_.is(r1)) {
1017 __ mov(r0, Operand(Smi::FromInt(value_)));
1018 } else {
1019 ASSERT(tos_register_.is(r0));
1020 __ mov(r1, Operand(Smi::FromInt(value_)));
1021 }
1022 if (reversed_ == tos_register_.is(r1)) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001023 lhs = r0;
1024 rhs = r1;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001025 }
1026 break;
1027 }
1028
1029 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +00001030 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001031 UNREACHABLE();
1032 break;
1033 }
1034
ager@chromium.org357bf652010-04-12 11:30:10 +00001035 GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001036 __ CallStub(&stub);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001037
ager@chromium.org357bf652010-04-12 11:30:10 +00001038 // The generic stub returns its value in r0, but that's not
1039 // necessarily what we want. We want whatever the inlined code
1040 // expected, which is that the answer is in the same register as
1041 // the operand was.
1042 __ Move(tos_register_, r0);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001043
1044 // The tos register was not in use for the virtual frame that we
1045 // came into this function with, so we can merge back to that frame
1046 // without trashing it.
1047 copied_frame.MergeTo(frame_state()->frame());
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001048
1049 Exit();
1050
1051 if (non_smi_input_.is_linked()) {
1052 GenerateNonSmiInput();
1053 }
1054
1055 if (answer_out_of_range_.is_linked()) {
1056 GenerateAnswerOutOfRange();
1057 }
1058}
1059
1060
1061// Convert and write the integer answer into heap_number.
1062void DeferredInlineSmiOperation::WriteNonSmiAnswer(Register answer,
1063 Register heap_number,
1064 Register scratch) {
1065 if (CpuFeatures::IsSupported(VFP3)) {
1066 CpuFeatures::Scope scope(VFP3);
1067 __ vmov(s0, answer);
1068 if (op_ == Token::SHR) {
1069 __ vcvt_f64_u32(d0, s0);
1070 } else {
1071 __ vcvt_f64_s32(d0, s0);
1072 }
1073 __ sub(scratch, heap_number, Operand(kHeapObjectTag));
1074 __ vstr(d0, scratch, HeapNumber::kValueOffset);
1075 } else {
1076 WriteInt32ToHeapNumberStub stub(answer, heap_number, scratch);
1077 __ CallStub(&stub);
1078 }
1079}
1080
1081
1082void DeferredInlineSmiOperation::GenerateNonSmiInput() {
1083 // We know the left hand side is not a Smi and the right hand side is an
1084 // immediate value (value_) which can be represented as a Smi. We only
1085 // handle bit operations.
1086 ASSERT(Token::IsBitOp(op_));
1087
1088 if (FLAG_debug_code) {
1089 __ Abort("Should not fall through!");
1090 }
1091
1092 __ bind(&non_smi_input_);
1093 if (FLAG_debug_code) {
1094 __ AbortIfSmi(tos_register_);
1095 }
1096
1097 // This routine uses the registers from r2 to r6. At the moment they are
1098 // not used by the register allocator, but when they are it should use
1099 // SpillAll and MergeTo like DeferredInlineSmiOperation::Generate() above.
1100
1101 Register heap_number_map = r7;
1102 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1103 __ ldr(r3, FieldMemOperand(tos_register_, HeapNumber::kMapOffset));
1104 __ cmp(r3, heap_number_map);
1105 // Not a number, fall back to the GenericBinaryOpStub.
1106 __ b(ne, entry_label());
1107
1108 Register int32 = r2;
1109 // Not a 32bits signed int, fall back to the GenericBinaryOpStub.
1110 __ ConvertToInt32(tos_register_, int32, r4, r5, entry_label());
1111
1112 // tos_register_ (r0 or r1): Original heap number.
1113 // int32: signed 32bits int.
1114
1115 Label result_not_a_smi;
1116 int shift_value = value_ & 0x1f;
1117 switch (op_) {
1118 case Token::BIT_OR: __ orr(int32, int32, Operand(value_)); break;
1119 case Token::BIT_XOR: __ eor(int32, int32, Operand(value_)); break;
1120 case Token::BIT_AND: __ and_(int32, int32, Operand(value_)); break;
1121 case Token::SAR:
1122 ASSERT(!reversed_);
1123 if (shift_value != 0) {
1124 __ mov(int32, Operand(int32, ASR, shift_value));
1125 }
1126 break;
1127 case Token::SHR:
1128 ASSERT(!reversed_);
1129 if (shift_value != 0) {
1130 __ mov(int32, Operand(int32, LSR, shift_value), SetCC);
1131 } else {
1132 // SHR is special because it is required to produce a positive answer.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001133 __ cmp(int32, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001134 }
1135 if (CpuFeatures::IsSupported(VFP3)) {
1136 __ b(mi, &result_not_a_smi);
1137 } else {
1138 // Non VFP code cannot convert from unsigned to double, so fall back
1139 // to GenericBinaryOpStub.
1140 __ b(mi, entry_label());
1141 }
1142 break;
1143 case Token::SHL:
1144 ASSERT(!reversed_);
1145 if (shift_value != 0) {
1146 __ mov(int32, Operand(int32, LSL, shift_value));
1147 }
1148 break;
1149 default: UNREACHABLE();
1150 }
1151 // Check that the *signed* result fits in a smi. Not necessary for AND, SAR
1152 // if the shift if more than 0 or SHR if the shit is more than 1.
1153 if (!( (op_ == Token::AND) ||
1154 ((op_ == Token::SAR) && (shift_value > 0)) ||
1155 ((op_ == Token::SHR) && (shift_value > 1)))) {
1156 __ add(r3, int32, Operand(0x40000000), SetCC);
1157 __ b(mi, &result_not_a_smi);
1158 }
1159 __ mov(tos_register_, Operand(int32, LSL, kSmiTagSize));
1160 Exit();
1161
1162 if (result_not_a_smi.is_linked()) {
1163 __ bind(&result_not_a_smi);
1164 if (overwrite_mode_ != OVERWRITE_LEFT) {
1165 ASSERT((overwrite_mode_ == NO_OVERWRITE) ||
1166 (overwrite_mode_ == OVERWRITE_RIGHT));
1167 // If the allocation fails, fall back to the GenericBinaryOpStub.
1168 __ AllocateHeapNumber(r4, r5, r6, heap_number_map, entry_label());
1169 // Nothing can go wrong now, so overwrite tos.
1170 __ mov(tos_register_, Operand(r4));
1171 }
1172
1173 // int32: answer as signed 32bits integer.
1174 // tos_register_: Heap number to write the answer into.
1175 WriteNonSmiAnswer(int32, tos_register_, r3);
1176
1177 Exit();
1178 }
1179}
1180
1181
1182void DeferredInlineSmiOperation::GenerateAnswerOutOfRange() {
1183 // The input from a bitwise operation were Smis but the result cannot fit
lrn@chromium.org303ada72010-10-27 09:33:13 +00001184 // into a Smi, so we store it into a heap number. VirtualFrame::scratch0()
1185 // holds the untagged result to be converted. tos_register_ contains the
1186 // input. See the calls to JumpToAnswerOutOfRange to see how we got here.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001187 ASSERT(Token::IsBitOp(op_));
1188 ASSERT(!reversed_);
1189
lrn@chromium.org303ada72010-10-27 09:33:13 +00001190 Register untagged_result = VirtualFrame::scratch0();
1191
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001192 if (FLAG_debug_code) {
1193 __ Abort("Should not fall through!");
1194 }
1195
1196 __ bind(&answer_out_of_range_);
1197 if (((value_ & 0x1f) == 0) && (op_ == Token::SHR)) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001198 // >>> 0 is a special case where the untagged_result register is not set up
1199 // yet. We untag the input to get it.
1200 __ mov(untagged_result, Operand(tos_register_, ASR, kSmiTagSize));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001201 }
1202
1203 // This routine uses the registers from r2 to r6. At the moment they are
1204 // not used by the register allocator, but when they are it should use
1205 // SpillAll and MergeTo like DeferredInlineSmiOperation::Generate() above.
1206
1207 // Allocate the result heap number.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001208 Register heap_number_map = VirtualFrame::scratch1();
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001209 Register heap_number = r4;
1210 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1211 // If the allocation fails, fall back to the GenericBinaryOpStub.
1212 __ AllocateHeapNumber(heap_number, r5, r6, heap_number_map, entry_label());
lrn@chromium.org303ada72010-10-27 09:33:13 +00001213 WriteNonSmiAnswer(untagged_result, heap_number, r3);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001214 __ mov(tos_register_, Operand(heap_number));
1215
1216 Exit();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001217}
1218
1219
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001220static bool PopCountLessThanEqual2(unsigned int x) {
1221 x &= x - 1;
1222 return (x & (x - 1)) == 0;
1223}
1224
1225
1226// Returns the index of the lowest bit set.
1227static int BitPosition(unsigned x) {
1228 int bit_posn = 0;
1229 while ((x & 0xf) == 0) {
1230 bit_posn += 4;
1231 x >>= 4;
1232 }
1233 while ((x & 1) == 0) {
1234 bit_posn++;
1235 x >>= 1;
1236 }
1237 return bit_posn;
1238}
1239
1240
ricow@chromium.org65fae842010-08-25 15:26:24 +00001241// Can we multiply by x with max two shifts and an add.
1242// This answers yes to all integers from 2 to 10.
1243static bool IsEasyToMultiplyBy(int x) {
1244 if (x < 2) return false; // Avoid special cases.
1245 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
1246 if (IsPowerOf2(x)) return true; // Simple shift.
1247 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
1248 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
1249 return false;
1250}
1251
1252
1253// Can multiply by anything that IsEasyToMultiplyBy returns true for.
1254// Source and destination may be the same register. This routine does
1255// not set carry and overflow the way a mul instruction would.
1256static void InlineMultiplyByKnownInt(MacroAssembler* masm,
1257 Register source,
1258 Register destination,
1259 int known_int) {
1260 if (IsPowerOf2(known_int)) {
1261 masm->mov(destination, Operand(source, LSL, BitPosition(known_int)));
1262 } else if (PopCountLessThanEqual2(known_int)) {
1263 int first_bit = BitPosition(known_int);
1264 int second_bit = BitPosition(known_int ^ (1 << first_bit));
1265 masm->add(destination, source,
1266 Operand(source, LSL, second_bit - first_bit));
1267 if (first_bit != 0) {
1268 masm->mov(destination, Operand(destination, LSL, first_bit));
1269 }
1270 } else {
1271 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
1272 int the_bit = BitPosition(known_int + 1);
1273 masm->rsb(destination, source, Operand(source, LSL, the_bit));
1274 }
1275}
1276
1277
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001278void CodeGenerator::SmiOperation(Token::Value op,
1279 Handle<Object> value,
1280 bool reversed,
1281 OverwriteMode mode) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001282 int int_value = Smi::cast(*value)->value();
1283
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001284 bool both_sides_are_smi = frame_->KnownSmiAt(0);
1285
ager@chromium.org357bf652010-04-12 11:30:10 +00001286 bool something_to_inline;
1287 switch (op) {
1288 case Token::ADD:
1289 case Token::SUB:
1290 case Token::BIT_AND:
1291 case Token::BIT_OR:
1292 case Token::BIT_XOR: {
1293 something_to_inline = true;
1294 break;
1295 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001296 case Token::SHL: {
1297 something_to_inline = (both_sides_are_smi || !reversed);
1298 break;
1299 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001300 case Token::SHR:
1301 case Token::SAR: {
1302 if (reversed) {
1303 something_to_inline = false;
1304 } else {
1305 something_to_inline = true;
1306 }
1307 break;
1308 }
1309 case Token::MOD: {
1310 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1311 something_to_inline = false;
1312 } else {
1313 something_to_inline = true;
1314 }
1315 break;
1316 }
1317 case Token::MUL: {
1318 if (!IsEasyToMultiplyBy(int_value)) {
1319 something_to_inline = false;
1320 } else {
1321 something_to_inline = true;
1322 }
1323 break;
1324 }
1325 default: {
1326 something_to_inline = false;
1327 break;
1328 }
1329 }
1330
1331 if (!something_to_inline) {
1332 if (!reversed) {
1333 // Push the rhs onto the virtual frame by putting it in a TOS register.
1334 Register rhs = frame_->GetTOSRegister();
1335 __ mov(rhs, Operand(value));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001336 frame_->EmitPush(rhs, TypeInfo::Smi());
1337 GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, int_value);
ager@chromium.org357bf652010-04-12 11:30:10 +00001338 } else {
1339 // Pop the rhs, then push lhs and rhs in the right order. Only performs
1340 // at most one pop, the rest takes place in TOS registers.
1341 Register lhs = frame_->GetTOSRegister(); // Get reg for pushing.
1342 Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this.
1343 __ mov(lhs, Operand(value));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001344 frame_->EmitPush(lhs, TypeInfo::Smi());
1345 TypeInfo t = both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Unknown();
1346 frame_->EmitPush(rhs, t);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001347 GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI,
1348 GenericBinaryOpStub::kUnknownIntValue);
ager@chromium.org357bf652010-04-12 11:30:10 +00001349 }
1350 return;
1351 }
1352
1353 // We move the top of stack to a register (normally no move is invoved).
1354 Register tos = frame_->PopToRegister();
ager@chromium.org357bf652010-04-12 11:30:10 +00001355 switch (op) {
1356 case Token::ADD: {
1357 DeferredCode* deferred =
1358 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1359
1360 __ add(tos, tos, Operand(value), SetCC);
1361 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001362 if (!both_sides_are_smi) {
1363 __ tst(tos, Operand(kSmiTagMask));
1364 deferred->Branch(ne);
1365 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001366 deferred->BindExit();
1367 frame_->EmitPush(tos);
1368 break;
1369 }
1370
1371 case Token::SUB: {
1372 DeferredCode* deferred =
1373 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1374
1375 if (reversed) {
1376 __ rsb(tos, tos, Operand(value), SetCC);
1377 } else {
1378 __ sub(tos, tos, Operand(value), SetCC);
1379 }
1380 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001381 if (!both_sides_are_smi) {
1382 __ tst(tos, Operand(kSmiTagMask));
1383 deferred->Branch(ne);
1384 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001385 deferred->BindExit();
1386 frame_->EmitPush(tos);
1387 break;
1388 }
1389
1390
1391 case Token::BIT_OR:
1392 case Token::BIT_XOR:
1393 case Token::BIT_AND: {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001394 if (both_sides_are_smi) {
1395 switch (op) {
1396 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1397 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001398 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001399 default: UNREACHABLE();
1400 }
1401 frame_->EmitPush(tos, TypeInfo::Smi());
1402 } else {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001403 DeferredInlineSmiOperation* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001404 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1405 __ tst(tos, Operand(kSmiTagMask));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001406 deferred->JumpToNonSmiInput(ne);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001407 switch (op) {
1408 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1409 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001410 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001411 default: UNREACHABLE();
1412 }
1413 deferred->BindExit();
1414 TypeInfo result_type =
1415 (op == Token::BIT_AND) ? TypeInfo::Smi() : TypeInfo::Integer32();
1416 frame_->EmitPush(tos, result_type);
ager@chromium.org357bf652010-04-12 11:30:10 +00001417 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001418 break;
1419 }
1420
1421 case Token::SHL:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001422 if (reversed) {
1423 ASSERT(both_sides_are_smi);
1424 int max_shift = 0;
1425 int max_result = int_value == 0 ? 1 : int_value;
1426 while (Smi::IsValid(max_result << 1)) {
1427 max_shift++;
1428 max_result <<= 1;
1429 }
1430 DeferredCode* deferred =
1431 new DeferredInlineSmiOperation(op, int_value, true, mode, tos);
1432 // Mask off the last 5 bits of the shift operand (rhs). This is part
1433 // of the definition of shift in JS and we know we have a Smi so we
1434 // can safely do this. The masked version gets passed to the
1435 // deferred code, but that makes no difference.
1436 __ and_(tos, tos, Operand(Smi::FromInt(0x1f)));
1437 __ cmp(tos, Operand(Smi::FromInt(max_shift)));
1438 deferred->Branch(ge);
1439 Register scratch = VirtualFrame::scratch0();
1440 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Untag.
1441 __ mov(tos, Operand(Smi::FromInt(int_value))); // Load constant.
1442 __ mov(tos, Operand(tos, LSL, scratch)); // Shift constant.
1443 deferred->BindExit();
1444 TypeInfo result = TypeInfo::Integer32();
1445 frame_->EmitPush(tos, result);
1446 break;
1447 }
1448 // Fall through!
ager@chromium.org357bf652010-04-12 11:30:10 +00001449 case Token::SHR:
1450 case Token::SAR: {
1451 ASSERT(!reversed);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001452 int shift_value = int_value & 0x1f;
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001453 TypeInfo result = TypeInfo::Number();
1454
1455 if (op == Token::SHR) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001456 if (shift_value > 1) {
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001457 result = TypeInfo::Smi();
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001458 } else if (shift_value > 0) {
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001459 result = TypeInfo::Integer32();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001460 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001461 } else if (op == Token::SAR) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001462 if (shift_value > 0) {
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001463 result = TypeInfo::Smi();
1464 } else {
1465 result = TypeInfo::Integer32();
1466 }
1467 } else {
1468 ASSERT(op == Token::SHL);
1469 result = TypeInfo::Integer32();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001470 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001471
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001472 DeferredInlineSmiOperation* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001473 new DeferredInlineSmiOperation(op, shift_value, false, mode, tos);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001474 if (!both_sides_are_smi) {
1475 __ tst(tos, Operand(kSmiTagMask));
1476 deferred->JumpToNonSmiInput(ne);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001477 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001478 switch (op) {
1479 case Token::SHL: {
1480 if (shift_value != 0) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001481 Register untagged_result = VirtualFrame::scratch0();
1482 Register scratch = VirtualFrame::scratch1();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001483 int adjusted_shift = shift_value - kSmiTagSize;
1484 ASSERT(adjusted_shift >= 0);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001485
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001486 if (adjusted_shift != 0) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001487 __ mov(untagged_result, Operand(tos, LSL, adjusted_shift));
1488 } else {
1489 __ mov(untagged_result, Operand(tos));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001490 }
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001491 // Check that the *signed* result fits in a smi.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001492 __ add(scratch, untagged_result, Operand(0x40000000), SetCC);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001493 deferred->JumpToAnswerOutOfRange(mi);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001494 __ mov(tos, Operand(untagged_result, LSL, kSmiTagSize));
ager@chromium.org357bf652010-04-12 11:30:10 +00001495 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001496 break;
1497 }
1498 case Token::SHR: {
ager@chromium.org357bf652010-04-12 11:30:10 +00001499 if (shift_value != 0) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001500 Register untagged_result = VirtualFrame::scratch0();
1501 // Remove tag.
1502 __ mov(untagged_result, Operand(tos, ASR, kSmiTagSize));
1503 __ mov(untagged_result, Operand(untagged_result, LSR, shift_value));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001504 if (shift_value == 1) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001505 // Check that the *unsigned* result fits in a smi.
1506 // Neither of the two high-order bits can be set:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001507 // - 0x80000000: high bit would be lost when smi tagging
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001508 // - 0x40000000: this number would convert to negative when Smi
1509 // tagging.
1510 // These two cases can only happen with shifts by 0 or 1 when
1511 // handed a valid smi.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001512 __ tst(untagged_result, Operand(0xc0000000));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001513 deferred->JumpToAnswerOutOfRange(ne);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001514 }
lrn@chromium.org303ada72010-10-27 09:33:13 +00001515 __ mov(tos, Operand(untagged_result, LSL, kSmiTagSize));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001516 } else {
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001517 __ cmp(tos, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001518 deferred->JumpToAnswerOutOfRange(mi);
ager@chromium.org357bf652010-04-12 11:30:10 +00001519 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001520 break;
1521 }
1522 case Token::SAR: {
1523 if (shift_value != 0) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001524 // Do the shift and the tag removal in one operation. If the shift
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001525 // is 31 bits (the highest possible value) then we emit the
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001526 // instruction as a shift by 0 which in the ARM ISA means shift
1527 // arithmetically by 32.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001528 __ mov(tos, Operand(tos, ASR, (kSmiTagSize + shift_value) & 0x1f));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001529 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
ager@chromium.org357bf652010-04-12 11:30:10 +00001530 }
1531 break;
1532 }
1533 default: UNREACHABLE();
1534 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001535 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001536 frame_->EmitPush(tos, result);
ager@chromium.org357bf652010-04-12 11:30:10 +00001537 break;
1538 }
1539
1540 case Token::MOD: {
1541 ASSERT(!reversed);
1542 ASSERT(int_value >= 2);
1543 ASSERT(IsPowerOf2(int_value));
1544 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001545 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001546 unsigned mask = (0x80000000u | kSmiTagMask);
1547 __ tst(tos, Operand(mask));
1548 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1549 mask = (int_value << kSmiTagSize) - 1;
1550 __ and_(tos, tos, Operand(mask));
1551 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001552 // Mod of positive power of 2 Smi gives a Smi if the lhs is an integer.
1553 frame_->EmitPush(
1554 tos,
1555 both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Number());
ager@chromium.org357bf652010-04-12 11:30:10 +00001556 break;
1557 }
1558
1559 case Token::MUL: {
1560 ASSERT(IsEasyToMultiplyBy(int_value));
1561 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001562 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001563 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1564 max_smi_that_wont_overflow <<= kSmiTagSize;
1565 unsigned mask = 0x80000000u;
1566 while ((mask & max_smi_that_wont_overflow) == 0) {
1567 mask |= mask >> 1;
1568 }
1569 mask |= kSmiTagMask;
1570 // This does a single mask that checks for a too high value in a
1571 // conservative way and for a non-Smi. It also filters out negative
1572 // numbers, unfortunately, but since this code is inline we prefer
1573 // brevity to comprehensiveness.
1574 __ tst(tos, Operand(mask));
1575 deferred->Branch(ne);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001576 InlineMultiplyByKnownInt(masm_, tos, tos, int_value);
ager@chromium.org357bf652010-04-12 11:30:10 +00001577 deferred->BindExit();
1578 frame_->EmitPush(tos);
1579 break;
1580 }
1581
1582 default:
1583 UNREACHABLE();
1584 break;
1585 }
1586}
1587
1588
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001589void CodeGenerator::Comparison(Condition cc,
1590 Expression* left,
1591 Expression* right,
1592 bool strict) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001593 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001594
ager@chromium.org357bf652010-04-12 11:30:10 +00001595 if (left != NULL) Load(left);
1596 if (right != NULL) Load(right);
1597
mads.s.ager31e71382008-08-13 09:32:07 +00001598 // sp[0] : y
1599 // sp[1] : x
1600 // result : cc register
1601
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602 // Strict only makes sense for equality comparisons.
1603 ASSERT(!strict || cc == eq);
1604
ager@chromium.org357bf652010-04-12 11:30:10 +00001605 Register lhs;
1606 Register rhs;
1607
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001608 bool lhs_is_smi;
1609 bool rhs_is_smi;
1610
ager@chromium.org357bf652010-04-12 11:30:10 +00001611 // We load the top two stack positions into registers chosen by the virtual
1612 // frame. This should keep the register shuffling to a minimum.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001613 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1614 if (cc == gt || cc == le) {
1615 cc = ReverseCondition(cc);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001616 lhs_is_smi = frame_->KnownSmiAt(0);
1617 rhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001618 lhs = frame_->PopToRegister();
1619 rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001620 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001621 rhs_is_smi = frame_->KnownSmiAt(0);
1622 lhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001623 rhs = frame_->PopToRegister();
1624 lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001625 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001626
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001627 bool both_sides_are_smi = (lhs_is_smi && rhs_is_smi);
1628
ager@chromium.org357bf652010-04-12 11:30:10 +00001629 ASSERT(rhs.is(r0) || rhs.is(r1));
1630 ASSERT(lhs.is(r0) || lhs.is(r1));
1631
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001632 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001634 if (!both_sides_are_smi) {
1635 // Now we have the two sides in r0 and r1. We flush any other registers
1636 // because the stub doesn't know about register allocation.
1637 frame_->SpillAll();
1638 Register scratch = VirtualFrame::scratch0();
1639 Register smi_test_reg;
1640 if (lhs_is_smi) {
1641 smi_test_reg = rhs;
1642 } else if (rhs_is_smi) {
1643 smi_test_reg = lhs;
1644 } else {
1645 __ orr(scratch, lhs, Operand(rhs));
1646 smi_test_reg = scratch;
1647 }
1648 __ tst(smi_test_reg, Operand(kSmiTagMask));
1649 JumpTarget smi;
1650 smi.Branch(eq);
1651
1652 // Perform non-smi comparison by stub.
1653 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1654 // We call with 0 args because there are 0 on the stack.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00001655 CompareStub stub(cc, strict, NO_SMI_COMPARE_IN_STUB, lhs, rhs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001656 frame_->CallStub(&stub, 0);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001657 __ cmp(r0, Operand(0, RelocInfo::NONE));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001658 exit.Jump();
1659
1660 smi.Bind();
ager@chromium.org357bf652010-04-12 11:30:10 +00001661 }
1662
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001663 // Do smi comparisons by pointer comparison.
ager@chromium.org357bf652010-04-12 11:30:10 +00001664 __ cmp(lhs, Operand(rhs));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001666 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001667 cc_reg_ = cc;
1668}
1669
1670
mads.s.ager31e71382008-08-13 09:32:07 +00001671// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001672void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001673 CallFunctionFlags flags,
1674 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001676 int arg_count = args->length();
1677 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001678 Load(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001679 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680
kasper.lund7276f142008-07-30 08:49:36 +00001681 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001682 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683
kasper.lund7276f142008-07-30 08:49:36 +00001684 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001685 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001686 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001687 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688
1689 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001690 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001691 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001692}
1693
1694
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001695void CodeGenerator::CallApplyLazy(Expression* applicand,
1696 Expression* receiver,
1697 VariableProxy* arguments,
1698 int position) {
1699 // An optimized implementation of expressions of the form
1700 // x.apply(y, arguments).
1701 // If the arguments object of the scope has not been allocated,
1702 // and x.apply is Function.prototype.apply, this optimization
1703 // just copies y and the arguments of the current function on the
1704 // stack, as receiver and arguments, and calls x.
1705 // In the implementation comments, we call x the applicand
1706 // and y the receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001707
1708 ASSERT(ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION);
1709 ASSERT(arguments->IsArguments());
1710
1711 // Load applicand.apply onto the stack. This will usually
1712 // give us a megamorphic load site. Not super, but it works.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001713 Load(applicand);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001714 Handle<String> name = Factory::LookupAsciiSymbol("apply");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001715 frame_->Dup();
ager@chromium.orgac091b72010-05-05 07:34:42 +00001716 frame_->CallLoadIC(name, RelocInfo::CODE_TARGET);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001717 frame_->EmitPush(r0);
1718
1719 // Load the receiver and the existing arguments object onto the
1720 // expression stack. Avoid allocating the arguments object here.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001721 Load(receiver);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001722 LoadFromSlot(scope()->arguments()->AsSlot(), NOT_INSIDE_TYPEOF);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001723
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001724 // At this point the top two stack elements are probably in registers
1725 // since they were just loaded. Ensure they are in regs and get the
1726 // regs.
1727 Register receiver_reg = frame_->Peek2();
1728 Register arguments_reg = frame_->Peek();
1729
1730 // From now on the frame is spilled.
1731 frame_->SpillAll();
1732
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001733 // Emit the source position information after having loaded the
1734 // receiver and the arguments.
1735 CodeForSourcePosition(position);
1736 // Contents of the stack at this point:
1737 // sp[0]: arguments object of the current function or the hole.
1738 // sp[1]: receiver
1739 // sp[2]: applicand.apply
1740 // sp[3]: applicand.
1741
1742 // Check if the arguments object has been lazily allocated
1743 // already. If so, just use that instead of copying the arguments
1744 // from the stack. This also deals with cases where a local variable
1745 // named 'arguments' has been introduced.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001746 JumpTarget slow;
1747 Label done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001748 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001749 __ cmp(ip, arguments_reg);
1750 slow.Branch(ne);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001751
1752 Label build_args;
1753 // Get rid of the arguments object probe.
1754 frame_->Drop();
1755 // Stack now has 3 elements on it.
1756 // Contents of stack at this point:
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001757 // sp[0]: receiver - in the receiver_reg register.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001758 // sp[1]: applicand.apply
1759 // sp[2]: applicand.
1760
1761 // Check that the receiver really is a JavaScript object.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001762 __ BranchOnSmi(receiver_reg, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001763 // We allow all JSObjects including JSFunctions. As long as
1764 // JS_FUNCTION_TYPE is the last instance type and it is right
1765 // after LAST_JS_OBJECT_TYPE, we do not have to check the upper
1766 // bound.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00001767 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1768 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001769 __ CompareObjectType(receiver_reg, r2, r3, FIRST_JS_OBJECT_TYPE);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001770 __ b(lt, &build_args);
1771
1772 // Check that applicand.apply is Function.prototype.apply.
1773 __ ldr(r0, MemOperand(sp, kPointerSize));
1774 __ BranchOnSmi(r0, &build_args);
1775 __ CompareObjectType(r0, r1, r2, JS_FUNCTION_TYPE);
1776 __ b(ne, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001777 Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply));
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001778 __ ldr(r1, FieldMemOperand(r0, JSFunction::kCodeEntryOffset));
1779 __ sub(r1, r1, Operand(Code::kHeaderSize - kHeapObjectTag));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001780 __ cmp(r1, Operand(apply_code));
1781 __ b(ne, &build_args);
1782
1783 // Check that applicand is a function.
1784 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1785 __ BranchOnSmi(r1, &build_args);
1786 __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE);
1787 __ b(ne, &build_args);
1788
1789 // Copy the arguments to this function possibly from the
1790 // adaptor frame below it.
1791 Label invoke, adapted;
1792 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1793 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1794 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1795 __ b(eq, &adapted);
1796
1797 // No arguments adaptor frame. Copy fixed number of arguments.
1798 __ mov(r0, Operand(scope()->num_parameters()));
1799 for (int i = 0; i < scope()->num_parameters(); i++) {
1800 __ ldr(r2, frame_->ParameterAt(i));
1801 __ push(r2);
1802 }
1803 __ jmp(&invoke);
1804
1805 // Arguments adaptor frame present. Copy arguments from there, but
1806 // avoid copying too many arguments to avoid stack overflows.
1807 __ bind(&adapted);
1808 static const uint32_t kArgumentsLimit = 1 * KB;
1809 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1810 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
1811 __ mov(r3, r0);
1812 __ cmp(r0, Operand(kArgumentsLimit));
1813 __ b(gt, &build_args);
1814
1815 // Loop through the arguments pushing them onto the execution
1816 // stack. We don't inform the virtual frame of the push, so we don't
1817 // have to worry about getting rid of the elements from the virtual
1818 // frame.
1819 Label loop;
1820 // r3 is a small non-negative integer, due to the test above.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001821 __ cmp(r3, Operand(0, RelocInfo::NONE));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001822 __ b(eq, &invoke);
1823 // Compute the address of the first argument.
1824 __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2));
1825 __ add(r2, r2, Operand(kPointerSize));
1826 __ bind(&loop);
1827 // Post-decrement argument address by kPointerSize on each iteration.
1828 __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex));
1829 __ push(r4);
1830 __ sub(r3, r3, Operand(1), SetCC);
1831 __ b(gt, &loop);
1832
1833 // Invoke the function.
1834 __ bind(&invoke);
1835 ParameterCount actual(r0);
1836 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1837 // Drop applicand.apply and applicand from the stack, and push
1838 // the result of the function call, but leave the spilled frame
1839 // unchanged, with 3 elements, so it is correct when we compile the
1840 // slow-case code.
1841 __ add(sp, sp, Operand(2 * kPointerSize));
1842 __ push(r0);
1843 // Stack now has 1 element:
1844 // sp[0]: result
1845 __ jmp(&done);
1846
1847 // Slow-case: Allocate the arguments object since we know it isn't
1848 // there, and fall-through to the slow-case where we call
1849 // applicand.apply.
1850 __ bind(&build_args);
1851 // Stack now has 3 elements, because we have jumped from where:
1852 // sp[0]: receiver
1853 // sp[1]: applicand.apply
1854 // sp[2]: applicand.
1855 StoreArgumentsObject(false);
1856
1857 // Stack and frame now have 4 elements.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001858 slow.Bind();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001859
1860 // Generic computation of x.apply(y, args) with no special optimization.
1861 // Flip applicand.apply and applicand on the stack, so
1862 // applicand looks like the receiver of the applicand.apply call.
1863 // Then process it as a normal function call.
1864 __ ldr(r0, MemOperand(sp, 3 * kPointerSize));
1865 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001866 __ Strd(r0, r1, MemOperand(sp, 2 * kPointerSize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001867
1868 CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS);
1869 frame_->CallStub(&call_function, 3);
1870 // The function and its two arguments have been dropped.
1871 frame_->Drop(); // Drop the receiver as well.
1872 frame_->EmitPush(r0);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001873 frame_->SpillAll(); // A spilled frame is also jumping to label done.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001874 // Stack now has 1 element:
1875 // sp[0]: result
1876 __ bind(&done);
1877
1878 // Restore the context register after a call.
1879 __ ldr(cp, frame_->Context());
1880}
1881
1882
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001883void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 ASSERT(has_cc());
1885 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001886 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887 cc_reg_ = al;
1888}
1889
1890
ager@chromium.org7c537e22008-10-16 08:43:32 +00001891void CodeGenerator::CheckStack() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001892 frame_->SpillAll();
ager@chromium.org3811b432009-10-28 14:53:37 +00001893 Comment cmnt(masm_, "[ check stack");
1894 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
ager@chromium.org3811b432009-10-28 14:53:37 +00001895 masm_->cmp(sp, Operand(ip));
1896 StackCheckStub stub;
1897 // Call the stub if lower.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001898 masm_->mov(ip,
ager@chromium.org3811b432009-10-28 14:53:37 +00001899 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1900 RelocInfo::CODE_TARGET),
1901 LeaveCC,
1902 lo);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001903 masm_->Call(ip, lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001904}
1905
1906
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001907void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1908#ifdef DEBUG
1909 int original_height = frame_->height();
1910#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001911 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001912 Visit(statements->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001913 }
1914 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1915}
1916
1917
ager@chromium.org7c537e22008-10-16 08:43:32 +00001918void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001919#ifdef DEBUG
1920 int original_height = frame_->height();
1921#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001923 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001924 node->break_target()->SetExpectedHeight();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001925 VisitStatements(node->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001926 if (node->break_target()->is_linked()) {
1927 node->break_target()->Bind();
1928 }
1929 node->break_target()->Unuse();
1930 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931}
1932
1933
ager@chromium.org7c537e22008-10-16 08:43:32 +00001934void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org3811b432009-10-28 14:53:37 +00001935 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001936 frame_->EmitPush(Operand(pairs));
1937 frame_->EmitPush(Operand(Smi::FromInt(is_eval() ? 1 : 0)));
1938
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001939 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001940 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941}
1942
1943
ager@chromium.org7c537e22008-10-16 08:43:32 +00001944void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001945#ifdef DEBUG
1946 int original_height = frame_->height();
1947#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 Comment cmnt(masm_, "[ Declaration");
1949 Variable* var = node->proxy()->var();
1950 ASSERT(var != NULL); // must have been resolved
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001951 Slot* slot = var->AsSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952
1953 // If it was not possible to allocate the variable at compile time,
1954 // we need to "declare" it at runtime to make sure it actually
1955 // exists in the local context.
1956 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1957 // Variables with a "LOOKUP" slot were introduced as non-locals
1958 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001959 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001961 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001962 frame_->EmitPush(Operand(var->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963 // Declaration nodes are always declared in only two modes.
1964 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1965 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001966 frame_->EmitPush(Operand(Smi::FromInt(attr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967 // Push initial value, if any.
1968 // Note: For variables we must not push an initial value (such as
1969 // 'undefined') because we may have a (legal) redeclaration and we
1970 // must not destroy the current value.
1971 if (node->mode() == Variable::CONST) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001972 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 } else if (node->fun() != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001974 Load(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975 } else {
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001976 frame_->EmitPush(Operand(0, RelocInfo::NONE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00001978
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001979 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001980 // Ignore the return value (declarations are statements).
ager@chromium.orgac091b72010-05-05 07:34:42 +00001981
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001982 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983 return;
1984 }
1985
1986 ASSERT(!var->is_global());
1987
1988 // If we have a function or a constant, we need to initialize the variable.
1989 Expression* val = NULL;
1990 if (node->mode() == Variable::CONST) {
1991 val = new Literal(Factory::the_hole_value());
1992 } else {
1993 val = node->fun(); // NULL if we don't have a function
1994 }
1995
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001996
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 if (val != NULL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001998 WriteBarrierCharacter wb_info =
1999 val->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
2000 if (val->AsLiteral() != NULL) wb_info = NEVER_NEWSPACE;
ager@chromium.orgac091b72010-05-05 07:34:42 +00002001 // Set initial value.
2002 Reference target(this, node->proxy());
2003 Load(val);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002004 target.SetValue(NOT_CONST_INIT, wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002005
iposva@chromium.org245aa852009-02-10 00:49:54 +00002006 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002007 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002009 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010}
2011
2012
ager@chromium.org7c537e22008-10-16 08:43:32 +00002013void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002014#ifdef DEBUG
2015 int original_height = frame_->height();
2016#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002018 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019 Expression* expression = node->expression();
2020 expression->MarkAsStatement();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002021 Load(expression);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002022 frame_->Drop();
2023 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024}
2025
2026
ager@chromium.org7c537e22008-10-16 08:43:32 +00002027void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002028#ifdef DEBUG
2029 int original_height = frame_->height();
2030#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002032 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002033 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002034 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035}
2036
2037
ager@chromium.org7c537e22008-10-16 08:43:32 +00002038void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002039#ifdef DEBUG
2040 int original_height = frame_->height();
2041#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002043 // Generate different code depending on which parts of the if statement
2044 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 bool has_then_stm = node->HasThenStatement();
2046 bool has_else_stm = node->HasElseStatement();
2047
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002048 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002050 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00002052 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002053 JumpTarget then;
2054 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002056 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002057 if (frame_ != NULL) {
2058 Branch(false, &else_);
2059 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002061 if (frame_ != NULL || then.is_linked()) {
2062 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002063 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002064 }
2065 if (frame_ != NULL) {
2066 exit.Jump();
2067 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002069 if (else_.is_linked()) {
2070 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002071 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002072 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073
2074 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00002075 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002077 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002079 LoadCondition(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002080 if (frame_ != NULL) {
2081 Branch(false, &exit);
2082 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002084 if (frame_ != NULL || then.is_linked()) {
2085 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002086 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002087 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088
2089 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00002090 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002092 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 // if (!cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002094 LoadCondition(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002095 if (frame_ != NULL) {
2096 Branch(true, &exit);
2097 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002098 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002099 if (frame_ != NULL || else_.is_linked()) {
2100 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002101 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002102 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103
2104 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002105 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 ASSERT(!has_then_stm && !has_else_stm);
2107 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002108 LoadCondition(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002109 if (frame_ != NULL) {
2110 if (has_cc()) {
2111 cc_reg_ = al;
2112 } else {
2113 frame_->Drop();
2114 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115 }
2116 }
2117
2118 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002119 if (exit.is_linked()) {
2120 exit.Bind();
2121 }
2122 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123}
2124
2125
ager@chromium.org7c537e22008-10-16 08:43:32 +00002126void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002128 CodeForStatementPosition(node);
2129 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130}
2131
2132
ager@chromium.org7c537e22008-10-16 08:43:32 +00002133void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002135 CodeForStatementPosition(node);
2136 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137}
2138
2139
ager@chromium.org7c537e22008-10-16 08:43:32 +00002140void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002141 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00002142
ager@chromium.org4af710e2009-09-15 12:20:11 +00002143 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002144 Load(node->expression());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002145 frame_->PopToR0();
2146 frame_->PrepareForReturn();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002147 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002148 function_return_.Jump();
2149 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002150 // Pop the result from the frame and prepare the frame for
2151 // returning thus making it easier to merge.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002152 if (function_return_.is_bound()) {
2153 // If the function return label is already bound we reuse the
2154 // code by jumping to the return site.
2155 function_return_.Jump();
2156 } else {
2157 function_return_.Bind();
2158 GenerateReturnSequence();
2159 }
2160 }
2161}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002162
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002163
2164void CodeGenerator::GenerateReturnSequence() {
2165 if (FLAG_trace) {
2166 // Push the return value on the stack as the parameter.
2167 // Runtime::TraceExit returns the parameter as it is.
2168 frame_->EmitPush(r0);
2169 frame_->CallRuntime(Runtime::kTraceExit, 1);
2170 }
2171
2172#ifdef DEBUG
2173 // Add a label for checking the size of the code used for returning.
2174 Label check_exit_codesize;
2175 masm_->bind(&check_exit_codesize);
2176#endif
2177 // Make sure that the constant pool is not emitted inside of the return
2178 // sequence.
2179 { Assembler::BlockConstPoolScope block_const_pool(masm_);
2180 // Tear down the frame which will restore the caller's frame pointer and
2181 // the link register.
2182 frame_->Exit();
2183
2184 // Here we use masm_-> instead of the __ macro to avoid the code coverage
2185 // tool from instrumenting as we rely on the code size here.
2186 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
2187 masm_->add(sp, sp, Operand(sp_delta));
2188 masm_->Jump(lr);
2189 DeleteFrame();
2190
2191#ifdef DEBUG
2192 // Check that the size of the code used for returning matches what is
2193 // expected by the debugger. If the sp_delts above cannot be encoded in
2194 // the add instruction the add will generate two instructions.
2195 int return_sequence_length =
2196 masm_->InstructionsGeneratedSince(&check_exit_codesize);
2197 CHECK(return_sequence_length ==
2198 Assembler::kJSReturnSequenceInstructions ||
2199 return_sequence_length ==
2200 Assembler::kJSReturnSequenceInstructions + 1);
2201#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002202 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203}
2204
2205
ager@chromium.org7c537e22008-10-16 08:43:32 +00002206void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002207#ifdef DEBUG
2208 int original_height = frame_->height();
2209#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002211 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002212 Load(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002213 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002214 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002215 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002216 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002217 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002218#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002219 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002220 __ cmp(r0, cp);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002221 verified_true.Branch(eq);
2222 __ stop("PushContext: r0 is expected to be the same as cp");
2223 verified_true.Bind();
2224#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002226 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002227 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228}
2229
2230
ager@chromium.org7c537e22008-10-16 08:43:32 +00002231void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002232#ifdef DEBUG
2233 int original_height = frame_->height();
2234#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002236 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 // Pop context.
2238 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
2239 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002240 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002241 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242}
2243
2244
ager@chromium.org7c537e22008-10-16 08:43:32 +00002245void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002246#ifdef DEBUG
2247 int original_height = frame_->height();
2248#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002250 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002251 node->break_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002253 Load(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002254
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002255 JumpTarget next_test;
2256 JumpTarget fall_through;
2257 JumpTarget default_entry;
2258 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002259 ZoneList<CaseClause*>* cases = node->cases();
2260 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002261 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262
2263 for (int i = 0; i < length; i++) {
2264 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002266 // Remember the default clause and compile it at the end.
2267 default_clause = clause;
2268 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269 }
2270
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002271 Comment cmnt(masm_, "[ Case clause");
2272 // Compile the test.
2273 next_test.Bind();
2274 next_test.Unuse();
2275 // Duplicate TOS.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002276 frame_->Dup();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002277 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002278 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002279
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002280 // Before entering the body from the test, remove the switch value from
2281 // the stack.
2282 frame_->Drop();
2283
2284 // Label the body so that fall through is enabled.
2285 if (i > 0 && cases->at(i - 1)->is_default()) {
2286 default_exit.Bind();
2287 } else {
2288 fall_through.Bind();
2289 fall_through.Unuse();
2290 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002291 VisitStatements(clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002292
2293 // If control flow can fall through from the body, jump to the next body
2294 // or the end of the statement.
2295 if (frame_ != NULL) {
2296 if (i < length - 1 && cases->at(i + 1)->is_default()) {
2297 default_entry.Jump();
2298 } else {
2299 fall_through.Jump();
2300 }
2301 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 }
2303
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002304 // The final "test" removes the switch value.
2305 next_test.Bind();
2306 frame_->Drop();
2307
2308 // If there is a default clause, compile it.
2309 if (default_clause != NULL) {
2310 Comment cmnt(masm_, "[ Default clause");
2311 default_entry.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002312 VisitStatements(default_clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002313 // If control flow can fall out of the default and there is a case after
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002314 // it, jump to that case's body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002315 if (frame_ != NULL && default_exit.is_bound()) {
2316 default_exit.Jump();
2317 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002318 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002320 if (fall_through.is_linked()) {
2321 fall_through.Bind();
2322 }
2323
2324 if (node->break_target()->is_linked()) {
2325 node->break_target()->Bind();
2326 }
2327 node->break_target()->Unuse();
2328 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329}
2330
2331
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002332void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002333#ifdef DEBUG
2334 int original_height = frame_->height();
2335#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002336 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002337 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002338 node->break_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002339 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002340 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002341
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002342 // Label the top of the loop for the backward CFG edge. If the test
2343 // is always true we can use the continue target, and if the test is
2344 // always false there is no need.
2345 ConditionAnalysis info = AnalyzeCondition(node->cond());
2346 switch (info) {
2347 case ALWAYS_TRUE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002348 node->continue_target()->SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002349 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002350 break;
2351 case ALWAYS_FALSE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002352 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002353 break;
2354 case DONT_KNOW:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002355 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002356 body.Bind();
2357 break;
2358 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002359
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002360 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002361 Visit(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002362
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002363 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002364 switch (info) {
2365 case ALWAYS_TRUE:
2366 // If control can fall off the end of the body, jump back to the
2367 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002368 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002369 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002370 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002371 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002372 case ALWAYS_FALSE:
2373 // If we have a continue in the body, we only have to bind its
2374 // jump target.
2375 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002376 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002377 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002378 break;
2379 case DONT_KNOW:
2380 // We have to compile the test expression if it can be reached by
2381 // control flow falling out of the body or via continue.
2382 if (node->continue_target()->is_linked()) {
2383 node->continue_target()->Bind();
2384 }
2385 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002386 Comment cmnt(masm_, "[ DoWhileCondition");
2387 CodeForDoWhileConditionPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002388 LoadCondition(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002389 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002390 // A invalid frame here indicates that control did not
2391 // fall out of the test expression.
2392 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002393 }
2394 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002395 break;
2396 }
2397
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002398 if (node->break_target()->is_linked()) {
2399 node->break_target()->Bind();
2400 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002401 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002402 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2403}
2404
2405
2406void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
2407#ifdef DEBUG
2408 int original_height = frame_->height();
2409#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002410 Comment cmnt(masm_, "[ WhileStatement");
2411 CodeForStatementPosition(node);
2412
2413 // If the test is never true and has no side effects there is no need
2414 // to compile the test or body.
2415 ConditionAnalysis info = AnalyzeCondition(node->cond());
2416 if (info == ALWAYS_FALSE) return;
2417
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002418 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002419 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002420
2421 // Label the top of the loop with the continue target for the backward
2422 // CFG edge.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002423 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002424 node->continue_target()->Bind();
2425
2426 if (info == DONT_KNOW) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002427 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002428 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002429 if (has_valid_frame()) {
2430 // A NULL frame indicates that control did not fall out of the
2431 // test expression.
2432 Branch(false, node->break_target());
2433 }
2434 if (has_valid_frame() || body.is_linked()) {
2435 body.Bind();
2436 }
2437 }
2438
2439 if (has_valid_frame()) {
2440 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002441 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002442
2443 // If control flow can fall out of the body, jump back to the top.
2444 if (has_valid_frame()) {
2445 node->continue_target()->Jump();
2446 }
2447 }
2448 if (node->break_target()->is_linked()) {
2449 node->break_target()->Bind();
2450 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002451 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002452 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2453}
2454
2455
2456void CodeGenerator::VisitForStatement(ForStatement* node) {
2457#ifdef DEBUG
2458 int original_height = frame_->height();
2459#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002460 Comment cmnt(masm_, "[ ForStatement");
2461 CodeForStatementPosition(node);
2462 if (node->init() != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002463 Visit(node->init());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002464 }
2465
2466 // If the test is never true there is no need to compile the test or
2467 // body.
2468 ConditionAnalysis info = AnalyzeCondition(node->cond());
2469 if (info == ALWAYS_FALSE) return;
2470
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002471 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002472 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002473
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002474 // We know that the loop index is a smi if it is not modified in the
2475 // loop body and it is checked against a constant limit in the loop
2476 // condition. In this case, we reset the static type information of the
2477 // loop index to smi before compiling the body, the update expression, and
2478 // the bottom check of the loop condition.
2479 TypeInfoCodeGenState type_info_scope(this,
2480 node->is_fast_smi_loop() ?
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002481 node->loop_variable()->AsSlot() :
2482 NULL,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002483 TypeInfo::Smi());
2484
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002485 // If there is no update statement, label the top of the loop with the
2486 // continue target, otherwise with the loop target.
2487 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2488 if (node->next() == NULL) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002489 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002490 node->continue_target()->Bind();
2491 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002492 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002493 loop.Bind();
2494 }
2495
2496 // If the test is always true, there is no need to compile it.
2497 if (info == DONT_KNOW) {
2498 JumpTarget body;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002499 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002500 if (has_valid_frame()) {
2501 Branch(false, node->break_target());
2502 }
2503 if (has_valid_frame() || body.is_linked()) {
2504 body.Bind();
2505 }
2506 }
2507
2508 if (has_valid_frame()) {
2509 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002510 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002511
2512 if (node->next() == NULL) {
2513 // If there is no update statement and control flow can fall out
2514 // of the loop, jump directly to the continue label.
2515 if (has_valid_frame()) {
2516 node->continue_target()->Jump();
2517 }
2518 } else {
2519 // If there is an update statement and control flow can reach it
2520 // via falling out of the body of the loop or continuing, we
2521 // compile the update statement.
2522 if (node->continue_target()->is_linked()) {
2523 node->continue_target()->Bind();
2524 }
2525 if (has_valid_frame()) {
2526 // Record source position of the statement as this code which is
2527 // after the code for the body actually belongs to the loop
2528 // statement and not the body.
2529 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002530 Visit(node->next());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002531 loop.Jump();
2532 }
2533 }
2534 }
2535 if (node->break_target()->is_linked()) {
2536 node->break_target()->Bind();
2537 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002538 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002539 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002540}
2541
2542
ager@chromium.org7c537e22008-10-16 08:43:32 +00002543void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002544#ifdef DEBUG
2545 int original_height = frame_->height();
2546#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002548 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002549
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002550 JumpTarget primitive;
2551 JumpTarget jsobject;
2552 JumpTarget fixed_array;
2553 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2554 JumpTarget end_del_check;
2555 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556
2557 // Get the object to enumerate over (converted to JSObject).
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002558 Load(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002559
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002560 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002561 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2562 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002563 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002564 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2565 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002566 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002567 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2568 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002569 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570
2571 // Stack layout in body:
2572 // [iteration counter (Smi)]
2573 // [length of array]
2574 // [FixedArray]
2575 // [Map or 0]
2576 // [Object]
2577
2578 // Check if enumerable is already a JSObject
2579 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002580 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002581 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002582 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002583
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002584 primitive.Bind();
2585 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002586 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002588 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002590 // r0: value to be iterated over
2591 frame_->EmitPush(r0); // Push the object being iterated over.
2592
2593 // Check cache validity in generated code. This is a fast case for
2594 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2595 // guarantee cache validity, call the runtime system to check cache
2596 // validity or get the property names in a fixed array.
2597 JumpTarget call_runtime;
2598 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2599 JumpTarget check_prototype;
2600 JumpTarget use_cache;
2601 __ mov(r1, Operand(r0));
2602 loop.Bind();
2603 // Check that there are no elements.
2604 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2605 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2606 __ cmp(r2, r4);
2607 call_runtime.Branch(ne);
2608 // Check that instance descriptors are not empty so that we can
2609 // check for an enum cache. Leave the map in r3 for the subsequent
2610 // prototype load.
2611 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2612 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2613 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2614 __ cmp(r2, ip);
2615 call_runtime.Branch(eq);
2616 // Check that there in an enum cache in the non-empty instance
2617 // descriptors. This is the case if the next enumeration index
2618 // field does not contain a smi.
2619 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2620 __ tst(r2, Operand(kSmiTagMask));
2621 call_runtime.Branch(eq);
2622 // For all objects but the receiver, check that the cache is empty.
2623 // r4: empty fixed array root.
2624 __ cmp(r1, r0);
2625 check_prototype.Branch(eq);
2626 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2627 __ cmp(r2, r4);
2628 call_runtime.Branch(ne);
2629 check_prototype.Bind();
2630 // Load the prototype from the map and loop if non-null.
2631 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2632 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2633 __ cmp(r1, ip);
2634 loop.Branch(ne);
2635 // The enum cache is valid. Load the map of the object being
2636 // iterated over and use the cache for the iteration.
2637 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2638 use_cache.Jump();
2639
2640 call_runtime.Bind();
2641 // Call the runtime to get the property names for the object.
2642 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002643 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002645 // If we got a map from the runtime call, we can do a fast
2646 // modification check. Otherwise, we got a fixed array, and we have
2647 // to do a slow check.
2648 // r0: map or fixed array (result from call to
2649 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002650 __ mov(r2, Operand(r0));
2651 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002652 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2653 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002654 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002656 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002658 // r0: map (either the result from a call to
2659 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2660 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002661 __ mov(r1, Operand(r0));
2662 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2663 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2664 __ ldr(r2,
2665 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2666
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002667 frame_->EmitPush(r0); // map
2668 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002669 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002670 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002671 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002672 frame_->EmitPush(r0);
2673 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002675 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002676 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002677 frame_->EmitPush(r1); // insert 0 in place of Map
2678 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679
2680 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002681 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002682 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002683 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002684 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002685
2686 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002687 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002688 // sp[0] : index
2689 // sp[1] : array/enum cache length
2690 // sp[2] : array or enum cache
2691 // sp[3] : 0 or map
2692 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002693 // Grab the current frame's height for the break and continue
2694 // targets only after all the state is pushed on the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002695 node->break_target()->SetExpectedHeight();
2696 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002697
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002698 // Load the current count to r0, load the length to r1.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002699 __ Ldrd(r0, r1, frame_->ElementAt(0));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002700 __ cmp(r0, r1); // compare to the array length
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002701 node->break_target()->Branch(hs);
2702
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002704 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2706 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2707
2708 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002709 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710 // Check if this (still) matches the map of the enumerable.
2711 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002712 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2714 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002715 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002716
2717 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002718 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2719 frame_->EmitPush(r0);
2720 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002721 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002722 __ mov(r3, Operand(r0), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723 // If the property has been removed while iterating, we just skip it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002724 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002726 end_del_check.Bind();
2727 // Store the entry in the 'each' expression and take another spin in the
2728 // loop. r3: i'th entry of the enum cache (or string there of)
2729 frame_->EmitPush(r3); // push entry
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002730 { VirtualFrame::RegisterAllocationScope scope(this);
2731 Reference each(this, node->each());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002732 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002733 if (each.size() > 0) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002734 // Loading a reference may leave the frame in an unspilled state.
2735 frame_->SpillAll(); // Sync stack to memory.
2736 // Get the value (under the reference on the stack) from memory.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002737 __ ldr(r0, frame_->ElementAt(each.size()));
2738 frame_->EmitPush(r0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002739 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002740 frame_->Drop(2); // The result of the set and the extra pushed value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002741 } else {
2742 // If the reference was to a slot we rely on the convenient property
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002743 // that it doesn't matter whether a value (eg, ebx pushed above) is
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002744 // right on top of or right underneath a zero-sized reference.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002745 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002746 frame_->Drop(1); // Drop the result of the set operation.
mads.s.ager31e71382008-08-13 09:32:07 +00002747 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002748 }
2749 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002750 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002751 CheckStack(); // TODO(1222600): ignore if body contains calls.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002752 { VirtualFrame::RegisterAllocationScope scope(this);
2753 Visit(node->body());
2754 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002755
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002756 // Next. Reestablish a spilled frame in case we are coming here via
2757 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002758 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002759 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002760 frame_->EmitPop(r0);
2761 __ add(r0, r0, Operand(Smi::FromInt(1)));
2762 frame_->EmitPush(r0);
2763 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002764
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002765 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2766 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002767 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002768 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002769
2770 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002771 exit.Bind();
2772 node->continue_target()->Unuse();
2773 node->break_target()->Unuse();
2774 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002775}
2776
2777
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002778void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002779#ifdef DEBUG
2780 int original_height = frame_->height();
2781#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002782 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002783 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002784 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002785
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002786 JumpTarget try_block;
2787 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002788
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002789 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002791 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002792
2793 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002794 Variable* catch_var = node->catch_var()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002795 ASSERT(catch_var != NULL && catch_var->AsSlot() != NULL);
2796 StoreToSlot(catch_var->AsSlot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002797
2798 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002799 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002800
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002801 { VirtualFrame::RegisterAllocationScope scope(this);
2802 VisitStatements(node->catch_block()->statements());
2803 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002804 if (frame_ != NULL) {
2805 exit.Jump();
2806 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002807
2808
2809 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002810 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002812 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2813 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002814
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002815 // Shadow the labels for all escapes from the try block, including
2816 // returns. During shadowing, the original label is hidden as the
2817 // LabelShadow and operations on the original actually affect the
2818 // shadowing label.
2819 //
2820 // We should probably try to unify the escaping labels and the return
2821 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002822 int nof_escapes = node->escaping_targets()->length();
2823 List<ShadowTarget*> shadows(1 + nof_escapes);
2824
2825 // Add the shadow target for the function return.
2826 static const int kReturnShadowIndex = 0;
2827 shadows.Add(new ShadowTarget(&function_return_));
2828 bool function_return_was_shadowed = function_return_is_shadowed_;
2829 function_return_is_shadowed_ = true;
2830 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2831
2832 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002833 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002834 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002835 }
2836
2837 // Generate code for the statements in the try block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002838 { VirtualFrame::RegisterAllocationScope scope(this);
2839 VisitStatements(node->try_block()->statements());
2840 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002841
2842 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002843 // After shadowing stops, the original labels are unshadowed and the
2844 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002845 bool has_unlinks = false;
2846 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002847 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002848 has_unlinks = has_unlinks || shadows[i]->is_linked();
2849 }
2850 function_return_is_shadowed_ = function_return_was_shadowed;
2851
2852 // Get an external reference to the handler address.
2853 ExternalReference handler_address(Top::k_handler_address);
2854
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002855 // If we can fall off the end of the try block, unlink from try chain.
2856 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002857 // The next handler address is on top of the frame. Unlink from
2858 // the handler list and drop the rest of this handler from the
2859 // frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002860 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002861 frame_->EmitPop(r1); // r0 can contain the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002862 __ mov(r3, Operand(handler_address));
2863 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002864 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002865 if (has_unlinks) {
2866 exit.Jump();
2867 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868 }
2869
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002870 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002871 // jumped to. Deallocate each shadow target.
2872 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002873 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002874 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002875 shadows[i]->Bind();
2876 // Because we can be jumping here (to spilled code) from unspilled
2877 // code, we need to reestablish a spilled frame at this block.
2878 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002879
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002880 // Reload sp from the top handler, because some statements that we
2881 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002882 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002883 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002884 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002885
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002886 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002887 frame_->EmitPop(r1); // r0 can contain the return value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002888 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002889 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002890
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002891 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2892 frame_->PrepareForReturn();
2893 }
2894 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002895 }
2896 }
2897
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002898 exit.Bind();
2899 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002900}
2901
2902
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002903void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002904#ifdef DEBUG
2905 int original_height = frame_->height();
2906#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002907 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002908 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002909 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910
2911 // State: Used to keep track of reason for entering the finally
2912 // block. Should probably be extended to hold information for
2913 // break/continue from within the try block.
2914 enum { FALLING, THROWING, JUMPING };
2915
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002916 JumpTarget try_block;
2917 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002919 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002920
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002921 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002922 // In case of thrown exceptions, this is where we continue.
2923 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002924 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002925
2926 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002927 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002928
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002929 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2930 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002931
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002932 // Shadow the labels for all escapes from the try block, including
2933 // returns. Shadowing hides the original label as the LabelShadow and
2934 // operations on the original actually affect the shadowing label.
2935 //
2936 // We should probably try to unify the escaping labels and the return
2937 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002938 int nof_escapes = node->escaping_targets()->length();
2939 List<ShadowTarget*> shadows(1 + nof_escapes);
2940
2941 // Add the shadow target for the function return.
2942 static const int kReturnShadowIndex = 0;
2943 shadows.Add(new ShadowTarget(&function_return_));
2944 bool function_return_was_shadowed = function_return_is_shadowed_;
2945 function_return_is_shadowed_ = true;
2946 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2947
2948 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002949 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002950 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951 }
2952
2953 // Generate code for the statements in the try block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002954 { VirtualFrame::RegisterAllocationScope scope(this);
2955 VisitStatements(node->try_block()->statements());
2956 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002958 // Stop the introduced shadowing and count the number of required unlinks.
2959 // After shadowing stops, the original labels are unshadowed and the
2960 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002962 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002963 shadows[i]->StopShadowing();
2964 if (shadows[i]->is_linked()) nof_unlinks++;
2965 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002966 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002967
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002968 // Get an external reference to the handler address.
2969 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002970
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002971 // If we can fall off the end of the try block, unlink from the try
2972 // chain and set the state on the frame to FALLING.
2973 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002974 // The next handler address is on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002975 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002976 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002977 __ mov(r3, Operand(handler_address));
2978 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002979 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002980
2981 // Fake a top of stack value (unneeded when FALLING) and set the
2982 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002983 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002984 frame_->EmitPush(r0);
2985 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2986 if (nof_unlinks > 0) {
2987 finally_block.Jump();
2988 }
2989 }
2990
2991 // Generate code to unlink and set the state for the (formerly)
2992 // shadowing targets that have been jumped to.
2993 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002994 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002995 // If we have come from the shadowed return, the return value is
2996 // in (a non-refcounted reference to) r0. We must preserve it
2997 // until it is pushed.
2998 //
2999 // Because we can be jumping here (to spilled code) from
3000 // unspilled code, we need to reestablish a spilled frame at
3001 // this block.
3002 shadows[i]->Bind();
3003 frame_->SpillAll();
3004
3005 // Reload sp from the top handler, because some statements that
3006 // we break from (eg, for...in) may have left stuff on the
3007 // stack.
3008 __ mov(r3, Operand(handler_address));
3009 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003010 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003011
3012 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003013 // handler address is currently on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00003014 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003015 frame_->EmitPop(r1);
3016 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003017 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003018
3019 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003020 // If this label shadowed the function return, materialize the
3021 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003022 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003023 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003024 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003025 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003026 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003027 }
3028 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003029 if (--nof_unlinks > 0) {
3030 // If this is not the last unlink block, jump around the next.
3031 finally_block.Jump();
3032 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003033 }
3034 }
3035
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003036 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003037 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003038
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003039 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003040 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003041
3042 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003043 // and the state - while evaluating the finally block.
3044 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003045 // Generate code for the statements in the finally block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003046 { VirtualFrame::RegisterAllocationScope scope(this);
3047 VisitStatements(node->finally_block()->statements());
3048 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003049
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003050 if (has_valid_frame()) {
3051 // Restore state and return value or faked TOS.
3052 frame_->EmitPop(r2);
3053 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003054 }
3055
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003056 // Generate code to jump to the right destination for all used
3057 // formerly shadowing targets. Deallocate each shadow target.
3058 for (int i = 0; i < shadows.length(); i++) {
3059 if (has_valid_frame() && shadows[i]->is_bound()) {
3060 JumpTarget* original = shadows[i]->other_target();
3061 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
3062 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003063 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003064 skip.Branch(ne);
3065 frame_->PrepareForReturn();
3066 original->Jump();
3067 skip.Bind();
3068 } else {
3069 original->Branch(eq);
3070 }
3071 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003072 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003073
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003074 if (has_valid_frame()) {
3075 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003076 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003077 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
3078 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003079
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003080 // Rethrow exception.
3081 frame_->EmitPush(r0);
3082 frame_->CallRuntime(Runtime::kReThrow, 1);
3083
3084 // Done.
3085 exit.Bind();
3086 }
3087 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003088}
3089
3090
ager@chromium.org7c537e22008-10-16 08:43:32 +00003091void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003092#ifdef DEBUG
3093 int original_height = frame_->height();
3094#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003095 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003096 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003097#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00003098 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003099#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003100 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003101 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003102}
3103
3104
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003105void CodeGenerator::InstantiateFunction(
3106 Handle<SharedFunctionInfo> function_info) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003107 // Use the fast case closure allocation code that allocates in new
3108 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003109 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003110 FastNewClosureStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003111 frame_->EmitPush(Operand(function_info));
3112 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003113 frame_->CallStub(&stub, 1);
3114 frame_->EmitPush(r0);
3115 } else {
3116 // Create a new closure.
3117 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003118 frame_->EmitPush(Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003119 frame_->CallRuntime(Runtime::kNewClosure, 2);
3120 frame_->EmitPush(r0);
3121 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003122}
3123
3124
ager@chromium.org7c537e22008-10-16 08:43:32 +00003125void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003126#ifdef DEBUG
3127 int original_height = frame_->height();
3128#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003129 Comment cmnt(masm_, "[ FunctionLiteral");
3130
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003131 // Build the function info and instantiate it.
3132 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00003133 Compiler::BuildFunctionInfo(node, script());
3134 if (function_info.is_null()) {
3135 SetStackOverflow();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003136 ASSERT(frame_->height() == original_height);
3137 return;
3138 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003139 InstantiateFunction(function_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003140 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003141}
3142
3143
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003144void CodeGenerator::VisitSharedFunctionInfoLiteral(
3145 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003146#ifdef DEBUG
3147 int original_height = frame_->height();
3148#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003149 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
3150 InstantiateFunction(node->shared_function_info());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003151 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003152}
3153
3154
ager@chromium.org7c537e22008-10-16 08:43:32 +00003155void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003156#ifdef DEBUG
3157 int original_height = frame_->height();
3158#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003159 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003160 JumpTarget then;
3161 JumpTarget else_;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003162 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003163 if (has_valid_frame()) {
3164 Branch(false, &else_);
3165 }
3166 if (has_valid_frame() || then.is_linked()) {
3167 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003168 Load(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003169 }
3170 if (else_.is_linked()) {
3171 JumpTarget exit;
3172 if (has_valid_frame()) exit.Jump();
3173 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003174 Load(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003175 if (exit.is_linked()) exit.Bind();
3176 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003177 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003178}
3179
3180
ager@chromium.org7c537e22008-10-16 08:43:32 +00003181void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
3182 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003183 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003184
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003185 // JumpTargets do not yet support merging frames so the frame must be
3186 // spilled when jumping to these targets.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003187 JumpTarget slow;
3188 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003189
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003190 // Generate fast case for loading from slots that correspond to
3191 // local/global variables or arguments unless they are shadowed by
3192 // eval-introduced bindings.
3193 EmitDynamicLoadFromSlotFastCase(slot,
3194 typeof_state,
3195 &slow,
3196 &done);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003197
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003198 slow.Bind();
3199 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003200 frame_->EmitPush(Operand(slot->var()->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003201
ager@chromium.org7c537e22008-10-16 08:43:32 +00003202 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003203 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003204 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003205 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003206 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00003207
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003208 done.Bind();
3209 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003210
3211 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00003212 Register scratch = VirtualFrame::scratch0();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003213 TypeInfo info = type_info(slot);
3214 frame_->EmitPush(SlotOperand(slot, scratch), info);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003215
ager@chromium.org7c537e22008-10-16 08:43:32 +00003216 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003217 // Const slots may contain 'the hole' value (the constant hasn't been
3218 // initialized yet) which needs to be converted into the 'undefined'
3219 // value.
3220 Comment cmnt(masm_, "[ Unhole const");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003221 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003222 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003223 __ cmp(tos, ip);
3224 __ LoadRoot(tos, Heap::kUndefinedValueRootIndex, eq);
3225 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003226 }
3227 }
3228}
3229
3230
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003231void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot,
3232 TypeofState state) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003233 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003234 LoadFromSlot(slot, state);
3235
3236 // Bail out quickly if we're not using lazy arguments allocation.
3237 if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return;
3238
3239 // ... or if the slot isn't a non-parameter arguments slot.
3240 if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return;
3241
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003242 // Load the loaded value from the stack into a register but leave it on the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003243 // stack.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003244 Register tos = frame_->Peek();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003245
3246 // If the loaded value is the sentinel that indicates that we
3247 // haven't loaded the arguments object yet, we need to do it now.
3248 JumpTarget exit;
3249 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003250 __ cmp(tos, ip);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003251 exit.Branch(ne);
3252 frame_->Drop();
3253 StoreArgumentsObject(false);
3254 exit.Bind();
3255}
3256
3257
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003258void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
3259 ASSERT(slot != NULL);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003260 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003261 if (slot->type() == Slot::LOOKUP) {
3262 ASSERT(slot->var()->is_dynamic());
3263
3264 // For now, just do a runtime call.
3265 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003266 frame_->EmitPush(Operand(slot->var()->name()));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003267
3268 if (init_state == CONST_INIT) {
3269 // Same as the case for a normal store, but ignores attribute
3270 // (e.g. READ_ONLY) of context slot so that we can initialize
3271 // const properties (introduced via eval("const foo = (some
3272 // expr);")). Also, uses the current function context instead of
3273 // the top context.
3274 //
3275 // Note that we must declare the foo upon entry of eval(), via a
3276 // context slot declaration, but we cannot initialize it at the
3277 // same time, because the const declaration may be at the end of
3278 // the eval code (sigh...) and the const variable may have been
3279 // used before (where its value is 'undefined'). Thus, we can only
3280 // do the initialization when we actually encounter the expression
3281 // and when the expression operands are defined and valid, and
3282 // thus we need the split into 2 operations: declaration of the
3283 // context slot followed by initialization.
3284 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3285 } else {
3286 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
3287 }
3288 // Storing a variable must keep the (new) value on the expression
3289 // stack. This is necessary for compiling assignment expressions.
3290 frame_->EmitPush(r0);
3291
3292 } else {
3293 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00003294 Register scratch = VirtualFrame::scratch0();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003295 Register scratch2 = VirtualFrame::scratch1();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003296
ager@chromium.org357bf652010-04-12 11:30:10 +00003297 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003298 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00003299
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003300 if (init_state == CONST_INIT) {
3301 ASSERT(slot->var()->mode() == Variable::CONST);
3302 // Only the first const initialization must be executed (the slot
3303 // still contains 'the hole' value). When the assignment is
3304 // executed, the code is identical to a normal store (see below).
3305 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00003306 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003307 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00003308 __ cmp(scratch, ip);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003309 exit.Branch(ne);
3310 }
3311
3312 // We must execute the store. Storing a variable must keep the
3313 // (new) value on the stack. This is necessary for compiling
3314 // assignment expressions.
3315 //
3316 // Note: We will reach here even with slot->var()->mode() ==
3317 // Variable::CONST because of const declarations which will
3318 // initialize consts to 'the hole' value and by doing so, end up
3319 // calling this code. r2 may be loaded with context; used below in
3320 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00003321 Register tos = frame_->Peek();
3322 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003323 if (slot->type() == Slot::CONTEXT) {
3324 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00003325 __ tst(tos, Operand(kSmiTagMask));
3326 // We don't use tos any more after here.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003327 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00003328 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003329 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003330 // We need an extra register. Until we have a way to do that in the
3331 // virtual frame we will cheat and ask for a free TOS register.
3332 Register scratch3 = frame_->GetTOSRegister();
3333 __ RecordWrite(scratch, Operand(offset), scratch2, scratch3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003334 }
3335 // If we definitely did not jump over the assignment, we do not need
3336 // to bind the exit label. Doing so can defeat peephole
3337 // optimization.
3338 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3339 exit.Bind();
3340 }
3341 }
3342}
3343
3344
ager@chromium.org381abbb2009-02-25 13:23:22 +00003345void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
3346 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003347 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003348 // Check that no extension objects have been created by calls to
3349 // eval from the current scope to the global scope.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003350 Register tmp = frame_->scratch0();
3351 Register tmp2 = frame_->scratch1();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003352 Register context = cp;
3353 Scope* s = scope();
3354 while (s != NULL) {
3355 if (s->num_heap_slots() > 0) {
3356 if (s->calls_eval()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003357 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003358 // Check that extension is NULL.
3359 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
3360 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003361 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003362 }
3363 // Load next context in chain.
3364 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
3365 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3366 context = tmp;
3367 }
3368 // If no outer scope calls eval, we do not need to check more
3369 // context extensions.
3370 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
3371 s = s->outer_scope();
3372 }
3373
3374 if (s->is_eval_scope()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003375 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003376 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00003377 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003378 __ bind(&next);
3379 // Terminate at global context.
3380 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003381 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
3382 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003383 __ b(eq, &fast);
3384 // Check that extension is NULL.
3385 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
3386 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003387 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003388 // Load next context in chain.
3389 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
3390 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3391 __ b(&next);
3392 __ bind(&fast);
3393 }
3394
ager@chromium.org381abbb2009-02-25 13:23:22 +00003395 // Load the global object.
3396 LoadGlobal();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003397 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00003398 frame_->CallLoadIC(slot->var()->name(),
3399 typeof_state == INSIDE_TYPEOF
3400 ? RelocInfo::CODE_TARGET
3401 : RelocInfo::CODE_TARGET_CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003402}
3403
3404
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003405void CodeGenerator::EmitDynamicLoadFromSlotFastCase(Slot* slot,
3406 TypeofState typeof_state,
3407 JumpTarget* slow,
3408 JumpTarget* done) {
3409 // Generate fast-case code for variables that might be shadowed by
3410 // eval-introduced variables. Eval is used a lot without
3411 // introducing variables. In those cases, we do not want to
3412 // perform a runtime call for all variables in the scope
3413 // containing the eval.
3414 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
3415 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, slow);
3416 frame_->SpillAll();
3417 done->Jump();
3418
3419 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
3420 frame_->SpillAll();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003421 Slot* potential_slot = slot->var()->local_if_not_shadowed()->AsSlot();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003422 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
3423 if (potential_slot != NULL) {
3424 // Generate fast case for locals that rewrite to slots.
3425 __ ldr(r0,
3426 ContextSlotOperandCheckExtensions(potential_slot,
3427 r1,
3428 r2,
3429 slow));
3430 if (potential_slot->var()->mode() == Variable::CONST) {
3431 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3432 __ cmp(r0, ip);
3433 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
3434 }
3435 done->Jump();
3436 } else if (rewrite != NULL) {
3437 // Generate fast case for argument loads.
3438 Property* property = rewrite->AsProperty();
3439 if (property != NULL) {
3440 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
3441 Literal* key_literal = property->key()->AsLiteral();
3442 if (obj_proxy != NULL &&
3443 key_literal != NULL &&
3444 obj_proxy->IsArguments() &&
3445 key_literal->handle()->IsSmi()) {
3446 // Load arguments object if there are no eval-introduced
3447 // variables. Then load the argument from the arguments
3448 // object using keyed load.
3449 __ ldr(r0,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003450 ContextSlotOperandCheckExtensions(obj_proxy->var()->AsSlot(),
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003451 r1,
3452 r2,
3453 slow));
3454 frame_->EmitPush(r0);
3455 __ mov(r1, Operand(key_literal->handle()));
3456 frame_->EmitPush(r1);
3457 EmitKeyedLoad();
3458 done->Jump();
3459 }
3460 }
3461 }
3462 }
3463}
3464
3465
ager@chromium.org7c537e22008-10-16 08:43:32 +00003466void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003467#ifdef DEBUG
3468 int original_height = frame_->height();
3469#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003470 Comment cmnt(masm_, "[ Slot");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003471 LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003472 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003473}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003474
ager@chromium.org7c537e22008-10-16 08:43:32 +00003475
3476void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003477#ifdef DEBUG
3478 int original_height = frame_->height();
3479#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003480 Comment cmnt(masm_, "[ VariableProxy");
3481
3482 Variable* var = node->var();
3483 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003484 if (expr != NULL) {
3485 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003486 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00003487 ASSERT(var->is_global());
3488 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003489 ref.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003490 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003491 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003492}
3493
3494
ager@chromium.org7c537e22008-10-16 08:43:32 +00003495void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003496#ifdef DEBUG
3497 int original_height = frame_->height();
3498#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003499 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00003500 Register reg = frame_->GetTOSRegister();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003501 bool is_smi = node->handle()->IsSmi();
ager@chromium.org357bf652010-04-12 11:30:10 +00003502 __ mov(reg, Operand(node->handle()));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003503 frame_->EmitPush(reg, is_smi ? TypeInfo::Smi() : TypeInfo::Unknown());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003504 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003505}
3506
3507
ager@chromium.org7c537e22008-10-16 08:43:32 +00003508void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003509#ifdef DEBUG
3510 int original_height = frame_->height();
3511#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003512 Comment cmnt(masm_, "[ RexExp Literal");
3513
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003514 Register tmp = VirtualFrame::scratch0();
3515 // Free up a TOS register that can be used to push the literal.
3516 Register literal = frame_->GetTOSRegister();
3517
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003518 // Retrieve the literal array and check the allocated entry.
3519
3520 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003521 __ ldr(tmp, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003522
3523 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003524 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kLiteralsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003525
3526 // Load the literal at the ast saved index.
3527 int literal_offset =
3528 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003529 __ ldr(literal, FieldMemOperand(tmp, literal_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003530
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003531 JumpTarget materialized;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003532 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003533 __ cmp(literal, ip);
3534 // This branch locks the virtual frame at the done label to match the
3535 // one we have here, where the literal register is not on the stack and
3536 // nothing is spilled.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003537 materialized.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003538
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003539 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003540 // the literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003541 // literal array (0)
3542 frame_->EmitPush(tmp);
3543 // literal index (1)
3544 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3545 // RegExp pattern (2)
3546 frame_->EmitPush(Operand(node->pattern()));
3547 // RegExp flags (3)
3548 frame_->EmitPush(Operand(node->flags()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003549 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003550 __ Move(literal, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003551
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003552 materialized.Bind();
3553
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003554 frame_->EmitPush(literal);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003555 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
3556 frame_->EmitPush(Operand(Smi::FromInt(size)));
3557 frame_->CallRuntime(Runtime::kAllocateInNewSpace, 1);
3558 // TODO(lrn): Use AllocateInNewSpace macro with fallback to runtime.
3559 // r0 is newly allocated space.
3560
3561 // Reuse literal variable with (possibly) a new register, still holding
3562 // the materialized boilerplate.
3563 literal = frame_->PopToRegister(r0);
3564
3565 __ CopyFields(r0, literal, tmp.bit(), size / kPointerSize);
3566
3567 // Push the clone.
3568 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003569 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003570}
3571
3572
ager@chromium.org7c537e22008-10-16 08:43:32 +00003573void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003574#ifdef DEBUG
3575 int original_height = frame_->height();
3576#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003577 Comment cmnt(masm_, "[ ObjectLiteral");
3578
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003579 Register literal = frame_->GetTOSRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003580 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003581 __ ldr(literal, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003582 // Literal array.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003583 __ ldr(literal, FieldMemOperand(literal, JSFunction::kLiteralsOffset));
3584 frame_->EmitPush(literal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003585 // Literal index.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003586 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003587 // Constant properties.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003588 frame_->EmitPush(Operand(node->constant_properties()));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003589 // Should the object literal have fast elements?
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003590 frame_->EmitPush(Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003591 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003592 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003593 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003594 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003595 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003596 frame_->EmitPush(r0); // save the result
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003597
3598 // Mark all computed expressions that are bound to a key that
3599 // is shadowed by a later occurrence of the same key. For the
3600 // marked expressions, no store code is emitted.
3601 node->CalculateEmitStore();
3602
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003604 // At the start of each iteration, the top of stack contains
3605 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003606 ObjectLiteral::Property* property = node->properties()->at(i);
3607 Literal* key = property->key();
3608 Expression* value = property->value();
3609 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003610 case ObjectLiteral::Property::CONSTANT:
3611 break;
3612 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3613 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3614 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003615 case ObjectLiteral::Property::COMPUTED:
3616 if (key->handle()->IsSymbol()) {
3617 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003618 Load(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003619 if (property->emit_store()) {
3620 frame_->PopToR0();
3621 // Fetch the object literal.
3622 frame_->SpillAllButCopyTOSToR1();
3623 __ mov(r2, Operand(key->handle()));
3624 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3625 } else {
3626 frame_->Drop();
3627 }
ager@chromium.org5c838252010-02-19 08:53:10 +00003628 break;
3629 }
3630 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003631 case ObjectLiteral::Property::PROTOTYPE: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003632 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003633 Load(key);
3634 Load(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003635 if (property->emit_store()) {
3636 frame_->CallRuntime(Runtime::kSetProperty, 3);
3637 } else {
3638 frame_->Drop(3);
3639 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003640 break;
3641 }
3642 case ObjectLiteral::Property::SETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003643 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003644 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003645 frame_->EmitPush(Operand(Smi::FromInt(1)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003646 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003647 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003648 break;
3649 }
3650 case ObjectLiteral::Property::GETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003651 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003652 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003653 frame_->EmitPush(Operand(Smi::FromInt(0)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003654 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003655 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003656 break;
3657 }
3658 }
3659 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003660 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003661}
3662
3663
ager@chromium.org7c537e22008-10-16 08:43:32 +00003664void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003665#ifdef DEBUG
3666 int original_height = frame_->height();
3667#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003668 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003669
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003670 Register tos = frame_->GetTOSRegister();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003671 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003672 __ ldr(tos, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003673 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003674 __ ldr(tos, FieldMemOperand(tos, JSFunction::kLiteralsOffset));
3675 frame_->EmitPush(tos);
3676 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3677 frame_->EmitPush(Operand(node->constant_elements()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003678 int length = node->values()->length();
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003679 if (node->constant_elements()->map() == Heap::fixed_cow_array_map()) {
3680 FastCloneShallowArrayStub stub(
3681 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
3682 frame_->CallStub(&stub, 3);
3683 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
3684 } else if (node->depth() > 1) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003685 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003686 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003687 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003688 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003689 FastCloneShallowArrayStub stub(
3690 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
ager@chromium.org5c838252010-02-19 08:53:10 +00003691 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003692 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003693 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003694 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003695
3696 // Generate code to set the elements in the array that are not
3697 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003698 for (int i = 0; i < node->values()->length(); i++) {
3699 Expression* value = node->values()->at(i);
3700
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003701 // If value is a literal the property value is already set in the
3702 // boilerplate object.
3703 if (value->AsLiteral() != NULL) continue;
3704 // If value is a materialized literal the property value is already set
3705 // in the boilerplate object if it is simple.
3706 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003707
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003708 // The property must be set by generated code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003709 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003710 frame_->PopToR0();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003711 // Fetch the object literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003712 frame_->SpillAllButCopyTOSToR1();
3713
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003714 // Get the elements array.
3715 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003716
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003717 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003718 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003719 __ str(r0, FieldMemOperand(r1, offset));
3720
3721 // Update the write barrier for the array address.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003722 __ RecordWrite(r1, Operand(offset), r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003723 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003724 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003725}
3726
3727
ager@chromium.org32912102009-01-16 10:38:43 +00003728void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003729#ifdef DEBUG
3730 int original_height = frame_->height();
3731#endif
ager@chromium.org32912102009-01-16 10:38:43 +00003732 // Call runtime routine to allocate the catch extension object and
3733 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003734 Comment cmnt(masm_, "[ CatchExtensionObject");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003735 Load(node->key());
3736 Load(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003737 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3738 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003739 ASSERT_EQ(original_height + 1, frame_->height());
3740}
3741
3742
3743void CodeGenerator::EmitSlotAssignment(Assignment* node) {
3744#ifdef DEBUG
3745 int original_height = frame_->height();
3746#endif
3747 Comment cmnt(masm(), "[ Variable Assignment");
3748 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3749 ASSERT(var != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003750 Slot* slot = var->AsSlot();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003751 ASSERT(slot != NULL);
3752
3753 // Evaluate the right-hand side.
3754 if (node->is_compound()) {
3755 // For a compound assignment the right-hand side is a binary operation
3756 // between the current property value and the actual right-hand side.
3757 LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
3758
3759 // Perform the binary operation.
3760 Literal* literal = node->value()->AsLiteral();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003761 bool overwrite_value = node->value()->ResultOverwriteAllowed();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003762 if (literal != NULL && literal->handle()->IsSmi()) {
3763 SmiOperation(node->binary_op(),
3764 literal->handle(),
3765 false,
3766 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3767 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003768 GenerateInlineSmi inline_smi =
3769 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3770 if (literal != NULL) {
3771 ASSERT(!literal->handle()->IsSmi());
3772 inline_smi = DONT_GENERATE_INLINE_SMI;
3773 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003774 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003775 GenericBinaryOperation(node->binary_op(),
3776 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3777 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003778 }
3779 } else {
3780 Load(node->value());
3781 }
3782
3783 // Perform the assignment.
3784 if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) {
3785 CodeForSourcePosition(node->position());
3786 StoreToSlot(slot,
3787 node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT);
3788 }
3789 ASSERT_EQ(original_height + 1, frame_->height());
3790}
3791
3792
3793void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) {
3794#ifdef DEBUG
3795 int original_height = frame_->height();
3796#endif
3797 Comment cmnt(masm(), "[ Named Property Assignment");
3798 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3799 Property* prop = node->target()->AsProperty();
3800 ASSERT(var == NULL || (prop == NULL && var->is_global()));
3801
3802 // Initialize name and evaluate the receiver sub-expression if necessary. If
3803 // the receiver is trivial it is not placed on the stack at this point, but
3804 // loaded whenever actually needed.
3805 Handle<String> name;
3806 bool is_trivial_receiver = false;
3807 if (var != NULL) {
3808 name = var->name();
3809 } else {
3810 Literal* lit = prop->key()->AsLiteral();
3811 ASSERT_NOT_NULL(lit);
3812 name = Handle<String>::cast(lit->handle());
3813 // Do not materialize the receiver on the frame if it is trivial.
3814 is_trivial_receiver = prop->obj()->IsTrivial();
3815 if (!is_trivial_receiver) Load(prop->obj());
3816 }
3817
3818 // Change to slow case in the beginning of an initialization block to
3819 // avoid the quadratic behavior of repeatedly adding fast properties.
3820 if (node->starts_initialization_block()) {
3821 // Initialization block consists of assignments of the form expr.x = ..., so
3822 // this will never be an assignment to a variable, so there must be a
3823 // receiver object.
3824 ASSERT_EQ(NULL, var);
3825 if (is_trivial_receiver) {
3826 Load(prop->obj());
3827 } else {
3828 frame_->Dup();
3829 }
3830 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3831 }
3832
3833 // Change to fast case at the end of an initialization block. To prepare for
3834 // that add an extra copy of the receiver to the frame, so that it can be
3835 // converted back to fast case after the assignment.
3836 if (node->ends_initialization_block() && !is_trivial_receiver) {
3837 frame_->Dup();
3838 }
3839
3840 // Stack layout:
3841 // [tos] : receiver (only materialized if non-trivial)
3842 // [tos+1] : receiver if at the end of an initialization block
3843
3844 // Evaluate the right-hand side.
3845 if (node->is_compound()) {
3846 // For a compound assignment the right-hand side is a binary operation
3847 // between the current property value and the actual right-hand side.
3848 if (is_trivial_receiver) {
3849 Load(prop->obj());
3850 } else if (var != NULL) {
3851 LoadGlobal();
3852 } else {
3853 frame_->Dup();
3854 }
3855 EmitNamedLoad(name, var != NULL);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003856
3857 // Perform the binary operation.
3858 Literal* literal = node->value()->AsLiteral();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003859 bool overwrite_value = node->value()->ResultOverwriteAllowed();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003860 if (literal != NULL && literal->handle()->IsSmi()) {
3861 SmiOperation(node->binary_op(),
3862 literal->handle(),
3863 false,
3864 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3865 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003866 GenerateInlineSmi inline_smi =
3867 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3868 if (literal != NULL) {
3869 ASSERT(!literal->handle()->IsSmi());
3870 inline_smi = DONT_GENERATE_INLINE_SMI;
3871 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003872 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003873 GenericBinaryOperation(node->binary_op(),
3874 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3875 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003876 }
3877 } else {
3878 // For non-compound assignment just load the right-hand side.
3879 Load(node->value());
3880 }
3881
3882 // Stack layout:
3883 // [tos] : value
3884 // [tos+1] : receiver (only materialized if non-trivial)
3885 // [tos+2] : receiver if at the end of an initialization block
3886
3887 // Perform the assignment. It is safe to ignore constants here.
3888 ASSERT(var == NULL || var->mode() != Variable::CONST);
3889 ASSERT_NE(Token::INIT_CONST, node->op());
3890 if (is_trivial_receiver) {
3891 // Load the receiver and swap with the value.
3892 Load(prop->obj());
3893 Register t0 = frame_->PopToRegister();
3894 Register t1 = frame_->PopToRegister(t0);
3895 frame_->EmitPush(t0);
3896 frame_->EmitPush(t1);
3897 }
3898 CodeForSourcePosition(node->position());
3899 bool is_contextual = (var != NULL);
3900 EmitNamedStore(name, is_contextual);
3901 frame_->EmitPush(r0);
3902
3903 // Change to fast case at the end of an initialization block.
3904 if (node->ends_initialization_block()) {
3905 ASSERT_EQ(NULL, var);
3906 // The argument to the runtime call is the receiver.
3907 if (is_trivial_receiver) {
3908 Load(prop->obj());
3909 } else {
3910 // A copy of the receiver is below the value of the assignment. Swap
3911 // the receiver and the value of the assignment expression.
3912 Register t0 = frame_->PopToRegister();
3913 Register t1 = frame_->PopToRegister(t0);
3914 frame_->EmitPush(t0);
3915 frame_->EmitPush(t1);
3916 }
3917 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3918 }
3919
3920 // Stack layout:
3921 // [tos] : result
3922
3923 ASSERT_EQ(original_height + 1, frame_->height());
3924}
3925
3926
3927void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) {
3928#ifdef DEBUG
3929 int original_height = frame_->height();
3930#endif
3931 Comment cmnt(masm_, "[ Keyed Property Assignment");
3932 Property* prop = node->target()->AsProperty();
3933 ASSERT_NOT_NULL(prop);
3934
3935 // Evaluate the receiver subexpression.
3936 Load(prop->obj());
3937
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003938 WriteBarrierCharacter wb_info;
3939
ager@chromium.orgac091b72010-05-05 07:34:42 +00003940 // Change to slow case in the beginning of an initialization block to
3941 // avoid the quadratic behavior of repeatedly adding fast properties.
3942 if (node->starts_initialization_block()) {
3943 frame_->Dup();
3944 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3945 }
3946
3947 // Change to fast case at the end of an initialization block. To prepare for
3948 // that add an extra copy of the receiver to the frame, so that it can be
3949 // converted back to fast case after the assignment.
3950 if (node->ends_initialization_block()) {
3951 frame_->Dup();
3952 }
3953
3954 // Evaluate the key subexpression.
3955 Load(prop->key());
3956
3957 // Stack layout:
3958 // [tos] : key
3959 // [tos+1] : receiver
3960 // [tos+2] : receiver if at the end of an initialization block
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003961 //
ager@chromium.orgac091b72010-05-05 07:34:42 +00003962 // Evaluate the right-hand side.
3963 if (node->is_compound()) {
3964 // For a compound assignment the right-hand side is a binary operation
3965 // between the current property value and the actual right-hand side.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003966 // Duplicate receiver and key for loading the current property value.
3967 frame_->Dup2();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003968 EmitKeyedLoad();
3969 frame_->EmitPush(r0);
3970
3971 // Perform the binary operation.
3972 Literal* literal = node->value()->AsLiteral();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003973 bool overwrite_value = node->value()->ResultOverwriteAllowed();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003974 if (literal != NULL && literal->handle()->IsSmi()) {
3975 SmiOperation(node->binary_op(),
3976 literal->handle(),
3977 false,
3978 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3979 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003980 GenerateInlineSmi inline_smi =
3981 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3982 if (literal != NULL) {
3983 ASSERT(!literal->handle()->IsSmi());
3984 inline_smi = DONT_GENERATE_INLINE_SMI;
3985 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003986 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003987 GenericBinaryOperation(node->binary_op(),
3988 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3989 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003990 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003991 wb_info = node->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
ager@chromium.orgac091b72010-05-05 07:34:42 +00003992 } else {
3993 // For non-compound assignment just load the right-hand side.
3994 Load(node->value());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003995 wb_info = node->value()->AsLiteral() != NULL ?
3996 NEVER_NEWSPACE :
3997 (node->value()->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003998 }
3999
4000 // Stack layout:
4001 // [tos] : value
4002 // [tos+1] : key
4003 // [tos+2] : receiver
4004 // [tos+3] : receiver if at the end of an initialization block
4005
4006 // Perform the assignment. It is safe to ignore constants here.
4007 ASSERT(node->op() != Token::INIT_CONST);
4008 CodeForSourcePosition(node->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004009 EmitKeyedStore(prop->key()->type(), wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00004010 frame_->EmitPush(r0);
4011
4012 // Stack layout:
4013 // [tos] : result
4014 // [tos+1] : receiver if at the end of an initialization block
4015
4016 // Change to fast case at the end of an initialization block.
4017 if (node->ends_initialization_block()) {
4018 // The argument to the runtime call is the extra copy of the receiver,
4019 // which is below the value of the assignment. Swap the receiver and
4020 // the value of the assignment expression.
4021 Register t0 = frame_->PopToRegister();
4022 Register t1 = frame_->PopToRegister(t0);
4023 frame_->EmitPush(t1);
4024 frame_->EmitPush(t0);
4025 frame_->CallRuntime(Runtime::kToFastProperties, 1);
4026 }
4027
4028 // Stack layout:
4029 // [tos] : result
4030
4031 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org32912102009-01-16 10:38:43 +00004032}
4033
4034
ager@chromium.org7c537e22008-10-16 08:43:32 +00004035void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004036 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004037#ifdef DEBUG
4038 int original_height = frame_->height();
4039#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004040 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00004041
ager@chromium.orgac091b72010-05-05 07:34:42 +00004042 Variable* var = node->target()->AsVariableProxy()->AsVariable();
4043 Property* prop = node->target()->AsProperty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004044
ager@chromium.orgac091b72010-05-05 07:34:42 +00004045 if (var != NULL && !var->is_global()) {
4046 EmitSlotAssignment(node);
mads.s.ager31e71382008-08-13 09:32:07 +00004047
ager@chromium.orgac091b72010-05-05 07:34:42 +00004048 } else if ((prop != NULL && prop->key()->IsPropertyName()) ||
4049 (var != NULL && var->is_global())) {
4050 // Properties whose keys are property names and global variables are
4051 // treated as named property references. We do not need to consider
4052 // global 'this' because it is not a valid left-hand side.
4053 EmitNamedPropertyAssignment(node);
4054
4055 } else if (prop != NULL) {
4056 // Other properties (including rewritten parameters for a function that
4057 // uses arguments) are keyed property assignments.
4058 EmitKeyedPropertyAssignment(node);
4059
4060 } else {
4061 // Invalid left-hand side.
4062 Load(node->target());
4063 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
4064 // The runtime call doesn't actually return but the code generator will
4065 // still generate code and expects a certain frame height.
4066 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004067 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004068 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004069}
4070
4071
ager@chromium.org7c537e22008-10-16 08:43:32 +00004072void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004073#ifdef DEBUG
4074 int original_height = frame_->height();
4075#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004076 Comment cmnt(masm_, "[ Throw");
4077
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004078 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004079 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004080 frame_->CallRuntime(Runtime::kThrow, 1);
4081 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00004082 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004083}
4084
4085
ager@chromium.org7c537e22008-10-16 08:43:32 +00004086void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004087#ifdef DEBUG
4088 int original_height = frame_->height();
4089#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004090 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004091
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004092 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00004093 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004094 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004095 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004096}
4097
4098
ager@chromium.org7c537e22008-10-16 08:43:32 +00004099void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004100#ifdef DEBUG
4101 int original_height = frame_->height();
4102#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004103 Comment cmnt(masm_, "[ Call");
4104
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004105 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004106 ZoneList<Expression*>* args = node->arguments();
4107
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004108 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004109 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004110 Variable* var = function->AsVariableProxy()->AsVariable();
4111 Property* property = function->AsProperty();
4112
4113 // ------------------------------------------------------------------------
4114 // Fast-case: Use inline caching.
4115 // ---
4116 // According to ECMA-262, section 11.2.3, page 44, the function to call
4117 // must be resolved after the arguments have been evaluated. The IC code
4118 // automatically handles this by loading the arguments before the function
4119 // is resolved in cache misses (this also holds for megamorphic calls).
4120 // ------------------------------------------------------------------------
4121
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004122 if (var != NULL && var->is_possibly_eval()) {
4123 // ----------------------------------
4124 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
4125 // ----------------------------------
4126
4127 // In a call to eval, we first call %ResolvePossiblyDirectEval to
4128 // resolve the function we need to call and the receiver of the
4129 // call. Then we call the resolved function using the given
4130 // arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004131
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004132 // Prepare stack for call to resolved function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004133 Load(function);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004134
4135 // Allocate a frame slot for the receiver.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00004136 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004137
4138 // Load the arguments.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004139 int arg_count = args->length();
4140 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004141 Load(args->at(i));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004142 }
4143
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00004144 VirtualFrame::SpilledScope spilled_scope(frame_);
4145
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004146 // If we know that eval can only be shadowed by eval-introduced
4147 // variables we attempt to load the global eval function directly
4148 // in generated code. If we succeed, there is no need to perform a
4149 // context lookup in the runtime system.
4150 JumpTarget done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004151 if (var->AsSlot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
4152 ASSERT(var->AsSlot()->type() == Slot::LOOKUP);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004153 JumpTarget slow;
4154 // Prepare the stack for the call to
4155 // ResolvePossiblyDirectEvalNoLookup by pushing the loaded
4156 // function, the first argument to the eval call and the
4157 // receiver.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004158 LoadFromGlobalSlotCheckExtensions(var->AsSlot(),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004159 NOT_INSIDE_TYPEOF,
4160 &slow);
4161 frame_->EmitPush(r0);
4162 if (arg_count > 0) {
4163 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
4164 frame_->EmitPush(r1);
4165 } else {
4166 frame_->EmitPush(r2);
4167 }
4168 __ ldr(r1, frame_->Receiver());
4169 frame_->EmitPush(r1);
4170
4171 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEvalNoLookup, 3);
4172
4173 done.Jump();
4174 slow.Bind();
4175 }
4176
4177 // Prepare the stack for the call to ResolvePossiblyDirectEval by
4178 // pushing the loaded function, the first argument to the eval
4179 // call and the receiver.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004180 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
4181 frame_->EmitPush(r1);
4182 if (arg_count > 0) {
4183 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
4184 frame_->EmitPush(r1);
4185 } else {
4186 frame_->EmitPush(r2);
4187 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004188 __ ldr(r1, frame_->Receiver());
4189 frame_->EmitPush(r1);
4190
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004191 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004192 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004193
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00004194 // If we generated fast-case code bind the jump-target where fast
4195 // and slow case merge.
4196 if (done.is_linked()) done.Bind();
4197
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004198 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004199 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004200 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
4201
4202 // Call the function.
4203 CodeForSourcePosition(node->position());
4204
4205 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004206 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004207 frame_->CallStub(&call_function, arg_count + 1);
4208
4209 __ ldr(cp, frame_->Context());
4210 // Remove the function from the stack.
4211 frame_->Drop();
4212 frame_->EmitPush(r0);
4213
4214 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004215 // ----------------------------------
4216 // JavaScript example: 'foo(1, 2, 3)' // foo is global
4217 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004218 // Pass the global object as the receiver and let the IC stub
4219 // patch the stack to use the global proxy as 'this' in the
4220 // invoked function.
4221 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004222
4223 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004224 int arg_count = args->length();
4225 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004226 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004227 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004228
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004229 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +00004230 // Setup the name register and call the IC initialization code.
4231 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004232 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004233 Handle<Code> stub = StubCache::ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004234 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004235 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
4236 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004237 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004238 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004239
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004240 } else if (var != NULL && var->AsSlot() != NULL &&
4241 var->AsSlot()->type() == Slot::LOOKUP) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004242 // ----------------------------------
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004243 // JavaScript examples:
4244 //
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004245 // with (obj) foo(1, 2, 3) // foo may be in obj.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004246 //
4247 // function f() {};
4248 // function g() {
4249 // eval(...);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004250 // f(); // f could be in extension object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004251 // }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004252 // ----------------------------------
4253
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004254 JumpTarget slow, done;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004255
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004256 // Generate fast case for loading functions from slots that
4257 // correspond to local/global variables or arguments unless they
4258 // are shadowed by eval-introduced bindings.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004259 EmitDynamicLoadFromSlotFastCase(var->AsSlot(),
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004260 NOT_INSIDE_TYPEOF,
4261 &slow,
4262 &done);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004263
4264 slow.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004265 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004266 frame_->EmitPush(cp);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004267 frame_->EmitPush(Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004268 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004269 // r0: slot value; r1: receiver
4270
4271 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004272 frame_->EmitPush(r0); // function
4273 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004274
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004275 // If fast case code has been generated, emit code to push the
4276 // function and receiver and have the slow path jump around this
4277 // code.
4278 if (done.is_linked()) {
4279 JumpTarget call;
4280 call.Jump();
4281 done.Bind();
4282 frame_->EmitPush(r0); // function
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004283 LoadGlobalReceiver(VirtualFrame::scratch0()); // receiver
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004284 call.Bind();
4285 }
4286
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004287 // Call the function. At this point, everything is spilled but the
4288 // function and receiver are in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004289 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004290 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004291
4292 } else if (property != NULL) {
4293 // Check if the key is a literal string.
4294 Literal* literal = property->key()->AsLiteral();
4295
4296 if (literal != NULL && literal->handle()->IsSymbol()) {
4297 // ------------------------------------------------------------------
4298 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
4299 // ------------------------------------------------------------------
4300
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004301 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004302
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004303 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
4304 name->IsEqualTo(CStrVector("apply")) &&
4305 args->length() == 2 &&
4306 args->at(1)->AsVariableProxy() != NULL &&
4307 args->at(1)->AsVariableProxy()->IsArguments()) {
4308 // Use the optimized Function.prototype.apply that avoids
4309 // allocating lazily allocated arguments objects.
4310 CallApplyLazy(property->obj(),
4311 args->at(0),
4312 args->at(1)->AsVariableProxy(),
4313 node->position());
4314
4315 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004316 Load(property->obj()); // Receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004317 // Load the arguments.
4318 int arg_count = args->length();
4319 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004320 Load(args->at(i));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004321 }
4322
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004323 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004324 // Set the name register and call the IC initialization code.
4325 __ mov(r2, Operand(name));
4326 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004327 Handle<Code> stub =
4328 StubCache::ComputeCallInitialize(arg_count, in_loop);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004329 CodeForSourcePosition(node->position());
4330 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4331 __ ldr(cp, frame_->Context());
4332 frame_->EmitPush(r0);
4333 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004334
4335 } else {
4336 // -------------------------------------------
4337 // JavaScript example: 'array[index](1, 2, 3)'
4338 // -------------------------------------------
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004339
4340 // Load the receiver and name of the function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004341 Load(property->obj());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004342 Load(property->key());
4343
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004344 if (property->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004345 EmitKeyedLoad();
4346 // Put the function below the receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004347 // Use the global receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004348 frame_->EmitPush(r0); // Function.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004349 LoadGlobalReceiver(VirtualFrame::scratch0());
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004350 // Call the function.
4351 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
4352 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004353 } else {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004354 // Swap the name of the function and the receiver on the stack to follow
4355 // the calling convention for call ICs.
4356 Register key = frame_->PopToRegister();
4357 Register receiver = frame_->PopToRegister(key);
4358 frame_->EmitPush(key);
4359 frame_->EmitPush(receiver);
4360
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004361 // Load the arguments.
4362 int arg_count = args->length();
4363 for (int i = 0; i < arg_count; i++) {
4364 Load(args->at(i));
4365 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004366
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004367 // Load the key into r2 and call the IC initialization code.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004368 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004369 Handle<Code> stub =
4370 StubCache::ComputeKeyedCallInitialize(arg_count, in_loop);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004371 CodeForSourcePosition(node->position());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004372 frame_->SpillAll();
4373 __ ldr(r2, frame_->ElementAt(arg_count + 1));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004374 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004375 frame_->Drop(); // Drop the key still on the stack.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004376 __ ldr(cp, frame_->Context());
4377 frame_->EmitPush(r0);
4378 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004379 }
4380
4381 } else {
4382 // ----------------------------------
4383 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
4384 // ----------------------------------
4385
4386 // Load the function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004387 Load(function);
4388
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004389 // Pass the global proxy as the receiver.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004390 LoadGlobalReceiver(VirtualFrame::scratch0());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004391
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004392 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004393 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004394 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004395 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004396 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004397}
4398
4399
ager@chromium.org7c537e22008-10-16 08:43:32 +00004400void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004401#ifdef DEBUG
4402 int original_height = frame_->height();
4403#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004404 Comment cmnt(masm_, "[ CallNew");
4405
4406 // According to ECMA-262, section 11.2.2, page 44, the function
4407 // expression in new calls must be evaluated before the
4408 // arguments. This is different from ordinary calls, where the
4409 // actual function to call is resolved after the arguments have been
4410 // evaluated.
4411
ricow@chromium.org65fae842010-08-25 15:26:24 +00004412 // Push constructor on the stack. If it's not a function it's used as
4413 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
4414 // ignored.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004415 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004416
4417 // Push the arguments ("left-to-right") on the stack.
4418 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004419 int arg_count = args->length();
4420 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004421 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004422 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004423
ricow@chromium.org65fae842010-08-25 15:26:24 +00004424 // Spill everything from here to simplify the implementation.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004425 VirtualFrame::SpilledScope spilled_scope(frame_);
4426
ricow@chromium.org65fae842010-08-25 15:26:24 +00004427 // Load the argument count into r0 and the function into r1 as per
4428 // calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004429 __ mov(r0, Operand(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004430 __ ldr(r1, frame_->ElementAt(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004431
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004432 // Call the construct call builtin that handles allocation and
4433 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004434 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004435 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004436 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004437 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004438
ager@chromium.orgac091b72010-05-05 07:34:42 +00004439 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004440}
4441
4442
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004443void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004444 Register scratch = VirtualFrame::scratch0();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004445 JumpTarget null, function, leave, non_function_constructor;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004446
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004447 // Load the object into register.
4448 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004449 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004450 Register tos = frame_->PopToRegister();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004451
4452 // If the object is a smi, we return null.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004453 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004454 null.Branch(eq);
4455
4456 // Check that the object is a JS object but take special care of JS
4457 // functions to make sure they have 'Function' as their class.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004458 __ CompareObjectType(tos, tos, scratch, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004459 null.Branch(lt);
4460
4461 // As long as JS_FUNCTION_TYPE is the last instance type and it is
4462 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
4463 // LAST_JS_OBJECT_TYPE.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00004464 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4465 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004466 __ cmp(scratch, Operand(JS_FUNCTION_TYPE));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004467 function.Branch(eq);
4468
4469 // Check if the constructor in the map is a function.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004470 __ ldr(tos, FieldMemOperand(tos, Map::kConstructorOffset));
4471 __ CompareObjectType(tos, scratch, scratch, JS_FUNCTION_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004472 non_function_constructor.Branch(ne);
4473
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004474 // The tos register now contains the constructor function. Grab the
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004475 // instance class name from there.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004476 __ ldr(tos, FieldMemOperand(tos, JSFunction::kSharedFunctionInfoOffset));
4477 __ ldr(tos,
4478 FieldMemOperand(tos, SharedFunctionInfo::kInstanceClassNameOffset));
4479 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004480 leave.Jump();
4481
4482 // Functions have class 'Function'.
4483 function.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004484 __ mov(tos, Operand(Factory::function_class_symbol()));
4485 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004486 leave.Jump();
4487
4488 // Objects with a non-function constructor have class 'Object'.
4489 non_function_constructor.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004490 __ mov(tos, Operand(Factory::Object_symbol()));
4491 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004492 leave.Jump();
4493
4494 // Non-JS objects have class null.
4495 null.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004496 __ LoadRoot(tos, Heap::kNullValueRootIndex);
4497 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004498
4499 // All done.
4500 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004501}
4502
4503
ager@chromium.org7c537e22008-10-16 08:43:32 +00004504void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004505 Register scratch = VirtualFrame::scratch0();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004506 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004507
4508 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004509 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004510 Register tos = frame_->PopToRegister(); // tos contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00004511 // if (object->IsSmi()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004512 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004513 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004514 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004515 __ CompareObjectType(tos, scratch, scratch, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004516 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004517 // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004518 __ ldr(tos, FieldMemOperand(tos, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004519 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004520 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004521}
4522
4523
ager@chromium.org7c537e22008-10-16 08:43:32 +00004524void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004525 Register scratch1 = VirtualFrame::scratch0();
4526 Register scratch2 = VirtualFrame::scratch1();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004527 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004528
4529 ASSERT(args->length() == 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004530 Load(args->at(0)); // Load the object.
4531 Load(args->at(1)); // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004532 Register value = frame_->PopToRegister();
4533 Register object = frame_->PopToRegister(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004534 // if (object->IsSmi()) return object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004535 __ tst(object, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004536 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004537 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004538 __ CompareObjectType(object, scratch1, scratch1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004539 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004540 // Store the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004541 __ str(value, FieldMemOperand(object, JSValue::kValueOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004542 // Update the write barrier.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004543 __ RecordWrite(object,
4544 Operand(JSValue::kValueOffset - kHeapObjectTag),
4545 scratch1,
4546 scratch2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004547 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004548 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004549 frame_->EmitPush(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004550}
4551
4552
ager@chromium.org7c537e22008-10-16 08:43:32 +00004553void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004554 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004555 Load(args->at(0));
4556 Register reg = frame_->PopToRegister();
4557 __ tst(reg, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004558 cc_reg_ = eq;
4559}
4560
4561
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004562void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
4563 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
4564 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004565#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004566 if (ShouldGenerateLog(args->at(0))) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004567 Load(args->at(1));
4568 Load(args->at(2));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004569 frame_->CallRuntime(Runtime::kLog, 2);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004570 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004571#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004572 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004573}
4574
4575
ager@chromium.org7c537e22008-10-16 08:43:32 +00004576void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004577 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004578 Load(args->at(0));
4579 Register reg = frame_->PopToRegister();
4580 __ tst(reg, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004581 cc_reg_ = eq;
4582}
4583
4584
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004585// Generates the Math.pow method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004586void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
4587 ASSERT(args->length() == 2);
4588 Load(args->at(0));
4589 Load(args->at(1));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004590
4591 if (!CpuFeatures::IsSupported(VFP3)) {
4592 frame_->CallRuntime(Runtime::kMath_pow, 2);
4593 frame_->EmitPush(r0);
4594 } else {
4595 CpuFeatures::Scope scope(VFP3);
4596 JumpTarget runtime, done;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004597 Label exponent_nonsmi, base_nonsmi, powi, not_minus_half, allocate_return;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004598
4599 Register scratch1 = VirtualFrame::scratch0();
4600 Register scratch2 = VirtualFrame::scratch1();
4601
4602 // Get base and exponent to registers.
4603 Register exponent = frame_->PopToRegister();
4604 Register base = frame_->PopToRegister(exponent);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004605 Register heap_number_map = no_reg;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004606
4607 // Set the frame for the runtime jump target. The code below jumps to the
4608 // jump target label so the frame needs to be established before that.
4609 ASSERT(runtime.entry_frame() == NULL);
4610 runtime.set_entry_frame(frame_);
4611
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004612 __ BranchOnNotSmi(exponent, &exponent_nonsmi);
4613 __ BranchOnNotSmi(base, &base_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004614
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004615 heap_number_map = r6;
4616 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4617
4618 // Exponent is a smi and base is a smi. Get the smi value into vfp register
4619 // d1.
4620 __ SmiToDoubleVFPRegister(base, d1, scratch1, s0);
4621 __ b(&powi);
4622
4623 __ bind(&base_nonsmi);
4624 // Exponent is smi and base is non smi. Get the double value from the base
4625 // into vfp register d1.
4626 __ ObjectToDoubleVFPRegister(base, d1,
4627 scratch1, scratch2, heap_number_map, s0,
4628 runtime.entry_label());
4629
4630 __ bind(&powi);
4631
4632 // Load 1.0 into d0.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004633 __ vmov(d0, 1.0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004634
4635 // Get the absolute untagged value of the exponent and use that for the
4636 // calculation.
4637 __ mov(scratch1, Operand(exponent, ASR, kSmiTagSize), SetCC);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00004638 // Negate if negative.
4639 __ rsb(scratch1, scratch1, Operand(0, RelocInfo::NONE), LeaveCC, mi);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004640 __ vmov(d2, d0, mi); // 1.0 needed in d2 later if exponent is negative.
4641
4642 // Run through all the bits in the exponent. The result is calculated in d0
4643 // and d1 holds base^(bit^2).
4644 Label more_bits;
4645 __ bind(&more_bits);
4646 __ mov(scratch1, Operand(scratch1, LSR, 1), SetCC);
4647 __ vmul(d0, d0, d1, cs); // Multiply with base^(bit^2) if bit is set.
4648 __ vmul(d1, d1, d1, ne); // Don't bother calculating next d1 if done.
4649 __ b(ne, &more_bits);
4650
4651 // If exponent is positive we are done.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00004652 __ cmp(exponent, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004653 __ b(ge, &allocate_return);
4654
4655 // If exponent is negative result is 1/result (d2 already holds 1.0 in that
4656 // case). However if d0 has reached infinity this will not provide the
4657 // correct result, so call runtime if that is the case.
4658 __ mov(scratch2, Operand(0x7FF00000));
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00004659 __ mov(scratch1, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004660 __ vmov(d1, scratch1, scratch2); // Load infinity into d1.
4661 __ vcmp(d0, d1);
4662 __ vmrs(pc);
4663 runtime.Branch(eq); // d0 reached infinity.
4664 __ vdiv(d0, d2, d0);
4665 __ b(&allocate_return);
4666
4667 __ bind(&exponent_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004668 // Special handling of raising to the power of -0.5 and 0.5. First check
4669 // that the value is a heap number and that the lower bits (which for both
4670 // values are zero).
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004671 heap_number_map = r6;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004672 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4673 __ ldr(scratch1, FieldMemOperand(exponent, HeapObject::kMapOffset));
4674 __ ldr(scratch2, FieldMemOperand(exponent, HeapNumber::kMantissaOffset));
4675 __ cmp(scratch1, heap_number_map);
4676 runtime.Branch(ne);
4677 __ tst(scratch2, scratch2);
4678 runtime.Branch(ne);
4679
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004680 // Load the higher bits (which contains the floating point exponent).
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004681 __ ldr(scratch1, FieldMemOperand(exponent, HeapNumber::kExponentOffset));
4682
4683 // Compare exponent with -0.5.
4684 __ cmp(scratch1, Operand(0xbfe00000));
4685 __ b(ne, &not_minus_half);
4686
4687 // Get the double value from the base into vfp register d0.
4688 __ ObjectToDoubleVFPRegister(base, d0,
4689 scratch1, scratch2, heap_number_map, s0,
4690 runtime.entry_label(),
4691 AVOID_NANS_AND_INFINITIES);
4692
4693 // Load 1.0 into d2.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004694 __ vmov(d2, 1.0);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004695
4696 // Calculate the reciprocal of the square root. 1/sqrt(x) = sqrt(1/x).
4697 __ vdiv(d0, d2, d0);
4698 __ vsqrt(d0, d0);
4699
4700 __ b(&allocate_return);
4701
4702 __ bind(&not_minus_half);
4703 // Compare exponent with 0.5.
4704 __ cmp(scratch1, Operand(0x3fe00000));
4705 runtime.Branch(ne);
4706
4707 // Get the double value from the base into vfp register d0.
4708 __ ObjectToDoubleVFPRegister(base, d0,
4709 scratch1, scratch2, heap_number_map, s0,
4710 runtime.entry_label(),
4711 AVOID_NANS_AND_INFINITIES);
4712 __ vsqrt(d0, d0);
4713
4714 __ bind(&allocate_return);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004715 Register scratch3 = r5;
4716 __ AllocateHeapNumberWithValue(scratch3, d0, scratch1, scratch2,
4717 heap_number_map, runtime.entry_label());
4718 __ mov(base, scratch3);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004719 done.Jump();
4720
4721 runtime.Bind();
4722
4723 // Push back the arguments again for the runtime call.
4724 frame_->EmitPush(base);
4725 frame_->EmitPush(exponent);
4726 frame_->CallRuntime(Runtime::kMath_pow, 2);
4727 __ Move(base, r0);
4728
4729 done.Bind();
4730 frame_->EmitPush(base);
4731 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004732}
4733
4734
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004735// Generates the Math.sqrt method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004736void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
4737 ASSERT(args->length() == 1);
4738 Load(args->at(0));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004739
4740 if (!CpuFeatures::IsSupported(VFP3)) {
4741 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4742 frame_->EmitPush(r0);
4743 } else {
4744 CpuFeatures::Scope scope(VFP3);
4745 JumpTarget runtime, done;
4746
4747 Register scratch1 = VirtualFrame::scratch0();
4748 Register scratch2 = VirtualFrame::scratch1();
4749
4750 // Get the value from the frame.
4751 Register tos = frame_->PopToRegister();
4752
4753 // Set the frame for the runtime jump target. The code below jumps to the
4754 // jump target label so the frame needs to be established before that.
4755 ASSERT(runtime.entry_frame() == NULL);
4756 runtime.set_entry_frame(frame_);
4757
4758 Register heap_number_map = r6;
lrn@chromium.org303ada72010-10-27 09:33:13 +00004759 Register new_heap_number = r5;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004760 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4761
4762 // Get the double value from the heap number into vfp register d0.
4763 __ ObjectToDoubleVFPRegister(tos, d0,
4764 scratch1, scratch2, heap_number_map, s0,
4765 runtime.entry_label());
4766
4767 // Calculate the square root of d0 and place result in a heap number object.
4768 __ vsqrt(d0, d0);
lrn@chromium.org303ada72010-10-27 09:33:13 +00004769 __ AllocateHeapNumberWithValue(new_heap_number,
4770 d0,
4771 scratch1, scratch2,
4772 heap_number_map,
4773 runtime.entry_label());
4774 __ mov(tos, Operand(new_heap_number));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004775 done.Jump();
4776
4777 runtime.Bind();
4778 // Push back the argument again for the runtime call.
4779 frame_->EmitPush(tos);
4780 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4781 __ Move(tos, r0);
4782
4783 done.Bind();
4784 frame_->EmitPush(tos);
4785 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004786}
4787
4788
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004789class DeferredStringCharCodeAt : public DeferredCode {
4790 public:
4791 DeferredStringCharCodeAt(Register object,
4792 Register index,
4793 Register scratch,
4794 Register result)
4795 : result_(result),
4796 char_code_at_generator_(object,
4797 index,
4798 scratch,
4799 result,
4800 &need_conversion_,
4801 &need_conversion_,
4802 &index_out_of_range_,
4803 STRING_INDEX_IS_NUMBER) {}
4804
4805 StringCharCodeAtGenerator* fast_case_generator() {
4806 return &char_code_at_generator_;
4807 }
4808
4809 virtual void Generate() {
4810 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4811 char_code_at_generator_.GenerateSlow(masm(), call_helper);
4812
4813 __ bind(&need_conversion_);
4814 // Move the undefined value into the result register, which will
4815 // trigger conversion.
4816 __ LoadRoot(result_, Heap::kUndefinedValueRootIndex);
4817 __ jmp(exit_label());
4818
4819 __ bind(&index_out_of_range_);
4820 // When the index is out of range, the spec requires us to return
4821 // NaN.
4822 __ LoadRoot(result_, Heap::kNanValueRootIndex);
4823 __ jmp(exit_label());
4824 }
4825
4826 private:
4827 Register result_;
4828
4829 Label need_conversion_;
4830 Label index_out_of_range_;
4831
4832 StringCharCodeAtGenerator char_code_at_generator_;
4833};
4834
4835
4836// This generates code that performs a String.prototype.charCodeAt() call
4837// or returns a smi in order to trigger conversion.
4838void CodeGenerator::GenerateStringCharCodeAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004839 Comment(masm_, "[ GenerateStringCharCodeAt");
kasper.lund7276f142008-07-30 08:49:36 +00004840 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004841
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004842 Load(args->at(0));
4843 Load(args->at(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004844
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004845 Register index = frame_->PopToRegister();
4846 Register object = frame_->PopToRegister(index);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004847
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004848 // We need two extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004849 Register scratch = VirtualFrame::scratch0();
4850 Register result = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004851
4852 DeferredStringCharCodeAt* deferred =
4853 new DeferredStringCharCodeAt(object,
4854 index,
4855 scratch,
4856 result);
4857 deferred->fast_case_generator()->GenerateFast(masm_);
4858 deferred->BindExit();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004859 frame_->EmitPush(result);
kasper.lund7276f142008-07-30 08:49:36 +00004860}
4861
4862
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004863class DeferredStringCharFromCode : public DeferredCode {
4864 public:
4865 DeferredStringCharFromCode(Register code,
4866 Register result)
4867 : char_from_code_generator_(code, result) {}
4868
4869 StringCharFromCodeGenerator* fast_case_generator() {
4870 return &char_from_code_generator_;
4871 }
4872
4873 virtual void Generate() {
4874 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4875 char_from_code_generator_.GenerateSlow(masm(), call_helper);
4876 }
4877
4878 private:
4879 StringCharFromCodeGenerator char_from_code_generator_;
4880};
4881
4882
4883// Generates code for creating a one-char string from a char code.
4884void CodeGenerator::GenerateStringCharFromCode(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004885 Comment(masm_, "[ GenerateStringCharFromCode");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004886 ASSERT(args->length() == 1);
4887
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004888 Load(args->at(0));
4889
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004890 Register result = frame_->GetTOSRegister();
4891 Register code = frame_->PopToRegister(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004892
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004893 DeferredStringCharFromCode* deferred = new DeferredStringCharFromCode(
4894 code, result);
4895 deferred->fast_case_generator()->GenerateFast(masm_);
4896 deferred->BindExit();
4897 frame_->EmitPush(result);
4898}
4899
4900
4901class DeferredStringCharAt : public DeferredCode {
4902 public:
4903 DeferredStringCharAt(Register object,
4904 Register index,
4905 Register scratch1,
4906 Register scratch2,
4907 Register result)
4908 : result_(result),
4909 char_at_generator_(object,
4910 index,
4911 scratch1,
4912 scratch2,
4913 result,
4914 &need_conversion_,
4915 &need_conversion_,
4916 &index_out_of_range_,
4917 STRING_INDEX_IS_NUMBER) {}
4918
4919 StringCharAtGenerator* fast_case_generator() {
4920 return &char_at_generator_;
4921 }
4922
4923 virtual void Generate() {
4924 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4925 char_at_generator_.GenerateSlow(masm(), call_helper);
4926
4927 __ bind(&need_conversion_);
4928 // Move smi zero into the result register, which will trigger
4929 // conversion.
4930 __ mov(result_, Operand(Smi::FromInt(0)));
4931 __ jmp(exit_label());
4932
4933 __ bind(&index_out_of_range_);
4934 // When the index is out of range, the spec requires us to return
4935 // the empty string.
4936 __ LoadRoot(result_, Heap::kEmptyStringRootIndex);
4937 __ jmp(exit_label());
4938 }
4939
4940 private:
4941 Register result_;
4942
4943 Label need_conversion_;
4944 Label index_out_of_range_;
4945
4946 StringCharAtGenerator char_at_generator_;
4947};
4948
4949
4950// This generates code that performs a String.prototype.charAt() call
4951// or returns a smi in order to trigger conversion.
4952void CodeGenerator::GenerateStringCharAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004953 Comment(masm_, "[ GenerateStringCharAt");
4954 ASSERT(args->length() == 2);
4955
4956 Load(args->at(0));
4957 Load(args->at(1));
4958
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004959 Register index = frame_->PopToRegister();
4960 Register object = frame_->PopToRegister(index);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004961
4962 // We need three extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004963 Register scratch1 = VirtualFrame::scratch0();
4964 Register scratch2 = VirtualFrame::scratch1();
4965 // Use r6 without notifying the virtual frame.
4966 Register result = r6;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004967
4968 DeferredStringCharAt* deferred =
4969 new DeferredStringCharAt(object,
4970 index,
4971 scratch1,
4972 scratch2,
4973 result);
4974 deferred->fast_case_generator()->GenerateFast(masm_);
4975 deferred->BindExit();
ager@chromium.orgac091b72010-05-05 07:34:42 +00004976 frame_->EmitPush(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004977}
4978
4979
ager@chromium.org7c537e22008-10-16 08:43:32 +00004980void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004981 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004982 Load(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004983 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004984 // We need the CC bits to come out as not_equal in the case where the
4985 // object is a smi. This can't be done with the usual test opcode so
4986 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004987 Register possible_array = frame_->PopToRegister();
4988 Register scratch = VirtualFrame::scratch0();
4989 __ and_(scratch, possible_array, Operand(kSmiTagMask));
4990 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004991 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004992 // It is a heap object - get the map. Check if the object is a JS array.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004993 __ CompareObjectType(possible_array, scratch, scratch, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004994 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004995 cc_reg_ = eq;
4996}
4997
4998
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004999void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00005000 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005001 Load(args->at(0));
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00005002 JumpTarget answer;
5003 // We need the CC bits to come out as not_equal in the case where the
5004 // object is a smi. This can't be done with the usual test opcode so
5005 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005006 Register possible_regexp = frame_->PopToRegister();
5007 Register scratch = VirtualFrame::scratch0();
5008 __ and_(scratch, possible_regexp, Operand(kSmiTagMask));
5009 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00005010 answer.Branch(ne);
5011 // It is a heap object - get the map. Check if the object is a regexp.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005012 __ CompareObjectType(possible_regexp, scratch, scratch, JS_REGEXP_TYPE);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00005013 answer.Bind();
5014 cc_reg_ = eq;
5015}
5016
5017
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005018void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
5019 // This generates a fast version of:
5020 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005021 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005022 Load(args->at(0));
5023 Register possible_object = frame_->PopToRegister();
5024 __ tst(possible_object, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005025 false_target()->Branch(eq);
5026
5027 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005028 __ cmp(possible_object, ip);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005029 true_target()->Branch(eq);
5030
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005031 Register map_reg = VirtualFrame::scratch0();
5032 __ ldr(map_reg, FieldMemOperand(possible_object, HeapObject::kMapOffset));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005033 // Undetectable objects behave like undefined when tested with typeof.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005034 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kBitFieldOffset));
5035 __ tst(possible_object, Operand(1 << Map::kIsUndetectable));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005036 false_target()->Branch(ne);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005037
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005038 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
5039 __ cmp(possible_object, Operand(FIRST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005040 false_target()->Branch(lt);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005041 __ cmp(possible_object, Operand(LAST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005042 cc_reg_ = le;
5043}
5044
5045
ricow@chromium.org4980dff2010-07-19 08:33:45 +00005046void CodeGenerator::GenerateIsSpecObject(ZoneList<Expression*>* args) {
5047 // This generates a fast version of:
5048 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp' ||
5049 // typeof(arg) == function).
5050 // It includes undetectable objects (as opposed to IsObject).
5051 ASSERT(args->length() == 1);
5052 Load(args->at(0));
5053 Register value = frame_->PopToRegister();
5054 __ tst(value, Operand(kSmiTagMask));
5055 false_target()->Branch(eq);
5056 // Check that this is an object.
5057 __ ldr(value, FieldMemOperand(value, HeapObject::kMapOffset));
5058 __ ldrb(value, FieldMemOperand(value, Map::kInstanceTypeOffset));
5059 __ cmp(value, Operand(FIRST_JS_OBJECT_TYPE));
5060 cc_reg_ = ge;
5061}
5062
5063
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00005064// Deferred code to check whether the String JavaScript object is safe for using
5065// default value of. This code is called after the bit caching this information
5066// in the map has been checked with the map for the object in the map_result_
5067// register. On return the register map_result_ contains 1 for true and 0 for
5068// false.
5069class DeferredIsStringWrapperSafeForDefaultValueOf : public DeferredCode {
5070 public:
5071 DeferredIsStringWrapperSafeForDefaultValueOf(Register object,
5072 Register map_result,
5073 Register scratch1,
5074 Register scratch2)
5075 : object_(object),
5076 map_result_(map_result),
5077 scratch1_(scratch1),
5078 scratch2_(scratch2) { }
5079
5080 virtual void Generate() {
5081 Label false_result;
5082
5083 // Check that map is loaded as expected.
5084 if (FLAG_debug_code) {
5085 __ ldr(ip, FieldMemOperand(object_, HeapObject::kMapOffset));
5086 __ cmp(map_result_, ip);
5087 __ Assert(eq, "Map not in expected register");
5088 }
5089
5090 // Check for fast case object. Generate false result for slow case object.
5091 __ ldr(scratch1_, FieldMemOperand(object_, JSObject::kPropertiesOffset));
5092 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
5093 __ LoadRoot(ip, Heap::kHashTableMapRootIndex);
5094 __ cmp(scratch1_, ip);
5095 __ b(eq, &false_result);
5096
5097 // Look for valueOf symbol in the descriptor array, and indicate false if
5098 // found. The type is not checked, so if it is a transition it is a false
5099 // negative.
5100 __ ldr(map_result_,
5101 FieldMemOperand(map_result_, Map::kInstanceDescriptorsOffset));
5102 __ ldr(scratch2_, FieldMemOperand(map_result_, FixedArray::kLengthOffset));
5103 // map_result_: descriptor array
5104 // scratch2_: length of descriptor array
5105 // Calculate the end of the descriptor array.
5106 STATIC_ASSERT(kSmiTag == 0);
5107 STATIC_ASSERT(kSmiTagSize == 1);
5108 STATIC_ASSERT(kPointerSize == 4);
5109 __ add(scratch1_,
5110 map_result_,
5111 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5112 __ add(scratch1_,
5113 scratch1_,
5114 Operand(scratch2_, LSL, kPointerSizeLog2 - kSmiTagSize));
5115
5116 // Calculate location of the first key name.
5117 __ add(map_result_,
5118 map_result_,
5119 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
5120 DescriptorArray::kFirstIndex * kPointerSize));
5121 // Loop through all the keys in the descriptor array. If one of these is the
5122 // symbol valueOf the result is false.
5123 Label entry, loop;
5124 // The use of ip to store the valueOf symbol asumes that it is not otherwise
5125 // used in the loop below.
5126 __ mov(ip, Operand(Factory::value_of_symbol()));
5127 __ jmp(&entry);
5128 __ bind(&loop);
5129 __ ldr(scratch2_, MemOperand(map_result_, 0));
5130 __ cmp(scratch2_, ip);
5131 __ b(eq, &false_result);
5132 __ add(map_result_, map_result_, Operand(kPointerSize));
5133 __ bind(&entry);
5134 __ cmp(map_result_, Operand(scratch1_));
5135 __ b(ne, &loop);
5136
5137 // Reload map as register map_result_ was used as temporary above.
5138 __ ldr(map_result_, FieldMemOperand(object_, HeapObject::kMapOffset));
5139
5140 // If a valueOf property is not found on the object check that it's
5141 // prototype is the un-modified String prototype. If not result is false.
5142 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kPrototypeOffset));
5143 __ tst(scratch1_, Operand(kSmiTagMask));
5144 __ b(eq, &false_result);
5145 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
5146 __ ldr(scratch2_,
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005147 ContextOperand(cp, Context::GLOBAL_INDEX));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00005148 __ ldr(scratch2_,
5149 FieldMemOperand(scratch2_, GlobalObject::kGlobalContextOffset));
5150 __ ldr(scratch2_,
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005151 ContextOperand(
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00005152 scratch2_, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
5153 __ cmp(scratch1_, scratch2_);
5154 __ b(ne, &false_result);
5155
5156 // Set the bit in the map to indicate that it has been checked safe for
5157 // default valueOf and set true result.
5158 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
5159 __ orr(scratch1_,
5160 scratch1_,
5161 Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
5162 __ str(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
5163 __ mov(map_result_, Operand(1));
5164 __ jmp(exit_label());
5165 __ bind(&false_result);
5166 // Set false result.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00005167 __ mov(map_result_, Operand(0, RelocInfo::NONE));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00005168 }
5169
5170 private:
5171 Register object_;
5172 Register map_result_;
5173 Register scratch1_;
5174 Register scratch2_;
5175};
5176
5177
5178void CodeGenerator::GenerateIsStringWrapperSafeForDefaultValueOf(
5179 ZoneList<Expression*>* args) {
5180 ASSERT(args->length() == 1);
5181 Load(args->at(0));
5182 Register obj = frame_->PopToRegister(); // Pop the string wrapper.
5183 if (FLAG_debug_code) {
5184 __ AbortIfSmi(obj);
5185 }
5186
5187 // Check whether this map has already been checked to be safe for default
5188 // valueOf.
5189 Register map_result = VirtualFrame::scratch0();
5190 __ ldr(map_result, FieldMemOperand(obj, HeapObject::kMapOffset));
5191 __ ldrb(ip, FieldMemOperand(map_result, Map::kBitField2Offset));
5192 __ tst(ip, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
5193 true_target()->Branch(ne);
5194
5195 // We need an additional two scratch registers for the deferred code.
5196 Register scratch1 = VirtualFrame::scratch1();
5197 // Use r6 without notifying the virtual frame.
5198 Register scratch2 = r6;
5199
5200 DeferredIsStringWrapperSafeForDefaultValueOf* deferred =
5201 new DeferredIsStringWrapperSafeForDefaultValueOf(
5202 obj, map_result, scratch1, scratch2);
5203 deferred->Branch(eq);
5204 deferred->BindExit();
5205 __ tst(map_result, Operand(map_result));
5206 cc_reg_ = ne;
5207}
5208
5209
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005210void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
5211 // This generates a fast version of:
5212 // (%_ClassOf(arg) === 'Function')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005213 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005214 Load(args->at(0));
5215 Register possible_function = frame_->PopToRegister();
5216 __ tst(possible_function, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005217 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005218 Register map_reg = VirtualFrame::scratch0();
5219 Register scratch = VirtualFrame::scratch1();
5220 __ CompareObjectType(possible_function, map_reg, scratch, JS_FUNCTION_TYPE);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005221 cc_reg_ = eq;
5222}
5223
5224
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005225void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005226 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005227 Load(args->at(0));
5228 Register possible_undetectable = frame_->PopToRegister();
5229 __ tst(possible_undetectable, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005230 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005231 Register scratch = VirtualFrame::scratch0();
5232 __ ldr(scratch,
5233 FieldMemOperand(possible_undetectable, HeapObject::kMapOffset));
5234 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5235 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005236 cc_reg_ = ne;
5237}
5238
5239
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005240void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005241 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005242
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005243 Register scratch0 = VirtualFrame::scratch0();
5244 Register scratch1 = VirtualFrame::scratch1();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005245 // Get the frame pointer for the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005246 __ ldr(scratch0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005247
5248 // Skip the arguments adaptor frame if it exists.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005249 __ ldr(scratch1,
5250 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5251 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5252 __ ldr(scratch0,
5253 MemOperand(scratch0, StandardFrameConstants::kCallerFPOffset), eq);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005254
5255 // Check the marker in the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005256 __ ldr(scratch1,
5257 MemOperand(scratch0, StandardFrameConstants::kMarkerOffset));
5258 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005259 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005260}
5261
5262
ager@chromium.org7c537e22008-10-16 08:43:32 +00005263void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005264 ASSERT(args->length() == 0);
5265
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005266 Register tos = frame_->GetTOSRegister();
5267 Register scratch0 = VirtualFrame::scratch0();
5268 Register scratch1 = VirtualFrame::scratch1();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005269
lrn@chromium.org25156de2010-04-06 13:10:27 +00005270 // Check if the calling frame is an arguments adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005271 __ ldr(scratch0,
5272 MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5273 __ ldr(scratch1,
5274 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5275 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5276
5277 // Get the number of formal parameters.
5278 __ mov(tos, Operand(Smi::FromInt(scope()->num_parameters())), LeaveCC, ne);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005279
5280 // Arguments adaptor case: Read the arguments length from the
5281 // adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005282 __ ldr(tos,
5283 MemOperand(scratch0, ArgumentsAdaptorFrameConstants::kLengthOffset),
5284 eq);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005285
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005286 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005287}
5288
5289
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00005290void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005291 ASSERT(args->length() == 1);
5292
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00005293 // Satisfy contract with ArgumentsAccessStub:
5294 // Load the key into r1 and the formal parameters count into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005295 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005296 frame_->PopToR1();
5297 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005298 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005299
5300 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005301 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005302 frame_->CallStub(&stub, 0);
5303 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005304}
5305
5306
ager@chromium.org357bf652010-04-12 11:30:10 +00005307void CodeGenerator::GenerateRandomHeapNumber(
5308 ZoneList<Expression*>* args) {
5309 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005310 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00005311
5312 Label slow_allocate_heapnumber;
5313 Label heapnumber_allocated;
5314
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005315 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
5316 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00005317 __ jmp(&heapnumber_allocated);
5318
5319 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00005320 // Allocate a heap number.
5321 __ CallRuntime(Runtime::kNumberAlloc, 0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005322 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00005323
5324 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005325
5326 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
5327 // by computing:
5328 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
5329 if (CpuFeatures::IsSupported(VFP3)) {
5330 __ PrepareCallCFunction(0, r1);
5331 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
5332
5333 CpuFeatures::Scope scope(VFP3);
5334 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
5335 // Create this constant using mov/orr to avoid PC relative load.
5336 __ mov(r1, Operand(0x41000000));
5337 __ orr(r1, r1, Operand(0x300000));
5338 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
5339 __ vmov(d7, r0, r1);
5340 // Move 0x4130000000000000 to VFP.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00005341 __ mov(r0, Operand(0, RelocInfo::NONE));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005342 __ vmov(d8, r0, r1);
5343 // Subtract and store the result in the heap number.
5344 __ vsub(d7, d7, d8);
5345 __ sub(r0, r4, Operand(kHeapObjectTag));
5346 __ vstr(d7, r0, HeapNumber::kValueOffset);
5347 frame_->EmitPush(r4);
5348 } else {
5349 __ mov(r0, Operand(r4));
5350 __ PrepareCallCFunction(1, r1);
5351 __ CallCFunction(
5352 ExternalReference::fill_heap_number_with_random_function(), 1);
5353 frame_->EmitPush(r0);
5354 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005355}
5356
5357
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005358void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
5359 ASSERT_EQ(2, args->length());
5360
5361 Load(args->at(0));
5362 Load(args->at(1));
5363
ager@chromium.org5c838252010-02-19 08:53:10 +00005364 StringAddStub stub(NO_STRING_ADD_FLAGS);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005365 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005366 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005367 frame_->EmitPush(r0);
5368}
5369
5370
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005371void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
5372 ASSERT_EQ(3, args->length());
5373
5374 Load(args->at(0));
5375 Load(args->at(1));
5376 Load(args->at(2));
5377
ager@chromium.org5c838252010-02-19 08:53:10 +00005378 SubStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005379 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005380 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005381 frame_->EmitPush(r0);
5382}
5383
5384
5385void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
5386 ASSERT_EQ(2, args->length());
5387
5388 Load(args->at(0));
5389 Load(args->at(1));
5390
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005391 StringCompareStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005392 frame_->SpillAll();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005393 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005394 frame_->EmitPush(r0);
5395}
5396
5397
5398void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
5399 ASSERT_EQ(4, args->length());
5400
5401 Load(args->at(0));
5402 Load(args->at(1));
5403 Load(args->at(2));
5404 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005405 RegExpExecStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005406 frame_->SpillAll();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005407 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005408 frame_->EmitPush(r0);
5409}
5410
5411
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005412void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
5413 // No stub. This code only occurs a few times in regexp.js.
5414 const int kMaxInlineLength = 100;
5415 ASSERT_EQ(3, args->length());
5416 Load(args->at(0)); // Size of array, smi.
5417 Load(args->at(1)); // "index" property value.
5418 Load(args->at(2)); // "input" property value.
5419 {
5420 VirtualFrame::SpilledScope spilled_scope(frame_);
5421 Label slowcase;
5422 Label done;
5423 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
5424 STATIC_ASSERT(kSmiTag == 0);
5425 STATIC_ASSERT(kSmiTagSize == 1);
5426 __ tst(r1, Operand(kSmiTagMask));
5427 __ b(ne, &slowcase);
5428 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
5429 __ b(hi, &slowcase);
5430 // Smi-tagging is equivalent to multiplying by 2.
5431 // Allocate RegExpResult followed by FixedArray with size in ebx.
5432 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
5433 // Elements: [Map][Length][..elements..]
5434 // Size of JSArray with two in-object properties and the header of a
5435 // FixedArray.
5436 int objects_size =
5437 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
5438 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
5439 __ add(r2, r5, Operand(objects_size));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005440 __ AllocateInNewSpace(
5441 r2, // In: Size, in words.
5442 r0, // Out: Start of allocation (tagged).
5443 r3, // Scratch register.
5444 r4, // Scratch register.
5445 &slowcase,
5446 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005447 // r0: Start of allocated area, object-tagged.
5448 // r1: Number of elements in array, as smi.
5449 // r5: Number of elements, untagged.
5450
5451 // Set JSArray map to global.regexp_result_map().
5452 // Set empty properties FixedArray.
5453 // Set elements to point to FixedArray allocated right after the JSArray.
5454 // Interleave operations for better latency.
5455 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
5456 __ add(r3, r0, Operand(JSRegExpResult::kSize));
5457 __ mov(r4, Operand(Factory::empty_fixed_array()));
5458 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5459 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
5460 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
5461 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5462 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5463
5464 // Set input, index and length fields from arguments.
5465 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
5466 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
5467 __ add(sp, sp, Operand(kPointerSize));
5468 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
5469 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
5470
5471 // Fill out the elements FixedArray.
5472 // r0: JSArray, tagged.
5473 // r3: FixedArray, tagged.
5474 // r5: Number of elements in array, untagged.
5475
5476 // Set map.
5477 __ mov(r2, Operand(Factory::fixed_array_map()));
5478 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
5479 // Set FixedArray length.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005480 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
5481 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005482 // Fill contents of fixed-array with the-hole.
5483 __ mov(r2, Operand(Factory::the_hole_value()));
5484 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5485 // Fill fixed array elements with hole.
5486 // r0: JSArray, tagged.
5487 // r2: the hole.
5488 // r3: Start of elements in FixedArray.
5489 // r5: Number of elements to fill.
5490 Label loop;
5491 __ tst(r5, Operand(r5));
5492 __ bind(&loop);
5493 __ b(le, &done); // Jump if r1 is negative or zero.
5494 __ sub(r5, r5, Operand(1), SetCC);
5495 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
5496 __ jmp(&loop);
5497
5498 __ bind(&slowcase);
5499 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
5500
5501 __ bind(&done);
5502 }
5503 frame_->Forget(3);
5504 frame_->EmitPush(r0);
5505}
5506
5507
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005508class DeferredSearchCache: public DeferredCode {
5509 public:
5510 DeferredSearchCache(Register dst, Register cache, Register key)
5511 : dst_(dst), cache_(cache), key_(key) {
5512 set_comment("[ DeferredSearchCache");
5513 }
5514
5515 virtual void Generate();
5516
5517 private:
5518 Register dst_, cache_, key_;
5519};
5520
5521
5522void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005523 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005524 __ CallRuntime(Runtime::kGetFromCache, 2);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005525 __ Move(dst_, r0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005526}
5527
5528
5529void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
5530 ASSERT_EQ(2, args->length());
5531
5532 ASSERT_NE(NULL, args->at(0)->AsLiteral());
5533 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
5534
5535 Handle<FixedArray> jsfunction_result_caches(
5536 Top::global_context()->jsfunction_result_caches());
5537 if (jsfunction_result_caches->length() <= cache_id) {
5538 __ Abort("Attempt to use undefined cache.");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005539 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005540 return;
5541 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005542
5543 Load(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005544
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005545 frame_->PopToR1();
5546 frame_->SpillAll();
5547 Register key = r1; // Just poped to r1
5548 Register result = r0; // Free, as frame has just been spilled.
5549 Register scratch1 = VirtualFrame::scratch0();
5550 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005551
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005552 __ ldr(scratch1, ContextOperand(cp, Context::GLOBAL_INDEX));
5553 __ ldr(scratch1,
5554 FieldMemOperand(scratch1, GlobalObject::kGlobalContextOffset));
5555 __ ldr(scratch1,
5556 ContextOperand(scratch1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
5557 __ ldr(scratch1,
5558 FieldMemOperand(scratch1, FixedArray::OffsetOfElementAt(cache_id)));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005559
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005560 DeferredSearchCache* deferred =
5561 new DeferredSearchCache(result, scratch1, key);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005562
5563 const int kFingerOffset =
5564 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00005565 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005566 __ ldr(result, FieldMemOperand(scratch1, kFingerOffset));
5567 // result now holds finger offset as a smi.
5568 __ add(scratch2, scratch1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5569 // scratch2 now points to the start of fixed array elements.
5570 __ ldr(result,
5571 MemOperand(
5572 scratch2, result, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
5573 // Note side effect of PreIndex: scratch2 now points to the key of the pair.
5574 __ cmp(key, result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005575 deferred->Branch(ne);
5576
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005577 __ ldr(result, MemOperand(scratch2, kPointerSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005578
5579 deferred->BindExit();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005580 frame_->EmitPush(result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005581}
5582
5583
ager@chromium.org5c838252010-02-19 08:53:10 +00005584void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
5585 ASSERT_EQ(args->length(), 1);
5586
5587 // Load the argument on the stack and jump to the runtime.
5588 Load(args->at(0));
5589
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005590 NumberToStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005591 frame_->SpillAll();
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005592 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005593 frame_->EmitPush(r0);
5594}
5595
5596
ager@chromium.orgac091b72010-05-05 07:34:42 +00005597class DeferredSwapElements: public DeferredCode {
5598 public:
5599 DeferredSwapElements(Register object, Register index1, Register index2)
5600 : object_(object), index1_(index1), index2_(index2) {
5601 set_comment("[ DeferredSwapElements");
5602 }
5603
5604 virtual void Generate();
5605
5606 private:
5607 Register object_, index1_, index2_;
5608};
5609
5610
5611void DeferredSwapElements::Generate() {
5612 __ push(object_);
5613 __ push(index1_);
5614 __ push(index2_);
5615 __ CallRuntime(Runtime::kSwapElements, 3);
5616}
5617
5618
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005619void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
5620 Comment cmnt(masm_, "[ GenerateSwapElements");
5621
5622 ASSERT_EQ(3, args->length());
5623
5624 Load(args->at(0));
5625 Load(args->at(1));
5626 Load(args->at(2));
5627
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005628 VirtualFrame::SpilledScope spilled_scope(frame_);
5629
ager@chromium.orgac091b72010-05-05 07:34:42 +00005630 Register index2 = r2;
5631 Register index1 = r1;
5632 Register object = r0;
5633 Register tmp1 = r3;
5634 Register tmp2 = r4;
5635
5636 frame_->EmitPop(index2);
5637 frame_->EmitPop(index1);
5638 frame_->EmitPop(object);
5639
5640 DeferredSwapElements* deferred =
5641 new DeferredSwapElements(object, index1, index2);
5642
5643 // Fetch the map and check if array is in fast case.
5644 // Check that object doesn't require security checks and
5645 // has no indexed interceptor.
5646 __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE);
5647 deferred->Branch(lt);
5648 __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset));
5649 __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
5650 deferred->Branch(nz);
5651
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00005652 // Check the object's elements are in fast case and writable.
ager@chromium.orgac091b72010-05-05 07:34:42 +00005653 __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset));
5654 __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset));
5655 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5656 __ cmp(tmp2, ip);
5657 deferred->Branch(ne);
5658
5659 // Smi-tagging is equivalent to multiplying by 2.
5660 STATIC_ASSERT(kSmiTag == 0);
5661 STATIC_ASSERT(kSmiTagSize == 1);
5662
5663 // Check that both indices are smis.
5664 __ mov(tmp2, index1);
5665 __ orr(tmp2, tmp2, index2);
5666 __ tst(tmp2, Operand(kSmiTagMask));
5667 deferred->Branch(nz);
5668
5669 // Bring the offsets into the fixed array in tmp1 into index1 and
5670 // index2.
5671 __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5672 __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize));
5673 __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize));
5674
5675 // Swap elements.
5676 Register tmp3 = object;
5677 object = no_reg;
5678 __ ldr(tmp3, MemOperand(tmp1, index1));
5679 __ ldr(tmp2, MemOperand(tmp1, index2));
5680 __ str(tmp3, MemOperand(tmp1, index2));
5681 __ str(tmp2, MemOperand(tmp1, index1));
5682
5683 Label done;
5684 __ InNewSpace(tmp1, tmp2, eq, &done);
5685 // Possible optimization: do a check that both values are Smis
5686 // (or them and test against Smi mask.)
5687
5688 __ mov(tmp2, tmp1);
5689 RecordWriteStub recordWrite1(tmp1, index1, tmp3);
5690 __ CallStub(&recordWrite1);
5691
5692 RecordWriteStub recordWrite2(tmp2, index2, tmp3);
5693 __ CallStub(&recordWrite2);
5694
5695 __ bind(&done);
5696
5697 deferred->BindExit();
5698 __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex);
5699 frame_->EmitPush(tmp1);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005700}
5701
5702
ager@chromium.org357bf652010-04-12 11:30:10 +00005703void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
5704 Comment cmnt(masm_, "[ GenerateCallFunction");
5705
5706 ASSERT(args->length() >= 2);
5707
5708 int n_args = args->length() - 2; // for receiver and function.
5709 Load(args->at(0)); // receiver
5710 for (int i = 0; i < n_args; i++) {
5711 Load(args->at(i + 1));
5712 }
5713 Load(args->at(n_args + 1)); // function
5714 frame_->CallJSFunction(n_args);
5715 frame_->EmitPush(r0);
5716}
5717
5718
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005719void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
5720 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005721 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005722 if (CpuFeatures::IsSupported(VFP3)) {
5723 TranscendentalCacheStub stub(TranscendentalCache::SIN);
5724 frame_->SpillAllButCopyTOSToR0();
5725 frame_->CallStub(&stub, 1);
5726 } else {
5727 frame_->CallRuntime(Runtime::kMath_sin, 1);
5728 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005729 frame_->EmitPush(r0);
5730}
5731
5732
5733void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
5734 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005735 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005736 if (CpuFeatures::IsSupported(VFP3)) {
5737 TranscendentalCacheStub stub(TranscendentalCache::COS);
5738 frame_->SpillAllButCopyTOSToR0();
5739 frame_->CallStub(&stub, 1);
5740 } else {
5741 frame_->CallRuntime(Runtime::kMath_cos, 1);
5742 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005743 frame_->EmitPush(r0);
5744}
5745
5746
ager@chromium.org7c537e22008-10-16 08:43:32 +00005747void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005748 ASSERT(args->length() == 2);
5749
5750 // Load the two objects into registers and perform the comparison.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005751 Load(args->at(0));
5752 Load(args->at(1));
5753 Register lhs = frame_->PopToRegister();
5754 Register rhs = frame_->PopToRegister(lhs);
5755 __ cmp(lhs, rhs);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005756 cc_reg_ = eq;
5757}
5758
5759
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005760void CodeGenerator::GenerateIsRegExpEquivalent(ZoneList<Expression*>* args) {
5761 ASSERT(args->length() == 2);
5762
5763 // Load the two objects into registers and perform the comparison.
5764 Load(args->at(0));
5765 Load(args->at(1));
5766 Register right = frame_->PopToRegister();
5767 Register left = frame_->PopToRegister(right);
5768 Register tmp = frame_->scratch0();
5769 Register tmp2 = frame_->scratch1();
5770
5771 // Jumps to done must have the eq flag set if the test is successful
5772 // and clear if the test has failed.
5773 Label done;
5774
5775 // Fail if either is a non-HeapObject.
5776 __ cmp(left, Operand(right));
5777 __ b(eq, &done);
5778 __ and_(tmp, left, Operand(right));
5779 __ eor(tmp, tmp, Operand(kSmiTagMask));
5780 __ tst(tmp, Operand(kSmiTagMask));
5781 __ b(ne, &done);
5782 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
5783 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
5784 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
5785 __ b(ne, &done);
5786 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
5787 __ cmp(tmp, Operand(tmp2));
5788 __ b(ne, &done);
5789 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
5790 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
5791 __ cmp(tmp, tmp2);
5792 __ bind(&done);
5793 cc_reg_ = eq;
5794}
5795
5796
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00005797void CodeGenerator::GenerateHasCachedArrayIndex(ZoneList<Expression*>* args) {
5798 ASSERT(args->length() == 1);
5799 Load(args->at(0));
5800 Register value = frame_->PopToRegister();
5801 Register tmp = frame_->scratch0();
5802 __ ldr(tmp, FieldMemOperand(value, String::kHashFieldOffset));
5803 __ tst(tmp, Operand(String::kContainsCachedArrayIndexMask));
5804 cc_reg_ = eq;
5805}
5806
5807
5808void CodeGenerator::GenerateGetCachedArrayIndex(ZoneList<Expression*>* args) {
5809 ASSERT(args->length() == 1);
5810 Load(args->at(0));
5811 Register value = frame_->PopToRegister();
5812
5813 __ ldr(value, FieldMemOperand(value, String::kHashFieldOffset));
5814 __ IndexFromHash(value, value);
5815 frame_->EmitPush(value);
5816}
5817
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005818
ager@chromium.org7c537e22008-10-16 08:43:32 +00005819void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005820#ifdef DEBUG
5821 int original_height = frame_->height();
5822#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005823 if (CheckForInlineRuntimeCall(node)) {
5824 ASSERT((has_cc() && frame_->height() == original_height) ||
5825 (!has_cc() && frame_->height() == original_height + 1));
5826 return;
5827 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005828
5829 ZoneList<Expression*>* args = node->arguments();
5830 Comment cmnt(masm_, "[ CallRuntime");
5831 Runtime::Function* function = node->function();
5832
ager@chromium.org41826e72009-03-30 13:30:57 +00005833 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00005834 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00005835 // Push the builtins object found in the current global object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005836 Register scratch = VirtualFrame::scratch0();
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005837 __ ldr(scratch, GlobalObjectOperand());
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005838 Register builtins = frame_->GetTOSRegister();
5839 __ ldr(builtins, FieldMemOperand(scratch, GlobalObject::kBuiltinsOffset));
5840 frame_->EmitPush(builtins);
ager@chromium.org41826e72009-03-30 13:30:57 +00005841 }
mads.s.ager31e71382008-08-13 09:32:07 +00005842
ager@chromium.org41826e72009-03-30 13:30:57 +00005843 // Push the arguments ("left-to-right").
5844 int arg_count = args->length();
5845 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005846 Load(args->at(i));
ager@chromium.org41826e72009-03-30 13:30:57 +00005847 }
mads.s.ager31e71382008-08-13 09:32:07 +00005848
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005849 VirtualFrame::SpilledScope spilled_scope(frame_);
5850
ager@chromium.org41826e72009-03-30 13:30:57 +00005851 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005852 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00005853 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005854 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005855 Handle<Code> stub = StubCache::ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005856 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005857 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005858 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00005859 } else {
5860 // Call the C runtime function.
5861 frame_->CallRuntime(function, arg_count);
5862 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005863 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00005864 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005865}
5866
5867
ager@chromium.org7c537e22008-10-16 08:43:32 +00005868void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005869#ifdef DEBUG
5870 int original_height = frame_->height();
5871#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005872 Comment cmnt(masm_, "[ UnaryOperation");
5873
5874 Token::Value op = node->op();
5875
5876 if (op == Token::NOT) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005877 LoadCondition(node->expression(), false_target(), true_target(), true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005878 // LoadCondition may (and usually does) leave a test and branch to
5879 // be emitted by the caller. In that case, negate the condition.
5880 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005881
5882 } else if (op == Token::DELETE) {
5883 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00005884 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005885 if (property != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005886 Load(property->obj());
5887 Load(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005888 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005889 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005890
mads.s.ager31e71382008-08-13 09:32:07 +00005891 } else if (variable != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005892 Slot* slot = variable->AsSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005893 if (variable->is_global()) {
5894 LoadGlobal();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005895 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005896 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005897 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005898
5899 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
5900 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005901 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005902 frame_->EmitPush(Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005903 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005904 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005905 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005906 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005907 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005908 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005909
mads.s.ager31e71382008-08-13 09:32:07 +00005910 } else {
5911 // Default: Result of deleting non-global, not dynamically
5912 // introduced variables is false.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005913 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00005914 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005915
5916 } else {
5917 // Default: Result of deleting expressions is true.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005918 Load(node->expression()); // may have side-effects
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005919 frame_->Drop();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005920 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005921 }
5922
5923 } else if (op == Token::TYPEOF) {
5924 // Special case for loading the typeof expression; see comment on
5925 // LoadTypeofExpression().
5926 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005927 frame_->CallRuntime(Runtime::kTypeof, 1);
5928 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005929
5930 } else {
ricow@chromium.org65fae842010-08-25 15:26:24 +00005931 bool can_overwrite = node->expression()->ResultOverwriteAllowed();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005932 UnaryOverwriteMode overwrite =
5933 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
5934
5935 bool no_negative_zero = node->expression()->no_negative_zero();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005936 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005937 switch (op) {
5938 case Token::NOT:
5939 case Token::DELETE:
5940 case Token::TYPEOF:
5941 UNREACHABLE(); // handled above
5942 break;
5943
5944 case Token::SUB: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005945 frame_->PopToR0();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005946 GenericUnaryOpStub stub(
5947 Token::SUB,
5948 overwrite,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005949 NO_UNARY_FLAGS,
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005950 no_negative_zero ? kIgnoreNegativeZero : kStrictNegativeZero);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005951 frame_->CallStub(&stub, 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005952 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005953 break;
5954 }
5955
5956 case Token::BIT_NOT: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005957 Register tos = frame_->PopToRegister();
5958 JumpTarget not_smi_label;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005959 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005960 // Smi check.
5961 __ tst(tos, Operand(kSmiTagMask));
5962 not_smi_label.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005963
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005964 __ mvn(tos, Operand(tos));
5965 __ bic(tos, tos, Operand(kSmiTagMask)); // Bit-clear inverted smi-tag.
5966 frame_->EmitPush(tos);
5967 // The fast case is the first to jump to the continue label, so it gets
5968 // to decide the virtual frame layout.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005969 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005970
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005971 not_smi_label.Bind();
5972 frame_->SpillAll();
5973 __ Move(r0, tos);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005974 GenericUnaryOpStub stub(Token::BIT_NOT,
5975 overwrite,
5976 NO_UNARY_SMI_CODE_IN_STUB);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005977 frame_->CallStub(&stub, 0);
5978 frame_->EmitPush(r0);
5979
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005980 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005981 break;
5982 }
5983
5984 case Token::VOID:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005985 frame_->Drop();
5986 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005987 break;
5988
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005989 case Token::ADD: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005990 Register tos = frame_->Peek();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005991 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005992 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005993 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005994 continue_label.Branch(eq);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005995
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005996 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005997 frame_->EmitPush(r0);
5998
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005999 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006000 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00006001 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006002 default:
6003 UNREACHABLE();
6004 }
6005 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006006 ASSERT(!has_valid_frame() ||
6007 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006008 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006009}
6010
6011
ager@chromium.org7c537e22008-10-16 08:43:32 +00006012void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006013#ifdef DEBUG
6014 int original_height = frame_->height();
6015#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006016 Comment cmnt(masm_, "[ CountOperation");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006017 VirtualFrame::RegisterAllocationScope scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006018
6019 bool is_postfix = node->is_postfix();
6020 bool is_increment = node->op() == Token::INC;
6021
6022 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
6023 bool is_const = (var != NULL && var->mode() == Variable::CONST);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006024 bool is_slot = (var != NULL && var->mode() == Variable::VAR);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006025
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006026 if (!is_const && is_slot && type_info(var->AsSlot()).IsSmi()) {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006027 // The type info declares that this variable is always a Smi. That
6028 // means it is a Smi both before and after the increment/decrement.
6029 // Lets make use of that to make a very minimal count.
6030 Reference target(this, node->expression(), !is_const);
6031 ASSERT(!target.is_illegal());
6032 target.GetValue(); // Pushes the value.
6033 Register value = frame_->PopToRegister();
6034 if (is_postfix) frame_->EmitPush(value);
6035 if (is_increment) {
6036 __ add(value, value, Operand(Smi::FromInt(1)));
6037 } else {
6038 __ sub(value, value, Operand(Smi::FromInt(1)));
6039 }
6040 frame_->EmitPush(value);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006041 target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006042 if (is_postfix) frame_->Pop();
6043 ASSERT_EQ(original_height + 1, frame_->height());
6044 return;
6045 }
6046
6047 // If it's a postfix expression and its result is not ignored and the
6048 // reference is non-trivial, then push a placeholder on the stack now
6049 // to hold the result of the expression.
6050 bool placeholder_pushed = false;
6051 if (!is_slot && is_postfix) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006052 frame_->EmitPush(Operand(Smi::FromInt(0)));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006053 placeholder_pushed = true;
mads.s.ager31e71382008-08-13 09:32:07 +00006054 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006055
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006056 // A constant reference is not saved to, so a constant reference is not a
6057 // compound assignment reference.
6058 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006059 if (target.is_illegal()) {
6060 // Spoof the virtual frame to have the expected height (one higher
6061 // than on entry).
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006062 if (!placeholder_pushed) frame_->EmitPush(Operand(Smi::FromInt(0)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00006063 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006064 return;
6065 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006066
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006067 // This pushes 0, 1 or 2 words on the object to be used later when updating
6068 // the target. It also pushes the current value of the target.
ager@chromium.org357bf652010-04-12 11:30:10 +00006069 target.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006070
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006071 JumpTarget slow;
6072 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006073
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006074 Register value = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006075
6076 // Postfix: Store the old value as the result.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006077 if (placeholder_pushed) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006078 frame_->SetElementAt(value, target.size());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006079 } else if (is_postfix) {
6080 frame_->EmitPush(value);
6081 __ mov(VirtualFrame::scratch0(), value);
6082 value = VirtualFrame::scratch0();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006083 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006084
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006085 // Check for smi operand.
6086 __ tst(value, Operand(kSmiTagMask));
6087 slow.Branch(ne);
6088
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006089 // Perform optimistic increment/decrement.
6090 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006091 __ add(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006092 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006093 __ sub(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006094 }
6095
6096 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006097 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006098
6099 // Revert optimistic increment/decrement.
6100 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006101 __ sub(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006102 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006103 __ add(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006104 }
6105
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006106 // Slow case: Convert to number. At this point the
6107 // value to be incremented is in the value register..
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006108 slow.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006109
6110 // Convert the operand to a number.
6111 frame_->EmitPush(value);
6112
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00006113 {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006114 VirtualFrame::SpilledScope spilled(frame_);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006115 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006116
6117 if (is_postfix) {
6118 // Postfix: store to result (on the stack).
6119 __ str(r0, frame_->ElementAt(target.size()));
6120 }
6121
6122 // Compute the new value.
6123 frame_->EmitPush(r0);
6124 frame_->EmitPush(Operand(Smi::FromInt(1)));
6125 if (is_increment) {
6126 frame_->CallRuntime(Runtime::kNumberAdd, 2);
6127 } else {
6128 frame_->CallRuntime(Runtime::kNumberSub, 2);
6129 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006130 }
6131
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006132 __ Move(value, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006133 // Store the new value in the target if not const.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006134 // At this point the answer is in the value register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006135 exit.Bind();
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.orgeadaf222009-06-16 09:43:10 +00006472 InstanceofStub stub;
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,
6525 Handle<String> name)
6526 : receiver_(receiver), name_(name) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006527 set_comment("[ DeferredReferenceGetNamedValue");
6528 }
6529
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006530 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006531
6532 private:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006533 Register receiver_;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006534 Handle<String> name_;
6535};
6536
6537
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006538// Convention for this is that on entry the receiver is in a register that
6539// is not used by the stack. On exit the answer is found in that same
6540// register and the stack has the same height.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006541void DeferredReferenceGetNamedValue::Generate() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006542#ifdef DEBUG
6543 int expected_height = frame_state()->frame()->height();
6544#endif
6545 VirtualFrame copied_frame(*frame_state()->frame());
6546 copied_frame.SpillAll();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006547
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006548 Register scratch1 = VirtualFrame::scratch0();
6549 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006550 ASSERT(!receiver_.is(scratch1) && !receiver_.is(scratch2));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006551 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
6552 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
6553
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006554 // Ensure receiver in r0 and name in r2 to match load ic calling convention.
6555 __ Move(r0, receiver_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006556 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006557
6558 // The rest of the instructions in the deferred code must be together.
6559 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6560 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
6561 __ Call(ic, RelocInfo::CODE_TARGET);
6562 // The call must be followed by a nop(1) instruction to indicate that the
6563 // in-object has been inlined.
6564 __ nop(PROPERTY_ACCESS_INLINED);
6565
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006566 // At this point the answer is in r0. We move it to the expected register
6567 // if necessary.
6568 __ Move(receiver_, r0);
6569
6570 // Now go back to the frame that we entered with. This will not overwrite
6571 // the receiver register since that register was not in use when we came
6572 // in. The instructions emitted by this merge are skipped over by the
6573 // inline load patching mechanism when looking for the branch instruction
6574 // that tells it where the code to patch is.
6575 copied_frame.MergeTo(frame_state()->frame());
6576
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006577 // Block the constant pool for one more instruction after leaving this
6578 // constant pool block scope to include the branch instruction ending the
6579 // deferred code.
6580 __ BlockConstPoolFor(1);
6581 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006582 ASSERT_EQ(expected_height, frame_state()->frame()->height());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006583}
6584
6585
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006586class DeferredReferenceGetKeyedValue: public DeferredCode {
6587 public:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006588 DeferredReferenceGetKeyedValue(Register key, Register receiver)
6589 : key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006590 set_comment("[ DeferredReferenceGetKeyedValue");
6591 }
6592
6593 virtual void Generate();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006594
6595 private:
6596 Register key_;
6597 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006598};
6599
6600
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006601// Takes key and register in r0 and r1 or vice versa. Returns result
6602// in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006603void DeferredReferenceGetKeyedValue::Generate() {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006604 ASSERT((key_.is(r0) && receiver_.is(r1)) ||
6605 (key_.is(r1) && receiver_.is(r0)));
6606
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006607 VirtualFrame copied_frame(*frame_state()->frame());
6608 copied_frame.SpillAll();
6609
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006610 Register scratch1 = VirtualFrame::scratch0();
6611 Register scratch2 = VirtualFrame::scratch1();
6612 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
6613 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
6614
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006615 // Ensure key in r0 and receiver in r1 to match keyed load ic calling
6616 // convention.
6617 if (key_.is(r1)) {
6618 __ Swap(r0, r1, ip);
6619 }
6620
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006621 // The rest of the instructions in the deferred code must be together.
6622 { Assembler::BlockConstPoolScope block_const_pool(masm_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006623 // Call keyed load IC. It has the arguments key and receiver in r0 and r1.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006624 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
6625 __ Call(ic, RelocInfo::CODE_TARGET);
6626 // The call must be followed by a nop instruction to indicate that the
6627 // keyed load has been inlined.
6628 __ nop(PROPERTY_ACCESS_INLINED);
6629
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006630 // Now go back to the frame that we entered with. This will not overwrite
6631 // the receiver or key registers since they were not in use when we came
6632 // in. The instructions emitted by this merge are skipped over by the
6633 // inline load patching mechanism when looking for the branch instruction
6634 // that tells it where the code to patch is.
6635 copied_frame.MergeTo(frame_state()->frame());
6636
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006637 // Block the constant pool for one more instruction after leaving this
6638 // constant pool block scope to include the branch instruction ending the
6639 // deferred code.
6640 __ BlockConstPoolFor(1);
6641 }
6642}
6643
6644
6645class DeferredReferenceSetKeyedValue: public DeferredCode {
6646 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006647 DeferredReferenceSetKeyedValue(Register value,
6648 Register key,
6649 Register receiver)
6650 : value_(value), key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006651 set_comment("[ DeferredReferenceSetKeyedValue");
6652 }
6653
6654 virtual void Generate();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006655
6656 private:
6657 Register value_;
6658 Register key_;
6659 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006660};
6661
6662
6663void DeferredReferenceSetKeyedValue::Generate() {
6664 Register scratch1 = VirtualFrame::scratch0();
6665 Register scratch2 = VirtualFrame::scratch1();
6666 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
6667 __ IncrementCounter(
6668 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
6669
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006670 // Ensure value in r0, key in r1 and receiver in r2 to match keyed store ic
6671 // calling convention.
6672 if (value_.is(r1)) {
6673 __ Swap(r0, r1, ip);
6674 }
6675 ASSERT(receiver_.is(r2));
6676
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006677 // The rest of the instructions in the deferred code must be together.
6678 { Assembler::BlockConstPoolScope block_const_pool(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006679 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6680 // r1 and r2.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006681 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
6682 __ Call(ic, RelocInfo::CODE_TARGET);
6683 // The call must be followed by a nop instruction to indicate that the
6684 // keyed store has been inlined.
6685 __ nop(PROPERTY_ACCESS_INLINED);
6686
6687 // Block the constant pool for one more instruction after leaving this
6688 // constant pool block scope to include the branch instruction ending the
6689 // deferred code.
6690 __ BlockConstPoolFor(1);
6691 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006692}
6693
6694
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006695class DeferredReferenceSetNamedValue: public DeferredCode {
6696 public:
6697 DeferredReferenceSetNamedValue(Register value,
6698 Register receiver,
6699 Handle<String> name)
6700 : value_(value), receiver_(receiver), name_(name) {
6701 set_comment("[ DeferredReferenceSetNamedValue");
6702 }
6703
6704 virtual void Generate();
6705
6706 private:
6707 Register value_;
6708 Register receiver_;
6709 Handle<String> name_;
6710};
6711
6712
6713// Takes value in r0, receiver in r1 and returns the result (the
6714// value) in r0.
6715void DeferredReferenceSetNamedValue::Generate() {
6716 // Record the entry frame and spill.
6717 VirtualFrame copied_frame(*frame_state()->frame());
6718 copied_frame.SpillAll();
6719
6720 // Ensure value in r0, receiver in r1 to match store ic calling
6721 // convention.
6722 ASSERT(value_.is(r0) && receiver_.is(r1));
6723 __ mov(r2, Operand(name_));
6724
6725 // The rest of the instructions in the deferred code must be together.
6726 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6727 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6728 // r1 and r2.
6729 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
6730 __ Call(ic, RelocInfo::CODE_TARGET);
6731 // The call must be followed by a nop instruction to indicate that the
6732 // named store has been inlined.
6733 __ nop(PROPERTY_ACCESS_INLINED);
6734
6735 // Go back to the frame we entered with. The instructions
6736 // generated by this merge are skipped over by the inline store
6737 // patching mechanism when looking for the branch instruction that
6738 // tells it where the code to patch is.
6739 copied_frame.MergeTo(frame_state()->frame());
6740
6741 // Block the constant pool for one more instruction after leaving this
6742 // constant pool block scope to include the branch instruction ending the
6743 // deferred code.
6744 __ BlockConstPoolFor(1);
6745 }
6746}
6747
6748
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006749// Consumes the top of stack (the receiver) and pushes the result instead.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006750void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
6751 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6752 Comment cmnt(masm(), "[ Load from named Property");
6753 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00006754 frame_->CallLoadIC(name,
6755 is_contextual
6756 ? RelocInfo::CODE_TARGET_CONTEXT
6757 : RelocInfo::CODE_TARGET);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006758 frame_->EmitPush(r0); // Push answer.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006759 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006760 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006761 Comment cmnt(masm(), "[ Inlined named property load");
6762
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006763 // Counter will be decremented in the deferred code. Placed here to avoid
6764 // having it in the instruction stream below where patching will occur.
6765 __ IncrementCounter(&Counters::named_load_inline, 1,
6766 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006767
6768 // The following instructions are the inlined load of an in-object property.
6769 // Parts of this code is patched, so the exact instructions generated needs
6770 // to be fixed. Therefore the instruction pool is blocked when generating
6771 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006772
6773 // Load the receiver from the stack.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006774 Register receiver = frame_->PopToRegister();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006775
6776 DeferredReferenceGetNamedValue* deferred =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006777 new DeferredReferenceGetNamedValue(receiver, name);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006778
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006779#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006780 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006781 Label check_inlined_codesize;
6782 masm_->bind(&check_inlined_codesize);
6783#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006784
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006785 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006786 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006787 __ tst(receiver, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006788 deferred->Branch(eq);
6789
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006790 Register scratch = VirtualFrame::scratch0();
6791 Register scratch2 = VirtualFrame::scratch1();
6792
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006793 // Check the map. The null map used below is patched by the inline cache
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006794 // code. Therefore we can't use a LoadRoot call.
6795 __ ldr(scratch, FieldMemOperand(receiver, HeapObject::kMapOffset));
6796 __ mov(scratch2, Operand(Factory::null_value()));
6797 __ cmp(scratch, scratch2);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006798 deferred->Branch(ne);
6799
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006800 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006801 // inline cache code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006802 __ ldr(receiver, MemOperand(receiver, 0));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006803
6804 // Make sure that the expected number of instructions are generated.
6805 ASSERT_EQ(kInlinedNamedLoadInstructions,
6806 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006807 }
6808
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006809 deferred->BindExit();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006810 // At this point the receiver register has the result, either from the
6811 // deferred code or from the inlined code.
6812 frame_->EmitPush(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006813 }
6814}
6815
6816
ager@chromium.orgac091b72010-05-05 07:34:42 +00006817void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) {
6818#ifdef DEBUG
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006819 int expected_height = frame()->height() - (is_contextual ? 1 : 2);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006820#endif
ager@chromium.orgac091b72010-05-05 07:34:42 +00006821
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006822 Result result;
6823 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6824 frame()->CallStoreIC(name, is_contextual);
6825 } else {
6826 // Inline the in-object property case.
6827 JumpTarget slow, done;
6828
6829 // Get the value and receiver from the stack.
6830 frame()->PopToR0();
6831 Register value = r0;
6832 frame()->PopToR1();
6833 Register receiver = r1;
6834
6835 DeferredReferenceSetNamedValue* deferred =
6836 new DeferredReferenceSetNamedValue(value, receiver, name);
6837
6838 // Check that the receiver is a heap object.
6839 __ tst(receiver, Operand(kSmiTagMask));
6840 deferred->Branch(eq);
6841
6842 // The following instructions are the part of the inlined
6843 // in-object property store code which can be patched. Therefore
6844 // the exact number of instructions generated must be fixed, so
6845 // the constant pool is blocked while generating this code.
6846 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6847 Register scratch0 = VirtualFrame::scratch0();
6848 Register scratch1 = VirtualFrame::scratch1();
6849
6850 // Check the map. Initially use an invalid map to force a
6851 // failure. The map check will be patched in the runtime system.
6852 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
6853
6854#ifdef DEBUG
6855 Label check_inlined_codesize;
6856 masm_->bind(&check_inlined_codesize);
6857#endif
6858 __ mov(scratch0, Operand(Factory::null_value()));
6859 __ cmp(scratch0, scratch1);
6860 deferred->Branch(ne);
6861
6862 int offset = 0;
6863 __ str(value, MemOperand(receiver, offset));
6864
6865 // Update the write barrier and record its size. We do not use
6866 // the RecordWrite macro here because we want the offset
6867 // addition instruction first to make it easy to patch.
6868 Label record_write_start, record_write_done;
6869 __ bind(&record_write_start);
6870 // Add offset into the object.
6871 __ add(scratch0, receiver, Operand(offset));
6872 // Test that the object is not in the new space. We cannot set
6873 // region marks for new space pages.
6874 __ InNewSpace(receiver, scratch1, eq, &record_write_done);
6875 // Record the actual write.
6876 __ RecordWriteHelper(receiver, scratch0, scratch1);
6877 __ bind(&record_write_done);
6878 // Clobber all input registers when running with the debug-code flag
6879 // turned on to provoke errors.
6880 if (FLAG_debug_code) {
6881 __ mov(receiver, Operand(BitCast<int32_t>(kZapValue)));
6882 __ mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
6883 __ mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
6884 }
6885 // Check that this is the first inlined write barrier or that
6886 // this inlined write barrier has the same size as all the other
6887 // inlined write barriers.
6888 ASSERT((inlined_write_barrier_size_ == -1) ||
6889 (inlined_write_barrier_size_ ==
6890 masm()->InstructionsGeneratedSince(&record_write_start)));
6891 inlined_write_barrier_size_ =
6892 masm()->InstructionsGeneratedSince(&record_write_start);
6893
6894 // Make sure that the expected number of instructions are generated.
6895 ASSERT_EQ(GetInlinedNamedStoreInstructionsAfterPatch(),
6896 masm()->InstructionsGeneratedSince(&check_inlined_codesize));
6897 }
6898 deferred->BindExit();
6899 }
6900 ASSERT_EQ(expected_height, frame()->height());
ager@chromium.orgac091b72010-05-05 07:34:42 +00006901}
6902
6903
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006904void CodeGenerator::EmitKeyedLoad() {
6905 if (loop_nesting() == 0) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006906 Comment cmnt(masm_, "[ Load from keyed property");
6907 frame_->CallKeyedLoadIC();
6908 } else {
6909 // Inline the keyed load.
6910 Comment cmnt(masm_, "[ Inlined load from keyed property");
6911
6912 // Counter will be decremented in the deferred code. Placed here to avoid
6913 // having it in the instruction stream below where patching will occur.
6914 __ IncrementCounter(&Counters::keyed_load_inline, 1,
6915 frame_->scratch0(), frame_->scratch1());
6916
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006917 // Load the key and receiver from the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006918 bool key_is_known_smi = frame_->KnownSmiAt(0);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006919 Register key = frame_->PopToRegister();
6920 Register receiver = frame_->PopToRegister(key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006921
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006922 // The deferred code expects key and receiver in registers.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006923 DeferredReferenceGetKeyedValue* deferred =
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006924 new DeferredReferenceGetKeyedValue(key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006925
6926 // Check that the receiver is a heap object.
6927 __ tst(receiver, Operand(kSmiTagMask));
6928 deferred->Branch(eq);
6929
6930 // The following instructions are the part of the inlined load keyed
6931 // property code which can be patched. Therefore the exact number of
6932 // instructions generated need to be fixed, so the constant pool is blocked
6933 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006934 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6935 Register scratch1 = VirtualFrame::scratch0();
6936 Register scratch2 = VirtualFrame::scratch1();
6937 // Check the map. The null map used below is patched by the inline cache
6938 // code.
6939 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006940
6941 // Check that the key is a smi.
6942 if (!key_is_known_smi) {
6943 __ tst(key, Operand(kSmiTagMask));
6944 deferred->Branch(ne);
6945 }
6946
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006947#ifdef DEBUG
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006948 Label check_inlined_codesize;
6949 masm_->bind(&check_inlined_codesize);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006950#endif
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006951 __ mov(scratch2, Operand(Factory::null_value()));
6952 __ cmp(scratch1, scratch2);
6953 deferred->Branch(ne);
6954
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00006955 // Get the elements array from the receiver.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006956 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00006957 __ AssertFastElements(scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006958
6959 // Check that key is within bounds. Use unsigned comparison to handle
6960 // negative keys.
6961 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006962 __ cmp(scratch2, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006963 deferred->Branch(ls); // Unsigned less equal.
6964
6965 // Load and check that the result is not the hole (key is a smi).
6966 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
6967 __ add(scratch1,
6968 scratch1,
6969 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006970 __ ldr(scratch1,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006971 MemOperand(scratch1, key, LSL,
6972 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006973 __ cmp(scratch1, scratch2);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006974 deferred->Branch(eq);
6975
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006976 __ mov(r0, scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006977 // Make sure that the expected number of instructions are generated.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006978 ASSERT_EQ(GetInlinedKeyedLoadInstructionsAfterPatch(),
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006979 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6980 }
6981
6982 deferred->BindExit();
6983 }
6984}
6985
6986
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006987void CodeGenerator::EmitKeyedStore(StaticType* key_type,
6988 WriteBarrierCharacter wb_info) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006989 // Generate inlined version of the keyed store if the code is in a loop
6990 // and the key is likely to be a smi.
6991 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
6992 // Inline the keyed store.
6993 Comment cmnt(masm_, "[ Inlined store to keyed property");
6994
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006995 Register scratch1 = VirtualFrame::scratch0();
6996 Register scratch2 = VirtualFrame::scratch1();
6997 Register scratch3 = r3;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006998
6999 // Counter will be decremented in the deferred code. Placed here to avoid
7000 // having it in the instruction stream below where patching will occur.
7001 __ IncrementCounter(&Counters::keyed_store_inline, 1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007002 scratch1, scratch2);
7003
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007004
7005
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007006 // Load the value, key and receiver from the stack.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007007 bool value_is_harmless = frame_->KnownSmiAt(0);
7008 if (wb_info == NEVER_NEWSPACE) value_is_harmless = true;
7009 bool key_is_smi = frame_->KnownSmiAt(1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007010 Register value = frame_->PopToRegister();
7011 Register key = frame_->PopToRegister(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00007012 VirtualFrame::SpilledScope spilled(frame_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007013 Register receiver = r2;
7014 frame_->EmitPop(receiver);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007015
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007016#ifdef DEBUG
7017 bool we_remembered_the_write_barrier = value_is_harmless;
7018#endif
7019
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007020 // The deferred code expects value, key and receiver in registers.
7021 DeferredReferenceSetKeyedValue* deferred =
7022 new DeferredReferenceSetKeyedValue(value, key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007023
7024 // Check that the value is a smi. As this inlined code does not set the
7025 // write barrier it is only possible to store smi values.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007026 if (!value_is_harmless) {
7027 // If the value is not likely to be a Smi then let's test the fixed array
7028 // for new space instead. See below.
7029 if (wb_info == LIKELY_SMI) {
7030 __ tst(value, Operand(kSmiTagMask));
7031 deferred->Branch(ne);
7032#ifdef DEBUG
7033 we_remembered_the_write_barrier = true;
7034#endif
7035 }
7036 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007037
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007038 if (!key_is_smi) {
7039 // Check that the key is a smi.
7040 __ tst(key, Operand(kSmiTagMask));
7041 deferred->Branch(ne);
7042 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007043
7044 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007045 __ tst(receiver, Operand(kSmiTagMask));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007046 deferred->Branch(eq);
7047
7048 // Check that the receiver is a JSArray.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007049 __ CompareObjectType(receiver, scratch1, scratch1, JS_ARRAY_TYPE);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007050 deferred->Branch(ne);
7051
7052 // Check that the key is within bounds. Both the key and the length of
7053 // the JSArray are smis. Use unsigned comparison to handle negative keys.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007054 __ ldr(scratch1, FieldMemOperand(receiver, JSArray::kLengthOffset));
7055 __ cmp(scratch1, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007056 deferred->Branch(ls); // Unsigned less equal.
7057
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007058 // Get the elements array from the receiver.
7059 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
7060 if (!value_is_harmless && wb_info != LIKELY_SMI) {
7061 Label ok;
7062 __ and_(scratch2, scratch1, Operand(ExternalReference::new_space_mask()));
7063 __ cmp(scratch2, Operand(ExternalReference::new_space_start()));
7064 __ tst(value, Operand(kSmiTagMask), ne);
7065 deferred->Branch(ne);
7066#ifdef DEBUG
7067 we_remembered_the_write_barrier = true;
7068#endif
7069 }
7070 // Check that the elements array is not a dictionary.
7071 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007072 // The following instructions are the part of the inlined store keyed
7073 // property code which can be patched. Therefore the exact number of
7074 // instructions generated need to be fixed, so the constant pool is blocked
7075 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007076 { Assembler::BlockConstPoolScope block_const_pool(masm_);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007077#ifdef DEBUG
7078 Label check_inlined_codesize;
7079 masm_->bind(&check_inlined_codesize);
7080#endif
7081
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007082 // Read the fixed array map from the constant pool (not from the root
7083 // array) so that the value can be patched. When debugging, we patch this
7084 // comparison to always fail so that we will hit the IC call in the
7085 // deferred code which will allow the debugger to break for fast case
7086 // stores.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007087 __ mov(scratch3, Operand(Factory::fixed_array_map()));
7088 __ cmp(scratch2, scratch3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007089 deferred->Branch(ne);
7090
7091 // Store the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007092 __ add(scratch1, scratch1,
7093 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
7094 __ str(value,
7095 MemOperand(scratch1, key, LSL,
7096 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007097
7098 // Make sure that the expected number of instructions are generated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007099 ASSERT_EQ(kInlinedKeyedStoreInstructionsAfterPatch,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007100 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
7101 }
7102
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007103 ASSERT(we_remembered_the_write_barrier);
7104
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007105 deferred->BindExit();
7106 } else {
7107 frame()->CallKeyedStoreIC();
7108 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007109}
7110
7111
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00007112#ifdef DEBUG
7113bool CodeGenerator::HasValidEntryRegisters() { return true; }
7114#endif
7115
7116
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007117#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007118#define __ ACCESS_MASM(masm)
7119
ager@chromium.org7c537e22008-10-16 08:43:32 +00007120Handle<String> Reference::GetName() {
7121 ASSERT(type_ == NAMED);
7122 Property* property = expression_->AsProperty();
7123 if (property == NULL) {
7124 // Global variable reference treated as a named property reference.
7125 VariableProxy* proxy = expression_->AsVariableProxy();
7126 ASSERT(proxy->AsVariable() != NULL);
7127 ASSERT(proxy->AsVariable()->is_global());
7128 return proxy->name();
7129 } else {
7130 Literal* raw_name = property->key()->AsLiteral();
7131 ASSERT(raw_name != NULL);
7132 return Handle<String>(String::cast(*raw_name->handle()));
7133 }
7134}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007135
ager@chromium.org7c537e22008-10-16 08:43:32 +00007136
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007137void Reference::DupIfPersist() {
7138 if (persist_after_get_) {
7139 switch (type_) {
7140 case KEYED:
7141 cgen_->frame()->Dup2();
7142 break;
7143 case NAMED:
7144 cgen_->frame()->Dup();
7145 // Fall through.
7146 case UNLOADED:
7147 case ILLEGAL:
7148 case SLOT:
7149 // Do nothing.
7150 ;
7151 }
7152 } else {
7153 set_unloaded();
7154 }
7155}
7156
7157
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007158void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00007159 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00007160 ASSERT(!is_illegal());
7161 ASSERT(!cgen_->has_cc());
7162 MacroAssembler* masm = cgen_->masm();
7163 Property* property = expression_->AsProperty();
7164 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00007165 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00007166 }
7167
7168 switch (type_) {
7169 case SLOT: {
7170 Comment cmnt(masm, "[ Load from Slot");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00007171 Slot* slot = expression_->AsVariableProxy()->AsVariable()->AsSlot();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007172 ASSERT(slot != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007173 DupIfPersist();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007174 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007175 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007176 }
7177
ager@chromium.org7c537e22008-10-16 08:43:32 +00007178 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00007179 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007180 bool is_global = var != NULL;
7181 ASSERT(!is_global || var->is_global());
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007182 Handle<String> name = GetName();
7183 DupIfPersist();
7184 cgen_->EmitNamedLoad(name, is_global);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007185 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007186 }
7187
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007188 case KEYED: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007189 ASSERT(property != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007190 DupIfPersist();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007191 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007192 cgen_->frame()->EmitPush(r0);
7193 break;
7194 }
7195
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007196 default:
7197 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007198 }
7199}
7200
7201
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007202void Reference::SetValue(InitState init_state, WriteBarrierCharacter wb_info) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00007203 ASSERT(!is_illegal());
7204 ASSERT(!cgen_->has_cc());
7205 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00007206 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007207 Property* property = expression_->AsProperty();
7208 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00007209 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007210 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007211
ager@chromium.org7c537e22008-10-16 08:43:32 +00007212 switch (type_) {
7213 case SLOT: {
7214 Comment cmnt(masm, "[ Store to Slot");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00007215 Slot* slot = expression_->AsVariableProxy()->AsVariable()->AsSlot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007216 cgen_->StoreToSlot(slot, init_state);
ager@chromium.orgac091b72010-05-05 07:34:42 +00007217 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007218 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007219 }
7220
ager@chromium.org7c537e22008-10-16 08:43:32 +00007221 case NAMED: {
7222 Comment cmnt(masm, "[ Store to named Property");
ager@chromium.orgac091b72010-05-05 07:34:42 +00007223 cgen_->EmitNamedStore(GetName(), false);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00007224 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00007225 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007226 break;
7227 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007228
ager@chromium.org7c537e22008-10-16 08:43:32 +00007229 case KEYED: {
7230 Comment cmnt(masm, "[ Store to keyed Property");
7231 Property* property = expression_->AsProperty();
7232 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00007233 cgen_->CodeForSourcePosition(property->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007234 cgen_->EmitKeyedStore(property->key()->type(), wb_info);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00007235 frame->EmitPush(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007236 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007237 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007238 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00007239
7240 default:
7241 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007242 }
7243}
7244
7245
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007246const char* GenericBinaryOpStub::GetName() {
7247 if (name_ != NULL) return name_;
7248 const int len = 100;
7249 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
7250 if (name_ == NULL) return "OOM";
7251 const char* op_name = Token::Name(op_);
7252 const char* overwrite_name;
7253 switch (mode_) {
7254 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
7255 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
7256 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
7257 default: overwrite_name = "UnknownOverwrite"; break;
7258 }
7259
7260 OS::SNPrintF(Vector<char>(name_, len),
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007261 "GenericBinaryOpStub_%s_%s%s_%s",
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007262 op_name,
7263 overwrite_name,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007264 specialized_on_rhs_ ? "_ConstantRhs" : "",
7265 BinaryOpIC::GetName(runtime_operands_type_));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007266 return name_;
7267}
7268
7269
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007270#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007271
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007272} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007273
7274#endif // V8_TARGET_ARCH_ARM