blob: e9bdfe55f77d0708e8c18cf9b9120e6a38457122 [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())));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000128 __ Push(r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000129
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
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000197#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000198 // Add a label for checking the size of the code used for returning.
199 Label check_exit_codesize;
200 masm_->bind(&check_exit_codesize);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000201#endif
202 // Make sure that the constant pool is not emitted inside of the return
203 // sequence.
204 { Assembler::BlockConstPoolScope block_const_pool(masm_);
205 // Here we use masm_-> instead of the __ macro to avoid the code coverage
206 // tool from instrumenting as we rely on the code size here.
207 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
208 CodeGenerator::RecordPositions(masm_, position);
209 __ RecordJSReturn();
210 masm_->mov(sp, fp);
211 masm_->ldm(ia_w, sp, fp.bit() | lr.bit());
212 masm_->add(sp, sp, Operand(sp_delta));
213 masm_->Jump(lr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000214 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000215
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000216#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000217 // Check that the size of the code used for returning matches what is
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000218 // expected by the debugger. If the sp_delts above cannot be encoded in the
219 // add instruction the add will generate two instructions.
220 int return_sequence_length =
221 masm_->InstructionsGeneratedSince(&check_exit_codesize);
222 CHECK(return_sequence_length == Assembler::kJSReturnSequenceLength ||
223 return_sequence_length == Assembler::kJSReturnSequenceLength + 1);
224#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000225 }
226}
227
228
229void FullCodeGenerator::Apply(Expression::Context context, Register reg) {
230 switch (context) {
231 case Expression::kUninitialized:
232 UNREACHABLE();
233
234 case Expression::kEffect:
235 // Nothing to do.
236 break;
237
238 case Expression::kValue:
239 // Move value into place.
240 switch (location_) {
241 case kAccumulator:
242 if (!reg.is(result_register())) __ mov(result_register(), reg);
243 break;
244 case kStack:
245 __ push(reg);
246 break;
247 }
248 break;
249
250 case Expression::kValueTest:
251 case Expression::kTestValue:
252 // Push an extra copy of the value in case it's needed.
253 __ push(reg);
254 // Fall through.
255
256 case Expression::kTest:
257 // We always call the runtime on ARM, so push the value as argument.
258 __ push(reg);
259 DoTest(context);
260 break;
261 }
262}
263
264
265void FullCodeGenerator::Apply(Expression::Context context, Slot* slot) {
266 switch (context) {
267 case Expression::kUninitialized:
268 UNREACHABLE();
269 case Expression::kEffect:
270 // Nothing to do.
271 break;
272 case Expression::kValue:
273 case Expression::kTest:
274 case Expression::kValueTest:
275 case Expression::kTestValue:
276 // On ARM we have to move the value into a register to do anything
277 // with it.
278 Move(result_register(), slot);
279 Apply(context, result_register());
280 break;
281 }
282}
283
284
285void FullCodeGenerator::Apply(Expression::Context context, Literal* lit) {
286 switch (context) {
287 case Expression::kUninitialized:
288 UNREACHABLE();
289 case Expression::kEffect:
290 break;
291 // Nothing to do.
292 case Expression::kValue:
293 case Expression::kTest:
294 case Expression::kValueTest:
295 case Expression::kTestValue:
296 // On ARM we have to move the value into a register to do anything
297 // with it.
298 __ mov(result_register(), Operand(lit->handle()));
299 Apply(context, result_register());
300 break;
301 }
302}
303
304
305void FullCodeGenerator::ApplyTOS(Expression::Context context) {
306 switch (context) {
307 case Expression::kUninitialized:
308 UNREACHABLE();
309
310 case Expression::kEffect:
311 __ Drop(1);
312 break;
313
314 case Expression::kValue:
315 switch (location_) {
316 case kAccumulator:
317 __ pop(result_register());
318 break;
319 case kStack:
320 break;
321 }
322 break;
323
324 case Expression::kValueTest:
325 case Expression::kTestValue:
326 // Duplicate the value on the stack in case it's needed.
327 __ ldr(ip, MemOperand(sp));
328 __ push(ip);
329 // Fall through.
330
331 case Expression::kTest:
332 DoTest(context);
333 break;
334 }
335}
336
337
338void FullCodeGenerator::DropAndApply(int count,
339 Expression::Context context,
340 Register reg) {
341 ASSERT(count > 0);
342 ASSERT(!reg.is(sp));
343 switch (context) {
344 case Expression::kUninitialized:
345 UNREACHABLE();
346
347 case Expression::kEffect:
348 __ Drop(count);
349 break;
350
351 case Expression::kValue:
352 switch (location_) {
353 case kAccumulator:
354 __ Drop(count);
355 if (!reg.is(result_register())) __ mov(result_register(), reg);
356 break;
357 case kStack:
358 if (count > 1) __ Drop(count - 1);
359 __ str(reg, MemOperand(sp));
360 break;
361 }
362 break;
363
364 case Expression::kTest:
365 if (count > 1) __ Drop(count - 1);
366 __ str(reg, MemOperand(sp));
367 DoTest(context);
368 break;
369
370 case Expression::kValueTest:
371 case Expression::kTestValue:
372 if (count == 1) {
373 __ str(reg, MemOperand(sp));
374 __ push(reg);
375 } else { // count > 1
376 __ Drop(count - 2);
377 __ str(reg, MemOperand(sp, kPointerSize));
378 __ str(reg, MemOperand(sp));
379 }
380 DoTest(context);
381 break;
382 }
383}
384
385
386void FullCodeGenerator::Apply(Expression::Context context,
387 Label* materialize_true,
388 Label* materialize_false) {
389 switch (context) {
390 case Expression::kUninitialized:
391
392 case Expression::kEffect:
393 ASSERT_EQ(materialize_true, materialize_false);
394 __ bind(materialize_true);
395 break;
396
397 case Expression::kValue: {
398 Label done;
399 __ bind(materialize_true);
400 __ mov(result_register(), Operand(Factory::true_value()));
401 __ jmp(&done);
402 __ bind(materialize_false);
403 __ mov(result_register(), Operand(Factory::false_value()));
404 __ bind(&done);
405 switch (location_) {
406 case kAccumulator:
407 break;
408 case kStack:
409 __ push(result_register());
410 break;
411 }
412 break;
413 }
414
415 case Expression::kTest:
416 break;
417
418 case Expression::kValueTest:
419 __ bind(materialize_true);
420 __ mov(result_register(), Operand(Factory::true_value()));
421 switch (location_) {
422 case kAccumulator:
423 break;
424 case kStack:
425 __ push(result_register());
426 break;
427 }
428 __ jmp(true_label_);
429 break;
430
431 case Expression::kTestValue:
432 __ bind(materialize_false);
433 __ mov(result_register(), Operand(Factory::false_value()));
434 switch (location_) {
435 case kAccumulator:
436 break;
437 case kStack:
438 __ push(result_register());
439 break;
440 }
441 __ jmp(false_label_);
442 break;
443 }
444}
445
446
447void FullCodeGenerator::DoTest(Expression::Context context) {
448 // The value to test is pushed on the stack, and duplicated on the stack
449 // if necessary (for value/test and test/value contexts).
450 ASSERT_NE(NULL, true_label_);
451 ASSERT_NE(NULL, false_label_);
452
453 // Call the runtime to find the boolean value of the source and then
454 // translate it into control flow to the pair of labels.
455 __ CallRuntime(Runtime::kToBool, 1);
456 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
457 __ cmp(r0, ip);
458
459 // Complete based on the context.
460 switch (context) {
461 case Expression::kUninitialized:
462 case Expression::kEffect:
463 case Expression::kValue:
464 UNREACHABLE();
465
466 case Expression::kTest:
467 __ b(eq, true_label_);
468 __ jmp(false_label_);
469 break;
470
471 case Expression::kValueTest: {
472 Label discard;
473 switch (location_) {
474 case kAccumulator:
475 __ b(ne, &discard);
476 __ pop(result_register());
477 __ jmp(true_label_);
478 break;
479 case kStack:
480 __ b(eq, true_label_);
481 break;
482 }
483 __ bind(&discard);
484 __ Drop(1);
485 __ jmp(false_label_);
486 break;
487 }
488
489 case Expression::kTestValue: {
490 Label discard;
491 switch (location_) {
492 case kAccumulator:
493 __ b(eq, &discard);
494 __ pop(result_register());
495 __ jmp(false_label_);
496 break;
497 case kStack:
498 __ b(ne, false_label_);
499 break;
500 }
501 __ bind(&discard);
502 __ Drop(1);
503 __ jmp(true_label_);
504 break;
505 }
506 }
507}
508
509
510MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
511 switch (slot->type()) {
512 case Slot::PARAMETER:
513 case Slot::LOCAL:
514 return MemOperand(fp, SlotOffset(slot));
515 case Slot::CONTEXT: {
516 int context_chain_length =
ager@chromium.org5c838252010-02-19 08:53:10 +0000517 scope()->ContextChainLength(slot->var()->scope());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000518 __ LoadContext(scratch, context_chain_length);
519 return CodeGenerator::ContextOperand(scratch, slot->index());
520 }
521 case Slot::LOOKUP:
522 UNREACHABLE();
523 }
524 UNREACHABLE();
525 return MemOperand(r0, 0);
526}
527
528
529void FullCodeGenerator::Move(Register destination, Slot* source) {
530 // Use destination as scratch.
531 MemOperand slot_operand = EmitSlotSearch(source, destination);
532 __ ldr(destination, slot_operand);
533}
534
535
536void FullCodeGenerator::Move(Slot* dst,
537 Register src,
538 Register scratch1,
539 Register scratch2) {
540 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
541 ASSERT(!scratch1.is(src) && !scratch2.is(src));
542 MemOperand location = EmitSlotSearch(dst, scratch1);
543 __ str(src, location);
544 // Emit the write barrier code if the location is in the heap.
545 if (dst->type() == Slot::CONTEXT) {
546 __ mov(scratch2, Operand(Context::SlotOffset(dst->index())));
547 __ RecordWrite(scratch1, scratch2, src);
548 }
549}
550
551
552void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
553 Comment cmnt(masm_, "[ Declaration");
554 Variable* var = decl->proxy()->var();
555 ASSERT(var != NULL); // Must have been resolved.
556 Slot* slot = var->slot();
557 Property* prop = var->AsProperty();
558
559 if (slot != NULL) {
560 switch (slot->type()) {
561 case Slot::PARAMETER:
562 case Slot::LOCAL:
563 if (decl->mode() == Variable::CONST) {
564 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
565 __ str(ip, MemOperand(fp, SlotOffset(slot)));
566 } else if (decl->fun() != NULL) {
567 VisitForValue(decl->fun(), kAccumulator);
568 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
569 }
570 break;
571
572 case Slot::CONTEXT:
573 // We bypass the general EmitSlotSearch because we know more about
574 // this specific context.
575
576 // The variable in the decl always resides in the current context.
ager@chromium.org5c838252010-02-19 08:53:10 +0000577 ASSERT_EQ(0, scope()->ContextChainLength(var->scope()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000578 if (FLAG_debug_code) {
579 // Check if we have the correct context pointer.
580 __ ldr(r1,
581 CodeGenerator::ContextOperand(cp, Context::FCONTEXT_INDEX));
582 __ cmp(r1, cp);
583 __ Check(eq, "Unexpected declaration in current context.");
584 }
585 if (decl->mode() == Variable::CONST) {
586 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
587 __ str(ip, CodeGenerator::ContextOperand(cp, slot->index()));
588 // No write barrier since the_hole_value is in old space.
589 } else if (decl->fun() != NULL) {
590 VisitForValue(decl->fun(), kAccumulator);
591 __ str(result_register(),
592 CodeGenerator::ContextOperand(cp, slot->index()));
593 int offset = Context::SlotOffset(slot->index());
594 __ mov(r2, Operand(offset));
595 // We know that we have written a function, which is not a smi.
596 __ mov(r1, Operand(cp));
597 __ RecordWrite(r1, r2, result_register());
598 }
599 break;
600
601 case Slot::LOOKUP: {
602 __ mov(r2, Operand(var->name()));
603 // Declaration nodes are always introduced in one of two modes.
604 ASSERT(decl->mode() == Variable::VAR ||
605 decl->mode() == Variable::CONST);
606 PropertyAttributes attr =
607 (decl->mode() == Variable::VAR) ? NONE : READ_ONLY;
608 __ mov(r1, Operand(Smi::FromInt(attr)));
609 // Push initial value, if any.
610 // Note: For variables we must not push an initial value (such as
611 // 'undefined') because we may have a (legal) redeclaration and we
612 // must not destroy the current value.
613 if (decl->mode() == Variable::CONST) {
614 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
615 __ stm(db_w, sp, cp.bit() | r2.bit() | r1.bit() | r0.bit());
616 } else if (decl->fun() != NULL) {
617 __ stm(db_w, sp, cp.bit() | r2.bit() | r1.bit());
618 // Push initial value for function declaration.
619 VisitForValue(decl->fun(), kStack);
620 } else {
621 __ mov(r0, Operand(Smi::FromInt(0))); // No initial value!
622 __ stm(db_w, sp, cp.bit() | r2.bit() | r1.bit() | r0.bit());
623 }
624 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
625 break;
626 }
627 }
628
629 } else if (prop != NULL) {
630 if (decl->fun() != NULL || decl->mode() == Variable::CONST) {
631 // We are declaring a function or constant that rewrites to a
632 // property. Use (keyed) IC to set the initial value.
633 VisitForValue(prop->obj(), kStack);
634 VisitForValue(prop->key(), kStack);
635
636 if (decl->fun() != NULL) {
637 VisitForValue(decl->fun(), kAccumulator);
638 } else {
639 __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex);
640 }
641
642 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
643 __ Call(ic, RelocInfo::CODE_TARGET);
644
645 // Value in r0 is ignored (declarations are statements). Receiver
646 // and key on stack are discarded.
647 __ Drop(2);
648 }
649 }
650}
651
652
653void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
654 // Call the runtime to declare the globals.
655 // The context is the first argument.
656 __ mov(r1, Operand(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000657 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000658 __ stm(db_w, sp, cp.bit() | r1.bit() | r0.bit());
659 __ CallRuntime(Runtime::kDeclareGlobals, 3);
660 // Return value is ignored.
661}
662
663
664void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
665 Comment cmnt(masm_, "[ FunctionLiteral");
666
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000667 // Build the shared function info and instantiate the function based
668 // on it.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000669 Handle<SharedFunctionInfo> function_info =
670 Compiler::BuildFunctionInfo(expr, script(), this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000671 if (HasStackOverflow()) return;
672
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000673 // Create a new closure.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000674 __ mov(r0, Operand(function_info));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000675 __ stm(db_w, sp, cp.bit() | r0.bit());
676 __ CallRuntime(Runtime::kNewClosure, 2);
677 Apply(context_, r0);
678}
679
680
681void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
682 Comment cmnt(masm_, "[ VariableProxy");
683 EmitVariableLoad(expr->var(), context_);
684}
685
686
687void FullCodeGenerator::EmitVariableLoad(Variable* var,
688 Expression::Context context) {
689 // Four cases: non-this global variables, lookup slots, all other
690 // types of slots, and parameters that rewrite to explicit property
691 // accesses on the arguments object.
692 Slot* slot = var->slot();
693 Property* property = var->AsProperty();
694
695 if (var->is_global() && !var->is_this()) {
696 Comment cmnt(masm_, "Global variable");
697 // Use inline caching. Variable name is passed in r2 and the global
698 // object on the stack.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000699 __ ldr(r0, CodeGenerator::GlobalObject());
700 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000701 __ mov(r2, Operand(var->name()));
702 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
703 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
704 DropAndApply(1, context, r0);
705
706 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
707 Comment cmnt(masm_, "Lookup slot");
708 __ mov(r1, Operand(var->name()));
709 __ stm(db_w, sp, cp.bit() | r1.bit()); // Context and name.
710 __ CallRuntime(Runtime::kLoadContextSlot, 2);
711 Apply(context, r0);
712
713 } else if (slot != NULL) {
714 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
715 ? "Context slot"
716 : "Stack slot");
717 Apply(context, slot);
718
719 } else {
720 Comment cmnt(masm_, "Rewritten parameter");
721 ASSERT_NOT_NULL(property);
722 // Rewritten parameter accesses are of the form "slot[literal]".
723
724 // Assert that the object is in a slot.
725 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
726 ASSERT_NOT_NULL(object_var);
727 Slot* object_slot = object_var->slot();
728 ASSERT_NOT_NULL(object_slot);
729
730 // Load the object.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000731 Move(r1, object_slot);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000732
733 // Assert that the key is a smi.
734 Literal* key_literal = property->key()->AsLiteral();
735 ASSERT_NOT_NULL(key_literal);
736 ASSERT(key_literal->handle()->IsSmi());
737
738 // Load the key.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000739 __ mov(r0, Operand(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000740
741 // Push both as arguments to ic.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000742 __ Push(r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000743
ager@chromium.orgac091b72010-05-05 07:34:42 +0000744 // Call keyed load IC. It has all arguments on the stack and the key in r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000745 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
746 __ Call(ic, RelocInfo::CODE_TARGET);
747
748 // Drop key and object left on the stack by IC, and push the result.
749 DropAndApply(2, context, r0);
750 }
751}
752
753
754void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
755 Comment cmnt(masm_, "[ RegExpLiteral");
756 Label done;
757 // Registers will be used as follows:
758 // r4 = JS function, literals array
759 // r3 = literal index
760 // r2 = RegExp pattern
761 // r1 = RegExp flags
762 // r0 = temp + return value (RegExp literal)
763 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
764 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
765 int literal_offset =
766 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
767 __ ldr(r0, FieldMemOperand(r4, literal_offset));
768 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
769 __ cmp(r0, ip);
770 __ b(ne, &done);
771 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
772 __ mov(r2, Operand(expr->pattern()));
773 __ mov(r1, Operand(expr->flags()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000774 __ Push(r4, r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000775 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
776 __ bind(&done);
777 Apply(context_, r0);
778}
779
780
781void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
782 Comment cmnt(masm_, "[ ObjectLiteral");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000783 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
784 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
785 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
786 __ mov(r1, Operand(expr->constant_properties()));
787 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000788 __ Push(r3, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000789 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000790 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000791 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000792 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000793 }
794
795 // If result_saved is true the result is on top of the stack. If
796 // result_saved is false the result is in r0.
797 bool result_saved = false;
798
799 for (int i = 0; i < expr->properties()->length(); i++) {
800 ObjectLiteral::Property* property = expr->properties()->at(i);
801 if (property->IsCompileTimeValue()) continue;
802
803 Literal* key = property->key();
804 Expression* value = property->value();
805 if (!result_saved) {
806 __ push(r0); // Save result on stack
807 result_saved = true;
808 }
809 switch (property->kind()) {
810 case ObjectLiteral::Property::CONSTANT:
811 UNREACHABLE();
812 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
813 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
814 // Fall through.
815 case ObjectLiteral::Property::COMPUTED:
816 if (key->handle()->IsSymbol()) {
817 VisitForValue(value, kAccumulator);
818 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000819 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000820 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
821 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000822 break;
823 }
824 // Fall through.
825 case ObjectLiteral::Property::PROTOTYPE:
826 // Duplicate receiver on stack.
827 __ ldr(r0, MemOperand(sp));
828 __ push(r0);
829 VisitForValue(key, kStack);
830 VisitForValue(value, kStack);
831 __ CallRuntime(Runtime::kSetProperty, 3);
832 break;
833 case ObjectLiteral::Property::GETTER:
834 case ObjectLiteral::Property::SETTER:
835 // Duplicate receiver on stack.
836 __ ldr(r0, MemOperand(sp));
837 __ push(r0);
838 VisitForValue(key, kStack);
839 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
840 Smi::FromInt(1) :
841 Smi::FromInt(0)));
842 __ push(r1);
843 VisitForValue(value, kStack);
844 __ CallRuntime(Runtime::kDefineAccessor, 4);
845 break;
846 }
847 }
848
849 if (result_saved) {
850 ApplyTOS(context_);
851 } else {
852 Apply(context_, r0);
853 }
854}
855
856
857void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
858 Comment cmnt(masm_, "[ ArrayLiteral");
859 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
860 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
861 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
862 __ mov(r1, Operand(expr->constant_elements()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000863 __ Push(r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000864 if (expr->depth() > 1) {
865 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
866 } else {
867 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
868 }
869
870 bool result_saved = false; // Is the result saved to the stack?
871
872 // Emit code to evaluate all the non-constant subexpressions and to store
873 // them into the newly cloned array.
874 ZoneList<Expression*>* subexprs = expr->values();
875 for (int i = 0, len = subexprs->length(); i < len; i++) {
876 Expression* subexpr = subexprs->at(i);
877 // If the subexpression is a literal or a simple materialized literal it
878 // is already set in the cloned array.
879 if (subexpr->AsLiteral() != NULL ||
880 CompileTimeValue::IsCompileTimeValue(subexpr)) {
881 continue;
882 }
883
884 if (!result_saved) {
885 __ push(r0);
886 result_saved = true;
887 }
888 VisitForValue(subexpr, kAccumulator);
889
890 // Store the subexpression value in the array's elements.
891 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
892 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
893 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
894 __ str(result_register(), FieldMemOperand(r1, offset));
895
896 // Update the write barrier for the array store with r0 as the scratch
897 // register.
898 __ mov(r2, Operand(offset));
899 __ RecordWrite(r1, r2, result_register());
900 }
901
902 if (result_saved) {
903 ApplyTOS(context_);
904 } else {
905 Apply(context_, r0);
906 }
907}
908
909
ager@chromium.org5c838252010-02-19 08:53:10 +0000910void FullCodeGenerator::VisitAssignment(Assignment* expr) {
911 Comment cmnt(masm_, "[ Assignment");
912 ASSERT(expr->op() != Token::INIT_CONST);
913 // Left-hand side can only be a property, a global or a (parameter or local)
914 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
915 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
916 LhsKind assign_type = VARIABLE;
917 Property* prop = expr->target()->AsProperty();
918 if (prop != NULL) {
919 assign_type =
920 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
921 }
922
923 // Evaluate LHS expression.
924 switch (assign_type) {
925 case VARIABLE:
926 // Nothing to do here.
927 break;
928 case NAMED_PROPERTY:
929 if (expr->is_compound()) {
930 // We need the receiver both on the stack and in the accumulator.
931 VisitForValue(prop->obj(), kAccumulator);
932 __ push(result_register());
933 } else {
934 VisitForValue(prop->obj(), kStack);
935 }
936 break;
937 case KEYED_PROPERTY:
938 VisitForValue(prop->obj(), kStack);
939 VisitForValue(prop->key(), kStack);
940 break;
941 }
942
943 // If we have a compound assignment: Get value of LHS expression and
944 // store in on top of the stack.
945 if (expr->is_compound()) {
946 Location saved_location = location_;
947 location_ = kStack;
948 switch (assign_type) {
949 case VARIABLE:
950 EmitVariableLoad(expr->target()->AsVariableProxy()->var(),
951 Expression::kValue);
952 break;
953 case NAMED_PROPERTY:
954 EmitNamedPropertyLoad(prop);
955 __ push(result_register());
956 break;
957 case KEYED_PROPERTY:
958 EmitKeyedPropertyLoad(prop);
959 __ push(result_register());
960 break;
961 }
962 location_ = saved_location;
963 }
964
965 // Evaluate RHS expression.
966 Expression* rhs = expr->value();
967 VisitForValue(rhs, kAccumulator);
968
969 // If we have a compound assignment: Apply operator.
970 if (expr->is_compound()) {
971 Location saved_location = location_;
972 location_ = kAccumulator;
973 EmitBinaryOp(expr->binary_op(), Expression::kValue);
974 location_ = saved_location;
975 }
976
977 // Record source position before possible IC call.
978 SetSourcePosition(expr->position());
979
980 // Store the value.
981 switch (assign_type) {
982 case VARIABLE:
983 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
984 context_);
985 break;
986 case NAMED_PROPERTY:
987 EmitNamedPropertyAssignment(expr);
988 break;
989 case KEYED_PROPERTY:
990 EmitKeyedPropertyAssignment(expr);
991 break;
992 }
993}
994
995
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000996void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
997 SetSourcePosition(prop->position());
998 Literal* key = prop->key()->AsLiteral();
999 __ mov(r2, Operand(key->handle()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001000 __ ldr(r0, MemOperand(sp, 0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001001 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());
ager@chromium.orgac091b72010-05-05 07:34:42 +00001008 // Call keyed load IC. It has all arguments on the stack and the key in r0.
1009 __ ldr(r0, MemOperand(sp, 0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001010 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);
ager@chromium.org357bf652010-04-12 11:30:10 +00001018 GenericBinaryOpStub stub(op, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001019 __ 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());
ager@chromium.orgac091b72010-05-05 07:34:42 +00001252 // Call keyed load IC. It has all arguments on the stack and the key in
1253 // r0.
1254 __ ldr(r0, MemOperand(sp, 0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001255 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1256 __ Call(ic, RelocInfo::CODE_TARGET);
1257 // Load receiver object into r1.
1258 if (prop->is_synthetic()) {
1259 __ ldr(r1, CodeGenerator::GlobalObject());
1260 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1261 } else {
1262 __ ldr(r1, MemOperand(sp, kPointerSize));
1263 }
1264 // Overwrite (object, key) with (function, receiver).
1265 __ str(r0, MemOperand(sp, kPointerSize));
1266 __ str(r1, MemOperand(sp));
1267 EmitCallWithStub(expr);
1268 }
1269 } else {
1270 // Call to some other expression. If the expression is an anonymous
1271 // function literal not called in a loop, mark it as one that should
1272 // also use the fast code generator.
1273 FunctionLiteral* lit = fun->AsFunctionLiteral();
1274 if (lit != NULL &&
1275 lit->name()->Equals(Heap::empty_string()) &&
1276 loop_depth() == 0) {
1277 lit->set_try_full_codegen(true);
1278 }
1279 VisitForValue(fun, kStack);
1280 // Load global receiver object.
1281 __ ldr(r1, CodeGenerator::GlobalObject());
1282 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1283 __ push(r1);
1284 // Emit function call.
1285 EmitCallWithStub(expr);
1286 }
1287}
1288
1289
1290void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1291 Comment cmnt(masm_, "[ CallNew");
1292 // According to ECMA-262, section 11.2.2, page 44, the function
1293 // expression in new calls must be evaluated before the
1294 // arguments.
1295 // Push function on the stack.
1296 VisitForValue(expr->expression(), kStack);
1297
1298 // Push global object (receiver).
1299 __ ldr(r0, CodeGenerator::GlobalObject());
1300 __ push(r0);
1301 // Push the arguments ("left-to-right") on the stack.
1302 ZoneList<Expression*>* args = expr->arguments();
1303 int arg_count = args->length();
1304 for (int i = 0; i < arg_count; i++) {
1305 VisitForValue(args->at(i), kStack);
1306 }
1307
1308 // Call the construct call builtin that handles allocation and
1309 // constructor invocation.
1310 SetSourcePosition(expr->position());
1311
1312 // Load function, arg_count into r1 and r0.
1313 __ mov(r0, Operand(arg_count));
1314 // Function is in sp[arg_count + 1].
1315 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1316
1317 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1318 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
1319
1320 // Replace function on TOS with result in r0, or pop it.
1321 DropAndApply(1, context_, r0);
1322}
1323
1324
1325void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
1326 Comment cmnt(masm_, "[ CallRuntime");
1327 ZoneList<Expression*>* args = expr->arguments();
1328
1329 if (expr->is_jsruntime()) {
1330 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001331 __ ldr(r0, CodeGenerator::GlobalObject());
1332 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001333 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001334 }
1335
1336 // Push the arguments ("left-to-right").
1337 int arg_count = args->length();
1338 for (int i = 0; i < arg_count; i++) {
1339 VisitForValue(args->at(i), kStack);
1340 }
1341
1342 if (expr->is_jsruntime()) {
1343 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00001344 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001345 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
1346 NOT_IN_LOOP);
1347 __ Call(ic, RelocInfo::CODE_TARGET);
1348 // Restore context register.
1349 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001350 } else {
1351 // Call the C runtime function.
1352 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001353 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001354 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001355}
1356
1357
1358void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
1359 switch (expr->op()) {
1360 case Token::VOID: {
1361 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
1362 VisitForEffect(expr->expression());
1363 switch (context_) {
1364 case Expression::kUninitialized:
1365 UNREACHABLE();
1366 break;
1367 case Expression::kEffect:
1368 break;
1369 case Expression::kValue:
1370 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
1371 switch (location_) {
1372 case kAccumulator:
1373 break;
1374 case kStack:
1375 __ push(result_register());
1376 break;
1377 }
1378 break;
1379 case Expression::kTestValue:
1380 // Value is false so it's needed.
1381 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
1382 switch (location_) {
1383 case kAccumulator:
1384 break;
1385 case kStack:
1386 __ push(result_register());
1387 break;
1388 }
1389 // Fall through.
1390 case Expression::kTest:
1391 case Expression::kValueTest:
1392 __ jmp(false_label_);
1393 break;
1394 }
1395 break;
1396 }
1397
1398 case Token::NOT: {
1399 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
1400 Label materialize_true, materialize_false, done;
1401 // Initially assume a pure test context. Notice that the labels are
1402 // swapped.
1403 Label* if_true = false_label_;
1404 Label* if_false = true_label_;
1405 switch (context_) {
1406 case Expression::kUninitialized:
1407 UNREACHABLE();
1408 break;
1409 case Expression::kEffect:
1410 if_true = &done;
1411 if_false = &done;
1412 break;
1413 case Expression::kValue:
1414 if_true = &materialize_false;
1415 if_false = &materialize_true;
1416 break;
1417 case Expression::kTest:
1418 break;
1419 case Expression::kValueTest:
1420 if_false = &materialize_true;
1421 break;
1422 case Expression::kTestValue:
1423 if_true = &materialize_false;
1424 break;
1425 }
1426 VisitForControl(expr->expression(), if_true, if_false);
1427 Apply(context_, if_false, if_true); // Labels swapped.
1428 break;
1429 }
1430
1431 case Token::TYPEOF: {
1432 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
1433 VariableProxy* proxy = expr->expression()->AsVariableProxy();
1434 if (proxy != NULL &&
1435 !proxy->var()->is_this() &&
1436 proxy->var()->is_global()) {
1437 Comment cmnt(masm_, "Global variable");
1438 __ ldr(r0, CodeGenerator::GlobalObject());
1439 __ push(r0);
1440 __ mov(r2, Operand(proxy->name()));
1441 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1442 // Use a regular load, not a contextual load, to avoid a reference
1443 // error.
1444 __ Call(ic, RelocInfo::CODE_TARGET);
1445 __ str(r0, MemOperand(sp));
1446 } else if (proxy != NULL &&
1447 proxy->var()->slot() != NULL &&
1448 proxy->var()->slot()->type() == Slot::LOOKUP) {
1449 __ mov(r0, Operand(proxy->name()));
1450 __ stm(db_w, sp, cp.bit() | r0.bit());
1451 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
1452 __ push(r0);
1453 } else {
1454 // This expression cannot throw a reference error at the top level.
1455 VisitForValue(expr->expression(), kStack);
1456 }
1457
1458 __ CallRuntime(Runtime::kTypeof, 1);
1459 Apply(context_, r0);
1460 break;
1461 }
1462
1463 case Token::ADD: {
1464 Comment cmt(masm_, "[ UnaryOperation (ADD)");
1465 VisitForValue(expr->expression(), kAccumulator);
1466 Label no_conversion;
1467 __ tst(result_register(), Operand(kSmiTagMask));
1468 __ b(eq, &no_conversion);
1469 __ push(r0);
1470 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
1471 __ bind(&no_conversion);
1472 Apply(context_, result_register());
1473 break;
1474 }
1475
1476 case Token::SUB: {
1477 Comment cmt(masm_, "[ UnaryOperation (SUB)");
1478 bool overwrite =
1479 (expr->expression()->AsBinaryOperation() != NULL &&
1480 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
1481 GenericUnaryOpStub stub(Token::SUB, overwrite);
1482 // GenericUnaryOpStub expects the argument to be in the
1483 // accumulator register r0.
1484 VisitForValue(expr->expression(), kAccumulator);
1485 __ CallStub(&stub);
1486 Apply(context_, r0);
1487 break;
1488 }
1489
1490 case Token::BIT_NOT: {
1491 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
1492 bool overwrite =
1493 (expr->expression()->AsBinaryOperation() != NULL &&
1494 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
1495 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
1496 // GenericUnaryOpStub expects the argument to be in the
1497 // accumulator register r0.
1498 VisitForValue(expr->expression(), kAccumulator);
1499 // Avoid calling the stub for Smis.
1500 Label smi, done;
1501 __ tst(result_register(), Operand(kSmiTagMask));
1502 __ b(eq, &smi);
1503 // Non-smi: call stub leaving result in accumulator register.
1504 __ CallStub(&stub);
1505 __ b(&done);
1506 // Perform operation directly on Smis.
1507 __ bind(&smi);
1508 __ mvn(result_register(), Operand(result_register()));
1509 // Bit-clear inverted smi-tag.
1510 __ bic(result_register(), result_register(), Operand(kSmiTagMask));
1511 __ bind(&done);
1512 Apply(context_, result_register());
1513 break;
1514 }
1515
1516 default:
1517 UNREACHABLE();
1518 }
1519}
1520
1521
1522void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
1523 Comment cmnt(masm_, "[ CountOperation");
1524
1525 // Expression can only be a property, a global or a (parameter or local)
1526 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1527 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1528 LhsKind assign_type = VARIABLE;
1529 Property* prop = expr->expression()->AsProperty();
1530 // In case of a property we use the uninitialized expression context
1531 // of the key to detect a named property.
1532 if (prop != NULL) {
1533 assign_type =
1534 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
1535 }
1536
1537 // Evaluate expression and get value.
1538 if (assign_type == VARIABLE) {
1539 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
1540 Location saved_location = location_;
1541 location_ = kAccumulator;
1542 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
1543 Expression::kValue);
1544 location_ = saved_location;
1545 } else {
1546 // Reserve space for result of postfix operation.
1547 if (expr->is_postfix() && context_ != Expression::kEffect) {
1548 __ mov(ip, Operand(Smi::FromInt(0)));
1549 __ push(ip);
1550 }
1551 VisitForValue(prop->obj(), kStack);
1552 if (assign_type == NAMED_PROPERTY) {
1553 EmitNamedPropertyLoad(prop);
1554 } else {
1555 VisitForValue(prop->key(), kStack);
1556 EmitKeyedPropertyLoad(prop);
1557 }
1558 }
1559
1560 // Call ToNumber only if operand is not a smi.
1561 Label no_conversion;
1562 __ tst(r0, Operand(kSmiTagMask));
1563 __ b(eq, &no_conversion);
1564 __ push(r0);
1565 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
1566 __ bind(&no_conversion);
1567
1568 // Save result for postfix expressions.
1569 if (expr->is_postfix()) {
1570 switch (context_) {
1571 case Expression::kUninitialized:
1572 UNREACHABLE();
1573 case Expression::kEffect:
1574 // Do not save result.
1575 break;
1576 case Expression::kValue:
1577 case Expression::kTest:
1578 case Expression::kValueTest:
1579 case Expression::kTestValue:
1580 // Save the result on the stack. If we have a named or keyed property
1581 // we store the result under the receiver that is currently on top
1582 // of the stack.
1583 switch (assign_type) {
1584 case VARIABLE:
1585 __ push(r0);
1586 break;
1587 case NAMED_PROPERTY:
1588 __ str(r0, MemOperand(sp, kPointerSize));
1589 break;
1590 case KEYED_PROPERTY:
1591 __ str(r0, MemOperand(sp, 2 * kPointerSize));
1592 break;
1593 }
1594 break;
1595 }
1596 }
1597
1598
1599 // Inline smi case if we are in a loop.
1600 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001601 int count_value = expr->op() == Token::INC ? 1 : -1;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001602 if (loop_depth() > 0) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001603 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001604 __ b(vs, &stub_call);
1605 // We could eliminate this smi check if we split the code at
1606 // the first smi check before calling ToNumber.
1607 __ tst(r0, Operand(kSmiTagMask));
1608 __ b(eq, &done);
1609 __ bind(&stub_call);
1610 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001611 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001612 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001613 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00001614 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001615 __ 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