blob: 58d737834bcc78a83b97604b12cf989cc4a3cc19 [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.org2cc82ae2010-06-14 07:35:38 +0000199 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000200}
201
202
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000203void FullCodeGenerator::EmitReturnSequence() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000204 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;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000227 CodeGenerator::RecordPositions(masm_, function()->end_position());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000228 __ 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);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000241 CHECK(return_sequence_length ==
242 Assembler::kJSReturnSequenceInstructions ||
243 return_sequence_length ==
244 Assembler::kJSReturnSequenceInstructions + 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000245#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000246 }
247}
248
249
250void FullCodeGenerator::Apply(Expression::Context context, Register reg) {
251 switch (context) {
252 case Expression::kUninitialized:
253 UNREACHABLE();
254
255 case Expression::kEffect:
256 // Nothing to do.
257 break;
258
259 case Expression::kValue:
260 // Move value into place.
261 switch (location_) {
262 case kAccumulator:
263 if (!reg.is(result_register())) __ mov(result_register(), reg);
264 break;
265 case kStack:
266 __ push(reg);
267 break;
268 }
269 break;
270
271 case Expression::kValueTest:
272 case Expression::kTestValue:
273 // Push an extra copy of the value in case it's needed.
274 __ push(reg);
275 // Fall through.
276
277 case Expression::kTest:
278 // We always call the runtime on ARM, so push the value as argument.
279 __ push(reg);
280 DoTest(context);
281 break;
282 }
283}
284
285
286void FullCodeGenerator::Apply(Expression::Context context, Slot* slot) {
287 switch (context) {
288 case Expression::kUninitialized:
289 UNREACHABLE();
290 case Expression::kEffect:
291 // Nothing to do.
292 break;
293 case Expression::kValue:
294 case Expression::kTest:
295 case Expression::kValueTest:
296 case Expression::kTestValue:
297 // On ARM we have to move the value into a register to do anything
298 // with it.
299 Move(result_register(), slot);
300 Apply(context, result_register());
301 break;
302 }
303}
304
305
306void FullCodeGenerator::Apply(Expression::Context context, Literal* lit) {
307 switch (context) {
308 case Expression::kUninitialized:
309 UNREACHABLE();
310 case Expression::kEffect:
311 break;
312 // Nothing to do.
313 case Expression::kValue:
314 case Expression::kTest:
315 case Expression::kValueTest:
316 case Expression::kTestValue:
317 // On ARM we have to move the value into a register to do anything
318 // with it.
319 __ mov(result_register(), Operand(lit->handle()));
320 Apply(context, result_register());
321 break;
322 }
323}
324
325
326void FullCodeGenerator::ApplyTOS(Expression::Context context) {
327 switch (context) {
328 case Expression::kUninitialized:
329 UNREACHABLE();
330
331 case Expression::kEffect:
332 __ Drop(1);
333 break;
334
335 case Expression::kValue:
336 switch (location_) {
337 case kAccumulator:
338 __ pop(result_register());
339 break;
340 case kStack:
341 break;
342 }
343 break;
344
345 case Expression::kValueTest:
346 case Expression::kTestValue:
347 // Duplicate the value on the stack in case it's needed.
348 __ ldr(ip, MemOperand(sp));
349 __ push(ip);
350 // Fall through.
351
352 case Expression::kTest:
353 DoTest(context);
354 break;
355 }
356}
357
358
359void FullCodeGenerator::DropAndApply(int count,
360 Expression::Context context,
361 Register reg) {
362 ASSERT(count > 0);
363 ASSERT(!reg.is(sp));
364 switch (context) {
365 case Expression::kUninitialized:
366 UNREACHABLE();
367
368 case Expression::kEffect:
369 __ Drop(count);
370 break;
371
372 case Expression::kValue:
373 switch (location_) {
374 case kAccumulator:
375 __ Drop(count);
376 if (!reg.is(result_register())) __ mov(result_register(), reg);
377 break;
378 case kStack:
379 if (count > 1) __ Drop(count - 1);
380 __ str(reg, MemOperand(sp));
381 break;
382 }
383 break;
384
385 case Expression::kTest:
386 if (count > 1) __ Drop(count - 1);
387 __ str(reg, MemOperand(sp));
388 DoTest(context);
389 break;
390
391 case Expression::kValueTest:
392 case Expression::kTestValue:
393 if (count == 1) {
394 __ str(reg, MemOperand(sp));
395 __ push(reg);
396 } else { // count > 1
397 __ Drop(count - 2);
398 __ str(reg, MemOperand(sp, kPointerSize));
399 __ str(reg, MemOperand(sp));
400 }
401 DoTest(context);
402 break;
403 }
404}
405
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000406void FullCodeGenerator::PrepareTest(Label* materialize_true,
407 Label* materialize_false,
408 Label** if_true,
409 Label** if_false) {
410 switch (context_) {
411 case Expression::kUninitialized:
412 UNREACHABLE();
413 break;
414 case Expression::kEffect:
415 // In an effect context, the true and the false case branch to the
416 // same label.
417 *if_true = *if_false = materialize_true;
418 break;
419 case Expression::kValue:
420 *if_true = materialize_true;
421 *if_false = materialize_false;
422 break;
423 case Expression::kTest:
424 *if_true = true_label_;
425 *if_false = false_label_;
426 break;
427 case Expression::kValueTest:
428 *if_true = materialize_true;
429 *if_false = false_label_;
430 break;
431 case Expression::kTestValue:
432 *if_true = true_label_;
433 *if_false = materialize_false;
434 break;
435 }
436}
437
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000438
439void FullCodeGenerator::Apply(Expression::Context context,
440 Label* materialize_true,
441 Label* materialize_false) {
442 switch (context) {
443 case Expression::kUninitialized:
444
445 case Expression::kEffect:
446 ASSERT_EQ(materialize_true, materialize_false);
447 __ bind(materialize_true);
448 break;
449
450 case Expression::kValue: {
451 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000452 switch (location_) {
453 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000454 __ bind(materialize_true);
455 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
456 __ jmp(&done);
457 __ bind(materialize_false);
458 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000459 break;
460 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000461 __ bind(materialize_true);
462 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
463 __ push(ip);
464 __ jmp(&done);
465 __ bind(materialize_false);
466 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
467 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000468 break;
469 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000470 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000471 break;
472 }
473
474 case Expression::kTest:
475 break;
476
477 case Expression::kValueTest:
478 __ bind(materialize_true);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000479 switch (location_) {
480 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000481 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000482 break;
483 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000484 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
485 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000486 break;
487 }
488 __ jmp(true_label_);
489 break;
490
491 case Expression::kTestValue:
492 __ bind(materialize_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000493 switch (location_) {
494 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000495 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000496 break;
497 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000498 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
499 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000500 break;
501 }
502 __ jmp(false_label_);
503 break;
504 }
505}
506
507
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000508// Convert constant control flow (true or false) to the result expected for
509// a given expression context.
510void FullCodeGenerator::Apply(Expression::Context context, bool flag) {
511 switch (context) {
512 case Expression::kUninitialized:
513 UNREACHABLE();
514 break;
515 case Expression::kEffect:
516 break;
517 case Expression::kValue: {
518 Heap::RootListIndex value_root_index =
519 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
520 switch (location_) {
521 case kAccumulator:
522 __ LoadRoot(result_register(), value_root_index);
523 break;
524 case kStack:
525 __ LoadRoot(ip, value_root_index);
526 __ push(ip);
527 break;
528 }
529 break;
530 }
531 case Expression::kTest:
532 __ b(flag ? true_label_ : false_label_);
533 break;
534 case Expression::kTestValue:
535 switch (location_) {
536 case kAccumulator:
537 // If value is false it's needed.
538 if (!flag) __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
539 break;
540 case kStack:
541 // If value is false it's needed.
542 if (!flag) {
543 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
544 __ push(ip);
545 }
546 break;
547 }
548 __ b(flag ? true_label_ : false_label_);
549 break;
550 case Expression::kValueTest:
551 switch (location_) {
552 case kAccumulator:
553 // If value is true it's needed.
554 if (flag) __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
555 break;
556 case kStack:
557 // If value is true it's needed.
558 if (flag) {
559 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
560 __ push(ip);
561 }
562 break;
563 }
564 __ b(flag ? true_label_ : false_label_);
565 break;
566 }
567}
568
569
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000570void FullCodeGenerator::DoTest(Expression::Context context) {
571 // The value to test is pushed on the stack, and duplicated on the stack
572 // if necessary (for value/test and test/value contexts).
573 ASSERT_NE(NULL, true_label_);
574 ASSERT_NE(NULL, false_label_);
575
576 // Call the runtime to find the boolean value of the source and then
577 // translate it into control flow to the pair of labels.
578 __ CallRuntime(Runtime::kToBool, 1);
579 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
580 __ cmp(r0, ip);
581
582 // Complete based on the context.
583 switch (context) {
584 case Expression::kUninitialized:
585 case Expression::kEffect:
586 case Expression::kValue:
587 UNREACHABLE();
588
589 case Expression::kTest:
590 __ b(eq, true_label_);
591 __ jmp(false_label_);
592 break;
593
594 case Expression::kValueTest: {
595 Label discard;
596 switch (location_) {
597 case kAccumulator:
598 __ b(ne, &discard);
599 __ pop(result_register());
600 __ jmp(true_label_);
601 break;
602 case kStack:
603 __ b(eq, true_label_);
604 break;
605 }
606 __ bind(&discard);
607 __ Drop(1);
608 __ jmp(false_label_);
609 break;
610 }
611
612 case Expression::kTestValue: {
613 Label discard;
614 switch (location_) {
615 case kAccumulator:
616 __ b(eq, &discard);
617 __ pop(result_register());
618 __ jmp(false_label_);
619 break;
620 case kStack:
621 __ b(ne, false_label_);
622 break;
623 }
624 __ bind(&discard);
625 __ Drop(1);
626 __ jmp(true_label_);
627 break;
628 }
629 }
630}
631
632
633MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
634 switch (slot->type()) {
635 case Slot::PARAMETER:
636 case Slot::LOCAL:
637 return MemOperand(fp, SlotOffset(slot));
638 case Slot::CONTEXT: {
639 int context_chain_length =
ager@chromium.org5c838252010-02-19 08:53:10 +0000640 scope()->ContextChainLength(slot->var()->scope());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000641 __ LoadContext(scratch, context_chain_length);
642 return CodeGenerator::ContextOperand(scratch, slot->index());
643 }
644 case Slot::LOOKUP:
645 UNREACHABLE();
646 }
647 UNREACHABLE();
648 return MemOperand(r0, 0);
649}
650
651
652void FullCodeGenerator::Move(Register destination, Slot* source) {
653 // Use destination as scratch.
654 MemOperand slot_operand = EmitSlotSearch(source, destination);
655 __ ldr(destination, slot_operand);
656}
657
658
659void FullCodeGenerator::Move(Slot* dst,
660 Register src,
661 Register scratch1,
662 Register scratch2) {
663 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
664 ASSERT(!scratch1.is(src) && !scratch2.is(src));
665 MemOperand location = EmitSlotSearch(dst, scratch1);
666 __ str(src, location);
667 // Emit the write barrier code if the location is in the heap.
668 if (dst->type() == Slot::CONTEXT) {
669 __ mov(scratch2, Operand(Context::SlotOffset(dst->index())));
670 __ RecordWrite(scratch1, scratch2, src);
671 }
672}
673
674
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000675void FullCodeGenerator::EmitDeclaration(Variable* variable,
676 Variable::Mode mode,
677 FunctionLiteral* function) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000678 Comment cmnt(masm_, "[ Declaration");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000679 ASSERT(variable != NULL); // Must have been resolved.
680 Slot* slot = variable->slot();
681 Property* prop = variable->AsProperty();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000682
683 if (slot != NULL) {
684 switch (slot->type()) {
685 case Slot::PARAMETER:
686 case Slot::LOCAL:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000687 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000688 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
689 __ str(ip, MemOperand(fp, SlotOffset(slot)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000690 } else if (function != NULL) {
691 VisitForValue(function, kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000692 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
693 }
694 break;
695
696 case Slot::CONTEXT:
697 // We bypass the general EmitSlotSearch because we know more about
698 // this specific context.
699
700 // The variable in the decl always resides in the current context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000701 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000702 if (FLAG_debug_code) {
703 // Check if we have the correct context pointer.
704 __ ldr(r1,
705 CodeGenerator::ContextOperand(cp, Context::FCONTEXT_INDEX));
706 __ cmp(r1, cp);
707 __ Check(eq, "Unexpected declaration in current context.");
708 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000709 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000710 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
711 __ str(ip, CodeGenerator::ContextOperand(cp, slot->index()));
712 // No write barrier since the_hole_value is in old space.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000713 } else if (function != NULL) {
714 VisitForValue(function, kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000715 __ str(result_register(),
716 CodeGenerator::ContextOperand(cp, slot->index()));
717 int offset = Context::SlotOffset(slot->index());
718 __ mov(r2, Operand(offset));
719 // We know that we have written a function, which is not a smi.
720 __ mov(r1, Operand(cp));
721 __ RecordWrite(r1, r2, result_register());
722 }
723 break;
724
725 case Slot::LOOKUP: {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000726 __ mov(r2, Operand(variable->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000727 // Declaration nodes are always introduced in one of two modes.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000728 ASSERT(mode == Variable::VAR ||
729 mode == Variable::CONST);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000730 PropertyAttributes attr =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000731 (mode == Variable::VAR) ? NONE : READ_ONLY;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000732 __ mov(r1, Operand(Smi::FromInt(attr)));
733 // Push initial value, if any.
734 // Note: For variables we must not push an initial value (such as
735 // 'undefined') because we may have a (legal) redeclaration and we
736 // must not destroy the current value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000737 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000738 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000739 __ Push(cp, r2, r1, r0);
740 } else if (function != NULL) {
741 __ Push(cp, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000742 // Push initial value for function declaration.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000743 VisitForValue(function, kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000744 } else {
745 __ mov(r0, Operand(Smi::FromInt(0))); // No initial value!
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000746 __ Push(cp, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000747 }
748 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
749 break;
750 }
751 }
752
753 } else if (prop != NULL) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000754 if (function != NULL || mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000755 // We are declaring a function or constant that rewrites to a
756 // property. Use (keyed) IC to set the initial value.
757 VisitForValue(prop->obj(), kStack);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000758 if (function != NULL) {
759 VisitForValue(prop->key(), kStack);
760 VisitForValue(function, kAccumulator);
761 __ pop(r1); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000762 } else {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000763 VisitForValue(prop->key(), kAccumulator);
764 __ mov(r1, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000765 __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex);
766 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000767 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000768
769 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
770 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000771 // Value in r0 is ignored (declarations are statements).
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000772 }
773 }
774}
775
776
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000777void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
778 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
779}
780
781
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000782void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
783 // Call the runtime to declare the globals.
784 // The context is the first argument.
785 __ mov(r1, Operand(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000786 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000787 __ Push(cp, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000788 __ CallRuntime(Runtime::kDeclareGlobals, 3);
789 // Return value is ignored.
790}
791
792
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000793void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000794 Comment cmnt(masm_, "[ SwitchStatement");
795 Breakable nested_statement(this, stmt);
796 SetStatementPosition(stmt);
797 // Keep the switch value on the stack until a case matches.
798 VisitForValue(stmt->tag(), kStack);
799
800 ZoneList<CaseClause*>* clauses = stmt->cases();
801 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
802
803 Label next_test; // Recycled for each test.
804 // Compile all the tests with branches to their bodies.
805 for (int i = 0; i < clauses->length(); i++) {
806 CaseClause* clause = clauses->at(i);
807 // The default is not a test, but remember it as final fall through.
808 if (clause->is_default()) {
809 default_clause = clause;
810 continue;
811 }
812
813 Comment cmnt(masm_, "[ Case comparison");
814 __ bind(&next_test);
815 next_test.Unuse();
816
817 // Compile the label expression.
818 VisitForValue(clause->label(), kAccumulator);
819
820 // Perform the comparison as if via '==='. The comparison stub expects
821 // the smi vs. smi case to be handled before it is called.
822 Label slow_case;
823 __ ldr(r1, MemOperand(sp, 0)); // Switch value.
824 __ mov(r2, r1);
825 __ orr(r2, r2, r0);
826 __ tst(r2, Operand(kSmiTagMask));
827 __ b(ne, &slow_case);
828 __ cmp(r1, r0);
829 __ b(ne, &next_test);
830 __ Drop(1); // Switch value is no longer needed.
831 __ b(clause->body_target()->entry_label());
832
833 __ bind(&slow_case);
834 CompareStub stub(eq, true);
835 __ CallStub(&stub);
836 __ tst(r0, r0);
837 __ b(ne, &next_test);
838 __ Drop(1); // Switch value is no longer needed.
839 __ b(clause->body_target()->entry_label());
840 }
841
842 // Discard the test value and jump to the default if present, otherwise to
843 // the end of the statement.
844 __ bind(&next_test);
845 __ Drop(1); // Switch value is no longer needed.
846 if (default_clause == NULL) {
847 __ b(nested_statement.break_target());
848 } else {
849 __ b(default_clause->body_target()->entry_label());
850 }
851
852 // Compile all the case bodies.
853 for (int i = 0; i < clauses->length(); i++) {
854 Comment cmnt(masm_, "[ Case body");
855 CaseClause* clause = clauses->at(i);
856 __ bind(clause->body_target()->entry_label());
857 VisitStatements(clause->statements());
858 }
859
860 __ bind(nested_statement.break_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000861}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000862
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000863
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000864void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000865 Comment cmnt(masm_, "[ ForInStatement");
866 SetStatementPosition(stmt);
867
868 Label loop, exit;
869 ForIn loop_statement(this, stmt);
870 increment_loop_depth();
871
872 // Get the object to enumerate over. Both SpiderMonkey and JSC
873 // ignore null and undefined in contrast to the specification; see
874 // ECMA-262 section 12.6.4.
875 VisitForValue(stmt->enumerable(), kAccumulator);
876 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
877 __ cmp(r0, ip);
878 __ b(eq, &exit);
879 __ LoadRoot(ip, Heap::kNullValueRootIndex);
880 __ cmp(r0, ip);
881 __ b(eq, &exit);
882
883 // Convert the object to a JS object.
884 Label convert, done_convert;
885 __ BranchOnSmi(r0, &convert);
886 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
887 __ b(hs, &done_convert);
888 __ bind(&convert);
889 __ push(r0);
890 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
891 __ bind(&done_convert);
892 __ push(r0);
893
894 // TODO(kasperl): Check cache validity in generated code. This is a
895 // fast case for the JSObject::IsSimpleEnum cache validity
896 // checks. If we cannot guarantee cache validity, call the runtime
897 // system to check cache validity or get the property names in a
898 // fixed array.
899
900 // Get the set of properties to enumerate.
901 __ push(r0); // Duplicate the enumerable object on the stack.
902 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
903
904 // If we got a map from the runtime call, we can do a fast
905 // modification check. Otherwise, we got a fixed array, and we have
906 // to do a slow check.
907 Label fixed_array;
908 __ mov(r2, r0);
909 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
910 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
911 __ cmp(r1, ip);
912 __ b(ne, &fixed_array);
913
914 // We got a map in register r0. Get the enumeration cache from it.
915 __ ldr(r1, FieldMemOperand(r0, Map::kInstanceDescriptorsOffset));
916 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
917 __ ldr(r2, FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
918
919 // Setup the four remaining stack slots.
920 __ push(r0); // Map.
921 __ ldr(r1, FieldMemOperand(r2, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000922 __ mov(r0, Operand(Smi::FromInt(0)));
923 // Push enumeration cache, enumeration cache length (as smi) and zero.
924 __ Push(r2, r1, r0);
925 __ jmp(&loop);
926
927 // We got a fixed array in register r0. Iterate through that.
928 __ bind(&fixed_array);
929 __ mov(r1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
930 __ Push(r1, r0);
931 __ ldr(r1, FieldMemOperand(r0, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000932 __ 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
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001651void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
1652 Expression* key,
1653 RelocInfo::Mode mode) {
1654 // Code common for calls using the IC.
1655 ZoneList<Expression*>* args = expr->arguments();
1656 int arg_count = args->length();
1657 for (int i = 0; i < arg_count; i++) {
1658 VisitForValue(args->at(i), kStack);
1659 }
1660 VisitForValue(key, kAccumulator);
1661 __ mov(r2, r0);
1662 // Record source position for debugger.
1663 SetSourcePosition(expr->position());
1664 // Call the IC initialization code.
1665 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1666 Handle<Code> ic = CodeGenerator::ComputeKeyedCallInitialize(arg_count,
1667 in_loop);
1668 __ Call(ic, mode);
1669 // Restore context register.
1670 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1671 Apply(context_, r0);
1672}
1673
1674
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001675void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1676 // Code common for calls using the call stub.
1677 ZoneList<Expression*>* args = expr->arguments();
1678 int arg_count = args->length();
1679 for (int i = 0; i < arg_count; i++) {
1680 VisitForValue(args->at(i), kStack);
1681 }
1682 // Record source position for debugger.
1683 SetSourcePosition(expr->position());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001684 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1685 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001686 __ CallStub(&stub);
1687 // Restore context register.
1688 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001689 DropAndApply(1, context_, r0);
1690}
1691
1692
1693void FullCodeGenerator::VisitCall(Call* expr) {
1694 Comment cmnt(masm_, "[ Call");
1695 Expression* fun = expr->expression();
1696 Variable* var = fun->AsVariableProxy()->AsVariable();
1697
1698 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001699 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1700 // resolve the function we need to call and the receiver of the
1701 // call. Then we call the resolved function using the given
1702 // arguments.
1703 VisitForValue(fun, kStack);
1704 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1705 __ push(r2); // Reserved receiver slot.
1706
1707 // Push the arguments.
1708 ZoneList<Expression*>* args = expr->arguments();
1709 int arg_count = args->length();
1710 for (int i = 0; i < arg_count; i++) {
1711 VisitForValue(args->at(i), kStack);
1712 }
1713
1714 // Push copy of the function - found below the arguments.
1715 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1716 __ push(r1);
1717
1718 // Push copy of the first argument or undefined if it doesn't exist.
1719 if (arg_count > 0) {
1720 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1721 __ push(r1);
1722 } else {
1723 __ push(r2);
1724 }
1725
1726 // Push the receiver of the enclosing function and do runtime call.
1727 __ ldr(r1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1728 __ push(r1);
1729 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1730
1731 // The runtime call returns a pair of values in r0 (function) and
1732 // r1 (receiver). Touch up the stack with the right values.
1733 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1734 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
1735
1736 // Record source position for debugger.
1737 SetSourcePosition(expr->position());
1738 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1739 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1740 __ CallStub(&stub);
1741 // Restore context register.
1742 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1743 DropAndApply(1, context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001744 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001745 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001746 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001747 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001748 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1749 } else if (var != NULL && var->slot() != NULL &&
1750 var->slot()->type() == Slot::LOOKUP) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001751 // Call to a lookup slot (dynamically introduced variable). Call the
1752 // runtime to find the function to call (returned in eax) and the object
1753 // holding it (returned in edx).
1754 __ push(context_register());
1755 __ mov(r2, Operand(var->name()));
1756 __ push(r2);
1757 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1758 __ push(r0); // Function.
1759 __ push(r1); // Receiver.
1760 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001761 } else if (fun->AsProperty() != NULL) {
1762 // Call to an object property.
1763 Property* prop = fun->AsProperty();
1764 Literal* key = prop->key()->AsLiteral();
1765 if (key != NULL && key->handle()->IsSymbol()) {
1766 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001767 VisitForValue(prop->obj(), kStack);
1768 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1769 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001770 // Call to a keyed property.
1771 // For a synthetic property use keyed load IC followed by function call,
1772 // for a regular property use keyed CallIC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001773 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001774 if (prop->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001775 VisitForValue(prop->key(), kAccumulator);
1776 // Record source code position for IC call.
1777 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001778 __ pop(r1); // We do not need to keep the receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001779
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001780 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1781 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001782 // Push result (function).
1783 __ push(r0);
1784 // Push Global receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001785 __ ldr(r1, CodeGenerator::GlobalObject());
1786 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001787 __ push(r1);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001788 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001789 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001790 EmitKeyedCallWithIC(expr, prop->key(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001791 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001792 }
1793 } else {
1794 // Call to some other expression. If the expression is an anonymous
1795 // function literal not called in a loop, mark it as one that should
1796 // also use the fast code generator.
1797 FunctionLiteral* lit = fun->AsFunctionLiteral();
1798 if (lit != NULL &&
1799 lit->name()->Equals(Heap::empty_string()) &&
1800 loop_depth() == 0) {
1801 lit->set_try_full_codegen(true);
1802 }
1803 VisitForValue(fun, kStack);
1804 // Load global receiver object.
1805 __ ldr(r1, CodeGenerator::GlobalObject());
1806 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1807 __ push(r1);
1808 // Emit function call.
1809 EmitCallWithStub(expr);
1810 }
1811}
1812
1813
1814void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1815 Comment cmnt(masm_, "[ CallNew");
1816 // According to ECMA-262, section 11.2.2, page 44, the function
1817 // expression in new calls must be evaluated before the
1818 // arguments.
1819 // Push function on the stack.
1820 VisitForValue(expr->expression(), kStack);
1821
1822 // Push global object (receiver).
1823 __ ldr(r0, CodeGenerator::GlobalObject());
1824 __ push(r0);
1825 // Push the arguments ("left-to-right") on the stack.
1826 ZoneList<Expression*>* args = expr->arguments();
1827 int arg_count = args->length();
1828 for (int i = 0; i < arg_count; i++) {
1829 VisitForValue(args->at(i), kStack);
1830 }
1831
1832 // Call the construct call builtin that handles allocation and
1833 // constructor invocation.
1834 SetSourcePosition(expr->position());
1835
1836 // Load function, arg_count into r1 and r0.
1837 __ mov(r0, Operand(arg_count));
1838 // Function is in sp[arg_count + 1].
1839 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1840
1841 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1842 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
1843
1844 // Replace function on TOS with result in r0, or pop it.
1845 DropAndApply(1, context_, r0);
1846}
1847
1848
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001849void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1850 ASSERT(args->length() == 1);
1851
1852 VisitForValue(args->at(0), kAccumulator);
1853
1854 Label materialize_true, materialize_false;
1855 Label* if_true = NULL;
1856 Label* if_false = NULL;
1857 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1858
1859 __ BranchOnSmi(r0, if_true);
1860 __ b(if_false);
1861
1862 Apply(context_, if_true, if_false);
1863}
1864
1865
1866void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1867 ASSERT(args->length() == 1);
1868
1869 VisitForValue(args->at(0), kAccumulator);
1870
1871 Label materialize_true, materialize_false;
1872 Label* if_true = NULL;
1873 Label* if_false = NULL;
1874 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1875
1876 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
1877 __ b(eq, if_true);
1878 __ b(if_false);
1879
1880 Apply(context_, if_true, if_false);
1881}
1882
1883
1884void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1885 ASSERT(args->length() == 1);
1886
1887 VisitForValue(args->at(0), kAccumulator);
1888
1889 Label materialize_true, materialize_false;
1890 Label* if_true = NULL;
1891 Label* if_false = NULL;
1892 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1893 __ BranchOnSmi(r0, if_false);
1894 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1895 __ cmp(r0, ip);
1896 __ b(eq, if_true);
1897 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1898 // Undetectable objects behave like undefined when tested with typeof.
1899 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
1900 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1901 __ b(ne, if_false);
1902 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1903 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
1904 __ b(lt, if_false);
1905 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
1906 __ b(le, if_true);
1907 __ b(if_false);
1908
1909 Apply(context_, if_true, if_false);
1910}
1911
1912
1913void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
1914 ASSERT(args->length() == 1);
1915
1916 VisitForValue(args->at(0), kAccumulator);
1917
1918 Label materialize_true, materialize_false;
1919 Label* if_true = NULL;
1920 Label* if_false = NULL;
1921 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1922
1923 __ BranchOnSmi(r0, if_false);
1924 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1925 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
1926 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1927 __ b(ne, if_true);
1928 __ b(if_false);
1929
1930 Apply(context_, if_true, if_false);
1931}
1932
1933
1934void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
1935 ASSERT(args->length() == 1);
1936
1937 VisitForValue(args->at(0), kAccumulator);
1938
1939 Label materialize_true, materialize_false;
1940 Label* if_true = NULL;
1941 Label* if_false = NULL;
1942 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1943
1944 __ BranchOnSmi(r0, if_false);
1945 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
1946 __ b(eq, if_true);
1947 __ b(if_false);
1948
1949 Apply(context_, if_true, if_false);
1950}
1951
1952
1953void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
1954 ASSERT(args->length() == 1);
1955
1956 VisitForValue(args->at(0), kAccumulator);
1957
1958 Label materialize_true, materialize_false;
1959 Label* if_true = NULL;
1960 Label* if_false = NULL;
1961 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1962
1963 __ BranchOnSmi(r0, if_false);
1964 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
1965 __ b(eq, if_true);
1966 __ b(if_false);
1967
1968 Apply(context_, if_true, if_false);
1969}
1970
1971
1972void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
1973 ASSERT(args->length() == 1);
1974
1975 VisitForValue(args->at(0), kAccumulator);
1976
1977 Label materialize_true, materialize_false;
1978 Label* if_true = NULL;
1979 Label* if_false = NULL;
1980 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1981
1982 __ BranchOnSmi(r0, if_false);
1983 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
1984 __ b(eq, if_true);
1985 __ b(if_false);
1986
1987 Apply(context_, if_true, if_false);
1988}
1989
1990
1991
1992void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
1993 ASSERT(args->length() == 0);
1994
1995 Label materialize_true, materialize_false;
1996 Label* if_true = NULL;
1997 Label* if_false = NULL;
1998 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1999
2000 // Get the frame pointer for the calling frame.
2001 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2002
2003 // Skip the arguments adaptor frame if it exists.
2004 Label check_frame_marker;
2005 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
2006 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2007 __ b(ne, &check_frame_marker);
2008 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
2009
2010 // Check the marker in the calling frame.
2011 __ bind(&check_frame_marker);
2012 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
2013 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
2014 __ b(eq, if_true);
2015 __ b(if_false);
2016
2017 Apply(context_, if_true, if_false);
2018}
2019
2020
2021void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
2022 ASSERT(args->length() == 2);
2023
2024 // Load the two objects into registers and perform the comparison.
2025 VisitForValue(args->at(0), kStack);
2026 VisitForValue(args->at(1), kAccumulator);
2027
2028 Label materialize_true, materialize_false;
2029 Label* if_true = NULL;
2030 Label* if_false = NULL;
2031 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2032
2033 __ pop(r1);
2034 __ cmp(r0, r1);
2035 __ b(eq, if_true);
2036 __ b(if_false);
2037
2038 Apply(context_, if_true, if_false);
2039}
2040
2041
2042void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
2043 ASSERT(args->length() == 1);
2044
2045 // ArgumentsAccessStub expects the key in edx and the formal
2046 // parameter count in eax.
2047 VisitForValue(args->at(0), kAccumulator);
2048 __ mov(r1, r0);
2049 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2050 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2051 __ CallStub(&stub);
2052 Apply(context_, r0);
2053}
2054
2055
2056void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
2057 ASSERT(args->length() == 0);
2058
2059 Label exit;
2060 // Get the number of formal parameters.
2061 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2062
2063 // Check if the calling frame is an arguments adaptor frame.
2064 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2065 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
2066 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2067 __ b(ne, &exit);
2068
2069 // Arguments adaptor case: Read the arguments length from the
2070 // adaptor frame.
2071 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2072
2073 __ bind(&exit);
2074 Apply(context_, r0);
2075}
2076
2077
2078void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
2079 ASSERT(args->length() == 1);
2080 Label done, null, function, non_function_constructor;
2081
2082 VisitForValue(args->at(0), kAccumulator);
2083
2084 // If the object is a smi, we return null.
2085 __ BranchOnSmi(r0, &null);
2086
2087 // Check that the object is a JS object but take special care of JS
2088 // functions to make sure they have 'Function' as their class.
2089 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2090 __ b(lt, &null);
2091
2092 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2093 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2094 // LAST_JS_OBJECT_TYPE.
2095 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2096 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2097 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2098 __ b(eq, &function);
2099
2100 // Check if the constructor in the map is a function.
2101 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2102 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2103 __ b(ne, &non_function_constructor);
2104
2105 // r0 now contains the constructor function. Grab the
2106 // instance class name from there.
2107 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2108 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2109 __ b(&done);
2110
2111 // Functions have class 'Function'.
2112 __ bind(&function);
2113 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2114 __ jmp(&done);
2115
2116 // Objects with a non-function constructor have class 'Object'.
2117 __ bind(&non_function_constructor);
2118 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2119 __ jmp(&done);
2120
2121 // Non-JS objects have class null.
2122 __ bind(&null);
2123 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2124
2125 // All done.
2126 __ bind(&done);
2127
2128 Apply(context_, r0);
2129}
2130
2131
2132void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2133 // Conditionally generate a log call.
2134 // Args:
2135 // 0 (literal string): The type of logging (corresponds to the flags).
2136 // This is used to determine whether or not to generate the log call.
2137 // 1 (string): Format string. Access the string at argument index 2
2138 // with '%2s' (see Logger::LogRuntime for all the formats).
2139 // 2 (array): Arguments to the format string.
2140 ASSERT_EQ(args->length(), 3);
2141#ifdef ENABLE_LOGGING_AND_PROFILING
2142 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2143 VisitForValue(args->at(1), kStack);
2144 VisitForValue(args->at(2), kStack);
2145 __ CallRuntime(Runtime::kLog, 2);
2146 }
2147#endif
2148 // Finally, we're expected to leave a value on the top of the stack.
2149 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2150 Apply(context_, r0);
2151}
2152
2153
2154void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2155 ASSERT(args->length() == 0);
2156
2157 Label slow_allocate_heapnumber;
2158 Label heapnumber_allocated;
2159
2160 __ AllocateHeapNumber(r4, r1, r2, &slow_allocate_heapnumber);
2161 __ jmp(&heapnumber_allocated);
2162
2163 __ bind(&slow_allocate_heapnumber);
2164 // To allocate a heap number, and ensure that it is not a smi, we
2165 // call the runtime function FUnaryMinus on 0, returning the double
2166 // -0.0. A new, distinct heap number is returned each time.
2167 __ mov(r0, Operand(Smi::FromInt(0)));
2168 __ push(r0);
2169 __ CallRuntime(Runtime::kNumberUnaryMinus, 1);
2170 __ mov(r4, Operand(r0));
2171
2172 __ bind(&heapnumber_allocated);
2173
2174 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
2175 // by computing:
2176 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2177 if (CpuFeatures::IsSupported(VFP3)) {
2178 __ PrepareCallCFunction(0, r1);
2179 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
2180
2181 CpuFeatures::Scope scope(VFP3);
2182 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2183 // Create this constant using mov/orr to avoid PC relative load.
2184 __ mov(r1, Operand(0x41000000));
2185 __ orr(r1, r1, Operand(0x300000));
2186 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
2187 __ vmov(d7, r0, r1);
2188 // Move 0x4130000000000000 to VFP.
2189 __ mov(r0, Operand(0));
2190 __ vmov(d8, r0, r1);
2191 // Subtract and store the result in the heap number.
2192 __ vsub(d7, d7, d8);
2193 __ sub(r0, r4, Operand(kHeapObjectTag));
2194 __ vstr(d7, r0, HeapNumber::kValueOffset);
2195 __ mov(r0, r4);
2196 } else {
2197 __ mov(r0, Operand(r4));
2198 __ PrepareCallCFunction(1, r1);
2199 __ CallCFunction(
2200 ExternalReference::fill_heap_number_with_random_function(), 1);
2201 }
2202
2203 Apply(context_, r0);
2204}
2205
2206
2207void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
2208 // Load the arguments on the stack and call the stub.
2209 SubStringStub stub;
2210 ASSERT(args->length() == 3);
2211 VisitForValue(args->at(0), kStack);
2212 VisitForValue(args->at(1), kStack);
2213 VisitForValue(args->at(2), kStack);
2214 __ CallStub(&stub);
2215 Apply(context_, r0);
2216}
2217
2218
2219void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
2220 // Load the arguments on the stack and call the stub.
2221 RegExpExecStub stub;
2222 ASSERT(args->length() == 4);
2223 VisitForValue(args->at(0), kStack);
2224 VisitForValue(args->at(1), kStack);
2225 VisitForValue(args->at(2), kStack);
2226 VisitForValue(args->at(3), kStack);
2227 __ CallStub(&stub);
2228 Apply(context_, r0);
2229}
2230
2231
2232void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
2233 ASSERT(args->length() == 1);
2234
2235 VisitForValue(args->at(0), kAccumulator); // Load the object.
2236
2237 Label done;
2238 // If the object is a smi return the object.
2239 __ BranchOnSmi(r0, &done);
2240 // If the object is not a value type, return the object.
2241 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
2242 __ b(ne, &done);
2243 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2244
2245 __ bind(&done);
2246 Apply(context_, r0);
2247}
2248
2249
2250void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
2251 // Load the arguments on the stack and call the runtime function.
2252 ASSERT(args->length() == 2);
2253 VisitForValue(args->at(0), kStack);
2254 VisitForValue(args->at(1), kStack);
2255 __ CallRuntime(Runtime::kMath_pow, 2);
2256 Apply(context_, r0);
2257}
2258
2259
2260void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
2261 ASSERT(args->length() == 2);
2262
2263 VisitForValue(args->at(0), kStack); // Load the object.
2264 VisitForValue(args->at(1), kAccumulator); // Load the value.
2265 __ pop(r1); // r0 = value. r1 = object.
2266
2267 Label done;
2268 // If the object is a smi, return the value.
2269 __ BranchOnSmi(r1, &done);
2270
2271 // If the object is not a value type, return the value.
2272 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
2273 __ b(ne, &done);
2274
2275 // Store the value.
2276 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2277 // Update the write barrier. Save the value as it will be
2278 // overwritten by the write barrier code and is needed afterward.
2279 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
2280 __ RecordWrite(r1, r2, r3);
2281
2282 __ bind(&done);
2283 Apply(context_, r0);
2284}
2285
2286
2287void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2288 ASSERT_EQ(args->length(), 1);
2289
2290 // Load the argument on the stack and call the stub.
2291 VisitForValue(args->at(0), kStack);
2292
2293 NumberToStringStub stub;
2294 __ CallStub(&stub);
2295 Apply(context_, r0);
2296}
2297
2298
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002299void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002300 ASSERT(args->length() == 1);
2301
2302 VisitForValue(args->at(0), kAccumulator);
2303
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002304 Label done;
2305 StringCharFromCodeGenerator generator(r0, r1);
2306 generator.GenerateFast(masm_);
2307 __ jmp(&done);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002308
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002309 NopRuntimeCallHelper call_helper;
2310 generator.GenerateSlow(masm_, call_helper);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002311
2312 __ bind(&done);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002313 Apply(context_, r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002314}
2315
2316
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002317void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
2318 ASSERT(args->length() == 2);
2319
2320 VisitForValue(args->at(0), kStack);
2321 VisitForValue(args->at(1), kAccumulator);
2322
2323 Register object = r1;
2324 Register index = r0;
2325 Register scratch = r2;
2326 Register result = r3;
2327
2328 __ pop(object);
2329
2330 Label need_conversion;
2331 Label index_out_of_range;
2332 Label done;
2333 StringCharCodeAtGenerator generator(object,
2334 index,
2335 scratch,
2336 result,
2337 &need_conversion,
2338 &need_conversion,
2339 &index_out_of_range,
2340 STRING_INDEX_IS_NUMBER);
2341 generator.GenerateFast(masm_);
2342 __ jmp(&done);
2343
2344 __ bind(&index_out_of_range);
2345 // When the index is out of range, the spec requires us to return
2346 // NaN.
2347 __ LoadRoot(result, Heap::kNanValueRootIndex);
2348 __ jmp(&done);
2349
2350 __ bind(&need_conversion);
2351 // Load the undefined value into the result register, which will
2352 // trigger conversion.
2353 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2354 __ jmp(&done);
2355
2356 NopRuntimeCallHelper call_helper;
2357 generator.GenerateSlow(masm_, call_helper);
2358
2359 __ bind(&done);
2360 Apply(context_, result);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002361}
2362
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002363
2364void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
2365 ASSERT(args->length() == 2);
2366
2367 VisitForValue(args->at(0), kStack);
2368 VisitForValue(args->at(1), kAccumulator);
2369
2370 Register object = r1;
2371 Register index = r0;
2372 Register scratch1 = r2;
2373 Register scratch2 = r3;
2374 Register result = r0;
2375
2376 __ pop(object);
2377
2378 Label need_conversion;
2379 Label index_out_of_range;
2380 Label done;
2381 StringCharAtGenerator generator(object,
2382 index,
2383 scratch1,
2384 scratch2,
2385 result,
2386 &need_conversion,
2387 &need_conversion,
2388 &index_out_of_range,
2389 STRING_INDEX_IS_NUMBER);
2390 generator.GenerateFast(masm_);
2391 __ jmp(&done);
2392
2393 __ bind(&index_out_of_range);
2394 // When the index is out of range, the spec requires us to return
2395 // the empty string.
2396 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2397 __ jmp(&done);
2398
2399 __ bind(&need_conversion);
2400 // Move smi zero into the result register, which will trigger
2401 // conversion.
2402 __ mov(result, Operand(Smi::FromInt(0)));
2403 __ jmp(&done);
2404
2405 NopRuntimeCallHelper call_helper;
2406 generator.GenerateSlow(masm_, call_helper);
2407
2408 __ bind(&done);
2409 Apply(context_, result);
2410}
2411
2412
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002413void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2414 ASSERT_EQ(2, args->length());
2415
2416 VisitForValue(args->at(0), kStack);
2417 VisitForValue(args->at(1), kStack);
2418
2419 StringAddStub stub(NO_STRING_ADD_FLAGS);
2420 __ CallStub(&stub);
2421 Apply(context_, r0);
2422}
2423
2424
2425void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
2426 ASSERT_EQ(2, args->length());
2427
2428 VisitForValue(args->at(0), kStack);
2429 VisitForValue(args->at(1), kStack);
2430
2431 StringCompareStub stub;
2432 __ CallStub(&stub);
2433 Apply(context_, r0);
2434}
2435
2436
2437void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
2438 // Load the argument on the stack and call the runtime.
2439 ASSERT(args->length() == 1);
2440 VisitForValue(args->at(0), kStack);
2441 __ CallRuntime(Runtime::kMath_sin, 1);
2442 Apply(context_, r0);
2443}
2444
2445
2446void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
2447 // Load the argument on the stack and call the runtime.
2448 ASSERT(args->length() == 1);
2449 VisitForValue(args->at(0), kStack);
2450 __ CallRuntime(Runtime::kMath_cos, 1);
2451 Apply(context_, r0);
2452}
2453
2454
2455void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2456 // Load the argument on the stack and call the runtime function.
2457 ASSERT(args->length() == 1);
2458 VisitForValue(args->at(0), kStack);
2459 __ CallRuntime(Runtime::kMath_sqrt, 1);
2460 Apply(context_, r0);
2461}
2462
2463
2464void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2465 ASSERT(args->length() >= 2);
2466
2467 int arg_count = args->length() - 2; // For receiver and function.
2468 VisitForValue(args->at(0), kStack); // Receiver.
2469 for (int i = 0; i < arg_count; i++) {
2470 VisitForValue(args->at(i + 1), kStack);
2471 }
2472 VisitForValue(args->at(arg_count + 1), kAccumulator); // Function.
2473
2474 // InvokeFunction requires function in r1. Move it in there.
2475 if (!result_register().is(r1)) __ mov(r1, result_register());
2476 ParameterCount count(arg_count);
2477 __ InvokeFunction(r1, count, CALL_FUNCTION);
2478 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2479 Apply(context_, r0);
2480}
2481
2482
2483void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2484 ASSERT(args->length() == 3);
2485 VisitForValue(args->at(0), kStack);
2486 VisitForValue(args->at(1), kStack);
2487 VisitForValue(args->at(2), kStack);
2488 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
2489 Apply(context_, r0);
2490}
2491
2492
2493void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2494 ASSERT(args->length() == 3);
2495 VisitForValue(args->at(0), kStack);
2496 VisitForValue(args->at(1), kStack);
2497 VisitForValue(args->at(2), kStack);
2498 __ CallRuntime(Runtime::kSwapElements, 3);
2499 Apply(context_, r0);
2500}
2501
2502
2503void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2504 ASSERT_EQ(2, args->length());
2505
2506 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2507 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2508
2509 Handle<FixedArray> jsfunction_result_caches(
2510 Top::global_context()->jsfunction_result_caches());
2511 if (jsfunction_result_caches->length() <= cache_id) {
2512 __ Abort("Attempt to use undefined cache.");
2513 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2514 Apply(context_, r0);
2515 return;
2516 }
2517
2518 VisitForValue(args->at(1), kAccumulator);
2519
2520 Register key = r0;
2521 Register cache = r1;
2522 __ ldr(cache, CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX));
2523 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
2524 __ ldr(cache,
2525 CodeGenerator::ContextOperand(
2526 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
2527 __ ldr(cache,
2528 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2529
2530
2531 Label done, not_found;
2532 // tmp now holds finger offset as a smi.
2533 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2534 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2535 // r2 now holds finger offset as a smi.
2536 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2537 // r3 now points to the start of fixed array elements.
2538 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2539 // Note side effect of PreIndex: r3 now points to the key of the pair.
2540 __ cmp(key, r2);
2541 __ b(ne, &not_found);
2542
2543 __ ldr(r0, MemOperand(r3, kPointerSize));
2544 __ b(&done);
2545
2546 __ bind(&not_found);
2547 // Call runtime to perform the lookup.
2548 __ Push(cache, key);
2549 __ CallRuntime(Runtime::kGetFromCache, 2);
2550
2551 __ bind(&done);
2552 Apply(context_, r0);
2553}
2554
2555
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002556void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002557 Handle<String> name = expr->name();
2558 if (name->length() > 0 && name->Get(0) == '_') {
2559 Comment cmnt(masm_, "[ InlineRuntimeCall");
2560 EmitInlineRuntimeCall(expr);
2561 return;
2562 }
2563
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002564 Comment cmnt(masm_, "[ CallRuntime");
2565 ZoneList<Expression*>* args = expr->arguments();
2566
2567 if (expr->is_jsruntime()) {
2568 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002569 __ ldr(r0, CodeGenerator::GlobalObject());
2570 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002571 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002572 }
2573
2574 // Push the arguments ("left-to-right").
2575 int arg_count = args->length();
2576 for (int i = 0; i < arg_count; i++) {
2577 VisitForValue(args->at(i), kStack);
2578 }
2579
2580 if (expr->is_jsruntime()) {
2581 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002582 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002583 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2584 NOT_IN_LOOP);
2585 __ Call(ic, RelocInfo::CODE_TARGET);
2586 // Restore context register.
2587 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002588 } else {
2589 // Call the C runtime function.
2590 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002591 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002592 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002593}
2594
2595
2596void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2597 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002598 case Token::DELETE: {
2599 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2600 Property* prop = expr->expression()->AsProperty();
2601 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2602 if (prop == NULL && var == NULL) {
2603 // Result of deleting non-property, non-variable reference is true.
2604 // The subexpression may have side effects.
2605 VisitForEffect(expr->expression());
2606 Apply(context_, true);
2607 } else if (var != NULL &&
2608 !var->is_global() &&
2609 var->slot() != NULL &&
2610 var->slot()->type() != Slot::LOOKUP) {
2611 // Result of deleting non-global, non-dynamic variables is false.
2612 // The subexpression does not have side effects.
2613 Apply(context_, false);
2614 } else {
2615 // Property or variable reference. Call the delete builtin with
2616 // object and property name as arguments.
2617 if (prop != NULL) {
2618 VisitForValue(prop->obj(), kStack);
2619 VisitForValue(prop->key(), kStack);
2620 } else if (var->is_global()) {
2621 __ ldr(r1, CodeGenerator::GlobalObject());
2622 __ mov(r0, Operand(var->name()));
2623 __ Push(r1, r0);
2624 } else {
2625 // Non-global variable. Call the runtime to look up the context
2626 // where the variable was introduced.
2627 __ push(context_register());
2628 __ mov(r2, Operand(var->name()));
2629 __ push(r2);
2630 __ CallRuntime(Runtime::kLookupContext, 2);
2631 __ push(r0);
2632 __ mov(r2, Operand(var->name()));
2633 __ push(r2);
2634 }
2635 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
2636 Apply(context_, r0);
2637 }
2638 break;
2639 }
2640
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002641 case Token::VOID: {
2642 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2643 VisitForEffect(expr->expression());
2644 switch (context_) {
2645 case Expression::kUninitialized:
2646 UNREACHABLE();
2647 break;
2648 case Expression::kEffect:
2649 break;
2650 case Expression::kValue:
2651 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2652 switch (location_) {
2653 case kAccumulator:
2654 break;
2655 case kStack:
2656 __ push(result_register());
2657 break;
2658 }
2659 break;
2660 case Expression::kTestValue:
2661 // Value is false so it's needed.
2662 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2663 switch (location_) {
2664 case kAccumulator:
2665 break;
2666 case kStack:
2667 __ push(result_register());
2668 break;
2669 }
2670 // Fall through.
2671 case Expression::kTest:
2672 case Expression::kValueTest:
2673 __ jmp(false_label_);
2674 break;
2675 }
2676 break;
2677 }
2678
2679 case Token::NOT: {
2680 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002681 Label materialize_true, materialize_false;
2682 Label* if_true = NULL;
2683 Label* if_false = NULL;
2684
2685 // Notice that the labels are swapped.
2686 PrepareTest(&materialize_true, &materialize_false, &if_false, &if_true);
2687
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002688 VisitForControl(expr->expression(), if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002689
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002690 Apply(context_, if_false, if_true); // Labels swapped.
2691 break;
2692 }
2693
2694 case Token::TYPEOF: {
2695 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
2696 VariableProxy* proxy = expr->expression()->AsVariableProxy();
2697 if (proxy != NULL &&
2698 !proxy->var()->is_this() &&
2699 proxy->var()->is_global()) {
2700 Comment cmnt(masm_, "Global variable");
2701 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002702 __ mov(r2, Operand(proxy->name()));
2703 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2704 // Use a regular load, not a contextual load, to avoid a reference
2705 // error.
2706 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002707 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002708 } else if (proxy != NULL &&
2709 proxy->var()->slot() != NULL &&
2710 proxy->var()->slot()->type() == Slot::LOOKUP) {
2711 __ mov(r0, Operand(proxy->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002712 __ Push(cp, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002713 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
2714 __ push(r0);
2715 } else {
2716 // This expression cannot throw a reference error at the top level.
2717 VisitForValue(expr->expression(), kStack);
2718 }
2719
2720 __ CallRuntime(Runtime::kTypeof, 1);
2721 Apply(context_, r0);
2722 break;
2723 }
2724
2725 case Token::ADD: {
2726 Comment cmt(masm_, "[ UnaryOperation (ADD)");
2727 VisitForValue(expr->expression(), kAccumulator);
2728 Label no_conversion;
2729 __ tst(result_register(), Operand(kSmiTagMask));
2730 __ b(eq, &no_conversion);
2731 __ push(r0);
2732 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2733 __ bind(&no_conversion);
2734 Apply(context_, result_register());
2735 break;
2736 }
2737
2738 case Token::SUB: {
2739 Comment cmt(masm_, "[ UnaryOperation (SUB)");
2740 bool overwrite =
2741 (expr->expression()->AsBinaryOperation() != NULL &&
2742 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
2743 GenericUnaryOpStub stub(Token::SUB, overwrite);
2744 // GenericUnaryOpStub expects the argument to be in the
2745 // accumulator register r0.
2746 VisitForValue(expr->expression(), kAccumulator);
2747 __ CallStub(&stub);
2748 Apply(context_, r0);
2749 break;
2750 }
2751
2752 case Token::BIT_NOT: {
2753 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
2754 bool overwrite =
2755 (expr->expression()->AsBinaryOperation() != NULL &&
2756 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
2757 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
2758 // GenericUnaryOpStub expects the argument to be in the
2759 // accumulator register r0.
2760 VisitForValue(expr->expression(), kAccumulator);
2761 // Avoid calling the stub for Smis.
2762 Label smi, done;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002763 __ BranchOnSmi(result_register(), &smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002764 // Non-smi: call stub leaving result in accumulator register.
2765 __ CallStub(&stub);
2766 __ b(&done);
2767 // Perform operation directly on Smis.
2768 __ bind(&smi);
2769 __ mvn(result_register(), Operand(result_register()));
2770 // Bit-clear inverted smi-tag.
2771 __ bic(result_register(), result_register(), Operand(kSmiTagMask));
2772 __ bind(&done);
2773 Apply(context_, result_register());
2774 break;
2775 }
2776
2777 default:
2778 UNREACHABLE();
2779 }
2780}
2781
2782
2783void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2784 Comment cmnt(masm_, "[ CountOperation");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002785 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2786 // as the left-hand side.
2787 if (!expr->expression()->IsValidLeftHandSide()) {
2788 VisitForEffect(expr->expression());
2789 return;
2790 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002791
2792 // Expression can only be a property, a global or a (parameter or local)
2793 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2794 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2795 LhsKind assign_type = VARIABLE;
2796 Property* prop = expr->expression()->AsProperty();
2797 // In case of a property we use the uninitialized expression context
2798 // of the key to detect a named property.
2799 if (prop != NULL) {
2800 assign_type =
2801 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2802 }
2803
2804 // Evaluate expression and get value.
2805 if (assign_type == VARIABLE) {
2806 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
2807 Location saved_location = location_;
2808 location_ = kAccumulator;
2809 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
2810 Expression::kValue);
2811 location_ = saved_location;
2812 } else {
2813 // Reserve space for result of postfix operation.
2814 if (expr->is_postfix() && context_ != Expression::kEffect) {
2815 __ mov(ip, Operand(Smi::FromInt(0)));
2816 __ push(ip);
2817 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002818 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002819 // Put the object both on the stack and in the accumulator.
2820 VisitForValue(prop->obj(), kAccumulator);
2821 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002822 EmitNamedPropertyLoad(prop);
2823 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002824 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002825 VisitForValue(prop->key(), kAccumulator);
2826 __ ldr(r1, MemOperand(sp, 0));
2827 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002828 EmitKeyedPropertyLoad(prop);
2829 }
2830 }
2831
2832 // Call ToNumber only if operand is not a smi.
2833 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002834 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002835 __ push(r0);
2836 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2837 __ bind(&no_conversion);
2838
2839 // Save result for postfix expressions.
2840 if (expr->is_postfix()) {
2841 switch (context_) {
2842 case Expression::kUninitialized:
2843 UNREACHABLE();
2844 case Expression::kEffect:
2845 // Do not save result.
2846 break;
2847 case Expression::kValue:
2848 case Expression::kTest:
2849 case Expression::kValueTest:
2850 case Expression::kTestValue:
2851 // Save the result on the stack. If we have a named or keyed property
2852 // we store the result under the receiver that is currently on top
2853 // of the stack.
2854 switch (assign_type) {
2855 case VARIABLE:
2856 __ push(r0);
2857 break;
2858 case NAMED_PROPERTY:
2859 __ str(r0, MemOperand(sp, kPointerSize));
2860 break;
2861 case KEYED_PROPERTY:
2862 __ str(r0, MemOperand(sp, 2 * kPointerSize));
2863 break;
2864 }
2865 break;
2866 }
2867 }
2868
2869
2870 // Inline smi case if we are in a loop.
2871 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002872 int count_value = expr->op() == Token::INC ? 1 : -1;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002873 if (loop_depth() > 0) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002874 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002875 __ b(vs, &stub_call);
2876 // We could eliminate this smi check if we split the code at
2877 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002878 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002879 __ bind(&stub_call);
2880 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002881 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002882 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002883 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00002884 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002885 __ CallStub(&stub);
2886 __ bind(&done);
2887
2888 // Store the value returned in r0.
2889 switch (assign_type) {
2890 case VARIABLE:
2891 if (expr->is_postfix()) {
2892 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002893 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002894 Expression::kEffect);
2895 // For all contexts except kEffect: We have the result on
2896 // top of the stack.
2897 if (context_ != Expression::kEffect) {
2898 ApplyTOS(context_);
2899 }
2900 } else {
2901 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002902 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002903 context_);
2904 }
2905 break;
2906 case NAMED_PROPERTY: {
2907 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00002908 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002909 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2910 __ Call(ic, RelocInfo::CODE_TARGET);
2911 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002912 if (context_ != Expression::kEffect) {
2913 ApplyTOS(context_);
2914 }
2915 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00002916 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002917 }
2918 break;
2919 }
2920 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002921 __ pop(r1); // Key.
2922 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002923 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
2924 __ Call(ic, RelocInfo::CODE_TARGET);
2925 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002926 if (context_ != Expression::kEffect) {
2927 ApplyTOS(context_);
2928 }
2929 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002930 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002931 }
2932 break;
2933 }
2934 }
2935}
2936
2937
2938void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
2939 Comment cmnt(masm_, "[ BinaryOperation");
2940 switch (expr->op()) {
2941 case Token::COMMA:
2942 VisitForEffect(expr->left());
2943 Visit(expr->right());
2944 break;
2945
2946 case Token::OR:
2947 case Token::AND:
2948 EmitLogicalOperation(expr);
2949 break;
2950
2951 case Token::ADD:
2952 case Token::SUB:
2953 case Token::DIV:
2954 case Token::MOD:
2955 case Token::MUL:
2956 case Token::BIT_OR:
2957 case Token::BIT_AND:
2958 case Token::BIT_XOR:
2959 case Token::SHL:
2960 case Token::SHR:
2961 case Token::SAR:
2962 VisitForValue(expr->left(), kStack);
2963 VisitForValue(expr->right(), kAccumulator);
2964 EmitBinaryOp(expr->op(), context_);
2965 break;
2966
2967 default:
2968 UNREACHABLE();
2969 }
2970}
2971
2972
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002973void FullCodeGenerator::EmitNullCompare(bool strict,
2974 Register obj,
2975 Register null_const,
2976 Label* if_true,
2977 Label* if_false,
2978 Register scratch) {
2979 __ cmp(obj, null_const);
2980 if (strict) {
2981 __ b(eq, if_true);
2982 } else {
2983 __ b(eq, if_true);
2984 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2985 __ cmp(obj, ip);
2986 __ b(eq, if_true);
2987 __ BranchOnSmi(obj, if_false);
2988 // It can be an undetectable object.
2989 __ ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
2990 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
2991 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
2992 __ b(ne, if_true);
2993 }
2994 __ jmp(if_false);
2995}
2996
2997
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002998void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
2999 Comment cmnt(masm_, "[ CompareOperation");
3000
3001 // Always perform the comparison for its control flow. Pack the result
3002 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003003
3004 Label materialize_true, materialize_false;
3005 Label* if_true = NULL;
3006 Label* if_false = NULL;
3007 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003008
3009 VisitForValue(expr->left(), kStack);
3010 switch (expr->op()) {
3011 case Token::IN:
3012 VisitForValue(expr->right(), kStack);
3013 __ InvokeBuiltin(Builtins::IN, CALL_JS);
3014 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3015 __ cmp(r0, ip);
3016 __ b(eq, if_true);
3017 __ jmp(if_false);
3018 break;
3019
3020 case Token::INSTANCEOF: {
3021 VisitForValue(expr->right(), kStack);
3022 InstanceofStub stub;
3023 __ CallStub(&stub);
3024 __ tst(r0, r0);
3025 __ b(eq, if_true); // The stub returns 0 for true.
3026 __ jmp(if_false);
3027 break;
3028 }
3029
3030 default: {
3031 VisitForValue(expr->right(), kAccumulator);
3032 Condition cc = eq;
3033 bool strict = false;
3034 switch (expr->op()) {
3035 case Token::EQ_STRICT:
3036 strict = true;
3037 // Fall through
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003038 case Token::EQ: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003039 cc = eq;
3040 __ pop(r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003041 // If either operand is constant null we do a fast compare
3042 // against null.
3043 Literal* right_literal = expr->right()->AsLiteral();
3044 Literal* left_literal = expr->left()->AsLiteral();
3045 if (right_literal != NULL && right_literal->handle()->IsNull()) {
3046 EmitNullCompare(strict, r1, r0, if_true, if_false, r2);
3047 Apply(context_, if_true, if_false);
3048 return;
3049 } else if (left_literal != NULL && left_literal->handle()->IsNull()) {
3050 EmitNullCompare(strict, r0, r1, if_true, if_false, r2);
3051 Apply(context_, if_true, if_false);
3052 return;
3053 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003054 break;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003055 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003056 case Token::LT:
3057 cc = lt;
3058 __ pop(r1);
3059 break;
3060 case Token::GT:
3061 // Reverse left and right sides to obtain ECMA-262 conversion order.
3062 cc = lt;
3063 __ mov(r1, result_register());
3064 __ pop(r0);
3065 break;
3066 case Token::LTE:
3067 // Reverse left and right sides to obtain ECMA-262 conversion order.
3068 cc = ge;
3069 __ mov(r1, result_register());
3070 __ pop(r0);
3071 break;
3072 case Token::GTE:
3073 cc = ge;
3074 __ pop(r1);
3075 break;
3076 case Token::IN:
3077 case Token::INSTANCEOF:
3078 default:
3079 UNREACHABLE();
3080 }
3081
3082 // The comparison stub expects the smi vs. smi case to be handled
3083 // before it is called.
3084 Label slow_case;
3085 __ orr(r2, r0, Operand(r1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003086 __ BranchOnNotSmi(r2, &slow_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003087 __ cmp(r1, r0);
3088 __ b(cc, if_true);
3089 __ jmp(if_false);
3090
3091 __ bind(&slow_case);
3092 CompareStub stub(cc, strict);
3093 __ CallStub(&stub);
3094 __ cmp(r0, Operand(0));
3095 __ b(cc, if_true);
3096 __ jmp(if_false);
3097 }
3098 }
3099
3100 // Convert the result of the comparison into one expected for this
3101 // expression's context.
3102 Apply(context_, if_true, if_false);
3103}
3104
3105
3106void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3107 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3108 Apply(context_, r0);
3109}
3110
3111
3112Register FullCodeGenerator::result_register() { return r0; }
3113
3114
3115Register FullCodeGenerator::context_register() { return cp; }
3116
3117
3118void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3119 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3120 __ str(value, MemOperand(fp, frame_offset));
3121}
3122
3123
3124void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
3125 __ ldr(dst, CodeGenerator::ContextOperand(cp, context_index));
3126}
3127
3128
3129// ----------------------------------------------------------------------------
3130// Non-local control flow support.
3131
3132void FullCodeGenerator::EnterFinallyBlock() {
3133 ASSERT(!result_register().is(r1));
3134 // Store result register while executing finally block.
3135 __ push(result_register());
3136 // Cook return address in link register to stack (smi encoded Code* delta)
3137 __ sub(r1, lr, Operand(masm_->CodeObject()));
3138 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3139 ASSERT_EQ(0, kSmiTag);
3140 __ add(r1, r1, Operand(r1)); // Convert to smi.
3141 __ push(r1);
3142}
3143
3144
3145void FullCodeGenerator::ExitFinallyBlock() {
3146 ASSERT(!result_register().is(r1));
3147 // Restore result register from stack.
3148 __ pop(r1);
3149 // Uncook return address and return.
3150 __ pop(result_register());
3151 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3152 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3153 __ add(pc, r1, Operand(masm_->CodeObject()));
3154}
3155
3156
3157#undef __
3158
3159} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003160
3161#endif // V8_TARGET_ARCH_ARM