blob: 5fa412ffc955782df60a659b8a4f167eac48d6b5 [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
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000032#include "codegen-inl.h"
33#include "compiler.h"
34#include "debug.h"
35#include "full-codegen.h"
36#include "parser.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000037#include "scopes.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000038
39namespace v8 {
40namespace internal {
41
42#define __ ACCESS_MASM(masm_)
43
44// Generate code for a JS function. On entry to the function the receiver
45// and arguments have been pushed on the stack left to right. The actual
46// argument count matches the formal parameter count expected by the
47// function.
48//
49// The live registers are:
50// o r1: the JS function object being called (ie, ourselves)
51// o cp: our context
52// o fp: our caller's frame pointer
53// o sp: stack pointer
54// o lr: return address
55//
56// The function builds a JS frame. Please see JavaScriptFrameConstants in
57// frames-arm.h for its layout.
ager@chromium.org5c838252010-02-19 08:53:10 +000058void FullCodeGenerator::Generate(CompilationInfo* info, Mode mode) {
59 ASSERT(info_ == NULL);
60 info_ = info;
61 SetFunctionPosition(function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000062 Comment cmnt(masm_, "[ function compiled by full code generator");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000063
64 if (mode == PRIMARY) {
ager@chromium.org5c838252010-02-19 08:53:10 +000065 int locals_count = scope()->num_stack_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000066
kmillikin@chromium.org9155e252010-05-26 13:27:57 +000067 __ Push(lr, fp, cp, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000068 if (locals_count > 0) {
69 // Load undefined value here, so the value is ready for the loop
70 // below.
71 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
72 }
73 // Adjust fp to point to caller's fp.
74 __ add(fp, sp, Operand(2 * kPointerSize));
75
76 { Comment cmnt(masm_, "[ Allocate locals");
77 for (int i = 0; i < locals_count; i++) {
78 __ push(ip);
79 }
80 }
81
82 bool function_in_register = true;
83
84 // Possibly allocate a local context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +000085 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
86 if (heap_slots > 0) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000087 Comment cmnt(masm_, "[ Allocate local context");
88 // Argument to NewContext is the function, which is in r1.
89 __ push(r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +000090 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
91 FastNewContextStub stub(heap_slots);
92 __ CallStub(&stub);
93 } else {
94 __ CallRuntime(Runtime::kNewContext, 1);
95 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000096 function_in_register = false;
97 // Context is returned in both r0 and cp. It replaces the context
98 // passed to us. It's saved in the stack and kept live in cp.
99 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
100 // Copy any necessary parameters into the context.
ager@chromium.org5c838252010-02-19 08:53:10 +0000101 int num_parameters = scope()->num_parameters();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000102 for (int i = 0; i < num_parameters; i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000103 Slot* slot = scope()->parameter(i)->slot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000104 if (slot != NULL && slot->type() == Slot::CONTEXT) {
105 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
106 (num_parameters - 1 - i) * kPointerSize;
107 // Load parameter from stack.
108 __ ldr(r0, MemOperand(fp, parameter_offset));
109 // Store it in the context.
110 __ mov(r1, Operand(Context::SlotOffset(slot->index())));
111 __ str(r0, MemOperand(cp, r1));
112 // Update the write barrier. This clobbers all involved
113 // registers, so we have use a third register to avoid
114 // clobbering cp.
115 __ mov(r2, Operand(cp));
116 __ RecordWrite(r2, r1, r0);
117 }
118 }
119 }
120
ager@chromium.org5c838252010-02-19 08:53:10 +0000121 Variable* arguments = scope()->arguments()->AsVariable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000122 if (arguments != NULL) {
123 // Function uses arguments object.
124 Comment cmnt(masm_, "[ Allocate arguments object");
125 if (!function_in_register) {
126 // Load this again, if it's used by the local context below.
127 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
128 } else {
129 __ mov(r3, r1);
130 }
131 // Receiver is just before the parameters on the caller's stack.
ager@chromium.org5c838252010-02-19 08:53:10 +0000132 int offset = scope()->num_parameters() * kPointerSize;
133 __ add(r2, fp,
134 Operand(StandardFrameConstants::kCallerSPOffset + offset));
135 __ mov(r1, Operand(Smi::FromInt(scope()->num_parameters())));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000136 __ Push(r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000137
138 // Arguments to ArgumentsAccessStub:
139 // function, receiver address, parameter count.
140 // The stub will rewrite receiever and parameter count if the previous
141 // stack frame was an arguments adapter frame.
142 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
143 __ CallStub(&stub);
144 // Duplicate the value; move-to-slot operation might clobber registers.
145 __ mov(r3, r0);
146 Move(arguments->slot(), r0, r1, r2);
147 Slot* dot_arguments_slot =
ager@chromium.org5c838252010-02-19 08:53:10 +0000148 scope()->arguments_shadow()->AsVariable()->slot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000149 Move(dot_arguments_slot, r3, r1, r2);
150 }
151 }
152
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000153 { Comment cmnt(masm_, "[ Declarations");
154 // For named function expressions, declare the function name as a
155 // constant.
156 if (scope()->is_function_scope() && scope()->function() != NULL) {
157 EmitDeclaration(scope()->function(), Variable::CONST, NULL);
158 }
159 // Visit all the explicit declarations unless there is an illegal
160 // redeclaration.
161 if (scope()->HasIllegalRedeclaration()) {
162 scope()->VisitIllegalRedeclaration(this);
163 } else {
164 VisitDeclarations(scope()->declarations());
165 }
166 }
167
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000168 // Check the stack for overflow or break request.
169 // Put the lr setup instruction in the delay slot. The kInstrSize is
170 // added to the implicit 8 byte offset that always applies to operations
171 // with pc and gives a return address 12 bytes down.
172 { Comment cmnt(masm_, "[ Stack check");
173 __ LoadRoot(r2, Heap::kStackLimitRootIndex);
174 __ add(lr, pc, Operand(Assembler::kInstrSize));
175 __ cmp(sp, Operand(r2));
176 StackCheckStub stub;
177 __ mov(pc,
178 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
179 RelocInfo::CODE_TARGET),
180 LeaveCC,
181 lo);
182 }
183
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000184 if (FLAG_trace) {
185 __ CallRuntime(Runtime::kTraceEnter, 0);
186 }
187
188 { Comment cmnt(masm_, "[ Body");
189 ASSERT(loop_depth() == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000190 VisitStatements(function()->body());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000191 ASSERT(loop_depth() == 0);
192 }
193
194 { Comment cmnt(masm_, "[ return <undefined>;");
195 // Emit a 'return undefined' in case control fell off the end of the
196 // body.
197 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
198 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000199 EmitReturnSequence(function()->end_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000200}
201
202
203void FullCodeGenerator::EmitReturnSequence(int position) {
204 Comment cmnt(masm_, "[ Return sequence");
205 if (return_label_.is_bound()) {
206 __ b(&return_label_);
207 } else {
208 __ bind(&return_label_);
209 if (FLAG_trace) {
210 // Push the return value on the stack as the parameter.
211 // Runtime::TraceExit returns its parameter in r0.
212 __ push(r0);
213 __ CallRuntime(Runtime::kTraceExit, 1);
214 }
215
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000216#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000217 // Add a label for checking the size of the code used for returning.
218 Label check_exit_codesize;
219 masm_->bind(&check_exit_codesize);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000220#endif
221 // Make sure that the constant pool is not emitted inside of the return
222 // sequence.
223 { Assembler::BlockConstPoolScope block_const_pool(masm_);
224 // Here we use masm_-> instead of the __ macro to avoid the code coverage
225 // tool from instrumenting as we rely on the code size here.
226 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
227 CodeGenerator::RecordPositions(masm_, position);
228 __ RecordJSReturn();
229 masm_->mov(sp, fp);
230 masm_->ldm(ia_w, sp, fp.bit() | lr.bit());
231 masm_->add(sp, sp, Operand(sp_delta));
232 masm_->Jump(lr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000233 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000234
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000235#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000236 // Check that the size of the code used for returning matches what is
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000237 // expected by the debugger. If the sp_delts above cannot be encoded in the
238 // add instruction the add will generate two instructions.
239 int return_sequence_length =
240 masm_->InstructionsGeneratedSince(&check_exit_codesize);
241 CHECK(return_sequence_length == Assembler::kJSReturnSequenceLength ||
242 return_sequence_length == Assembler::kJSReturnSequenceLength + 1);
243#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000244 }
245}
246
247
248void FullCodeGenerator::Apply(Expression::Context context, Register reg) {
249 switch (context) {
250 case Expression::kUninitialized:
251 UNREACHABLE();
252
253 case Expression::kEffect:
254 // Nothing to do.
255 break;
256
257 case Expression::kValue:
258 // Move value into place.
259 switch (location_) {
260 case kAccumulator:
261 if (!reg.is(result_register())) __ mov(result_register(), reg);
262 break;
263 case kStack:
264 __ push(reg);
265 break;
266 }
267 break;
268
269 case Expression::kValueTest:
270 case Expression::kTestValue:
271 // Push an extra copy of the value in case it's needed.
272 __ push(reg);
273 // Fall through.
274
275 case Expression::kTest:
276 // We always call the runtime on ARM, so push the value as argument.
277 __ push(reg);
278 DoTest(context);
279 break;
280 }
281}
282
283
284void FullCodeGenerator::Apply(Expression::Context context, Slot* slot) {
285 switch (context) {
286 case Expression::kUninitialized:
287 UNREACHABLE();
288 case Expression::kEffect:
289 // Nothing to do.
290 break;
291 case Expression::kValue:
292 case Expression::kTest:
293 case Expression::kValueTest:
294 case Expression::kTestValue:
295 // On ARM we have to move the value into a register to do anything
296 // with it.
297 Move(result_register(), slot);
298 Apply(context, result_register());
299 break;
300 }
301}
302
303
304void FullCodeGenerator::Apply(Expression::Context context, Literal* lit) {
305 switch (context) {
306 case Expression::kUninitialized:
307 UNREACHABLE();
308 case Expression::kEffect:
309 break;
310 // Nothing to do.
311 case Expression::kValue:
312 case Expression::kTest:
313 case Expression::kValueTest:
314 case Expression::kTestValue:
315 // On ARM we have to move the value into a register to do anything
316 // with it.
317 __ mov(result_register(), Operand(lit->handle()));
318 Apply(context, result_register());
319 break;
320 }
321}
322
323
324void FullCodeGenerator::ApplyTOS(Expression::Context context) {
325 switch (context) {
326 case Expression::kUninitialized:
327 UNREACHABLE();
328
329 case Expression::kEffect:
330 __ Drop(1);
331 break;
332
333 case Expression::kValue:
334 switch (location_) {
335 case kAccumulator:
336 __ pop(result_register());
337 break;
338 case kStack:
339 break;
340 }
341 break;
342
343 case Expression::kValueTest:
344 case Expression::kTestValue:
345 // Duplicate the value on the stack in case it's needed.
346 __ ldr(ip, MemOperand(sp));
347 __ push(ip);
348 // Fall through.
349
350 case Expression::kTest:
351 DoTest(context);
352 break;
353 }
354}
355
356
357void FullCodeGenerator::DropAndApply(int count,
358 Expression::Context context,
359 Register reg) {
360 ASSERT(count > 0);
361 ASSERT(!reg.is(sp));
362 switch (context) {
363 case Expression::kUninitialized:
364 UNREACHABLE();
365
366 case Expression::kEffect:
367 __ Drop(count);
368 break;
369
370 case Expression::kValue:
371 switch (location_) {
372 case kAccumulator:
373 __ Drop(count);
374 if (!reg.is(result_register())) __ mov(result_register(), reg);
375 break;
376 case kStack:
377 if (count > 1) __ Drop(count - 1);
378 __ str(reg, MemOperand(sp));
379 break;
380 }
381 break;
382
383 case Expression::kTest:
384 if (count > 1) __ Drop(count - 1);
385 __ str(reg, MemOperand(sp));
386 DoTest(context);
387 break;
388
389 case Expression::kValueTest:
390 case Expression::kTestValue:
391 if (count == 1) {
392 __ str(reg, MemOperand(sp));
393 __ push(reg);
394 } else { // count > 1
395 __ Drop(count - 2);
396 __ str(reg, MemOperand(sp, kPointerSize));
397 __ str(reg, MemOperand(sp));
398 }
399 DoTest(context);
400 break;
401 }
402}
403
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000404void FullCodeGenerator::PrepareTest(Label* materialize_true,
405 Label* materialize_false,
406 Label** if_true,
407 Label** if_false) {
408 switch (context_) {
409 case Expression::kUninitialized:
410 UNREACHABLE();
411 break;
412 case Expression::kEffect:
413 // In an effect context, the true and the false case branch to the
414 // same label.
415 *if_true = *if_false = materialize_true;
416 break;
417 case Expression::kValue:
418 *if_true = materialize_true;
419 *if_false = materialize_false;
420 break;
421 case Expression::kTest:
422 *if_true = true_label_;
423 *if_false = false_label_;
424 break;
425 case Expression::kValueTest:
426 *if_true = materialize_true;
427 *if_false = false_label_;
428 break;
429 case Expression::kTestValue:
430 *if_true = true_label_;
431 *if_false = materialize_false;
432 break;
433 }
434}
435
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000436
437void FullCodeGenerator::Apply(Expression::Context context,
438 Label* materialize_true,
439 Label* materialize_false) {
440 switch (context) {
441 case Expression::kUninitialized:
442
443 case Expression::kEffect:
444 ASSERT_EQ(materialize_true, materialize_false);
445 __ bind(materialize_true);
446 break;
447
448 case Expression::kValue: {
449 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000450 switch (location_) {
451 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000452 __ bind(materialize_true);
453 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
454 __ jmp(&done);
455 __ bind(materialize_false);
456 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000457 break;
458 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000459 __ bind(materialize_true);
460 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
461 __ push(ip);
462 __ jmp(&done);
463 __ bind(materialize_false);
464 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
465 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000466 break;
467 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000468 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000469 break;
470 }
471
472 case Expression::kTest:
473 break;
474
475 case Expression::kValueTest:
476 __ bind(materialize_true);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000477 switch (location_) {
478 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000479 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000480 break;
481 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000482 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
483 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000484 break;
485 }
486 __ jmp(true_label_);
487 break;
488
489 case Expression::kTestValue:
490 __ bind(materialize_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000491 switch (location_) {
492 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000493 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000494 break;
495 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000496 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
497 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000498 break;
499 }
500 __ jmp(false_label_);
501 break;
502 }
503}
504
505
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000506// Convert constant control flow (true or false) to the result expected for
507// a given expression context.
508void FullCodeGenerator::Apply(Expression::Context context, bool flag) {
509 switch (context) {
510 case Expression::kUninitialized:
511 UNREACHABLE();
512 break;
513 case Expression::kEffect:
514 break;
515 case Expression::kValue: {
516 Heap::RootListIndex value_root_index =
517 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
518 switch (location_) {
519 case kAccumulator:
520 __ LoadRoot(result_register(), value_root_index);
521 break;
522 case kStack:
523 __ LoadRoot(ip, value_root_index);
524 __ push(ip);
525 break;
526 }
527 break;
528 }
529 case Expression::kTest:
530 __ b(flag ? true_label_ : false_label_);
531 break;
532 case Expression::kTestValue:
533 switch (location_) {
534 case kAccumulator:
535 // If value is false it's needed.
536 if (!flag) __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
537 break;
538 case kStack:
539 // If value is false it's needed.
540 if (!flag) {
541 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
542 __ push(ip);
543 }
544 break;
545 }
546 __ b(flag ? true_label_ : false_label_);
547 break;
548 case Expression::kValueTest:
549 switch (location_) {
550 case kAccumulator:
551 // If value is true it's needed.
552 if (flag) __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
553 break;
554 case kStack:
555 // If value is true it's needed.
556 if (flag) {
557 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
558 __ push(ip);
559 }
560 break;
561 }
562 __ b(flag ? true_label_ : false_label_);
563 break;
564 }
565}
566
567
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000568void FullCodeGenerator::DoTest(Expression::Context context) {
569 // The value to test is pushed on the stack, and duplicated on the stack
570 // if necessary (for value/test and test/value contexts).
571 ASSERT_NE(NULL, true_label_);
572 ASSERT_NE(NULL, false_label_);
573
574 // Call the runtime to find the boolean value of the source and then
575 // translate it into control flow to the pair of labels.
576 __ CallRuntime(Runtime::kToBool, 1);
577 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
578 __ cmp(r0, ip);
579
580 // Complete based on the context.
581 switch (context) {
582 case Expression::kUninitialized:
583 case Expression::kEffect:
584 case Expression::kValue:
585 UNREACHABLE();
586
587 case Expression::kTest:
588 __ b(eq, true_label_);
589 __ jmp(false_label_);
590 break;
591
592 case Expression::kValueTest: {
593 Label discard;
594 switch (location_) {
595 case kAccumulator:
596 __ b(ne, &discard);
597 __ pop(result_register());
598 __ jmp(true_label_);
599 break;
600 case kStack:
601 __ b(eq, true_label_);
602 break;
603 }
604 __ bind(&discard);
605 __ Drop(1);
606 __ jmp(false_label_);
607 break;
608 }
609
610 case Expression::kTestValue: {
611 Label discard;
612 switch (location_) {
613 case kAccumulator:
614 __ b(eq, &discard);
615 __ pop(result_register());
616 __ jmp(false_label_);
617 break;
618 case kStack:
619 __ b(ne, false_label_);
620 break;
621 }
622 __ bind(&discard);
623 __ Drop(1);
624 __ jmp(true_label_);
625 break;
626 }
627 }
628}
629
630
631MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
632 switch (slot->type()) {
633 case Slot::PARAMETER:
634 case Slot::LOCAL:
635 return MemOperand(fp, SlotOffset(slot));
636 case Slot::CONTEXT: {
637 int context_chain_length =
ager@chromium.org5c838252010-02-19 08:53:10 +0000638 scope()->ContextChainLength(slot->var()->scope());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000639 __ LoadContext(scratch, context_chain_length);
640 return CodeGenerator::ContextOperand(scratch, slot->index());
641 }
642 case Slot::LOOKUP:
643 UNREACHABLE();
644 }
645 UNREACHABLE();
646 return MemOperand(r0, 0);
647}
648
649
650void FullCodeGenerator::Move(Register destination, Slot* source) {
651 // Use destination as scratch.
652 MemOperand slot_operand = EmitSlotSearch(source, destination);
653 __ ldr(destination, slot_operand);
654}
655
656
657void FullCodeGenerator::Move(Slot* dst,
658 Register src,
659 Register scratch1,
660 Register scratch2) {
661 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
662 ASSERT(!scratch1.is(src) && !scratch2.is(src));
663 MemOperand location = EmitSlotSearch(dst, scratch1);
664 __ str(src, location);
665 // Emit the write barrier code if the location is in the heap.
666 if (dst->type() == Slot::CONTEXT) {
667 __ mov(scratch2, Operand(Context::SlotOffset(dst->index())));
668 __ RecordWrite(scratch1, scratch2, src);
669 }
670}
671
672
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000673void FullCodeGenerator::EmitDeclaration(Variable* variable,
674 Variable::Mode mode,
675 FunctionLiteral* function) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000676 Comment cmnt(masm_, "[ Declaration");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000677 ASSERT(variable != NULL); // Must have been resolved.
678 Slot* slot = variable->slot();
679 Property* prop = variable->AsProperty();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000680
681 if (slot != NULL) {
682 switch (slot->type()) {
683 case Slot::PARAMETER:
684 case Slot::LOCAL:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000685 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000686 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
687 __ str(ip, MemOperand(fp, SlotOffset(slot)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000688 } else if (function != NULL) {
689 VisitForValue(function, kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000690 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
691 }
692 break;
693
694 case Slot::CONTEXT:
695 // We bypass the general EmitSlotSearch because we know more about
696 // this specific context.
697
698 // The variable in the decl always resides in the current context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000699 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000700 if (FLAG_debug_code) {
701 // Check if we have the correct context pointer.
702 __ ldr(r1,
703 CodeGenerator::ContextOperand(cp, Context::FCONTEXT_INDEX));
704 __ cmp(r1, cp);
705 __ Check(eq, "Unexpected declaration in current context.");
706 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000707 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000708 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
709 __ str(ip, CodeGenerator::ContextOperand(cp, slot->index()));
710 // No write barrier since the_hole_value is in old space.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000711 } else if (function != NULL) {
712 VisitForValue(function, kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000713 __ str(result_register(),
714 CodeGenerator::ContextOperand(cp, slot->index()));
715 int offset = Context::SlotOffset(slot->index());
716 __ mov(r2, Operand(offset));
717 // We know that we have written a function, which is not a smi.
718 __ mov(r1, Operand(cp));
719 __ RecordWrite(r1, r2, result_register());
720 }
721 break;
722
723 case Slot::LOOKUP: {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000724 __ mov(r2, Operand(variable->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000725 // Declaration nodes are always introduced in one of two modes.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000726 ASSERT(mode == Variable::VAR ||
727 mode == Variable::CONST);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000728 PropertyAttributes attr =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000729 (mode == Variable::VAR) ? NONE : READ_ONLY;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000730 __ mov(r1, Operand(Smi::FromInt(attr)));
731 // Push initial value, if any.
732 // Note: For variables we must not push an initial value (such as
733 // 'undefined') because we may have a (legal) redeclaration and we
734 // must not destroy the current value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000735 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000736 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000737 __ Push(cp, r2, r1, r0);
738 } else if (function != NULL) {
739 __ Push(cp, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000740 // Push initial value for function declaration.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000741 VisitForValue(function, kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000742 } else {
743 __ mov(r0, Operand(Smi::FromInt(0))); // No initial value!
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000744 __ Push(cp, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000745 }
746 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
747 break;
748 }
749 }
750
751 } else if (prop != NULL) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000752 if (function != NULL || mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000753 // We are declaring a function or constant that rewrites to a
754 // property. Use (keyed) IC to set the initial value.
755 VisitForValue(prop->obj(), kStack);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000756 if (function != NULL) {
757 VisitForValue(prop->key(), kStack);
758 VisitForValue(function, kAccumulator);
759 __ pop(r1); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000760 } else {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000761 VisitForValue(prop->key(), kAccumulator);
762 __ mov(r1, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000763 __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex);
764 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000765 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000766
767 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
768 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000769 // Value in r0 is ignored (declarations are statements).
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000770 }
771 }
772}
773
774
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000775void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
776 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
777}
778
779
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000780void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
781 // Call the runtime to declare the globals.
782 // The context is the first argument.
783 __ mov(r1, Operand(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000784 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000785 __ Push(cp, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000786 __ CallRuntime(Runtime::kDeclareGlobals, 3);
787 // Return value is ignored.
788}
789
790
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000791void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000792 Comment cmnt(masm_, "[ SwitchStatement");
793 Breakable nested_statement(this, stmt);
794 SetStatementPosition(stmt);
795 // Keep the switch value on the stack until a case matches.
796 VisitForValue(stmt->tag(), kStack);
797
798 ZoneList<CaseClause*>* clauses = stmt->cases();
799 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
800
801 Label next_test; // Recycled for each test.
802 // Compile all the tests with branches to their bodies.
803 for (int i = 0; i < clauses->length(); i++) {
804 CaseClause* clause = clauses->at(i);
805 // The default is not a test, but remember it as final fall through.
806 if (clause->is_default()) {
807 default_clause = clause;
808 continue;
809 }
810
811 Comment cmnt(masm_, "[ Case comparison");
812 __ bind(&next_test);
813 next_test.Unuse();
814
815 // Compile the label expression.
816 VisitForValue(clause->label(), kAccumulator);
817
818 // Perform the comparison as if via '==='. The comparison stub expects
819 // the smi vs. smi case to be handled before it is called.
820 Label slow_case;
821 __ ldr(r1, MemOperand(sp, 0)); // Switch value.
822 __ mov(r2, r1);
823 __ orr(r2, r2, r0);
824 __ tst(r2, Operand(kSmiTagMask));
825 __ b(ne, &slow_case);
826 __ cmp(r1, r0);
827 __ b(ne, &next_test);
828 __ Drop(1); // Switch value is no longer needed.
829 __ b(clause->body_target()->entry_label());
830
831 __ bind(&slow_case);
832 CompareStub stub(eq, true);
833 __ CallStub(&stub);
834 __ tst(r0, r0);
835 __ b(ne, &next_test);
836 __ Drop(1); // Switch value is no longer needed.
837 __ b(clause->body_target()->entry_label());
838 }
839
840 // Discard the test value and jump to the default if present, otherwise to
841 // the end of the statement.
842 __ bind(&next_test);
843 __ Drop(1); // Switch value is no longer needed.
844 if (default_clause == NULL) {
845 __ b(nested_statement.break_target());
846 } else {
847 __ b(default_clause->body_target()->entry_label());
848 }
849
850 // Compile all the case bodies.
851 for (int i = 0; i < clauses->length(); i++) {
852 Comment cmnt(masm_, "[ Case body");
853 CaseClause* clause = clauses->at(i);
854 __ bind(clause->body_target()->entry_label());
855 VisitStatements(clause->statements());
856 }
857
858 __ bind(nested_statement.break_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000859}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000860
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000861
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000862void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000863 Comment cmnt(masm_, "[ ForInStatement");
864 SetStatementPosition(stmt);
865
866 Label loop, exit;
867 ForIn loop_statement(this, stmt);
868 increment_loop_depth();
869
870 // Get the object to enumerate over. Both SpiderMonkey and JSC
871 // ignore null and undefined in contrast to the specification; see
872 // ECMA-262 section 12.6.4.
873 VisitForValue(stmt->enumerable(), kAccumulator);
874 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
875 __ cmp(r0, ip);
876 __ b(eq, &exit);
877 __ LoadRoot(ip, Heap::kNullValueRootIndex);
878 __ cmp(r0, ip);
879 __ b(eq, &exit);
880
881 // Convert the object to a JS object.
882 Label convert, done_convert;
883 __ BranchOnSmi(r0, &convert);
884 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
885 __ b(hs, &done_convert);
886 __ bind(&convert);
887 __ push(r0);
888 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
889 __ bind(&done_convert);
890 __ push(r0);
891
892 // TODO(kasperl): Check cache validity in generated code. This is a
893 // fast case for the JSObject::IsSimpleEnum cache validity
894 // checks. If we cannot guarantee cache validity, call the runtime
895 // system to check cache validity or get the property names in a
896 // fixed array.
897
898 // Get the set of properties to enumerate.
899 __ push(r0); // Duplicate the enumerable object on the stack.
900 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
901
902 // If we got a map from the runtime call, we can do a fast
903 // modification check. Otherwise, we got a fixed array, and we have
904 // to do a slow check.
905 Label fixed_array;
906 __ mov(r2, r0);
907 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
908 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
909 __ cmp(r1, ip);
910 __ b(ne, &fixed_array);
911
912 // We got a map in register r0. Get the enumeration cache from it.
913 __ ldr(r1, FieldMemOperand(r0, Map::kInstanceDescriptorsOffset));
914 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
915 __ ldr(r2, FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
916
917 // Setup the four remaining stack slots.
918 __ push(r0); // Map.
919 __ ldr(r1, FieldMemOperand(r2, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000920 __ mov(r0, Operand(Smi::FromInt(0)));
921 // Push enumeration cache, enumeration cache length (as smi) and zero.
922 __ Push(r2, r1, r0);
923 __ jmp(&loop);
924
925 // We got a fixed array in register r0. Iterate through that.
926 __ bind(&fixed_array);
927 __ mov(r1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
928 __ Push(r1, r0);
929 __ ldr(r1, FieldMemOperand(r0, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000930 __ mov(r0, Operand(Smi::FromInt(0)));
931 __ Push(r1, r0); // Fixed array length (as smi) and initial index.
932
933 // Generate code for doing the condition check.
934 __ bind(&loop);
935 // Load the current count to r0, load the length to r1.
936 __ Ldrd(r0, r1, MemOperand(sp, 0 * kPointerSize));
937 __ cmp(r0, r1); // Compare to the array length.
938 __ b(hs, loop_statement.break_target());
939
940 // Get the current entry of the array into register r3.
941 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
942 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
943 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
944
945 // Get the expected map from the stack or a zero map in the
946 // permanent slow case into register r2.
947 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
948
949 // Check if the expected map still matches that of the enumerable.
950 // If not, we have to filter the key.
951 Label update_each;
952 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
953 __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
954 __ cmp(r4, Operand(r2));
955 __ b(eq, &update_each);
956
957 // Convert the entry to a string or null if it isn't a property
958 // anymore. If the property has been removed while iterating, we
959 // just skip it.
960 __ push(r1); // Enumerable.
961 __ push(r3); // Current entry.
962 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
963 __ mov(r3, Operand(r0));
964 __ LoadRoot(ip, Heap::kNullValueRootIndex);
965 __ cmp(r3, ip);
966 __ b(eq, loop_statement.continue_target());
967
968 // Update the 'each' property or variable from the possibly filtered
969 // entry in register r3.
970 __ bind(&update_each);
971 __ mov(result_register(), r3);
972 // Perform the assignment as if via '='.
973 EmitAssignment(stmt->each());
974
975 // Generate code for the body of the loop.
976 Label stack_limit_hit, stack_check_done;
977 Visit(stmt->body());
978
979 __ StackLimitCheck(&stack_limit_hit);
980 __ bind(&stack_check_done);
981
982 // Generate code for the going to the next element by incrementing
983 // the index (smi) stored on top of the stack.
984 __ bind(loop_statement.continue_target());
985 __ pop(r0);
986 __ add(r0, r0, Operand(Smi::FromInt(1)));
987 __ push(r0);
988 __ b(&loop);
989
990 // Slow case for the stack limit check.
991 StackCheckStub stack_check_stub;
992 __ bind(&stack_limit_hit);
993 __ CallStub(&stack_check_stub);
994 __ b(&stack_check_done);
995
996 // Remove the pointers stored on the stack.
997 __ bind(loop_statement.break_target());
998 __ Drop(5);
999
1000 // Exit and decrement the loop depth.
1001 __ bind(&exit);
1002 decrement_loop_depth();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001003}
1004
1005
1006void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info) {
1007 // Use the fast case closure allocation code that allocates in new
1008 // space for nested functions that don't need literals cloning.
1009 if (scope()->is_function_scope() && info->num_literals() == 0) {
1010 FastNewClosureStub stub;
1011 __ mov(r0, Operand(info));
1012 __ push(r0);
1013 __ CallStub(&stub);
1014 } else {
1015 __ mov(r0, Operand(info));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001016 __ Push(cp, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001017 __ CallRuntime(Runtime::kNewClosure, 2);
1018 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001019 Apply(context_, r0);
1020}
1021
1022
1023void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
1024 Comment cmnt(masm_, "[ VariableProxy");
1025 EmitVariableLoad(expr->var(), context_);
1026}
1027
1028
1029void FullCodeGenerator::EmitVariableLoad(Variable* var,
1030 Expression::Context context) {
1031 // Four cases: non-this global variables, lookup slots, all other
1032 // types of slots, and parameters that rewrite to explicit property
1033 // accesses on the arguments object.
1034 Slot* slot = var->slot();
1035 Property* property = var->AsProperty();
1036
1037 if (var->is_global() && !var->is_this()) {
1038 Comment cmnt(masm_, "Global variable");
1039 // Use inline caching. Variable name is passed in r2 and the global
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001040 // object (receiver) in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001041 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001042 __ mov(r2, Operand(var->name()));
1043 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1044 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001045 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001046
1047 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
1048 Comment cmnt(masm_, "Lookup slot");
1049 __ mov(r1, Operand(var->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001050 __ Push(cp, r1); // Context and name.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001051 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1052 Apply(context, r0);
1053
1054 } else if (slot != NULL) {
1055 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1056 ? "Context slot"
1057 : "Stack slot");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001058 if (var->mode() == Variable::CONST) {
1059 // Constants may be the hole value if they have not been initialized.
1060 // Unhole them.
1061 Label done;
1062 MemOperand slot_operand = EmitSlotSearch(slot, r0);
1063 __ ldr(r0, slot_operand);
1064 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1065 __ cmp(r0, ip);
1066 __ b(ne, &done);
1067 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1068 __ bind(&done);
1069 Apply(context, r0);
1070 } else {
1071 Apply(context, slot);
1072 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001073 } else {
1074 Comment cmnt(masm_, "Rewritten parameter");
1075 ASSERT_NOT_NULL(property);
1076 // Rewritten parameter accesses are of the form "slot[literal]".
1077
1078 // Assert that the object is in a slot.
1079 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
1080 ASSERT_NOT_NULL(object_var);
1081 Slot* object_slot = object_var->slot();
1082 ASSERT_NOT_NULL(object_slot);
1083
1084 // Load the object.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001085 Move(r1, object_slot);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001086
1087 // Assert that the key is a smi.
1088 Literal* key_literal = property->key()->AsLiteral();
1089 ASSERT_NOT_NULL(key_literal);
1090 ASSERT(key_literal->handle()->IsSmi());
1091
1092 // Load the key.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001093 __ mov(r0, Operand(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001094
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001095 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001096 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1097 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001098 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001099 }
1100}
1101
1102
1103void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1104 Comment cmnt(masm_, "[ RegExpLiteral");
1105 Label done;
1106 // Registers will be used as follows:
1107 // r4 = JS function, literals array
1108 // r3 = literal index
1109 // r2 = RegExp pattern
1110 // r1 = RegExp flags
1111 // r0 = temp + return value (RegExp literal)
1112 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1113 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
1114 int literal_offset =
1115 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1116 __ ldr(r0, FieldMemOperand(r4, literal_offset));
1117 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1118 __ cmp(r0, ip);
1119 __ b(ne, &done);
1120 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
1121 __ mov(r2, Operand(expr->pattern()));
1122 __ mov(r1, Operand(expr->flags()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001123 __ Push(r4, r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001124 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1125 __ bind(&done);
1126 Apply(context_, r0);
1127}
1128
1129
1130void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1131 Comment cmnt(masm_, "[ ObjectLiteral");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001132 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1133 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1134 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1135 __ mov(r1, Operand(expr->constant_properties()));
1136 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001137 __ Push(r3, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001138 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001139 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001140 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001141 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001142 }
1143
1144 // If result_saved is true the result is on top of the stack. If
1145 // result_saved is false the result is in r0.
1146 bool result_saved = false;
1147
1148 for (int i = 0; i < expr->properties()->length(); i++) {
1149 ObjectLiteral::Property* property = expr->properties()->at(i);
1150 if (property->IsCompileTimeValue()) continue;
1151
1152 Literal* key = property->key();
1153 Expression* value = property->value();
1154 if (!result_saved) {
1155 __ push(r0); // Save result on stack
1156 result_saved = true;
1157 }
1158 switch (property->kind()) {
1159 case ObjectLiteral::Property::CONSTANT:
1160 UNREACHABLE();
1161 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1162 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1163 // Fall through.
1164 case ObjectLiteral::Property::COMPUTED:
1165 if (key->handle()->IsSymbol()) {
1166 VisitForValue(value, kAccumulator);
1167 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001168 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001169 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1170 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001171 break;
1172 }
1173 // Fall through.
1174 case ObjectLiteral::Property::PROTOTYPE:
1175 // Duplicate receiver on stack.
1176 __ ldr(r0, MemOperand(sp));
1177 __ push(r0);
1178 VisitForValue(key, kStack);
1179 VisitForValue(value, kStack);
1180 __ CallRuntime(Runtime::kSetProperty, 3);
1181 break;
1182 case ObjectLiteral::Property::GETTER:
1183 case ObjectLiteral::Property::SETTER:
1184 // Duplicate receiver on stack.
1185 __ ldr(r0, MemOperand(sp));
1186 __ push(r0);
1187 VisitForValue(key, kStack);
1188 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1189 Smi::FromInt(1) :
1190 Smi::FromInt(0)));
1191 __ push(r1);
1192 VisitForValue(value, kStack);
1193 __ CallRuntime(Runtime::kDefineAccessor, 4);
1194 break;
1195 }
1196 }
1197
1198 if (result_saved) {
1199 ApplyTOS(context_);
1200 } else {
1201 Apply(context_, r0);
1202 }
1203}
1204
1205
1206void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1207 Comment cmnt(masm_, "[ ArrayLiteral");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001208
1209 ZoneList<Expression*>* subexprs = expr->values();
1210 int length = subexprs->length();
1211
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001212 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1213 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1214 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1215 __ mov(r1, Operand(expr->constant_elements()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001216 __ Push(r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001217 if (expr->depth() > 1) {
1218 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001219 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001220 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001221 } else {
1222 FastCloneShallowArrayStub stub(length);
1223 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001224 }
1225
1226 bool result_saved = false; // Is the result saved to the stack?
1227
1228 // Emit code to evaluate all the non-constant subexpressions and to store
1229 // them into the newly cloned array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001230 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001231 Expression* subexpr = subexprs->at(i);
1232 // If the subexpression is a literal or a simple materialized literal it
1233 // is already set in the cloned array.
1234 if (subexpr->AsLiteral() != NULL ||
1235 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1236 continue;
1237 }
1238
1239 if (!result_saved) {
1240 __ push(r0);
1241 result_saved = true;
1242 }
1243 VisitForValue(subexpr, kAccumulator);
1244
1245 // Store the subexpression value in the array's elements.
1246 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
1247 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1248 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1249 __ str(result_register(), FieldMemOperand(r1, offset));
1250
1251 // Update the write barrier for the array store with r0 as the scratch
1252 // register.
1253 __ mov(r2, Operand(offset));
1254 __ RecordWrite(r1, r2, result_register());
1255 }
1256
1257 if (result_saved) {
1258 ApplyTOS(context_);
1259 } else {
1260 Apply(context_, r0);
1261 }
1262}
1263
1264
ager@chromium.org5c838252010-02-19 08:53:10 +00001265void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1266 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001267 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1268 // on the left-hand side.
1269 if (!expr->target()->IsValidLeftHandSide()) {
1270 VisitForEffect(expr->target());
1271 return;
1272 }
1273
ager@chromium.org5c838252010-02-19 08:53:10 +00001274 // Left-hand side can only be a property, a global or a (parameter or local)
1275 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1276 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1277 LhsKind assign_type = VARIABLE;
1278 Property* prop = expr->target()->AsProperty();
1279 if (prop != NULL) {
1280 assign_type =
1281 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
1282 }
1283
1284 // Evaluate LHS expression.
1285 switch (assign_type) {
1286 case VARIABLE:
1287 // Nothing to do here.
1288 break;
1289 case NAMED_PROPERTY:
1290 if (expr->is_compound()) {
1291 // We need the receiver both on the stack and in the accumulator.
1292 VisitForValue(prop->obj(), kAccumulator);
1293 __ push(result_register());
1294 } else {
1295 VisitForValue(prop->obj(), kStack);
1296 }
1297 break;
1298 case KEYED_PROPERTY:
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001299 // We need the key and receiver on both the stack and in r0 and r1.
1300 if (expr->is_compound()) {
1301 VisitForValue(prop->obj(), kStack);
1302 VisitForValue(prop->key(), kAccumulator);
1303 __ ldr(r1, MemOperand(sp, 0));
1304 __ push(r0);
1305 } else {
1306 VisitForValue(prop->obj(), kStack);
1307 VisitForValue(prop->key(), kStack);
1308 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001309 break;
1310 }
1311
1312 // If we have a compound assignment: Get value of LHS expression and
1313 // store in on top of the stack.
1314 if (expr->is_compound()) {
1315 Location saved_location = location_;
1316 location_ = kStack;
1317 switch (assign_type) {
1318 case VARIABLE:
1319 EmitVariableLoad(expr->target()->AsVariableProxy()->var(),
1320 Expression::kValue);
1321 break;
1322 case NAMED_PROPERTY:
1323 EmitNamedPropertyLoad(prop);
1324 __ push(result_register());
1325 break;
1326 case KEYED_PROPERTY:
1327 EmitKeyedPropertyLoad(prop);
1328 __ push(result_register());
1329 break;
1330 }
1331 location_ = saved_location;
1332 }
1333
1334 // Evaluate RHS expression.
1335 Expression* rhs = expr->value();
1336 VisitForValue(rhs, kAccumulator);
1337
1338 // If we have a compound assignment: Apply operator.
1339 if (expr->is_compound()) {
1340 Location saved_location = location_;
1341 location_ = kAccumulator;
1342 EmitBinaryOp(expr->binary_op(), Expression::kValue);
1343 location_ = saved_location;
1344 }
1345
1346 // Record source position before possible IC call.
1347 SetSourcePosition(expr->position());
1348
1349 // Store the value.
1350 switch (assign_type) {
1351 case VARIABLE:
1352 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001353 expr->op(),
ager@chromium.org5c838252010-02-19 08:53:10 +00001354 context_);
1355 break;
1356 case NAMED_PROPERTY:
1357 EmitNamedPropertyAssignment(expr);
1358 break;
1359 case KEYED_PROPERTY:
1360 EmitKeyedPropertyAssignment(expr);
1361 break;
1362 }
1363}
1364
1365
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001366void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1367 SetSourcePosition(prop->position());
1368 Literal* key = prop->key()->AsLiteral();
1369 __ mov(r2, Operand(key->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001370 // Call load IC. It has arguments receiver and property name r0 and r2.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001371 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1372 __ Call(ic, RelocInfo::CODE_TARGET);
1373}
1374
1375
1376void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1377 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001378 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001379 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1380 __ Call(ic, RelocInfo::CODE_TARGET);
1381}
1382
1383
1384void FullCodeGenerator::EmitBinaryOp(Token::Value op,
1385 Expression::Context context) {
1386 __ pop(r1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001387 GenericBinaryOpStub stub(op, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001388 __ CallStub(&stub);
1389 Apply(context, r0);
1390}
1391
1392
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001393void FullCodeGenerator::EmitAssignment(Expression* expr) {
1394 // Invalid left-hand sides are rewritten to have a 'throw
1395 // ReferenceError' on the left-hand side.
1396 if (!expr->IsValidLeftHandSide()) {
1397 VisitForEffect(expr);
1398 return;
1399 }
1400
1401 // Left-hand side can only be a property, a global or a (parameter or local)
1402 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1403 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1404 LhsKind assign_type = VARIABLE;
1405 Property* prop = expr->AsProperty();
1406 if (prop != NULL) {
1407 assign_type = (prop->key()->IsPropertyName())
1408 ? NAMED_PROPERTY
1409 : KEYED_PROPERTY;
1410 }
1411
1412 switch (assign_type) {
1413 case VARIABLE: {
1414 Variable* var = expr->AsVariableProxy()->var();
1415 EmitVariableAssignment(var, Token::ASSIGN, Expression::kEffect);
1416 break;
1417 }
1418 case NAMED_PROPERTY: {
1419 __ push(r0); // Preserve value.
1420 VisitForValue(prop->obj(), kAccumulator);
1421 __ mov(r1, r0);
1422 __ pop(r0); // Restore value.
1423 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
1424 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1425 __ Call(ic, RelocInfo::CODE_TARGET);
1426 break;
1427 }
1428 case KEYED_PROPERTY: {
1429 __ push(r0); // Preserve value.
1430 VisitForValue(prop->obj(), kStack);
1431 VisitForValue(prop->key(), kAccumulator);
1432 __ mov(r1, r0);
1433 __ pop(r2);
1434 __ pop(r0); // Restore value.
1435 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1436 __ Call(ic, RelocInfo::CODE_TARGET);
1437 break;
1438 }
1439 }
1440}
1441
1442
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001443void FullCodeGenerator::EmitVariableAssignment(Variable* var,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001444 Token::Value op,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001445 Expression::Context context) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001446 // Left-hand sides that rewrite to explicit property accesses do not reach
1447 // here.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001448 ASSERT(var != NULL);
1449 ASSERT(var->is_global() || var->slot() != NULL);
1450
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001451 if (var->is_global()) {
1452 ASSERT(!var->is_this());
1453 // Assignment to a global variable. Use inline caching for the
1454 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001455 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001456 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001457 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001458 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1459 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001460
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001461 } else if (var->mode() != Variable::CONST || op == Token::INIT_CONST) {
1462 // Perform the assignment for non-const variables and for initialization
1463 // of const variables. Const assignments are simply skipped.
1464 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001465 Slot* slot = var->slot();
1466 switch (slot->type()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001467 case Slot::PARAMETER:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001468 case Slot::LOCAL:
1469 if (op == Token::INIT_CONST) {
1470 // Detect const reinitialization by checking for the hole value.
1471 __ ldr(r1, MemOperand(fp, SlotOffset(slot)));
1472 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1473 __ cmp(r1, ip);
1474 __ b(ne, &done);
1475 }
1476 // Perform the assignment.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001477 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1478 break;
1479
1480 case Slot::CONTEXT: {
1481 MemOperand target = EmitSlotSearch(slot, r1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001482 if (op == Token::INIT_CONST) {
1483 // Detect const reinitialization by checking for the hole value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001484 __ ldr(r2, target);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001485 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001486 __ cmp(r2, ip);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001487 __ b(ne, &done);
1488 }
1489 // Perform the assignment and issue the write barrier.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001490 __ str(result_register(), target);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001491 // RecordWrite may destroy all its register arguments.
1492 __ mov(r3, result_register());
1493 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001494 __ mov(r2, Operand(offset));
1495 __ RecordWrite(r1, r2, r3);
1496 break;
1497 }
1498
1499 case Slot::LOOKUP:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001500 // Call the runtime for the assignment. The runtime will ignore
1501 // const reinitialization.
1502 __ push(r0); // Value.
1503 __ mov(r0, Operand(slot->var()->name()));
1504 __ Push(cp, r0); // Context and name.
1505 if (op == Token::INIT_CONST) {
1506 // The runtime will ignore const redeclaration.
1507 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1508 } else {
1509 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1510 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001511 break;
1512 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001513 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001514 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001515
ager@chromium.org5c838252010-02-19 08:53:10 +00001516 Apply(context, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001517}
1518
1519
1520void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1521 // Assignment to a property, using a named store IC.
1522 Property* prop = expr->target()->AsProperty();
1523 ASSERT(prop != NULL);
1524 ASSERT(prop->key()->AsLiteral() != NULL);
1525
1526 // If the assignment starts a block of assignments to the same object,
1527 // change to slow case to avoid the quadratic behavior of repeatedly
1528 // adding fast properties.
1529 if (expr->starts_initialization_block()) {
1530 __ push(result_register());
1531 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1532 __ push(ip);
1533 __ CallRuntime(Runtime::kToSlowProperties, 1);
1534 __ pop(result_register());
1535 }
1536
1537 // Record source code position before IC call.
1538 SetSourcePosition(expr->position());
1539 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001540 // Load receiver to r1. Leave a copy in the stack if needed for turning the
1541 // receiver into fast case.
ager@chromium.org5c838252010-02-19 08:53:10 +00001542 if (expr->ends_initialization_block()) {
1543 __ ldr(r1, MemOperand(sp));
1544 } else {
1545 __ pop(r1);
1546 }
1547
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001548 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1549 __ Call(ic, RelocInfo::CODE_TARGET);
1550
1551 // If the assignment ends an initialization block, revert to fast case.
1552 if (expr->ends_initialization_block()) {
1553 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001554 // Receiver is under the result value.
1555 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001556 __ push(ip);
1557 __ CallRuntime(Runtime::kToFastProperties, 1);
1558 __ pop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001559 DropAndApply(1, context_, r0);
1560 } else {
1561 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001562 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001563}
1564
1565
1566void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1567 // Assignment to a property, using a keyed store IC.
1568
1569 // If the assignment starts a block of assignments to the same object,
1570 // change to slow case to avoid the quadratic behavior of repeatedly
1571 // adding fast properties.
1572 if (expr->starts_initialization_block()) {
1573 __ push(result_register());
1574 // Receiver is now under the key and value.
1575 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1576 __ push(ip);
1577 __ CallRuntime(Runtime::kToSlowProperties, 1);
1578 __ pop(result_register());
1579 }
1580
1581 // Record source code position before IC call.
1582 SetSourcePosition(expr->position());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001583 __ pop(r1); // Key.
1584 // Load receiver to r2. Leave a copy in the stack if needed for turning the
1585 // receiver into fast case.
1586 if (expr->ends_initialization_block()) {
1587 __ ldr(r2, MemOperand(sp));
1588 } else {
1589 __ pop(r2);
1590 }
1591
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001592 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1593 __ Call(ic, RelocInfo::CODE_TARGET);
1594
1595 // If the assignment ends an initialization block, revert to fast case.
1596 if (expr->ends_initialization_block()) {
1597 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001598 // Receiver is under the result value.
1599 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001600 __ push(ip);
1601 __ CallRuntime(Runtime::kToFastProperties, 1);
1602 __ pop(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001603 DropAndApply(1, context_, r0);
1604 } else {
1605 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001606 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001607}
1608
1609
1610void FullCodeGenerator::VisitProperty(Property* expr) {
1611 Comment cmnt(masm_, "[ Property");
1612 Expression* key = expr->key();
1613
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001614 if (key->IsPropertyName()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001615 VisitForValue(expr->obj(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001616 EmitNamedPropertyLoad(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001617 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001618 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001619 VisitForValue(expr->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001620 VisitForValue(expr->key(), kAccumulator);
1621 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001622 EmitKeyedPropertyLoad(expr);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001623 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001624 }
1625}
1626
1627void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001628 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001629 RelocInfo::Mode mode) {
1630 // Code common for calls using the IC.
1631 ZoneList<Expression*>* args = expr->arguments();
1632 int arg_count = args->length();
1633 for (int i = 0; i < arg_count; i++) {
1634 VisitForValue(args->at(i), kStack);
1635 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001636 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001637 // Record source position for debugger.
1638 SetSourcePosition(expr->position());
1639 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001640 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1641 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001642 __ Call(ic, mode);
1643 // Restore context register.
1644 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001645 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001646}
1647
1648
1649void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1650 // Code common for calls using the call stub.
1651 ZoneList<Expression*>* args = expr->arguments();
1652 int arg_count = args->length();
1653 for (int i = 0; i < arg_count; i++) {
1654 VisitForValue(args->at(i), kStack);
1655 }
1656 // Record source position for debugger.
1657 SetSourcePosition(expr->position());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001658 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1659 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001660 __ CallStub(&stub);
1661 // Restore context register.
1662 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001663 DropAndApply(1, context_, r0);
1664}
1665
1666
1667void FullCodeGenerator::VisitCall(Call* expr) {
1668 Comment cmnt(masm_, "[ Call");
1669 Expression* fun = expr->expression();
1670 Variable* var = fun->AsVariableProxy()->AsVariable();
1671
1672 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001673 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1674 // resolve the function we need to call and the receiver of the
1675 // call. Then we call the resolved function using the given
1676 // arguments.
1677 VisitForValue(fun, kStack);
1678 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1679 __ push(r2); // Reserved receiver slot.
1680
1681 // Push the arguments.
1682 ZoneList<Expression*>* args = expr->arguments();
1683 int arg_count = args->length();
1684 for (int i = 0; i < arg_count; i++) {
1685 VisitForValue(args->at(i), kStack);
1686 }
1687
1688 // Push copy of the function - found below the arguments.
1689 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1690 __ push(r1);
1691
1692 // Push copy of the first argument or undefined if it doesn't exist.
1693 if (arg_count > 0) {
1694 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1695 __ push(r1);
1696 } else {
1697 __ push(r2);
1698 }
1699
1700 // Push the receiver of the enclosing function and do runtime call.
1701 __ ldr(r1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1702 __ push(r1);
1703 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1704
1705 // The runtime call returns a pair of values in r0 (function) and
1706 // r1 (receiver). Touch up the stack with the right values.
1707 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1708 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
1709
1710 // Record source position for debugger.
1711 SetSourcePosition(expr->position());
1712 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1713 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1714 __ CallStub(&stub);
1715 // Restore context register.
1716 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1717 DropAndApply(1, context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001718 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001719 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001720 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001721 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001722 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1723 } else if (var != NULL && var->slot() != NULL &&
1724 var->slot()->type() == Slot::LOOKUP) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001725 // Call to a lookup slot (dynamically introduced variable). Call the
1726 // runtime to find the function to call (returned in eax) and the object
1727 // holding it (returned in edx).
1728 __ push(context_register());
1729 __ mov(r2, Operand(var->name()));
1730 __ push(r2);
1731 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1732 __ push(r0); // Function.
1733 __ push(r1); // Receiver.
1734 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001735 } else if (fun->AsProperty() != NULL) {
1736 // Call to an object property.
1737 Property* prop = fun->AsProperty();
1738 Literal* key = prop->key()->AsLiteral();
1739 if (key != NULL && key->handle()->IsSymbol()) {
1740 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001741 VisitForValue(prop->obj(), kStack);
1742 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1743 } else {
1744 // Call to a keyed property, use keyed load IC followed by function
1745 // call.
1746 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001747 VisitForValue(prop->key(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001748 // Record source code position for IC call.
1749 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001750 if (prop->is_synthetic()) {
1751 __ pop(r1); // We do not need to keep the receiver.
1752 } else {
1753 __ ldr(r1, MemOperand(sp, 0)); // Keep receiver, to call function on.
1754 }
1755
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001756 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1757 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001758 if (prop->is_synthetic()) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001759 // Push result (function).
1760 __ push(r0);
1761 // Push Global receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001762 __ ldr(r1, CodeGenerator::GlobalObject());
1763 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001764 __ push(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001765 } else {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001766 // Pop receiver.
1767 __ pop(r1);
1768 // Push result (function).
1769 __ push(r0);
1770 __ push(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001771 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001772 EmitCallWithStub(expr);
1773 }
1774 } else {
1775 // Call to some other expression. If the expression is an anonymous
1776 // function literal not called in a loop, mark it as one that should
1777 // also use the fast code generator.
1778 FunctionLiteral* lit = fun->AsFunctionLiteral();
1779 if (lit != NULL &&
1780 lit->name()->Equals(Heap::empty_string()) &&
1781 loop_depth() == 0) {
1782 lit->set_try_full_codegen(true);
1783 }
1784 VisitForValue(fun, kStack);
1785 // Load global receiver object.
1786 __ ldr(r1, CodeGenerator::GlobalObject());
1787 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1788 __ push(r1);
1789 // Emit function call.
1790 EmitCallWithStub(expr);
1791 }
1792}
1793
1794
1795void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1796 Comment cmnt(masm_, "[ CallNew");
1797 // According to ECMA-262, section 11.2.2, page 44, the function
1798 // expression in new calls must be evaluated before the
1799 // arguments.
1800 // Push function on the stack.
1801 VisitForValue(expr->expression(), kStack);
1802
1803 // Push global object (receiver).
1804 __ ldr(r0, CodeGenerator::GlobalObject());
1805 __ push(r0);
1806 // Push the arguments ("left-to-right") on the stack.
1807 ZoneList<Expression*>* args = expr->arguments();
1808 int arg_count = args->length();
1809 for (int i = 0; i < arg_count; i++) {
1810 VisitForValue(args->at(i), kStack);
1811 }
1812
1813 // Call the construct call builtin that handles allocation and
1814 // constructor invocation.
1815 SetSourcePosition(expr->position());
1816
1817 // Load function, arg_count into r1 and r0.
1818 __ mov(r0, Operand(arg_count));
1819 // Function is in sp[arg_count + 1].
1820 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1821
1822 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1823 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
1824
1825 // Replace function on TOS with result in r0, or pop it.
1826 DropAndApply(1, context_, r0);
1827}
1828
1829
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001830void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1831 ASSERT(args->length() == 1);
1832
1833 VisitForValue(args->at(0), kAccumulator);
1834
1835 Label materialize_true, materialize_false;
1836 Label* if_true = NULL;
1837 Label* if_false = NULL;
1838 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1839
1840 __ BranchOnSmi(r0, if_true);
1841 __ b(if_false);
1842
1843 Apply(context_, if_true, if_false);
1844}
1845
1846
1847void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1848 ASSERT(args->length() == 1);
1849
1850 VisitForValue(args->at(0), kAccumulator);
1851
1852 Label materialize_true, materialize_false;
1853 Label* if_true = NULL;
1854 Label* if_false = NULL;
1855 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1856
1857 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
1858 __ b(eq, if_true);
1859 __ b(if_false);
1860
1861 Apply(context_, if_true, if_false);
1862}
1863
1864
1865void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1866 ASSERT(args->length() == 1);
1867
1868 VisitForValue(args->at(0), kAccumulator);
1869
1870 Label materialize_true, materialize_false;
1871 Label* if_true = NULL;
1872 Label* if_false = NULL;
1873 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1874 __ BranchOnSmi(r0, if_false);
1875 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1876 __ cmp(r0, ip);
1877 __ b(eq, if_true);
1878 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1879 // Undetectable objects behave like undefined when tested with typeof.
1880 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
1881 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1882 __ b(ne, if_false);
1883 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1884 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
1885 __ b(lt, if_false);
1886 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
1887 __ b(le, if_true);
1888 __ b(if_false);
1889
1890 Apply(context_, if_true, if_false);
1891}
1892
1893
1894void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
1895 ASSERT(args->length() == 1);
1896
1897 VisitForValue(args->at(0), kAccumulator);
1898
1899 Label materialize_true, materialize_false;
1900 Label* if_true = NULL;
1901 Label* if_false = NULL;
1902 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1903
1904 __ BranchOnSmi(r0, if_false);
1905 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1906 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
1907 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1908 __ b(ne, if_true);
1909 __ b(if_false);
1910
1911 Apply(context_, if_true, if_false);
1912}
1913
1914
1915void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
1916 ASSERT(args->length() == 1);
1917
1918 VisitForValue(args->at(0), kAccumulator);
1919
1920 Label materialize_true, materialize_false;
1921 Label* if_true = NULL;
1922 Label* if_false = NULL;
1923 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1924
1925 __ BranchOnSmi(r0, if_false);
1926 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
1927 __ b(eq, if_true);
1928 __ b(if_false);
1929
1930 Apply(context_, if_true, if_false);
1931}
1932
1933
1934void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
1935 ASSERT(args->length() == 1);
1936
1937 VisitForValue(args->at(0), kAccumulator);
1938
1939 Label materialize_true, materialize_false;
1940 Label* if_true = NULL;
1941 Label* if_false = NULL;
1942 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1943
1944 __ BranchOnSmi(r0, if_false);
1945 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
1946 __ b(eq, if_true);
1947 __ b(if_false);
1948
1949 Apply(context_, if_true, if_false);
1950}
1951
1952
1953void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
1954 ASSERT(args->length() == 1);
1955
1956 VisitForValue(args->at(0), kAccumulator);
1957
1958 Label materialize_true, materialize_false;
1959 Label* if_true = NULL;
1960 Label* if_false = NULL;
1961 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1962
1963 __ BranchOnSmi(r0, if_false);
1964 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
1965 __ b(eq, if_true);
1966 __ b(if_false);
1967
1968 Apply(context_, if_true, if_false);
1969}
1970
1971
1972
1973void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
1974 ASSERT(args->length() == 0);
1975
1976 Label materialize_true, materialize_false;
1977 Label* if_true = NULL;
1978 Label* if_false = NULL;
1979 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1980
1981 // Get the frame pointer for the calling frame.
1982 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1983
1984 // Skip the arguments adaptor frame if it exists.
1985 Label check_frame_marker;
1986 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
1987 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1988 __ b(ne, &check_frame_marker);
1989 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
1990
1991 // Check the marker in the calling frame.
1992 __ bind(&check_frame_marker);
1993 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
1994 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
1995 __ b(eq, if_true);
1996 __ b(if_false);
1997
1998 Apply(context_, if_true, if_false);
1999}
2000
2001
2002void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
2003 ASSERT(args->length() == 2);
2004
2005 // Load the two objects into registers and perform the comparison.
2006 VisitForValue(args->at(0), kStack);
2007 VisitForValue(args->at(1), kAccumulator);
2008
2009 Label materialize_true, materialize_false;
2010 Label* if_true = NULL;
2011 Label* if_false = NULL;
2012 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2013
2014 __ pop(r1);
2015 __ cmp(r0, r1);
2016 __ b(eq, if_true);
2017 __ b(if_false);
2018
2019 Apply(context_, if_true, if_false);
2020}
2021
2022
2023void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
2024 ASSERT(args->length() == 1);
2025
2026 // ArgumentsAccessStub expects the key in edx and the formal
2027 // parameter count in eax.
2028 VisitForValue(args->at(0), kAccumulator);
2029 __ mov(r1, r0);
2030 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2031 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2032 __ CallStub(&stub);
2033 Apply(context_, r0);
2034}
2035
2036
2037void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
2038 ASSERT(args->length() == 0);
2039
2040 Label exit;
2041 // Get the number of formal parameters.
2042 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2043
2044 // Check if the calling frame is an arguments adaptor frame.
2045 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2046 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
2047 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2048 __ b(ne, &exit);
2049
2050 // Arguments adaptor case: Read the arguments length from the
2051 // adaptor frame.
2052 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2053
2054 __ bind(&exit);
2055 Apply(context_, r0);
2056}
2057
2058
2059void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
2060 ASSERT(args->length() == 1);
2061 Label done, null, function, non_function_constructor;
2062
2063 VisitForValue(args->at(0), kAccumulator);
2064
2065 // If the object is a smi, we return null.
2066 __ BranchOnSmi(r0, &null);
2067
2068 // Check that the object is a JS object but take special care of JS
2069 // functions to make sure they have 'Function' as their class.
2070 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2071 __ b(lt, &null);
2072
2073 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2074 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2075 // LAST_JS_OBJECT_TYPE.
2076 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2077 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2078 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2079 __ b(eq, &function);
2080
2081 // Check if the constructor in the map is a function.
2082 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2083 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2084 __ b(ne, &non_function_constructor);
2085
2086 // r0 now contains the constructor function. Grab the
2087 // instance class name from there.
2088 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2089 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2090 __ b(&done);
2091
2092 // Functions have class 'Function'.
2093 __ bind(&function);
2094 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2095 __ jmp(&done);
2096
2097 // Objects with a non-function constructor have class 'Object'.
2098 __ bind(&non_function_constructor);
2099 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2100 __ jmp(&done);
2101
2102 // Non-JS objects have class null.
2103 __ bind(&null);
2104 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2105
2106 // All done.
2107 __ bind(&done);
2108
2109 Apply(context_, r0);
2110}
2111
2112
2113void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2114 // Conditionally generate a log call.
2115 // Args:
2116 // 0 (literal string): The type of logging (corresponds to the flags).
2117 // This is used to determine whether or not to generate the log call.
2118 // 1 (string): Format string. Access the string at argument index 2
2119 // with '%2s' (see Logger::LogRuntime for all the formats).
2120 // 2 (array): Arguments to the format string.
2121 ASSERT_EQ(args->length(), 3);
2122#ifdef ENABLE_LOGGING_AND_PROFILING
2123 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2124 VisitForValue(args->at(1), kStack);
2125 VisitForValue(args->at(2), kStack);
2126 __ CallRuntime(Runtime::kLog, 2);
2127 }
2128#endif
2129 // Finally, we're expected to leave a value on the top of the stack.
2130 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2131 Apply(context_, r0);
2132}
2133
2134
2135void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2136 ASSERT(args->length() == 0);
2137
2138 Label slow_allocate_heapnumber;
2139 Label heapnumber_allocated;
2140
2141 __ AllocateHeapNumber(r4, r1, r2, &slow_allocate_heapnumber);
2142 __ jmp(&heapnumber_allocated);
2143
2144 __ bind(&slow_allocate_heapnumber);
2145 // To allocate a heap number, and ensure that it is not a smi, we
2146 // call the runtime function FUnaryMinus on 0, returning the double
2147 // -0.0. A new, distinct heap number is returned each time.
2148 __ mov(r0, Operand(Smi::FromInt(0)));
2149 __ push(r0);
2150 __ CallRuntime(Runtime::kNumberUnaryMinus, 1);
2151 __ mov(r4, Operand(r0));
2152
2153 __ bind(&heapnumber_allocated);
2154
2155 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
2156 // by computing:
2157 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2158 if (CpuFeatures::IsSupported(VFP3)) {
2159 __ PrepareCallCFunction(0, r1);
2160 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
2161
2162 CpuFeatures::Scope scope(VFP3);
2163 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2164 // Create this constant using mov/orr to avoid PC relative load.
2165 __ mov(r1, Operand(0x41000000));
2166 __ orr(r1, r1, Operand(0x300000));
2167 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
2168 __ vmov(d7, r0, r1);
2169 // Move 0x4130000000000000 to VFP.
2170 __ mov(r0, Operand(0));
2171 __ vmov(d8, r0, r1);
2172 // Subtract and store the result in the heap number.
2173 __ vsub(d7, d7, d8);
2174 __ sub(r0, r4, Operand(kHeapObjectTag));
2175 __ vstr(d7, r0, HeapNumber::kValueOffset);
2176 __ mov(r0, r4);
2177 } else {
2178 __ mov(r0, Operand(r4));
2179 __ PrepareCallCFunction(1, r1);
2180 __ CallCFunction(
2181 ExternalReference::fill_heap_number_with_random_function(), 1);
2182 }
2183
2184 Apply(context_, r0);
2185}
2186
2187
2188void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
2189 // Load the arguments on the stack and call the stub.
2190 SubStringStub stub;
2191 ASSERT(args->length() == 3);
2192 VisitForValue(args->at(0), kStack);
2193 VisitForValue(args->at(1), kStack);
2194 VisitForValue(args->at(2), kStack);
2195 __ CallStub(&stub);
2196 Apply(context_, r0);
2197}
2198
2199
2200void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
2201 // Load the arguments on the stack and call the stub.
2202 RegExpExecStub stub;
2203 ASSERT(args->length() == 4);
2204 VisitForValue(args->at(0), kStack);
2205 VisitForValue(args->at(1), kStack);
2206 VisitForValue(args->at(2), kStack);
2207 VisitForValue(args->at(3), kStack);
2208 __ CallStub(&stub);
2209 Apply(context_, r0);
2210}
2211
2212
2213void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
2214 ASSERT(args->length() == 1);
2215
2216 VisitForValue(args->at(0), kAccumulator); // Load the object.
2217
2218 Label done;
2219 // If the object is a smi return the object.
2220 __ BranchOnSmi(r0, &done);
2221 // If the object is not a value type, return the object.
2222 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
2223 __ b(ne, &done);
2224 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2225
2226 __ bind(&done);
2227 Apply(context_, r0);
2228}
2229
2230
2231void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
2232 // Load the arguments on the stack and call the runtime function.
2233 ASSERT(args->length() == 2);
2234 VisitForValue(args->at(0), kStack);
2235 VisitForValue(args->at(1), kStack);
2236 __ CallRuntime(Runtime::kMath_pow, 2);
2237 Apply(context_, r0);
2238}
2239
2240
2241void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
2242 ASSERT(args->length() == 2);
2243
2244 VisitForValue(args->at(0), kStack); // Load the object.
2245 VisitForValue(args->at(1), kAccumulator); // Load the value.
2246 __ pop(r1); // r0 = value. r1 = object.
2247
2248 Label done;
2249 // If the object is a smi, return the value.
2250 __ BranchOnSmi(r1, &done);
2251
2252 // If the object is not a value type, return the value.
2253 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
2254 __ b(ne, &done);
2255
2256 // Store the value.
2257 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2258 // Update the write barrier. Save the value as it will be
2259 // overwritten by the write barrier code and is needed afterward.
2260 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
2261 __ RecordWrite(r1, r2, r3);
2262
2263 __ bind(&done);
2264 Apply(context_, r0);
2265}
2266
2267
2268void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2269 ASSERT_EQ(args->length(), 1);
2270
2271 // Load the argument on the stack and call the stub.
2272 VisitForValue(args->at(0), kStack);
2273
2274 NumberToStringStub stub;
2275 __ CallStub(&stub);
2276 Apply(context_, r0);
2277}
2278
2279
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002280void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002281 ASSERT(args->length() == 1);
2282
2283 VisitForValue(args->at(0), kAccumulator);
2284
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002285 Label done;
2286 StringCharFromCodeGenerator generator(r0, r1);
2287 generator.GenerateFast(masm_);
2288 __ jmp(&done);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002289
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002290 NopRuntimeCallHelper call_helper;
2291 generator.GenerateSlow(masm_, call_helper);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002292
2293 __ bind(&done);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002294 Apply(context_, r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002295}
2296
2297
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002298void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
2299 ASSERT(args->length() == 2);
2300
2301 VisitForValue(args->at(0), kStack);
2302 VisitForValue(args->at(1), kAccumulator);
2303
2304 Register object = r1;
2305 Register index = r0;
2306 Register scratch = r2;
2307 Register result = r3;
2308
2309 __ pop(object);
2310
2311 Label need_conversion;
2312 Label index_out_of_range;
2313 Label done;
2314 StringCharCodeAtGenerator generator(object,
2315 index,
2316 scratch,
2317 result,
2318 &need_conversion,
2319 &need_conversion,
2320 &index_out_of_range,
2321 STRING_INDEX_IS_NUMBER);
2322 generator.GenerateFast(masm_);
2323 __ jmp(&done);
2324
2325 __ bind(&index_out_of_range);
2326 // When the index is out of range, the spec requires us to return
2327 // NaN.
2328 __ LoadRoot(result, Heap::kNanValueRootIndex);
2329 __ jmp(&done);
2330
2331 __ bind(&need_conversion);
2332 // Load the undefined value into the result register, which will
2333 // trigger conversion.
2334 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2335 __ jmp(&done);
2336
2337 NopRuntimeCallHelper call_helper;
2338 generator.GenerateSlow(masm_, call_helper);
2339
2340 __ bind(&done);
2341 Apply(context_, result);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002342}
2343
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002344
2345void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
2346 ASSERT(args->length() == 2);
2347
2348 VisitForValue(args->at(0), kStack);
2349 VisitForValue(args->at(1), kAccumulator);
2350
2351 Register object = r1;
2352 Register index = r0;
2353 Register scratch1 = r2;
2354 Register scratch2 = r3;
2355 Register result = r0;
2356
2357 __ pop(object);
2358
2359 Label need_conversion;
2360 Label index_out_of_range;
2361 Label done;
2362 StringCharAtGenerator generator(object,
2363 index,
2364 scratch1,
2365 scratch2,
2366 result,
2367 &need_conversion,
2368 &need_conversion,
2369 &index_out_of_range,
2370 STRING_INDEX_IS_NUMBER);
2371 generator.GenerateFast(masm_);
2372 __ jmp(&done);
2373
2374 __ bind(&index_out_of_range);
2375 // When the index is out of range, the spec requires us to return
2376 // the empty string.
2377 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2378 __ jmp(&done);
2379
2380 __ bind(&need_conversion);
2381 // Move smi zero into the result register, which will trigger
2382 // conversion.
2383 __ mov(result, Operand(Smi::FromInt(0)));
2384 __ jmp(&done);
2385
2386 NopRuntimeCallHelper call_helper;
2387 generator.GenerateSlow(masm_, call_helper);
2388
2389 __ bind(&done);
2390 Apply(context_, result);
2391}
2392
2393
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002394void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2395 ASSERT_EQ(2, args->length());
2396
2397 VisitForValue(args->at(0), kStack);
2398 VisitForValue(args->at(1), kStack);
2399
2400 StringAddStub stub(NO_STRING_ADD_FLAGS);
2401 __ CallStub(&stub);
2402 Apply(context_, r0);
2403}
2404
2405
2406void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
2407 ASSERT_EQ(2, args->length());
2408
2409 VisitForValue(args->at(0), kStack);
2410 VisitForValue(args->at(1), kStack);
2411
2412 StringCompareStub stub;
2413 __ CallStub(&stub);
2414 Apply(context_, r0);
2415}
2416
2417
2418void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
2419 // Load the argument on the stack and call the runtime.
2420 ASSERT(args->length() == 1);
2421 VisitForValue(args->at(0), kStack);
2422 __ CallRuntime(Runtime::kMath_sin, 1);
2423 Apply(context_, r0);
2424}
2425
2426
2427void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
2428 // Load the argument on the stack and call the runtime.
2429 ASSERT(args->length() == 1);
2430 VisitForValue(args->at(0), kStack);
2431 __ CallRuntime(Runtime::kMath_cos, 1);
2432 Apply(context_, r0);
2433}
2434
2435
2436void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2437 // Load the argument on the stack and call the runtime function.
2438 ASSERT(args->length() == 1);
2439 VisitForValue(args->at(0), kStack);
2440 __ CallRuntime(Runtime::kMath_sqrt, 1);
2441 Apply(context_, r0);
2442}
2443
2444
2445void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2446 ASSERT(args->length() >= 2);
2447
2448 int arg_count = args->length() - 2; // For receiver and function.
2449 VisitForValue(args->at(0), kStack); // Receiver.
2450 for (int i = 0; i < arg_count; i++) {
2451 VisitForValue(args->at(i + 1), kStack);
2452 }
2453 VisitForValue(args->at(arg_count + 1), kAccumulator); // Function.
2454
2455 // InvokeFunction requires function in r1. Move it in there.
2456 if (!result_register().is(r1)) __ mov(r1, result_register());
2457 ParameterCount count(arg_count);
2458 __ InvokeFunction(r1, count, CALL_FUNCTION);
2459 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2460 Apply(context_, r0);
2461}
2462
2463
2464void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2465 ASSERT(args->length() == 3);
2466 VisitForValue(args->at(0), kStack);
2467 VisitForValue(args->at(1), kStack);
2468 VisitForValue(args->at(2), kStack);
2469 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
2470 Apply(context_, r0);
2471}
2472
2473
2474void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2475 ASSERT(args->length() == 3);
2476 VisitForValue(args->at(0), kStack);
2477 VisitForValue(args->at(1), kStack);
2478 VisitForValue(args->at(2), kStack);
2479 __ CallRuntime(Runtime::kSwapElements, 3);
2480 Apply(context_, r0);
2481}
2482
2483
2484void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2485 ASSERT_EQ(2, args->length());
2486
2487 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2488 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2489
2490 Handle<FixedArray> jsfunction_result_caches(
2491 Top::global_context()->jsfunction_result_caches());
2492 if (jsfunction_result_caches->length() <= cache_id) {
2493 __ Abort("Attempt to use undefined cache.");
2494 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2495 Apply(context_, r0);
2496 return;
2497 }
2498
2499 VisitForValue(args->at(1), kAccumulator);
2500
2501 Register key = r0;
2502 Register cache = r1;
2503 __ ldr(cache, CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX));
2504 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
2505 __ ldr(cache,
2506 CodeGenerator::ContextOperand(
2507 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
2508 __ ldr(cache,
2509 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2510
2511
2512 Label done, not_found;
2513 // tmp now holds finger offset as a smi.
2514 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2515 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2516 // r2 now holds finger offset as a smi.
2517 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2518 // r3 now points to the start of fixed array elements.
2519 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2520 // Note side effect of PreIndex: r3 now points to the key of the pair.
2521 __ cmp(key, r2);
2522 __ b(ne, &not_found);
2523
2524 __ ldr(r0, MemOperand(r3, kPointerSize));
2525 __ b(&done);
2526
2527 __ bind(&not_found);
2528 // Call runtime to perform the lookup.
2529 __ Push(cache, key);
2530 __ CallRuntime(Runtime::kGetFromCache, 2);
2531
2532 __ bind(&done);
2533 Apply(context_, r0);
2534}
2535
2536
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002537void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002538 Handle<String> name = expr->name();
2539 if (name->length() > 0 && name->Get(0) == '_') {
2540 Comment cmnt(masm_, "[ InlineRuntimeCall");
2541 EmitInlineRuntimeCall(expr);
2542 return;
2543 }
2544
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002545 Comment cmnt(masm_, "[ CallRuntime");
2546 ZoneList<Expression*>* args = expr->arguments();
2547
2548 if (expr->is_jsruntime()) {
2549 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002550 __ ldr(r0, CodeGenerator::GlobalObject());
2551 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002552 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002553 }
2554
2555 // Push the arguments ("left-to-right").
2556 int arg_count = args->length();
2557 for (int i = 0; i < arg_count; i++) {
2558 VisitForValue(args->at(i), kStack);
2559 }
2560
2561 if (expr->is_jsruntime()) {
2562 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002563 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002564 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2565 NOT_IN_LOOP);
2566 __ Call(ic, RelocInfo::CODE_TARGET);
2567 // Restore context register.
2568 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002569 } else {
2570 // Call the C runtime function.
2571 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002572 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002573 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002574}
2575
2576
2577void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2578 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002579 case Token::DELETE: {
2580 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2581 Property* prop = expr->expression()->AsProperty();
2582 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2583 if (prop == NULL && var == NULL) {
2584 // Result of deleting non-property, non-variable reference is true.
2585 // The subexpression may have side effects.
2586 VisitForEffect(expr->expression());
2587 Apply(context_, true);
2588 } else if (var != NULL &&
2589 !var->is_global() &&
2590 var->slot() != NULL &&
2591 var->slot()->type() != Slot::LOOKUP) {
2592 // Result of deleting non-global, non-dynamic variables is false.
2593 // The subexpression does not have side effects.
2594 Apply(context_, false);
2595 } else {
2596 // Property or variable reference. Call the delete builtin with
2597 // object and property name as arguments.
2598 if (prop != NULL) {
2599 VisitForValue(prop->obj(), kStack);
2600 VisitForValue(prop->key(), kStack);
2601 } else if (var->is_global()) {
2602 __ ldr(r1, CodeGenerator::GlobalObject());
2603 __ mov(r0, Operand(var->name()));
2604 __ Push(r1, r0);
2605 } else {
2606 // Non-global variable. Call the runtime to look up the context
2607 // where the variable was introduced.
2608 __ push(context_register());
2609 __ mov(r2, Operand(var->name()));
2610 __ push(r2);
2611 __ CallRuntime(Runtime::kLookupContext, 2);
2612 __ push(r0);
2613 __ mov(r2, Operand(var->name()));
2614 __ push(r2);
2615 }
2616 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
2617 Apply(context_, r0);
2618 }
2619 break;
2620 }
2621
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002622 case Token::VOID: {
2623 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2624 VisitForEffect(expr->expression());
2625 switch (context_) {
2626 case Expression::kUninitialized:
2627 UNREACHABLE();
2628 break;
2629 case Expression::kEffect:
2630 break;
2631 case Expression::kValue:
2632 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2633 switch (location_) {
2634 case kAccumulator:
2635 break;
2636 case kStack:
2637 __ push(result_register());
2638 break;
2639 }
2640 break;
2641 case Expression::kTestValue:
2642 // Value is false so it's needed.
2643 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2644 switch (location_) {
2645 case kAccumulator:
2646 break;
2647 case kStack:
2648 __ push(result_register());
2649 break;
2650 }
2651 // Fall through.
2652 case Expression::kTest:
2653 case Expression::kValueTest:
2654 __ jmp(false_label_);
2655 break;
2656 }
2657 break;
2658 }
2659
2660 case Token::NOT: {
2661 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002662 Label materialize_true, materialize_false;
2663 Label* if_true = NULL;
2664 Label* if_false = NULL;
2665
2666 // Notice that the labels are swapped.
2667 PrepareTest(&materialize_true, &materialize_false, &if_false, &if_true);
2668
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002669 VisitForControl(expr->expression(), if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002670
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002671 Apply(context_, if_false, if_true); // Labels swapped.
2672 break;
2673 }
2674
2675 case Token::TYPEOF: {
2676 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
2677 VariableProxy* proxy = expr->expression()->AsVariableProxy();
2678 if (proxy != NULL &&
2679 !proxy->var()->is_this() &&
2680 proxy->var()->is_global()) {
2681 Comment cmnt(masm_, "Global variable");
2682 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002683 __ mov(r2, Operand(proxy->name()));
2684 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2685 // Use a regular load, not a contextual load, to avoid a reference
2686 // error.
2687 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002688 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002689 } else if (proxy != NULL &&
2690 proxy->var()->slot() != NULL &&
2691 proxy->var()->slot()->type() == Slot::LOOKUP) {
2692 __ mov(r0, Operand(proxy->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002693 __ Push(cp, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002694 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
2695 __ push(r0);
2696 } else {
2697 // This expression cannot throw a reference error at the top level.
2698 VisitForValue(expr->expression(), kStack);
2699 }
2700
2701 __ CallRuntime(Runtime::kTypeof, 1);
2702 Apply(context_, r0);
2703 break;
2704 }
2705
2706 case Token::ADD: {
2707 Comment cmt(masm_, "[ UnaryOperation (ADD)");
2708 VisitForValue(expr->expression(), kAccumulator);
2709 Label no_conversion;
2710 __ tst(result_register(), Operand(kSmiTagMask));
2711 __ b(eq, &no_conversion);
2712 __ push(r0);
2713 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2714 __ bind(&no_conversion);
2715 Apply(context_, result_register());
2716 break;
2717 }
2718
2719 case Token::SUB: {
2720 Comment cmt(masm_, "[ UnaryOperation (SUB)");
2721 bool overwrite =
2722 (expr->expression()->AsBinaryOperation() != NULL &&
2723 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
2724 GenericUnaryOpStub stub(Token::SUB, overwrite);
2725 // GenericUnaryOpStub expects the argument to be in the
2726 // accumulator register r0.
2727 VisitForValue(expr->expression(), kAccumulator);
2728 __ CallStub(&stub);
2729 Apply(context_, r0);
2730 break;
2731 }
2732
2733 case Token::BIT_NOT: {
2734 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
2735 bool overwrite =
2736 (expr->expression()->AsBinaryOperation() != NULL &&
2737 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
2738 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
2739 // GenericUnaryOpStub expects the argument to be in the
2740 // accumulator register r0.
2741 VisitForValue(expr->expression(), kAccumulator);
2742 // Avoid calling the stub for Smis.
2743 Label smi, done;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002744 __ BranchOnSmi(result_register(), &smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002745 // Non-smi: call stub leaving result in accumulator register.
2746 __ CallStub(&stub);
2747 __ b(&done);
2748 // Perform operation directly on Smis.
2749 __ bind(&smi);
2750 __ mvn(result_register(), Operand(result_register()));
2751 // Bit-clear inverted smi-tag.
2752 __ bic(result_register(), result_register(), Operand(kSmiTagMask));
2753 __ bind(&done);
2754 Apply(context_, result_register());
2755 break;
2756 }
2757
2758 default:
2759 UNREACHABLE();
2760 }
2761}
2762
2763
2764void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2765 Comment cmnt(masm_, "[ CountOperation");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002766 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2767 // as the left-hand side.
2768 if (!expr->expression()->IsValidLeftHandSide()) {
2769 VisitForEffect(expr->expression());
2770 return;
2771 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002772
2773 // Expression can only be a property, a global or a (parameter or local)
2774 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2775 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2776 LhsKind assign_type = VARIABLE;
2777 Property* prop = expr->expression()->AsProperty();
2778 // In case of a property we use the uninitialized expression context
2779 // of the key to detect a named property.
2780 if (prop != NULL) {
2781 assign_type =
2782 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2783 }
2784
2785 // Evaluate expression and get value.
2786 if (assign_type == VARIABLE) {
2787 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
2788 Location saved_location = location_;
2789 location_ = kAccumulator;
2790 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
2791 Expression::kValue);
2792 location_ = saved_location;
2793 } else {
2794 // Reserve space for result of postfix operation.
2795 if (expr->is_postfix() && context_ != Expression::kEffect) {
2796 __ mov(ip, Operand(Smi::FromInt(0)));
2797 __ push(ip);
2798 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002799 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002800 // Put the object both on the stack and in the accumulator.
2801 VisitForValue(prop->obj(), kAccumulator);
2802 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002803 EmitNamedPropertyLoad(prop);
2804 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002805 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002806 VisitForValue(prop->key(), kAccumulator);
2807 __ ldr(r1, MemOperand(sp, 0));
2808 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002809 EmitKeyedPropertyLoad(prop);
2810 }
2811 }
2812
2813 // Call ToNumber only if operand is not a smi.
2814 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002815 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002816 __ push(r0);
2817 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2818 __ bind(&no_conversion);
2819
2820 // Save result for postfix expressions.
2821 if (expr->is_postfix()) {
2822 switch (context_) {
2823 case Expression::kUninitialized:
2824 UNREACHABLE();
2825 case Expression::kEffect:
2826 // Do not save result.
2827 break;
2828 case Expression::kValue:
2829 case Expression::kTest:
2830 case Expression::kValueTest:
2831 case Expression::kTestValue:
2832 // Save the result on the stack. If we have a named or keyed property
2833 // we store the result under the receiver that is currently on top
2834 // of the stack.
2835 switch (assign_type) {
2836 case VARIABLE:
2837 __ push(r0);
2838 break;
2839 case NAMED_PROPERTY:
2840 __ str(r0, MemOperand(sp, kPointerSize));
2841 break;
2842 case KEYED_PROPERTY:
2843 __ str(r0, MemOperand(sp, 2 * kPointerSize));
2844 break;
2845 }
2846 break;
2847 }
2848 }
2849
2850
2851 // Inline smi case if we are in a loop.
2852 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002853 int count_value = expr->op() == Token::INC ? 1 : -1;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002854 if (loop_depth() > 0) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002855 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002856 __ b(vs, &stub_call);
2857 // We could eliminate this smi check if we split the code at
2858 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002859 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002860 __ bind(&stub_call);
2861 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002862 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002863 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002864 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00002865 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002866 __ CallStub(&stub);
2867 __ bind(&done);
2868
2869 // Store the value returned in r0.
2870 switch (assign_type) {
2871 case VARIABLE:
2872 if (expr->is_postfix()) {
2873 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002874 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002875 Expression::kEffect);
2876 // For all contexts except kEffect: We have the result on
2877 // top of the stack.
2878 if (context_ != Expression::kEffect) {
2879 ApplyTOS(context_);
2880 }
2881 } else {
2882 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002883 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002884 context_);
2885 }
2886 break;
2887 case NAMED_PROPERTY: {
2888 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00002889 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002890 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2891 __ Call(ic, RelocInfo::CODE_TARGET);
2892 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002893 if (context_ != Expression::kEffect) {
2894 ApplyTOS(context_);
2895 }
2896 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00002897 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002898 }
2899 break;
2900 }
2901 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002902 __ pop(r1); // Key.
2903 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002904 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
2905 __ Call(ic, RelocInfo::CODE_TARGET);
2906 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002907 if (context_ != Expression::kEffect) {
2908 ApplyTOS(context_);
2909 }
2910 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002911 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002912 }
2913 break;
2914 }
2915 }
2916}
2917
2918
2919void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
2920 Comment cmnt(masm_, "[ BinaryOperation");
2921 switch (expr->op()) {
2922 case Token::COMMA:
2923 VisitForEffect(expr->left());
2924 Visit(expr->right());
2925 break;
2926
2927 case Token::OR:
2928 case Token::AND:
2929 EmitLogicalOperation(expr);
2930 break;
2931
2932 case Token::ADD:
2933 case Token::SUB:
2934 case Token::DIV:
2935 case Token::MOD:
2936 case Token::MUL:
2937 case Token::BIT_OR:
2938 case Token::BIT_AND:
2939 case Token::BIT_XOR:
2940 case Token::SHL:
2941 case Token::SHR:
2942 case Token::SAR:
2943 VisitForValue(expr->left(), kStack);
2944 VisitForValue(expr->right(), kAccumulator);
2945 EmitBinaryOp(expr->op(), context_);
2946 break;
2947
2948 default:
2949 UNREACHABLE();
2950 }
2951}
2952
2953
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002954void FullCodeGenerator::EmitNullCompare(bool strict,
2955 Register obj,
2956 Register null_const,
2957 Label* if_true,
2958 Label* if_false,
2959 Register scratch) {
2960 __ cmp(obj, null_const);
2961 if (strict) {
2962 __ b(eq, if_true);
2963 } else {
2964 __ b(eq, if_true);
2965 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2966 __ cmp(obj, ip);
2967 __ b(eq, if_true);
2968 __ BranchOnSmi(obj, if_false);
2969 // It can be an undetectable object.
2970 __ ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
2971 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
2972 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
2973 __ b(ne, if_true);
2974 }
2975 __ jmp(if_false);
2976}
2977
2978
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002979void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
2980 Comment cmnt(masm_, "[ CompareOperation");
2981
2982 // Always perform the comparison for its control flow. Pack the result
2983 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002984
2985 Label materialize_true, materialize_false;
2986 Label* if_true = NULL;
2987 Label* if_false = NULL;
2988 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002989
2990 VisitForValue(expr->left(), kStack);
2991 switch (expr->op()) {
2992 case Token::IN:
2993 VisitForValue(expr->right(), kStack);
2994 __ InvokeBuiltin(Builtins::IN, CALL_JS);
2995 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
2996 __ cmp(r0, ip);
2997 __ b(eq, if_true);
2998 __ jmp(if_false);
2999 break;
3000
3001 case Token::INSTANCEOF: {
3002 VisitForValue(expr->right(), kStack);
3003 InstanceofStub stub;
3004 __ CallStub(&stub);
3005 __ tst(r0, r0);
3006 __ b(eq, if_true); // The stub returns 0 for true.
3007 __ jmp(if_false);
3008 break;
3009 }
3010
3011 default: {
3012 VisitForValue(expr->right(), kAccumulator);
3013 Condition cc = eq;
3014 bool strict = false;
3015 switch (expr->op()) {
3016 case Token::EQ_STRICT:
3017 strict = true;
3018 // Fall through
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003019 case Token::EQ: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003020 cc = eq;
3021 __ pop(r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003022 // If either operand is constant null we do a fast compare
3023 // against null.
3024 Literal* right_literal = expr->right()->AsLiteral();
3025 Literal* left_literal = expr->left()->AsLiteral();
3026 if (right_literal != NULL && right_literal->handle()->IsNull()) {
3027 EmitNullCompare(strict, r1, r0, if_true, if_false, r2);
3028 Apply(context_, if_true, if_false);
3029 return;
3030 } else if (left_literal != NULL && left_literal->handle()->IsNull()) {
3031 EmitNullCompare(strict, r0, r1, if_true, if_false, r2);
3032 Apply(context_, if_true, if_false);
3033 return;
3034 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003035 break;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003036 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003037 case Token::LT:
3038 cc = lt;
3039 __ pop(r1);
3040 break;
3041 case Token::GT:
3042 // Reverse left and right sides to obtain ECMA-262 conversion order.
3043 cc = lt;
3044 __ mov(r1, result_register());
3045 __ pop(r0);
3046 break;
3047 case Token::LTE:
3048 // Reverse left and right sides to obtain ECMA-262 conversion order.
3049 cc = ge;
3050 __ mov(r1, result_register());
3051 __ pop(r0);
3052 break;
3053 case Token::GTE:
3054 cc = ge;
3055 __ pop(r1);
3056 break;
3057 case Token::IN:
3058 case Token::INSTANCEOF:
3059 default:
3060 UNREACHABLE();
3061 }
3062
3063 // The comparison stub expects the smi vs. smi case to be handled
3064 // before it is called.
3065 Label slow_case;
3066 __ orr(r2, r0, Operand(r1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003067 __ BranchOnNotSmi(r2, &slow_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003068 __ cmp(r1, r0);
3069 __ b(cc, if_true);
3070 __ jmp(if_false);
3071
3072 __ bind(&slow_case);
3073 CompareStub stub(cc, strict);
3074 __ CallStub(&stub);
3075 __ cmp(r0, Operand(0));
3076 __ b(cc, if_true);
3077 __ jmp(if_false);
3078 }
3079 }
3080
3081 // Convert the result of the comparison into one expected for this
3082 // expression's context.
3083 Apply(context_, if_true, if_false);
3084}
3085
3086
3087void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3088 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3089 Apply(context_, r0);
3090}
3091
3092
3093Register FullCodeGenerator::result_register() { return r0; }
3094
3095
3096Register FullCodeGenerator::context_register() { return cp; }
3097
3098
3099void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3100 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3101 __ str(value, MemOperand(fp, frame_offset));
3102}
3103
3104
3105void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
3106 __ ldr(dst, CodeGenerator::ContextOperand(cp, context_index));
3107}
3108
3109
3110// ----------------------------------------------------------------------------
3111// Non-local control flow support.
3112
3113void FullCodeGenerator::EnterFinallyBlock() {
3114 ASSERT(!result_register().is(r1));
3115 // Store result register while executing finally block.
3116 __ push(result_register());
3117 // Cook return address in link register to stack (smi encoded Code* delta)
3118 __ sub(r1, lr, Operand(masm_->CodeObject()));
3119 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3120 ASSERT_EQ(0, kSmiTag);
3121 __ add(r1, r1, Operand(r1)); // Convert to smi.
3122 __ push(r1);
3123}
3124
3125
3126void FullCodeGenerator::ExitFinallyBlock() {
3127 ASSERT(!result_register().is(r1));
3128 // Restore result register from stack.
3129 __ pop(r1);
3130 // Uncook return address and return.
3131 __ pop(result_register());
3132 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3133 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3134 __ add(pc, r1, Operand(masm_->CodeObject()));
3135}
3136
3137
3138#undef __
3139
3140} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003141
3142#endif // V8_TARGET_ARCH_ARM