blob: 08a8da0f738b48af7e71acba48fd21baee6ba717 [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"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000046#include "virtual-frame-inl.h"
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000047#include "virtual-frame-arm-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048
kasperl@chromium.org71affb52009-05-26 05:44:31 +000049namespace v8 {
50namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000052
ricow@chromium.org30ce4112010-05-31 10:38:25 +000053#define __ ACCESS_MASM(masm_)
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000054
ager@chromium.orge2902be2009-06-08 12:21:35 +000055// -------------------------------------------------------------------------
56// Platform-specific DeferredCode functions.
57
58void DeferredCode::SaveRegisters() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000059 // On ARM you either have a completely spilled frame or you
60 // handle it yourself, but at the moment there's no automation
61 // of registers and deferred code.
ager@chromium.orge2902be2009-06-08 12:21:35 +000062}
63
64
65void DeferredCode::RestoreRegisters() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000066}
67
68
69// -------------------------------------------------------------------------
70// Platform-specific RuntimeCallHelper functions.
71
72void VirtualFrameRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
73 frame_state_->frame()->AssertIsSpilled();
74}
75
76
77void VirtualFrameRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
78}
79
80
81void ICRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
82 masm->EnterInternalFrame();
83}
84
85
86void ICRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
87 masm->LeaveInternalFrame();
ager@chromium.orge2902be2009-06-08 12:21:35 +000088}
89
ager@chromium.org3bf7b912008-11-17 09:09:45 +000090
91// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000092// CodeGenState implementation.
93
ager@chromium.org7c537e22008-10-16 08:43:32 +000094CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000095 : owner_(owner),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +000096 previous_(owner->state()) {
97 owner->set_state(this);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000098}
99
100
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000101ConditionCodeGenState::ConditionCodeGenState(CodeGenerator* owner,
102 JumpTarget* true_target,
103 JumpTarget* false_target)
104 : CodeGenState(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000105 true_target_(true_target),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000106 false_target_(false_target) {
107 owner->set_state(this);
108}
109
110
111TypeInfoCodeGenState::TypeInfoCodeGenState(CodeGenerator* owner,
112 Slot* slot,
113 TypeInfo type_info)
114 : CodeGenState(owner),
115 slot_(slot) {
116 owner->set_state(this);
117 old_type_info_ = owner->set_type_info(slot, type_info);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000118}
119
120
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000121CodeGenState::~CodeGenState() {
122 ASSERT(owner_->state() == this);
123 owner_->set_state(previous_);
124}
125
126
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000127TypeInfoCodeGenState::~TypeInfoCodeGenState() {
128 owner()->set_type_info(slot_, old_type_info_);
129}
130
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000131// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000132// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000134int CodeGenerator::inlined_write_barrier_size_ = -1;
135
ager@chromium.org5c838252010-02-19 08:53:10 +0000136CodeGenerator::CodeGenerator(MacroAssembler* masm)
137 : deferred_(8),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000138 masm_(masm),
ager@chromium.org5c838252010-02-19 08:53:10 +0000139 info_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000140 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000141 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142 cc_reg_(al),
143 state_(NULL),
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000144 loop_nesting_(0),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000145 type_info_(NULL),
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000146 function_return_(JumpTarget::BIDIRECTIONAL),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000147 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148}
149
150
151// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000152// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000154// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155// cp: callee's context
156
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000157void CodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000158 // Record the position for debugging purposes.
ager@chromium.org5c838252010-02-19 08:53:10 +0000159 CodeForFunctionPosition(info->function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000160 Comment cmnt(masm_, "[ function compiled by virtual frame code generator");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161
162 // Initialize state.
ager@chromium.org5c838252010-02-19 08:53:10 +0000163 info_ = info;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000164
165 int slots = scope()->num_parameters() + scope()->num_stack_slots();
166 ScopedVector<TypeInfo> type_info_array(slots);
167 type_info_ = &type_info_array;
168
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000169 ASSERT(allocator_ == NULL);
170 RegisterAllocator register_allocator(this);
171 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000172 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000173 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000174 cc_reg_ = al;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000175
176 // Adjust for function-level loop nesting.
177 ASSERT_EQ(0, loop_nesting_);
178 loop_nesting_ = info->loop_nesting();
179
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000180 {
181 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000183 // Entry:
184 // Stack: receiver, arguments
185 // lr: return address
186 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000188 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000190 allocator_->Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000191
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192#ifdef DEBUG
193 if (strlen(FLAG_stop_at) > 0 &&
ager@chromium.org5c838252010-02-19 08:53:10 +0000194 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000195 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000196 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197 }
198#endif
199
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000200 frame_->Enter();
201 // tos: code slot
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000202
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000203 // Allocate space for locals and initialize them. This also checks
204 // for stack overflow.
205 frame_->AllocateStackSlots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000206
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000207 frame_->AssertIsSpilled();
208 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
209 if (heap_slots > 0) {
210 // Allocate local context.
211 // Get outer context and create a new context based on it.
212 __ ldr(r0, frame_->Function());
213 frame_->EmitPush(r0);
214 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
215 FastNewContextStub stub(heap_slots);
216 frame_->CallStub(&stub, 1);
217 } else {
218 frame_->CallRuntime(Runtime::kNewContext, 1);
219 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000220
221#ifdef DEBUG
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000222 JumpTarget verified_true;
223 __ cmp(r0, cp);
224 verified_true.Branch(eq);
225 __ stop("NewContext: r0 is expected to be the same as cp");
226 verified_true.Bind();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000227#endif
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000228 // Update context local.
229 __ str(cp, frame_->Context());
230 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000231
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000232 // TODO(1241774): Improve this code:
233 // 1) only needed if we have a context
234 // 2) no need to recompute context ptr every single time
235 // 3) don't copy parameter operand code from SlotOperand!
236 {
237 Comment cmnt2(masm_, "[ copy context parameters into .context");
238 // Note that iteration order is relevant here! If we have the same
239 // parameter twice (e.g., function (x, y, x)), and that parameter
240 // needs to be copied into the context, it must be the last argument
241 // passed to the parameter that needs to be copied. This is a rare
242 // case so we don't check for it, instead we rely on the copying
243 // order: such a parameter is copied repeatedly into the same
244 // context location and thus the last value is what is seen inside
245 // the function.
246 frame_->AssertIsSpilled();
247 for (int i = 0; i < scope()->num_parameters(); i++) {
248 Variable* par = scope()->parameter(i);
249 Slot* slot = par->slot();
250 if (slot != NULL && slot->type() == Slot::CONTEXT) {
251 ASSERT(!scope()->is_global_scope()); // No params in global scope.
252 __ ldr(r1, frame_->ParameterAt(i));
253 // Loads r2 with context; used below in RecordWrite.
254 __ str(r1, SlotOperand(slot, r2));
255 // Load the offset into r3.
256 int slot_offset =
257 FixedArray::kHeaderSize + slot->index() * kPointerSize;
258 __ RecordWrite(r2, Operand(slot_offset), r3, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000259 }
260 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000261 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000262
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000263 // Store the arguments object. This must happen after context
264 // initialization because the arguments object may be stored in
265 // the context.
266 if (ArgumentsMode() != NO_ARGUMENTS_ALLOCATION) {
267 StoreArgumentsObject(true);
268 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000269
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000270 // Initialize ThisFunction reference if present.
271 if (scope()->is_function_scope() && scope()->function() != NULL) {
272 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
273 StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000274 }
275
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000276 // Initialize the function return target after the locals are set
277 // up, because it needs the expected frame height from the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000278 function_return_.SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000279 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000281 // Generate code to 'execute' declarations and initialize functions
282 // (source elements). In case of an illegal redeclaration we need to
283 // handle that instead of processing the declarations.
ager@chromium.org5c838252010-02-19 08:53:10 +0000284 if (scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000286 scope()->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 } else {
288 Comment cmnt(masm_, "[ declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000289 ProcessDeclarations(scope()->declarations());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000290 // Bail out if a stack-overflow exception occurred when processing
291 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000292 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 }
294
mads.s.ager31e71382008-08-13 09:32:07 +0000295 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000296 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000297 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000298 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299
300 // Compile the body of the function in a vanilla state. Don't
301 // bother compiling all the code if the scope has an illegal
302 // redeclaration.
ager@chromium.org5c838252010-02-19 08:53:10 +0000303 if (!scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 Comment cmnt(masm_, "[ function body");
305#ifdef DEBUG
306 bool is_builtin = Bootstrapper::IsActive();
307 bool should_trace =
308 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000309 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000310 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000311 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000312 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313#endif
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000314 VisitStatements(info->function()->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 }
317
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000318 // Handle the return from the function.
319 if (has_valid_frame()) {
320 // If there is a valid frame, control flow can fall off the end of
321 // the body. In that case there is an implicit return statement.
322 ASSERT(!function_return_is_shadowed_);
323 frame_->PrepareForReturn();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000324 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000325 if (function_return_.is_bound()) {
326 function_return_.Jump();
327 } else {
328 function_return_.Bind();
329 GenerateReturnSequence();
330 }
331 } else if (function_return_.is_linked()) {
332 // If the return target has dangling jumps to it, then we have not
333 // yet generated the return sequence. This can happen when (a)
334 // control does not flow off the end of the body so we did not
335 // compile an artificial return statement just above, and (b) there
336 // are return statements in the body but (c) they are all shadowed.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000337 function_return_.Bind();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000338 GenerateReturnSequence();
mads.s.ager31e71382008-08-13 09:32:07 +0000339 }
340
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000341 // Adjust for function-level loop nesting.
342 ASSERT(loop_nesting_ == info->loop_nesting());
343 loop_nesting_ = 0;
344
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 ASSERT(!has_cc());
347 ASSERT(state_ == NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000348 ASSERT(loop_nesting() == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000349 ASSERT(!function_return_is_shadowed_);
350 function_return_.Unuse();
351 DeleteFrame();
352
353 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000354 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000355 ProcessDeferred();
356 }
357
358 allocator_ = NULL;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000359 type_info_ = NULL;
360}
361
362
363int CodeGenerator::NumberOfSlot(Slot* slot) {
364 if (slot == NULL) return kInvalidSlotNumber;
365 switch (slot->type()) {
366 case Slot::PARAMETER:
367 return slot->index();
368 case Slot::LOCAL:
369 return slot->index() + scope()->num_parameters();
370 default:
371 break;
372 }
373 return kInvalidSlotNumber;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374}
375
376
ager@chromium.org7c537e22008-10-16 08:43:32 +0000377MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
378 // Currently, this assertion will fail if we try to assign to
379 // a constant variable that is constant because it is read-only
380 // (such as the variable referring to a named function expression).
381 // We need to implement assignments to read-only variables.
382 // Ideally, we should do this during AST generation (by converting
383 // such assignments into expression statements); however, in general
384 // we may not be able to make the decision until past AST generation,
385 // that is when the entire program is known.
386 ASSERT(slot != NULL);
387 int index = slot->index();
388 switch (slot->type()) {
389 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000390 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000391
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000392 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000393 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000394
395 case Slot::CONTEXT: {
396 // Follow the context chain if necessary.
397 ASSERT(!tmp.is(cp)); // do not overwrite context register
398 Register context = cp;
399 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000400 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000401 // Load the closure.
402 // (All contexts, even 'with' contexts, have a closure,
403 // and it is the same for all contexts inside a function.
404 // There is no need to go to the function context first.)
405 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
406 // Load the function context (which is the incoming, outer context).
407 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
408 context = tmp;
409 }
410 // We may have a 'with' context now. Get the function context.
411 // (In fact this mov may never be the needed, since the scope analysis
412 // may not permit a direct context access in this case and thus we are
413 // always at a function context. However it is safe to dereference be-
414 // cause the function context of a function context is itself. Before
415 // deleting this mov we should try to create a counter-example first,
416 // though...)
417 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
418 return ContextOperand(tmp, index);
419 }
420
421 default:
422 UNREACHABLE();
423 return MemOperand(r0, 0);
424 }
425}
426
427
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000428MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
429 Slot* slot,
430 Register tmp,
431 Register tmp2,
432 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000433 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000434 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000435
ager@chromium.org381abbb2009-02-25 13:23:22 +0000436 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
437 if (s->num_heap_slots() > 0) {
438 if (s->calls_eval()) {
439 // Check that extension is NULL.
440 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
441 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000442 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000443 }
444 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
445 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
446 context = tmp;
447 }
448 }
449 // Check that last extension is NULL.
450 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
451 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000452 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000453 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000454 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000455}
456
457
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000458// Loads a value on TOS. If it is a boolean value, the result may have been
459// (partially) translated into branches, or it may have set the condition
460// code register. If force_cc is set, the value is forced to set the
461// condition code register and no value is pushed. If the condition code
462// register was set, has_cc() is true and cc_reg_ contains the condition to
463// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000464void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000465 JumpTarget* true_target,
466 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000467 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000468 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000469 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000471 { ConditionCodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000472 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000473
474 // If we hit a stack overflow, we may not have actually visited
475 // the expression. In that case, we ensure that we have a
476 // valid-looking frame state because we will continue to generate
477 // code as we unwind the C++ stack.
478 //
479 // It's possible to have both a stack overflow and a valid frame
480 // state (eg, a subexpression overflowed, visiting it returned
481 // with a dummied frame state, and visiting this expression
482 // returned with a normal-looking state).
483 if (HasStackOverflow() &&
484 has_valid_frame() &&
485 !has_cc() &&
486 frame_->height() == original_height) {
487 true_target->Jump();
488 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000489 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000490 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000491 // Convert the TOS value to a boolean in the condition code register.
492 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000493 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000494 ASSERT(!force_cc || !has_valid_frame() || has_cc());
495 ASSERT(!has_valid_frame() ||
496 (has_cc() && frame_->height() == original_height) ||
497 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498}
499
500
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000501void CodeGenerator::Load(Expression* expr) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000502 // We generally assume that we are not in a spilled scope for most
503 // of the code generator. A failure to ensure this caused issue 815
504 // and this assert is designed to catch similar issues.
505 frame_->AssertIsNotSpilled();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000506#ifdef DEBUG
507 int original_height = frame_->height();
508#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000509 JumpTarget true_target;
510 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000511 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512
513 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000514 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000515 JumpTarget loaded;
516 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000517 materialize_true.Branch(cc_reg_);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000518 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000519 loaded.Jump();
520 materialize_true.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000521 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000522 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 cc_reg_ = al;
524 }
525
526 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000527 // We have at least one condition value that has been "translated"
528 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000529 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000530 if (frame_ != NULL) {
531 loaded.Jump(); // Don't lose the current TOS.
532 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000534 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000536 true_target.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000537 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000539 // If both "true" and "false" need to be loaded jump across the code for
540 // "false".
541 if (both) {
542 loaded.Jump();
543 }
544 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000546 false_target.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000547 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000549 // A value is loaded on all paths reaching this point.
550 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000552 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 ASSERT(!has_cc());
ager@chromium.orgac091b72010-05-05 07:34:42 +0000554 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555}
556
557
ager@chromium.org7c537e22008-10-16 08:43:32 +0000558void CodeGenerator::LoadGlobal() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000559 Register reg = frame_->GetTOSRegister();
560 __ ldr(reg, GlobalObject());
561 frame_->EmitPush(reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562}
563
564
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000565void CodeGenerator::LoadGlobalReceiver(Register scratch) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000566 Register reg = frame_->GetTOSRegister();
567 __ ldr(reg, ContextOperand(cp, Context::GLOBAL_INDEX));
568 __ ldr(reg,
569 FieldMemOperand(reg, GlobalObject::kGlobalReceiverOffset));
570 frame_->EmitPush(reg);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000571}
572
573
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000574ArgumentsAllocationMode CodeGenerator::ArgumentsMode() {
575 if (scope()->arguments() == NULL) return NO_ARGUMENTS_ALLOCATION;
576 ASSERT(scope()->arguments_shadow() != NULL);
577 // We don't want to do lazy arguments allocation for functions that
578 // have heap-allocated contexts, because it interfers with the
579 // uninitialized const tracking in the context objects.
580 return (scope()->num_heap_slots() > 0)
581 ? EAGER_ARGUMENTS_ALLOCATION
582 : LAZY_ARGUMENTS_ALLOCATION;
583}
584
585
586void CodeGenerator::StoreArgumentsObject(bool initial) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000587 ArgumentsAllocationMode mode = ArgumentsMode();
588 ASSERT(mode != NO_ARGUMENTS_ALLOCATION);
589
590 Comment cmnt(masm_, "[ store arguments object");
591 if (mode == LAZY_ARGUMENTS_ALLOCATION && initial) {
592 // When using lazy arguments allocation, we store the hole value
593 // as a sentinel indicating that the arguments object hasn't been
594 // allocated yet.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000595 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000596 } else {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000597 frame_->SpillAll();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000598 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
599 __ ldr(r2, frame_->Function());
600 // The receiver is below the arguments, the return address, and the
601 // frame pointer on the stack.
602 const int kReceiverDisplacement = 2 + scope()->num_parameters();
603 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
604 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
605 frame_->Adjust(3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000606 __ Push(r2, r1, r0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000607 frame_->CallStub(&stub, 3);
608 frame_->EmitPush(r0);
609 }
610
611 Variable* arguments = scope()->arguments()->var();
612 Variable* shadow = scope()->arguments_shadow()->var();
613 ASSERT(arguments != NULL && arguments->slot() != NULL);
614 ASSERT(shadow != NULL && shadow->slot() != NULL);
615 JumpTarget done;
616 if (mode == LAZY_ARGUMENTS_ALLOCATION && !initial) {
617 // We have to skip storing into the arguments slot if it has
618 // already been written to. This can happen if the a function
619 // has a local variable named 'arguments'.
620 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000621 Register arguments = frame_->PopToRegister();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000622 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000623 __ cmp(arguments, ip);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000624 done.Branch(ne);
625 }
626 StoreToSlot(arguments->slot(), NOT_CONST_INIT);
627 if (mode == LAZY_ARGUMENTS_ALLOCATION) done.Bind();
628 StoreToSlot(shadow->slot(), NOT_CONST_INIT);
629}
630
631
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000632void CodeGenerator::LoadTypeofExpression(Expression* expr) {
633 // Special handling of identifiers as subexpressions of typeof.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000634 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000636 // For a global variable we build the property reference
637 // <global>.<variable> and perform a (regular non-contextual) property
638 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
640 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000641 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000642 Reference ref(this, &property);
ager@chromium.org357bf652010-04-12 11:30:10 +0000643 ref.GetValue();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000644 } else if (variable != NULL && variable->slot() != NULL) {
645 // For a variable that rewrites to a slot, we signal it is the immediate
646 // subexpression of a typeof.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000647 LoadFromSlotCheckForArguments(variable->slot(), INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000649 // Anything else can be handled normally.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000650 Load(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 }
652}
653
654
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000655Reference::Reference(CodeGenerator* cgen,
656 Expression* expression,
657 bool persist_after_get)
658 : cgen_(cgen),
659 expression_(expression),
660 type_(ILLEGAL),
661 persist_after_get_(persist_after_get) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000662 // We generally assume that we are not in a spilled scope for most
663 // of the code generator. A failure to ensure this caused issue 815
664 // and this assert is designed to catch similar issues.
665 cgen->frame()->AssertIsNotSpilled();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 cgen->LoadReference(this);
667}
668
669
670Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000671 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672}
673
674
ager@chromium.org7c537e22008-10-16 08:43:32 +0000675void CodeGenerator::LoadReference(Reference* ref) {
676 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677 Expression* e = ref->expression();
678 Property* property = e->AsProperty();
679 Variable* var = e->AsVariableProxy()->AsVariable();
680
681 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000682 // The expression is either a property or a variable proxy that rewrites
683 // to a property.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000684 Load(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000685 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686 ref->set_type(Reference::NAMED);
687 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000688 Load(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689 ref->set_type(Reference::KEYED);
690 }
691 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000692 // The expression is a variable proxy that does not rewrite to a
693 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695 LoadGlobal();
696 ref->set_type(Reference::NAMED);
697 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000698 ASSERT(var->slot() != NULL);
699 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700 }
701 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000702 // Anything else is a runtime error.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000703 Load(e);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000704 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705 }
706}
707
708
ager@chromium.org7c537e22008-10-16 08:43:32 +0000709void CodeGenerator::UnloadReference(Reference* ref) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710 int size = ref->size();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000711 ref->set_unloaded();
ager@chromium.org357bf652010-04-12 11:30:10 +0000712 if (size == 0) return;
713
714 // Pop a reference from the stack while preserving TOS.
715 VirtualFrame::RegisterAllocationScope scope(this);
716 Comment cmnt(masm_, "[ UnloadReference");
717 if (size > 0) {
718 Register tos = frame_->PopToRegister();
719 frame_->Drop(size);
720 frame_->EmitPush(tos);
721 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722}
723
724
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
726// register to a boolean in the condition code register. The code
727// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000728void CodeGenerator::ToBoolean(JumpTarget* true_target,
729 JumpTarget* false_target) {
mads.s.ager31e71382008-08-13 09:32:07 +0000730 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731 // Only the condition code should be set.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000732 bool known_smi = frame_->KnownSmiAt(0);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000733 Register tos = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000734
735 // Fast case checks
736
mads.s.ager31e71382008-08-13 09:32:07 +0000737 // Check if the value is 'false'.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000738 if (!known_smi) {
739 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
740 __ cmp(tos, ip);
741 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000743 // Check if the value is 'true'.
744 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
745 __ cmp(tos, ip);
746 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000748 // Check if the value is 'undefined'.
749 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
750 __ cmp(tos, ip);
751 false_target->Branch(eq);
752 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753
mads.s.ager31e71382008-08-13 09:32:07 +0000754 // Check if the value is a smi.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000755 __ cmp(tos, Operand(Smi::FromInt(0)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000757 if (!known_smi) {
758 false_target->Branch(eq);
759 __ tst(tos, Operand(kSmiTagMask));
760 true_target->Branch(eq);
761
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000762 // Slow case.
763 if (CpuFeatures::IsSupported(VFP3)) {
764 CpuFeatures::Scope scope(VFP3);
765 // Implements the slow case by using ToBooleanStub.
766 // The ToBooleanStub takes a single argument, and
767 // returns a non-zero value for true, or zero for false.
768 // Both the argument value and the return value use the
769 // register assigned to tos_
770 ToBooleanStub stub(tos);
771 frame_->CallStub(&stub, 0);
772 // Convert the result in "tos" to a condition code.
773 __ cmp(tos, Operand(0));
774 } else {
775 // Implements slow case by calling the runtime.
776 frame_->EmitPush(tos);
777 frame_->CallRuntime(Runtime::kToBool, 1);
778 // Convert the result (r0) to a condition code.
779 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
780 __ cmp(r0, ip);
781 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000782 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783
784 cc_reg_ = ne;
785}
786
787
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000788void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000789 OverwriteMode overwrite_mode,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000790 GenerateInlineSmi inline_smi,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000791 int constant_rhs) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000792 // top of virtual frame: y
793 // 2nd elt. on virtual frame : x
794 // result : top of virtual frame
795
796 // Stub is entered with a call: 'return address' is in lr.
797 switch (op) {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000798 case Token::ADD:
799 case Token::SUB:
800 if (inline_smi) {
801 JumpTarget done;
802 Register rhs = frame_->PopToRegister();
803 Register lhs = frame_->PopToRegister(rhs);
804 Register scratch = VirtualFrame::scratch0();
805 __ orr(scratch, rhs, Operand(lhs));
806 // Check they are both small and positive.
807 __ tst(scratch, Operand(kSmiTagMask | 0xc0000000));
808 ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000809 STATIC_ASSERT(kSmiTag == 0);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000810 if (op == Token::ADD) {
811 __ add(r0, lhs, Operand(rhs), LeaveCC, eq);
812 } else {
813 __ sub(r0, lhs, Operand(rhs), LeaveCC, eq);
814 }
815 done.Branch(eq);
816 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
817 frame_->SpillAll();
818 frame_->CallStub(&stub, 0);
819 done.Bind();
820 frame_->EmitPush(r0);
821 break;
822 } else {
823 // Fall through!
824 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000825 case Token::BIT_OR:
826 case Token::BIT_AND:
827 case Token::BIT_XOR:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000828 if (inline_smi) {
829 bool rhs_is_smi = frame_->KnownSmiAt(0);
830 bool lhs_is_smi = frame_->KnownSmiAt(1);
831 Register rhs = frame_->PopToRegister();
832 Register lhs = frame_->PopToRegister(rhs);
833 Register smi_test_reg;
834 Condition cond;
835 if (!rhs_is_smi || !lhs_is_smi) {
836 if (rhs_is_smi) {
837 smi_test_reg = lhs;
838 } else if (lhs_is_smi) {
839 smi_test_reg = rhs;
840 } else {
841 smi_test_reg = VirtualFrame::scratch0();
842 __ orr(smi_test_reg, rhs, Operand(lhs));
843 }
844 // Check they are both Smis.
845 __ tst(smi_test_reg, Operand(kSmiTagMask));
846 cond = eq;
847 } else {
848 cond = al;
849 }
850 ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now.
851 if (op == Token::BIT_OR) {
852 __ orr(r0, lhs, Operand(rhs), LeaveCC, cond);
853 } else if (op == Token::BIT_AND) {
854 __ and_(r0, lhs, Operand(rhs), LeaveCC, cond);
855 } else {
856 ASSERT(op == Token::BIT_XOR);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000857 STATIC_ASSERT(kSmiTag == 0);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000858 __ eor(r0, lhs, Operand(rhs), LeaveCC, cond);
859 }
860 if (cond != al) {
861 JumpTarget done;
862 done.Branch(cond);
863 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
864 frame_->SpillAll();
865 frame_->CallStub(&stub, 0);
866 done.Bind();
867 }
868 frame_->EmitPush(r0);
869 break;
870 } else {
871 // Fall through!
872 }
873 case Token::MUL:
874 case Token::DIV:
875 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000876 case Token::SHL:
877 case Token::SHR:
878 case Token::SAR: {
ager@chromium.org357bf652010-04-12 11:30:10 +0000879 Register rhs = frame_->PopToRegister();
880 Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000881 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
882 frame_->SpillAll();
883 frame_->CallStub(&stub, 0);
ager@chromium.org357bf652010-04-12 11:30:10 +0000884 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 break;
886 }
887
ager@chromium.org357bf652010-04-12 11:30:10 +0000888 case Token::COMMA: {
889 Register scratch = frame_->PopToRegister();
890 // Simply discard left value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000891 frame_->Drop();
ager@chromium.org357bf652010-04-12 11:30:10 +0000892 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 break;
ager@chromium.org357bf652010-04-12 11:30:10 +0000894 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895
896 default:
897 // Other cases should have been handled before this point.
898 UNREACHABLE();
899 break;
900 }
901}
902
903
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000904class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000905 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000906 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000907 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000908 bool reversed,
ager@chromium.org357bf652010-04-12 11:30:10 +0000909 OverwriteMode overwrite_mode,
910 Register tos)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000911 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000912 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000913 reversed_(reversed),
ager@chromium.org357bf652010-04-12 11:30:10 +0000914 overwrite_mode_(overwrite_mode),
915 tos_register_(tos) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000916 set_comment("[ DeferredInlinedSmiOperation");
917 }
918
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000919 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000920
921 private:
922 Token::Value op_;
923 int value_;
924 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000925 OverwriteMode overwrite_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000926 Register tos_register_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000927};
928
929
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000930
931// On entry the non-constant side of the binary operation is in tos_register_
932// and the constant smi side is nowhere. The tos_register_ is not used by the
933// virtual frame. On exit the answer is in the tos_register_ and the virtual
934// frame is unchanged.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000935void DeferredInlineSmiOperation::Generate() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000936 VirtualFrame copied_frame(*frame_state()->frame());
937 copied_frame.SpillAll();
938
ager@chromium.org357bf652010-04-12 11:30:10 +0000939 Register lhs = r1;
940 Register rhs = r0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000941 switch (op_) {
942 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000943 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000944 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000945 __ sub(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000946 __ mov(r1, Operand(Smi::FromInt(value_)));
947 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000948 __ sub(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000949 __ mov(r0, Operand(Smi::FromInt(value_)));
950 }
951 break;
952 }
953
954 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000955 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000956 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000957 __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000958 __ mov(r1, Operand(Smi::FromInt(value_)));
959 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000960 __ add(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000961 __ mov(r0, Operand(Smi::FromInt(value_)));
962 }
963 break;
964 }
965
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000966 // For these operations there is no optimistic operation that needs to be
967 // reverted.
968 case Token::MUL:
969 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000970 case Token::BIT_OR:
971 case Token::BIT_XOR:
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000972 case Token::BIT_AND:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000973 case Token::SHL:
974 case Token::SHR:
975 case Token::SAR: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000976 if (tos_register_.is(r1)) {
977 __ mov(r0, Operand(Smi::FromInt(value_)));
978 } else {
979 ASSERT(tos_register_.is(r0));
980 __ mov(r1, Operand(Smi::FromInt(value_)));
981 }
982 if (reversed_ == tos_register_.is(r1)) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000983 lhs = r0;
984 rhs = r1;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000985 }
986 break;
987 }
988
989 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000990 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000991 UNREACHABLE();
992 break;
993 }
994
ager@chromium.org357bf652010-04-12 11:30:10 +0000995 GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000996 __ CallStub(&stub);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000997
ager@chromium.org357bf652010-04-12 11:30:10 +0000998 // The generic stub returns its value in r0, but that's not
999 // necessarily what we want. We want whatever the inlined code
1000 // expected, which is that the answer is in the same register as
1001 // the operand was.
1002 __ Move(tos_register_, r0);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001003
1004 // The tos register was not in use for the virtual frame that we
1005 // came into this function with, so we can merge back to that frame
1006 // without trashing it.
1007 copied_frame.MergeTo(frame_state()->frame());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001008}
1009
1010
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001011static bool PopCountLessThanEqual2(unsigned int x) {
1012 x &= x - 1;
1013 return (x & (x - 1)) == 0;
1014}
1015
1016
1017// Returns the index of the lowest bit set.
1018static int BitPosition(unsigned x) {
1019 int bit_posn = 0;
1020 while ((x & 0xf) == 0) {
1021 bit_posn += 4;
1022 x >>= 4;
1023 }
1024 while ((x & 1) == 0) {
1025 bit_posn++;
1026 x >>= 1;
1027 }
1028 return bit_posn;
1029}
1030
1031
ricow@chromium.org65fae842010-08-25 15:26:24 +00001032// Can we multiply by x with max two shifts and an add.
1033// This answers yes to all integers from 2 to 10.
1034static bool IsEasyToMultiplyBy(int x) {
1035 if (x < 2) return false; // Avoid special cases.
1036 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
1037 if (IsPowerOf2(x)) return true; // Simple shift.
1038 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
1039 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
1040 return false;
1041}
1042
1043
1044// Can multiply by anything that IsEasyToMultiplyBy returns true for.
1045// Source and destination may be the same register. This routine does
1046// not set carry and overflow the way a mul instruction would.
1047static void InlineMultiplyByKnownInt(MacroAssembler* masm,
1048 Register source,
1049 Register destination,
1050 int known_int) {
1051 if (IsPowerOf2(known_int)) {
1052 masm->mov(destination, Operand(source, LSL, BitPosition(known_int)));
1053 } else if (PopCountLessThanEqual2(known_int)) {
1054 int first_bit = BitPosition(known_int);
1055 int second_bit = BitPosition(known_int ^ (1 << first_bit));
1056 masm->add(destination, source,
1057 Operand(source, LSL, second_bit - first_bit));
1058 if (first_bit != 0) {
1059 masm->mov(destination, Operand(destination, LSL, first_bit));
1060 }
1061 } else {
1062 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
1063 int the_bit = BitPosition(known_int + 1);
1064 masm->rsb(destination, source, Operand(source, LSL, the_bit));
1065 }
1066}
1067
1068
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001069void CodeGenerator::SmiOperation(Token::Value op,
1070 Handle<Object> value,
1071 bool reversed,
1072 OverwriteMode mode) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001073 int int_value = Smi::cast(*value)->value();
1074
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001075 bool both_sides_are_smi = frame_->KnownSmiAt(0);
1076
ager@chromium.org357bf652010-04-12 11:30:10 +00001077 bool something_to_inline;
1078 switch (op) {
1079 case Token::ADD:
1080 case Token::SUB:
1081 case Token::BIT_AND:
1082 case Token::BIT_OR:
1083 case Token::BIT_XOR: {
1084 something_to_inline = true;
1085 break;
1086 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001087 case Token::SHL: {
1088 something_to_inline = (both_sides_are_smi || !reversed);
1089 break;
1090 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001091 case Token::SHR:
1092 case Token::SAR: {
1093 if (reversed) {
1094 something_to_inline = false;
1095 } else {
1096 something_to_inline = true;
1097 }
1098 break;
1099 }
1100 case Token::MOD: {
1101 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1102 something_to_inline = false;
1103 } else {
1104 something_to_inline = true;
1105 }
1106 break;
1107 }
1108 case Token::MUL: {
1109 if (!IsEasyToMultiplyBy(int_value)) {
1110 something_to_inline = false;
1111 } else {
1112 something_to_inline = true;
1113 }
1114 break;
1115 }
1116 default: {
1117 something_to_inline = false;
1118 break;
1119 }
1120 }
1121
1122 if (!something_to_inline) {
1123 if (!reversed) {
1124 // Push the rhs onto the virtual frame by putting it in a TOS register.
1125 Register rhs = frame_->GetTOSRegister();
1126 __ mov(rhs, Operand(value));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001127 frame_->EmitPush(rhs, TypeInfo::Smi());
1128 GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, int_value);
ager@chromium.org357bf652010-04-12 11:30:10 +00001129 } else {
1130 // Pop the rhs, then push lhs and rhs in the right order. Only performs
1131 // at most one pop, the rest takes place in TOS registers.
1132 Register lhs = frame_->GetTOSRegister(); // Get reg for pushing.
1133 Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this.
1134 __ mov(lhs, Operand(value));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001135 frame_->EmitPush(lhs, TypeInfo::Smi());
1136 TypeInfo t = both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Unknown();
1137 frame_->EmitPush(rhs, t);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001138 GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI,
1139 GenericBinaryOpStub::kUnknownIntValue);
ager@chromium.org357bf652010-04-12 11:30:10 +00001140 }
1141 return;
1142 }
1143
1144 // We move the top of stack to a register (normally no move is invoved).
1145 Register tos = frame_->PopToRegister();
ager@chromium.org357bf652010-04-12 11:30:10 +00001146 switch (op) {
1147 case Token::ADD: {
1148 DeferredCode* deferred =
1149 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1150
1151 __ add(tos, tos, Operand(value), SetCC);
1152 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001153 if (!both_sides_are_smi) {
1154 __ tst(tos, Operand(kSmiTagMask));
1155 deferred->Branch(ne);
1156 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001157 deferred->BindExit();
1158 frame_->EmitPush(tos);
1159 break;
1160 }
1161
1162 case Token::SUB: {
1163 DeferredCode* deferred =
1164 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1165
1166 if (reversed) {
1167 __ rsb(tos, tos, Operand(value), SetCC);
1168 } else {
1169 __ sub(tos, tos, Operand(value), SetCC);
1170 }
1171 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001172 if (!both_sides_are_smi) {
1173 __ tst(tos, Operand(kSmiTagMask));
1174 deferred->Branch(ne);
1175 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001176 deferred->BindExit();
1177 frame_->EmitPush(tos);
1178 break;
1179 }
1180
1181
1182 case Token::BIT_OR:
1183 case Token::BIT_XOR:
1184 case Token::BIT_AND: {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001185 if (both_sides_are_smi) {
1186 switch (op) {
1187 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1188 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001189 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001190 default: UNREACHABLE();
1191 }
1192 frame_->EmitPush(tos, TypeInfo::Smi());
1193 } else {
1194 DeferredCode* deferred =
1195 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1196 __ tst(tos, Operand(kSmiTagMask));
1197 deferred->Branch(ne);
1198 switch (op) {
1199 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1200 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001201 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001202 default: UNREACHABLE();
1203 }
1204 deferred->BindExit();
1205 TypeInfo result_type =
1206 (op == Token::BIT_AND) ? TypeInfo::Smi() : TypeInfo::Integer32();
1207 frame_->EmitPush(tos, result_type);
ager@chromium.org357bf652010-04-12 11:30:10 +00001208 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001209 break;
1210 }
1211
1212 case Token::SHL:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001213 if (reversed) {
1214 ASSERT(both_sides_are_smi);
1215 int max_shift = 0;
1216 int max_result = int_value == 0 ? 1 : int_value;
1217 while (Smi::IsValid(max_result << 1)) {
1218 max_shift++;
1219 max_result <<= 1;
1220 }
1221 DeferredCode* deferred =
1222 new DeferredInlineSmiOperation(op, int_value, true, mode, tos);
1223 // Mask off the last 5 bits of the shift operand (rhs). This is part
1224 // of the definition of shift in JS and we know we have a Smi so we
1225 // can safely do this. The masked version gets passed to the
1226 // deferred code, but that makes no difference.
1227 __ and_(tos, tos, Operand(Smi::FromInt(0x1f)));
1228 __ cmp(tos, Operand(Smi::FromInt(max_shift)));
1229 deferred->Branch(ge);
1230 Register scratch = VirtualFrame::scratch0();
1231 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Untag.
1232 __ mov(tos, Operand(Smi::FromInt(int_value))); // Load constant.
1233 __ mov(tos, Operand(tos, LSL, scratch)); // Shift constant.
1234 deferred->BindExit();
1235 TypeInfo result = TypeInfo::Integer32();
1236 frame_->EmitPush(tos, result);
1237 break;
1238 }
1239 // Fall through!
ager@chromium.org357bf652010-04-12 11:30:10 +00001240 case Token::SHR:
1241 case Token::SAR: {
1242 ASSERT(!reversed);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001243 int shift_amount = int_value & 0x1f;
1244 TypeInfo result = TypeInfo::Number();
1245
1246 if (op == Token::SHR) {
1247 if (shift_amount > 1) {
1248 result = TypeInfo::Smi();
1249 } else if (shift_amount > 0) {
1250 result = TypeInfo::Integer32();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001251 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001252 } else if (op == Token::SAR) {
1253 if (shift_amount > 0) {
1254 result = TypeInfo::Smi();
1255 } else {
1256 result = TypeInfo::Integer32();
1257 }
1258 } else {
1259 ASSERT(op == Token::SHL);
1260 result = TypeInfo::Integer32();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001261 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001262
ager@chromium.org357bf652010-04-12 11:30:10 +00001263 Register scratch = VirtualFrame::scratch0();
1264 Register scratch2 = VirtualFrame::scratch1();
1265 int shift_value = int_value & 0x1f; // least significant 5 bits
1266 DeferredCode* deferred =
1267 new DeferredInlineSmiOperation(op, shift_value, false, mode, tos);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001268 uint32_t problematic_mask = kSmiTagMask;
1269 // For unsigned shift by zero all negative smis are problematic.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001270 bool skip_smi_test = both_sides_are_smi;
1271 if (shift_value == 0 && op == Token::SHR) {
1272 problematic_mask |= 0x80000000;
1273 skip_smi_test = false;
1274 }
1275 if (!skip_smi_test) {
1276 __ tst(tos, Operand(problematic_mask));
1277 deferred->Branch(ne); // Go slow for problematic input.
1278 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001279 switch (op) {
1280 case Token::SHL: {
1281 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001282 int adjusted_shift = shift_value - kSmiTagSize;
1283 ASSERT(adjusted_shift >= 0);
1284 if (adjusted_shift != 0) {
1285 __ mov(scratch, Operand(tos, LSL, adjusted_shift));
1286 // Check that the *signed* result fits in a smi.
1287 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
1288 deferred->Branch(mi);
1289 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
1290 } else {
1291 // Check that the *signed* result fits in a smi.
1292 __ add(scratch2, tos, Operand(0x40000000), SetCC);
1293 deferred->Branch(mi);
1294 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
1295 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001296 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001297 break;
1298 }
1299 case Token::SHR: {
ager@chromium.org357bf652010-04-12 11:30:10 +00001300 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001301 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Remove tag.
1302 // LSR by immediate 0 means shifting 32 bits.
ager@chromium.org357bf652010-04-12 11:30:10 +00001303 __ mov(scratch, Operand(scratch, LSR, shift_value));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001304 if (shift_value == 1) {
1305 // check that the *unsigned* result fits in a smi
1306 // neither of the two high-order bits can be set:
1307 // - 0x80000000: high bit would be lost when smi tagging
1308 // - 0x40000000: this number would convert to negative when
1309 // smi tagging these two cases can only happen with shifts
1310 // by 0 or 1 when handed a valid smi
1311 __ tst(scratch, Operand(0xc0000000));
1312 deferred->Branch(ne);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001313 } else {
1314 ASSERT(shift_value >= 2);
1315 result = TypeInfo::Smi(); // SHR by at least 2 gives a Smi.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001316 }
1317 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
ager@chromium.org357bf652010-04-12 11:30:10 +00001318 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001319 break;
1320 }
1321 case Token::SAR: {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001322 // In the ARM instructions set, ASR by immediate 0 means shifting 32
1323 // bits.
ager@chromium.org357bf652010-04-12 11:30:10 +00001324 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001325 // Do the shift and the tag removal in one operation. If the shift
1326 // is 31 bits (the highest possible value) then we emit the
1327 // instruction as a shift by 0 which means shift arithmetically by
1328 // 32.
1329 __ mov(tos, Operand(tos, ASR, (kSmiTagSize + shift_value) & 0x1f));
1330 // Put tag back.
1331 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001332 // SAR by at least 1 gives a Smi.
1333 result = TypeInfo::Smi();
ager@chromium.org357bf652010-04-12 11:30:10 +00001334 }
1335 break;
1336 }
1337 default: UNREACHABLE();
1338 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001339 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001340 frame_->EmitPush(tos, result);
ager@chromium.org357bf652010-04-12 11:30:10 +00001341 break;
1342 }
1343
1344 case Token::MOD: {
1345 ASSERT(!reversed);
1346 ASSERT(int_value >= 2);
1347 ASSERT(IsPowerOf2(int_value));
1348 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001349 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001350 unsigned mask = (0x80000000u | kSmiTagMask);
1351 __ tst(tos, Operand(mask));
1352 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1353 mask = (int_value << kSmiTagSize) - 1;
1354 __ and_(tos, tos, Operand(mask));
1355 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001356 // Mod of positive power of 2 Smi gives a Smi if the lhs is an integer.
1357 frame_->EmitPush(
1358 tos,
1359 both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Number());
ager@chromium.org357bf652010-04-12 11:30:10 +00001360 break;
1361 }
1362
1363 case Token::MUL: {
1364 ASSERT(IsEasyToMultiplyBy(int_value));
1365 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001366 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001367 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1368 max_smi_that_wont_overflow <<= kSmiTagSize;
1369 unsigned mask = 0x80000000u;
1370 while ((mask & max_smi_that_wont_overflow) == 0) {
1371 mask |= mask >> 1;
1372 }
1373 mask |= kSmiTagMask;
1374 // This does a single mask that checks for a too high value in a
1375 // conservative way and for a non-Smi. It also filters out negative
1376 // numbers, unfortunately, but since this code is inline we prefer
1377 // brevity to comprehensiveness.
1378 __ tst(tos, Operand(mask));
1379 deferred->Branch(ne);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001380 InlineMultiplyByKnownInt(masm_, tos, tos, int_value);
ager@chromium.org357bf652010-04-12 11:30:10 +00001381 deferred->BindExit();
1382 frame_->EmitPush(tos);
1383 break;
1384 }
1385
1386 default:
1387 UNREACHABLE();
1388 break;
1389 }
1390}
1391
1392
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001393void CodeGenerator::Comparison(Condition cc,
1394 Expression* left,
1395 Expression* right,
1396 bool strict) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001397 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001398
ager@chromium.org357bf652010-04-12 11:30:10 +00001399 if (left != NULL) Load(left);
1400 if (right != NULL) Load(right);
1401
mads.s.ager31e71382008-08-13 09:32:07 +00001402 // sp[0] : y
1403 // sp[1] : x
1404 // result : cc register
1405
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406 // Strict only makes sense for equality comparisons.
1407 ASSERT(!strict || cc == eq);
1408
ager@chromium.org357bf652010-04-12 11:30:10 +00001409 Register lhs;
1410 Register rhs;
1411
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001412 bool lhs_is_smi;
1413 bool rhs_is_smi;
1414
ager@chromium.org357bf652010-04-12 11:30:10 +00001415 // We load the top two stack positions into registers chosen by the virtual
1416 // frame. This should keep the register shuffling to a minimum.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001417 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1418 if (cc == gt || cc == le) {
1419 cc = ReverseCondition(cc);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001420 lhs_is_smi = frame_->KnownSmiAt(0);
1421 rhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001422 lhs = frame_->PopToRegister();
1423 rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001424 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001425 rhs_is_smi = frame_->KnownSmiAt(0);
1426 lhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001427 rhs = frame_->PopToRegister();
1428 lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001429 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001430
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001431 bool both_sides_are_smi = (lhs_is_smi && rhs_is_smi);
1432
ager@chromium.org357bf652010-04-12 11:30:10 +00001433 ASSERT(rhs.is(r0) || rhs.is(r1));
1434 ASSERT(lhs.is(r0) || lhs.is(r1));
1435
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001436 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001438 if (!both_sides_are_smi) {
1439 // Now we have the two sides in r0 and r1. We flush any other registers
1440 // because the stub doesn't know about register allocation.
1441 frame_->SpillAll();
1442 Register scratch = VirtualFrame::scratch0();
1443 Register smi_test_reg;
1444 if (lhs_is_smi) {
1445 smi_test_reg = rhs;
1446 } else if (rhs_is_smi) {
1447 smi_test_reg = lhs;
1448 } else {
1449 __ orr(scratch, lhs, Operand(rhs));
1450 smi_test_reg = scratch;
1451 }
1452 __ tst(smi_test_reg, Operand(kSmiTagMask));
1453 JumpTarget smi;
1454 smi.Branch(eq);
1455
1456 // Perform non-smi comparison by stub.
1457 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1458 // We call with 0 args because there are 0 on the stack.
ager@chromium.orgb5737492010-07-15 09:29:43 +00001459 CompareStub stub(cc, strict, kBothCouldBeNaN, true, lhs, rhs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001460 frame_->CallStub(&stub, 0);
1461 __ cmp(r0, Operand(0));
1462 exit.Jump();
1463
1464 smi.Bind();
ager@chromium.org357bf652010-04-12 11:30:10 +00001465 }
1466
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001467 // Do smi comparisons by pointer comparison.
ager@chromium.org357bf652010-04-12 11:30:10 +00001468 __ cmp(lhs, Operand(rhs));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001470 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471 cc_reg_ = cc;
1472}
1473
1474
mads.s.ager31e71382008-08-13 09:32:07 +00001475// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001476void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001477 CallFunctionFlags flags,
1478 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001480 int arg_count = args->length();
1481 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001482 Load(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001483 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001484
kasper.lund7276f142008-07-30 08:49:36 +00001485 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001486 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001487
kasper.lund7276f142008-07-30 08:49:36 +00001488 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001489 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001490 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001491 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001492
1493 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001494 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001495 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496}
1497
1498
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001499void CodeGenerator::CallApplyLazy(Expression* applicand,
1500 Expression* receiver,
1501 VariableProxy* arguments,
1502 int position) {
1503 // An optimized implementation of expressions of the form
1504 // x.apply(y, arguments).
1505 // If the arguments object of the scope has not been allocated,
1506 // and x.apply is Function.prototype.apply, this optimization
1507 // just copies y and the arguments of the current function on the
1508 // stack, as receiver and arguments, and calls x.
1509 // In the implementation comments, we call x the applicand
1510 // and y the receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001511
1512 ASSERT(ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION);
1513 ASSERT(arguments->IsArguments());
1514
1515 // Load applicand.apply onto the stack. This will usually
1516 // give us a megamorphic load site. Not super, but it works.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001517 Load(applicand);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001518 Handle<String> name = Factory::LookupAsciiSymbol("apply");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001519 frame_->Dup();
ager@chromium.orgac091b72010-05-05 07:34:42 +00001520 frame_->CallLoadIC(name, RelocInfo::CODE_TARGET);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001521 frame_->EmitPush(r0);
1522
1523 // Load the receiver and the existing arguments object onto the
1524 // expression stack. Avoid allocating the arguments object here.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001525 Load(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001526 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
1527
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001528 // At this point the top two stack elements are probably in registers
1529 // since they were just loaded. Ensure they are in regs and get the
1530 // regs.
1531 Register receiver_reg = frame_->Peek2();
1532 Register arguments_reg = frame_->Peek();
1533
1534 // From now on the frame is spilled.
1535 frame_->SpillAll();
1536
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001537 // Emit the source position information after having loaded the
1538 // receiver and the arguments.
1539 CodeForSourcePosition(position);
1540 // Contents of the stack at this point:
1541 // sp[0]: arguments object of the current function or the hole.
1542 // sp[1]: receiver
1543 // sp[2]: applicand.apply
1544 // sp[3]: applicand.
1545
1546 // Check if the arguments object has been lazily allocated
1547 // already. If so, just use that instead of copying the arguments
1548 // from the stack. This also deals with cases where a local variable
1549 // named 'arguments' has been introduced.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001550 JumpTarget slow;
1551 Label done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001552 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001553 __ cmp(ip, arguments_reg);
1554 slow.Branch(ne);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001555
1556 Label build_args;
1557 // Get rid of the arguments object probe.
1558 frame_->Drop();
1559 // Stack now has 3 elements on it.
1560 // Contents of stack at this point:
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001561 // sp[0]: receiver - in the receiver_reg register.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001562 // sp[1]: applicand.apply
1563 // sp[2]: applicand.
1564
1565 // Check that the receiver really is a JavaScript object.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001566 __ BranchOnSmi(receiver_reg, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001567 // We allow all JSObjects including JSFunctions. As long as
1568 // JS_FUNCTION_TYPE is the last instance type and it is right
1569 // after LAST_JS_OBJECT_TYPE, we do not have to check the upper
1570 // bound.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00001571 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1572 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001573 __ CompareObjectType(receiver_reg, r2, r3, FIRST_JS_OBJECT_TYPE);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001574 __ b(lt, &build_args);
1575
1576 // Check that applicand.apply is Function.prototype.apply.
1577 __ ldr(r0, MemOperand(sp, kPointerSize));
1578 __ BranchOnSmi(r0, &build_args);
1579 __ CompareObjectType(r0, r1, r2, JS_FUNCTION_TYPE);
1580 __ b(ne, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001581 Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply));
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001582 __ ldr(r1, FieldMemOperand(r0, JSFunction::kCodeEntryOffset));
1583 __ sub(r1, r1, Operand(Code::kHeaderSize - kHeapObjectTag));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001584 __ cmp(r1, Operand(apply_code));
1585 __ b(ne, &build_args);
1586
1587 // Check that applicand is a function.
1588 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1589 __ BranchOnSmi(r1, &build_args);
1590 __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE);
1591 __ b(ne, &build_args);
1592
1593 // Copy the arguments to this function possibly from the
1594 // adaptor frame below it.
1595 Label invoke, adapted;
1596 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1597 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1598 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1599 __ b(eq, &adapted);
1600
1601 // No arguments adaptor frame. Copy fixed number of arguments.
1602 __ mov(r0, Operand(scope()->num_parameters()));
1603 for (int i = 0; i < scope()->num_parameters(); i++) {
1604 __ ldr(r2, frame_->ParameterAt(i));
1605 __ push(r2);
1606 }
1607 __ jmp(&invoke);
1608
1609 // Arguments adaptor frame present. Copy arguments from there, but
1610 // avoid copying too many arguments to avoid stack overflows.
1611 __ bind(&adapted);
1612 static const uint32_t kArgumentsLimit = 1 * KB;
1613 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1614 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
1615 __ mov(r3, r0);
1616 __ cmp(r0, Operand(kArgumentsLimit));
1617 __ b(gt, &build_args);
1618
1619 // Loop through the arguments pushing them onto the execution
1620 // stack. We don't inform the virtual frame of the push, so we don't
1621 // have to worry about getting rid of the elements from the virtual
1622 // frame.
1623 Label loop;
1624 // r3 is a small non-negative integer, due to the test above.
1625 __ cmp(r3, Operand(0));
1626 __ b(eq, &invoke);
1627 // Compute the address of the first argument.
1628 __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2));
1629 __ add(r2, r2, Operand(kPointerSize));
1630 __ bind(&loop);
1631 // Post-decrement argument address by kPointerSize on each iteration.
1632 __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex));
1633 __ push(r4);
1634 __ sub(r3, r3, Operand(1), SetCC);
1635 __ b(gt, &loop);
1636
1637 // Invoke the function.
1638 __ bind(&invoke);
1639 ParameterCount actual(r0);
1640 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1641 // Drop applicand.apply and applicand from the stack, and push
1642 // the result of the function call, but leave the spilled frame
1643 // unchanged, with 3 elements, so it is correct when we compile the
1644 // slow-case code.
1645 __ add(sp, sp, Operand(2 * kPointerSize));
1646 __ push(r0);
1647 // Stack now has 1 element:
1648 // sp[0]: result
1649 __ jmp(&done);
1650
1651 // Slow-case: Allocate the arguments object since we know it isn't
1652 // there, and fall-through to the slow-case where we call
1653 // applicand.apply.
1654 __ bind(&build_args);
1655 // Stack now has 3 elements, because we have jumped from where:
1656 // sp[0]: receiver
1657 // sp[1]: applicand.apply
1658 // sp[2]: applicand.
1659 StoreArgumentsObject(false);
1660
1661 // Stack and frame now have 4 elements.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001662 slow.Bind();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001663
1664 // Generic computation of x.apply(y, args) with no special optimization.
1665 // Flip applicand.apply and applicand on the stack, so
1666 // applicand looks like the receiver of the applicand.apply call.
1667 // Then process it as a normal function call.
1668 __ ldr(r0, MemOperand(sp, 3 * kPointerSize));
1669 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001670 __ Strd(r0, r1, MemOperand(sp, 2 * kPointerSize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001671
1672 CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS);
1673 frame_->CallStub(&call_function, 3);
1674 // The function and its two arguments have been dropped.
1675 frame_->Drop(); // Drop the receiver as well.
1676 frame_->EmitPush(r0);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001677 frame_->SpillAll(); // A spilled frame is also jumping to label done.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001678 // Stack now has 1 element:
1679 // sp[0]: result
1680 __ bind(&done);
1681
1682 // Restore the context register after a call.
1683 __ ldr(cp, frame_->Context());
1684}
1685
1686
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001687void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688 ASSERT(has_cc());
1689 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001690 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691 cc_reg_ = al;
1692}
1693
1694
ager@chromium.org7c537e22008-10-16 08:43:32 +00001695void CodeGenerator::CheckStack() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001696 frame_->SpillAll();
ager@chromium.org3811b432009-10-28 14:53:37 +00001697 Comment cmnt(masm_, "[ check stack");
1698 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1699 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1700 // the implicit 8 byte offset that always applies to operations with pc and
1701 // gives a return address 12 bytes down.
1702 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1703 masm_->cmp(sp, Operand(ip));
1704 StackCheckStub stub;
1705 // Call the stub if lower.
1706 masm_->mov(pc,
1707 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1708 RelocInfo::CODE_TARGET),
1709 LeaveCC,
1710 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711}
1712
1713
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001714void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1715#ifdef DEBUG
1716 int original_height = frame_->height();
1717#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001718 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001719 Visit(statements->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001720 }
1721 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1722}
1723
1724
ager@chromium.org7c537e22008-10-16 08:43:32 +00001725void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001726#ifdef DEBUG
1727 int original_height = frame_->height();
1728#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001730 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001731 node->break_target()->SetExpectedHeight();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001732 VisitStatements(node->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001733 if (node->break_target()->is_linked()) {
1734 node->break_target()->Bind();
1735 }
1736 node->break_target()->Unuse();
1737 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001738}
1739
1740
ager@chromium.org7c537e22008-10-16 08:43:32 +00001741void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org3811b432009-10-28 14:53:37 +00001742 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001743 frame_->EmitPush(Operand(pairs));
1744 frame_->EmitPush(Operand(Smi::FromInt(is_eval() ? 1 : 0)));
1745
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001746 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001747 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748}
1749
1750
ager@chromium.org7c537e22008-10-16 08:43:32 +00001751void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001752#ifdef DEBUG
1753 int original_height = frame_->height();
1754#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755 Comment cmnt(masm_, "[ Declaration");
1756 Variable* var = node->proxy()->var();
1757 ASSERT(var != NULL); // must have been resolved
1758 Slot* slot = var->slot();
1759
1760 // If it was not possible to allocate the variable at compile time,
1761 // we need to "declare" it at runtime to make sure it actually
1762 // exists in the local context.
1763 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1764 // Variables with a "LOOKUP" slot were introduced as non-locals
1765 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001766 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001768 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001769 frame_->EmitPush(Operand(var->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001770 // Declaration nodes are always declared in only two modes.
1771 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1772 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001773 frame_->EmitPush(Operand(Smi::FromInt(attr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774 // Push initial value, if any.
1775 // Note: For variables we must not push an initial value (such as
1776 // 'undefined') because we may have a (legal) redeclaration and we
1777 // must not destroy the current value.
1778 if (node->mode() == Variable::CONST) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001779 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780 } else if (node->fun() != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001781 Load(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001782 } else {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001783 frame_->EmitPush(Operand(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00001785
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001786 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001787 // Ignore the return value (declarations are statements).
ager@chromium.orgac091b72010-05-05 07:34:42 +00001788
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001789 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790 return;
1791 }
1792
1793 ASSERT(!var->is_global());
1794
1795 // If we have a function or a constant, we need to initialize the variable.
1796 Expression* val = NULL;
1797 if (node->mode() == Variable::CONST) {
1798 val = new Literal(Factory::the_hole_value());
1799 } else {
1800 val = node->fun(); // NULL if we don't have a function
1801 }
1802
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001803
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804 if (val != NULL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001805 WriteBarrierCharacter wb_info =
1806 val->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
1807 if (val->AsLiteral() != NULL) wb_info = NEVER_NEWSPACE;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001808 // Set initial value.
1809 Reference target(this, node->proxy());
1810 Load(val);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001811 target.SetValue(NOT_CONST_INIT, wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001812
iposva@chromium.org245aa852009-02-10 00:49:54 +00001813 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001814 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001816 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817}
1818
1819
ager@chromium.org7c537e22008-10-16 08:43:32 +00001820void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001821#ifdef DEBUG
1822 int original_height = frame_->height();
1823#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001825 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001826 Expression* expression = node->expression();
1827 expression->MarkAsStatement();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001828 Load(expression);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001829 frame_->Drop();
1830 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001831}
1832
1833
ager@chromium.org7c537e22008-10-16 08:43:32 +00001834void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001835#ifdef DEBUG
1836 int original_height = frame_->height();
1837#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001839 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001841 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842}
1843
1844
ager@chromium.org7c537e22008-10-16 08:43:32 +00001845void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001846#ifdef DEBUG
1847 int original_height = frame_->height();
1848#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001850 // Generate different code depending on which parts of the if statement
1851 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001852 bool has_then_stm = node->HasThenStatement();
1853 bool has_else_stm = node->HasElseStatement();
1854
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001855 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001857 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001859 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001860 JumpTarget then;
1861 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001863 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001864 if (frame_ != NULL) {
1865 Branch(false, &else_);
1866 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001868 if (frame_ != NULL || then.is_linked()) {
1869 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001870 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001871 }
1872 if (frame_ != NULL) {
1873 exit.Jump();
1874 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001876 if (else_.is_linked()) {
1877 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001878 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001879 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880
1881 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001882 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001883 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001884 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001885 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001886 LoadCondition(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001887 if (frame_ != NULL) {
1888 Branch(false, &exit);
1889 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001891 if (frame_ != NULL || then.is_linked()) {
1892 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001893 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001894 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895
1896 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001897 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001899 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900 // if (!cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001901 LoadCondition(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001902 if (frame_ != NULL) {
1903 Branch(true, &exit);
1904 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001906 if (frame_ != NULL || else_.is_linked()) {
1907 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001908 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001909 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910
1911 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001912 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 ASSERT(!has_then_stm && !has_else_stm);
1914 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001915 LoadCondition(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001916 if (frame_ != NULL) {
1917 if (has_cc()) {
1918 cc_reg_ = al;
1919 } else {
1920 frame_->Drop();
1921 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922 }
1923 }
1924
1925 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001926 if (exit.is_linked()) {
1927 exit.Bind();
1928 }
1929 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930}
1931
1932
ager@chromium.org7c537e22008-10-16 08:43:32 +00001933void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001935 CodeForStatementPosition(node);
1936 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001937}
1938
1939
ager@chromium.org7c537e22008-10-16 08:43:32 +00001940void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001942 CodeForStatementPosition(node);
1943 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944}
1945
1946
ager@chromium.org7c537e22008-10-16 08:43:32 +00001947void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001949
ager@chromium.org4af710e2009-09-15 12:20:11 +00001950 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001951 Load(node->expression());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001952 frame_->PopToR0();
1953 frame_->PrepareForReturn();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001954 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001955 function_return_.Jump();
1956 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001957 // Pop the result from the frame and prepare the frame for
1958 // returning thus making it easier to merge.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001959 if (function_return_.is_bound()) {
1960 // If the function return label is already bound we reuse the
1961 // code by jumping to the return site.
1962 function_return_.Jump();
1963 } else {
1964 function_return_.Bind();
1965 GenerateReturnSequence();
1966 }
1967 }
1968}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001969
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001970
1971void CodeGenerator::GenerateReturnSequence() {
1972 if (FLAG_trace) {
1973 // Push the return value on the stack as the parameter.
1974 // Runtime::TraceExit returns the parameter as it is.
1975 frame_->EmitPush(r0);
1976 frame_->CallRuntime(Runtime::kTraceExit, 1);
1977 }
1978
1979#ifdef DEBUG
1980 // Add a label for checking the size of the code used for returning.
1981 Label check_exit_codesize;
1982 masm_->bind(&check_exit_codesize);
1983#endif
1984 // Make sure that the constant pool is not emitted inside of the return
1985 // sequence.
1986 { Assembler::BlockConstPoolScope block_const_pool(masm_);
1987 // Tear down the frame which will restore the caller's frame pointer and
1988 // the link register.
1989 frame_->Exit();
1990
1991 // Here we use masm_-> instead of the __ macro to avoid the code coverage
1992 // tool from instrumenting as we rely on the code size here.
1993 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
1994 masm_->add(sp, sp, Operand(sp_delta));
1995 masm_->Jump(lr);
1996 DeleteFrame();
1997
1998#ifdef DEBUG
1999 // Check that the size of the code used for returning matches what is
2000 // expected by the debugger. If the sp_delts above cannot be encoded in
2001 // the add instruction the add will generate two instructions.
2002 int return_sequence_length =
2003 masm_->InstructionsGeneratedSince(&check_exit_codesize);
2004 CHECK(return_sequence_length ==
2005 Assembler::kJSReturnSequenceInstructions ||
2006 return_sequence_length ==
2007 Assembler::kJSReturnSequenceInstructions + 1);
2008#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002009 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010}
2011
2012
ager@chromium.org7c537e22008-10-16 08:43:32 +00002013void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* 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_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002018 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002019 Load(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002020 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002021 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002022 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002023 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002024 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002025#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002026 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002027 __ cmp(r0, cp);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002028 verified_true.Branch(eq);
2029 __ stop("PushContext: r0 is expected to be the same as cp");
2030 verified_true.Bind();
2031#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002033 __ str(cp, frame_->Context());
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::VisitWithExitStatement(WithExitStatement* 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_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002043 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044 // Pop context.
2045 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
2046 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002047 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002048 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049}
2050
2051
ager@chromium.org7c537e22008-10-16 08:43:32 +00002052void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002053#ifdef DEBUG
2054 int original_height = frame_->height();
2055#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002057 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002058 node->break_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002059
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002060 Load(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002061
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002062 JumpTarget next_test;
2063 JumpTarget fall_through;
2064 JumpTarget default_entry;
2065 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066 ZoneList<CaseClause*>* cases = node->cases();
2067 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002068 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069
2070 for (int i = 0; i < length; i++) {
2071 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002073 // Remember the default clause and compile it at the end.
2074 default_clause = clause;
2075 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 }
2077
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002078 Comment cmnt(masm_, "[ Case clause");
2079 // Compile the test.
2080 next_test.Bind();
2081 next_test.Unuse();
2082 // Duplicate TOS.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002083 frame_->Dup();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002084 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002085 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002086
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002087 // Before entering the body from the test, remove the switch value from
2088 // the stack.
2089 frame_->Drop();
2090
2091 // Label the body so that fall through is enabled.
2092 if (i > 0 && cases->at(i - 1)->is_default()) {
2093 default_exit.Bind();
2094 } else {
2095 fall_through.Bind();
2096 fall_through.Unuse();
2097 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002098 VisitStatements(clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002099
2100 // If control flow can fall through from the body, jump to the next body
2101 // or the end of the statement.
2102 if (frame_ != NULL) {
2103 if (i < length - 1 && cases->at(i + 1)->is_default()) {
2104 default_entry.Jump();
2105 } else {
2106 fall_through.Jump();
2107 }
2108 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 }
2110
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002111 // The final "test" removes the switch value.
2112 next_test.Bind();
2113 frame_->Drop();
2114
2115 // If there is a default clause, compile it.
2116 if (default_clause != NULL) {
2117 Comment cmnt(masm_, "[ Default clause");
2118 default_entry.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002119 VisitStatements(default_clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002120 // If control flow can fall out of the default and there is a case after
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002121 // it, jump to that case's body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002122 if (frame_ != NULL && default_exit.is_bound()) {
2123 default_exit.Jump();
2124 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002125 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002127 if (fall_through.is_linked()) {
2128 fall_through.Bind();
2129 }
2130
2131 if (node->break_target()->is_linked()) {
2132 node->break_target()->Bind();
2133 }
2134 node->break_target()->Unuse();
2135 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136}
2137
2138
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002139void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002140#ifdef DEBUG
2141 int original_height = frame_->height();
2142#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002143 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002144 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002145 node->break_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002146 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002147 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002149 // Label the top of the loop for the backward CFG edge. If the test
2150 // is always true we can use the continue target, and if the test is
2151 // always false there is no need.
2152 ConditionAnalysis info = AnalyzeCondition(node->cond());
2153 switch (info) {
2154 case ALWAYS_TRUE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002155 node->continue_target()->SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002156 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002157 break;
2158 case ALWAYS_FALSE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002159 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002160 break;
2161 case DONT_KNOW:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002162 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002163 body.Bind();
2164 break;
2165 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002166
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002167 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002168 Visit(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002169
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002170 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002171 switch (info) {
2172 case ALWAYS_TRUE:
2173 // If control can fall off the end of the body, jump back to the
2174 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002175 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002176 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002177 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002179 case ALWAYS_FALSE:
2180 // If we have a continue in the body, we only have to bind its
2181 // jump target.
2182 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002183 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002184 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002185 break;
2186 case DONT_KNOW:
2187 // We have to compile the test expression if it can be reached by
2188 // control flow falling out of the body or via continue.
2189 if (node->continue_target()->is_linked()) {
2190 node->continue_target()->Bind();
2191 }
2192 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002193 Comment cmnt(masm_, "[ DoWhileCondition");
2194 CodeForDoWhileConditionPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002195 LoadCondition(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002196 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002197 // A invalid frame here indicates that control did not
2198 // fall out of the test expression.
2199 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002200 }
2201 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002202 break;
2203 }
2204
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002205 if (node->break_target()->is_linked()) {
2206 node->break_target()->Bind();
2207 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002208 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002209 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2210}
2211
2212
2213void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
2214#ifdef DEBUG
2215 int original_height = frame_->height();
2216#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002217 Comment cmnt(masm_, "[ WhileStatement");
2218 CodeForStatementPosition(node);
2219
2220 // If the test is never true and has no side effects there is no need
2221 // to compile the test or body.
2222 ConditionAnalysis info = AnalyzeCondition(node->cond());
2223 if (info == ALWAYS_FALSE) return;
2224
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002225 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002226 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002227
2228 // Label the top of the loop with the continue target for the backward
2229 // CFG edge.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002230 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002231 node->continue_target()->Bind();
2232
2233 if (info == DONT_KNOW) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002234 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002235 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002236 if (has_valid_frame()) {
2237 // A NULL frame indicates that control did not fall out of the
2238 // test expression.
2239 Branch(false, node->break_target());
2240 }
2241 if (has_valid_frame() || body.is_linked()) {
2242 body.Bind();
2243 }
2244 }
2245
2246 if (has_valid_frame()) {
2247 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002248 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002249
2250 // If control flow can fall out of the body, jump back to the top.
2251 if (has_valid_frame()) {
2252 node->continue_target()->Jump();
2253 }
2254 }
2255 if (node->break_target()->is_linked()) {
2256 node->break_target()->Bind();
2257 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002258 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002259 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2260}
2261
2262
2263void CodeGenerator::VisitForStatement(ForStatement* node) {
2264#ifdef DEBUG
2265 int original_height = frame_->height();
2266#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002267 Comment cmnt(masm_, "[ ForStatement");
2268 CodeForStatementPosition(node);
2269 if (node->init() != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002270 Visit(node->init());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002271 }
2272
2273 // If the test is never true there is no need to compile the test or
2274 // body.
2275 ConditionAnalysis info = AnalyzeCondition(node->cond());
2276 if (info == ALWAYS_FALSE) return;
2277
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002278 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002279 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002280
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002281 // We know that the loop index is a smi if it is not modified in the
2282 // loop body and it is checked against a constant limit in the loop
2283 // condition. In this case, we reset the static type information of the
2284 // loop index to smi before compiling the body, the update expression, and
2285 // the bottom check of the loop condition.
2286 TypeInfoCodeGenState type_info_scope(this,
2287 node->is_fast_smi_loop() ?
2288 node->loop_variable()->slot() :
2289 NULL,
2290 TypeInfo::Smi());
2291
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002292 // If there is no update statement, label the top of the loop with the
2293 // continue target, otherwise with the loop target.
2294 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2295 if (node->next() == NULL) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002296 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002297 node->continue_target()->Bind();
2298 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002299 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002300 loop.Bind();
2301 }
2302
2303 // If the test is always true, there is no need to compile it.
2304 if (info == DONT_KNOW) {
2305 JumpTarget body;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002306 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002307 if (has_valid_frame()) {
2308 Branch(false, node->break_target());
2309 }
2310 if (has_valid_frame() || body.is_linked()) {
2311 body.Bind();
2312 }
2313 }
2314
2315 if (has_valid_frame()) {
2316 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002317 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002318
2319 if (node->next() == NULL) {
2320 // If there is no update statement and control flow can fall out
2321 // of the loop, jump directly to the continue label.
2322 if (has_valid_frame()) {
2323 node->continue_target()->Jump();
2324 }
2325 } else {
2326 // If there is an update statement and control flow can reach it
2327 // via falling out of the body of the loop or continuing, we
2328 // compile the update statement.
2329 if (node->continue_target()->is_linked()) {
2330 node->continue_target()->Bind();
2331 }
2332 if (has_valid_frame()) {
2333 // Record source position of the statement as this code which is
2334 // after the code for the body actually belongs to the loop
2335 // statement and not the body.
2336 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002337 Visit(node->next());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002338 loop.Jump();
2339 }
2340 }
2341 }
2342 if (node->break_target()->is_linked()) {
2343 node->break_target()->Bind();
2344 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002345 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002346 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002347}
2348
2349
ager@chromium.org7c537e22008-10-16 08:43:32 +00002350void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002351#ifdef DEBUG
2352 int original_height = frame_->height();
2353#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002354 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002355 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002356
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002357 JumpTarget primitive;
2358 JumpTarget jsobject;
2359 JumpTarget fixed_array;
2360 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2361 JumpTarget end_del_check;
2362 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002363
2364 // Get the object to enumerate over (converted to JSObject).
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002365 Load(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002367 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2369 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002370 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002371 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2372 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002373 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002374 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2375 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002376 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377
2378 // Stack layout in body:
2379 // [iteration counter (Smi)]
2380 // [length of array]
2381 // [FixedArray]
2382 // [Map or 0]
2383 // [Object]
2384
2385 // Check if enumerable is already a JSObject
2386 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002387 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002388 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002389 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002391 primitive.Bind();
2392 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002393 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002394
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002395 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002396 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002397 // r0: value to be iterated over
2398 frame_->EmitPush(r0); // Push the object being iterated over.
2399
2400 // Check cache validity in generated code. This is a fast case for
2401 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2402 // guarantee cache validity, call the runtime system to check cache
2403 // validity or get the property names in a fixed array.
2404 JumpTarget call_runtime;
2405 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2406 JumpTarget check_prototype;
2407 JumpTarget use_cache;
2408 __ mov(r1, Operand(r0));
2409 loop.Bind();
2410 // Check that there are no elements.
2411 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2412 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2413 __ cmp(r2, r4);
2414 call_runtime.Branch(ne);
2415 // Check that instance descriptors are not empty so that we can
2416 // check for an enum cache. Leave the map in r3 for the subsequent
2417 // prototype load.
2418 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2419 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2420 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2421 __ cmp(r2, ip);
2422 call_runtime.Branch(eq);
2423 // Check that there in an enum cache in the non-empty instance
2424 // descriptors. This is the case if the next enumeration index
2425 // field does not contain a smi.
2426 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2427 __ tst(r2, Operand(kSmiTagMask));
2428 call_runtime.Branch(eq);
2429 // For all objects but the receiver, check that the cache is empty.
2430 // r4: empty fixed array root.
2431 __ cmp(r1, r0);
2432 check_prototype.Branch(eq);
2433 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2434 __ cmp(r2, r4);
2435 call_runtime.Branch(ne);
2436 check_prototype.Bind();
2437 // Load the prototype from the map and loop if non-null.
2438 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2439 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2440 __ cmp(r1, ip);
2441 loop.Branch(ne);
2442 // The enum cache is valid. Load the map of the object being
2443 // iterated over and use the cache for the iteration.
2444 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2445 use_cache.Jump();
2446
2447 call_runtime.Bind();
2448 // Call the runtime to get the property names for the object.
2449 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002450 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002451
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002452 // If we got a map from the runtime call, we can do a fast
2453 // modification check. Otherwise, we got a fixed array, and we have
2454 // to do a slow check.
2455 // r0: map or fixed array (result from call to
2456 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002457 __ mov(r2, Operand(r0));
2458 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002459 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2460 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002461 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002462
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002463 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002464 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002465 // r0: map (either the result from a call to
2466 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2467 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468 __ mov(r1, Operand(r0));
2469 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2470 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2471 __ ldr(r2,
2472 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2473
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002474 frame_->EmitPush(r0); // map
2475 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002476 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002477 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002478 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002479 frame_->EmitPush(r0);
2480 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002481
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002482 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002483 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002484 frame_->EmitPush(r1); // insert 0 in place of Map
2485 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002486
2487 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002488 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002489 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002490 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002491 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492
2493 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002494 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002495 // sp[0] : index
2496 // sp[1] : array/enum cache length
2497 // sp[2] : array or enum cache
2498 // sp[3] : 0 or map
2499 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002500 // Grab the current frame's height for the break and continue
2501 // targets only after all the state is pushed on the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002502 node->break_target()->SetExpectedHeight();
2503 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002504
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002505 // Load the current count to r0, load the length to r1.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002506 __ Ldrd(r0, r1, frame_->ElementAt(0));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002507 __ cmp(r0, r1); // compare to the array length
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002508 node->break_target()->Branch(hs);
2509
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002510 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002511 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2513 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2514
2515 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002516 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002517 // Check if this (still) matches the map of the enumerable.
2518 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002519 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002520 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2521 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002522 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002523
2524 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002525 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2526 frame_->EmitPush(r0);
2527 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002528 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002529 __ mov(r3, Operand(r0), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002530 // If the property has been removed while iterating, we just skip it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002531 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002532
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002533 end_del_check.Bind();
2534 // Store the entry in the 'each' expression and take another spin in the
2535 // loop. r3: i'th entry of the enum cache (or string there of)
2536 frame_->EmitPush(r3); // push entry
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002537 { VirtualFrame::RegisterAllocationScope scope(this);
2538 Reference each(this, node->each());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002539 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002540 if (each.size() > 0) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002541 // Loading a reference may leave the frame in an unspilled state.
2542 frame_->SpillAll(); // Sync stack to memory.
2543 // Get the value (under the reference on the stack) from memory.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002544 __ ldr(r0, frame_->ElementAt(each.size()));
2545 frame_->EmitPush(r0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002546 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002547 frame_->Drop(2); // The result of the set and the extra pushed value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002548 } else {
2549 // If the reference was to a slot we rely on the convenient property
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002550 // that it doesn't matter whether a value (eg, ebx pushed above) is
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002551 // right on top of or right underneath a zero-sized reference.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002552 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002553 frame_->Drop(1); // Drop the result of the set operation.
mads.s.ager31e71382008-08-13 09:32:07 +00002554 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002555 }
2556 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002557 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002558 CheckStack(); // TODO(1222600): ignore if body contains calls.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002559 { VirtualFrame::RegisterAllocationScope scope(this);
2560 Visit(node->body());
2561 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002562
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002563 // Next. Reestablish a spilled frame in case we are coming here via
2564 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002565 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002566 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002567 frame_->EmitPop(r0);
2568 __ add(r0, r0, Operand(Smi::FromInt(1)));
2569 frame_->EmitPush(r0);
2570 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002572 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2573 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002574 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002575 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002576
2577 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002578 exit.Bind();
2579 node->continue_target()->Unuse();
2580 node->break_target()->Unuse();
2581 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582}
2583
2584
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002585void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002586#ifdef DEBUG
2587 int original_height = frame_->height();
2588#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002589 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002590 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002591 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002593 JumpTarget try_block;
2594 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002595
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002596 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002598 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002599
2600 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002601 Variable* catch_var = node->catch_var()->var();
2602 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2603 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002604
2605 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002606 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002607
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002608 { VirtualFrame::RegisterAllocationScope scope(this);
2609 VisitStatements(node->catch_block()->statements());
2610 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002611 if (frame_ != NULL) {
2612 exit.Jump();
2613 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002614
2615
2616 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002617 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002618
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002619 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2620 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002622 // Shadow the labels for all escapes from the try block, including
2623 // returns. During shadowing, the original label is hidden as the
2624 // LabelShadow and operations on the original actually affect the
2625 // shadowing label.
2626 //
2627 // We should probably try to unify the escaping labels and the return
2628 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002629 int nof_escapes = node->escaping_targets()->length();
2630 List<ShadowTarget*> shadows(1 + nof_escapes);
2631
2632 // Add the shadow target for the function return.
2633 static const int kReturnShadowIndex = 0;
2634 shadows.Add(new ShadowTarget(&function_return_));
2635 bool function_return_was_shadowed = function_return_is_shadowed_;
2636 function_return_is_shadowed_ = true;
2637 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2638
2639 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002641 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642 }
2643
2644 // Generate code for the statements in the try block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002645 { VirtualFrame::RegisterAllocationScope scope(this);
2646 VisitStatements(node->try_block()->statements());
2647 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648
2649 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002650 // After shadowing stops, the original labels are unshadowed and the
2651 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002652 bool has_unlinks = false;
2653 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002655 has_unlinks = has_unlinks || shadows[i]->is_linked();
2656 }
2657 function_return_is_shadowed_ = function_return_was_shadowed;
2658
2659 // Get an external reference to the handler address.
2660 ExternalReference handler_address(Top::k_handler_address);
2661
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002662 // If we can fall off the end of the try block, unlink from try chain.
2663 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002664 // The next handler address is on top of the frame. Unlink from
2665 // the handler list and drop the rest of this handler from the
2666 // frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002667 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002668 frame_->EmitPop(r1); // r0 can contain the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002669 __ mov(r3, Operand(handler_address));
2670 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002671 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002672 if (has_unlinks) {
2673 exit.Jump();
2674 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002675 }
2676
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002677 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002678 // jumped to. Deallocate each shadow target.
2679 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002680 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002681 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002682 shadows[i]->Bind();
2683 // Because we can be jumping here (to spilled code) from unspilled
2684 // code, we need to reestablish a spilled frame at this block.
2685 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002686
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 // Reload sp from the top handler, because some statements that we
2688 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002689 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002691 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002692
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002693 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002694 frame_->EmitPop(r1); // r0 can contain the return value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002696 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002697
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002698 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2699 frame_->PrepareForReturn();
2700 }
2701 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702 }
2703 }
2704
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002705 exit.Bind();
2706 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002707}
2708
2709
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002710void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002711#ifdef DEBUG
2712 int original_height = frame_->height();
2713#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002714 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002715 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002716 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002717
2718 // State: Used to keep track of reason for entering the finally
2719 // block. Should probably be extended to hold information for
2720 // break/continue from within the try block.
2721 enum { FALLING, THROWING, JUMPING };
2722
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002723 JumpTarget try_block;
2724 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002726 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002727
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002728 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729 // In case of thrown exceptions, this is where we continue.
2730 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002731 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002732
2733 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002734 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002735
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002736 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2737 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002739 // Shadow the labels for all escapes from the try block, including
2740 // returns. Shadowing hides the original label as the LabelShadow and
2741 // operations on the original actually affect the shadowing label.
2742 //
2743 // We should probably try to unify the escaping labels and the return
2744 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002745 int nof_escapes = node->escaping_targets()->length();
2746 List<ShadowTarget*> shadows(1 + nof_escapes);
2747
2748 // Add the shadow target for the function return.
2749 static const int kReturnShadowIndex = 0;
2750 shadows.Add(new ShadowTarget(&function_return_));
2751 bool function_return_was_shadowed = function_return_is_shadowed_;
2752 function_return_is_shadowed_ = true;
2753 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2754
2755 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002756 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002757 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002758 }
2759
2760 // Generate code for the statements in the try block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002761 { VirtualFrame::RegisterAllocationScope scope(this);
2762 VisitStatements(node->try_block()->statements());
2763 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002764
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002765 // Stop the introduced shadowing and count the number of required unlinks.
2766 // After shadowing stops, the original labels are unshadowed and the
2767 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002768 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002769 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002770 shadows[i]->StopShadowing();
2771 if (shadows[i]->is_linked()) nof_unlinks++;
2772 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002773 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002774
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002775 // Get an external reference to the handler address.
2776 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002777
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002778 // If we can fall off the end of the try block, unlink from the try
2779 // chain and set the state on the frame to FALLING.
2780 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002781 // The next handler address is on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002782 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002783 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002784 __ mov(r3, Operand(handler_address));
2785 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002786 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002787
2788 // Fake a top of stack value (unneeded when FALLING) and set the
2789 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002790 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002791 frame_->EmitPush(r0);
2792 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2793 if (nof_unlinks > 0) {
2794 finally_block.Jump();
2795 }
2796 }
2797
2798 // Generate code to unlink and set the state for the (formerly)
2799 // shadowing targets that have been jumped to.
2800 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002801 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002802 // If we have come from the shadowed return, the return value is
2803 // in (a non-refcounted reference to) r0. We must preserve it
2804 // until it is pushed.
2805 //
2806 // Because we can be jumping here (to spilled code) from
2807 // unspilled code, we need to reestablish a spilled frame at
2808 // this block.
2809 shadows[i]->Bind();
2810 frame_->SpillAll();
2811
2812 // Reload sp from the top handler, because some statements that
2813 // we break from (eg, for...in) may have left stuff on the
2814 // stack.
2815 __ mov(r3, Operand(handler_address));
2816 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002817 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002818
2819 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002820 // handler address is currently on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002821 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002822 frame_->EmitPop(r1);
2823 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002824 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002825
2826 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002827 // If this label shadowed the function return, materialize the
2828 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002829 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002830 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002831 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002832 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002833 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002834 }
2835 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002836 if (--nof_unlinks > 0) {
2837 // If this is not the last unlink block, jump around the next.
2838 finally_block.Jump();
2839 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002840 }
2841 }
2842
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002843 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002844 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002845
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002846 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002847 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002848
2849 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002850 // and the state - while evaluating the finally block.
2851 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002852 // Generate code for the statements in the finally block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002853 { VirtualFrame::RegisterAllocationScope scope(this);
2854 VisitStatements(node->finally_block()->statements());
2855 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002856
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002857 if (has_valid_frame()) {
2858 // Restore state and return value or faked TOS.
2859 frame_->EmitPop(r2);
2860 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002861 }
2862
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002863 // Generate code to jump to the right destination for all used
2864 // formerly shadowing targets. Deallocate each shadow target.
2865 for (int i = 0; i < shadows.length(); i++) {
2866 if (has_valid_frame() && shadows[i]->is_bound()) {
2867 JumpTarget* original = shadows[i]->other_target();
2868 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2869 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002870 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002871 skip.Branch(ne);
2872 frame_->PrepareForReturn();
2873 original->Jump();
2874 skip.Bind();
2875 } else {
2876 original->Branch(eq);
2877 }
2878 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002879 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002880
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002881 if (has_valid_frame()) {
2882 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002883 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002884 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2885 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002886
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002887 // Rethrow exception.
2888 frame_->EmitPush(r0);
2889 frame_->CallRuntime(Runtime::kReThrow, 1);
2890
2891 // Done.
2892 exit.Bind();
2893 }
2894 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002895}
2896
2897
ager@chromium.org7c537e22008-10-16 08:43:32 +00002898void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002899#ifdef DEBUG
2900 int original_height = frame_->height();
2901#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002903 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002904#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002905 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002906#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002907 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002908 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002909}
2910
2911
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002912void CodeGenerator::InstantiateFunction(
2913 Handle<SharedFunctionInfo> function_info) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002914 // Use the fast case closure allocation code that allocates in new
2915 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002916 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002917 FastNewClosureStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002918 frame_->EmitPush(Operand(function_info));
2919 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002920 frame_->CallStub(&stub, 1);
2921 frame_->EmitPush(r0);
2922 } else {
2923 // Create a new closure.
2924 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002925 frame_->EmitPush(Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002926 frame_->CallRuntime(Runtime::kNewClosure, 2);
2927 frame_->EmitPush(r0);
2928 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002929}
2930
2931
ager@chromium.org7c537e22008-10-16 08:43:32 +00002932void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002933#ifdef DEBUG
2934 int original_height = frame_->height();
2935#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002936 Comment cmnt(masm_, "[ FunctionLiteral");
2937
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002938 // Build the function info and instantiate it.
2939 Handle<SharedFunctionInfo> function_info =
2940 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002941 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002942 if (HasStackOverflow()) {
2943 ASSERT(frame_->height() == original_height);
2944 return;
2945 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002946 InstantiateFunction(function_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002947 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002948}
2949
2950
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002951void CodeGenerator::VisitSharedFunctionInfoLiteral(
2952 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002953#ifdef DEBUG
2954 int original_height = frame_->height();
2955#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002956 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2957 InstantiateFunction(node->shared_function_info());
ager@chromium.orgac091b72010-05-05 07:34:42 +00002958 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002959}
2960
2961
ager@chromium.org7c537e22008-10-16 08:43:32 +00002962void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002963#ifdef DEBUG
2964 int original_height = frame_->height();
2965#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002966 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002967 JumpTarget then;
2968 JumpTarget else_;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002969 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002970 if (has_valid_frame()) {
2971 Branch(false, &else_);
2972 }
2973 if (has_valid_frame() || then.is_linked()) {
2974 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002975 Load(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002976 }
2977 if (else_.is_linked()) {
2978 JumpTarget exit;
2979 if (has_valid_frame()) exit.Jump();
2980 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002981 Load(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002982 if (exit.is_linked()) exit.Bind();
2983 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00002984 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002985}
2986
2987
ager@chromium.org7c537e22008-10-16 08:43:32 +00002988void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2989 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002990 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002991
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002992 // JumpTargets do not yet support merging frames so the frame must be
2993 // spilled when jumping to these targets.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002994 JumpTarget slow;
2995 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002996
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002997 // Generate fast case for loading from slots that correspond to
2998 // local/global variables or arguments unless they are shadowed by
2999 // eval-introduced bindings.
3000 EmitDynamicLoadFromSlotFastCase(slot,
3001 typeof_state,
3002 &slow,
3003 &done);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003004
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003005 slow.Bind();
3006 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003007 frame_->EmitPush(Operand(slot->var()->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003008
ager@chromium.org7c537e22008-10-16 08:43:32 +00003009 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003010 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003011 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003012 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003013 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00003014
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003015 done.Bind();
3016 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003017
3018 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00003019 Register scratch = VirtualFrame::scratch0();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003020 TypeInfo info = type_info(slot);
3021 frame_->EmitPush(SlotOperand(slot, scratch), info);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003022
ager@chromium.org7c537e22008-10-16 08:43:32 +00003023 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003024 // Const slots may contain 'the hole' value (the constant hasn't been
3025 // initialized yet) which needs to be converted into the 'undefined'
3026 // value.
3027 Comment cmnt(masm_, "[ Unhole const");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003028 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003029 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003030 __ cmp(tos, ip);
3031 __ LoadRoot(tos, Heap::kUndefinedValueRootIndex, eq);
3032 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003033 }
3034 }
3035}
3036
3037
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003038void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot,
3039 TypeofState state) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003040 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003041 LoadFromSlot(slot, state);
3042
3043 // Bail out quickly if we're not using lazy arguments allocation.
3044 if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return;
3045
3046 // ... or if the slot isn't a non-parameter arguments slot.
3047 if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return;
3048
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003049 // Load the loaded value from the stack into a register but leave it on the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003050 // stack.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003051 Register tos = frame_->Peek();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003052
3053 // If the loaded value is the sentinel that indicates that we
3054 // haven't loaded the arguments object yet, we need to do it now.
3055 JumpTarget exit;
3056 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003057 __ cmp(tos, ip);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003058 exit.Branch(ne);
3059 frame_->Drop();
3060 StoreArgumentsObject(false);
3061 exit.Bind();
3062}
3063
3064
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003065void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
3066 ASSERT(slot != NULL);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003067 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003068 if (slot->type() == Slot::LOOKUP) {
3069 ASSERT(slot->var()->is_dynamic());
3070
3071 // For now, just do a runtime call.
3072 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003073 frame_->EmitPush(Operand(slot->var()->name()));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003074
3075 if (init_state == CONST_INIT) {
3076 // Same as the case for a normal store, but ignores attribute
3077 // (e.g. READ_ONLY) of context slot so that we can initialize
3078 // const properties (introduced via eval("const foo = (some
3079 // expr);")). Also, uses the current function context instead of
3080 // the top context.
3081 //
3082 // Note that we must declare the foo upon entry of eval(), via a
3083 // context slot declaration, but we cannot initialize it at the
3084 // same time, because the const declaration may be at the end of
3085 // the eval code (sigh...) and the const variable may have been
3086 // used before (where its value is 'undefined'). Thus, we can only
3087 // do the initialization when we actually encounter the expression
3088 // and when the expression operands are defined and valid, and
3089 // thus we need the split into 2 operations: declaration of the
3090 // context slot followed by initialization.
3091 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3092 } else {
3093 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
3094 }
3095 // Storing a variable must keep the (new) value on the expression
3096 // stack. This is necessary for compiling assignment expressions.
3097 frame_->EmitPush(r0);
3098
3099 } else {
3100 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00003101 Register scratch = VirtualFrame::scratch0();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003102 Register scratch2 = VirtualFrame::scratch1();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003103
ager@chromium.org357bf652010-04-12 11:30:10 +00003104 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003105 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00003106
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003107 if (init_state == CONST_INIT) {
3108 ASSERT(slot->var()->mode() == Variable::CONST);
3109 // Only the first const initialization must be executed (the slot
3110 // still contains 'the hole' value). When the assignment is
3111 // executed, the code is identical to a normal store (see below).
3112 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00003113 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003114 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00003115 __ cmp(scratch, ip);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003116 exit.Branch(ne);
3117 }
3118
3119 // We must execute the store. Storing a variable must keep the
3120 // (new) value on the stack. This is necessary for compiling
3121 // assignment expressions.
3122 //
3123 // Note: We will reach here even with slot->var()->mode() ==
3124 // Variable::CONST because of const declarations which will
3125 // initialize consts to 'the hole' value and by doing so, end up
3126 // calling this code. r2 may be loaded with context; used below in
3127 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00003128 Register tos = frame_->Peek();
3129 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003130 if (slot->type() == Slot::CONTEXT) {
3131 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00003132 __ tst(tos, Operand(kSmiTagMask));
3133 // We don't use tos any more after here.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003134 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00003135 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003136 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003137 // We need an extra register. Until we have a way to do that in the
3138 // virtual frame we will cheat and ask for a free TOS register.
3139 Register scratch3 = frame_->GetTOSRegister();
3140 __ RecordWrite(scratch, Operand(offset), scratch2, scratch3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003141 }
3142 // If we definitely did not jump over the assignment, we do not need
3143 // to bind the exit label. Doing so can defeat peephole
3144 // optimization.
3145 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3146 exit.Bind();
3147 }
3148 }
3149}
3150
3151
ager@chromium.org381abbb2009-02-25 13:23:22 +00003152void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
3153 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003154 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003155 // Check that no extension objects have been created by calls to
3156 // eval from the current scope to the global scope.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003157 Register tmp = frame_->scratch0();
3158 Register tmp2 = frame_->scratch1();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003159 Register context = cp;
3160 Scope* s = scope();
3161 while (s != NULL) {
3162 if (s->num_heap_slots() > 0) {
3163 if (s->calls_eval()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003164 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003165 // Check that extension is NULL.
3166 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
3167 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003168 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003169 }
3170 // Load next context in chain.
3171 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
3172 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3173 context = tmp;
3174 }
3175 // If no outer scope calls eval, we do not need to check more
3176 // context extensions.
3177 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
3178 s = s->outer_scope();
3179 }
3180
3181 if (s->is_eval_scope()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003182 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003183 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00003184 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003185 __ bind(&next);
3186 // Terminate at global context.
3187 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003188 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
3189 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003190 __ b(eq, &fast);
3191 // Check that extension is NULL.
3192 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
3193 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003194 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003195 // Load next context in chain.
3196 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
3197 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3198 __ b(&next);
3199 __ bind(&fast);
3200 }
3201
ager@chromium.org381abbb2009-02-25 13:23:22 +00003202 // Load the global object.
3203 LoadGlobal();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003204 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00003205 frame_->CallLoadIC(slot->var()->name(),
3206 typeof_state == INSIDE_TYPEOF
3207 ? RelocInfo::CODE_TARGET
3208 : RelocInfo::CODE_TARGET_CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003209}
3210
3211
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003212void CodeGenerator::EmitDynamicLoadFromSlotFastCase(Slot* slot,
3213 TypeofState typeof_state,
3214 JumpTarget* slow,
3215 JumpTarget* done) {
3216 // Generate fast-case code for variables that might be shadowed by
3217 // eval-introduced variables. Eval is used a lot without
3218 // introducing variables. In those cases, we do not want to
3219 // perform a runtime call for all variables in the scope
3220 // containing the eval.
3221 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
3222 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, slow);
3223 frame_->SpillAll();
3224 done->Jump();
3225
3226 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
3227 frame_->SpillAll();
3228 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
3229 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
3230 if (potential_slot != NULL) {
3231 // Generate fast case for locals that rewrite to slots.
3232 __ ldr(r0,
3233 ContextSlotOperandCheckExtensions(potential_slot,
3234 r1,
3235 r2,
3236 slow));
3237 if (potential_slot->var()->mode() == Variable::CONST) {
3238 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3239 __ cmp(r0, ip);
3240 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
3241 }
3242 done->Jump();
3243 } else if (rewrite != NULL) {
3244 // Generate fast case for argument loads.
3245 Property* property = rewrite->AsProperty();
3246 if (property != NULL) {
3247 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
3248 Literal* key_literal = property->key()->AsLiteral();
3249 if (obj_proxy != NULL &&
3250 key_literal != NULL &&
3251 obj_proxy->IsArguments() &&
3252 key_literal->handle()->IsSmi()) {
3253 // Load arguments object if there are no eval-introduced
3254 // variables. Then load the argument from the arguments
3255 // object using keyed load.
3256 __ ldr(r0,
3257 ContextSlotOperandCheckExtensions(obj_proxy->var()->slot(),
3258 r1,
3259 r2,
3260 slow));
3261 frame_->EmitPush(r0);
3262 __ mov(r1, Operand(key_literal->handle()));
3263 frame_->EmitPush(r1);
3264 EmitKeyedLoad();
3265 done->Jump();
3266 }
3267 }
3268 }
3269 }
3270}
3271
3272
ager@chromium.org7c537e22008-10-16 08:43:32 +00003273void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003274#ifdef DEBUG
3275 int original_height = frame_->height();
3276#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003277 Comment cmnt(masm_, "[ Slot");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003278 LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003279 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003280}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003281
ager@chromium.org7c537e22008-10-16 08:43:32 +00003282
3283void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003284#ifdef DEBUG
3285 int original_height = frame_->height();
3286#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003287 Comment cmnt(masm_, "[ VariableProxy");
3288
3289 Variable* var = node->var();
3290 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003291 if (expr != NULL) {
3292 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003293 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00003294 ASSERT(var->is_global());
3295 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003296 ref.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003297 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003298 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003299}
3300
3301
ager@chromium.org7c537e22008-10-16 08:43:32 +00003302void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003303#ifdef DEBUG
3304 int original_height = frame_->height();
3305#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003306 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00003307 Register reg = frame_->GetTOSRegister();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003308 bool is_smi = node->handle()->IsSmi();
ager@chromium.org357bf652010-04-12 11:30:10 +00003309 __ mov(reg, Operand(node->handle()));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003310 frame_->EmitPush(reg, is_smi ? TypeInfo::Smi() : TypeInfo::Unknown());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003311 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003312}
3313
3314
ager@chromium.org7c537e22008-10-16 08:43:32 +00003315void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003316#ifdef DEBUG
3317 int original_height = frame_->height();
3318#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003319 Comment cmnt(masm_, "[ RexExp Literal");
3320
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003321 Register tmp = VirtualFrame::scratch0();
3322 // Free up a TOS register that can be used to push the literal.
3323 Register literal = frame_->GetTOSRegister();
3324
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003325 // Retrieve the literal array and check the allocated entry.
3326
3327 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003328 __ ldr(tmp, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003329
3330 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003331 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kLiteralsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003332
3333 // Load the literal at the ast saved index.
3334 int literal_offset =
3335 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003336 __ ldr(literal, FieldMemOperand(tmp, literal_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003337
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003338 JumpTarget materialized;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003339 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003340 __ cmp(literal, ip);
3341 // This branch locks the virtual frame at the done label to match the
3342 // one we have here, where the literal register is not on the stack and
3343 // nothing is spilled.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003344 materialized.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003345
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003346 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003347 // the literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003348 // literal array (0)
3349 frame_->EmitPush(tmp);
3350 // literal index (1)
3351 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3352 // RegExp pattern (2)
3353 frame_->EmitPush(Operand(node->pattern()));
3354 // RegExp flags (3)
3355 frame_->EmitPush(Operand(node->flags()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003356 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003357 __ Move(literal, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003358
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003359 materialized.Bind();
3360
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003361 frame_->EmitPush(literal);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003362 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
3363 frame_->EmitPush(Operand(Smi::FromInt(size)));
3364 frame_->CallRuntime(Runtime::kAllocateInNewSpace, 1);
3365 // TODO(lrn): Use AllocateInNewSpace macro with fallback to runtime.
3366 // r0 is newly allocated space.
3367
3368 // Reuse literal variable with (possibly) a new register, still holding
3369 // the materialized boilerplate.
3370 literal = frame_->PopToRegister(r0);
3371
3372 __ CopyFields(r0, literal, tmp.bit(), size / kPointerSize);
3373
3374 // Push the clone.
3375 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003376 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003377}
3378
3379
ager@chromium.org7c537e22008-10-16 08:43:32 +00003380void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003381#ifdef DEBUG
3382 int original_height = frame_->height();
3383#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003384 Comment cmnt(masm_, "[ ObjectLiteral");
3385
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003386 Register literal = frame_->GetTOSRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003387 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003388 __ ldr(literal, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003389 // Literal array.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003390 __ ldr(literal, FieldMemOperand(literal, JSFunction::kLiteralsOffset));
3391 frame_->EmitPush(literal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003392 // Literal index.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003393 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003394 // Constant properties.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003395 frame_->EmitPush(Operand(node->constant_properties()));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003396 // Should the object literal have fast elements?
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003397 frame_->EmitPush(Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003398 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003399 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003400 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003401 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003402 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003403 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003404 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003405 // At the start of each iteration, the top of stack contains
3406 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003407 ObjectLiteral::Property* property = node->properties()->at(i);
3408 Literal* key = property->key();
3409 Expression* value = property->value();
3410 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003411 case ObjectLiteral::Property::CONSTANT:
3412 break;
3413 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3414 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3415 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003416 case ObjectLiteral::Property::COMPUTED:
3417 if (key->handle()->IsSymbol()) {
3418 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003419 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003420 frame_->PopToR0();
3421 // Fetch the object literal.
3422 frame_->SpillAllButCopyTOSToR1();
ager@chromium.org5c838252010-02-19 08:53:10 +00003423 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003424 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3425 break;
3426 }
3427 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003428 case ObjectLiteral::Property::PROTOTYPE: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003429 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003430 Load(key);
3431 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003432 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003433 break;
3434 }
3435 case ObjectLiteral::Property::SETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003436 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003437 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003438 frame_->EmitPush(Operand(Smi::FromInt(1)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003439 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003440 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003441 break;
3442 }
3443 case ObjectLiteral::Property::GETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003444 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003445 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003446 frame_->EmitPush(Operand(Smi::FromInt(0)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003447 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003448 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003449 break;
3450 }
3451 }
3452 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003453 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003454}
3455
3456
ager@chromium.org7c537e22008-10-16 08:43:32 +00003457void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003458#ifdef DEBUG
3459 int original_height = frame_->height();
3460#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003461 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003462
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003463 Register tos = frame_->GetTOSRegister();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003464 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003465 __ ldr(tos, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003466 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003467 __ ldr(tos, FieldMemOperand(tos, JSFunction::kLiteralsOffset));
3468 frame_->EmitPush(tos);
3469 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3470 frame_->EmitPush(Operand(node->constant_elements()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003471 int length = node->values()->length();
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003472 if (node->constant_elements()->map() == Heap::fixed_cow_array_map()) {
3473 FastCloneShallowArrayStub stub(
3474 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
3475 frame_->CallStub(&stub, 3);
3476 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
3477 } else if (node->depth() > 1) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003478 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003479 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003480 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003481 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003482 FastCloneShallowArrayStub stub(
3483 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
ager@chromium.org5c838252010-02-19 08:53:10 +00003484 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003485 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003486 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003487 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003488
3489 // Generate code to set the elements in the array that are not
3490 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003491 for (int i = 0; i < node->values()->length(); i++) {
3492 Expression* value = node->values()->at(i);
3493
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003494 // If value is a literal the property value is already set in the
3495 // boilerplate object.
3496 if (value->AsLiteral() != NULL) continue;
3497 // If value is a materialized literal the property value is already set
3498 // in the boilerplate object if it is simple.
3499 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003500
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003501 // The property must be set by generated code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003502 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003503 frame_->PopToR0();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003504 // Fetch the object literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003505 frame_->SpillAllButCopyTOSToR1();
3506
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003507 // Get the elements array.
3508 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003509
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003510 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003511 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003512 __ str(r0, FieldMemOperand(r1, offset));
3513
3514 // Update the write barrier for the array address.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003515 __ RecordWrite(r1, Operand(offset), r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003516 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003517 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003518}
3519
3520
ager@chromium.org32912102009-01-16 10:38:43 +00003521void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003522#ifdef DEBUG
3523 int original_height = frame_->height();
3524#endif
ager@chromium.org32912102009-01-16 10:38:43 +00003525 // Call runtime routine to allocate the catch extension object and
3526 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003527 Comment cmnt(masm_, "[ CatchExtensionObject");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003528 Load(node->key());
3529 Load(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003530 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3531 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003532 ASSERT_EQ(original_height + 1, frame_->height());
3533}
3534
3535
3536void CodeGenerator::EmitSlotAssignment(Assignment* node) {
3537#ifdef DEBUG
3538 int original_height = frame_->height();
3539#endif
3540 Comment cmnt(masm(), "[ Variable Assignment");
3541 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3542 ASSERT(var != NULL);
3543 Slot* slot = var->slot();
3544 ASSERT(slot != NULL);
3545
3546 // Evaluate the right-hand side.
3547 if (node->is_compound()) {
3548 // For a compound assignment the right-hand side is a binary operation
3549 // between the current property value and the actual right-hand side.
3550 LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
3551
3552 // Perform the binary operation.
3553 Literal* literal = node->value()->AsLiteral();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003554 bool overwrite_value = node->value()->ResultOverwriteAllowed();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003555 if (literal != NULL && literal->handle()->IsSmi()) {
3556 SmiOperation(node->binary_op(),
3557 literal->handle(),
3558 false,
3559 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3560 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003561 GenerateInlineSmi inline_smi =
3562 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3563 if (literal != NULL) {
3564 ASSERT(!literal->handle()->IsSmi());
3565 inline_smi = DONT_GENERATE_INLINE_SMI;
3566 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003567 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003568 GenericBinaryOperation(node->binary_op(),
3569 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3570 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003571 }
3572 } else {
3573 Load(node->value());
3574 }
3575
3576 // Perform the assignment.
3577 if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) {
3578 CodeForSourcePosition(node->position());
3579 StoreToSlot(slot,
3580 node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT);
3581 }
3582 ASSERT_EQ(original_height + 1, frame_->height());
3583}
3584
3585
3586void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) {
3587#ifdef DEBUG
3588 int original_height = frame_->height();
3589#endif
3590 Comment cmnt(masm(), "[ Named Property Assignment");
3591 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3592 Property* prop = node->target()->AsProperty();
3593 ASSERT(var == NULL || (prop == NULL && var->is_global()));
3594
3595 // Initialize name and evaluate the receiver sub-expression if necessary. If
3596 // the receiver is trivial it is not placed on the stack at this point, but
3597 // loaded whenever actually needed.
3598 Handle<String> name;
3599 bool is_trivial_receiver = false;
3600 if (var != NULL) {
3601 name = var->name();
3602 } else {
3603 Literal* lit = prop->key()->AsLiteral();
3604 ASSERT_NOT_NULL(lit);
3605 name = Handle<String>::cast(lit->handle());
3606 // Do not materialize the receiver on the frame if it is trivial.
3607 is_trivial_receiver = prop->obj()->IsTrivial();
3608 if (!is_trivial_receiver) Load(prop->obj());
3609 }
3610
3611 // Change to slow case in the beginning of an initialization block to
3612 // avoid the quadratic behavior of repeatedly adding fast properties.
3613 if (node->starts_initialization_block()) {
3614 // Initialization block consists of assignments of the form expr.x = ..., so
3615 // this will never be an assignment to a variable, so there must be a
3616 // receiver object.
3617 ASSERT_EQ(NULL, var);
3618 if (is_trivial_receiver) {
3619 Load(prop->obj());
3620 } else {
3621 frame_->Dup();
3622 }
3623 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3624 }
3625
3626 // Change to fast case at the end of an initialization block. To prepare for
3627 // that add an extra copy of the receiver to the frame, so that it can be
3628 // converted back to fast case after the assignment.
3629 if (node->ends_initialization_block() && !is_trivial_receiver) {
3630 frame_->Dup();
3631 }
3632
3633 // Stack layout:
3634 // [tos] : receiver (only materialized if non-trivial)
3635 // [tos+1] : receiver if at the end of an initialization block
3636
3637 // Evaluate the right-hand side.
3638 if (node->is_compound()) {
3639 // For a compound assignment the right-hand side is a binary operation
3640 // between the current property value and the actual right-hand side.
3641 if (is_trivial_receiver) {
3642 Load(prop->obj());
3643 } else if (var != NULL) {
3644 LoadGlobal();
3645 } else {
3646 frame_->Dup();
3647 }
3648 EmitNamedLoad(name, var != NULL);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003649
3650 // Perform the binary operation.
3651 Literal* literal = node->value()->AsLiteral();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003652 bool overwrite_value = node->value()->ResultOverwriteAllowed();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003653 if (literal != NULL && literal->handle()->IsSmi()) {
3654 SmiOperation(node->binary_op(),
3655 literal->handle(),
3656 false,
3657 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3658 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003659 GenerateInlineSmi inline_smi =
3660 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3661 if (literal != NULL) {
3662 ASSERT(!literal->handle()->IsSmi());
3663 inline_smi = DONT_GENERATE_INLINE_SMI;
3664 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003665 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003666 GenericBinaryOperation(node->binary_op(),
3667 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3668 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003669 }
3670 } else {
3671 // For non-compound assignment just load the right-hand side.
3672 Load(node->value());
3673 }
3674
3675 // Stack layout:
3676 // [tos] : value
3677 // [tos+1] : receiver (only materialized if non-trivial)
3678 // [tos+2] : receiver if at the end of an initialization block
3679
3680 // Perform the assignment. It is safe to ignore constants here.
3681 ASSERT(var == NULL || var->mode() != Variable::CONST);
3682 ASSERT_NE(Token::INIT_CONST, node->op());
3683 if (is_trivial_receiver) {
3684 // Load the receiver and swap with the value.
3685 Load(prop->obj());
3686 Register t0 = frame_->PopToRegister();
3687 Register t1 = frame_->PopToRegister(t0);
3688 frame_->EmitPush(t0);
3689 frame_->EmitPush(t1);
3690 }
3691 CodeForSourcePosition(node->position());
3692 bool is_contextual = (var != NULL);
3693 EmitNamedStore(name, is_contextual);
3694 frame_->EmitPush(r0);
3695
3696 // Change to fast case at the end of an initialization block.
3697 if (node->ends_initialization_block()) {
3698 ASSERT_EQ(NULL, var);
3699 // The argument to the runtime call is the receiver.
3700 if (is_trivial_receiver) {
3701 Load(prop->obj());
3702 } else {
3703 // A copy of the receiver is below the value of the assignment. Swap
3704 // the receiver and the value of the assignment expression.
3705 Register t0 = frame_->PopToRegister();
3706 Register t1 = frame_->PopToRegister(t0);
3707 frame_->EmitPush(t0);
3708 frame_->EmitPush(t1);
3709 }
3710 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3711 }
3712
3713 // Stack layout:
3714 // [tos] : result
3715
3716 ASSERT_EQ(original_height + 1, frame_->height());
3717}
3718
3719
3720void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) {
3721#ifdef DEBUG
3722 int original_height = frame_->height();
3723#endif
3724 Comment cmnt(masm_, "[ Keyed Property Assignment");
3725 Property* prop = node->target()->AsProperty();
3726 ASSERT_NOT_NULL(prop);
3727
3728 // Evaluate the receiver subexpression.
3729 Load(prop->obj());
3730
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003731 WriteBarrierCharacter wb_info;
3732
ager@chromium.orgac091b72010-05-05 07:34:42 +00003733 // Change to slow case in the beginning of an initialization block to
3734 // avoid the quadratic behavior of repeatedly adding fast properties.
3735 if (node->starts_initialization_block()) {
3736 frame_->Dup();
3737 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3738 }
3739
3740 // Change to fast case at the end of an initialization block. To prepare for
3741 // that add an extra copy of the receiver to the frame, so that it can be
3742 // converted back to fast case after the assignment.
3743 if (node->ends_initialization_block()) {
3744 frame_->Dup();
3745 }
3746
3747 // Evaluate the key subexpression.
3748 Load(prop->key());
3749
3750 // Stack layout:
3751 // [tos] : key
3752 // [tos+1] : receiver
3753 // [tos+2] : receiver if at the end of an initialization block
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003754 //
ager@chromium.orgac091b72010-05-05 07:34:42 +00003755 // Evaluate the right-hand side.
3756 if (node->is_compound()) {
3757 // For a compound assignment the right-hand side is a binary operation
3758 // between the current property value and the actual right-hand side.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003759 // Duplicate receiver and key for loading the current property value.
3760 frame_->Dup2();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003761 EmitKeyedLoad();
3762 frame_->EmitPush(r0);
3763
3764 // Perform the binary operation.
3765 Literal* literal = node->value()->AsLiteral();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003766 bool overwrite_value = node->value()->ResultOverwriteAllowed();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003767 if (literal != NULL && literal->handle()->IsSmi()) {
3768 SmiOperation(node->binary_op(),
3769 literal->handle(),
3770 false,
3771 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3772 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003773 GenerateInlineSmi inline_smi =
3774 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3775 if (literal != NULL) {
3776 ASSERT(!literal->handle()->IsSmi());
3777 inline_smi = DONT_GENERATE_INLINE_SMI;
3778 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003779 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003780 GenericBinaryOperation(node->binary_op(),
3781 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3782 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003783 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003784 wb_info = node->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
ager@chromium.orgac091b72010-05-05 07:34:42 +00003785 } else {
3786 // For non-compound assignment just load the right-hand side.
3787 Load(node->value());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003788 wb_info = node->value()->AsLiteral() != NULL ?
3789 NEVER_NEWSPACE :
3790 (node->value()->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003791 }
3792
3793 // Stack layout:
3794 // [tos] : value
3795 // [tos+1] : key
3796 // [tos+2] : receiver
3797 // [tos+3] : receiver if at the end of an initialization block
3798
3799 // Perform the assignment. It is safe to ignore constants here.
3800 ASSERT(node->op() != Token::INIT_CONST);
3801 CodeForSourcePosition(node->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003802 EmitKeyedStore(prop->key()->type(), wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003803 frame_->EmitPush(r0);
3804
3805 // Stack layout:
3806 // [tos] : result
3807 // [tos+1] : receiver if at the end of an initialization block
3808
3809 // Change to fast case at the end of an initialization block.
3810 if (node->ends_initialization_block()) {
3811 // The argument to the runtime call is the extra copy of the receiver,
3812 // which is below the value of the assignment. Swap the receiver and
3813 // the value of the assignment expression.
3814 Register t0 = frame_->PopToRegister();
3815 Register t1 = frame_->PopToRegister(t0);
3816 frame_->EmitPush(t1);
3817 frame_->EmitPush(t0);
3818 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3819 }
3820
3821 // Stack layout:
3822 // [tos] : result
3823
3824 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org32912102009-01-16 10:38:43 +00003825}
3826
3827
ager@chromium.org7c537e22008-10-16 08:43:32 +00003828void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003829 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003830#ifdef DEBUG
3831 int original_height = frame_->height();
3832#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003833 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003834
ager@chromium.orgac091b72010-05-05 07:34:42 +00003835 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3836 Property* prop = node->target()->AsProperty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003837
ager@chromium.orgac091b72010-05-05 07:34:42 +00003838 if (var != NULL && !var->is_global()) {
3839 EmitSlotAssignment(node);
mads.s.ager31e71382008-08-13 09:32:07 +00003840
ager@chromium.orgac091b72010-05-05 07:34:42 +00003841 } else if ((prop != NULL && prop->key()->IsPropertyName()) ||
3842 (var != NULL && var->is_global())) {
3843 // Properties whose keys are property names and global variables are
3844 // treated as named property references. We do not need to consider
3845 // global 'this' because it is not a valid left-hand side.
3846 EmitNamedPropertyAssignment(node);
3847
3848 } else if (prop != NULL) {
3849 // Other properties (including rewritten parameters for a function that
3850 // uses arguments) are keyed property assignments.
3851 EmitKeyedPropertyAssignment(node);
3852
3853 } else {
3854 // Invalid left-hand side.
3855 Load(node->target());
3856 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
3857 // The runtime call doesn't actually return but the code generator will
3858 // still generate code and expects a certain frame height.
3859 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003860 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003861 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003862}
3863
3864
ager@chromium.org7c537e22008-10-16 08:43:32 +00003865void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003866#ifdef DEBUG
3867 int original_height = frame_->height();
3868#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003869 Comment cmnt(masm_, "[ Throw");
3870
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003871 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003872 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003873 frame_->CallRuntime(Runtime::kThrow, 1);
3874 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003875 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003876}
3877
3878
ager@chromium.org7c537e22008-10-16 08:43:32 +00003879void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003880#ifdef DEBUG
3881 int original_height = frame_->height();
3882#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003883 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003884
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003885 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003886 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003887 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003888 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003889}
3890
3891
ager@chromium.org7c537e22008-10-16 08:43:32 +00003892void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003893#ifdef DEBUG
3894 int original_height = frame_->height();
3895#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003896 Comment cmnt(masm_, "[ Call");
3897
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003898 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003899 ZoneList<Expression*>* args = node->arguments();
3900
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003901 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003902 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003903 Variable* var = function->AsVariableProxy()->AsVariable();
3904 Property* property = function->AsProperty();
3905
3906 // ------------------------------------------------------------------------
3907 // Fast-case: Use inline caching.
3908 // ---
3909 // According to ECMA-262, section 11.2.3, page 44, the function to call
3910 // must be resolved after the arguments have been evaluated. The IC code
3911 // automatically handles this by loading the arguments before the function
3912 // is resolved in cache misses (this also holds for megamorphic calls).
3913 // ------------------------------------------------------------------------
3914
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003915 if (var != NULL && var->is_possibly_eval()) {
3916 // ----------------------------------
3917 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3918 // ----------------------------------
3919
3920 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3921 // resolve the function we need to call and the receiver of the
3922 // call. Then we call the resolved function using the given
3923 // arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003924
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003925 // Prepare stack for call to resolved function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003926 Load(function);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003927
3928 // Allocate a frame slot for the receiver.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003929 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003930
3931 // Load the arguments.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003932 int arg_count = args->length();
3933 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003934 Load(args->at(i));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003935 }
3936
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003937 VirtualFrame::SpilledScope spilled_scope(frame_);
3938
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003939 // If we know that eval can only be shadowed by eval-introduced
3940 // variables we attempt to load the global eval function directly
3941 // in generated code. If we succeed, there is no need to perform a
3942 // context lookup in the runtime system.
3943 JumpTarget done;
3944 if (var->slot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
3945 ASSERT(var->slot()->type() == Slot::LOOKUP);
3946 JumpTarget slow;
3947 // Prepare the stack for the call to
3948 // ResolvePossiblyDirectEvalNoLookup by pushing the loaded
3949 // function, the first argument to the eval call and the
3950 // receiver.
3951 LoadFromGlobalSlotCheckExtensions(var->slot(),
3952 NOT_INSIDE_TYPEOF,
3953 &slow);
3954 frame_->EmitPush(r0);
3955 if (arg_count > 0) {
3956 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3957 frame_->EmitPush(r1);
3958 } else {
3959 frame_->EmitPush(r2);
3960 }
3961 __ ldr(r1, frame_->Receiver());
3962 frame_->EmitPush(r1);
3963
3964 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEvalNoLookup, 3);
3965
3966 done.Jump();
3967 slow.Bind();
3968 }
3969
3970 // Prepare the stack for the call to ResolvePossiblyDirectEval by
3971 // pushing the loaded function, the first argument to the eval
3972 // call and the receiver.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003973 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3974 frame_->EmitPush(r1);
3975 if (arg_count > 0) {
3976 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3977 frame_->EmitPush(r1);
3978 } else {
3979 frame_->EmitPush(r2);
3980 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003981 __ ldr(r1, frame_->Receiver());
3982 frame_->EmitPush(r1);
3983
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003984 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003985 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003986
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003987 // If we generated fast-case code bind the jump-target where fast
3988 // and slow case merge.
3989 if (done.is_linked()) done.Bind();
3990
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003991 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003992 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003993 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3994
3995 // Call the function.
3996 CodeForSourcePosition(node->position());
3997
3998 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003999 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004000 frame_->CallStub(&call_function, arg_count + 1);
4001
4002 __ ldr(cp, frame_->Context());
4003 // Remove the function from the stack.
4004 frame_->Drop();
4005 frame_->EmitPush(r0);
4006
4007 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004008 // ----------------------------------
4009 // JavaScript example: 'foo(1, 2, 3)' // foo is global
4010 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004011 // Pass the global object as the receiver and let the IC stub
4012 // patch the stack to use the global proxy as 'this' in the
4013 // invoked function.
4014 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004015
4016 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004017 int arg_count = args->length();
4018 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004019 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004020 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004021
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004022 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +00004023 // Setup the name register and call the IC initialization code.
4024 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004025 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4026 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004027 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004028 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
4029 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004030 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004031 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004032
4033 } else if (var != NULL && var->slot() != NULL &&
4034 var->slot()->type() == Slot::LOOKUP) {
4035 // ----------------------------------
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004036 // JavaScript examples:
4037 //
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004038 // with (obj) foo(1, 2, 3) // foo may be in obj.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004039 //
4040 // function f() {};
4041 // function g() {
4042 // eval(...);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004043 // f(); // f could be in extension object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004044 // }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004045 // ----------------------------------
4046
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004047 JumpTarget slow, done;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004048
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004049 // Generate fast case for loading functions from slots that
4050 // correspond to local/global variables or arguments unless they
4051 // are shadowed by eval-introduced bindings.
4052 EmitDynamicLoadFromSlotFastCase(var->slot(),
4053 NOT_INSIDE_TYPEOF,
4054 &slow,
4055 &done);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004056
4057 slow.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004058 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004059 frame_->EmitPush(cp);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004060 frame_->EmitPush(Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004061 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004062 // r0: slot value; r1: receiver
4063
4064 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004065 frame_->EmitPush(r0); // function
4066 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004067
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004068 // If fast case code has been generated, emit code to push the
4069 // function and receiver and have the slow path jump around this
4070 // code.
4071 if (done.is_linked()) {
4072 JumpTarget call;
4073 call.Jump();
4074 done.Bind();
4075 frame_->EmitPush(r0); // function
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004076 LoadGlobalReceiver(VirtualFrame::scratch0()); // receiver
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004077 call.Bind();
4078 }
4079
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004080 // Call the function. At this point, everything is spilled but the
4081 // function and receiver are in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004082 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004083 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004084
4085 } else if (property != NULL) {
4086 // Check if the key is a literal string.
4087 Literal* literal = property->key()->AsLiteral();
4088
4089 if (literal != NULL && literal->handle()->IsSymbol()) {
4090 // ------------------------------------------------------------------
4091 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
4092 // ------------------------------------------------------------------
4093
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004094 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004095
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004096 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
4097 name->IsEqualTo(CStrVector("apply")) &&
4098 args->length() == 2 &&
4099 args->at(1)->AsVariableProxy() != NULL &&
4100 args->at(1)->AsVariableProxy()->IsArguments()) {
4101 // Use the optimized Function.prototype.apply that avoids
4102 // allocating lazily allocated arguments objects.
4103 CallApplyLazy(property->obj(),
4104 args->at(0),
4105 args->at(1)->AsVariableProxy(),
4106 node->position());
4107
4108 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004109 Load(property->obj()); // Receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004110 // Load the arguments.
4111 int arg_count = args->length();
4112 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004113 Load(args->at(i));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004114 }
4115
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004116 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004117 // Set the name register and call the IC initialization code.
4118 __ mov(r2, Operand(name));
4119 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4120 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
4121 CodeForSourcePosition(node->position());
4122 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4123 __ ldr(cp, frame_->Context());
4124 frame_->EmitPush(r0);
4125 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004126
4127 } else {
4128 // -------------------------------------------
4129 // JavaScript example: 'array[index](1, 2, 3)'
4130 // -------------------------------------------
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004131 Load(property->obj());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004132 if (property->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004133 Load(property->key());
4134 EmitKeyedLoad();
4135 // Put the function below the receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004136 // Use the global receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004137 frame_->EmitPush(r0); // Function.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004138 LoadGlobalReceiver(VirtualFrame::scratch0());
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004139 // Call the function.
4140 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
4141 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004142 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004143 // Load the arguments.
4144 int arg_count = args->length();
4145 for (int i = 0; i < arg_count; i++) {
4146 Load(args->at(i));
4147 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004148
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004149 // Set the name register and call the IC initialization code.
4150 Load(property->key());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004151 frame_->SpillAll();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004152 frame_->EmitPop(r2); // Function name.
4153
4154 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4155 Handle<Code> stub = ComputeKeyedCallInitialize(arg_count, in_loop);
4156 CodeForSourcePosition(node->position());
4157 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4158 __ ldr(cp, frame_->Context());
4159 frame_->EmitPush(r0);
4160 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004161 }
4162
4163 } else {
4164 // ----------------------------------
4165 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
4166 // ----------------------------------
4167
4168 // Load the function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004169 Load(function);
4170
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004171 // Pass the global proxy as the receiver.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004172 LoadGlobalReceiver(VirtualFrame::scratch0());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004173
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004174 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004175 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004176 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004177 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004178 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004179}
4180
4181
ager@chromium.org7c537e22008-10-16 08:43:32 +00004182void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004183#ifdef DEBUG
4184 int original_height = frame_->height();
4185#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004186 Comment cmnt(masm_, "[ CallNew");
4187
4188 // According to ECMA-262, section 11.2.2, page 44, the function
4189 // expression in new calls must be evaluated before the
4190 // arguments. This is different from ordinary calls, where the
4191 // actual function to call is resolved after the arguments have been
4192 // evaluated.
4193
ricow@chromium.org65fae842010-08-25 15:26:24 +00004194 // Push constructor on the stack. If it's not a function it's used as
4195 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
4196 // ignored.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004197 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004198
4199 // Push the arguments ("left-to-right") on the stack.
4200 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004201 int arg_count = args->length();
4202 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004203 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004204 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004205
ricow@chromium.org65fae842010-08-25 15:26:24 +00004206 // Spill everything from here to simplify the implementation.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004207 VirtualFrame::SpilledScope spilled_scope(frame_);
4208
ricow@chromium.org65fae842010-08-25 15:26:24 +00004209 // Load the argument count into r0 and the function into r1 as per
4210 // calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004211 __ mov(r0, Operand(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004212 __ ldr(r1, frame_->ElementAt(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004213
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004214 // Call the construct call builtin that handles allocation and
4215 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004216 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004217 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004218 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004219 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004220
ager@chromium.orgac091b72010-05-05 07:34:42 +00004221 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004222}
4223
4224
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004225void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004226 Register scratch = VirtualFrame::scratch0();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004227 JumpTarget null, function, leave, non_function_constructor;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004228
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004229 // Load the object into register.
4230 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004231 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004232 Register tos = frame_->PopToRegister();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004233
4234 // If the object is a smi, we return null.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004235 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004236 null.Branch(eq);
4237
4238 // Check that the object is a JS object but take special care of JS
4239 // functions to make sure they have 'Function' as their class.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004240 __ CompareObjectType(tos, tos, scratch, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004241 null.Branch(lt);
4242
4243 // As long as JS_FUNCTION_TYPE is the last instance type and it is
4244 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
4245 // LAST_JS_OBJECT_TYPE.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00004246 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4247 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004248 __ cmp(scratch, Operand(JS_FUNCTION_TYPE));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004249 function.Branch(eq);
4250
4251 // Check if the constructor in the map is a function.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004252 __ ldr(tos, FieldMemOperand(tos, Map::kConstructorOffset));
4253 __ CompareObjectType(tos, scratch, scratch, JS_FUNCTION_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004254 non_function_constructor.Branch(ne);
4255
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004256 // The tos register now contains the constructor function. Grab the
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004257 // instance class name from there.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004258 __ ldr(tos, FieldMemOperand(tos, JSFunction::kSharedFunctionInfoOffset));
4259 __ ldr(tos,
4260 FieldMemOperand(tos, SharedFunctionInfo::kInstanceClassNameOffset));
4261 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004262 leave.Jump();
4263
4264 // Functions have class 'Function'.
4265 function.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004266 __ mov(tos, Operand(Factory::function_class_symbol()));
4267 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004268 leave.Jump();
4269
4270 // Objects with a non-function constructor have class 'Object'.
4271 non_function_constructor.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004272 __ mov(tos, Operand(Factory::Object_symbol()));
4273 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004274 leave.Jump();
4275
4276 // Non-JS objects have class null.
4277 null.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004278 __ LoadRoot(tos, Heap::kNullValueRootIndex);
4279 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004280
4281 // All done.
4282 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004283}
4284
4285
ager@chromium.org7c537e22008-10-16 08:43:32 +00004286void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004287 Register scratch = VirtualFrame::scratch0();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004288 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004289
4290 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004291 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004292 Register tos = frame_->PopToRegister(); // tos contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00004293 // if (object->IsSmi()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004294 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004295 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004296 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004297 __ CompareObjectType(tos, scratch, scratch, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004298 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004299 // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004300 __ ldr(tos, FieldMemOperand(tos, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004301 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004302 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004303}
4304
4305
ager@chromium.org7c537e22008-10-16 08:43:32 +00004306void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004307 Register scratch1 = VirtualFrame::scratch0();
4308 Register scratch2 = VirtualFrame::scratch1();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004309 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004310
4311 ASSERT(args->length() == 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004312 Load(args->at(0)); // Load the object.
4313 Load(args->at(1)); // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004314 Register value = frame_->PopToRegister();
4315 Register object = frame_->PopToRegister(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004316 // if (object->IsSmi()) return object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004317 __ tst(object, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004318 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004319 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004320 __ CompareObjectType(object, scratch1, scratch1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004321 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004322 // Store the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004323 __ str(value, FieldMemOperand(object, JSValue::kValueOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004324 // Update the write barrier.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004325 __ RecordWrite(object,
4326 Operand(JSValue::kValueOffset - kHeapObjectTag),
4327 scratch1,
4328 scratch2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004329 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004330 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004331 frame_->EmitPush(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004332}
4333
4334
ager@chromium.org7c537e22008-10-16 08:43:32 +00004335void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004336 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004337 Load(args->at(0));
4338 Register reg = frame_->PopToRegister();
4339 __ tst(reg, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004340 cc_reg_ = eq;
4341}
4342
4343
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004344void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
4345 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
4346 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004347#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004348 if (ShouldGenerateLog(args->at(0))) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004349 Load(args->at(1));
4350 Load(args->at(2));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004351 frame_->CallRuntime(Runtime::kLog, 2);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004352 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004353#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004354 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004355}
4356
4357
ager@chromium.org7c537e22008-10-16 08:43:32 +00004358void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004359 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004360 Load(args->at(0));
4361 Register reg = frame_->PopToRegister();
4362 __ tst(reg, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004363 cc_reg_ = eq;
4364}
4365
4366
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004367// Generates the Math.pow method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004368void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
4369 ASSERT(args->length() == 2);
4370 Load(args->at(0));
4371 Load(args->at(1));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004372
4373 if (!CpuFeatures::IsSupported(VFP3)) {
4374 frame_->CallRuntime(Runtime::kMath_pow, 2);
4375 frame_->EmitPush(r0);
4376 } else {
4377 CpuFeatures::Scope scope(VFP3);
4378 JumpTarget runtime, done;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004379 Label exponent_nonsmi, base_nonsmi, powi, not_minus_half, allocate_return;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004380
4381 Register scratch1 = VirtualFrame::scratch0();
4382 Register scratch2 = VirtualFrame::scratch1();
4383
4384 // Get base and exponent to registers.
4385 Register exponent = frame_->PopToRegister();
4386 Register base = frame_->PopToRegister(exponent);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004387 Register heap_number_map = no_reg;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004388
4389 // Set the frame for the runtime jump target. The code below jumps to the
4390 // jump target label so the frame needs to be established before that.
4391 ASSERT(runtime.entry_frame() == NULL);
4392 runtime.set_entry_frame(frame_);
4393
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004394 __ BranchOnNotSmi(exponent, &exponent_nonsmi);
4395 __ BranchOnNotSmi(base, &base_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004396
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004397 heap_number_map = r6;
4398 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4399
4400 // Exponent is a smi and base is a smi. Get the smi value into vfp register
4401 // d1.
4402 __ SmiToDoubleVFPRegister(base, d1, scratch1, s0);
4403 __ b(&powi);
4404
4405 __ bind(&base_nonsmi);
4406 // Exponent is smi and base is non smi. Get the double value from the base
4407 // into vfp register d1.
4408 __ ObjectToDoubleVFPRegister(base, d1,
4409 scratch1, scratch2, heap_number_map, s0,
4410 runtime.entry_label());
4411
4412 __ bind(&powi);
4413
4414 // Load 1.0 into d0.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004415 __ vmov(d0, 1.0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004416
4417 // Get the absolute untagged value of the exponent and use that for the
4418 // calculation.
4419 __ mov(scratch1, Operand(exponent, ASR, kSmiTagSize), SetCC);
4420 __ rsb(scratch1, scratch1, Operand(0), LeaveCC, mi); // Negate if negative.
4421 __ vmov(d2, d0, mi); // 1.0 needed in d2 later if exponent is negative.
4422
4423 // Run through all the bits in the exponent. The result is calculated in d0
4424 // and d1 holds base^(bit^2).
4425 Label more_bits;
4426 __ bind(&more_bits);
4427 __ mov(scratch1, Operand(scratch1, LSR, 1), SetCC);
4428 __ vmul(d0, d0, d1, cs); // Multiply with base^(bit^2) if bit is set.
4429 __ vmul(d1, d1, d1, ne); // Don't bother calculating next d1 if done.
4430 __ b(ne, &more_bits);
4431
4432 // If exponent is positive we are done.
4433 __ cmp(exponent, Operand(0));
4434 __ b(ge, &allocate_return);
4435
4436 // If exponent is negative result is 1/result (d2 already holds 1.0 in that
4437 // case). However if d0 has reached infinity this will not provide the
4438 // correct result, so call runtime if that is the case.
4439 __ mov(scratch2, Operand(0x7FF00000));
4440 __ mov(scratch1, Operand(0));
4441 __ vmov(d1, scratch1, scratch2); // Load infinity into d1.
4442 __ vcmp(d0, d1);
4443 __ vmrs(pc);
4444 runtime.Branch(eq); // d0 reached infinity.
4445 __ vdiv(d0, d2, d0);
4446 __ b(&allocate_return);
4447
4448 __ bind(&exponent_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004449 // Special handling of raising to the power of -0.5 and 0.5. First check
4450 // that the value is a heap number and that the lower bits (which for both
4451 // values are zero).
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004452 heap_number_map = r6;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004453 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4454 __ ldr(scratch1, FieldMemOperand(exponent, HeapObject::kMapOffset));
4455 __ ldr(scratch2, FieldMemOperand(exponent, HeapNumber::kMantissaOffset));
4456 __ cmp(scratch1, heap_number_map);
4457 runtime.Branch(ne);
4458 __ tst(scratch2, scratch2);
4459 runtime.Branch(ne);
4460
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004461 // Load the higher bits (which contains the floating point exponent).
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004462 __ ldr(scratch1, FieldMemOperand(exponent, HeapNumber::kExponentOffset));
4463
4464 // Compare exponent with -0.5.
4465 __ cmp(scratch1, Operand(0xbfe00000));
4466 __ b(ne, &not_minus_half);
4467
4468 // Get the double value from the base into vfp register d0.
4469 __ ObjectToDoubleVFPRegister(base, d0,
4470 scratch1, scratch2, heap_number_map, s0,
4471 runtime.entry_label(),
4472 AVOID_NANS_AND_INFINITIES);
4473
4474 // Load 1.0 into d2.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004475 __ vmov(d2, 1.0);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004476
4477 // Calculate the reciprocal of the square root. 1/sqrt(x) = sqrt(1/x).
4478 __ vdiv(d0, d2, d0);
4479 __ vsqrt(d0, d0);
4480
4481 __ b(&allocate_return);
4482
4483 __ bind(&not_minus_half);
4484 // Compare exponent with 0.5.
4485 __ cmp(scratch1, Operand(0x3fe00000));
4486 runtime.Branch(ne);
4487
4488 // Get the double value from the base into vfp register d0.
4489 __ ObjectToDoubleVFPRegister(base, d0,
4490 scratch1, scratch2, heap_number_map, s0,
4491 runtime.entry_label(),
4492 AVOID_NANS_AND_INFINITIES);
4493 __ vsqrt(d0, d0);
4494
4495 __ bind(&allocate_return);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004496 Register scratch3 = r5;
4497 __ AllocateHeapNumberWithValue(scratch3, d0, scratch1, scratch2,
4498 heap_number_map, runtime.entry_label());
4499 __ mov(base, scratch3);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004500 done.Jump();
4501
4502 runtime.Bind();
4503
4504 // Push back the arguments again for the runtime call.
4505 frame_->EmitPush(base);
4506 frame_->EmitPush(exponent);
4507 frame_->CallRuntime(Runtime::kMath_pow, 2);
4508 __ Move(base, r0);
4509
4510 done.Bind();
4511 frame_->EmitPush(base);
4512 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004513}
4514
4515
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004516// Generates the Math.sqrt method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004517void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
4518 ASSERT(args->length() == 1);
4519 Load(args->at(0));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004520
4521 if (!CpuFeatures::IsSupported(VFP3)) {
4522 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4523 frame_->EmitPush(r0);
4524 } else {
4525 CpuFeatures::Scope scope(VFP3);
4526 JumpTarget runtime, done;
4527
4528 Register scratch1 = VirtualFrame::scratch0();
4529 Register scratch2 = VirtualFrame::scratch1();
4530
4531 // Get the value from the frame.
4532 Register tos = frame_->PopToRegister();
4533
4534 // Set the frame for the runtime jump target. The code below jumps to the
4535 // jump target label so the frame needs to be established before that.
4536 ASSERT(runtime.entry_frame() == NULL);
4537 runtime.set_entry_frame(frame_);
4538
4539 Register heap_number_map = r6;
4540 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4541
4542 // Get the double value from the heap number into vfp register d0.
4543 __ ObjectToDoubleVFPRegister(tos, d0,
4544 scratch1, scratch2, heap_number_map, s0,
4545 runtime.entry_label());
4546
4547 // Calculate the square root of d0 and place result in a heap number object.
4548 __ vsqrt(d0, d0);
4549 __ AllocateHeapNumberWithValue(
4550 tos, d0, scratch1, scratch2, heap_number_map, runtime.entry_label());
4551 done.Jump();
4552
4553 runtime.Bind();
4554 // Push back the argument again for the runtime call.
4555 frame_->EmitPush(tos);
4556 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4557 __ Move(tos, r0);
4558
4559 done.Bind();
4560 frame_->EmitPush(tos);
4561 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004562}
4563
4564
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004565class DeferredStringCharCodeAt : public DeferredCode {
4566 public:
4567 DeferredStringCharCodeAt(Register object,
4568 Register index,
4569 Register scratch,
4570 Register result)
4571 : result_(result),
4572 char_code_at_generator_(object,
4573 index,
4574 scratch,
4575 result,
4576 &need_conversion_,
4577 &need_conversion_,
4578 &index_out_of_range_,
4579 STRING_INDEX_IS_NUMBER) {}
4580
4581 StringCharCodeAtGenerator* fast_case_generator() {
4582 return &char_code_at_generator_;
4583 }
4584
4585 virtual void Generate() {
4586 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4587 char_code_at_generator_.GenerateSlow(masm(), call_helper);
4588
4589 __ bind(&need_conversion_);
4590 // Move the undefined value into the result register, which will
4591 // trigger conversion.
4592 __ LoadRoot(result_, Heap::kUndefinedValueRootIndex);
4593 __ jmp(exit_label());
4594
4595 __ bind(&index_out_of_range_);
4596 // When the index is out of range, the spec requires us to return
4597 // NaN.
4598 __ LoadRoot(result_, Heap::kNanValueRootIndex);
4599 __ jmp(exit_label());
4600 }
4601
4602 private:
4603 Register result_;
4604
4605 Label need_conversion_;
4606 Label index_out_of_range_;
4607
4608 StringCharCodeAtGenerator char_code_at_generator_;
4609};
4610
4611
4612// This generates code that performs a String.prototype.charCodeAt() call
4613// or returns a smi in order to trigger conversion.
4614void CodeGenerator::GenerateStringCharCodeAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004615 Comment(masm_, "[ GenerateStringCharCodeAt");
kasper.lund7276f142008-07-30 08:49:36 +00004616 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004617
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004618 Load(args->at(0));
4619 Load(args->at(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004620
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004621 Register index = frame_->PopToRegister();
4622 Register object = frame_->PopToRegister(index);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004623
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004624 // We need two extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004625 Register scratch = VirtualFrame::scratch0();
4626 Register result = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004627
4628 DeferredStringCharCodeAt* deferred =
4629 new DeferredStringCharCodeAt(object,
4630 index,
4631 scratch,
4632 result);
4633 deferred->fast_case_generator()->GenerateFast(masm_);
4634 deferred->BindExit();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004635 frame_->EmitPush(result);
kasper.lund7276f142008-07-30 08:49:36 +00004636}
4637
4638
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004639class DeferredStringCharFromCode : public DeferredCode {
4640 public:
4641 DeferredStringCharFromCode(Register code,
4642 Register result)
4643 : char_from_code_generator_(code, result) {}
4644
4645 StringCharFromCodeGenerator* fast_case_generator() {
4646 return &char_from_code_generator_;
4647 }
4648
4649 virtual void Generate() {
4650 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4651 char_from_code_generator_.GenerateSlow(masm(), call_helper);
4652 }
4653
4654 private:
4655 StringCharFromCodeGenerator char_from_code_generator_;
4656};
4657
4658
4659// Generates code for creating a one-char string from a char code.
4660void CodeGenerator::GenerateStringCharFromCode(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004661 Comment(masm_, "[ GenerateStringCharFromCode");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004662 ASSERT(args->length() == 1);
4663
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004664 Load(args->at(0));
4665
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004666 Register result = frame_->GetTOSRegister();
4667 Register code = frame_->PopToRegister(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004668
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004669 DeferredStringCharFromCode* deferred = new DeferredStringCharFromCode(
4670 code, result);
4671 deferred->fast_case_generator()->GenerateFast(masm_);
4672 deferred->BindExit();
4673 frame_->EmitPush(result);
4674}
4675
4676
4677class DeferredStringCharAt : public DeferredCode {
4678 public:
4679 DeferredStringCharAt(Register object,
4680 Register index,
4681 Register scratch1,
4682 Register scratch2,
4683 Register result)
4684 : result_(result),
4685 char_at_generator_(object,
4686 index,
4687 scratch1,
4688 scratch2,
4689 result,
4690 &need_conversion_,
4691 &need_conversion_,
4692 &index_out_of_range_,
4693 STRING_INDEX_IS_NUMBER) {}
4694
4695 StringCharAtGenerator* fast_case_generator() {
4696 return &char_at_generator_;
4697 }
4698
4699 virtual void Generate() {
4700 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4701 char_at_generator_.GenerateSlow(masm(), call_helper);
4702
4703 __ bind(&need_conversion_);
4704 // Move smi zero into the result register, which will trigger
4705 // conversion.
4706 __ mov(result_, Operand(Smi::FromInt(0)));
4707 __ jmp(exit_label());
4708
4709 __ bind(&index_out_of_range_);
4710 // When the index is out of range, the spec requires us to return
4711 // the empty string.
4712 __ LoadRoot(result_, Heap::kEmptyStringRootIndex);
4713 __ jmp(exit_label());
4714 }
4715
4716 private:
4717 Register result_;
4718
4719 Label need_conversion_;
4720 Label index_out_of_range_;
4721
4722 StringCharAtGenerator char_at_generator_;
4723};
4724
4725
4726// This generates code that performs a String.prototype.charAt() call
4727// or returns a smi in order to trigger conversion.
4728void CodeGenerator::GenerateStringCharAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004729 Comment(masm_, "[ GenerateStringCharAt");
4730 ASSERT(args->length() == 2);
4731
4732 Load(args->at(0));
4733 Load(args->at(1));
4734
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004735 Register index = frame_->PopToRegister();
4736 Register object = frame_->PopToRegister(index);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004737
4738 // We need three extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004739 Register scratch1 = VirtualFrame::scratch0();
4740 Register scratch2 = VirtualFrame::scratch1();
4741 // Use r6 without notifying the virtual frame.
4742 Register result = r6;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004743
4744 DeferredStringCharAt* deferred =
4745 new DeferredStringCharAt(object,
4746 index,
4747 scratch1,
4748 scratch2,
4749 result);
4750 deferred->fast_case_generator()->GenerateFast(masm_);
4751 deferred->BindExit();
ager@chromium.orgac091b72010-05-05 07:34:42 +00004752 frame_->EmitPush(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004753}
4754
4755
ager@chromium.org7c537e22008-10-16 08:43:32 +00004756void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004757 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004758 Load(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004759 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004760 // We need the CC bits to come out as not_equal in the case where the
4761 // object is a smi. This can't be done with the usual test opcode so
4762 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004763 Register possible_array = frame_->PopToRegister();
4764 Register scratch = VirtualFrame::scratch0();
4765 __ and_(scratch, possible_array, Operand(kSmiTagMask));
4766 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004767 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004768 // It is a heap object - get the map. Check if the object is a JS array.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004769 __ CompareObjectType(possible_array, scratch, scratch, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004770 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004771 cc_reg_ = eq;
4772}
4773
4774
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004775void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004776 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004777 Load(args->at(0));
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004778 JumpTarget answer;
4779 // We need the CC bits to come out as not_equal in the case where the
4780 // object is a smi. This can't be done with the usual test opcode so
4781 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004782 Register possible_regexp = frame_->PopToRegister();
4783 Register scratch = VirtualFrame::scratch0();
4784 __ and_(scratch, possible_regexp, Operand(kSmiTagMask));
4785 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004786 answer.Branch(ne);
4787 // It is a heap object - get the map. Check if the object is a regexp.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004788 __ CompareObjectType(possible_regexp, scratch, scratch, JS_REGEXP_TYPE);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004789 answer.Bind();
4790 cc_reg_ = eq;
4791}
4792
4793
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004794void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
4795 // This generates a fast version of:
4796 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004797 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004798 Load(args->at(0));
4799 Register possible_object = frame_->PopToRegister();
4800 __ tst(possible_object, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004801 false_target()->Branch(eq);
4802
4803 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004804 __ cmp(possible_object, ip);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004805 true_target()->Branch(eq);
4806
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004807 Register map_reg = VirtualFrame::scratch0();
4808 __ ldr(map_reg, FieldMemOperand(possible_object, HeapObject::kMapOffset));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004809 // Undetectable objects behave like undefined when tested with typeof.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004810 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4811 __ tst(possible_object, Operand(1 << Map::kIsUndetectable));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004812 false_target()->Branch(ne);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004813
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004814 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4815 __ cmp(possible_object, Operand(FIRST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004816 false_target()->Branch(lt);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004817 __ cmp(possible_object, Operand(LAST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004818 cc_reg_ = le;
4819}
4820
4821
ricow@chromium.org4980dff2010-07-19 08:33:45 +00004822void CodeGenerator::GenerateIsSpecObject(ZoneList<Expression*>* args) {
4823 // This generates a fast version of:
4824 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp' ||
4825 // typeof(arg) == function).
4826 // It includes undetectable objects (as opposed to IsObject).
4827 ASSERT(args->length() == 1);
4828 Load(args->at(0));
4829 Register value = frame_->PopToRegister();
4830 __ tst(value, Operand(kSmiTagMask));
4831 false_target()->Branch(eq);
4832 // Check that this is an object.
4833 __ ldr(value, FieldMemOperand(value, HeapObject::kMapOffset));
4834 __ ldrb(value, FieldMemOperand(value, Map::kInstanceTypeOffset));
4835 __ cmp(value, Operand(FIRST_JS_OBJECT_TYPE));
4836 cc_reg_ = ge;
4837}
4838
4839
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004840// Deferred code to check whether the String JavaScript object is safe for using
4841// default value of. This code is called after the bit caching this information
4842// in the map has been checked with the map for the object in the map_result_
4843// register. On return the register map_result_ contains 1 for true and 0 for
4844// false.
4845class DeferredIsStringWrapperSafeForDefaultValueOf : public DeferredCode {
4846 public:
4847 DeferredIsStringWrapperSafeForDefaultValueOf(Register object,
4848 Register map_result,
4849 Register scratch1,
4850 Register scratch2)
4851 : object_(object),
4852 map_result_(map_result),
4853 scratch1_(scratch1),
4854 scratch2_(scratch2) { }
4855
4856 virtual void Generate() {
4857 Label false_result;
4858
4859 // Check that map is loaded as expected.
4860 if (FLAG_debug_code) {
4861 __ ldr(ip, FieldMemOperand(object_, HeapObject::kMapOffset));
4862 __ cmp(map_result_, ip);
4863 __ Assert(eq, "Map not in expected register");
4864 }
4865
4866 // Check for fast case object. Generate false result for slow case object.
4867 __ ldr(scratch1_, FieldMemOperand(object_, JSObject::kPropertiesOffset));
4868 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
4869 __ LoadRoot(ip, Heap::kHashTableMapRootIndex);
4870 __ cmp(scratch1_, ip);
4871 __ b(eq, &false_result);
4872
4873 // Look for valueOf symbol in the descriptor array, and indicate false if
4874 // found. The type is not checked, so if it is a transition it is a false
4875 // negative.
4876 __ ldr(map_result_,
4877 FieldMemOperand(map_result_, Map::kInstanceDescriptorsOffset));
4878 __ ldr(scratch2_, FieldMemOperand(map_result_, FixedArray::kLengthOffset));
4879 // map_result_: descriptor array
4880 // scratch2_: length of descriptor array
4881 // Calculate the end of the descriptor array.
4882 STATIC_ASSERT(kSmiTag == 0);
4883 STATIC_ASSERT(kSmiTagSize == 1);
4884 STATIC_ASSERT(kPointerSize == 4);
4885 __ add(scratch1_,
4886 map_result_,
4887 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4888 __ add(scratch1_,
4889 scratch1_,
4890 Operand(scratch2_, LSL, kPointerSizeLog2 - kSmiTagSize));
4891
4892 // Calculate location of the first key name.
4893 __ add(map_result_,
4894 map_result_,
4895 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
4896 DescriptorArray::kFirstIndex * kPointerSize));
4897 // Loop through all the keys in the descriptor array. If one of these is the
4898 // symbol valueOf the result is false.
4899 Label entry, loop;
4900 // The use of ip to store the valueOf symbol asumes that it is not otherwise
4901 // used in the loop below.
4902 __ mov(ip, Operand(Factory::value_of_symbol()));
4903 __ jmp(&entry);
4904 __ bind(&loop);
4905 __ ldr(scratch2_, MemOperand(map_result_, 0));
4906 __ cmp(scratch2_, ip);
4907 __ b(eq, &false_result);
4908 __ add(map_result_, map_result_, Operand(kPointerSize));
4909 __ bind(&entry);
4910 __ cmp(map_result_, Operand(scratch1_));
4911 __ b(ne, &loop);
4912
4913 // Reload map as register map_result_ was used as temporary above.
4914 __ ldr(map_result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4915
4916 // If a valueOf property is not found on the object check that it's
4917 // prototype is the un-modified String prototype. If not result is false.
4918 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kPrototypeOffset));
4919 __ tst(scratch1_, Operand(kSmiTagMask));
4920 __ b(eq, &false_result);
4921 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
4922 __ ldr(scratch2_,
4923 CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX));
4924 __ ldr(scratch2_,
4925 FieldMemOperand(scratch2_, GlobalObject::kGlobalContextOffset));
4926 __ ldr(scratch2_,
4927 CodeGenerator::ContextOperand(
4928 scratch2_, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
4929 __ cmp(scratch1_, scratch2_);
4930 __ b(ne, &false_result);
4931
4932 // Set the bit in the map to indicate that it has been checked safe for
4933 // default valueOf and set true result.
4934 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
4935 __ orr(scratch1_,
4936 scratch1_,
4937 Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
4938 __ str(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
4939 __ mov(map_result_, Operand(1));
4940 __ jmp(exit_label());
4941 __ bind(&false_result);
4942 // Set false result.
4943 __ mov(map_result_, Operand(0));
4944 }
4945
4946 private:
4947 Register object_;
4948 Register map_result_;
4949 Register scratch1_;
4950 Register scratch2_;
4951};
4952
4953
4954void CodeGenerator::GenerateIsStringWrapperSafeForDefaultValueOf(
4955 ZoneList<Expression*>* args) {
4956 ASSERT(args->length() == 1);
4957 Load(args->at(0));
4958 Register obj = frame_->PopToRegister(); // Pop the string wrapper.
4959 if (FLAG_debug_code) {
4960 __ AbortIfSmi(obj);
4961 }
4962
4963 // Check whether this map has already been checked to be safe for default
4964 // valueOf.
4965 Register map_result = VirtualFrame::scratch0();
4966 __ ldr(map_result, FieldMemOperand(obj, HeapObject::kMapOffset));
4967 __ ldrb(ip, FieldMemOperand(map_result, Map::kBitField2Offset));
4968 __ tst(ip, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
4969 true_target()->Branch(ne);
4970
4971 // We need an additional two scratch registers for the deferred code.
4972 Register scratch1 = VirtualFrame::scratch1();
4973 // Use r6 without notifying the virtual frame.
4974 Register scratch2 = r6;
4975
4976 DeferredIsStringWrapperSafeForDefaultValueOf* deferred =
4977 new DeferredIsStringWrapperSafeForDefaultValueOf(
4978 obj, map_result, scratch1, scratch2);
4979 deferred->Branch(eq);
4980 deferred->BindExit();
4981 __ tst(map_result, Operand(map_result));
4982 cc_reg_ = ne;
4983}
4984
4985
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004986void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
4987 // This generates a fast version of:
4988 // (%_ClassOf(arg) === 'Function')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004989 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004990 Load(args->at(0));
4991 Register possible_function = frame_->PopToRegister();
4992 __ tst(possible_function, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004993 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004994 Register map_reg = VirtualFrame::scratch0();
4995 Register scratch = VirtualFrame::scratch1();
4996 __ CompareObjectType(possible_function, map_reg, scratch, JS_FUNCTION_TYPE);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004997 cc_reg_ = eq;
4998}
4999
5000
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005001void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005002 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005003 Load(args->at(0));
5004 Register possible_undetectable = frame_->PopToRegister();
5005 __ tst(possible_undetectable, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005006 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005007 Register scratch = VirtualFrame::scratch0();
5008 __ ldr(scratch,
5009 FieldMemOperand(possible_undetectable, HeapObject::kMapOffset));
5010 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5011 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005012 cc_reg_ = ne;
5013}
5014
5015
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005016void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005017 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005018
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005019 Register scratch0 = VirtualFrame::scratch0();
5020 Register scratch1 = VirtualFrame::scratch1();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005021 // Get the frame pointer for the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005022 __ ldr(scratch0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005023
5024 // Skip the arguments adaptor frame if it exists.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005025 __ ldr(scratch1,
5026 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5027 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5028 __ ldr(scratch0,
5029 MemOperand(scratch0, StandardFrameConstants::kCallerFPOffset), eq);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005030
5031 // Check the marker in the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005032 __ ldr(scratch1,
5033 MemOperand(scratch0, StandardFrameConstants::kMarkerOffset));
5034 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005035 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005036}
5037
5038
ager@chromium.org7c537e22008-10-16 08:43:32 +00005039void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005040 ASSERT(args->length() == 0);
5041
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005042 Register tos = frame_->GetTOSRegister();
5043 Register scratch0 = VirtualFrame::scratch0();
5044 Register scratch1 = VirtualFrame::scratch1();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005045
lrn@chromium.org25156de2010-04-06 13:10:27 +00005046 // Check if the calling frame is an arguments adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005047 __ ldr(scratch0,
5048 MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5049 __ ldr(scratch1,
5050 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5051 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5052
5053 // Get the number of formal parameters.
5054 __ mov(tos, Operand(Smi::FromInt(scope()->num_parameters())), LeaveCC, ne);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005055
5056 // Arguments adaptor case: Read the arguments length from the
5057 // adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005058 __ ldr(tos,
5059 MemOperand(scratch0, ArgumentsAdaptorFrameConstants::kLengthOffset),
5060 eq);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005061
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005062 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005063}
5064
5065
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00005066void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005067 ASSERT(args->length() == 1);
5068
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00005069 // Satisfy contract with ArgumentsAccessStub:
5070 // Load the key into r1 and the formal parameters count into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005071 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005072 frame_->PopToR1();
5073 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005074 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005075
5076 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005077 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005078 frame_->CallStub(&stub, 0);
5079 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005080}
5081
5082
ager@chromium.org357bf652010-04-12 11:30:10 +00005083void CodeGenerator::GenerateRandomHeapNumber(
5084 ZoneList<Expression*>* args) {
5085 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005086 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00005087
5088 Label slow_allocate_heapnumber;
5089 Label heapnumber_allocated;
5090
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005091 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
5092 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00005093 __ jmp(&heapnumber_allocated);
5094
5095 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00005096 // Allocate a heap number.
5097 __ CallRuntime(Runtime::kNumberAlloc, 0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005098 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00005099
5100 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005101
5102 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
5103 // by computing:
5104 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
5105 if (CpuFeatures::IsSupported(VFP3)) {
5106 __ PrepareCallCFunction(0, r1);
5107 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
5108
5109 CpuFeatures::Scope scope(VFP3);
5110 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
5111 // Create this constant using mov/orr to avoid PC relative load.
5112 __ mov(r1, Operand(0x41000000));
5113 __ orr(r1, r1, Operand(0x300000));
5114 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
5115 __ vmov(d7, r0, r1);
5116 // Move 0x4130000000000000 to VFP.
5117 __ mov(r0, Operand(0));
5118 __ vmov(d8, r0, r1);
5119 // Subtract and store the result in the heap number.
5120 __ vsub(d7, d7, d8);
5121 __ sub(r0, r4, Operand(kHeapObjectTag));
5122 __ vstr(d7, r0, HeapNumber::kValueOffset);
5123 frame_->EmitPush(r4);
5124 } else {
5125 __ mov(r0, Operand(r4));
5126 __ PrepareCallCFunction(1, r1);
5127 __ CallCFunction(
5128 ExternalReference::fill_heap_number_with_random_function(), 1);
5129 frame_->EmitPush(r0);
5130 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005131}
5132
5133
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005134void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
5135 ASSERT_EQ(2, args->length());
5136
5137 Load(args->at(0));
5138 Load(args->at(1));
5139
ager@chromium.org5c838252010-02-19 08:53:10 +00005140 StringAddStub stub(NO_STRING_ADD_FLAGS);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005141 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005142 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005143 frame_->EmitPush(r0);
5144}
5145
5146
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005147void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
5148 ASSERT_EQ(3, args->length());
5149
5150 Load(args->at(0));
5151 Load(args->at(1));
5152 Load(args->at(2));
5153
ager@chromium.org5c838252010-02-19 08:53:10 +00005154 SubStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005155 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005156 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005157 frame_->EmitPush(r0);
5158}
5159
5160
5161void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
5162 ASSERT_EQ(2, args->length());
5163
5164 Load(args->at(0));
5165 Load(args->at(1));
5166
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005167 StringCompareStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005168 frame_->SpillAll();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005169 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005170 frame_->EmitPush(r0);
5171}
5172
5173
5174void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
5175 ASSERT_EQ(4, args->length());
5176
5177 Load(args->at(0));
5178 Load(args->at(1));
5179 Load(args->at(2));
5180 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005181 RegExpExecStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005182 frame_->SpillAll();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005183 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005184 frame_->EmitPush(r0);
5185}
5186
5187
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005188void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
5189 // No stub. This code only occurs a few times in regexp.js.
5190 const int kMaxInlineLength = 100;
5191 ASSERT_EQ(3, args->length());
5192 Load(args->at(0)); // Size of array, smi.
5193 Load(args->at(1)); // "index" property value.
5194 Load(args->at(2)); // "input" property value.
5195 {
5196 VirtualFrame::SpilledScope spilled_scope(frame_);
5197 Label slowcase;
5198 Label done;
5199 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
5200 STATIC_ASSERT(kSmiTag == 0);
5201 STATIC_ASSERT(kSmiTagSize == 1);
5202 __ tst(r1, Operand(kSmiTagMask));
5203 __ b(ne, &slowcase);
5204 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
5205 __ b(hi, &slowcase);
5206 // Smi-tagging is equivalent to multiplying by 2.
5207 // Allocate RegExpResult followed by FixedArray with size in ebx.
5208 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
5209 // Elements: [Map][Length][..elements..]
5210 // Size of JSArray with two in-object properties and the header of a
5211 // FixedArray.
5212 int objects_size =
5213 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
5214 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
5215 __ add(r2, r5, Operand(objects_size));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005216 __ AllocateInNewSpace(
5217 r2, // In: Size, in words.
5218 r0, // Out: Start of allocation (tagged).
5219 r3, // Scratch register.
5220 r4, // Scratch register.
5221 &slowcase,
5222 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005223 // r0: Start of allocated area, object-tagged.
5224 // r1: Number of elements in array, as smi.
5225 // r5: Number of elements, untagged.
5226
5227 // Set JSArray map to global.regexp_result_map().
5228 // Set empty properties FixedArray.
5229 // Set elements to point to FixedArray allocated right after the JSArray.
5230 // Interleave operations for better latency.
5231 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
5232 __ add(r3, r0, Operand(JSRegExpResult::kSize));
5233 __ mov(r4, Operand(Factory::empty_fixed_array()));
5234 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5235 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
5236 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
5237 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5238 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5239
5240 // Set input, index and length fields from arguments.
5241 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
5242 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
5243 __ add(sp, sp, Operand(kPointerSize));
5244 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
5245 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
5246
5247 // Fill out the elements FixedArray.
5248 // r0: JSArray, tagged.
5249 // r3: FixedArray, tagged.
5250 // r5: Number of elements in array, untagged.
5251
5252 // Set map.
5253 __ mov(r2, Operand(Factory::fixed_array_map()));
5254 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
5255 // Set FixedArray length.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005256 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
5257 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005258 // Fill contents of fixed-array with the-hole.
5259 __ mov(r2, Operand(Factory::the_hole_value()));
5260 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5261 // Fill fixed array elements with hole.
5262 // r0: JSArray, tagged.
5263 // r2: the hole.
5264 // r3: Start of elements in FixedArray.
5265 // r5: Number of elements to fill.
5266 Label loop;
5267 __ tst(r5, Operand(r5));
5268 __ bind(&loop);
5269 __ b(le, &done); // Jump if r1 is negative or zero.
5270 __ sub(r5, r5, Operand(1), SetCC);
5271 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
5272 __ jmp(&loop);
5273
5274 __ bind(&slowcase);
5275 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
5276
5277 __ bind(&done);
5278 }
5279 frame_->Forget(3);
5280 frame_->EmitPush(r0);
5281}
5282
5283
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00005284void CodeGenerator::GenerateRegExpCloneResult(ZoneList<Expression*>* args) {
5285 ASSERT_EQ(1, args->length());
5286
5287 Load(args->at(0));
5288 frame_->PopToR0();
5289 {
5290 VirtualFrame::SpilledScope spilled_scope(frame_);
5291
5292 Label done;
5293 Label call_runtime;
5294 __ BranchOnSmi(r0, &done);
5295
5296 // Load JSRegExp map into r1. Check that argument object has this map.
5297 // Arguments to this function should be results of calling RegExp exec,
5298 // which is either an unmodified JSRegExpResult or null. Anything not having
5299 // the unmodified JSRegExpResult map is returned unmodified.
5300 // This also ensures that elements are fast.
5301
5302 __ ldr(r1, ContextOperand(cp, Context::GLOBAL_INDEX));
5303 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalContextOffset));
5304 __ ldr(r1, ContextOperand(r1, Context::REGEXP_RESULT_MAP_INDEX));
5305 __ ldr(ip, FieldMemOperand(r0, HeapObject::kMapOffset));
5306 __ cmp(r1, Operand(ip));
5307 __ b(ne, &done);
5308
ricow@chromium.org65fae842010-08-25 15:26:24 +00005309 if (FLAG_debug_code) {
5310 __ LoadRoot(r2, Heap::kEmptyFixedArrayRootIndex);
5311 __ ldr(ip, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5312 __ cmp(ip, r2);
5313 __ Check(eq, "JSRegExpResult: default map but non-empty properties.");
5314 }
5315
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00005316 // All set, copy the contents to a new object.
5317 __ AllocateInNewSpace(JSRegExpResult::kSize,
5318 r2,
5319 r3,
5320 r4,
5321 &call_runtime,
5322 NO_ALLOCATION_FLAGS);
5323 // Store RegExpResult map as map of allocated object.
5324 ASSERT(JSRegExpResult::kSize == 6 * kPointerSize);
5325 // Copy all fields (map is already in r1) from (untagged) r0 to r2.
5326 // Change map of elements array (ends up in r4) to be a FixedCOWArray.
5327 __ bic(r0, r0, Operand(kHeapObjectTagMask));
5328 __ ldm(ib, r0, r3.bit() | r4.bit() | r5.bit() | r6.bit() | r7.bit());
5329 __ stm(ia, r2,
5330 r1.bit() | r3.bit() | r4.bit() | r5.bit() | r6.bit() | r7.bit());
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00005331 ASSERT(JSRegExp::kElementsOffset == 2 * kPointerSize);
5332 // Check whether elements array is empty fixed array, and otherwise make
5333 // it copy-on-write (it never should be empty unless someone is messing
5334 // with the arguments to the runtime function).
5335 __ LoadRoot(ip, Heap::kEmptyFixedArrayRootIndex);
5336 __ add(r0, r2, Operand(kHeapObjectTag)); // Tag result and move it to r0.
5337 __ cmp(r4, ip);
5338 __ b(eq, &done);
5339 __ LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
5340 __ str(ip, FieldMemOperand(r4, HeapObject::kMapOffset));
5341 __ b(&done);
5342 __ bind(&call_runtime);
5343 __ push(r0);
5344 __ CallRuntime(Runtime::kRegExpCloneResult, 1);
5345 __ bind(&done);
5346 }
5347 frame_->EmitPush(r0);
5348}
5349
5350
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005351class DeferredSearchCache: public DeferredCode {
5352 public:
5353 DeferredSearchCache(Register dst, Register cache, Register key)
5354 : dst_(dst), cache_(cache), key_(key) {
5355 set_comment("[ DeferredSearchCache");
5356 }
5357
5358 virtual void Generate();
5359
5360 private:
5361 Register dst_, cache_, key_;
5362};
5363
5364
5365void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005366 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005367 __ CallRuntime(Runtime::kGetFromCache, 2);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005368 __ Move(dst_, r0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005369}
5370
5371
5372void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
5373 ASSERT_EQ(2, args->length());
5374
5375 ASSERT_NE(NULL, args->at(0)->AsLiteral());
5376 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
5377
5378 Handle<FixedArray> jsfunction_result_caches(
5379 Top::global_context()->jsfunction_result_caches());
5380 if (jsfunction_result_caches->length() <= cache_id) {
5381 __ Abort("Attempt to use undefined cache.");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005382 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005383 return;
5384 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005385
5386 Load(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005387
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005388 frame_->PopToR1();
5389 frame_->SpillAll();
5390 Register key = r1; // Just poped to r1
5391 Register result = r0; // Free, as frame has just been spilled.
5392 Register scratch1 = VirtualFrame::scratch0();
5393 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005394
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005395 __ ldr(scratch1, ContextOperand(cp, Context::GLOBAL_INDEX));
5396 __ ldr(scratch1,
5397 FieldMemOperand(scratch1, GlobalObject::kGlobalContextOffset));
5398 __ ldr(scratch1,
5399 ContextOperand(scratch1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
5400 __ ldr(scratch1,
5401 FieldMemOperand(scratch1, FixedArray::OffsetOfElementAt(cache_id)));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005402
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005403 DeferredSearchCache* deferred =
5404 new DeferredSearchCache(result, scratch1, key);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005405
5406 const int kFingerOffset =
5407 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00005408 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005409 __ ldr(result, FieldMemOperand(scratch1, kFingerOffset));
5410 // result now holds finger offset as a smi.
5411 __ add(scratch2, scratch1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5412 // scratch2 now points to the start of fixed array elements.
5413 __ ldr(result,
5414 MemOperand(
5415 scratch2, result, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
5416 // Note side effect of PreIndex: scratch2 now points to the key of the pair.
5417 __ cmp(key, result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005418 deferred->Branch(ne);
5419
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005420 __ ldr(result, MemOperand(scratch2, kPointerSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005421
5422 deferred->BindExit();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005423 frame_->EmitPush(result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005424}
5425
5426
ager@chromium.org5c838252010-02-19 08:53:10 +00005427void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
5428 ASSERT_EQ(args->length(), 1);
5429
5430 // Load the argument on the stack and jump to the runtime.
5431 Load(args->at(0));
5432
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005433 NumberToStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005434 frame_->SpillAll();
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005435 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005436 frame_->EmitPush(r0);
5437}
5438
5439
ager@chromium.orgac091b72010-05-05 07:34:42 +00005440class DeferredSwapElements: public DeferredCode {
5441 public:
5442 DeferredSwapElements(Register object, Register index1, Register index2)
5443 : object_(object), index1_(index1), index2_(index2) {
5444 set_comment("[ DeferredSwapElements");
5445 }
5446
5447 virtual void Generate();
5448
5449 private:
5450 Register object_, index1_, index2_;
5451};
5452
5453
5454void DeferredSwapElements::Generate() {
5455 __ push(object_);
5456 __ push(index1_);
5457 __ push(index2_);
5458 __ CallRuntime(Runtime::kSwapElements, 3);
5459}
5460
5461
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005462void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
5463 Comment cmnt(masm_, "[ GenerateSwapElements");
5464
5465 ASSERT_EQ(3, args->length());
5466
5467 Load(args->at(0));
5468 Load(args->at(1));
5469 Load(args->at(2));
5470
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005471 VirtualFrame::SpilledScope spilled_scope(frame_);
5472
ager@chromium.orgac091b72010-05-05 07:34:42 +00005473 Register index2 = r2;
5474 Register index1 = r1;
5475 Register object = r0;
5476 Register tmp1 = r3;
5477 Register tmp2 = r4;
5478
5479 frame_->EmitPop(index2);
5480 frame_->EmitPop(index1);
5481 frame_->EmitPop(object);
5482
5483 DeferredSwapElements* deferred =
5484 new DeferredSwapElements(object, index1, index2);
5485
5486 // Fetch the map and check if array is in fast case.
5487 // Check that object doesn't require security checks and
5488 // has no indexed interceptor.
5489 __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE);
5490 deferred->Branch(lt);
5491 __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset));
5492 __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
5493 deferred->Branch(nz);
5494
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00005495 // Check the object's elements are in fast case and writable.
ager@chromium.orgac091b72010-05-05 07:34:42 +00005496 __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset));
5497 __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset));
5498 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5499 __ cmp(tmp2, ip);
5500 deferred->Branch(ne);
5501
5502 // Smi-tagging is equivalent to multiplying by 2.
5503 STATIC_ASSERT(kSmiTag == 0);
5504 STATIC_ASSERT(kSmiTagSize == 1);
5505
5506 // Check that both indices are smis.
5507 __ mov(tmp2, index1);
5508 __ orr(tmp2, tmp2, index2);
5509 __ tst(tmp2, Operand(kSmiTagMask));
5510 deferred->Branch(nz);
5511
5512 // Bring the offsets into the fixed array in tmp1 into index1 and
5513 // index2.
5514 __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5515 __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize));
5516 __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize));
5517
5518 // Swap elements.
5519 Register tmp3 = object;
5520 object = no_reg;
5521 __ ldr(tmp3, MemOperand(tmp1, index1));
5522 __ ldr(tmp2, MemOperand(tmp1, index2));
5523 __ str(tmp3, MemOperand(tmp1, index2));
5524 __ str(tmp2, MemOperand(tmp1, index1));
5525
5526 Label done;
5527 __ InNewSpace(tmp1, tmp2, eq, &done);
5528 // Possible optimization: do a check that both values are Smis
5529 // (or them and test against Smi mask.)
5530
5531 __ mov(tmp2, tmp1);
5532 RecordWriteStub recordWrite1(tmp1, index1, tmp3);
5533 __ CallStub(&recordWrite1);
5534
5535 RecordWriteStub recordWrite2(tmp2, index2, tmp3);
5536 __ CallStub(&recordWrite2);
5537
5538 __ bind(&done);
5539
5540 deferred->BindExit();
5541 __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex);
5542 frame_->EmitPush(tmp1);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005543}
5544
5545
ager@chromium.org357bf652010-04-12 11:30:10 +00005546void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
5547 Comment cmnt(masm_, "[ GenerateCallFunction");
5548
5549 ASSERT(args->length() >= 2);
5550
5551 int n_args = args->length() - 2; // for receiver and function.
5552 Load(args->at(0)); // receiver
5553 for (int i = 0; i < n_args; i++) {
5554 Load(args->at(i + 1));
5555 }
5556 Load(args->at(n_args + 1)); // function
5557 frame_->CallJSFunction(n_args);
5558 frame_->EmitPush(r0);
5559}
5560
5561
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005562void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
5563 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005564 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005565 if (CpuFeatures::IsSupported(VFP3)) {
5566 TranscendentalCacheStub stub(TranscendentalCache::SIN);
5567 frame_->SpillAllButCopyTOSToR0();
5568 frame_->CallStub(&stub, 1);
5569 } else {
5570 frame_->CallRuntime(Runtime::kMath_sin, 1);
5571 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005572 frame_->EmitPush(r0);
5573}
5574
5575
5576void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
5577 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005578 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005579 if (CpuFeatures::IsSupported(VFP3)) {
5580 TranscendentalCacheStub stub(TranscendentalCache::COS);
5581 frame_->SpillAllButCopyTOSToR0();
5582 frame_->CallStub(&stub, 1);
5583 } else {
5584 frame_->CallRuntime(Runtime::kMath_cos, 1);
5585 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005586 frame_->EmitPush(r0);
5587}
5588
5589
ager@chromium.org7c537e22008-10-16 08:43:32 +00005590void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005591 ASSERT(args->length() == 2);
5592
5593 // Load the two objects into registers and perform the comparison.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005594 Load(args->at(0));
5595 Load(args->at(1));
5596 Register lhs = frame_->PopToRegister();
5597 Register rhs = frame_->PopToRegister(lhs);
5598 __ cmp(lhs, rhs);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005599 cc_reg_ = eq;
5600}
5601
5602
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005603void CodeGenerator::GenerateIsRegExpEquivalent(ZoneList<Expression*>* args) {
5604 ASSERT(args->length() == 2);
5605
5606 // Load the two objects into registers and perform the comparison.
5607 Load(args->at(0));
5608 Load(args->at(1));
5609 Register right = frame_->PopToRegister();
5610 Register left = frame_->PopToRegister(right);
5611 Register tmp = frame_->scratch0();
5612 Register tmp2 = frame_->scratch1();
5613
5614 // Jumps to done must have the eq flag set if the test is successful
5615 // and clear if the test has failed.
5616 Label done;
5617
5618 // Fail if either is a non-HeapObject.
5619 __ cmp(left, Operand(right));
5620 __ b(eq, &done);
5621 __ and_(tmp, left, Operand(right));
5622 __ eor(tmp, tmp, Operand(kSmiTagMask));
5623 __ tst(tmp, Operand(kSmiTagMask));
5624 __ b(ne, &done);
5625 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
5626 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
5627 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
5628 __ b(ne, &done);
5629 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
5630 __ cmp(tmp, Operand(tmp2));
5631 __ b(ne, &done);
5632 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
5633 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
5634 __ cmp(tmp, tmp2);
5635 __ bind(&done);
5636 cc_reg_ = eq;
5637}
5638
5639
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00005640void CodeGenerator::GenerateHasCachedArrayIndex(ZoneList<Expression*>* args) {
5641 ASSERT(args->length() == 1);
5642 Load(args->at(0));
5643 Register value = frame_->PopToRegister();
5644 Register tmp = frame_->scratch0();
5645 __ ldr(tmp, FieldMemOperand(value, String::kHashFieldOffset));
5646 __ tst(tmp, Operand(String::kContainsCachedArrayIndexMask));
5647 cc_reg_ = eq;
5648}
5649
5650
5651void CodeGenerator::GenerateGetCachedArrayIndex(ZoneList<Expression*>* args) {
5652 ASSERT(args->length() == 1);
5653 Load(args->at(0));
5654 Register value = frame_->PopToRegister();
5655
5656 __ ldr(value, FieldMemOperand(value, String::kHashFieldOffset));
5657 __ IndexFromHash(value, value);
5658 frame_->EmitPush(value);
5659}
5660
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005661
ager@chromium.org7c537e22008-10-16 08:43:32 +00005662void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005663#ifdef DEBUG
5664 int original_height = frame_->height();
5665#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005666 if (CheckForInlineRuntimeCall(node)) {
5667 ASSERT((has_cc() && frame_->height() == original_height) ||
5668 (!has_cc() && frame_->height() == original_height + 1));
5669 return;
5670 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005671
5672 ZoneList<Expression*>* args = node->arguments();
5673 Comment cmnt(masm_, "[ CallRuntime");
5674 Runtime::Function* function = node->function();
5675
ager@chromium.org41826e72009-03-30 13:30:57 +00005676 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00005677 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00005678 // Push the builtins object found in the current global object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005679 Register scratch = VirtualFrame::scratch0();
5680 __ ldr(scratch, GlobalObject());
5681 Register builtins = frame_->GetTOSRegister();
5682 __ ldr(builtins, FieldMemOperand(scratch, GlobalObject::kBuiltinsOffset));
5683 frame_->EmitPush(builtins);
ager@chromium.org41826e72009-03-30 13:30:57 +00005684 }
mads.s.ager31e71382008-08-13 09:32:07 +00005685
ager@chromium.org41826e72009-03-30 13:30:57 +00005686 // Push the arguments ("left-to-right").
5687 int arg_count = args->length();
5688 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005689 Load(args->at(i));
ager@chromium.org41826e72009-03-30 13:30:57 +00005690 }
mads.s.ager31e71382008-08-13 09:32:07 +00005691
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005692 VirtualFrame::SpilledScope spilled_scope(frame_);
5693
ager@chromium.org41826e72009-03-30 13:30:57 +00005694 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005695 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00005696 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005697 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
5698 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005699 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005700 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005701 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00005702 } else {
5703 // Call the C runtime function.
5704 frame_->CallRuntime(function, arg_count);
5705 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005706 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00005707 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005708}
5709
5710
ager@chromium.org7c537e22008-10-16 08:43:32 +00005711void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005712#ifdef DEBUG
5713 int original_height = frame_->height();
5714#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005715 Comment cmnt(masm_, "[ UnaryOperation");
5716
5717 Token::Value op = node->op();
5718
5719 if (op == Token::NOT) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005720 LoadCondition(node->expression(), false_target(), true_target(), true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005721 // LoadCondition may (and usually does) leave a test and branch to
5722 // be emitted by the caller. In that case, negate the condition.
5723 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005724
5725 } else if (op == Token::DELETE) {
5726 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00005727 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005728 if (property != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005729 Load(property->obj());
5730 Load(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005731 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005732 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005733
mads.s.ager31e71382008-08-13 09:32:07 +00005734 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005735 Slot* slot = variable->slot();
5736 if (variable->is_global()) {
5737 LoadGlobal();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005738 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005739 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005740 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005741
5742 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
5743 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005744 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005745 frame_->EmitPush(Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005746 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005747 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005748 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005749 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005750 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005751 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005752
mads.s.ager31e71382008-08-13 09:32:07 +00005753 } else {
5754 // Default: Result of deleting non-global, not dynamically
5755 // introduced variables is false.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005756 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00005757 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005758
5759 } else {
5760 // Default: Result of deleting expressions is true.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005761 Load(node->expression()); // may have side-effects
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005762 frame_->Drop();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005763 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005764 }
5765
5766 } else if (op == Token::TYPEOF) {
5767 // Special case for loading the typeof expression; see comment on
5768 // LoadTypeofExpression().
5769 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005770 frame_->CallRuntime(Runtime::kTypeof, 1);
5771 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005772
5773 } else {
ricow@chromium.org65fae842010-08-25 15:26:24 +00005774 bool can_overwrite = node->expression()->ResultOverwriteAllowed();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005775 UnaryOverwriteMode overwrite =
5776 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
5777
5778 bool no_negative_zero = node->expression()->no_negative_zero();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005779 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005780 switch (op) {
5781 case Token::NOT:
5782 case Token::DELETE:
5783 case Token::TYPEOF:
5784 UNREACHABLE(); // handled above
5785 break;
5786
5787 case Token::SUB: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005788 frame_->PopToR0();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005789 GenericUnaryOpStub stub(
5790 Token::SUB,
5791 overwrite,
5792 no_negative_zero ? kIgnoreNegativeZero : kStrictNegativeZero);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005793 frame_->CallStub(&stub, 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005794 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005795 break;
5796 }
5797
5798 case Token::BIT_NOT: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005799 Register tos = frame_->PopToRegister();
5800 JumpTarget not_smi_label;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005801 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005802 // Smi check.
5803 __ tst(tos, Operand(kSmiTagMask));
5804 not_smi_label.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005805
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005806 __ mvn(tos, Operand(tos));
5807 __ bic(tos, tos, Operand(kSmiTagMask)); // Bit-clear inverted smi-tag.
5808 frame_->EmitPush(tos);
5809 // The fast case is the first to jump to the continue label, so it gets
5810 // to decide the virtual frame layout.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005811 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005812
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005813 not_smi_label.Bind();
5814 frame_->SpillAll();
5815 __ Move(r0, tos);
5816 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
5817 frame_->CallStub(&stub, 0);
5818 frame_->EmitPush(r0);
5819
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005820 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005821 break;
5822 }
5823
5824 case Token::VOID:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005825 frame_->Drop();
5826 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005827 break;
5828
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005829 case Token::ADD: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005830 Register tos = frame_->Peek();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005831 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005832 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005833 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005834 continue_label.Branch(eq);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005835
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005836 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005837 frame_->EmitPush(r0);
5838
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005839 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005840 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005841 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005842 default:
5843 UNREACHABLE();
5844 }
5845 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005846 ASSERT(!has_valid_frame() ||
5847 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005848 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005849}
5850
5851
ager@chromium.org7c537e22008-10-16 08:43:32 +00005852void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005853#ifdef DEBUG
5854 int original_height = frame_->height();
5855#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005856 Comment cmnt(masm_, "[ CountOperation");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005857 VirtualFrame::RegisterAllocationScope scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005858
5859 bool is_postfix = node->is_postfix();
5860 bool is_increment = node->op() == Token::INC;
5861
5862 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
5863 bool is_const = (var != NULL && var->mode() == Variable::CONST);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005864 bool is_slot = (var != NULL && var->mode() == Variable::VAR);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005865
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005866 if (!is_const && is_slot && type_info(var->slot()).IsSmi()) {
5867 // The type info declares that this variable is always a Smi. That
5868 // means it is a Smi both before and after the increment/decrement.
5869 // Lets make use of that to make a very minimal count.
5870 Reference target(this, node->expression(), !is_const);
5871 ASSERT(!target.is_illegal());
5872 target.GetValue(); // Pushes the value.
5873 Register value = frame_->PopToRegister();
5874 if (is_postfix) frame_->EmitPush(value);
5875 if (is_increment) {
5876 __ add(value, value, Operand(Smi::FromInt(1)));
5877 } else {
5878 __ sub(value, value, Operand(Smi::FromInt(1)));
5879 }
5880 frame_->EmitPush(value);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005881 target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005882 if (is_postfix) frame_->Pop();
5883 ASSERT_EQ(original_height + 1, frame_->height());
5884 return;
5885 }
5886
5887 // If it's a postfix expression and its result is not ignored and the
5888 // reference is non-trivial, then push a placeholder on the stack now
5889 // to hold the result of the expression.
5890 bool placeholder_pushed = false;
5891 if (!is_slot && is_postfix) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005892 frame_->EmitPush(Operand(Smi::FromInt(0)));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005893 placeholder_pushed = true;
mads.s.ager31e71382008-08-13 09:32:07 +00005894 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005895
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005896 // A constant reference is not saved to, so a constant reference is not a
5897 // compound assignment reference.
5898 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005899 if (target.is_illegal()) {
5900 // Spoof the virtual frame to have the expected height (one higher
5901 // than on entry).
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005902 if (!placeholder_pushed) frame_->EmitPush(Operand(Smi::FromInt(0)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00005903 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005904 return;
5905 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005906
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005907 // This pushes 0, 1 or 2 words on the object to be used later when updating
5908 // the target. It also pushes the current value of the target.
ager@chromium.org357bf652010-04-12 11:30:10 +00005909 target.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005910
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005911 JumpTarget slow;
5912 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005913
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005914 Register value = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005915
5916 // Postfix: Store the old value as the result.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005917 if (placeholder_pushed) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005918 frame_->SetElementAt(value, target.size());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005919 } else if (is_postfix) {
5920 frame_->EmitPush(value);
5921 __ mov(VirtualFrame::scratch0(), value);
5922 value = VirtualFrame::scratch0();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005923 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005924
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005925 // Check for smi operand.
5926 __ tst(value, Operand(kSmiTagMask));
5927 slow.Branch(ne);
5928
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005929 // Perform optimistic increment/decrement.
5930 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005931 __ add(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005932 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005933 __ sub(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005934 }
5935
5936 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005937 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005938
5939 // Revert optimistic increment/decrement.
5940 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005941 __ sub(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005942 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005943 __ add(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005944 }
5945
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005946 // Slow case: Convert to number. At this point the
5947 // value to be incremented is in the value register..
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005948 slow.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005949
5950 // Convert the operand to a number.
5951 frame_->EmitPush(value);
5952
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00005953 {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005954 VirtualFrame::SpilledScope spilled(frame_);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005955 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005956
5957 if (is_postfix) {
5958 // Postfix: store to result (on the stack).
5959 __ str(r0, frame_->ElementAt(target.size()));
5960 }
5961
5962 // Compute the new value.
5963 frame_->EmitPush(r0);
5964 frame_->EmitPush(Operand(Smi::FromInt(1)));
5965 if (is_increment) {
5966 frame_->CallRuntime(Runtime::kNumberAdd, 2);
5967 } else {
5968 frame_->CallRuntime(Runtime::kNumberSub, 2);
5969 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005970 }
5971
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005972 __ Move(value, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005973 // Store the new value in the target if not const.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005974 // At this point the answer is in the value register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005975 exit.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005976 frame_->EmitPush(value);
5977 // Set the target with the result, leaving the result on
5978 // top of the stack. Removes the target from the stack if
5979 // it has a non-zero size.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005980 if (!is_const) target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005981 }
5982
5983 // Postfix: Discard the new value and use the old.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005984 if (is_postfix) frame_->Pop();
ager@chromium.orgac091b72010-05-05 07:34:42 +00005985 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005986}
5987
5988
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005989void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005990 // According to ECMA-262 section 11.11, page 58, the binary logical
5991 // operators must yield the result of one of the two expressions
5992 // before any ToBoolean() conversions. This means that the value
5993 // produced by a && or || operator is not necessarily a boolean.
5994
5995 // NOTE: If the left hand side produces a materialized value (not in
5996 // the CC register), we force the right hand side to do the
5997 // same. This is necessary because we may have to branch to the exit
5998 // after evaluating the left hand side (due to the shortcut
5999 // semantics), but the compiler must (statically) know if the result
6000 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006001 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006002 JumpTarget is_true;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006003 LoadCondition(node->left(), &is_true, false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006004 if (has_valid_frame() && !has_cc()) {
6005 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006006 JumpTarget pop_and_continue;
6007 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006008
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006009 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006010 // Avoid popping the result if it converts to 'false' using the
6011 // standard ToBoolean() conversion as described in ECMA-262,
6012 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00006013 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006014 Branch(false, &exit);
6015
6016 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006017 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006018 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006019
6020 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006021 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006022 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006023
6024 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006025 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006026 } else if (has_cc() || is_true.is_linked()) {
6027 // The left-hand side is either (a) partially compiled to
6028 // control flow with a final branch left to emit or (b) fully
6029 // compiled to control flow and possibly true.
6030 if (has_cc()) {
6031 Branch(false, false_target());
6032 }
6033 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006034 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006035 } else {
6036 // Nothing to do.
6037 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006038 }
6039
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006040 } else {
6041 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006042 JumpTarget is_false;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006043 LoadCondition(node->left(), true_target(), &is_false, false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006044 if (has_valid_frame() && !has_cc()) {
6045 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006046 JumpTarget pop_and_continue;
6047 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006048
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006049 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006050 // Avoid popping the result if it converts to 'true' using the
6051 // standard ToBoolean() conversion as described in ECMA-262,
6052 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00006053 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006054 Branch(true, &exit);
6055
6056 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006057 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006058 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006059
6060 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006061 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006062 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006063
6064 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006065 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006066 } else if (has_cc() || is_false.is_linked()) {
6067 // The left-hand side is either (a) partially compiled to
6068 // control flow with a final branch left to emit or (b) fully
6069 // compiled to control flow and possibly false.
6070 if (has_cc()) {
6071 Branch(true, true_target());
6072 }
6073 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006074 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006075 } else {
6076 // Nothing to do.
6077 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006078 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006079 }
6080}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006081
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006082
6083void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
6084#ifdef DEBUG
6085 int original_height = frame_->height();
6086#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006087 Comment cmnt(masm_, "[ BinaryOperation");
6088
6089 if (node->op() == Token::AND || node->op() == Token::OR) {
6090 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006091 } else {
6092 // Optimize for the case where (at least) one of the expressions
6093 // is a literal small integer.
6094 Literal* lliteral = node->left()->AsLiteral();
6095 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006096 // NOTE: The code below assumes that the slow cases (calls to runtime)
6097 // never return a constant/immutable object.
ricow@chromium.org65fae842010-08-25 15:26:24 +00006098 bool overwrite_left = node->left()->ResultOverwriteAllowed();
6099 bool overwrite_right = node->right()->ResultOverwriteAllowed();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006100
6101 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006102 VirtualFrame::RegisterAllocationScope scope(this);
6103 Load(node->left());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006104 if (frame_->KnownSmiAt(0)) overwrite_left = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006105 SmiOperation(node->op(),
6106 rliteral->handle(),
6107 false,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006108 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006109 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006110 VirtualFrame::RegisterAllocationScope scope(this);
6111 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006112 if (frame_->KnownSmiAt(0)) overwrite_right = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006113 SmiOperation(node->op(),
6114 lliteral->handle(),
6115 true,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006116 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006117 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006118 GenerateInlineSmi inline_smi =
6119 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
6120 if (lliteral != NULL) {
6121 ASSERT(!lliteral->handle()->IsSmi());
6122 inline_smi = DONT_GENERATE_INLINE_SMI;
6123 }
6124 if (rliteral != NULL) {
6125 ASSERT(!rliteral->handle()->IsSmi());
6126 inline_smi = DONT_GENERATE_INLINE_SMI;
6127 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006128 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006129 OverwriteMode overwrite_mode = NO_OVERWRITE;
6130 if (overwrite_left) {
6131 overwrite_mode = OVERWRITE_LEFT;
6132 } else if (overwrite_right) {
6133 overwrite_mode = OVERWRITE_RIGHT;
6134 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006135 Load(node->left());
6136 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006137 GenericBinaryOperation(node->op(), overwrite_mode, inline_smi);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006138 }
6139 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006140 ASSERT(!has_valid_frame() ||
6141 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006142 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006143}
6144
6145
ager@chromium.org7c537e22008-10-16 08:43:32 +00006146void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006147#ifdef DEBUG
6148 int original_height = frame_->height();
6149#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006150 frame_->EmitPush(MemOperand(frame_->Function()));
ager@chromium.orgac091b72010-05-05 07:34:42 +00006151 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006152}
6153
6154
ager@chromium.org7c537e22008-10-16 08:43:32 +00006155void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006156#ifdef DEBUG
6157 int original_height = frame_->height();
6158#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006159 Comment cmnt(masm_, "[ CompareOperation");
6160
ager@chromium.org357bf652010-04-12 11:30:10 +00006161 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
6162
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006163 // Get the expressions from the node.
6164 Expression* left = node->left();
6165 Expression* right = node->right();
6166 Token::Value op = node->op();
6167
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006168 // To make typeof testing for natives implemented in JavaScript really
6169 // efficient, we generate special code for expressions of the form:
6170 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006171 UnaryOperation* operation = left->AsUnaryOperation();
6172 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
6173 (operation != NULL && operation->op() == Token::TYPEOF) &&
6174 (right->AsLiteral() != NULL &&
6175 right->AsLiteral()->handle()->IsString())) {
6176 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
6177
ager@chromium.org357bf652010-04-12 11:30:10 +00006178 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006179 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00006180 Register tos = frame_->PopToRegister();
6181
ager@chromium.org357bf652010-04-12 11:30:10 +00006182 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006183
6184 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006185 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006186 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00006187 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006188 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006189 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006190 cc_reg_ = eq;
6191
6192 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006193 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006194 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006195
ager@chromium.org357bf652010-04-12 11:30:10 +00006196 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006197
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006198 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006199 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
6200 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
6201 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006202 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006203
ager@chromium.org357bf652010-04-12 11:30:10 +00006204 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
6205 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006206 cc_reg_ = lt;
6207
6208 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006209 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006210 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006211 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006212 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006213 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006214 cc_reg_ = eq;
6215
6216 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006217 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006218 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006219 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006220
ager@chromium.org357bf652010-04-12 11:30:10 +00006221 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006222 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006223
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006224 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006225 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
6226 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
6227 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
6228 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006229
6230 cc_reg_ = eq;
6231
6232 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006233 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006234 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00006235 Register map_reg = scratch;
6236 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006237 true_target()->Branch(eq);
6238 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00006239 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006240 cc_reg_ = eq;
6241
6242 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006243 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006244 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006245
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006246 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006247 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006248 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006249
ager@chromium.org357bf652010-04-12 11:30:10 +00006250 Register map_reg = scratch;
6251 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006252 false_target()->Branch(eq);
6253
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006254 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006255 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
6256 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6257 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006258 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006259
ager@chromium.org357bf652010-04-12 11:30:10 +00006260 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
6261 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006262 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00006263 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006264 cc_reg_ = le;
6265
6266 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006267 // Uncommon case: typeof testing against a string literal that is
6268 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006269 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006270 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006271 ASSERT(!has_valid_frame() ||
6272 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006273 return;
6274 }
6275
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006276 switch (op) {
6277 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006278 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006279 break;
6280
6281 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006282 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006283 break;
6284
6285 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006286 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006287 break;
6288
6289 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006290 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006291 break;
6292
6293 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006294 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006295 break;
6296
6297 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006298 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006299 break;
6300
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006301 case Token::IN: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006302 Load(left);
6303 Load(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006304 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006305 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006306 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006307 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006308
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006309 case Token::INSTANCEOF: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006310 Load(left);
6311 Load(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006312 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006313 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006314 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006315 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006316 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006317 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006318 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006319
6320 default:
6321 UNREACHABLE();
6322 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006323 ASSERT((has_cc() && frame_->height() == original_height) ||
6324 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006325}
6326
6327
ricow@chromium.org65fae842010-08-25 15:26:24 +00006328void CodeGenerator::VisitCompareToNull(CompareToNull* node) {
6329#ifdef DEBUG
6330 int original_height = frame_->height();
6331#endif
6332 Comment cmnt(masm_, "[ CompareToNull");
6333
6334 Load(node->expression());
6335 Register tos = frame_->PopToRegister();
6336 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6337 __ cmp(tos, ip);
6338
6339 // The 'null' value is only equal to 'undefined' if using non-strict
6340 // comparisons.
6341 if (!node->is_strict()) {
6342 true_target()->Branch(eq);
6343 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
6344 __ cmp(tos, Operand(ip));
6345 true_target()->Branch(eq);
6346
6347 __ tst(tos, Operand(kSmiTagMask));
6348 false_target()->Branch(eq);
6349
6350 // It can be an undetectable object.
6351 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
6352 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
6353 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6354 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
6355 }
6356
6357 cc_reg_ = eq;
6358 ASSERT(has_cc() && frame_->height() == original_height);
6359}
6360
6361
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006362class DeferredReferenceGetNamedValue: public DeferredCode {
6363 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006364 explicit DeferredReferenceGetNamedValue(Register receiver,
6365 Handle<String> name)
6366 : receiver_(receiver), name_(name) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006367 set_comment("[ DeferredReferenceGetNamedValue");
6368 }
6369
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006370 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006371
6372 private:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006373 Register receiver_;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006374 Handle<String> name_;
6375};
6376
6377
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006378// Convention for this is that on entry the receiver is in a register that
6379// is not used by the stack. On exit the answer is found in that same
6380// register and the stack has the same height.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006381void DeferredReferenceGetNamedValue::Generate() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006382#ifdef DEBUG
6383 int expected_height = frame_state()->frame()->height();
6384#endif
6385 VirtualFrame copied_frame(*frame_state()->frame());
6386 copied_frame.SpillAll();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006387
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006388 Register scratch1 = VirtualFrame::scratch0();
6389 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006390 ASSERT(!receiver_.is(scratch1) && !receiver_.is(scratch2));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006391 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
6392 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
6393
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006394 // Ensure receiver in r0 and name in r2 to match load ic calling convention.
6395 __ Move(r0, receiver_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006396 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006397
6398 // The rest of the instructions in the deferred code must be together.
6399 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6400 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
6401 __ Call(ic, RelocInfo::CODE_TARGET);
6402 // The call must be followed by a nop(1) instruction to indicate that the
6403 // in-object has been inlined.
6404 __ nop(PROPERTY_ACCESS_INLINED);
6405
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006406 // At this point the answer is in r0. We move it to the expected register
6407 // if necessary.
6408 __ Move(receiver_, r0);
6409
6410 // Now go back to the frame that we entered with. This will not overwrite
6411 // the receiver register since that register was not in use when we came
6412 // in. The instructions emitted by this merge are skipped over by the
6413 // inline load patching mechanism when looking for the branch instruction
6414 // that tells it where the code to patch is.
6415 copied_frame.MergeTo(frame_state()->frame());
6416
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006417 // Block the constant pool for one more instruction after leaving this
6418 // constant pool block scope to include the branch instruction ending the
6419 // deferred code.
6420 __ BlockConstPoolFor(1);
6421 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006422 ASSERT_EQ(expected_height, frame_state()->frame()->height());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006423}
6424
6425
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006426class DeferredReferenceGetKeyedValue: public DeferredCode {
6427 public:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006428 DeferredReferenceGetKeyedValue(Register key, Register receiver)
6429 : key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006430 set_comment("[ DeferredReferenceGetKeyedValue");
6431 }
6432
6433 virtual void Generate();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006434
6435 private:
6436 Register key_;
6437 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006438};
6439
6440
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006441// Takes key and register in r0 and r1 or vice versa. Returns result
6442// in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006443void DeferredReferenceGetKeyedValue::Generate() {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006444 ASSERT((key_.is(r0) && receiver_.is(r1)) ||
6445 (key_.is(r1) && receiver_.is(r0)));
6446
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006447 VirtualFrame copied_frame(*frame_state()->frame());
6448 copied_frame.SpillAll();
6449
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006450 Register scratch1 = VirtualFrame::scratch0();
6451 Register scratch2 = VirtualFrame::scratch1();
6452 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
6453 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
6454
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006455 // Ensure key in r0 and receiver in r1 to match keyed load ic calling
6456 // convention.
6457 if (key_.is(r1)) {
6458 __ Swap(r0, r1, ip);
6459 }
6460
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006461 // The rest of the instructions in the deferred code must be together.
6462 { Assembler::BlockConstPoolScope block_const_pool(masm_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006463 // Call keyed load IC. It has the arguments key and receiver in r0 and r1.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006464 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
6465 __ Call(ic, RelocInfo::CODE_TARGET);
6466 // The call must be followed by a nop instruction to indicate that the
6467 // keyed load has been inlined.
6468 __ nop(PROPERTY_ACCESS_INLINED);
6469
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006470 // Now go back to the frame that we entered with. This will not overwrite
6471 // the receiver or key registers since they were not in use when we came
6472 // in. The instructions emitted by this merge are skipped over by the
6473 // inline load patching mechanism when looking for the branch instruction
6474 // that tells it where the code to patch is.
6475 copied_frame.MergeTo(frame_state()->frame());
6476
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006477 // Block the constant pool for one more instruction after leaving this
6478 // constant pool block scope to include the branch instruction ending the
6479 // deferred code.
6480 __ BlockConstPoolFor(1);
6481 }
6482}
6483
6484
6485class DeferredReferenceSetKeyedValue: public DeferredCode {
6486 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006487 DeferredReferenceSetKeyedValue(Register value,
6488 Register key,
6489 Register receiver)
6490 : value_(value), key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006491 set_comment("[ DeferredReferenceSetKeyedValue");
6492 }
6493
6494 virtual void Generate();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006495
6496 private:
6497 Register value_;
6498 Register key_;
6499 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006500};
6501
6502
6503void DeferredReferenceSetKeyedValue::Generate() {
6504 Register scratch1 = VirtualFrame::scratch0();
6505 Register scratch2 = VirtualFrame::scratch1();
6506 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
6507 __ IncrementCounter(
6508 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
6509
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006510 // Ensure value in r0, key in r1 and receiver in r2 to match keyed store ic
6511 // calling convention.
6512 if (value_.is(r1)) {
6513 __ Swap(r0, r1, ip);
6514 }
6515 ASSERT(receiver_.is(r2));
6516
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006517 // The rest of the instructions in the deferred code must be together.
6518 { Assembler::BlockConstPoolScope block_const_pool(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006519 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6520 // r1 and r2.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006521 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
6522 __ Call(ic, RelocInfo::CODE_TARGET);
6523 // The call must be followed by a nop instruction to indicate that the
6524 // keyed store has been inlined.
6525 __ nop(PROPERTY_ACCESS_INLINED);
6526
6527 // Block the constant pool for one more instruction after leaving this
6528 // constant pool block scope to include the branch instruction ending the
6529 // deferred code.
6530 __ BlockConstPoolFor(1);
6531 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006532}
6533
6534
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006535class DeferredReferenceSetNamedValue: public DeferredCode {
6536 public:
6537 DeferredReferenceSetNamedValue(Register value,
6538 Register receiver,
6539 Handle<String> name)
6540 : value_(value), receiver_(receiver), name_(name) {
6541 set_comment("[ DeferredReferenceSetNamedValue");
6542 }
6543
6544 virtual void Generate();
6545
6546 private:
6547 Register value_;
6548 Register receiver_;
6549 Handle<String> name_;
6550};
6551
6552
6553// Takes value in r0, receiver in r1 and returns the result (the
6554// value) in r0.
6555void DeferredReferenceSetNamedValue::Generate() {
6556 // Record the entry frame and spill.
6557 VirtualFrame copied_frame(*frame_state()->frame());
6558 copied_frame.SpillAll();
6559
6560 // Ensure value in r0, receiver in r1 to match store ic calling
6561 // convention.
6562 ASSERT(value_.is(r0) && receiver_.is(r1));
6563 __ mov(r2, Operand(name_));
6564
6565 // The rest of the instructions in the deferred code must be together.
6566 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6567 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6568 // r1 and r2.
6569 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
6570 __ Call(ic, RelocInfo::CODE_TARGET);
6571 // The call must be followed by a nop instruction to indicate that the
6572 // named store has been inlined.
6573 __ nop(PROPERTY_ACCESS_INLINED);
6574
6575 // Go back to the frame we entered with. The instructions
6576 // generated by this merge are skipped over by the inline store
6577 // patching mechanism when looking for the branch instruction that
6578 // tells it where the code to patch is.
6579 copied_frame.MergeTo(frame_state()->frame());
6580
6581 // Block the constant pool for one more instruction after leaving this
6582 // constant pool block scope to include the branch instruction ending the
6583 // deferred code.
6584 __ BlockConstPoolFor(1);
6585 }
6586}
6587
6588
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006589// Consumes the top of stack (the receiver) and pushes the result instead.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006590void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
6591 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6592 Comment cmnt(masm(), "[ Load from named Property");
6593 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00006594 frame_->CallLoadIC(name,
6595 is_contextual
6596 ? RelocInfo::CODE_TARGET_CONTEXT
6597 : RelocInfo::CODE_TARGET);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006598 frame_->EmitPush(r0); // Push answer.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006599 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006600 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006601 Comment cmnt(masm(), "[ Inlined named property load");
6602
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006603 // Counter will be decremented in the deferred code. Placed here to avoid
6604 // having it in the instruction stream below where patching will occur.
6605 __ IncrementCounter(&Counters::named_load_inline, 1,
6606 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006607
6608 // The following instructions are the inlined load of an in-object property.
6609 // Parts of this code is patched, so the exact instructions generated needs
6610 // to be fixed. Therefore the instruction pool is blocked when generating
6611 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006612
6613 // Load the receiver from the stack.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006614 Register receiver = frame_->PopToRegister();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006615
6616 DeferredReferenceGetNamedValue* deferred =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006617 new DeferredReferenceGetNamedValue(receiver, name);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006618
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006619#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006620 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006621 Label check_inlined_codesize;
6622 masm_->bind(&check_inlined_codesize);
6623#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006624
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006625 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006626 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006627 __ tst(receiver, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006628 deferred->Branch(eq);
6629
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006630 Register scratch = VirtualFrame::scratch0();
6631 Register scratch2 = VirtualFrame::scratch1();
6632
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006633 // Check the map. The null map used below is patched by the inline cache
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006634 // code. Therefore we can't use a LoadRoot call.
6635 __ ldr(scratch, FieldMemOperand(receiver, HeapObject::kMapOffset));
6636 __ mov(scratch2, Operand(Factory::null_value()));
6637 __ cmp(scratch, scratch2);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006638 deferred->Branch(ne);
6639
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006640 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006641 // inline cache code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006642 __ ldr(receiver, MemOperand(receiver, 0));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006643
6644 // Make sure that the expected number of instructions are generated.
6645 ASSERT_EQ(kInlinedNamedLoadInstructions,
6646 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006647 }
6648
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006649 deferred->BindExit();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006650 // At this point the receiver register has the result, either from the
6651 // deferred code or from the inlined code.
6652 frame_->EmitPush(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006653 }
6654}
6655
6656
ager@chromium.orgac091b72010-05-05 07:34:42 +00006657void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) {
6658#ifdef DEBUG
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006659 int expected_height = frame()->height() - (is_contextual ? 1 : 2);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006660#endif
ager@chromium.orgac091b72010-05-05 07:34:42 +00006661
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006662 Result result;
6663 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6664 frame()->CallStoreIC(name, is_contextual);
6665 } else {
6666 // Inline the in-object property case.
6667 JumpTarget slow, done;
6668
6669 // Get the value and receiver from the stack.
6670 frame()->PopToR0();
6671 Register value = r0;
6672 frame()->PopToR1();
6673 Register receiver = r1;
6674
6675 DeferredReferenceSetNamedValue* deferred =
6676 new DeferredReferenceSetNamedValue(value, receiver, name);
6677
6678 // Check that the receiver is a heap object.
6679 __ tst(receiver, Operand(kSmiTagMask));
6680 deferred->Branch(eq);
6681
6682 // The following instructions are the part of the inlined
6683 // in-object property store code which can be patched. Therefore
6684 // the exact number of instructions generated must be fixed, so
6685 // the constant pool is blocked while generating this code.
6686 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6687 Register scratch0 = VirtualFrame::scratch0();
6688 Register scratch1 = VirtualFrame::scratch1();
6689
6690 // Check the map. Initially use an invalid map to force a
6691 // failure. The map check will be patched in the runtime system.
6692 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
6693
6694#ifdef DEBUG
6695 Label check_inlined_codesize;
6696 masm_->bind(&check_inlined_codesize);
6697#endif
6698 __ mov(scratch0, Operand(Factory::null_value()));
6699 __ cmp(scratch0, scratch1);
6700 deferred->Branch(ne);
6701
6702 int offset = 0;
6703 __ str(value, MemOperand(receiver, offset));
6704
6705 // Update the write barrier and record its size. We do not use
6706 // the RecordWrite macro here because we want the offset
6707 // addition instruction first to make it easy to patch.
6708 Label record_write_start, record_write_done;
6709 __ bind(&record_write_start);
6710 // Add offset into the object.
6711 __ add(scratch0, receiver, Operand(offset));
6712 // Test that the object is not in the new space. We cannot set
6713 // region marks for new space pages.
6714 __ InNewSpace(receiver, scratch1, eq, &record_write_done);
6715 // Record the actual write.
6716 __ RecordWriteHelper(receiver, scratch0, scratch1);
6717 __ bind(&record_write_done);
6718 // Clobber all input registers when running with the debug-code flag
6719 // turned on to provoke errors.
6720 if (FLAG_debug_code) {
6721 __ mov(receiver, Operand(BitCast<int32_t>(kZapValue)));
6722 __ mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
6723 __ mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
6724 }
6725 // Check that this is the first inlined write barrier or that
6726 // this inlined write barrier has the same size as all the other
6727 // inlined write barriers.
6728 ASSERT((inlined_write_barrier_size_ == -1) ||
6729 (inlined_write_barrier_size_ ==
6730 masm()->InstructionsGeneratedSince(&record_write_start)));
6731 inlined_write_barrier_size_ =
6732 masm()->InstructionsGeneratedSince(&record_write_start);
6733
6734 // Make sure that the expected number of instructions are generated.
6735 ASSERT_EQ(GetInlinedNamedStoreInstructionsAfterPatch(),
6736 masm()->InstructionsGeneratedSince(&check_inlined_codesize));
6737 }
6738 deferred->BindExit();
6739 }
6740 ASSERT_EQ(expected_height, frame()->height());
ager@chromium.orgac091b72010-05-05 07:34:42 +00006741}
6742
6743
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006744void CodeGenerator::EmitKeyedLoad() {
6745 if (loop_nesting() == 0) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006746 Comment cmnt(masm_, "[ Load from keyed property");
6747 frame_->CallKeyedLoadIC();
6748 } else {
6749 // Inline the keyed load.
6750 Comment cmnt(masm_, "[ Inlined load from keyed property");
6751
6752 // Counter will be decremented in the deferred code. Placed here to avoid
6753 // having it in the instruction stream below where patching will occur.
6754 __ IncrementCounter(&Counters::keyed_load_inline, 1,
6755 frame_->scratch0(), frame_->scratch1());
6756
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006757 // Load the key and receiver from the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006758 bool key_is_known_smi = frame_->KnownSmiAt(0);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006759 Register key = frame_->PopToRegister();
6760 Register receiver = frame_->PopToRegister(key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006761
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006762 // The deferred code expects key and receiver in registers.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006763 DeferredReferenceGetKeyedValue* deferred =
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006764 new DeferredReferenceGetKeyedValue(key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006765
6766 // Check that the receiver is a heap object.
6767 __ tst(receiver, Operand(kSmiTagMask));
6768 deferred->Branch(eq);
6769
6770 // The following instructions are the part of the inlined load keyed
6771 // property code which can be patched. Therefore the exact number of
6772 // instructions generated need to be fixed, so the constant pool is blocked
6773 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006774 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6775 Register scratch1 = VirtualFrame::scratch0();
6776 Register scratch2 = VirtualFrame::scratch1();
6777 // Check the map. The null map used below is patched by the inline cache
6778 // code.
6779 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006780
6781 // Check that the key is a smi.
6782 if (!key_is_known_smi) {
6783 __ tst(key, Operand(kSmiTagMask));
6784 deferred->Branch(ne);
6785 }
6786
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006787#ifdef DEBUG
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006788 Label check_inlined_codesize;
6789 masm_->bind(&check_inlined_codesize);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006790#endif
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006791 __ mov(scratch2, Operand(Factory::null_value()));
6792 __ cmp(scratch1, scratch2);
6793 deferred->Branch(ne);
6794
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00006795 // Get the elements array from the receiver.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006796 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00006797 __ AssertFastElements(scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006798
6799 // Check that key is within bounds. Use unsigned comparison to handle
6800 // negative keys.
6801 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006802 __ cmp(scratch2, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006803 deferred->Branch(ls); // Unsigned less equal.
6804
6805 // Load and check that the result is not the hole (key is a smi).
6806 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
6807 __ add(scratch1,
6808 scratch1,
6809 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006810 __ ldr(scratch1,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006811 MemOperand(scratch1, key, LSL,
6812 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006813 __ cmp(scratch1, scratch2);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006814 deferred->Branch(eq);
6815
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006816 __ mov(r0, scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006817 // Make sure that the expected number of instructions are generated.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006818 ASSERT_EQ(GetInlinedKeyedLoadInstructionsAfterPatch(),
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006819 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6820 }
6821
6822 deferred->BindExit();
6823 }
6824}
6825
6826
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006827void CodeGenerator::EmitKeyedStore(StaticType* key_type,
6828 WriteBarrierCharacter wb_info) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006829 // Generate inlined version of the keyed store if the code is in a loop
6830 // and the key is likely to be a smi.
6831 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
6832 // Inline the keyed store.
6833 Comment cmnt(masm_, "[ Inlined store to keyed property");
6834
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006835 Register scratch1 = VirtualFrame::scratch0();
6836 Register scratch2 = VirtualFrame::scratch1();
6837 Register scratch3 = r3;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006838
6839 // Counter will be decremented in the deferred code. Placed here to avoid
6840 // having it in the instruction stream below where patching will occur.
6841 __ IncrementCounter(&Counters::keyed_store_inline, 1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006842 scratch1, scratch2);
6843
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006844
6845
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006846 // Load the value, key and receiver from the stack.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006847 bool value_is_harmless = frame_->KnownSmiAt(0);
6848 if (wb_info == NEVER_NEWSPACE) value_is_harmless = true;
6849 bool key_is_smi = frame_->KnownSmiAt(1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006850 Register value = frame_->PopToRegister();
6851 Register key = frame_->PopToRegister(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006852 VirtualFrame::SpilledScope spilled(frame_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006853 Register receiver = r2;
6854 frame_->EmitPop(receiver);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006855
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006856#ifdef DEBUG
6857 bool we_remembered_the_write_barrier = value_is_harmless;
6858#endif
6859
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006860 // The deferred code expects value, key and receiver in registers.
6861 DeferredReferenceSetKeyedValue* deferred =
6862 new DeferredReferenceSetKeyedValue(value, key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006863
6864 // Check that the value is a smi. As this inlined code does not set the
6865 // write barrier it is only possible to store smi values.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006866 if (!value_is_harmless) {
6867 // If the value is not likely to be a Smi then let's test the fixed array
6868 // for new space instead. See below.
6869 if (wb_info == LIKELY_SMI) {
6870 __ tst(value, Operand(kSmiTagMask));
6871 deferred->Branch(ne);
6872#ifdef DEBUG
6873 we_remembered_the_write_barrier = true;
6874#endif
6875 }
6876 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006877
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006878 if (!key_is_smi) {
6879 // Check that the key is a smi.
6880 __ tst(key, Operand(kSmiTagMask));
6881 deferred->Branch(ne);
6882 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006883
6884 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006885 __ tst(receiver, Operand(kSmiTagMask));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006886 deferred->Branch(eq);
6887
6888 // Check that the receiver is a JSArray.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006889 __ CompareObjectType(receiver, scratch1, scratch1, JS_ARRAY_TYPE);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006890 deferred->Branch(ne);
6891
6892 // Check that the key is within bounds. Both the key and the length of
6893 // the JSArray are smis. Use unsigned comparison to handle negative keys.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006894 __ ldr(scratch1, FieldMemOperand(receiver, JSArray::kLengthOffset));
6895 __ cmp(scratch1, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006896 deferred->Branch(ls); // Unsigned less equal.
6897
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006898 // Get the elements array from the receiver.
6899 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
6900 if (!value_is_harmless && wb_info != LIKELY_SMI) {
6901 Label ok;
6902 __ and_(scratch2, scratch1, Operand(ExternalReference::new_space_mask()));
6903 __ cmp(scratch2, Operand(ExternalReference::new_space_start()));
6904 __ tst(value, Operand(kSmiTagMask), ne);
6905 deferred->Branch(ne);
6906#ifdef DEBUG
6907 we_remembered_the_write_barrier = true;
6908#endif
6909 }
6910 // Check that the elements array is not a dictionary.
6911 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006912 // The following instructions are the part of the inlined store keyed
6913 // property code which can be patched. Therefore the exact number of
6914 // instructions generated need to be fixed, so the constant pool is blocked
6915 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006916 { Assembler::BlockConstPoolScope block_const_pool(masm_);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006917#ifdef DEBUG
6918 Label check_inlined_codesize;
6919 masm_->bind(&check_inlined_codesize);
6920#endif
6921
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006922 // Read the fixed array map from the constant pool (not from the root
6923 // array) so that the value can be patched. When debugging, we patch this
6924 // comparison to always fail so that we will hit the IC call in the
6925 // deferred code which will allow the debugger to break for fast case
6926 // stores.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006927 __ mov(scratch3, Operand(Factory::fixed_array_map()));
6928 __ cmp(scratch2, scratch3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006929 deferred->Branch(ne);
6930
6931 // Store the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006932 __ add(scratch1, scratch1,
6933 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6934 __ str(value,
6935 MemOperand(scratch1, key, LSL,
6936 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006937
6938 // Make sure that the expected number of instructions are generated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006939 ASSERT_EQ(kInlinedKeyedStoreInstructionsAfterPatch,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006940 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6941 }
6942
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006943 ASSERT(we_remembered_the_write_barrier);
6944
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006945 deferred->BindExit();
6946 } else {
6947 frame()->CallKeyedStoreIC();
6948 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006949}
6950
6951
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006952#ifdef DEBUG
6953bool CodeGenerator::HasValidEntryRegisters() { return true; }
6954#endif
6955
6956
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006957#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006958#define __ ACCESS_MASM(masm)
6959
ager@chromium.org7c537e22008-10-16 08:43:32 +00006960Handle<String> Reference::GetName() {
6961 ASSERT(type_ == NAMED);
6962 Property* property = expression_->AsProperty();
6963 if (property == NULL) {
6964 // Global variable reference treated as a named property reference.
6965 VariableProxy* proxy = expression_->AsVariableProxy();
6966 ASSERT(proxy->AsVariable() != NULL);
6967 ASSERT(proxy->AsVariable()->is_global());
6968 return proxy->name();
6969 } else {
6970 Literal* raw_name = property->key()->AsLiteral();
6971 ASSERT(raw_name != NULL);
6972 return Handle<String>(String::cast(*raw_name->handle()));
6973 }
6974}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006975
ager@chromium.org7c537e22008-10-16 08:43:32 +00006976
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006977void Reference::DupIfPersist() {
6978 if (persist_after_get_) {
6979 switch (type_) {
6980 case KEYED:
6981 cgen_->frame()->Dup2();
6982 break;
6983 case NAMED:
6984 cgen_->frame()->Dup();
6985 // Fall through.
6986 case UNLOADED:
6987 case ILLEGAL:
6988 case SLOT:
6989 // Do nothing.
6990 ;
6991 }
6992 } else {
6993 set_unloaded();
6994 }
6995}
6996
6997
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006998void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006999 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00007000 ASSERT(!is_illegal());
7001 ASSERT(!cgen_->has_cc());
7002 MacroAssembler* masm = cgen_->masm();
7003 Property* property = expression_->AsProperty();
7004 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00007005 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00007006 }
7007
7008 switch (type_) {
7009 case SLOT: {
7010 Comment cmnt(masm, "[ Load from Slot");
7011 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
7012 ASSERT(slot != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007013 DupIfPersist();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007014 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007015 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007016 }
7017
ager@chromium.org7c537e22008-10-16 08:43:32 +00007018 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00007019 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007020 bool is_global = var != NULL;
7021 ASSERT(!is_global || var->is_global());
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007022 Handle<String> name = GetName();
7023 DupIfPersist();
7024 cgen_->EmitNamedLoad(name, is_global);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007025 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007026 }
7027
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007028 case KEYED: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007029 ASSERT(property != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007030 DupIfPersist();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007031 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007032 cgen_->frame()->EmitPush(r0);
7033 break;
7034 }
7035
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007036 default:
7037 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007038 }
7039}
7040
7041
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007042void Reference::SetValue(InitState init_state, WriteBarrierCharacter wb_info) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00007043 ASSERT(!is_illegal());
7044 ASSERT(!cgen_->has_cc());
7045 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00007046 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007047 Property* property = expression_->AsProperty();
7048 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00007049 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007050 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007051
ager@chromium.org7c537e22008-10-16 08:43:32 +00007052 switch (type_) {
7053 case SLOT: {
7054 Comment cmnt(masm, "[ Store to Slot");
7055 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007056 cgen_->StoreToSlot(slot, init_state);
ager@chromium.orgac091b72010-05-05 07:34:42 +00007057 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007058 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007059 }
7060
ager@chromium.org7c537e22008-10-16 08:43:32 +00007061 case NAMED: {
7062 Comment cmnt(masm, "[ Store to named Property");
ager@chromium.orgac091b72010-05-05 07:34:42 +00007063 cgen_->EmitNamedStore(GetName(), false);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00007064 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00007065 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007066 break;
7067 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007068
ager@chromium.org7c537e22008-10-16 08:43:32 +00007069 case KEYED: {
7070 Comment cmnt(masm, "[ Store to keyed Property");
7071 Property* property = expression_->AsProperty();
7072 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00007073 cgen_->CodeForSourcePosition(property->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007074 cgen_->EmitKeyedStore(property->key()->type(), wb_info);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00007075 frame->EmitPush(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007076 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007077 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007078 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00007079
7080 default:
7081 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007082 }
7083}
7084
7085
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007086const char* GenericBinaryOpStub::GetName() {
7087 if (name_ != NULL) return name_;
7088 const int len = 100;
7089 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
7090 if (name_ == NULL) return "OOM";
7091 const char* op_name = Token::Name(op_);
7092 const char* overwrite_name;
7093 switch (mode_) {
7094 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
7095 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
7096 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
7097 default: overwrite_name = "UnknownOverwrite"; break;
7098 }
7099
7100 OS::SNPrintF(Vector<char>(name_, len),
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007101 "GenericBinaryOpStub_%s_%s%s_%s",
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007102 op_name,
7103 overwrite_name,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007104 specialized_on_rhs_ ? "_ConstantRhs" : "",
7105 BinaryOpIC::GetName(runtime_operands_type_));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007106 return name_;
7107}
7108
7109
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007110#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007111
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007112} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007113
7114#endif // V8_TARGET_ARCH_ARM