blob: b58a4a5854a6b77b050797bfa4605a0cc4b58e95 [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.orgea4f62e2010-08-16 16:28:43 +000058void FullCodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.org5c838252010-02-19 08:53:10 +000059 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
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000064 int locals_count = scope()->num_stack_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000065
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000066 __ Push(lr, fp, cp, r1);
67 if (locals_count > 0) {
68 // Load undefined value here, so the value is ready for the loop
69 // below.
70 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
71 }
72 // Adjust fp to point to caller's fp.
73 __ add(fp, sp, Operand(2 * kPointerSize));
74
75 { Comment cmnt(masm_, "[ Allocate locals");
76 for (int i = 0; i < locals_count; i++) {
77 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000078 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000079 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000080
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000081 bool function_in_register = true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000082
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000083 // Possibly allocate a local context.
84 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
85 if (heap_slots > 0) {
86 Comment cmnt(masm_, "[ Allocate local context");
87 // Argument to NewContext is the function, which is in r1.
88 __ push(r1);
89 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
90 FastNewContextStub stub(heap_slots);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000091 __ CallStub(&stub);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000092 } else {
93 __ CallRuntime(Runtime::kNewContext, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000094 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000095 function_in_register = false;
96 // Context is returned in both r0 and cp. It replaces the context
97 // passed to us. It's saved in the stack and kept live in cp.
98 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
99 // Copy any necessary parameters into the context.
100 int num_parameters = scope()->num_parameters();
101 for (int i = 0; i < num_parameters; i++) {
102 Slot* slot = scope()->parameter(i)->slot();
103 if (slot != NULL && slot->type() == Slot::CONTEXT) {
104 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
105 (num_parameters - 1 - i) * kPointerSize;
106 // Load parameter from stack.
107 __ ldr(r0, MemOperand(fp, parameter_offset));
108 // Store it in the context.
109 __ mov(r1, Operand(Context::SlotOffset(slot->index())));
110 __ str(r0, MemOperand(cp, r1));
111 // Update the write barrier. This clobbers all involved
112 // registers, so we have to use two more registers to avoid
113 // clobbering cp.
114 __ mov(r2, Operand(cp));
115 __ RecordWrite(r2, Operand(r1), r3, r0);
116 }
117 }
118 }
119
120 Variable* arguments = scope()->arguments()->AsVariable();
121 if (arguments != NULL) {
122 // Function uses arguments object.
123 Comment cmnt(masm_, "[ Allocate arguments object");
124 if (!function_in_register) {
125 // Load this again, if it's used by the local context below.
126 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
127 } else {
128 __ mov(r3, r1);
129 }
130 // Receiver is just before the parameters on the caller's stack.
131 int offset = scope()->num_parameters() * kPointerSize;
132 __ add(r2, fp,
133 Operand(StandardFrameConstants::kCallerSPOffset + offset));
134 __ mov(r1, Operand(Smi::FromInt(scope()->num_parameters())));
135 __ Push(r3, r2, r1);
136
137 // Arguments to ArgumentsAccessStub:
138 // function, receiver address, parameter count.
139 // The stub will rewrite receiever and parameter count if the previous
140 // stack frame was an arguments adapter frame.
141 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
142 __ CallStub(&stub);
143 // Duplicate the value; move-to-slot operation might clobber registers.
144 __ mov(r3, r0);
145 Move(arguments->slot(), r0, r1, r2);
146 Slot* dot_arguments_slot =
147 scope()->arguments_shadow()->AsVariable()->slot();
148 Move(dot_arguments_slot, r3, r1, r2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000149 }
150
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000151 { Comment cmnt(masm_, "[ Declarations");
152 // For named function expressions, declare the function name as a
153 // constant.
154 if (scope()->is_function_scope() && scope()->function() != NULL) {
155 EmitDeclaration(scope()->function(), Variable::CONST, NULL);
156 }
157 // Visit all the explicit declarations unless there is an illegal
158 // redeclaration.
159 if (scope()->HasIllegalRedeclaration()) {
160 scope()->VisitIllegalRedeclaration(this);
161 } else {
162 VisitDeclarations(scope()->declarations());
163 }
164 }
165
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000166 // Check the stack for overflow or break request.
167 // Put the lr setup instruction in the delay slot. The kInstrSize is
168 // added to the implicit 8 byte offset that always applies to operations
169 // with pc and gives a return address 12 bytes down.
170 { Comment cmnt(masm_, "[ Stack check");
171 __ LoadRoot(r2, Heap::kStackLimitRootIndex);
172 __ add(lr, pc, Operand(Assembler::kInstrSize));
173 __ cmp(sp, Operand(r2));
174 StackCheckStub stub;
175 __ mov(pc,
176 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
177 RelocInfo::CODE_TARGET),
178 LeaveCC,
179 lo);
180 }
181
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000182 if (FLAG_trace) {
183 __ CallRuntime(Runtime::kTraceEnter, 0);
184 }
185
186 { Comment cmnt(masm_, "[ Body");
187 ASSERT(loop_depth() == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000188 VisitStatements(function()->body());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000189 ASSERT(loop_depth() == 0);
190 }
191
192 { Comment cmnt(masm_, "[ return <undefined>;");
193 // Emit a 'return undefined' in case control fell off the end of the
194 // body.
195 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
196 }
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000197 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000198}
199
200
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000201void FullCodeGenerator::EmitReturnSequence() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000202 Comment cmnt(masm_, "[ Return sequence");
203 if (return_label_.is_bound()) {
204 __ b(&return_label_);
205 } else {
206 __ bind(&return_label_);
207 if (FLAG_trace) {
208 // Push the return value on the stack as the parameter.
209 // Runtime::TraceExit returns its parameter in r0.
210 __ push(r0);
211 __ CallRuntime(Runtime::kTraceExit, 1);
212 }
213
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000214#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000215 // Add a label for checking the size of the code used for returning.
216 Label check_exit_codesize;
217 masm_->bind(&check_exit_codesize);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000218#endif
219 // Make sure that the constant pool is not emitted inside of the return
220 // sequence.
221 { Assembler::BlockConstPoolScope block_const_pool(masm_);
222 // Here we use masm_-> instead of the __ macro to avoid the code coverage
223 // tool from instrumenting as we rely on the code size here.
224 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000225 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000226 __ RecordJSReturn();
227 masm_->mov(sp, fp);
228 masm_->ldm(ia_w, sp, fp.bit() | lr.bit());
229 masm_->add(sp, sp, Operand(sp_delta));
230 masm_->Jump(lr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000231 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000232
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000233#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000234 // Check that the size of the code used for returning matches what is
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000235 // expected by the debugger. If the sp_delts above cannot be encoded in the
236 // add instruction the add will generate two instructions.
237 int return_sequence_length =
238 masm_->InstructionsGeneratedSince(&check_exit_codesize);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000239 CHECK(return_sequence_length ==
240 Assembler::kJSReturnSequenceInstructions ||
241 return_sequence_length ==
242 Assembler::kJSReturnSequenceInstructions + 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000243#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000244 }
245}
246
247
248void FullCodeGenerator::Apply(Expression::Context context, Register reg) {
249 switch (context) {
250 case Expression::kUninitialized:
251 UNREACHABLE();
252
253 case Expression::kEffect:
254 // Nothing to do.
255 break;
256
257 case Expression::kValue:
258 // Move value into place.
259 switch (location_) {
260 case kAccumulator:
261 if (!reg.is(result_register())) __ mov(result_register(), reg);
262 break;
263 case kStack:
264 __ push(reg);
265 break;
266 }
267 break;
268
269 case Expression::kValueTest:
270 case Expression::kTestValue:
271 // Push an extra copy of the value in case it's needed.
272 __ push(reg);
273 // Fall through.
274
275 case Expression::kTest:
276 // We always call the runtime on ARM, so push the value as argument.
277 __ push(reg);
278 DoTest(context);
279 break;
280 }
281}
282
283
284void FullCodeGenerator::Apply(Expression::Context context, Slot* slot) {
285 switch (context) {
286 case Expression::kUninitialized:
287 UNREACHABLE();
288 case Expression::kEffect:
289 // Nothing to do.
290 break;
291 case Expression::kValue:
292 case Expression::kTest:
293 case Expression::kValueTest:
294 case Expression::kTestValue:
295 // On ARM we have to move the value into a register to do anything
296 // with it.
297 Move(result_register(), slot);
298 Apply(context, result_register());
299 break;
300 }
301}
302
303
304void FullCodeGenerator::Apply(Expression::Context context, Literal* lit) {
305 switch (context) {
306 case Expression::kUninitialized:
307 UNREACHABLE();
308 case Expression::kEffect:
309 break;
310 // Nothing to do.
311 case Expression::kValue:
312 case Expression::kTest:
313 case Expression::kValueTest:
314 case Expression::kTestValue:
315 // On ARM we have to move the value into a register to do anything
316 // with it.
317 __ mov(result_register(), Operand(lit->handle()));
318 Apply(context, result_register());
319 break;
320 }
321}
322
323
324void FullCodeGenerator::ApplyTOS(Expression::Context context) {
325 switch (context) {
326 case Expression::kUninitialized:
327 UNREACHABLE();
328
329 case Expression::kEffect:
330 __ Drop(1);
331 break;
332
333 case Expression::kValue:
334 switch (location_) {
335 case kAccumulator:
336 __ pop(result_register());
337 break;
338 case kStack:
339 break;
340 }
341 break;
342
343 case Expression::kValueTest:
344 case Expression::kTestValue:
345 // Duplicate the value on the stack in case it's needed.
346 __ ldr(ip, MemOperand(sp));
347 __ push(ip);
348 // Fall through.
349
350 case Expression::kTest:
351 DoTest(context);
352 break;
353 }
354}
355
356
357void FullCodeGenerator::DropAndApply(int count,
358 Expression::Context context,
359 Register reg) {
360 ASSERT(count > 0);
361 ASSERT(!reg.is(sp));
362 switch (context) {
363 case Expression::kUninitialized:
364 UNREACHABLE();
365
366 case Expression::kEffect:
367 __ Drop(count);
368 break;
369
370 case Expression::kValue:
371 switch (location_) {
372 case kAccumulator:
373 __ Drop(count);
374 if (!reg.is(result_register())) __ mov(result_register(), reg);
375 break;
376 case kStack:
377 if (count > 1) __ Drop(count - 1);
378 __ str(reg, MemOperand(sp));
379 break;
380 }
381 break;
382
383 case Expression::kTest:
384 if (count > 1) __ Drop(count - 1);
385 __ str(reg, MemOperand(sp));
386 DoTest(context);
387 break;
388
389 case Expression::kValueTest:
390 case Expression::kTestValue:
391 if (count == 1) {
392 __ str(reg, MemOperand(sp));
393 __ push(reg);
394 } else { // count > 1
395 __ Drop(count - 2);
396 __ str(reg, MemOperand(sp, kPointerSize));
397 __ str(reg, MemOperand(sp));
398 }
399 DoTest(context);
400 break;
401 }
402}
403
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000404void FullCodeGenerator::PrepareTest(Label* materialize_true,
405 Label* materialize_false,
406 Label** if_true,
407 Label** if_false) {
408 switch (context_) {
409 case Expression::kUninitialized:
410 UNREACHABLE();
411 break;
412 case Expression::kEffect:
413 // In an effect context, the true and the false case branch to the
414 // same label.
415 *if_true = *if_false = materialize_true;
416 break;
417 case Expression::kValue:
418 *if_true = materialize_true;
419 *if_false = materialize_false;
420 break;
421 case Expression::kTest:
422 *if_true = true_label_;
423 *if_false = false_label_;
424 break;
425 case Expression::kValueTest:
426 *if_true = materialize_true;
427 *if_false = false_label_;
428 break;
429 case Expression::kTestValue:
430 *if_true = true_label_;
431 *if_false = materialize_false;
432 break;
433 }
434}
435
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000436
437void FullCodeGenerator::Apply(Expression::Context context,
438 Label* materialize_true,
439 Label* materialize_false) {
440 switch (context) {
441 case Expression::kUninitialized:
442
443 case Expression::kEffect:
444 ASSERT_EQ(materialize_true, materialize_false);
445 __ bind(materialize_true);
446 break;
447
448 case Expression::kValue: {
449 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000450 switch (location_) {
451 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000452 __ bind(materialize_true);
453 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
454 __ jmp(&done);
455 __ bind(materialize_false);
456 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000457 break;
458 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000459 __ bind(materialize_true);
460 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
461 __ push(ip);
462 __ jmp(&done);
463 __ bind(materialize_false);
464 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
465 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000466 break;
467 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000468 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000469 break;
470 }
471
472 case Expression::kTest:
473 break;
474
475 case Expression::kValueTest:
476 __ bind(materialize_true);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000477 switch (location_) {
478 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000479 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000480 break;
481 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000482 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
483 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000484 break;
485 }
486 __ jmp(true_label_);
487 break;
488
489 case Expression::kTestValue:
490 __ bind(materialize_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000491 switch (location_) {
492 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000493 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000494 break;
495 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000496 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
497 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000498 break;
499 }
500 __ jmp(false_label_);
501 break;
502 }
503}
504
505
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000506// Convert constant control flow (true or false) to the result expected for
507// a given expression context.
508void FullCodeGenerator::Apply(Expression::Context context, bool flag) {
509 switch (context) {
510 case Expression::kUninitialized:
511 UNREACHABLE();
512 break;
513 case Expression::kEffect:
514 break;
515 case Expression::kValue: {
516 Heap::RootListIndex value_root_index =
517 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
518 switch (location_) {
519 case kAccumulator:
520 __ LoadRoot(result_register(), value_root_index);
521 break;
522 case kStack:
523 __ LoadRoot(ip, value_root_index);
524 __ push(ip);
525 break;
526 }
527 break;
528 }
529 case Expression::kTest:
530 __ b(flag ? true_label_ : false_label_);
531 break;
532 case Expression::kTestValue:
533 switch (location_) {
534 case kAccumulator:
535 // If value is false it's needed.
536 if (!flag) __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
537 break;
538 case kStack:
539 // If value is false it's needed.
540 if (!flag) {
541 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
542 __ push(ip);
543 }
544 break;
545 }
546 __ b(flag ? true_label_ : false_label_);
547 break;
548 case Expression::kValueTest:
549 switch (location_) {
550 case kAccumulator:
551 // If value is true it's needed.
552 if (flag) __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
553 break;
554 case kStack:
555 // If value is true it's needed.
556 if (flag) {
557 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
558 __ push(ip);
559 }
560 break;
561 }
562 __ b(flag ? true_label_ : false_label_);
563 break;
564 }
565}
566
567
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000568void FullCodeGenerator::DoTest(Expression::Context context) {
569 // The value to test is pushed on the stack, and duplicated on the stack
570 // if necessary (for value/test and test/value contexts).
571 ASSERT_NE(NULL, true_label_);
572 ASSERT_NE(NULL, false_label_);
573
574 // Call the runtime to find the boolean value of the source and then
575 // translate it into control flow to the pair of labels.
576 __ CallRuntime(Runtime::kToBool, 1);
577 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
578 __ cmp(r0, ip);
579
580 // Complete based on the context.
581 switch (context) {
582 case Expression::kUninitialized:
583 case Expression::kEffect:
584 case Expression::kValue:
585 UNREACHABLE();
586
587 case Expression::kTest:
588 __ b(eq, true_label_);
589 __ jmp(false_label_);
590 break;
591
592 case Expression::kValueTest: {
593 Label discard;
594 switch (location_) {
595 case kAccumulator:
596 __ b(ne, &discard);
597 __ pop(result_register());
598 __ jmp(true_label_);
599 break;
600 case kStack:
601 __ b(eq, true_label_);
602 break;
603 }
604 __ bind(&discard);
605 __ Drop(1);
606 __ jmp(false_label_);
607 break;
608 }
609
610 case Expression::kTestValue: {
611 Label discard;
612 switch (location_) {
613 case kAccumulator:
614 __ b(eq, &discard);
615 __ pop(result_register());
616 __ jmp(false_label_);
617 break;
618 case kStack:
619 __ b(ne, false_label_);
620 break;
621 }
622 __ bind(&discard);
623 __ Drop(1);
624 __ jmp(true_label_);
625 break;
626 }
627 }
628}
629
630
631MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
632 switch (slot->type()) {
633 case Slot::PARAMETER:
634 case Slot::LOCAL:
635 return MemOperand(fp, SlotOffset(slot));
636 case Slot::CONTEXT: {
637 int context_chain_length =
ager@chromium.org5c838252010-02-19 08:53:10 +0000638 scope()->ContextChainLength(slot->var()->scope());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000639 __ LoadContext(scratch, context_chain_length);
640 return CodeGenerator::ContextOperand(scratch, slot->index());
641 }
642 case Slot::LOOKUP:
643 UNREACHABLE();
644 }
645 UNREACHABLE();
646 return MemOperand(r0, 0);
647}
648
649
650void FullCodeGenerator::Move(Register destination, Slot* source) {
651 // Use destination as scratch.
652 MemOperand slot_operand = EmitSlotSearch(source, destination);
653 __ ldr(destination, slot_operand);
654}
655
656
657void FullCodeGenerator::Move(Slot* dst,
658 Register src,
659 Register scratch1,
660 Register scratch2) {
661 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
662 ASSERT(!scratch1.is(src) && !scratch2.is(src));
663 MemOperand location = EmitSlotSearch(dst, scratch1);
664 __ str(src, location);
665 // Emit the write barrier code if the location is in the heap.
666 if (dst->type() == Slot::CONTEXT) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000667 __ RecordWrite(scratch1,
668 Operand(Context::SlotOffset(dst->index())),
669 scratch2,
670 src);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000671 }
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());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000718 // We know that we have written a function, which is not a smi.
719 __ mov(r1, Operand(cp));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000720 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000721 }
722 break;
723
724 case Slot::LOOKUP: {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000725 __ mov(r2, Operand(variable->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000726 // Declaration nodes are always introduced in one of two modes.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000727 ASSERT(mode == Variable::VAR ||
728 mode == Variable::CONST);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000729 PropertyAttributes attr =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000730 (mode == Variable::VAR) ? NONE : READ_ONLY;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000731 __ mov(r1, Operand(Smi::FromInt(attr)));
732 // Push initial value, if any.
733 // Note: For variables we must not push an initial value (such as
734 // 'undefined') because we may have a (legal) redeclaration and we
735 // must not destroy the current value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000736 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000737 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000738 __ Push(cp, r2, r1, r0);
739 } else if (function != NULL) {
740 __ Push(cp, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000741 // Push initial value for function declaration.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000742 VisitForValue(function, kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000743 } else {
744 __ mov(r0, Operand(Smi::FromInt(0))); // No initial value!
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000745 __ Push(cp, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000746 }
747 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
748 break;
749 }
750 }
751
752 } else if (prop != NULL) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000753 if (function != NULL || mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000754 // We are declaring a function or constant that rewrites to a
755 // property. Use (keyed) IC to set the initial value.
756 VisitForValue(prop->obj(), kStack);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000757 if (function != NULL) {
758 VisitForValue(prop->key(), kStack);
759 VisitForValue(function, kAccumulator);
760 __ pop(r1); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000761 } else {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000762 VisitForValue(prop->key(), kAccumulator);
763 __ mov(r1, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000764 __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex);
765 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000766 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000767
768 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
769 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000770 // Value in r0 is ignored (declarations are statements).
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000771 }
772 }
773}
774
775
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000776void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
777 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
778}
779
780
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000781void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
782 // Call the runtime to declare the globals.
783 // The context is the first argument.
784 __ mov(r1, Operand(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000785 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000786 __ Push(cp, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000787 __ CallRuntime(Runtime::kDeclareGlobals, 3);
788 // Return value is ignored.
789}
790
791
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000792void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000793 Comment cmnt(masm_, "[ SwitchStatement");
794 Breakable nested_statement(this, stmt);
795 SetStatementPosition(stmt);
796 // Keep the switch value on the stack until a case matches.
797 VisitForValue(stmt->tag(), kStack);
798
799 ZoneList<CaseClause*>* clauses = stmt->cases();
800 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
801
802 Label next_test; // Recycled for each test.
803 // Compile all the tests with branches to their bodies.
804 for (int i = 0; i < clauses->length(); i++) {
805 CaseClause* clause = clauses->at(i);
806 // The default is not a test, but remember it as final fall through.
807 if (clause->is_default()) {
808 default_clause = clause;
809 continue;
810 }
811
812 Comment cmnt(masm_, "[ Case comparison");
813 __ bind(&next_test);
814 next_test.Unuse();
815
816 // Compile the label expression.
817 VisitForValue(clause->label(), kAccumulator);
818
819 // Perform the comparison as if via '==='. The comparison stub expects
820 // the smi vs. smi case to be handled before it is called.
821 Label slow_case;
822 __ ldr(r1, MemOperand(sp, 0)); // Switch value.
ager@chromium.orgb5737492010-07-15 09:29:43 +0000823 __ orr(r2, r1, r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000824 __ tst(r2, Operand(kSmiTagMask));
825 __ b(ne, &slow_case);
826 __ cmp(r1, r0);
827 __ b(ne, &next_test);
828 __ Drop(1); // Switch value is no longer needed.
829 __ b(clause->body_target()->entry_label());
830
831 __ bind(&slow_case);
ager@chromium.orgb5737492010-07-15 09:29:43 +0000832 CompareStub stub(eq, true, kBothCouldBeNaN, true, r1, r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000833 __ CallStub(&stub);
ager@chromium.orgb5737492010-07-15 09:29:43 +0000834 __ cmp(r0, Operand(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000835 __ b(ne, &next_test);
836 __ Drop(1); // Switch value is no longer needed.
837 __ b(clause->body_target()->entry_label());
838 }
839
840 // Discard the test value and jump to the default if present, otherwise to
841 // the end of the statement.
842 __ bind(&next_test);
843 __ Drop(1); // Switch value is no longer needed.
844 if (default_clause == NULL) {
845 __ b(nested_statement.break_target());
846 } else {
847 __ b(default_clause->body_target()->entry_label());
848 }
849
850 // Compile all the case bodies.
851 for (int i = 0; i < clauses->length(); i++) {
852 Comment cmnt(masm_, "[ Case body");
853 CaseClause* clause = clauses->at(i);
854 __ bind(clause->body_target()->entry_label());
855 VisitStatements(clause->statements());
856 }
857
858 __ bind(nested_statement.break_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000859}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000860
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000861
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000862void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000863 Comment cmnt(masm_, "[ ForInStatement");
864 SetStatementPosition(stmt);
865
866 Label loop, exit;
867 ForIn loop_statement(this, stmt);
868 increment_loop_depth();
869
870 // Get the object to enumerate over. Both SpiderMonkey and JSC
871 // ignore null and undefined in contrast to the specification; see
872 // ECMA-262 section 12.6.4.
873 VisitForValue(stmt->enumerable(), kAccumulator);
874 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
875 __ cmp(r0, ip);
876 __ b(eq, &exit);
877 __ LoadRoot(ip, Heap::kNullValueRootIndex);
878 __ cmp(r0, ip);
879 __ b(eq, &exit);
880
881 // Convert the object to a JS object.
882 Label convert, done_convert;
883 __ BranchOnSmi(r0, &convert);
884 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
885 __ b(hs, &done_convert);
886 __ bind(&convert);
887 __ push(r0);
888 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
889 __ bind(&done_convert);
890 __ push(r0);
891
892 // TODO(kasperl): Check cache validity in generated code. This is a
893 // fast case for the JSObject::IsSimpleEnum cache validity
894 // checks. If we cannot guarantee cache validity, call the runtime
895 // system to check cache validity or get the property names in a
896 // fixed array.
897
898 // Get the set of properties to enumerate.
899 __ push(r0); // Duplicate the enumerable object on the stack.
900 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
901
902 // If we got a map from the runtime call, we can do a fast
903 // modification check. Otherwise, we got a fixed array, and we have
904 // to do a slow check.
905 Label fixed_array;
906 __ mov(r2, r0);
907 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
908 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
909 __ cmp(r1, ip);
910 __ b(ne, &fixed_array);
911
912 // We got a map in register r0. Get the enumeration cache from it.
913 __ ldr(r1, FieldMemOperand(r0, Map::kInstanceDescriptorsOffset));
914 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
915 __ ldr(r2, FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
916
917 // Setup the four remaining stack slots.
918 __ push(r0); // Map.
919 __ ldr(r1, FieldMemOperand(r2, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000920 __ mov(r0, Operand(Smi::FromInt(0)));
921 // Push enumeration cache, enumeration cache length (as smi) and zero.
922 __ Push(r2, r1, r0);
923 __ jmp(&loop);
924
925 // We got a fixed array in register r0. Iterate through that.
926 __ bind(&fixed_array);
927 __ mov(r1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
928 __ Push(r1, r0);
929 __ ldr(r1, FieldMemOperand(r0, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000930 __ mov(r0, Operand(Smi::FromInt(0)));
931 __ Push(r1, r0); // Fixed array length (as smi) and initial index.
932
933 // Generate code for doing the condition check.
934 __ bind(&loop);
935 // Load the current count to r0, load the length to r1.
936 __ Ldrd(r0, r1, MemOperand(sp, 0 * kPointerSize));
937 __ cmp(r0, r1); // Compare to the array length.
938 __ b(hs, loop_statement.break_target());
939
940 // Get the current entry of the array into register r3.
941 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
942 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
943 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
944
945 // Get the expected map from the stack or a zero map in the
946 // permanent slow case into register r2.
947 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
948
949 // Check if the expected map still matches that of the enumerable.
950 // If not, we have to filter the key.
951 Label update_each;
952 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
953 __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
954 __ cmp(r4, Operand(r2));
955 __ b(eq, &update_each);
956
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000957 // Convert the entry to a string or (smi) 0 if it isn't a property
958 // any more. If the property has been removed while iterating, we
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000959 // just skip it.
960 __ push(r1); // Enumerable.
961 __ push(r3); // Current entry.
962 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000963 __ mov(r3, Operand(r0), SetCC);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000964 __ b(eq, loop_statement.continue_target());
965
966 // Update the 'each' property or variable from the possibly filtered
967 // entry in register r3.
968 __ bind(&update_each);
969 __ mov(result_register(), r3);
970 // Perform the assignment as if via '='.
971 EmitAssignment(stmt->each());
972
973 // Generate code for the body of the loop.
974 Label stack_limit_hit, stack_check_done;
975 Visit(stmt->body());
976
977 __ StackLimitCheck(&stack_limit_hit);
978 __ bind(&stack_check_done);
979
980 // Generate code for the going to the next element by incrementing
981 // the index (smi) stored on top of the stack.
982 __ bind(loop_statement.continue_target());
983 __ pop(r0);
984 __ add(r0, r0, Operand(Smi::FromInt(1)));
985 __ push(r0);
986 __ b(&loop);
987
988 // Slow case for the stack limit check.
989 StackCheckStub stack_check_stub;
990 __ bind(&stack_limit_hit);
991 __ CallStub(&stack_check_stub);
992 __ b(&stack_check_done);
993
994 // Remove the pointers stored on the stack.
995 __ bind(loop_statement.break_target());
996 __ Drop(5);
997
998 // Exit and decrement the loop depth.
999 __ bind(&exit);
1000 decrement_loop_depth();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001001}
1002
1003
1004void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info) {
1005 // Use the fast case closure allocation code that allocates in new
1006 // space for nested functions that don't need literals cloning.
1007 if (scope()->is_function_scope() && info->num_literals() == 0) {
1008 FastNewClosureStub stub;
1009 __ mov(r0, Operand(info));
1010 __ push(r0);
1011 __ CallStub(&stub);
1012 } else {
1013 __ mov(r0, Operand(info));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001014 __ Push(cp, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001015 __ CallRuntime(Runtime::kNewClosure, 2);
1016 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001017 Apply(context_, r0);
1018}
1019
1020
1021void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
1022 Comment cmnt(masm_, "[ VariableProxy");
1023 EmitVariableLoad(expr->var(), context_);
1024}
1025
1026
1027void FullCodeGenerator::EmitVariableLoad(Variable* var,
1028 Expression::Context context) {
1029 // Four cases: non-this global variables, lookup slots, all other
1030 // types of slots, and parameters that rewrite to explicit property
1031 // accesses on the arguments object.
1032 Slot* slot = var->slot();
1033 Property* property = var->AsProperty();
1034
1035 if (var->is_global() && !var->is_this()) {
1036 Comment cmnt(masm_, "Global variable");
1037 // Use inline caching. Variable name is passed in r2 and the global
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001038 // object (receiver) in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001039 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001040 __ mov(r2, Operand(var->name()));
1041 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1042 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001043 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001044
1045 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
1046 Comment cmnt(masm_, "Lookup slot");
1047 __ mov(r1, Operand(var->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001048 __ Push(cp, r1); // Context and name.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001049 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1050 Apply(context, r0);
1051
1052 } else if (slot != NULL) {
1053 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1054 ? "Context slot"
1055 : "Stack slot");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001056 if (var->mode() == Variable::CONST) {
1057 // Constants may be the hole value if they have not been initialized.
1058 // Unhole them.
1059 Label done;
1060 MemOperand slot_operand = EmitSlotSearch(slot, r0);
1061 __ ldr(r0, slot_operand);
1062 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1063 __ cmp(r0, ip);
1064 __ b(ne, &done);
1065 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1066 __ bind(&done);
1067 Apply(context, r0);
1068 } else {
1069 Apply(context, slot);
1070 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001071 } else {
1072 Comment cmnt(masm_, "Rewritten parameter");
1073 ASSERT_NOT_NULL(property);
1074 // Rewritten parameter accesses are of the form "slot[literal]".
1075
1076 // Assert that the object is in a slot.
1077 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
1078 ASSERT_NOT_NULL(object_var);
1079 Slot* object_slot = object_var->slot();
1080 ASSERT_NOT_NULL(object_slot);
1081
1082 // Load the object.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001083 Move(r1, object_slot);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001084
1085 // Assert that the key is a smi.
1086 Literal* key_literal = property->key()->AsLiteral();
1087 ASSERT_NOT_NULL(key_literal);
1088 ASSERT(key_literal->handle()->IsSmi());
1089
1090 // Load the key.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001091 __ mov(r0, Operand(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001092
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001093 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001094 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1095 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001096 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001097 }
1098}
1099
1100
1101void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1102 Comment cmnt(masm_, "[ RegExpLiteral");
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001103 Label materialized;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001104 // Registers will be used as follows:
1105 // r4 = JS function, literals array
1106 // r3 = literal index
1107 // r2 = RegExp pattern
1108 // r1 = RegExp flags
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001109 // r0 = temp + materialized value (RegExp literal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001110 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1111 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
1112 int literal_offset =
1113 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1114 __ ldr(r0, FieldMemOperand(r4, literal_offset));
1115 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1116 __ cmp(r0, ip);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001117 __ b(ne, &materialized);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001118 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
1119 __ mov(r2, Operand(expr->pattern()));
1120 __ mov(r1, Operand(expr->flags()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001121 __ Push(r4, r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001122 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001123 __ bind(&materialized);
1124 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1125 __ push(r0);
1126 __ mov(r0, Operand(Smi::FromInt(size)));
1127 __ push(r0);
1128 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1129 // After this, registers are used as follows:
1130 // r0: Newly allocated regexp.
1131 // r1: Materialized regexp
1132 // r2: temp.
1133 __ pop(r1);
1134 __ CopyFields(r0, r1, r2.bit(), size / kPointerSize);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001135 Apply(context_, r0);
1136}
1137
1138
1139void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1140 Comment cmnt(masm_, "[ ObjectLiteral");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001141 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1142 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1143 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1144 __ mov(r1, Operand(expr->constant_properties()));
1145 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001146 __ Push(r3, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001147 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001148 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001149 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001150 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001151 }
1152
1153 // If result_saved is true the result is on top of the stack. If
1154 // result_saved is false the result is in r0.
1155 bool result_saved = false;
1156
1157 for (int i = 0; i < expr->properties()->length(); i++) {
1158 ObjectLiteral::Property* property = expr->properties()->at(i);
1159 if (property->IsCompileTimeValue()) continue;
1160
1161 Literal* key = property->key();
1162 Expression* value = property->value();
1163 if (!result_saved) {
1164 __ push(r0); // Save result on stack
1165 result_saved = true;
1166 }
1167 switch (property->kind()) {
1168 case ObjectLiteral::Property::CONSTANT:
1169 UNREACHABLE();
1170 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1171 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1172 // Fall through.
1173 case ObjectLiteral::Property::COMPUTED:
1174 if (key->handle()->IsSymbol()) {
1175 VisitForValue(value, kAccumulator);
1176 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001177 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001178 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1179 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001180 break;
1181 }
1182 // Fall through.
1183 case ObjectLiteral::Property::PROTOTYPE:
1184 // Duplicate receiver on stack.
1185 __ ldr(r0, MemOperand(sp));
1186 __ push(r0);
1187 VisitForValue(key, kStack);
1188 VisitForValue(value, kStack);
1189 __ CallRuntime(Runtime::kSetProperty, 3);
1190 break;
1191 case ObjectLiteral::Property::GETTER:
1192 case ObjectLiteral::Property::SETTER:
1193 // Duplicate receiver on stack.
1194 __ ldr(r0, MemOperand(sp));
1195 __ push(r0);
1196 VisitForValue(key, kStack);
1197 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1198 Smi::FromInt(1) :
1199 Smi::FromInt(0)));
1200 __ push(r1);
1201 VisitForValue(value, kStack);
1202 __ CallRuntime(Runtime::kDefineAccessor, 4);
1203 break;
1204 }
1205 }
1206
1207 if (result_saved) {
1208 ApplyTOS(context_);
1209 } else {
1210 Apply(context_, r0);
1211 }
1212}
1213
1214
1215void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1216 Comment cmnt(masm_, "[ ArrayLiteral");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001217
1218 ZoneList<Expression*>* subexprs = expr->values();
1219 int length = subexprs->length();
1220
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001221 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1222 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1223 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1224 __ mov(r1, Operand(expr->constant_elements()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001225 __ Push(r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001226 if (expr->depth() > 1) {
1227 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001228 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001229 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001230 } else {
1231 FastCloneShallowArrayStub stub(length);
1232 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001233 }
1234
1235 bool result_saved = false; // Is the result saved to the stack?
1236
1237 // Emit code to evaluate all the non-constant subexpressions and to store
1238 // them into the newly cloned array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001239 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001240 Expression* subexpr = subexprs->at(i);
1241 // If the subexpression is a literal or a simple materialized literal it
1242 // is already set in the cloned array.
1243 if (subexpr->AsLiteral() != NULL ||
1244 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1245 continue;
1246 }
1247
1248 if (!result_saved) {
1249 __ push(r0);
1250 result_saved = true;
1251 }
1252 VisitForValue(subexpr, kAccumulator);
1253
1254 // Store the subexpression value in the array's elements.
1255 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
1256 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1257 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1258 __ str(result_register(), FieldMemOperand(r1, offset));
1259
1260 // Update the write barrier for the array store with r0 as the scratch
1261 // register.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001262 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001263 }
1264
1265 if (result_saved) {
1266 ApplyTOS(context_);
1267 } else {
1268 Apply(context_, r0);
1269 }
1270}
1271
1272
ager@chromium.org5c838252010-02-19 08:53:10 +00001273void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1274 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001275 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1276 // on the left-hand side.
1277 if (!expr->target()->IsValidLeftHandSide()) {
1278 VisitForEffect(expr->target());
1279 return;
1280 }
1281
ager@chromium.org5c838252010-02-19 08:53:10 +00001282 // Left-hand side can only be a property, a global or a (parameter or local)
1283 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1284 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1285 LhsKind assign_type = VARIABLE;
1286 Property* prop = expr->target()->AsProperty();
1287 if (prop != NULL) {
1288 assign_type =
1289 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
1290 }
1291
1292 // Evaluate LHS expression.
1293 switch (assign_type) {
1294 case VARIABLE:
1295 // Nothing to do here.
1296 break;
1297 case NAMED_PROPERTY:
1298 if (expr->is_compound()) {
1299 // We need the receiver both on the stack and in the accumulator.
1300 VisitForValue(prop->obj(), kAccumulator);
1301 __ push(result_register());
1302 } else {
1303 VisitForValue(prop->obj(), kStack);
1304 }
1305 break;
1306 case KEYED_PROPERTY:
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001307 // We need the key and receiver on both the stack and in r0 and r1.
1308 if (expr->is_compound()) {
1309 VisitForValue(prop->obj(), kStack);
1310 VisitForValue(prop->key(), kAccumulator);
1311 __ ldr(r1, MemOperand(sp, 0));
1312 __ push(r0);
1313 } else {
1314 VisitForValue(prop->obj(), kStack);
1315 VisitForValue(prop->key(), kStack);
1316 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001317 break;
1318 }
1319
1320 // If we have a compound assignment: Get value of LHS expression and
1321 // store in on top of the stack.
1322 if (expr->is_compound()) {
1323 Location saved_location = location_;
1324 location_ = kStack;
1325 switch (assign_type) {
1326 case VARIABLE:
1327 EmitVariableLoad(expr->target()->AsVariableProxy()->var(),
1328 Expression::kValue);
1329 break;
1330 case NAMED_PROPERTY:
1331 EmitNamedPropertyLoad(prop);
1332 __ push(result_register());
1333 break;
1334 case KEYED_PROPERTY:
1335 EmitKeyedPropertyLoad(prop);
1336 __ push(result_register());
1337 break;
1338 }
1339 location_ = saved_location;
1340 }
1341
1342 // Evaluate RHS expression.
1343 Expression* rhs = expr->value();
1344 VisitForValue(rhs, kAccumulator);
1345
1346 // If we have a compound assignment: Apply operator.
1347 if (expr->is_compound()) {
1348 Location saved_location = location_;
1349 location_ = kAccumulator;
1350 EmitBinaryOp(expr->binary_op(), Expression::kValue);
1351 location_ = saved_location;
1352 }
1353
1354 // Record source position before possible IC call.
1355 SetSourcePosition(expr->position());
1356
1357 // Store the value.
1358 switch (assign_type) {
1359 case VARIABLE:
1360 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001361 expr->op(),
ager@chromium.org5c838252010-02-19 08:53:10 +00001362 context_);
1363 break;
1364 case NAMED_PROPERTY:
1365 EmitNamedPropertyAssignment(expr);
1366 break;
1367 case KEYED_PROPERTY:
1368 EmitKeyedPropertyAssignment(expr);
1369 break;
1370 }
1371}
1372
1373
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001374void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1375 SetSourcePosition(prop->position());
1376 Literal* key = prop->key()->AsLiteral();
1377 __ mov(r2, Operand(key->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001378 // Call load IC. It has arguments receiver and property name r0 and r2.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001379 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1380 __ Call(ic, RelocInfo::CODE_TARGET);
1381}
1382
1383
1384void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1385 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001386 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001387 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1388 __ Call(ic, RelocInfo::CODE_TARGET);
1389}
1390
1391
1392void FullCodeGenerator::EmitBinaryOp(Token::Value op,
1393 Expression::Context context) {
1394 __ pop(r1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001395 GenericBinaryOpStub stub(op, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001396 __ CallStub(&stub);
1397 Apply(context, r0);
1398}
1399
1400
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001401void FullCodeGenerator::EmitAssignment(Expression* expr) {
1402 // Invalid left-hand sides are rewritten to have a 'throw
1403 // ReferenceError' on the left-hand side.
1404 if (!expr->IsValidLeftHandSide()) {
1405 VisitForEffect(expr);
1406 return;
1407 }
1408
1409 // Left-hand side can only be a property, a global or a (parameter or local)
1410 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1411 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1412 LhsKind assign_type = VARIABLE;
1413 Property* prop = expr->AsProperty();
1414 if (prop != NULL) {
1415 assign_type = (prop->key()->IsPropertyName())
1416 ? NAMED_PROPERTY
1417 : KEYED_PROPERTY;
1418 }
1419
1420 switch (assign_type) {
1421 case VARIABLE: {
1422 Variable* var = expr->AsVariableProxy()->var();
1423 EmitVariableAssignment(var, Token::ASSIGN, Expression::kEffect);
1424 break;
1425 }
1426 case NAMED_PROPERTY: {
1427 __ push(r0); // Preserve value.
1428 VisitForValue(prop->obj(), kAccumulator);
1429 __ mov(r1, r0);
1430 __ pop(r0); // Restore value.
1431 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
1432 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1433 __ Call(ic, RelocInfo::CODE_TARGET);
1434 break;
1435 }
1436 case KEYED_PROPERTY: {
1437 __ push(r0); // Preserve value.
1438 VisitForValue(prop->obj(), kStack);
1439 VisitForValue(prop->key(), kAccumulator);
1440 __ mov(r1, r0);
1441 __ pop(r2);
1442 __ pop(r0); // Restore value.
1443 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1444 __ Call(ic, RelocInfo::CODE_TARGET);
1445 break;
1446 }
1447 }
1448}
1449
1450
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001451void FullCodeGenerator::EmitVariableAssignment(Variable* var,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001452 Token::Value op,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001453 Expression::Context context) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001454 // Left-hand sides that rewrite to explicit property accesses do not reach
1455 // here.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001456 ASSERT(var != NULL);
1457 ASSERT(var->is_global() || var->slot() != NULL);
1458
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001459 if (var->is_global()) {
1460 ASSERT(!var->is_this());
1461 // Assignment to a global variable. Use inline caching for the
1462 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001463 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001464 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001465 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001466 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1467 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001468
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001469 } else if (var->mode() != Variable::CONST || op == Token::INIT_CONST) {
1470 // Perform the assignment for non-const variables and for initialization
1471 // of const variables. Const assignments are simply skipped.
1472 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001473 Slot* slot = var->slot();
1474 switch (slot->type()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001475 case Slot::PARAMETER:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001476 case Slot::LOCAL:
1477 if (op == Token::INIT_CONST) {
1478 // Detect const reinitialization by checking for the hole value.
1479 __ ldr(r1, MemOperand(fp, SlotOffset(slot)));
1480 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1481 __ cmp(r1, ip);
1482 __ b(ne, &done);
1483 }
1484 // Perform the assignment.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001485 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1486 break;
1487
1488 case Slot::CONTEXT: {
1489 MemOperand target = EmitSlotSearch(slot, r1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001490 if (op == Token::INIT_CONST) {
1491 // Detect const reinitialization by checking for the hole value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001492 __ ldr(r2, target);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001493 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001494 __ cmp(r2, ip);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001495 __ b(ne, &done);
1496 }
1497 // Perform the assignment and issue the write barrier.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001498 __ str(result_register(), target);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001499 // RecordWrite may destroy all its register arguments.
1500 __ mov(r3, result_register());
1501 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001502 __ RecordWrite(r1, Operand(offset), r2, r3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001503 break;
1504 }
1505
1506 case Slot::LOOKUP:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001507 // Call the runtime for the assignment. The runtime will ignore
1508 // const reinitialization.
1509 __ push(r0); // Value.
1510 __ mov(r0, Operand(slot->var()->name()));
1511 __ Push(cp, r0); // Context and name.
1512 if (op == Token::INIT_CONST) {
1513 // The runtime will ignore const redeclaration.
1514 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1515 } else {
1516 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1517 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001518 break;
1519 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001520 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001521 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001522
ager@chromium.org5c838252010-02-19 08:53:10 +00001523 Apply(context, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001524}
1525
1526
1527void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1528 // Assignment to a property, using a named store IC.
1529 Property* prop = expr->target()->AsProperty();
1530 ASSERT(prop != NULL);
1531 ASSERT(prop->key()->AsLiteral() != NULL);
1532
1533 // If the assignment starts a block of assignments to the same object,
1534 // change to slow case to avoid the quadratic behavior of repeatedly
1535 // adding fast properties.
1536 if (expr->starts_initialization_block()) {
1537 __ push(result_register());
1538 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1539 __ push(ip);
1540 __ CallRuntime(Runtime::kToSlowProperties, 1);
1541 __ pop(result_register());
1542 }
1543
1544 // Record source code position before IC call.
1545 SetSourcePosition(expr->position());
1546 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001547 // Load receiver to r1. Leave a copy in the stack if needed for turning the
1548 // receiver into fast case.
ager@chromium.org5c838252010-02-19 08:53:10 +00001549 if (expr->ends_initialization_block()) {
1550 __ ldr(r1, MemOperand(sp));
1551 } else {
1552 __ pop(r1);
1553 }
1554
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001555 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1556 __ Call(ic, RelocInfo::CODE_TARGET);
1557
1558 // If the assignment ends an initialization block, revert to fast case.
1559 if (expr->ends_initialization_block()) {
1560 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001561 // Receiver is under the result value.
1562 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001563 __ push(ip);
1564 __ CallRuntime(Runtime::kToFastProperties, 1);
1565 __ pop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001566 DropAndApply(1, context_, r0);
1567 } else {
1568 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001569 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001570}
1571
1572
1573void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1574 // Assignment to a property, using a keyed store IC.
1575
1576 // If the assignment starts a block of assignments to the same object,
1577 // change to slow case to avoid the quadratic behavior of repeatedly
1578 // adding fast properties.
1579 if (expr->starts_initialization_block()) {
1580 __ push(result_register());
1581 // Receiver is now under the key and value.
1582 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1583 __ push(ip);
1584 __ CallRuntime(Runtime::kToSlowProperties, 1);
1585 __ pop(result_register());
1586 }
1587
1588 // Record source code position before IC call.
1589 SetSourcePosition(expr->position());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001590 __ pop(r1); // Key.
1591 // Load receiver to r2. Leave a copy in the stack if needed for turning the
1592 // receiver into fast case.
1593 if (expr->ends_initialization_block()) {
1594 __ ldr(r2, MemOperand(sp));
1595 } else {
1596 __ pop(r2);
1597 }
1598
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001599 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1600 __ Call(ic, RelocInfo::CODE_TARGET);
1601
1602 // If the assignment ends an initialization block, revert to fast case.
1603 if (expr->ends_initialization_block()) {
1604 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001605 // Receiver is under the result value.
1606 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001607 __ push(ip);
1608 __ CallRuntime(Runtime::kToFastProperties, 1);
1609 __ pop(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001610 DropAndApply(1, context_, r0);
1611 } else {
1612 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001613 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001614}
1615
1616
1617void FullCodeGenerator::VisitProperty(Property* expr) {
1618 Comment cmnt(masm_, "[ Property");
1619 Expression* key = expr->key();
1620
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001621 if (key->IsPropertyName()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001622 VisitForValue(expr->obj(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001623 EmitNamedPropertyLoad(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001624 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001625 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001626 VisitForValue(expr->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001627 VisitForValue(expr->key(), kAccumulator);
1628 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001629 EmitKeyedPropertyLoad(expr);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001630 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001631 }
1632}
1633
1634void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001635 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001636 RelocInfo::Mode mode) {
1637 // Code common for calls using the IC.
1638 ZoneList<Expression*>* args = expr->arguments();
1639 int arg_count = args->length();
1640 for (int i = 0; i < arg_count; i++) {
1641 VisitForValue(args->at(i), kStack);
1642 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001643 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001644 // Record source position for debugger.
1645 SetSourcePosition(expr->position());
1646 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001647 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1648 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001649 __ Call(ic, mode);
1650 // Restore context register.
1651 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001652 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001653}
1654
1655
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001656void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
1657 Expression* key,
1658 RelocInfo::Mode mode) {
1659 // Code common for calls using the IC.
1660 ZoneList<Expression*>* args = expr->arguments();
1661 int arg_count = args->length();
1662 for (int i = 0; i < arg_count; i++) {
1663 VisitForValue(args->at(i), kStack);
1664 }
1665 VisitForValue(key, kAccumulator);
1666 __ mov(r2, r0);
1667 // Record source position for debugger.
1668 SetSourcePosition(expr->position());
1669 // Call the IC initialization code.
1670 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1671 Handle<Code> ic = CodeGenerator::ComputeKeyedCallInitialize(arg_count,
1672 in_loop);
1673 __ Call(ic, mode);
1674 // Restore context register.
1675 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1676 Apply(context_, r0);
1677}
1678
1679
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001680void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1681 // Code common for calls using the call stub.
1682 ZoneList<Expression*>* args = expr->arguments();
1683 int arg_count = args->length();
1684 for (int i = 0; i < arg_count; i++) {
1685 VisitForValue(args->at(i), kStack);
1686 }
1687 // Record source position for debugger.
1688 SetSourcePosition(expr->position());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001689 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1690 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001691 __ CallStub(&stub);
1692 // Restore context register.
1693 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001694 DropAndApply(1, context_, r0);
1695}
1696
1697
1698void FullCodeGenerator::VisitCall(Call* expr) {
1699 Comment cmnt(masm_, "[ Call");
1700 Expression* fun = expr->expression();
1701 Variable* var = fun->AsVariableProxy()->AsVariable();
1702
1703 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001704 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1705 // resolve the function we need to call and the receiver of the
1706 // call. Then we call the resolved function using the given
1707 // arguments.
1708 VisitForValue(fun, kStack);
1709 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1710 __ push(r2); // Reserved receiver slot.
1711
1712 // Push the arguments.
1713 ZoneList<Expression*>* args = expr->arguments();
1714 int arg_count = args->length();
1715 for (int i = 0; i < arg_count; i++) {
1716 VisitForValue(args->at(i), kStack);
1717 }
1718
1719 // Push copy of the function - found below the arguments.
1720 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1721 __ push(r1);
1722
1723 // Push copy of the first argument or undefined if it doesn't exist.
1724 if (arg_count > 0) {
1725 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1726 __ push(r1);
1727 } else {
1728 __ push(r2);
1729 }
1730
1731 // Push the receiver of the enclosing function and do runtime call.
1732 __ ldr(r1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1733 __ push(r1);
1734 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1735
1736 // The runtime call returns a pair of values in r0 (function) and
1737 // r1 (receiver). Touch up the stack with the right values.
1738 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1739 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
1740
1741 // Record source position for debugger.
1742 SetSourcePosition(expr->position());
1743 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1744 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1745 __ CallStub(&stub);
1746 // Restore context register.
1747 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1748 DropAndApply(1, context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001749 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001750 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001751 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001752 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001753 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1754 } else if (var != NULL && var->slot() != NULL &&
1755 var->slot()->type() == Slot::LOOKUP) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001756 // Call to a lookup slot (dynamically introduced variable). Call the
1757 // runtime to find the function to call (returned in eax) and the object
1758 // holding it (returned in edx).
1759 __ push(context_register());
1760 __ mov(r2, Operand(var->name()));
1761 __ push(r2);
1762 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1763 __ push(r0); // Function.
1764 __ push(r1); // Receiver.
1765 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001766 } else if (fun->AsProperty() != NULL) {
1767 // Call to an object property.
1768 Property* prop = fun->AsProperty();
1769 Literal* key = prop->key()->AsLiteral();
1770 if (key != NULL && key->handle()->IsSymbol()) {
1771 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001772 VisitForValue(prop->obj(), kStack);
1773 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1774 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001775 // Call to a keyed property.
1776 // For a synthetic property use keyed load IC followed by function call,
1777 // for a regular property use keyed CallIC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001778 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001779 if (prop->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001780 VisitForValue(prop->key(), kAccumulator);
1781 // Record source code position for IC call.
1782 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001783 __ pop(r1); // We do not need to keep the receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001784
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001785 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1786 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001787 // Push result (function).
1788 __ push(r0);
1789 // Push Global receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001790 __ ldr(r1, CodeGenerator::GlobalObject());
1791 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001792 __ push(r1);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001793 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001794 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001795 EmitKeyedCallWithIC(expr, prop->key(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001796 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001797 }
1798 } else {
1799 // Call to some other expression. If the expression is an anonymous
1800 // function literal not called in a loop, mark it as one that should
1801 // also use the fast code generator.
1802 FunctionLiteral* lit = fun->AsFunctionLiteral();
1803 if (lit != NULL &&
1804 lit->name()->Equals(Heap::empty_string()) &&
1805 loop_depth() == 0) {
1806 lit->set_try_full_codegen(true);
1807 }
1808 VisitForValue(fun, kStack);
1809 // Load global receiver object.
1810 __ ldr(r1, CodeGenerator::GlobalObject());
1811 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1812 __ push(r1);
1813 // Emit function call.
1814 EmitCallWithStub(expr);
1815 }
1816}
1817
1818
1819void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1820 Comment cmnt(masm_, "[ CallNew");
1821 // According to ECMA-262, section 11.2.2, page 44, the function
1822 // expression in new calls must be evaluated before the
1823 // arguments.
1824 // Push function on the stack.
1825 VisitForValue(expr->expression(), kStack);
1826
1827 // Push global object (receiver).
1828 __ ldr(r0, CodeGenerator::GlobalObject());
1829 __ push(r0);
1830 // Push the arguments ("left-to-right") on the stack.
1831 ZoneList<Expression*>* args = expr->arguments();
1832 int arg_count = args->length();
1833 for (int i = 0; i < arg_count; i++) {
1834 VisitForValue(args->at(i), kStack);
1835 }
1836
1837 // Call the construct call builtin that handles allocation and
1838 // constructor invocation.
1839 SetSourcePosition(expr->position());
1840
1841 // Load function, arg_count into r1 and r0.
1842 __ mov(r0, Operand(arg_count));
1843 // Function is in sp[arg_count + 1].
1844 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1845
1846 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1847 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
1848
1849 // Replace function on TOS with result in r0, or pop it.
1850 DropAndApply(1, context_, r0);
1851}
1852
1853
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001854void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1855 ASSERT(args->length() == 1);
1856
1857 VisitForValue(args->at(0), kAccumulator);
1858
1859 Label materialize_true, materialize_false;
1860 Label* if_true = NULL;
1861 Label* if_false = NULL;
1862 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1863
1864 __ BranchOnSmi(r0, if_true);
1865 __ b(if_false);
1866
1867 Apply(context_, if_true, if_false);
1868}
1869
1870
1871void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1872 ASSERT(args->length() == 1);
1873
1874 VisitForValue(args->at(0), kAccumulator);
1875
1876 Label materialize_true, materialize_false;
1877 Label* if_true = NULL;
1878 Label* if_false = NULL;
1879 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1880
1881 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
1882 __ b(eq, if_true);
1883 __ b(if_false);
1884
1885 Apply(context_, if_true, if_false);
1886}
1887
1888
1889void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1890 ASSERT(args->length() == 1);
1891
1892 VisitForValue(args->at(0), kAccumulator);
1893
1894 Label materialize_true, materialize_false;
1895 Label* if_true = NULL;
1896 Label* if_false = NULL;
1897 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1898 __ BranchOnSmi(r0, if_false);
1899 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1900 __ cmp(r0, ip);
1901 __ b(eq, if_true);
1902 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1903 // Undetectable objects behave like undefined when tested with typeof.
1904 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
1905 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1906 __ b(ne, if_false);
1907 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1908 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
1909 __ b(lt, if_false);
1910 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
1911 __ b(le, if_true);
1912 __ b(if_false);
1913
1914 Apply(context_, if_true, if_false);
1915}
1916
1917
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001918void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
1919 ASSERT(args->length() == 1);
1920
1921 VisitForValue(args->at(0), kAccumulator);
1922
1923 Label materialize_true, materialize_false;
1924 Label* if_true = NULL;
1925 Label* if_false = NULL;
1926 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1927
1928 __ BranchOnSmi(r0, if_false);
1929 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
1930 __ b(ge, if_true);
1931 __ b(if_false);
1932
1933 Apply(context_, if_true, if_false);
1934}
1935
1936
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001937void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
1938 ASSERT(args->length() == 1);
1939
1940 VisitForValue(args->at(0), kAccumulator);
1941
1942 Label materialize_true, materialize_false;
1943 Label* if_true = NULL;
1944 Label* if_false = NULL;
1945 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1946
1947 __ BranchOnSmi(r0, if_false);
1948 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1949 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
1950 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1951 __ b(ne, if_true);
1952 __ b(if_false);
1953
1954 Apply(context_, if_true, if_false);
1955}
1956
1957
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001958void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
1959 ZoneList<Expression*>* args) {
1960
1961 ASSERT(args->length() == 1);
1962
1963 VisitForValue(args->at(0), kAccumulator);
1964
1965 Label materialize_true, materialize_false;
1966 Label* if_true = NULL;
1967 Label* if_false = NULL;
1968 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1969
1970 // Just indicate false, as %_IsStringWrapperSafeForDefaultValueOf() is only
1971 // used in a few functions in runtime.js which should not normally be hit by
1972 // this compiler.
1973 __ jmp(if_false);
1974 Apply(context_, if_true, if_false);
1975}
1976
1977
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001978void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
1979 ASSERT(args->length() == 1);
1980
1981 VisitForValue(args->at(0), kAccumulator);
1982
1983 Label materialize_true, materialize_false;
1984 Label* if_true = NULL;
1985 Label* if_false = NULL;
1986 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1987
1988 __ BranchOnSmi(r0, if_false);
1989 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
1990 __ b(eq, if_true);
1991 __ b(if_false);
1992
1993 Apply(context_, if_true, if_false);
1994}
1995
1996
1997void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
1998 ASSERT(args->length() == 1);
1999
2000 VisitForValue(args->at(0), kAccumulator);
2001
2002 Label materialize_true, materialize_false;
2003 Label* if_true = NULL;
2004 Label* if_false = NULL;
2005 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2006
2007 __ BranchOnSmi(r0, if_false);
2008 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
2009 __ b(eq, if_true);
2010 __ b(if_false);
2011
2012 Apply(context_, if_true, if_false);
2013}
2014
2015
2016void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
2017 ASSERT(args->length() == 1);
2018
2019 VisitForValue(args->at(0), kAccumulator);
2020
2021 Label materialize_true, materialize_false;
2022 Label* if_true = NULL;
2023 Label* if_false = NULL;
2024 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2025
2026 __ BranchOnSmi(r0, if_false);
2027 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
2028 __ b(eq, if_true);
2029 __ b(if_false);
2030
2031 Apply(context_, if_true, if_false);
2032}
2033
2034
2035
2036void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
2037 ASSERT(args->length() == 0);
2038
2039 Label materialize_true, materialize_false;
2040 Label* if_true = NULL;
2041 Label* if_false = NULL;
2042 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2043
2044 // Get the frame pointer for the calling frame.
2045 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2046
2047 // Skip the arguments adaptor frame if it exists.
2048 Label check_frame_marker;
2049 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
2050 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2051 __ b(ne, &check_frame_marker);
2052 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
2053
2054 // Check the marker in the calling frame.
2055 __ bind(&check_frame_marker);
2056 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
2057 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
2058 __ b(eq, if_true);
2059 __ b(if_false);
2060
2061 Apply(context_, if_true, if_false);
2062}
2063
2064
2065void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
2066 ASSERT(args->length() == 2);
2067
2068 // Load the two objects into registers and perform the comparison.
2069 VisitForValue(args->at(0), kStack);
2070 VisitForValue(args->at(1), kAccumulator);
2071
2072 Label materialize_true, materialize_false;
2073 Label* if_true = NULL;
2074 Label* if_false = NULL;
2075 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2076
2077 __ pop(r1);
2078 __ cmp(r0, r1);
2079 __ b(eq, if_true);
2080 __ b(if_false);
2081
2082 Apply(context_, if_true, if_false);
2083}
2084
2085
2086void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
2087 ASSERT(args->length() == 1);
2088
2089 // ArgumentsAccessStub expects the key in edx and the formal
2090 // parameter count in eax.
2091 VisitForValue(args->at(0), kAccumulator);
2092 __ mov(r1, r0);
2093 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2094 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2095 __ CallStub(&stub);
2096 Apply(context_, r0);
2097}
2098
2099
2100void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
2101 ASSERT(args->length() == 0);
2102
2103 Label exit;
2104 // Get the number of formal parameters.
2105 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2106
2107 // Check if the calling frame is an arguments adaptor frame.
2108 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2109 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
2110 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2111 __ b(ne, &exit);
2112
2113 // Arguments adaptor case: Read the arguments length from the
2114 // adaptor frame.
2115 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2116
2117 __ bind(&exit);
2118 Apply(context_, r0);
2119}
2120
2121
2122void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
2123 ASSERT(args->length() == 1);
2124 Label done, null, function, non_function_constructor;
2125
2126 VisitForValue(args->at(0), kAccumulator);
2127
2128 // If the object is a smi, we return null.
2129 __ BranchOnSmi(r0, &null);
2130
2131 // Check that the object is a JS object but take special care of JS
2132 // functions to make sure they have 'Function' as their class.
2133 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2134 __ b(lt, &null);
2135
2136 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2137 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2138 // LAST_JS_OBJECT_TYPE.
2139 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2140 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2141 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2142 __ b(eq, &function);
2143
2144 // Check if the constructor in the map is a function.
2145 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2146 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2147 __ b(ne, &non_function_constructor);
2148
2149 // r0 now contains the constructor function. Grab the
2150 // instance class name from there.
2151 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2152 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2153 __ b(&done);
2154
2155 // Functions have class 'Function'.
2156 __ bind(&function);
2157 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2158 __ jmp(&done);
2159
2160 // Objects with a non-function constructor have class 'Object'.
2161 __ bind(&non_function_constructor);
2162 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2163 __ jmp(&done);
2164
2165 // Non-JS objects have class null.
2166 __ bind(&null);
2167 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2168
2169 // All done.
2170 __ bind(&done);
2171
2172 Apply(context_, r0);
2173}
2174
2175
2176void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2177 // Conditionally generate a log call.
2178 // Args:
2179 // 0 (literal string): The type of logging (corresponds to the flags).
2180 // This is used to determine whether or not to generate the log call.
2181 // 1 (string): Format string. Access the string at argument index 2
2182 // with '%2s' (see Logger::LogRuntime for all the formats).
2183 // 2 (array): Arguments to the format string.
2184 ASSERT_EQ(args->length(), 3);
2185#ifdef ENABLE_LOGGING_AND_PROFILING
2186 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2187 VisitForValue(args->at(1), kStack);
2188 VisitForValue(args->at(2), kStack);
2189 __ CallRuntime(Runtime::kLog, 2);
2190 }
2191#endif
2192 // Finally, we're expected to leave a value on the top of the stack.
2193 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2194 Apply(context_, r0);
2195}
2196
2197
2198void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2199 ASSERT(args->length() == 0);
2200
2201 Label slow_allocate_heapnumber;
2202 Label heapnumber_allocated;
2203
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002204 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
2205 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002206 __ jmp(&heapnumber_allocated);
2207
2208 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002209 // Allocate a heap number.
2210 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002211 __ mov(r4, Operand(r0));
2212
2213 __ bind(&heapnumber_allocated);
2214
2215 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
2216 // by computing:
2217 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2218 if (CpuFeatures::IsSupported(VFP3)) {
2219 __ PrepareCallCFunction(0, r1);
2220 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
2221
2222 CpuFeatures::Scope scope(VFP3);
2223 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2224 // Create this constant using mov/orr to avoid PC relative load.
2225 __ mov(r1, Operand(0x41000000));
2226 __ orr(r1, r1, Operand(0x300000));
2227 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
2228 __ vmov(d7, r0, r1);
2229 // Move 0x4130000000000000 to VFP.
2230 __ mov(r0, Operand(0));
2231 __ vmov(d8, r0, r1);
2232 // Subtract and store the result in the heap number.
2233 __ vsub(d7, d7, d8);
2234 __ sub(r0, r4, Operand(kHeapObjectTag));
2235 __ vstr(d7, r0, HeapNumber::kValueOffset);
2236 __ mov(r0, r4);
2237 } else {
2238 __ mov(r0, Operand(r4));
2239 __ PrepareCallCFunction(1, r1);
2240 __ CallCFunction(
2241 ExternalReference::fill_heap_number_with_random_function(), 1);
2242 }
2243
2244 Apply(context_, r0);
2245}
2246
2247
2248void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
2249 // Load the arguments on the stack and call the stub.
2250 SubStringStub stub;
2251 ASSERT(args->length() == 3);
2252 VisitForValue(args->at(0), kStack);
2253 VisitForValue(args->at(1), kStack);
2254 VisitForValue(args->at(2), kStack);
2255 __ CallStub(&stub);
2256 Apply(context_, r0);
2257}
2258
2259
2260void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
2261 // Load the arguments on the stack and call the stub.
2262 RegExpExecStub stub;
2263 ASSERT(args->length() == 4);
2264 VisitForValue(args->at(0), kStack);
2265 VisitForValue(args->at(1), kStack);
2266 VisitForValue(args->at(2), kStack);
2267 VisitForValue(args->at(3), kStack);
2268 __ CallStub(&stub);
2269 Apply(context_, r0);
2270}
2271
2272
2273void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
2274 ASSERT(args->length() == 1);
2275
2276 VisitForValue(args->at(0), kAccumulator); // Load the object.
2277
2278 Label done;
2279 // If the object is a smi return the object.
2280 __ BranchOnSmi(r0, &done);
2281 // If the object is not a value type, return the object.
2282 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
2283 __ b(ne, &done);
2284 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2285
2286 __ bind(&done);
2287 Apply(context_, r0);
2288}
2289
2290
2291void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
2292 // Load the arguments on the stack and call the runtime function.
2293 ASSERT(args->length() == 2);
2294 VisitForValue(args->at(0), kStack);
2295 VisitForValue(args->at(1), kStack);
2296 __ CallRuntime(Runtime::kMath_pow, 2);
2297 Apply(context_, r0);
2298}
2299
2300
2301void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
2302 ASSERT(args->length() == 2);
2303
2304 VisitForValue(args->at(0), kStack); // Load the object.
2305 VisitForValue(args->at(1), kAccumulator); // Load the value.
2306 __ pop(r1); // r0 = value. r1 = object.
2307
2308 Label done;
2309 // If the object is a smi, return the value.
2310 __ BranchOnSmi(r1, &done);
2311
2312 // If the object is not a value type, return the value.
2313 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
2314 __ b(ne, &done);
2315
2316 // Store the value.
2317 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2318 // Update the write barrier. Save the value as it will be
2319 // overwritten by the write barrier code and is needed afterward.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002320 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002321
2322 __ bind(&done);
2323 Apply(context_, r0);
2324}
2325
2326
2327void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2328 ASSERT_EQ(args->length(), 1);
2329
2330 // Load the argument on the stack and call the stub.
2331 VisitForValue(args->at(0), kStack);
2332
2333 NumberToStringStub stub;
2334 __ CallStub(&stub);
2335 Apply(context_, r0);
2336}
2337
2338
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002339void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002340 ASSERT(args->length() == 1);
2341
2342 VisitForValue(args->at(0), kAccumulator);
2343
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002344 Label done;
2345 StringCharFromCodeGenerator generator(r0, r1);
2346 generator.GenerateFast(masm_);
2347 __ jmp(&done);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002348
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002349 NopRuntimeCallHelper call_helper;
2350 generator.GenerateSlow(masm_, call_helper);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002351
2352 __ bind(&done);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002353 Apply(context_, r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002354}
2355
2356
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002357void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
2358 ASSERT(args->length() == 2);
2359
2360 VisitForValue(args->at(0), kStack);
2361 VisitForValue(args->at(1), kAccumulator);
2362
2363 Register object = r1;
2364 Register index = r0;
2365 Register scratch = r2;
2366 Register result = r3;
2367
2368 __ pop(object);
2369
2370 Label need_conversion;
2371 Label index_out_of_range;
2372 Label done;
2373 StringCharCodeAtGenerator generator(object,
2374 index,
2375 scratch,
2376 result,
2377 &need_conversion,
2378 &need_conversion,
2379 &index_out_of_range,
2380 STRING_INDEX_IS_NUMBER);
2381 generator.GenerateFast(masm_);
2382 __ jmp(&done);
2383
2384 __ bind(&index_out_of_range);
2385 // When the index is out of range, the spec requires us to return
2386 // NaN.
2387 __ LoadRoot(result, Heap::kNanValueRootIndex);
2388 __ jmp(&done);
2389
2390 __ bind(&need_conversion);
2391 // Load the undefined value into the result register, which will
2392 // trigger conversion.
2393 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2394 __ jmp(&done);
2395
2396 NopRuntimeCallHelper call_helper;
2397 generator.GenerateSlow(masm_, call_helper);
2398
2399 __ bind(&done);
2400 Apply(context_, result);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002401}
2402
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002403
2404void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
2405 ASSERT(args->length() == 2);
2406
2407 VisitForValue(args->at(0), kStack);
2408 VisitForValue(args->at(1), kAccumulator);
2409
2410 Register object = r1;
2411 Register index = r0;
2412 Register scratch1 = r2;
2413 Register scratch2 = r3;
2414 Register result = r0;
2415
2416 __ pop(object);
2417
2418 Label need_conversion;
2419 Label index_out_of_range;
2420 Label done;
2421 StringCharAtGenerator generator(object,
2422 index,
2423 scratch1,
2424 scratch2,
2425 result,
2426 &need_conversion,
2427 &need_conversion,
2428 &index_out_of_range,
2429 STRING_INDEX_IS_NUMBER);
2430 generator.GenerateFast(masm_);
2431 __ jmp(&done);
2432
2433 __ bind(&index_out_of_range);
2434 // When the index is out of range, the spec requires us to return
2435 // the empty string.
2436 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2437 __ jmp(&done);
2438
2439 __ bind(&need_conversion);
2440 // Move smi zero into the result register, which will trigger
2441 // conversion.
2442 __ mov(result, Operand(Smi::FromInt(0)));
2443 __ jmp(&done);
2444
2445 NopRuntimeCallHelper call_helper;
2446 generator.GenerateSlow(masm_, call_helper);
2447
2448 __ bind(&done);
2449 Apply(context_, result);
2450}
2451
2452
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002453void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2454 ASSERT_EQ(2, args->length());
2455
2456 VisitForValue(args->at(0), kStack);
2457 VisitForValue(args->at(1), kStack);
2458
2459 StringAddStub stub(NO_STRING_ADD_FLAGS);
2460 __ CallStub(&stub);
2461 Apply(context_, r0);
2462}
2463
2464
2465void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
2466 ASSERT_EQ(2, args->length());
2467
2468 VisitForValue(args->at(0), kStack);
2469 VisitForValue(args->at(1), kStack);
2470
2471 StringCompareStub stub;
2472 __ CallStub(&stub);
2473 Apply(context_, r0);
2474}
2475
2476
2477void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
2478 // Load the argument on the stack and call the runtime.
2479 ASSERT(args->length() == 1);
2480 VisitForValue(args->at(0), kStack);
2481 __ CallRuntime(Runtime::kMath_sin, 1);
2482 Apply(context_, r0);
2483}
2484
2485
2486void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
2487 // Load the argument on the stack and call the runtime.
2488 ASSERT(args->length() == 1);
2489 VisitForValue(args->at(0), kStack);
2490 __ CallRuntime(Runtime::kMath_cos, 1);
2491 Apply(context_, r0);
2492}
2493
2494
2495void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2496 // Load the argument on the stack and call the runtime function.
2497 ASSERT(args->length() == 1);
2498 VisitForValue(args->at(0), kStack);
2499 __ CallRuntime(Runtime::kMath_sqrt, 1);
2500 Apply(context_, r0);
2501}
2502
2503
2504void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2505 ASSERT(args->length() >= 2);
2506
2507 int arg_count = args->length() - 2; // For receiver and function.
2508 VisitForValue(args->at(0), kStack); // Receiver.
2509 for (int i = 0; i < arg_count; i++) {
2510 VisitForValue(args->at(i + 1), kStack);
2511 }
2512 VisitForValue(args->at(arg_count + 1), kAccumulator); // Function.
2513
2514 // InvokeFunction requires function in r1. Move it in there.
2515 if (!result_register().is(r1)) __ mov(r1, result_register());
2516 ParameterCount count(arg_count);
2517 __ InvokeFunction(r1, count, CALL_FUNCTION);
2518 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2519 Apply(context_, r0);
2520}
2521
2522
2523void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2524 ASSERT(args->length() == 3);
2525 VisitForValue(args->at(0), kStack);
2526 VisitForValue(args->at(1), kStack);
2527 VisitForValue(args->at(2), kStack);
2528 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
2529 Apply(context_, r0);
2530}
2531
2532
2533void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2534 ASSERT(args->length() == 3);
2535 VisitForValue(args->at(0), kStack);
2536 VisitForValue(args->at(1), kStack);
2537 VisitForValue(args->at(2), kStack);
2538 __ CallRuntime(Runtime::kSwapElements, 3);
2539 Apply(context_, r0);
2540}
2541
2542
2543void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2544 ASSERT_EQ(2, args->length());
2545
2546 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2547 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2548
2549 Handle<FixedArray> jsfunction_result_caches(
2550 Top::global_context()->jsfunction_result_caches());
2551 if (jsfunction_result_caches->length() <= cache_id) {
2552 __ Abort("Attempt to use undefined cache.");
2553 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2554 Apply(context_, r0);
2555 return;
2556 }
2557
2558 VisitForValue(args->at(1), kAccumulator);
2559
2560 Register key = r0;
2561 Register cache = r1;
2562 __ ldr(cache, CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX));
2563 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
2564 __ ldr(cache,
2565 CodeGenerator::ContextOperand(
2566 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
2567 __ ldr(cache,
2568 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2569
2570
2571 Label done, not_found;
2572 // tmp now holds finger offset as a smi.
2573 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2574 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2575 // r2 now holds finger offset as a smi.
2576 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2577 // r3 now points to the start of fixed array elements.
2578 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2579 // Note side effect of PreIndex: r3 now points to the key of the pair.
2580 __ cmp(key, r2);
2581 __ b(ne, &not_found);
2582
2583 __ ldr(r0, MemOperand(r3, kPointerSize));
2584 __ b(&done);
2585
2586 __ bind(&not_found);
2587 // Call runtime to perform the lookup.
2588 __ Push(cache, key);
2589 __ CallRuntime(Runtime::kGetFromCache, 2);
2590
2591 __ bind(&done);
2592 Apply(context_, r0);
2593}
2594
2595
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002596void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
2597 ASSERT_EQ(2, args->length());
2598
2599 Register right = r0;
2600 Register left = r1;
2601 Register tmp = r2;
2602 Register tmp2 = r3;
2603
2604 VisitForValue(args->at(0), kStack);
2605 VisitForValue(args->at(1), kAccumulator);
2606 __ pop(left);
2607
2608 Label done, fail, ok;
2609 __ cmp(left, Operand(right));
2610 __ b(eq, &ok);
2611 // Fail if either is a non-HeapObject.
2612 __ and_(tmp, left, Operand(right));
2613 __ tst(tmp, Operand(kSmiTagMask));
2614 __ b(eq, &fail);
2615 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
2616 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
2617 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
2618 __ b(ne, &fail);
2619 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
2620 __ cmp(tmp, Operand(tmp2));
2621 __ b(ne, &fail);
2622 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
2623 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
2624 __ cmp(tmp, tmp2);
2625 __ b(eq, &ok);
2626 __ bind(&fail);
2627 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
2628 __ jmp(&done);
2629 __ bind(&ok);
2630 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
2631 __ bind(&done);
2632
2633 Apply(context_, r0);
2634}
2635
2636
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002637void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002638 Handle<String> name = expr->name();
2639 if (name->length() > 0 && name->Get(0) == '_') {
2640 Comment cmnt(masm_, "[ InlineRuntimeCall");
2641 EmitInlineRuntimeCall(expr);
2642 return;
2643 }
2644
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002645 Comment cmnt(masm_, "[ CallRuntime");
2646 ZoneList<Expression*>* args = expr->arguments();
2647
2648 if (expr->is_jsruntime()) {
2649 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002650 __ ldr(r0, CodeGenerator::GlobalObject());
2651 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002652 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002653 }
2654
2655 // Push the arguments ("left-to-right").
2656 int arg_count = args->length();
2657 for (int i = 0; i < arg_count; i++) {
2658 VisitForValue(args->at(i), kStack);
2659 }
2660
2661 if (expr->is_jsruntime()) {
2662 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002663 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002664 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2665 NOT_IN_LOOP);
2666 __ Call(ic, RelocInfo::CODE_TARGET);
2667 // Restore context register.
2668 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002669 } else {
2670 // Call the C runtime function.
2671 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002672 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002673 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002674}
2675
2676
2677void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2678 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002679 case Token::DELETE: {
2680 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2681 Property* prop = expr->expression()->AsProperty();
2682 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2683 if (prop == NULL && var == NULL) {
2684 // Result of deleting non-property, non-variable reference is true.
2685 // The subexpression may have side effects.
2686 VisitForEffect(expr->expression());
2687 Apply(context_, true);
2688 } else if (var != NULL &&
2689 !var->is_global() &&
2690 var->slot() != NULL &&
2691 var->slot()->type() != Slot::LOOKUP) {
2692 // Result of deleting non-global, non-dynamic variables is false.
2693 // The subexpression does not have side effects.
2694 Apply(context_, false);
2695 } else {
2696 // Property or variable reference. Call the delete builtin with
2697 // object and property name as arguments.
2698 if (prop != NULL) {
2699 VisitForValue(prop->obj(), kStack);
2700 VisitForValue(prop->key(), kStack);
2701 } else if (var->is_global()) {
2702 __ ldr(r1, CodeGenerator::GlobalObject());
2703 __ mov(r0, Operand(var->name()));
2704 __ Push(r1, r0);
2705 } else {
2706 // Non-global variable. Call the runtime to look up the context
2707 // where the variable was introduced.
2708 __ push(context_register());
2709 __ mov(r2, Operand(var->name()));
2710 __ push(r2);
2711 __ CallRuntime(Runtime::kLookupContext, 2);
2712 __ push(r0);
2713 __ mov(r2, Operand(var->name()));
2714 __ push(r2);
2715 }
2716 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
2717 Apply(context_, r0);
2718 }
2719 break;
2720 }
2721
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002722 case Token::VOID: {
2723 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2724 VisitForEffect(expr->expression());
2725 switch (context_) {
2726 case Expression::kUninitialized:
2727 UNREACHABLE();
2728 break;
2729 case Expression::kEffect:
2730 break;
2731 case Expression::kValue:
2732 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2733 switch (location_) {
2734 case kAccumulator:
2735 break;
2736 case kStack:
2737 __ push(result_register());
2738 break;
2739 }
2740 break;
2741 case Expression::kTestValue:
2742 // Value is false so it's needed.
2743 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2744 switch (location_) {
2745 case kAccumulator:
2746 break;
2747 case kStack:
2748 __ push(result_register());
2749 break;
2750 }
2751 // Fall through.
2752 case Expression::kTest:
2753 case Expression::kValueTest:
2754 __ jmp(false_label_);
2755 break;
2756 }
2757 break;
2758 }
2759
2760 case Token::NOT: {
2761 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002762 Label materialize_true, materialize_false;
2763 Label* if_true = NULL;
2764 Label* if_false = NULL;
2765
2766 // Notice that the labels are swapped.
2767 PrepareTest(&materialize_true, &materialize_false, &if_false, &if_true);
2768
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002769 VisitForControl(expr->expression(), if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002770
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002771 Apply(context_, if_false, if_true); // Labels swapped.
2772 break;
2773 }
2774
2775 case Token::TYPEOF: {
2776 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
2777 VariableProxy* proxy = expr->expression()->AsVariableProxy();
2778 if (proxy != NULL &&
2779 !proxy->var()->is_this() &&
2780 proxy->var()->is_global()) {
2781 Comment cmnt(masm_, "Global variable");
2782 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002783 __ mov(r2, Operand(proxy->name()));
2784 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2785 // Use a regular load, not a contextual load, to avoid a reference
2786 // error.
2787 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002788 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002789 } else if (proxy != NULL &&
2790 proxy->var()->slot() != NULL &&
2791 proxy->var()->slot()->type() == Slot::LOOKUP) {
2792 __ mov(r0, Operand(proxy->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002793 __ Push(cp, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002794 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
2795 __ push(r0);
2796 } else {
2797 // This expression cannot throw a reference error at the top level.
2798 VisitForValue(expr->expression(), kStack);
2799 }
2800
2801 __ CallRuntime(Runtime::kTypeof, 1);
2802 Apply(context_, r0);
2803 break;
2804 }
2805
2806 case Token::ADD: {
2807 Comment cmt(masm_, "[ UnaryOperation (ADD)");
2808 VisitForValue(expr->expression(), kAccumulator);
2809 Label no_conversion;
2810 __ tst(result_register(), Operand(kSmiTagMask));
2811 __ b(eq, &no_conversion);
2812 __ push(r0);
2813 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2814 __ bind(&no_conversion);
2815 Apply(context_, result_register());
2816 break;
2817 }
2818
2819 case Token::SUB: {
2820 Comment cmt(masm_, "[ UnaryOperation (SUB)");
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002821 bool can_overwrite =
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002822 (expr->expression()->AsBinaryOperation() != NULL &&
2823 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002824 UnaryOverwriteMode overwrite =
2825 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002826 GenericUnaryOpStub stub(Token::SUB, overwrite);
2827 // GenericUnaryOpStub expects the argument to be in the
2828 // accumulator register r0.
2829 VisitForValue(expr->expression(), kAccumulator);
2830 __ CallStub(&stub);
2831 Apply(context_, r0);
2832 break;
2833 }
2834
2835 case Token::BIT_NOT: {
2836 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002837 bool can_overwrite =
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002838 (expr->expression()->AsBinaryOperation() != NULL &&
2839 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002840 UnaryOverwriteMode overwrite =
2841 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002842 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
2843 // GenericUnaryOpStub expects the argument to be in the
2844 // accumulator register r0.
2845 VisitForValue(expr->expression(), kAccumulator);
2846 // Avoid calling the stub for Smis.
2847 Label smi, done;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002848 __ BranchOnSmi(result_register(), &smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002849 // Non-smi: call stub leaving result in accumulator register.
2850 __ CallStub(&stub);
2851 __ b(&done);
2852 // Perform operation directly on Smis.
2853 __ bind(&smi);
2854 __ mvn(result_register(), Operand(result_register()));
2855 // Bit-clear inverted smi-tag.
2856 __ bic(result_register(), result_register(), Operand(kSmiTagMask));
2857 __ bind(&done);
2858 Apply(context_, result_register());
2859 break;
2860 }
2861
2862 default:
2863 UNREACHABLE();
2864 }
2865}
2866
2867
2868void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2869 Comment cmnt(masm_, "[ CountOperation");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002870 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2871 // as the left-hand side.
2872 if (!expr->expression()->IsValidLeftHandSide()) {
2873 VisitForEffect(expr->expression());
2874 return;
2875 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002876
2877 // Expression can only be a property, a global or a (parameter or local)
2878 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2879 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2880 LhsKind assign_type = VARIABLE;
2881 Property* prop = expr->expression()->AsProperty();
2882 // In case of a property we use the uninitialized expression context
2883 // of the key to detect a named property.
2884 if (prop != NULL) {
2885 assign_type =
2886 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2887 }
2888
2889 // Evaluate expression and get value.
2890 if (assign_type == VARIABLE) {
2891 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
2892 Location saved_location = location_;
2893 location_ = kAccumulator;
2894 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
2895 Expression::kValue);
2896 location_ = saved_location;
2897 } else {
2898 // Reserve space for result of postfix operation.
2899 if (expr->is_postfix() && context_ != Expression::kEffect) {
2900 __ mov(ip, Operand(Smi::FromInt(0)));
2901 __ push(ip);
2902 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002903 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002904 // Put the object both on the stack and in the accumulator.
2905 VisitForValue(prop->obj(), kAccumulator);
2906 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002907 EmitNamedPropertyLoad(prop);
2908 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002909 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002910 VisitForValue(prop->key(), kAccumulator);
2911 __ ldr(r1, MemOperand(sp, 0));
2912 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002913 EmitKeyedPropertyLoad(prop);
2914 }
2915 }
2916
2917 // Call ToNumber only if operand is not a smi.
2918 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002919 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002920 __ push(r0);
2921 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2922 __ bind(&no_conversion);
2923
2924 // Save result for postfix expressions.
2925 if (expr->is_postfix()) {
2926 switch (context_) {
2927 case Expression::kUninitialized:
2928 UNREACHABLE();
2929 case Expression::kEffect:
2930 // Do not save result.
2931 break;
2932 case Expression::kValue:
2933 case Expression::kTest:
2934 case Expression::kValueTest:
2935 case Expression::kTestValue:
2936 // Save the result on the stack. If we have a named or keyed property
2937 // we store the result under the receiver that is currently on top
2938 // of the stack.
2939 switch (assign_type) {
2940 case VARIABLE:
2941 __ push(r0);
2942 break;
2943 case NAMED_PROPERTY:
2944 __ str(r0, MemOperand(sp, kPointerSize));
2945 break;
2946 case KEYED_PROPERTY:
2947 __ str(r0, MemOperand(sp, 2 * kPointerSize));
2948 break;
2949 }
2950 break;
2951 }
2952 }
2953
2954
2955 // Inline smi case if we are in a loop.
2956 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002957 int count_value = expr->op() == Token::INC ? 1 : -1;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002958 if (loop_depth() > 0) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002959 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002960 __ b(vs, &stub_call);
2961 // We could eliminate this smi check if we split the code at
2962 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002963 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002964 __ bind(&stub_call);
2965 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002966 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002967 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002968 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00002969 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002970 __ CallStub(&stub);
2971 __ bind(&done);
2972
2973 // Store the value returned in r0.
2974 switch (assign_type) {
2975 case VARIABLE:
2976 if (expr->is_postfix()) {
2977 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002978 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002979 Expression::kEffect);
2980 // For all contexts except kEffect: We have the result on
2981 // top of the stack.
2982 if (context_ != Expression::kEffect) {
2983 ApplyTOS(context_);
2984 }
2985 } else {
2986 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002987 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002988 context_);
2989 }
2990 break;
2991 case NAMED_PROPERTY: {
2992 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00002993 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002994 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2995 __ Call(ic, RelocInfo::CODE_TARGET);
2996 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002997 if (context_ != Expression::kEffect) {
2998 ApplyTOS(context_);
2999 }
3000 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00003001 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003002 }
3003 break;
3004 }
3005 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003006 __ pop(r1); // Key.
3007 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003008 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
3009 __ Call(ic, RelocInfo::CODE_TARGET);
3010 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003011 if (context_ != Expression::kEffect) {
3012 ApplyTOS(context_);
3013 }
3014 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003015 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003016 }
3017 break;
3018 }
3019 }
3020}
3021
3022
3023void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
3024 Comment cmnt(masm_, "[ BinaryOperation");
3025 switch (expr->op()) {
3026 case Token::COMMA:
3027 VisitForEffect(expr->left());
3028 Visit(expr->right());
3029 break;
3030
3031 case Token::OR:
3032 case Token::AND:
3033 EmitLogicalOperation(expr);
3034 break;
3035
3036 case Token::ADD:
3037 case Token::SUB:
3038 case Token::DIV:
3039 case Token::MOD:
3040 case Token::MUL:
3041 case Token::BIT_OR:
3042 case Token::BIT_AND:
3043 case Token::BIT_XOR:
3044 case Token::SHL:
3045 case Token::SHR:
3046 case Token::SAR:
3047 VisitForValue(expr->left(), kStack);
3048 VisitForValue(expr->right(), kAccumulator);
3049 EmitBinaryOp(expr->op(), context_);
3050 break;
3051
3052 default:
3053 UNREACHABLE();
3054 }
3055}
3056
3057
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003058void FullCodeGenerator::EmitNullCompare(bool strict,
3059 Register obj,
3060 Register null_const,
3061 Label* if_true,
3062 Label* if_false,
3063 Register scratch) {
3064 __ cmp(obj, null_const);
3065 if (strict) {
3066 __ b(eq, if_true);
3067 } else {
3068 __ b(eq, if_true);
3069 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3070 __ cmp(obj, ip);
3071 __ b(eq, if_true);
3072 __ BranchOnSmi(obj, if_false);
3073 // It can be an undetectable object.
3074 __ ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
3075 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
3076 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
3077 __ b(ne, if_true);
3078 }
3079 __ jmp(if_false);
3080}
3081
3082
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003083void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
3084 Comment cmnt(masm_, "[ CompareOperation");
3085
3086 // Always perform the comparison for its control flow. Pack the result
3087 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003088
3089 Label materialize_true, materialize_false;
3090 Label* if_true = NULL;
3091 Label* if_false = NULL;
3092 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003093
3094 VisitForValue(expr->left(), kStack);
3095 switch (expr->op()) {
3096 case Token::IN:
3097 VisitForValue(expr->right(), kStack);
3098 __ InvokeBuiltin(Builtins::IN, CALL_JS);
3099 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3100 __ cmp(r0, ip);
3101 __ b(eq, if_true);
3102 __ jmp(if_false);
3103 break;
3104
3105 case Token::INSTANCEOF: {
3106 VisitForValue(expr->right(), kStack);
3107 InstanceofStub stub;
3108 __ CallStub(&stub);
3109 __ tst(r0, r0);
3110 __ b(eq, if_true); // The stub returns 0 for true.
3111 __ jmp(if_false);
3112 break;
3113 }
3114
3115 default: {
3116 VisitForValue(expr->right(), kAccumulator);
3117 Condition cc = eq;
3118 bool strict = false;
3119 switch (expr->op()) {
3120 case Token::EQ_STRICT:
3121 strict = true;
3122 // Fall through
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003123 case Token::EQ: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003124 cc = eq;
3125 __ pop(r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003126 // If either operand is constant null we do a fast compare
3127 // against null.
3128 Literal* right_literal = expr->right()->AsLiteral();
3129 Literal* left_literal = expr->left()->AsLiteral();
3130 if (right_literal != NULL && right_literal->handle()->IsNull()) {
3131 EmitNullCompare(strict, r1, r0, if_true, if_false, r2);
3132 Apply(context_, if_true, if_false);
3133 return;
3134 } else if (left_literal != NULL && left_literal->handle()->IsNull()) {
3135 EmitNullCompare(strict, r0, r1, if_true, if_false, r2);
3136 Apply(context_, if_true, if_false);
3137 return;
3138 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003139 break;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003140 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003141 case Token::LT:
3142 cc = lt;
3143 __ pop(r1);
3144 break;
3145 case Token::GT:
3146 // Reverse left and right sides to obtain ECMA-262 conversion order.
3147 cc = lt;
3148 __ mov(r1, result_register());
3149 __ pop(r0);
3150 break;
3151 case Token::LTE:
3152 // Reverse left and right sides to obtain ECMA-262 conversion order.
3153 cc = ge;
3154 __ mov(r1, result_register());
3155 __ pop(r0);
3156 break;
3157 case Token::GTE:
3158 cc = ge;
3159 __ pop(r1);
3160 break;
3161 case Token::IN:
3162 case Token::INSTANCEOF:
3163 default:
3164 UNREACHABLE();
3165 }
3166
3167 // The comparison stub expects the smi vs. smi case to be handled
3168 // before it is called.
3169 Label slow_case;
3170 __ orr(r2, r0, Operand(r1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003171 __ BranchOnNotSmi(r2, &slow_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003172 __ cmp(r1, r0);
3173 __ b(cc, if_true);
3174 __ jmp(if_false);
3175
3176 __ bind(&slow_case);
ager@chromium.orgb5737492010-07-15 09:29:43 +00003177 CompareStub stub(cc, strict, kBothCouldBeNaN, true, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003178 __ CallStub(&stub);
3179 __ cmp(r0, Operand(0));
3180 __ b(cc, if_true);
3181 __ jmp(if_false);
3182 }
3183 }
3184
3185 // Convert the result of the comparison into one expected for this
3186 // expression's context.
3187 Apply(context_, if_true, if_false);
3188}
3189
3190
3191void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3192 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3193 Apply(context_, r0);
3194}
3195
3196
3197Register FullCodeGenerator::result_register() { return r0; }
3198
3199
3200Register FullCodeGenerator::context_register() { return cp; }
3201
3202
3203void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3204 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3205 __ str(value, MemOperand(fp, frame_offset));
3206}
3207
3208
3209void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
3210 __ ldr(dst, CodeGenerator::ContextOperand(cp, context_index));
3211}
3212
3213
3214// ----------------------------------------------------------------------------
3215// Non-local control flow support.
3216
3217void FullCodeGenerator::EnterFinallyBlock() {
3218 ASSERT(!result_register().is(r1));
3219 // Store result register while executing finally block.
3220 __ push(result_register());
3221 // Cook return address in link register to stack (smi encoded Code* delta)
3222 __ sub(r1, lr, Operand(masm_->CodeObject()));
3223 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3224 ASSERT_EQ(0, kSmiTag);
3225 __ add(r1, r1, Operand(r1)); // Convert to smi.
3226 __ push(r1);
3227}
3228
3229
3230void FullCodeGenerator::ExitFinallyBlock() {
3231 ASSERT(!result_register().is(r1));
3232 // Restore result register from stack.
3233 __ pop(r1);
3234 // Uncook return address and return.
3235 __ pop(result_register());
3236 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3237 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3238 __ add(pc, r1, Operand(masm_->CodeObject()));
3239}
3240
3241
3242#undef __
3243
3244} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003245
3246#endif // V8_TARGET_ARCH_ARM