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