blob: 673287388a20d1012be2808e3134d5f9a8d35453 [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
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000113 // registers, so we have to use two more registers to avoid
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000114 // clobbering cp.
115 __ mov(r2, Operand(cp));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000116 __ RecordWrite(r2, Operand(r1), r3, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000117 }
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) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000669 __ RecordWrite(scratch1,
670 Operand(Context::SlotOffset(dst->index())),
671 scratch2,
672 src);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000673 }
674}
675
676
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000677void FullCodeGenerator::EmitDeclaration(Variable* variable,
678 Variable::Mode mode,
679 FunctionLiteral* function) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000680 Comment cmnt(masm_, "[ Declaration");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000681 ASSERT(variable != NULL); // Must have been resolved.
682 Slot* slot = variable->slot();
683 Property* prop = variable->AsProperty();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000684
685 if (slot != NULL) {
686 switch (slot->type()) {
687 case Slot::PARAMETER:
688 case Slot::LOCAL:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000689 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000690 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
691 __ str(ip, MemOperand(fp, SlotOffset(slot)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000692 } else if (function != NULL) {
693 VisitForValue(function, kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000694 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
695 }
696 break;
697
698 case Slot::CONTEXT:
699 // We bypass the general EmitSlotSearch because we know more about
700 // this specific context.
701
702 // The variable in the decl always resides in the current context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000703 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000704 if (FLAG_debug_code) {
705 // Check if we have the correct context pointer.
706 __ ldr(r1,
707 CodeGenerator::ContextOperand(cp, Context::FCONTEXT_INDEX));
708 __ cmp(r1, cp);
709 __ Check(eq, "Unexpected declaration in current context.");
710 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000711 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000712 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
713 __ str(ip, CodeGenerator::ContextOperand(cp, slot->index()));
714 // No write barrier since the_hole_value is in old space.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000715 } else if (function != NULL) {
716 VisitForValue(function, kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000717 __ str(result_register(),
718 CodeGenerator::ContextOperand(cp, slot->index()));
719 int offset = Context::SlotOffset(slot->index());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000720 // We know that we have written a function, which is not a smi.
721 __ mov(r1, Operand(cp));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000722 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000723 }
724 break;
725
726 case Slot::LOOKUP: {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000727 __ mov(r2, Operand(variable->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000728 // Declaration nodes are always introduced in one of two modes.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000729 ASSERT(mode == Variable::VAR ||
730 mode == Variable::CONST);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000731 PropertyAttributes attr =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000732 (mode == Variable::VAR) ? NONE : READ_ONLY;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000733 __ mov(r1, Operand(Smi::FromInt(attr)));
734 // Push initial value, if any.
735 // Note: For variables we must not push an initial value (such as
736 // 'undefined') because we may have a (legal) redeclaration and we
737 // must not destroy the current value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000738 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000739 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000740 __ Push(cp, r2, r1, r0);
741 } else if (function != NULL) {
742 __ Push(cp, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000743 // Push initial value for function declaration.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000744 VisitForValue(function, kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000745 } else {
746 __ mov(r0, Operand(Smi::FromInt(0))); // No initial value!
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000747 __ Push(cp, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000748 }
749 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
750 break;
751 }
752 }
753
754 } else if (prop != NULL) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000755 if (function != NULL || mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000756 // We are declaring a function or constant that rewrites to a
757 // property. Use (keyed) IC to set the initial value.
758 VisitForValue(prop->obj(), kStack);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000759 if (function != NULL) {
760 VisitForValue(prop->key(), kStack);
761 VisitForValue(function, kAccumulator);
762 __ pop(r1); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000763 } else {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000764 VisitForValue(prop->key(), kAccumulator);
765 __ mov(r1, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000766 __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex);
767 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000768 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000769
770 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
771 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000772 // Value in r0 is ignored (declarations are statements).
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000773 }
774 }
775}
776
777
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000778void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
779 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
780}
781
782
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000783void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
784 // Call the runtime to declare the globals.
785 // The context is the first argument.
786 __ mov(r1, Operand(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000787 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000788 __ Push(cp, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000789 __ CallRuntime(Runtime::kDeclareGlobals, 3);
790 // Return value is ignored.
791}
792
793
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000794void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000795 Comment cmnt(masm_, "[ SwitchStatement");
796 Breakable nested_statement(this, stmt);
797 SetStatementPosition(stmt);
798 // Keep the switch value on the stack until a case matches.
799 VisitForValue(stmt->tag(), kStack);
800
801 ZoneList<CaseClause*>* clauses = stmt->cases();
802 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
803
804 Label next_test; // Recycled for each test.
805 // Compile all the tests with branches to their bodies.
806 for (int i = 0; i < clauses->length(); i++) {
807 CaseClause* clause = clauses->at(i);
808 // The default is not a test, but remember it as final fall through.
809 if (clause->is_default()) {
810 default_clause = clause;
811 continue;
812 }
813
814 Comment cmnt(masm_, "[ Case comparison");
815 __ bind(&next_test);
816 next_test.Unuse();
817
818 // Compile the label expression.
819 VisitForValue(clause->label(), kAccumulator);
820
821 // Perform the comparison as if via '==='. The comparison stub expects
822 // the smi vs. smi case to be handled before it is called.
823 Label slow_case;
824 __ ldr(r1, MemOperand(sp, 0)); // Switch value.
825 __ mov(r2, r1);
826 __ orr(r2, r2, r0);
827 __ tst(r2, Operand(kSmiTagMask));
828 __ b(ne, &slow_case);
829 __ cmp(r1, r0);
830 __ b(ne, &next_test);
831 __ Drop(1); // Switch value is no longer needed.
832 __ b(clause->body_target()->entry_label());
833
834 __ bind(&slow_case);
835 CompareStub stub(eq, true);
836 __ CallStub(&stub);
837 __ tst(r0, r0);
838 __ b(ne, &next_test);
839 __ Drop(1); // Switch value is no longer needed.
840 __ b(clause->body_target()->entry_label());
841 }
842
843 // Discard the test value and jump to the default if present, otherwise to
844 // the end of the statement.
845 __ bind(&next_test);
846 __ Drop(1); // Switch value is no longer needed.
847 if (default_clause == NULL) {
848 __ b(nested_statement.break_target());
849 } else {
850 __ b(default_clause->body_target()->entry_label());
851 }
852
853 // Compile all the case bodies.
854 for (int i = 0; i < clauses->length(); i++) {
855 Comment cmnt(masm_, "[ Case body");
856 CaseClause* clause = clauses->at(i);
857 __ bind(clause->body_target()->entry_label());
858 VisitStatements(clause->statements());
859 }
860
861 __ bind(nested_statement.break_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000862}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000863
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000864
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000865void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000866 Comment cmnt(masm_, "[ ForInStatement");
867 SetStatementPosition(stmt);
868
869 Label loop, exit;
870 ForIn loop_statement(this, stmt);
871 increment_loop_depth();
872
873 // Get the object to enumerate over. Both SpiderMonkey and JSC
874 // ignore null and undefined in contrast to the specification; see
875 // ECMA-262 section 12.6.4.
876 VisitForValue(stmt->enumerable(), kAccumulator);
877 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
878 __ cmp(r0, ip);
879 __ b(eq, &exit);
880 __ LoadRoot(ip, Heap::kNullValueRootIndex);
881 __ cmp(r0, ip);
882 __ b(eq, &exit);
883
884 // Convert the object to a JS object.
885 Label convert, done_convert;
886 __ BranchOnSmi(r0, &convert);
887 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
888 __ b(hs, &done_convert);
889 __ bind(&convert);
890 __ push(r0);
891 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
892 __ bind(&done_convert);
893 __ push(r0);
894
895 // TODO(kasperl): Check cache validity in generated code. This is a
896 // fast case for the JSObject::IsSimpleEnum cache validity
897 // checks. If we cannot guarantee cache validity, call the runtime
898 // system to check cache validity or get the property names in a
899 // fixed array.
900
901 // Get the set of properties to enumerate.
902 __ push(r0); // Duplicate the enumerable object on the stack.
903 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
904
905 // If we got a map from the runtime call, we can do a fast
906 // modification check. Otherwise, we got a fixed array, and we have
907 // to do a slow check.
908 Label fixed_array;
909 __ mov(r2, r0);
910 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
911 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
912 __ cmp(r1, ip);
913 __ b(ne, &fixed_array);
914
915 // We got a map in register r0. Get the enumeration cache from it.
916 __ ldr(r1, FieldMemOperand(r0, Map::kInstanceDescriptorsOffset));
917 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
918 __ ldr(r2, FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
919
920 // Setup the four remaining stack slots.
921 __ push(r0); // Map.
922 __ ldr(r1, FieldMemOperand(r2, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000923 __ mov(r0, Operand(Smi::FromInt(0)));
924 // Push enumeration cache, enumeration cache length (as smi) and zero.
925 __ Push(r2, r1, r0);
926 __ jmp(&loop);
927
928 // We got a fixed array in register r0. Iterate through that.
929 __ bind(&fixed_array);
930 __ mov(r1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
931 __ Push(r1, r0);
932 __ ldr(r1, FieldMemOperand(r0, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000933 __ mov(r0, Operand(Smi::FromInt(0)));
934 __ Push(r1, r0); // Fixed array length (as smi) and initial index.
935
936 // Generate code for doing the condition check.
937 __ bind(&loop);
938 // Load the current count to r0, load the length to r1.
939 __ Ldrd(r0, r1, MemOperand(sp, 0 * kPointerSize));
940 __ cmp(r0, r1); // Compare to the array length.
941 __ b(hs, loop_statement.break_target());
942
943 // Get the current entry of the array into register r3.
944 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
945 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
946 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
947
948 // Get the expected map from the stack or a zero map in the
949 // permanent slow case into register r2.
950 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
951
952 // Check if the expected map still matches that of the enumerable.
953 // If not, we have to filter the key.
954 Label update_each;
955 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
956 __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
957 __ cmp(r4, Operand(r2));
958 __ b(eq, &update_each);
959
960 // Convert the entry to a string or null if it isn't a property
961 // anymore. If the property has been removed while iterating, we
962 // just skip it.
963 __ push(r1); // Enumerable.
964 __ push(r3); // Current entry.
965 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
966 __ mov(r3, Operand(r0));
967 __ LoadRoot(ip, Heap::kNullValueRootIndex);
968 __ cmp(r3, ip);
969 __ b(eq, loop_statement.continue_target());
970
971 // Update the 'each' property or variable from the possibly filtered
972 // entry in register r3.
973 __ bind(&update_each);
974 __ mov(result_register(), r3);
975 // Perform the assignment as if via '='.
976 EmitAssignment(stmt->each());
977
978 // Generate code for the body of the loop.
979 Label stack_limit_hit, stack_check_done;
980 Visit(stmt->body());
981
982 __ StackLimitCheck(&stack_limit_hit);
983 __ bind(&stack_check_done);
984
985 // Generate code for the going to the next element by incrementing
986 // the index (smi) stored on top of the stack.
987 __ bind(loop_statement.continue_target());
988 __ pop(r0);
989 __ add(r0, r0, Operand(Smi::FromInt(1)));
990 __ push(r0);
991 __ b(&loop);
992
993 // Slow case for the stack limit check.
994 StackCheckStub stack_check_stub;
995 __ bind(&stack_limit_hit);
996 __ CallStub(&stack_check_stub);
997 __ b(&stack_check_done);
998
999 // Remove the pointers stored on the stack.
1000 __ bind(loop_statement.break_target());
1001 __ Drop(5);
1002
1003 // Exit and decrement the loop depth.
1004 __ bind(&exit);
1005 decrement_loop_depth();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001006}
1007
1008
1009void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info) {
1010 // Use the fast case closure allocation code that allocates in new
1011 // space for nested functions that don't need literals cloning.
1012 if (scope()->is_function_scope() && info->num_literals() == 0) {
1013 FastNewClosureStub stub;
1014 __ mov(r0, Operand(info));
1015 __ push(r0);
1016 __ CallStub(&stub);
1017 } else {
1018 __ mov(r0, Operand(info));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001019 __ Push(cp, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001020 __ CallRuntime(Runtime::kNewClosure, 2);
1021 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001022 Apply(context_, r0);
1023}
1024
1025
1026void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
1027 Comment cmnt(masm_, "[ VariableProxy");
1028 EmitVariableLoad(expr->var(), context_);
1029}
1030
1031
1032void FullCodeGenerator::EmitVariableLoad(Variable* var,
1033 Expression::Context context) {
1034 // Four cases: non-this global variables, lookup slots, all other
1035 // types of slots, and parameters that rewrite to explicit property
1036 // accesses on the arguments object.
1037 Slot* slot = var->slot();
1038 Property* property = var->AsProperty();
1039
1040 if (var->is_global() && !var->is_this()) {
1041 Comment cmnt(masm_, "Global variable");
1042 // Use inline caching. Variable name is passed in r2 and the global
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001043 // object (receiver) in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001044 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001045 __ mov(r2, Operand(var->name()));
1046 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1047 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001048 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001049
1050 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
1051 Comment cmnt(masm_, "Lookup slot");
1052 __ mov(r1, Operand(var->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001053 __ Push(cp, r1); // Context and name.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001054 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1055 Apply(context, r0);
1056
1057 } else if (slot != NULL) {
1058 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1059 ? "Context slot"
1060 : "Stack slot");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001061 if (var->mode() == Variable::CONST) {
1062 // Constants may be the hole value if they have not been initialized.
1063 // Unhole them.
1064 Label done;
1065 MemOperand slot_operand = EmitSlotSearch(slot, r0);
1066 __ ldr(r0, slot_operand);
1067 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1068 __ cmp(r0, ip);
1069 __ b(ne, &done);
1070 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1071 __ bind(&done);
1072 Apply(context, r0);
1073 } else {
1074 Apply(context, slot);
1075 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001076 } else {
1077 Comment cmnt(masm_, "Rewritten parameter");
1078 ASSERT_NOT_NULL(property);
1079 // Rewritten parameter accesses are of the form "slot[literal]".
1080
1081 // Assert that the object is in a slot.
1082 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
1083 ASSERT_NOT_NULL(object_var);
1084 Slot* object_slot = object_var->slot();
1085 ASSERT_NOT_NULL(object_slot);
1086
1087 // Load the object.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001088 Move(r1, object_slot);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001089
1090 // Assert that the key is a smi.
1091 Literal* key_literal = property->key()->AsLiteral();
1092 ASSERT_NOT_NULL(key_literal);
1093 ASSERT(key_literal->handle()->IsSmi());
1094
1095 // Load the key.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001096 __ mov(r0, Operand(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001097
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001098 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001099 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1100 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001101 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001102 }
1103}
1104
1105
1106void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1107 Comment cmnt(masm_, "[ RegExpLiteral");
1108 Label done;
1109 // Registers will be used as follows:
1110 // r4 = JS function, literals array
1111 // r3 = literal index
1112 // r2 = RegExp pattern
1113 // r1 = RegExp flags
1114 // r0 = temp + return value (RegExp literal)
1115 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1116 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
1117 int literal_offset =
1118 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1119 __ ldr(r0, FieldMemOperand(r4, literal_offset));
1120 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1121 __ cmp(r0, ip);
1122 __ b(ne, &done);
1123 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
1124 __ mov(r2, Operand(expr->pattern()));
1125 __ mov(r1, Operand(expr->flags()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001126 __ Push(r4, r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001127 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1128 __ bind(&done);
1129 Apply(context_, r0);
1130}
1131
1132
1133void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1134 Comment cmnt(masm_, "[ ObjectLiteral");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001135 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1136 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1137 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1138 __ mov(r1, Operand(expr->constant_properties()));
1139 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001140 __ Push(r3, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001141 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001142 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001143 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001144 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001145 }
1146
1147 // If result_saved is true the result is on top of the stack. If
1148 // result_saved is false the result is in r0.
1149 bool result_saved = false;
1150
1151 for (int i = 0; i < expr->properties()->length(); i++) {
1152 ObjectLiteral::Property* property = expr->properties()->at(i);
1153 if (property->IsCompileTimeValue()) continue;
1154
1155 Literal* key = property->key();
1156 Expression* value = property->value();
1157 if (!result_saved) {
1158 __ push(r0); // Save result on stack
1159 result_saved = true;
1160 }
1161 switch (property->kind()) {
1162 case ObjectLiteral::Property::CONSTANT:
1163 UNREACHABLE();
1164 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1165 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1166 // Fall through.
1167 case ObjectLiteral::Property::COMPUTED:
1168 if (key->handle()->IsSymbol()) {
1169 VisitForValue(value, kAccumulator);
1170 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001171 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001172 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1173 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001174 break;
1175 }
1176 // Fall through.
1177 case ObjectLiteral::Property::PROTOTYPE:
1178 // Duplicate receiver on stack.
1179 __ ldr(r0, MemOperand(sp));
1180 __ push(r0);
1181 VisitForValue(key, kStack);
1182 VisitForValue(value, kStack);
1183 __ CallRuntime(Runtime::kSetProperty, 3);
1184 break;
1185 case ObjectLiteral::Property::GETTER:
1186 case ObjectLiteral::Property::SETTER:
1187 // Duplicate receiver on stack.
1188 __ ldr(r0, MemOperand(sp));
1189 __ push(r0);
1190 VisitForValue(key, kStack);
1191 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1192 Smi::FromInt(1) :
1193 Smi::FromInt(0)));
1194 __ push(r1);
1195 VisitForValue(value, kStack);
1196 __ CallRuntime(Runtime::kDefineAccessor, 4);
1197 break;
1198 }
1199 }
1200
1201 if (result_saved) {
1202 ApplyTOS(context_);
1203 } else {
1204 Apply(context_, r0);
1205 }
1206}
1207
1208
1209void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1210 Comment cmnt(masm_, "[ ArrayLiteral");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001211
1212 ZoneList<Expression*>* subexprs = expr->values();
1213 int length = subexprs->length();
1214
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001215 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1216 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1217 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1218 __ mov(r1, Operand(expr->constant_elements()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001219 __ Push(r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001220 if (expr->depth() > 1) {
1221 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001222 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001223 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001224 } else {
1225 FastCloneShallowArrayStub stub(length);
1226 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001227 }
1228
1229 bool result_saved = false; // Is the result saved to the stack?
1230
1231 // Emit code to evaluate all the non-constant subexpressions and to store
1232 // them into the newly cloned array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001233 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001234 Expression* subexpr = subexprs->at(i);
1235 // If the subexpression is a literal or a simple materialized literal it
1236 // is already set in the cloned array.
1237 if (subexpr->AsLiteral() != NULL ||
1238 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1239 continue;
1240 }
1241
1242 if (!result_saved) {
1243 __ push(r0);
1244 result_saved = true;
1245 }
1246 VisitForValue(subexpr, kAccumulator);
1247
1248 // Store the subexpression value in the array's elements.
1249 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
1250 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1251 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1252 __ str(result_register(), FieldMemOperand(r1, offset));
1253
1254 // Update the write barrier for the array store with r0 as the scratch
1255 // register.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001256 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001257 }
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;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001496 __ RecordWrite(r1, Operand(offset), r2, r3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001497 break;
1498 }
1499
1500 case Slot::LOOKUP:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001501 // Call the runtime for the assignment. The runtime will ignore
1502 // const reinitialization.
1503 __ push(r0); // Value.
1504 __ mov(r0, Operand(slot->var()->name()));
1505 __ Push(cp, r0); // Context and name.
1506 if (op == Token::INIT_CONST) {
1507 // The runtime will ignore const redeclaration.
1508 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1509 } else {
1510 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1511 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001512 break;
1513 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001514 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001515 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001516
ager@chromium.org5c838252010-02-19 08:53:10 +00001517 Apply(context, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001518}
1519
1520
1521void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1522 // Assignment to a property, using a named store IC.
1523 Property* prop = expr->target()->AsProperty();
1524 ASSERT(prop != NULL);
1525 ASSERT(prop->key()->AsLiteral() != NULL);
1526
1527 // If the assignment starts a block of assignments to the same object,
1528 // change to slow case to avoid the quadratic behavior of repeatedly
1529 // adding fast properties.
1530 if (expr->starts_initialization_block()) {
1531 __ push(result_register());
1532 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1533 __ push(ip);
1534 __ CallRuntime(Runtime::kToSlowProperties, 1);
1535 __ pop(result_register());
1536 }
1537
1538 // Record source code position before IC call.
1539 SetSourcePosition(expr->position());
1540 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001541 // Load receiver to r1. Leave a copy in the stack if needed for turning the
1542 // receiver into fast case.
ager@chromium.org5c838252010-02-19 08:53:10 +00001543 if (expr->ends_initialization_block()) {
1544 __ ldr(r1, MemOperand(sp));
1545 } else {
1546 __ pop(r1);
1547 }
1548
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001549 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1550 __ Call(ic, RelocInfo::CODE_TARGET);
1551
1552 // If the assignment ends an initialization block, revert to fast case.
1553 if (expr->ends_initialization_block()) {
1554 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001555 // Receiver is under the result value.
1556 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001557 __ push(ip);
1558 __ CallRuntime(Runtime::kToFastProperties, 1);
1559 __ pop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001560 DropAndApply(1, context_, r0);
1561 } else {
1562 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001563 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001564}
1565
1566
1567void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1568 // Assignment to a property, using a keyed store IC.
1569
1570 // If the assignment starts a block of assignments to the same object,
1571 // change to slow case to avoid the quadratic behavior of repeatedly
1572 // adding fast properties.
1573 if (expr->starts_initialization_block()) {
1574 __ push(result_register());
1575 // Receiver is now under the key and value.
1576 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1577 __ push(ip);
1578 __ CallRuntime(Runtime::kToSlowProperties, 1);
1579 __ pop(result_register());
1580 }
1581
1582 // Record source code position before IC call.
1583 SetSourcePosition(expr->position());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001584 __ pop(r1); // Key.
1585 // Load receiver to r2. Leave a copy in the stack if needed for turning the
1586 // receiver into fast case.
1587 if (expr->ends_initialization_block()) {
1588 __ ldr(r2, MemOperand(sp));
1589 } else {
1590 __ pop(r2);
1591 }
1592
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001593 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1594 __ Call(ic, RelocInfo::CODE_TARGET);
1595
1596 // If the assignment ends an initialization block, revert to fast case.
1597 if (expr->ends_initialization_block()) {
1598 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001599 // Receiver is under the result value.
1600 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001601 __ push(ip);
1602 __ CallRuntime(Runtime::kToFastProperties, 1);
1603 __ pop(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001604 DropAndApply(1, context_, r0);
1605 } else {
1606 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001607 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001608}
1609
1610
1611void FullCodeGenerator::VisitProperty(Property* expr) {
1612 Comment cmnt(masm_, "[ Property");
1613 Expression* key = expr->key();
1614
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001615 if (key->IsPropertyName()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001616 VisitForValue(expr->obj(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001617 EmitNamedPropertyLoad(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001618 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001619 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001620 VisitForValue(expr->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001621 VisitForValue(expr->key(), kAccumulator);
1622 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001623 EmitKeyedPropertyLoad(expr);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001624 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001625 }
1626}
1627
1628void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001629 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001630 RelocInfo::Mode mode) {
1631 // Code common for calls using the IC.
1632 ZoneList<Expression*>* args = expr->arguments();
1633 int arg_count = args->length();
1634 for (int i = 0; i < arg_count; i++) {
1635 VisitForValue(args->at(i), kStack);
1636 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001637 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001638 // Record source position for debugger.
1639 SetSourcePosition(expr->position());
1640 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001641 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1642 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001643 __ Call(ic, mode);
1644 // Restore context register.
1645 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001646 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001647}
1648
1649
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001650void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
1651 Expression* key,
1652 RelocInfo::Mode mode) {
1653 // Code common for calls using the IC.
1654 ZoneList<Expression*>* args = expr->arguments();
1655 int arg_count = args->length();
1656 for (int i = 0; i < arg_count; i++) {
1657 VisitForValue(args->at(i), kStack);
1658 }
1659 VisitForValue(key, kAccumulator);
1660 __ mov(r2, r0);
1661 // Record source position for debugger.
1662 SetSourcePosition(expr->position());
1663 // Call the IC initialization code.
1664 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1665 Handle<Code> ic = CodeGenerator::ComputeKeyedCallInitialize(arg_count,
1666 in_loop);
1667 __ Call(ic, mode);
1668 // Restore context register.
1669 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1670 Apply(context_, r0);
1671}
1672
1673
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001674void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1675 // Code common for calls using the call stub.
1676 ZoneList<Expression*>* args = expr->arguments();
1677 int arg_count = args->length();
1678 for (int i = 0; i < arg_count; i++) {
1679 VisitForValue(args->at(i), kStack);
1680 }
1681 // Record source position for debugger.
1682 SetSourcePosition(expr->position());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001683 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1684 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001685 __ CallStub(&stub);
1686 // Restore context register.
1687 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001688 DropAndApply(1, context_, r0);
1689}
1690
1691
1692void FullCodeGenerator::VisitCall(Call* expr) {
1693 Comment cmnt(masm_, "[ Call");
1694 Expression* fun = expr->expression();
1695 Variable* var = fun->AsVariableProxy()->AsVariable();
1696
1697 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001698 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1699 // resolve the function we need to call and the receiver of the
1700 // call. Then we call the resolved function using the given
1701 // arguments.
1702 VisitForValue(fun, kStack);
1703 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1704 __ push(r2); // Reserved receiver slot.
1705
1706 // Push the arguments.
1707 ZoneList<Expression*>* args = expr->arguments();
1708 int arg_count = args->length();
1709 for (int i = 0; i < arg_count; i++) {
1710 VisitForValue(args->at(i), kStack);
1711 }
1712
1713 // Push copy of the function - found below the arguments.
1714 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1715 __ push(r1);
1716
1717 // Push copy of the first argument or undefined if it doesn't exist.
1718 if (arg_count > 0) {
1719 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1720 __ push(r1);
1721 } else {
1722 __ push(r2);
1723 }
1724
1725 // Push the receiver of the enclosing function and do runtime call.
1726 __ ldr(r1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1727 __ push(r1);
1728 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1729
1730 // The runtime call returns a pair of values in r0 (function) and
1731 // r1 (receiver). Touch up the stack with the right values.
1732 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1733 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
1734
1735 // Record source position for debugger.
1736 SetSourcePosition(expr->position());
1737 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1738 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1739 __ CallStub(&stub);
1740 // Restore context register.
1741 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1742 DropAndApply(1, context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001743 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001744 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001745 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001746 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001747 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1748 } else if (var != NULL && var->slot() != NULL &&
1749 var->slot()->type() == Slot::LOOKUP) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001750 // Call to a lookup slot (dynamically introduced variable). Call the
1751 // runtime to find the function to call (returned in eax) and the object
1752 // holding it (returned in edx).
1753 __ push(context_register());
1754 __ mov(r2, Operand(var->name()));
1755 __ push(r2);
1756 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1757 __ push(r0); // Function.
1758 __ push(r1); // Receiver.
1759 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001760 } else if (fun->AsProperty() != NULL) {
1761 // Call to an object property.
1762 Property* prop = fun->AsProperty();
1763 Literal* key = prop->key()->AsLiteral();
1764 if (key != NULL && key->handle()->IsSymbol()) {
1765 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001766 VisitForValue(prop->obj(), kStack);
1767 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1768 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001769 // Call to a keyed property.
1770 // For a synthetic property use keyed load IC followed by function call,
1771 // for a regular property use keyed CallIC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001772 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001773 if (prop->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001774 VisitForValue(prop->key(), kAccumulator);
1775 // Record source code position for IC call.
1776 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001777 __ pop(r1); // We do not need to keep the receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001778
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001779 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1780 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001781 // Push result (function).
1782 __ push(r0);
1783 // Push Global receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001784 __ ldr(r1, CodeGenerator::GlobalObject());
1785 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001786 __ push(r1);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001787 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001788 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001789 EmitKeyedCallWithIC(expr, prop->key(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001790 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001791 }
1792 } else {
1793 // Call to some other expression. If the expression is an anonymous
1794 // function literal not called in a loop, mark it as one that should
1795 // also use the fast code generator.
1796 FunctionLiteral* lit = fun->AsFunctionLiteral();
1797 if (lit != NULL &&
1798 lit->name()->Equals(Heap::empty_string()) &&
1799 loop_depth() == 0) {
1800 lit->set_try_full_codegen(true);
1801 }
1802 VisitForValue(fun, kStack);
1803 // Load global receiver object.
1804 __ ldr(r1, CodeGenerator::GlobalObject());
1805 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1806 __ push(r1);
1807 // Emit function call.
1808 EmitCallWithStub(expr);
1809 }
1810}
1811
1812
1813void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1814 Comment cmnt(masm_, "[ CallNew");
1815 // According to ECMA-262, section 11.2.2, page 44, the function
1816 // expression in new calls must be evaluated before the
1817 // arguments.
1818 // Push function on the stack.
1819 VisitForValue(expr->expression(), kStack);
1820
1821 // Push global object (receiver).
1822 __ ldr(r0, CodeGenerator::GlobalObject());
1823 __ push(r0);
1824 // Push the arguments ("left-to-right") on the stack.
1825 ZoneList<Expression*>* args = expr->arguments();
1826 int arg_count = args->length();
1827 for (int i = 0; i < arg_count; i++) {
1828 VisitForValue(args->at(i), kStack);
1829 }
1830
1831 // Call the construct call builtin that handles allocation and
1832 // constructor invocation.
1833 SetSourcePosition(expr->position());
1834
1835 // Load function, arg_count into r1 and r0.
1836 __ mov(r0, Operand(arg_count));
1837 // Function is in sp[arg_count + 1].
1838 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1839
1840 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1841 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
1842
1843 // Replace function on TOS with result in r0, or pop it.
1844 DropAndApply(1, context_, r0);
1845}
1846
1847
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001848void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1849 ASSERT(args->length() == 1);
1850
1851 VisitForValue(args->at(0), kAccumulator);
1852
1853 Label materialize_true, materialize_false;
1854 Label* if_true = NULL;
1855 Label* if_false = NULL;
1856 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1857
1858 __ BranchOnSmi(r0, if_true);
1859 __ b(if_false);
1860
1861 Apply(context_, if_true, if_false);
1862}
1863
1864
1865void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1866 ASSERT(args->length() == 1);
1867
1868 VisitForValue(args->at(0), kAccumulator);
1869
1870 Label materialize_true, materialize_false;
1871 Label* if_true = NULL;
1872 Label* if_false = NULL;
1873 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1874
1875 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
1876 __ b(eq, if_true);
1877 __ b(if_false);
1878
1879 Apply(context_, if_true, if_false);
1880}
1881
1882
1883void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1884 ASSERT(args->length() == 1);
1885
1886 VisitForValue(args->at(0), kAccumulator);
1887
1888 Label materialize_true, materialize_false;
1889 Label* if_true = NULL;
1890 Label* if_false = NULL;
1891 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1892 __ BranchOnSmi(r0, if_false);
1893 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1894 __ cmp(r0, ip);
1895 __ b(eq, if_true);
1896 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1897 // Undetectable objects behave like undefined when tested with typeof.
1898 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
1899 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1900 __ b(ne, if_false);
1901 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1902 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
1903 __ b(lt, if_false);
1904 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
1905 __ b(le, if_true);
1906 __ b(if_false);
1907
1908 Apply(context_, if_true, if_false);
1909}
1910
1911
1912void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
1913 ASSERT(args->length() == 1);
1914
1915 VisitForValue(args->at(0), kAccumulator);
1916
1917 Label materialize_true, materialize_false;
1918 Label* if_true = NULL;
1919 Label* if_false = NULL;
1920 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1921
1922 __ BranchOnSmi(r0, if_false);
1923 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1924 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
1925 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1926 __ b(ne, if_true);
1927 __ b(if_false);
1928
1929 Apply(context_, if_true, if_false);
1930}
1931
1932
1933void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
1934 ASSERT(args->length() == 1);
1935
1936 VisitForValue(args->at(0), kAccumulator);
1937
1938 Label materialize_true, materialize_false;
1939 Label* if_true = NULL;
1940 Label* if_false = NULL;
1941 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1942
1943 __ BranchOnSmi(r0, if_false);
1944 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
1945 __ b(eq, if_true);
1946 __ b(if_false);
1947
1948 Apply(context_, if_true, if_false);
1949}
1950
1951
1952void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
1953 ASSERT(args->length() == 1);
1954
1955 VisitForValue(args->at(0), kAccumulator);
1956
1957 Label materialize_true, materialize_false;
1958 Label* if_true = NULL;
1959 Label* if_false = NULL;
1960 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1961
1962 __ BranchOnSmi(r0, if_false);
1963 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
1964 __ b(eq, if_true);
1965 __ b(if_false);
1966
1967 Apply(context_, if_true, if_false);
1968}
1969
1970
1971void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
1972 ASSERT(args->length() == 1);
1973
1974 VisitForValue(args->at(0), kAccumulator);
1975
1976 Label materialize_true, materialize_false;
1977 Label* if_true = NULL;
1978 Label* if_false = NULL;
1979 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1980
1981 __ BranchOnSmi(r0, if_false);
1982 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
1983 __ b(eq, if_true);
1984 __ b(if_false);
1985
1986 Apply(context_, if_true, if_false);
1987}
1988
1989
1990
1991void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
1992 ASSERT(args->length() == 0);
1993
1994 Label materialize_true, materialize_false;
1995 Label* if_true = NULL;
1996 Label* if_false = NULL;
1997 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1998
1999 // Get the frame pointer for the calling frame.
2000 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2001
2002 // Skip the arguments adaptor frame if it exists.
2003 Label check_frame_marker;
2004 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
2005 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2006 __ b(ne, &check_frame_marker);
2007 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
2008
2009 // Check the marker in the calling frame.
2010 __ bind(&check_frame_marker);
2011 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
2012 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
2013 __ b(eq, if_true);
2014 __ b(if_false);
2015
2016 Apply(context_, if_true, if_false);
2017}
2018
2019
2020void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
2021 ASSERT(args->length() == 2);
2022
2023 // Load the two objects into registers and perform the comparison.
2024 VisitForValue(args->at(0), kStack);
2025 VisitForValue(args->at(1), kAccumulator);
2026
2027 Label materialize_true, materialize_false;
2028 Label* if_true = NULL;
2029 Label* if_false = NULL;
2030 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2031
2032 __ pop(r1);
2033 __ cmp(r0, r1);
2034 __ b(eq, if_true);
2035 __ b(if_false);
2036
2037 Apply(context_, if_true, if_false);
2038}
2039
2040
2041void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
2042 ASSERT(args->length() == 1);
2043
2044 // ArgumentsAccessStub expects the key in edx and the formal
2045 // parameter count in eax.
2046 VisitForValue(args->at(0), kAccumulator);
2047 __ mov(r1, r0);
2048 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2049 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2050 __ CallStub(&stub);
2051 Apply(context_, r0);
2052}
2053
2054
2055void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
2056 ASSERT(args->length() == 0);
2057
2058 Label exit;
2059 // Get the number of formal parameters.
2060 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2061
2062 // Check if the calling frame is an arguments adaptor frame.
2063 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2064 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
2065 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2066 __ b(ne, &exit);
2067
2068 // Arguments adaptor case: Read the arguments length from the
2069 // adaptor frame.
2070 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2071
2072 __ bind(&exit);
2073 Apply(context_, r0);
2074}
2075
2076
2077void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
2078 ASSERT(args->length() == 1);
2079 Label done, null, function, non_function_constructor;
2080
2081 VisitForValue(args->at(0), kAccumulator);
2082
2083 // If the object is a smi, we return null.
2084 __ BranchOnSmi(r0, &null);
2085
2086 // Check that the object is a JS object but take special care of JS
2087 // functions to make sure they have 'Function' as their class.
2088 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2089 __ b(lt, &null);
2090
2091 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2092 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2093 // LAST_JS_OBJECT_TYPE.
2094 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2095 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2096 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2097 __ b(eq, &function);
2098
2099 // Check if the constructor in the map is a function.
2100 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2101 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2102 __ b(ne, &non_function_constructor);
2103
2104 // r0 now contains the constructor function. Grab the
2105 // instance class name from there.
2106 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2107 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2108 __ b(&done);
2109
2110 // Functions have class 'Function'.
2111 __ bind(&function);
2112 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2113 __ jmp(&done);
2114
2115 // Objects with a non-function constructor have class 'Object'.
2116 __ bind(&non_function_constructor);
2117 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2118 __ jmp(&done);
2119
2120 // Non-JS objects have class null.
2121 __ bind(&null);
2122 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2123
2124 // All done.
2125 __ bind(&done);
2126
2127 Apply(context_, r0);
2128}
2129
2130
2131void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2132 // Conditionally generate a log call.
2133 // Args:
2134 // 0 (literal string): The type of logging (corresponds to the flags).
2135 // This is used to determine whether or not to generate the log call.
2136 // 1 (string): Format string. Access the string at argument index 2
2137 // with '%2s' (see Logger::LogRuntime for all the formats).
2138 // 2 (array): Arguments to the format string.
2139 ASSERT_EQ(args->length(), 3);
2140#ifdef ENABLE_LOGGING_AND_PROFILING
2141 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2142 VisitForValue(args->at(1), kStack);
2143 VisitForValue(args->at(2), kStack);
2144 __ CallRuntime(Runtime::kLog, 2);
2145 }
2146#endif
2147 // Finally, we're expected to leave a value on the top of the stack.
2148 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2149 Apply(context_, r0);
2150}
2151
2152
2153void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2154 ASSERT(args->length() == 0);
2155
2156 Label slow_allocate_heapnumber;
2157 Label heapnumber_allocated;
2158
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002159 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
2160 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002161 __ 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.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002279 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002280
2281 __ bind(&done);
2282 Apply(context_, r0);
2283}
2284
2285
2286void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2287 ASSERT_EQ(args->length(), 1);
2288
2289 // Load the argument on the stack and call the stub.
2290 VisitForValue(args->at(0), kStack);
2291
2292 NumberToStringStub stub;
2293 __ CallStub(&stub);
2294 Apply(context_, r0);
2295}
2296
2297
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002298void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002299 ASSERT(args->length() == 1);
2300
2301 VisitForValue(args->at(0), kAccumulator);
2302
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002303 Label done;
2304 StringCharFromCodeGenerator generator(r0, r1);
2305 generator.GenerateFast(masm_);
2306 __ jmp(&done);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002307
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002308 NopRuntimeCallHelper call_helper;
2309 generator.GenerateSlow(masm_, call_helper);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002310
2311 __ bind(&done);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002312 Apply(context_, r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002313}
2314
2315
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002316void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
2317 ASSERT(args->length() == 2);
2318
2319 VisitForValue(args->at(0), kStack);
2320 VisitForValue(args->at(1), kAccumulator);
2321
2322 Register object = r1;
2323 Register index = r0;
2324 Register scratch = r2;
2325 Register result = r3;
2326
2327 __ pop(object);
2328
2329 Label need_conversion;
2330 Label index_out_of_range;
2331 Label done;
2332 StringCharCodeAtGenerator generator(object,
2333 index,
2334 scratch,
2335 result,
2336 &need_conversion,
2337 &need_conversion,
2338 &index_out_of_range,
2339 STRING_INDEX_IS_NUMBER);
2340 generator.GenerateFast(masm_);
2341 __ jmp(&done);
2342
2343 __ bind(&index_out_of_range);
2344 // When the index is out of range, the spec requires us to return
2345 // NaN.
2346 __ LoadRoot(result, Heap::kNanValueRootIndex);
2347 __ jmp(&done);
2348
2349 __ bind(&need_conversion);
2350 // Load the undefined value into the result register, which will
2351 // trigger conversion.
2352 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2353 __ jmp(&done);
2354
2355 NopRuntimeCallHelper call_helper;
2356 generator.GenerateSlow(masm_, call_helper);
2357
2358 __ bind(&done);
2359 Apply(context_, result);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002360}
2361
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002362
2363void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
2364 ASSERT(args->length() == 2);
2365
2366 VisitForValue(args->at(0), kStack);
2367 VisitForValue(args->at(1), kAccumulator);
2368
2369 Register object = r1;
2370 Register index = r0;
2371 Register scratch1 = r2;
2372 Register scratch2 = r3;
2373 Register result = r0;
2374
2375 __ pop(object);
2376
2377 Label need_conversion;
2378 Label index_out_of_range;
2379 Label done;
2380 StringCharAtGenerator generator(object,
2381 index,
2382 scratch1,
2383 scratch2,
2384 result,
2385 &need_conversion,
2386 &need_conversion,
2387 &index_out_of_range,
2388 STRING_INDEX_IS_NUMBER);
2389 generator.GenerateFast(masm_);
2390 __ jmp(&done);
2391
2392 __ bind(&index_out_of_range);
2393 // When the index is out of range, the spec requires us to return
2394 // the empty string.
2395 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2396 __ jmp(&done);
2397
2398 __ bind(&need_conversion);
2399 // Move smi zero into the result register, which will trigger
2400 // conversion.
2401 __ mov(result, Operand(Smi::FromInt(0)));
2402 __ jmp(&done);
2403
2404 NopRuntimeCallHelper call_helper;
2405 generator.GenerateSlow(masm_, call_helper);
2406
2407 __ bind(&done);
2408 Apply(context_, result);
2409}
2410
2411
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002412void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2413 ASSERT_EQ(2, args->length());
2414
2415 VisitForValue(args->at(0), kStack);
2416 VisitForValue(args->at(1), kStack);
2417
2418 StringAddStub stub(NO_STRING_ADD_FLAGS);
2419 __ CallStub(&stub);
2420 Apply(context_, r0);
2421}
2422
2423
2424void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
2425 ASSERT_EQ(2, args->length());
2426
2427 VisitForValue(args->at(0), kStack);
2428 VisitForValue(args->at(1), kStack);
2429
2430 StringCompareStub stub;
2431 __ CallStub(&stub);
2432 Apply(context_, r0);
2433}
2434
2435
2436void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
2437 // Load the argument on the stack and call the runtime.
2438 ASSERT(args->length() == 1);
2439 VisitForValue(args->at(0), kStack);
2440 __ CallRuntime(Runtime::kMath_sin, 1);
2441 Apply(context_, r0);
2442}
2443
2444
2445void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
2446 // Load the argument on the stack and call the runtime.
2447 ASSERT(args->length() == 1);
2448 VisitForValue(args->at(0), kStack);
2449 __ CallRuntime(Runtime::kMath_cos, 1);
2450 Apply(context_, r0);
2451}
2452
2453
2454void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2455 // Load the argument on the stack and call the runtime function.
2456 ASSERT(args->length() == 1);
2457 VisitForValue(args->at(0), kStack);
2458 __ CallRuntime(Runtime::kMath_sqrt, 1);
2459 Apply(context_, r0);
2460}
2461
2462
2463void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2464 ASSERT(args->length() >= 2);
2465
2466 int arg_count = args->length() - 2; // For receiver and function.
2467 VisitForValue(args->at(0), kStack); // Receiver.
2468 for (int i = 0; i < arg_count; i++) {
2469 VisitForValue(args->at(i + 1), kStack);
2470 }
2471 VisitForValue(args->at(arg_count + 1), kAccumulator); // Function.
2472
2473 // InvokeFunction requires function in r1. Move it in there.
2474 if (!result_register().is(r1)) __ mov(r1, result_register());
2475 ParameterCount count(arg_count);
2476 __ InvokeFunction(r1, count, CALL_FUNCTION);
2477 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2478 Apply(context_, r0);
2479}
2480
2481
2482void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2483 ASSERT(args->length() == 3);
2484 VisitForValue(args->at(0), kStack);
2485 VisitForValue(args->at(1), kStack);
2486 VisitForValue(args->at(2), kStack);
2487 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
2488 Apply(context_, r0);
2489}
2490
2491
2492void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2493 ASSERT(args->length() == 3);
2494 VisitForValue(args->at(0), kStack);
2495 VisitForValue(args->at(1), kStack);
2496 VisitForValue(args->at(2), kStack);
2497 __ CallRuntime(Runtime::kSwapElements, 3);
2498 Apply(context_, r0);
2499}
2500
2501
2502void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2503 ASSERT_EQ(2, args->length());
2504
2505 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2506 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2507
2508 Handle<FixedArray> jsfunction_result_caches(
2509 Top::global_context()->jsfunction_result_caches());
2510 if (jsfunction_result_caches->length() <= cache_id) {
2511 __ Abort("Attempt to use undefined cache.");
2512 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2513 Apply(context_, r0);
2514 return;
2515 }
2516
2517 VisitForValue(args->at(1), kAccumulator);
2518
2519 Register key = r0;
2520 Register cache = r1;
2521 __ ldr(cache, CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX));
2522 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
2523 __ ldr(cache,
2524 CodeGenerator::ContextOperand(
2525 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
2526 __ ldr(cache,
2527 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2528
2529
2530 Label done, not_found;
2531 // tmp now holds finger offset as a smi.
2532 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2533 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2534 // r2 now holds finger offset as a smi.
2535 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2536 // r3 now points to the start of fixed array elements.
2537 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2538 // Note side effect of PreIndex: r3 now points to the key of the pair.
2539 __ cmp(key, r2);
2540 __ b(ne, &not_found);
2541
2542 __ ldr(r0, MemOperand(r3, kPointerSize));
2543 __ b(&done);
2544
2545 __ bind(&not_found);
2546 // Call runtime to perform the lookup.
2547 __ Push(cache, key);
2548 __ CallRuntime(Runtime::kGetFromCache, 2);
2549
2550 __ bind(&done);
2551 Apply(context_, r0);
2552}
2553
2554
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002555void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002556 Handle<String> name = expr->name();
2557 if (name->length() > 0 && name->Get(0) == '_') {
2558 Comment cmnt(masm_, "[ InlineRuntimeCall");
2559 EmitInlineRuntimeCall(expr);
2560 return;
2561 }
2562
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002563 Comment cmnt(masm_, "[ CallRuntime");
2564 ZoneList<Expression*>* args = expr->arguments();
2565
2566 if (expr->is_jsruntime()) {
2567 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002568 __ ldr(r0, CodeGenerator::GlobalObject());
2569 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002570 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002571 }
2572
2573 // Push the arguments ("left-to-right").
2574 int arg_count = args->length();
2575 for (int i = 0; i < arg_count; i++) {
2576 VisitForValue(args->at(i), kStack);
2577 }
2578
2579 if (expr->is_jsruntime()) {
2580 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002581 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002582 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2583 NOT_IN_LOOP);
2584 __ Call(ic, RelocInfo::CODE_TARGET);
2585 // Restore context register.
2586 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002587 } else {
2588 // Call the C runtime function.
2589 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002590 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002591 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002592}
2593
2594
2595void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2596 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002597 case Token::DELETE: {
2598 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2599 Property* prop = expr->expression()->AsProperty();
2600 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2601 if (prop == NULL && var == NULL) {
2602 // Result of deleting non-property, non-variable reference is true.
2603 // The subexpression may have side effects.
2604 VisitForEffect(expr->expression());
2605 Apply(context_, true);
2606 } else if (var != NULL &&
2607 !var->is_global() &&
2608 var->slot() != NULL &&
2609 var->slot()->type() != Slot::LOOKUP) {
2610 // Result of deleting non-global, non-dynamic variables is false.
2611 // The subexpression does not have side effects.
2612 Apply(context_, false);
2613 } else {
2614 // Property or variable reference. Call the delete builtin with
2615 // object and property name as arguments.
2616 if (prop != NULL) {
2617 VisitForValue(prop->obj(), kStack);
2618 VisitForValue(prop->key(), kStack);
2619 } else if (var->is_global()) {
2620 __ ldr(r1, CodeGenerator::GlobalObject());
2621 __ mov(r0, Operand(var->name()));
2622 __ Push(r1, r0);
2623 } else {
2624 // Non-global variable. Call the runtime to look up the context
2625 // where the variable was introduced.
2626 __ push(context_register());
2627 __ mov(r2, Operand(var->name()));
2628 __ push(r2);
2629 __ CallRuntime(Runtime::kLookupContext, 2);
2630 __ push(r0);
2631 __ mov(r2, Operand(var->name()));
2632 __ push(r2);
2633 }
2634 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
2635 Apply(context_, r0);
2636 }
2637 break;
2638 }
2639
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002640 case Token::VOID: {
2641 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2642 VisitForEffect(expr->expression());
2643 switch (context_) {
2644 case Expression::kUninitialized:
2645 UNREACHABLE();
2646 break;
2647 case Expression::kEffect:
2648 break;
2649 case Expression::kValue:
2650 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2651 switch (location_) {
2652 case kAccumulator:
2653 break;
2654 case kStack:
2655 __ push(result_register());
2656 break;
2657 }
2658 break;
2659 case Expression::kTestValue:
2660 // Value is false so it's needed.
2661 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2662 switch (location_) {
2663 case kAccumulator:
2664 break;
2665 case kStack:
2666 __ push(result_register());
2667 break;
2668 }
2669 // Fall through.
2670 case Expression::kTest:
2671 case Expression::kValueTest:
2672 __ jmp(false_label_);
2673 break;
2674 }
2675 break;
2676 }
2677
2678 case Token::NOT: {
2679 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002680 Label materialize_true, materialize_false;
2681 Label* if_true = NULL;
2682 Label* if_false = NULL;
2683
2684 // Notice that the labels are swapped.
2685 PrepareTest(&materialize_true, &materialize_false, &if_false, &if_true);
2686
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002687 VisitForControl(expr->expression(), if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002688
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002689 Apply(context_, if_false, if_true); // Labels swapped.
2690 break;
2691 }
2692
2693 case Token::TYPEOF: {
2694 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
2695 VariableProxy* proxy = expr->expression()->AsVariableProxy();
2696 if (proxy != NULL &&
2697 !proxy->var()->is_this() &&
2698 proxy->var()->is_global()) {
2699 Comment cmnt(masm_, "Global variable");
2700 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002701 __ mov(r2, Operand(proxy->name()));
2702 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2703 // Use a regular load, not a contextual load, to avoid a reference
2704 // error.
2705 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002706 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002707 } else if (proxy != NULL &&
2708 proxy->var()->slot() != NULL &&
2709 proxy->var()->slot()->type() == Slot::LOOKUP) {
2710 __ mov(r0, Operand(proxy->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002711 __ Push(cp, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002712 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
2713 __ push(r0);
2714 } else {
2715 // This expression cannot throw a reference error at the top level.
2716 VisitForValue(expr->expression(), kStack);
2717 }
2718
2719 __ CallRuntime(Runtime::kTypeof, 1);
2720 Apply(context_, r0);
2721 break;
2722 }
2723
2724 case Token::ADD: {
2725 Comment cmt(masm_, "[ UnaryOperation (ADD)");
2726 VisitForValue(expr->expression(), kAccumulator);
2727 Label no_conversion;
2728 __ tst(result_register(), Operand(kSmiTagMask));
2729 __ b(eq, &no_conversion);
2730 __ push(r0);
2731 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2732 __ bind(&no_conversion);
2733 Apply(context_, result_register());
2734 break;
2735 }
2736
2737 case Token::SUB: {
2738 Comment cmt(masm_, "[ UnaryOperation (SUB)");
2739 bool overwrite =
2740 (expr->expression()->AsBinaryOperation() != NULL &&
2741 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
2742 GenericUnaryOpStub stub(Token::SUB, overwrite);
2743 // GenericUnaryOpStub expects the argument to be in the
2744 // accumulator register r0.
2745 VisitForValue(expr->expression(), kAccumulator);
2746 __ CallStub(&stub);
2747 Apply(context_, r0);
2748 break;
2749 }
2750
2751 case Token::BIT_NOT: {
2752 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
2753 bool overwrite =
2754 (expr->expression()->AsBinaryOperation() != NULL &&
2755 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
2756 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
2757 // GenericUnaryOpStub expects the argument to be in the
2758 // accumulator register r0.
2759 VisitForValue(expr->expression(), kAccumulator);
2760 // Avoid calling the stub for Smis.
2761 Label smi, done;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002762 __ BranchOnSmi(result_register(), &smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002763 // Non-smi: call stub leaving result in accumulator register.
2764 __ CallStub(&stub);
2765 __ b(&done);
2766 // Perform operation directly on Smis.
2767 __ bind(&smi);
2768 __ mvn(result_register(), Operand(result_register()));
2769 // Bit-clear inverted smi-tag.
2770 __ bic(result_register(), result_register(), Operand(kSmiTagMask));
2771 __ bind(&done);
2772 Apply(context_, result_register());
2773 break;
2774 }
2775
2776 default:
2777 UNREACHABLE();
2778 }
2779}
2780
2781
2782void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2783 Comment cmnt(masm_, "[ CountOperation");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002784 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2785 // as the left-hand side.
2786 if (!expr->expression()->IsValidLeftHandSide()) {
2787 VisitForEffect(expr->expression());
2788 return;
2789 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002790
2791 // Expression can only be a property, a global or a (parameter or local)
2792 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2793 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2794 LhsKind assign_type = VARIABLE;
2795 Property* prop = expr->expression()->AsProperty();
2796 // In case of a property we use the uninitialized expression context
2797 // of the key to detect a named property.
2798 if (prop != NULL) {
2799 assign_type =
2800 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2801 }
2802
2803 // Evaluate expression and get value.
2804 if (assign_type == VARIABLE) {
2805 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
2806 Location saved_location = location_;
2807 location_ = kAccumulator;
2808 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
2809 Expression::kValue);
2810 location_ = saved_location;
2811 } else {
2812 // Reserve space for result of postfix operation.
2813 if (expr->is_postfix() && context_ != Expression::kEffect) {
2814 __ mov(ip, Operand(Smi::FromInt(0)));
2815 __ push(ip);
2816 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002817 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002818 // Put the object both on the stack and in the accumulator.
2819 VisitForValue(prop->obj(), kAccumulator);
2820 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002821 EmitNamedPropertyLoad(prop);
2822 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002823 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002824 VisitForValue(prop->key(), kAccumulator);
2825 __ ldr(r1, MemOperand(sp, 0));
2826 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002827 EmitKeyedPropertyLoad(prop);
2828 }
2829 }
2830
2831 // Call ToNumber only if operand is not a smi.
2832 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002833 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002834 __ push(r0);
2835 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2836 __ bind(&no_conversion);
2837
2838 // Save result for postfix expressions.
2839 if (expr->is_postfix()) {
2840 switch (context_) {
2841 case Expression::kUninitialized:
2842 UNREACHABLE();
2843 case Expression::kEffect:
2844 // Do not save result.
2845 break;
2846 case Expression::kValue:
2847 case Expression::kTest:
2848 case Expression::kValueTest:
2849 case Expression::kTestValue:
2850 // Save the result on the stack. If we have a named or keyed property
2851 // we store the result under the receiver that is currently on top
2852 // of the stack.
2853 switch (assign_type) {
2854 case VARIABLE:
2855 __ push(r0);
2856 break;
2857 case NAMED_PROPERTY:
2858 __ str(r0, MemOperand(sp, kPointerSize));
2859 break;
2860 case KEYED_PROPERTY:
2861 __ str(r0, MemOperand(sp, 2 * kPointerSize));
2862 break;
2863 }
2864 break;
2865 }
2866 }
2867
2868
2869 // Inline smi case if we are in a loop.
2870 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002871 int count_value = expr->op() == Token::INC ? 1 : -1;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002872 if (loop_depth() > 0) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002873 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002874 __ b(vs, &stub_call);
2875 // We could eliminate this smi check if we split the code at
2876 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002877 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002878 __ bind(&stub_call);
2879 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002880 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002881 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002882 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00002883 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002884 __ CallStub(&stub);
2885 __ bind(&done);
2886
2887 // Store the value returned in r0.
2888 switch (assign_type) {
2889 case VARIABLE:
2890 if (expr->is_postfix()) {
2891 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002892 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002893 Expression::kEffect);
2894 // For all contexts except kEffect: We have the result on
2895 // top of the stack.
2896 if (context_ != Expression::kEffect) {
2897 ApplyTOS(context_);
2898 }
2899 } else {
2900 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002901 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002902 context_);
2903 }
2904 break;
2905 case NAMED_PROPERTY: {
2906 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00002907 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002908 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2909 __ Call(ic, RelocInfo::CODE_TARGET);
2910 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002911 if (context_ != Expression::kEffect) {
2912 ApplyTOS(context_);
2913 }
2914 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00002915 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002916 }
2917 break;
2918 }
2919 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002920 __ pop(r1); // Key.
2921 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002922 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
2923 __ Call(ic, RelocInfo::CODE_TARGET);
2924 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002925 if (context_ != Expression::kEffect) {
2926 ApplyTOS(context_);
2927 }
2928 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002929 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002930 }
2931 break;
2932 }
2933 }
2934}
2935
2936
2937void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
2938 Comment cmnt(masm_, "[ BinaryOperation");
2939 switch (expr->op()) {
2940 case Token::COMMA:
2941 VisitForEffect(expr->left());
2942 Visit(expr->right());
2943 break;
2944
2945 case Token::OR:
2946 case Token::AND:
2947 EmitLogicalOperation(expr);
2948 break;
2949
2950 case Token::ADD:
2951 case Token::SUB:
2952 case Token::DIV:
2953 case Token::MOD:
2954 case Token::MUL:
2955 case Token::BIT_OR:
2956 case Token::BIT_AND:
2957 case Token::BIT_XOR:
2958 case Token::SHL:
2959 case Token::SHR:
2960 case Token::SAR:
2961 VisitForValue(expr->left(), kStack);
2962 VisitForValue(expr->right(), kAccumulator);
2963 EmitBinaryOp(expr->op(), context_);
2964 break;
2965
2966 default:
2967 UNREACHABLE();
2968 }
2969}
2970
2971
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002972void FullCodeGenerator::EmitNullCompare(bool strict,
2973 Register obj,
2974 Register null_const,
2975 Label* if_true,
2976 Label* if_false,
2977 Register scratch) {
2978 __ cmp(obj, null_const);
2979 if (strict) {
2980 __ b(eq, if_true);
2981 } else {
2982 __ b(eq, if_true);
2983 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2984 __ cmp(obj, ip);
2985 __ b(eq, if_true);
2986 __ BranchOnSmi(obj, if_false);
2987 // It can be an undetectable object.
2988 __ ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
2989 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
2990 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
2991 __ b(ne, if_true);
2992 }
2993 __ jmp(if_false);
2994}
2995
2996
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002997void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
2998 Comment cmnt(masm_, "[ CompareOperation");
2999
3000 // Always perform the comparison for its control flow. Pack the result
3001 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003002
3003 Label materialize_true, materialize_false;
3004 Label* if_true = NULL;
3005 Label* if_false = NULL;
3006 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003007
3008 VisitForValue(expr->left(), kStack);
3009 switch (expr->op()) {
3010 case Token::IN:
3011 VisitForValue(expr->right(), kStack);
3012 __ InvokeBuiltin(Builtins::IN, CALL_JS);
3013 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3014 __ cmp(r0, ip);
3015 __ b(eq, if_true);
3016 __ jmp(if_false);
3017 break;
3018
3019 case Token::INSTANCEOF: {
3020 VisitForValue(expr->right(), kStack);
3021 InstanceofStub stub;
3022 __ CallStub(&stub);
3023 __ tst(r0, r0);
3024 __ b(eq, if_true); // The stub returns 0 for true.
3025 __ jmp(if_false);
3026 break;
3027 }
3028
3029 default: {
3030 VisitForValue(expr->right(), kAccumulator);
3031 Condition cc = eq;
3032 bool strict = false;
3033 switch (expr->op()) {
3034 case Token::EQ_STRICT:
3035 strict = true;
3036 // Fall through
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003037 case Token::EQ: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003038 cc = eq;
3039 __ pop(r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003040 // If either operand is constant null we do a fast compare
3041 // against null.
3042 Literal* right_literal = expr->right()->AsLiteral();
3043 Literal* left_literal = expr->left()->AsLiteral();
3044 if (right_literal != NULL && right_literal->handle()->IsNull()) {
3045 EmitNullCompare(strict, r1, r0, if_true, if_false, r2);
3046 Apply(context_, if_true, if_false);
3047 return;
3048 } else if (left_literal != NULL && left_literal->handle()->IsNull()) {
3049 EmitNullCompare(strict, r0, r1, if_true, if_false, r2);
3050 Apply(context_, if_true, if_false);
3051 return;
3052 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003053 break;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003054 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003055 case Token::LT:
3056 cc = lt;
3057 __ pop(r1);
3058 break;
3059 case Token::GT:
3060 // Reverse left and right sides to obtain ECMA-262 conversion order.
3061 cc = lt;
3062 __ mov(r1, result_register());
3063 __ pop(r0);
3064 break;
3065 case Token::LTE:
3066 // Reverse left and right sides to obtain ECMA-262 conversion order.
3067 cc = ge;
3068 __ mov(r1, result_register());
3069 __ pop(r0);
3070 break;
3071 case Token::GTE:
3072 cc = ge;
3073 __ pop(r1);
3074 break;
3075 case Token::IN:
3076 case Token::INSTANCEOF:
3077 default:
3078 UNREACHABLE();
3079 }
3080
3081 // The comparison stub expects the smi vs. smi case to be handled
3082 // before it is called.
3083 Label slow_case;
3084 __ orr(r2, r0, Operand(r1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003085 __ BranchOnNotSmi(r2, &slow_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003086 __ cmp(r1, r0);
3087 __ b(cc, if_true);
3088 __ jmp(if_false);
3089
3090 __ bind(&slow_case);
3091 CompareStub stub(cc, strict);
3092 __ CallStub(&stub);
3093 __ cmp(r0, Operand(0));
3094 __ b(cc, if_true);
3095 __ jmp(if_false);
3096 }
3097 }
3098
3099 // Convert the result of the comparison into one expected for this
3100 // expression's context.
3101 Apply(context_, if_true, if_false);
3102}
3103
3104
3105void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3106 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3107 Apply(context_, r0);
3108}
3109
3110
3111Register FullCodeGenerator::result_register() { return r0; }
3112
3113
3114Register FullCodeGenerator::context_register() { return cp; }
3115
3116
3117void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3118 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3119 __ str(value, MemOperand(fp, frame_offset));
3120}
3121
3122
3123void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
3124 __ ldr(dst, CodeGenerator::ContextOperand(cp, context_index));
3125}
3126
3127
3128// ----------------------------------------------------------------------------
3129// Non-local control flow support.
3130
3131void FullCodeGenerator::EnterFinallyBlock() {
3132 ASSERT(!result_register().is(r1));
3133 // Store result register while executing finally block.
3134 __ push(result_register());
3135 // Cook return address in link register to stack (smi encoded Code* delta)
3136 __ sub(r1, lr, Operand(masm_->CodeObject()));
3137 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3138 ASSERT_EQ(0, kSmiTag);
3139 __ add(r1, r1, Operand(r1)); // Convert to smi.
3140 __ push(r1);
3141}
3142
3143
3144void FullCodeGenerator::ExitFinallyBlock() {
3145 ASSERT(!result_register().is(r1));
3146 // Restore result register from stack.
3147 __ pop(r1);
3148 // Uncook return address and return.
3149 __ pop(result_register());
3150 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3151 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3152 __ add(pc, r1, Operand(masm_->CodeObject()));
3153}
3154
3155
3156#undef __
3157
3158} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003159
3160#endif // V8_TARGET_ARCH_ARM