blob: 230818f5d13de828e06e9f0fd1334308f49bae13 [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
30#include "codegen-inl.h"
31#include "compiler.h"
32#include "debug.h"
33#include "full-codegen.h"
34#include "parser.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000035#include "scopes.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000036
37namespace v8 {
38namespace internal {
39
40#define __ ACCESS_MASM(masm_)
41
42// Generate code for a JS function. On entry to the function the receiver
43// and arguments have been pushed on the stack left to right. The actual
44// argument count matches the formal parameter count expected by the
45// function.
46//
47// The live registers are:
48// o r1: the JS function object being called (ie, ourselves)
49// o cp: our context
50// o fp: our caller's frame pointer
51// o sp: stack pointer
52// o lr: return address
53//
54// The function builds a JS frame. Please see JavaScriptFrameConstants in
55// frames-arm.h for its layout.
ager@chromium.org5c838252010-02-19 08:53:10 +000056void FullCodeGenerator::Generate(CompilationInfo* info, Mode mode) {
57 ASSERT(info_ == NULL);
58 info_ = info;
59 SetFunctionPosition(function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000060 Comment cmnt(masm_, "[ function compiled by full code generator");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000061
62 if (mode == PRIMARY) {
ager@chromium.org5c838252010-02-19 08:53:10 +000063 int locals_count = scope()->num_stack_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000064
65 __ stm(db_w, sp, r1.bit() | cp.bit() | fp.bit() | lr.bit());
66 if (locals_count > 0) {
67 // Load undefined value here, so the value is ready for the loop
68 // below.
69 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
70 }
71 // Adjust fp to point to caller's fp.
72 __ add(fp, sp, Operand(2 * kPointerSize));
73
74 { Comment cmnt(masm_, "[ Allocate locals");
75 for (int i = 0; i < locals_count; i++) {
76 __ push(ip);
77 }
78 }
79
80 bool function_in_register = true;
81
82 // Possibly allocate a local context.
ager@chromium.org5c838252010-02-19 08:53:10 +000083 if (scope()->num_heap_slots() > 0) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000084 Comment cmnt(masm_, "[ Allocate local context");
85 // Argument to NewContext is the function, which is in r1.
86 __ push(r1);
87 __ CallRuntime(Runtime::kNewContext, 1);
88 function_in_register = false;
89 // Context is returned in both r0 and cp. It replaces the context
90 // passed to us. It's saved in the stack and kept live in cp.
91 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
92 // Copy any necessary parameters into the context.
ager@chromium.org5c838252010-02-19 08:53:10 +000093 int num_parameters = scope()->num_parameters();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000094 for (int i = 0; i < num_parameters; i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +000095 Slot* slot = scope()->parameter(i)->slot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000096 if (slot != NULL && slot->type() == Slot::CONTEXT) {
97 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
98 (num_parameters - 1 - i) * kPointerSize;
99 // Load parameter from stack.
100 __ ldr(r0, MemOperand(fp, parameter_offset));
101 // Store it in the context.
102 __ mov(r1, Operand(Context::SlotOffset(slot->index())));
103 __ str(r0, MemOperand(cp, r1));
104 // Update the write barrier. This clobbers all involved
105 // registers, so we have use a third register to avoid
106 // clobbering cp.
107 __ mov(r2, Operand(cp));
108 __ RecordWrite(r2, r1, r0);
109 }
110 }
111 }
112
ager@chromium.org5c838252010-02-19 08:53:10 +0000113 Variable* arguments = scope()->arguments()->AsVariable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000114 if (arguments != NULL) {
115 // Function uses arguments object.
116 Comment cmnt(masm_, "[ Allocate arguments object");
117 if (!function_in_register) {
118 // Load this again, if it's used by the local context below.
119 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
120 } else {
121 __ mov(r3, r1);
122 }
123 // Receiver is just before the parameters on the caller's stack.
ager@chromium.org5c838252010-02-19 08:53:10 +0000124 int offset = scope()->num_parameters() * kPointerSize;
125 __ add(r2, fp,
126 Operand(StandardFrameConstants::kCallerSPOffset + offset));
127 __ mov(r1, Operand(Smi::FromInt(scope()->num_parameters())));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000128 __ stm(db_w, sp, r3.bit() | r2.bit() | r1.bit());
129
130 // Arguments to ArgumentsAccessStub:
131 // function, receiver address, parameter count.
132 // The stub will rewrite receiever and parameter count if the previous
133 // stack frame was an arguments adapter frame.
134 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
135 __ CallStub(&stub);
136 // Duplicate the value; move-to-slot operation might clobber registers.
137 __ mov(r3, r0);
138 Move(arguments->slot(), r0, r1, r2);
139 Slot* dot_arguments_slot =
ager@chromium.org5c838252010-02-19 08:53:10 +0000140 scope()->arguments_shadow()->AsVariable()->slot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000141 Move(dot_arguments_slot, r3, r1, r2);
142 }
143 }
144
145 // Check the stack for overflow or break request.
146 // Put the lr setup instruction in the delay slot. The kInstrSize is
147 // added to the implicit 8 byte offset that always applies to operations
148 // with pc and gives a return address 12 bytes down.
149 { Comment cmnt(masm_, "[ Stack check");
150 __ LoadRoot(r2, Heap::kStackLimitRootIndex);
151 __ add(lr, pc, Operand(Assembler::kInstrSize));
152 __ cmp(sp, Operand(r2));
153 StackCheckStub stub;
154 __ mov(pc,
155 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
156 RelocInfo::CODE_TARGET),
157 LeaveCC,
158 lo);
159 }
160
161 { Comment cmnt(masm_, "[ Declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000162 VisitDeclarations(scope()->declarations());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000163 }
164
165 if (FLAG_trace) {
166 __ CallRuntime(Runtime::kTraceEnter, 0);
167 }
168
169 { Comment cmnt(masm_, "[ Body");
170 ASSERT(loop_depth() == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000171 VisitStatements(function()->body());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000172 ASSERT(loop_depth() == 0);
173 }
174
175 { Comment cmnt(masm_, "[ return <undefined>;");
176 // Emit a 'return undefined' in case control fell off the end of the
177 // body.
178 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
179 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000180 EmitReturnSequence(function()->end_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000181}
182
183
184void FullCodeGenerator::EmitReturnSequence(int position) {
185 Comment cmnt(masm_, "[ Return sequence");
186 if (return_label_.is_bound()) {
187 __ b(&return_label_);
188 } else {
189 __ bind(&return_label_);
190 if (FLAG_trace) {
191 // Push the return value on the stack as the parameter.
192 // Runtime::TraceExit returns its parameter in r0.
193 __ push(r0);
194 __ CallRuntime(Runtime::kTraceExit, 1);
195 }
196
197 // Add a label for checking the size of the code used for returning.
198 Label check_exit_codesize;
199 masm_->bind(&check_exit_codesize);
200
201 // Calculate the exact length of the return sequence and make sure that
202 // the constant pool is not emitted inside of the return sequence.
ager@chromium.org5c838252010-02-19 08:53:10 +0000203 int num_parameters = scope()->num_parameters();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000204 int32_t sp_delta = (num_parameters + 1) * kPointerSize;
205 int return_sequence_length = Assembler::kJSReturnSequenceLength;
206 if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) {
207 // Additional mov instruction generated.
208 return_sequence_length++;
209 }
210 masm_->BlockConstPoolFor(return_sequence_length);
211
212 CodeGenerator::RecordPositions(masm_, position);
213 __ RecordJSReturn();
214 __ mov(sp, fp);
215 __ ldm(ia_w, sp, fp.bit() | lr.bit());
216 __ add(sp, sp, Operand(sp_delta));
217 __ Jump(lr);
218
219 // Check that the size of the code used for returning matches what is
220 // expected by the debugger. The add instruction above is an addressing
221 // mode 1 instruction where there are restrictions on which immediate values
222 // can be encoded in the instruction and which immediate values requires
223 // use of an additional instruction for moving the immediate to a temporary
224 // register.
225 ASSERT_EQ(return_sequence_length,
226 masm_->InstructionsGeneratedSince(&check_exit_codesize));
227 }
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);
402 __ mov(result_register(), Operand(Factory::true_value()));
403 __ jmp(&done);
404 __ bind(materialize_false);
405 __ mov(result_register(), Operand(Factory::false_value()));
406 __ 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);
422 __ mov(result_register(), Operand(Factory::true_value()));
423 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);
435 __ mov(result_register(), Operand(Factory::false_value()));
436 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));
645 __ Call(ic, RelocInfo::CODE_TARGET);
646
647 // Value in r0 is ignored (declarations are statements). Receiver
648 // and key on stack are discarded.
649 __ Drop(2);
650 }
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
666void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
667 Comment cmnt(masm_, "[ FunctionLiteral");
668
669 // Build the function boilerplate and instantiate it.
670 Handle<JSFunction> boilerplate =
ager@chromium.org5c838252010-02-19 08:53:10 +0000671 Compiler::BuildBoilerplate(expr, script(), this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000672 if (HasStackOverflow()) return;
673
674 ASSERT(boilerplate->IsBoilerplate());
675
676 // Create a new closure.
677 __ mov(r0, Operand(boilerplate));
678 __ stm(db_w, sp, cp.bit() | r0.bit());
679 __ CallRuntime(Runtime::kNewClosure, 2);
680 Apply(context_, r0);
681}
682
683
684void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
685 Comment cmnt(masm_, "[ VariableProxy");
686 EmitVariableLoad(expr->var(), context_);
687}
688
689
690void FullCodeGenerator::EmitVariableLoad(Variable* var,
691 Expression::Context context) {
692 // Four cases: non-this global variables, lookup slots, all other
693 // types of slots, and parameters that rewrite to explicit property
694 // accesses on the arguments object.
695 Slot* slot = var->slot();
696 Property* property = var->AsProperty();
697
698 if (var->is_global() && !var->is_this()) {
699 Comment cmnt(masm_, "Global variable");
700 // Use inline caching. Variable name is passed in r2 and the global
701 // object on the stack.
702 __ ldr(ip, CodeGenerator::GlobalObject());
703 __ push(ip);
704 __ mov(r2, Operand(var->name()));
705 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
706 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
707 DropAndApply(1, context, r0);
708
709 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
710 Comment cmnt(masm_, "Lookup slot");
711 __ mov(r1, Operand(var->name()));
712 __ stm(db_w, sp, cp.bit() | r1.bit()); // Context and name.
713 __ CallRuntime(Runtime::kLoadContextSlot, 2);
714 Apply(context, r0);
715
716 } else if (slot != NULL) {
717 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
718 ? "Context slot"
719 : "Stack slot");
720 Apply(context, slot);
721
722 } else {
723 Comment cmnt(masm_, "Rewritten parameter");
724 ASSERT_NOT_NULL(property);
725 // Rewritten parameter accesses are of the form "slot[literal]".
726
727 // Assert that the object is in a slot.
728 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
729 ASSERT_NOT_NULL(object_var);
730 Slot* object_slot = object_var->slot();
731 ASSERT_NOT_NULL(object_slot);
732
733 // Load the object.
734 Move(r2, object_slot);
735
736 // Assert that the key is a smi.
737 Literal* key_literal = property->key()->AsLiteral();
738 ASSERT_NOT_NULL(key_literal);
739 ASSERT(key_literal->handle()->IsSmi());
740
741 // Load the key.
742 __ mov(r1, Operand(key_literal->handle()));
743
744 // Push both as arguments to ic.
745 __ stm(db_w, sp, r2.bit() | r1.bit());
746
747 // Do a keyed property load.
748 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
749 __ Call(ic, RelocInfo::CODE_TARGET);
750
751 // Drop key and object left on the stack by IC, and push the result.
752 DropAndApply(2, context, r0);
753 }
754}
755
756
757void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
758 Comment cmnt(masm_, "[ RegExpLiteral");
759 Label done;
760 // Registers will be used as follows:
761 // r4 = JS function, literals array
762 // r3 = literal index
763 // r2 = RegExp pattern
764 // r1 = RegExp flags
765 // r0 = temp + return value (RegExp literal)
766 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
767 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
768 int literal_offset =
769 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
770 __ ldr(r0, FieldMemOperand(r4, literal_offset));
771 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
772 __ cmp(r0, ip);
773 __ b(ne, &done);
774 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
775 __ mov(r2, Operand(expr->pattern()));
776 __ mov(r1, Operand(expr->flags()));
777 __ stm(db_w, sp, r4.bit() | r3.bit() | r2.bit() | r1.bit());
778 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
779 __ bind(&done);
780 Apply(context_, r0);
781}
782
783
784void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
785 Comment cmnt(masm_, "[ ObjectLiteral");
786 __ ldr(r2, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
787 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
788 __ mov(r1, Operand(Smi::FromInt(expr->literal_index())));
789 __ mov(r0, Operand(expr->constant_properties()));
790 __ stm(db_w, sp, r2.bit() | r1.bit() | r0.bit());
791 if (expr->depth() > 1) {
792 __ CallRuntime(Runtime::kCreateObjectLiteral, 3);
793 } else {
794 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 3);
795 }
796
797 // If result_saved is true the result is on top of the stack. If
798 // result_saved is false the result is in r0.
799 bool result_saved = false;
800
801 for (int i = 0; i < expr->properties()->length(); i++) {
802 ObjectLiteral::Property* property = expr->properties()->at(i);
803 if (property->IsCompileTimeValue()) continue;
804
805 Literal* key = property->key();
806 Expression* value = property->value();
807 if (!result_saved) {
808 __ push(r0); // Save result on stack
809 result_saved = true;
810 }
811 switch (property->kind()) {
812 case ObjectLiteral::Property::CONSTANT:
813 UNREACHABLE();
814 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
815 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
816 // Fall through.
817 case ObjectLiteral::Property::COMPUTED:
818 if (key->handle()->IsSymbol()) {
819 VisitForValue(value, kAccumulator);
820 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000821 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000822 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
823 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000824 break;
825 }
826 // Fall through.
827 case ObjectLiteral::Property::PROTOTYPE:
828 // Duplicate receiver on stack.
829 __ ldr(r0, MemOperand(sp));
830 __ push(r0);
831 VisitForValue(key, kStack);
832 VisitForValue(value, kStack);
833 __ CallRuntime(Runtime::kSetProperty, 3);
834 break;
835 case ObjectLiteral::Property::GETTER:
836 case ObjectLiteral::Property::SETTER:
837 // Duplicate receiver on stack.
838 __ ldr(r0, MemOperand(sp));
839 __ push(r0);
840 VisitForValue(key, kStack);
841 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
842 Smi::FromInt(1) :
843 Smi::FromInt(0)));
844 __ push(r1);
845 VisitForValue(value, kStack);
846 __ CallRuntime(Runtime::kDefineAccessor, 4);
847 break;
848 }
849 }
850
851 if (result_saved) {
852 ApplyTOS(context_);
853 } else {
854 Apply(context_, r0);
855 }
856}
857
858
859void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
860 Comment cmnt(masm_, "[ ArrayLiteral");
861 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
862 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
863 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
864 __ mov(r1, Operand(expr->constant_elements()));
865 __ stm(db_w, sp, r3.bit() | r2.bit() | r1.bit());
866 if (expr->depth() > 1) {
867 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
868 } else {
869 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
870 }
871
872 bool result_saved = false; // Is the result saved to the stack?
873
874 // Emit code to evaluate all the non-constant subexpressions and to store
875 // them into the newly cloned array.
876 ZoneList<Expression*>* subexprs = expr->values();
877 for (int i = 0, len = subexprs->length(); i < len; i++) {
878 Expression* subexpr = subexprs->at(i);
879 // If the subexpression is a literal or a simple materialized literal it
880 // is already set in the cloned array.
881 if (subexpr->AsLiteral() != NULL ||
882 CompileTimeValue::IsCompileTimeValue(subexpr)) {
883 continue;
884 }
885
886 if (!result_saved) {
887 __ push(r0);
888 result_saved = true;
889 }
890 VisitForValue(subexpr, kAccumulator);
891
892 // Store the subexpression value in the array's elements.
893 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
894 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
895 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
896 __ str(result_register(), FieldMemOperand(r1, offset));
897
898 // Update the write barrier for the array store with r0 as the scratch
899 // register.
900 __ mov(r2, Operand(offset));
901 __ RecordWrite(r1, r2, result_register());
902 }
903
904 if (result_saved) {
905 ApplyTOS(context_);
906 } else {
907 Apply(context_, r0);
908 }
909}
910
911
ager@chromium.org5c838252010-02-19 08:53:10 +0000912void FullCodeGenerator::VisitAssignment(Assignment* expr) {
913 Comment cmnt(masm_, "[ Assignment");
914 ASSERT(expr->op() != Token::INIT_CONST);
915 // Left-hand side can only be a property, a global or a (parameter or local)
916 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
917 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
918 LhsKind assign_type = VARIABLE;
919 Property* prop = expr->target()->AsProperty();
920 if (prop != NULL) {
921 assign_type =
922 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
923 }
924
925 // Evaluate LHS expression.
926 switch (assign_type) {
927 case VARIABLE:
928 // Nothing to do here.
929 break;
930 case NAMED_PROPERTY:
931 if (expr->is_compound()) {
932 // We need the receiver both on the stack and in the accumulator.
933 VisitForValue(prop->obj(), kAccumulator);
934 __ push(result_register());
935 } else {
936 VisitForValue(prop->obj(), kStack);
937 }
938 break;
939 case KEYED_PROPERTY:
940 VisitForValue(prop->obj(), kStack);
941 VisitForValue(prop->key(), kStack);
942 break;
943 }
944
945 // If we have a compound assignment: Get value of LHS expression and
946 // store in on top of the stack.
947 if (expr->is_compound()) {
948 Location saved_location = location_;
949 location_ = kStack;
950 switch (assign_type) {
951 case VARIABLE:
952 EmitVariableLoad(expr->target()->AsVariableProxy()->var(),
953 Expression::kValue);
954 break;
955 case NAMED_PROPERTY:
956 EmitNamedPropertyLoad(prop);
957 __ push(result_register());
958 break;
959 case KEYED_PROPERTY:
960 EmitKeyedPropertyLoad(prop);
961 __ push(result_register());
962 break;
963 }
964 location_ = saved_location;
965 }
966
967 // Evaluate RHS expression.
968 Expression* rhs = expr->value();
969 VisitForValue(rhs, kAccumulator);
970
971 // If we have a compound assignment: Apply operator.
972 if (expr->is_compound()) {
973 Location saved_location = location_;
974 location_ = kAccumulator;
975 EmitBinaryOp(expr->binary_op(), Expression::kValue);
976 location_ = saved_location;
977 }
978
979 // Record source position before possible IC call.
980 SetSourcePosition(expr->position());
981
982 // Store the value.
983 switch (assign_type) {
984 case VARIABLE:
985 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
986 context_);
987 break;
988 case NAMED_PROPERTY:
989 EmitNamedPropertyAssignment(expr);
990 break;
991 case KEYED_PROPERTY:
992 EmitKeyedPropertyAssignment(expr);
993 break;
994 }
995}
996
997
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000998void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
999 SetSourcePosition(prop->position());
1000 Literal* key = prop->key()->AsLiteral();
1001 __ mov(r2, Operand(key->handle()));
1002 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1003 __ Call(ic, RelocInfo::CODE_TARGET);
1004}
1005
1006
1007void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1008 SetSourcePosition(prop->position());
1009 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1010 __ Call(ic, RelocInfo::CODE_TARGET);
1011}
1012
1013
1014void FullCodeGenerator::EmitBinaryOp(Token::Value op,
1015 Expression::Context context) {
1016 __ pop(r1);
1017 GenericBinaryOpStub stub(op, NO_OVERWRITE);
1018 __ CallStub(&stub);
1019 Apply(context, r0);
1020}
1021
1022
1023void FullCodeGenerator::EmitVariableAssignment(Variable* var,
1024 Expression::Context context) {
1025 // Three main cases: global variables, lookup slots, and all other
1026 // types of slots. Left-hand-side parameters that rewrite to
1027 // explicit property accesses do not reach here.
1028 ASSERT(var != NULL);
1029 ASSERT(var->is_global() || var->slot() != NULL);
1030
1031 Slot* slot = var->slot();
1032 if (var->is_global()) {
1033 ASSERT(!var->is_this());
1034 // Assignment to a global variable. Use inline caching for the
1035 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001036 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001037 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001038 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001039 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1040 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001041
1042 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
1043 __ push(result_register()); // Value.
1044 __ mov(r1, Operand(var->name()));
1045 __ stm(db_w, sp, cp.bit() | r1.bit()); // Context and name.
1046 __ CallRuntime(Runtime::kStoreContextSlot, 3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001047
1048 } else if (var->slot() != NULL) {
1049 Slot* slot = var->slot();
1050 switch (slot->type()) {
1051 case Slot::LOCAL:
1052 case Slot::PARAMETER:
1053 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1054 break;
1055
1056 case Slot::CONTEXT: {
1057 MemOperand target = EmitSlotSearch(slot, r1);
1058 __ str(result_register(), target);
1059
1060 // RecordWrite may destroy all its register arguments.
1061 __ mov(r3, result_register());
1062 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
1063
1064 __ mov(r2, Operand(offset));
1065 __ RecordWrite(r1, r2, r3);
1066 break;
1067 }
1068
1069 case Slot::LOOKUP:
1070 UNREACHABLE();
1071 break;
1072 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001073
1074 } else {
1075 // Variables rewritten as properties are not treated as variables in
1076 // assignments.
1077 UNREACHABLE();
1078 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001079 Apply(context, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001080}
1081
1082
1083void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1084 // Assignment to a property, using a named store IC.
1085 Property* prop = expr->target()->AsProperty();
1086 ASSERT(prop != NULL);
1087 ASSERT(prop->key()->AsLiteral() != NULL);
1088
1089 // If the assignment starts a block of assignments to the same object,
1090 // change to slow case to avoid the quadratic behavior of repeatedly
1091 // adding fast properties.
1092 if (expr->starts_initialization_block()) {
1093 __ push(result_register());
1094 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1095 __ push(ip);
1096 __ CallRuntime(Runtime::kToSlowProperties, 1);
1097 __ pop(result_register());
1098 }
1099
1100 // Record source code position before IC call.
1101 SetSourcePosition(expr->position());
1102 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001103 if (expr->ends_initialization_block()) {
1104 __ ldr(r1, MemOperand(sp));
1105 } else {
1106 __ pop(r1);
1107 }
1108
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001109 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1110 __ Call(ic, RelocInfo::CODE_TARGET);
1111
1112 // If the assignment ends an initialization block, revert to fast case.
1113 if (expr->ends_initialization_block()) {
1114 __ push(r0); // Result of assignment, saved even if not needed.
1115 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is under value.
1116 __ push(ip);
1117 __ CallRuntime(Runtime::kToFastProperties, 1);
1118 __ pop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001119 DropAndApply(1, context_, r0);
1120 } else {
1121 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001122 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001123}
1124
1125
1126void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1127 // Assignment to a property, using a keyed store IC.
1128
1129 // If the assignment starts a block of assignments to the same object,
1130 // change to slow case to avoid the quadratic behavior of repeatedly
1131 // adding fast properties.
1132 if (expr->starts_initialization_block()) {
1133 __ push(result_register());
1134 // Receiver is now under the key and value.
1135 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1136 __ push(ip);
1137 __ CallRuntime(Runtime::kToSlowProperties, 1);
1138 __ pop(result_register());
1139 }
1140
1141 // Record source code position before IC call.
1142 SetSourcePosition(expr->position());
1143 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1144 __ Call(ic, RelocInfo::CODE_TARGET);
1145
1146 // If the assignment ends an initialization block, revert to fast case.
1147 if (expr->ends_initialization_block()) {
1148 __ push(r0); // Result of assignment, saved even if not needed.
1149 // Receiver is under the key and value.
1150 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1151 __ push(ip);
1152 __ CallRuntime(Runtime::kToFastProperties, 1);
1153 __ pop(r0);
1154 }
1155
1156 // Receiver and key are still on stack.
1157 DropAndApply(2, context_, r0);
1158}
1159
1160
1161void FullCodeGenerator::VisitProperty(Property* expr) {
1162 Comment cmnt(masm_, "[ Property");
1163 Expression* key = expr->key();
1164
1165 // Evaluate receiver.
1166 VisitForValue(expr->obj(), kStack);
1167
1168 if (key->IsPropertyName()) {
1169 EmitNamedPropertyLoad(expr);
1170 // Drop receiver left on the stack by IC.
1171 DropAndApply(1, context_, r0);
1172 } else {
1173 VisitForValue(expr->key(), kStack);
1174 EmitKeyedPropertyLoad(expr);
1175 // Drop key and receiver left on the stack by IC.
1176 DropAndApply(2, context_, r0);
1177 }
1178}
1179
1180void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001181 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001182 RelocInfo::Mode mode) {
1183 // Code common for calls using the IC.
1184 ZoneList<Expression*>* args = expr->arguments();
1185 int arg_count = args->length();
1186 for (int i = 0; i < arg_count; i++) {
1187 VisitForValue(args->at(i), kStack);
1188 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001189 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001190 // Record source position for debugger.
1191 SetSourcePosition(expr->position());
1192 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001193 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1194 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001195 __ Call(ic, mode);
1196 // Restore context register.
1197 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001198 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001199}
1200
1201
1202void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1203 // Code common for calls using the call stub.
1204 ZoneList<Expression*>* args = expr->arguments();
1205 int arg_count = args->length();
1206 for (int i = 0; i < arg_count; i++) {
1207 VisitForValue(args->at(i), kStack);
1208 }
1209 // Record source position for debugger.
1210 SetSourcePosition(expr->position());
1211 CallFunctionStub stub(arg_count, NOT_IN_LOOP, RECEIVER_MIGHT_BE_VALUE);
1212 __ CallStub(&stub);
1213 // Restore context register.
1214 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001215 DropAndApply(1, context_, r0);
1216}
1217
1218
1219void FullCodeGenerator::VisitCall(Call* expr) {
1220 Comment cmnt(masm_, "[ Call");
1221 Expression* fun = expr->expression();
1222 Variable* var = fun->AsVariableProxy()->AsVariable();
1223
1224 if (var != NULL && var->is_possibly_eval()) {
1225 // Call to the identifier 'eval'.
1226 UNREACHABLE();
1227 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001228 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001229 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001230 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001231 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1232 } else if (var != NULL && var->slot() != NULL &&
1233 var->slot()->type() == Slot::LOOKUP) {
1234 // Call to a lookup slot.
1235 UNREACHABLE();
1236 } else if (fun->AsProperty() != NULL) {
1237 // Call to an object property.
1238 Property* prop = fun->AsProperty();
1239 Literal* key = prop->key()->AsLiteral();
1240 if (key != NULL && key->handle()->IsSymbol()) {
1241 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001242 VisitForValue(prop->obj(), kStack);
1243 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1244 } else {
1245 // Call to a keyed property, use keyed load IC followed by function
1246 // call.
1247 VisitForValue(prop->obj(), kStack);
1248 VisitForValue(prop->key(), kStack);
1249 // Record source code position for IC call.
1250 SetSourcePosition(prop->position());
1251 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1252 __ Call(ic, RelocInfo::CODE_TARGET);
1253 // Load receiver object into r1.
1254 if (prop->is_synthetic()) {
1255 __ ldr(r1, CodeGenerator::GlobalObject());
1256 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1257 } else {
1258 __ ldr(r1, MemOperand(sp, kPointerSize));
1259 }
1260 // Overwrite (object, key) with (function, receiver).
1261 __ str(r0, MemOperand(sp, kPointerSize));
1262 __ str(r1, MemOperand(sp));
1263 EmitCallWithStub(expr);
1264 }
1265 } else {
1266 // Call to some other expression. If the expression is an anonymous
1267 // function literal not called in a loop, mark it as one that should
1268 // also use the fast code generator.
1269 FunctionLiteral* lit = fun->AsFunctionLiteral();
1270 if (lit != NULL &&
1271 lit->name()->Equals(Heap::empty_string()) &&
1272 loop_depth() == 0) {
1273 lit->set_try_full_codegen(true);
1274 }
1275 VisitForValue(fun, kStack);
1276 // Load global receiver object.
1277 __ ldr(r1, CodeGenerator::GlobalObject());
1278 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1279 __ push(r1);
1280 // Emit function call.
1281 EmitCallWithStub(expr);
1282 }
1283}
1284
1285
1286void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1287 Comment cmnt(masm_, "[ CallNew");
1288 // According to ECMA-262, section 11.2.2, page 44, the function
1289 // expression in new calls must be evaluated before the
1290 // arguments.
1291 // Push function on the stack.
1292 VisitForValue(expr->expression(), kStack);
1293
1294 // Push global object (receiver).
1295 __ ldr(r0, CodeGenerator::GlobalObject());
1296 __ push(r0);
1297 // Push the arguments ("left-to-right") on the stack.
1298 ZoneList<Expression*>* args = expr->arguments();
1299 int arg_count = args->length();
1300 for (int i = 0; i < arg_count; i++) {
1301 VisitForValue(args->at(i), kStack);
1302 }
1303
1304 // Call the construct call builtin that handles allocation and
1305 // constructor invocation.
1306 SetSourcePosition(expr->position());
1307
1308 // Load function, arg_count into r1 and r0.
1309 __ mov(r0, Operand(arg_count));
1310 // Function is in sp[arg_count + 1].
1311 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1312
1313 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1314 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
1315
1316 // Replace function on TOS with result in r0, or pop it.
1317 DropAndApply(1, context_, r0);
1318}
1319
1320
1321void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
1322 Comment cmnt(masm_, "[ CallRuntime");
1323 ZoneList<Expression*>* args = expr->arguments();
1324
1325 if (expr->is_jsruntime()) {
1326 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001327 __ ldr(r0, CodeGenerator::GlobalObject());
1328 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001329 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001330 }
1331
1332 // Push the arguments ("left-to-right").
1333 int arg_count = args->length();
1334 for (int i = 0; i < arg_count; i++) {
1335 VisitForValue(args->at(i), kStack);
1336 }
1337
1338 if (expr->is_jsruntime()) {
1339 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00001340 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001341 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
1342 NOT_IN_LOOP);
1343 __ Call(ic, RelocInfo::CODE_TARGET);
1344 // Restore context register.
1345 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001346 } else {
1347 // Call the C runtime function.
1348 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001349 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001350 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001351}
1352
1353
1354void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
1355 switch (expr->op()) {
1356 case Token::VOID: {
1357 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
1358 VisitForEffect(expr->expression());
1359 switch (context_) {
1360 case Expression::kUninitialized:
1361 UNREACHABLE();
1362 break;
1363 case Expression::kEffect:
1364 break;
1365 case Expression::kValue:
1366 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
1367 switch (location_) {
1368 case kAccumulator:
1369 break;
1370 case kStack:
1371 __ push(result_register());
1372 break;
1373 }
1374 break;
1375 case Expression::kTestValue:
1376 // Value is false so it's needed.
1377 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
1378 switch (location_) {
1379 case kAccumulator:
1380 break;
1381 case kStack:
1382 __ push(result_register());
1383 break;
1384 }
1385 // Fall through.
1386 case Expression::kTest:
1387 case Expression::kValueTest:
1388 __ jmp(false_label_);
1389 break;
1390 }
1391 break;
1392 }
1393
1394 case Token::NOT: {
1395 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
1396 Label materialize_true, materialize_false, done;
1397 // Initially assume a pure test context. Notice that the labels are
1398 // swapped.
1399 Label* if_true = false_label_;
1400 Label* if_false = true_label_;
1401 switch (context_) {
1402 case Expression::kUninitialized:
1403 UNREACHABLE();
1404 break;
1405 case Expression::kEffect:
1406 if_true = &done;
1407 if_false = &done;
1408 break;
1409 case Expression::kValue:
1410 if_true = &materialize_false;
1411 if_false = &materialize_true;
1412 break;
1413 case Expression::kTest:
1414 break;
1415 case Expression::kValueTest:
1416 if_false = &materialize_true;
1417 break;
1418 case Expression::kTestValue:
1419 if_true = &materialize_false;
1420 break;
1421 }
1422 VisitForControl(expr->expression(), if_true, if_false);
1423 Apply(context_, if_false, if_true); // Labels swapped.
1424 break;
1425 }
1426
1427 case Token::TYPEOF: {
1428 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
1429 VariableProxy* proxy = expr->expression()->AsVariableProxy();
1430 if (proxy != NULL &&
1431 !proxy->var()->is_this() &&
1432 proxy->var()->is_global()) {
1433 Comment cmnt(masm_, "Global variable");
1434 __ ldr(r0, CodeGenerator::GlobalObject());
1435 __ push(r0);
1436 __ mov(r2, Operand(proxy->name()));
1437 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1438 // Use a regular load, not a contextual load, to avoid a reference
1439 // error.
1440 __ Call(ic, RelocInfo::CODE_TARGET);
1441 __ str(r0, MemOperand(sp));
1442 } else if (proxy != NULL &&
1443 proxy->var()->slot() != NULL &&
1444 proxy->var()->slot()->type() == Slot::LOOKUP) {
1445 __ mov(r0, Operand(proxy->name()));
1446 __ stm(db_w, sp, cp.bit() | r0.bit());
1447 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
1448 __ push(r0);
1449 } else {
1450 // This expression cannot throw a reference error at the top level.
1451 VisitForValue(expr->expression(), kStack);
1452 }
1453
1454 __ CallRuntime(Runtime::kTypeof, 1);
1455 Apply(context_, r0);
1456 break;
1457 }
1458
1459 case Token::ADD: {
1460 Comment cmt(masm_, "[ UnaryOperation (ADD)");
1461 VisitForValue(expr->expression(), kAccumulator);
1462 Label no_conversion;
1463 __ tst(result_register(), Operand(kSmiTagMask));
1464 __ b(eq, &no_conversion);
1465 __ push(r0);
1466 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
1467 __ bind(&no_conversion);
1468 Apply(context_, result_register());
1469 break;
1470 }
1471
1472 case Token::SUB: {
1473 Comment cmt(masm_, "[ UnaryOperation (SUB)");
1474 bool overwrite =
1475 (expr->expression()->AsBinaryOperation() != NULL &&
1476 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
1477 GenericUnaryOpStub stub(Token::SUB, overwrite);
1478 // GenericUnaryOpStub expects the argument to be in the
1479 // accumulator register r0.
1480 VisitForValue(expr->expression(), kAccumulator);
1481 __ CallStub(&stub);
1482 Apply(context_, r0);
1483 break;
1484 }
1485
1486 case Token::BIT_NOT: {
1487 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
1488 bool overwrite =
1489 (expr->expression()->AsBinaryOperation() != NULL &&
1490 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
1491 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
1492 // GenericUnaryOpStub expects the argument to be in the
1493 // accumulator register r0.
1494 VisitForValue(expr->expression(), kAccumulator);
1495 // Avoid calling the stub for Smis.
1496 Label smi, done;
1497 __ tst(result_register(), Operand(kSmiTagMask));
1498 __ b(eq, &smi);
1499 // Non-smi: call stub leaving result in accumulator register.
1500 __ CallStub(&stub);
1501 __ b(&done);
1502 // Perform operation directly on Smis.
1503 __ bind(&smi);
1504 __ mvn(result_register(), Operand(result_register()));
1505 // Bit-clear inverted smi-tag.
1506 __ bic(result_register(), result_register(), Operand(kSmiTagMask));
1507 __ bind(&done);
1508 Apply(context_, result_register());
1509 break;
1510 }
1511
1512 default:
1513 UNREACHABLE();
1514 }
1515}
1516
1517
1518void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
1519 Comment cmnt(masm_, "[ CountOperation");
1520
1521 // Expression can only be a property, a global or a (parameter or local)
1522 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1523 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1524 LhsKind assign_type = VARIABLE;
1525 Property* prop = expr->expression()->AsProperty();
1526 // In case of a property we use the uninitialized expression context
1527 // of the key to detect a named property.
1528 if (prop != NULL) {
1529 assign_type =
1530 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
1531 }
1532
1533 // Evaluate expression and get value.
1534 if (assign_type == VARIABLE) {
1535 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
1536 Location saved_location = location_;
1537 location_ = kAccumulator;
1538 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
1539 Expression::kValue);
1540 location_ = saved_location;
1541 } else {
1542 // Reserve space for result of postfix operation.
1543 if (expr->is_postfix() && context_ != Expression::kEffect) {
1544 __ mov(ip, Operand(Smi::FromInt(0)));
1545 __ push(ip);
1546 }
1547 VisitForValue(prop->obj(), kStack);
1548 if (assign_type == NAMED_PROPERTY) {
1549 EmitNamedPropertyLoad(prop);
1550 } else {
1551 VisitForValue(prop->key(), kStack);
1552 EmitKeyedPropertyLoad(prop);
1553 }
1554 }
1555
1556 // Call ToNumber only if operand is not a smi.
1557 Label no_conversion;
1558 __ tst(r0, Operand(kSmiTagMask));
1559 __ b(eq, &no_conversion);
1560 __ push(r0);
1561 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
1562 __ bind(&no_conversion);
1563
1564 // Save result for postfix expressions.
1565 if (expr->is_postfix()) {
1566 switch (context_) {
1567 case Expression::kUninitialized:
1568 UNREACHABLE();
1569 case Expression::kEffect:
1570 // Do not save result.
1571 break;
1572 case Expression::kValue:
1573 case Expression::kTest:
1574 case Expression::kValueTest:
1575 case Expression::kTestValue:
1576 // Save the result on the stack. If we have a named or keyed property
1577 // we store the result under the receiver that is currently on top
1578 // of the stack.
1579 switch (assign_type) {
1580 case VARIABLE:
1581 __ push(r0);
1582 break;
1583 case NAMED_PROPERTY:
1584 __ str(r0, MemOperand(sp, kPointerSize));
1585 break;
1586 case KEYED_PROPERTY:
1587 __ str(r0, MemOperand(sp, 2 * kPointerSize));
1588 break;
1589 }
1590 break;
1591 }
1592 }
1593
1594
1595 // Inline smi case if we are in a loop.
1596 Label stub_call, done;
1597 if (loop_depth() > 0) {
1598 __ add(r0, r0, Operand(expr->op() == Token::INC
1599 ? Smi::FromInt(1)
1600 : Smi::FromInt(-1)));
1601 __ b(vs, &stub_call);
1602 // We could eliminate this smi check if we split the code at
1603 // the first smi check before calling ToNumber.
1604 __ tst(r0, Operand(kSmiTagMask));
1605 __ b(eq, &done);
1606 __ bind(&stub_call);
1607 // Call stub. Undo operation first.
1608 __ sub(r0, r0, Operand(r1));
1609 }
1610 __ mov(r1, Operand(expr->op() == Token::INC
1611 ? Smi::FromInt(1)
1612 : Smi::FromInt(-1)));
1613 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE);
1614 __ CallStub(&stub);
1615 __ bind(&done);
1616
1617 // Store the value returned in r0.
1618 switch (assign_type) {
1619 case VARIABLE:
1620 if (expr->is_postfix()) {
1621 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
1622 Expression::kEffect);
1623 // For all contexts except kEffect: We have the result on
1624 // top of the stack.
1625 if (context_ != Expression::kEffect) {
1626 ApplyTOS(context_);
1627 }
1628 } else {
1629 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
1630 context_);
1631 }
1632 break;
1633 case NAMED_PROPERTY: {
1634 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001635 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001636 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1637 __ Call(ic, RelocInfo::CODE_TARGET);
1638 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001639 if (context_ != Expression::kEffect) {
1640 ApplyTOS(context_);
1641 }
1642 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00001643 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001644 }
1645 break;
1646 }
1647 case KEYED_PROPERTY: {
1648 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1649 __ Call(ic, RelocInfo::CODE_TARGET);
1650 if (expr->is_postfix()) {
1651 __ Drop(2); // Result is on the stack under the key and the receiver.
1652 if (context_ != Expression::kEffect) {
1653 ApplyTOS(context_);
1654 }
1655 } else {
1656 DropAndApply(2, context_, r0);
1657 }
1658 break;
1659 }
1660 }
1661}
1662
1663
1664void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
1665 Comment cmnt(masm_, "[ BinaryOperation");
1666 switch (expr->op()) {
1667 case Token::COMMA:
1668 VisitForEffect(expr->left());
1669 Visit(expr->right());
1670 break;
1671
1672 case Token::OR:
1673 case Token::AND:
1674 EmitLogicalOperation(expr);
1675 break;
1676
1677 case Token::ADD:
1678 case Token::SUB:
1679 case Token::DIV:
1680 case Token::MOD:
1681 case Token::MUL:
1682 case Token::BIT_OR:
1683 case Token::BIT_AND:
1684 case Token::BIT_XOR:
1685 case Token::SHL:
1686 case Token::SHR:
1687 case Token::SAR:
1688 VisitForValue(expr->left(), kStack);
1689 VisitForValue(expr->right(), kAccumulator);
1690 EmitBinaryOp(expr->op(), context_);
1691 break;
1692
1693 default:
1694 UNREACHABLE();
1695 }
1696}
1697
1698
1699void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
1700 Comment cmnt(masm_, "[ CompareOperation");
1701
1702 // Always perform the comparison for its control flow. Pack the result
1703 // into the expression's context after the comparison is performed.
1704 Label materialize_true, materialize_false, done;
1705 // Initially assume we are in a test context.
1706 Label* if_true = true_label_;
1707 Label* if_false = false_label_;
1708 switch (context_) {
1709 case Expression::kUninitialized:
1710 UNREACHABLE();
1711 break;
1712 case Expression::kEffect:
1713 if_true = &done;
1714 if_false = &done;
1715 break;
1716 case Expression::kValue:
1717 if_true = &materialize_true;
1718 if_false = &materialize_false;
1719 break;
1720 case Expression::kTest:
1721 break;
1722 case Expression::kValueTest:
1723 if_true = &materialize_true;
1724 break;
1725 case Expression::kTestValue:
1726 if_false = &materialize_false;
1727 break;
1728 }
1729
1730 VisitForValue(expr->left(), kStack);
1731 switch (expr->op()) {
1732 case Token::IN:
1733 VisitForValue(expr->right(), kStack);
1734 __ InvokeBuiltin(Builtins::IN, CALL_JS);
1735 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
1736 __ cmp(r0, ip);
1737 __ b(eq, if_true);
1738 __ jmp(if_false);
1739 break;
1740
1741 case Token::INSTANCEOF: {
1742 VisitForValue(expr->right(), kStack);
1743 InstanceofStub stub;
1744 __ CallStub(&stub);
1745 __ tst(r0, r0);
1746 __ b(eq, if_true); // The stub returns 0 for true.
1747 __ jmp(if_false);
1748 break;
1749 }
1750
1751 default: {
1752 VisitForValue(expr->right(), kAccumulator);
1753 Condition cc = eq;
1754 bool strict = false;
1755 switch (expr->op()) {
1756 case Token::EQ_STRICT:
1757 strict = true;
1758 // Fall through
1759 case Token::EQ:
1760 cc = eq;
1761 __ pop(r1);
1762 break;
1763 case Token::LT:
1764 cc = lt;
1765 __ pop(r1);
1766 break;
1767 case Token::GT:
1768 // Reverse left and right sides to obtain ECMA-262 conversion order.
1769 cc = lt;
1770 __ mov(r1, result_register());
1771 __ pop(r0);
1772 break;
1773 case Token::LTE:
1774 // Reverse left and right sides to obtain ECMA-262 conversion order.
1775 cc = ge;
1776 __ mov(r1, result_register());
1777 __ pop(r0);
1778 break;
1779 case Token::GTE:
1780 cc = ge;
1781 __ pop(r1);
1782 break;
1783 case Token::IN:
1784 case Token::INSTANCEOF:
1785 default:
1786 UNREACHABLE();
1787 }
1788
1789 // The comparison stub expects the smi vs. smi case to be handled
1790 // before it is called.
1791 Label slow_case;
1792 __ orr(r2, r0, Operand(r1));
1793 __ tst(r2, Operand(kSmiTagMask));
1794 __ b(ne, &slow_case);
1795 __ cmp(r1, r0);
1796 __ b(cc, if_true);
1797 __ jmp(if_false);
1798
1799 __ bind(&slow_case);
1800 CompareStub stub(cc, strict);
1801 __ CallStub(&stub);
1802 __ cmp(r0, Operand(0));
1803 __ b(cc, if_true);
1804 __ jmp(if_false);
1805 }
1806 }
1807
1808 // Convert the result of the comparison into one expected for this
1809 // expression's context.
1810 Apply(context_, if_true, if_false);
1811}
1812
1813
1814void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
1815 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1816 Apply(context_, r0);
1817}
1818
1819
1820Register FullCodeGenerator::result_register() { return r0; }
1821
1822
1823Register FullCodeGenerator::context_register() { return cp; }
1824
1825
1826void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
1827 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
1828 __ str(value, MemOperand(fp, frame_offset));
1829}
1830
1831
1832void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
1833 __ ldr(dst, CodeGenerator::ContextOperand(cp, context_index));
1834}
1835
1836
1837// ----------------------------------------------------------------------------
1838// Non-local control flow support.
1839
1840void FullCodeGenerator::EnterFinallyBlock() {
1841 ASSERT(!result_register().is(r1));
1842 // Store result register while executing finally block.
1843 __ push(result_register());
1844 // Cook return address in link register to stack (smi encoded Code* delta)
1845 __ sub(r1, lr, Operand(masm_->CodeObject()));
1846 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
1847 ASSERT_EQ(0, kSmiTag);
1848 __ add(r1, r1, Operand(r1)); // Convert to smi.
1849 __ push(r1);
1850}
1851
1852
1853void FullCodeGenerator::ExitFinallyBlock() {
1854 ASSERT(!result_register().is(r1));
1855 // Restore result register from stack.
1856 __ pop(r1);
1857 // Uncook return address and return.
1858 __ pop(result_register());
1859 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
1860 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
1861 __ add(pc, r1, Operand(masm_->CodeObject()));
1862}
1863
1864
1865#undef __
1866
1867} } // namespace v8::internal