blob: f3adab2814eb76e20d50b894b414665d2ccb64aa [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);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001226 if (expr->constant_elements()->map() == Heap::fixed_cow_array_map()) {
1227 FastCloneShallowArrayStub stub(
1228 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1229 __ CallStub(&stub);
1230 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
1231 } else if (expr->depth() > 1) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001232 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001233 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001234 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001235 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001236 FastCloneShallowArrayStub stub(
1237 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001238 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001239 }
1240
1241 bool result_saved = false; // Is the result saved to the stack?
1242
1243 // Emit code to evaluate all the non-constant subexpressions and to store
1244 // them into the newly cloned array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001245 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001246 Expression* subexpr = subexprs->at(i);
1247 // If the subexpression is a literal or a simple materialized literal it
1248 // is already set in the cloned array.
1249 if (subexpr->AsLiteral() != NULL ||
1250 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1251 continue;
1252 }
1253
1254 if (!result_saved) {
1255 __ push(r0);
1256 result_saved = true;
1257 }
1258 VisitForValue(subexpr, kAccumulator);
1259
1260 // Store the subexpression value in the array's elements.
1261 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
1262 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1263 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1264 __ str(result_register(), FieldMemOperand(r1, offset));
1265
1266 // Update the write barrier for the array store with r0 as the scratch
1267 // register.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001268 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001269 }
1270
1271 if (result_saved) {
1272 ApplyTOS(context_);
1273 } else {
1274 Apply(context_, r0);
1275 }
1276}
1277
1278
ager@chromium.org5c838252010-02-19 08:53:10 +00001279void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1280 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001281 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1282 // on the left-hand side.
1283 if (!expr->target()->IsValidLeftHandSide()) {
1284 VisitForEffect(expr->target());
1285 return;
1286 }
1287
ager@chromium.org5c838252010-02-19 08:53:10 +00001288 // Left-hand side can only be a property, a global or a (parameter or local)
1289 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1290 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1291 LhsKind assign_type = VARIABLE;
1292 Property* prop = expr->target()->AsProperty();
1293 if (prop != NULL) {
1294 assign_type =
1295 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
1296 }
1297
1298 // Evaluate LHS expression.
1299 switch (assign_type) {
1300 case VARIABLE:
1301 // Nothing to do here.
1302 break;
1303 case NAMED_PROPERTY:
1304 if (expr->is_compound()) {
1305 // We need the receiver both on the stack and in the accumulator.
1306 VisitForValue(prop->obj(), kAccumulator);
1307 __ push(result_register());
1308 } else {
1309 VisitForValue(prop->obj(), kStack);
1310 }
1311 break;
1312 case KEYED_PROPERTY:
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001313 // We need the key and receiver on both the stack and in r0 and r1.
1314 if (expr->is_compound()) {
1315 VisitForValue(prop->obj(), kStack);
1316 VisitForValue(prop->key(), kAccumulator);
1317 __ ldr(r1, MemOperand(sp, 0));
1318 __ push(r0);
1319 } else {
1320 VisitForValue(prop->obj(), kStack);
1321 VisitForValue(prop->key(), kStack);
1322 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001323 break;
1324 }
1325
1326 // If we have a compound assignment: Get value of LHS expression and
1327 // store in on top of the stack.
1328 if (expr->is_compound()) {
1329 Location saved_location = location_;
1330 location_ = kStack;
1331 switch (assign_type) {
1332 case VARIABLE:
1333 EmitVariableLoad(expr->target()->AsVariableProxy()->var(),
1334 Expression::kValue);
1335 break;
1336 case NAMED_PROPERTY:
1337 EmitNamedPropertyLoad(prop);
1338 __ push(result_register());
1339 break;
1340 case KEYED_PROPERTY:
1341 EmitKeyedPropertyLoad(prop);
1342 __ push(result_register());
1343 break;
1344 }
1345 location_ = saved_location;
1346 }
1347
1348 // Evaluate RHS expression.
1349 Expression* rhs = expr->value();
1350 VisitForValue(rhs, kAccumulator);
1351
1352 // If we have a compound assignment: Apply operator.
1353 if (expr->is_compound()) {
1354 Location saved_location = location_;
1355 location_ = kAccumulator;
1356 EmitBinaryOp(expr->binary_op(), Expression::kValue);
1357 location_ = saved_location;
1358 }
1359
1360 // Record source position before possible IC call.
1361 SetSourcePosition(expr->position());
1362
1363 // Store the value.
1364 switch (assign_type) {
1365 case VARIABLE:
1366 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001367 expr->op(),
ager@chromium.org5c838252010-02-19 08:53:10 +00001368 context_);
1369 break;
1370 case NAMED_PROPERTY:
1371 EmitNamedPropertyAssignment(expr);
1372 break;
1373 case KEYED_PROPERTY:
1374 EmitKeyedPropertyAssignment(expr);
1375 break;
1376 }
1377}
1378
1379
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001380void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1381 SetSourcePosition(prop->position());
1382 Literal* key = prop->key()->AsLiteral();
1383 __ mov(r2, Operand(key->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001384 // Call load IC. It has arguments receiver and property name r0 and r2.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001385 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1386 __ Call(ic, RelocInfo::CODE_TARGET);
1387}
1388
1389
1390void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1391 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001392 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001393 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1394 __ Call(ic, RelocInfo::CODE_TARGET);
1395}
1396
1397
1398void FullCodeGenerator::EmitBinaryOp(Token::Value op,
1399 Expression::Context context) {
1400 __ pop(r1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001401 GenericBinaryOpStub stub(op, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001402 __ CallStub(&stub);
1403 Apply(context, r0);
1404}
1405
1406
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001407void FullCodeGenerator::EmitAssignment(Expression* expr) {
1408 // Invalid left-hand sides are rewritten to have a 'throw
1409 // ReferenceError' on the left-hand side.
1410 if (!expr->IsValidLeftHandSide()) {
1411 VisitForEffect(expr);
1412 return;
1413 }
1414
1415 // Left-hand side can only be a property, a global or a (parameter or local)
1416 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1417 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1418 LhsKind assign_type = VARIABLE;
1419 Property* prop = expr->AsProperty();
1420 if (prop != NULL) {
1421 assign_type = (prop->key()->IsPropertyName())
1422 ? NAMED_PROPERTY
1423 : KEYED_PROPERTY;
1424 }
1425
1426 switch (assign_type) {
1427 case VARIABLE: {
1428 Variable* var = expr->AsVariableProxy()->var();
1429 EmitVariableAssignment(var, Token::ASSIGN, Expression::kEffect);
1430 break;
1431 }
1432 case NAMED_PROPERTY: {
1433 __ push(r0); // Preserve value.
1434 VisitForValue(prop->obj(), kAccumulator);
1435 __ mov(r1, r0);
1436 __ pop(r0); // Restore value.
1437 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
1438 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1439 __ Call(ic, RelocInfo::CODE_TARGET);
1440 break;
1441 }
1442 case KEYED_PROPERTY: {
1443 __ push(r0); // Preserve value.
1444 VisitForValue(prop->obj(), kStack);
1445 VisitForValue(prop->key(), kAccumulator);
1446 __ mov(r1, r0);
1447 __ pop(r2);
1448 __ pop(r0); // Restore value.
1449 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1450 __ Call(ic, RelocInfo::CODE_TARGET);
1451 break;
1452 }
1453 }
1454}
1455
1456
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001457void FullCodeGenerator::EmitVariableAssignment(Variable* var,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001458 Token::Value op,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001459 Expression::Context context) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001460 // Left-hand sides that rewrite to explicit property accesses do not reach
1461 // here.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001462 ASSERT(var != NULL);
1463 ASSERT(var->is_global() || var->slot() != NULL);
1464
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001465 if (var->is_global()) {
1466 ASSERT(!var->is_this());
1467 // Assignment to a global variable. Use inline caching for the
1468 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001469 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001470 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001471 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001472 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1473 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001474
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001475 } else if (var->mode() != Variable::CONST || op == Token::INIT_CONST) {
1476 // Perform the assignment for non-const variables and for initialization
1477 // of const variables. Const assignments are simply skipped.
1478 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001479 Slot* slot = var->slot();
1480 switch (slot->type()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001481 case Slot::PARAMETER:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001482 case Slot::LOCAL:
1483 if (op == Token::INIT_CONST) {
1484 // Detect const reinitialization by checking for the hole value.
1485 __ ldr(r1, MemOperand(fp, SlotOffset(slot)));
1486 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1487 __ cmp(r1, ip);
1488 __ b(ne, &done);
1489 }
1490 // Perform the assignment.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001491 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1492 break;
1493
1494 case Slot::CONTEXT: {
1495 MemOperand target = EmitSlotSearch(slot, r1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001496 if (op == Token::INIT_CONST) {
1497 // Detect const reinitialization by checking for the hole value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001498 __ ldr(r2, target);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001499 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001500 __ cmp(r2, ip);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001501 __ b(ne, &done);
1502 }
1503 // Perform the assignment and issue the write barrier.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001504 __ str(result_register(), target);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001505 // RecordWrite may destroy all its register arguments.
1506 __ mov(r3, result_register());
1507 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001508 __ RecordWrite(r1, Operand(offset), r2, r3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001509 break;
1510 }
1511
1512 case Slot::LOOKUP:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001513 // Call the runtime for the assignment. The runtime will ignore
1514 // const reinitialization.
1515 __ push(r0); // Value.
1516 __ mov(r0, Operand(slot->var()->name()));
1517 __ Push(cp, r0); // Context and name.
1518 if (op == Token::INIT_CONST) {
1519 // The runtime will ignore const redeclaration.
1520 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1521 } else {
1522 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1523 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001524 break;
1525 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001526 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001527 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001528
ager@chromium.org5c838252010-02-19 08:53:10 +00001529 Apply(context, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001530}
1531
1532
1533void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1534 // Assignment to a property, using a named store IC.
1535 Property* prop = expr->target()->AsProperty();
1536 ASSERT(prop != NULL);
1537 ASSERT(prop->key()->AsLiteral() != NULL);
1538
1539 // If the assignment starts a block of assignments to the same object,
1540 // change to slow case to avoid the quadratic behavior of repeatedly
1541 // adding fast properties.
1542 if (expr->starts_initialization_block()) {
1543 __ push(result_register());
1544 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1545 __ push(ip);
1546 __ CallRuntime(Runtime::kToSlowProperties, 1);
1547 __ pop(result_register());
1548 }
1549
1550 // Record source code position before IC call.
1551 SetSourcePosition(expr->position());
1552 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001553 // Load receiver to r1. Leave a copy in the stack if needed for turning the
1554 // receiver into fast case.
ager@chromium.org5c838252010-02-19 08:53:10 +00001555 if (expr->ends_initialization_block()) {
1556 __ ldr(r1, MemOperand(sp));
1557 } else {
1558 __ pop(r1);
1559 }
1560
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001561 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1562 __ Call(ic, RelocInfo::CODE_TARGET);
1563
1564 // If the assignment ends an initialization block, revert to fast case.
1565 if (expr->ends_initialization_block()) {
1566 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001567 // Receiver is under the result value.
1568 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001569 __ push(ip);
1570 __ CallRuntime(Runtime::kToFastProperties, 1);
1571 __ pop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001572 DropAndApply(1, context_, r0);
1573 } else {
1574 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001575 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001576}
1577
1578
1579void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1580 // Assignment to a property, using a keyed store IC.
1581
1582 // If the assignment starts a block of assignments to the same object,
1583 // change to slow case to avoid the quadratic behavior of repeatedly
1584 // adding fast properties.
1585 if (expr->starts_initialization_block()) {
1586 __ push(result_register());
1587 // Receiver is now under the key and value.
1588 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1589 __ push(ip);
1590 __ CallRuntime(Runtime::kToSlowProperties, 1);
1591 __ pop(result_register());
1592 }
1593
1594 // Record source code position before IC call.
1595 SetSourcePosition(expr->position());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001596 __ pop(r1); // Key.
1597 // Load receiver to r2. Leave a copy in the stack if needed for turning the
1598 // receiver into fast case.
1599 if (expr->ends_initialization_block()) {
1600 __ ldr(r2, MemOperand(sp));
1601 } else {
1602 __ pop(r2);
1603 }
1604
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001605 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1606 __ Call(ic, RelocInfo::CODE_TARGET);
1607
1608 // If the assignment ends an initialization block, revert to fast case.
1609 if (expr->ends_initialization_block()) {
1610 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001611 // Receiver is under the result value.
1612 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001613 __ push(ip);
1614 __ CallRuntime(Runtime::kToFastProperties, 1);
1615 __ pop(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001616 DropAndApply(1, context_, r0);
1617 } else {
1618 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001619 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001620}
1621
1622
1623void FullCodeGenerator::VisitProperty(Property* expr) {
1624 Comment cmnt(masm_, "[ Property");
1625 Expression* key = expr->key();
1626
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001627 if (key->IsPropertyName()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001628 VisitForValue(expr->obj(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001629 EmitNamedPropertyLoad(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001630 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001631 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001632 VisitForValue(expr->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001633 VisitForValue(expr->key(), kAccumulator);
1634 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001635 EmitKeyedPropertyLoad(expr);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001636 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001637 }
1638}
1639
1640void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001641 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001642 RelocInfo::Mode mode) {
1643 // Code common for calls using the IC.
1644 ZoneList<Expression*>* args = expr->arguments();
1645 int arg_count = args->length();
1646 for (int i = 0; i < arg_count; i++) {
1647 VisitForValue(args->at(i), kStack);
1648 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001649 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001650 // Record source position for debugger.
1651 SetSourcePosition(expr->position());
1652 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001653 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1654 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001655 __ Call(ic, mode);
1656 // Restore context register.
1657 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001658 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001659}
1660
1661
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001662void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
1663 Expression* key,
1664 RelocInfo::Mode mode) {
1665 // Code common for calls using the IC.
1666 ZoneList<Expression*>* args = expr->arguments();
1667 int arg_count = args->length();
1668 for (int i = 0; i < arg_count; i++) {
1669 VisitForValue(args->at(i), kStack);
1670 }
1671 VisitForValue(key, kAccumulator);
1672 __ mov(r2, r0);
1673 // Record source position for debugger.
1674 SetSourcePosition(expr->position());
1675 // Call the IC initialization code.
1676 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1677 Handle<Code> ic = CodeGenerator::ComputeKeyedCallInitialize(arg_count,
1678 in_loop);
1679 __ Call(ic, mode);
1680 // Restore context register.
1681 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1682 Apply(context_, r0);
1683}
1684
1685
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001686void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1687 // Code common for calls using the call stub.
1688 ZoneList<Expression*>* args = expr->arguments();
1689 int arg_count = args->length();
1690 for (int i = 0; i < arg_count; i++) {
1691 VisitForValue(args->at(i), kStack);
1692 }
1693 // Record source position for debugger.
1694 SetSourcePosition(expr->position());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001695 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1696 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001697 __ CallStub(&stub);
1698 // Restore context register.
1699 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001700 DropAndApply(1, context_, r0);
1701}
1702
1703
1704void FullCodeGenerator::VisitCall(Call* expr) {
1705 Comment cmnt(masm_, "[ Call");
1706 Expression* fun = expr->expression();
1707 Variable* var = fun->AsVariableProxy()->AsVariable();
1708
1709 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001710 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1711 // resolve the function we need to call and the receiver of the
1712 // call. Then we call the resolved function using the given
1713 // arguments.
1714 VisitForValue(fun, kStack);
1715 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1716 __ push(r2); // Reserved receiver slot.
1717
1718 // Push the arguments.
1719 ZoneList<Expression*>* args = expr->arguments();
1720 int arg_count = args->length();
1721 for (int i = 0; i < arg_count; i++) {
1722 VisitForValue(args->at(i), kStack);
1723 }
1724
1725 // Push copy of the function - found below the arguments.
1726 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1727 __ push(r1);
1728
1729 // Push copy of the first argument or undefined if it doesn't exist.
1730 if (arg_count > 0) {
1731 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1732 __ push(r1);
1733 } else {
1734 __ push(r2);
1735 }
1736
1737 // Push the receiver of the enclosing function and do runtime call.
1738 __ ldr(r1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1739 __ push(r1);
1740 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1741
1742 // The runtime call returns a pair of values in r0 (function) and
1743 // r1 (receiver). Touch up the stack with the right values.
1744 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1745 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
1746
1747 // Record source position for debugger.
1748 SetSourcePosition(expr->position());
1749 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1750 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1751 __ CallStub(&stub);
1752 // Restore context register.
1753 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1754 DropAndApply(1, context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001755 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001756 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001757 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001758 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001759 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1760 } else if (var != NULL && var->slot() != NULL &&
1761 var->slot()->type() == Slot::LOOKUP) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001762 // Call to a lookup slot (dynamically introduced variable). Call the
1763 // runtime to find the function to call (returned in eax) and the object
1764 // holding it (returned in edx).
1765 __ push(context_register());
1766 __ mov(r2, Operand(var->name()));
1767 __ push(r2);
1768 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1769 __ push(r0); // Function.
1770 __ push(r1); // Receiver.
1771 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001772 } else if (fun->AsProperty() != NULL) {
1773 // Call to an object property.
1774 Property* prop = fun->AsProperty();
1775 Literal* key = prop->key()->AsLiteral();
1776 if (key != NULL && key->handle()->IsSymbol()) {
1777 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001778 VisitForValue(prop->obj(), kStack);
1779 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1780 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001781 // Call to a keyed property.
1782 // For a synthetic property use keyed load IC followed by function call,
1783 // for a regular property use keyed CallIC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001784 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001785 if (prop->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001786 VisitForValue(prop->key(), kAccumulator);
1787 // Record source code position for IC call.
1788 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001789 __ pop(r1); // We do not need to keep the receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001790
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001791 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1792 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001793 // Push result (function).
1794 __ push(r0);
1795 // Push Global receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001796 __ ldr(r1, CodeGenerator::GlobalObject());
1797 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001798 __ push(r1);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001799 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001800 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001801 EmitKeyedCallWithIC(expr, prop->key(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001802 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001803 }
1804 } else {
1805 // Call to some other expression. If the expression is an anonymous
1806 // function literal not called in a loop, mark it as one that should
1807 // also use the fast code generator.
1808 FunctionLiteral* lit = fun->AsFunctionLiteral();
1809 if (lit != NULL &&
1810 lit->name()->Equals(Heap::empty_string()) &&
1811 loop_depth() == 0) {
1812 lit->set_try_full_codegen(true);
1813 }
1814 VisitForValue(fun, kStack);
1815 // Load global receiver object.
1816 __ ldr(r1, CodeGenerator::GlobalObject());
1817 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1818 __ push(r1);
1819 // Emit function call.
1820 EmitCallWithStub(expr);
1821 }
1822}
1823
1824
1825void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1826 Comment cmnt(masm_, "[ CallNew");
1827 // According to ECMA-262, section 11.2.2, page 44, the function
1828 // expression in new calls must be evaluated before the
1829 // arguments.
1830 // Push function on the stack.
1831 VisitForValue(expr->expression(), kStack);
1832
1833 // Push global object (receiver).
1834 __ ldr(r0, CodeGenerator::GlobalObject());
1835 __ push(r0);
1836 // Push the arguments ("left-to-right") on the stack.
1837 ZoneList<Expression*>* args = expr->arguments();
1838 int arg_count = args->length();
1839 for (int i = 0; i < arg_count; i++) {
1840 VisitForValue(args->at(i), kStack);
1841 }
1842
1843 // Call the construct call builtin that handles allocation and
1844 // constructor invocation.
1845 SetSourcePosition(expr->position());
1846
1847 // Load function, arg_count into r1 and r0.
1848 __ mov(r0, Operand(arg_count));
1849 // Function is in sp[arg_count + 1].
1850 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1851
1852 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1853 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
1854
1855 // Replace function on TOS with result in r0, or pop it.
1856 DropAndApply(1, context_, r0);
1857}
1858
1859
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001860void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1861 ASSERT(args->length() == 1);
1862
1863 VisitForValue(args->at(0), kAccumulator);
1864
1865 Label materialize_true, materialize_false;
1866 Label* if_true = NULL;
1867 Label* if_false = NULL;
1868 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1869
1870 __ BranchOnSmi(r0, if_true);
1871 __ b(if_false);
1872
1873 Apply(context_, if_true, if_false);
1874}
1875
1876
1877void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1878 ASSERT(args->length() == 1);
1879
1880 VisitForValue(args->at(0), kAccumulator);
1881
1882 Label materialize_true, materialize_false;
1883 Label* if_true = NULL;
1884 Label* if_false = NULL;
1885 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1886
1887 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
1888 __ b(eq, if_true);
1889 __ b(if_false);
1890
1891 Apply(context_, if_true, if_false);
1892}
1893
1894
1895void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1896 ASSERT(args->length() == 1);
1897
1898 VisitForValue(args->at(0), kAccumulator);
1899
1900 Label materialize_true, materialize_false;
1901 Label* if_true = NULL;
1902 Label* if_false = NULL;
1903 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1904 __ BranchOnSmi(r0, if_false);
1905 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1906 __ cmp(r0, ip);
1907 __ b(eq, if_true);
1908 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1909 // Undetectable objects behave like undefined when tested with typeof.
1910 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
1911 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1912 __ b(ne, if_false);
1913 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1914 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
1915 __ b(lt, if_false);
1916 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
1917 __ b(le, if_true);
1918 __ b(if_false);
1919
1920 Apply(context_, if_true, if_false);
1921}
1922
1923
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001924void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
1925 ASSERT(args->length() == 1);
1926
1927 VisitForValue(args->at(0), kAccumulator);
1928
1929 Label materialize_true, materialize_false;
1930 Label* if_true = NULL;
1931 Label* if_false = NULL;
1932 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1933
1934 __ BranchOnSmi(r0, if_false);
1935 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
1936 __ b(ge, if_true);
1937 __ b(if_false);
1938
1939 Apply(context_, if_true, if_false);
1940}
1941
1942
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001943void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
1944 ASSERT(args->length() == 1);
1945
1946 VisitForValue(args->at(0), kAccumulator);
1947
1948 Label materialize_true, materialize_false;
1949 Label* if_true = NULL;
1950 Label* if_false = NULL;
1951 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1952
1953 __ BranchOnSmi(r0, if_false);
1954 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1955 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
1956 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1957 __ b(ne, if_true);
1958 __ b(if_false);
1959
1960 Apply(context_, if_true, if_false);
1961}
1962
1963
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001964void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
1965 ZoneList<Expression*>* args) {
1966
1967 ASSERT(args->length() == 1);
1968
1969 VisitForValue(args->at(0), kAccumulator);
1970
1971 Label materialize_true, materialize_false;
1972 Label* if_true = NULL;
1973 Label* if_false = NULL;
1974 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1975
1976 // Just indicate false, as %_IsStringWrapperSafeForDefaultValueOf() is only
1977 // used in a few functions in runtime.js which should not normally be hit by
1978 // this compiler.
1979 __ jmp(if_false);
1980 Apply(context_, if_true, if_false);
1981}
1982
1983
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001984void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
1985 ASSERT(args->length() == 1);
1986
1987 VisitForValue(args->at(0), kAccumulator);
1988
1989 Label materialize_true, materialize_false;
1990 Label* if_true = NULL;
1991 Label* if_false = NULL;
1992 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
1993
1994 __ BranchOnSmi(r0, if_false);
1995 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
1996 __ b(eq, if_true);
1997 __ b(if_false);
1998
1999 Apply(context_, if_true, if_false);
2000}
2001
2002
2003void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
2004 ASSERT(args->length() == 1);
2005
2006 VisitForValue(args->at(0), kAccumulator);
2007
2008 Label materialize_true, materialize_false;
2009 Label* if_true = NULL;
2010 Label* if_false = NULL;
2011 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2012
2013 __ BranchOnSmi(r0, if_false);
2014 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
2015 __ b(eq, if_true);
2016 __ b(if_false);
2017
2018 Apply(context_, if_true, if_false);
2019}
2020
2021
2022void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
2023 ASSERT(args->length() == 1);
2024
2025 VisitForValue(args->at(0), kAccumulator);
2026
2027 Label materialize_true, materialize_false;
2028 Label* if_true = NULL;
2029 Label* if_false = NULL;
2030 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2031
2032 __ BranchOnSmi(r0, if_false);
2033 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
2034 __ b(eq, if_true);
2035 __ b(if_false);
2036
2037 Apply(context_, if_true, if_false);
2038}
2039
2040
2041
2042void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
2043 ASSERT(args->length() == 0);
2044
2045 Label materialize_true, materialize_false;
2046 Label* if_true = NULL;
2047 Label* if_false = NULL;
2048 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2049
2050 // Get the frame pointer for the calling frame.
2051 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2052
2053 // Skip the arguments adaptor frame if it exists.
2054 Label check_frame_marker;
2055 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
2056 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2057 __ b(ne, &check_frame_marker);
2058 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
2059
2060 // Check the marker in the calling frame.
2061 __ bind(&check_frame_marker);
2062 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
2063 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
2064 __ b(eq, if_true);
2065 __ b(if_false);
2066
2067 Apply(context_, if_true, if_false);
2068}
2069
2070
2071void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
2072 ASSERT(args->length() == 2);
2073
2074 // Load the two objects into registers and perform the comparison.
2075 VisitForValue(args->at(0), kStack);
2076 VisitForValue(args->at(1), kAccumulator);
2077
2078 Label materialize_true, materialize_false;
2079 Label* if_true = NULL;
2080 Label* if_false = NULL;
2081 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
2082
2083 __ pop(r1);
2084 __ cmp(r0, r1);
2085 __ b(eq, if_true);
2086 __ b(if_false);
2087
2088 Apply(context_, if_true, if_false);
2089}
2090
2091
2092void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
2093 ASSERT(args->length() == 1);
2094
2095 // ArgumentsAccessStub expects the key in edx and the formal
2096 // parameter count in eax.
2097 VisitForValue(args->at(0), kAccumulator);
2098 __ mov(r1, r0);
2099 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2100 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2101 __ CallStub(&stub);
2102 Apply(context_, r0);
2103}
2104
2105
2106void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
2107 ASSERT(args->length() == 0);
2108
2109 Label exit;
2110 // Get the number of formal parameters.
2111 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2112
2113 // Check if the calling frame is an arguments adaptor frame.
2114 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2115 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
2116 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2117 __ b(ne, &exit);
2118
2119 // Arguments adaptor case: Read the arguments length from the
2120 // adaptor frame.
2121 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2122
2123 __ bind(&exit);
2124 Apply(context_, r0);
2125}
2126
2127
2128void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
2129 ASSERT(args->length() == 1);
2130 Label done, null, function, non_function_constructor;
2131
2132 VisitForValue(args->at(0), kAccumulator);
2133
2134 // If the object is a smi, we return null.
2135 __ BranchOnSmi(r0, &null);
2136
2137 // Check that the object is a JS object but take special care of JS
2138 // functions to make sure they have 'Function' as their class.
2139 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2140 __ b(lt, &null);
2141
2142 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2143 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2144 // LAST_JS_OBJECT_TYPE.
2145 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2146 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2147 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2148 __ b(eq, &function);
2149
2150 // Check if the constructor in the map is a function.
2151 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2152 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2153 __ b(ne, &non_function_constructor);
2154
2155 // r0 now contains the constructor function. Grab the
2156 // instance class name from there.
2157 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2158 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2159 __ b(&done);
2160
2161 // Functions have class 'Function'.
2162 __ bind(&function);
2163 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2164 __ jmp(&done);
2165
2166 // Objects with a non-function constructor have class 'Object'.
2167 __ bind(&non_function_constructor);
2168 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2169 __ jmp(&done);
2170
2171 // Non-JS objects have class null.
2172 __ bind(&null);
2173 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2174
2175 // All done.
2176 __ bind(&done);
2177
2178 Apply(context_, r0);
2179}
2180
2181
2182void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2183 // Conditionally generate a log call.
2184 // Args:
2185 // 0 (literal string): The type of logging (corresponds to the flags).
2186 // This is used to determine whether or not to generate the log call.
2187 // 1 (string): Format string. Access the string at argument index 2
2188 // with '%2s' (see Logger::LogRuntime for all the formats).
2189 // 2 (array): Arguments to the format string.
2190 ASSERT_EQ(args->length(), 3);
2191#ifdef ENABLE_LOGGING_AND_PROFILING
2192 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2193 VisitForValue(args->at(1), kStack);
2194 VisitForValue(args->at(2), kStack);
2195 __ CallRuntime(Runtime::kLog, 2);
2196 }
2197#endif
2198 // Finally, we're expected to leave a value on the top of the stack.
2199 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2200 Apply(context_, r0);
2201}
2202
2203
2204void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2205 ASSERT(args->length() == 0);
2206
2207 Label slow_allocate_heapnumber;
2208 Label heapnumber_allocated;
2209
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002210 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
2211 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002212 __ jmp(&heapnumber_allocated);
2213
2214 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002215 // Allocate a heap number.
2216 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002217 __ mov(r4, Operand(r0));
2218
2219 __ bind(&heapnumber_allocated);
2220
2221 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
2222 // by computing:
2223 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2224 if (CpuFeatures::IsSupported(VFP3)) {
2225 __ PrepareCallCFunction(0, r1);
2226 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
2227
2228 CpuFeatures::Scope scope(VFP3);
2229 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2230 // Create this constant using mov/orr to avoid PC relative load.
2231 __ mov(r1, Operand(0x41000000));
2232 __ orr(r1, r1, Operand(0x300000));
2233 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
2234 __ vmov(d7, r0, r1);
2235 // Move 0x4130000000000000 to VFP.
2236 __ mov(r0, Operand(0));
2237 __ vmov(d8, r0, r1);
2238 // Subtract and store the result in the heap number.
2239 __ vsub(d7, d7, d8);
2240 __ sub(r0, r4, Operand(kHeapObjectTag));
2241 __ vstr(d7, r0, HeapNumber::kValueOffset);
2242 __ mov(r0, r4);
2243 } else {
2244 __ mov(r0, Operand(r4));
2245 __ PrepareCallCFunction(1, r1);
2246 __ CallCFunction(
2247 ExternalReference::fill_heap_number_with_random_function(), 1);
2248 }
2249
2250 Apply(context_, r0);
2251}
2252
2253
2254void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
2255 // Load the arguments on the stack and call the stub.
2256 SubStringStub stub;
2257 ASSERT(args->length() == 3);
2258 VisitForValue(args->at(0), kStack);
2259 VisitForValue(args->at(1), kStack);
2260 VisitForValue(args->at(2), kStack);
2261 __ CallStub(&stub);
2262 Apply(context_, r0);
2263}
2264
2265
2266void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
2267 // Load the arguments on the stack and call the stub.
2268 RegExpExecStub stub;
2269 ASSERT(args->length() == 4);
2270 VisitForValue(args->at(0), kStack);
2271 VisitForValue(args->at(1), kStack);
2272 VisitForValue(args->at(2), kStack);
2273 VisitForValue(args->at(3), kStack);
2274 __ CallStub(&stub);
2275 Apply(context_, r0);
2276}
2277
2278
2279void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
2280 ASSERT(args->length() == 1);
2281
2282 VisitForValue(args->at(0), kAccumulator); // Load the object.
2283
2284 Label done;
2285 // If the object is a smi return the object.
2286 __ BranchOnSmi(r0, &done);
2287 // If the object is not a value type, return the object.
2288 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
2289 __ b(ne, &done);
2290 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2291
2292 __ bind(&done);
2293 Apply(context_, r0);
2294}
2295
2296
2297void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
2298 // Load the arguments on the stack and call the runtime function.
2299 ASSERT(args->length() == 2);
2300 VisitForValue(args->at(0), kStack);
2301 VisitForValue(args->at(1), kStack);
2302 __ CallRuntime(Runtime::kMath_pow, 2);
2303 Apply(context_, r0);
2304}
2305
2306
2307void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
2308 ASSERT(args->length() == 2);
2309
2310 VisitForValue(args->at(0), kStack); // Load the object.
2311 VisitForValue(args->at(1), kAccumulator); // Load the value.
2312 __ pop(r1); // r0 = value. r1 = object.
2313
2314 Label done;
2315 // If the object is a smi, return the value.
2316 __ BranchOnSmi(r1, &done);
2317
2318 // If the object is not a value type, return the value.
2319 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
2320 __ b(ne, &done);
2321
2322 // Store the value.
2323 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2324 // Update the write barrier. Save the value as it will be
2325 // overwritten by the write barrier code and is needed afterward.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002326 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002327
2328 __ bind(&done);
2329 Apply(context_, r0);
2330}
2331
2332
2333void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2334 ASSERT_EQ(args->length(), 1);
2335
2336 // Load the argument on the stack and call the stub.
2337 VisitForValue(args->at(0), kStack);
2338
2339 NumberToStringStub stub;
2340 __ CallStub(&stub);
2341 Apply(context_, r0);
2342}
2343
2344
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002345void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002346 ASSERT(args->length() == 1);
2347
2348 VisitForValue(args->at(0), kAccumulator);
2349
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002350 Label done;
2351 StringCharFromCodeGenerator generator(r0, r1);
2352 generator.GenerateFast(masm_);
2353 __ jmp(&done);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002354
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002355 NopRuntimeCallHelper call_helper;
2356 generator.GenerateSlow(masm_, call_helper);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002357
2358 __ bind(&done);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002359 Apply(context_, r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002360}
2361
2362
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002363void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
2364 ASSERT(args->length() == 2);
2365
2366 VisitForValue(args->at(0), kStack);
2367 VisitForValue(args->at(1), kAccumulator);
2368
2369 Register object = r1;
2370 Register index = r0;
2371 Register scratch = r2;
2372 Register result = r3;
2373
2374 __ pop(object);
2375
2376 Label need_conversion;
2377 Label index_out_of_range;
2378 Label done;
2379 StringCharCodeAtGenerator generator(object,
2380 index,
2381 scratch,
2382 result,
2383 &need_conversion,
2384 &need_conversion,
2385 &index_out_of_range,
2386 STRING_INDEX_IS_NUMBER);
2387 generator.GenerateFast(masm_);
2388 __ jmp(&done);
2389
2390 __ bind(&index_out_of_range);
2391 // When the index is out of range, the spec requires us to return
2392 // NaN.
2393 __ LoadRoot(result, Heap::kNanValueRootIndex);
2394 __ jmp(&done);
2395
2396 __ bind(&need_conversion);
2397 // Load the undefined value into the result register, which will
2398 // trigger conversion.
2399 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2400 __ jmp(&done);
2401
2402 NopRuntimeCallHelper call_helper;
2403 generator.GenerateSlow(masm_, call_helper);
2404
2405 __ bind(&done);
2406 Apply(context_, result);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002407}
2408
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002409
2410void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
2411 ASSERT(args->length() == 2);
2412
2413 VisitForValue(args->at(0), kStack);
2414 VisitForValue(args->at(1), kAccumulator);
2415
2416 Register object = r1;
2417 Register index = r0;
2418 Register scratch1 = r2;
2419 Register scratch2 = r3;
2420 Register result = r0;
2421
2422 __ pop(object);
2423
2424 Label need_conversion;
2425 Label index_out_of_range;
2426 Label done;
2427 StringCharAtGenerator generator(object,
2428 index,
2429 scratch1,
2430 scratch2,
2431 result,
2432 &need_conversion,
2433 &need_conversion,
2434 &index_out_of_range,
2435 STRING_INDEX_IS_NUMBER);
2436 generator.GenerateFast(masm_);
2437 __ jmp(&done);
2438
2439 __ bind(&index_out_of_range);
2440 // When the index is out of range, the spec requires us to return
2441 // the empty string.
2442 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2443 __ jmp(&done);
2444
2445 __ bind(&need_conversion);
2446 // Move smi zero into the result register, which will trigger
2447 // conversion.
2448 __ mov(result, Operand(Smi::FromInt(0)));
2449 __ jmp(&done);
2450
2451 NopRuntimeCallHelper call_helper;
2452 generator.GenerateSlow(masm_, call_helper);
2453
2454 __ bind(&done);
2455 Apply(context_, result);
2456}
2457
2458
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002459void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2460 ASSERT_EQ(2, args->length());
2461
2462 VisitForValue(args->at(0), kStack);
2463 VisitForValue(args->at(1), kStack);
2464
2465 StringAddStub stub(NO_STRING_ADD_FLAGS);
2466 __ CallStub(&stub);
2467 Apply(context_, r0);
2468}
2469
2470
2471void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
2472 ASSERT_EQ(2, args->length());
2473
2474 VisitForValue(args->at(0), kStack);
2475 VisitForValue(args->at(1), kStack);
2476
2477 StringCompareStub stub;
2478 __ CallStub(&stub);
2479 Apply(context_, r0);
2480}
2481
2482
2483void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
2484 // Load the argument on the stack and call the runtime.
2485 ASSERT(args->length() == 1);
2486 VisitForValue(args->at(0), kStack);
2487 __ CallRuntime(Runtime::kMath_sin, 1);
2488 Apply(context_, r0);
2489}
2490
2491
2492void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
2493 // Load the argument on the stack and call the runtime.
2494 ASSERT(args->length() == 1);
2495 VisitForValue(args->at(0), kStack);
2496 __ CallRuntime(Runtime::kMath_cos, 1);
2497 Apply(context_, r0);
2498}
2499
2500
2501void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2502 // Load the argument on the stack and call the runtime function.
2503 ASSERT(args->length() == 1);
2504 VisitForValue(args->at(0), kStack);
2505 __ CallRuntime(Runtime::kMath_sqrt, 1);
2506 Apply(context_, r0);
2507}
2508
2509
2510void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2511 ASSERT(args->length() >= 2);
2512
2513 int arg_count = args->length() - 2; // For receiver and function.
2514 VisitForValue(args->at(0), kStack); // Receiver.
2515 for (int i = 0; i < arg_count; i++) {
2516 VisitForValue(args->at(i + 1), kStack);
2517 }
2518 VisitForValue(args->at(arg_count + 1), kAccumulator); // Function.
2519
2520 // InvokeFunction requires function in r1. Move it in there.
2521 if (!result_register().is(r1)) __ mov(r1, result_register());
2522 ParameterCount count(arg_count);
2523 __ InvokeFunction(r1, count, CALL_FUNCTION);
2524 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2525 Apply(context_, r0);
2526}
2527
2528
2529void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2530 ASSERT(args->length() == 3);
2531 VisitForValue(args->at(0), kStack);
2532 VisitForValue(args->at(1), kStack);
2533 VisitForValue(args->at(2), kStack);
2534 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
2535 Apply(context_, r0);
2536}
2537
2538
2539void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2540 ASSERT(args->length() == 3);
2541 VisitForValue(args->at(0), kStack);
2542 VisitForValue(args->at(1), kStack);
2543 VisitForValue(args->at(2), kStack);
2544 __ CallRuntime(Runtime::kSwapElements, 3);
2545 Apply(context_, r0);
2546}
2547
2548
2549void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2550 ASSERT_EQ(2, args->length());
2551
2552 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2553 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2554
2555 Handle<FixedArray> jsfunction_result_caches(
2556 Top::global_context()->jsfunction_result_caches());
2557 if (jsfunction_result_caches->length() <= cache_id) {
2558 __ Abort("Attempt to use undefined cache.");
2559 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2560 Apply(context_, r0);
2561 return;
2562 }
2563
2564 VisitForValue(args->at(1), kAccumulator);
2565
2566 Register key = r0;
2567 Register cache = r1;
2568 __ ldr(cache, CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX));
2569 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
2570 __ ldr(cache,
2571 CodeGenerator::ContextOperand(
2572 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
2573 __ ldr(cache,
2574 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2575
2576
2577 Label done, not_found;
2578 // tmp now holds finger offset as a smi.
2579 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2580 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2581 // r2 now holds finger offset as a smi.
2582 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2583 // r3 now points to the start of fixed array elements.
2584 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2585 // Note side effect of PreIndex: r3 now points to the key of the pair.
2586 __ cmp(key, r2);
2587 __ b(ne, &not_found);
2588
2589 __ ldr(r0, MemOperand(r3, kPointerSize));
2590 __ b(&done);
2591
2592 __ bind(&not_found);
2593 // Call runtime to perform the lookup.
2594 __ Push(cache, key);
2595 __ CallRuntime(Runtime::kGetFromCache, 2);
2596
2597 __ bind(&done);
2598 Apply(context_, r0);
2599}
2600
2601
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002602void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
2603 ASSERT_EQ(2, args->length());
2604
2605 Register right = r0;
2606 Register left = r1;
2607 Register tmp = r2;
2608 Register tmp2 = r3;
2609
2610 VisitForValue(args->at(0), kStack);
2611 VisitForValue(args->at(1), kAccumulator);
2612 __ pop(left);
2613
2614 Label done, fail, ok;
2615 __ cmp(left, Operand(right));
2616 __ b(eq, &ok);
2617 // Fail if either is a non-HeapObject.
2618 __ and_(tmp, left, Operand(right));
2619 __ tst(tmp, Operand(kSmiTagMask));
2620 __ b(eq, &fail);
2621 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
2622 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
2623 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
2624 __ b(ne, &fail);
2625 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
2626 __ cmp(tmp, Operand(tmp2));
2627 __ b(ne, &fail);
2628 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
2629 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
2630 __ cmp(tmp, tmp2);
2631 __ b(eq, &ok);
2632 __ bind(&fail);
2633 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
2634 __ jmp(&done);
2635 __ bind(&ok);
2636 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
2637 __ bind(&done);
2638
2639 Apply(context_, r0);
2640}
2641
2642
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002643void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002644 Handle<String> name = expr->name();
2645 if (name->length() > 0 && name->Get(0) == '_') {
2646 Comment cmnt(masm_, "[ InlineRuntimeCall");
2647 EmitInlineRuntimeCall(expr);
2648 return;
2649 }
2650
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002651 Comment cmnt(masm_, "[ CallRuntime");
2652 ZoneList<Expression*>* args = expr->arguments();
2653
2654 if (expr->is_jsruntime()) {
2655 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002656 __ ldr(r0, CodeGenerator::GlobalObject());
2657 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002658 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002659 }
2660
2661 // Push the arguments ("left-to-right").
2662 int arg_count = args->length();
2663 for (int i = 0; i < arg_count; i++) {
2664 VisitForValue(args->at(i), kStack);
2665 }
2666
2667 if (expr->is_jsruntime()) {
2668 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002669 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002670 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2671 NOT_IN_LOOP);
2672 __ Call(ic, RelocInfo::CODE_TARGET);
2673 // Restore context register.
2674 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002675 } else {
2676 // Call the C runtime function.
2677 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002678 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002679 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002680}
2681
2682
2683void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2684 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002685 case Token::DELETE: {
2686 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2687 Property* prop = expr->expression()->AsProperty();
2688 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2689 if (prop == NULL && var == NULL) {
2690 // Result of deleting non-property, non-variable reference is true.
2691 // The subexpression may have side effects.
2692 VisitForEffect(expr->expression());
2693 Apply(context_, true);
2694 } else if (var != NULL &&
2695 !var->is_global() &&
2696 var->slot() != NULL &&
2697 var->slot()->type() != Slot::LOOKUP) {
2698 // Result of deleting non-global, non-dynamic variables is false.
2699 // The subexpression does not have side effects.
2700 Apply(context_, false);
2701 } else {
2702 // Property or variable reference. Call the delete builtin with
2703 // object and property name as arguments.
2704 if (prop != NULL) {
2705 VisitForValue(prop->obj(), kStack);
2706 VisitForValue(prop->key(), kStack);
2707 } else if (var->is_global()) {
2708 __ ldr(r1, CodeGenerator::GlobalObject());
2709 __ mov(r0, Operand(var->name()));
2710 __ Push(r1, r0);
2711 } else {
2712 // Non-global variable. Call the runtime to look up the context
2713 // where the variable was introduced.
2714 __ push(context_register());
2715 __ mov(r2, Operand(var->name()));
2716 __ push(r2);
2717 __ CallRuntime(Runtime::kLookupContext, 2);
2718 __ push(r0);
2719 __ mov(r2, Operand(var->name()));
2720 __ push(r2);
2721 }
2722 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
2723 Apply(context_, r0);
2724 }
2725 break;
2726 }
2727
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002728 case Token::VOID: {
2729 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2730 VisitForEffect(expr->expression());
2731 switch (context_) {
2732 case Expression::kUninitialized:
2733 UNREACHABLE();
2734 break;
2735 case Expression::kEffect:
2736 break;
2737 case Expression::kValue:
2738 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2739 switch (location_) {
2740 case kAccumulator:
2741 break;
2742 case kStack:
2743 __ push(result_register());
2744 break;
2745 }
2746 break;
2747 case Expression::kTestValue:
2748 // Value is false so it's needed.
2749 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2750 switch (location_) {
2751 case kAccumulator:
2752 break;
2753 case kStack:
2754 __ push(result_register());
2755 break;
2756 }
2757 // Fall through.
2758 case Expression::kTest:
2759 case Expression::kValueTest:
2760 __ jmp(false_label_);
2761 break;
2762 }
2763 break;
2764 }
2765
2766 case Token::NOT: {
2767 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002768 Label materialize_true, materialize_false;
2769 Label* if_true = NULL;
2770 Label* if_false = NULL;
2771
2772 // Notice that the labels are swapped.
2773 PrepareTest(&materialize_true, &materialize_false, &if_false, &if_true);
2774
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002775 VisitForControl(expr->expression(), if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002776
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002777 Apply(context_, if_false, if_true); // Labels swapped.
2778 break;
2779 }
2780
2781 case Token::TYPEOF: {
2782 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
2783 VariableProxy* proxy = expr->expression()->AsVariableProxy();
2784 if (proxy != NULL &&
2785 !proxy->var()->is_this() &&
2786 proxy->var()->is_global()) {
2787 Comment cmnt(masm_, "Global variable");
2788 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002789 __ mov(r2, Operand(proxy->name()));
2790 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2791 // Use a regular load, not a contextual load, to avoid a reference
2792 // error.
2793 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002794 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002795 } else if (proxy != NULL &&
2796 proxy->var()->slot() != NULL &&
2797 proxy->var()->slot()->type() == Slot::LOOKUP) {
2798 __ mov(r0, Operand(proxy->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002799 __ Push(cp, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002800 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
2801 __ push(r0);
2802 } else {
2803 // This expression cannot throw a reference error at the top level.
2804 VisitForValue(expr->expression(), kStack);
2805 }
2806
2807 __ CallRuntime(Runtime::kTypeof, 1);
2808 Apply(context_, r0);
2809 break;
2810 }
2811
2812 case Token::ADD: {
2813 Comment cmt(masm_, "[ UnaryOperation (ADD)");
2814 VisitForValue(expr->expression(), kAccumulator);
2815 Label no_conversion;
2816 __ tst(result_register(), Operand(kSmiTagMask));
2817 __ b(eq, &no_conversion);
2818 __ push(r0);
2819 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2820 __ bind(&no_conversion);
2821 Apply(context_, result_register());
2822 break;
2823 }
2824
2825 case Token::SUB: {
2826 Comment cmt(masm_, "[ UnaryOperation (SUB)");
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002827 bool can_overwrite =
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002828 (expr->expression()->AsBinaryOperation() != NULL &&
2829 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002830 UnaryOverwriteMode overwrite =
2831 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002832 GenericUnaryOpStub stub(Token::SUB, overwrite);
2833 // GenericUnaryOpStub expects the argument to be in the
2834 // accumulator register r0.
2835 VisitForValue(expr->expression(), kAccumulator);
2836 __ CallStub(&stub);
2837 Apply(context_, r0);
2838 break;
2839 }
2840
2841 case Token::BIT_NOT: {
2842 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002843 bool can_overwrite =
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002844 (expr->expression()->AsBinaryOperation() != NULL &&
2845 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002846 UnaryOverwriteMode overwrite =
2847 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002848 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
2849 // GenericUnaryOpStub expects the argument to be in the
2850 // accumulator register r0.
2851 VisitForValue(expr->expression(), kAccumulator);
2852 // Avoid calling the stub for Smis.
2853 Label smi, done;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002854 __ BranchOnSmi(result_register(), &smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002855 // Non-smi: call stub leaving result in accumulator register.
2856 __ CallStub(&stub);
2857 __ b(&done);
2858 // Perform operation directly on Smis.
2859 __ bind(&smi);
2860 __ mvn(result_register(), Operand(result_register()));
2861 // Bit-clear inverted smi-tag.
2862 __ bic(result_register(), result_register(), Operand(kSmiTagMask));
2863 __ bind(&done);
2864 Apply(context_, result_register());
2865 break;
2866 }
2867
2868 default:
2869 UNREACHABLE();
2870 }
2871}
2872
2873
2874void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2875 Comment cmnt(masm_, "[ CountOperation");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002876 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2877 // as the left-hand side.
2878 if (!expr->expression()->IsValidLeftHandSide()) {
2879 VisitForEffect(expr->expression());
2880 return;
2881 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002882
2883 // Expression can only be a property, a global or a (parameter or local)
2884 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2885 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2886 LhsKind assign_type = VARIABLE;
2887 Property* prop = expr->expression()->AsProperty();
2888 // In case of a property we use the uninitialized expression context
2889 // of the key to detect a named property.
2890 if (prop != NULL) {
2891 assign_type =
2892 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2893 }
2894
2895 // Evaluate expression and get value.
2896 if (assign_type == VARIABLE) {
2897 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
2898 Location saved_location = location_;
2899 location_ = kAccumulator;
2900 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
2901 Expression::kValue);
2902 location_ = saved_location;
2903 } else {
2904 // Reserve space for result of postfix operation.
2905 if (expr->is_postfix() && context_ != Expression::kEffect) {
2906 __ mov(ip, Operand(Smi::FromInt(0)));
2907 __ push(ip);
2908 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002909 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002910 // Put the object both on the stack and in the accumulator.
2911 VisitForValue(prop->obj(), kAccumulator);
2912 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002913 EmitNamedPropertyLoad(prop);
2914 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002915 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002916 VisitForValue(prop->key(), kAccumulator);
2917 __ ldr(r1, MemOperand(sp, 0));
2918 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002919 EmitKeyedPropertyLoad(prop);
2920 }
2921 }
2922
2923 // Call ToNumber only if operand is not a smi.
2924 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002925 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002926 __ push(r0);
2927 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2928 __ bind(&no_conversion);
2929
2930 // Save result for postfix expressions.
2931 if (expr->is_postfix()) {
2932 switch (context_) {
2933 case Expression::kUninitialized:
2934 UNREACHABLE();
2935 case Expression::kEffect:
2936 // Do not save result.
2937 break;
2938 case Expression::kValue:
2939 case Expression::kTest:
2940 case Expression::kValueTest:
2941 case Expression::kTestValue:
2942 // Save the result on the stack. If we have a named or keyed property
2943 // we store the result under the receiver that is currently on top
2944 // of the stack.
2945 switch (assign_type) {
2946 case VARIABLE:
2947 __ push(r0);
2948 break;
2949 case NAMED_PROPERTY:
2950 __ str(r0, MemOperand(sp, kPointerSize));
2951 break;
2952 case KEYED_PROPERTY:
2953 __ str(r0, MemOperand(sp, 2 * kPointerSize));
2954 break;
2955 }
2956 break;
2957 }
2958 }
2959
2960
2961 // Inline smi case if we are in a loop.
2962 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002963 int count_value = expr->op() == Token::INC ? 1 : -1;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002964 if (loop_depth() > 0) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002965 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002966 __ b(vs, &stub_call);
2967 // We could eliminate this smi check if we split the code at
2968 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002969 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002970 __ bind(&stub_call);
2971 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002972 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002973 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002974 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00002975 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002976 __ CallStub(&stub);
2977 __ bind(&done);
2978
2979 // Store the value returned in r0.
2980 switch (assign_type) {
2981 case VARIABLE:
2982 if (expr->is_postfix()) {
2983 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002984 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002985 Expression::kEffect);
2986 // For all contexts except kEffect: We have the result on
2987 // top of the stack.
2988 if (context_ != Expression::kEffect) {
2989 ApplyTOS(context_);
2990 }
2991 } else {
2992 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002993 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002994 context_);
2995 }
2996 break;
2997 case NAMED_PROPERTY: {
2998 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00002999 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003000 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
3001 __ Call(ic, RelocInfo::CODE_TARGET);
3002 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003003 if (context_ != Expression::kEffect) {
3004 ApplyTOS(context_);
3005 }
3006 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00003007 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003008 }
3009 break;
3010 }
3011 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003012 __ pop(r1); // Key.
3013 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003014 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
3015 __ Call(ic, RelocInfo::CODE_TARGET);
3016 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003017 if (context_ != Expression::kEffect) {
3018 ApplyTOS(context_);
3019 }
3020 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003021 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003022 }
3023 break;
3024 }
3025 }
3026}
3027
3028
3029void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
3030 Comment cmnt(masm_, "[ BinaryOperation");
3031 switch (expr->op()) {
3032 case Token::COMMA:
3033 VisitForEffect(expr->left());
3034 Visit(expr->right());
3035 break;
3036
3037 case Token::OR:
3038 case Token::AND:
3039 EmitLogicalOperation(expr);
3040 break;
3041
3042 case Token::ADD:
3043 case Token::SUB:
3044 case Token::DIV:
3045 case Token::MOD:
3046 case Token::MUL:
3047 case Token::BIT_OR:
3048 case Token::BIT_AND:
3049 case Token::BIT_XOR:
3050 case Token::SHL:
3051 case Token::SHR:
3052 case Token::SAR:
3053 VisitForValue(expr->left(), kStack);
3054 VisitForValue(expr->right(), kAccumulator);
3055 EmitBinaryOp(expr->op(), context_);
3056 break;
3057
3058 default:
3059 UNREACHABLE();
3060 }
3061}
3062
3063
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003064void FullCodeGenerator::EmitNullCompare(bool strict,
3065 Register obj,
3066 Register null_const,
3067 Label* if_true,
3068 Label* if_false,
3069 Register scratch) {
3070 __ cmp(obj, null_const);
3071 if (strict) {
3072 __ b(eq, if_true);
3073 } else {
3074 __ b(eq, if_true);
3075 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3076 __ cmp(obj, ip);
3077 __ b(eq, if_true);
3078 __ BranchOnSmi(obj, if_false);
3079 // It can be an undetectable object.
3080 __ ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
3081 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
3082 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
3083 __ b(ne, if_true);
3084 }
3085 __ jmp(if_false);
3086}
3087
3088
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003089void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
3090 Comment cmnt(masm_, "[ CompareOperation");
3091
3092 // Always perform the comparison for its control flow. Pack the result
3093 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003094
3095 Label materialize_true, materialize_false;
3096 Label* if_true = NULL;
3097 Label* if_false = NULL;
3098 PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003099
3100 VisitForValue(expr->left(), kStack);
3101 switch (expr->op()) {
3102 case Token::IN:
3103 VisitForValue(expr->right(), kStack);
3104 __ InvokeBuiltin(Builtins::IN, CALL_JS);
3105 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3106 __ cmp(r0, ip);
3107 __ b(eq, if_true);
3108 __ jmp(if_false);
3109 break;
3110
3111 case Token::INSTANCEOF: {
3112 VisitForValue(expr->right(), kStack);
3113 InstanceofStub stub;
3114 __ CallStub(&stub);
3115 __ tst(r0, r0);
3116 __ b(eq, if_true); // The stub returns 0 for true.
3117 __ jmp(if_false);
3118 break;
3119 }
3120
3121 default: {
3122 VisitForValue(expr->right(), kAccumulator);
3123 Condition cc = eq;
3124 bool strict = false;
3125 switch (expr->op()) {
3126 case Token::EQ_STRICT:
3127 strict = true;
3128 // Fall through
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003129 case Token::EQ: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003130 cc = eq;
3131 __ pop(r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003132 // If either operand is constant null we do a fast compare
3133 // against null.
3134 Literal* right_literal = expr->right()->AsLiteral();
3135 Literal* left_literal = expr->left()->AsLiteral();
3136 if (right_literal != NULL && right_literal->handle()->IsNull()) {
3137 EmitNullCompare(strict, r1, r0, if_true, if_false, r2);
3138 Apply(context_, if_true, if_false);
3139 return;
3140 } else if (left_literal != NULL && left_literal->handle()->IsNull()) {
3141 EmitNullCompare(strict, r0, r1, if_true, if_false, r2);
3142 Apply(context_, if_true, if_false);
3143 return;
3144 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003145 break;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003146 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003147 case Token::LT:
3148 cc = lt;
3149 __ pop(r1);
3150 break;
3151 case Token::GT:
3152 // Reverse left and right sides to obtain ECMA-262 conversion order.
3153 cc = lt;
3154 __ mov(r1, result_register());
3155 __ pop(r0);
3156 break;
3157 case Token::LTE:
3158 // Reverse left and right sides to obtain ECMA-262 conversion order.
3159 cc = ge;
3160 __ mov(r1, result_register());
3161 __ pop(r0);
3162 break;
3163 case Token::GTE:
3164 cc = ge;
3165 __ pop(r1);
3166 break;
3167 case Token::IN:
3168 case Token::INSTANCEOF:
3169 default:
3170 UNREACHABLE();
3171 }
3172
3173 // The comparison stub expects the smi vs. smi case to be handled
3174 // before it is called.
3175 Label slow_case;
3176 __ orr(r2, r0, Operand(r1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003177 __ BranchOnNotSmi(r2, &slow_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003178 __ cmp(r1, r0);
3179 __ b(cc, if_true);
3180 __ jmp(if_false);
3181
3182 __ bind(&slow_case);
ager@chromium.orgb5737492010-07-15 09:29:43 +00003183 CompareStub stub(cc, strict, kBothCouldBeNaN, true, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003184 __ CallStub(&stub);
3185 __ cmp(r0, Operand(0));
3186 __ b(cc, if_true);
3187 __ jmp(if_false);
3188 }
3189 }
3190
3191 // Convert the result of the comparison into one expected for this
3192 // expression's context.
3193 Apply(context_, if_true, if_false);
3194}
3195
3196
3197void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3198 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3199 Apply(context_, r0);
3200}
3201
3202
3203Register FullCodeGenerator::result_register() { return r0; }
3204
3205
3206Register FullCodeGenerator::context_register() { return cp; }
3207
3208
3209void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3210 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3211 __ str(value, MemOperand(fp, frame_offset));
3212}
3213
3214
3215void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
3216 __ ldr(dst, CodeGenerator::ContextOperand(cp, context_index));
3217}
3218
3219
3220// ----------------------------------------------------------------------------
3221// Non-local control flow support.
3222
3223void FullCodeGenerator::EnterFinallyBlock() {
3224 ASSERT(!result_register().is(r1));
3225 // Store result register while executing finally block.
3226 __ push(result_register());
3227 // Cook return address in link register to stack (smi encoded Code* delta)
3228 __ sub(r1, lr, Operand(masm_->CodeObject()));
3229 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3230 ASSERT_EQ(0, kSmiTag);
3231 __ add(r1, r1, Operand(r1)); // Convert to smi.
3232 __ push(r1);
3233}
3234
3235
3236void FullCodeGenerator::ExitFinallyBlock() {
3237 ASSERT(!result_register().is(r1));
3238 // Restore result register from stack.
3239 __ pop(r1);
3240 // Uncook return address and return.
3241 __ pop(result_register());
3242 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3243 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3244 __ add(pc, r1, Operand(masm_->CodeObject()));
3245}
3246
3247
3248#undef __
3249
3250} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003251
3252#endif // V8_TARGET_ARCH_ARM