blob: 5b7b0d50efb8f43f49c83b9b39e03692782c15a1 [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.org65fae842010-08-25 15:26:24 +000033#include "code-stubs-arm.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);
1138 GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, kUnknownIntValue);
ager@chromium.org357bf652010-04-12 11:30:10 +00001139 }
1140 return;
1141 }
1142
1143 // We move the top of stack to a register (normally no move is invoved).
1144 Register tos = frame_->PopToRegister();
ager@chromium.org357bf652010-04-12 11:30:10 +00001145 switch (op) {
1146 case Token::ADD: {
1147 DeferredCode* deferred =
1148 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1149
1150 __ add(tos, tos, Operand(value), SetCC);
1151 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001152 if (!both_sides_are_smi) {
1153 __ tst(tos, Operand(kSmiTagMask));
1154 deferred->Branch(ne);
1155 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001156 deferred->BindExit();
1157 frame_->EmitPush(tos);
1158 break;
1159 }
1160
1161 case Token::SUB: {
1162 DeferredCode* deferred =
1163 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1164
1165 if (reversed) {
1166 __ rsb(tos, tos, Operand(value), SetCC);
1167 } else {
1168 __ sub(tos, tos, Operand(value), SetCC);
1169 }
1170 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001171 if (!both_sides_are_smi) {
1172 __ tst(tos, Operand(kSmiTagMask));
1173 deferred->Branch(ne);
1174 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001175 deferred->BindExit();
1176 frame_->EmitPush(tos);
1177 break;
1178 }
1179
1180
1181 case Token::BIT_OR:
1182 case Token::BIT_XOR:
1183 case Token::BIT_AND: {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001184 if (both_sides_are_smi) {
1185 switch (op) {
1186 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1187 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001188 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001189 default: UNREACHABLE();
1190 }
1191 frame_->EmitPush(tos, TypeInfo::Smi());
1192 } else {
1193 DeferredCode* deferred =
1194 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1195 __ tst(tos, Operand(kSmiTagMask));
1196 deferred->Branch(ne);
1197 switch (op) {
1198 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1199 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001200 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001201 default: UNREACHABLE();
1202 }
1203 deferred->BindExit();
1204 TypeInfo result_type =
1205 (op == Token::BIT_AND) ? TypeInfo::Smi() : TypeInfo::Integer32();
1206 frame_->EmitPush(tos, result_type);
ager@chromium.org357bf652010-04-12 11:30:10 +00001207 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001208 break;
1209 }
1210
1211 case Token::SHL:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001212 if (reversed) {
1213 ASSERT(both_sides_are_smi);
1214 int max_shift = 0;
1215 int max_result = int_value == 0 ? 1 : int_value;
1216 while (Smi::IsValid(max_result << 1)) {
1217 max_shift++;
1218 max_result <<= 1;
1219 }
1220 DeferredCode* deferred =
1221 new DeferredInlineSmiOperation(op, int_value, true, mode, tos);
1222 // Mask off the last 5 bits of the shift operand (rhs). This is part
1223 // of the definition of shift in JS and we know we have a Smi so we
1224 // can safely do this. The masked version gets passed to the
1225 // deferred code, but that makes no difference.
1226 __ and_(tos, tos, Operand(Smi::FromInt(0x1f)));
1227 __ cmp(tos, Operand(Smi::FromInt(max_shift)));
1228 deferred->Branch(ge);
1229 Register scratch = VirtualFrame::scratch0();
1230 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Untag.
1231 __ mov(tos, Operand(Smi::FromInt(int_value))); // Load constant.
1232 __ mov(tos, Operand(tos, LSL, scratch)); // Shift constant.
1233 deferred->BindExit();
1234 TypeInfo result = TypeInfo::Integer32();
1235 frame_->EmitPush(tos, result);
1236 break;
1237 }
1238 // Fall through!
ager@chromium.org357bf652010-04-12 11:30:10 +00001239 case Token::SHR:
1240 case Token::SAR: {
1241 ASSERT(!reversed);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001242 int shift_amount = int_value & 0x1f;
1243 TypeInfo result = TypeInfo::Number();
1244
1245 if (op == Token::SHR) {
1246 if (shift_amount > 1) {
1247 result = TypeInfo::Smi();
1248 } else if (shift_amount > 0) {
1249 result = TypeInfo::Integer32();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001250 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001251 } else if (op == Token::SAR) {
1252 if (shift_amount > 0) {
1253 result = TypeInfo::Smi();
1254 } else {
1255 result = TypeInfo::Integer32();
1256 }
1257 } else {
1258 ASSERT(op == Token::SHL);
1259 result = TypeInfo::Integer32();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001260 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001261
ager@chromium.org357bf652010-04-12 11:30:10 +00001262 Register scratch = VirtualFrame::scratch0();
1263 Register scratch2 = VirtualFrame::scratch1();
1264 int shift_value = int_value & 0x1f; // least significant 5 bits
1265 DeferredCode* deferred =
1266 new DeferredInlineSmiOperation(op, shift_value, false, mode, tos);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001267 uint32_t problematic_mask = kSmiTagMask;
1268 // For unsigned shift by zero all negative smis are problematic.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001269 bool skip_smi_test = both_sides_are_smi;
1270 if (shift_value == 0 && op == Token::SHR) {
1271 problematic_mask |= 0x80000000;
1272 skip_smi_test = false;
1273 }
1274 if (!skip_smi_test) {
1275 __ tst(tos, Operand(problematic_mask));
1276 deferred->Branch(ne); // Go slow for problematic input.
1277 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001278 switch (op) {
1279 case Token::SHL: {
1280 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001281 int adjusted_shift = shift_value - kSmiTagSize;
1282 ASSERT(adjusted_shift >= 0);
1283 if (adjusted_shift != 0) {
1284 __ mov(scratch, Operand(tos, LSL, adjusted_shift));
1285 // Check that the *signed* result fits in a smi.
1286 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
1287 deferred->Branch(mi);
1288 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
1289 } else {
1290 // Check that the *signed* result fits in a smi.
1291 __ add(scratch2, tos, Operand(0x40000000), SetCC);
1292 deferred->Branch(mi);
1293 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
1294 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001295 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001296 break;
1297 }
1298 case Token::SHR: {
ager@chromium.org357bf652010-04-12 11:30:10 +00001299 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001300 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Remove tag.
1301 // LSR by immediate 0 means shifting 32 bits.
ager@chromium.org357bf652010-04-12 11:30:10 +00001302 __ mov(scratch, Operand(scratch, LSR, shift_value));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001303 if (shift_value == 1) {
1304 // check that the *unsigned* result fits in a smi
1305 // neither of the two high-order bits can be set:
1306 // - 0x80000000: high bit would be lost when smi tagging
1307 // - 0x40000000: this number would convert to negative when
1308 // smi tagging these two cases can only happen with shifts
1309 // by 0 or 1 when handed a valid smi
1310 __ tst(scratch, Operand(0xc0000000));
1311 deferred->Branch(ne);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001312 } else {
1313 ASSERT(shift_value >= 2);
1314 result = TypeInfo::Smi(); // SHR by at least 2 gives a Smi.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001315 }
1316 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
ager@chromium.org357bf652010-04-12 11:30:10 +00001317 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001318 break;
1319 }
1320 case Token::SAR: {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001321 // In the ARM instructions set, ASR by immediate 0 means shifting 32
1322 // bits.
ager@chromium.org357bf652010-04-12 11:30:10 +00001323 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001324 // Do the shift and the tag removal in one operation. If the shift
1325 // is 31 bits (the highest possible value) then we emit the
1326 // instruction as a shift by 0 which means shift arithmetically by
1327 // 32.
1328 __ mov(tos, Operand(tos, ASR, (kSmiTagSize + shift_value) & 0x1f));
1329 // Put tag back.
1330 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001331 // SAR by at least 1 gives a Smi.
1332 result = TypeInfo::Smi();
ager@chromium.org357bf652010-04-12 11:30:10 +00001333 }
1334 break;
1335 }
1336 default: UNREACHABLE();
1337 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001338 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001339 frame_->EmitPush(tos, result);
ager@chromium.org357bf652010-04-12 11:30:10 +00001340 break;
1341 }
1342
1343 case Token::MOD: {
1344 ASSERT(!reversed);
1345 ASSERT(int_value >= 2);
1346 ASSERT(IsPowerOf2(int_value));
1347 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001348 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001349 unsigned mask = (0x80000000u | kSmiTagMask);
1350 __ tst(tos, Operand(mask));
1351 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1352 mask = (int_value << kSmiTagSize) - 1;
1353 __ and_(tos, tos, Operand(mask));
1354 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001355 // Mod of positive power of 2 Smi gives a Smi if the lhs is an integer.
1356 frame_->EmitPush(
1357 tos,
1358 both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Number());
ager@chromium.org357bf652010-04-12 11:30:10 +00001359 break;
1360 }
1361
1362 case Token::MUL: {
1363 ASSERT(IsEasyToMultiplyBy(int_value));
1364 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001365 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001366 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1367 max_smi_that_wont_overflow <<= kSmiTagSize;
1368 unsigned mask = 0x80000000u;
1369 while ((mask & max_smi_that_wont_overflow) == 0) {
1370 mask |= mask >> 1;
1371 }
1372 mask |= kSmiTagMask;
1373 // This does a single mask that checks for a too high value in a
1374 // conservative way and for a non-Smi. It also filters out negative
1375 // numbers, unfortunately, but since this code is inline we prefer
1376 // brevity to comprehensiveness.
1377 __ tst(tos, Operand(mask));
1378 deferred->Branch(ne);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001379 InlineMultiplyByKnownInt(masm_, tos, tos, int_value);
ager@chromium.org357bf652010-04-12 11:30:10 +00001380 deferred->BindExit();
1381 frame_->EmitPush(tos);
1382 break;
1383 }
1384
1385 default:
1386 UNREACHABLE();
1387 break;
1388 }
1389}
1390
1391
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001392void CodeGenerator::Comparison(Condition cc,
1393 Expression* left,
1394 Expression* right,
1395 bool strict) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001396 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001397
ager@chromium.org357bf652010-04-12 11:30:10 +00001398 if (left != NULL) Load(left);
1399 if (right != NULL) Load(right);
1400
mads.s.ager31e71382008-08-13 09:32:07 +00001401 // sp[0] : y
1402 // sp[1] : x
1403 // result : cc register
1404
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405 // Strict only makes sense for equality comparisons.
1406 ASSERT(!strict || cc == eq);
1407
ager@chromium.org357bf652010-04-12 11:30:10 +00001408 Register lhs;
1409 Register rhs;
1410
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001411 bool lhs_is_smi;
1412 bool rhs_is_smi;
1413
ager@chromium.org357bf652010-04-12 11:30:10 +00001414 // We load the top two stack positions into registers chosen by the virtual
1415 // frame. This should keep the register shuffling to a minimum.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001416 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1417 if (cc == gt || cc == le) {
1418 cc = ReverseCondition(cc);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001419 lhs_is_smi = frame_->KnownSmiAt(0);
1420 rhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001421 lhs = frame_->PopToRegister();
1422 rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001423 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001424 rhs_is_smi = frame_->KnownSmiAt(0);
1425 lhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001426 rhs = frame_->PopToRegister();
1427 lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001428 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001429
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001430 bool both_sides_are_smi = (lhs_is_smi && rhs_is_smi);
1431
ager@chromium.org357bf652010-04-12 11:30:10 +00001432 ASSERT(rhs.is(r0) || rhs.is(r1));
1433 ASSERT(lhs.is(r0) || lhs.is(r1));
1434
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001435 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001437 if (!both_sides_are_smi) {
1438 // Now we have the two sides in r0 and r1. We flush any other registers
1439 // because the stub doesn't know about register allocation.
1440 frame_->SpillAll();
1441 Register scratch = VirtualFrame::scratch0();
1442 Register smi_test_reg;
1443 if (lhs_is_smi) {
1444 smi_test_reg = rhs;
1445 } else if (rhs_is_smi) {
1446 smi_test_reg = lhs;
1447 } else {
1448 __ orr(scratch, lhs, Operand(rhs));
1449 smi_test_reg = scratch;
1450 }
1451 __ tst(smi_test_reg, Operand(kSmiTagMask));
1452 JumpTarget smi;
1453 smi.Branch(eq);
1454
1455 // Perform non-smi comparison by stub.
1456 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1457 // We call with 0 args because there are 0 on the stack.
ager@chromium.orgb5737492010-07-15 09:29:43 +00001458 CompareStub stub(cc, strict, kBothCouldBeNaN, true, lhs, rhs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001459 frame_->CallStub(&stub, 0);
1460 __ cmp(r0, Operand(0));
1461 exit.Jump();
1462
1463 smi.Bind();
ager@chromium.org357bf652010-04-12 11:30:10 +00001464 }
1465
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001466 // Do smi comparisons by pointer comparison.
ager@chromium.org357bf652010-04-12 11:30:10 +00001467 __ cmp(lhs, Operand(rhs));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001469 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001470 cc_reg_ = cc;
1471}
1472
1473
mads.s.ager31e71382008-08-13 09:32:07 +00001474// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001475void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001476 CallFunctionFlags flags,
1477 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001479 int arg_count = args->length();
1480 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001481 Load(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001482 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483
kasper.lund7276f142008-07-30 08:49:36 +00001484 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001485 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486
kasper.lund7276f142008-07-30 08:49:36 +00001487 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001488 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001489 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001490 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491
1492 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001493 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001494 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001495}
1496
1497
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001498void CodeGenerator::CallApplyLazy(Expression* applicand,
1499 Expression* receiver,
1500 VariableProxy* arguments,
1501 int position) {
1502 // An optimized implementation of expressions of the form
1503 // x.apply(y, arguments).
1504 // If the arguments object of the scope has not been allocated,
1505 // and x.apply is Function.prototype.apply, this optimization
1506 // just copies y and the arguments of the current function on the
1507 // stack, as receiver and arguments, and calls x.
1508 // In the implementation comments, we call x the applicand
1509 // and y the receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001510
1511 ASSERT(ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION);
1512 ASSERT(arguments->IsArguments());
1513
1514 // Load applicand.apply onto the stack. This will usually
1515 // give us a megamorphic load site. Not super, but it works.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001516 Load(applicand);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001517 Handle<String> name = Factory::LookupAsciiSymbol("apply");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001518 frame_->Dup();
ager@chromium.orgac091b72010-05-05 07:34:42 +00001519 frame_->CallLoadIC(name, RelocInfo::CODE_TARGET);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001520 frame_->EmitPush(r0);
1521
1522 // Load the receiver and the existing arguments object onto the
1523 // expression stack. Avoid allocating the arguments object here.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001524 Load(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001525 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
1526
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001527 // At this point the top two stack elements are probably in registers
1528 // since they were just loaded. Ensure they are in regs and get the
1529 // regs.
1530 Register receiver_reg = frame_->Peek2();
1531 Register arguments_reg = frame_->Peek();
1532
1533 // From now on the frame is spilled.
1534 frame_->SpillAll();
1535
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001536 // Emit the source position information after having loaded the
1537 // receiver and the arguments.
1538 CodeForSourcePosition(position);
1539 // Contents of the stack at this point:
1540 // sp[0]: arguments object of the current function or the hole.
1541 // sp[1]: receiver
1542 // sp[2]: applicand.apply
1543 // sp[3]: applicand.
1544
1545 // Check if the arguments object has been lazily allocated
1546 // already. If so, just use that instead of copying the arguments
1547 // from the stack. This also deals with cases where a local variable
1548 // named 'arguments' has been introduced.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001549 JumpTarget slow;
1550 Label done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001551 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001552 __ cmp(ip, arguments_reg);
1553 slow.Branch(ne);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001554
1555 Label build_args;
1556 // Get rid of the arguments object probe.
1557 frame_->Drop();
1558 // Stack now has 3 elements on it.
1559 // Contents of stack at this point:
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001560 // sp[0]: receiver - in the receiver_reg register.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001561 // sp[1]: applicand.apply
1562 // sp[2]: applicand.
1563
1564 // Check that the receiver really is a JavaScript object.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001565 __ BranchOnSmi(receiver_reg, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001566 // We allow all JSObjects including JSFunctions. As long as
1567 // JS_FUNCTION_TYPE is the last instance type and it is right
1568 // after LAST_JS_OBJECT_TYPE, we do not have to check the upper
1569 // bound.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00001570 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1571 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001572 __ CompareObjectType(receiver_reg, r2, r3, FIRST_JS_OBJECT_TYPE);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001573 __ b(lt, &build_args);
1574
1575 // Check that applicand.apply is Function.prototype.apply.
1576 __ ldr(r0, MemOperand(sp, kPointerSize));
1577 __ BranchOnSmi(r0, &build_args);
1578 __ CompareObjectType(r0, r1, r2, JS_FUNCTION_TYPE);
1579 __ b(ne, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001580 Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply));
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001581 __ ldr(r1, FieldMemOperand(r0, JSFunction::kCodeEntryOffset));
1582 __ sub(r1, r1, Operand(Code::kHeaderSize - kHeapObjectTag));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001583 __ cmp(r1, Operand(apply_code));
1584 __ b(ne, &build_args);
1585
1586 // Check that applicand is a function.
1587 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1588 __ BranchOnSmi(r1, &build_args);
1589 __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE);
1590 __ b(ne, &build_args);
1591
1592 // Copy the arguments to this function possibly from the
1593 // adaptor frame below it.
1594 Label invoke, adapted;
1595 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1596 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1597 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1598 __ b(eq, &adapted);
1599
1600 // No arguments adaptor frame. Copy fixed number of arguments.
1601 __ mov(r0, Operand(scope()->num_parameters()));
1602 for (int i = 0; i < scope()->num_parameters(); i++) {
1603 __ ldr(r2, frame_->ParameterAt(i));
1604 __ push(r2);
1605 }
1606 __ jmp(&invoke);
1607
1608 // Arguments adaptor frame present. Copy arguments from there, but
1609 // avoid copying too many arguments to avoid stack overflows.
1610 __ bind(&adapted);
1611 static const uint32_t kArgumentsLimit = 1 * KB;
1612 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1613 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
1614 __ mov(r3, r0);
1615 __ cmp(r0, Operand(kArgumentsLimit));
1616 __ b(gt, &build_args);
1617
1618 // Loop through the arguments pushing them onto the execution
1619 // stack. We don't inform the virtual frame of the push, so we don't
1620 // have to worry about getting rid of the elements from the virtual
1621 // frame.
1622 Label loop;
1623 // r3 is a small non-negative integer, due to the test above.
1624 __ cmp(r3, Operand(0));
1625 __ b(eq, &invoke);
1626 // Compute the address of the first argument.
1627 __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2));
1628 __ add(r2, r2, Operand(kPointerSize));
1629 __ bind(&loop);
1630 // Post-decrement argument address by kPointerSize on each iteration.
1631 __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex));
1632 __ push(r4);
1633 __ sub(r3, r3, Operand(1), SetCC);
1634 __ b(gt, &loop);
1635
1636 // Invoke the function.
1637 __ bind(&invoke);
1638 ParameterCount actual(r0);
1639 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1640 // Drop applicand.apply and applicand from the stack, and push
1641 // the result of the function call, but leave the spilled frame
1642 // unchanged, with 3 elements, so it is correct when we compile the
1643 // slow-case code.
1644 __ add(sp, sp, Operand(2 * kPointerSize));
1645 __ push(r0);
1646 // Stack now has 1 element:
1647 // sp[0]: result
1648 __ jmp(&done);
1649
1650 // Slow-case: Allocate the arguments object since we know it isn't
1651 // there, and fall-through to the slow-case where we call
1652 // applicand.apply.
1653 __ bind(&build_args);
1654 // Stack now has 3 elements, because we have jumped from where:
1655 // sp[0]: receiver
1656 // sp[1]: applicand.apply
1657 // sp[2]: applicand.
1658 StoreArgumentsObject(false);
1659
1660 // Stack and frame now have 4 elements.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001661 slow.Bind();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001662
1663 // Generic computation of x.apply(y, args) with no special optimization.
1664 // Flip applicand.apply and applicand on the stack, so
1665 // applicand looks like the receiver of the applicand.apply call.
1666 // Then process it as a normal function call.
1667 __ ldr(r0, MemOperand(sp, 3 * kPointerSize));
1668 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001669 __ Strd(r0, r1, MemOperand(sp, 2 * kPointerSize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001670
1671 CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS);
1672 frame_->CallStub(&call_function, 3);
1673 // The function and its two arguments have been dropped.
1674 frame_->Drop(); // Drop the receiver as well.
1675 frame_->EmitPush(r0);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001676 frame_->SpillAll(); // A spilled frame is also jumping to label done.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001677 // Stack now has 1 element:
1678 // sp[0]: result
1679 __ bind(&done);
1680
1681 // Restore the context register after a call.
1682 __ ldr(cp, frame_->Context());
1683}
1684
1685
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001686void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687 ASSERT(has_cc());
1688 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001689 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 cc_reg_ = al;
1691}
1692
1693
ager@chromium.org7c537e22008-10-16 08:43:32 +00001694void CodeGenerator::CheckStack() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001695 frame_->SpillAll();
ager@chromium.org3811b432009-10-28 14:53:37 +00001696 Comment cmnt(masm_, "[ check stack");
1697 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1698 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1699 // the implicit 8 byte offset that always applies to operations with pc and
1700 // gives a return address 12 bytes down.
1701 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1702 masm_->cmp(sp, Operand(ip));
1703 StackCheckStub stub;
1704 // Call the stub if lower.
1705 masm_->mov(pc,
1706 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1707 RelocInfo::CODE_TARGET),
1708 LeaveCC,
1709 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001710}
1711
1712
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001713void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1714#ifdef DEBUG
1715 int original_height = frame_->height();
1716#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001717 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001718 Visit(statements->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001719 }
1720 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1721}
1722
1723
ager@chromium.org7c537e22008-10-16 08:43:32 +00001724void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001725#ifdef DEBUG
1726 int original_height = frame_->height();
1727#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001728 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001729 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001730 node->break_target()->SetExpectedHeight();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001731 VisitStatements(node->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001732 if (node->break_target()->is_linked()) {
1733 node->break_target()->Bind();
1734 }
1735 node->break_target()->Unuse();
1736 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737}
1738
1739
ager@chromium.org7c537e22008-10-16 08:43:32 +00001740void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org3811b432009-10-28 14:53:37 +00001741 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001742 frame_->EmitPush(Operand(pairs));
1743 frame_->EmitPush(Operand(Smi::FromInt(is_eval() ? 1 : 0)));
1744
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001745 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001746 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747}
1748
1749
ager@chromium.org7c537e22008-10-16 08:43:32 +00001750void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001751#ifdef DEBUG
1752 int original_height = frame_->height();
1753#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001754 Comment cmnt(masm_, "[ Declaration");
1755 Variable* var = node->proxy()->var();
1756 ASSERT(var != NULL); // must have been resolved
1757 Slot* slot = var->slot();
1758
1759 // If it was not possible to allocate the variable at compile time,
1760 // we need to "declare" it at runtime to make sure it actually
1761 // exists in the local context.
1762 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1763 // Variables with a "LOOKUP" slot were introduced as non-locals
1764 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001765 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001767 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001768 frame_->EmitPush(Operand(var->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769 // Declaration nodes are always declared in only two modes.
1770 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1771 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001772 frame_->EmitPush(Operand(Smi::FromInt(attr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773 // Push initial value, if any.
1774 // Note: For variables we must not push an initial value (such as
1775 // 'undefined') because we may have a (legal) redeclaration and we
1776 // must not destroy the current value.
1777 if (node->mode() == Variable::CONST) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001778 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779 } else if (node->fun() != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001780 Load(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781 } else {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001782 frame_->EmitPush(Operand(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00001784
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001785 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001786 // Ignore the return value (declarations are statements).
ager@chromium.orgac091b72010-05-05 07:34:42 +00001787
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001788 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 return;
1790 }
1791
1792 ASSERT(!var->is_global());
1793
1794 // If we have a function or a constant, we need to initialize the variable.
1795 Expression* val = NULL;
1796 if (node->mode() == Variable::CONST) {
1797 val = new Literal(Factory::the_hole_value());
1798 } else {
1799 val = node->fun(); // NULL if we don't have a function
1800 }
1801
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001802
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 if (val != NULL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001804 WriteBarrierCharacter wb_info =
1805 val->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
1806 if (val->AsLiteral() != NULL) wb_info = NEVER_NEWSPACE;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001807 // Set initial value.
1808 Reference target(this, node->proxy());
1809 Load(val);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001810 target.SetValue(NOT_CONST_INIT, wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001811
iposva@chromium.org245aa852009-02-10 00:49:54 +00001812 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001813 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001815 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816}
1817
1818
ager@chromium.org7c537e22008-10-16 08:43:32 +00001819void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001820#ifdef DEBUG
1821 int original_height = frame_->height();
1822#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001823 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001824 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825 Expression* expression = node->expression();
1826 expression->MarkAsStatement();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001827 Load(expression);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001828 frame_->Drop();
1829 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001830}
1831
1832
ager@chromium.org7c537e22008-10-16 08:43:32 +00001833void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001834#ifdef DEBUG
1835 int original_height = frame_->height();
1836#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001838 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001840 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841}
1842
1843
ager@chromium.org7c537e22008-10-16 08:43:32 +00001844void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001845#ifdef DEBUG
1846 int original_height = frame_->height();
1847#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001849 // Generate different code depending on which parts of the if statement
1850 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 bool has_then_stm = node->HasThenStatement();
1852 bool has_else_stm = node->HasElseStatement();
1853
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001854 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001856 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001858 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001859 JumpTarget then;
1860 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001862 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001863 if (frame_ != NULL) {
1864 Branch(false, &else_);
1865 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001867 if (frame_ != NULL || then.is_linked()) {
1868 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001869 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001870 }
1871 if (frame_ != NULL) {
1872 exit.Jump();
1873 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001874 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001875 if (else_.is_linked()) {
1876 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001877 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001878 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879
1880 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001881 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001882 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001883 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001885 LoadCondition(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001886 if (frame_ != NULL) {
1887 Branch(false, &exit);
1888 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001890 if (frame_ != NULL || then.is_linked()) {
1891 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001892 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001893 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001894
1895 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001896 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001898 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899 // if (!cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001900 LoadCondition(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001901 if (frame_ != NULL) {
1902 Branch(true, &exit);
1903 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001904 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001905 if (frame_ != NULL || else_.is_linked()) {
1906 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001907 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001908 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909
1910 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001911 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001912 ASSERT(!has_then_stm && !has_else_stm);
1913 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001914 LoadCondition(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001915 if (frame_ != NULL) {
1916 if (has_cc()) {
1917 cc_reg_ = al;
1918 } else {
1919 frame_->Drop();
1920 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921 }
1922 }
1923
1924 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001925 if (exit.is_linked()) {
1926 exit.Bind();
1927 }
1928 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929}
1930
1931
ager@chromium.org7c537e22008-10-16 08:43:32 +00001932void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001934 CodeForStatementPosition(node);
1935 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936}
1937
1938
ager@chromium.org7c537e22008-10-16 08:43:32 +00001939void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001941 CodeForStatementPosition(node);
1942 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943}
1944
1945
ager@chromium.org7c537e22008-10-16 08:43:32 +00001946void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001948
ager@chromium.org4af710e2009-09-15 12:20:11 +00001949 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001950 Load(node->expression());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001951 frame_->PopToR0();
1952 frame_->PrepareForReturn();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001953 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001954 function_return_.Jump();
1955 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001956 // Pop the result from the frame and prepare the frame for
1957 // returning thus making it easier to merge.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001958 if (function_return_.is_bound()) {
1959 // If the function return label is already bound we reuse the
1960 // code by jumping to the return site.
1961 function_return_.Jump();
1962 } else {
1963 function_return_.Bind();
1964 GenerateReturnSequence();
1965 }
1966 }
1967}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001968
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001969
1970void CodeGenerator::GenerateReturnSequence() {
1971 if (FLAG_trace) {
1972 // Push the return value on the stack as the parameter.
1973 // Runtime::TraceExit returns the parameter as it is.
1974 frame_->EmitPush(r0);
1975 frame_->CallRuntime(Runtime::kTraceExit, 1);
1976 }
1977
1978#ifdef DEBUG
1979 // Add a label for checking the size of the code used for returning.
1980 Label check_exit_codesize;
1981 masm_->bind(&check_exit_codesize);
1982#endif
1983 // Make sure that the constant pool is not emitted inside of the return
1984 // sequence.
1985 { Assembler::BlockConstPoolScope block_const_pool(masm_);
1986 // Tear down the frame which will restore the caller's frame pointer and
1987 // the link register.
1988 frame_->Exit();
1989
1990 // Here we use masm_-> instead of the __ macro to avoid the code coverage
1991 // tool from instrumenting as we rely on the code size here.
1992 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
1993 masm_->add(sp, sp, Operand(sp_delta));
1994 masm_->Jump(lr);
1995 DeleteFrame();
1996
1997#ifdef DEBUG
1998 // Check that the size of the code used for returning matches what is
1999 // expected by the debugger. If the sp_delts above cannot be encoded in
2000 // the add instruction the add will generate two instructions.
2001 int return_sequence_length =
2002 masm_->InstructionsGeneratedSince(&check_exit_codesize);
2003 CHECK(return_sequence_length ==
2004 Assembler::kJSReturnSequenceInstructions ||
2005 return_sequence_length ==
2006 Assembler::kJSReturnSequenceInstructions + 1);
2007#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002008 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009}
2010
2011
ager@chromium.org7c537e22008-10-16 08:43:32 +00002012void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002013#ifdef DEBUG
2014 int original_height = frame_->height();
2015#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002017 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002018 Load(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002019 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002020 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002021 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002022 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002023 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002024#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002025 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002026 __ cmp(r0, cp);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002027 verified_true.Branch(eq);
2028 __ stop("PushContext: r0 is expected to be the same as cp");
2029 verified_true.Bind();
2030#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002032 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002033 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034}
2035
2036
ager@chromium.org7c537e22008-10-16 08:43:32 +00002037void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002038#ifdef DEBUG
2039 int original_height = frame_->height();
2040#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002042 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043 // Pop context.
2044 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
2045 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002046 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002047 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048}
2049
2050
ager@chromium.org7c537e22008-10-16 08:43:32 +00002051void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002052#ifdef DEBUG
2053 int original_height = frame_->height();
2054#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002056 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002057 node->break_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002059 Load(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002060
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002061 JumpTarget next_test;
2062 JumpTarget fall_through;
2063 JumpTarget default_entry;
2064 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065 ZoneList<CaseClause*>* cases = node->cases();
2066 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002067 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068
2069 for (int i = 0; i < length; i++) {
2070 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002071 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002072 // Remember the default clause and compile it at the end.
2073 default_clause = clause;
2074 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 }
2076
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002077 Comment cmnt(masm_, "[ Case clause");
2078 // Compile the test.
2079 next_test.Bind();
2080 next_test.Unuse();
2081 // Duplicate TOS.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002082 frame_->Dup();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002083 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002084 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002085
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002086 // Before entering the body from the test, remove the switch value from
2087 // the stack.
2088 frame_->Drop();
2089
2090 // Label the body so that fall through is enabled.
2091 if (i > 0 && cases->at(i - 1)->is_default()) {
2092 default_exit.Bind();
2093 } else {
2094 fall_through.Bind();
2095 fall_through.Unuse();
2096 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002097 VisitStatements(clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002098
2099 // If control flow can fall through from the body, jump to the next body
2100 // or the end of the statement.
2101 if (frame_ != NULL) {
2102 if (i < length - 1 && cases->at(i + 1)->is_default()) {
2103 default_entry.Jump();
2104 } else {
2105 fall_through.Jump();
2106 }
2107 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108 }
2109
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002110 // The final "test" removes the switch value.
2111 next_test.Bind();
2112 frame_->Drop();
2113
2114 // If there is a default clause, compile it.
2115 if (default_clause != NULL) {
2116 Comment cmnt(masm_, "[ Default clause");
2117 default_entry.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002118 VisitStatements(default_clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002119 // If control flow can fall out of the default and there is a case after
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002120 // it, jump to that case's body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002121 if (frame_ != NULL && default_exit.is_bound()) {
2122 default_exit.Jump();
2123 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002124 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002126 if (fall_through.is_linked()) {
2127 fall_through.Bind();
2128 }
2129
2130 if (node->break_target()->is_linked()) {
2131 node->break_target()->Bind();
2132 }
2133 node->break_target()->Unuse();
2134 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135}
2136
2137
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002138void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002139#ifdef DEBUG
2140 int original_height = frame_->height();
2141#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002142 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002143 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002144 node->break_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002145 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002146 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002148 // Label the top of the loop for the backward CFG edge. If the test
2149 // is always true we can use the continue target, and if the test is
2150 // always false there is no need.
2151 ConditionAnalysis info = AnalyzeCondition(node->cond());
2152 switch (info) {
2153 case ALWAYS_TRUE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002154 node->continue_target()->SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002155 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002156 break;
2157 case ALWAYS_FALSE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002158 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002159 break;
2160 case DONT_KNOW:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002161 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002162 body.Bind();
2163 break;
2164 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002165
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002166 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002167 Visit(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002168
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002169 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002170 switch (info) {
2171 case ALWAYS_TRUE:
2172 // If control can fall off the end of the body, jump back to the
2173 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002174 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002175 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002176 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002178 case ALWAYS_FALSE:
2179 // If we have a continue in the body, we only have to bind its
2180 // jump target.
2181 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002182 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002183 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002184 break;
2185 case DONT_KNOW:
2186 // We have to compile the test expression if it can be reached by
2187 // control flow falling out of the body or via continue.
2188 if (node->continue_target()->is_linked()) {
2189 node->continue_target()->Bind();
2190 }
2191 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002192 Comment cmnt(masm_, "[ DoWhileCondition");
2193 CodeForDoWhileConditionPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002194 LoadCondition(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002195 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002196 // A invalid frame here indicates that control did not
2197 // fall out of the test expression.
2198 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002199 }
2200 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201 break;
2202 }
2203
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002204 if (node->break_target()->is_linked()) {
2205 node->break_target()->Bind();
2206 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002207 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002208 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2209}
2210
2211
2212void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
2213#ifdef DEBUG
2214 int original_height = frame_->height();
2215#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002216 Comment cmnt(masm_, "[ WhileStatement");
2217 CodeForStatementPosition(node);
2218
2219 // If the test is never true and has no side effects there is no need
2220 // to compile the test or body.
2221 ConditionAnalysis info = AnalyzeCondition(node->cond());
2222 if (info == ALWAYS_FALSE) return;
2223
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002224 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002225 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002226
2227 // Label the top of the loop with the continue target for the backward
2228 // CFG edge.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002229 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002230 node->continue_target()->Bind();
2231
2232 if (info == DONT_KNOW) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002233 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002234 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002235 if (has_valid_frame()) {
2236 // A NULL frame indicates that control did not fall out of the
2237 // test expression.
2238 Branch(false, node->break_target());
2239 }
2240 if (has_valid_frame() || body.is_linked()) {
2241 body.Bind();
2242 }
2243 }
2244
2245 if (has_valid_frame()) {
2246 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002247 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002248
2249 // If control flow can fall out of the body, jump back to the top.
2250 if (has_valid_frame()) {
2251 node->continue_target()->Jump();
2252 }
2253 }
2254 if (node->break_target()->is_linked()) {
2255 node->break_target()->Bind();
2256 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002257 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002258 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2259}
2260
2261
2262void CodeGenerator::VisitForStatement(ForStatement* node) {
2263#ifdef DEBUG
2264 int original_height = frame_->height();
2265#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002266 Comment cmnt(masm_, "[ ForStatement");
2267 CodeForStatementPosition(node);
2268 if (node->init() != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002269 Visit(node->init());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002270 }
2271
2272 // If the test is never true there is no need to compile the test or
2273 // body.
2274 ConditionAnalysis info = AnalyzeCondition(node->cond());
2275 if (info == ALWAYS_FALSE) return;
2276
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002277 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002278 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002279
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002280 // We know that the loop index is a smi if it is not modified in the
2281 // loop body and it is checked against a constant limit in the loop
2282 // condition. In this case, we reset the static type information of the
2283 // loop index to smi before compiling the body, the update expression, and
2284 // the bottom check of the loop condition.
2285 TypeInfoCodeGenState type_info_scope(this,
2286 node->is_fast_smi_loop() ?
2287 node->loop_variable()->slot() :
2288 NULL,
2289 TypeInfo::Smi());
2290
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002291 // If there is no update statement, label the top of the loop with the
2292 // continue target, otherwise with the loop target.
2293 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2294 if (node->next() == NULL) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002295 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002296 node->continue_target()->Bind();
2297 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002298 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002299 loop.Bind();
2300 }
2301
2302 // If the test is always true, there is no need to compile it.
2303 if (info == DONT_KNOW) {
2304 JumpTarget body;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002305 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002306 if (has_valid_frame()) {
2307 Branch(false, node->break_target());
2308 }
2309 if (has_valid_frame() || body.is_linked()) {
2310 body.Bind();
2311 }
2312 }
2313
2314 if (has_valid_frame()) {
2315 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002316 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002317
2318 if (node->next() == NULL) {
2319 // If there is no update statement and control flow can fall out
2320 // of the loop, jump directly to the continue label.
2321 if (has_valid_frame()) {
2322 node->continue_target()->Jump();
2323 }
2324 } else {
2325 // If there is an update statement and control flow can reach it
2326 // via falling out of the body of the loop or continuing, we
2327 // compile the update statement.
2328 if (node->continue_target()->is_linked()) {
2329 node->continue_target()->Bind();
2330 }
2331 if (has_valid_frame()) {
2332 // Record source position of the statement as this code which is
2333 // after the code for the body actually belongs to the loop
2334 // statement and not the body.
2335 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002336 Visit(node->next());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002337 loop.Jump();
2338 }
2339 }
2340 }
2341 if (node->break_target()->is_linked()) {
2342 node->break_target()->Bind();
2343 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002344 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002345 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346}
2347
2348
ager@chromium.org7c537e22008-10-16 08:43:32 +00002349void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002350#ifdef DEBUG
2351 int original_height = frame_->height();
2352#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002354 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002355
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002356 JumpTarget primitive;
2357 JumpTarget jsobject;
2358 JumpTarget fixed_array;
2359 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2360 JumpTarget end_del_check;
2361 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002362
2363 // Get the object to enumerate over (converted to JSObject).
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002364 Load(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002365
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002366 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2368 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002369 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002370 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2371 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002372 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002373 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2374 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002375 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002376
2377 // Stack layout in body:
2378 // [iteration counter (Smi)]
2379 // [length of array]
2380 // [FixedArray]
2381 // [Map or 0]
2382 // [Object]
2383
2384 // Check if enumerable is already a JSObject
2385 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002386 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002387 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002388 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002390 primitive.Bind();
2391 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002392 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002393
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002394 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002395 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002396 // r0: value to be iterated over
2397 frame_->EmitPush(r0); // Push the object being iterated over.
2398
2399 // Check cache validity in generated code. This is a fast case for
2400 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2401 // guarantee cache validity, call the runtime system to check cache
2402 // validity or get the property names in a fixed array.
2403 JumpTarget call_runtime;
2404 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2405 JumpTarget check_prototype;
2406 JumpTarget use_cache;
2407 __ mov(r1, Operand(r0));
2408 loop.Bind();
2409 // Check that there are no elements.
2410 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2411 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2412 __ cmp(r2, r4);
2413 call_runtime.Branch(ne);
2414 // Check that instance descriptors are not empty so that we can
2415 // check for an enum cache. Leave the map in r3 for the subsequent
2416 // prototype load.
2417 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2418 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2419 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2420 __ cmp(r2, ip);
2421 call_runtime.Branch(eq);
2422 // Check that there in an enum cache in the non-empty instance
2423 // descriptors. This is the case if the next enumeration index
2424 // field does not contain a smi.
2425 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2426 __ tst(r2, Operand(kSmiTagMask));
2427 call_runtime.Branch(eq);
2428 // For all objects but the receiver, check that the cache is empty.
2429 // r4: empty fixed array root.
2430 __ cmp(r1, r0);
2431 check_prototype.Branch(eq);
2432 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2433 __ cmp(r2, r4);
2434 call_runtime.Branch(ne);
2435 check_prototype.Bind();
2436 // Load the prototype from the map and loop if non-null.
2437 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2438 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2439 __ cmp(r1, ip);
2440 loop.Branch(ne);
2441 // The enum cache is valid. Load the map of the object being
2442 // iterated over and use the cache for the iteration.
2443 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2444 use_cache.Jump();
2445
2446 call_runtime.Bind();
2447 // Call the runtime to get the property names for the object.
2448 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002449 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002450
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002451 // If we got a map from the runtime call, we can do a fast
2452 // modification check. Otherwise, we got a fixed array, and we have
2453 // to do a slow check.
2454 // r0: map or fixed array (result from call to
2455 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002456 __ mov(r2, Operand(r0));
2457 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002458 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2459 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002460 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002462 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002463 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002464 // r0: map (either the result from a call to
2465 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2466 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002467 __ mov(r1, Operand(r0));
2468 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2469 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2470 __ ldr(r2,
2471 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2472
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002473 frame_->EmitPush(r0); // map
2474 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002475 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002476 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002477 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002478 frame_->EmitPush(r0);
2479 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002481 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002482 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002483 frame_->EmitPush(r1); // insert 0 in place of Map
2484 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485
2486 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002487 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002488 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002489 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002490 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002491
2492 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002493 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002494 // sp[0] : index
2495 // sp[1] : array/enum cache length
2496 // sp[2] : array or enum cache
2497 // sp[3] : 0 or map
2498 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002499 // Grab the current frame's height for the break and continue
2500 // targets only after all the state is pushed on the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002501 node->break_target()->SetExpectedHeight();
2502 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002503
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002504 // Load the current count to r0, load the length to r1.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002505 __ Ldrd(r0, r1, frame_->ElementAt(0));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002506 __ cmp(r0, r1); // compare to the array length
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002507 node->break_target()->Branch(hs);
2508
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002509 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002510 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002511 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2512 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2513
2514 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002515 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002516 // Check if this (still) matches the map of the enumerable.
2517 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002518 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002519 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2520 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002521 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002522
2523 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002524 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2525 frame_->EmitPush(r0);
2526 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002527 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002528 __ mov(r3, Operand(r0), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002529 // If the property has been removed while iterating, we just skip it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002530 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002532 end_del_check.Bind();
2533 // Store the entry in the 'each' expression and take another spin in the
2534 // loop. r3: i'th entry of the enum cache (or string there of)
2535 frame_->EmitPush(r3); // push entry
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002536 { VirtualFrame::RegisterAllocationScope scope(this);
2537 Reference each(this, node->each());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002539 if (each.size() > 0) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002540 // Loading a reference may leave the frame in an unspilled state.
2541 frame_->SpillAll(); // Sync stack to memory.
2542 // Get the value (under the reference on the stack) from memory.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002543 __ ldr(r0, frame_->ElementAt(each.size()));
2544 frame_->EmitPush(r0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002545 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002546 frame_->Drop(2); // The result of the set and the extra pushed value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002547 } else {
2548 // If the reference was to a slot we rely on the convenient property
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002549 // that it doesn't matter whether a value (eg, ebx pushed above) is
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002550 // right on top of or right underneath a zero-sized reference.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002551 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002552 frame_->Drop(1); // Drop the result of the set operation.
mads.s.ager31e71382008-08-13 09:32:07 +00002553 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002554 }
2555 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002556 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002557 CheckStack(); // TODO(1222600): ignore if body contains calls.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002558 { VirtualFrame::RegisterAllocationScope scope(this);
2559 Visit(node->body());
2560 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002561
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002562 // Next. Reestablish a spilled frame in case we are coming here via
2563 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002564 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002565 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002566 frame_->EmitPop(r0);
2567 __ add(r0, r0, Operand(Smi::FromInt(1)));
2568 frame_->EmitPush(r0);
2569 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002571 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2572 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002573 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002574 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575
2576 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002577 exit.Bind();
2578 node->continue_target()->Unuse();
2579 node->break_target()->Unuse();
2580 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002581}
2582
2583
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002584void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002585#ifdef DEBUG
2586 int original_height = frame_->height();
2587#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002588 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002589 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002590 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002592 JumpTarget try_block;
2593 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002594
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002595 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002597 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002598
2599 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002600 Variable* catch_var = node->catch_var()->var();
2601 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2602 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603
2604 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002605 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002606
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002607 { VirtualFrame::RegisterAllocationScope scope(this);
2608 VisitStatements(node->catch_block()->statements());
2609 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002610 if (frame_ != NULL) {
2611 exit.Jump();
2612 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002613
2614
2615 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002616 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002617
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002618 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2619 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002620
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002621 // Shadow the labels for all escapes from the try block, including
2622 // returns. During shadowing, the original label is hidden as the
2623 // LabelShadow and operations on the original actually affect the
2624 // shadowing label.
2625 //
2626 // We should probably try to unify the escaping labels and the return
2627 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002628 int nof_escapes = node->escaping_targets()->length();
2629 List<ShadowTarget*> shadows(1 + nof_escapes);
2630
2631 // Add the shadow target for the function return.
2632 static const int kReturnShadowIndex = 0;
2633 shadows.Add(new ShadowTarget(&function_return_));
2634 bool function_return_was_shadowed = function_return_is_shadowed_;
2635 function_return_is_shadowed_ = true;
2636 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2637
2638 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002639 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002640 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641 }
2642
2643 // Generate code for the statements in the try block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002644 { VirtualFrame::RegisterAllocationScope scope(this);
2645 VisitStatements(node->try_block()->statements());
2646 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647
2648 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002649 // After shadowing stops, the original labels are unshadowed and the
2650 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002651 bool has_unlinks = false;
2652 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002654 has_unlinks = has_unlinks || shadows[i]->is_linked();
2655 }
2656 function_return_is_shadowed_ = function_return_was_shadowed;
2657
2658 // Get an external reference to the handler address.
2659 ExternalReference handler_address(Top::k_handler_address);
2660
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002661 // If we can fall off the end of the try block, unlink from try chain.
2662 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002663 // The next handler address is on top of the frame. Unlink from
2664 // the handler list and drop the rest of this handler from the
2665 // frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002666 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002667 frame_->EmitPop(r1); // r0 can contain the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002668 __ mov(r3, Operand(handler_address));
2669 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002670 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002671 if (has_unlinks) {
2672 exit.Jump();
2673 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674 }
2675
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002676 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002677 // jumped to. Deallocate each shadow target.
2678 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002680 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002681 shadows[i]->Bind();
2682 // Because we can be jumping here (to spilled code) from unspilled
2683 // code, we need to reestablish a spilled frame at this block.
2684 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002685
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002686 // Reload sp from the top handler, because some statements that we
2687 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002688 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002690 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002692 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002693 frame_->EmitPop(r1); // r0 can contain the return value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002695 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002696
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002697 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2698 frame_->PrepareForReturn();
2699 }
2700 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701 }
2702 }
2703
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002704 exit.Bind();
2705 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706}
2707
2708
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002709void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002710#ifdef DEBUG
2711 int original_height = frame_->height();
2712#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002713 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002714 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002715 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002716
2717 // State: Used to keep track of reason for entering the finally
2718 // block. Should probably be extended to hold information for
2719 // break/continue from within the try block.
2720 enum { FALLING, THROWING, JUMPING };
2721
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002722 JumpTarget try_block;
2723 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002725 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002726
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002727 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002728 // In case of thrown exceptions, this is where we continue.
2729 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002730 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002731
2732 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002733 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002734
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002735 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2736 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002738 // Shadow the labels for all escapes from the try block, including
2739 // returns. Shadowing hides the original label as the LabelShadow and
2740 // operations on the original actually affect the shadowing label.
2741 //
2742 // We should probably try to unify the escaping labels and the return
2743 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002744 int nof_escapes = node->escaping_targets()->length();
2745 List<ShadowTarget*> shadows(1 + nof_escapes);
2746
2747 // Add the shadow target for the function return.
2748 static const int kReturnShadowIndex = 0;
2749 shadows.Add(new ShadowTarget(&function_return_));
2750 bool function_return_was_shadowed = function_return_is_shadowed_;
2751 function_return_is_shadowed_ = true;
2752 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2753
2754 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002755 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002756 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002757 }
2758
2759 // Generate code for the statements in the try block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002760 { VirtualFrame::RegisterAllocationScope scope(this);
2761 VisitStatements(node->try_block()->statements());
2762 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002763
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002764 // Stop the introduced shadowing and count the number of required unlinks.
2765 // After shadowing stops, the original labels are unshadowed and the
2766 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002768 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002769 shadows[i]->StopShadowing();
2770 if (shadows[i]->is_linked()) nof_unlinks++;
2771 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002772 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002773
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002774 // Get an external reference to the handler address.
2775 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002776
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002777 // If we can fall off the end of the try block, unlink from the try
2778 // chain and set the state on the frame to FALLING.
2779 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002780 // The next handler address is on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002781 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002782 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002783 __ mov(r3, Operand(handler_address));
2784 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002785 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002786
2787 // Fake a top of stack value (unneeded when FALLING) and set the
2788 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002789 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002790 frame_->EmitPush(r0);
2791 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2792 if (nof_unlinks > 0) {
2793 finally_block.Jump();
2794 }
2795 }
2796
2797 // Generate code to unlink and set the state for the (formerly)
2798 // shadowing targets that have been jumped to.
2799 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002800 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002801 // If we have come from the shadowed return, the return value is
2802 // in (a non-refcounted reference to) r0. We must preserve it
2803 // until it is pushed.
2804 //
2805 // Because we can be jumping here (to spilled code) from
2806 // unspilled code, we need to reestablish a spilled frame at
2807 // this block.
2808 shadows[i]->Bind();
2809 frame_->SpillAll();
2810
2811 // Reload sp from the top handler, because some statements that
2812 // we break from (eg, for...in) may have left stuff on the
2813 // stack.
2814 __ mov(r3, Operand(handler_address));
2815 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002816 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002817
2818 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002819 // handler address is currently on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002820 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002821 frame_->EmitPop(r1);
2822 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002823 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002824
2825 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002826 // If this label shadowed the function return, materialize the
2827 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002828 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002829 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002830 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002831 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002832 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002833 }
2834 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002835 if (--nof_unlinks > 0) {
2836 // If this is not the last unlink block, jump around the next.
2837 finally_block.Jump();
2838 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002839 }
2840 }
2841
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002842 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002843 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002844
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002845 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002846 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002847
2848 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002849 // and the state - while evaluating the finally block.
2850 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002851 // Generate code for the statements in the finally block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002852 { VirtualFrame::RegisterAllocationScope scope(this);
2853 VisitStatements(node->finally_block()->statements());
2854 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002855
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002856 if (has_valid_frame()) {
2857 // Restore state and return value or faked TOS.
2858 frame_->EmitPop(r2);
2859 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002860 }
2861
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002862 // Generate code to jump to the right destination for all used
2863 // formerly shadowing targets. Deallocate each shadow target.
2864 for (int i = 0; i < shadows.length(); i++) {
2865 if (has_valid_frame() && shadows[i]->is_bound()) {
2866 JumpTarget* original = shadows[i]->other_target();
2867 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2868 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002869 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002870 skip.Branch(ne);
2871 frame_->PrepareForReturn();
2872 original->Jump();
2873 skip.Bind();
2874 } else {
2875 original->Branch(eq);
2876 }
2877 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002878 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002879
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002880 if (has_valid_frame()) {
2881 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002882 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002883 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2884 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002885
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002886 // Rethrow exception.
2887 frame_->EmitPush(r0);
2888 frame_->CallRuntime(Runtime::kReThrow, 1);
2889
2890 // Done.
2891 exit.Bind();
2892 }
2893 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002894}
2895
2896
ager@chromium.org7c537e22008-10-16 08:43:32 +00002897void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002898#ifdef DEBUG
2899 int original_height = frame_->height();
2900#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002901 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002902 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002903#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002904 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002905#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002906 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002907 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002908}
2909
2910
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002911void CodeGenerator::InstantiateFunction(
2912 Handle<SharedFunctionInfo> function_info) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002913 // Use the fast case closure allocation code that allocates in new
2914 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002915 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002916 FastNewClosureStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002917 frame_->EmitPush(Operand(function_info));
2918 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002919 frame_->CallStub(&stub, 1);
2920 frame_->EmitPush(r0);
2921 } else {
2922 // Create a new closure.
2923 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002924 frame_->EmitPush(Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002925 frame_->CallRuntime(Runtime::kNewClosure, 2);
2926 frame_->EmitPush(r0);
2927 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002928}
2929
2930
ager@chromium.org7c537e22008-10-16 08:43:32 +00002931void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002932#ifdef DEBUG
2933 int original_height = frame_->height();
2934#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002935 Comment cmnt(masm_, "[ FunctionLiteral");
2936
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002937 // Build the function info and instantiate it.
2938 Handle<SharedFunctionInfo> function_info =
2939 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002940 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002941 if (HasStackOverflow()) {
2942 ASSERT(frame_->height() == original_height);
2943 return;
2944 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002945 InstantiateFunction(function_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002946 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002947}
2948
2949
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002950void CodeGenerator::VisitSharedFunctionInfoLiteral(
2951 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002952#ifdef DEBUG
2953 int original_height = frame_->height();
2954#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002955 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2956 InstantiateFunction(node->shared_function_info());
ager@chromium.orgac091b72010-05-05 07:34:42 +00002957 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958}
2959
2960
ager@chromium.org7c537e22008-10-16 08:43:32 +00002961void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002962#ifdef DEBUG
2963 int original_height = frame_->height();
2964#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002965 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002966 JumpTarget then;
2967 JumpTarget else_;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002968 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002969 if (has_valid_frame()) {
2970 Branch(false, &else_);
2971 }
2972 if (has_valid_frame() || then.is_linked()) {
2973 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002974 Load(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002975 }
2976 if (else_.is_linked()) {
2977 JumpTarget exit;
2978 if (has_valid_frame()) exit.Jump();
2979 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002980 Load(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002981 if (exit.is_linked()) exit.Bind();
2982 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00002983 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002984}
2985
2986
ager@chromium.org7c537e22008-10-16 08:43:32 +00002987void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2988 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002989 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002990
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002991 // JumpTargets do not yet support merging frames so the frame must be
2992 // spilled when jumping to these targets.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002993 JumpTarget slow;
2994 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002995
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002996 // Generate fast case for loading from slots that correspond to
2997 // local/global variables or arguments unless they are shadowed by
2998 // eval-introduced bindings.
2999 EmitDynamicLoadFromSlotFastCase(slot,
3000 typeof_state,
3001 &slow,
3002 &done);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003003
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003004 slow.Bind();
3005 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003006 frame_->EmitPush(Operand(slot->var()->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003007
ager@chromium.org7c537e22008-10-16 08:43:32 +00003008 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003009 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003010 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003011 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003012 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00003013
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003014 done.Bind();
3015 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003016
3017 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00003018 Register scratch = VirtualFrame::scratch0();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003019 TypeInfo info = type_info(slot);
3020 frame_->EmitPush(SlotOperand(slot, scratch), info);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003021
ager@chromium.org7c537e22008-10-16 08:43:32 +00003022 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003023 // Const slots may contain 'the hole' value (the constant hasn't been
3024 // initialized yet) which needs to be converted into the 'undefined'
3025 // value.
3026 Comment cmnt(masm_, "[ Unhole const");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003027 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003028 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003029 __ cmp(tos, ip);
3030 __ LoadRoot(tos, Heap::kUndefinedValueRootIndex, eq);
3031 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003032 }
3033 }
3034}
3035
3036
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003037void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot,
3038 TypeofState state) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003039 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003040 LoadFromSlot(slot, state);
3041
3042 // Bail out quickly if we're not using lazy arguments allocation.
3043 if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return;
3044
3045 // ... or if the slot isn't a non-parameter arguments slot.
3046 if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return;
3047
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003048 // Load the loaded value from the stack into a register but leave it on the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003049 // stack.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003050 Register tos = frame_->Peek();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003051
3052 // If the loaded value is the sentinel that indicates that we
3053 // haven't loaded the arguments object yet, we need to do it now.
3054 JumpTarget exit;
3055 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003056 __ cmp(tos, ip);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003057 exit.Branch(ne);
3058 frame_->Drop();
3059 StoreArgumentsObject(false);
3060 exit.Bind();
3061}
3062
3063
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003064void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
3065 ASSERT(slot != NULL);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003066 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003067 if (slot->type() == Slot::LOOKUP) {
3068 ASSERT(slot->var()->is_dynamic());
3069
3070 // For now, just do a runtime call.
3071 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003072 frame_->EmitPush(Operand(slot->var()->name()));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003073
3074 if (init_state == CONST_INIT) {
3075 // Same as the case for a normal store, but ignores attribute
3076 // (e.g. READ_ONLY) of context slot so that we can initialize
3077 // const properties (introduced via eval("const foo = (some
3078 // expr);")). Also, uses the current function context instead of
3079 // the top context.
3080 //
3081 // Note that we must declare the foo upon entry of eval(), via a
3082 // context slot declaration, but we cannot initialize it at the
3083 // same time, because the const declaration may be at the end of
3084 // the eval code (sigh...) and the const variable may have been
3085 // used before (where its value is 'undefined'). Thus, we can only
3086 // do the initialization when we actually encounter the expression
3087 // and when the expression operands are defined and valid, and
3088 // thus we need the split into 2 operations: declaration of the
3089 // context slot followed by initialization.
3090 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3091 } else {
3092 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
3093 }
3094 // Storing a variable must keep the (new) value on the expression
3095 // stack. This is necessary for compiling assignment expressions.
3096 frame_->EmitPush(r0);
3097
3098 } else {
3099 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00003100 Register scratch = VirtualFrame::scratch0();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003101 Register scratch2 = VirtualFrame::scratch1();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003102
ager@chromium.org357bf652010-04-12 11:30:10 +00003103 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003104 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00003105
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003106 if (init_state == CONST_INIT) {
3107 ASSERT(slot->var()->mode() == Variable::CONST);
3108 // Only the first const initialization must be executed (the slot
3109 // still contains 'the hole' value). When the assignment is
3110 // executed, the code is identical to a normal store (see below).
3111 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00003112 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003113 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00003114 __ cmp(scratch, ip);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003115 exit.Branch(ne);
3116 }
3117
3118 // We must execute the store. Storing a variable must keep the
3119 // (new) value on the stack. This is necessary for compiling
3120 // assignment expressions.
3121 //
3122 // Note: We will reach here even with slot->var()->mode() ==
3123 // Variable::CONST because of const declarations which will
3124 // initialize consts to 'the hole' value and by doing so, end up
3125 // calling this code. r2 may be loaded with context; used below in
3126 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00003127 Register tos = frame_->Peek();
3128 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003129 if (slot->type() == Slot::CONTEXT) {
3130 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00003131 __ tst(tos, Operand(kSmiTagMask));
3132 // We don't use tos any more after here.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003133 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00003134 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003135 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003136 // We need an extra register. Until we have a way to do that in the
3137 // virtual frame we will cheat and ask for a free TOS register.
3138 Register scratch3 = frame_->GetTOSRegister();
3139 __ RecordWrite(scratch, Operand(offset), scratch2, scratch3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003140 }
3141 // If we definitely did not jump over the assignment, we do not need
3142 // to bind the exit label. Doing so can defeat peephole
3143 // optimization.
3144 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3145 exit.Bind();
3146 }
3147 }
3148}
3149
3150
ager@chromium.org381abbb2009-02-25 13:23:22 +00003151void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
3152 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003153 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003154 // Check that no extension objects have been created by calls to
3155 // eval from the current scope to the global scope.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003156 Register tmp = frame_->scratch0();
3157 Register tmp2 = frame_->scratch1();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003158 Register context = cp;
3159 Scope* s = scope();
3160 while (s != NULL) {
3161 if (s->num_heap_slots() > 0) {
3162 if (s->calls_eval()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003163 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003164 // Check that extension is NULL.
3165 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
3166 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003167 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003168 }
3169 // Load next context in chain.
3170 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
3171 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3172 context = tmp;
3173 }
3174 // If no outer scope calls eval, we do not need to check more
3175 // context extensions.
3176 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
3177 s = s->outer_scope();
3178 }
3179
3180 if (s->is_eval_scope()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003181 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003182 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00003183 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003184 __ bind(&next);
3185 // Terminate at global context.
3186 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003187 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
3188 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003189 __ b(eq, &fast);
3190 // Check that extension is NULL.
3191 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
3192 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003193 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003194 // Load next context in chain.
3195 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
3196 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3197 __ b(&next);
3198 __ bind(&fast);
3199 }
3200
ager@chromium.org381abbb2009-02-25 13:23:22 +00003201 // Load the global object.
3202 LoadGlobal();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003203 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00003204 frame_->CallLoadIC(slot->var()->name(),
3205 typeof_state == INSIDE_TYPEOF
3206 ? RelocInfo::CODE_TARGET
3207 : RelocInfo::CODE_TARGET_CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003208}
3209
3210
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003211void CodeGenerator::EmitDynamicLoadFromSlotFastCase(Slot* slot,
3212 TypeofState typeof_state,
3213 JumpTarget* slow,
3214 JumpTarget* done) {
3215 // Generate fast-case code for variables that might be shadowed by
3216 // eval-introduced variables. Eval is used a lot without
3217 // introducing variables. In those cases, we do not want to
3218 // perform a runtime call for all variables in the scope
3219 // containing the eval.
3220 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
3221 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, slow);
3222 frame_->SpillAll();
3223 done->Jump();
3224
3225 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
3226 frame_->SpillAll();
3227 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
3228 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
3229 if (potential_slot != NULL) {
3230 // Generate fast case for locals that rewrite to slots.
3231 __ ldr(r0,
3232 ContextSlotOperandCheckExtensions(potential_slot,
3233 r1,
3234 r2,
3235 slow));
3236 if (potential_slot->var()->mode() == Variable::CONST) {
3237 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3238 __ cmp(r0, ip);
3239 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
3240 }
3241 done->Jump();
3242 } else if (rewrite != NULL) {
3243 // Generate fast case for argument loads.
3244 Property* property = rewrite->AsProperty();
3245 if (property != NULL) {
3246 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
3247 Literal* key_literal = property->key()->AsLiteral();
3248 if (obj_proxy != NULL &&
3249 key_literal != NULL &&
3250 obj_proxy->IsArguments() &&
3251 key_literal->handle()->IsSmi()) {
3252 // Load arguments object if there are no eval-introduced
3253 // variables. Then load the argument from the arguments
3254 // object using keyed load.
3255 __ ldr(r0,
3256 ContextSlotOperandCheckExtensions(obj_proxy->var()->slot(),
3257 r1,
3258 r2,
3259 slow));
3260 frame_->EmitPush(r0);
3261 __ mov(r1, Operand(key_literal->handle()));
3262 frame_->EmitPush(r1);
3263 EmitKeyedLoad();
3264 done->Jump();
3265 }
3266 }
3267 }
3268 }
3269}
3270
3271
ager@chromium.org7c537e22008-10-16 08:43:32 +00003272void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003273#ifdef DEBUG
3274 int original_height = frame_->height();
3275#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003276 Comment cmnt(masm_, "[ Slot");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003277 LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003278 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003279}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003280
ager@chromium.org7c537e22008-10-16 08:43:32 +00003281
3282void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003283#ifdef DEBUG
3284 int original_height = frame_->height();
3285#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003286 Comment cmnt(masm_, "[ VariableProxy");
3287
3288 Variable* var = node->var();
3289 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003290 if (expr != NULL) {
3291 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003292 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00003293 ASSERT(var->is_global());
3294 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003295 ref.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003296 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003297 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003298}
3299
3300
ager@chromium.org7c537e22008-10-16 08:43:32 +00003301void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003302#ifdef DEBUG
3303 int original_height = frame_->height();
3304#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003305 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00003306 Register reg = frame_->GetTOSRegister();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003307 bool is_smi = node->handle()->IsSmi();
ager@chromium.org357bf652010-04-12 11:30:10 +00003308 __ mov(reg, Operand(node->handle()));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003309 frame_->EmitPush(reg, is_smi ? TypeInfo::Smi() : TypeInfo::Unknown());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003310 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003311}
3312
3313
ager@chromium.org7c537e22008-10-16 08:43:32 +00003314void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003315#ifdef DEBUG
3316 int original_height = frame_->height();
3317#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003318 Comment cmnt(masm_, "[ RexExp Literal");
3319
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003320 Register tmp = VirtualFrame::scratch0();
3321 // Free up a TOS register that can be used to push the literal.
3322 Register literal = frame_->GetTOSRegister();
3323
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003324 // Retrieve the literal array and check the allocated entry.
3325
3326 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003327 __ ldr(tmp, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003328
3329 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003330 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kLiteralsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003331
3332 // Load the literal at the ast saved index.
3333 int literal_offset =
3334 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003335 __ ldr(literal, FieldMemOperand(tmp, literal_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003336
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003337 JumpTarget materialized;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003338 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003339 __ cmp(literal, ip);
3340 // This branch locks the virtual frame at the done label to match the
3341 // one we have here, where the literal register is not on the stack and
3342 // nothing is spilled.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003343 materialized.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003344
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003345 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003346 // the literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003347 // literal array (0)
3348 frame_->EmitPush(tmp);
3349 // literal index (1)
3350 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3351 // RegExp pattern (2)
3352 frame_->EmitPush(Operand(node->pattern()));
3353 // RegExp flags (3)
3354 frame_->EmitPush(Operand(node->flags()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003355 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003356 __ Move(literal, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003357
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003358 materialized.Bind();
3359
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003360 frame_->EmitPush(literal);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003361 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
3362 frame_->EmitPush(Operand(Smi::FromInt(size)));
3363 frame_->CallRuntime(Runtime::kAllocateInNewSpace, 1);
3364 // TODO(lrn): Use AllocateInNewSpace macro with fallback to runtime.
3365 // r0 is newly allocated space.
3366
3367 // Reuse literal variable with (possibly) a new register, still holding
3368 // the materialized boilerplate.
3369 literal = frame_->PopToRegister(r0);
3370
3371 __ CopyFields(r0, literal, tmp.bit(), size / kPointerSize);
3372
3373 // Push the clone.
3374 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003375 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003376}
3377
3378
ager@chromium.org7c537e22008-10-16 08:43:32 +00003379void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003380#ifdef DEBUG
3381 int original_height = frame_->height();
3382#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003383 Comment cmnt(masm_, "[ ObjectLiteral");
3384
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003385 Register literal = frame_->GetTOSRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003386 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003387 __ ldr(literal, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003388 // Literal array.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003389 __ ldr(literal, FieldMemOperand(literal, JSFunction::kLiteralsOffset));
3390 frame_->EmitPush(literal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003391 // Literal index.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003392 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003393 // Constant properties.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003394 frame_->EmitPush(Operand(node->constant_properties()));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003395 // Should the object literal have fast elements?
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003396 frame_->EmitPush(Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003397 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003398 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003399 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003400 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003401 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003402 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003403 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003404 // At the start of each iteration, the top of stack contains
3405 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003406 ObjectLiteral::Property* property = node->properties()->at(i);
3407 Literal* key = property->key();
3408 Expression* value = property->value();
3409 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003410 case ObjectLiteral::Property::CONSTANT:
3411 break;
3412 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3413 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3414 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003415 case ObjectLiteral::Property::COMPUTED:
3416 if (key->handle()->IsSymbol()) {
3417 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003418 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003419 frame_->PopToR0();
3420 // Fetch the object literal.
3421 frame_->SpillAllButCopyTOSToR1();
ager@chromium.org5c838252010-02-19 08:53:10 +00003422 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003423 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3424 break;
3425 }
3426 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003427 case ObjectLiteral::Property::PROTOTYPE: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003428 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003429 Load(key);
3430 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003431 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003432 break;
3433 }
3434 case ObjectLiteral::Property::SETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003435 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003436 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003437 frame_->EmitPush(Operand(Smi::FromInt(1)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003438 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003439 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003440 break;
3441 }
3442 case ObjectLiteral::Property::GETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003443 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003444 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003445 frame_->EmitPush(Operand(Smi::FromInt(0)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003446 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003447 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003448 break;
3449 }
3450 }
3451 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003452 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003453}
3454
3455
ager@chromium.org7c537e22008-10-16 08:43:32 +00003456void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003457#ifdef DEBUG
3458 int original_height = frame_->height();
3459#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003460 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003461
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003462 Register tos = frame_->GetTOSRegister();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003463 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003464 __ ldr(tos, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003465 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003466 __ ldr(tos, FieldMemOperand(tos, JSFunction::kLiteralsOffset));
3467 frame_->EmitPush(tos);
3468 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3469 frame_->EmitPush(Operand(node->constant_elements()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003470 int length = node->values()->length();
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003471 if (node->constant_elements()->map() == Heap::fixed_cow_array_map()) {
3472 FastCloneShallowArrayStub stub(
3473 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
3474 frame_->CallStub(&stub, 3);
3475 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
3476 } else if (node->depth() > 1) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003477 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003478 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003479 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003480 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003481 FastCloneShallowArrayStub stub(
3482 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
ager@chromium.org5c838252010-02-19 08:53:10 +00003483 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003484 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003485 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003486 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003487
3488 // Generate code to set the elements in the array that are not
3489 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003490 for (int i = 0; i < node->values()->length(); i++) {
3491 Expression* value = node->values()->at(i);
3492
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003493 // If value is a literal the property value is already set in the
3494 // boilerplate object.
3495 if (value->AsLiteral() != NULL) continue;
3496 // If value is a materialized literal the property value is already set
3497 // in the boilerplate object if it is simple.
3498 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003499
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003500 // The property must be set by generated code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003501 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003502 frame_->PopToR0();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003503 // Fetch the object literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003504 frame_->SpillAllButCopyTOSToR1();
3505
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003506 // Get the elements array.
3507 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003508
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003509 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003510 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003511 __ str(r0, FieldMemOperand(r1, offset));
3512
3513 // Update the write barrier for the array address.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003514 __ RecordWrite(r1, Operand(offset), r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003515 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003516 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003517}
3518
3519
ager@chromium.org32912102009-01-16 10:38:43 +00003520void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003521#ifdef DEBUG
3522 int original_height = frame_->height();
3523#endif
ager@chromium.org32912102009-01-16 10:38:43 +00003524 // Call runtime routine to allocate the catch extension object and
3525 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003526 Comment cmnt(masm_, "[ CatchExtensionObject");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003527 Load(node->key());
3528 Load(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003529 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3530 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003531 ASSERT_EQ(original_height + 1, frame_->height());
3532}
3533
3534
3535void CodeGenerator::EmitSlotAssignment(Assignment* node) {
3536#ifdef DEBUG
3537 int original_height = frame_->height();
3538#endif
3539 Comment cmnt(masm(), "[ Variable Assignment");
3540 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3541 ASSERT(var != NULL);
3542 Slot* slot = var->slot();
3543 ASSERT(slot != NULL);
3544
3545 // Evaluate the right-hand side.
3546 if (node->is_compound()) {
3547 // For a compound assignment the right-hand side is a binary operation
3548 // between the current property value and the actual right-hand side.
3549 LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
3550
3551 // Perform the binary operation.
3552 Literal* literal = node->value()->AsLiteral();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003553 bool overwrite_value = node->value()->ResultOverwriteAllowed();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003554 if (literal != NULL && literal->handle()->IsSmi()) {
3555 SmiOperation(node->binary_op(),
3556 literal->handle(),
3557 false,
3558 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3559 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003560 GenerateInlineSmi inline_smi =
3561 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3562 if (literal != NULL) {
3563 ASSERT(!literal->handle()->IsSmi());
3564 inline_smi = DONT_GENERATE_INLINE_SMI;
3565 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003566 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003567 GenericBinaryOperation(node->binary_op(),
3568 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3569 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003570 }
3571 } else {
3572 Load(node->value());
3573 }
3574
3575 // Perform the assignment.
3576 if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) {
3577 CodeForSourcePosition(node->position());
3578 StoreToSlot(slot,
3579 node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT);
3580 }
3581 ASSERT_EQ(original_height + 1, frame_->height());
3582}
3583
3584
3585void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) {
3586#ifdef DEBUG
3587 int original_height = frame_->height();
3588#endif
3589 Comment cmnt(masm(), "[ Named Property Assignment");
3590 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3591 Property* prop = node->target()->AsProperty();
3592 ASSERT(var == NULL || (prop == NULL && var->is_global()));
3593
3594 // Initialize name and evaluate the receiver sub-expression if necessary. If
3595 // the receiver is trivial it is not placed on the stack at this point, but
3596 // loaded whenever actually needed.
3597 Handle<String> name;
3598 bool is_trivial_receiver = false;
3599 if (var != NULL) {
3600 name = var->name();
3601 } else {
3602 Literal* lit = prop->key()->AsLiteral();
3603 ASSERT_NOT_NULL(lit);
3604 name = Handle<String>::cast(lit->handle());
3605 // Do not materialize the receiver on the frame if it is trivial.
3606 is_trivial_receiver = prop->obj()->IsTrivial();
3607 if (!is_trivial_receiver) Load(prop->obj());
3608 }
3609
3610 // Change to slow case in the beginning of an initialization block to
3611 // avoid the quadratic behavior of repeatedly adding fast properties.
3612 if (node->starts_initialization_block()) {
3613 // Initialization block consists of assignments of the form expr.x = ..., so
3614 // this will never be an assignment to a variable, so there must be a
3615 // receiver object.
3616 ASSERT_EQ(NULL, var);
3617 if (is_trivial_receiver) {
3618 Load(prop->obj());
3619 } else {
3620 frame_->Dup();
3621 }
3622 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3623 }
3624
3625 // Change to fast case at the end of an initialization block. To prepare for
3626 // that add an extra copy of the receiver to the frame, so that it can be
3627 // converted back to fast case after the assignment.
3628 if (node->ends_initialization_block() && !is_trivial_receiver) {
3629 frame_->Dup();
3630 }
3631
3632 // Stack layout:
3633 // [tos] : receiver (only materialized if non-trivial)
3634 // [tos+1] : receiver if at the end of an initialization block
3635
3636 // Evaluate the right-hand side.
3637 if (node->is_compound()) {
3638 // For a compound assignment the right-hand side is a binary operation
3639 // between the current property value and the actual right-hand side.
3640 if (is_trivial_receiver) {
3641 Load(prop->obj());
3642 } else if (var != NULL) {
3643 LoadGlobal();
3644 } else {
3645 frame_->Dup();
3646 }
3647 EmitNamedLoad(name, var != NULL);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003648
3649 // Perform the binary operation.
3650 Literal* literal = node->value()->AsLiteral();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003651 bool overwrite_value = node->value()->ResultOverwriteAllowed();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003652 if (literal != NULL && literal->handle()->IsSmi()) {
3653 SmiOperation(node->binary_op(),
3654 literal->handle(),
3655 false,
3656 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3657 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003658 GenerateInlineSmi inline_smi =
3659 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3660 if (literal != NULL) {
3661 ASSERT(!literal->handle()->IsSmi());
3662 inline_smi = DONT_GENERATE_INLINE_SMI;
3663 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003664 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003665 GenericBinaryOperation(node->binary_op(),
3666 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3667 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003668 }
3669 } else {
3670 // For non-compound assignment just load the right-hand side.
3671 Load(node->value());
3672 }
3673
3674 // Stack layout:
3675 // [tos] : value
3676 // [tos+1] : receiver (only materialized if non-trivial)
3677 // [tos+2] : receiver if at the end of an initialization block
3678
3679 // Perform the assignment. It is safe to ignore constants here.
3680 ASSERT(var == NULL || var->mode() != Variable::CONST);
3681 ASSERT_NE(Token::INIT_CONST, node->op());
3682 if (is_trivial_receiver) {
3683 // Load the receiver and swap with the value.
3684 Load(prop->obj());
3685 Register t0 = frame_->PopToRegister();
3686 Register t1 = frame_->PopToRegister(t0);
3687 frame_->EmitPush(t0);
3688 frame_->EmitPush(t1);
3689 }
3690 CodeForSourcePosition(node->position());
3691 bool is_contextual = (var != NULL);
3692 EmitNamedStore(name, is_contextual);
3693 frame_->EmitPush(r0);
3694
3695 // Change to fast case at the end of an initialization block.
3696 if (node->ends_initialization_block()) {
3697 ASSERT_EQ(NULL, var);
3698 // The argument to the runtime call is the receiver.
3699 if (is_trivial_receiver) {
3700 Load(prop->obj());
3701 } else {
3702 // A copy of the receiver is below the value of the assignment. Swap
3703 // the receiver and the value of the assignment expression.
3704 Register t0 = frame_->PopToRegister();
3705 Register t1 = frame_->PopToRegister(t0);
3706 frame_->EmitPush(t0);
3707 frame_->EmitPush(t1);
3708 }
3709 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3710 }
3711
3712 // Stack layout:
3713 // [tos] : result
3714
3715 ASSERT_EQ(original_height + 1, frame_->height());
3716}
3717
3718
3719void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) {
3720#ifdef DEBUG
3721 int original_height = frame_->height();
3722#endif
3723 Comment cmnt(masm_, "[ Keyed Property Assignment");
3724 Property* prop = node->target()->AsProperty();
3725 ASSERT_NOT_NULL(prop);
3726
3727 // Evaluate the receiver subexpression.
3728 Load(prop->obj());
3729
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003730 WriteBarrierCharacter wb_info;
3731
ager@chromium.orgac091b72010-05-05 07:34:42 +00003732 // Change to slow case in the beginning of an initialization block to
3733 // avoid the quadratic behavior of repeatedly adding fast properties.
3734 if (node->starts_initialization_block()) {
3735 frame_->Dup();
3736 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3737 }
3738
3739 // Change to fast case at the end of an initialization block. To prepare for
3740 // that add an extra copy of the receiver to the frame, so that it can be
3741 // converted back to fast case after the assignment.
3742 if (node->ends_initialization_block()) {
3743 frame_->Dup();
3744 }
3745
3746 // Evaluate the key subexpression.
3747 Load(prop->key());
3748
3749 // Stack layout:
3750 // [tos] : key
3751 // [tos+1] : receiver
3752 // [tos+2] : receiver if at the end of an initialization block
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003753 //
ager@chromium.orgac091b72010-05-05 07:34:42 +00003754 // Evaluate the right-hand side.
3755 if (node->is_compound()) {
3756 // For a compound assignment the right-hand side is a binary operation
3757 // between the current property value and the actual right-hand side.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003758 // Duplicate receiver and key for loading the current property value.
3759 frame_->Dup2();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003760 EmitKeyedLoad();
3761 frame_->EmitPush(r0);
3762
3763 // Perform the binary operation.
3764 Literal* literal = node->value()->AsLiteral();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003765 bool overwrite_value = node->value()->ResultOverwriteAllowed();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003766 if (literal != NULL && literal->handle()->IsSmi()) {
3767 SmiOperation(node->binary_op(),
3768 literal->handle(),
3769 false,
3770 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3771 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003772 GenerateInlineSmi inline_smi =
3773 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3774 if (literal != NULL) {
3775 ASSERT(!literal->handle()->IsSmi());
3776 inline_smi = DONT_GENERATE_INLINE_SMI;
3777 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003778 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003779 GenericBinaryOperation(node->binary_op(),
3780 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3781 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003782 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003783 wb_info = node->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
ager@chromium.orgac091b72010-05-05 07:34:42 +00003784 } else {
3785 // For non-compound assignment just load the right-hand side.
3786 Load(node->value());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003787 wb_info = node->value()->AsLiteral() != NULL ?
3788 NEVER_NEWSPACE :
3789 (node->value()->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003790 }
3791
3792 // Stack layout:
3793 // [tos] : value
3794 // [tos+1] : key
3795 // [tos+2] : receiver
3796 // [tos+3] : receiver if at the end of an initialization block
3797
3798 // Perform the assignment. It is safe to ignore constants here.
3799 ASSERT(node->op() != Token::INIT_CONST);
3800 CodeForSourcePosition(node->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003801 EmitKeyedStore(prop->key()->type(), wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003802 frame_->EmitPush(r0);
3803
3804 // Stack layout:
3805 // [tos] : result
3806 // [tos+1] : receiver if at the end of an initialization block
3807
3808 // Change to fast case at the end of an initialization block.
3809 if (node->ends_initialization_block()) {
3810 // The argument to the runtime call is the extra copy of the receiver,
3811 // which is below the value of the assignment. Swap the receiver and
3812 // the value of the assignment expression.
3813 Register t0 = frame_->PopToRegister();
3814 Register t1 = frame_->PopToRegister(t0);
3815 frame_->EmitPush(t1);
3816 frame_->EmitPush(t0);
3817 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3818 }
3819
3820 // Stack layout:
3821 // [tos] : result
3822
3823 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org32912102009-01-16 10:38:43 +00003824}
3825
3826
ager@chromium.org7c537e22008-10-16 08:43:32 +00003827void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003828 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003829#ifdef DEBUG
3830 int original_height = frame_->height();
3831#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003832 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003833
ager@chromium.orgac091b72010-05-05 07:34:42 +00003834 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3835 Property* prop = node->target()->AsProperty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003836
ager@chromium.orgac091b72010-05-05 07:34:42 +00003837 if (var != NULL && !var->is_global()) {
3838 EmitSlotAssignment(node);
mads.s.ager31e71382008-08-13 09:32:07 +00003839
ager@chromium.orgac091b72010-05-05 07:34:42 +00003840 } else if ((prop != NULL && prop->key()->IsPropertyName()) ||
3841 (var != NULL && var->is_global())) {
3842 // Properties whose keys are property names and global variables are
3843 // treated as named property references. We do not need to consider
3844 // global 'this' because it is not a valid left-hand side.
3845 EmitNamedPropertyAssignment(node);
3846
3847 } else if (prop != NULL) {
3848 // Other properties (including rewritten parameters for a function that
3849 // uses arguments) are keyed property assignments.
3850 EmitKeyedPropertyAssignment(node);
3851
3852 } else {
3853 // Invalid left-hand side.
3854 Load(node->target());
3855 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
3856 // The runtime call doesn't actually return but the code generator will
3857 // still generate code and expects a certain frame height.
3858 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003859 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003860 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003861}
3862
3863
ager@chromium.org7c537e22008-10-16 08:43:32 +00003864void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003865#ifdef DEBUG
3866 int original_height = frame_->height();
3867#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003868 Comment cmnt(masm_, "[ Throw");
3869
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003870 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003871 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003872 frame_->CallRuntime(Runtime::kThrow, 1);
3873 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003874 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003875}
3876
3877
ager@chromium.org7c537e22008-10-16 08:43:32 +00003878void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003879#ifdef DEBUG
3880 int original_height = frame_->height();
3881#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003882 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003883
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003884 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003885 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003886 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003887 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003888}
3889
3890
ager@chromium.org7c537e22008-10-16 08:43:32 +00003891void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003892#ifdef DEBUG
3893 int original_height = frame_->height();
3894#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003895 Comment cmnt(masm_, "[ Call");
3896
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003897 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003898 ZoneList<Expression*>* args = node->arguments();
3899
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003900 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003901 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003902 Variable* var = function->AsVariableProxy()->AsVariable();
3903 Property* property = function->AsProperty();
3904
3905 // ------------------------------------------------------------------------
3906 // Fast-case: Use inline caching.
3907 // ---
3908 // According to ECMA-262, section 11.2.3, page 44, the function to call
3909 // must be resolved after the arguments have been evaluated. The IC code
3910 // automatically handles this by loading the arguments before the function
3911 // is resolved in cache misses (this also holds for megamorphic calls).
3912 // ------------------------------------------------------------------------
3913
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003914 if (var != NULL && var->is_possibly_eval()) {
3915 // ----------------------------------
3916 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3917 // ----------------------------------
3918
3919 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3920 // resolve the function we need to call and the receiver of the
3921 // call. Then we call the resolved function using the given
3922 // arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003923
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003924 // Prepare stack for call to resolved function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003925 Load(function);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003926
3927 // Allocate a frame slot for the receiver.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003928 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003929
3930 // Load the arguments.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003931 int arg_count = args->length();
3932 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003933 Load(args->at(i));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003934 }
3935
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003936 VirtualFrame::SpilledScope spilled_scope(frame_);
3937
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003938 // If we know that eval can only be shadowed by eval-introduced
3939 // variables we attempt to load the global eval function directly
3940 // in generated code. If we succeed, there is no need to perform a
3941 // context lookup in the runtime system.
3942 JumpTarget done;
3943 if (var->slot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
3944 ASSERT(var->slot()->type() == Slot::LOOKUP);
3945 JumpTarget slow;
3946 // Prepare the stack for the call to
3947 // ResolvePossiblyDirectEvalNoLookup by pushing the loaded
3948 // function, the first argument to the eval call and the
3949 // receiver.
3950 LoadFromGlobalSlotCheckExtensions(var->slot(),
3951 NOT_INSIDE_TYPEOF,
3952 &slow);
3953 frame_->EmitPush(r0);
3954 if (arg_count > 0) {
3955 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3956 frame_->EmitPush(r1);
3957 } else {
3958 frame_->EmitPush(r2);
3959 }
3960 __ ldr(r1, frame_->Receiver());
3961 frame_->EmitPush(r1);
3962
3963 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEvalNoLookup, 3);
3964
3965 done.Jump();
3966 slow.Bind();
3967 }
3968
3969 // Prepare the stack for the call to ResolvePossiblyDirectEval by
3970 // pushing the loaded function, the first argument to the eval
3971 // call and the receiver.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003972 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3973 frame_->EmitPush(r1);
3974 if (arg_count > 0) {
3975 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3976 frame_->EmitPush(r1);
3977 } else {
3978 frame_->EmitPush(r2);
3979 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003980 __ ldr(r1, frame_->Receiver());
3981 frame_->EmitPush(r1);
3982
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003983 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003984 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003985
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003986 // If we generated fast-case code bind the jump-target where fast
3987 // and slow case merge.
3988 if (done.is_linked()) done.Bind();
3989
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003990 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003991 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003992 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3993
3994 // Call the function.
3995 CodeForSourcePosition(node->position());
3996
3997 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003998 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003999 frame_->CallStub(&call_function, arg_count + 1);
4000
4001 __ ldr(cp, frame_->Context());
4002 // Remove the function from the stack.
4003 frame_->Drop();
4004 frame_->EmitPush(r0);
4005
4006 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004007 // ----------------------------------
4008 // JavaScript example: 'foo(1, 2, 3)' // foo is global
4009 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004010 // Pass the global object as the receiver and let the IC stub
4011 // patch the stack to use the global proxy as 'this' in the
4012 // invoked function.
4013 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004014
4015 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004016 int arg_count = args->length();
4017 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004018 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004019 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004020
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004021 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +00004022 // Setup the name register and call the IC initialization code.
4023 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004024 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4025 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004026 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004027 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
4028 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004029 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004030 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004031
4032 } else if (var != NULL && var->slot() != NULL &&
4033 var->slot()->type() == Slot::LOOKUP) {
4034 // ----------------------------------
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004035 // JavaScript examples:
4036 //
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004037 // with (obj) foo(1, 2, 3) // foo may be in obj.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004038 //
4039 // function f() {};
4040 // function g() {
4041 // eval(...);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004042 // f(); // f could be in extension object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004043 // }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004044 // ----------------------------------
4045
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004046 JumpTarget slow, done;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004047
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004048 // Generate fast case for loading functions from slots that
4049 // correspond to local/global variables or arguments unless they
4050 // are shadowed by eval-introduced bindings.
4051 EmitDynamicLoadFromSlotFastCase(var->slot(),
4052 NOT_INSIDE_TYPEOF,
4053 &slow,
4054 &done);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004055
4056 slow.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004057 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004058 frame_->EmitPush(cp);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004059 frame_->EmitPush(Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004060 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004061 // r0: slot value; r1: receiver
4062
4063 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004064 frame_->EmitPush(r0); // function
4065 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004066
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004067 // If fast case code has been generated, emit code to push the
4068 // function and receiver and have the slow path jump around this
4069 // code.
4070 if (done.is_linked()) {
4071 JumpTarget call;
4072 call.Jump();
4073 done.Bind();
4074 frame_->EmitPush(r0); // function
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004075 LoadGlobalReceiver(VirtualFrame::scratch0()); // receiver
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004076 call.Bind();
4077 }
4078
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004079 // Call the function. At this point, everything is spilled but the
4080 // function and receiver are in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004081 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004082 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004083
4084 } else if (property != NULL) {
4085 // Check if the key is a literal string.
4086 Literal* literal = property->key()->AsLiteral();
4087
4088 if (literal != NULL && literal->handle()->IsSymbol()) {
4089 // ------------------------------------------------------------------
4090 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
4091 // ------------------------------------------------------------------
4092
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004093 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004094
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004095 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
4096 name->IsEqualTo(CStrVector("apply")) &&
4097 args->length() == 2 &&
4098 args->at(1)->AsVariableProxy() != NULL &&
4099 args->at(1)->AsVariableProxy()->IsArguments()) {
4100 // Use the optimized Function.prototype.apply that avoids
4101 // allocating lazily allocated arguments objects.
4102 CallApplyLazy(property->obj(),
4103 args->at(0),
4104 args->at(1)->AsVariableProxy(),
4105 node->position());
4106
4107 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004108 Load(property->obj()); // Receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004109 // Load the arguments.
4110 int arg_count = args->length();
4111 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004112 Load(args->at(i));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004113 }
4114
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004115 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004116 // Set the name register and call the IC initialization code.
4117 __ mov(r2, Operand(name));
4118 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4119 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
4120 CodeForSourcePosition(node->position());
4121 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4122 __ ldr(cp, frame_->Context());
4123 frame_->EmitPush(r0);
4124 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004125
4126 } else {
4127 // -------------------------------------------
4128 // JavaScript example: 'array[index](1, 2, 3)'
4129 // -------------------------------------------
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004130 Load(property->obj());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004131 if (property->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004132 Load(property->key());
4133 EmitKeyedLoad();
4134 // Put the function below the receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004135 // Use the global receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004136 frame_->EmitPush(r0); // Function.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004137 LoadGlobalReceiver(VirtualFrame::scratch0());
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004138 // Call the function.
4139 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
4140 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004141 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004142 // Load the arguments.
4143 int arg_count = args->length();
4144 for (int i = 0; i < arg_count; i++) {
4145 Load(args->at(i));
4146 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004147
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004148 // Set the name register and call the IC initialization code.
4149 Load(property->key());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004150 frame_->SpillAll();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004151 frame_->EmitPop(r2); // Function name.
4152
4153 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4154 Handle<Code> stub = ComputeKeyedCallInitialize(arg_count, in_loop);
4155 CodeForSourcePosition(node->position());
4156 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4157 __ ldr(cp, frame_->Context());
4158 frame_->EmitPush(r0);
4159 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004160 }
4161
4162 } else {
4163 // ----------------------------------
4164 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
4165 // ----------------------------------
4166
4167 // Load the function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004168 Load(function);
4169
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004170 // Pass the global proxy as the receiver.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004171 LoadGlobalReceiver(VirtualFrame::scratch0());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004172
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004173 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004174 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004175 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004176 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004177 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004178}
4179
4180
ager@chromium.org7c537e22008-10-16 08:43:32 +00004181void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004182#ifdef DEBUG
4183 int original_height = frame_->height();
4184#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004185 Comment cmnt(masm_, "[ CallNew");
4186
4187 // According to ECMA-262, section 11.2.2, page 44, the function
4188 // expression in new calls must be evaluated before the
4189 // arguments. This is different from ordinary calls, where the
4190 // actual function to call is resolved after the arguments have been
4191 // evaluated.
4192
ricow@chromium.org65fae842010-08-25 15:26:24 +00004193 // Push constructor on the stack. If it's not a function it's used as
4194 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
4195 // ignored.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004196 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004197
4198 // Push the arguments ("left-to-right") on the stack.
4199 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004200 int arg_count = args->length();
4201 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004202 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004203 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004204
ricow@chromium.org65fae842010-08-25 15:26:24 +00004205 // Spill everything from here to simplify the implementation.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004206 VirtualFrame::SpilledScope spilled_scope(frame_);
4207
ricow@chromium.org65fae842010-08-25 15:26:24 +00004208 // Load the argument count into r0 and the function into r1 as per
4209 // calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004210 __ mov(r0, Operand(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004211 __ ldr(r1, frame_->ElementAt(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004212
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004213 // Call the construct call builtin that handles allocation and
4214 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004215 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004216 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004217 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004218 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004219
ager@chromium.orgac091b72010-05-05 07:34:42 +00004220 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004221}
4222
4223
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004224void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004225 Register scratch = VirtualFrame::scratch0();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004226 JumpTarget null, function, leave, non_function_constructor;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004227
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004228 // Load the object into register.
4229 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004230 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004231 Register tos = frame_->PopToRegister();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004232
4233 // If the object is a smi, we return null.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004234 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004235 null.Branch(eq);
4236
4237 // Check that the object is a JS object but take special care of JS
4238 // functions to make sure they have 'Function' as their class.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004239 __ CompareObjectType(tos, tos, scratch, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004240 null.Branch(lt);
4241
4242 // As long as JS_FUNCTION_TYPE is the last instance type and it is
4243 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
4244 // LAST_JS_OBJECT_TYPE.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00004245 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4246 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004247 __ cmp(scratch, Operand(JS_FUNCTION_TYPE));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004248 function.Branch(eq);
4249
4250 // Check if the constructor in the map is a function.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004251 __ ldr(tos, FieldMemOperand(tos, Map::kConstructorOffset));
4252 __ CompareObjectType(tos, scratch, scratch, JS_FUNCTION_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004253 non_function_constructor.Branch(ne);
4254
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004255 // The tos register now contains the constructor function. Grab the
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004256 // instance class name from there.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004257 __ ldr(tos, FieldMemOperand(tos, JSFunction::kSharedFunctionInfoOffset));
4258 __ ldr(tos,
4259 FieldMemOperand(tos, SharedFunctionInfo::kInstanceClassNameOffset));
4260 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004261 leave.Jump();
4262
4263 // Functions have class 'Function'.
4264 function.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004265 __ mov(tos, Operand(Factory::function_class_symbol()));
4266 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004267 leave.Jump();
4268
4269 // Objects with a non-function constructor have class 'Object'.
4270 non_function_constructor.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004271 __ mov(tos, Operand(Factory::Object_symbol()));
4272 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004273 leave.Jump();
4274
4275 // Non-JS objects have class null.
4276 null.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004277 __ LoadRoot(tos, Heap::kNullValueRootIndex);
4278 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004279
4280 // All done.
4281 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004282}
4283
4284
ager@chromium.org7c537e22008-10-16 08:43:32 +00004285void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004286 Register scratch = VirtualFrame::scratch0();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004287 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004288
4289 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004290 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004291 Register tos = frame_->PopToRegister(); // tos contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00004292 // if (object->IsSmi()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004293 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004294 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004295 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004296 __ CompareObjectType(tos, scratch, scratch, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004297 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004298 // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004299 __ ldr(tos, FieldMemOperand(tos, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004300 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004301 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004302}
4303
4304
ager@chromium.org7c537e22008-10-16 08:43:32 +00004305void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004306 Register scratch1 = VirtualFrame::scratch0();
4307 Register scratch2 = VirtualFrame::scratch1();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004308 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004309
4310 ASSERT(args->length() == 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004311 Load(args->at(0)); // Load the object.
4312 Load(args->at(1)); // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004313 Register value = frame_->PopToRegister();
4314 Register object = frame_->PopToRegister(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004315 // if (object->IsSmi()) return object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004316 __ tst(object, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004317 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004318 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004319 __ CompareObjectType(object, scratch1, scratch1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004320 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004321 // Store the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004322 __ str(value, FieldMemOperand(object, JSValue::kValueOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004323 // Update the write barrier.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004324 __ RecordWrite(object,
4325 Operand(JSValue::kValueOffset - kHeapObjectTag),
4326 scratch1,
4327 scratch2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004328 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004329 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004330 frame_->EmitPush(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004331}
4332
4333
ager@chromium.org7c537e22008-10-16 08:43:32 +00004334void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004335 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004336 Load(args->at(0));
4337 Register reg = frame_->PopToRegister();
4338 __ tst(reg, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004339 cc_reg_ = eq;
4340}
4341
4342
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004343void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
4344 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
4345 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004346#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004347 if (ShouldGenerateLog(args->at(0))) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004348 Load(args->at(1));
4349 Load(args->at(2));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004350 frame_->CallRuntime(Runtime::kLog, 2);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004351 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004352#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004353 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004354}
4355
4356
ager@chromium.org7c537e22008-10-16 08:43:32 +00004357void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004358 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004359 Load(args->at(0));
4360 Register reg = frame_->PopToRegister();
4361 __ tst(reg, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004362 cc_reg_ = eq;
4363}
4364
4365
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004366// Generates the Math.pow method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004367void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
4368 ASSERT(args->length() == 2);
4369 Load(args->at(0));
4370 Load(args->at(1));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004371
4372 if (!CpuFeatures::IsSupported(VFP3)) {
4373 frame_->CallRuntime(Runtime::kMath_pow, 2);
4374 frame_->EmitPush(r0);
4375 } else {
4376 CpuFeatures::Scope scope(VFP3);
4377 JumpTarget runtime, done;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004378 Label exponent_nonsmi, base_nonsmi, powi, not_minus_half, allocate_return;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004379
4380 Register scratch1 = VirtualFrame::scratch0();
4381 Register scratch2 = VirtualFrame::scratch1();
4382
4383 // Get base and exponent to registers.
4384 Register exponent = frame_->PopToRegister();
4385 Register base = frame_->PopToRegister(exponent);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004386 Register heap_number_map = no_reg;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004387
4388 // Set the frame for the runtime jump target. The code below jumps to the
4389 // jump target label so the frame needs to be established before that.
4390 ASSERT(runtime.entry_frame() == NULL);
4391 runtime.set_entry_frame(frame_);
4392
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004393 __ BranchOnNotSmi(exponent, &exponent_nonsmi);
4394 __ BranchOnNotSmi(base, &base_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004395
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004396 heap_number_map = r6;
4397 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4398
4399 // Exponent is a smi and base is a smi. Get the smi value into vfp register
4400 // d1.
4401 __ SmiToDoubleVFPRegister(base, d1, scratch1, s0);
4402 __ b(&powi);
4403
4404 __ bind(&base_nonsmi);
4405 // Exponent is smi and base is non smi. Get the double value from the base
4406 // into vfp register d1.
4407 __ ObjectToDoubleVFPRegister(base, d1,
4408 scratch1, scratch2, heap_number_map, s0,
4409 runtime.entry_label());
4410
4411 __ bind(&powi);
4412
4413 // Load 1.0 into d0.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004414 __ vmov(d0, 1.0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004415
4416 // Get the absolute untagged value of the exponent and use that for the
4417 // calculation.
4418 __ mov(scratch1, Operand(exponent, ASR, kSmiTagSize), SetCC);
4419 __ rsb(scratch1, scratch1, Operand(0), LeaveCC, mi); // Negate if negative.
4420 __ vmov(d2, d0, mi); // 1.0 needed in d2 later if exponent is negative.
4421
4422 // Run through all the bits in the exponent. The result is calculated in d0
4423 // and d1 holds base^(bit^2).
4424 Label more_bits;
4425 __ bind(&more_bits);
4426 __ mov(scratch1, Operand(scratch1, LSR, 1), SetCC);
4427 __ vmul(d0, d0, d1, cs); // Multiply with base^(bit^2) if bit is set.
4428 __ vmul(d1, d1, d1, ne); // Don't bother calculating next d1 if done.
4429 __ b(ne, &more_bits);
4430
4431 // If exponent is positive we are done.
4432 __ cmp(exponent, Operand(0));
4433 __ b(ge, &allocate_return);
4434
4435 // If exponent is negative result is 1/result (d2 already holds 1.0 in that
4436 // case). However if d0 has reached infinity this will not provide the
4437 // correct result, so call runtime if that is the case.
4438 __ mov(scratch2, Operand(0x7FF00000));
4439 __ mov(scratch1, Operand(0));
4440 __ vmov(d1, scratch1, scratch2); // Load infinity into d1.
4441 __ vcmp(d0, d1);
4442 __ vmrs(pc);
4443 runtime.Branch(eq); // d0 reached infinity.
4444 __ vdiv(d0, d2, d0);
4445 __ b(&allocate_return);
4446
4447 __ bind(&exponent_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004448 // Special handling of raising to the power of -0.5 and 0.5. First check
4449 // that the value is a heap number and that the lower bits (which for both
4450 // values are zero).
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004451 heap_number_map = r6;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004452 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4453 __ ldr(scratch1, FieldMemOperand(exponent, HeapObject::kMapOffset));
4454 __ ldr(scratch2, FieldMemOperand(exponent, HeapNumber::kMantissaOffset));
4455 __ cmp(scratch1, heap_number_map);
4456 runtime.Branch(ne);
4457 __ tst(scratch2, scratch2);
4458 runtime.Branch(ne);
4459
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004460 // Load the higher bits (which contains the floating point exponent).
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004461 __ ldr(scratch1, FieldMemOperand(exponent, HeapNumber::kExponentOffset));
4462
4463 // Compare exponent with -0.5.
4464 __ cmp(scratch1, Operand(0xbfe00000));
4465 __ b(ne, &not_minus_half);
4466
4467 // Get the double value from the base into vfp register d0.
4468 __ ObjectToDoubleVFPRegister(base, d0,
4469 scratch1, scratch2, heap_number_map, s0,
4470 runtime.entry_label(),
4471 AVOID_NANS_AND_INFINITIES);
4472
4473 // Load 1.0 into d2.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004474 __ vmov(d2, 1.0);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004475
4476 // Calculate the reciprocal of the square root. 1/sqrt(x) = sqrt(1/x).
4477 __ vdiv(d0, d2, d0);
4478 __ vsqrt(d0, d0);
4479
4480 __ b(&allocate_return);
4481
4482 __ bind(&not_minus_half);
4483 // Compare exponent with 0.5.
4484 __ cmp(scratch1, Operand(0x3fe00000));
4485 runtime.Branch(ne);
4486
4487 // Get the double value from the base into vfp register d0.
4488 __ ObjectToDoubleVFPRegister(base, d0,
4489 scratch1, scratch2, heap_number_map, s0,
4490 runtime.entry_label(),
4491 AVOID_NANS_AND_INFINITIES);
4492 __ vsqrt(d0, d0);
4493
4494 __ bind(&allocate_return);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004495 Register scratch3 = r5;
4496 __ AllocateHeapNumberWithValue(scratch3, d0, scratch1, scratch2,
4497 heap_number_map, runtime.entry_label());
4498 __ mov(base, scratch3);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004499 done.Jump();
4500
4501 runtime.Bind();
4502
4503 // Push back the arguments again for the runtime call.
4504 frame_->EmitPush(base);
4505 frame_->EmitPush(exponent);
4506 frame_->CallRuntime(Runtime::kMath_pow, 2);
4507 __ Move(base, r0);
4508
4509 done.Bind();
4510 frame_->EmitPush(base);
4511 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004512}
4513
4514
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004515// Generates the Math.sqrt method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004516void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
4517 ASSERT(args->length() == 1);
4518 Load(args->at(0));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004519
4520 if (!CpuFeatures::IsSupported(VFP3)) {
4521 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4522 frame_->EmitPush(r0);
4523 } else {
4524 CpuFeatures::Scope scope(VFP3);
4525 JumpTarget runtime, done;
4526
4527 Register scratch1 = VirtualFrame::scratch0();
4528 Register scratch2 = VirtualFrame::scratch1();
4529
4530 // Get the value from the frame.
4531 Register tos = frame_->PopToRegister();
4532
4533 // Set the frame for the runtime jump target. The code below jumps to the
4534 // jump target label so the frame needs to be established before that.
4535 ASSERT(runtime.entry_frame() == NULL);
4536 runtime.set_entry_frame(frame_);
4537
4538 Register heap_number_map = r6;
4539 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4540
4541 // Get the double value from the heap number into vfp register d0.
4542 __ ObjectToDoubleVFPRegister(tos, d0,
4543 scratch1, scratch2, heap_number_map, s0,
4544 runtime.entry_label());
4545
4546 // Calculate the square root of d0 and place result in a heap number object.
4547 __ vsqrt(d0, d0);
4548 __ AllocateHeapNumberWithValue(
4549 tos, d0, scratch1, scratch2, heap_number_map, runtime.entry_label());
4550 done.Jump();
4551
4552 runtime.Bind();
4553 // Push back the argument again for the runtime call.
4554 frame_->EmitPush(tos);
4555 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4556 __ Move(tos, r0);
4557
4558 done.Bind();
4559 frame_->EmitPush(tos);
4560 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004561}
4562
4563
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004564class DeferredStringCharCodeAt : public DeferredCode {
4565 public:
4566 DeferredStringCharCodeAt(Register object,
4567 Register index,
4568 Register scratch,
4569 Register result)
4570 : result_(result),
4571 char_code_at_generator_(object,
4572 index,
4573 scratch,
4574 result,
4575 &need_conversion_,
4576 &need_conversion_,
4577 &index_out_of_range_,
4578 STRING_INDEX_IS_NUMBER) {}
4579
4580 StringCharCodeAtGenerator* fast_case_generator() {
4581 return &char_code_at_generator_;
4582 }
4583
4584 virtual void Generate() {
4585 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4586 char_code_at_generator_.GenerateSlow(masm(), call_helper);
4587
4588 __ bind(&need_conversion_);
4589 // Move the undefined value into the result register, which will
4590 // trigger conversion.
4591 __ LoadRoot(result_, Heap::kUndefinedValueRootIndex);
4592 __ jmp(exit_label());
4593
4594 __ bind(&index_out_of_range_);
4595 // When the index is out of range, the spec requires us to return
4596 // NaN.
4597 __ LoadRoot(result_, Heap::kNanValueRootIndex);
4598 __ jmp(exit_label());
4599 }
4600
4601 private:
4602 Register result_;
4603
4604 Label need_conversion_;
4605 Label index_out_of_range_;
4606
4607 StringCharCodeAtGenerator char_code_at_generator_;
4608};
4609
4610
4611// This generates code that performs a String.prototype.charCodeAt() call
4612// or returns a smi in order to trigger conversion.
4613void CodeGenerator::GenerateStringCharCodeAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004614 Comment(masm_, "[ GenerateStringCharCodeAt");
kasper.lund7276f142008-07-30 08:49:36 +00004615 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004616
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004617 Load(args->at(0));
4618 Load(args->at(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004619
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004620 Register index = frame_->PopToRegister();
4621 Register object = frame_->PopToRegister(index);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004622
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004623 // We need two extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004624 Register scratch = VirtualFrame::scratch0();
4625 Register result = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004626
4627 DeferredStringCharCodeAt* deferred =
4628 new DeferredStringCharCodeAt(object,
4629 index,
4630 scratch,
4631 result);
4632 deferred->fast_case_generator()->GenerateFast(masm_);
4633 deferred->BindExit();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004634 frame_->EmitPush(result);
kasper.lund7276f142008-07-30 08:49:36 +00004635}
4636
4637
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004638class DeferredStringCharFromCode : public DeferredCode {
4639 public:
4640 DeferredStringCharFromCode(Register code,
4641 Register result)
4642 : char_from_code_generator_(code, result) {}
4643
4644 StringCharFromCodeGenerator* fast_case_generator() {
4645 return &char_from_code_generator_;
4646 }
4647
4648 virtual void Generate() {
4649 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4650 char_from_code_generator_.GenerateSlow(masm(), call_helper);
4651 }
4652
4653 private:
4654 StringCharFromCodeGenerator char_from_code_generator_;
4655};
4656
4657
4658// Generates code for creating a one-char string from a char code.
4659void CodeGenerator::GenerateStringCharFromCode(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004660 Comment(masm_, "[ GenerateStringCharFromCode");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004661 ASSERT(args->length() == 1);
4662
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004663 Load(args->at(0));
4664
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004665 Register result = frame_->GetTOSRegister();
4666 Register code = frame_->PopToRegister(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004667
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004668 DeferredStringCharFromCode* deferred = new DeferredStringCharFromCode(
4669 code, result);
4670 deferred->fast_case_generator()->GenerateFast(masm_);
4671 deferred->BindExit();
4672 frame_->EmitPush(result);
4673}
4674
4675
4676class DeferredStringCharAt : public DeferredCode {
4677 public:
4678 DeferredStringCharAt(Register object,
4679 Register index,
4680 Register scratch1,
4681 Register scratch2,
4682 Register result)
4683 : result_(result),
4684 char_at_generator_(object,
4685 index,
4686 scratch1,
4687 scratch2,
4688 result,
4689 &need_conversion_,
4690 &need_conversion_,
4691 &index_out_of_range_,
4692 STRING_INDEX_IS_NUMBER) {}
4693
4694 StringCharAtGenerator* fast_case_generator() {
4695 return &char_at_generator_;
4696 }
4697
4698 virtual void Generate() {
4699 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4700 char_at_generator_.GenerateSlow(masm(), call_helper);
4701
4702 __ bind(&need_conversion_);
4703 // Move smi zero into the result register, which will trigger
4704 // conversion.
4705 __ mov(result_, Operand(Smi::FromInt(0)));
4706 __ jmp(exit_label());
4707
4708 __ bind(&index_out_of_range_);
4709 // When the index is out of range, the spec requires us to return
4710 // the empty string.
4711 __ LoadRoot(result_, Heap::kEmptyStringRootIndex);
4712 __ jmp(exit_label());
4713 }
4714
4715 private:
4716 Register result_;
4717
4718 Label need_conversion_;
4719 Label index_out_of_range_;
4720
4721 StringCharAtGenerator char_at_generator_;
4722};
4723
4724
4725// This generates code that performs a String.prototype.charAt() call
4726// or returns a smi in order to trigger conversion.
4727void CodeGenerator::GenerateStringCharAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004728 Comment(masm_, "[ GenerateStringCharAt");
4729 ASSERT(args->length() == 2);
4730
4731 Load(args->at(0));
4732 Load(args->at(1));
4733
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004734 Register index = frame_->PopToRegister();
4735 Register object = frame_->PopToRegister(index);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004736
4737 // We need three extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004738 Register scratch1 = VirtualFrame::scratch0();
4739 Register scratch2 = VirtualFrame::scratch1();
4740 // Use r6 without notifying the virtual frame.
4741 Register result = r6;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004742
4743 DeferredStringCharAt* deferred =
4744 new DeferredStringCharAt(object,
4745 index,
4746 scratch1,
4747 scratch2,
4748 result);
4749 deferred->fast_case_generator()->GenerateFast(masm_);
4750 deferred->BindExit();
ager@chromium.orgac091b72010-05-05 07:34:42 +00004751 frame_->EmitPush(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004752}
4753
4754
ager@chromium.org7c537e22008-10-16 08:43:32 +00004755void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004756 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004757 Load(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004758 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004759 // We need the CC bits to come out as not_equal in the case where the
4760 // object is a smi. This can't be done with the usual test opcode so
4761 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004762 Register possible_array = frame_->PopToRegister();
4763 Register scratch = VirtualFrame::scratch0();
4764 __ and_(scratch, possible_array, Operand(kSmiTagMask));
4765 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004766 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004767 // It is a heap object - get the map. Check if the object is a JS array.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004768 __ CompareObjectType(possible_array, scratch, scratch, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004769 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004770 cc_reg_ = eq;
4771}
4772
4773
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004774void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004775 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004776 Load(args->at(0));
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004777 JumpTarget answer;
4778 // We need the CC bits to come out as not_equal in the case where the
4779 // object is a smi. This can't be done with the usual test opcode so
4780 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004781 Register possible_regexp = frame_->PopToRegister();
4782 Register scratch = VirtualFrame::scratch0();
4783 __ and_(scratch, possible_regexp, Operand(kSmiTagMask));
4784 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004785 answer.Branch(ne);
4786 // It is a heap object - get the map. Check if the object is a regexp.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004787 __ CompareObjectType(possible_regexp, scratch, scratch, JS_REGEXP_TYPE);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004788 answer.Bind();
4789 cc_reg_ = eq;
4790}
4791
4792
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004793void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
4794 // This generates a fast version of:
4795 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004796 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004797 Load(args->at(0));
4798 Register possible_object = frame_->PopToRegister();
4799 __ tst(possible_object, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004800 false_target()->Branch(eq);
4801
4802 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004803 __ cmp(possible_object, ip);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004804 true_target()->Branch(eq);
4805
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004806 Register map_reg = VirtualFrame::scratch0();
4807 __ ldr(map_reg, FieldMemOperand(possible_object, HeapObject::kMapOffset));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004808 // Undetectable objects behave like undefined when tested with typeof.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004809 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4810 __ tst(possible_object, Operand(1 << Map::kIsUndetectable));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004811 false_target()->Branch(ne);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004812
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004813 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4814 __ cmp(possible_object, Operand(FIRST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004815 false_target()->Branch(lt);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004816 __ cmp(possible_object, Operand(LAST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004817 cc_reg_ = le;
4818}
4819
4820
ricow@chromium.org4980dff2010-07-19 08:33:45 +00004821void CodeGenerator::GenerateIsSpecObject(ZoneList<Expression*>* args) {
4822 // This generates a fast version of:
4823 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp' ||
4824 // typeof(arg) == function).
4825 // It includes undetectable objects (as opposed to IsObject).
4826 ASSERT(args->length() == 1);
4827 Load(args->at(0));
4828 Register value = frame_->PopToRegister();
4829 __ tst(value, Operand(kSmiTagMask));
4830 false_target()->Branch(eq);
4831 // Check that this is an object.
4832 __ ldr(value, FieldMemOperand(value, HeapObject::kMapOffset));
4833 __ ldrb(value, FieldMemOperand(value, Map::kInstanceTypeOffset));
4834 __ cmp(value, Operand(FIRST_JS_OBJECT_TYPE));
4835 cc_reg_ = ge;
4836}
4837
4838
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004839// Deferred code to check whether the String JavaScript object is safe for using
4840// default value of. This code is called after the bit caching this information
4841// in the map has been checked with the map for the object in the map_result_
4842// register. On return the register map_result_ contains 1 for true and 0 for
4843// false.
4844class DeferredIsStringWrapperSafeForDefaultValueOf : public DeferredCode {
4845 public:
4846 DeferredIsStringWrapperSafeForDefaultValueOf(Register object,
4847 Register map_result,
4848 Register scratch1,
4849 Register scratch2)
4850 : object_(object),
4851 map_result_(map_result),
4852 scratch1_(scratch1),
4853 scratch2_(scratch2) { }
4854
4855 virtual void Generate() {
4856 Label false_result;
4857
4858 // Check that map is loaded as expected.
4859 if (FLAG_debug_code) {
4860 __ ldr(ip, FieldMemOperand(object_, HeapObject::kMapOffset));
4861 __ cmp(map_result_, ip);
4862 __ Assert(eq, "Map not in expected register");
4863 }
4864
4865 // Check for fast case object. Generate false result for slow case object.
4866 __ ldr(scratch1_, FieldMemOperand(object_, JSObject::kPropertiesOffset));
4867 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
4868 __ LoadRoot(ip, Heap::kHashTableMapRootIndex);
4869 __ cmp(scratch1_, ip);
4870 __ b(eq, &false_result);
4871
4872 // Look for valueOf symbol in the descriptor array, and indicate false if
4873 // found. The type is not checked, so if it is a transition it is a false
4874 // negative.
4875 __ ldr(map_result_,
4876 FieldMemOperand(map_result_, Map::kInstanceDescriptorsOffset));
4877 __ ldr(scratch2_, FieldMemOperand(map_result_, FixedArray::kLengthOffset));
4878 // map_result_: descriptor array
4879 // scratch2_: length of descriptor array
4880 // Calculate the end of the descriptor array.
4881 STATIC_ASSERT(kSmiTag == 0);
4882 STATIC_ASSERT(kSmiTagSize == 1);
4883 STATIC_ASSERT(kPointerSize == 4);
4884 __ add(scratch1_,
4885 map_result_,
4886 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4887 __ add(scratch1_,
4888 scratch1_,
4889 Operand(scratch2_, LSL, kPointerSizeLog2 - kSmiTagSize));
4890
4891 // Calculate location of the first key name.
4892 __ add(map_result_,
4893 map_result_,
4894 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
4895 DescriptorArray::kFirstIndex * kPointerSize));
4896 // Loop through all the keys in the descriptor array. If one of these is the
4897 // symbol valueOf the result is false.
4898 Label entry, loop;
4899 // The use of ip to store the valueOf symbol asumes that it is not otherwise
4900 // used in the loop below.
4901 __ mov(ip, Operand(Factory::value_of_symbol()));
4902 __ jmp(&entry);
4903 __ bind(&loop);
4904 __ ldr(scratch2_, MemOperand(map_result_, 0));
4905 __ cmp(scratch2_, ip);
4906 __ b(eq, &false_result);
4907 __ add(map_result_, map_result_, Operand(kPointerSize));
4908 __ bind(&entry);
4909 __ cmp(map_result_, Operand(scratch1_));
4910 __ b(ne, &loop);
4911
4912 // Reload map as register map_result_ was used as temporary above.
4913 __ ldr(map_result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4914
4915 // If a valueOf property is not found on the object check that it's
4916 // prototype is the un-modified String prototype. If not result is false.
4917 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kPrototypeOffset));
4918 __ tst(scratch1_, Operand(kSmiTagMask));
4919 __ b(eq, &false_result);
4920 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
4921 __ ldr(scratch2_,
4922 CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX));
4923 __ ldr(scratch2_,
4924 FieldMemOperand(scratch2_, GlobalObject::kGlobalContextOffset));
4925 __ ldr(scratch2_,
4926 CodeGenerator::ContextOperand(
4927 scratch2_, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
4928 __ cmp(scratch1_, scratch2_);
4929 __ b(ne, &false_result);
4930
4931 // Set the bit in the map to indicate that it has been checked safe for
4932 // default valueOf and set true result.
4933 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
4934 __ orr(scratch1_,
4935 scratch1_,
4936 Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
4937 __ str(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
4938 __ mov(map_result_, Operand(1));
4939 __ jmp(exit_label());
4940 __ bind(&false_result);
4941 // Set false result.
4942 __ mov(map_result_, Operand(0));
4943 }
4944
4945 private:
4946 Register object_;
4947 Register map_result_;
4948 Register scratch1_;
4949 Register scratch2_;
4950};
4951
4952
4953void CodeGenerator::GenerateIsStringWrapperSafeForDefaultValueOf(
4954 ZoneList<Expression*>* args) {
4955 ASSERT(args->length() == 1);
4956 Load(args->at(0));
4957 Register obj = frame_->PopToRegister(); // Pop the string wrapper.
4958 if (FLAG_debug_code) {
4959 __ AbortIfSmi(obj);
4960 }
4961
4962 // Check whether this map has already been checked to be safe for default
4963 // valueOf.
4964 Register map_result = VirtualFrame::scratch0();
4965 __ ldr(map_result, FieldMemOperand(obj, HeapObject::kMapOffset));
4966 __ ldrb(ip, FieldMemOperand(map_result, Map::kBitField2Offset));
4967 __ tst(ip, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
4968 true_target()->Branch(ne);
4969
4970 // We need an additional two scratch registers for the deferred code.
4971 Register scratch1 = VirtualFrame::scratch1();
4972 // Use r6 without notifying the virtual frame.
4973 Register scratch2 = r6;
4974
4975 DeferredIsStringWrapperSafeForDefaultValueOf* deferred =
4976 new DeferredIsStringWrapperSafeForDefaultValueOf(
4977 obj, map_result, scratch1, scratch2);
4978 deferred->Branch(eq);
4979 deferred->BindExit();
4980 __ tst(map_result, Operand(map_result));
4981 cc_reg_ = ne;
4982}
4983
4984
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004985void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
4986 // This generates a fast version of:
4987 // (%_ClassOf(arg) === 'Function')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004988 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004989 Load(args->at(0));
4990 Register possible_function = frame_->PopToRegister();
4991 __ tst(possible_function, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004992 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004993 Register map_reg = VirtualFrame::scratch0();
4994 Register scratch = VirtualFrame::scratch1();
4995 __ CompareObjectType(possible_function, map_reg, scratch, JS_FUNCTION_TYPE);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004996 cc_reg_ = eq;
4997}
4998
4999
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005000void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005001 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005002 Load(args->at(0));
5003 Register possible_undetectable = frame_->PopToRegister();
5004 __ tst(possible_undetectable, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005005 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005006 Register scratch = VirtualFrame::scratch0();
5007 __ ldr(scratch,
5008 FieldMemOperand(possible_undetectable, HeapObject::kMapOffset));
5009 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5010 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005011 cc_reg_ = ne;
5012}
5013
5014
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005015void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005016 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005017
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005018 Register scratch0 = VirtualFrame::scratch0();
5019 Register scratch1 = VirtualFrame::scratch1();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005020 // Get the frame pointer for the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005021 __ ldr(scratch0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005022
5023 // Skip the arguments adaptor frame if it exists.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005024 __ ldr(scratch1,
5025 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5026 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5027 __ ldr(scratch0,
5028 MemOperand(scratch0, StandardFrameConstants::kCallerFPOffset), eq);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005029
5030 // Check the marker in the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005031 __ ldr(scratch1,
5032 MemOperand(scratch0, StandardFrameConstants::kMarkerOffset));
5033 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005034 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005035}
5036
5037
ager@chromium.org7c537e22008-10-16 08:43:32 +00005038void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005039 ASSERT(args->length() == 0);
5040
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005041 Register tos = frame_->GetTOSRegister();
5042 Register scratch0 = VirtualFrame::scratch0();
5043 Register scratch1 = VirtualFrame::scratch1();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005044
lrn@chromium.org25156de2010-04-06 13:10:27 +00005045 // Check if the calling frame is an arguments adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005046 __ ldr(scratch0,
5047 MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5048 __ ldr(scratch1,
5049 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5050 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5051
5052 // Get the number of formal parameters.
5053 __ mov(tos, Operand(Smi::FromInt(scope()->num_parameters())), LeaveCC, ne);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005054
5055 // Arguments adaptor case: Read the arguments length from the
5056 // adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005057 __ ldr(tos,
5058 MemOperand(scratch0, ArgumentsAdaptorFrameConstants::kLengthOffset),
5059 eq);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005060
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005061 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005062}
5063
5064
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00005065void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005066 ASSERT(args->length() == 1);
5067
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00005068 // Satisfy contract with ArgumentsAccessStub:
5069 // Load the key into r1 and the formal parameters count into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005070 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005071 frame_->PopToR1();
5072 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005073 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005074
5075 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005076 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005077 frame_->CallStub(&stub, 0);
5078 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005079}
5080
5081
ager@chromium.org357bf652010-04-12 11:30:10 +00005082void CodeGenerator::GenerateRandomHeapNumber(
5083 ZoneList<Expression*>* args) {
5084 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005085 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00005086
5087 Label slow_allocate_heapnumber;
5088 Label heapnumber_allocated;
5089
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005090 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
5091 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00005092 __ jmp(&heapnumber_allocated);
5093
5094 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00005095 // Allocate a heap number.
5096 __ CallRuntime(Runtime::kNumberAlloc, 0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005097 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00005098
5099 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005100
5101 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
5102 // by computing:
5103 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
5104 if (CpuFeatures::IsSupported(VFP3)) {
5105 __ PrepareCallCFunction(0, r1);
5106 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
5107
5108 CpuFeatures::Scope scope(VFP3);
5109 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
5110 // Create this constant using mov/orr to avoid PC relative load.
5111 __ mov(r1, Operand(0x41000000));
5112 __ orr(r1, r1, Operand(0x300000));
5113 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
5114 __ vmov(d7, r0, r1);
5115 // Move 0x4130000000000000 to VFP.
5116 __ mov(r0, Operand(0));
5117 __ vmov(d8, r0, r1);
5118 // Subtract and store the result in the heap number.
5119 __ vsub(d7, d7, d8);
5120 __ sub(r0, r4, Operand(kHeapObjectTag));
5121 __ vstr(d7, r0, HeapNumber::kValueOffset);
5122 frame_->EmitPush(r4);
5123 } else {
5124 __ mov(r0, Operand(r4));
5125 __ PrepareCallCFunction(1, r1);
5126 __ CallCFunction(
5127 ExternalReference::fill_heap_number_with_random_function(), 1);
5128 frame_->EmitPush(r0);
5129 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005130}
5131
5132
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005133void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
5134 ASSERT_EQ(2, args->length());
5135
5136 Load(args->at(0));
5137 Load(args->at(1));
5138
ager@chromium.org5c838252010-02-19 08:53:10 +00005139 StringAddStub stub(NO_STRING_ADD_FLAGS);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005140 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005141 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005142 frame_->EmitPush(r0);
5143}
5144
5145
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005146void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
5147 ASSERT_EQ(3, args->length());
5148
5149 Load(args->at(0));
5150 Load(args->at(1));
5151 Load(args->at(2));
5152
ager@chromium.org5c838252010-02-19 08:53:10 +00005153 SubStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005154 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005155 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005156 frame_->EmitPush(r0);
5157}
5158
5159
5160void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
5161 ASSERT_EQ(2, args->length());
5162
5163 Load(args->at(0));
5164 Load(args->at(1));
5165
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005166 StringCompareStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005167 frame_->SpillAll();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005168 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005169 frame_->EmitPush(r0);
5170}
5171
5172
5173void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
5174 ASSERT_EQ(4, args->length());
5175
5176 Load(args->at(0));
5177 Load(args->at(1));
5178 Load(args->at(2));
5179 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005180 RegExpExecStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005181 frame_->SpillAll();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005182 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005183 frame_->EmitPush(r0);
5184}
5185
5186
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005187void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
5188 // No stub. This code only occurs a few times in regexp.js.
5189 const int kMaxInlineLength = 100;
5190 ASSERT_EQ(3, args->length());
5191 Load(args->at(0)); // Size of array, smi.
5192 Load(args->at(1)); // "index" property value.
5193 Load(args->at(2)); // "input" property value.
5194 {
5195 VirtualFrame::SpilledScope spilled_scope(frame_);
5196 Label slowcase;
5197 Label done;
5198 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
5199 STATIC_ASSERT(kSmiTag == 0);
5200 STATIC_ASSERT(kSmiTagSize == 1);
5201 __ tst(r1, Operand(kSmiTagMask));
5202 __ b(ne, &slowcase);
5203 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
5204 __ b(hi, &slowcase);
5205 // Smi-tagging is equivalent to multiplying by 2.
5206 // Allocate RegExpResult followed by FixedArray with size in ebx.
5207 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
5208 // Elements: [Map][Length][..elements..]
5209 // Size of JSArray with two in-object properties and the header of a
5210 // FixedArray.
5211 int objects_size =
5212 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
5213 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
5214 __ add(r2, r5, Operand(objects_size));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005215 __ AllocateInNewSpace(
5216 r2, // In: Size, in words.
5217 r0, // Out: Start of allocation (tagged).
5218 r3, // Scratch register.
5219 r4, // Scratch register.
5220 &slowcase,
5221 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005222 // r0: Start of allocated area, object-tagged.
5223 // r1: Number of elements in array, as smi.
5224 // r5: Number of elements, untagged.
5225
5226 // Set JSArray map to global.regexp_result_map().
5227 // Set empty properties FixedArray.
5228 // Set elements to point to FixedArray allocated right after the JSArray.
5229 // Interleave operations for better latency.
5230 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
5231 __ add(r3, r0, Operand(JSRegExpResult::kSize));
5232 __ mov(r4, Operand(Factory::empty_fixed_array()));
5233 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5234 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
5235 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
5236 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5237 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5238
5239 // Set input, index and length fields from arguments.
5240 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
5241 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
5242 __ add(sp, sp, Operand(kPointerSize));
5243 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
5244 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
5245
5246 // Fill out the elements FixedArray.
5247 // r0: JSArray, tagged.
5248 // r3: FixedArray, tagged.
5249 // r5: Number of elements in array, untagged.
5250
5251 // Set map.
5252 __ mov(r2, Operand(Factory::fixed_array_map()));
5253 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
5254 // Set FixedArray length.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005255 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
5256 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005257 // Fill contents of fixed-array with the-hole.
5258 __ mov(r2, Operand(Factory::the_hole_value()));
5259 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5260 // Fill fixed array elements with hole.
5261 // r0: JSArray, tagged.
5262 // r2: the hole.
5263 // r3: Start of elements in FixedArray.
5264 // r5: Number of elements to fill.
5265 Label loop;
5266 __ tst(r5, Operand(r5));
5267 __ bind(&loop);
5268 __ b(le, &done); // Jump if r1 is negative or zero.
5269 __ sub(r5, r5, Operand(1), SetCC);
5270 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
5271 __ jmp(&loop);
5272
5273 __ bind(&slowcase);
5274 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
5275
5276 __ bind(&done);
5277 }
5278 frame_->Forget(3);
5279 frame_->EmitPush(r0);
5280}
5281
5282
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00005283void CodeGenerator::GenerateRegExpCloneResult(ZoneList<Expression*>* args) {
5284 ASSERT_EQ(1, args->length());
5285
5286 Load(args->at(0));
5287 frame_->PopToR0();
5288 {
5289 VirtualFrame::SpilledScope spilled_scope(frame_);
5290
5291 Label done;
5292 Label call_runtime;
5293 __ BranchOnSmi(r0, &done);
5294
5295 // Load JSRegExp map into r1. Check that argument object has this map.
5296 // Arguments to this function should be results of calling RegExp exec,
5297 // which is either an unmodified JSRegExpResult or null. Anything not having
5298 // the unmodified JSRegExpResult map is returned unmodified.
5299 // This also ensures that elements are fast.
5300
5301 __ ldr(r1, ContextOperand(cp, Context::GLOBAL_INDEX));
5302 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalContextOffset));
5303 __ ldr(r1, ContextOperand(r1, Context::REGEXP_RESULT_MAP_INDEX));
5304 __ ldr(ip, FieldMemOperand(r0, HeapObject::kMapOffset));
5305 __ cmp(r1, Operand(ip));
5306 __ b(ne, &done);
5307
ricow@chromium.org65fae842010-08-25 15:26:24 +00005308 if (FLAG_debug_code) {
5309 __ LoadRoot(r2, Heap::kEmptyFixedArrayRootIndex);
5310 __ ldr(ip, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5311 __ cmp(ip, r2);
5312 __ Check(eq, "JSRegExpResult: default map but non-empty properties.");
5313 }
5314
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00005315 // All set, copy the contents to a new object.
5316 __ AllocateInNewSpace(JSRegExpResult::kSize,
5317 r2,
5318 r3,
5319 r4,
5320 &call_runtime,
5321 NO_ALLOCATION_FLAGS);
5322 // Store RegExpResult map as map of allocated object.
5323 ASSERT(JSRegExpResult::kSize == 6 * kPointerSize);
5324 // Copy all fields (map is already in r1) from (untagged) r0 to r2.
5325 // Change map of elements array (ends up in r4) to be a FixedCOWArray.
5326 __ bic(r0, r0, Operand(kHeapObjectTagMask));
5327 __ ldm(ib, r0, r3.bit() | r4.bit() | r5.bit() | r6.bit() | r7.bit());
5328 __ stm(ia, r2,
5329 r1.bit() | r3.bit() | r4.bit() | r5.bit() | r6.bit() | r7.bit());
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00005330 ASSERT(JSRegExp::kElementsOffset == 2 * kPointerSize);
5331 // Check whether elements array is empty fixed array, and otherwise make
5332 // it copy-on-write (it never should be empty unless someone is messing
5333 // with the arguments to the runtime function).
5334 __ LoadRoot(ip, Heap::kEmptyFixedArrayRootIndex);
5335 __ add(r0, r2, Operand(kHeapObjectTag)); // Tag result and move it to r0.
5336 __ cmp(r4, ip);
5337 __ b(eq, &done);
5338 __ LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
5339 __ str(ip, FieldMemOperand(r4, HeapObject::kMapOffset));
5340 __ b(&done);
5341 __ bind(&call_runtime);
5342 __ push(r0);
5343 __ CallRuntime(Runtime::kRegExpCloneResult, 1);
5344 __ bind(&done);
5345 }
5346 frame_->EmitPush(r0);
5347}
5348
5349
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005350class DeferredSearchCache: public DeferredCode {
5351 public:
5352 DeferredSearchCache(Register dst, Register cache, Register key)
5353 : dst_(dst), cache_(cache), key_(key) {
5354 set_comment("[ DeferredSearchCache");
5355 }
5356
5357 virtual void Generate();
5358
5359 private:
5360 Register dst_, cache_, key_;
5361};
5362
5363
5364void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005365 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005366 __ CallRuntime(Runtime::kGetFromCache, 2);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005367 __ Move(dst_, r0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005368}
5369
5370
5371void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
5372 ASSERT_EQ(2, args->length());
5373
5374 ASSERT_NE(NULL, args->at(0)->AsLiteral());
5375 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
5376
5377 Handle<FixedArray> jsfunction_result_caches(
5378 Top::global_context()->jsfunction_result_caches());
5379 if (jsfunction_result_caches->length() <= cache_id) {
5380 __ Abort("Attempt to use undefined cache.");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005381 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005382 return;
5383 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005384
5385 Load(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005386
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005387 frame_->PopToR1();
5388 frame_->SpillAll();
5389 Register key = r1; // Just poped to r1
5390 Register result = r0; // Free, as frame has just been spilled.
5391 Register scratch1 = VirtualFrame::scratch0();
5392 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005393
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005394 __ ldr(scratch1, ContextOperand(cp, Context::GLOBAL_INDEX));
5395 __ ldr(scratch1,
5396 FieldMemOperand(scratch1, GlobalObject::kGlobalContextOffset));
5397 __ ldr(scratch1,
5398 ContextOperand(scratch1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
5399 __ ldr(scratch1,
5400 FieldMemOperand(scratch1, FixedArray::OffsetOfElementAt(cache_id)));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005401
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005402 DeferredSearchCache* deferred =
5403 new DeferredSearchCache(result, scratch1, key);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005404
5405 const int kFingerOffset =
5406 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00005407 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005408 __ ldr(result, FieldMemOperand(scratch1, kFingerOffset));
5409 // result now holds finger offset as a smi.
5410 __ add(scratch2, scratch1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5411 // scratch2 now points to the start of fixed array elements.
5412 __ ldr(result,
5413 MemOperand(
5414 scratch2, result, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
5415 // Note side effect of PreIndex: scratch2 now points to the key of the pair.
5416 __ cmp(key, result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005417 deferred->Branch(ne);
5418
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005419 __ ldr(result, MemOperand(scratch2, kPointerSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005420
5421 deferred->BindExit();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005422 frame_->EmitPush(result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005423}
5424
5425
ager@chromium.org5c838252010-02-19 08:53:10 +00005426void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
5427 ASSERT_EQ(args->length(), 1);
5428
5429 // Load the argument on the stack and jump to the runtime.
5430 Load(args->at(0));
5431
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005432 NumberToStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005433 frame_->SpillAll();
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005434 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005435 frame_->EmitPush(r0);
5436}
5437
5438
ager@chromium.orgac091b72010-05-05 07:34:42 +00005439class DeferredSwapElements: public DeferredCode {
5440 public:
5441 DeferredSwapElements(Register object, Register index1, Register index2)
5442 : object_(object), index1_(index1), index2_(index2) {
5443 set_comment("[ DeferredSwapElements");
5444 }
5445
5446 virtual void Generate();
5447
5448 private:
5449 Register object_, index1_, index2_;
5450};
5451
5452
5453void DeferredSwapElements::Generate() {
5454 __ push(object_);
5455 __ push(index1_);
5456 __ push(index2_);
5457 __ CallRuntime(Runtime::kSwapElements, 3);
5458}
5459
5460
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005461void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
5462 Comment cmnt(masm_, "[ GenerateSwapElements");
5463
5464 ASSERT_EQ(3, args->length());
5465
5466 Load(args->at(0));
5467 Load(args->at(1));
5468 Load(args->at(2));
5469
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005470 VirtualFrame::SpilledScope spilled_scope(frame_);
5471
ager@chromium.orgac091b72010-05-05 07:34:42 +00005472 Register index2 = r2;
5473 Register index1 = r1;
5474 Register object = r0;
5475 Register tmp1 = r3;
5476 Register tmp2 = r4;
5477
5478 frame_->EmitPop(index2);
5479 frame_->EmitPop(index1);
5480 frame_->EmitPop(object);
5481
5482 DeferredSwapElements* deferred =
5483 new DeferredSwapElements(object, index1, index2);
5484
5485 // Fetch the map and check if array is in fast case.
5486 // Check that object doesn't require security checks and
5487 // has no indexed interceptor.
5488 __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE);
5489 deferred->Branch(lt);
5490 __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset));
5491 __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
5492 deferred->Branch(nz);
5493
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00005494 // Check the object's elements are in fast case and writable.
ager@chromium.orgac091b72010-05-05 07:34:42 +00005495 __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset));
5496 __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset));
5497 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5498 __ cmp(tmp2, ip);
5499 deferred->Branch(ne);
5500
5501 // Smi-tagging is equivalent to multiplying by 2.
5502 STATIC_ASSERT(kSmiTag == 0);
5503 STATIC_ASSERT(kSmiTagSize == 1);
5504
5505 // Check that both indices are smis.
5506 __ mov(tmp2, index1);
5507 __ orr(tmp2, tmp2, index2);
5508 __ tst(tmp2, Operand(kSmiTagMask));
5509 deferred->Branch(nz);
5510
5511 // Bring the offsets into the fixed array in tmp1 into index1 and
5512 // index2.
5513 __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5514 __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize));
5515 __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize));
5516
5517 // Swap elements.
5518 Register tmp3 = object;
5519 object = no_reg;
5520 __ ldr(tmp3, MemOperand(tmp1, index1));
5521 __ ldr(tmp2, MemOperand(tmp1, index2));
5522 __ str(tmp3, MemOperand(tmp1, index2));
5523 __ str(tmp2, MemOperand(tmp1, index1));
5524
5525 Label done;
5526 __ InNewSpace(tmp1, tmp2, eq, &done);
5527 // Possible optimization: do a check that both values are Smis
5528 // (or them and test against Smi mask.)
5529
5530 __ mov(tmp2, tmp1);
5531 RecordWriteStub recordWrite1(tmp1, index1, tmp3);
5532 __ CallStub(&recordWrite1);
5533
5534 RecordWriteStub recordWrite2(tmp2, index2, tmp3);
5535 __ CallStub(&recordWrite2);
5536
5537 __ bind(&done);
5538
5539 deferred->BindExit();
5540 __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex);
5541 frame_->EmitPush(tmp1);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005542}
5543
5544
ager@chromium.org357bf652010-04-12 11:30:10 +00005545void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
5546 Comment cmnt(masm_, "[ GenerateCallFunction");
5547
5548 ASSERT(args->length() >= 2);
5549
5550 int n_args = args->length() - 2; // for receiver and function.
5551 Load(args->at(0)); // receiver
5552 for (int i = 0; i < n_args; i++) {
5553 Load(args->at(i + 1));
5554 }
5555 Load(args->at(n_args + 1)); // function
5556 frame_->CallJSFunction(n_args);
5557 frame_->EmitPush(r0);
5558}
5559
5560
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005561void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
5562 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005563 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005564 if (CpuFeatures::IsSupported(VFP3)) {
5565 TranscendentalCacheStub stub(TranscendentalCache::SIN);
5566 frame_->SpillAllButCopyTOSToR0();
5567 frame_->CallStub(&stub, 1);
5568 } else {
5569 frame_->CallRuntime(Runtime::kMath_sin, 1);
5570 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005571 frame_->EmitPush(r0);
5572}
5573
5574
5575void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
5576 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005577 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005578 if (CpuFeatures::IsSupported(VFP3)) {
5579 TranscendentalCacheStub stub(TranscendentalCache::COS);
5580 frame_->SpillAllButCopyTOSToR0();
5581 frame_->CallStub(&stub, 1);
5582 } else {
5583 frame_->CallRuntime(Runtime::kMath_cos, 1);
5584 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005585 frame_->EmitPush(r0);
5586}
5587
5588
ager@chromium.org7c537e22008-10-16 08:43:32 +00005589void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005590 ASSERT(args->length() == 2);
5591
5592 // Load the two objects into registers and perform the comparison.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005593 Load(args->at(0));
5594 Load(args->at(1));
5595 Register lhs = frame_->PopToRegister();
5596 Register rhs = frame_->PopToRegister(lhs);
5597 __ cmp(lhs, rhs);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005598 cc_reg_ = eq;
5599}
5600
5601
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005602void CodeGenerator::GenerateIsRegExpEquivalent(ZoneList<Expression*>* args) {
5603 ASSERT(args->length() == 2);
5604
5605 // Load the two objects into registers and perform the comparison.
5606 Load(args->at(0));
5607 Load(args->at(1));
5608 Register right = frame_->PopToRegister();
5609 Register left = frame_->PopToRegister(right);
5610 Register tmp = frame_->scratch0();
5611 Register tmp2 = frame_->scratch1();
5612
5613 // Jumps to done must have the eq flag set if the test is successful
5614 // and clear if the test has failed.
5615 Label done;
5616
5617 // Fail if either is a non-HeapObject.
5618 __ cmp(left, Operand(right));
5619 __ b(eq, &done);
5620 __ and_(tmp, left, Operand(right));
5621 __ eor(tmp, tmp, Operand(kSmiTagMask));
5622 __ tst(tmp, Operand(kSmiTagMask));
5623 __ b(ne, &done);
5624 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
5625 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
5626 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
5627 __ b(ne, &done);
5628 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
5629 __ cmp(tmp, Operand(tmp2));
5630 __ b(ne, &done);
5631 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
5632 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
5633 __ cmp(tmp, tmp2);
5634 __ bind(&done);
5635 cc_reg_ = eq;
5636}
5637
5638
5639
ager@chromium.org7c537e22008-10-16 08:43:32 +00005640void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005641#ifdef DEBUG
5642 int original_height = frame_->height();
5643#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005644 if (CheckForInlineRuntimeCall(node)) {
5645 ASSERT((has_cc() && frame_->height() == original_height) ||
5646 (!has_cc() && frame_->height() == original_height + 1));
5647 return;
5648 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005649
5650 ZoneList<Expression*>* args = node->arguments();
5651 Comment cmnt(masm_, "[ CallRuntime");
5652 Runtime::Function* function = node->function();
5653
ager@chromium.org41826e72009-03-30 13:30:57 +00005654 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00005655 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00005656 // Push the builtins object found in the current global object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005657 Register scratch = VirtualFrame::scratch0();
5658 __ ldr(scratch, GlobalObject());
5659 Register builtins = frame_->GetTOSRegister();
5660 __ ldr(builtins, FieldMemOperand(scratch, GlobalObject::kBuiltinsOffset));
5661 frame_->EmitPush(builtins);
ager@chromium.org41826e72009-03-30 13:30:57 +00005662 }
mads.s.ager31e71382008-08-13 09:32:07 +00005663
ager@chromium.org41826e72009-03-30 13:30:57 +00005664 // Push the arguments ("left-to-right").
5665 int arg_count = args->length();
5666 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005667 Load(args->at(i));
ager@chromium.org41826e72009-03-30 13:30:57 +00005668 }
mads.s.ager31e71382008-08-13 09:32:07 +00005669
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005670 VirtualFrame::SpilledScope spilled_scope(frame_);
5671
ager@chromium.org41826e72009-03-30 13:30:57 +00005672 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005673 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00005674 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005675 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
5676 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005677 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005678 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005679 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00005680 } else {
5681 // Call the C runtime function.
5682 frame_->CallRuntime(function, arg_count);
5683 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005684 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00005685 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005686}
5687
5688
ager@chromium.org7c537e22008-10-16 08:43:32 +00005689void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005690#ifdef DEBUG
5691 int original_height = frame_->height();
5692#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005693 Comment cmnt(masm_, "[ UnaryOperation");
5694
5695 Token::Value op = node->op();
5696
5697 if (op == Token::NOT) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005698 LoadCondition(node->expression(), false_target(), true_target(), true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005699 // LoadCondition may (and usually does) leave a test and branch to
5700 // be emitted by the caller. In that case, negate the condition.
5701 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005702
5703 } else if (op == Token::DELETE) {
5704 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00005705 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005706 if (property != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005707 Load(property->obj());
5708 Load(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005709 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005710 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005711
mads.s.ager31e71382008-08-13 09:32:07 +00005712 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005713 Slot* slot = variable->slot();
5714 if (variable->is_global()) {
5715 LoadGlobal();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005716 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005717 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005718 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005719
5720 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
5721 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005722 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005723 frame_->EmitPush(Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005724 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005725 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005726 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005727 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005728 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005729 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005730
mads.s.ager31e71382008-08-13 09:32:07 +00005731 } else {
5732 // Default: Result of deleting non-global, not dynamically
5733 // introduced variables is false.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005734 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00005735 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005736
5737 } else {
5738 // Default: Result of deleting expressions is true.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005739 Load(node->expression()); // may have side-effects
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005740 frame_->Drop();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005741 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005742 }
5743
5744 } else if (op == Token::TYPEOF) {
5745 // Special case for loading the typeof expression; see comment on
5746 // LoadTypeofExpression().
5747 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005748 frame_->CallRuntime(Runtime::kTypeof, 1);
5749 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005750
5751 } else {
ricow@chromium.org65fae842010-08-25 15:26:24 +00005752 bool can_overwrite = node->expression()->ResultOverwriteAllowed();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005753 UnaryOverwriteMode overwrite =
5754 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
5755
5756 bool no_negative_zero = node->expression()->no_negative_zero();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005757 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005758 switch (op) {
5759 case Token::NOT:
5760 case Token::DELETE:
5761 case Token::TYPEOF:
5762 UNREACHABLE(); // handled above
5763 break;
5764
5765 case Token::SUB: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005766 frame_->PopToR0();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005767 GenericUnaryOpStub stub(
5768 Token::SUB,
5769 overwrite,
5770 no_negative_zero ? kIgnoreNegativeZero : kStrictNegativeZero);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005771 frame_->CallStub(&stub, 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005772 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005773 break;
5774 }
5775
5776 case Token::BIT_NOT: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005777 Register tos = frame_->PopToRegister();
5778 JumpTarget not_smi_label;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005779 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005780 // Smi check.
5781 __ tst(tos, Operand(kSmiTagMask));
5782 not_smi_label.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005783
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005784 __ mvn(tos, Operand(tos));
5785 __ bic(tos, tos, Operand(kSmiTagMask)); // Bit-clear inverted smi-tag.
5786 frame_->EmitPush(tos);
5787 // The fast case is the first to jump to the continue label, so it gets
5788 // to decide the virtual frame layout.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005789 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005790
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005791 not_smi_label.Bind();
5792 frame_->SpillAll();
5793 __ Move(r0, tos);
5794 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
5795 frame_->CallStub(&stub, 0);
5796 frame_->EmitPush(r0);
5797
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005798 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005799 break;
5800 }
5801
5802 case Token::VOID:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005803 frame_->Drop();
5804 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005805 break;
5806
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005807 case Token::ADD: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005808 Register tos = frame_->Peek();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005809 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005810 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005811 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005812 continue_label.Branch(eq);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005813
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005814 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005815 frame_->EmitPush(r0);
5816
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005817 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005818 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005819 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005820 default:
5821 UNREACHABLE();
5822 }
5823 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005824 ASSERT(!has_valid_frame() ||
5825 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005826 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005827}
5828
5829
ager@chromium.org7c537e22008-10-16 08:43:32 +00005830void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005831#ifdef DEBUG
5832 int original_height = frame_->height();
5833#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005834 Comment cmnt(masm_, "[ CountOperation");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005835 VirtualFrame::RegisterAllocationScope scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005836
5837 bool is_postfix = node->is_postfix();
5838 bool is_increment = node->op() == Token::INC;
5839
5840 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
5841 bool is_const = (var != NULL && var->mode() == Variable::CONST);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005842 bool is_slot = (var != NULL && var->mode() == Variable::VAR);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005843
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005844 if (!is_const && is_slot && type_info(var->slot()).IsSmi()) {
5845 // The type info declares that this variable is always a Smi. That
5846 // means it is a Smi both before and after the increment/decrement.
5847 // Lets make use of that to make a very minimal count.
5848 Reference target(this, node->expression(), !is_const);
5849 ASSERT(!target.is_illegal());
5850 target.GetValue(); // Pushes the value.
5851 Register value = frame_->PopToRegister();
5852 if (is_postfix) frame_->EmitPush(value);
5853 if (is_increment) {
5854 __ add(value, value, Operand(Smi::FromInt(1)));
5855 } else {
5856 __ sub(value, value, Operand(Smi::FromInt(1)));
5857 }
5858 frame_->EmitPush(value);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005859 target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005860 if (is_postfix) frame_->Pop();
5861 ASSERT_EQ(original_height + 1, frame_->height());
5862 return;
5863 }
5864
5865 // If it's a postfix expression and its result is not ignored and the
5866 // reference is non-trivial, then push a placeholder on the stack now
5867 // to hold the result of the expression.
5868 bool placeholder_pushed = false;
5869 if (!is_slot && is_postfix) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005870 frame_->EmitPush(Operand(Smi::FromInt(0)));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005871 placeholder_pushed = true;
mads.s.ager31e71382008-08-13 09:32:07 +00005872 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005873
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005874 // A constant reference is not saved to, so a constant reference is not a
5875 // compound assignment reference.
5876 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005877 if (target.is_illegal()) {
5878 // Spoof the virtual frame to have the expected height (one higher
5879 // than on entry).
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005880 if (!placeholder_pushed) frame_->EmitPush(Operand(Smi::FromInt(0)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00005881 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005882 return;
5883 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005884
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005885 // This pushes 0, 1 or 2 words on the object to be used later when updating
5886 // the target. It also pushes the current value of the target.
ager@chromium.org357bf652010-04-12 11:30:10 +00005887 target.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005888
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005889 JumpTarget slow;
5890 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005891
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005892 Register value = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005893
5894 // Postfix: Store the old value as the result.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005895 if (placeholder_pushed) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005896 frame_->SetElementAt(value, target.size());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005897 } else if (is_postfix) {
5898 frame_->EmitPush(value);
5899 __ mov(VirtualFrame::scratch0(), value);
5900 value = VirtualFrame::scratch0();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005901 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005902
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005903 // Check for smi operand.
5904 __ tst(value, Operand(kSmiTagMask));
5905 slow.Branch(ne);
5906
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005907 // Perform optimistic increment/decrement.
5908 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005909 __ add(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005910 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005911 __ sub(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005912 }
5913
5914 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005915 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005916
5917 // Revert optimistic increment/decrement.
5918 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005919 __ sub(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005920 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005921 __ add(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005922 }
5923
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005924 // Slow case: Convert to number. At this point the
5925 // value to be incremented is in the value register..
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005926 slow.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005927
5928 // Convert the operand to a number.
5929 frame_->EmitPush(value);
5930
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00005931 {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005932 VirtualFrame::SpilledScope spilled(frame_);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005933 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005934
5935 if (is_postfix) {
5936 // Postfix: store to result (on the stack).
5937 __ str(r0, frame_->ElementAt(target.size()));
5938 }
5939
5940 // Compute the new value.
5941 frame_->EmitPush(r0);
5942 frame_->EmitPush(Operand(Smi::FromInt(1)));
5943 if (is_increment) {
5944 frame_->CallRuntime(Runtime::kNumberAdd, 2);
5945 } else {
5946 frame_->CallRuntime(Runtime::kNumberSub, 2);
5947 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005948 }
5949
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005950 __ Move(value, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005951 // Store the new value in the target if not const.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005952 // At this point the answer is in the value register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005953 exit.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005954 frame_->EmitPush(value);
5955 // Set the target with the result, leaving the result on
5956 // top of the stack. Removes the target from the stack if
5957 // it has a non-zero size.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005958 if (!is_const) target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005959 }
5960
5961 // Postfix: Discard the new value and use the old.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005962 if (is_postfix) frame_->Pop();
ager@chromium.orgac091b72010-05-05 07:34:42 +00005963 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005964}
5965
5966
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005967void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005968 // According to ECMA-262 section 11.11, page 58, the binary logical
5969 // operators must yield the result of one of the two expressions
5970 // before any ToBoolean() conversions. This means that the value
5971 // produced by a && or || operator is not necessarily a boolean.
5972
5973 // NOTE: If the left hand side produces a materialized value (not in
5974 // the CC register), we force the right hand side to do the
5975 // same. This is necessary because we may have to branch to the exit
5976 // after evaluating the left hand side (due to the shortcut
5977 // semantics), but the compiler must (statically) know if the result
5978 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005979 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005980 JumpTarget is_true;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005981 LoadCondition(node->left(), &is_true, false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005982 if (has_valid_frame() && !has_cc()) {
5983 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005984 JumpTarget pop_and_continue;
5985 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005986
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005987 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005988 // Avoid popping the result if it converts to 'false' using the
5989 // standard ToBoolean() conversion as described in ECMA-262,
5990 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005991 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005992 Branch(false, &exit);
5993
5994 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005995 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005996 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005997
5998 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005999 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006000 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006001
6002 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006003 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006004 } else if (has_cc() || is_true.is_linked()) {
6005 // The left-hand side is either (a) partially compiled to
6006 // control flow with a final branch left to emit or (b) fully
6007 // compiled to control flow and possibly true.
6008 if (has_cc()) {
6009 Branch(false, false_target());
6010 }
6011 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006012 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006013 } else {
6014 // Nothing to do.
6015 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006016 }
6017
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006018 } else {
6019 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006020 JumpTarget is_false;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006021 LoadCondition(node->left(), true_target(), &is_false, false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006022 if (has_valid_frame() && !has_cc()) {
6023 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006024 JumpTarget pop_and_continue;
6025 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006026
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006027 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006028 // Avoid popping the result if it converts to 'true' using the
6029 // standard ToBoolean() conversion as described in ECMA-262,
6030 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00006031 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006032 Branch(true, &exit);
6033
6034 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006035 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006036 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006037
6038 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006039 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006040 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006041
6042 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006043 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006044 } else if (has_cc() || is_false.is_linked()) {
6045 // The left-hand side is either (a) partially compiled to
6046 // control flow with a final branch left to emit or (b) fully
6047 // compiled to control flow and possibly false.
6048 if (has_cc()) {
6049 Branch(true, true_target());
6050 }
6051 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006052 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006053 } else {
6054 // Nothing to do.
6055 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006056 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006057 }
6058}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006059
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006060
6061void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
6062#ifdef DEBUG
6063 int original_height = frame_->height();
6064#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006065 Comment cmnt(masm_, "[ BinaryOperation");
6066
6067 if (node->op() == Token::AND || node->op() == Token::OR) {
6068 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006069 } else {
6070 // Optimize for the case where (at least) one of the expressions
6071 // is a literal small integer.
6072 Literal* lliteral = node->left()->AsLiteral();
6073 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006074 // NOTE: The code below assumes that the slow cases (calls to runtime)
6075 // never return a constant/immutable object.
ricow@chromium.org65fae842010-08-25 15:26:24 +00006076 bool overwrite_left = node->left()->ResultOverwriteAllowed();
6077 bool overwrite_right = node->right()->ResultOverwriteAllowed();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006078
6079 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006080 VirtualFrame::RegisterAllocationScope scope(this);
6081 Load(node->left());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006082 if (frame_->KnownSmiAt(0)) overwrite_left = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006083 SmiOperation(node->op(),
6084 rliteral->handle(),
6085 false,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006086 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006087 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006088 VirtualFrame::RegisterAllocationScope scope(this);
6089 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006090 if (frame_->KnownSmiAt(0)) overwrite_right = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006091 SmiOperation(node->op(),
6092 lliteral->handle(),
6093 true,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006094 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006095 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006096 GenerateInlineSmi inline_smi =
6097 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
6098 if (lliteral != NULL) {
6099 ASSERT(!lliteral->handle()->IsSmi());
6100 inline_smi = DONT_GENERATE_INLINE_SMI;
6101 }
6102 if (rliteral != NULL) {
6103 ASSERT(!rliteral->handle()->IsSmi());
6104 inline_smi = DONT_GENERATE_INLINE_SMI;
6105 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006106 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006107 OverwriteMode overwrite_mode = NO_OVERWRITE;
6108 if (overwrite_left) {
6109 overwrite_mode = OVERWRITE_LEFT;
6110 } else if (overwrite_right) {
6111 overwrite_mode = OVERWRITE_RIGHT;
6112 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006113 Load(node->left());
6114 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006115 GenericBinaryOperation(node->op(), overwrite_mode, inline_smi);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006116 }
6117 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006118 ASSERT(!has_valid_frame() ||
6119 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006120 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006121}
6122
6123
ager@chromium.org7c537e22008-10-16 08:43:32 +00006124void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006125#ifdef DEBUG
6126 int original_height = frame_->height();
6127#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006128 frame_->EmitPush(MemOperand(frame_->Function()));
ager@chromium.orgac091b72010-05-05 07:34:42 +00006129 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006130}
6131
6132
ager@chromium.org7c537e22008-10-16 08:43:32 +00006133void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006134#ifdef DEBUG
6135 int original_height = frame_->height();
6136#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006137 Comment cmnt(masm_, "[ CompareOperation");
6138
ager@chromium.org357bf652010-04-12 11:30:10 +00006139 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
6140
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006141 // Get the expressions from the node.
6142 Expression* left = node->left();
6143 Expression* right = node->right();
6144 Token::Value op = node->op();
6145
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006146 // To make typeof testing for natives implemented in JavaScript really
6147 // efficient, we generate special code for expressions of the form:
6148 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006149 UnaryOperation* operation = left->AsUnaryOperation();
6150 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
6151 (operation != NULL && operation->op() == Token::TYPEOF) &&
6152 (right->AsLiteral() != NULL &&
6153 right->AsLiteral()->handle()->IsString())) {
6154 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
6155
ager@chromium.org357bf652010-04-12 11:30:10 +00006156 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006157 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00006158 Register tos = frame_->PopToRegister();
6159
ager@chromium.org357bf652010-04-12 11:30:10 +00006160 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006161
6162 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006163 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006164 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00006165 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006166 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006167 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006168 cc_reg_ = eq;
6169
6170 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006171 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006172 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006173
ager@chromium.org357bf652010-04-12 11:30:10 +00006174 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006175
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006176 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006177 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
6178 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
6179 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006180 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006181
ager@chromium.org357bf652010-04-12 11:30:10 +00006182 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
6183 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006184 cc_reg_ = lt;
6185
6186 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006187 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006188 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006189 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006190 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006191 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006192 cc_reg_ = eq;
6193
6194 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006195 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006196 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006197 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006198
ager@chromium.org357bf652010-04-12 11:30:10 +00006199 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006200 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006201
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006202 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006203 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
6204 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
6205 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
6206 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006207
6208 cc_reg_ = eq;
6209
6210 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006211 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006212 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00006213 Register map_reg = scratch;
6214 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006215 true_target()->Branch(eq);
6216 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00006217 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006218 cc_reg_ = eq;
6219
6220 } else if (check->Equals(Heap::object_symbol())) {
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.orgab99eea2009-08-25 07:05:41 +00006224 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006225 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006226 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006227
ager@chromium.org357bf652010-04-12 11:30:10 +00006228 Register map_reg = scratch;
6229 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006230 false_target()->Branch(eq);
6231
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006232 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006233 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
6234 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6235 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006236 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006237
ager@chromium.org357bf652010-04-12 11:30:10 +00006238 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
6239 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006240 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00006241 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006242 cc_reg_ = le;
6243
6244 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006245 // Uncommon case: typeof testing against a string literal that is
6246 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006247 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006248 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006249 ASSERT(!has_valid_frame() ||
6250 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006251 return;
6252 }
6253
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006254 switch (op) {
6255 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006256 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006257 break;
6258
6259 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006260 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006261 break;
6262
6263 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006264 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006265 break;
6266
6267 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006268 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006269 break;
6270
6271 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006272 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006273 break;
6274
6275 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006276 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006277 break;
6278
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006279 case Token::IN: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006280 Load(left);
6281 Load(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006282 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006283 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006284 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006285 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006286
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006287 case Token::INSTANCEOF: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006288 Load(left);
6289 Load(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006290 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006291 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006292 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006293 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006294 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006295 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006296 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006297
6298 default:
6299 UNREACHABLE();
6300 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006301 ASSERT((has_cc() && frame_->height() == original_height) ||
6302 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006303}
6304
6305
ricow@chromium.org65fae842010-08-25 15:26:24 +00006306void CodeGenerator::VisitCompareToNull(CompareToNull* node) {
6307#ifdef DEBUG
6308 int original_height = frame_->height();
6309#endif
6310 Comment cmnt(masm_, "[ CompareToNull");
6311
6312 Load(node->expression());
6313 Register tos = frame_->PopToRegister();
6314 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6315 __ cmp(tos, ip);
6316
6317 // The 'null' value is only equal to 'undefined' if using non-strict
6318 // comparisons.
6319 if (!node->is_strict()) {
6320 true_target()->Branch(eq);
6321 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
6322 __ cmp(tos, Operand(ip));
6323 true_target()->Branch(eq);
6324
6325 __ tst(tos, Operand(kSmiTagMask));
6326 false_target()->Branch(eq);
6327
6328 // It can be an undetectable object.
6329 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
6330 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
6331 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6332 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
6333 }
6334
6335 cc_reg_ = eq;
6336 ASSERT(has_cc() && frame_->height() == original_height);
6337}
6338
6339
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006340class DeferredReferenceGetNamedValue: public DeferredCode {
6341 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006342 explicit DeferredReferenceGetNamedValue(Register receiver,
6343 Handle<String> name)
6344 : receiver_(receiver), name_(name) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006345 set_comment("[ DeferredReferenceGetNamedValue");
6346 }
6347
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006348 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006349
6350 private:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006351 Register receiver_;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006352 Handle<String> name_;
6353};
6354
6355
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006356// Convention for this is that on entry the receiver is in a register that
6357// is not used by the stack. On exit the answer is found in that same
6358// register and the stack has the same height.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006359void DeferredReferenceGetNamedValue::Generate() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006360#ifdef DEBUG
6361 int expected_height = frame_state()->frame()->height();
6362#endif
6363 VirtualFrame copied_frame(*frame_state()->frame());
6364 copied_frame.SpillAll();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006365
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006366 Register scratch1 = VirtualFrame::scratch0();
6367 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006368 ASSERT(!receiver_.is(scratch1) && !receiver_.is(scratch2));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006369 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
6370 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
6371
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006372 // Ensure receiver in r0 and name in r2 to match load ic calling convention.
6373 __ Move(r0, receiver_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006374 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006375
6376 // The rest of the instructions in the deferred code must be together.
6377 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6378 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
6379 __ Call(ic, RelocInfo::CODE_TARGET);
6380 // The call must be followed by a nop(1) instruction to indicate that the
6381 // in-object has been inlined.
6382 __ nop(PROPERTY_ACCESS_INLINED);
6383
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006384 // At this point the answer is in r0. We move it to the expected register
6385 // if necessary.
6386 __ Move(receiver_, r0);
6387
6388 // Now go back to the frame that we entered with. This will not overwrite
6389 // the receiver register since that register was not in use when we came
6390 // in. The instructions emitted by this merge are skipped over by the
6391 // inline load patching mechanism when looking for the branch instruction
6392 // that tells it where the code to patch is.
6393 copied_frame.MergeTo(frame_state()->frame());
6394
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006395 // Block the constant pool for one more instruction after leaving this
6396 // constant pool block scope to include the branch instruction ending the
6397 // deferred code.
6398 __ BlockConstPoolFor(1);
6399 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006400 ASSERT_EQ(expected_height, frame_state()->frame()->height());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006401}
6402
6403
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006404class DeferredReferenceGetKeyedValue: public DeferredCode {
6405 public:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006406 DeferredReferenceGetKeyedValue(Register key, Register receiver)
6407 : key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006408 set_comment("[ DeferredReferenceGetKeyedValue");
6409 }
6410
6411 virtual void Generate();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006412
6413 private:
6414 Register key_;
6415 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006416};
6417
6418
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006419// Takes key and register in r0 and r1 or vice versa. Returns result
6420// in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006421void DeferredReferenceGetKeyedValue::Generate() {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006422 ASSERT((key_.is(r0) && receiver_.is(r1)) ||
6423 (key_.is(r1) && receiver_.is(r0)));
6424
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006425 VirtualFrame copied_frame(*frame_state()->frame());
6426 copied_frame.SpillAll();
6427
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006428 Register scratch1 = VirtualFrame::scratch0();
6429 Register scratch2 = VirtualFrame::scratch1();
6430 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
6431 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
6432
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006433 // Ensure key in r0 and receiver in r1 to match keyed load ic calling
6434 // convention.
6435 if (key_.is(r1)) {
6436 __ Swap(r0, r1, ip);
6437 }
6438
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006439 // The rest of the instructions in the deferred code must be together.
6440 { Assembler::BlockConstPoolScope block_const_pool(masm_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006441 // Call keyed load IC. It has the arguments key and receiver in r0 and r1.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006442 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
6443 __ Call(ic, RelocInfo::CODE_TARGET);
6444 // The call must be followed by a nop instruction to indicate that the
6445 // keyed load has been inlined.
6446 __ nop(PROPERTY_ACCESS_INLINED);
6447
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006448 // Now go back to the frame that we entered with. This will not overwrite
6449 // the receiver or key registers since they were not in use when we came
6450 // in. The instructions emitted by this merge are skipped over by the
6451 // inline load patching mechanism when looking for the branch instruction
6452 // that tells it where the code to patch is.
6453 copied_frame.MergeTo(frame_state()->frame());
6454
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006455 // Block the constant pool for one more instruction after leaving this
6456 // constant pool block scope to include the branch instruction ending the
6457 // deferred code.
6458 __ BlockConstPoolFor(1);
6459 }
6460}
6461
6462
6463class DeferredReferenceSetKeyedValue: public DeferredCode {
6464 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006465 DeferredReferenceSetKeyedValue(Register value,
6466 Register key,
6467 Register receiver)
6468 : value_(value), key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006469 set_comment("[ DeferredReferenceSetKeyedValue");
6470 }
6471
6472 virtual void Generate();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006473
6474 private:
6475 Register value_;
6476 Register key_;
6477 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006478};
6479
6480
6481void DeferredReferenceSetKeyedValue::Generate() {
6482 Register scratch1 = VirtualFrame::scratch0();
6483 Register scratch2 = VirtualFrame::scratch1();
6484 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
6485 __ IncrementCounter(
6486 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
6487
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006488 // Ensure value in r0, key in r1 and receiver in r2 to match keyed store ic
6489 // calling convention.
6490 if (value_.is(r1)) {
6491 __ Swap(r0, r1, ip);
6492 }
6493 ASSERT(receiver_.is(r2));
6494
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006495 // The rest of the instructions in the deferred code must be together.
6496 { Assembler::BlockConstPoolScope block_const_pool(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006497 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6498 // r1 and r2.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006499 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
6500 __ Call(ic, RelocInfo::CODE_TARGET);
6501 // The call must be followed by a nop instruction to indicate that the
6502 // keyed store has been inlined.
6503 __ nop(PROPERTY_ACCESS_INLINED);
6504
6505 // Block the constant pool for one more instruction after leaving this
6506 // constant pool block scope to include the branch instruction ending the
6507 // deferred code.
6508 __ BlockConstPoolFor(1);
6509 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006510}
6511
6512
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006513class DeferredReferenceSetNamedValue: public DeferredCode {
6514 public:
6515 DeferredReferenceSetNamedValue(Register value,
6516 Register receiver,
6517 Handle<String> name)
6518 : value_(value), receiver_(receiver), name_(name) {
6519 set_comment("[ DeferredReferenceSetNamedValue");
6520 }
6521
6522 virtual void Generate();
6523
6524 private:
6525 Register value_;
6526 Register receiver_;
6527 Handle<String> name_;
6528};
6529
6530
6531// Takes value in r0, receiver in r1 and returns the result (the
6532// value) in r0.
6533void DeferredReferenceSetNamedValue::Generate() {
6534 // Record the entry frame and spill.
6535 VirtualFrame copied_frame(*frame_state()->frame());
6536 copied_frame.SpillAll();
6537
6538 // Ensure value in r0, receiver in r1 to match store ic calling
6539 // convention.
6540 ASSERT(value_.is(r0) && receiver_.is(r1));
6541 __ mov(r2, Operand(name_));
6542
6543 // The rest of the instructions in the deferred code must be together.
6544 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6545 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6546 // r1 and r2.
6547 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
6548 __ Call(ic, RelocInfo::CODE_TARGET);
6549 // The call must be followed by a nop instruction to indicate that the
6550 // named store has been inlined.
6551 __ nop(PROPERTY_ACCESS_INLINED);
6552
6553 // Go back to the frame we entered with. The instructions
6554 // generated by this merge are skipped over by the inline store
6555 // patching mechanism when looking for the branch instruction that
6556 // tells it where the code to patch is.
6557 copied_frame.MergeTo(frame_state()->frame());
6558
6559 // Block the constant pool for one more instruction after leaving this
6560 // constant pool block scope to include the branch instruction ending the
6561 // deferred code.
6562 __ BlockConstPoolFor(1);
6563 }
6564}
6565
6566
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006567// Consumes the top of stack (the receiver) and pushes the result instead.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006568void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
6569 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6570 Comment cmnt(masm(), "[ Load from named Property");
6571 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00006572 frame_->CallLoadIC(name,
6573 is_contextual
6574 ? RelocInfo::CODE_TARGET_CONTEXT
6575 : RelocInfo::CODE_TARGET);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006576 frame_->EmitPush(r0); // Push answer.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006577 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006578 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006579 Comment cmnt(masm(), "[ Inlined named property load");
6580
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006581 // Counter will be decremented in the deferred code. Placed here to avoid
6582 // having it in the instruction stream below where patching will occur.
6583 __ IncrementCounter(&Counters::named_load_inline, 1,
6584 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006585
6586 // The following instructions are the inlined load of an in-object property.
6587 // Parts of this code is patched, so the exact instructions generated needs
6588 // to be fixed. Therefore the instruction pool is blocked when generating
6589 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006590
6591 // Load the receiver from the stack.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006592 Register receiver = frame_->PopToRegister();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006593
6594 DeferredReferenceGetNamedValue* deferred =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006595 new DeferredReferenceGetNamedValue(receiver, name);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006596
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006597#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006598 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006599 Label check_inlined_codesize;
6600 masm_->bind(&check_inlined_codesize);
6601#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006602
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006603 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006604 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006605 __ tst(receiver, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006606 deferred->Branch(eq);
6607
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006608 Register scratch = VirtualFrame::scratch0();
6609 Register scratch2 = VirtualFrame::scratch1();
6610
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006611 // Check the map. The null map used below is patched by the inline cache
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006612 // code. Therefore we can't use a LoadRoot call.
6613 __ ldr(scratch, FieldMemOperand(receiver, HeapObject::kMapOffset));
6614 __ mov(scratch2, Operand(Factory::null_value()));
6615 __ cmp(scratch, scratch2);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006616 deferred->Branch(ne);
6617
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006618 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006619 // inline cache code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006620 __ ldr(receiver, MemOperand(receiver, 0));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006621
6622 // Make sure that the expected number of instructions are generated.
6623 ASSERT_EQ(kInlinedNamedLoadInstructions,
6624 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006625 }
6626
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006627 deferred->BindExit();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006628 // At this point the receiver register has the result, either from the
6629 // deferred code or from the inlined code.
6630 frame_->EmitPush(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006631 }
6632}
6633
6634
ager@chromium.orgac091b72010-05-05 07:34:42 +00006635void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) {
6636#ifdef DEBUG
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006637 int expected_height = frame()->height() - (is_contextual ? 1 : 2);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006638#endif
ager@chromium.orgac091b72010-05-05 07:34:42 +00006639
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006640 Result result;
6641 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6642 frame()->CallStoreIC(name, is_contextual);
6643 } else {
6644 // Inline the in-object property case.
6645 JumpTarget slow, done;
6646
6647 // Get the value and receiver from the stack.
6648 frame()->PopToR0();
6649 Register value = r0;
6650 frame()->PopToR1();
6651 Register receiver = r1;
6652
6653 DeferredReferenceSetNamedValue* deferred =
6654 new DeferredReferenceSetNamedValue(value, receiver, name);
6655
6656 // Check that the receiver is a heap object.
6657 __ tst(receiver, Operand(kSmiTagMask));
6658 deferred->Branch(eq);
6659
6660 // The following instructions are the part of the inlined
6661 // in-object property store code which can be patched. Therefore
6662 // the exact number of instructions generated must be fixed, so
6663 // the constant pool is blocked while generating this code.
6664 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6665 Register scratch0 = VirtualFrame::scratch0();
6666 Register scratch1 = VirtualFrame::scratch1();
6667
6668 // Check the map. Initially use an invalid map to force a
6669 // failure. The map check will be patched in the runtime system.
6670 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
6671
6672#ifdef DEBUG
6673 Label check_inlined_codesize;
6674 masm_->bind(&check_inlined_codesize);
6675#endif
6676 __ mov(scratch0, Operand(Factory::null_value()));
6677 __ cmp(scratch0, scratch1);
6678 deferred->Branch(ne);
6679
6680 int offset = 0;
6681 __ str(value, MemOperand(receiver, offset));
6682
6683 // Update the write barrier and record its size. We do not use
6684 // the RecordWrite macro here because we want the offset
6685 // addition instruction first to make it easy to patch.
6686 Label record_write_start, record_write_done;
6687 __ bind(&record_write_start);
6688 // Add offset into the object.
6689 __ add(scratch0, receiver, Operand(offset));
6690 // Test that the object is not in the new space. We cannot set
6691 // region marks for new space pages.
6692 __ InNewSpace(receiver, scratch1, eq, &record_write_done);
6693 // Record the actual write.
6694 __ RecordWriteHelper(receiver, scratch0, scratch1);
6695 __ bind(&record_write_done);
6696 // Clobber all input registers when running with the debug-code flag
6697 // turned on to provoke errors.
6698 if (FLAG_debug_code) {
6699 __ mov(receiver, Operand(BitCast<int32_t>(kZapValue)));
6700 __ mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
6701 __ mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
6702 }
6703 // Check that this is the first inlined write barrier or that
6704 // this inlined write barrier has the same size as all the other
6705 // inlined write barriers.
6706 ASSERT((inlined_write_barrier_size_ == -1) ||
6707 (inlined_write_barrier_size_ ==
6708 masm()->InstructionsGeneratedSince(&record_write_start)));
6709 inlined_write_barrier_size_ =
6710 masm()->InstructionsGeneratedSince(&record_write_start);
6711
6712 // Make sure that the expected number of instructions are generated.
6713 ASSERT_EQ(GetInlinedNamedStoreInstructionsAfterPatch(),
6714 masm()->InstructionsGeneratedSince(&check_inlined_codesize));
6715 }
6716 deferred->BindExit();
6717 }
6718 ASSERT_EQ(expected_height, frame()->height());
ager@chromium.orgac091b72010-05-05 07:34:42 +00006719}
6720
6721
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006722void CodeGenerator::EmitKeyedLoad() {
6723 if (loop_nesting() == 0) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006724 Comment cmnt(masm_, "[ Load from keyed property");
6725 frame_->CallKeyedLoadIC();
6726 } else {
6727 // Inline the keyed load.
6728 Comment cmnt(masm_, "[ Inlined load from keyed property");
6729
6730 // Counter will be decremented in the deferred code. Placed here to avoid
6731 // having it in the instruction stream below where patching will occur.
6732 __ IncrementCounter(&Counters::keyed_load_inline, 1,
6733 frame_->scratch0(), frame_->scratch1());
6734
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006735 // Load the key and receiver from the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006736 bool key_is_known_smi = frame_->KnownSmiAt(0);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006737 Register key = frame_->PopToRegister();
6738 Register receiver = frame_->PopToRegister(key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006739
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006740 // The deferred code expects key and receiver in registers.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006741 DeferredReferenceGetKeyedValue* deferred =
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006742 new DeferredReferenceGetKeyedValue(key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006743
6744 // Check that the receiver is a heap object.
6745 __ tst(receiver, Operand(kSmiTagMask));
6746 deferred->Branch(eq);
6747
6748 // The following instructions are the part of the inlined load keyed
6749 // property code which can be patched. Therefore the exact number of
6750 // instructions generated need to be fixed, so the constant pool is blocked
6751 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006752 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6753 Register scratch1 = VirtualFrame::scratch0();
6754 Register scratch2 = VirtualFrame::scratch1();
6755 // Check the map. The null map used below is patched by the inline cache
6756 // code.
6757 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006758
6759 // Check that the key is a smi.
6760 if (!key_is_known_smi) {
6761 __ tst(key, Operand(kSmiTagMask));
6762 deferred->Branch(ne);
6763 }
6764
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006765#ifdef DEBUG
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006766 Label check_inlined_codesize;
6767 masm_->bind(&check_inlined_codesize);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006768#endif
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006769 __ mov(scratch2, Operand(Factory::null_value()));
6770 __ cmp(scratch1, scratch2);
6771 deferred->Branch(ne);
6772
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00006773 // Get the elements array from the receiver.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006774 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00006775 __ AssertFastElements(scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006776
6777 // Check that key is within bounds. Use unsigned comparison to handle
6778 // negative keys.
6779 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006780 __ cmp(scratch2, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006781 deferred->Branch(ls); // Unsigned less equal.
6782
6783 // Load and check that the result is not the hole (key is a smi).
6784 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
6785 __ add(scratch1,
6786 scratch1,
6787 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006788 __ ldr(scratch1,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006789 MemOperand(scratch1, key, LSL,
6790 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006791 __ cmp(scratch1, scratch2);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006792 deferred->Branch(eq);
6793
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006794 __ mov(r0, scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006795 // Make sure that the expected number of instructions are generated.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006796 ASSERT_EQ(GetInlinedKeyedLoadInstructionsAfterPatch(),
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006797 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6798 }
6799
6800 deferred->BindExit();
6801 }
6802}
6803
6804
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006805void CodeGenerator::EmitKeyedStore(StaticType* key_type,
6806 WriteBarrierCharacter wb_info) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006807 // Generate inlined version of the keyed store if the code is in a loop
6808 // and the key is likely to be a smi.
6809 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
6810 // Inline the keyed store.
6811 Comment cmnt(masm_, "[ Inlined store to keyed property");
6812
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006813 Register scratch1 = VirtualFrame::scratch0();
6814 Register scratch2 = VirtualFrame::scratch1();
6815 Register scratch3 = r3;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006816
6817 // Counter will be decremented in the deferred code. Placed here to avoid
6818 // having it in the instruction stream below where patching will occur.
6819 __ IncrementCounter(&Counters::keyed_store_inline, 1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006820 scratch1, scratch2);
6821
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006822
6823
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006824 // Load the value, key and receiver from the stack.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006825 bool value_is_harmless = frame_->KnownSmiAt(0);
6826 if (wb_info == NEVER_NEWSPACE) value_is_harmless = true;
6827 bool key_is_smi = frame_->KnownSmiAt(1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006828 Register value = frame_->PopToRegister();
6829 Register key = frame_->PopToRegister(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006830 VirtualFrame::SpilledScope spilled(frame_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006831 Register receiver = r2;
6832 frame_->EmitPop(receiver);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006833
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006834#ifdef DEBUG
6835 bool we_remembered_the_write_barrier = value_is_harmless;
6836#endif
6837
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006838 // The deferred code expects value, key and receiver in registers.
6839 DeferredReferenceSetKeyedValue* deferred =
6840 new DeferredReferenceSetKeyedValue(value, key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006841
6842 // Check that the value is a smi. As this inlined code does not set the
6843 // write barrier it is only possible to store smi values.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006844 if (!value_is_harmless) {
6845 // If the value is not likely to be a Smi then let's test the fixed array
6846 // for new space instead. See below.
6847 if (wb_info == LIKELY_SMI) {
6848 __ tst(value, Operand(kSmiTagMask));
6849 deferred->Branch(ne);
6850#ifdef DEBUG
6851 we_remembered_the_write_barrier = true;
6852#endif
6853 }
6854 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006855
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006856 if (!key_is_smi) {
6857 // Check that the key is a smi.
6858 __ tst(key, Operand(kSmiTagMask));
6859 deferred->Branch(ne);
6860 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006861
6862 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006863 __ tst(receiver, Operand(kSmiTagMask));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006864 deferred->Branch(eq);
6865
6866 // Check that the receiver is a JSArray.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006867 __ CompareObjectType(receiver, scratch1, scratch1, JS_ARRAY_TYPE);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006868 deferred->Branch(ne);
6869
6870 // Check that the key is within bounds. Both the key and the length of
6871 // the JSArray are smis. Use unsigned comparison to handle negative keys.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006872 __ ldr(scratch1, FieldMemOperand(receiver, JSArray::kLengthOffset));
6873 __ cmp(scratch1, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006874 deferred->Branch(ls); // Unsigned less equal.
6875
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006876 // Get the elements array from the receiver.
6877 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
6878 if (!value_is_harmless && wb_info != LIKELY_SMI) {
6879 Label ok;
6880 __ and_(scratch2, scratch1, Operand(ExternalReference::new_space_mask()));
6881 __ cmp(scratch2, Operand(ExternalReference::new_space_start()));
6882 __ tst(value, Operand(kSmiTagMask), ne);
6883 deferred->Branch(ne);
6884#ifdef DEBUG
6885 we_remembered_the_write_barrier = true;
6886#endif
6887 }
6888 // Check that the elements array is not a dictionary.
6889 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006890 // The following instructions are the part of the inlined store keyed
6891 // property code which can be patched. Therefore the exact number of
6892 // instructions generated need to be fixed, so the constant pool is blocked
6893 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006894 { Assembler::BlockConstPoolScope block_const_pool(masm_);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006895#ifdef DEBUG
6896 Label check_inlined_codesize;
6897 masm_->bind(&check_inlined_codesize);
6898#endif
6899
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006900 // Read the fixed array map from the constant pool (not from the root
6901 // array) so that the value can be patched. When debugging, we patch this
6902 // comparison to always fail so that we will hit the IC call in the
6903 // deferred code which will allow the debugger to break for fast case
6904 // stores.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006905 __ mov(scratch3, Operand(Factory::fixed_array_map()));
6906 __ cmp(scratch2, scratch3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006907 deferred->Branch(ne);
6908
6909 // Store the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006910 __ add(scratch1, scratch1,
6911 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6912 __ str(value,
6913 MemOperand(scratch1, key, LSL,
6914 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006915
6916 // Make sure that the expected number of instructions are generated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006917 ASSERT_EQ(kInlinedKeyedStoreInstructionsAfterPatch,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006918 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6919 }
6920
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006921 ASSERT(we_remembered_the_write_barrier);
6922
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006923 deferred->BindExit();
6924 } else {
6925 frame()->CallKeyedStoreIC();
6926 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006927}
6928
6929
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006930#ifdef DEBUG
6931bool CodeGenerator::HasValidEntryRegisters() { return true; }
6932#endif
6933
6934
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006935#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006936#define __ ACCESS_MASM(masm)
6937
ager@chromium.org7c537e22008-10-16 08:43:32 +00006938Handle<String> Reference::GetName() {
6939 ASSERT(type_ == NAMED);
6940 Property* property = expression_->AsProperty();
6941 if (property == NULL) {
6942 // Global variable reference treated as a named property reference.
6943 VariableProxy* proxy = expression_->AsVariableProxy();
6944 ASSERT(proxy->AsVariable() != NULL);
6945 ASSERT(proxy->AsVariable()->is_global());
6946 return proxy->name();
6947 } else {
6948 Literal* raw_name = property->key()->AsLiteral();
6949 ASSERT(raw_name != NULL);
6950 return Handle<String>(String::cast(*raw_name->handle()));
6951 }
6952}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006953
ager@chromium.org7c537e22008-10-16 08:43:32 +00006954
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006955void Reference::DupIfPersist() {
6956 if (persist_after_get_) {
6957 switch (type_) {
6958 case KEYED:
6959 cgen_->frame()->Dup2();
6960 break;
6961 case NAMED:
6962 cgen_->frame()->Dup();
6963 // Fall through.
6964 case UNLOADED:
6965 case ILLEGAL:
6966 case SLOT:
6967 // Do nothing.
6968 ;
6969 }
6970 } else {
6971 set_unloaded();
6972 }
6973}
6974
6975
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006976void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006977 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006978 ASSERT(!is_illegal());
6979 ASSERT(!cgen_->has_cc());
6980 MacroAssembler* masm = cgen_->masm();
6981 Property* property = expression_->AsProperty();
6982 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006983 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006984 }
6985
6986 switch (type_) {
6987 case SLOT: {
6988 Comment cmnt(masm, "[ Load from Slot");
6989 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
6990 ASSERT(slot != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006991 DupIfPersist();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006992 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006993 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006994 }
6995
ager@chromium.org7c537e22008-10-16 08:43:32 +00006996 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00006997 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006998 bool is_global = var != NULL;
6999 ASSERT(!is_global || var->is_global());
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007000 Handle<String> name = GetName();
7001 DupIfPersist();
7002 cgen_->EmitNamedLoad(name, is_global);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007003 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007004 }
7005
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007006 case KEYED: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007007 ASSERT(property != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007008 DupIfPersist();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007009 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007010 cgen_->frame()->EmitPush(r0);
7011 break;
7012 }
7013
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007014 default:
7015 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007016 }
7017}
7018
7019
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007020void Reference::SetValue(InitState init_state, WriteBarrierCharacter wb_info) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00007021 ASSERT(!is_illegal());
7022 ASSERT(!cgen_->has_cc());
7023 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00007024 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007025 Property* property = expression_->AsProperty();
7026 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00007027 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007028 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007029
ager@chromium.org7c537e22008-10-16 08:43:32 +00007030 switch (type_) {
7031 case SLOT: {
7032 Comment cmnt(masm, "[ Store to Slot");
7033 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007034 cgen_->StoreToSlot(slot, init_state);
ager@chromium.orgac091b72010-05-05 07:34:42 +00007035 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007036 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007037 }
7038
ager@chromium.org7c537e22008-10-16 08:43:32 +00007039 case NAMED: {
7040 Comment cmnt(masm, "[ Store to named Property");
ager@chromium.orgac091b72010-05-05 07:34:42 +00007041 cgen_->EmitNamedStore(GetName(), false);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00007042 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00007043 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007044 break;
7045 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007046
ager@chromium.org7c537e22008-10-16 08:43:32 +00007047 case KEYED: {
7048 Comment cmnt(masm, "[ Store to keyed Property");
7049 Property* property = expression_->AsProperty();
7050 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00007051 cgen_->CodeForSourcePosition(property->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007052 cgen_->EmitKeyedStore(property->key()->type(), wb_info);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00007053 frame->EmitPush(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007054 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007055 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007056 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00007057
7058 default:
7059 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007060 }
7061}
7062
7063
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007064const char* GenericBinaryOpStub::GetName() {
7065 if (name_ != NULL) return name_;
7066 const int len = 100;
7067 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
7068 if (name_ == NULL) return "OOM";
7069 const char* op_name = Token::Name(op_);
7070 const char* overwrite_name;
7071 switch (mode_) {
7072 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
7073 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
7074 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
7075 default: overwrite_name = "UnknownOverwrite"; break;
7076 }
7077
7078 OS::SNPrintF(Vector<char>(name_, len),
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007079 "GenericBinaryOpStub_%s_%s%s_%s",
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007080 op_name,
7081 overwrite_name,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007082 specialized_on_rhs_ ? "_ConstantRhs" : "",
7083 BinaryOpIC::GetName(runtime_operands_type_));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007084 return name_;
7085}
7086
7087
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007088#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007089
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007090} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007091
7092#endif // V8_TARGET_ARCH_ARM