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