blob: fecc2137d3c7a6ce3a8313647e95519023a4f9ab [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
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000030#if defined(V8_TARGET_ARCH_ARM)
31
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000032#include "codegen-inl.h"
33#include "compiler.h"
34#include "debug.h"
35#include "full-codegen.h"
36#include "parser.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000037#include "scopes.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000038
39namespace v8 {
40namespace internal {
41
42#define __ ACCESS_MASM(masm_)
43
44// Generate code for a JS function. On entry to the function the receiver
45// and arguments have been pushed on the stack left to right. The actual
46// argument count matches the formal parameter count expected by the
47// function.
48//
49// The live registers are:
50// o r1: the JS function object being called (ie, ourselves)
51// o cp: our context
52// o fp: our caller's frame pointer
53// o sp: stack pointer
54// o lr: return address
55//
56// The function builds a JS frame. Please see JavaScriptFrameConstants in
57// frames-arm.h for its layout.
ager@chromium.org5c838252010-02-19 08:53:10 +000058void FullCodeGenerator::Generate(CompilationInfo* info, Mode mode) {
59 ASSERT(info_ == NULL);
60 info_ = info;
61 SetFunctionPosition(function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000062 Comment cmnt(masm_, "[ function compiled by full code generator");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000063
64 if (mode == PRIMARY) {
ager@chromium.org5c838252010-02-19 08:53:10 +000065 int locals_count = scope()->num_stack_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000066
kmillikin@chromium.org9155e252010-05-26 13:27:57 +000067 __ Push(lr, fp, cp, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000068 if (locals_count > 0) {
69 // Load undefined value here, so the value is ready for the loop
70 // below.
71 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
72 }
73 // Adjust fp to point to caller's fp.
74 __ add(fp, sp, Operand(2 * kPointerSize));
75
76 { Comment cmnt(masm_, "[ Allocate locals");
77 for (int i = 0; i < locals_count; i++) {
78 __ push(ip);
79 }
80 }
81
82 bool function_in_register = true;
83
84 // Possibly allocate a local context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +000085 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
86 if (heap_slots > 0) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000087 Comment cmnt(masm_, "[ Allocate local context");
88 // Argument to NewContext is the function, which is in r1.
89 __ push(r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +000090 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
91 FastNewContextStub stub(heap_slots);
92 __ CallStub(&stub);
93 } else {
94 __ CallRuntime(Runtime::kNewContext, 1);
95 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000096 function_in_register = false;
97 // Context is returned in both r0 and cp. It replaces the context
98 // passed to us. It's saved in the stack and kept live in cp.
99 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
100 // Copy any necessary parameters into the context.
ager@chromium.org5c838252010-02-19 08:53:10 +0000101 int num_parameters = scope()->num_parameters();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000102 for (int i = 0; i < num_parameters; i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000103 Slot* slot = scope()->parameter(i)->slot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000104 if (slot != NULL && slot->type() == Slot::CONTEXT) {
105 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
106 (num_parameters - 1 - i) * kPointerSize;
107 // Load parameter from stack.
108 __ ldr(r0, MemOperand(fp, parameter_offset));
109 // Store it in the context.
110 __ mov(r1, Operand(Context::SlotOffset(slot->index())));
111 __ str(r0, MemOperand(cp, r1));
112 // Update the write barrier. This clobbers all involved
113 // registers, so we have use a third register to avoid
114 // clobbering cp.
115 __ mov(r2, Operand(cp));
116 __ RecordWrite(r2, r1, r0);
117 }
118 }
119 }
120
ager@chromium.org5c838252010-02-19 08:53:10 +0000121 Variable* arguments = scope()->arguments()->AsVariable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000122 if (arguments != NULL) {
123 // Function uses arguments object.
124 Comment cmnt(masm_, "[ Allocate arguments object");
125 if (!function_in_register) {
126 // Load this again, if it's used by the local context below.
127 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
128 } else {
129 __ mov(r3, r1);
130 }
131 // Receiver is just before the parameters on the caller's stack.
ager@chromium.org5c838252010-02-19 08:53:10 +0000132 int offset = scope()->num_parameters() * kPointerSize;
133 __ add(r2, fp,
134 Operand(StandardFrameConstants::kCallerSPOffset + offset));
135 __ mov(r1, Operand(Smi::FromInt(scope()->num_parameters())));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000136 __ Push(r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000137
138 // Arguments to ArgumentsAccessStub:
139 // function, receiver address, parameter count.
140 // The stub will rewrite receiever and parameter count if the previous
141 // stack frame was an arguments adapter frame.
142 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
143 __ CallStub(&stub);
144 // Duplicate the value; move-to-slot operation might clobber registers.
145 __ mov(r3, r0);
146 Move(arguments->slot(), r0, r1, r2);
147 Slot* dot_arguments_slot =
ager@chromium.org5c838252010-02-19 08:53:10 +0000148 scope()->arguments_shadow()->AsVariable()->slot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000149 Move(dot_arguments_slot, r3, r1, r2);
150 }
151 }
152
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000153 { Comment cmnt(masm_, "[ Declarations");
154 // For named function expressions, declare the function name as a
155 // constant.
156 if (scope()->is_function_scope() && scope()->function() != NULL) {
157 EmitDeclaration(scope()->function(), Variable::CONST, NULL);
158 }
159 // Visit all the explicit declarations unless there is an illegal
160 // redeclaration.
161 if (scope()->HasIllegalRedeclaration()) {
162 scope()->VisitIllegalRedeclaration(this);
163 } else {
164 VisitDeclarations(scope()->declarations());
165 }
166 }
167
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000168 // Check the stack for overflow or break request.
169 // Put the lr setup instruction in the delay slot. The kInstrSize is
170 // added to the implicit 8 byte offset that always applies to operations
171 // with pc and gives a return address 12 bytes down.
172 { Comment cmnt(masm_, "[ Stack check");
173 __ LoadRoot(r2, Heap::kStackLimitRootIndex);
174 __ add(lr, pc, Operand(Assembler::kInstrSize));
175 __ cmp(sp, Operand(r2));
176 StackCheckStub stub;
177 __ mov(pc,
178 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
179 RelocInfo::CODE_TARGET),
180 LeaveCC,
181 lo);
182 }
183
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000184 if (FLAG_trace) {
185 __ CallRuntime(Runtime::kTraceEnter, 0);
186 }
187
188 { Comment cmnt(masm_, "[ Body");
189 ASSERT(loop_depth() == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000190 VisitStatements(function()->body());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000191 ASSERT(loop_depth() == 0);
192 }
193
194 { Comment cmnt(masm_, "[ return <undefined>;");
195 // Emit a 'return undefined' in case control fell off the end of the
196 // body.
197 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
198 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000199 EmitReturnSequence(function()->end_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000200}
201
202
203void FullCodeGenerator::EmitReturnSequence(int position) {
204 Comment cmnt(masm_, "[ Return sequence");
205 if (return_label_.is_bound()) {
206 __ b(&return_label_);
207 } else {
208 __ bind(&return_label_);
209 if (FLAG_trace) {
210 // Push the return value on the stack as the parameter.
211 // Runtime::TraceExit returns its parameter in r0.
212 __ push(r0);
213 __ CallRuntime(Runtime::kTraceExit, 1);
214 }
215
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000216#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000217 // Add a label for checking the size of the code used for returning.
218 Label check_exit_codesize;
219 masm_->bind(&check_exit_codesize);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000220#endif
221 // Make sure that the constant pool is not emitted inside of the return
222 // sequence.
223 { Assembler::BlockConstPoolScope block_const_pool(masm_);
224 // Here we use masm_-> instead of the __ macro to avoid the code coverage
225 // tool from instrumenting as we rely on the code size here.
226 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
227 CodeGenerator::RecordPositions(masm_, position);
228 __ RecordJSReturn();
229 masm_->mov(sp, fp);
230 masm_->ldm(ia_w, sp, fp.bit() | lr.bit());
231 masm_->add(sp, sp, Operand(sp_delta));
232 masm_->Jump(lr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000233 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000234
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000235#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000236 // Check that the size of the code used for returning matches what is
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000237 // expected by the debugger. If the sp_delts above cannot be encoded in the
238 // add instruction the add will generate two instructions.
239 int return_sequence_length =
240 masm_->InstructionsGeneratedSince(&check_exit_codesize);
241 CHECK(return_sequence_length == Assembler::kJSReturnSequenceLength ||
242 return_sequence_length == Assembler::kJSReturnSequenceLength + 1);
243#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000244 }
245}
246
247
248void FullCodeGenerator::Apply(Expression::Context context, Register reg) {
249 switch (context) {
250 case Expression::kUninitialized:
251 UNREACHABLE();
252
253 case Expression::kEffect:
254 // Nothing to do.
255 break;
256
257 case Expression::kValue:
258 // Move value into place.
259 switch (location_) {
260 case kAccumulator:
261 if (!reg.is(result_register())) __ mov(result_register(), reg);
262 break;
263 case kStack:
264 __ push(reg);
265 break;
266 }
267 break;
268
269 case Expression::kValueTest:
270 case Expression::kTestValue:
271 // Push an extra copy of the value in case it's needed.
272 __ push(reg);
273 // Fall through.
274
275 case Expression::kTest:
276 // We always call the runtime on ARM, so push the value as argument.
277 __ push(reg);
278 DoTest(context);
279 break;
280 }
281}
282
283
284void FullCodeGenerator::Apply(Expression::Context context, Slot* slot) {
285 switch (context) {
286 case Expression::kUninitialized:
287 UNREACHABLE();
288 case Expression::kEffect:
289 // Nothing to do.
290 break;
291 case Expression::kValue:
292 case Expression::kTest:
293 case Expression::kValueTest:
294 case Expression::kTestValue:
295 // On ARM we have to move the value into a register to do anything
296 // with it.
297 Move(result_register(), slot);
298 Apply(context, result_register());
299 break;
300 }
301}
302
303
304void FullCodeGenerator::Apply(Expression::Context context, Literal* lit) {
305 switch (context) {
306 case Expression::kUninitialized:
307 UNREACHABLE();
308 case Expression::kEffect:
309 break;
310 // Nothing to do.
311 case Expression::kValue:
312 case Expression::kTest:
313 case Expression::kValueTest:
314 case Expression::kTestValue:
315 // On ARM we have to move the value into a register to do anything
316 // with it.
317 __ mov(result_register(), Operand(lit->handle()));
318 Apply(context, result_register());
319 break;
320 }
321}
322
323
324void FullCodeGenerator::ApplyTOS(Expression::Context context) {
325 switch (context) {
326 case Expression::kUninitialized:
327 UNREACHABLE();
328
329 case Expression::kEffect:
330 __ Drop(1);
331 break;
332
333 case Expression::kValue:
334 switch (location_) {
335 case kAccumulator:
336 __ pop(result_register());
337 break;
338 case kStack:
339 break;
340 }
341 break;
342
343 case Expression::kValueTest:
344 case Expression::kTestValue:
345 // Duplicate the value on the stack in case it's needed.
346 __ ldr(ip, MemOperand(sp));
347 __ push(ip);
348 // Fall through.
349
350 case Expression::kTest:
351 DoTest(context);
352 break;
353 }
354}
355
356
357void FullCodeGenerator::DropAndApply(int count,
358 Expression::Context context,
359 Register reg) {
360 ASSERT(count > 0);
361 ASSERT(!reg.is(sp));
362 switch (context) {
363 case Expression::kUninitialized:
364 UNREACHABLE();
365
366 case Expression::kEffect:
367 __ Drop(count);
368 break;
369
370 case Expression::kValue:
371 switch (location_) {
372 case kAccumulator:
373 __ Drop(count);
374 if (!reg.is(result_register())) __ mov(result_register(), reg);
375 break;
376 case kStack:
377 if (count > 1) __ Drop(count - 1);
378 __ str(reg, MemOperand(sp));
379 break;
380 }
381 break;
382
383 case Expression::kTest:
384 if (count > 1) __ Drop(count - 1);
385 __ str(reg, MemOperand(sp));
386 DoTest(context);
387 break;
388
389 case Expression::kValueTest:
390 case Expression::kTestValue:
391 if (count == 1) {
392 __ str(reg, MemOperand(sp));
393 __ push(reg);
394 } else { // count > 1
395 __ Drop(count - 2);
396 __ str(reg, MemOperand(sp, kPointerSize));
397 __ str(reg, MemOperand(sp));
398 }
399 DoTest(context);
400 break;
401 }
402}
403
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000404void FullCodeGenerator::PrepareTest(Label* materialize_true,
405 Label* materialize_false,
406 Label** if_true,
407 Label** if_false) {
408 switch (context_) {
409 case Expression::kUninitialized:
410 UNREACHABLE();
411 break;
412 case Expression::kEffect:
413 // In an effect context, the true and the false case branch to the
414 // same label.
415 *if_true = *if_false = materialize_true;
416 break;
417 case Expression::kValue:
418 *if_true = materialize_true;
419 *if_false = materialize_false;
420 break;
421 case Expression::kTest:
422 *if_true = true_label_;
423 *if_false = false_label_;
424 break;
425 case Expression::kValueTest:
426 *if_true = materialize_true;
427 *if_false = false_label_;
428 break;
429 case Expression::kTestValue:
430 *if_true = true_label_;
431 *if_false = materialize_false;
432 break;
433 }
434}
435
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000436
437void FullCodeGenerator::Apply(Expression::Context context,
438 Label* materialize_true,
439 Label* materialize_false) {
440 switch (context) {
441 case Expression::kUninitialized:
442
443 case Expression::kEffect:
444 ASSERT_EQ(materialize_true, materialize_false);
445 __ bind(materialize_true);
446 break;
447
448 case Expression::kValue: {
449 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000450 switch (location_) {
451 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000452 __ bind(materialize_true);
453 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
454 __ jmp(&done);
455 __ bind(materialize_false);
456 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000457 break;
458 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000459 __ bind(materialize_true);
460 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
461 __ push(ip);
462 __ jmp(&done);
463 __ bind(materialize_false);
464 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
465 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000466 break;
467 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000468 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000469 break;
470 }
471
472 case Expression::kTest:
473 break;
474
475 case Expression::kValueTest:
476 __ bind(materialize_true);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000477 switch (location_) {
478 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000479 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000480 break;
481 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000482 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
483 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000484 break;
485 }
486 __ jmp(true_label_);
487 break;
488
489 case Expression::kTestValue:
490 __ bind(materialize_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000491 switch (location_) {
492 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000493 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000494 break;
495 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000496 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
497 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000498 break;
499 }
500 __ jmp(false_label_);
501 break;
502 }
503}
504
505
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000506// Convert constant control flow (true or false) to the result expected for
507// a given expression context.
508void FullCodeGenerator::Apply(Expression::Context context, bool flag) {
509 switch (context) {
510 case Expression::kUninitialized:
511 UNREACHABLE();
512 break;
513 case Expression::kEffect:
514 break;
515 case Expression::kValue: {
516 Heap::RootListIndex value_root_index =
517 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
518 switch (location_) {
519 case kAccumulator:
520 __ LoadRoot(result_register(), value_root_index);
521 break;
522 case kStack:
523 __ LoadRoot(ip, value_root_index);
524 __ push(ip);
525 break;
526 }
527 break;
528 }
529 case Expression::kTest:
530 __ b(flag ? true_label_ : false_label_);
531 break;
532 case Expression::kTestValue:
533 switch (location_) {
534 case kAccumulator:
535 // If value is false it's needed.
536 if (!flag) __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
537 break;
538 case kStack:
539 // If value is false it's needed.
540 if (!flag) {
541 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
542 __ push(ip);
543 }
544 break;
545 }
546 __ b(flag ? true_label_ : false_label_);
547 break;
548 case Expression::kValueTest:
549 switch (location_) {
550 case kAccumulator:
551 // If value is true it's needed.
552 if (flag) __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
553 break;
554 case kStack:
555 // If value is true it's needed.
556 if (flag) {
557 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
558 __ push(ip);
559 }
560 break;
561 }
562 __ b(flag ? true_label_ : false_label_);
563 break;
564 }
565}
566
567
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000568void FullCodeGenerator::DoTest(Expression::Context context) {
569 // The value to test is pushed on the stack, and duplicated on the stack
570 // if necessary (for value/test and test/value contexts).
571 ASSERT_NE(NULL, true_label_);
572 ASSERT_NE(NULL, false_label_);
573
574 // Call the runtime to find the boolean value of the source and then
575 // translate it into control flow to the pair of labels.
576 __ CallRuntime(Runtime::kToBool, 1);
577 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
578 __ cmp(r0, ip);
579
580 // Complete based on the context.
581 switch (context) {
582 case Expression::kUninitialized:
583 case Expression::kEffect:
584 case Expression::kValue:
585 UNREACHABLE();
586
587 case Expression::kTest:
588 __ b(eq, true_label_);
589 __ jmp(false_label_);
590 break;
591
592 case Expression::kValueTest: {
593 Label discard;
594 switch (location_) {
595 case kAccumulator:
596 __ b(ne, &discard);
597 __ pop(result_register());
598 __ jmp(true_label_);
599 break;
600 case kStack:
601 __ b(eq, true_label_);
602 break;
603 }
604 __ bind(&discard);
605 __ Drop(1);
606 __ jmp(false_label_);
607 break;
608 }
609
610 case Expression::kTestValue: {
611 Label discard;
612 switch (location_) {
613 case kAccumulator:
614 __ b(eq, &discard);
615 __ pop(result_register());
616 __ jmp(false_label_);
617 break;
618 case kStack:
619 __ b(ne, false_label_);
620 break;
621 }
622 __ bind(&discard);
623 __ Drop(1);
624 __ jmp(true_label_);
625 break;
626 }
627 }
628}
629
630
631MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
632 switch (slot->type()) {
633 case Slot::PARAMETER:
634 case Slot::LOCAL:
635 return MemOperand(fp, SlotOffset(slot));
636 case Slot::CONTEXT: {
637 int context_chain_length =
ager@chromium.org5c838252010-02-19 08:53:10 +0000638 scope()->ContextChainLength(slot->var()->scope());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000639 __ LoadContext(scratch, context_chain_length);
640 return CodeGenerator::ContextOperand(scratch, slot->index());
641 }
642 case Slot::LOOKUP:
643 UNREACHABLE();
644 }
645 UNREACHABLE();
646 return MemOperand(r0, 0);
647}
648
649
650void FullCodeGenerator::Move(Register destination, Slot* source) {
651 // Use destination as scratch.
652 MemOperand slot_operand = EmitSlotSearch(source, destination);
653 __ ldr(destination, slot_operand);
654}
655
656
657void FullCodeGenerator::Move(Slot* dst,
658 Register src,
659 Register scratch1,
660 Register scratch2) {
661 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
662 ASSERT(!scratch1.is(src) && !scratch2.is(src));
663 MemOperand location = EmitSlotSearch(dst, scratch1);
664 __ str(src, location);
665 // Emit the write barrier code if the location is in the heap.
666 if (dst->type() == Slot::CONTEXT) {
667 __ mov(scratch2, Operand(Context::SlotOffset(dst->index())));
668 __ RecordWrite(scratch1, scratch2, src);
669 }
670}
671
672
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000673void FullCodeGenerator::EmitDeclaration(Variable* variable,
674 Variable::Mode mode,
675 FunctionLiteral* function) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000676 Comment cmnt(masm_, "[ Declaration");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000677 ASSERT(variable != NULL); // Must have been resolved.
678 Slot* slot = variable->slot();
679 Property* prop = variable->AsProperty();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000680
681 if (slot != NULL) {
682 switch (slot->type()) {
683 case Slot::PARAMETER:
684 case Slot::LOCAL:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000685 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000686 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
687 __ str(ip, MemOperand(fp, SlotOffset(slot)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000688 } else if (function != NULL) {
689 VisitForValue(function, kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000690 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
691 }
692 break;
693
694 case Slot::CONTEXT:
695 // We bypass the general EmitSlotSearch because we know more about
696 // this specific context.
697
698 // The variable in the decl always resides in the current context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000699 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000700 if (FLAG_debug_code) {
701 // Check if we have the correct context pointer.
702 __ ldr(r1,
703 CodeGenerator::ContextOperand(cp, Context::FCONTEXT_INDEX));
704 __ cmp(r1, cp);
705 __ Check(eq, "Unexpected declaration in current context.");
706 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000707 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000708 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
709 __ str(ip, CodeGenerator::ContextOperand(cp, slot->index()));
710 // No write barrier since the_hole_value is in old space.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000711 } else if (function != NULL) {
712 VisitForValue(function, kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000713 __ str(result_register(),
714 CodeGenerator::ContextOperand(cp, slot->index()));
715 int offset = Context::SlotOffset(slot->index());
716 __ mov(r2, Operand(offset));
717 // We know that we have written a function, which is not a smi.
718 __ mov(r1, Operand(cp));
719 __ RecordWrite(r1, r2, result_register());
720 }
721 break;
722
723 case Slot::LOOKUP: {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000724 __ mov(r2, Operand(variable->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000725 // Declaration nodes are always introduced in one of two modes.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000726 ASSERT(mode == Variable::VAR ||
727 mode == Variable::CONST);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000728 PropertyAttributes attr =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000729 (mode == Variable::VAR) ? NONE : READ_ONLY;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000730 __ mov(r1, Operand(Smi::FromInt(attr)));
731 // Push initial value, if any.
732 // Note: For variables we must not push an initial value (such as
733 // 'undefined') because we may have a (legal) redeclaration and we
734 // must not destroy the current value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000735 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000736 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000737 __ Push(cp, r2, r1, r0);
738 } else if (function != NULL) {
739 __ Push(cp, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000740 // Push initial value for function declaration.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000741 VisitForValue(function, kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000742 } else {
743 __ mov(r0, Operand(Smi::FromInt(0))); // No initial value!
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000744 __ Push(cp, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000745 }
746 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
747 break;
748 }
749 }
750
751 } else if (prop != NULL) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000752 if (function != NULL || mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000753 // We are declaring a function or constant that rewrites to a
754 // property. Use (keyed) IC to set the initial value.
755 VisitForValue(prop->obj(), kStack);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000756 if (function != NULL) {
757 VisitForValue(prop->key(), kStack);
758 VisitForValue(function, kAccumulator);
759 __ pop(r1); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000760 } else {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000761 VisitForValue(prop->key(), kAccumulator);
762 __ mov(r1, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000763 __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex);
764 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000765 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000766
767 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
768 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000769 // Value in r0 is ignored (declarations are statements).
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000770 }
771 }
772}
773
774
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000775void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
776 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
777}
778
779
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000780void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
781 // Call the runtime to declare the globals.
782 // The context is the first argument.
783 __ mov(r1, Operand(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000784 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000785 __ Push(cp, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000786 __ CallRuntime(Runtime::kDeclareGlobals, 3);
787 // Return value is ignored.
788}
789
790
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000791void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000792 Comment cmnt(masm_, "[ SwitchStatement");
793 Breakable nested_statement(this, stmt);
794 SetStatementPosition(stmt);
795 // Keep the switch value on the stack until a case matches.
796 VisitForValue(stmt->tag(), kStack);
797
798 ZoneList<CaseClause*>* clauses = stmt->cases();
799 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
800
801 Label next_test; // Recycled for each test.
802 // Compile all the tests with branches to their bodies.
803 for (int i = 0; i < clauses->length(); i++) {
804 CaseClause* clause = clauses->at(i);
805 // The default is not a test, but remember it as final fall through.
806 if (clause->is_default()) {
807 default_clause = clause;
808 continue;
809 }
810
811 Comment cmnt(masm_, "[ Case comparison");
812 __ bind(&next_test);
813 next_test.Unuse();
814
815 // Compile the label expression.
816 VisitForValue(clause->label(), kAccumulator);
817
818 // Perform the comparison as if via '==='. The comparison stub expects
819 // the smi vs. smi case to be handled before it is called.
820 Label slow_case;
821 __ ldr(r1, MemOperand(sp, 0)); // Switch value.
822 __ mov(r2, r1);
823 __ orr(r2, r2, r0);
824 __ tst(r2, Operand(kSmiTagMask));
825 __ b(ne, &slow_case);
826 __ cmp(r1, r0);
827 __ b(ne, &next_test);
828 __ Drop(1); // Switch value is no longer needed.
829 __ b(clause->body_target()->entry_label());
830
831 __ bind(&slow_case);
832 CompareStub stub(eq, true);
833 __ CallStub(&stub);
834 __ tst(r0, r0);
835 __ b(ne, &next_test);
836 __ Drop(1); // Switch value is no longer needed.
837 __ b(clause->body_target()->entry_label());
838 }
839
840 // Discard the test value and jump to the default if present, otherwise to
841 // the end of the statement.
842 __ bind(&next_test);
843 __ Drop(1); // Switch value is no longer needed.
844 if (default_clause == NULL) {
845 __ b(nested_statement.break_target());
846 } else {
847 __ b(default_clause->body_target()->entry_label());
848 }
849
850 // Compile all the case bodies.
851 for (int i = 0; i < clauses->length(); i++) {
852 Comment cmnt(masm_, "[ Case body");
853 CaseClause* clause = clauses->at(i);
854 __ bind(clause->body_target()->entry_label());
855 VisitStatements(clause->statements());
856 }
857
858 __ bind(nested_statement.break_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000859}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000860
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000861
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000862void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000863 Comment cmnt(masm_, "[ ForInStatement");
864 SetStatementPosition(stmt);
865
866 Label loop, exit;
867 ForIn loop_statement(this, stmt);
868 increment_loop_depth();
869
870 // Get the object to enumerate over. Both SpiderMonkey and JSC
871 // ignore null and undefined in contrast to the specification; see
872 // ECMA-262 section 12.6.4.
873 VisitForValue(stmt->enumerable(), kAccumulator);
874 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
875 __ cmp(r0, ip);
876 __ b(eq, &exit);
877 __ LoadRoot(ip, Heap::kNullValueRootIndex);
878 __ cmp(r0, ip);
879 __ b(eq, &exit);
880
881 // Convert the object to a JS object.
882 Label convert, done_convert;
883 __ BranchOnSmi(r0, &convert);
884 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
885 __ b(hs, &done_convert);
886 __ bind(&convert);
887 __ push(r0);
888 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
889 __ bind(&done_convert);
890 __ push(r0);
891
892 // TODO(kasperl): Check cache validity in generated code. This is a
893 // fast case for the JSObject::IsSimpleEnum cache validity
894 // checks. If we cannot guarantee cache validity, call the runtime
895 // system to check cache validity or get the property names in a
896 // fixed array.
897
898 // Get the set of properties to enumerate.
899 __ push(r0); // Duplicate the enumerable object on the stack.
900 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
901
902 // If we got a map from the runtime call, we can do a fast
903 // modification check. Otherwise, we got a fixed array, and we have
904 // to do a slow check.
905 Label fixed_array;
906 __ mov(r2, r0);
907 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
908 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
909 __ cmp(r1, ip);
910 __ b(ne, &fixed_array);
911
912 // We got a map in register r0. Get the enumeration cache from it.
913 __ ldr(r1, FieldMemOperand(r0, Map::kInstanceDescriptorsOffset));
914 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
915 __ ldr(r2, FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
916
917 // Setup the four remaining stack slots.
918 __ push(r0); // Map.
919 __ ldr(r1, FieldMemOperand(r2, FixedArray::kLengthOffset));
920 __ mov(r1, Operand(r1, LSL, kSmiTagSize));
921 __ mov(r0, Operand(Smi::FromInt(0)));
922 // Push enumeration cache, enumeration cache length (as smi) and zero.
923 __ Push(r2, r1, r0);
924 __ jmp(&loop);
925
926 // We got a fixed array in register r0. Iterate through that.
927 __ bind(&fixed_array);
928 __ mov(r1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
929 __ Push(r1, r0);
930 __ ldr(r1, FieldMemOperand(r0, FixedArray::kLengthOffset));
931 __ mov(r1, Operand(r1, LSL, kSmiTagSize));
932 __ mov(r0, Operand(Smi::FromInt(0)));
933 __ Push(r1, r0); // Fixed array length (as smi) and initial index.
934
935 // Generate code for doing the condition check.
936 __ bind(&loop);
937 // Load the current count to r0, load the length to r1.
938 __ Ldrd(r0, r1, MemOperand(sp, 0 * kPointerSize));
939 __ cmp(r0, r1); // Compare to the array length.
940 __ b(hs, loop_statement.break_target());
941
942 // Get the current entry of the array into register r3.
943 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
944 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
945 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
946
947 // Get the expected map from the stack or a zero map in the
948 // permanent slow case into register r2.
949 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
950
951 // Check if the expected map still matches that of the enumerable.
952 // If not, we have to filter the key.
953 Label update_each;
954 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
955 __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
956 __ cmp(r4, Operand(r2));
957 __ b(eq, &update_each);
958
959 // Convert the entry to a string or null if it isn't a property
960 // anymore. If the property has been removed while iterating, we
961 // just skip it.
962 __ push(r1); // Enumerable.
963 __ push(r3); // Current entry.
964 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
965 __ mov(r3, Operand(r0));
966 __ LoadRoot(ip, Heap::kNullValueRootIndex);
967 __ cmp(r3, ip);
968 __ b(eq, loop_statement.continue_target());
969
970 // Update the 'each' property or variable from the possibly filtered
971 // entry in register r3.
972 __ bind(&update_each);
973 __ mov(result_register(), r3);
974 // Perform the assignment as if via '='.
975 EmitAssignment(stmt->each());
976
977 // Generate code for the body of the loop.
978 Label stack_limit_hit, stack_check_done;
979 Visit(stmt->body());
980
981 __ StackLimitCheck(&stack_limit_hit);
982 __ bind(&stack_check_done);
983
984 // Generate code for the going to the next element by incrementing
985 // the index (smi) stored on top of the stack.
986 __ bind(loop_statement.continue_target());
987 __ pop(r0);
988 __ add(r0, r0, Operand(Smi::FromInt(1)));
989 __ push(r0);
990 __ b(&loop);
991
992 // Slow case for the stack limit check.
993 StackCheckStub stack_check_stub;
994 __ bind(&stack_limit_hit);
995 __ CallStub(&stack_check_stub);
996 __ b(&stack_check_done);
997
998 // Remove the pointers stored on the stack.
999 __ bind(loop_statement.break_target());
1000 __ Drop(5);
1001
1002 // Exit and decrement the loop depth.
1003 __ bind(&exit);
1004 decrement_loop_depth();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001005}
1006
1007
1008void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info) {
1009 // Use the fast case closure allocation code that allocates in new
1010 // space for nested functions that don't need literals cloning.
1011 if (scope()->is_function_scope() && info->num_literals() == 0) {
1012 FastNewClosureStub stub;
1013 __ mov(r0, Operand(info));
1014 __ push(r0);
1015 __ CallStub(&stub);
1016 } else {
1017 __ mov(r0, Operand(info));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001018 __ Push(cp, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001019 __ CallRuntime(Runtime::kNewClosure, 2);
1020 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001021 Apply(context_, r0);
1022}
1023
1024
1025void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
1026 Comment cmnt(masm_, "[ VariableProxy");
1027 EmitVariableLoad(expr->var(), context_);
1028}
1029
1030
1031void FullCodeGenerator::EmitVariableLoad(Variable* var,
1032 Expression::Context context) {
1033 // Four cases: non-this global variables, lookup slots, all other
1034 // types of slots, and parameters that rewrite to explicit property
1035 // accesses on the arguments object.
1036 Slot* slot = var->slot();
1037 Property* property = var->AsProperty();
1038
1039 if (var->is_global() && !var->is_this()) {
1040 Comment cmnt(masm_, "Global variable");
1041 // Use inline caching. Variable name is passed in r2 and the global
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001042 // object (receiver) in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001043 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001044 __ mov(r2, Operand(var->name()));
1045 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1046 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001047 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001048
1049 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
1050 Comment cmnt(masm_, "Lookup slot");
1051 __ mov(r1, Operand(var->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001052 __ Push(cp, r1); // Context and name.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001053 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1054 Apply(context, r0);
1055
1056 } else if (slot != NULL) {
1057 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1058 ? "Context slot"
1059 : "Stack slot");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001060 if (var->mode() == Variable::CONST) {
1061 // Constants may be the hole value if they have not been initialized.
1062 // Unhole them.
1063 Label done;
1064 MemOperand slot_operand = EmitSlotSearch(slot, r0);
1065 __ ldr(r0, slot_operand);
1066 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1067 __ cmp(r0, ip);
1068 __ b(ne, &done);
1069 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1070 __ bind(&done);
1071 Apply(context, r0);
1072 } else {
1073 Apply(context, slot);
1074 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001075 } else {
1076 Comment cmnt(masm_, "Rewritten parameter");
1077 ASSERT_NOT_NULL(property);
1078 // Rewritten parameter accesses are of the form "slot[literal]".
1079
1080 // Assert that the object is in a slot.
1081 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
1082 ASSERT_NOT_NULL(object_var);
1083 Slot* object_slot = object_var->slot();
1084 ASSERT_NOT_NULL(object_slot);
1085
1086 // Load the object.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001087 Move(r1, object_slot);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001088
1089 // Assert that the key is a smi.
1090 Literal* key_literal = property->key()->AsLiteral();
1091 ASSERT_NOT_NULL(key_literal);
1092 ASSERT(key_literal->handle()->IsSmi());
1093
1094 // Load the key.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001095 __ mov(r0, Operand(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001096
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001097 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001098 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1099 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001100 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001101 }
1102}
1103
1104
1105void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1106 Comment cmnt(masm_, "[ RegExpLiteral");
1107 Label done;
1108 // Registers will be used as follows:
1109 // r4 = JS function, literals array
1110 // r3 = literal index
1111 // r2 = RegExp pattern
1112 // r1 = RegExp flags
1113 // r0 = temp + return value (RegExp literal)
1114 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1115 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
1116 int literal_offset =
1117 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1118 __ ldr(r0, FieldMemOperand(r4, literal_offset));
1119 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1120 __ cmp(r0, ip);
1121 __ b(ne, &done);
1122 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
1123 __ mov(r2, Operand(expr->pattern()));
1124 __ mov(r1, Operand(expr->flags()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001125 __ Push(r4, r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001126 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1127 __ bind(&done);
1128 Apply(context_, r0);
1129}
1130
1131
1132void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1133 Comment cmnt(masm_, "[ ObjectLiteral");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001134 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1135 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1136 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1137 __ mov(r1, Operand(expr->constant_properties()));
1138 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001139 __ Push(r3, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001140 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001141 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001142 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001143 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001144 }
1145
1146 // If result_saved is true the result is on top of the stack. If
1147 // result_saved is false the result is in r0.
1148 bool result_saved = false;
1149
1150 for (int i = 0; i < expr->properties()->length(); i++) {
1151 ObjectLiteral::Property* property = expr->properties()->at(i);
1152 if (property->IsCompileTimeValue()) continue;
1153
1154 Literal* key = property->key();
1155 Expression* value = property->value();
1156 if (!result_saved) {
1157 __ push(r0); // Save result on stack
1158 result_saved = true;
1159 }
1160 switch (property->kind()) {
1161 case ObjectLiteral::Property::CONSTANT:
1162 UNREACHABLE();
1163 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1164 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1165 // Fall through.
1166 case ObjectLiteral::Property::COMPUTED:
1167 if (key->handle()->IsSymbol()) {
1168 VisitForValue(value, kAccumulator);
1169 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001170 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001171 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1172 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001173 break;
1174 }
1175 // Fall through.
1176 case ObjectLiteral::Property::PROTOTYPE:
1177 // Duplicate receiver on stack.
1178 __ ldr(r0, MemOperand(sp));
1179 __ push(r0);
1180 VisitForValue(key, kStack);
1181 VisitForValue(value, kStack);
1182 __ CallRuntime(Runtime::kSetProperty, 3);
1183 break;
1184 case ObjectLiteral::Property::GETTER:
1185 case ObjectLiteral::Property::SETTER:
1186 // Duplicate receiver on stack.
1187 __ ldr(r0, MemOperand(sp));
1188 __ push(r0);
1189 VisitForValue(key, kStack);
1190 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1191 Smi::FromInt(1) :
1192 Smi::FromInt(0)));
1193 __ push(r1);
1194 VisitForValue(value, kStack);
1195 __ CallRuntime(Runtime::kDefineAccessor, 4);
1196 break;
1197 }
1198 }
1199
1200 if (result_saved) {
1201 ApplyTOS(context_);
1202 } else {
1203 Apply(context_, r0);
1204 }
1205}
1206
1207
1208void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1209 Comment cmnt(masm_, "[ ArrayLiteral");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001210
1211 ZoneList<Expression*>* subexprs = expr->values();
1212 int length = subexprs->length();
1213
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001214 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1215 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1216 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1217 __ mov(r1, Operand(expr->constant_elements()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001218 __ Push(r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001219 if (expr->depth() > 1) {
1220 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001221 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001222 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001223 } else {
1224 FastCloneShallowArrayStub stub(length);
1225 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001226 }
1227
1228 bool result_saved = false; // Is the result saved to the stack?
1229
1230 // Emit code to evaluate all the non-constant subexpressions and to store
1231 // them into the newly cloned array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001232 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001233 Expression* subexpr = subexprs->at(i);
1234 // If the subexpression is a literal or a simple materialized literal it
1235 // is already set in the cloned array.
1236 if (subexpr->AsLiteral() != NULL ||
1237 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1238 continue;
1239 }
1240
1241 if (!result_saved) {
1242 __ push(r0);
1243 result_saved = true;
1244 }
1245 VisitForValue(subexpr, kAccumulator);
1246
1247 // Store the subexpression value in the array's elements.
1248 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
1249 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1250 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1251 __ str(result_register(), FieldMemOperand(r1, offset));
1252
1253 // Update the write barrier for the array store with r0 as the scratch
1254 // register.
1255 __ mov(r2, Operand(offset));
1256 __ RecordWrite(r1, r2, result_register());
1257 }
1258
1259 if (result_saved) {
1260 ApplyTOS(context_);
1261 } else {
1262 Apply(context_, r0);
1263 }
1264}
1265
1266
ager@chromium.org5c838252010-02-19 08:53:10 +00001267void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1268 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001269 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1270 // on the left-hand side.
1271 if (!expr->target()->IsValidLeftHandSide()) {
1272 VisitForEffect(expr->target());
1273 return;
1274 }
1275
ager@chromium.org5c838252010-02-19 08:53:10 +00001276 // Left-hand side can only be a property, a global or a (parameter or local)
1277 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1278 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1279 LhsKind assign_type = VARIABLE;
1280 Property* prop = expr->target()->AsProperty();
1281 if (prop != NULL) {
1282 assign_type =
1283 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
1284 }
1285
1286 // Evaluate LHS expression.
1287 switch (assign_type) {
1288 case VARIABLE:
1289 // Nothing to do here.
1290 break;
1291 case NAMED_PROPERTY:
1292 if (expr->is_compound()) {
1293 // We need the receiver both on the stack and in the accumulator.
1294 VisitForValue(prop->obj(), kAccumulator);
1295 __ push(result_register());
1296 } else {
1297 VisitForValue(prop->obj(), kStack);
1298 }
1299 break;
1300 case KEYED_PROPERTY:
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001301 // We need the key and receiver on both the stack and in r0 and r1.
1302 if (expr->is_compound()) {
1303 VisitForValue(prop->obj(), kStack);
1304 VisitForValue(prop->key(), kAccumulator);
1305 __ ldr(r1, MemOperand(sp, 0));
1306 __ push(r0);
1307 } else {
1308 VisitForValue(prop->obj(), kStack);
1309 VisitForValue(prop->key(), kStack);
1310 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001311 break;
1312 }
1313
1314 // If we have a compound assignment: Get value of LHS expression and
1315 // store in on top of the stack.
1316 if (expr->is_compound()) {
1317 Location saved_location = location_;
1318 location_ = kStack;
1319 switch (assign_type) {
1320 case VARIABLE:
1321 EmitVariableLoad(expr->target()->AsVariableProxy()->var(),
1322 Expression::kValue);
1323 break;
1324 case NAMED_PROPERTY:
1325 EmitNamedPropertyLoad(prop);
1326 __ push(result_register());
1327 break;
1328 case KEYED_PROPERTY:
1329 EmitKeyedPropertyLoad(prop);
1330 __ push(result_register());
1331 break;
1332 }
1333 location_ = saved_location;
1334 }
1335
1336 // Evaluate RHS expression.
1337 Expression* rhs = expr->value();
1338 VisitForValue(rhs, kAccumulator);
1339
1340 // If we have a compound assignment: Apply operator.
1341 if (expr->is_compound()) {
1342 Location saved_location = location_;
1343 location_ = kAccumulator;
1344 EmitBinaryOp(expr->binary_op(), Expression::kValue);
1345 location_ = saved_location;
1346 }
1347
1348 // Record source position before possible IC call.
1349 SetSourcePosition(expr->position());
1350
1351 // Store the value.
1352 switch (assign_type) {
1353 case VARIABLE:
1354 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001355 expr->op(),
ager@chromium.org5c838252010-02-19 08:53:10 +00001356 context_);
1357 break;
1358 case NAMED_PROPERTY:
1359 EmitNamedPropertyAssignment(expr);
1360 break;
1361 case KEYED_PROPERTY:
1362 EmitKeyedPropertyAssignment(expr);
1363 break;
1364 }
1365}
1366
1367
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001368void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1369 SetSourcePosition(prop->position());
1370 Literal* key = prop->key()->AsLiteral();
1371 __ mov(r2, Operand(key->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001372 // Call load IC. It has arguments receiver and property name r0 and r2.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001373 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1374 __ Call(ic, RelocInfo::CODE_TARGET);
1375}
1376
1377
1378void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1379 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001380 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001381 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1382 __ Call(ic, RelocInfo::CODE_TARGET);
1383}
1384
1385
1386void FullCodeGenerator::EmitBinaryOp(Token::Value op,
1387 Expression::Context context) {
1388 __ pop(r1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001389 GenericBinaryOpStub stub(op, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001390 __ CallStub(&stub);
1391 Apply(context, r0);
1392}
1393
1394
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001395void FullCodeGenerator::EmitAssignment(Expression* expr) {
1396 // Invalid left-hand sides are rewritten to have a 'throw
1397 // ReferenceError' on the left-hand side.
1398 if (!expr->IsValidLeftHandSide()) {
1399 VisitForEffect(expr);
1400 return;
1401 }
1402
1403 // Left-hand side can only be a property, a global or a (parameter or local)
1404 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1405 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1406 LhsKind assign_type = VARIABLE;
1407 Property* prop = expr->AsProperty();
1408 if (prop != NULL) {
1409 assign_type = (prop->key()->IsPropertyName())
1410 ? NAMED_PROPERTY
1411 : KEYED_PROPERTY;
1412 }
1413
1414 switch (assign_type) {
1415 case VARIABLE: {
1416 Variable* var = expr->AsVariableProxy()->var();
1417 EmitVariableAssignment(var, Token::ASSIGN, Expression::kEffect);
1418 break;
1419 }
1420 case NAMED_PROPERTY: {
1421 __ push(r0); // Preserve value.
1422 VisitForValue(prop->obj(), kAccumulator);
1423 __ mov(r1, r0);
1424 __ pop(r0); // Restore value.
1425 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
1426 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1427 __ Call(ic, RelocInfo::CODE_TARGET);
1428 break;
1429 }
1430 case KEYED_PROPERTY: {
1431 __ push(r0); // Preserve value.
1432 VisitForValue(prop->obj(), kStack);
1433 VisitForValue(prop->key(), kAccumulator);
1434 __ mov(r1, r0);
1435 __ pop(r2);
1436 __ pop(r0); // Restore value.
1437 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1438 __ Call(ic, RelocInfo::CODE_TARGET);
1439 break;
1440 }
1441 }
1442}
1443
1444
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001445void FullCodeGenerator::EmitVariableAssignment(Variable* var,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001446 Token::Value op,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001447 Expression::Context context) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001448 // Left-hand sides that rewrite to explicit property accesses do not reach
1449 // here.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001450 ASSERT(var != NULL);
1451 ASSERT(var->is_global() || var->slot() != NULL);
1452
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001453 if (var->is_global()) {
1454 ASSERT(!var->is_this());
1455 // Assignment to a global variable. Use inline caching for the
1456 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001457 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001458 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001459 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001460 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1461 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001462
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001463 } else if (var->mode() != Variable::CONST || op == Token::INIT_CONST) {
1464 // Perform the assignment for non-const variables and for initialization
1465 // of const variables. Const assignments are simply skipped.
1466 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001467 Slot* slot = var->slot();
1468 switch (slot->type()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001469 case Slot::PARAMETER:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001470 case Slot::LOCAL:
1471 if (op == Token::INIT_CONST) {
1472 // Detect const reinitialization by checking for the hole value.
1473 __ ldr(r1, MemOperand(fp, SlotOffset(slot)));
1474 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1475 __ cmp(r1, ip);
1476 __ b(ne, &done);
1477 }
1478 // Perform the assignment.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001479 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1480 break;
1481
1482 case Slot::CONTEXT: {
1483 MemOperand target = EmitSlotSearch(slot, r1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001484 if (op == Token::INIT_CONST) {
1485 // Detect const reinitialization by checking for the hole value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001486 __ ldr(r2, target);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001487 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001488 __ cmp(r2, ip);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001489 __ b(ne, &done);
1490 }
1491 // Perform the assignment and issue the write barrier.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001492 __ str(result_register(), target);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001493 // RecordWrite may destroy all its register arguments.
1494 __ mov(r3, result_register());
1495 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001496 __ mov(r2, Operand(offset));
1497 __ RecordWrite(r1, r2, r3);
1498 break;
1499 }
1500
1501 case Slot::LOOKUP:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001502 // Call the runtime for the assignment. The runtime will ignore
1503 // const reinitialization.
1504 __ push(r0); // Value.
1505 __ mov(r0, Operand(slot->var()->name()));
1506 __ Push(cp, r0); // Context and name.
1507 if (op == Token::INIT_CONST) {
1508 // The runtime will ignore const redeclaration.
1509 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1510 } else {
1511 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1512 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001513 break;
1514 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001515 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001516 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001517
ager@chromium.org5c838252010-02-19 08:53:10 +00001518 Apply(context, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001519}
1520
1521
1522void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1523 // Assignment to a property, using a named store IC.
1524 Property* prop = expr->target()->AsProperty();
1525 ASSERT(prop != NULL);
1526 ASSERT(prop->key()->AsLiteral() != NULL);
1527
1528 // If the assignment starts a block of assignments to the same object,
1529 // change to slow case to avoid the quadratic behavior of repeatedly
1530 // adding fast properties.
1531 if (expr->starts_initialization_block()) {
1532 __ push(result_register());
1533 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1534 __ push(ip);
1535 __ CallRuntime(Runtime::kToSlowProperties, 1);
1536 __ pop(result_register());
1537 }
1538
1539 // Record source code position before IC call.
1540 SetSourcePosition(expr->position());
1541 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001542 // Load receiver to r1. Leave a copy in the stack if needed for turning the
1543 // receiver into fast case.
ager@chromium.org5c838252010-02-19 08:53:10 +00001544 if (expr->ends_initialization_block()) {
1545 __ ldr(r1, MemOperand(sp));
1546 } else {
1547 __ pop(r1);
1548 }
1549
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001550 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1551 __ Call(ic, RelocInfo::CODE_TARGET);
1552
1553 // If the assignment ends an initialization block, revert to fast case.
1554 if (expr->ends_initialization_block()) {
1555 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001556 // Receiver is under the result value.
1557 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001558 __ push(ip);
1559 __ CallRuntime(Runtime::kToFastProperties, 1);
1560 __ pop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001561 DropAndApply(1, context_, r0);
1562 } else {
1563 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001564 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001565}
1566
1567
1568void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1569 // Assignment to a property, using a keyed store IC.
1570
1571 // If the assignment starts a block of assignments to the same object,
1572 // change to slow case to avoid the quadratic behavior of repeatedly
1573 // adding fast properties.
1574 if (expr->starts_initialization_block()) {
1575 __ push(result_register());
1576 // Receiver is now under the key and value.
1577 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1578 __ push(ip);
1579 __ CallRuntime(Runtime::kToSlowProperties, 1);
1580 __ pop(result_register());
1581 }
1582
1583 // Record source code position before IC call.
1584 SetSourcePosition(expr->position());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001585 __ pop(r1); // Key.
1586 // Load receiver to r2. Leave a copy in the stack if needed for turning the
1587 // receiver into fast case.
1588 if (expr->ends_initialization_block()) {
1589 __ ldr(r2, MemOperand(sp));
1590 } else {
1591 __ pop(r2);
1592 }
1593
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001594 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1595 __ Call(ic, RelocInfo::CODE_TARGET);
1596
1597 // If the assignment ends an initialization block, revert to fast case.
1598 if (expr->ends_initialization_block()) {
1599 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001600 // Receiver is under the result value.
1601 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001602 __ push(ip);
1603 __ CallRuntime(Runtime::kToFastProperties, 1);
1604 __ pop(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001605 DropAndApply(1, context_, r0);
1606 } else {
1607 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001608 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001609}
1610
1611
1612void FullCodeGenerator::VisitProperty(Property* expr) {
1613 Comment cmnt(masm_, "[ Property");
1614 Expression* key = expr->key();
1615
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001616 if (key->IsPropertyName()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001617 VisitForValue(expr->obj(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001618 EmitNamedPropertyLoad(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001619 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001620 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001621 VisitForValue(expr->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001622 VisitForValue(expr->key(), kAccumulator);
1623 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001624 EmitKeyedPropertyLoad(expr);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001625 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001626 }
1627}
1628
1629void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001630 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001631 RelocInfo::Mode mode) {
1632 // Code common for calls using the IC.
1633 ZoneList<Expression*>* args = expr->arguments();
1634 int arg_count = args->length();
1635 for (int i = 0; i < arg_count; i++) {
1636 VisitForValue(args->at(i), kStack);
1637 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001638 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001639 // Record source position for debugger.
1640 SetSourcePosition(expr->position());
1641 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001642 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1643 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001644 __ Call(ic, mode);
1645 // Restore context register.
1646 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001647 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001648}
1649
1650
1651void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1652 // Code common for calls using the call stub.
1653 ZoneList<Expression*>* args = expr->arguments();
1654 int arg_count = args->length();
1655 for (int i = 0; i < arg_count; i++) {
1656 VisitForValue(args->at(i), kStack);
1657 }
1658 // Record source position for debugger.
1659 SetSourcePosition(expr->position());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001660 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1661 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001662 __ CallStub(&stub);
1663 // Restore context register.
1664 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001665 DropAndApply(1, context_, r0);
1666}
1667
1668
1669void FullCodeGenerator::VisitCall(Call* expr) {
1670 Comment cmnt(masm_, "[ Call");
1671 Expression* fun = expr->expression();
1672 Variable* var = fun->AsVariableProxy()->AsVariable();
1673
1674 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001675 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1676 // resolve the function we need to call and the receiver of the
1677 // call. Then we call the resolved function using the given
1678 // arguments.
1679 VisitForValue(fun, kStack);
1680 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1681 __ push(r2); // Reserved receiver slot.
1682
1683 // Push the arguments.
1684 ZoneList<Expression*>* args = expr->arguments();
1685 int arg_count = args->length();
1686 for (int i = 0; i < arg_count; i++) {
1687 VisitForValue(args->at(i), kStack);
1688 }
1689
1690 // Push copy of the function - found below the arguments.
1691 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1692 __ push(r1);
1693
1694 // Push copy of the first argument or undefined if it doesn't exist.
1695 if (arg_count > 0) {
1696 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1697 __ push(r1);
1698 } else {
1699 __ push(r2);
1700 }
1701
1702 // Push the receiver of the enclosing function and do runtime call.
1703 __ ldr(r1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1704 __ push(r1);
1705 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1706
1707 // The runtime call returns a pair of values in r0 (function) and
1708 // r1 (receiver). Touch up the stack with the right values.
1709 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1710 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
1711
1712 // Record source position for debugger.
1713 SetSourcePosition(expr->position());
1714 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1715 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1716 __ CallStub(&stub);
1717 // Restore context register.
1718 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1719 DropAndApply(1, context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001720 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001721 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001722 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001723 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001724 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1725 } else if (var != NULL && var->slot() != NULL &&
1726 var->slot()->type() == Slot::LOOKUP) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001727 // Call to a lookup slot (dynamically introduced variable). Call the
1728 // runtime to find the function to call (returned in eax) and the object
1729 // holding it (returned in edx).
1730 __ push(context_register());
1731 __ mov(r2, Operand(var->name()));
1732 __ push(r2);
1733 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1734 __ push(r0); // Function.
1735 __ push(r1); // Receiver.
1736 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001737 } else if (fun->AsProperty() != NULL) {
1738 // Call to an object property.
1739 Property* prop = fun->AsProperty();
1740 Literal* key = prop->key()->AsLiteral();
1741 if (key != NULL && key->handle()->IsSymbol()) {
1742 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001743 VisitForValue(prop->obj(), kStack);
1744 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1745 } else {
1746 // Call to a keyed property, use keyed load IC followed by function
1747 // call.
1748 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001749 VisitForValue(prop->key(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001750 // Record source code position for IC call.
1751 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001752 if (prop->is_synthetic()) {
1753 __ pop(r1); // We do not need to keep the receiver.
1754 } else {
1755 __ ldr(r1, MemOperand(sp, 0)); // Keep receiver, to call function on.
1756 }
1757
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001758 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1759 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001760 if (prop->is_synthetic()) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001761 // Push result (function).
1762 __ push(r0);
1763 // Push Global receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001764 __ ldr(r1, CodeGenerator::GlobalObject());
1765 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001766 __ push(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001767 } else {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001768 // Pop receiver.
1769 __ pop(r1);
1770 // Push result (function).
1771 __ push(r0);
1772 __ push(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001773 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001774 EmitCallWithStub(expr);
1775 }
1776 } else {
1777 // Call to some other expression. If the expression is an anonymous
1778 // function literal not called in a loop, mark it as one that should
1779 // also use the fast code generator.
1780 FunctionLiteral* lit = fun->AsFunctionLiteral();
1781 if (lit != NULL &&
1782 lit->name()->Equals(Heap::empty_string()) &&
1783 loop_depth() == 0) {
1784 lit->set_try_full_codegen(true);
1785 }
1786 VisitForValue(fun, kStack);
1787 // Load global receiver object.
1788 __ ldr(r1, CodeGenerator::GlobalObject());
1789 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1790 __ push(r1);
1791 // Emit function call.
1792 EmitCallWithStub(expr);
1793 }
1794}
1795
1796
1797void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1798 Comment cmnt(masm_, "[ CallNew");
1799 // According to ECMA-262, section 11.2.2, page 44, the function
1800 // expression in new calls must be evaluated before the
1801 // arguments.
1802 // Push function on the stack.
1803 VisitForValue(expr->expression(), kStack);
1804
1805 // Push global object (receiver).
1806 __ ldr(r0, CodeGenerator::GlobalObject());
1807 __ push(r0);
1808 // Push the arguments ("left-to-right") on the stack.
1809 ZoneList<Expression*>* args = expr->arguments();
1810 int arg_count = args->length();
1811 for (int i = 0; i < arg_count; i++) {
1812 VisitForValue(args->at(i), kStack);
1813 }
1814
1815 // Call the construct call builtin that handles allocation and
1816 // constructor invocation.
1817 SetSourcePosition(expr->position());
1818
1819 // Load function, arg_count into r1 and r0.
1820 __ mov(r0, Operand(arg_count));
1821 // Function is in sp[arg_count + 1].
1822 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1823
1824 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1825 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
1826
1827 // Replace function on TOS with result in r0, or pop it.
1828 DropAndApply(1, context_, r0);
1829}
1830
1831
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001832void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
1833 Handle<String> name = expr->name();
1834 if (strcmp("_IsSmi", *name->ToCString()) == 0) {
1835 EmitIsSmi(expr->arguments());
1836 } else if (strcmp("_IsNonNegativeSmi", *name->ToCString()) == 0) {
1837 EmitIsNonNegativeSmi(expr->arguments());
1838 } else if (strcmp("_IsObject", *name->ToCString()) == 0) {
1839 EmitIsObject(expr->arguments());
1840 } else if (strcmp("_IsUndetectableObject", *name->ToCString()) == 0) {
1841 EmitIsUndetectableObject(expr->arguments());
1842 } else if (strcmp("_IsFunction", *name->ToCString()) == 0) {
1843 EmitIsFunction(expr->arguments());
1844 } else if (strcmp("_IsArray", *name->ToCString()) == 0) {
1845 EmitIsArray(expr->arguments());
1846 } else if (strcmp("_IsRegExp", *name->ToCString()) == 0) {
1847 EmitIsRegExp(expr->arguments());
1848 } else if (strcmp("_IsConstructCall", *name->ToCString()) == 0) {
1849 EmitIsConstructCall(expr->arguments());
1850 } else if (strcmp("_ObjectEquals", *name->ToCString()) == 0) {
1851 EmitObjectEquals(expr->arguments());
1852 } else if (strcmp("_Arguments", *name->ToCString()) == 0) {
1853 EmitArguments(expr->arguments());
1854 } else if (strcmp("_ArgumentsLength", *name->ToCString()) == 0) {
1855 EmitArgumentsLength(expr->arguments());
1856 } else if (strcmp("_ClassOf", *name->ToCString()) == 0) {
1857 EmitClassOf(expr->arguments());
1858 } else if (strcmp("_Log", *name->ToCString()) == 0) {
1859 EmitLog(expr->arguments());
1860 } else if (strcmp("_RandomHeapNumber", *name->ToCString()) == 0) {
1861 EmitRandomHeapNumber(expr->arguments());
1862 } else if (strcmp("_SubString", *name->ToCString()) == 0) {
1863 EmitSubString(expr->arguments());
1864 } else if (strcmp("_RegExpExec", *name->ToCString()) == 0) {
1865 EmitRegExpExec(expr->arguments());
1866 } else if (strcmp("_ValueOf", *name->ToCString()) == 0) {
1867 EmitValueOf(expr->arguments());
1868 } else if (strcmp("_SetValueOf", *name->ToCString()) == 0) {
1869 EmitSetValueOf(expr->arguments());
1870 } else if (strcmp("_NumberToString", *name->ToCString()) == 0) {
1871 EmitNumberToString(expr->arguments());
1872 } else if (strcmp("_CharFromCode", *name->ToCString()) == 0) {
1873 EmitCharFromCode(expr->arguments());
1874 } else if (strcmp("_FastCharCodeAt", *name->ToCString()) == 0) {
1875 EmitFastCharCodeAt(expr->arguments());
1876 } else if (strcmp("_StringAdd", *name->ToCString()) == 0) {
1877 EmitStringAdd(expr->arguments());
1878 } else if (strcmp("_StringCompare", *name->ToCString()) == 0) {
1879 EmitStringCompare(expr->arguments());
1880 } else if (strcmp("_MathPow", *name->ToCString()) == 0) {
1881 EmitMathPow(expr->arguments());
1882 } else if (strcmp("_MathSin", *name->ToCString()) == 0) {
1883 EmitMathSin(expr->arguments());
1884 } else if (strcmp("_MathCos", *name->ToCString()) == 0) {
1885 EmitMathCos(expr->arguments());
1886 } else if (strcmp("_MathSqrt", *name->ToCString()) == 0) {
1887 EmitMathSqrt(expr->arguments());
1888 } else if (strcmp("_CallFunction", *name->ToCString()) == 0) {
1889 EmitCallFunction(expr->arguments());
1890 } else if (strcmp("_RegExpConstructResult", *name->ToCString()) == 0) {
1891 EmitRegExpConstructResult(expr->arguments());
1892 } else if (strcmp("_SwapElements", *name->ToCString()) == 0) {
1893 EmitSwapElements(expr->arguments());
1894 } else if (strcmp("_GetFromCache", *name->ToCString()) == 0) {
1895 EmitGetFromCache(expr->arguments());
1896 } else {
1897 UNREACHABLE();
1898 }
1899}
1900
1901
1902void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1903 ASSERT(args->length() == 1);
1904
1905 VisitForValue(args->at(0), kAccumulator);
1906
1907 Label materialize_true, materialize_false;
1908 Label* if_true = NULL;
1909 Label* if_false = NULL;
1910 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1911
1912 __ BranchOnSmi(r0, if_true);
1913 __ b(if_false);
1914
1915 Apply(context_, if_true, if_false);
1916}
1917
1918
1919void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1920 ASSERT(args->length() == 1);
1921
1922 VisitForValue(args->at(0), kAccumulator);
1923
1924 Label materialize_true, materialize_false;
1925 Label* if_true = NULL;
1926 Label* if_false = NULL;
1927 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1928
1929 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
1930 __ b(eq, if_true);
1931 __ b(if_false);
1932
1933 Apply(context_, if_true, if_false);
1934}
1935
1936
1937void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1938 ASSERT(args->length() == 1);
1939
1940 VisitForValue(args->at(0), kAccumulator);
1941
1942 Label materialize_true, materialize_false;
1943 Label* if_true = NULL;
1944 Label* if_false = NULL;
1945 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1946 __ BranchOnSmi(r0, if_false);
1947 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1948 __ cmp(r0, ip);
1949 __ b(eq, if_true);
1950 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1951 // Undetectable objects behave like undefined when tested with typeof.
1952 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
1953 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1954 __ b(ne, if_false);
1955 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1956 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
1957 __ b(lt, if_false);
1958 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
1959 __ b(le, if_true);
1960 __ b(if_false);
1961
1962 Apply(context_, if_true, if_false);
1963}
1964
1965
1966void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
1967 ASSERT(args->length() == 1);
1968
1969 VisitForValue(args->at(0), kAccumulator);
1970
1971 Label materialize_true, materialize_false;
1972 Label* if_true = NULL;
1973 Label* if_false = NULL;
1974 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1975
1976 __ BranchOnSmi(r0, if_false);
1977 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1978 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
1979 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1980 __ b(ne, if_true);
1981 __ b(if_false);
1982
1983 Apply(context_, if_true, if_false);
1984}
1985
1986
1987void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
1988 ASSERT(args->length() == 1);
1989
1990 VisitForValue(args->at(0), kAccumulator);
1991
1992 Label materialize_true, materialize_false;
1993 Label* if_true = NULL;
1994 Label* if_false = NULL;
1995 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1996
1997 __ BranchOnSmi(r0, if_false);
1998 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
1999 __ b(eq, if_true);
2000 __ b(if_false);
2001
2002 Apply(context_, if_true, if_false);
2003}
2004
2005
2006void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
2007 ASSERT(args->length() == 1);
2008
2009 VisitForValue(args->at(0), kAccumulator);
2010
2011 Label materialize_true, materialize_false;
2012 Label* if_true = NULL;
2013 Label* if_false = NULL;
2014 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2015
2016 __ BranchOnSmi(r0, if_false);
2017 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
2018 __ b(eq, if_true);
2019 __ b(if_false);
2020
2021 Apply(context_, if_true, if_false);
2022}
2023
2024
2025void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
2026 ASSERT(args->length() == 1);
2027
2028 VisitForValue(args->at(0), kAccumulator);
2029
2030 Label materialize_true, materialize_false;
2031 Label* if_true = NULL;
2032 Label* if_false = NULL;
2033 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2034
2035 __ BranchOnSmi(r0, if_false);
2036 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
2037 __ b(eq, if_true);
2038 __ b(if_false);
2039
2040 Apply(context_, if_true, if_false);
2041}
2042
2043
2044
2045void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
2046 ASSERT(args->length() == 0);
2047
2048 Label materialize_true, materialize_false;
2049 Label* if_true = NULL;
2050 Label* if_false = NULL;
2051 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2052
2053 // Get the frame pointer for the calling frame.
2054 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2055
2056 // Skip the arguments adaptor frame if it exists.
2057 Label check_frame_marker;
2058 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
2059 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2060 __ b(ne, &check_frame_marker);
2061 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
2062
2063 // Check the marker in the calling frame.
2064 __ bind(&check_frame_marker);
2065 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
2066 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
2067 __ b(eq, if_true);
2068 __ b(if_false);
2069
2070 Apply(context_, if_true, if_false);
2071}
2072
2073
2074void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
2075 ASSERT(args->length() == 2);
2076
2077 // Load the two objects into registers and perform the comparison.
2078 VisitForValue(args->at(0), kStack);
2079 VisitForValue(args->at(1), kAccumulator);
2080
2081 Label materialize_true, materialize_false;
2082 Label* if_true = NULL;
2083 Label* if_false = NULL;
2084 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2085
2086 __ pop(r1);
2087 __ cmp(r0, r1);
2088 __ b(eq, if_true);
2089 __ b(if_false);
2090
2091 Apply(context_, if_true, if_false);
2092}
2093
2094
2095void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
2096 ASSERT(args->length() == 1);
2097
2098 // ArgumentsAccessStub expects the key in edx and the formal
2099 // parameter count in eax.
2100 VisitForValue(args->at(0), kAccumulator);
2101 __ mov(r1, r0);
2102 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2103 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2104 __ CallStub(&stub);
2105 Apply(context_, r0);
2106}
2107
2108
2109void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
2110 ASSERT(args->length() == 0);
2111
2112 Label exit;
2113 // Get the number of formal parameters.
2114 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2115
2116 // Check if the calling frame is an arguments adaptor frame.
2117 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2118 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
2119 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2120 __ b(ne, &exit);
2121
2122 // Arguments adaptor case: Read the arguments length from the
2123 // adaptor frame.
2124 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2125
2126 __ bind(&exit);
2127 Apply(context_, r0);
2128}
2129
2130
2131void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
2132 ASSERT(args->length() == 1);
2133 Label done, null, function, non_function_constructor;
2134
2135 VisitForValue(args->at(0), kAccumulator);
2136
2137 // If the object is a smi, we return null.
2138 __ BranchOnSmi(r0, &null);
2139
2140 // Check that the object is a JS object but take special care of JS
2141 // functions to make sure they have 'Function' as their class.
2142 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2143 __ b(lt, &null);
2144
2145 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2146 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2147 // LAST_JS_OBJECT_TYPE.
2148 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2149 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2150 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2151 __ b(eq, &function);
2152
2153 // Check if the constructor in the map is a function.
2154 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2155 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2156 __ b(ne, &non_function_constructor);
2157
2158 // r0 now contains the constructor function. Grab the
2159 // instance class name from there.
2160 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2161 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2162 __ b(&done);
2163
2164 // Functions have class 'Function'.
2165 __ bind(&function);
2166 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2167 __ jmp(&done);
2168
2169 // Objects with a non-function constructor have class 'Object'.
2170 __ bind(&non_function_constructor);
2171 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2172 __ jmp(&done);
2173
2174 // Non-JS objects have class null.
2175 __ bind(&null);
2176 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2177
2178 // All done.
2179 __ bind(&done);
2180
2181 Apply(context_, r0);
2182}
2183
2184
2185void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2186 // Conditionally generate a log call.
2187 // Args:
2188 // 0 (literal string): The type of logging (corresponds to the flags).
2189 // This is used to determine whether or not to generate the log call.
2190 // 1 (string): Format string. Access the string at argument index 2
2191 // with '%2s' (see Logger::LogRuntime for all the formats).
2192 // 2 (array): Arguments to the format string.
2193 ASSERT_EQ(args->length(), 3);
2194#ifdef ENABLE_LOGGING_AND_PROFILING
2195 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2196 VisitForValue(args->at(1), kStack);
2197 VisitForValue(args->at(2), kStack);
2198 __ CallRuntime(Runtime::kLog, 2);
2199 }
2200#endif
2201 // Finally, we're expected to leave a value on the top of the stack.
2202 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2203 Apply(context_, r0);
2204}
2205
2206
2207void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2208 ASSERT(args->length() == 0);
2209
2210 Label slow_allocate_heapnumber;
2211 Label heapnumber_allocated;
2212
2213 __ AllocateHeapNumber(r4, r1, r2, &slow_allocate_heapnumber);
2214 __ jmp(&heapnumber_allocated);
2215
2216 __ bind(&slow_allocate_heapnumber);
2217 // To allocate a heap number, and ensure that it is not a smi, we
2218 // call the runtime function FUnaryMinus on 0, returning the double
2219 // -0.0. A new, distinct heap number is returned each time.
2220 __ mov(r0, Operand(Smi::FromInt(0)));
2221 __ push(r0);
2222 __ CallRuntime(Runtime::kNumberUnaryMinus, 1);
2223 __ mov(r4, Operand(r0));
2224
2225 __ bind(&heapnumber_allocated);
2226
2227 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
2228 // by computing:
2229 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2230 if (CpuFeatures::IsSupported(VFP3)) {
2231 __ PrepareCallCFunction(0, r1);
2232 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
2233
2234 CpuFeatures::Scope scope(VFP3);
2235 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2236 // Create this constant using mov/orr to avoid PC relative load.
2237 __ mov(r1, Operand(0x41000000));
2238 __ orr(r1, r1, Operand(0x300000));
2239 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
2240 __ vmov(d7, r0, r1);
2241 // Move 0x4130000000000000 to VFP.
2242 __ mov(r0, Operand(0));
2243 __ vmov(d8, r0, r1);
2244 // Subtract and store the result in the heap number.
2245 __ vsub(d7, d7, d8);
2246 __ sub(r0, r4, Operand(kHeapObjectTag));
2247 __ vstr(d7, r0, HeapNumber::kValueOffset);
2248 __ mov(r0, r4);
2249 } else {
2250 __ mov(r0, Operand(r4));
2251 __ PrepareCallCFunction(1, r1);
2252 __ CallCFunction(
2253 ExternalReference::fill_heap_number_with_random_function(), 1);
2254 }
2255
2256 Apply(context_, r0);
2257}
2258
2259
2260void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
2261 // Load the arguments on the stack and call the stub.
2262 SubStringStub stub;
2263 ASSERT(args->length() == 3);
2264 VisitForValue(args->at(0), kStack);
2265 VisitForValue(args->at(1), kStack);
2266 VisitForValue(args->at(2), kStack);
2267 __ CallStub(&stub);
2268 Apply(context_, r0);
2269}
2270
2271
2272void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
2273 // Load the arguments on the stack and call the stub.
2274 RegExpExecStub stub;
2275 ASSERT(args->length() == 4);
2276 VisitForValue(args->at(0), kStack);
2277 VisitForValue(args->at(1), kStack);
2278 VisitForValue(args->at(2), kStack);
2279 VisitForValue(args->at(3), kStack);
2280 __ CallStub(&stub);
2281 Apply(context_, r0);
2282}
2283
2284
2285void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
2286 ASSERT(args->length() == 1);
2287
2288 VisitForValue(args->at(0), kAccumulator); // Load the object.
2289
2290 Label done;
2291 // If the object is a smi return the object.
2292 __ BranchOnSmi(r0, &done);
2293 // If the object is not a value type, return the object.
2294 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
2295 __ b(ne, &done);
2296 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2297
2298 __ bind(&done);
2299 Apply(context_, r0);
2300}
2301
2302
2303void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
2304 // Load the arguments on the stack and call the runtime function.
2305 ASSERT(args->length() == 2);
2306 VisitForValue(args->at(0), kStack);
2307 VisitForValue(args->at(1), kStack);
2308 __ CallRuntime(Runtime::kMath_pow, 2);
2309 Apply(context_, r0);
2310}
2311
2312
2313void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
2314 ASSERT(args->length() == 2);
2315
2316 VisitForValue(args->at(0), kStack); // Load the object.
2317 VisitForValue(args->at(1), kAccumulator); // Load the value.
2318 __ pop(r1); // r0 = value. r1 = object.
2319
2320 Label done;
2321 // If the object is a smi, return the value.
2322 __ BranchOnSmi(r1, &done);
2323
2324 // If the object is not a value type, return the value.
2325 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
2326 __ b(ne, &done);
2327
2328 // Store the value.
2329 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2330 // Update the write barrier. Save the value as it will be
2331 // overwritten by the write barrier code and is needed afterward.
2332 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
2333 __ RecordWrite(r1, r2, r3);
2334
2335 __ bind(&done);
2336 Apply(context_, r0);
2337}
2338
2339
2340void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2341 ASSERT_EQ(args->length(), 1);
2342
2343 // Load the argument on the stack and call the stub.
2344 VisitForValue(args->at(0), kStack);
2345
2346 NumberToStringStub stub;
2347 __ CallStub(&stub);
2348 Apply(context_, r0);
2349}
2350
2351
2352void FullCodeGenerator::EmitCharFromCode(ZoneList<Expression*>* args) {
2353 ASSERT(args->length() == 1);
2354
2355 VisitForValue(args->at(0), kAccumulator);
2356
2357 Label slow_case, done;
2358 // Fast case of Heap::LookupSingleCharacterStringFromCode.
2359 ASSERT(kSmiTag == 0);
2360 ASSERT(kSmiShiftSize == 0);
2361 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
2362 __ tst(r0, Operand(kSmiTagMask |
2363 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
2364 __ b(nz, &slow_case);
2365 __ mov(r1, Operand(Factory::single_character_string_cache()));
2366 ASSERT(kSmiTag == 0);
2367 ASSERT(kSmiTagSize == 1);
2368 ASSERT(kSmiShiftSize == 0);
2369 // At this point code register contains smi tagged ascii char code.
2370 __ add(r1, r1, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2371 __ ldr(r1, MemOperand(r1, FixedArray::kHeaderSize - kHeapObjectTag));
2372 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
2373 __ cmp(r1, r2);
2374 __ b(eq, &slow_case);
2375 __ mov(r0, r1);
2376 __ b(&done);
2377
2378 __ bind(&slow_case);
2379 __ push(r0);
2380 __ CallRuntime(Runtime::kCharFromCode, 1);
2381
2382 __ bind(&done);
2383 Apply(context_, r0);
2384}
2385
2386
2387void FullCodeGenerator::EmitFastCharCodeAt(ZoneList<Expression*>* args) {
2388 // TODO(fsc): Port the complete implementation from the classic back-end.
2389 // Move the undefined value into the result register, which will
2390 // trigger the slow case.
2391 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2392 Apply(context_, r0);
2393}
2394
2395void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2396 ASSERT_EQ(2, args->length());
2397
2398 VisitForValue(args->at(0), kStack);
2399 VisitForValue(args->at(1), kStack);
2400
2401 StringAddStub stub(NO_STRING_ADD_FLAGS);
2402 __ CallStub(&stub);
2403 Apply(context_, r0);
2404}
2405
2406
2407void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
2408 ASSERT_EQ(2, args->length());
2409
2410 VisitForValue(args->at(0), kStack);
2411 VisitForValue(args->at(1), kStack);
2412
2413 StringCompareStub stub;
2414 __ CallStub(&stub);
2415 Apply(context_, r0);
2416}
2417
2418
2419void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
2420 // Load the argument on the stack and call the runtime.
2421 ASSERT(args->length() == 1);
2422 VisitForValue(args->at(0), kStack);
2423 __ CallRuntime(Runtime::kMath_sin, 1);
2424 Apply(context_, r0);
2425}
2426
2427
2428void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
2429 // Load the argument on the stack and call the runtime.
2430 ASSERT(args->length() == 1);
2431 VisitForValue(args->at(0), kStack);
2432 __ CallRuntime(Runtime::kMath_cos, 1);
2433 Apply(context_, r0);
2434}
2435
2436
2437void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2438 // Load the argument on the stack and call the runtime function.
2439 ASSERT(args->length() == 1);
2440 VisitForValue(args->at(0), kStack);
2441 __ CallRuntime(Runtime::kMath_sqrt, 1);
2442 Apply(context_, r0);
2443}
2444
2445
2446void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2447 ASSERT(args->length() >= 2);
2448
2449 int arg_count = args->length() - 2; // For receiver and function.
2450 VisitForValue(args->at(0), kStack); // Receiver.
2451 for (int i = 0; i < arg_count; i++) {
2452 VisitForValue(args->at(i + 1), kStack);
2453 }
2454 VisitForValue(args->at(arg_count + 1), kAccumulator); // Function.
2455
2456 // InvokeFunction requires function in r1. Move it in there.
2457 if (!result_register().is(r1)) __ mov(r1, result_register());
2458 ParameterCount count(arg_count);
2459 __ InvokeFunction(r1, count, CALL_FUNCTION);
2460 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2461 Apply(context_, r0);
2462}
2463
2464
2465void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2466 ASSERT(args->length() == 3);
2467 VisitForValue(args->at(0), kStack);
2468 VisitForValue(args->at(1), kStack);
2469 VisitForValue(args->at(2), kStack);
2470 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
2471 Apply(context_, r0);
2472}
2473
2474
2475void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2476 ASSERT(args->length() == 3);
2477 VisitForValue(args->at(0), kStack);
2478 VisitForValue(args->at(1), kStack);
2479 VisitForValue(args->at(2), kStack);
2480 __ CallRuntime(Runtime::kSwapElements, 3);
2481 Apply(context_, r0);
2482}
2483
2484
2485void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2486 ASSERT_EQ(2, args->length());
2487
2488 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2489 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2490
2491 Handle<FixedArray> jsfunction_result_caches(
2492 Top::global_context()->jsfunction_result_caches());
2493 if (jsfunction_result_caches->length() <= cache_id) {
2494 __ Abort("Attempt to use undefined cache.");
2495 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2496 Apply(context_, r0);
2497 return;
2498 }
2499
2500 VisitForValue(args->at(1), kAccumulator);
2501
2502 Register key = r0;
2503 Register cache = r1;
2504 __ ldr(cache, CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX));
2505 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
2506 __ ldr(cache,
2507 CodeGenerator::ContextOperand(
2508 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
2509 __ ldr(cache,
2510 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2511
2512
2513 Label done, not_found;
2514 // tmp now holds finger offset as a smi.
2515 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2516 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2517 // r2 now holds finger offset as a smi.
2518 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2519 // r3 now points to the start of fixed array elements.
2520 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2521 // Note side effect of PreIndex: r3 now points to the key of the pair.
2522 __ cmp(key, r2);
2523 __ b(ne, &not_found);
2524
2525 __ ldr(r0, MemOperand(r3, kPointerSize));
2526 __ b(&done);
2527
2528 __ bind(&not_found);
2529 // Call runtime to perform the lookup.
2530 __ Push(cache, key);
2531 __ CallRuntime(Runtime::kGetFromCache, 2);
2532
2533 __ bind(&done);
2534 Apply(context_, r0);
2535}
2536
2537
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002538void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002539 Handle<String> name = expr->name();
2540 if (name->length() > 0 && name->Get(0) == '_') {
2541 Comment cmnt(masm_, "[ InlineRuntimeCall");
2542 EmitInlineRuntimeCall(expr);
2543 return;
2544 }
2545
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002546 Comment cmnt(masm_, "[ CallRuntime");
2547 ZoneList<Expression*>* args = expr->arguments();
2548
2549 if (expr->is_jsruntime()) {
2550 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002551 __ ldr(r0, CodeGenerator::GlobalObject());
2552 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002553 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002554 }
2555
2556 // Push the arguments ("left-to-right").
2557 int arg_count = args->length();
2558 for (int i = 0; i < arg_count; i++) {
2559 VisitForValue(args->at(i), kStack);
2560 }
2561
2562 if (expr->is_jsruntime()) {
2563 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002564 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002565 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2566 NOT_IN_LOOP);
2567 __ Call(ic, RelocInfo::CODE_TARGET);
2568 // Restore context register.
2569 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002570 } else {
2571 // Call the C runtime function.
2572 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002573 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002574 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002575}
2576
2577
2578void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2579 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002580 case Token::DELETE: {
2581 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2582 Property* prop = expr->expression()->AsProperty();
2583 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2584 if (prop == NULL && var == NULL) {
2585 // Result of deleting non-property, non-variable reference is true.
2586 // The subexpression may have side effects.
2587 VisitForEffect(expr->expression());
2588 Apply(context_, true);
2589 } else if (var != NULL &&
2590 !var->is_global() &&
2591 var->slot() != NULL &&
2592 var->slot()->type() != Slot::LOOKUP) {
2593 // Result of deleting non-global, non-dynamic variables is false.
2594 // The subexpression does not have side effects.
2595 Apply(context_, false);
2596 } else {
2597 // Property or variable reference. Call the delete builtin with
2598 // object and property name as arguments.
2599 if (prop != NULL) {
2600 VisitForValue(prop->obj(), kStack);
2601 VisitForValue(prop->key(), kStack);
2602 } else if (var->is_global()) {
2603 __ ldr(r1, CodeGenerator::GlobalObject());
2604 __ mov(r0, Operand(var->name()));
2605 __ Push(r1, r0);
2606 } else {
2607 // Non-global variable. Call the runtime to look up the context
2608 // where the variable was introduced.
2609 __ push(context_register());
2610 __ mov(r2, Operand(var->name()));
2611 __ push(r2);
2612 __ CallRuntime(Runtime::kLookupContext, 2);
2613 __ push(r0);
2614 __ mov(r2, Operand(var->name()));
2615 __ push(r2);
2616 }
2617 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
2618 Apply(context_, r0);
2619 }
2620 break;
2621 }
2622
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002623 case Token::VOID: {
2624 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2625 VisitForEffect(expr->expression());
2626 switch (context_) {
2627 case Expression::kUninitialized:
2628 UNREACHABLE();
2629 break;
2630 case Expression::kEffect:
2631 break;
2632 case Expression::kValue:
2633 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2634 switch (location_) {
2635 case kAccumulator:
2636 break;
2637 case kStack:
2638 __ push(result_register());
2639 break;
2640 }
2641 break;
2642 case Expression::kTestValue:
2643 // Value is false so it's needed.
2644 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2645 switch (location_) {
2646 case kAccumulator:
2647 break;
2648 case kStack:
2649 __ push(result_register());
2650 break;
2651 }
2652 // Fall through.
2653 case Expression::kTest:
2654 case Expression::kValueTest:
2655 __ jmp(false_label_);
2656 break;
2657 }
2658 break;
2659 }
2660
2661 case Token::NOT: {
2662 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002663 Label materialize_true, materialize_false;
2664 Label* if_true = NULL;
2665 Label* if_false = NULL;
2666
2667 // Notice that the labels are swapped.
2668 PrepareTest(&materialize_true, &materialize_false, &if_false, &if_true);
2669
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002670 VisitForControl(expr->expression(), if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002671
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002672 Apply(context_, if_false, if_true); // Labels swapped.
2673 break;
2674 }
2675
2676 case Token::TYPEOF: {
2677 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
2678 VariableProxy* proxy = expr->expression()->AsVariableProxy();
2679 if (proxy != NULL &&
2680 !proxy->var()->is_this() &&
2681 proxy->var()->is_global()) {
2682 Comment cmnt(masm_, "Global variable");
2683 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002684 __ mov(r2, Operand(proxy->name()));
2685 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2686 // Use a regular load, not a contextual load, to avoid a reference
2687 // error.
2688 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002689 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002690 } else if (proxy != NULL &&
2691 proxy->var()->slot() != NULL &&
2692 proxy->var()->slot()->type() == Slot::LOOKUP) {
2693 __ mov(r0, Operand(proxy->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002694 __ Push(cp, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002695 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
2696 __ push(r0);
2697 } else {
2698 // This expression cannot throw a reference error at the top level.
2699 VisitForValue(expr->expression(), kStack);
2700 }
2701
2702 __ CallRuntime(Runtime::kTypeof, 1);
2703 Apply(context_, r0);
2704 break;
2705 }
2706
2707 case Token::ADD: {
2708 Comment cmt(masm_, "[ UnaryOperation (ADD)");
2709 VisitForValue(expr->expression(), kAccumulator);
2710 Label no_conversion;
2711 __ tst(result_register(), Operand(kSmiTagMask));
2712 __ b(eq, &no_conversion);
2713 __ push(r0);
2714 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2715 __ bind(&no_conversion);
2716 Apply(context_, result_register());
2717 break;
2718 }
2719
2720 case Token::SUB: {
2721 Comment cmt(masm_, "[ UnaryOperation (SUB)");
2722 bool overwrite =
2723 (expr->expression()->AsBinaryOperation() != NULL &&
2724 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
2725 GenericUnaryOpStub stub(Token::SUB, overwrite);
2726 // GenericUnaryOpStub expects the argument to be in the
2727 // accumulator register r0.
2728 VisitForValue(expr->expression(), kAccumulator);
2729 __ CallStub(&stub);
2730 Apply(context_, r0);
2731 break;
2732 }
2733
2734 case Token::BIT_NOT: {
2735 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
2736 bool overwrite =
2737 (expr->expression()->AsBinaryOperation() != NULL &&
2738 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
2739 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
2740 // GenericUnaryOpStub expects the argument to be in the
2741 // accumulator register r0.
2742 VisitForValue(expr->expression(), kAccumulator);
2743 // Avoid calling the stub for Smis.
2744 Label smi, done;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002745 __ BranchOnSmi(result_register(), &smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002746 // Non-smi: call stub leaving result in accumulator register.
2747 __ CallStub(&stub);
2748 __ b(&done);
2749 // Perform operation directly on Smis.
2750 __ bind(&smi);
2751 __ mvn(result_register(), Operand(result_register()));
2752 // Bit-clear inverted smi-tag.
2753 __ bic(result_register(), result_register(), Operand(kSmiTagMask));
2754 __ bind(&done);
2755 Apply(context_, result_register());
2756 break;
2757 }
2758
2759 default:
2760 UNREACHABLE();
2761 }
2762}
2763
2764
2765void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2766 Comment cmnt(masm_, "[ CountOperation");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002767 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2768 // as the left-hand side.
2769 if (!expr->expression()->IsValidLeftHandSide()) {
2770 VisitForEffect(expr->expression());
2771 return;
2772 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002773
2774 // Expression can only be a property, a global or a (parameter or local)
2775 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2776 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2777 LhsKind assign_type = VARIABLE;
2778 Property* prop = expr->expression()->AsProperty();
2779 // In case of a property we use the uninitialized expression context
2780 // of the key to detect a named property.
2781 if (prop != NULL) {
2782 assign_type =
2783 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2784 }
2785
2786 // Evaluate expression and get value.
2787 if (assign_type == VARIABLE) {
2788 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
2789 Location saved_location = location_;
2790 location_ = kAccumulator;
2791 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
2792 Expression::kValue);
2793 location_ = saved_location;
2794 } else {
2795 // Reserve space for result of postfix operation.
2796 if (expr->is_postfix() && context_ != Expression::kEffect) {
2797 __ mov(ip, Operand(Smi::FromInt(0)));
2798 __ push(ip);
2799 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002800 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002801 // Put the object both on the stack and in the accumulator.
2802 VisitForValue(prop->obj(), kAccumulator);
2803 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002804 EmitNamedPropertyLoad(prop);
2805 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002806 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002807 VisitForValue(prop->key(), kAccumulator);
2808 __ ldr(r1, MemOperand(sp, 0));
2809 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002810 EmitKeyedPropertyLoad(prop);
2811 }
2812 }
2813
2814 // Call ToNumber only if operand is not a smi.
2815 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002816 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002817 __ push(r0);
2818 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2819 __ bind(&no_conversion);
2820
2821 // Save result for postfix expressions.
2822 if (expr->is_postfix()) {
2823 switch (context_) {
2824 case Expression::kUninitialized:
2825 UNREACHABLE();
2826 case Expression::kEffect:
2827 // Do not save result.
2828 break;
2829 case Expression::kValue:
2830 case Expression::kTest:
2831 case Expression::kValueTest:
2832 case Expression::kTestValue:
2833 // Save the result on the stack. If we have a named or keyed property
2834 // we store the result under the receiver that is currently on top
2835 // of the stack.
2836 switch (assign_type) {
2837 case VARIABLE:
2838 __ push(r0);
2839 break;
2840 case NAMED_PROPERTY:
2841 __ str(r0, MemOperand(sp, kPointerSize));
2842 break;
2843 case KEYED_PROPERTY:
2844 __ str(r0, MemOperand(sp, 2 * kPointerSize));
2845 break;
2846 }
2847 break;
2848 }
2849 }
2850
2851
2852 // Inline smi case if we are in a loop.
2853 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002854 int count_value = expr->op() == Token::INC ? 1 : -1;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002855 if (loop_depth() > 0) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002856 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002857 __ b(vs, &stub_call);
2858 // We could eliminate this smi check if we split the code at
2859 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002860 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002861 __ bind(&stub_call);
2862 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002863 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002864 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002865 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00002866 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002867 __ CallStub(&stub);
2868 __ bind(&done);
2869
2870 // Store the value returned in r0.
2871 switch (assign_type) {
2872 case VARIABLE:
2873 if (expr->is_postfix()) {
2874 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002875 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002876 Expression::kEffect);
2877 // For all contexts except kEffect: We have the result on
2878 // top of the stack.
2879 if (context_ != Expression::kEffect) {
2880 ApplyTOS(context_);
2881 }
2882 } else {
2883 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002884 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002885 context_);
2886 }
2887 break;
2888 case NAMED_PROPERTY: {
2889 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00002890 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002891 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2892 __ Call(ic, RelocInfo::CODE_TARGET);
2893 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002894 if (context_ != Expression::kEffect) {
2895 ApplyTOS(context_);
2896 }
2897 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00002898 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002899 }
2900 break;
2901 }
2902 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002903 __ pop(r1); // Key.
2904 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002905 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
2906 __ Call(ic, RelocInfo::CODE_TARGET);
2907 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002908 if (context_ != Expression::kEffect) {
2909 ApplyTOS(context_);
2910 }
2911 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002912 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002913 }
2914 break;
2915 }
2916 }
2917}
2918
2919
2920void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
2921 Comment cmnt(masm_, "[ BinaryOperation");
2922 switch (expr->op()) {
2923 case Token::COMMA:
2924 VisitForEffect(expr->left());
2925 Visit(expr->right());
2926 break;
2927
2928 case Token::OR:
2929 case Token::AND:
2930 EmitLogicalOperation(expr);
2931 break;
2932
2933 case Token::ADD:
2934 case Token::SUB:
2935 case Token::DIV:
2936 case Token::MOD:
2937 case Token::MUL:
2938 case Token::BIT_OR:
2939 case Token::BIT_AND:
2940 case Token::BIT_XOR:
2941 case Token::SHL:
2942 case Token::SHR:
2943 case Token::SAR:
2944 VisitForValue(expr->left(), kStack);
2945 VisitForValue(expr->right(), kAccumulator);
2946 EmitBinaryOp(expr->op(), context_);
2947 break;
2948
2949 default:
2950 UNREACHABLE();
2951 }
2952}
2953
2954
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002955void FullCodeGenerator::EmitNullCompare(bool strict,
2956 Register obj,
2957 Register null_const,
2958 Label* if_true,
2959 Label* if_false,
2960 Register scratch) {
2961 __ cmp(obj, null_const);
2962 if (strict) {
2963 __ b(eq, if_true);
2964 } else {
2965 __ b(eq, if_true);
2966 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2967 __ cmp(obj, ip);
2968 __ b(eq, if_true);
2969 __ BranchOnSmi(obj, if_false);
2970 // It can be an undetectable object.
2971 __ ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
2972 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
2973 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
2974 __ b(ne, if_true);
2975 }
2976 __ jmp(if_false);
2977}
2978
2979
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002980void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
2981 Comment cmnt(masm_, "[ CompareOperation");
2982
2983 // Always perform the comparison for its control flow. Pack the result
2984 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002985
2986 Label materialize_true, materialize_false;
2987 Label* if_true = NULL;
2988 Label* if_false = NULL;
2989 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002990
2991 VisitForValue(expr->left(), kStack);
2992 switch (expr->op()) {
2993 case Token::IN:
2994 VisitForValue(expr->right(), kStack);
2995 __ InvokeBuiltin(Builtins::IN, CALL_JS);
2996 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
2997 __ cmp(r0, ip);
2998 __ b(eq, if_true);
2999 __ jmp(if_false);
3000 break;
3001
3002 case Token::INSTANCEOF: {
3003 VisitForValue(expr->right(), kStack);
3004 InstanceofStub stub;
3005 __ CallStub(&stub);
3006 __ tst(r0, r0);
3007 __ b(eq, if_true); // The stub returns 0 for true.
3008 __ jmp(if_false);
3009 break;
3010 }
3011
3012 default: {
3013 VisitForValue(expr->right(), kAccumulator);
3014 Condition cc = eq;
3015 bool strict = false;
3016 switch (expr->op()) {
3017 case Token::EQ_STRICT:
3018 strict = true;
3019 // Fall through
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003020 case Token::EQ: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003021 cc = eq;
3022 __ pop(r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003023 // If either operand is constant null we do a fast compare
3024 // against null.
3025 Literal* right_literal = expr->right()->AsLiteral();
3026 Literal* left_literal = expr->left()->AsLiteral();
3027 if (right_literal != NULL && right_literal->handle()->IsNull()) {
3028 EmitNullCompare(strict, r1, r0, if_true, if_false, r2);
3029 Apply(context_, if_true, if_false);
3030 return;
3031 } else if (left_literal != NULL && left_literal->handle()->IsNull()) {
3032 EmitNullCompare(strict, r0, r1, if_true, if_false, r2);
3033 Apply(context_, if_true, if_false);
3034 return;
3035 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003036 break;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003037 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003038 case Token::LT:
3039 cc = lt;
3040 __ pop(r1);
3041 break;
3042 case Token::GT:
3043 // Reverse left and right sides to obtain ECMA-262 conversion order.
3044 cc = lt;
3045 __ mov(r1, result_register());
3046 __ pop(r0);
3047 break;
3048 case Token::LTE:
3049 // Reverse left and right sides to obtain ECMA-262 conversion order.
3050 cc = ge;
3051 __ mov(r1, result_register());
3052 __ pop(r0);
3053 break;
3054 case Token::GTE:
3055 cc = ge;
3056 __ pop(r1);
3057 break;
3058 case Token::IN:
3059 case Token::INSTANCEOF:
3060 default:
3061 UNREACHABLE();
3062 }
3063
3064 // The comparison stub expects the smi vs. smi case to be handled
3065 // before it is called.
3066 Label slow_case;
3067 __ orr(r2, r0, Operand(r1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003068 __ BranchOnNotSmi(r2, &slow_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003069 __ cmp(r1, r0);
3070 __ b(cc, if_true);
3071 __ jmp(if_false);
3072
3073 __ bind(&slow_case);
3074 CompareStub stub(cc, strict);
3075 __ CallStub(&stub);
3076 __ cmp(r0, Operand(0));
3077 __ b(cc, if_true);
3078 __ jmp(if_false);
3079 }
3080 }
3081
3082 // Convert the result of the comparison into one expected for this
3083 // expression's context.
3084 Apply(context_, if_true, if_false);
3085}
3086
3087
3088void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3089 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3090 Apply(context_, r0);
3091}
3092
3093
3094Register FullCodeGenerator::result_register() { return r0; }
3095
3096
3097Register FullCodeGenerator::context_register() { return cp; }
3098
3099
3100void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3101 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3102 __ str(value, MemOperand(fp, frame_offset));
3103}
3104
3105
3106void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
3107 __ ldr(dst, CodeGenerator::ContextOperand(cp, context_index));
3108}
3109
3110
3111// ----------------------------------------------------------------------------
3112// Non-local control flow support.
3113
3114void FullCodeGenerator::EnterFinallyBlock() {
3115 ASSERT(!result_register().is(r1));
3116 // Store result register while executing finally block.
3117 __ push(result_register());
3118 // Cook return address in link register to stack (smi encoded Code* delta)
3119 __ sub(r1, lr, Operand(masm_->CodeObject()));
3120 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3121 ASSERT_EQ(0, kSmiTag);
3122 __ add(r1, r1, Operand(r1)); // Convert to smi.
3123 __ push(r1);
3124}
3125
3126
3127void FullCodeGenerator::ExitFinallyBlock() {
3128 ASSERT(!result_register().is(r1));
3129 // Restore result register from stack.
3130 __ pop(r1);
3131 // Uncook return address and return.
3132 __ pop(result_register());
3133 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3134 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3135 __ add(pc, r1, Operand(masm_->CodeObject()));
3136}
3137
3138
3139#undef __
3140
3141} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003142
3143#endif // V8_TARGET_ARCH_ARM