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