blob: bed93640ff49abf1ea1606b099f4737700dd788b [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.
731 Move(r2, object_slot);
732
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.
739 __ mov(r1, Operand(key_literal->handle()));
740
741 // Push both as arguments to ic.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000742 __ Push(r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000743
744 // Do a keyed property load.
745 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());
1008 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1009 __ Call(ic, RelocInfo::CODE_TARGET);
1010}
1011
1012
1013void FullCodeGenerator::EmitBinaryOp(Token::Value op,
1014 Expression::Context context) {
1015 __ pop(r1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001016 GenericBinaryOpStub stub(op, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001017 __ CallStub(&stub);
1018 Apply(context, r0);
1019}
1020
1021
1022void FullCodeGenerator::EmitVariableAssignment(Variable* var,
1023 Expression::Context context) {
1024 // Three main cases: global variables, lookup slots, and all other
1025 // types of slots. Left-hand-side parameters that rewrite to
1026 // explicit property accesses do not reach here.
1027 ASSERT(var != NULL);
1028 ASSERT(var->is_global() || var->slot() != NULL);
1029
1030 Slot* slot = var->slot();
1031 if (var->is_global()) {
1032 ASSERT(!var->is_this());
1033 // Assignment to a global variable. Use inline caching for the
1034 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001035 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001036 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001037 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001038 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1039 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001040
1041 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
1042 __ push(result_register()); // Value.
1043 __ mov(r1, Operand(var->name()));
1044 __ stm(db_w, sp, cp.bit() | r1.bit()); // Context and name.
1045 __ CallRuntime(Runtime::kStoreContextSlot, 3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001046
1047 } else if (var->slot() != NULL) {
1048 Slot* slot = var->slot();
1049 switch (slot->type()) {
1050 case Slot::LOCAL:
1051 case Slot::PARAMETER:
1052 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1053 break;
1054
1055 case Slot::CONTEXT: {
1056 MemOperand target = EmitSlotSearch(slot, r1);
1057 __ str(result_register(), target);
1058
1059 // RecordWrite may destroy all its register arguments.
1060 __ mov(r3, result_register());
1061 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
1062
1063 __ mov(r2, Operand(offset));
1064 __ RecordWrite(r1, r2, r3);
1065 break;
1066 }
1067
1068 case Slot::LOOKUP:
1069 UNREACHABLE();
1070 break;
1071 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001072
1073 } else {
1074 // Variables rewritten as properties are not treated as variables in
1075 // assignments.
1076 UNREACHABLE();
1077 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001078 Apply(context, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001079}
1080
1081
1082void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1083 // Assignment to a property, using a named store IC.
1084 Property* prop = expr->target()->AsProperty();
1085 ASSERT(prop != NULL);
1086 ASSERT(prop->key()->AsLiteral() != NULL);
1087
1088 // If the assignment starts a block of assignments to the same object,
1089 // change to slow case to avoid the quadratic behavior of repeatedly
1090 // adding fast properties.
1091 if (expr->starts_initialization_block()) {
1092 __ push(result_register());
1093 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1094 __ push(ip);
1095 __ CallRuntime(Runtime::kToSlowProperties, 1);
1096 __ pop(result_register());
1097 }
1098
1099 // Record source code position before IC call.
1100 SetSourcePosition(expr->position());
1101 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001102 if (expr->ends_initialization_block()) {
1103 __ ldr(r1, MemOperand(sp));
1104 } else {
1105 __ pop(r1);
1106 }
1107
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001108 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1109 __ Call(ic, RelocInfo::CODE_TARGET);
1110
1111 // If the assignment ends an initialization block, revert to fast case.
1112 if (expr->ends_initialization_block()) {
1113 __ push(r0); // Result of assignment, saved even if not needed.
1114 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is under value.
1115 __ push(ip);
1116 __ CallRuntime(Runtime::kToFastProperties, 1);
1117 __ pop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001118 DropAndApply(1, context_, r0);
1119 } else {
1120 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001121 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001122}
1123
1124
1125void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1126 // Assignment to a property, using a keyed store IC.
1127
1128 // If the assignment starts a block of assignments to the same object,
1129 // change to slow case to avoid the quadratic behavior of repeatedly
1130 // adding fast properties.
1131 if (expr->starts_initialization_block()) {
1132 __ push(result_register());
1133 // Receiver is now under the key and value.
1134 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1135 __ push(ip);
1136 __ CallRuntime(Runtime::kToSlowProperties, 1);
1137 __ pop(result_register());
1138 }
1139
1140 // Record source code position before IC call.
1141 SetSourcePosition(expr->position());
1142 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1143 __ Call(ic, RelocInfo::CODE_TARGET);
1144
1145 // If the assignment ends an initialization block, revert to fast case.
1146 if (expr->ends_initialization_block()) {
1147 __ push(r0); // Result of assignment, saved even if not needed.
1148 // Receiver is under the key and value.
1149 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1150 __ push(ip);
1151 __ CallRuntime(Runtime::kToFastProperties, 1);
1152 __ pop(r0);
1153 }
1154
1155 // Receiver and key are still on stack.
1156 DropAndApply(2, context_, r0);
1157}
1158
1159
1160void FullCodeGenerator::VisitProperty(Property* expr) {
1161 Comment cmnt(masm_, "[ Property");
1162 Expression* key = expr->key();
1163
1164 // Evaluate receiver.
1165 VisitForValue(expr->obj(), kStack);
1166
1167 if (key->IsPropertyName()) {
1168 EmitNamedPropertyLoad(expr);
1169 // Drop receiver left on the stack by IC.
1170 DropAndApply(1, context_, r0);
1171 } else {
1172 VisitForValue(expr->key(), kStack);
1173 EmitKeyedPropertyLoad(expr);
1174 // Drop key and receiver left on the stack by IC.
1175 DropAndApply(2, context_, r0);
1176 }
1177}
1178
1179void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001180 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001181 RelocInfo::Mode mode) {
1182 // Code common for calls using the IC.
1183 ZoneList<Expression*>* args = expr->arguments();
1184 int arg_count = args->length();
1185 for (int i = 0; i < arg_count; i++) {
1186 VisitForValue(args->at(i), kStack);
1187 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001188 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001189 // Record source position for debugger.
1190 SetSourcePosition(expr->position());
1191 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001192 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1193 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001194 __ Call(ic, mode);
1195 // Restore context register.
1196 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001197 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001198}
1199
1200
1201void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1202 // Code common for calls using the call stub.
1203 ZoneList<Expression*>* args = expr->arguments();
1204 int arg_count = args->length();
1205 for (int i = 0; i < arg_count; i++) {
1206 VisitForValue(args->at(i), kStack);
1207 }
1208 // Record source position for debugger.
1209 SetSourcePosition(expr->position());
1210 CallFunctionStub stub(arg_count, NOT_IN_LOOP, RECEIVER_MIGHT_BE_VALUE);
1211 __ CallStub(&stub);
1212 // Restore context register.
1213 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001214 DropAndApply(1, context_, r0);
1215}
1216
1217
1218void FullCodeGenerator::VisitCall(Call* expr) {
1219 Comment cmnt(masm_, "[ Call");
1220 Expression* fun = expr->expression();
1221 Variable* var = fun->AsVariableProxy()->AsVariable();
1222
1223 if (var != NULL && var->is_possibly_eval()) {
1224 // Call to the identifier 'eval'.
1225 UNREACHABLE();
1226 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001227 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001228 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001229 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001230 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1231 } else if (var != NULL && var->slot() != NULL &&
1232 var->slot()->type() == Slot::LOOKUP) {
1233 // Call to a lookup slot.
1234 UNREACHABLE();
1235 } else if (fun->AsProperty() != NULL) {
1236 // Call to an object property.
1237 Property* prop = fun->AsProperty();
1238 Literal* key = prop->key()->AsLiteral();
1239 if (key != NULL && key->handle()->IsSymbol()) {
1240 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001241 VisitForValue(prop->obj(), kStack);
1242 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1243 } else {
1244 // Call to a keyed property, use keyed load IC followed by function
1245 // call.
1246 VisitForValue(prop->obj(), kStack);
1247 VisitForValue(prop->key(), kStack);
1248 // Record source code position for IC call.
1249 SetSourcePosition(prop->position());
1250 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1251 __ Call(ic, RelocInfo::CODE_TARGET);
1252 // Load receiver object into r1.
1253 if (prop->is_synthetic()) {
1254 __ ldr(r1, CodeGenerator::GlobalObject());
1255 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1256 } else {
1257 __ ldr(r1, MemOperand(sp, kPointerSize));
1258 }
1259 // Overwrite (object, key) with (function, receiver).
1260 __ str(r0, MemOperand(sp, kPointerSize));
1261 __ str(r1, MemOperand(sp));
1262 EmitCallWithStub(expr);
1263 }
1264 } else {
1265 // Call to some other expression. If the expression is an anonymous
1266 // function literal not called in a loop, mark it as one that should
1267 // also use the fast code generator.
1268 FunctionLiteral* lit = fun->AsFunctionLiteral();
1269 if (lit != NULL &&
1270 lit->name()->Equals(Heap::empty_string()) &&
1271 loop_depth() == 0) {
1272 lit->set_try_full_codegen(true);
1273 }
1274 VisitForValue(fun, kStack);
1275 // Load global receiver object.
1276 __ ldr(r1, CodeGenerator::GlobalObject());
1277 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1278 __ push(r1);
1279 // Emit function call.
1280 EmitCallWithStub(expr);
1281 }
1282}
1283
1284
1285void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1286 Comment cmnt(masm_, "[ CallNew");
1287 // According to ECMA-262, section 11.2.2, page 44, the function
1288 // expression in new calls must be evaluated before the
1289 // arguments.
1290 // Push function on the stack.
1291 VisitForValue(expr->expression(), kStack);
1292
1293 // Push global object (receiver).
1294 __ ldr(r0, CodeGenerator::GlobalObject());
1295 __ push(r0);
1296 // Push the arguments ("left-to-right") on the stack.
1297 ZoneList<Expression*>* args = expr->arguments();
1298 int arg_count = args->length();
1299 for (int i = 0; i < arg_count; i++) {
1300 VisitForValue(args->at(i), kStack);
1301 }
1302
1303 // Call the construct call builtin that handles allocation and
1304 // constructor invocation.
1305 SetSourcePosition(expr->position());
1306
1307 // Load function, arg_count into r1 and r0.
1308 __ mov(r0, Operand(arg_count));
1309 // Function is in sp[arg_count + 1].
1310 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1311
1312 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1313 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
1314
1315 // Replace function on TOS with result in r0, or pop it.
1316 DropAndApply(1, context_, r0);
1317}
1318
1319
1320void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
1321 Comment cmnt(masm_, "[ CallRuntime");
1322 ZoneList<Expression*>* args = expr->arguments();
1323
1324 if (expr->is_jsruntime()) {
1325 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001326 __ ldr(r0, CodeGenerator::GlobalObject());
1327 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001328 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001329 }
1330
1331 // Push the arguments ("left-to-right").
1332 int arg_count = args->length();
1333 for (int i = 0; i < arg_count; i++) {
1334 VisitForValue(args->at(i), kStack);
1335 }
1336
1337 if (expr->is_jsruntime()) {
1338 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00001339 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001340 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
1341 NOT_IN_LOOP);
1342 __ Call(ic, RelocInfo::CODE_TARGET);
1343 // Restore context register.
1344 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001345 } else {
1346 // Call the C runtime function.
1347 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001348 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001349 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001350}
1351
1352
1353void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
1354 switch (expr->op()) {
1355 case Token::VOID: {
1356 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
1357 VisitForEffect(expr->expression());
1358 switch (context_) {
1359 case Expression::kUninitialized:
1360 UNREACHABLE();
1361 break;
1362 case Expression::kEffect:
1363 break;
1364 case Expression::kValue:
1365 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
1366 switch (location_) {
1367 case kAccumulator:
1368 break;
1369 case kStack:
1370 __ push(result_register());
1371 break;
1372 }
1373 break;
1374 case Expression::kTestValue:
1375 // Value is false so it's needed.
1376 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
1377 switch (location_) {
1378 case kAccumulator:
1379 break;
1380 case kStack:
1381 __ push(result_register());
1382 break;
1383 }
1384 // Fall through.
1385 case Expression::kTest:
1386 case Expression::kValueTest:
1387 __ jmp(false_label_);
1388 break;
1389 }
1390 break;
1391 }
1392
1393 case Token::NOT: {
1394 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
1395 Label materialize_true, materialize_false, done;
1396 // Initially assume a pure test context. Notice that the labels are
1397 // swapped.
1398 Label* if_true = false_label_;
1399 Label* if_false = true_label_;
1400 switch (context_) {
1401 case Expression::kUninitialized:
1402 UNREACHABLE();
1403 break;
1404 case Expression::kEffect:
1405 if_true = &done;
1406 if_false = &done;
1407 break;
1408 case Expression::kValue:
1409 if_true = &materialize_false;
1410 if_false = &materialize_true;
1411 break;
1412 case Expression::kTest:
1413 break;
1414 case Expression::kValueTest:
1415 if_false = &materialize_true;
1416 break;
1417 case Expression::kTestValue:
1418 if_true = &materialize_false;
1419 break;
1420 }
1421 VisitForControl(expr->expression(), if_true, if_false);
1422 Apply(context_, if_false, if_true); // Labels swapped.
1423 break;
1424 }
1425
1426 case Token::TYPEOF: {
1427 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
1428 VariableProxy* proxy = expr->expression()->AsVariableProxy();
1429 if (proxy != NULL &&
1430 !proxy->var()->is_this() &&
1431 proxy->var()->is_global()) {
1432 Comment cmnt(masm_, "Global variable");
1433 __ ldr(r0, CodeGenerator::GlobalObject());
1434 __ push(r0);
1435 __ mov(r2, Operand(proxy->name()));
1436 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1437 // Use a regular load, not a contextual load, to avoid a reference
1438 // error.
1439 __ Call(ic, RelocInfo::CODE_TARGET);
1440 __ str(r0, MemOperand(sp));
1441 } else if (proxy != NULL &&
1442 proxy->var()->slot() != NULL &&
1443 proxy->var()->slot()->type() == Slot::LOOKUP) {
1444 __ mov(r0, Operand(proxy->name()));
1445 __ stm(db_w, sp, cp.bit() | r0.bit());
1446 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
1447 __ push(r0);
1448 } else {
1449 // This expression cannot throw a reference error at the top level.
1450 VisitForValue(expr->expression(), kStack);
1451 }
1452
1453 __ CallRuntime(Runtime::kTypeof, 1);
1454 Apply(context_, r0);
1455 break;
1456 }
1457
1458 case Token::ADD: {
1459 Comment cmt(masm_, "[ UnaryOperation (ADD)");
1460 VisitForValue(expr->expression(), kAccumulator);
1461 Label no_conversion;
1462 __ tst(result_register(), Operand(kSmiTagMask));
1463 __ b(eq, &no_conversion);
1464 __ push(r0);
1465 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
1466 __ bind(&no_conversion);
1467 Apply(context_, result_register());
1468 break;
1469 }
1470
1471 case Token::SUB: {
1472 Comment cmt(masm_, "[ UnaryOperation (SUB)");
1473 bool overwrite =
1474 (expr->expression()->AsBinaryOperation() != NULL &&
1475 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
1476 GenericUnaryOpStub stub(Token::SUB, overwrite);
1477 // GenericUnaryOpStub expects the argument to be in the
1478 // accumulator register r0.
1479 VisitForValue(expr->expression(), kAccumulator);
1480 __ CallStub(&stub);
1481 Apply(context_, r0);
1482 break;
1483 }
1484
1485 case Token::BIT_NOT: {
1486 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
1487 bool overwrite =
1488 (expr->expression()->AsBinaryOperation() != NULL &&
1489 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
1490 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
1491 // GenericUnaryOpStub expects the argument to be in the
1492 // accumulator register r0.
1493 VisitForValue(expr->expression(), kAccumulator);
1494 // Avoid calling the stub for Smis.
1495 Label smi, done;
1496 __ tst(result_register(), Operand(kSmiTagMask));
1497 __ b(eq, &smi);
1498 // Non-smi: call stub leaving result in accumulator register.
1499 __ CallStub(&stub);
1500 __ b(&done);
1501 // Perform operation directly on Smis.
1502 __ bind(&smi);
1503 __ mvn(result_register(), Operand(result_register()));
1504 // Bit-clear inverted smi-tag.
1505 __ bic(result_register(), result_register(), Operand(kSmiTagMask));
1506 __ bind(&done);
1507 Apply(context_, result_register());
1508 break;
1509 }
1510
1511 default:
1512 UNREACHABLE();
1513 }
1514}
1515
1516
1517void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
1518 Comment cmnt(masm_, "[ CountOperation");
1519
1520 // Expression can only be a property, a global or a (parameter or local)
1521 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1522 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1523 LhsKind assign_type = VARIABLE;
1524 Property* prop = expr->expression()->AsProperty();
1525 // In case of a property we use the uninitialized expression context
1526 // of the key to detect a named property.
1527 if (prop != NULL) {
1528 assign_type =
1529 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
1530 }
1531
1532 // Evaluate expression and get value.
1533 if (assign_type == VARIABLE) {
1534 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
1535 Location saved_location = location_;
1536 location_ = kAccumulator;
1537 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
1538 Expression::kValue);
1539 location_ = saved_location;
1540 } else {
1541 // Reserve space for result of postfix operation.
1542 if (expr->is_postfix() && context_ != Expression::kEffect) {
1543 __ mov(ip, Operand(Smi::FromInt(0)));
1544 __ push(ip);
1545 }
1546 VisitForValue(prop->obj(), kStack);
1547 if (assign_type == NAMED_PROPERTY) {
1548 EmitNamedPropertyLoad(prop);
1549 } else {
1550 VisitForValue(prop->key(), kStack);
1551 EmitKeyedPropertyLoad(prop);
1552 }
1553 }
1554
1555 // Call ToNumber only if operand is not a smi.
1556 Label no_conversion;
1557 __ tst(r0, Operand(kSmiTagMask));
1558 __ b(eq, &no_conversion);
1559 __ push(r0);
1560 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
1561 __ bind(&no_conversion);
1562
1563 // Save result for postfix expressions.
1564 if (expr->is_postfix()) {
1565 switch (context_) {
1566 case Expression::kUninitialized:
1567 UNREACHABLE();
1568 case Expression::kEffect:
1569 // Do not save result.
1570 break;
1571 case Expression::kValue:
1572 case Expression::kTest:
1573 case Expression::kValueTest:
1574 case Expression::kTestValue:
1575 // Save the result on the stack. If we have a named or keyed property
1576 // we store the result under the receiver that is currently on top
1577 // of the stack.
1578 switch (assign_type) {
1579 case VARIABLE:
1580 __ push(r0);
1581 break;
1582 case NAMED_PROPERTY:
1583 __ str(r0, MemOperand(sp, kPointerSize));
1584 break;
1585 case KEYED_PROPERTY:
1586 __ str(r0, MemOperand(sp, 2 * kPointerSize));
1587 break;
1588 }
1589 break;
1590 }
1591 }
1592
1593
1594 // Inline smi case if we are in a loop.
1595 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001596 int count_value = expr->op() == Token::INC ? 1 : -1;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001597 if (loop_depth() > 0) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001598 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001599 __ b(vs, &stub_call);
1600 // We could eliminate this smi check if we split the code at
1601 // the first smi check before calling ToNumber.
1602 __ tst(r0, Operand(kSmiTagMask));
1603 __ b(eq, &done);
1604 __ bind(&stub_call);
1605 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001606 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001607 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001608 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00001609 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001610 __ CallStub(&stub);
1611 __ bind(&done);
1612
1613 // Store the value returned in r0.
1614 switch (assign_type) {
1615 case VARIABLE:
1616 if (expr->is_postfix()) {
1617 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
1618 Expression::kEffect);
1619 // For all contexts except kEffect: We have the result on
1620 // top of the stack.
1621 if (context_ != Expression::kEffect) {
1622 ApplyTOS(context_);
1623 }
1624 } else {
1625 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
1626 context_);
1627 }
1628 break;
1629 case NAMED_PROPERTY: {
1630 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001631 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001632 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1633 __ Call(ic, RelocInfo::CODE_TARGET);
1634 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001635 if (context_ != Expression::kEffect) {
1636 ApplyTOS(context_);
1637 }
1638 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00001639 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001640 }
1641 break;
1642 }
1643 case KEYED_PROPERTY: {
1644 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1645 __ Call(ic, RelocInfo::CODE_TARGET);
1646 if (expr->is_postfix()) {
1647 __ Drop(2); // Result is on the stack under the key and the receiver.
1648 if (context_ != Expression::kEffect) {
1649 ApplyTOS(context_);
1650 }
1651 } else {
1652 DropAndApply(2, context_, r0);
1653 }
1654 break;
1655 }
1656 }
1657}
1658
1659
1660void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
1661 Comment cmnt(masm_, "[ BinaryOperation");
1662 switch (expr->op()) {
1663 case Token::COMMA:
1664 VisitForEffect(expr->left());
1665 Visit(expr->right());
1666 break;
1667
1668 case Token::OR:
1669 case Token::AND:
1670 EmitLogicalOperation(expr);
1671 break;
1672
1673 case Token::ADD:
1674 case Token::SUB:
1675 case Token::DIV:
1676 case Token::MOD:
1677 case Token::MUL:
1678 case Token::BIT_OR:
1679 case Token::BIT_AND:
1680 case Token::BIT_XOR:
1681 case Token::SHL:
1682 case Token::SHR:
1683 case Token::SAR:
1684 VisitForValue(expr->left(), kStack);
1685 VisitForValue(expr->right(), kAccumulator);
1686 EmitBinaryOp(expr->op(), context_);
1687 break;
1688
1689 default:
1690 UNREACHABLE();
1691 }
1692}
1693
1694
1695void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
1696 Comment cmnt(masm_, "[ CompareOperation");
1697
1698 // Always perform the comparison for its control flow. Pack the result
1699 // into the expression's context after the comparison is performed.
1700 Label materialize_true, materialize_false, done;
1701 // Initially assume we are in a test context.
1702 Label* if_true = true_label_;
1703 Label* if_false = false_label_;
1704 switch (context_) {
1705 case Expression::kUninitialized:
1706 UNREACHABLE();
1707 break;
1708 case Expression::kEffect:
1709 if_true = &done;
1710 if_false = &done;
1711 break;
1712 case Expression::kValue:
1713 if_true = &materialize_true;
1714 if_false = &materialize_false;
1715 break;
1716 case Expression::kTest:
1717 break;
1718 case Expression::kValueTest:
1719 if_true = &materialize_true;
1720 break;
1721 case Expression::kTestValue:
1722 if_false = &materialize_false;
1723 break;
1724 }
1725
1726 VisitForValue(expr->left(), kStack);
1727 switch (expr->op()) {
1728 case Token::IN:
1729 VisitForValue(expr->right(), kStack);
1730 __ InvokeBuiltin(Builtins::IN, CALL_JS);
1731 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
1732 __ cmp(r0, ip);
1733 __ b(eq, if_true);
1734 __ jmp(if_false);
1735 break;
1736
1737 case Token::INSTANCEOF: {
1738 VisitForValue(expr->right(), kStack);
1739 InstanceofStub stub;
1740 __ CallStub(&stub);
1741 __ tst(r0, r0);
1742 __ b(eq, if_true); // The stub returns 0 for true.
1743 __ jmp(if_false);
1744 break;
1745 }
1746
1747 default: {
1748 VisitForValue(expr->right(), kAccumulator);
1749 Condition cc = eq;
1750 bool strict = false;
1751 switch (expr->op()) {
1752 case Token::EQ_STRICT:
1753 strict = true;
1754 // Fall through
1755 case Token::EQ:
1756 cc = eq;
1757 __ pop(r1);
1758 break;
1759 case Token::LT:
1760 cc = lt;
1761 __ pop(r1);
1762 break;
1763 case Token::GT:
1764 // Reverse left and right sides to obtain ECMA-262 conversion order.
1765 cc = lt;
1766 __ mov(r1, result_register());
1767 __ pop(r0);
1768 break;
1769 case Token::LTE:
1770 // Reverse left and right sides to obtain ECMA-262 conversion order.
1771 cc = ge;
1772 __ mov(r1, result_register());
1773 __ pop(r0);
1774 break;
1775 case Token::GTE:
1776 cc = ge;
1777 __ pop(r1);
1778 break;
1779 case Token::IN:
1780 case Token::INSTANCEOF:
1781 default:
1782 UNREACHABLE();
1783 }
1784
1785 // The comparison stub expects the smi vs. smi case to be handled
1786 // before it is called.
1787 Label slow_case;
1788 __ orr(r2, r0, Operand(r1));
1789 __ tst(r2, Operand(kSmiTagMask));
1790 __ b(ne, &slow_case);
1791 __ cmp(r1, r0);
1792 __ b(cc, if_true);
1793 __ jmp(if_false);
1794
1795 __ bind(&slow_case);
1796 CompareStub stub(cc, strict);
1797 __ CallStub(&stub);
1798 __ cmp(r0, Operand(0));
1799 __ b(cc, if_true);
1800 __ jmp(if_false);
1801 }
1802 }
1803
1804 // Convert the result of the comparison into one expected for this
1805 // expression's context.
1806 Apply(context_, if_true, if_false);
1807}
1808
1809
1810void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
1811 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1812 Apply(context_, r0);
1813}
1814
1815
1816Register FullCodeGenerator::result_register() { return r0; }
1817
1818
1819Register FullCodeGenerator::context_register() { return cp; }
1820
1821
1822void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
1823 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
1824 __ str(value, MemOperand(fp, frame_offset));
1825}
1826
1827
1828void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
1829 __ ldr(dst, CodeGenerator::ContextOperand(cp, context_index));
1830}
1831
1832
1833// ----------------------------------------------------------------------------
1834// Non-local control flow support.
1835
1836void FullCodeGenerator::EnterFinallyBlock() {
1837 ASSERT(!result_register().is(r1));
1838 // Store result register while executing finally block.
1839 __ push(result_register());
1840 // Cook return address in link register to stack (smi encoded Code* delta)
1841 __ sub(r1, lr, Operand(masm_->CodeObject()));
1842 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
1843 ASSERT_EQ(0, kSmiTag);
1844 __ add(r1, r1, Operand(r1)); // Convert to smi.
1845 __ push(r1);
1846}
1847
1848
1849void FullCodeGenerator::ExitFinallyBlock() {
1850 ASSERT(!result_register().is(r1));
1851 // Restore result register from stack.
1852 __ pop(r1);
1853 // Uncook return address and return.
1854 __ pop(result_register());
1855 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
1856 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
1857 __ add(pc, r1, Operand(masm_->CodeObject()));
1858}
1859
1860
1861#undef __
1862
1863} } // namespace v8::internal