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