blob: c2f6ea96bdd1986e7c8fa5fd76430d8d4a6e08c9 [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
67 __ stm(db_w, sp, r1.bit() | cp.bit() | fp.bit() | lr.bit());
68 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.
ager@chromium.org5c838252010-02-19 08:53:10 +000085 if (scope()->num_heap_slots() > 0) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000086 Comment cmnt(masm_, "[ Allocate local context");
87 // Argument to NewContext is the function, which is in r1.
88 __ push(r1);
89 __ CallRuntime(Runtime::kNewContext, 1);
90 function_in_register = false;
91 // Context is returned in both r0 and cp. It replaces the context
92 // passed to us. It's saved in the stack and kept live in cp.
93 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
94 // Copy any necessary parameters into the context.
ager@chromium.org5c838252010-02-19 08:53:10 +000095 int num_parameters = scope()->num_parameters();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000096 for (int i = 0; i < num_parameters; i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +000097 Slot* slot = scope()->parameter(i)->slot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000098 if (slot != NULL && slot->type() == Slot::CONTEXT) {
99 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
100 (num_parameters - 1 - i) * kPointerSize;
101 // Load parameter from stack.
102 __ ldr(r0, MemOperand(fp, parameter_offset));
103 // Store it in the context.
104 __ mov(r1, Operand(Context::SlotOffset(slot->index())));
105 __ str(r0, MemOperand(cp, r1));
106 // Update the write barrier. This clobbers all involved
107 // registers, so we have use a third register to avoid
108 // clobbering cp.
109 __ mov(r2, Operand(cp));
110 __ RecordWrite(r2, r1, r0);
111 }
112 }
113 }
114
ager@chromium.org5c838252010-02-19 08:53:10 +0000115 Variable* arguments = scope()->arguments()->AsVariable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000116 if (arguments != NULL) {
117 // Function uses arguments object.
118 Comment cmnt(masm_, "[ Allocate arguments object");
119 if (!function_in_register) {
120 // Load this again, if it's used by the local context below.
121 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
122 } else {
123 __ mov(r3, r1);
124 }
125 // Receiver is just before the parameters on the caller's stack.
ager@chromium.org5c838252010-02-19 08:53:10 +0000126 int offset = scope()->num_parameters() * kPointerSize;
127 __ add(r2, fp,
128 Operand(StandardFrameConstants::kCallerSPOffset + offset));
129 __ mov(r1, Operand(Smi::FromInt(scope()->num_parameters())));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000130 __ Push(r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000131
132 // Arguments to ArgumentsAccessStub:
133 // function, receiver address, parameter count.
134 // The stub will rewrite receiever and parameter count if the previous
135 // stack frame was an arguments adapter frame.
136 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
137 __ CallStub(&stub);
138 // Duplicate the value; move-to-slot operation might clobber registers.
139 __ mov(r3, r0);
140 Move(arguments->slot(), r0, r1, r2);
141 Slot* dot_arguments_slot =
ager@chromium.org5c838252010-02-19 08:53:10 +0000142 scope()->arguments_shadow()->AsVariable()->slot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000143 Move(dot_arguments_slot, r3, r1, r2);
144 }
145 }
146
147 // Check the stack for overflow or break request.
148 // Put the lr setup instruction in the delay slot. The kInstrSize is
149 // added to the implicit 8 byte offset that always applies to operations
150 // with pc and gives a return address 12 bytes down.
151 { Comment cmnt(masm_, "[ Stack check");
152 __ LoadRoot(r2, Heap::kStackLimitRootIndex);
153 __ add(lr, pc, Operand(Assembler::kInstrSize));
154 __ cmp(sp, Operand(r2));
155 StackCheckStub stub;
156 __ mov(pc,
157 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
158 RelocInfo::CODE_TARGET),
159 LeaveCC,
160 lo);
161 }
162
163 { Comment cmnt(masm_, "[ Declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000164 VisitDeclarations(scope()->declarations());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000165 }
166
167 if (FLAG_trace) {
168 __ CallRuntime(Runtime::kTraceEnter, 0);
169 }
170
171 { Comment cmnt(masm_, "[ Body");
172 ASSERT(loop_depth() == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000173 VisitStatements(function()->body());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000174 ASSERT(loop_depth() == 0);
175 }
176
177 { Comment cmnt(masm_, "[ return <undefined>;");
178 // Emit a 'return undefined' in case control fell off the end of the
179 // body.
180 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
181 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000182 EmitReturnSequence(function()->end_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000183}
184
185
186void FullCodeGenerator::EmitReturnSequence(int position) {
187 Comment cmnt(masm_, "[ Return sequence");
188 if (return_label_.is_bound()) {
189 __ b(&return_label_);
190 } else {
191 __ bind(&return_label_);
192 if (FLAG_trace) {
193 // Push the return value on the stack as the parameter.
194 // Runtime::TraceExit returns its parameter in r0.
195 __ push(r0);
196 __ CallRuntime(Runtime::kTraceExit, 1);
197 }
198
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000199#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000200 // Add a label for checking the size of the code used for returning.
201 Label check_exit_codesize;
202 masm_->bind(&check_exit_codesize);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000203#endif
204 // Make sure that the constant pool is not emitted inside of the return
205 // sequence.
206 { Assembler::BlockConstPoolScope block_const_pool(masm_);
207 // Here we use masm_-> instead of the __ macro to avoid the code coverage
208 // tool from instrumenting as we rely on the code size here.
209 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
210 CodeGenerator::RecordPositions(masm_, position);
211 __ RecordJSReturn();
212 masm_->mov(sp, fp);
213 masm_->ldm(ia_w, sp, fp.bit() | lr.bit());
214 masm_->add(sp, sp, Operand(sp_delta));
215 masm_->Jump(lr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000216 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000217
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000218#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000219 // Check that the size of the code used for returning matches what is
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000220 // expected by the debugger. If the sp_delts above cannot be encoded in the
221 // add instruction the add will generate two instructions.
222 int return_sequence_length =
223 masm_->InstructionsGeneratedSince(&check_exit_codesize);
224 CHECK(return_sequence_length == Assembler::kJSReturnSequenceLength ||
225 return_sequence_length == Assembler::kJSReturnSequenceLength + 1);
226#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000227 }
228}
229
230
231void FullCodeGenerator::Apply(Expression::Context context, Register reg) {
232 switch (context) {
233 case Expression::kUninitialized:
234 UNREACHABLE();
235
236 case Expression::kEffect:
237 // Nothing to do.
238 break;
239
240 case Expression::kValue:
241 // Move value into place.
242 switch (location_) {
243 case kAccumulator:
244 if (!reg.is(result_register())) __ mov(result_register(), reg);
245 break;
246 case kStack:
247 __ push(reg);
248 break;
249 }
250 break;
251
252 case Expression::kValueTest:
253 case Expression::kTestValue:
254 // Push an extra copy of the value in case it's needed.
255 __ push(reg);
256 // Fall through.
257
258 case Expression::kTest:
259 // We always call the runtime on ARM, so push the value as argument.
260 __ push(reg);
261 DoTest(context);
262 break;
263 }
264}
265
266
267void FullCodeGenerator::Apply(Expression::Context context, Slot* slot) {
268 switch (context) {
269 case Expression::kUninitialized:
270 UNREACHABLE();
271 case Expression::kEffect:
272 // Nothing to do.
273 break;
274 case Expression::kValue:
275 case Expression::kTest:
276 case Expression::kValueTest:
277 case Expression::kTestValue:
278 // On ARM we have to move the value into a register to do anything
279 // with it.
280 Move(result_register(), slot);
281 Apply(context, result_register());
282 break;
283 }
284}
285
286
287void FullCodeGenerator::Apply(Expression::Context context, Literal* lit) {
288 switch (context) {
289 case Expression::kUninitialized:
290 UNREACHABLE();
291 case Expression::kEffect:
292 break;
293 // Nothing to do.
294 case Expression::kValue:
295 case Expression::kTest:
296 case Expression::kValueTest:
297 case Expression::kTestValue:
298 // On ARM we have to move the value into a register to do anything
299 // with it.
300 __ mov(result_register(), Operand(lit->handle()));
301 Apply(context, result_register());
302 break;
303 }
304}
305
306
307void FullCodeGenerator::ApplyTOS(Expression::Context context) {
308 switch (context) {
309 case Expression::kUninitialized:
310 UNREACHABLE();
311
312 case Expression::kEffect:
313 __ Drop(1);
314 break;
315
316 case Expression::kValue:
317 switch (location_) {
318 case kAccumulator:
319 __ pop(result_register());
320 break;
321 case kStack:
322 break;
323 }
324 break;
325
326 case Expression::kValueTest:
327 case Expression::kTestValue:
328 // Duplicate the value on the stack in case it's needed.
329 __ ldr(ip, MemOperand(sp));
330 __ push(ip);
331 // Fall through.
332
333 case Expression::kTest:
334 DoTest(context);
335 break;
336 }
337}
338
339
340void FullCodeGenerator::DropAndApply(int count,
341 Expression::Context context,
342 Register reg) {
343 ASSERT(count > 0);
344 ASSERT(!reg.is(sp));
345 switch (context) {
346 case Expression::kUninitialized:
347 UNREACHABLE();
348
349 case Expression::kEffect:
350 __ Drop(count);
351 break;
352
353 case Expression::kValue:
354 switch (location_) {
355 case kAccumulator:
356 __ Drop(count);
357 if (!reg.is(result_register())) __ mov(result_register(), reg);
358 break;
359 case kStack:
360 if (count > 1) __ Drop(count - 1);
361 __ str(reg, MemOperand(sp));
362 break;
363 }
364 break;
365
366 case Expression::kTest:
367 if (count > 1) __ Drop(count - 1);
368 __ str(reg, MemOperand(sp));
369 DoTest(context);
370 break;
371
372 case Expression::kValueTest:
373 case Expression::kTestValue:
374 if (count == 1) {
375 __ str(reg, MemOperand(sp));
376 __ push(reg);
377 } else { // count > 1
378 __ Drop(count - 2);
379 __ str(reg, MemOperand(sp, kPointerSize));
380 __ str(reg, MemOperand(sp));
381 }
382 DoTest(context);
383 break;
384 }
385}
386
387
388void FullCodeGenerator::Apply(Expression::Context context,
389 Label* materialize_true,
390 Label* materialize_false) {
391 switch (context) {
392 case Expression::kUninitialized:
393
394 case Expression::kEffect:
395 ASSERT_EQ(materialize_true, materialize_false);
396 __ bind(materialize_true);
397 break;
398
399 case Expression::kValue: {
400 Label done;
401 __ bind(materialize_true);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000402 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000403 __ jmp(&done);
404 __ bind(materialize_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000405 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000406 __ bind(&done);
407 switch (location_) {
408 case kAccumulator:
409 break;
410 case kStack:
411 __ push(result_register());
412 break;
413 }
414 break;
415 }
416
417 case Expression::kTest:
418 break;
419
420 case Expression::kValueTest:
421 __ bind(materialize_true);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000422 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000423 switch (location_) {
424 case kAccumulator:
425 break;
426 case kStack:
427 __ push(result_register());
428 break;
429 }
430 __ jmp(true_label_);
431 break;
432
433 case Expression::kTestValue:
434 __ bind(materialize_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000435 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000436 switch (location_) {
437 case kAccumulator:
438 break;
439 case kStack:
440 __ push(result_register());
441 break;
442 }
443 __ jmp(false_label_);
444 break;
445 }
446}
447
448
449void FullCodeGenerator::DoTest(Expression::Context context) {
450 // The value to test is pushed on the stack, and duplicated on the stack
451 // if necessary (for value/test and test/value contexts).
452 ASSERT_NE(NULL, true_label_);
453 ASSERT_NE(NULL, false_label_);
454
455 // Call the runtime to find the boolean value of the source and then
456 // translate it into control flow to the pair of labels.
457 __ CallRuntime(Runtime::kToBool, 1);
458 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
459 __ cmp(r0, ip);
460
461 // Complete based on the context.
462 switch (context) {
463 case Expression::kUninitialized:
464 case Expression::kEffect:
465 case Expression::kValue:
466 UNREACHABLE();
467
468 case Expression::kTest:
469 __ b(eq, true_label_);
470 __ jmp(false_label_);
471 break;
472
473 case Expression::kValueTest: {
474 Label discard;
475 switch (location_) {
476 case kAccumulator:
477 __ b(ne, &discard);
478 __ pop(result_register());
479 __ jmp(true_label_);
480 break;
481 case kStack:
482 __ b(eq, true_label_);
483 break;
484 }
485 __ bind(&discard);
486 __ Drop(1);
487 __ jmp(false_label_);
488 break;
489 }
490
491 case Expression::kTestValue: {
492 Label discard;
493 switch (location_) {
494 case kAccumulator:
495 __ b(eq, &discard);
496 __ pop(result_register());
497 __ jmp(false_label_);
498 break;
499 case kStack:
500 __ b(ne, false_label_);
501 break;
502 }
503 __ bind(&discard);
504 __ Drop(1);
505 __ jmp(true_label_);
506 break;
507 }
508 }
509}
510
511
512MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
513 switch (slot->type()) {
514 case Slot::PARAMETER:
515 case Slot::LOCAL:
516 return MemOperand(fp, SlotOffset(slot));
517 case Slot::CONTEXT: {
518 int context_chain_length =
ager@chromium.org5c838252010-02-19 08:53:10 +0000519 scope()->ContextChainLength(slot->var()->scope());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000520 __ LoadContext(scratch, context_chain_length);
521 return CodeGenerator::ContextOperand(scratch, slot->index());
522 }
523 case Slot::LOOKUP:
524 UNREACHABLE();
525 }
526 UNREACHABLE();
527 return MemOperand(r0, 0);
528}
529
530
531void FullCodeGenerator::Move(Register destination, Slot* source) {
532 // Use destination as scratch.
533 MemOperand slot_operand = EmitSlotSearch(source, destination);
534 __ ldr(destination, slot_operand);
535}
536
537
538void FullCodeGenerator::Move(Slot* dst,
539 Register src,
540 Register scratch1,
541 Register scratch2) {
542 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
543 ASSERT(!scratch1.is(src) && !scratch2.is(src));
544 MemOperand location = EmitSlotSearch(dst, scratch1);
545 __ str(src, location);
546 // Emit the write barrier code if the location is in the heap.
547 if (dst->type() == Slot::CONTEXT) {
548 __ mov(scratch2, Operand(Context::SlotOffset(dst->index())));
549 __ RecordWrite(scratch1, scratch2, src);
550 }
551}
552
553
554void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
555 Comment cmnt(masm_, "[ Declaration");
556 Variable* var = decl->proxy()->var();
557 ASSERT(var != NULL); // Must have been resolved.
558 Slot* slot = var->slot();
559 Property* prop = var->AsProperty();
560
561 if (slot != NULL) {
562 switch (slot->type()) {
563 case Slot::PARAMETER:
564 case Slot::LOCAL:
565 if (decl->mode() == Variable::CONST) {
566 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
567 __ str(ip, MemOperand(fp, SlotOffset(slot)));
568 } else if (decl->fun() != NULL) {
569 VisitForValue(decl->fun(), kAccumulator);
570 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
571 }
572 break;
573
574 case Slot::CONTEXT:
575 // We bypass the general EmitSlotSearch because we know more about
576 // this specific context.
577
578 // The variable in the decl always resides in the current context.
ager@chromium.org5c838252010-02-19 08:53:10 +0000579 ASSERT_EQ(0, scope()->ContextChainLength(var->scope()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000580 if (FLAG_debug_code) {
581 // Check if we have the correct context pointer.
582 __ ldr(r1,
583 CodeGenerator::ContextOperand(cp, Context::FCONTEXT_INDEX));
584 __ cmp(r1, cp);
585 __ Check(eq, "Unexpected declaration in current context.");
586 }
587 if (decl->mode() == Variable::CONST) {
588 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
589 __ str(ip, CodeGenerator::ContextOperand(cp, slot->index()));
590 // No write barrier since the_hole_value is in old space.
591 } else if (decl->fun() != NULL) {
592 VisitForValue(decl->fun(), kAccumulator);
593 __ str(result_register(),
594 CodeGenerator::ContextOperand(cp, slot->index()));
595 int offset = Context::SlotOffset(slot->index());
596 __ mov(r2, Operand(offset));
597 // We know that we have written a function, which is not a smi.
598 __ mov(r1, Operand(cp));
599 __ RecordWrite(r1, r2, result_register());
600 }
601 break;
602
603 case Slot::LOOKUP: {
604 __ mov(r2, Operand(var->name()));
605 // Declaration nodes are always introduced in one of two modes.
606 ASSERT(decl->mode() == Variable::VAR ||
607 decl->mode() == Variable::CONST);
608 PropertyAttributes attr =
609 (decl->mode() == Variable::VAR) ? NONE : READ_ONLY;
610 __ mov(r1, Operand(Smi::FromInt(attr)));
611 // Push initial value, if any.
612 // Note: For variables we must not push an initial value (such as
613 // 'undefined') because we may have a (legal) redeclaration and we
614 // must not destroy the current value.
615 if (decl->mode() == Variable::CONST) {
616 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
617 __ stm(db_w, sp, cp.bit() | r2.bit() | r1.bit() | r0.bit());
618 } else if (decl->fun() != NULL) {
619 __ stm(db_w, sp, cp.bit() | r2.bit() | r1.bit());
620 // Push initial value for function declaration.
621 VisitForValue(decl->fun(), kStack);
622 } else {
623 __ mov(r0, Operand(Smi::FromInt(0))); // No initial value!
624 __ stm(db_w, sp, cp.bit() | r2.bit() | r1.bit() | r0.bit());
625 }
626 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
627 break;
628 }
629 }
630
631 } else if (prop != NULL) {
632 if (decl->fun() != NULL || decl->mode() == Variable::CONST) {
633 // We are declaring a function or constant that rewrites to a
634 // property. Use (keyed) IC to set the initial value.
635 VisitForValue(prop->obj(), kStack);
636 VisitForValue(prop->key(), kStack);
637
638 if (decl->fun() != NULL) {
639 VisitForValue(decl->fun(), kAccumulator);
640 } else {
641 __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex);
642 }
643
644 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000645 __ pop(r1); // Key.
646 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000647 __ Call(ic, RelocInfo::CODE_TARGET);
648
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000649 // Value in r0 is ignored (declarations are statements).
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000650 }
651 }
652}
653
654
655void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
656 // Call the runtime to declare the globals.
657 // The context is the first argument.
658 __ mov(r1, Operand(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000659 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000660 __ stm(db_w, sp, cp.bit() | r1.bit() | r0.bit());
661 __ CallRuntime(Runtime::kDeclareGlobals, 3);
662 // Return value is ignored.
663}
664
665
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000666void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
667 UNREACHABLE();
668}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000669
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000670
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000671void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
672 UNREACHABLE();
673}
674
675
676void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info) {
677 // Use the fast case closure allocation code that allocates in new
678 // space for nested functions that don't need literals cloning.
679 if (scope()->is_function_scope() && info->num_literals() == 0) {
680 FastNewClosureStub stub;
681 __ mov(r0, Operand(info));
682 __ push(r0);
683 __ CallStub(&stub);
684 } else {
685 __ mov(r0, Operand(info));
686 __ stm(db_w, sp, cp.bit() | r0.bit());
687 __ CallRuntime(Runtime::kNewClosure, 2);
688 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000689 Apply(context_, r0);
690}
691
692
693void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
694 Comment cmnt(masm_, "[ VariableProxy");
695 EmitVariableLoad(expr->var(), context_);
696}
697
698
699void FullCodeGenerator::EmitVariableLoad(Variable* var,
700 Expression::Context context) {
701 // Four cases: non-this global variables, lookup slots, all other
702 // types of slots, and parameters that rewrite to explicit property
703 // accesses on the arguments object.
704 Slot* slot = var->slot();
705 Property* property = var->AsProperty();
706
707 if (var->is_global() && !var->is_this()) {
708 Comment cmnt(masm_, "Global variable");
709 // Use inline caching. Variable name is passed in r2 and the global
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000710 // object (receiver) in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000711 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000712 __ mov(r2, Operand(var->name()));
713 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
714 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000715 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000716
717 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
718 Comment cmnt(masm_, "Lookup slot");
719 __ mov(r1, Operand(var->name()));
720 __ stm(db_w, sp, cp.bit() | r1.bit()); // Context and name.
721 __ CallRuntime(Runtime::kLoadContextSlot, 2);
722 Apply(context, r0);
723
724 } else if (slot != NULL) {
725 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
726 ? "Context slot"
727 : "Stack slot");
728 Apply(context, slot);
729
730 } else {
731 Comment cmnt(masm_, "Rewritten parameter");
732 ASSERT_NOT_NULL(property);
733 // Rewritten parameter accesses are of the form "slot[literal]".
734
735 // Assert that the object is in a slot.
736 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
737 ASSERT_NOT_NULL(object_var);
738 Slot* object_slot = object_var->slot();
739 ASSERT_NOT_NULL(object_slot);
740
741 // Load the object.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000742 Move(r1, object_slot);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000743
744 // Assert that the key is a smi.
745 Literal* key_literal = property->key()->AsLiteral();
746 ASSERT_NOT_NULL(key_literal);
747 ASSERT(key_literal->handle()->IsSmi());
748
749 // Load the key.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000750 __ mov(r0, Operand(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000751
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000752 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000753 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
754 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000755 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000756 }
757}
758
759
760void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
761 Comment cmnt(masm_, "[ RegExpLiteral");
762 Label done;
763 // Registers will be used as follows:
764 // r4 = JS function, literals array
765 // r3 = literal index
766 // r2 = RegExp pattern
767 // r1 = RegExp flags
768 // r0 = temp + return value (RegExp literal)
769 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
770 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
771 int literal_offset =
772 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
773 __ ldr(r0, FieldMemOperand(r4, literal_offset));
774 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
775 __ cmp(r0, ip);
776 __ b(ne, &done);
777 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
778 __ mov(r2, Operand(expr->pattern()));
779 __ mov(r1, Operand(expr->flags()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000780 __ Push(r4, r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000781 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
782 __ bind(&done);
783 Apply(context_, r0);
784}
785
786
787void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
788 Comment cmnt(masm_, "[ ObjectLiteral");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000789 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
790 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
791 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
792 __ mov(r1, Operand(expr->constant_properties()));
793 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000794 __ Push(r3, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000795 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000796 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000797 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000798 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000799 }
800
801 // If result_saved is true the result is on top of the stack. If
802 // result_saved is false the result is in r0.
803 bool result_saved = false;
804
805 for (int i = 0; i < expr->properties()->length(); i++) {
806 ObjectLiteral::Property* property = expr->properties()->at(i);
807 if (property->IsCompileTimeValue()) continue;
808
809 Literal* key = property->key();
810 Expression* value = property->value();
811 if (!result_saved) {
812 __ push(r0); // Save result on stack
813 result_saved = true;
814 }
815 switch (property->kind()) {
816 case ObjectLiteral::Property::CONSTANT:
817 UNREACHABLE();
818 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
819 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
820 // Fall through.
821 case ObjectLiteral::Property::COMPUTED:
822 if (key->handle()->IsSymbol()) {
823 VisitForValue(value, kAccumulator);
824 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000825 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000826 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
827 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000828 break;
829 }
830 // Fall through.
831 case ObjectLiteral::Property::PROTOTYPE:
832 // Duplicate receiver on stack.
833 __ ldr(r0, MemOperand(sp));
834 __ push(r0);
835 VisitForValue(key, kStack);
836 VisitForValue(value, kStack);
837 __ CallRuntime(Runtime::kSetProperty, 3);
838 break;
839 case ObjectLiteral::Property::GETTER:
840 case ObjectLiteral::Property::SETTER:
841 // Duplicate receiver on stack.
842 __ ldr(r0, MemOperand(sp));
843 __ push(r0);
844 VisitForValue(key, kStack);
845 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
846 Smi::FromInt(1) :
847 Smi::FromInt(0)));
848 __ push(r1);
849 VisitForValue(value, kStack);
850 __ CallRuntime(Runtime::kDefineAccessor, 4);
851 break;
852 }
853 }
854
855 if (result_saved) {
856 ApplyTOS(context_);
857 } else {
858 Apply(context_, r0);
859 }
860}
861
862
863void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
864 Comment cmnt(masm_, "[ ArrayLiteral");
865 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
866 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
867 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
868 __ mov(r1, Operand(expr->constant_elements()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000869 __ Push(r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000870 if (expr->depth() > 1) {
871 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
872 } else {
873 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
874 }
875
876 bool result_saved = false; // Is the result saved to the stack?
877
878 // Emit code to evaluate all the non-constant subexpressions and to store
879 // them into the newly cloned array.
880 ZoneList<Expression*>* subexprs = expr->values();
881 for (int i = 0, len = subexprs->length(); i < len; i++) {
882 Expression* subexpr = subexprs->at(i);
883 // If the subexpression is a literal or a simple materialized literal it
884 // is already set in the cloned array.
885 if (subexpr->AsLiteral() != NULL ||
886 CompileTimeValue::IsCompileTimeValue(subexpr)) {
887 continue;
888 }
889
890 if (!result_saved) {
891 __ push(r0);
892 result_saved = true;
893 }
894 VisitForValue(subexpr, kAccumulator);
895
896 // Store the subexpression value in the array's elements.
897 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
898 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
899 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
900 __ str(result_register(), FieldMemOperand(r1, offset));
901
902 // Update the write barrier for the array store with r0 as the scratch
903 // register.
904 __ mov(r2, Operand(offset));
905 __ RecordWrite(r1, r2, result_register());
906 }
907
908 if (result_saved) {
909 ApplyTOS(context_);
910 } else {
911 Apply(context_, r0);
912 }
913}
914
915
ager@chromium.org5c838252010-02-19 08:53:10 +0000916void FullCodeGenerator::VisitAssignment(Assignment* expr) {
917 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000918 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
919 // on the left-hand side.
920 if (!expr->target()->IsValidLeftHandSide()) {
921 VisitForEffect(expr->target());
922 return;
923 }
924
ager@chromium.org5c838252010-02-19 08:53:10 +0000925 // Left-hand side can only be a property, a global or a (parameter or local)
926 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
927 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
928 LhsKind assign_type = VARIABLE;
929 Property* prop = expr->target()->AsProperty();
930 if (prop != NULL) {
931 assign_type =
932 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
933 }
934
935 // Evaluate LHS expression.
936 switch (assign_type) {
937 case VARIABLE:
938 // Nothing to do here.
939 break;
940 case NAMED_PROPERTY:
941 if (expr->is_compound()) {
942 // We need the receiver both on the stack and in the accumulator.
943 VisitForValue(prop->obj(), kAccumulator);
944 __ push(result_register());
945 } else {
946 VisitForValue(prop->obj(), kStack);
947 }
948 break;
949 case KEYED_PROPERTY:
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000950 // We need the key and receiver on both the stack and in r0 and r1.
951 if (expr->is_compound()) {
952 VisitForValue(prop->obj(), kStack);
953 VisitForValue(prop->key(), kAccumulator);
954 __ ldr(r1, MemOperand(sp, 0));
955 __ push(r0);
956 } else {
957 VisitForValue(prop->obj(), kStack);
958 VisitForValue(prop->key(), kStack);
959 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000960 break;
961 }
962
963 // If we have a compound assignment: Get value of LHS expression and
964 // store in on top of the stack.
965 if (expr->is_compound()) {
966 Location saved_location = location_;
967 location_ = kStack;
968 switch (assign_type) {
969 case VARIABLE:
970 EmitVariableLoad(expr->target()->AsVariableProxy()->var(),
971 Expression::kValue);
972 break;
973 case NAMED_PROPERTY:
974 EmitNamedPropertyLoad(prop);
975 __ push(result_register());
976 break;
977 case KEYED_PROPERTY:
978 EmitKeyedPropertyLoad(prop);
979 __ push(result_register());
980 break;
981 }
982 location_ = saved_location;
983 }
984
985 // Evaluate RHS expression.
986 Expression* rhs = expr->value();
987 VisitForValue(rhs, kAccumulator);
988
989 // If we have a compound assignment: Apply operator.
990 if (expr->is_compound()) {
991 Location saved_location = location_;
992 location_ = kAccumulator;
993 EmitBinaryOp(expr->binary_op(), Expression::kValue);
994 location_ = saved_location;
995 }
996
997 // Record source position before possible IC call.
998 SetSourcePosition(expr->position());
999
1000 // Store the value.
1001 switch (assign_type) {
1002 case VARIABLE:
1003 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001004 expr->op(),
ager@chromium.org5c838252010-02-19 08:53:10 +00001005 context_);
1006 break;
1007 case NAMED_PROPERTY:
1008 EmitNamedPropertyAssignment(expr);
1009 break;
1010 case KEYED_PROPERTY:
1011 EmitKeyedPropertyAssignment(expr);
1012 break;
1013 }
1014}
1015
1016
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001017void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1018 SetSourcePosition(prop->position());
1019 Literal* key = prop->key()->AsLiteral();
1020 __ mov(r2, Operand(key->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001021 // Call load IC. It has arguments receiver and property name r0 and r2.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001022 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1023 __ Call(ic, RelocInfo::CODE_TARGET);
1024}
1025
1026
1027void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1028 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001029 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001030 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1031 __ Call(ic, RelocInfo::CODE_TARGET);
1032}
1033
1034
1035void FullCodeGenerator::EmitBinaryOp(Token::Value op,
1036 Expression::Context context) {
1037 __ pop(r1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001038 GenericBinaryOpStub stub(op, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001039 __ CallStub(&stub);
1040 Apply(context, r0);
1041}
1042
1043
1044void FullCodeGenerator::EmitVariableAssignment(Variable* var,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001045 Token::Value op,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001046 Expression::Context context) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001047 // Left-hand sides that rewrite to explicit property accesses do not reach
1048 // here.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001049 ASSERT(var != NULL);
1050 ASSERT(var->is_global() || var->slot() != NULL);
1051
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001052 if (var->is_global()) {
1053 ASSERT(!var->is_this());
1054 // Assignment to a global variable. Use inline caching for the
1055 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001056 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001057 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001058 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001059 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1060 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001061
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001062 } else if (var->mode() != Variable::CONST || op == Token::INIT_CONST) {
1063 // Perform the assignment for non-const variables and for initialization
1064 // of const variables. Const assignments are simply skipped.
1065 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001066 Slot* slot = var->slot();
1067 switch (slot->type()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001068 case Slot::PARAMETER:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001069 case Slot::LOCAL:
1070 if (op == Token::INIT_CONST) {
1071 // Detect const reinitialization by checking for the hole value.
1072 __ ldr(r1, MemOperand(fp, SlotOffset(slot)));
1073 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1074 __ cmp(r1, ip);
1075 __ b(ne, &done);
1076 }
1077 // Perform the assignment.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001078 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1079 break;
1080
1081 case Slot::CONTEXT: {
1082 MemOperand target = EmitSlotSearch(slot, r1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001083 if (op == Token::INIT_CONST) {
1084 // Detect const reinitialization by checking for the hole value.
1085 __ ldr(r1, target);
1086 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1087 __ cmp(r1, ip);
1088 __ b(ne, &done);
1089 }
1090 // Perform the assignment and issue the write barrier.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001091 __ str(result_register(), target);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001092 // RecordWrite may destroy all its register arguments.
1093 __ mov(r3, result_register());
1094 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001095 __ mov(r2, Operand(offset));
1096 __ RecordWrite(r1, r2, r3);
1097 break;
1098 }
1099
1100 case Slot::LOOKUP:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001101 // Call the runtime for the assignment. The runtime will ignore
1102 // const reinitialization.
1103 __ push(r0); // Value.
1104 __ mov(r0, Operand(slot->var()->name()));
1105 __ Push(cp, r0); // Context and name.
1106 if (op == Token::INIT_CONST) {
1107 // The runtime will ignore const redeclaration.
1108 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1109 } else {
1110 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1111 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001112 break;
1113 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001114 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001115 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001116
ager@chromium.org5c838252010-02-19 08:53:10 +00001117 Apply(context, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001118}
1119
1120
1121void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1122 // Assignment to a property, using a named store IC.
1123 Property* prop = expr->target()->AsProperty();
1124 ASSERT(prop != NULL);
1125 ASSERT(prop->key()->AsLiteral() != NULL);
1126
1127 // If the assignment starts a block of assignments to the same object,
1128 // change to slow case to avoid the quadratic behavior of repeatedly
1129 // adding fast properties.
1130 if (expr->starts_initialization_block()) {
1131 __ push(result_register());
1132 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1133 __ push(ip);
1134 __ CallRuntime(Runtime::kToSlowProperties, 1);
1135 __ pop(result_register());
1136 }
1137
1138 // Record source code position before IC call.
1139 SetSourcePosition(expr->position());
1140 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001141 // Load receiver to r1. Leave a copy in the stack if needed for turning the
1142 // receiver into fast case.
ager@chromium.org5c838252010-02-19 08:53:10 +00001143 if (expr->ends_initialization_block()) {
1144 __ ldr(r1, MemOperand(sp));
1145 } else {
1146 __ pop(r1);
1147 }
1148
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001149 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1150 __ Call(ic, RelocInfo::CODE_TARGET);
1151
1152 // If the assignment ends an initialization block, revert to fast case.
1153 if (expr->ends_initialization_block()) {
1154 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001155 // Receiver is under the result value.
1156 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001157 __ push(ip);
1158 __ CallRuntime(Runtime::kToFastProperties, 1);
1159 __ pop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001160 DropAndApply(1, context_, r0);
1161 } else {
1162 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001163 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001164}
1165
1166
1167void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1168 // Assignment to a property, using a keyed store IC.
1169
1170 // If the assignment starts a block of assignments to the same object,
1171 // change to slow case to avoid the quadratic behavior of repeatedly
1172 // adding fast properties.
1173 if (expr->starts_initialization_block()) {
1174 __ push(result_register());
1175 // Receiver is now under the key and value.
1176 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1177 __ push(ip);
1178 __ CallRuntime(Runtime::kToSlowProperties, 1);
1179 __ pop(result_register());
1180 }
1181
1182 // Record source code position before IC call.
1183 SetSourcePosition(expr->position());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001184 __ pop(r1); // Key.
1185 // Load receiver to r2. Leave a copy in the stack if needed for turning the
1186 // receiver into fast case.
1187 if (expr->ends_initialization_block()) {
1188 __ ldr(r2, MemOperand(sp));
1189 } else {
1190 __ pop(r2);
1191 }
1192
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001193 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1194 __ Call(ic, RelocInfo::CODE_TARGET);
1195
1196 // If the assignment ends an initialization block, revert to fast case.
1197 if (expr->ends_initialization_block()) {
1198 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001199 // Receiver is under the result value.
1200 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001201 __ push(ip);
1202 __ CallRuntime(Runtime::kToFastProperties, 1);
1203 __ pop(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001204 DropAndApply(1, context_, r0);
1205 } else {
1206 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001207 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001208}
1209
1210
1211void FullCodeGenerator::VisitProperty(Property* expr) {
1212 Comment cmnt(masm_, "[ Property");
1213 Expression* key = expr->key();
1214
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001215 if (key->IsPropertyName()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001216 VisitForValue(expr->obj(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001217 EmitNamedPropertyLoad(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001218 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001219 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001220 VisitForValue(expr->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001221 VisitForValue(expr->key(), kAccumulator);
1222 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001223 EmitKeyedPropertyLoad(expr);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001224 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001225 }
1226}
1227
1228void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001229 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001230 RelocInfo::Mode mode) {
1231 // Code common for calls using the IC.
1232 ZoneList<Expression*>* args = expr->arguments();
1233 int arg_count = args->length();
1234 for (int i = 0; i < arg_count; i++) {
1235 VisitForValue(args->at(i), kStack);
1236 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001237 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001238 // Record source position for debugger.
1239 SetSourcePosition(expr->position());
1240 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001241 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1242 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001243 __ Call(ic, mode);
1244 // Restore context register.
1245 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001246 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001247}
1248
1249
1250void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1251 // Code common for calls using the call stub.
1252 ZoneList<Expression*>* args = expr->arguments();
1253 int arg_count = args->length();
1254 for (int i = 0; i < arg_count; i++) {
1255 VisitForValue(args->at(i), kStack);
1256 }
1257 // Record source position for debugger.
1258 SetSourcePosition(expr->position());
1259 CallFunctionStub stub(arg_count, NOT_IN_LOOP, RECEIVER_MIGHT_BE_VALUE);
1260 __ CallStub(&stub);
1261 // Restore context register.
1262 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001263 DropAndApply(1, context_, r0);
1264}
1265
1266
1267void FullCodeGenerator::VisitCall(Call* expr) {
1268 Comment cmnt(masm_, "[ Call");
1269 Expression* fun = expr->expression();
1270 Variable* var = fun->AsVariableProxy()->AsVariable();
1271
1272 if (var != NULL && var->is_possibly_eval()) {
1273 // Call to the identifier 'eval'.
1274 UNREACHABLE();
1275 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001276 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001277 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001278 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001279 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1280 } else if (var != NULL && var->slot() != NULL &&
1281 var->slot()->type() == Slot::LOOKUP) {
1282 // Call to a lookup slot.
1283 UNREACHABLE();
1284 } else if (fun->AsProperty() != NULL) {
1285 // Call to an object property.
1286 Property* prop = fun->AsProperty();
1287 Literal* key = prop->key()->AsLiteral();
1288 if (key != NULL && key->handle()->IsSymbol()) {
1289 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001290 VisitForValue(prop->obj(), kStack);
1291 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1292 } else {
1293 // Call to a keyed property, use keyed load IC followed by function
1294 // call.
1295 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001296 VisitForValue(prop->key(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001297 // Record source code position for IC call.
1298 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001299 if (prop->is_synthetic()) {
1300 __ pop(r1); // We do not need to keep the receiver.
1301 } else {
1302 __ ldr(r1, MemOperand(sp, 0)); // Keep receiver, to call function on.
1303 }
1304
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001305 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1306 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001307 if (prop->is_synthetic()) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001308 // Push result (function).
1309 __ push(r0);
1310 // Push Global receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001311 __ ldr(r1, CodeGenerator::GlobalObject());
1312 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001313 __ push(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001314 } else {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001315 // Pop receiver.
1316 __ pop(r1);
1317 // Push result (function).
1318 __ push(r0);
1319 __ push(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001320 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001321 EmitCallWithStub(expr);
1322 }
1323 } else {
1324 // Call to some other expression. If the expression is an anonymous
1325 // function literal not called in a loop, mark it as one that should
1326 // also use the fast code generator.
1327 FunctionLiteral* lit = fun->AsFunctionLiteral();
1328 if (lit != NULL &&
1329 lit->name()->Equals(Heap::empty_string()) &&
1330 loop_depth() == 0) {
1331 lit->set_try_full_codegen(true);
1332 }
1333 VisitForValue(fun, kStack);
1334 // Load global receiver object.
1335 __ ldr(r1, CodeGenerator::GlobalObject());
1336 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1337 __ push(r1);
1338 // Emit function call.
1339 EmitCallWithStub(expr);
1340 }
1341}
1342
1343
1344void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1345 Comment cmnt(masm_, "[ CallNew");
1346 // According to ECMA-262, section 11.2.2, page 44, the function
1347 // expression in new calls must be evaluated before the
1348 // arguments.
1349 // Push function on the stack.
1350 VisitForValue(expr->expression(), kStack);
1351
1352 // Push global object (receiver).
1353 __ ldr(r0, CodeGenerator::GlobalObject());
1354 __ push(r0);
1355 // Push the arguments ("left-to-right") on the stack.
1356 ZoneList<Expression*>* args = expr->arguments();
1357 int arg_count = args->length();
1358 for (int i = 0; i < arg_count; i++) {
1359 VisitForValue(args->at(i), kStack);
1360 }
1361
1362 // Call the construct call builtin that handles allocation and
1363 // constructor invocation.
1364 SetSourcePosition(expr->position());
1365
1366 // Load function, arg_count into r1 and r0.
1367 __ mov(r0, Operand(arg_count));
1368 // Function is in sp[arg_count + 1].
1369 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1370
1371 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1372 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
1373
1374 // Replace function on TOS with result in r0, or pop it.
1375 DropAndApply(1, context_, r0);
1376}
1377
1378
1379void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
1380 Comment cmnt(masm_, "[ CallRuntime");
1381 ZoneList<Expression*>* args = expr->arguments();
1382
1383 if (expr->is_jsruntime()) {
1384 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001385 __ ldr(r0, CodeGenerator::GlobalObject());
1386 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001387 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001388 }
1389
1390 // Push the arguments ("left-to-right").
1391 int arg_count = args->length();
1392 for (int i = 0; i < arg_count; i++) {
1393 VisitForValue(args->at(i), kStack);
1394 }
1395
1396 if (expr->is_jsruntime()) {
1397 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00001398 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001399 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
1400 NOT_IN_LOOP);
1401 __ Call(ic, RelocInfo::CODE_TARGET);
1402 // Restore context register.
1403 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001404 } else {
1405 // Call the C runtime function.
1406 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001407 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001408 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001409}
1410
1411
1412void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
1413 switch (expr->op()) {
1414 case Token::VOID: {
1415 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
1416 VisitForEffect(expr->expression());
1417 switch (context_) {
1418 case Expression::kUninitialized:
1419 UNREACHABLE();
1420 break;
1421 case Expression::kEffect:
1422 break;
1423 case Expression::kValue:
1424 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
1425 switch (location_) {
1426 case kAccumulator:
1427 break;
1428 case kStack:
1429 __ push(result_register());
1430 break;
1431 }
1432 break;
1433 case Expression::kTestValue:
1434 // Value is false so it's needed.
1435 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
1436 switch (location_) {
1437 case kAccumulator:
1438 break;
1439 case kStack:
1440 __ push(result_register());
1441 break;
1442 }
1443 // Fall through.
1444 case Expression::kTest:
1445 case Expression::kValueTest:
1446 __ jmp(false_label_);
1447 break;
1448 }
1449 break;
1450 }
1451
1452 case Token::NOT: {
1453 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
1454 Label materialize_true, materialize_false, done;
1455 // Initially assume a pure test context. Notice that the labels are
1456 // swapped.
1457 Label* if_true = false_label_;
1458 Label* if_false = true_label_;
1459 switch (context_) {
1460 case Expression::kUninitialized:
1461 UNREACHABLE();
1462 break;
1463 case Expression::kEffect:
1464 if_true = &done;
1465 if_false = &done;
1466 break;
1467 case Expression::kValue:
1468 if_true = &materialize_false;
1469 if_false = &materialize_true;
1470 break;
1471 case Expression::kTest:
1472 break;
1473 case Expression::kValueTest:
1474 if_false = &materialize_true;
1475 break;
1476 case Expression::kTestValue:
1477 if_true = &materialize_false;
1478 break;
1479 }
1480 VisitForControl(expr->expression(), if_true, if_false);
1481 Apply(context_, if_false, if_true); // Labels swapped.
1482 break;
1483 }
1484
1485 case Token::TYPEOF: {
1486 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
1487 VariableProxy* proxy = expr->expression()->AsVariableProxy();
1488 if (proxy != NULL &&
1489 !proxy->var()->is_this() &&
1490 proxy->var()->is_global()) {
1491 Comment cmnt(masm_, "Global variable");
1492 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001493 __ mov(r2, Operand(proxy->name()));
1494 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1495 // Use a regular load, not a contextual load, to avoid a reference
1496 // error.
1497 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001498 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001499 } else if (proxy != NULL &&
1500 proxy->var()->slot() != NULL &&
1501 proxy->var()->slot()->type() == Slot::LOOKUP) {
1502 __ mov(r0, Operand(proxy->name()));
1503 __ stm(db_w, sp, cp.bit() | r0.bit());
1504 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
1505 __ push(r0);
1506 } else {
1507 // This expression cannot throw a reference error at the top level.
1508 VisitForValue(expr->expression(), kStack);
1509 }
1510
1511 __ CallRuntime(Runtime::kTypeof, 1);
1512 Apply(context_, r0);
1513 break;
1514 }
1515
1516 case Token::ADD: {
1517 Comment cmt(masm_, "[ UnaryOperation (ADD)");
1518 VisitForValue(expr->expression(), kAccumulator);
1519 Label no_conversion;
1520 __ tst(result_register(), Operand(kSmiTagMask));
1521 __ b(eq, &no_conversion);
1522 __ push(r0);
1523 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
1524 __ bind(&no_conversion);
1525 Apply(context_, result_register());
1526 break;
1527 }
1528
1529 case Token::SUB: {
1530 Comment cmt(masm_, "[ UnaryOperation (SUB)");
1531 bool overwrite =
1532 (expr->expression()->AsBinaryOperation() != NULL &&
1533 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
1534 GenericUnaryOpStub stub(Token::SUB, overwrite);
1535 // GenericUnaryOpStub expects the argument to be in the
1536 // accumulator register r0.
1537 VisitForValue(expr->expression(), kAccumulator);
1538 __ CallStub(&stub);
1539 Apply(context_, r0);
1540 break;
1541 }
1542
1543 case Token::BIT_NOT: {
1544 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
1545 bool overwrite =
1546 (expr->expression()->AsBinaryOperation() != NULL &&
1547 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
1548 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
1549 // GenericUnaryOpStub expects the argument to be in the
1550 // accumulator register r0.
1551 VisitForValue(expr->expression(), kAccumulator);
1552 // Avoid calling the stub for Smis.
1553 Label smi, done;
1554 __ tst(result_register(), Operand(kSmiTagMask));
1555 __ b(eq, &smi);
1556 // Non-smi: call stub leaving result in accumulator register.
1557 __ CallStub(&stub);
1558 __ b(&done);
1559 // Perform operation directly on Smis.
1560 __ bind(&smi);
1561 __ mvn(result_register(), Operand(result_register()));
1562 // Bit-clear inverted smi-tag.
1563 __ bic(result_register(), result_register(), Operand(kSmiTagMask));
1564 __ bind(&done);
1565 Apply(context_, result_register());
1566 break;
1567 }
1568
1569 default:
1570 UNREACHABLE();
1571 }
1572}
1573
1574
1575void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
1576 Comment cmnt(masm_, "[ CountOperation");
1577
1578 // Expression can only be a property, a global or a (parameter or local)
1579 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1580 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1581 LhsKind assign_type = VARIABLE;
1582 Property* prop = expr->expression()->AsProperty();
1583 // In case of a property we use the uninitialized expression context
1584 // of the key to detect a named property.
1585 if (prop != NULL) {
1586 assign_type =
1587 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
1588 }
1589
1590 // Evaluate expression and get value.
1591 if (assign_type == VARIABLE) {
1592 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
1593 Location saved_location = location_;
1594 location_ = kAccumulator;
1595 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
1596 Expression::kValue);
1597 location_ = saved_location;
1598 } else {
1599 // Reserve space for result of postfix operation.
1600 if (expr->is_postfix() && context_ != Expression::kEffect) {
1601 __ mov(ip, Operand(Smi::FromInt(0)));
1602 __ push(ip);
1603 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001604 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001605 // Put the object both on the stack and in the accumulator.
1606 VisitForValue(prop->obj(), kAccumulator);
1607 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001608 EmitNamedPropertyLoad(prop);
1609 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001610 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001611 VisitForValue(prop->key(), kAccumulator);
1612 __ ldr(r1, MemOperand(sp, 0));
1613 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001614 EmitKeyedPropertyLoad(prop);
1615 }
1616 }
1617
1618 // Call ToNumber only if operand is not a smi.
1619 Label no_conversion;
1620 __ tst(r0, Operand(kSmiTagMask));
1621 __ b(eq, &no_conversion);
1622 __ push(r0);
1623 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
1624 __ bind(&no_conversion);
1625
1626 // Save result for postfix expressions.
1627 if (expr->is_postfix()) {
1628 switch (context_) {
1629 case Expression::kUninitialized:
1630 UNREACHABLE();
1631 case Expression::kEffect:
1632 // Do not save result.
1633 break;
1634 case Expression::kValue:
1635 case Expression::kTest:
1636 case Expression::kValueTest:
1637 case Expression::kTestValue:
1638 // Save the result on the stack. If we have a named or keyed property
1639 // we store the result under the receiver that is currently on top
1640 // of the stack.
1641 switch (assign_type) {
1642 case VARIABLE:
1643 __ push(r0);
1644 break;
1645 case NAMED_PROPERTY:
1646 __ str(r0, MemOperand(sp, kPointerSize));
1647 break;
1648 case KEYED_PROPERTY:
1649 __ str(r0, MemOperand(sp, 2 * kPointerSize));
1650 break;
1651 }
1652 break;
1653 }
1654 }
1655
1656
1657 // Inline smi case if we are in a loop.
1658 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001659 int count_value = expr->op() == Token::INC ? 1 : -1;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001660 if (loop_depth() > 0) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001661 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001662 __ b(vs, &stub_call);
1663 // We could eliminate this smi check if we split the code at
1664 // the first smi check before calling ToNumber.
1665 __ tst(r0, Operand(kSmiTagMask));
1666 __ b(eq, &done);
1667 __ bind(&stub_call);
1668 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001669 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001670 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001671 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00001672 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001673 __ CallStub(&stub);
1674 __ bind(&done);
1675
1676 // Store the value returned in r0.
1677 switch (assign_type) {
1678 case VARIABLE:
1679 if (expr->is_postfix()) {
1680 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001681 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001682 Expression::kEffect);
1683 // For all contexts except kEffect: We have the result on
1684 // top of the stack.
1685 if (context_ != Expression::kEffect) {
1686 ApplyTOS(context_);
1687 }
1688 } else {
1689 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001690 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001691 context_);
1692 }
1693 break;
1694 case NAMED_PROPERTY: {
1695 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001696 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001697 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1698 __ Call(ic, RelocInfo::CODE_TARGET);
1699 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001700 if (context_ != Expression::kEffect) {
1701 ApplyTOS(context_);
1702 }
1703 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00001704 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001705 }
1706 break;
1707 }
1708 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001709 __ pop(r1); // Key.
1710 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001711 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1712 __ Call(ic, RelocInfo::CODE_TARGET);
1713 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001714 if (context_ != Expression::kEffect) {
1715 ApplyTOS(context_);
1716 }
1717 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001718 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001719 }
1720 break;
1721 }
1722 }
1723}
1724
1725
1726void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
1727 Comment cmnt(masm_, "[ BinaryOperation");
1728 switch (expr->op()) {
1729 case Token::COMMA:
1730 VisitForEffect(expr->left());
1731 Visit(expr->right());
1732 break;
1733
1734 case Token::OR:
1735 case Token::AND:
1736 EmitLogicalOperation(expr);
1737 break;
1738
1739 case Token::ADD:
1740 case Token::SUB:
1741 case Token::DIV:
1742 case Token::MOD:
1743 case Token::MUL:
1744 case Token::BIT_OR:
1745 case Token::BIT_AND:
1746 case Token::BIT_XOR:
1747 case Token::SHL:
1748 case Token::SHR:
1749 case Token::SAR:
1750 VisitForValue(expr->left(), kStack);
1751 VisitForValue(expr->right(), kAccumulator);
1752 EmitBinaryOp(expr->op(), context_);
1753 break;
1754
1755 default:
1756 UNREACHABLE();
1757 }
1758}
1759
1760
1761void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
1762 Comment cmnt(masm_, "[ CompareOperation");
1763
1764 // Always perform the comparison for its control flow. Pack the result
1765 // into the expression's context after the comparison is performed.
1766 Label materialize_true, materialize_false, done;
1767 // Initially assume we are in a test context.
1768 Label* if_true = true_label_;
1769 Label* if_false = false_label_;
1770 switch (context_) {
1771 case Expression::kUninitialized:
1772 UNREACHABLE();
1773 break;
1774 case Expression::kEffect:
1775 if_true = &done;
1776 if_false = &done;
1777 break;
1778 case Expression::kValue:
1779 if_true = &materialize_true;
1780 if_false = &materialize_false;
1781 break;
1782 case Expression::kTest:
1783 break;
1784 case Expression::kValueTest:
1785 if_true = &materialize_true;
1786 break;
1787 case Expression::kTestValue:
1788 if_false = &materialize_false;
1789 break;
1790 }
1791
1792 VisitForValue(expr->left(), kStack);
1793 switch (expr->op()) {
1794 case Token::IN:
1795 VisitForValue(expr->right(), kStack);
1796 __ InvokeBuiltin(Builtins::IN, CALL_JS);
1797 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
1798 __ cmp(r0, ip);
1799 __ b(eq, if_true);
1800 __ jmp(if_false);
1801 break;
1802
1803 case Token::INSTANCEOF: {
1804 VisitForValue(expr->right(), kStack);
1805 InstanceofStub stub;
1806 __ CallStub(&stub);
1807 __ tst(r0, r0);
1808 __ b(eq, if_true); // The stub returns 0 for true.
1809 __ jmp(if_false);
1810 break;
1811 }
1812
1813 default: {
1814 VisitForValue(expr->right(), kAccumulator);
1815 Condition cc = eq;
1816 bool strict = false;
1817 switch (expr->op()) {
1818 case Token::EQ_STRICT:
1819 strict = true;
1820 // Fall through
1821 case Token::EQ:
1822 cc = eq;
1823 __ pop(r1);
1824 break;
1825 case Token::LT:
1826 cc = lt;
1827 __ pop(r1);
1828 break;
1829 case Token::GT:
1830 // Reverse left and right sides to obtain ECMA-262 conversion order.
1831 cc = lt;
1832 __ mov(r1, result_register());
1833 __ pop(r0);
1834 break;
1835 case Token::LTE:
1836 // Reverse left and right sides to obtain ECMA-262 conversion order.
1837 cc = ge;
1838 __ mov(r1, result_register());
1839 __ pop(r0);
1840 break;
1841 case Token::GTE:
1842 cc = ge;
1843 __ pop(r1);
1844 break;
1845 case Token::IN:
1846 case Token::INSTANCEOF:
1847 default:
1848 UNREACHABLE();
1849 }
1850
1851 // The comparison stub expects the smi vs. smi case to be handled
1852 // before it is called.
1853 Label slow_case;
1854 __ orr(r2, r0, Operand(r1));
1855 __ tst(r2, Operand(kSmiTagMask));
1856 __ b(ne, &slow_case);
1857 __ cmp(r1, r0);
1858 __ b(cc, if_true);
1859 __ jmp(if_false);
1860
1861 __ bind(&slow_case);
1862 CompareStub stub(cc, strict);
1863 __ CallStub(&stub);
1864 __ cmp(r0, Operand(0));
1865 __ b(cc, if_true);
1866 __ jmp(if_false);
1867 }
1868 }
1869
1870 // Convert the result of the comparison into one expected for this
1871 // expression's context.
1872 Apply(context_, if_true, if_false);
1873}
1874
1875
1876void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
1877 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1878 Apply(context_, r0);
1879}
1880
1881
1882Register FullCodeGenerator::result_register() { return r0; }
1883
1884
1885Register FullCodeGenerator::context_register() { return cp; }
1886
1887
1888void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
1889 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
1890 __ str(value, MemOperand(fp, frame_offset));
1891}
1892
1893
1894void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
1895 __ ldr(dst, CodeGenerator::ContextOperand(cp, context_index));
1896}
1897
1898
1899// ----------------------------------------------------------------------------
1900// Non-local control flow support.
1901
1902void FullCodeGenerator::EnterFinallyBlock() {
1903 ASSERT(!result_register().is(r1));
1904 // Store result register while executing finally block.
1905 __ push(result_register());
1906 // Cook return address in link register to stack (smi encoded Code* delta)
1907 __ sub(r1, lr, Operand(masm_->CodeObject()));
1908 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
1909 ASSERT_EQ(0, kSmiTag);
1910 __ add(r1, r1, Operand(r1)); // Convert to smi.
1911 __ push(r1);
1912}
1913
1914
1915void FullCodeGenerator::ExitFinallyBlock() {
1916 ASSERT(!result_register().is(r1));
1917 // Restore result register from stack.
1918 __ pop(r1);
1919 // Uncook return address and return.
1920 __ pop(result_register());
1921 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
1922 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
1923 __ add(pc, r1, Operand(masm_->CodeObject()));
1924}
1925
1926
1927#undef __
1928
1929} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001930
1931#endif // V8_TARGET_ARCH_ARM