blob: a70cf44f800ebdafec74d06e28372680fa8a75db [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");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000786 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
787 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
788 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
789 __ mov(r1, Operand(expr->constant_properties()));
790 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
791 __ stm(db_w, sp, r3.bit() | r2.bit() | r1.bit() | r0.bit());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000792 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000793 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000794 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000795 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000796 }
797
798 // If result_saved is true the result is on top of the stack. If
799 // result_saved is false the result is in r0.
800 bool result_saved = false;
801
802 for (int i = 0; i < expr->properties()->length(); i++) {
803 ObjectLiteral::Property* property = expr->properties()->at(i);
804 if (property->IsCompileTimeValue()) continue;
805
806 Literal* key = property->key();
807 Expression* value = property->value();
808 if (!result_saved) {
809 __ push(r0); // Save result on stack
810 result_saved = true;
811 }
812 switch (property->kind()) {
813 case ObjectLiteral::Property::CONSTANT:
814 UNREACHABLE();
815 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
816 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
817 // Fall through.
818 case ObjectLiteral::Property::COMPUTED:
819 if (key->handle()->IsSymbol()) {
820 VisitForValue(value, kAccumulator);
821 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000822 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000823 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
824 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000825 break;
826 }
827 // Fall through.
828 case ObjectLiteral::Property::PROTOTYPE:
829 // Duplicate receiver on stack.
830 __ ldr(r0, MemOperand(sp));
831 __ push(r0);
832 VisitForValue(key, kStack);
833 VisitForValue(value, kStack);
834 __ CallRuntime(Runtime::kSetProperty, 3);
835 break;
836 case ObjectLiteral::Property::GETTER:
837 case ObjectLiteral::Property::SETTER:
838 // Duplicate receiver on stack.
839 __ ldr(r0, MemOperand(sp));
840 __ push(r0);
841 VisitForValue(key, kStack);
842 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
843 Smi::FromInt(1) :
844 Smi::FromInt(0)));
845 __ push(r1);
846 VisitForValue(value, kStack);
847 __ CallRuntime(Runtime::kDefineAccessor, 4);
848 break;
849 }
850 }
851
852 if (result_saved) {
853 ApplyTOS(context_);
854 } else {
855 Apply(context_, r0);
856 }
857}
858
859
860void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
861 Comment cmnt(masm_, "[ ArrayLiteral");
862 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
863 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
864 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
865 __ mov(r1, Operand(expr->constant_elements()));
866 __ stm(db_w, sp, r3.bit() | r2.bit() | r1.bit());
867 if (expr->depth() > 1) {
868 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
869 } else {
870 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
871 }
872
873 bool result_saved = false; // Is the result saved to the stack?
874
875 // Emit code to evaluate all the non-constant subexpressions and to store
876 // them into the newly cloned array.
877 ZoneList<Expression*>* subexprs = expr->values();
878 for (int i = 0, len = subexprs->length(); i < len; i++) {
879 Expression* subexpr = subexprs->at(i);
880 // If the subexpression is a literal or a simple materialized literal it
881 // is already set in the cloned array.
882 if (subexpr->AsLiteral() != NULL ||
883 CompileTimeValue::IsCompileTimeValue(subexpr)) {
884 continue;
885 }
886
887 if (!result_saved) {
888 __ push(r0);
889 result_saved = true;
890 }
891 VisitForValue(subexpr, kAccumulator);
892
893 // Store the subexpression value in the array's elements.
894 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
895 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
896 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
897 __ str(result_register(), FieldMemOperand(r1, offset));
898
899 // Update the write barrier for the array store with r0 as the scratch
900 // register.
901 __ mov(r2, Operand(offset));
902 __ RecordWrite(r1, r2, result_register());
903 }
904
905 if (result_saved) {
906 ApplyTOS(context_);
907 } else {
908 Apply(context_, r0);
909 }
910}
911
912
ager@chromium.org5c838252010-02-19 08:53:10 +0000913void FullCodeGenerator::VisitAssignment(Assignment* expr) {
914 Comment cmnt(masm_, "[ Assignment");
915 ASSERT(expr->op() != Token::INIT_CONST);
916 // Left-hand side can only be a property, a global or a (parameter or local)
917 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
918 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
919 LhsKind assign_type = VARIABLE;
920 Property* prop = expr->target()->AsProperty();
921 if (prop != NULL) {
922 assign_type =
923 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
924 }
925
926 // Evaluate LHS expression.
927 switch (assign_type) {
928 case VARIABLE:
929 // Nothing to do here.
930 break;
931 case NAMED_PROPERTY:
932 if (expr->is_compound()) {
933 // We need the receiver both on the stack and in the accumulator.
934 VisitForValue(prop->obj(), kAccumulator);
935 __ push(result_register());
936 } else {
937 VisitForValue(prop->obj(), kStack);
938 }
939 break;
940 case KEYED_PROPERTY:
941 VisitForValue(prop->obj(), kStack);
942 VisitForValue(prop->key(), kStack);
943 break;
944 }
945
946 // If we have a compound assignment: Get value of LHS expression and
947 // store in on top of the stack.
948 if (expr->is_compound()) {
949 Location saved_location = location_;
950 location_ = kStack;
951 switch (assign_type) {
952 case VARIABLE:
953 EmitVariableLoad(expr->target()->AsVariableProxy()->var(),
954 Expression::kValue);
955 break;
956 case NAMED_PROPERTY:
957 EmitNamedPropertyLoad(prop);
958 __ push(result_register());
959 break;
960 case KEYED_PROPERTY:
961 EmitKeyedPropertyLoad(prop);
962 __ push(result_register());
963 break;
964 }
965 location_ = saved_location;
966 }
967
968 // Evaluate RHS expression.
969 Expression* rhs = expr->value();
970 VisitForValue(rhs, kAccumulator);
971
972 // If we have a compound assignment: Apply operator.
973 if (expr->is_compound()) {
974 Location saved_location = location_;
975 location_ = kAccumulator;
976 EmitBinaryOp(expr->binary_op(), Expression::kValue);
977 location_ = saved_location;
978 }
979
980 // Record source position before possible IC call.
981 SetSourcePosition(expr->position());
982
983 // Store the value.
984 switch (assign_type) {
985 case VARIABLE:
986 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
987 context_);
988 break;
989 case NAMED_PROPERTY:
990 EmitNamedPropertyAssignment(expr);
991 break;
992 case KEYED_PROPERTY:
993 EmitKeyedPropertyAssignment(expr);
994 break;
995 }
996}
997
998
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000999void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1000 SetSourcePosition(prop->position());
1001 Literal* key = prop->key()->AsLiteral();
1002 __ mov(r2, Operand(key->handle()));
1003 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1004 __ Call(ic, RelocInfo::CODE_TARGET);
1005}
1006
1007
1008void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1009 SetSourcePosition(prop->position());
1010 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1011 __ Call(ic, RelocInfo::CODE_TARGET);
1012}
1013
1014
1015void FullCodeGenerator::EmitBinaryOp(Token::Value op,
1016 Expression::Context context) {
1017 __ pop(r1);
1018 GenericBinaryOpStub stub(op, NO_OVERWRITE);
1019 __ CallStub(&stub);
1020 Apply(context, r0);
1021}
1022
1023
1024void FullCodeGenerator::EmitVariableAssignment(Variable* var,
1025 Expression::Context context) {
1026 // Three main cases: global variables, lookup slots, and all other
1027 // types of slots. Left-hand-side parameters that rewrite to
1028 // explicit property accesses do not reach here.
1029 ASSERT(var != NULL);
1030 ASSERT(var->is_global() || var->slot() != NULL);
1031
1032 Slot* slot = var->slot();
1033 if (var->is_global()) {
1034 ASSERT(!var->is_this());
1035 // Assignment to a global variable. Use inline caching for the
1036 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001037 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001038 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001039 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001040 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1041 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001042
1043 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
1044 __ push(result_register()); // Value.
1045 __ mov(r1, Operand(var->name()));
1046 __ stm(db_w, sp, cp.bit() | r1.bit()); // Context and name.
1047 __ CallRuntime(Runtime::kStoreContextSlot, 3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001048
1049 } else if (var->slot() != NULL) {
1050 Slot* slot = var->slot();
1051 switch (slot->type()) {
1052 case Slot::LOCAL:
1053 case Slot::PARAMETER:
1054 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1055 break;
1056
1057 case Slot::CONTEXT: {
1058 MemOperand target = EmitSlotSearch(slot, r1);
1059 __ str(result_register(), target);
1060
1061 // RecordWrite may destroy all its register arguments.
1062 __ mov(r3, result_register());
1063 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
1064
1065 __ mov(r2, Operand(offset));
1066 __ RecordWrite(r1, r2, r3);
1067 break;
1068 }
1069
1070 case Slot::LOOKUP:
1071 UNREACHABLE();
1072 break;
1073 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001074
1075 } else {
1076 // Variables rewritten as properties are not treated as variables in
1077 // assignments.
1078 UNREACHABLE();
1079 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001080 Apply(context, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001081}
1082
1083
1084void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1085 // Assignment to a property, using a named store IC.
1086 Property* prop = expr->target()->AsProperty();
1087 ASSERT(prop != NULL);
1088 ASSERT(prop->key()->AsLiteral() != NULL);
1089
1090 // If the assignment starts a block of assignments to the same object,
1091 // change to slow case to avoid the quadratic behavior of repeatedly
1092 // adding fast properties.
1093 if (expr->starts_initialization_block()) {
1094 __ push(result_register());
1095 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1096 __ push(ip);
1097 __ CallRuntime(Runtime::kToSlowProperties, 1);
1098 __ pop(result_register());
1099 }
1100
1101 // Record source code position before IC call.
1102 SetSourcePosition(expr->position());
1103 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001104 if (expr->ends_initialization_block()) {
1105 __ ldr(r1, MemOperand(sp));
1106 } else {
1107 __ pop(r1);
1108 }
1109
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001110 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1111 __ Call(ic, RelocInfo::CODE_TARGET);
1112
1113 // If the assignment ends an initialization block, revert to fast case.
1114 if (expr->ends_initialization_block()) {
1115 __ push(r0); // Result of assignment, saved even if not needed.
1116 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is under value.
1117 __ push(ip);
1118 __ CallRuntime(Runtime::kToFastProperties, 1);
1119 __ pop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001120 DropAndApply(1, context_, r0);
1121 } else {
1122 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001123 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001124}
1125
1126
1127void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1128 // Assignment to a property, using a keyed store IC.
1129
1130 // If the assignment starts a block of assignments to the same object,
1131 // change to slow case to avoid the quadratic behavior of repeatedly
1132 // adding fast properties.
1133 if (expr->starts_initialization_block()) {
1134 __ push(result_register());
1135 // Receiver is now under the key and value.
1136 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1137 __ push(ip);
1138 __ CallRuntime(Runtime::kToSlowProperties, 1);
1139 __ pop(result_register());
1140 }
1141
1142 // Record source code position before IC call.
1143 SetSourcePosition(expr->position());
1144 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1145 __ Call(ic, RelocInfo::CODE_TARGET);
1146
1147 // If the assignment ends an initialization block, revert to fast case.
1148 if (expr->ends_initialization_block()) {
1149 __ push(r0); // Result of assignment, saved even if not needed.
1150 // Receiver is under the key and value.
1151 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1152 __ push(ip);
1153 __ CallRuntime(Runtime::kToFastProperties, 1);
1154 __ pop(r0);
1155 }
1156
1157 // Receiver and key are still on stack.
1158 DropAndApply(2, context_, r0);
1159}
1160
1161
1162void FullCodeGenerator::VisitProperty(Property* expr) {
1163 Comment cmnt(masm_, "[ Property");
1164 Expression* key = expr->key();
1165
1166 // Evaluate receiver.
1167 VisitForValue(expr->obj(), kStack);
1168
1169 if (key->IsPropertyName()) {
1170 EmitNamedPropertyLoad(expr);
1171 // Drop receiver left on the stack by IC.
1172 DropAndApply(1, context_, r0);
1173 } else {
1174 VisitForValue(expr->key(), kStack);
1175 EmitKeyedPropertyLoad(expr);
1176 // Drop key and receiver left on the stack by IC.
1177 DropAndApply(2, context_, r0);
1178 }
1179}
1180
1181void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001182 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001183 RelocInfo::Mode mode) {
1184 // Code common for calls using the IC.
1185 ZoneList<Expression*>* args = expr->arguments();
1186 int arg_count = args->length();
1187 for (int i = 0; i < arg_count; i++) {
1188 VisitForValue(args->at(i), kStack);
1189 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001190 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001191 // Record source position for debugger.
1192 SetSourcePosition(expr->position());
1193 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001194 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1195 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001196 __ Call(ic, mode);
1197 // Restore context register.
1198 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001199 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001200}
1201
1202
1203void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1204 // Code common for calls using the call stub.
1205 ZoneList<Expression*>* args = expr->arguments();
1206 int arg_count = args->length();
1207 for (int i = 0; i < arg_count; i++) {
1208 VisitForValue(args->at(i), kStack);
1209 }
1210 // Record source position for debugger.
1211 SetSourcePosition(expr->position());
1212 CallFunctionStub stub(arg_count, NOT_IN_LOOP, RECEIVER_MIGHT_BE_VALUE);
1213 __ CallStub(&stub);
1214 // Restore context register.
1215 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001216 DropAndApply(1, context_, r0);
1217}
1218
1219
1220void FullCodeGenerator::VisitCall(Call* expr) {
1221 Comment cmnt(masm_, "[ Call");
1222 Expression* fun = expr->expression();
1223 Variable* var = fun->AsVariableProxy()->AsVariable();
1224
1225 if (var != NULL && var->is_possibly_eval()) {
1226 // Call to the identifier 'eval'.
1227 UNREACHABLE();
1228 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001229 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001230 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001231 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001232 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1233 } else if (var != NULL && var->slot() != NULL &&
1234 var->slot()->type() == Slot::LOOKUP) {
1235 // Call to a lookup slot.
1236 UNREACHABLE();
1237 } else if (fun->AsProperty() != NULL) {
1238 // Call to an object property.
1239 Property* prop = fun->AsProperty();
1240 Literal* key = prop->key()->AsLiteral();
1241 if (key != NULL && key->handle()->IsSymbol()) {
1242 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001243 VisitForValue(prop->obj(), kStack);
1244 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1245 } else {
1246 // Call to a keyed property, use keyed load IC followed by function
1247 // call.
1248 VisitForValue(prop->obj(), kStack);
1249 VisitForValue(prop->key(), kStack);
1250 // Record source code position for IC call.
1251 SetSourcePosition(prop->position());
1252 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1253 __ Call(ic, RelocInfo::CODE_TARGET);
1254 // Load receiver object into r1.
1255 if (prop->is_synthetic()) {
1256 __ ldr(r1, CodeGenerator::GlobalObject());
1257 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1258 } else {
1259 __ ldr(r1, MemOperand(sp, kPointerSize));
1260 }
1261 // Overwrite (object, key) with (function, receiver).
1262 __ str(r0, MemOperand(sp, kPointerSize));
1263 __ str(r1, MemOperand(sp));
1264 EmitCallWithStub(expr);
1265 }
1266 } else {
1267 // Call to some other expression. If the expression is an anonymous
1268 // function literal not called in a loop, mark it as one that should
1269 // also use the fast code generator.
1270 FunctionLiteral* lit = fun->AsFunctionLiteral();
1271 if (lit != NULL &&
1272 lit->name()->Equals(Heap::empty_string()) &&
1273 loop_depth() == 0) {
1274 lit->set_try_full_codegen(true);
1275 }
1276 VisitForValue(fun, kStack);
1277 // Load global receiver object.
1278 __ ldr(r1, CodeGenerator::GlobalObject());
1279 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1280 __ push(r1);
1281 // Emit function call.
1282 EmitCallWithStub(expr);
1283 }
1284}
1285
1286
1287void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1288 Comment cmnt(masm_, "[ CallNew");
1289 // According to ECMA-262, section 11.2.2, page 44, the function
1290 // expression in new calls must be evaluated before the
1291 // arguments.
1292 // Push function on the stack.
1293 VisitForValue(expr->expression(), kStack);
1294
1295 // Push global object (receiver).
1296 __ ldr(r0, CodeGenerator::GlobalObject());
1297 __ push(r0);
1298 // Push the arguments ("left-to-right") on the stack.
1299 ZoneList<Expression*>* args = expr->arguments();
1300 int arg_count = args->length();
1301 for (int i = 0; i < arg_count; i++) {
1302 VisitForValue(args->at(i), kStack);
1303 }
1304
1305 // Call the construct call builtin that handles allocation and
1306 // constructor invocation.
1307 SetSourcePosition(expr->position());
1308
1309 // Load function, arg_count into r1 and r0.
1310 __ mov(r0, Operand(arg_count));
1311 // Function is in sp[arg_count + 1].
1312 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1313
1314 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1315 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
1316
1317 // Replace function on TOS with result in r0, or pop it.
1318 DropAndApply(1, context_, r0);
1319}
1320
1321
1322void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
1323 Comment cmnt(masm_, "[ CallRuntime");
1324 ZoneList<Expression*>* args = expr->arguments();
1325
1326 if (expr->is_jsruntime()) {
1327 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001328 __ ldr(r0, CodeGenerator::GlobalObject());
1329 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001330 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001331 }
1332
1333 // Push the arguments ("left-to-right").
1334 int arg_count = args->length();
1335 for (int i = 0; i < arg_count; i++) {
1336 VisitForValue(args->at(i), kStack);
1337 }
1338
1339 if (expr->is_jsruntime()) {
1340 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00001341 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001342 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
1343 NOT_IN_LOOP);
1344 __ Call(ic, RelocInfo::CODE_TARGET);
1345 // Restore context register.
1346 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001347 } else {
1348 // Call the C runtime function.
1349 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001350 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001351 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001352}
1353
1354
1355void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
1356 switch (expr->op()) {
1357 case Token::VOID: {
1358 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
1359 VisitForEffect(expr->expression());
1360 switch (context_) {
1361 case Expression::kUninitialized:
1362 UNREACHABLE();
1363 break;
1364 case Expression::kEffect:
1365 break;
1366 case Expression::kValue:
1367 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
1368 switch (location_) {
1369 case kAccumulator:
1370 break;
1371 case kStack:
1372 __ push(result_register());
1373 break;
1374 }
1375 break;
1376 case Expression::kTestValue:
1377 // Value is false so it's needed.
1378 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
1379 switch (location_) {
1380 case kAccumulator:
1381 break;
1382 case kStack:
1383 __ push(result_register());
1384 break;
1385 }
1386 // Fall through.
1387 case Expression::kTest:
1388 case Expression::kValueTest:
1389 __ jmp(false_label_);
1390 break;
1391 }
1392 break;
1393 }
1394
1395 case Token::NOT: {
1396 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
1397 Label materialize_true, materialize_false, done;
1398 // Initially assume a pure test context. Notice that the labels are
1399 // swapped.
1400 Label* if_true = false_label_;
1401 Label* if_false = true_label_;
1402 switch (context_) {
1403 case Expression::kUninitialized:
1404 UNREACHABLE();
1405 break;
1406 case Expression::kEffect:
1407 if_true = &done;
1408 if_false = &done;
1409 break;
1410 case Expression::kValue:
1411 if_true = &materialize_false;
1412 if_false = &materialize_true;
1413 break;
1414 case Expression::kTest:
1415 break;
1416 case Expression::kValueTest:
1417 if_false = &materialize_true;
1418 break;
1419 case Expression::kTestValue:
1420 if_true = &materialize_false;
1421 break;
1422 }
1423 VisitForControl(expr->expression(), if_true, if_false);
1424 Apply(context_, if_false, if_true); // Labels swapped.
1425 break;
1426 }
1427
1428 case Token::TYPEOF: {
1429 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
1430 VariableProxy* proxy = expr->expression()->AsVariableProxy();
1431 if (proxy != NULL &&
1432 !proxy->var()->is_this() &&
1433 proxy->var()->is_global()) {
1434 Comment cmnt(masm_, "Global variable");
1435 __ ldr(r0, CodeGenerator::GlobalObject());
1436 __ push(r0);
1437 __ mov(r2, Operand(proxy->name()));
1438 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1439 // Use a regular load, not a contextual load, to avoid a reference
1440 // error.
1441 __ Call(ic, RelocInfo::CODE_TARGET);
1442 __ str(r0, MemOperand(sp));
1443 } else if (proxy != NULL &&
1444 proxy->var()->slot() != NULL &&
1445 proxy->var()->slot()->type() == Slot::LOOKUP) {
1446 __ mov(r0, Operand(proxy->name()));
1447 __ stm(db_w, sp, cp.bit() | r0.bit());
1448 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
1449 __ push(r0);
1450 } else {
1451 // This expression cannot throw a reference error at the top level.
1452 VisitForValue(expr->expression(), kStack);
1453 }
1454
1455 __ CallRuntime(Runtime::kTypeof, 1);
1456 Apply(context_, r0);
1457 break;
1458 }
1459
1460 case Token::ADD: {
1461 Comment cmt(masm_, "[ UnaryOperation (ADD)");
1462 VisitForValue(expr->expression(), kAccumulator);
1463 Label no_conversion;
1464 __ tst(result_register(), Operand(kSmiTagMask));
1465 __ b(eq, &no_conversion);
1466 __ push(r0);
1467 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
1468 __ bind(&no_conversion);
1469 Apply(context_, result_register());
1470 break;
1471 }
1472
1473 case Token::SUB: {
1474 Comment cmt(masm_, "[ UnaryOperation (SUB)");
1475 bool overwrite =
1476 (expr->expression()->AsBinaryOperation() != NULL &&
1477 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
1478 GenericUnaryOpStub stub(Token::SUB, overwrite);
1479 // GenericUnaryOpStub expects the argument to be in the
1480 // accumulator register r0.
1481 VisitForValue(expr->expression(), kAccumulator);
1482 __ CallStub(&stub);
1483 Apply(context_, r0);
1484 break;
1485 }
1486
1487 case Token::BIT_NOT: {
1488 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
1489 bool overwrite =
1490 (expr->expression()->AsBinaryOperation() != NULL &&
1491 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
1492 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
1493 // GenericUnaryOpStub expects the argument to be in the
1494 // accumulator register r0.
1495 VisitForValue(expr->expression(), kAccumulator);
1496 // Avoid calling the stub for Smis.
1497 Label smi, done;
1498 __ tst(result_register(), Operand(kSmiTagMask));
1499 __ b(eq, &smi);
1500 // Non-smi: call stub leaving result in accumulator register.
1501 __ CallStub(&stub);
1502 __ b(&done);
1503 // Perform operation directly on Smis.
1504 __ bind(&smi);
1505 __ mvn(result_register(), Operand(result_register()));
1506 // Bit-clear inverted smi-tag.
1507 __ bic(result_register(), result_register(), Operand(kSmiTagMask));
1508 __ bind(&done);
1509 Apply(context_, result_register());
1510 break;
1511 }
1512
1513 default:
1514 UNREACHABLE();
1515 }
1516}
1517
1518
1519void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
1520 Comment cmnt(masm_, "[ CountOperation");
1521
1522 // Expression can only be a property, a global or a (parameter or local)
1523 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1524 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1525 LhsKind assign_type = VARIABLE;
1526 Property* prop = expr->expression()->AsProperty();
1527 // In case of a property we use the uninitialized expression context
1528 // of the key to detect a named property.
1529 if (prop != NULL) {
1530 assign_type =
1531 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
1532 }
1533
1534 // Evaluate expression and get value.
1535 if (assign_type == VARIABLE) {
1536 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
1537 Location saved_location = location_;
1538 location_ = kAccumulator;
1539 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
1540 Expression::kValue);
1541 location_ = saved_location;
1542 } else {
1543 // Reserve space for result of postfix operation.
1544 if (expr->is_postfix() && context_ != Expression::kEffect) {
1545 __ mov(ip, Operand(Smi::FromInt(0)));
1546 __ push(ip);
1547 }
1548 VisitForValue(prop->obj(), kStack);
1549 if (assign_type == NAMED_PROPERTY) {
1550 EmitNamedPropertyLoad(prop);
1551 } else {
1552 VisitForValue(prop->key(), kStack);
1553 EmitKeyedPropertyLoad(prop);
1554 }
1555 }
1556
1557 // Call ToNumber only if operand is not a smi.
1558 Label no_conversion;
1559 __ tst(r0, Operand(kSmiTagMask));
1560 __ b(eq, &no_conversion);
1561 __ push(r0);
1562 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
1563 __ bind(&no_conversion);
1564
1565 // Save result for postfix expressions.
1566 if (expr->is_postfix()) {
1567 switch (context_) {
1568 case Expression::kUninitialized:
1569 UNREACHABLE();
1570 case Expression::kEffect:
1571 // Do not save result.
1572 break;
1573 case Expression::kValue:
1574 case Expression::kTest:
1575 case Expression::kValueTest:
1576 case Expression::kTestValue:
1577 // Save the result on the stack. If we have a named or keyed property
1578 // we store the result under the receiver that is currently on top
1579 // of the stack.
1580 switch (assign_type) {
1581 case VARIABLE:
1582 __ push(r0);
1583 break;
1584 case NAMED_PROPERTY:
1585 __ str(r0, MemOperand(sp, kPointerSize));
1586 break;
1587 case KEYED_PROPERTY:
1588 __ str(r0, MemOperand(sp, 2 * kPointerSize));
1589 break;
1590 }
1591 break;
1592 }
1593 }
1594
1595
1596 // Inline smi case if we are in a loop.
1597 Label stub_call, done;
1598 if (loop_depth() > 0) {
1599 __ add(r0, r0, Operand(expr->op() == Token::INC
1600 ? Smi::FromInt(1)
1601 : Smi::FromInt(-1)));
1602 __ b(vs, &stub_call);
1603 // We could eliminate this smi check if we split the code at
1604 // the first smi check before calling ToNumber.
1605 __ tst(r0, Operand(kSmiTagMask));
1606 __ b(eq, &done);
1607 __ bind(&stub_call);
1608 // Call stub. Undo operation first.
1609 __ sub(r0, r0, Operand(r1));
1610 }
1611 __ mov(r1, Operand(expr->op() == Token::INC
1612 ? Smi::FromInt(1)
1613 : Smi::FromInt(-1)));
1614 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE);
1615 __ CallStub(&stub);
1616 __ bind(&done);
1617
1618 // Store the value returned in r0.
1619 switch (assign_type) {
1620 case VARIABLE:
1621 if (expr->is_postfix()) {
1622 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
1623 Expression::kEffect);
1624 // For all contexts except kEffect: We have the result on
1625 // top of the stack.
1626 if (context_ != Expression::kEffect) {
1627 ApplyTOS(context_);
1628 }
1629 } else {
1630 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
1631 context_);
1632 }
1633 break;
1634 case NAMED_PROPERTY: {
1635 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001636 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001637 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1638 __ Call(ic, RelocInfo::CODE_TARGET);
1639 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001640 if (context_ != Expression::kEffect) {
1641 ApplyTOS(context_);
1642 }
1643 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00001644 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001645 }
1646 break;
1647 }
1648 case KEYED_PROPERTY: {
1649 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1650 __ Call(ic, RelocInfo::CODE_TARGET);
1651 if (expr->is_postfix()) {
1652 __ Drop(2); // Result is on the stack under the key and the receiver.
1653 if (context_ != Expression::kEffect) {
1654 ApplyTOS(context_);
1655 }
1656 } else {
1657 DropAndApply(2, context_, r0);
1658 }
1659 break;
1660 }
1661 }
1662}
1663
1664
1665void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
1666 Comment cmnt(masm_, "[ BinaryOperation");
1667 switch (expr->op()) {
1668 case Token::COMMA:
1669 VisitForEffect(expr->left());
1670 Visit(expr->right());
1671 break;
1672
1673 case Token::OR:
1674 case Token::AND:
1675 EmitLogicalOperation(expr);
1676 break;
1677
1678 case Token::ADD:
1679 case Token::SUB:
1680 case Token::DIV:
1681 case Token::MOD:
1682 case Token::MUL:
1683 case Token::BIT_OR:
1684 case Token::BIT_AND:
1685 case Token::BIT_XOR:
1686 case Token::SHL:
1687 case Token::SHR:
1688 case Token::SAR:
1689 VisitForValue(expr->left(), kStack);
1690 VisitForValue(expr->right(), kAccumulator);
1691 EmitBinaryOp(expr->op(), context_);
1692 break;
1693
1694 default:
1695 UNREACHABLE();
1696 }
1697}
1698
1699
1700void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
1701 Comment cmnt(masm_, "[ CompareOperation");
1702
1703 // Always perform the comparison for its control flow. Pack the result
1704 // into the expression's context after the comparison is performed.
1705 Label materialize_true, materialize_false, done;
1706 // Initially assume we are in a test context.
1707 Label* if_true = true_label_;
1708 Label* if_false = false_label_;
1709 switch (context_) {
1710 case Expression::kUninitialized:
1711 UNREACHABLE();
1712 break;
1713 case Expression::kEffect:
1714 if_true = &done;
1715 if_false = &done;
1716 break;
1717 case Expression::kValue:
1718 if_true = &materialize_true;
1719 if_false = &materialize_false;
1720 break;
1721 case Expression::kTest:
1722 break;
1723 case Expression::kValueTest:
1724 if_true = &materialize_true;
1725 break;
1726 case Expression::kTestValue:
1727 if_false = &materialize_false;
1728 break;
1729 }
1730
1731 VisitForValue(expr->left(), kStack);
1732 switch (expr->op()) {
1733 case Token::IN:
1734 VisitForValue(expr->right(), kStack);
1735 __ InvokeBuiltin(Builtins::IN, CALL_JS);
1736 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
1737 __ cmp(r0, ip);
1738 __ b(eq, if_true);
1739 __ jmp(if_false);
1740 break;
1741
1742 case Token::INSTANCEOF: {
1743 VisitForValue(expr->right(), kStack);
1744 InstanceofStub stub;
1745 __ CallStub(&stub);
1746 __ tst(r0, r0);
1747 __ b(eq, if_true); // The stub returns 0 for true.
1748 __ jmp(if_false);
1749 break;
1750 }
1751
1752 default: {
1753 VisitForValue(expr->right(), kAccumulator);
1754 Condition cc = eq;
1755 bool strict = false;
1756 switch (expr->op()) {
1757 case Token::EQ_STRICT:
1758 strict = true;
1759 // Fall through
1760 case Token::EQ:
1761 cc = eq;
1762 __ pop(r1);
1763 break;
1764 case Token::LT:
1765 cc = lt;
1766 __ pop(r1);
1767 break;
1768 case Token::GT:
1769 // Reverse left and right sides to obtain ECMA-262 conversion order.
1770 cc = lt;
1771 __ mov(r1, result_register());
1772 __ pop(r0);
1773 break;
1774 case Token::LTE:
1775 // Reverse left and right sides to obtain ECMA-262 conversion order.
1776 cc = ge;
1777 __ mov(r1, result_register());
1778 __ pop(r0);
1779 break;
1780 case Token::GTE:
1781 cc = ge;
1782 __ pop(r1);
1783 break;
1784 case Token::IN:
1785 case Token::INSTANCEOF:
1786 default:
1787 UNREACHABLE();
1788 }
1789
1790 // The comparison stub expects the smi vs. smi case to be handled
1791 // before it is called.
1792 Label slow_case;
1793 __ orr(r2, r0, Operand(r1));
1794 __ tst(r2, Operand(kSmiTagMask));
1795 __ b(ne, &slow_case);
1796 __ cmp(r1, r0);
1797 __ b(cc, if_true);
1798 __ jmp(if_false);
1799
1800 __ bind(&slow_case);
1801 CompareStub stub(cc, strict);
1802 __ CallStub(&stub);
1803 __ cmp(r0, Operand(0));
1804 __ b(cc, if_true);
1805 __ jmp(if_false);
1806 }
1807 }
1808
1809 // Convert the result of the comparison into one expected for this
1810 // expression's context.
1811 Apply(context_, if_true, if_false);
1812}
1813
1814
1815void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
1816 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1817 Apply(context_, r0);
1818}
1819
1820
1821Register FullCodeGenerator::result_register() { return r0; }
1822
1823
1824Register FullCodeGenerator::context_register() { return cp; }
1825
1826
1827void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
1828 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
1829 __ str(value, MemOperand(fp, frame_offset));
1830}
1831
1832
1833void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
1834 __ ldr(dst, CodeGenerator::ContextOperand(cp, context_index));
1835}
1836
1837
1838// ----------------------------------------------------------------------------
1839// Non-local control flow support.
1840
1841void FullCodeGenerator::EnterFinallyBlock() {
1842 ASSERT(!result_register().is(r1));
1843 // Store result register while executing finally block.
1844 __ push(result_register());
1845 // Cook return address in link register to stack (smi encoded Code* delta)
1846 __ sub(r1, lr, Operand(masm_->CodeObject()));
1847 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
1848 ASSERT_EQ(0, kSmiTag);
1849 __ add(r1, r1, Operand(r1)); // Convert to smi.
1850 __ push(r1);
1851}
1852
1853
1854void FullCodeGenerator::ExitFinallyBlock() {
1855 ASSERT(!result_register().is(r1));
1856 // Restore result register from stack.
1857 __ pop(r1);
1858 // Uncook return address and return.
1859 __ pop(result_register());
1860 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
1861 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
1862 __ add(pc, r1, Operand(masm_->CodeObject()));
1863}
1864
1865
1866#undef __
1867
1868} } // namespace v8::internal