blob: 2855ca4f3b3ac55df962a092296af33804866726 [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000030#if defined(V8_TARGET_ARCH_ARM)
31
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000032#include "code-stubs.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000033#include "codegen-inl.h"
34#include "compiler.h"
35#include "debug.h"
36#include "full-codegen.h"
37#include "parser.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000038#include "scopes.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000039
40namespace v8 {
41namespace internal {
42
43#define __ ACCESS_MASM(masm_)
44
45// Generate code for a JS function. On entry to the function the receiver
46// and arguments have been pushed on the stack left to right. The actual
47// argument count matches the formal parameter count expected by the
48// function.
49//
50// The live registers are:
51// o r1: the JS function object being called (ie, ourselves)
52// o cp: our context
53// o fp: our caller's frame pointer
54// o sp: stack pointer
55// o lr: return address
56//
57// The function builds a JS frame. Please see JavaScriptFrameConstants in
58// frames-arm.h for its layout.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000059void FullCodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.org5c838252010-02-19 08:53:10 +000060 ASSERT(info_ == NULL);
61 info_ = info;
62 SetFunctionPosition(function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000063 Comment cmnt(masm_, "[ function compiled by full code generator");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000064
vegorov@chromium.org42841962010-10-18 11:18:59 +000065#ifdef DEBUG
66 if (strlen(FLAG_stop_at) > 0 &&
67 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
68 __ stop("stop-at");
69 }
70#endif
71
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000072 int locals_count = scope()->num_stack_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000073
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000074 __ Push(lr, fp, cp, r1);
75 if (locals_count > 0) {
76 // Load undefined value here, so the value is ready for the loop
77 // below.
78 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
79 }
80 // Adjust fp to point to caller's fp.
81 __ add(fp, sp, Operand(2 * kPointerSize));
82
83 { Comment cmnt(masm_, "[ Allocate locals");
84 for (int i = 0; i < locals_count; i++) {
85 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000086 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000087 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000088
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000089 bool function_in_register = true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000090
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000091 // Possibly allocate a local context.
92 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
93 if (heap_slots > 0) {
94 Comment cmnt(masm_, "[ Allocate local context");
95 // Argument to NewContext is the function, which is in r1.
96 __ push(r1);
97 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
98 FastNewContextStub stub(heap_slots);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000099 __ CallStub(&stub);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000100 } else {
101 __ CallRuntime(Runtime::kNewContext, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000102 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000103 function_in_register = false;
104 // Context is returned in both r0 and cp. It replaces the context
105 // passed to us. It's saved in the stack and kept live in cp.
106 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
107 // Copy any necessary parameters into the context.
108 int num_parameters = scope()->num_parameters();
109 for (int i = 0; i < num_parameters; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000110 Slot* slot = scope()->parameter(i)->AsSlot();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000111 if (slot != NULL && slot->type() == Slot::CONTEXT) {
112 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
113 (num_parameters - 1 - i) * kPointerSize;
114 // Load parameter from stack.
115 __ ldr(r0, MemOperand(fp, parameter_offset));
116 // Store it in the context.
117 __ mov(r1, Operand(Context::SlotOffset(slot->index())));
118 __ str(r0, MemOperand(cp, r1));
119 // Update the write barrier. This clobbers all involved
120 // registers, so we have to use two more registers to avoid
121 // clobbering cp.
122 __ mov(r2, Operand(cp));
123 __ RecordWrite(r2, Operand(r1), r3, r0);
124 }
125 }
126 }
127
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000128 Variable* arguments = scope()->arguments();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000129 if (arguments != NULL) {
130 // Function uses arguments object.
131 Comment cmnt(masm_, "[ Allocate arguments object");
132 if (!function_in_register) {
133 // Load this again, if it's used by the local context below.
134 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
135 } else {
136 __ mov(r3, r1);
137 }
138 // Receiver is just before the parameters on the caller's stack.
139 int offset = scope()->num_parameters() * kPointerSize;
140 __ add(r2, fp,
141 Operand(StandardFrameConstants::kCallerSPOffset + offset));
142 __ mov(r1, Operand(Smi::FromInt(scope()->num_parameters())));
143 __ Push(r3, r2, r1);
144
145 // Arguments to ArgumentsAccessStub:
146 // function, receiver address, parameter count.
147 // The stub will rewrite receiever and parameter count if the previous
148 // stack frame was an arguments adapter frame.
149 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
150 __ CallStub(&stub);
151 // Duplicate the value; move-to-slot operation might clobber registers.
152 __ mov(r3, r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000153 Move(arguments->AsSlot(), r0, r1, r2);
154 Slot* dot_arguments_slot = scope()->arguments_shadow()->AsSlot();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000155 Move(dot_arguments_slot, r3, r1, r2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000156 }
157
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000158 { Comment cmnt(masm_, "[ Declarations");
159 // For named function expressions, declare the function name as a
160 // constant.
161 if (scope()->is_function_scope() && scope()->function() != NULL) {
162 EmitDeclaration(scope()->function(), Variable::CONST, NULL);
163 }
164 // Visit all the explicit declarations unless there is an illegal
165 // redeclaration.
166 if (scope()->HasIllegalRedeclaration()) {
167 scope()->VisitIllegalRedeclaration(this);
168 } else {
169 VisitDeclarations(scope()->declarations());
170 }
171 }
172
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000173 // Check the stack for overflow or break request.
174 // Put the lr setup instruction in the delay slot. The kInstrSize is
175 // added to the implicit 8 byte offset that always applies to operations
176 // with pc and gives a return address 12 bytes down.
177 { Comment cmnt(masm_, "[ Stack check");
178 __ LoadRoot(r2, Heap::kStackLimitRootIndex);
179 __ add(lr, pc, Operand(Assembler::kInstrSize));
180 __ cmp(sp, Operand(r2));
181 StackCheckStub stub;
182 __ mov(pc,
183 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
184 RelocInfo::CODE_TARGET),
185 LeaveCC,
186 lo);
187 }
188
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000189 if (FLAG_trace) {
190 __ CallRuntime(Runtime::kTraceEnter, 0);
191 }
192
193 { Comment cmnt(masm_, "[ Body");
194 ASSERT(loop_depth() == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000195 VisitStatements(function()->body());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000196 ASSERT(loop_depth() == 0);
197 }
198
199 { Comment cmnt(masm_, "[ return <undefined>;");
200 // Emit a 'return undefined' in case control fell off the end of the
201 // body.
202 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
203 }
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000204 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000205}
206
207
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000208void FullCodeGenerator::EmitReturnSequence() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000209 Comment cmnt(masm_, "[ Return sequence");
210 if (return_label_.is_bound()) {
211 __ b(&return_label_);
212 } else {
213 __ bind(&return_label_);
214 if (FLAG_trace) {
215 // Push the return value on the stack as the parameter.
216 // Runtime::TraceExit returns its parameter in r0.
217 __ push(r0);
218 __ CallRuntime(Runtime::kTraceExit, 1);
219 }
220
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000221#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000222 // Add a label for checking the size of the code used for returning.
223 Label check_exit_codesize;
224 masm_->bind(&check_exit_codesize);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000225#endif
226 // Make sure that the constant pool is not emitted inside of the return
227 // sequence.
228 { Assembler::BlockConstPoolScope block_const_pool(masm_);
229 // Here we use masm_-> instead of the __ macro to avoid the code coverage
230 // tool from instrumenting as we rely on the code size here.
231 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000232 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000233 __ RecordJSReturn();
234 masm_->mov(sp, fp);
235 masm_->ldm(ia_w, sp, fp.bit() | lr.bit());
236 masm_->add(sp, sp, Operand(sp_delta));
237 masm_->Jump(lr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000238 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000239
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000240#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000241 // Check that the size of the code used for returning matches what is
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000242 // expected by the debugger. If the sp_delts above cannot be encoded in the
243 // add instruction the add will generate two instructions.
244 int return_sequence_length =
245 masm_->InstructionsGeneratedSince(&check_exit_codesize);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000246 CHECK(return_sequence_length ==
247 Assembler::kJSReturnSequenceInstructions ||
248 return_sequence_length ==
249 Assembler::kJSReturnSequenceInstructions + 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000250#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000251 }
252}
253
254
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000255FullCodeGenerator::ConstantOperand FullCodeGenerator::GetConstantOperand(
256 Token::Value op, Expression* left, Expression* right) {
257 ASSERT(ShouldInlineSmiCase(op));
258 return kNoConstants;
259}
260
261
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000262void FullCodeGenerator::EffectContext::Plug(Slot* slot) const {
263}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000264
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000265
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000266void FullCodeGenerator::AccumulatorValueContext::Plug(Slot* slot) const {
267 codegen()->Move(result_register(), slot);
268}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000269
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000270
271void FullCodeGenerator::StackValueContext::Plug(Slot* slot) const {
272 codegen()->Move(result_register(), slot);
273 __ push(result_register());
274}
275
276
277void FullCodeGenerator::TestContext::Plug(Slot* slot) const {
278 // For simplicity we always test the accumulator register.
279 codegen()->Move(result_register(), slot);
280 codegen()->DoTest(true_label_, false_label_, fall_through_);
281}
282
283
284void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
285}
286
287
288void FullCodeGenerator::AccumulatorValueContext::Plug(
289 Heap::RootListIndex index) const {
290 __ LoadRoot(result_register(), index);
291}
292
293
294void FullCodeGenerator::StackValueContext::Plug(
295 Heap::RootListIndex index) const {
296 __ LoadRoot(result_register(), index);
297 __ push(result_register());
298}
299
300
301void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
302 if (index == Heap::kUndefinedValueRootIndex ||
303 index == Heap::kNullValueRootIndex ||
304 index == Heap::kFalseValueRootIndex) {
305 __ b(false_label_);
306 } else if (index == Heap::kTrueValueRootIndex) {
307 __ b(true_label_);
308 } else {
309 __ LoadRoot(result_register(), index);
310 codegen()->DoTest(true_label_, false_label_, fall_through_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000311 }
312}
313
314
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000315void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
316}
317
318
319void FullCodeGenerator::AccumulatorValueContext::Plug(
320 Handle<Object> lit) const {
321 __ mov(result_register(), Operand(lit));
322}
323
324
325void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
326 // Immediates can be pushed directly.
327 __ mov(result_register(), Operand(lit));
328 __ push(result_register());
329}
330
331
332void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
333 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
334 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
335 __ b(false_label_);
336 } else if (lit->IsTrue() || lit->IsJSObject()) {
337 __ b(true_label_);
338 } else if (lit->IsString()) {
339 if (String::cast(*lit)->length() == 0) {
340 __ b(false_label_);
341 } else {
342 __ b(true_label_);
343 }
344 } else if (lit->IsSmi()) {
345 if (Smi::cast(*lit)->value() == 0) {
346 __ b(false_label_);
347 } else {
348 __ b(true_label_);
349 }
350 } else {
351 // For simplicity we always test the accumulator register.
352 __ mov(result_register(), Operand(lit));
353 codegen()->DoTest(true_label_, false_label_, fall_through_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000354 }
355}
356
357
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000358void FullCodeGenerator::EffectContext::DropAndPlug(int count,
359 Register reg) const {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000360 ASSERT(count > 0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000361 __ Drop(count);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000362}
363
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000364
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000365void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
366 int count,
367 Register reg) const {
368 ASSERT(count > 0);
369 __ Drop(count);
370 __ Move(result_register(), reg);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000371}
372
373
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000374void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
375 Register reg) const {
376 ASSERT(count > 0);
377 if (count > 1) __ Drop(count - 1);
378 __ str(reg, MemOperand(sp, 0));
379}
380
381
382void FullCodeGenerator::TestContext::DropAndPlug(int count,
383 Register reg) const {
384 ASSERT(count > 0);
385 // For simplicity we always test the accumulator register.
386 __ Drop(count);
387 __ Move(result_register(), reg);
388 codegen()->DoTest(true_label_, false_label_, fall_through_);
389}
390
391
392void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
393 Label* materialize_false) const {
394 ASSERT_EQ(materialize_true, materialize_false);
395 __ bind(materialize_true);
396}
397
398
399void FullCodeGenerator::AccumulatorValueContext::Plug(
400 Label* materialize_true,
401 Label* materialize_false) const {
402 Label done;
403 __ bind(materialize_true);
404 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
405 __ jmp(&done);
406 __ bind(materialize_false);
407 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
408 __ bind(&done);
409}
410
411
412void FullCodeGenerator::StackValueContext::Plug(
413 Label* materialize_true,
414 Label* materialize_false) const {
415 Label done;
416 __ bind(materialize_true);
417 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
418 __ push(ip);
419 __ jmp(&done);
420 __ bind(materialize_false);
421 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
422 __ push(ip);
423 __ bind(&done);
424}
425
426
427void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
428 Label* materialize_false) const {
429 ASSERT(materialize_false == false_label_);
430 ASSERT(materialize_true == true_label_);
431}
432
433
434void FullCodeGenerator::EffectContext::Plug(bool flag) const {
435}
436
437
438void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
439 Heap::RootListIndex value_root_index =
440 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
441 __ LoadRoot(result_register(), value_root_index);
442}
443
444
445void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
446 Heap::RootListIndex value_root_index =
447 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
448 __ LoadRoot(ip, value_root_index);
449 __ push(ip);
450}
451
452
453void FullCodeGenerator::TestContext::Plug(bool flag) const {
454 if (flag) {
455 if (true_label_ != fall_through_) __ b(true_label_);
456 } else {
457 if (false_label_ != fall_through_) __ b(false_label_);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000458 }
459}
460
461
ricow@chromium.org65fae842010-08-25 15:26:24 +0000462void FullCodeGenerator::DoTest(Label* if_true,
463 Label* if_false,
464 Label* fall_through) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000465 // Call the runtime to find the boolean value of the source and then
466 // translate it into control flow to the pair of labels.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000467 __ push(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000468 __ CallRuntime(Runtime::kToBool, 1);
469 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
470 __ cmp(r0, ip);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000471 Split(eq, if_true, if_false, fall_through);
472}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000473
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000474
ricow@chromium.org65fae842010-08-25 15:26:24 +0000475void FullCodeGenerator::Split(Condition cc,
476 Label* if_true,
477 Label* if_false,
478 Label* fall_through) {
479 if (if_false == fall_through) {
480 __ b(cc, if_true);
481 } else if (if_true == fall_through) {
482 __ b(NegateCondition(cc), if_false);
483 } else {
484 __ b(cc, if_true);
485 __ b(if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000486 }
487}
488
489
490MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
491 switch (slot->type()) {
492 case Slot::PARAMETER:
493 case Slot::LOCAL:
494 return MemOperand(fp, SlotOffset(slot));
495 case Slot::CONTEXT: {
496 int context_chain_length =
ager@chromium.org5c838252010-02-19 08:53:10 +0000497 scope()->ContextChainLength(slot->var()->scope());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000498 __ LoadContext(scratch, context_chain_length);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000499 return ContextOperand(scratch, slot->index());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000500 }
501 case Slot::LOOKUP:
502 UNREACHABLE();
503 }
504 UNREACHABLE();
505 return MemOperand(r0, 0);
506}
507
508
509void FullCodeGenerator::Move(Register destination, Slot* source) {
510 // Use destination as scratch.
511 MemOperand slot_operand = EmitSlotSearch(source, destination);
512 __ ldr(destination, slot_operand);
513}
514
515
516void FullCodeGenerator::Move(Slot* dst,
517 Register src,
518 Register scratch1,
519 Register scratch2) {
520 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
521 ASSERT(!scratch1.is(src) && !scratch2.is(src));
522 MemOperand location = EmitSlotSearch(dst, scratch1);
523 __ str(src, location);
524 // Emit the write barrier code if the location is in the heap.
525 if (dst->type() == Slot::CONTEXT) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000526 __ RecordWrite(scratch1,
527 Operand(Context::SlotOffset(dst->index())),
528 scratch2,
529 src);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000530 }
531}
532
533
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000534void FullCodeGenerator::EmitDeclaration(Variable* variable,
535 Variable::Mode mode,
536 FunctionLiteral* function) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000537 Comment cmnt(masm_, "[ Declaration");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000538 ASSERT(variable != NULL); // Must have been resolved.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000539 Slot* slot = variable->AsSlot();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000540 Property* prop = variable->AsProperty();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000541
542 if (slot != NULL) {
543 switch (slot->type()) {
544 case Slot::PARAMETER:
545 case Slot::LOCAL:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000546 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000547 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
548 __ str(ip, MemOperand(fp, SlotOffset(slot)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000549 } else if (function != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000550 VisitForAccumulatorValue(function);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000551 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
552 }
553 break;
554
555 case Slot::CONTEXT:
556 // We bypass the general EmitSlotSearch because we know more about
557 // this specific context.
558
559 // The variable in the decl always resides in the current context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000560 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000561 if (FLAG_debug_code) {
562 // Check if we have the correct context pointer.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000563 __ ldr(r1, ContextOperand(cp, Context::FCONTEXT_INDEX));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000564 __ cmp(r1, cp);
565 __ Check(eq, "Unexpected declaration in current context.");
566 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000567 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000568 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000569 __ str(ip, ContextOperand(cp, slot->index()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000570 // No write barrier since the_hole_value is in old space.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000571 } else if (function != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000572 VisitForAccumulatorValue(function);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000573 __ str(result_register(), ContextOperand(cp, slot->index()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000574 int offset = Context::SlotOffset(slot->index());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000575 // We know that we have written a function, which is not a smi.
576 __ mov(r1, Operand(cp));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000577 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000578 }
579 break;
580
581 case Slot::LOOKUP: {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000582 __ mov(r2, Operand(variable->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000583 // Declaration nodes are always introduced in one of two modes.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000584 ASSERT(mode == Variable::VAR ||
585 mode == Variable::CONST);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000586 PropertyAttributes attr =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000587 (mode == Variable::VAR) ? NONE : READ_ONLY;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000588 __ mov(r1, Operand(Smi::FromInt(attr)));
589 // Push initial value, if any.
590 // Note: For variables we must not push an initial value (such as
591 // 'undefined') because we may have a (legal) redeclaration and we
592 // must not destroy the current value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000593 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000594 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000595 __ Push(cp, r2, r1, r0);
596 } else if (function != NULL) {
597 __ Push(cp, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000598 // Push initial value for function declaration.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000599 VisitForStackValue(function);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000600 } else {
601 __ mov(r0, Operand(Smi::FromInt(0))); // No initial value!
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000602 __ Push(cp, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000603 }
604 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
605 break;
606 }
607 }
608
609 } else if (prop != NULL) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000610 if (function != NULL || mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000611 // We are declaring a function or constant that rewrites to a
612 // property. Use (keyed) IC to set the initial value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000613 VisitForStackValue(prop->obj());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000614 if (function != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000615 VisitForStackValue(prop->key());
616 VisitForAccumulatorValue(function);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000617 __ pop(r1); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000618 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000619 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000620 __ mov(r1, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000621 __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex);
622 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000623 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000624
625 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000626 EmitCallIC(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000627 // Value in r0 is ignored (declarations are statements).
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000628 }
629 }
630}
631
632
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000633void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
634 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
635}
636
637
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000638void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
639 // Call the runtime to declare the globals.
640 // The context is the first argument.
641 __ mov(r1, Operand(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000642 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000643 __ Push(cp, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000644 __ CallRuntime(Runtime::kDeclareGlobals, 3);
645 // Return value is ignored.
646}
647
648
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000649void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000650 Comment cmnt(masm_, "[ SwitchStatement");
651 Breakable nested_statement(this, stmt);
652 SetStatementPosition(stmt);
653 // Keep the switch value on the stack until a case matches.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000654 VisitForStackValue(stmt->tag());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000655
656 ZoneList<CaseClause*>* clauses = stmt->cases();
657 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
658
659 Label next_test; // Recycled for each test.
660 // Compile all the tests with branches to their bodies.
661 for (int i = 0; i < clauses->length(); i++) {
662 CaseClause* clause = clauses->at(i);
663 // The default is not a test, but remember it as final fall through.
664 if (clause->is_default()) {
665 default_clause = clause;
666 continue;
667 }
668
669 Comment cmnt(masm_, "[ Case comparison");
670 __ bind(&next_test);
671 next_test.Unuse();
672
673 // Compile the label expression.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000674 VisitForAccumulatorValue(clause->label());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000675
ricow@chromium.org65fae842010-08-25 15:26:24 +0000676 // Perform the comparison as if via '==='.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000677 __ ldr(r1, MemOperand(sp, 0)); // Switch value.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000678 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
679 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000680 Label slow_case;
681 __ orr(r2, r1, r0);
682 __ tst(r2, Operand(kSmiTagMask));
683 __ b(ne, &slow_case);
684 __ cmp(r1, r0);
685 __ b(ne, &next_test);
686 __ Drop(1); // Switch value is no longer needed.
687 __ b(clause->body_target()->entry_label());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000688 __ bind(&slow_case);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000689 }
690
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000691 CompareFlags flags = inline_smi_code
692 ? NO_SMI_COMPARE_IN_STUB
693 : NO_COMPARE_FLAGS;
694 CompareStub stub(eq, true, flags, r1, r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000695 __ CallStub(&stub);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000696 __ cmp(r0, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000697 __ b(ne, &next_test);
698 __ Drop(1); // Switch value is no longer needed.
699 __ b(clause->body_target()->entry_label());
700 }
701
702 // Discard the test value and jump to the default if present, otherwise to
703 // the end of the statement.
704 __ bind(&next_test);
705 __ Drop(1); // Switch value is no longer needed.
706 if (default_clause == NULL) {
707 __ b(nested_statement.break_target());
708 } else {
709 __ b(default_clause->body_target()->entry_label());
710 }
711
712 // Compile all the case bodies.
713 for (int i = 0; i < clauses->length(); i++) {
714 Comment cmnt(masm_, "[ Case body");
715 CaseClause* clause = clauses->at(i);
716 __ bind(clause->body_target()->entry_label());
717 VisitStatements(clause->statements());
718 }
719
720 __ bind(nested_statement.break_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000721}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000722
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000723
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000724void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000725 Comment cmnt(masm_, "[ ForInStatement");
726 SetStatementPosition(stmt);
727
728 Label loop, exit;
729 ForIn loop_statement(this, stmt);
730 increment_loop_depth();
731
732 // Get the object to enumerate over. Both SpiderMonkey and JSC
733 // ignore null and undefined in contrast to the specification; see
734 // ECMA-262 section 12.6.4.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000735 VisitForAccumulatorValue(stmt->enumerable());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000736 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
737 __ cmp(r0, ip);
738 __ b(eq, &exit);
739 __ LoadRoot(ip, Heap::kNullValueRootIndex);
740 __ cmp(r0, ip);
741 __ b(eq, &exit);
742
743 // Convert the object to a JS object.
744 Label convert, done_convert;
745 __ BranchOnSmi(r0, &convert);
746 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
747 __ b(hs, &done_convert);
748 __ bind(&convert);
749 __ push(r0);
750 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
751 __ bind(&done_convert);
752 __ push(r0);
753
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000754 // BUG(867): Check cache validity in generated code. This is a fast
755 // case for the JSObject::IsSimpleEnum cache validity checks. If we
756 // cannot guarantee cache validity, call the runtime system to check
757 // cache validity or get the property names in a fixed array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000758
759 // Get the set of properties to enumerate.
760 __ push(r0); // Duplicate the enumerable object on the stack.
761 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
762
763 // If we got a map from the runtime call, we can do a fast
764 // modification check. Otherwise, we got a fixed array, and we have
765 // to do a slow check.
766 Label fixed_array;
767 __ mov(r2, r0);
768 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
769 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
770 __ cmp(r1, ip);
771 __ b(ne, &fixed_array);
772
773 // We got a map in register r0. Get the enumeration cache from it.
774 __ ldr(r1, FieldMemOperand(r0, Map::kInstanceDescriptorsOffset));
775 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
776 __ ldr(r2, FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
777
778 // Setup the four remaining stack slots.
779 __ push(r0); // Map.
780 __ ldr(r1, FieldMemOperand(r2, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000781 __ mov(r0, Operand(Smi::FromInt(0)));
782 // Push enumeration cache, enumeration cache length (as smi) and zero.
783 __ Push(r2, r1, r0);
784 __ jmp(&loop);
785
786 // We got a fixed array in register r0. Iterate through that.
787 __ bind(&fixed_array);
788 __ mov(r1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
789 __ Push(r1, r0);
790 __ ldr(r1, FieldMemOperand(r0, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000791 __ mov(r0, Operand(Smi::FromInt(0)));
792 __ Push(r1, r0); // Fixed array length (as smi) and initial index.
793
794 // Generate code for doing the condition check.
795 __ bind(&loop);
796 // Load the current count to r0, load the length to r1.
797 __ Ldrd(r0, r1, MemOperand(sp, 0 * kPointerSize));
798 __ cmp(r0, r1); // Compare to the array length.
799 __ b(hs, loop_statement.break_target());
800
801 // Get the current entry of the array into register r3.
802 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
803 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
804 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
805
806 // Get the expected map from the stack or a zero map in the
807 // permanent slow case into register r2.
808 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
809
810 // Check if the expected map still matches that of the enumerable.
811 // If not, we have to filter the key.
812 Label update_each;
813 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
814 __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
815 __ cmp(r4, Operand(r2));
816 __ b(eq, &update_each);
817
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000818 // Convert the entry to a string or (smi) 0 if it isn't a property
819 // any more. If the property has been removed while iterating, we
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000820 // just skip it.
821 __ push(r1); // Enumerable.
822 __ push(r3); // Current entry.
823 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000824 __ mov(r3, Operand(r0), SetCC);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000825 __ b(eq, loop_statement.continue_target());
826
827 // Update the 'each' property or variable from the possibly filtered
828 // entry in register r3.
829 __ bind(&update_each);
830 __ mov(result_register(), r3);
831 // Perform the assignment as if via '='.
832 EmitAssignment(stmt->each());
833
834 // Generate code for the body of the loop.
835 Label stack_limit_hit, stack_check_done;
836 Visit(stmt->body());
837
838 __ StackLimitCheck(&stack_limit_hit);
839 __ bind(&stack_check_done);
840
841 // Generate code for the going to the next element by incrementing
842 // the index (smi) stored on top of the stack.
843 __ bind(loop_statement.continue_target());
844 __ pop(r0);
845 __ add(r0, r0, Operand(Smi::FromInt(1)));
846 __ push(r0);
847 __ b(&loop);
848
849 // Slow case for the stack limit check.
850 StackCheckStub stack_check_stub;
851 __ bind(&stack_limit_hit);
852 __ CallStub(&stack_check_stub);
853 __ b(&stack_check_done);
854
855 // Remove the pointers stored on the stack.
856 __ bind(loop_statement.break_target());
857 __ Drop(5);
858
859 // Exit and decrement the loop depth.
860 __ bind(&exit);
861 decrement_loop_depth();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000862}
863
864
865void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info) {
866 // Use the fast case closure allocation code that allocates in new
867 // space for nested functions that don't need literals cloning.
868 if (scope()->is_function_scope() && info->num_literals() == 0) {
869 FastNewClosureStub stub;
870 __ mov(r0, Operand(info));
871 __ push(r0);
872 __ CallStub(&stub);
873 } else {
874 __ mov(r0, Operand(info));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000875 __ Push(cp, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000876 __ CallRuntime(Runtime::kNewClosure, 2);
877 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000878 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000879}
880
881
882void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
883 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000884 EmitVariableLoad(expr->var());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000885}
886
887
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000888MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(
889 Slot* slot,
890 Label* slow) {
891 ASSERT(slot->type() == Slot::CONTEXT);
892 Register current = cp;
893 Register next = r3;
894 Register temp = r4;
895
896 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
897 if (s->num_heap_slots() > 0) {
898 if (s->calls_eval()) {
899 // Check that extension is NULL.
900 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
901 __ tst(temp, temp);
902 __ b(ne, slow);
903 }
904 __ ldr(next, ContextOperand(current, Context::CLOSURE_INDEX));
905 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
906 // Walk the rest of the chain without clobbering cp.
907 current = next;
908 }
909 }
910 // Check that last extension is NULL.
911 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
912 __ tst(temp, temp);
913 __ b(ne, slow);
914 __ ldr(temp, ContextOperand(current, Context::FCONTEXT_INDEX));
915 return ContextOperand(temp, slot->index());
916}
917
918
919void FullCodeGenerator::EmitDynamicLoadFromSlotFastCase(
920 Slot* slot,
921 TypeofState typeof_state,
922 Label* slow,
923 Label* done) {
924 // Generate fast-case code for variables that might be shadowed by
925 // eval-introduced variables. Eval is used a lot without
926 // introducing variables. In those cases, we do not want to
927 // perform a runtime call for all variables in the scope
928 // containing the eval.
929 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
930 EmitLoadGlobalSlotCheckExtensions(slot, typeof_state, slow);
931 __ jmp(done);
932 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000933 Slot* potential_slot = slot->var()->local_if_not_shadowed()->AsSlot();
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000934 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
935 if (potential_slot != NULL) {
936 // Generate fast case for locals that rewrite to slots.
937 __ ldr(r0, ContextSlotOperandCheckExtensions(potential_slot, slow));
938 if (potential_slot->var()->mode() == Variable::CONST) {
939 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
940 __ cmp(r0, ip);
941 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
942 }
943 __ jmp(done);
944 } else if (rewrite != NULL) {
945 // Generate fast case for calls of an argument function.
946 Property* property = rewrite->AsProperty();
947 if (property != NULL) {
948 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
949 Literal* key_literal = property->key()->AsLiteral();
950 if (obj_proxy != NULL &&
951 key_literal != NULL &&
952 obj_proxy->IsArguments() &&
953 key_literal->handle()->IsSmi()) {
954 // Load arguments object if there are no eval-introduced
955 // variables. Then load the argument from the arguments
956 // object using keyed load.
957 __ ldr(r1,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000958 ContextSlotOperandCheckExtensions(obj_proxy->var()->AsSlot(),
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000959 slow));
960 __ mov(r0, Operand(key_literal->handle()));
961 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000962 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000963 __ jmp(done);
964 }
965 }
966 }
967 }
968}
969
970
971void FullCodeGenerator::EmitLoadGlobalSlotCheckExtensions(
972 Slot* slot,
973 TypeofState typeof_state,
974 Label* slow) {
975 Register current = cp;
976 Register next = r1;
977 Register temp = r2;
978
979 Scope* s = scope();
980 while (s != NULL) {
981 if (s->num_heap_slots() > 0) {
982 if (s->calls_eval()) {
983 // Check that extension is NULL.
984 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
985 __ tst(temp, temp);
986 __ b(ne, slow);
987 }
988 // Load next context in chain.
989 __ ldr(next, ContextOperand(current, Context::CLOSURE_INDEX));
990 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
991 // Walk the rest of the chain without clobbering cp.
992 current = next;
993 }
994 // If no outer scope calls eval, we do not need to check more
995 // context extensions.
996 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
997 s = s->outer_scope();
998 }
999
1000 if (s->is_eval_scope()) {
1001 Label loop, fast;
1002 if (!current.is(next)) {
1003 __ Move(next, current);
1004 }
1005 __ bind(&loop);
1006 // Terminate at global context.
1007 __ ldr(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1008 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1009 __ cmp(temp, ip);
1010 __ b(eq, &fast);
1011 // Check that extension is NULL.
1012 __ ldr(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1013 __ tst(temp, temp);
1014 __ b(ne, slow);
1015 // Load next context in chain.
1016 __ ldr(next, ContextOperand(next, Context::CLOSURE_INDEX));
1017 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
1018 __ b(&loop);
1019 __ bind(&fast);
1020 }
1021
1022 __ ldr(r0, CodeGenerator::GlobalObject());
1023 __ mov(r2, Operand(slot->var()->name()));
1024 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1025 ? RelocInfo::CODE_TARGET
1026 : RelocInfo::CODE_TARGET_CONTEXT;
1027 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001028 EmitCallIC(ic, mode);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001029}
1030
1031
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001032void FullCodeGenerator::EmitVariableLoad(Variable* var) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001033 // Four cases: non-this global variables, lookup slots, all other
1034 // types of slots, and parameters that rewrite to explicit property
1035 // accesses on the arguments object.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001036 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001037 Property* property = var->AsProperty();
1038
1039 if (var->is_global() && !var->is_this()) {
1040 Comment cmnt(masm_, "Global variable");
1041 // Use inline caching. Variable name is passed in r2 and the global
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001042 // object (receiver) in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001043 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001044 __ mov(r2, Operand(var->name()));
1045 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001046 EmitCallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001047 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001048
1049 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001050 Label done, slow;
1051
1052 // Generate code for loading from variables potentially shadowed
1053 // by eval-introduced variables.
1054 EmitDynamicLoadFromSlotFastCase(slot, NOT_INSIDE_TYPEOF, &slow, &done);
1055
1056 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001057 Comment cmnt(masm_, "Lookup slot");
1058 __ mov(r1, Operand(var->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001059 __ Push(cp, r1); // Context and name.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001060 __ CallRuntime(Runtime::kLoadContextSlot, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001061 __ bind(&done);
1062
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001063 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001064
1065 } else if (slot != NULL) {
1066 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1067 ? "Context slot"
1068 : "Stack slot");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001069 if (var->mode() == Variable::CONST) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001070 // Constants may be the hole value if they have not been initialized.
1071 // Unhole them.
1072 MemOperand slot_operand = EmitSlotSearch(slot, r0);
1073 __ ldr(r0, slot_operand);
1074 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1075 __ cmp(r0, ip);
1076 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
1077 context()->Plug(r0);
1078 } else {
1079 context()->Plug(slot);
1080 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001081 } else {
1082 Comment cmnt(masm_, "Rewritten parameter");
1083 ASSERT_NOT_NULL(property);
1084 // Rewritten parameter accesses are of the form "slot[literal]".
1085
1086 // Assert that the object is in a slot.
1087 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
1088 ASSERT_NOT_NULL(object_var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001089 Slot* object_slot = object_var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001090 ASSERT_NOT_NULL(object_slot);
1091
1092 // Load the object.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001093 Move(r1, object_slot);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001094
1095 // Assert that the key is a smi.
1096 Literal* key_literal = property->key()->AsLiteral();
1097 ASSERT_NOT_NULL(key_literal);
1098 ASSERT(key_literal->handle()->IsSmi());
1099
1100 // Load the key.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001101 __ mov(r0, Operand(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001102
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001103 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001104 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001105 EmitCallIC(ic, RelocInfo::CODE_TARGET);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001106 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001107 }
1108}
1109
1110
1111void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1112 Comment cmnt(masm_, "[ RegExpLiteral");
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001113 Label materialized;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001114 // Registers will be used as follows:
1115 // r4 = JS function, literals array
1116 // r3 = literal index
1117 // r2 = RegExp pattern
1118 // r1 = RegExp flags
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001119 // r0 = temp + materialized value (RegExp literal)
ricow@chromium.org65fae842010-08-25 15:26:24 +00001120 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1121 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001122 int literal_offset =
ricow@chromium.org65fae842010-08-25 15:26:24 +00001123 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001124 __ ldr(r0, FieldMemOperand(r4, literal_offset));
1125 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1126 __ cmp(r0, ip);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001127 __ b(ne, &materialized);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001128
1129 // Create regexp literal using runtime function.
1130 // Result will be in r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001131 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
1132 __ mov(r2, Operand(expr->pattern()));
1133 __ mov(r1, Operand(expr->flags()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001134 __ Push(r4, r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001135 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001136
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001137 __ bind(&materialized);
1138 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1139 __ push(r0);
1140 __ mov(r0, Operand(Smi::FromInt(size)));
1141 __ push(r0);
1142 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001143
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001144 // After this, registers are used as follows:
1145 // r0: Newly allocated regexp.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001146 // r1: Materialized regexp.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001147 // r2: temp.
1148 __ pop(r1);
1149 __ CopyFields(r0, r1, r2.bit(), size / kPointerSize);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001150 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001151}
1152
1153
1154void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1155 Comment cmnt(masm_, "[ ObjectLiteral");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001156 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1157 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1158 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1159 __ mov(r1, Operand(expr->constant_properties()));
1160 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001161 __ Push(r3, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001162 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001163 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001164 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001165 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001166 }
1167
1168 // If result_saved is true the result is on top of the stack. If
1169 // result_saved is false the result is in r0.
1170 bool result_saved = false;
1171
1172 for (int i = 0; i < expr->properties()->length(); i++) {
1173 ObjectLiteral::Property* property = expr->properties()->at(i);
1174 if (property->IsCompileTimeValue()) continue;
1175
1176 Literal* key = property->key();
1177 Expression* value = property->value();
1178 if (!result_saved) {
1179 __ push(r0); // Save result on stack
1180 result_saved = true;
1181 }
1182 switch (property->kind()) {
1183 case ObjectLiteral::Property::CONSTANT:
1184 UNREACHABLE();
1185 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1186 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1187 // Fall through.
1188 case ObjectLiteral::Property::COMPUTED:
1189 if (key->handle()->IsSymbol()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001190 VisitForAccumulatorValue(value);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001191 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001192 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001193 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001194 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001195 break;
1196 }
1197 // Fall through.
1198 case ObjectLiteral::Property::PROTOTYPE:
1199 // Duplicate receiver on stack.
1200 __ ldr(r0, MemOperand(sp));
1201 __ push(r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001202 VisitForStackValue(key);
1203 VisitForStackValue(value);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001204 __ CallRuntime(Runtime::kSetProperty, 3);
1205 break;
1206 case ObjectLiteral::Property::GETTER:
1207 case ObjectLiteral::Property::SETTER:
1208 // Duplicate receiver on stack.
1209 __ ldr(r0, MemOperand(sp));
1210 __ push(r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001211 VisitForStackValue(key);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001212 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1213 Smi::FromInt(1) :
1214 Smi::FromInt(0)));
1215 __ push(r1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001216 VisitForStackValue(value);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001217 __ CallRuntime(Runtime::kDefineAccessor, 4);
1218 break;
1219 }
1220 }
1221
1222 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001223 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001224 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001225 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001226 }
1227}
1228
1229
1230void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1231 Comment cmnt(masm_, "[ ArrayLiteral");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001232
1233 ZoneList<Expression*>* subexprs = expr->values();
1234 int length = subexprs->length();
1235
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001236 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1237 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1238 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1239 __ mov(r1, Operand(expr->constant_elements()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001240 __ Push(r3, r2, r1);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001241 if (expr->constant_elements()->map() == Heap::fixed_cow_array_map()) {
1242 FastCloneShallowArrayStub stub(
1243 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1244 __ CallStub(&stub);
1245 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
1246 } else if (expr->depth() > 1) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001247 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001248 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001249 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001250 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001251 FastCloneShallowArrayStub stub(
1252 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001253 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001254 }
1255
1256 bool result_saved = false; // Is the result saved to the stack?
1257
1258 // Emit code to evaluate all the non-constant subexpressions and to store
1259 // them into the newly cloned array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001260 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001261 Expression* subexpr = subexprs->at(i);
1262 // If the subexpression is a literal or a simple materialized literal it
1263 // is already set in the cloned array.
1264 if (subexpr->AsLiteral() != NULL ||
1265 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1266 continue;
1267 }
1268
1269 if (!result_saved) {
1270 __ push(r0);
1271 result_saved = true;
1272 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001273 VisitForAccumulatorValue(subexpr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001274
1275 // Store the subexpression value in the array's elements.
1276 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
1277 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1278 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1279 __ str(result_register(), FieldMemOperand(r1, offset));
1280
1281 // Update the write barrier for the array store with r0 as the scratch
1282 // register.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001283 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001284 }
1285
1286 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001287 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001288 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001289 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001290 }
1291}
1292
1293
ager@chromium.org5c838252010-02-19 08:53:10 +00001294void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1295 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001296 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1297 // on the left-hand side.
1298 if (!expr->target()->IsValidLeftHandSide()) {
1299 VisitForEffect(expr->target());
1300 return;
1301 }
1302
ager@chromium.org5c838252010-02-19 08:53:10 +00001303 // Left-hand side can only be a property, a global or a (parameter or local)
1304 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1305 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1306 LhsKind assign_type = VARIABLE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001307 Property* property = expr->target()->AsProperty();
1308 if (property != NULL) {
1309 assign_type = (property->key()->IsPropertyName())
1310 ? NAMED_PROPERTY
1311 : KEYED_PROPERTY;
ager@chromium.org5c838252010-02-19 08:53:10 +00001312 }
1313
1314 // Evaluate LHS expression.
1315 switch (assign_type) {
1316 case VARIABLE:
1317 // Nothing to do here.
1318 break;
1319 case NAMED_PROPERTY:
1320 if (expr->is_compound()) {
1321 // We need the receiver both on the stack and in the accumulator.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001322 VisitForAccumulatorValue(property->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00001323 __ push(result_register());
1324 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001325 VisitForStackValue(property->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00001326 }
1327 break;
1328 case KEYED_PROPERTY:
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001329 if (expr->is_compound()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001330 VisitForStackValue(property->obj());
1331 VisitForAccumulatorValue(property->key());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001332 __ ldr(r1, MemOperand(sp, 0));
1333 __ push(r0);
1334 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001335 VisitForStackValue(property->obj());
1336 VisitForStackValue(property->key());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001337 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001338 break;
1339 }
1340
ager@chromium.org5c838252010-02-19 08:53:10 +00001341 if (expr->is_compound()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001342 { AccumulatorValueContext context(this);
1343 switch (assign_type) {
1344 case VARIABLE:
1345 EmitVariableLoad(expr->target()->AsVariableProxy()->var());
1346 break;
1347 case NAMED_PROPERTY:
1348 EmitNamedPropertyLoad(property);
1349 break;
1350 case KEYED_PROPERTY:
1351 EmitKeyedPropertyLoad(property);
1352 break;
1353 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001354 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001355
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001356 Token::Value op = expr->binary_op();
1357 ConstantOperand constant = ShouldInlineSmiCase(op)
1358 ? GetConstantOperand(op, expr->target(), expr->value())
1359 : kNoConstants;
1360 ASSERT(constant == kRightConstant || constant == kNoConstants);
1361 if (constant == kNoConstants) {
1362 __ push(r0); // Left operand goes on the stack.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001363 VisitForAccumulatorValue(expr->value());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001364 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001365
ricow@chromium.org65fae842010-08-25 15:26:24 +00001366 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1367 ? OVERWRITE_RIGHT
1368 : NO_OVERWRITE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001369 SetSourcePosition(expr->position() + 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001370 AccumulatorValueContext context(this);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001371 if (ShouldInlineSmiCase(op)) {
1372 EmitInlineSmiBinaryOp(expr,
1373 op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001374 mode,
1375 expr->target(),
1376 expr->value(),
1377 constant);
1378 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001379 EmitBinaryOp(op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001380 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001381 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001382 VisitForAccumulatorValue(expr->value());
ager@chromium.org5c838252010-02-19 08:53:10 +00001383 }
1384
1385 // Record source position before possible IC call.
1386 SetSourcePosition(expr->position());
1387
1388 // Store the value.
1389 switch (assign_type) {
1390 case VARIABLE:
1391 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001392 expr->op());
ager@chromium.org5c838252010-02-19 08:53:10 +00001393 break;
1394 case NAMED_PROPERTY:
1395 EmitNamedPropertyAssignment(expr);
1396 break;
1397 case KEYED_PROPERTY:
1398 EmitKeyedPropertyAssignment(expr);
1399 break;
1400 }
1401}
1402
1403
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001404void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1405 SetSourcePosition(prop->position());
1406 Literal* key = prop->key()->AsLiteral();
1407 __ mov(r2, Operand(key->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001408 // Call load IC. It has arguments receiver and property name r0 and r2.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001409 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001410 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001411}
1412
1413
1414void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1415 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001416 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001417 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001418 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001419}
1420
1421
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001422void FullCodeGenerator::EmitInlineSmiBinaryOp(Expression* expr,
1423 Token::Value op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001424 OverwriteMode mode,
1425 Expression* left,
1426 Expression* right,
1427 ConstantOperand constant) {
1428 ASSERT(constant == kNoConstants); // Only handled case.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001429 EmitBinaryOp(op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001430}
1431
1432
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001433void FullCodeGenerator::EmitBinaryOp(Token::Value op,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001434 OverwriteMode mode) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001435 __ pop(r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001436 GenericBinaryOpStub stub(op, mode, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001437 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001438 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001439}
1440
1441
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001442void FullCodeGenerator::EmitAssignment(Expression* expr) {
1443 // Invalid left-hand sides are rewritten to have a 'throw
1444 // ReferenceError' on the left-hand side.
1445 if (!expr->IsValidLeftHandSide()) {
1446 VisitForEffect(expr);
1447 return;
1448 }
1449
1450 // Left-hand side can only be a property, a global or a (parameter or local)
1451 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1452 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1453 LhsKind assign_type = VARIABLE;
1454 Property* prop = expr->AsProperty();
1455 if (prop != NULL) {
1456 assign_type = (prop->key()->IsPropertyName())
1457 ? NAMED_PROPERTY
1458 : KEYED_PROPERTY;
1459 }
1460
1461 switch (assign_type) {
1462 case VARIABLE: {
1463 Variable* var = expr->AsVariableProxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001464 EffectContext context(this);
1465 EmitVariableAssignment(var, Token::ASSIGN);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001466 break;
1467 }
1468 case NAMED_PROPERTY: {
1469 __ push(r0); // Preserve value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001470 VisitForAccumulatorValue(prop->obj());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001471 __ mov(r1, r0);
1472 __ pop(r0); // Restore value.
1473 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
1474 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001475 EmitCallIC(ic, RelocInfo::CODE_TARGET);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001476 break;
1477 }
1478 case KEYED_PROPERTY: {
1479 __ push(r0); // Preserve value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001480 VisitForStackValue(prop->obj());
1481 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001482 __ mov(r1, r0);
1483 __ pop(r2);
1484 __ pop(r0); // Restore value.
1485 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001486 EmitCallIC(ic, RelocInfo::CODE_TARGET);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001487 break;
1488 }
1489 }
1490}
1491
1492
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001493void FullCodeGenerator::EmitVariableAssignment(Variable* var,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001494 Token::Value op) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001495 // Left-hand sides that rewrite to explicit property accesses do not reach
1496 // here.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001497 ASSERT(var != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001498 ASSERT(var->is_global() || var->AsSlot() != NULL);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001499
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001500 if (var->is_global()) {
1501 ASSERT(!var->is_this());
1502 // Assignment to a global variable. Use inline caching for the
1503 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001504 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001505 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001506 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001507 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001508 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001509
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001510 } else if (var->mode() != Variable::CONST || op == Token::INIT_CONST) {
1511 // Perform the assignment for non-const variables and for initialization
1512 // of const variables. Const assignments are simply skipped.
1513 Label done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001514 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001515 switch (slot->type()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001516 case Slot::PARAMETER:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001517 case Slot::LOCAL:
1518 if (op == Token::INIT_CONST) {
1519 // Detect const reinitialization by checking for the hole value.
1520 __ ldr(r1, MemOperand(fp, SlotOffset(slot)));
1521 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1522 __ cmp(r1, ip);
1523 __ b(ne, &done);
1524 }
1525 // Perform the assignment.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001526 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1527 break;
1528
1529 case Slot::CONTEXT: {
1530 MemOperand target = EmitSlotSearch(slot, r1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001531 if (op == Token::INIT_CONST) {
1532 // Detect const reinitialization by checking for the hole value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001533 __ ldr(r2, target);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001534 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001535 __ cmp(r2, ip);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001536 __ b(ne, &done);
1537 }
1538 // Perform the assignment and issue the write barrier.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001539 __ str(result_register(), target);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001540 // RecordWrite may destroy all its register arguments.
1541 __ mov(r3, result_register());
1542 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001543 __ RecordWrite(r1, Operand(offset), r2, r3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001544 break;
1545 }
1546
1547 case Slot::LOOKUP:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001548 // Call the runtime for the assignment. The runtime will ignore
1549 // const reinitialization.
1550 __ push(r0); // Value.
1551 __ mov(r0, Operand(slot->var()->name()));
1552 __ Push(cp, r0); // Context and name.
1553 if (op == Token::INIT_CONST) {
1554 // The runtime will ignore const redeclaration.
1555 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1556 } else {
1557 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1558 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001559 break;
1560 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001561 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001562 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001563
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001564 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001565}
1566
1567
1568void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1569 // Assignment to a property, using a named store IC.
1570 Property* prop = expr->target()->AsProperty();
1571 ASSERT(prop != NULL);
1572 ASSERT(prop->key()->AsLiteral() != NULL);
1573
1574 // If the assignment starts a block of assignments to the same object,
1575 // change to slow case to avoid the quadratic behavior of repeatedly
1576 // adding fast properties.
1577 if (expr->starts_initialization_block()) {
1578 __ push(result_register());
1579 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1580 __ push(ip);
1581 __ CallRuntime(Runtime::kToSlowProperties, 1);
1582 __ pop(result_register());
1583 }
1584
1585 // Record source code position before IC call.
1586 SetSourcePosition(expr->position());
1587 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001588 // Load receiver to r1. Leave a copy in the stack if needed for turning the
1589 // receiver into fast case.
ager@chromium.org5c838252010-02-19 08:53:10 +00001590 if (expr->ends_initialization_block()) {
1591 __ ldr(r1, MemOperand(sp));
1592 } else {
1593 __ pop(r1);
1594 }
1595
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001596 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001597 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001598
1599 // If the assignment ends an initialization block, revert to fast case.
1600 if (expr->ends_initialization_block()) {
1601 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001602 // Receiver is under the result value.
1603 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001604 __ push(ip);
1605 __ CallRuntime(Runtime::kToFastProperties, 1);
1606 __ pop(r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001607 context()->DropAndPlug(1, r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001608 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001609 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001610 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001611}
1612
1613
1614void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1615 // Assignment to a property, using a keyed store IC.
1616
1617 // If the assignment starts a block of assignments to the same object,
1618 // change to slow case to avoid the quadratic behavior of repeatedly
1619 // adding fast properties.
1620 if (expr->starts_initialization_block()) {
1621 __ push(result_register());
1622 // Receiver is now under the key and value.
1623 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1624 __ push(ip);
1625 __ CallRuntime(Runtime::kToSlowProperties, 1);
1626 __ pop(result_register());
1627 }
1628
1629 // Record source code position before IC call.
1630 SetSourcePosition(expr->position());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001631 __ pop(r1); // Key.
1632 // Load receiver to r2. Leave a copy in the stack if needed for turning the
1633 // receiver into fast case.
1634 if (expr->ends_initialization_block()) {
1635 __ ldr(r2, MemOperand(sp));
1636 } else {
1637 __ pop(r2);
1638 }
1639
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001640 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001641 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001642
1643 // If the assignment ends an initialization block, revert to fast case.
1644 if (expr->ends_initialization_block()) {
1645 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001646 // Receiver is under the result value.
1647 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001648 __ push(ip);
1649 __ CallRuntime(Runtime::kToFastProperties, 1);
1650 __ pop(r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001651 context()->DropAndPlug(1, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001652 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001653 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001654 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001655}
1656
1657
1658void FullCodeGenerator::VisitProperty(Property* expr) {
1659 Comment cmnt(masm_, "[ Property");
1660 Expression* key = expr->key();
1661
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001662 if (key->IsPropertyName()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001663 VisitForAccumulatorValue(expr->obj());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001664 EmitNamedPropertyLoad(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001665 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001666 VisitForStackValue(expr->obj());
1667 VisitForAccumulatorValue(expr->key());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001668 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001669 EmitKeyedPropertyLoad(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001670 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001671 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001672}
1673
1674void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001675 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001676 RelocInfo::Mode mode) {
1677 // Code common for calls using the IC.
1678 ZoneList<Expression*>* args = expr->arguments();
1679 int arg_count = args->length();
1680 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001681 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001682 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001683 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001684 // Record source position for debugger.
1685 SetSourcePosition(expr->position());
1686 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001687 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1688 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001689 EmitCallIC(ic, mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001690 // Restore context register.
1691 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001692 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001693}
1694
1695
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001696void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
1697 Expression* key,
1698 RelocInfo::Mode mode) {
1699 // Code common for calls using the IC.
1700 ZoneList<Expression*>* args = expr->arguments();
1701 int arg_count = args->length();
1702 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001703 VisitForStackValue(args->at(i));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001704 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001705 VisitForAccumulatorValue(key);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001706 __ mov(r2, r0);
1707 // Record source position for debugger.
1708 SetSourcePosition(expr->position());
1709 // Call the IC initialization code.
1710 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1711 Handle<Code> ic = CodeGenerator::ComputeKeyedCallInitialize(arg_count,
1712 in_loop);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001713 EmitCallIC(ic, mode);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001714 // Restore context register.
1715 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001716 context()->Plug(r0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001717}
1718
1719
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001720void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1721 // Code common for calls using the call stub.
1722 ZoneList<Expression*>* args = expr->arguments();
1723 int arg_count = args->length();
1724 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001725 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001726 }
1727 // Record source position for debugger.
1728 SetSourcePosition(expr->position());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001729 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1730 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001731 __ CallStub(&stub);
1732 // Restore context register.
1733 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001734 context()->DropAndPlug(1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001735}
1736
1737
1738void FullCodeGenerator::VisitCall(Call* expr) {
1739 Comment cmnt(masm_, "[ Call");
1740 Expression* fun = expr->expression();
1741 Variable* var = fun->AsVariableProxy()->AsVariable();
1742
1743 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001744 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1745 // resolve the function we need to call and the receiver of the
1746 // call. Then we call the resolved function using the given
1747 // arguments.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001748 VisitForStackValue(fun);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001749 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1750 __ push(r2); // Reserved receiver slot.
1751
1752 // Push the arguments.
1753 ZoneList<Expression*>* args = expr->arguments();
1754 int arg_count = args->length();
1755 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001756 VisitForStackValue(args->at(i));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001757 }
1758
1759 // Push copy of the function - found below the arguments.
1760 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1761 __ push(r1);
1762
1763 // Push copy of the first argument or undefined if it doesn't exist.
1764 if (arg_count > 0) {
1765 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1766 __ push(r1);
1767 } else {
1768 __ push(r2);
1769 }
1770
1771 // Push the receiver of the enclosing function and do runtime call.
1772 __ ldr(r1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1773 __ push(r1);
1774 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1775
1776 // The runtime call returns a pair of values in r0 (function) and
1777 // r1 (receiver). Touch up the stack with the right values.
1778 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1779 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
1780
1781 // Record source position for debugger.
1782 SetSourcePosition(expr->position());
1783 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1784 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1785 __ CallStub(&stub);
1786 // Restore context register.
1787 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001788 context()->DropAndPlug(1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001789 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001790 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001791 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001792 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001793 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001794 } else if (var != NULL && var->AsSlot() != NULL &&
1795 var->AsSlot()->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001796 // Call to a lookup slot (dynamically introduced variable).
1797 Label slow, done;
1798
1799 // Generate code for loading from variables potentially shadowed
1800 // by eval-introduced variables.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001801 EmitDynamicLoadFromSlotFastCase(var->AsSlot(),
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001802 NOT_INSIDE_TYPEOF,
1803 &slow,
1804 &done);
1805
1806 __ bind(&slow);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001807 // Call the runtime to find the function to call (returned in r0)
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001808 // and the object holding it (returned in edx).
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001809 __ push(context_register());
1810 __ mov(r2, Operand(var->name()));
1811 __ push(r2);
1812 __ CallRuntime(Runtime::kLoadContextSlot, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001813 __ Push(r0, r1); // Function, receiver.
1814
1815 // If fast case code has been generated, emit code to push the
1816 // function and receiver and have the slow path jump around this
1817 // code.
1818 if (done.is_linked()) {
1819 Label call;
1820 __ b(&call);
1821 __ bind(&done);
1822 // Push function.
1823 __ push(r0);
1824 // Push global receiver.
1825 __ ldr(r1, CodeGenerator::GlobalObject());
1826 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1827 __ push(r1);
1828 __ bind(&call);
1829 }
1830
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001831 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001832 } else if (fun->AsProperty() != NULL) {
1833 // Call to an object property.
1834 Property* prop = fun->AsProperty();
1835 Literal* key = prop->key()->AsLiteral();
1836 if (key != NULL && key->handle()->IsSymbol()) {
1837 // Call to a named property, use call IC.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001838 VisitForStackValue(prop->obj());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001839 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1840 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001841 // Call to a keyed property.
1842 // For a synthetic property use keyed load IC followed by function call,
1843 // for a regular property use keyed CallIC.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001844 VisitForStackValue(prop->obj());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001845 if (prop->is_synthetic()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001846 VisitForAccumulatorValue(prop->key());
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001847 // Record source code position for IC call.
1848 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001849 __ pop(r1); // We do not need to keep the receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001850
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001851 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001852 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001853 __ ldr(r1, CodeGenerator::GlobalObject());
1854 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001855 __ Push(r0, r1); // Function, receiver.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001856 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001857 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001858 EmitKeyedCallWithIC(expr, prop->key(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001859 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001860 }
1861 } else {
1862 // Call to some other expression. If the expression is an anonymous
1863 // function literal not called in a loop, mark it as one that should
1864 // also use the fast code generator.
1865 FunctionLiteral* lit = fun->AsFunctionLiteral();
1866 if (lit != NULL &&
1867 lit->name()->Equals(Heap::empty_string()) &&
1868 loop_depth() == 0) {
1869 lit->set_try_full_codegen(true);
1870 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001871 VisitForStackValue(fun);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001872 // Load global receiver object.
1873 __ ldr(r1, CodeGenerator::GlobalObject());
1874 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1875 __ push(r1);
1876 // Emit function call.
1877 EmitCallWithStub(expr);
1878 }
1879}
1880
1881
1882void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1883 Comment cmnt(masm_, "[ CallNew");
1884 // According to ECMA-262, section 11.2.2, page 44, the function
1885 // expression in new calls must be evaluated before the
1886 // arguments.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001887
1888 // Push constructor on the stack. If it's not a function it's used as
1889 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
1890 // ignored.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001891 VisitForStackValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001892
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001893 // Push the arguments ("left-to-right") on the stack.
1894 ZoneList<Expression*>* args = expr->arguments();
1895 int arg_count = args->length();
1896 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001897 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001898 }
1899
1900 // Call the construct call builtin that handles allocation and
1901 // constructor invocation.
1902 SetSourcePosition(expr->position());
1903
ricow@chromium.org65fae842010-08-25 15:26:24 +00001904 // Load function and argument count into r1 and r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001905 __ mov(r0, Operand(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001906 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001907
1908 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1909 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001910 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001911}
1912
1913
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001914void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1915 ASSERT(args->length() == 1);
1916
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001917 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001918
1919 Label materialize_true, materialize_false;
1920 Label* if_true = NULL;
1921 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001922 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001923 context()->PrepareTest(&materialize_true, &materialize_false,
1924 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001925
1926 __ BranchOnSmi(r0, if_true);
1927 __ b(if_false);
1928
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001929 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001930}
1931
1932
1933void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1934 ASSERT(args->length() == 1);
1935
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001936 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001937
1938 Label materialize_true, materialize_false;
1939 Label* if_true = NULL;
1940 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001941 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001942 context()->PrepareTest(&materialize_true, &materialize_false,
1943 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001944
1945 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001946 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001947
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001948 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001949}
1950
1951
1952void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1953 ASSERT(args->length() == 1);
1954
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001955 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001956
1957 Label materialize_true, materialize_false;
1958 Label* if_true = NULL;
1959 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001960 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001961 context()->PrepareTest(&materialize_true, &materialize_false,
1962 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001963
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001964 __ BranchOnSmi(r0, if_false);
1965 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1966 __ cmp(r0, ip);
1967 __ b(eq, if_true);
1968 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1969 // Undetectable objects behave like undefined when tested with typeof.
1970 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
1971 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1972 __ b(ne, if_false);
1973 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1974 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
1975 __ b(lt, if_false);
1976 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001977 Split(le, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001978
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001979 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001980}
1981
1982
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001983void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
1984 ASSERT(args->length() == 1);
1985
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001986 VisitForAccumulatorValue(args->at(0));
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001987
1988 Label materialize_true, materialize_false;
1989 Label* if_true = NULL;
1990 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001991 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001992 context()->PrepareTest(&materialize_true, &materialize_false,
1993 &if_true, &if_false, &fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001994
1995 __ BranchOnSmi(r0, if_false);
1996 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001997 Split(ge, if_true, if_false, fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001998
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001999 context()->Plug(if_true, if_false);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002000}
2001
2002
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002003void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
2004 ASSERT(args->length() == 1);
2005
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002006 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002007
2008 Label materialize_true, materialize_false;
2009 Label* if_true = NULL;
2010 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002011 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002012 context()->PrepareTest(&materialize_true, &materialize_false,
2013 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002014
2015 __ BranchOnSmi(r0, if_false);
2016 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2017 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
2018 __ tst(r1, Operand(1 << Map::kIsUndetectable));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002019 Split(ne, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002020
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002021 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002022}
2023
2024
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002025void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2026 ZoneList<Expression*>* args) {
2027
2028 ASSERT(args->length() == 1);
2029
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002030 VisitForAccumulatorValue(args->at(0));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002031
2032 Label materialize_true, materialize_false;
2033 Label* if_true = NULL;
2034 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002035 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002036 context()->PrepareTest(&materialize_true, &materialize_false,
2037 &if_true, &if_false, &fall_through);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002038
2039 // Just indicate false, as %_IsStringWrapperSafeForDefaultValueOf() is only
2040 // used in a few functions in runtime.js which should not normally be hit by
2041 // this compiler.
2042 __ jmp(if_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002043 context()->Plug(if_true, if_false);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002044}
2045
2046
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002047void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
2048 ASSERT(args->length() == 1);
2049
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002050 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002051
2052 Label materialize_true, materialize_false;
2053 Label* if_true = NULL;
2054 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002055 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002056 context()->PrepareTest(&materialize_true, &materialize_false,
2057 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002058
2059 __ BranchOnSmi(r0, if_false);
2060 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002061 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002062
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002063 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002064}
2065
2066
2067void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
2068 ASSERT(args->length() == 1);
2069
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002070 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002071
2072 Label materialize_true, materialize_false;
2073 Label* if_true = NULL;
2074 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002075 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002076 context()->PrepareTest(&materialize_true, &materialize_false,
2077 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002078
2079 __ BranchOnSmi(r0, if_false);
2080 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002081 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002082
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002083 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002084}
2085
2086
2087void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
2088 ASSERT(args->length() == 1);
2089
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002090 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002091
2092 Label materialize_true, materialize_false;
2093 Label* if_true = NULL;
2094 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002095 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002096 context()->PrepareTest(&materialize_true, &materialize_false,
2097 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002098
2099 __ BranchOnSmi(r0, if_false);
2100 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002101 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002102
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002103 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002104}
2105
2106
2107
2108void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
2109 ASSERT(args->length() == 0);
2110
2111 Label materialize_true, materialize_false;
2112 Label* if_true = NULL;
2113 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002114 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002115 context()->PrepareTest(&materialize_true, &materialize_false,
2116 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002117
2118 // Get the frame pointer for the calling frame.
2119 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2120
2121 // Skip the arguments adaptor frame if it exists.
2122 Label check_frame_marker;
2123 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
2124 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2125 __ b(ne, &check_frame_marker);
2126 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
2127
2128 // Check the marker in the calling frame.
2129 __ bind(&check_frame_marker);
2130 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
2131 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002132 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002133
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002134 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002135}
2136
2137
2138void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
2139 ASSERT(args->length() == 2);
2140
2141 // Load the two objects into registers and perform the comparison.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002142 VisitForStackValue(args->at(0));
2143 VisitForAccumulatorValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002144
2145 Label materialize_true, materialize_false;
2146 Label* if_true = NULL;
2147 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002148 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002149 context()->PrepareTest(&materialize_true, &materialize_false,
2150 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002151
2152 __ pop(r1);
2153 __ cmp(r0, r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002154 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002155
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002156 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002157}
2158
2159
2160void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
2161 ASSERT(args->length() == 1);
2162
2163 // ArgumentsAccessStub expects the key in edx and the formal
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002164 // parameter count in r0.
2165 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002166 __ mov(r1, r0);
2167 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2168 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2169 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002170 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002171}
2172
2173
2174void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
2175 ASSERT(args->length() == 0);
2176
2177 Label exit;
2178 // Get the number of formal parameters.
2179 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2180
2181 // Check if the calling frame is an arguments adaptor frame.
2182 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2183 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
2184 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2185 __ b(ne, &exit);
2186
2187 // Arguments adaptor case: Read the arguments length from the
2188 // adaptor frame.
2189 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2190
2191 __ bind(&exit);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002192 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002193}
2194
2195
2196void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
2197 ASSERT(args->length() == 1);
2198 Label done, null, function, non_function_constructor;
2199
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002200 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002201
2202 // If the object is a smi, we return null.
2203 __ BranchOnSmi(r0, &null);
2204
2205 // Check that the object is a JS object but take special care of JS
2206 // functions to make sure they have 'Function' as their class.
2207 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2208 __ b(lt, &null);
2209
2210 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2211 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2212 // LAST_JS_OBJECT_TYPE.
2213 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2214 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2215 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2216 __ b(eq, &function);
2217
2218 // Check if the constructor in the map is a function.
2219 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2220 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2221 __ b(ne, &non_function_constructor);
2222
2223 // r0 now contains the constructor function. Grab the
2224 // instance class name from there.
2225 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2226 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2227 __ b(&done);
2228
2229 // Functions have class 'Function'.
2230 __ bind(&function);
2231 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2232 __ jmp(&done);
2233
2234 // Objects with a non-function constructor have class 'Object'.
2235 __ bind(&non_function_constructor);
2236 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2237 __ jmp(&done);
2238
2239 // Non-JS objects have class null.
2240 __ bind(&null);
2241 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2242
2243 // All done.
2244 __ bind(&done);
2245
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002246 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002247}
2248
2249
2250void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2251 // Conditionally generate a log call.
2252 // Args:
2253 // 0 (literal string): The type of logging (corresponds to the flags).
2254 // This is used to determine whether or not to generate the log call.
2255 // 1 (string): Format string. Access the string at argument index 2
2256 // with '%2s' (see Logger::LogRuntime for all the formats).
2257 // 2 (array): Arguments to the format string.
2258 ASSERT_EQ(args->length(), 3);
2259#ifdef ENABLE_LOGGING_AND_PROFILING
2260 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002261 VisitForStackValue(args->at(1));
2262 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002263 __ CallRuntime(Runtime::kLog, 2);
2264 }
2265#endif
2266 // Finally, we're expected to leave a value on the top of the stack.
2267 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002268 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002269}
2270
2271
2272void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2273 ASSERT(args->length() == 0);
2274
2275 Label slow_allocate_heapnumber;
2276 Label heapnumber_allocated;
2277
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002278 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
2279 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002280 __ jmp(&heapnumber_allocated);
2281
2282 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002283 // Allocate a heap number.
2284 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002285 __ mov(r4, Operand(r0));
2286
2287 __ bind(&heapnumber_allocated);
2288
2289 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
2290 // by computing:
2291 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2292 if (CpuFeatures::IsSupported(VFP3)) {
2293 __ PrepareCallCFunction(0, r1);
2294 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
2295
2296 CpuFeatures::Scope scope(VFP3);
2297 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2298 // Create this constant using mov/orr to avoid PC relative load.
2299 __ mov(r1, Operand(0x41000000));
2300 __ orr(r1, r1, Operand(0x300000));
2301 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
2302 __ vmov(d7, r0, r1);
2303 // Move 0x4130000000000000 to VFP.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00002304 __ mov(r0, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002305 __ vmov(d8, r0, r1);
2306 // Subtract and store the result in the heap number.
2307 __ vsub(d7, d7, d8);
2308 __ sub(r0, r4, Operand(kHeapObjectTag));
2309 __ vstr(d7, r0, HeapNumber::kValueOffset);
2310 __ mov(r0, r4);
2311 } else {
2312 __ mov(r0, Operand(r4));
2313 __ PrepareCallCFunction(1, r1);
2314 __ CallCFunction(
2315 ExternalReference::fill_heap_number_with_random_function(), 1);
2316 }
2317
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002318 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002319}
2320
2321
2322void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
2323 // Load the arguments on the stack and call the stub.
2324 SubStringStub stub;
2325 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002326 VisitForStackValue(args->at(0));
2327 VisitForStackValue(args->at(1));
2328 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002329 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002330 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002331}
2332
2333
2334void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
2335 // Load the arguments on the stack and call the stub.
2336 RegExpExecStub stub;
2337 ASSERT(args->length() == 4);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002338 VisitForStackValue(args->at(0));
2339 VisitForStackValue(args->at(1));
2340 VisitForStackValue(args->at(2));
2341 VisitForStackValue(args->at(3));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002342 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002343 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002344}
2345
2346
2347void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
2348 ASSERT(args->length() == 1);
2349
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002350 VisitForAccumulatorValue(args->at(0)); // Load the object.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002351
2352 Label done;
2353 // If the object is a smi return the object.
2354 __ BranchOnSmi(r0, &done);
2355 // If the object is not a value type, return the object.
2356 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
2357 __ b(ne, &done);
2358 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2359
2360 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002361 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002362}
2363
2364
2365void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
2366 // Load the arguments on the stack and call the runtime function.
2367 ASSERT(args->length() == 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002368 VisitForStackValue(args->at(0));
2369 VisitForStackValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002370 __ CallRuntime(Runtime::kMath_pow, 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002371 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002372}
2373
2374
2375void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
2376 ASSERT(args->length() == 2);
2377
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002378 VisitForStackValue(args->at(0)); // Load the object.
2379 VisitForAccumulatorValue(args->at(1)); // Load the value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002380 __ pop(r1); // r0 = value. r1 = object.
2381
2382 Label done;
2383 // If the object is a smi, return the value.
2384 __ BranchOnSmi(r1, &done);
2385
2386 // If the object is not a value type, return the value.
2387 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
2388 __ b(ne, &done);
2389
2390 // Store the value.
2391 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2392 // Update the write barrier. Save the value as it will be
2393 // overwritten by the write barrier code and is needed afterward.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002394 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002395
2396 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002397 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002398}
2399
2400
2401void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2402 ASSERT_EQ(args->length(), 1);
2403
2404 // Load the argument on the stack and call the stub.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002405 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002406
2407 NumberToStringStub stub;
2408 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002409 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002410}
2411
2412
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002413void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002414 ASSERT(args->length() == 1);
2415
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002416 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002417
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002418 Label done;
2419 StringCharFromCodeGenerator generator(r0, r1);
2420 generator.GenerateFast(masm_);
2421 __ jmp(&done);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002422
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002423 NopRuntimeCallHelper call_helper;
2424 generator.GenerateSlow(masm_, call_helper);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002425
2426 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002427 context()->Plug(r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002428}
2429
2430
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002431void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
2432 ASSERT(args->length() == 2);
2433
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002434 VisitForStackValue(args->at(0));
2435 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002436
2437 Register object = r1;
2438 Register index = r0;
2439 Register scratch = r2;
2440 Register result = r3;
2441
2442 __ pop(object);
2443
2444 Label need_conversion;
2445 Label index_out_of_range;
2446 Label done;
2447 StringCharCodeAtGenerator generator(object,
2448 index,
2449 scratch,
2450 result,
2451 &need_conversion,
2452 &need_conversion,
2453 &index_out_of_range,
2454 STRING_INDEX_IS_NUMBER);
2455 generator.GenerateFast(masm_);
2456 __ jmp(&done);
2457
2458 __ bind(&index_out_of_range);
2459 // When the index is out of range, the spec requires us to return
2460 // NaN.
2461 __ LoadRoot(result, Heap::kNanValueRootIndex);
2462 __ jmp(&done);
2463
2464 __ bind(&need_conversion);
2465 // Load the undefined value into the result register, which will
2466 // trigger conversion.
2467 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2468 __ jmp(&done);
2469
2470 NopRuntimeCallHelper call_helper;
2471 generator.GenerateSlow(masm_, call_helper);
2472
2473 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002474 context()->Plug(result);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002475}
2476
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002477
2478void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
2479 ASSERT(args->length() == 2);
2480
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002481 VisitForStackValue(args->at(0));
2482 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002483
2484 Register object = r1;
2485 Register index = r0;
2486 Register scratch1 = r2;
2487 Register scratch2 = r3;
2488 Register result = r0;
2489
2490 __ pop(object);
2491
2492 Label need_conversion;
2493 Label index_out_of_range;
2494 Label done;
2495 StringCharAtGenerator generator(object,
2496 index,
2497 scratch1,
2498 scratch2,
2499 result,
2500 &need_conversion,
2501 &need_conversion,
2502 &index_out_of_range,
2503 STRING_INDEX_IS_NUMBER);
2504 generator.GenerateFast(masm_);
2505 __ jmp(&done);
2506
2507 __ bind(&index_out_of_range);
2508 // When the index is out of range, the spec requires us to return
2509 // the empty string.
2510 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2511 __ jmp(&done);
2512
2513 __ bind(&need_conversion);
2514 // Move smi zero into the result register, which will trigger
2515 // conversion.
2516 __ mov(result, Operand(Smi::FromInt(0)));
2517 __ jmp(&done);
2518
2519 NopRuntimeCallHelper call_helper;
2520 generator.GenerateSlow(masm_, call_helper);
2521
2522 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002523 context()->Plug(result);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002524}
2525
2526
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002527void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2528 ASSERT_EQ(2, args->length());
2529
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002530 VisitForStackValue(args->at(0));
2531 VisitForStackValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002532
2533 StringAddStub stub(NO_STRING_ADD_FLAGS);
2534 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002535 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002536}
2537
2538
2539void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
2540 ASSERT_EQ(2, args->length());
2541
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002542 VisitForStackValue(args->at(0));
2543 VisitForStackValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002544
2545 StringCompareStub stub;
2546 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002547 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002548}
2549
2550
2551void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
2552 // Load the argument on the stack and call the runtime.
2553 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002554 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002555 __ CallRuntime(Runtime::kMath_sin, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002556 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002557}
2558
2559
2560void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
2561 // Load the argument on the stack and call the runtime.
2562 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002563 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002564 __ CallRuntime(Runtime::kMath_cos, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002565 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002566}
2567
2568
2569void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2570 // Load the argument on the stack and call the runtime function.
2571 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002572 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002573 __ CallRuntime(Runtime::kMath_sqrt, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002574 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002575}
2576
2577
2578void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2579 ASSERT(args->length() >= 2);
2580
2581 int arg_count = args->length() - 2; // For receiver and function.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002582 VisitForStackValue(args->at(0)); // Receiver.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002583 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002584 VisitForStackValue(args->at(i + 1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002585 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002586 VisitForAccumulatorValue(args->at(arg_count + 1)); // Function.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002587
2588 // InvokeFunction requires function in r1. Move it in there.
2589 if (!result_register().is(r1)) __ mov(r1, result_register());
2590 ParameterCount count(arg_count);
2591 __ InvokeFunction(r1, count, CALL_FUNCTION);
2592 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002593 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002594}
2595
2596
2597void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2598 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002599 VisitForStackValue(args->at(0));
2600 VisitForStackValue(args->at(1));
2601 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002602 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002603 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002604}
2605
2606
2607void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2608 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002609 VisitForStackValue(args->at(0));
2610 VisitForStackValue(args->at(1));
2611 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002612 __ CallRuntime(Runtime::kSwapElements, 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002613 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002614}
2615
2616
2617void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2618 ASSERT_EQ(2, args->length());
2619
2620 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2621 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2622
2623 Handle<FixedArray> jsfunction_result_caches(
2624 Top::global_context()->jsfunction_result_caches());
2625 if (jsfunction_result_caches->length() <= cache_id) {
2626 __ Abort("Attempt to use undefined cache.");
2627 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002628 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002629 return;
2630 }
2631
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002632 VisitForAccumulatorValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002633
2634 Register key = r0;
2635 Register cache = r1;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002636 __ ldr(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002637 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002638 __ ldr(cache, ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002639 __ ldr(cache,
2640 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2641
2642
2643 Label done, not_found;
2644 // tmp now holds finger offset as a smi.
2645 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2646 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2647 // r2 now holds finger offset as a smi.
2648 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2649 // r3 now points to the start of fixed array elements.
2650 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2651 // Note side effect of PreIndex: r3 now points to the key of the pair.
2652 __ cmp(key, r2);
2653 __ b(ne, &not_found);
2654
2655 __ ldr(r0, MemOperand(r3, kPointerSize));
2656 __ b(&done);
2657
2658 __ bind(&not_found);
2659 // Call runtime to perform the lookup.
2660 __ Push(cache, key);
2661 __ CallRuntime(Runtime::kGetFromCache, 2);
2662
2663 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002664 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002665}
2666
2667
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002668void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
2669 ASSERT_EQ(2, args->length());
2670
2671 Register right = r0;
2672 Register left = r1;
2673 Register tmp = r2;
2674 Register tmp2 = r3;
2675
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002676 VisitForStackValue(args->at(0));
2677 VisitForAccumulatorValue(args->at(1));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002678 __ pop(left);
2679
2680 Label done, fail, ok;
2681 __ cmp(left, Operand(right));
2682 __ b(eq, &ok);
2683 // Fail if either is a non-HeapObject.
2684 __ and_(tmp, left, Operand(right));
2685 __ tst(tmp, Operand(kSmiTagMask));
2686 __ b(eq, &fail);
2687 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
2688 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
2689 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
2690 __ b(ne, &fail);
2691 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
2692 __ cmp(tmp, Operand(tmp2));
2693 __ b(ne, &fail);
2694 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
2695 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
2696 __ cmp(tmp, tmp2);
2697 __ b(eq, &ok);
2698 __ bind(&fail);
2699 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
2700 __ jmp(&done);
2701 __ bind(&ok);
2702 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
2703 __ bind(&done);
2704
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002705 context()->Plug(r0);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002706}
2707
2708
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002709void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002710 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002711
2712 Label materialize_true, materialize_false;
2713 Label* if_true = NULL;
2714 Label* if_false = NULL;
2715 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002716 context()->PrepareTest(&materialize_true, &materialize_false,
2717 &if_true, &if_false, &fall_through);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002718
2719 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2720 __ tst(r0, Operand(String::kContainsCachedArrayIndexMask));
2721
2722 __ b(eq, if_true);
2723 __ b(if_false);
2724
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002725 context()->Plug(if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002726}
2727
2728
2729void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
2730 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002731 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002732 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2733 __ IndexFromHash(r0, r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002734 context()->Plug(r0);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002735}
2736
2737
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002738void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002739 Handle<String> name = expr->name();
2740 if (name->length() > 0 && name->Get(0) == '_') {
2741 Comment cmnt(masm_, "[ InlineRuntimeCall");
2742 EmitInlineRuntimeCall(expr);
2743 return;
2744 }
2745
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002746 Comment cmnt(masm_, "[ CallRuntime");
2747 ZoneList<Expression*>* args = expr->arguments();
2748
2749 if (expr->is_jsruntime()) {
2750 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002751 __ ldr(r0, CodeGenerator::GlobalObject());
2752 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002753 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002754 }
2755
2756 // Push the arguments ("left-to-right").
2757 int arg_count = args->length();
2758 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002759 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002760 }
2761
2762 if (expr->is_jsruntime()) {
2763 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002764 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002765 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2766 NOT_IN_LOOP);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002767 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002768 // Restore context register.
2769 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002770 } else {
2771 // Call the C runtime function.
2772 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002773 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002774 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002775}
2776
2777
2778void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2779 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002780 case Token::DELETE: {
2781 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2782 Property* prop = expr->expression()->AsProperty();
2783 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2784 if (prop == NULL && var == NULL) {
2785 // Result of deleting non-property, non-variable reference is true.
2786 // The subexpression may have side effects.
2787 VisitForEffect(expr->expression());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002788 context()->Plug(true);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002789 } else if (var != NULL &&
2790 !var->is_global() &&
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002791 var->AsSlot() != NULL &&
2792 var->AsSlot()->type() != Slot::LOOKUP) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002793 // Result of deleting non-global, non-dynamic variables is false.
2794 // The subexpression does not have side effects.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002795 context()->Plug(false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002796 } else {
2797 // Property or variable reference. Call the delete builtin with
2798 // object and property name as arguments.
2799 if (prop != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002800 VisitForStackValue(prop->obj());
2801 VisitForStackValue(prop->key());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002802 } else if (var->is_global()) {
2803 __ ldr(r1, CodeGenerator::GlobalObject());
2804 __ mov(r0, Operand(var->name()));
2805 __ Push(r1, r0);
2806 } else {
2807 // Non-global variable. Call the runtime to look up the context
2808 // where the variable was introduced.
2809 __ push(context_register());
2810 __ mov(r2, Operand(var->name()));
2811 __ push(r2);
2812 __ CallRuntime(Runtime::kLookupContext, 2);
2813 __ push(r0);
2814 __ mov(r2, Operand(var->name()));
2815 __ push(r2);
2816 }
2817 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002818 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002819 }
2820 break;
2821 }
2822
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002823 case Token::VOID: {
2824 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2825 VisitForEffect(expr->expression());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002826 context()->Plug(Heap::kUndefinedValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002827 break;
2828 }
2829
2830 case Token::NOT: {
2831 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002832 Label materialize_true, materialize_false;
2833 Label* if_true = NULL;
2834 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002835 Label* fall_through = NULL;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002836
2837 // Notice that the labels are swapped.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002838 context()->PrepareTest(&materialize_true, &materialize_false,
2839 &if_false, &if_true, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002840 VisitForControl(expr->expression(), if_true, if_false, fall_through);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002841 context()->Plug(if_false, if_true); // Labels swapped.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002842 break;
2843 }
2844
2845 case Token::TYPEOF: {
2846 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002847 { StackValueContext context(this);
2848 VisitForTypeofValue(expr->expression());
2849 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002850 __ CallRuntime(Runtime::kTypeof, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002851 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002852 break;
2853 }
2854
2855 case Token::ADD: {
2856 Comment cmt(masm_, "[ UnaryOperation (ADD)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002857 VisitForAccumulatorValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002858 Label no_conversion;
2859 __ tst(result_register(), Operand(kSmiTagMask));
2860 __ b(eq, &no_conversion);
2861 __ push(r0);
2862 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2863 __ bind(&no_conversion);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002864 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002865 break;
2866 }
2867
2868 case Token::SUB: {
2869 Comment cmt(masm_, "[ UnaryOperation (SUB)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002870 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002871 UnaryOverwriteMode overwrite =
2872 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002873 GenericUnaryOpStub stub(Token::SUB,
2874 overwrite,
2875 NO_UNARY_FLAGS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002876 // GenericUnaryOpStub expects the argument to be in the
2877 // accumulator register r0.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002878 VisitForAccumulatorValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002879 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002880 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002881 break;
2882 }
2883
2884 case Token::BIT_NOT: {
2885 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002886 // The generic unary operation stub expects the argument to be
2887 // in the accumulator register r0.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002888 VisitForAccumulatorValue(expr->expression());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002889 Label done;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002890 bool inline_smi_code = ShouldInlineSmiCase(expr->op());
2891 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002892 Label call_stub;
2893 __ BranchOnNotSmi(r0, &call_stub);
2894 __ mvn(r0, Operand(r0));
2895 // Bit-clear inverted smi-tag.
2896 __ bic(r0, r0, Operand(kSmiTagMask));
2897 __ b(&done);
2898 __ bind(&call_stub);
2899 }
2900 bool overwrite = expr->expression()->ResultOverwriteAllowed();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002901 UnaryOpFlags flags = inline_smi_code
2902 ? NO_UNARY_SMI_CODE_IN_STUB
2903 : NO_UNARY_FLAGS;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002904 UnaryOverwriteMode mode =
2905 overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002906 GenericUnaryOpStub stub(Token::BIT_NOT, mode, flags);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002907 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002908 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002909 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002910 break;
2911 }
2912
2913 default:
2914 UNREACHABLE();
2915 }
2916}
2917
2918
2919void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2920 Comment cmnt(masm_, "[ CountOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002921 SetSourcePosition(expr->position());
2922
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002923 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2924 // as the left-hand side.
2925 if (!expr->expression()->IsValidLeftHandSide()) {
2926 VisitForEffect(expr->expression());
2927 return;
2928 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002929
2930 // Expression can only be a property, a global or a (parameter or local)
2931 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2932 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2933 LhsKind assign_type = VARIABLE;
2934 Property* prop = expr->expression()->AsProperty();
2935 // In case of a property we use the uninitialized expression context
2936 // of the key to detect a named property.
2937 if (prop != NULL) {
2938 assign_type =
2939 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2940 }
2941
2942 // Evaluate expression and get value.
2943 if (assign_type == VARIABLE) {
2944 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002945 AccumulatorValueContext context(this);
2946 EmitVariableLoad(expr->expression()->AsVariableProxy()->var());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002947 } else {
2948 // Reserve space for result of postfix operation.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002949 if (expr->is_postfix() && !context()->IsEffect()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002950 __ mov(ip, Operand(Smi::FromInt(0)));
2951 __ push(ip);
2952 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002953 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002954 // Put the object both on the stack and in the accumulator.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002955 VisitForAccumulatorValue(prop->obj());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002956 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002957 EmitNamedPropertyLoad(prop);
2958 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002959 VisitForStackValue(prop->obj());
2960 VisitForAccumulatorValue(prop->key());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002961 __ ldr(r1, MemOperand(sp, 0));
2962 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002963 EmitKeyedPropertyLoad(prop);
2964 }
2965 }
2966
2967 // Call ToNumber only if operand is not a smi.
2968 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002969 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002970 __ push(r0);
2971 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2972 __ bind(&no_conversion);
2973
2974 // Save result for postfix expressions.
2975 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002976 if (!context()->IsEffect()) {
2977 // Save the result on the stack. If we have a named or keyed property
2978 // we store the result under the receiver that is currently on top
2979 // of the stack.
2980 switch (assign_type) {
2981 case VARIABLE:
2982 __ push(r0);
2983 break;
2984 case NAMED_PROPERTY:
2985 __ str(r0, MemOperand(sp, kPointerSize));
2986 break;
2987 case KEYED_PROPERTY:
2988 __ str(r0, MemOperand(sp, 2 * kPointerSize));
2989 break;
2990 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002991 }
2992 }
2993
2994
2995 // Inline smi case if we are in a loop.
2996 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002997 int count_value = expr->op() == Token::INC ? 1 : -1;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002998 if (ShouldInlineSmiCase(expr->op())) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002999 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003000 __ b(vs, &stub_call);
3001 // We could eliminate this smi check if we split the code at
3002 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003003 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003004 __ bind(&stub_call);
3005 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003006 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003007 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003008 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00003009 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003010 __ CallStub(&stub);
3011 __ bind(&done);
3012
3013 // Store the value returned in r0.
3014 switch (assign_type) {
3015 case VARIABLE:
3016 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003017 { EffectContext context(this);
3018 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3019 Token::ASSIGN);
3020 }
3021 // For all contexts except EffectConstant We have the result on
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003022 // top of the stack.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003023 if (!context()->IsEffect()) {
3024 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003025 }
3026 } else {
3027 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003028 Token::ASSIGN);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003029 }
3030 break;
3031 case NAMED_PROPERTY: {
3032 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003033 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003034 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003035 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003036 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003037 if (!context()->IsEffect()) {
3038 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003039 }
3040 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003041 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003042 }
3043 break;
3044 }
3045 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003046 __ pop(r1); // Key.
3047 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003048 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003049 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003050 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003051 if (!context()->IsEffect()) {
3052 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003053 }
3054 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003055 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003056 }
3057 break;
3058 }
3059 }
3060}
3061
3062
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003063void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
3064 ASSERT(!context()->IsEffect());
3065 ASSERT(!context()->IsTest());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003066 VariableProxy* proxy = expr->AsVariableProxy();
3067 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
3068 Comment cmnt(masm_, "Global variable");
3069 __ ldr(r0, CodeGenerator::GlobalObject());
3070 __ mov(r2, Operand(proxy->name()));
3071 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
3072 // Use a regular load, not a contextual load, to avoid a reference
3073 // error.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003074 EmitCallIC(ic, RelocInfo::CODE_TARGET);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003075 context()->Plug(r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003076 } else if (proxy != NULL &&
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003077 proxy->var()->AsSlot() != NULL &&
3078 proxy->var()->AsSlot()->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003079 Label done, slow;
3080
3081 // Generate code for loading from variables potentially shadowed
3082 // by eval-introduced variables.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003083 Slot* slot = proxy->var()->AsSlot();
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003084 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
3085
3086 __ bind(&slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003087 __ mov(r0, Operand(proxy->name()));
3088 __ Push(cp, r0);
3089 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003090 __ bind(&done);
3091
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003092 context()->Plug(r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003093 } else {
3094 // This expression cannot throw a reference error at the top level.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003095 Visit(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003096 }
3097}
3098
3099
ricow@chromium.org65fae842010-08-25 15:26:24 +00003100bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
3101 Expression* left,
3102 Expression* right,
3103 Label* if_true,
3104 Label* if_false,
3105 Label* fall_through) {
3106 if (op != Token::EQ && op != Token::EQ_STRICT) return false;
3107
3108 // Check for the pattern: typeof <expression> == <string literal>.
3109 Literal* right_literal = right->AsLiteral();
3110 if (right_literal == NULL) return false;
3111 Handle<Object> right_literal_value = right_literal->handle();
3112 if (!right_literal_value->IsString()) return false;
3113 UnaryOperation* left_unary = left->AsUnaryOperation();
3114 if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
3115 Handle<String> check = Handle<String>::cast(right_literal_value);
3116
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003117 { AccumulatorValueContext context(this);
3118 VisitForTypeofValue(left_unary->expression());
3119 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003120 if (check->Equals(Heap::number_symbol())) {
3121 __ tst(r0, Operand(kSmiTagMask));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003122 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003123 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3124 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
3125 __ cmp(r0, ip);
3126 Split(eq, if_true, if_false, fall_through);
3127 } else if (check->Equals(Heap::string_symbol())) {
3128 __ tst(r0, Operand(kSmiTagMask));
3129 __ b(eq, if_false);
3130 // Check for undetectable objects => false.
3131 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3132 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
3133 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3134 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3135 __ b(eq, if_false);
3136 __ ldrb(r1, FieldMemOperand(r0, Map::kInstanceTypeOffset));
3137 __ cmp(r1, Operand(FIRST_NONSTRING_TYPE));
3138 Split(lt, if_true, if_false, fall_through);
3139 } else if (check->Equals(Heap::boolean_symbol())) {
3140 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3141 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003142 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003143 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
3144 __ cmp(r0, ip);
3145 Split(eq, if_true, if_false, fall_through);
3146 } else if (check->Equals(Heap::undefined_symbol())) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003147 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003148 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003149 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003150 __ tst(r0, Operand(kSmiTagMask));
3151 __ b(eq, if_false);
3152 // Check for undetectable objects => true.
3153 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3154 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
3155 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3156 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3157 Split(eq, if_true, if_false, fall_through);
3158 } else if (check->Equals(Heap::function_symbol())) {
3159 __ tst(r0, Operand(kSmiTagMask));
3160 __ b(eq, if_false);
3161 __ CompareObjectType(r0, r1, r0, JS_FUNCTION_TYPE);
3162 __ b(eq, if_true);
3163 // Regular expressions => 'function' (they are callable).
3164 __ CompareInstanceType(r1, r0, JS_REGEXP_TYPE);
3165 Split(eq, if_true, if_false, fall_through);
3166 } else if (check->Equals(Heap::object_symbol())) {
3167 __ tst(r0, Operand(kSmiTagMask));
3168 __ b(eq, if_false);
3169 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3170 __ cmp(r0, ip);
3171 __ b(eq, if_true);
3172 // Regular expressions => 'function', not 'object'.
3173 __ CompareObjectType(r0, r1, r0, JS_REGEXP_TYPE);
3174 __ b(eq, if_false);
3175 // Check for undetectable objects => false.
3176 __ ldrb(r0, FieldMemOperand(r1, Map::kBitFieldOffset));
3177 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3178 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
3179 __ b(eq, if_false);
3180 // Check for JS objects => true.
3181 __ ldrb(r0, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3182 __ cmp(r0, Operand(FIRST_JS_OBJECT_TYPE));
3183 __ b(lt, if_false);
3184 __ cmp(r0, Operand(LAST_JS_OBJECT_TYPE));
3185 Split(le, if_true, if_false, fall_through);
3186 } else {
3187 if (if_false != fall_through) __ jmp(if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003188 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003189
3190 return true;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003191}
3192
3193
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003194void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
3195 Comment cmnt(masm_, "[ CompareOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00003196 SetSourcePosition(expr->position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003197
3198 // Always perform the comparison for its control flow. Pack the result
3199 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003200
3201 Label materialize_true, materialize_false;
3202 Label* if_true = NULL;
3203 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003204 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003205 context()->PrepareTest(&materialize_true, &materialize_false,
3206 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003207
3208 // First we try a fast inlined version of the compare when one of
3209 // the operands is a literal.
3210 Token::Value op = expr->op();
3211 Expression* left = expr->left();
3212 Expression* right = expr->right();
3213 if (TryLiteralCompare(op, left, right, if_true, if_false, fall_through)) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003214 context()->Plug(if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003215 return;
3216 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003217
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003218 VisitForStackValue(expr->left());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003219 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003220 case Token::IN:
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003221 VisitForStackValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003222 __ InvokeBuiltin(Builtins::IN, CALL_JS);
3223 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3224 __ cmp(r0, ip);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003225 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003226 break;
3227
3228 case Token::INSTANCEOF: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003229 VisitForStackValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003230 InstanceofStub stub;
3231 __ CallStub(&stub);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003232 // The stub returns 0 for true.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003233 __ tst(r0, r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003234 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003235 break;
3236 }
3237
3238 default: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003239 VisitForAccumulatorValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003240 Condition cc = eq;
3241 bool strict = false;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003242 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003243 case Token::EQ_STRICT:
3244 strict = true;
3245 // Fall through
ricow@chromium.org65fae842010-08-25 15:26:24 +00003246 case Token::EQ:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003247 cc = eq;
3248 __ pop(r1);
3249 break;
3250 case Token::LT:
3251 cc = lt;
3252 __ pop(r1);
3253 break;
3254 case Token::GT:
3255 // Reverse left and right sides to obtain ECMA-262 conversion order.
3256 cc = lt;
3257 __ mov(r1, result_register());
3258 __ pop(r0);
3259 break;
3260 case Token::LTE:
3261 // Reverse left and right sides to obtain ECMA-262 conversion order.
3262 cc = ge;
3263 __ mov(r1, result_register());
3264 __ pop(r0);
3265 break;
3266 case Token::GTE:
3267 cc = ge;
3268 __ pop(r1);
3269 break;
3270 case Token::IN:
3271 case Token::INSTANCEOF:
3272 default:
3273 UNREACHABLE();
3274 }
3275
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003276 bool inline_smi_code = ShouldInlineSmiCase(op);
3277 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003278 Label slow_case;
3279 __ orr(r2, r0, Operand(r1));
3280 __ BranchOnNotSmi(r2, &slow_case);
3281 __ cmp(r1, r0);
3282 Split(cc, if_true, if_false, NULL);
3283 __ bind(&slow_case);
3284 }
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003285 CompareFlags flags = inline_smi_code
3286 ? NO_SMI_COMPARE_IN_STUB
3287 : NO_COMPARE_FLAGS;
3288 CompareStub stub(cc, strict, flags, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003289 __ CallStub(&stub);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00003290 __ cmp(r0, Operand(0, RelocInfo::NONE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003291 Split(cc, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003292 }
3293 }
3294
3295 // Convert the result of the comparison into one expected for this
3296 // expression's context.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003297 context()->Plug(if_true, if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003298}
3299
3300
ricow@chromium.org65fae842010-08-25 15:26:24 +00003301void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
3302 Comment cmnt(masm_, "[ CompareToNull");
3303 Label materialize_true, materialize_false;
3304 Label* if_true = NULL;
3305 Label* if_false = NULL;
3306 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003307 context()->PrepareTest(&materialize_true, &materialize_false,
3308 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003309
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003310 VisitForAccumulatorValue(expr->expression());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003311 __ LoadRoot(r1, Heap::kNullValueRootIndex);
3312 __ cmp(r0, r1);
3313 if (expr->is_strict()) {
3314 Split(eq, if_true, if_false, fall_through);
3315 } else {
3316 __ b(eq, if_true);
3317 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
3318 __ cmp(r0, r1);
3319 __ b(eq, if_true);
3320 __ tst(r0, Operand(kSmiTagMask));
3321 __ b(eq, if_false);
3322 // It can be an undetectable object.
3323 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3324 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3325 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3326 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3327 Split(eq, if_true, if_false, fall_through);
3328 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003329 context()->Plug(if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003330}
3331
3332
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003333void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3334 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003335 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003336}
3337
3338
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003339Register FullCodeGenerator::result_register() {
3340 return r0;
3341}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003342
3343
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003344Register FullCodeGenerator::context_register() {
3345 return cp;
3346}
3347
3348
3349void FullCodeGenerator::EmitCallIC(Handle<Code> ic, RelocInfo::Mode mode) {
3350 ASSERT(mode == RelocInfo::CODE_TARGET ||
3351 mode == RelocInfo::CODE_TARGET_CONTEXT);
3352 __ Call(ic, mode);
3353}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003354
3355
3356void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3357 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3358 __ str(value, MemOperand(fp, frame_offset));
3359}
3360
3361
3362void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003363 __ ldr(dst, ContextOperand(cp, context_index));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003364}
3365
3366
3367// ----------------------------------------------------------------------------
3368// Non-local control flow support.
3369
3370void FullCodeGenerator::EnterFinallyBlock() {
3371 ASSERT(!result_register().is(r1));
3372 // Store result register while executing finally block.
3373 __ push(result_register());
3374 // Cook return address in link register to stack (smi encoded Code* delta)
3375 __ sub(r1, lr, Operand(masm_->CodeObject()));
3376 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3377 ASSERT_EQ(0, kSmiTag);
3378 __ add(r1, r1, Operand(r1)); // Convert to smi.
3379 __ push(r1);
3380}
3381
3382
3383void FullCodeGenerator::ExitFinallyBlock() {
3384 ASSERT(!result_register().is(r1));
3385 // Restore result register from stack.
3386 __ pop(r1);
3387 // Uncook return address and return.
3388 __ pop(result_register());
3389 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3390 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3391 __ add(pc, r1, Operand(masm_->CodeObject()));
3392}
3393
3394
3395#undef __
3396
3397} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003398
3399#endif // V8_TARGET_ARCH_ARM