blob: 912fefc79f2e18a6fb08f787fc317de5a133cb25 [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
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000032#include "code-stubs.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000033#include "codegen-inl.h"
34#include "compiler.h"
35#include "debug.h"
36#include "full-codegen.h"
37#include "parser.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000038#include "scopes.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000039
40namespace v8 {
41namespace internal {
42
43#define __ ACCESS_MASM(masm_)
44
45// Generate code for a JS function. On entry to the function the receiver
46// and arguments have been pushed on the stack left to right. The actual
47// argument count matches the formal parameter count expected by the
48// function.
49//
50// The live registers are:
51// o r1: the JS function object being called (ie, ourselves)
52// o cp: our context
53// o fp: our caller's frame pointer
54// o sp: stack pointer
55// o lr: return address
56//
57// The function builds a JS frame. Please see JavaScriptFrameConstants in
58// frames-arm.h for its layout.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000059void FullCodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.org5c838252010-02-19 08:53:10 +000060 ASSERT(info_ == NULL);
61 info_ = info;
62 SetFunctionPosition(function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000063 Comment cmnt(masm_, "[ function compiled by full code generator");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000064
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000065 int locals_count = scope()->num_stack_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000066
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000067 __ Push(lr, fp, cp, r1);
68 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);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000079 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000080 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000081
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000082 bool function_in_register = true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000083
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000084 // Possibly allocate a local context.
85 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
86 if (heap_slots > 0) {
87 Comment cmnt(masm_, "[ Allocate local context");
88 // Argument to NewContext is the function, which is in r1.
89 __ push(r1);
90 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
91 FastNewContextStub stub(heap_slots);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000092 __ CallStub(&stub);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000093 } else {
94 __ CallRuntime(Runtime::kNewContext, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000095 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +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.
101 int num_parameters = scope()->num_parameters();
102 for (int i = 0; i < num_parameters; i++) {
103 Slot* slot = scope()->parameter(i)->slot();
104 if (slot != NULL && slot->type() == Slot::CONTEXT) {
105 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
106 (num_parameters - 1 - i) * kPointerSize;
107 // Load parameter from stack.
108 __ ldr(r0, MemOperand(fp, parameter_offset));
109 // Store it in the context.
110 __ mov(r1, Operand(Context::SlotOffset(slot->index())));
111 __ str(r0, MemOperand(cp, r1));
112 // Update the write barrier. This clobbers all involved
113 // registers, so we have to use two more registers to avoid
114 // clobbering cp.
115 __ mov(r2, Operand(cp));
116 __ RecordWrite(r2, Operand(r1), r3, r0);
117 }
118 }
119 }
120
121 Variable* arguments = scope()->arguments()->AsVariable();
122 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.
132 int offset = scope()->num_parameters() * kPointerSize;
133 __ add(r2, fp,
134 Operand(StandardFrameConstants::kCallerSPOffset + offset));
135 __ mov(r1, Operand(Smi::FromInt(scope()->num_parameters())));
136 __ Push(r3, r2, r1);
137
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 =
148 scope()->arguments_shadow()->AsVariable()->slot();
149 Move(dot_arguments_slot, r3, r1, r2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000150 }
151
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000152 { Comment cmnt(masm_, "[ Declarations");
153 // For named function expressions, declare the function name as a
154 // constant.
155 if (scope()->is_function_scope() && scope()->function() != NULL) {
156 EmitDeclaration(scope()->function(), Variable::CONST, NULL);
157 }
158 // Visit all the explicit declarations unless there is an illegal
159 // redeclaration.
160 if (scope()->HasIllegalRedeclaration()) {
161 scope()->VisitIllegalRedeclaration(this);
162 } else {
163 VisitDeclarations(scope()->declarations());
164 }
165 }
166
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000167 // Check the stack for overflow or break request.
168 // Put the lr setup instruction in the delay slot. The kInstrSize is
169 // added to the implicit 8 byte offset that always applies to operations
170 // with pc and gives a return address 12 bytes down.
171 { Comment cmnt(masm_, "[ Stack check");
172 __ LoadRoot(r2, Heap::kStackLimitRootIndex);
173 __ add(lr, pc, Operand(Assembler::kInstrSize));
174 __ cmp(sp, Operand(r2));
175 StackCheckStub stub;
176 __ mov(pc,
177 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
178 RelocInfo::CODE_TARGET),
179 LeaveCC,
180 lo);
181 }
182
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000183 if (FLAG_trace) {
184 __ CallRuntime(Runtime::kTraceEnter, 0);
185 }
186
187 { Comment cmnt(masm_, "[ Body");
188 ASSERT(loop_depth() == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000189 VisitStatements(function()->body());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000190 ASSERT(loop_depth() == 0);
191 }
192
193 { Comment cmnt(masm_, "[ return <undefined>;");
194 // Emit a 'return undefined' in case control fell off the end of the
195 // body.
196 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
197 }
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000198 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000199}
200
201
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000202void FullCodeGenerator::EmitReturnSequence() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000203 Comment cmnt(masm_, "[ Return sequence");
204 if (return_label_.is_bound()) {
205 __ b(&return_label_);
206 } else {
207 __ bind(&return_label_);
208 if (FLAG_trace) {
209 // Push the return value on the stack as the parameter.
210 // Runtime::TraceExit returns its parameter in r0.
211 __ push(r0);
212 __ CallRuntime(Runtime::kTraceExit, 1);
213 }
214
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000215#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000216 // Add a label for checking the size of the code used for returning.
217 Label check_exit_codesize;
218 masm_->bind(&check_exit_codesize);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000219#endif
220 // Make sure that the constant pool is not emitted inside of the return
221 // sequence.
222 { Assembler::BlockConstPoolScope block_const_pool(masm_);
223 // Here we use masm_-> instead of the __ macro to avoid the code coverage
224 // tool from instrumenting as we rely on the code size here.
225 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000226 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000227 __ RecordJSReturn();
228 masm_->mov(sp, fp);
229 masm_->ldm(ia_w, sp, fp.bit() | lr.bit());
230 masm_->add(sp, sp, Operand(sp_delta));
231 masm_->Jump(lr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000232 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000233
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000234#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000235 // Check that the size of the code used for returning matches what is
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000236 // expected by the debugger. If the sp_delts above cannot be encoded in the
237 // add instruction the add will generate two instructions.
238 int return_sequence_length =
239 masm_->InstructionsGeneratedSince(&check_exit_codesize);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000240 CHECK(return_sequence_length ==
241 Assembler::kJSReturnSequenceInstructions ||
242 return_sequence_length ==
243 Assembler::kJSReturnSequenceInstructions + 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000244#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000245 }
246}
247
248
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000249FullCodeGenerator::ConstantOperand FullCodeGenerator::GetConstantOperand(
250 Token::Value op, Expression* left, Expression* right) {
251 ASSERT(ShouldInlineSmiCase(op));
252 return kNoConstants;
253}
254
255
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000256void FullCodeGenerator::Apply(Expression::Context context, Register reg) {
257 switch (context) {
258 case Expression::kUninitialized:
259 UNREACHABLE();
260
261 case Expression::kEffect:
262 // Nothing to do.
263 break;
264
265 case Expression::kValue:
266 // Move value into place.
267 switch (location_) {
268 case kAccumulator:
269 if (!reg.is(result_register())) __ mov(result_register(), reg);
270 break;
271 case kStack:
272 __ push(reg);
273 break;
274 }
275 break;
276
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000277 case Expression::kTest:
ricow@chromium.org65fae842010-08-25 15:26:24 +0000278 // For simplicity we always test the accumulator register.
279 if (!reg.is(result_register())) __ mov(result_register(), reg);
280 DoTest(true_label_, false_label_, fall_through_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000281 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:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000295 // On ARM we have to move the value into a register to do anything
296 // with it.
297 Move(result_register(), slot);
298 Apply(context, result_register());
299 break;
300 }
301}
302
303
304void FullCodeGenerator::Apply(Expression::Context context, Literal* lit) {
305 switch (context) {
306 case Expression::kUninitialized:
307 UNREACHABLE();
308 case Expression::kEffect:
309 break;
310 // Nothing to do.
311 case Expression::kValue:
312 case Expression::kTest:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000313 // On ARM we have to move the value into a register to do anything
314 // with it.
315 __ mov(result_register(), Operand(lit->handle()));
316 Apply(context, result_register());
317 break;
318 }
319}
320
321
322void FullCodeGenerator::ApplyTOS(Expression::Context context) {
323 switch (context) {
324 case Expression::kUninitialized:
325 UNREACHABLE();
326
327 case Expression::kEffect:
328 __ Drop(1);
329 break;
330
331 case Expression::kValue:
332 switch (location_) {
333 case kAccumulator:
334 __ pop(result_register());
335 break;
336 case kStack:
337 break;
338 }
339 break;
340
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000341 case Expression::kTest:
ricow@chromium.org65fae842010-08-25 15:26:24 +0000342 __ pop(result_register());
343 DoTest(true_label_, false_label_, fall_through_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000344 break;
345 }
346}
347
348
349void FullCodeGenerator::DropAndApply(int count,
350 Expression::Context context,
351 Register reg) {
352 ASSERT(count > 0);
353 ASSERT(!reg.is(sp));
354 switch (context) {
355 case Expression::kUninitialized:
356 UNREACHABLE();
357
358 case Expression::kEffect:
359 __ Drop(count);
360 break;
361
362 case Expression::kValue:
363 switch (location_) {
364 case kAccumulator:
365 __ Drop(count);
366 if (!reg.is(result_register())) __ mov(result_register(), reg);
367 break;
368 case kStack:
369 if (count > 1) __ Drop(count - 1);
370 __ str(reg, MemOperand(sp));
371 break;
372 }
373 break;
374
375 case Expression::kTest:
ricow@chromium.org65fae842010-08-25 15:26:24 +0000376 __ Drop(count);
377 if (!reg.is(result_register())) __ mov(result_register(), reg);
378 DoTest(true_label_, false_label_, fall_through_);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000379 break;
380 }
381}
382
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000383
384void FullCodeGenerator::Apply(Expression::Context context,
385 Label* materialize_true,
386 Label* materialize_false) {
387 switch (context) {
388 case Expression::kUninitialized:
389
390 case Expression::kEffect:
391 ASSERT_EQ(materialize_true, materialize_false);
392 __ bind(materialize_true);
393 break;
394
395 case Expression::kValue: {
396 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000397 switch (location_) {
398 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000399 __ bind(materialize_true);
400 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
401 __ jmp(&done);
402 __ bind(materialize_false);
403 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000404 break;
405 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000406 __ bind(materialize_true);
407 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
408 __ push(ip);
409 __ jmp(&done);
410 __ bind(materialize_false);
411 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
412 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000413 break;
414 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000415 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000416 break;
417 }
418
419 case Expression::kTest:
420 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000421 }
422}
423
424
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000425// Convert constant control flow (true or false) to the result expected for
426// a given expression context.
427void FullCodeGenerator::Apply(Expression::Context context, bool flag) {
428 switch (context) {
429 case Expression::kUninitialized:
430 UNREACHABLE();
431 break;
432 case Expression::kEffect:
433 break;
434 case Expression::kValue: {
435 Heap::RootListIndex value_root_index =
436 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
437 switch (location_) {
438 case kAccumulator:
439 __ LoadRoot(result_register(), value_root_index);
440 break;
441 case kStack:
442 __ LoadRoot(ip, value_root_index);
443 __ push(ip);
444 break;
445 }
446 break;
447 }
448 case Expression::kTest:
ricow@chromium.org65fae842010-08-25 15:26:24 +0000449 if (flag) {
450 if (true_label_ != fall_through_) __ b(true_label_);
451 } else {
452 if (false_label_ != fall_through_) __ b(false_label_);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000453 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000454 break;
455 }
456}
457
458
ricow@chromium.org65fae842010-08-25 15:26:24 +0000459void FullCodeGenerator::DoTest(Label* if_true,
460 Label* if_false,
461 Label* fall_through) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000462 // Call the runtime to find the boolean value of the source and then
463 // translate it into control flow to the pair of labels.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000464 __ push(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000465 __ CallRuntime(Runtime::kToBool, 1);
466 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
467 __ cmp(r0, ip);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000468 Split(eq, if_true, if_false, fall_through);
469}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000470
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000471
ricow@chromium.org65fae842010-08-25 15:26:24 +0000472void FullCodeGenerator::Split(Condition cc,
473 Label* if_true,
474 Label* if_false,
475 Label* fall_through) {
476 if (if_false == fall_through) {
477 __ b(cc, if_true);
478 } else if (if_true == fall_through) {
479 __ b(NegateCondition(cc), if_false);
480 } else {
481 __ b(cc, if_true);
482 __ b(if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000483 }
484}
485
486
487MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
488 switch (slot->type()) {
489 case Slot::PARAMETER:
490 case Slot::LOCAL:
491 return MemOperand(fp, SlotOffset(slot));
492 case Slot::CONTEXT: {
493 int context_chain_length =
ager@chromium.org5c838252010-02-19 08:53:10 +0000494 scope()->ContextChainLength(slot->var()->scope());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000495 __ LoadContext(scratch, context_chain_length);
496 return CodeGenerator::ContextOperand(scratch, slot->index());
497 }
498 case Slot::LOOKUP:
499 UNREACHABLE();
500 }
501 UNREACHABLE();
502 return MemOperand(r0, 0);
503}
504
505
506void FullCodeGenerator::Move(Register destination, Slot* source) {
507 // Use destination as scratch.
508 MemOperand slot_operand = EmitSlotSearch(source, destination);
509 __ ldr(destination, slot_operand);
510}
511
512
513void FullCodeGenerator::Move(Slot* dst,
514 Register src,
515 Register scratch1,
516 Register scratch2) {
517 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
518 ASSERT(!scratch1.is(src) && !scratch2.is(src));
519 MemOperand location = EmitSlotSearch(dst, scratch1);
520 __ str(src, location);
521 // Emit the write barrier code if the location is in the heap.
522 if (dst->type() == Slot::CONTEXT) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000523 __ RecordWrite(scratch1,
524 Operand(Context::SlotOffset(dst->index())),
525 scratch2,
526 src);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000527 }
528}
529
530
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000531void FullCodeGenerator::EmitDeclaration(Variable* variable,
532 Variable::Mode mode,
533 FunctionLiteral* function) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000534 Comment cmnt(masm_, "[ Declaration");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000535 ASSERT(variable != NULL); // Must have been resolved.
536 Slot* slot = variable->slot();
537 Property* prop = variable->AsProperty();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000538
539 if (slot != NULL) {
540 switch (slot->type()) {
541 case Slot::PARAMETER:
542 case Slot::LOCAL:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000543 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000544 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
545 __ str(ip, MemOperand(fp, SlotOffset(slot)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000546 } else if (function != NULL) {
547 VisitForValue(function, kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000548 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
549 }
550 break;
551
552 case Slot::CONTEXT:
553 // We bypass the general EmitSlotSearch because we know more about
554 // this specific context.
555
556 // The variable in the decl always resides in the current context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000557 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000558 if (FLAG_debug_code) {
559 // Check if we have the correct context pointer.
560 __ ldr(r1,
561 CodeGenerator::ContextOperand(cp, Context::FCONTEXT_INDEX));
562 __ cmp(r1, cp);
563 __ Check(eq, "Unexpected declaration in current context.");
564 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000565 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000566 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
567 __ str(ip, CodeGenerator::ContextOperand(cp, slot->index()));
568 // No write barrier since the_hole_value is in old space.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000569 } else if (function != NULL) {
570 VisitForValue(function, kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000571 __ str(result_register(),
572 CodeGenerator::ContextOperand(cp, slot->index()));
573 int offset = Context::SlotOffset(slot->index());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000574 // We know that we have written a function, which is not a smi.
575 __ mov(r1, Operand(cp));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000576 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000577 }
578 break;
579
580 case Slot::LOOKUP: {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000581 __ mov(r2, Operand(variable->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000582 // Declaration nodes are always introduced in one of two modes.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000583 ASSERT(mode == Variable::VAR ||
584 mode == Variable::CONST);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000585 PropertyAttributes attr =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000586 (mode == Variable::VAR) ? NONE : READ_ONLY;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000587 __ mov(r1, Operand(Smi::FromInt(attr)));
588 // Push initial value, if any.
589 // Note: For variables we must not push an initial value (such as
590 // 'undefined') because we may have a (legal) redeclaration and we
591 // must not destroy the current value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000592 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000593 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000594 __ Push(cp, r2, r1, r0);
595 } else if (function != NULL) {
596 __ Push(cp, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000597 // Push initial value for function declaration.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000598 VisitForValue(function, kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000599 } else {
600 __ mov(r0, Operand(Smi::FromInt(0))); // No initial value!
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000601 __ Push(cp, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000602 }
603 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
604 break;
605 }
606 }
607
608 } else if (prop != NULL) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000609 if (function != NULL || mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000610 // We are declaring a function or constant that rewrites to a
611 // property. Use (keyed) IC to set the initial value.
612 VisitForValue(prop->obj(), kStack);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000613 if (function != NULL) {
614 VisitForValue(prop->key(), kStack);
615 VisitForValue(function, kAccumulator);
616 __ pop(r1); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000617 } else {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000618 VisitForValue(prop->key(), kAccumulator);
619 __ mov(r1, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000620 __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex);
621 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000622 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000623
624 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
625 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000626 // Value in r0 is ignored (declarations are statements).
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000627 }
628 }
629}
630
631
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000632void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
633 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
634}
635
636
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000637void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
638 // Call the runtime to declare the globals.
639 // The context is the first argument.
640 __ mov(r1, Operand(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000641 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000642 __ Push(cp, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000643 __ CallRuntime(Runtime::kDeclareGlobals, 3);
644 // Return value is ignored.
645}
646
647
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000648void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000649 Comment cmnt(masm_, "[ SwitchStatement");
650 Breakable nested_statement(this, stmt);
651 SetStatementPosition(stmt);
652 // Keep the switch value on the stack until a case matches.
653 VisitForValue(stmt->tag(), kStack);
654
655 ZoneList<CaseClause*>* clauses = stmt->cases();
656 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
657
658 Label next_test; // Recycled for each test.
659 // Compile all the tests with branches to their bodies.
660 for (int i = 0; i < clauses->length(); i++) {
661 CaseClause* clause = clauses->at(i);
662 // The default is not a test, but remember it as final fall through.
663 if (clause->is_default()) {
664 default_clause = clause;
665 continue;
666 }
667
668 Comment cmnt(masm_, "[ Case comparison");
669 __ bind(&next_test);
670 next_test.Unuse();
671
672 // Compile the label expression.
673 VisitForValue(clause->label(), kAccumulator);
674
ricow@chromium.org65fae842010-08-25 15:26:24 +0000675 // Perform the comparison as if via '==='.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000676 __ ldr(r1, MemOperand(sp, 0)); // Switch value.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000677 if (ShouldInlineSmiCase(Token::EQ_STRICT)) {
678 Label slow_case;
679 __ orr(r2, r1, r0);
680 __ tst(r2, Operand(kSmiTagMask));
681 __ b(ne, &slow_case);
682 __ cmp(r1, r0);
683 __ b(ne, &next_test);
684 __ Drop(1); // Switch value is no longer needed.
685 __ b(clause->body_target()->entry_label());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000686 __ bind(&slow_case);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000687 }
688
ager@chromium.orgb5737492010-07-15 09:29:43 +0000689 CompareStub stub(eq, true, kBothCouldBeNaN, true, r1, r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000690 __ CallStub(&stub);
ager@chromium.orgb5737492010-07-15 09:29:43 +0000691 __ cmp(r0, Operand(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000692 __ b(ne, &next_test);
693 __ Drop(1); // Switch value is no longer needed.
694 __ b(clause->body_target()->entry_label());
695 }
696
697 // Discard the test value and jump to the default if present, otherwise to
698 // the end of the statement.
699 __ bind(&next_test);
700 __ Drop(1); // Switch value is no longer needed.
701 if (default_clause == NULL) {
702 __ b(nested_statement.break_target());
703 } else {
704 __ b(default_clause->body_target()->entry_label());
705 }
706
707 // Compile all the case bodies.
708 for (int i = 0; i < clauses->length(); i++) {
709 Comment cmnt(masm_, "[ Case body");
710 CaseClause* clause = clauses->at(i);
711 __ bind(clause->body_target()->entry_label());
712 VisitStatements(clause->statements());
713 }
714
715 __ bind(nested_statement.break_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000716}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000717
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000718
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000719void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000720 Comment cmnt(masm_, "[ ForInStatement");
721 SetStatementPosition(stmt);
722
723 Label loop, exit;
724 ForIn loop_statement(this, stmt);
725 increment_loop_depth();
726
727 // Get the object to enumerate over. Both SpiderMonkey and JSC
728 // ignore null and undefined in contrast to the specification; see
729 // ECMA-262 section 12.6.4.
730 VisitForValue(stmt->enumerable(), kAccumulator);
731 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
732 __ cmp(r0, ip);
733 __ b(eq, &exit);
734 __ LoadRoot(ip, Heap::kNullValueRootIndex);
735 __ cmp(r0, ip);
736 __ b(eq, &exit);
737
738 // Convert the object to a JS object.
739 Label convert, done_convert;
740 __ BranchOnSmi(r0, &convert);
741 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
742 __ b(hs, &done_convert);
743 __ bind(&convert);
744 __ push(r0);
745 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
746 __ bind(&done_convert);
747 __ push(r0);
748
749 // TODO(kasperl): Check cache validity in generated code. This is a
750 // fast case for the JSObject::IsSimpleEnum cache validity
751 // checks. If we cannot guarantee cache validity, call the runtime
752 // system to check cache validity or get the property names in a
753 // fixed array.
754
755 // Get the set of properties to enumerate.
756 __ push(r0); // Duplicate the enumerable object on the stack.
757 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
758
759 // If we got a map from the runtime call, we can do a fast
760 // modification check. Otherwise, we got a fixed array, and we have
761 // to do a slow check.
762 Label fixed_array;
763 __ mov(r2, r0);
764 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
765 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
766 __ cmp(r1, ip);
767 __ b(ne, &fixed_array);
768
769 // We got a map in register r0. Get the enumeration cache from it.
770 __ ldr(r1, FieldMemOperand(r0, Map::kInstanceDescriptorsOffset));
771 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
772 __ ldr(r2, FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
773
774 // Setup the four remaining stack slots.
775 __ push(r0); // Map.
776 __ ldr(r1, FieldMemOperand(r2, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000777 __ mov(r0, Operand(Smi::FromInt(0)));
778 // Push enumeration cache, enumeration cache length (as smi) and zero.
779 __ Push(r2, r1, r0);
780 __ jmp(&loop);
781
782 // We got a fixed array in register r0. Iterate through that.
783 __ bind(&fixed_array);
784 __ mov(r1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
785 __ Push(r1, r0);
786 __ ldr(r1, FieldMemOperand(r0, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000787 __ mov(r0, Operand(Smi::FromInt(0)));
788 __ Push(r1, r0); // Fixed array length (as smi) and initial index.
789
790 // Generate code for doing the condition check.
791 __ bind(&loop);
792 // Load the current count to r0, load the length to r1.
793 __ Ldrd(r0, r1, MemOperand(sp, 0 * kPointerSize));
794 __ cmp(r0, r1); // Compare to the array length.
795 __ b(hs, loop_statement.break_target());
796
797 // Get the current entry of the array into register r3.
798 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
799 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
800 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
801
802 // Get the expected map from the stack or a zero map in the
803 // permanent slow case into register r2.
804 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
805
806 // Check if the expected map still matches that of the enumerable.
807 // If not, we have to filter the key.
808 Label update_each;
809 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
810 __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
811 __ cmp(r4, Operand(r2));
812 __ b(eq, &update_each);
813
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000814 // Convert the entry to a string or (smi) 0 if it isn't a property
815 // any more. If the property has been removed while iterating, we
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000816 // just skip it.
817 __ push(r1); // Enumerable.
818 __ push(r3); // Current entry.
819 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000820 __ mov(r3, Operand(r0), SetCC);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000821 __ b(eq, loop_statement.continue_target());
822
823 // Update the 'each' property or variable from the possibly filtered
824 // entry in register r3.
825 __ bind(&update_each);
826 __ mov(result_register(), r3);
827 // Perform the assignment as if via '='.
828 EmitAssignment(stmt->each());
829
830 // Generate code for the body of the loop.
831 Label stack_limit_hit, stack_check_done;
832 Visit(stmt->body());
833
834 __ StackLimitCheck(&stack_limit_hit);
835 __ bind(&stack_check_done);
836
837 // Generate code for the going to the next element by incrementing
838 // the index (smi) stored on top of the stack.
839 __ bind(loop_statement.continue_target());
840 __ pop(r0);
841 __ add(r0, r0, Operand(Smi::FromInt(1)));
842 __ push(r0);
843 __ b(&loop);
844
845 // Slow case for the stack limit check.
846 StackCheckStub stack_check_stub;
847 __ bind(&stack_limit_hit);
848 __ CallStub(&stack_check_stub);
849 __ b(&stack_check_done);
850
851 // Remove the pointers stored on the stack.
852 __ bind(loop_statement.break_target());
853 __ Drop(5);
854
855 // Exit and decrement the loop depth.
856 __ bind(&exit);
857 decrement_loop_depth();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000858}
859
860
861void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info) {
862 // Use the fast case closure allocation code that allocates in new
863 // space for nested functions that don't need literals cloning.
864 if (scope()->is_function_scope() && info->num_literals() == 0) {
865 FastNewClosureStub stub;
866 __ mov(r0, Operand(info));
867 __ push(r0);
868 __ CallStub(&stub);
869 } else {
870 __ mov(r0, Operand(info));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000871 __ Push(cp, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000872 __ CallRuntime(Runtime::kNewClosure, 2);
873 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000874 Apply(context_, r0);
875}
876
877
878void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
879 Comment cmnt(masm_, "[ VariableProxy");
880 EmitVariableLoad(expr->var(), context_);
881}
882
883
884void FullCodeGenerator::EmitVariableLoad(Variable* var,
885 Expression::Context context) {
886 // Four cases: non-this global variables, lookup slots, all other
887 // types of slots, and parameters that rewrite to explicit property
888 // accesses on the arguments object.
889 Slot* slot = var->slot();
890 Property* property = var->AsProperty();
891
892 if (var->is_global() && !var->is_this()) {
893 Comment cmnt(masm_, "Global variable");
894 // Use inline caching. Variable name is passed in r2 and the global
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000895 // object (receiver) in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000896 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000897 __ mov(r2, Operand(var->name()));
898 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
899 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000900 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000901
902 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
903 Comment cmnt(masm_, "Lookup slot");
904 __ mov(r1, Operand(var->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000905 __ Push(cp, r1); // Context and name.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000906 __ CallRuntime(Runtime::kLoadContextSlot, 2);
907 Apply(context, r0);
908
909 } else if (slot != NULL) {
910 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
911 ? "Context slot"
912 : "Stack slot");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000913 if (var->mode() == Variable::CONST) {
914 // Constants may be the hole value if they have not been initialized.
915 // Unhole them.
916 Label done;
917 MemOperand slot_operand = EmitSlotSearch(slot, r0);
918 __ ldr(r0, slot_operand);
919 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
920 __ cmp(r0, ip);
921 __ b(ne, &done);
922 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
923 __ bind(&done);
924 Apply(context, r0);
925 } else {
926 Apply(context, slot);
927 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000928 } else {
929 Comment cmnt(masm_, "Rewritten parameter");
930 ASSERT_NOT_NULL(property);
931 // Rewritten parameter accesses are of the form "slot[literal]".
932
933 // Assert that the object is in a slot.
934 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
935 ASSERT_NOT_NULL(object_var);
936 Slot* object_slot = object_var->slot();
937 ASSERT_NOT_NULL(object_slot);
938
939 // Load the object.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000940 Move(r1, object_slot);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000941
942 // Assert that the key is a smi.
943 Literal* key_literal = property->key()->AsLiteral();
944 ASSERT_NOT_NULL(key_literal);
945 ASSERT(key_literal->handle()->IsSmi());
946
947 // Load the key.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000948 __ mov(r0, Operand(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000949
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000950 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000951 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
952 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000953 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000954 }
955}
956
957
958void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
959 Comment cmnt(masm_, "[ RegExpLiteral");
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000960 Label materialized;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000961 // Registers will be used as follows:
962 // r4 = JS function, literals array
963 // r3 = literal index
964 // r2 = RegExp pattern
965 // r1 = RegExp flags
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000966 // r0 = temp + materialized value (RegExp literal)
ricow@chromium.org65fae842010-08-25 15:26:24 +0000967 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
968 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000969 int literal_offset =
ricow@chromium.org65fae842010-08-25 15:26:24 +0000970 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000971 __ ldr(r0, FieldMemOperand(r4, literal_offset));
972 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
973 __ cmp(r0, ip);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000974 __ b(ne, &materialized);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000975
976 // Create regexp literal using runtime function.
977 // Result will be in r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000978 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
979 __ mov(r2, Operand(expr->pattern()));
980 __ mov(r1, Operand(expr->flags()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000981 __ Push(r4, r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000982 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000983
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000984 __ bind(&materialized);
985 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
986 __ push(r0);
987 __ mov(r0, Operand(Smi::FromInt(size)));
988 __ push(r0);
989 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000990
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000991 // After this, registers are used as follows:
992 // r0: Newly allocated regexp.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000993 // r1: Materialized regexp.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000994 // r2: temp.
995 __ pop(r1);
996 __ CopyFields(r0, r1, r2.bit(), size / kPointerSize);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000997 Apply(context_, r0);
998}
999
1000
1001void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1002 Comment cmnt(masm_, "[ ObjectLiteral");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001003 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1004 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1005 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1006 __ mov(r1, Operand(expr->constant_properties()));
1007 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001008 __ Push(r3, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001009 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001010 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001011 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001012 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001013 }
1014
1015 // If result_saved is true the result is on top of the stack. If
1016 // result_saved is false the result is in r0.
1017 bool result_saved = false;
1018
1019 for (int i = 0; i < expr->properties()->length(); i++) {
1020 ObjectLiteral::Property* property = expr->properties()->at(i);
1021 if (property->IsCompileTimeValue()) continue;
1022
1023 Literal* key = property->key();
1024 Expression* value = property->value();
1025 if (!result_saved) {
1026 __ push(r0); // Save result on stack
1027 result_saved = true;
1028 }
1029 switch (property->kind()) {
1030 case ObjectLiteral::Property::CONSTANT:
1031 UNREACHABLE();
1032 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1033 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1034 // Fall through.
1035 case ObjectLiteral::Property::COMPUTED:
1036 if (key->handle()->IsSymbol()) {
1037 VisitForValue(value, kAccumulator);
1038 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001039 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001040 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1041 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001042 break;
1043 }
1044 // Fall through.
1045 case ObjectLiteral::Property::PROTOTYPE:
1046 // Duplicate receiver on stack.
1047 __ ldr(r0, MemOperand(sp));
1048 __ push(r0);
1049 VisitForValue(key, kStack);
1050 VisitForValue(value, kStack);
1051 __ CallRuntime(Runtime::kSetProperty, 3);
1052 break;
1053 case ObjectLiteral::Property::GETTER:
1054 case ObjectLiteral::Property::SETTER:
1055 // Duplicate receiver on stack.
1056 __ ldr(r0, MemOperand(sp));
1057 __ push(r0);
1058 VisitForValue(key, kStack);
1059 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1060 Smi::FromInt(1) :
1061 Smi::FromInt(0)));
1062 __ push(r1);
1063 VisitForValue(value, kStack);
1064 __ CallRuntime(Runtime::kDefineAccessor, 4);
1065 break;
1066 }
1067 }
1068
1069 if (result_saved) {
1070 ApplyTOS(context_);
1071 } else {
1072 Apply(context_, r0);
1073 }
1074}
1075
1076
1077void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1078 Comment cmnt(masm_, "[ ArrayLiteral");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001079
1080 ZoneList<Expression*>* subexprs = expr->values();
1081 int length = subexprs->length();
1082
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001083 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1084 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1085 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1086 __ mov(r1, Operand(expr->constant_elements()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001087 __ Push(r3, r2, r1);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001088 if (expr->constant_elements()->map() == Heap::fixed_cow_array_map()) {
1089 FastCloneShallowArrayStub stub(
1090 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1091 __ CallStub(&stub);
1092 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
1093 } else if (expr->depth() > 1) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001094 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001095 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001096 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001097 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001098 FastCloneShallowArrayStub stub(
1099 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001100 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001101 }
1102
1103 bool result_saved = false; // Is the result saved to the stack?
1104
1105 // Emit code to evaluate all the non-constant subexpressions and to store
1106 // them into the newly cloned array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001107 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001108 Expression* subexpr = subexprs->at(i);
1109 // If the subexpression is a literal or a simple materialized literal it
1110 // is already set in the cloned array.
1111 if (subexpr->AsLiteral() != NULL ||
1112 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1113 continue;
1114 }
1115
1116 if (!result_saved) {
1117 __ push(r0);
1118 result_saved = true;
1119 }
1120 VisitForValue(subexpr, kAccumulator);
1121
1122 // Store the subexpression value in the array's elements.
1123 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
1124 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1125 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1126 __ str(result_register(), FieldMemOperand(r1, offset));
1127
1128 // Update the write barrier for the array store with r0 as the scratch
1129 // register.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001130 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001131 }
1132
1133 if (result_saved) {
1134 ApplyTOS(context_);
1135 } else {
1136 Apply(context_, r0);
1137 }
1138}
1139
1140
ager@chromium.org5c838252010-02-19 08:53:10 +00001141void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1142 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001143 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1144 // on the left-hand side.
1145 if (!expr->target()->IsValidLeftHandSide()) {
1146 VisitForEffect(expr->target());
1147 return;
1148 }
1149
ager@chromium.org5c838252010-02-19 08:53:10 +00001150 // Left-hand side can only be a property, a global or a (parameter or local)
1151 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1152 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1153 LhsKind assign_type = VARIABLE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001154 Property* property = expr->target()->AsProperty();
1155 if (property != NULL) {
1156 assign_type = (property->key()->IsPropertyName())
1157 ? NAMED_PROPERTY
1158 : KEYED_PROPERTY;
ager@chromium.org5c838252010-02-19 08:53:10 +00001159 }
1160
1161 // Evaluate LHS expression.
1162 switch (assign_type) {
1163 case VARIABLE:
1164 // Nothing to do here.
1165 break;
1166 case NAMED_PROPERTY:
1167 if (expr->is_compound()) {
1168 // We need the receiver both on the stack and in the accumulator.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001169 VisitForValue(property->obj(), kAccumulator);
ager@chromium.org5c838252010-02-19 08:53:10 +00001170 __ push(result_register());
1171 } else {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001172 VisitForValue(property->obj(), kStack);
ager@chromium.org5c838252010-02-19 08:53:10 +00001173 }
1174 break;
1175 case KEYED_PROPERTY:
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001176 if (expr->is_compound()) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001177 VisitForValue(property->obj(), kStack);
1178 VisitForValue(property->key(), kAccumulator);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001179 __ ldr(r1, MemOperand(sp, 0));
1180 __ push(r0);
1181 } else {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001182 VisitForValue(property->obj(), kStack);
1183 VisitForValue(property->key(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001184 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001185 break;
1186 }
1187
ager@chromium.org5c838252010-02-19 08:53:10 +00001188 if (expr->is_compound()) {
1189 Location saved_location = location_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001190 location_ = kAccumulator;
ager@chromium.org5c838252010-02-19 08:53:10 +00001191 switch (assign_type) {
1192 case VARIABLE:
1193 EmitVariableLoad(expr->target()->AsVariableProxy()->var(),
1194 Expression::kValue);
1195 break;
1196 case NAMED_PROPERTY:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001197 EmitNamedPropertyLoad(property);
ager@chromium.org5c838252010-02-19 08:53:10 +00001198 break;
1199 case KEYED_PROPERTY:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001200 EmitKeyedPropertyLoad(property);
ager@chromium.org5c838252010-02-19 08:53:10 +00001201 break;
1202 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001203
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001204 Token::Value op = expr->binary_op();
1205 ConstantOperand constant = ShouldInlineSmiCase(op)
1206 ? GetConstantOperand(op, expr->target(), expr->value())
1207 : kNoConstants;
1208 ASSERT(constant == kRightConstant || constant == kNoConstants);
1209 if (constant == kNoConstants) {
1210 __ push(r0); // Left operand goes on the stack.
1211 VisitForValue(expr->value(), kAccumulator);
1212 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001213
ricow@chromium.org65fae842010-08-25 15:26:24 +00001214 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1215 ? OVERWRITE_RIGHT
1216 : NO_OVERWRITE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001217 SetSourcePosition(expr->position() + 1);
1218 if (ShouldInlineSmiCase(op)) {
1219 EmitInlineSmiBinaryOp(expr,
1220 op,
1221 Expression::kValue,
1222 mode,
1223 expr->target(),
1224 expr->value(),
1225 constant);
1226 } else {
1227 EmitBinaryOp(op, Expression::kValue, mode);
1228 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001229 location_ = saved_location;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001230
1231 } else {
1232 VisitForValue(expr->value(), kAccumulator);
ager@chromium.org5c838252010-02-19 08:53:10 +00001233 }
1234
1235 // Record source position before possible IC call.
1236 SetSourcePosition(expr->position());
1237
1238 // Store the value.
1239 switch (assign_type) {
1240 case VARIABLE:
1241 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001242 expr->op(),
ager@chromium.org5c838252010-02-19 08:53:10 +00001243 context_);
1244 break;
1245 case NAMED_PROPERTY:
1246 EmitNamedPropertyAssignment(expr);
1247 break;
1248 case KEYED_PROPERTY:
1249 EmitKeyedPropertyAssignment(expr);
1250 break;
1251 }
1252}
1253
1254
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001255void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1256 SetSourcePosition(prop->position());
1257 Literal* key = prop->key()->AsLiteral();
1258 __ mov(r2, Operand(key->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001259 // Call load IC. It has arguments receiver and property name r0 and r2.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001260 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1261 __ Call(ic, RelocInfo::CODE_TARGET);
1262}
1263
1264
1265void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1266 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001267 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001268 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1269 __ Call(ic, RelocInfo::CODE_TARGET);
1270}
1271
1272
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001273void FullCodeGenerator::EmitInlineSmiBinaryOp(Expression* expr,
1274 Token::Value op,
1275 Expression::Context context,
1276 OverwriteMode mode,
1277 Expression* left,
1278 Expression* right,
1279 ConstantOperand constant) {
1280 ASSERT(constant == kNoConstants); // Only handled case.
1281 EmitBinaryOp(op, context, mode);
1282}
1283
1284
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001285void FullCodeGenerator::EmitBinaryOp(Token::Value op,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001286 Expression::Context context,
1287 OverwriteMode mode) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001288 __ pop(r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001289 GenericBinaryOpStub stub(op, mode, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001290 __ CallStub(&stub);
1291 Apply(context, r0);
1292}
1293
1294
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001295void FullCodeGenerator::EmitAssignment(Expression* expr) {
1296 // Invalid left-hand sides are rewritten to have a 'throw
1297 // ReferenceError' on the left-hand side.
1298 if (!expr->IsValidLeftHandSide()) {
1299 VisitForEffect(expr);
1300 return;
1301 }
1302
1303 // Left-hand side can only be a property, a global or a (parameter or local)
1304 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1305 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1306 LhsKind assign_type = VARIABLE;
1307 Property* prop = expr->AsProperty();
1308 if (prop != NULL) {
1309 assign_type = (prop->key()->IsPropertyName())
1310 ? NAMED_PROPERTY
1311 : KEYED_PROPERTY;
1312 }
1313
1314 switch (assign_type) {
1315 case VARIABLE: {
1316 Variable* var = expr->AsVariableProxy()->var();
1317 EmitVariableAssignment(var, Token::ASSIGN, Expression::kEffect);
1318 break;
1319 }
1320 case NAMED_PROPERTY: {
1321 __ push(r0); // Preserve value.
1322 VisitForValue(prop->obj(), kAccumulator);
1323 __ mov(r1, r0);
1324 __ pop(r0); // Restore value.
1325 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
1326 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1327 __ Call(ic, RelocInfo::CODE_TARGET);
1328 break;
1329 }
1330 case KEYED_PROPERTY: {
1331 __ push(r0); // Preserve value.
1332 VisitForValue(prop->obj(), kStack);
1333 VisitForValue(prop->key(), kAccumulator);
1334 __ mov(r1, r0);
1335 __ pop(r2);
1336 __ pop(r0); // Restore value.
1337 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1338 __ Call(ic, RelocInfo::CODE_TARGET);
1339 break;
1340 }
1341 }
1342}
1343
1344
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001345void FullCodeGenerator::EmitVariableAssignment(Variable* var,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001346 Token::Value op,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001347 Expression::Context context) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001348 // Left-hand sides that rewrite to explicit property accesses do not reach
1349 // here.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001350 ASSERT(var != NULL);
1351 ASSERT(var->is_global() || var->slot() != NULL);
1352
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001353 if (var->is_global()) {
1354 ASSERT(!var->is_this());
1355 // Assignment to a global variable. Use inline caching for the
1356 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001357 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001358 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001359 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001360 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1361 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001362
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001363 } else if (var->mode() != Variable::CONST || op == Token::INIT_CONST) {
1364 // Perform the assignment for non-const variables and for initialization
1365 // of const variables. Const assignments are simply skipped.
1366 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001367 Slot* slot = var->slot();
1368 switch (slot->type()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001369 case Slot::PARAMETER:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001370 case Slot::LOCAL:
1371 if (op == Token::INIT_CONST) {
1372 // Detect const reinitialization by checking for the hole value.
1373 __ ldr(r1, MemOperand(fp, SlotOffset(slot)));
1374 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1375 __ cmp(r1, ip);
1376 __ b(ne, &done);
1377 }
1378 // Perform the assignment.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001379 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1380 break;
1381
1382 case Slot::CONTEXT: {
1383 MemOperand target = EmitSlotSearch(slot, r1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001384 if (op == Token::INIT_CONST) {
1385 // Detect const reinitialization by checking for the hole value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001386 __ ldr(r2, target);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001387 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001388 __ cmp(r2, ip);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001389 __ b(ne, &done);
1390 }
1391 // Perform the assignment and issue the write barrier.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001392 __ str(result_register(), target);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001393 // RecordWrite may destroy all its register arguments.
1394 __ mov(r3, result_register());
1395 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001396 __ RecordWrite(r1, Operand(offset), r2, r3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001397 break;
1398 }
1399
1400 case Slot::LOOKUP:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001401 // Call the runtime for the assignment. The runtime will ignore
1402 // const reinitialization.
1403 __ push(r0); // Value.
1404 __ mov(r0, Operand(slot->var()->name()));
1405 __ Push(cp, r0); // Context and name.
1406 if (op == Token::INIT_CONST) {
1407 // The runtime will ignore const redeclaration.
1408 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1409 } else {
1410 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1411 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001412 break;
1413 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001414 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001415 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001416
ager@chromium.org5c838252010-02-19 08:53:10 +00001417 Apply(context, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001418}
1419
1420
1421void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1422 // Assignment to a property, using a named store IC.
1423 Property* prop = expr->target()->AsProperty();
1424 ASSERT(prop != NULL);
1425 ASSERT(prop->key()->AsLiteral() != NULL);
1426
1427 // If the assignment starts a block of assignments to the same object,
1428 // change to slow case to avoid the quadratic behavior of repeatedly
1429 // adding fast properties.
1430 if (expr->starts_initialization_block()) {
1431 __ push(result_register());
1432 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1433 __ push(ip);
1434 __ CallRuntime(Runtime::kToSlowProperties, 1);
1435 __ pop(result_register());
1436 }
1437
1438 // Record source code position before IC call.
1439 SetSourcePosition(expr->position());
1440 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001441 // Load receiver to r1. Leave a copy in the stack if needed for turning the
1442 // receiver into fast case.
ager@chromium.org5c838252010-02-19 08:53:10 +00001443 if (expr->ends_initialization_block()) {
1444 __ ldr(r1, MemOperand(sp));
1445 } else {
1446 __ pop(r1);
1447 }
1448
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001449 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1450 __ Call(ic, RelocInfo::CODE_TARGET);
1451
1452 // If the assignment ends an initialization block, revert to fast case.
1453 if (expr->ends_initialization_block()) {
1454 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001455 // Receiver is under the result value.
1456 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001457 __ push(ip);
1458 __ CallRuntime(Runtime::kToFastProperties, 1);
1459 __ pop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001460 DropAndApply(1, context_, r0);
1461 } else {
1462 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001463 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001464}
1465
1466
1467void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1468 // Assignment to a property, using a keyed store IC.
1469
1470 // If the assignment starts a block of assignments to the same object,
1471 // change to slow case to avoid the quadratic behavior of repeatedly
1472 // adding fast properties.
1473 if (expr->starts_initialization_block()) {
1474 __ push(result_register());
1475 // Receiver is now under the key and value.
1476 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1477 __ push(ip);
1478 __ CallRuntime(Runtime::kToSlowProperties, 1);
1479 __ pop(result_register());
1480 }
1481
1482 // Record source code position before IC call.
1483 SetSourcePosition(expr->position());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001484 __ pop(r1); // Key.
1485 // Load receiver to r2. Leave a copy in the stack if needed for turning the
1486 // receiver into fast case.
1487 if (expr->ends_initialization_block()) {
1488 __ ldr(r2, MemOperand(sp));
1489 } else {
1490 __ pop(r2);
1491 }
1492
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001493 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1494 __ Call(ic, RelocInfo::CODE_TARGET);
1495
1496 // If the assignment ends an initialization block, revert to fast case.
1497 if (expr->ends_initialization_block()) {
1498 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001499 // Receiver is under the result value.
1500 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001501 __ push(ip);
1502 __ CallRuntime(Runtime::kToFastProperties, 1);
1503 __ pop(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001504 DropAndApply(1, context_, r0);
1505 } else {
1506 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001507 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001508}
1509
1510
1511void FullCodeGenerator::VisitProperty(Property* expr) {
1512 Comment cmnt(masm_, "[ Property");
1513 Expression* key = expr->key();
1514
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001515 if (key->IsPropertyName()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001516 VisitForValue(expr->obj(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001517 EmitNamedPropertyLoad(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001518 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001519 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001520 VisitForValue(expr->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001521 VisitForValue(expr->key(), kAccumulator);
1522 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001523 EmitKeyedPropertyLoad(expr);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001524 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001525 }
1526}
1527
1528void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001529 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001530 RelocInfo::Mode mode) {
1531 // Code common for calls using the IC.
1532 ZoneList<Expression*>* args = expr->arguments();
1533 int arg_count = args->length();
1534 for (int i = 0; i < arg_count; i++) {
1535 VisitForValue(args->at(i), kStack);
1536 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001537 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001538 // Record source position for debugger.
1539 SetSourcePosition(expr->position());
1540 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001541 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1542 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001543 __ Call(ic, mode);
1544 // Restore context register.
1545 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001546 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001547}
1548
1549
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001550void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
1551 Expression* key,
1552 RelocInfo::Mode mode) {
1553 // Code common for calls using the IC.
1554 ZoneList<Expression*>* args = expr->arguments();
1555 int arg_count = args->length();
1556 for (int i = 0; i < arg_count; i++) {
1557 VisitForValue(args->at(i), kStack);
1558 }
1559 VisitForValue(key, kAccumulator);
1560 __ mov(r2, r0);
1561 // Record source position for debugger.
1562 SetSourcePosition(expr->position());
1563 // Call the IC initialization code.
1564 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1565 Handle<Code> ic = CodeGenerator::ComputeKeyedCallInitialize(arg_count,
1566 in_loop);
1567 __ Call(ic, mode);
1568 // Restore context register.
1569 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1570 Apply(context_, r0);
1571}
1572
1573
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001574void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1575 // Code common for calls using the call stub.
1576 ZoneList<Expression*>* args = expr->arguments();
1577 int arg_count = args->length();
1578 for (int i = 0; i < arg_count; i++) {
1579 VisitForValue(args->at(i), kStack);
1580 }
1581 // Record source position for debugger.
1582 SetSourcePosition(expr->position());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001583 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1584 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001585 __ CallStub(&stub);
1586 // Restore context register.
1587 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001588 DropAndApply(1, context_, r0);
1589}
1590
1591
1592void FullCodeGenerator::VisitCall(Call* expr) {
1593 Comment cmnt(masm_, "[ Call");
1594 Expression* fun = expr->expression();
1595 Variable* var = fun->AsVariableProxy()->AsVariable();
1596
1597 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001598 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1599 // resolve the function we need to call and the receiver of the
1600 // call. Then we call the resolved function using the given
1601 // arguments.
1602 VisitForValue(fun, kStack);
1603 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1604 __ push(r2); // Reserved receiver slot.
1605
1606 // Push the arguments.
1607 ZoneList<Expression*>* args = expr->arguments();
1608 int arg_count = args->length();
1609 for (int i = 0; i < arg_count; i++) {
1610 VisitForValue(args->at(i), kStack);
1611 }
1612
1613 // Push copy of the function - found below the arguments.
1614 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1615 __ push(r1);
1616
1617 // Push copy of the first argument or undefined if it doesn't exist.
1618 if (arg_count > 0) {
1619 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1620 __ push(r1);
1621 } else {
1622 __ push(r2);
1623 }
1624
1625 // Push the receiver of the enclosing function and do runtime call.
1626 __ ldr(r1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1627 __ push(r1);
1628 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1629
1630 // The runtime call returns a pair of values in r0 (function) and
1631 // r1 (receiver). Touch up the stack with the right values.
1632 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1633 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
1634
1635 // Record source position for debugger.
1636 SetSourcePosition(expr->position());
1637 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1638 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1639 __ CallStub(&stub);
1640 // Restore context register.
1641 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1642 DropAndApply(1, context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001643 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001644 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001645 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001646 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001647 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1648 } else if (var != NULL && var->slot() != NULL &&
1649 var->slot()->type() == Slot::LOOKUP) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001650 // Call to a lookup slot (dynamically introduced variable). Call the
1651 // runtime to find the function to call (returned in eax) and the object
1652 // holding it (returned in edx).
1653 __ push(context_register());
1654 __ mov(r2, Operand(var->name()));
1655 __ push(r2);
1656 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1657 __ push(r0); // Function.
1658 __ push(r1); // Receiver.
1659 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001660 } else if (fun->AsProperty() != NULL) {
1661 // Call to an object property.
1662 Property* prop = fun->AsProperty();
1663 Literal* key = prop->key()->AsLiteral();
1664 if (key != NULL && key->handle()->IsSymbol()) {
1665 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001666 VisitForValue(prop->obj(), kStack);
1667 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1668 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001669 // Call to a keyed property.
1670 // For a synthetic property use keyed load IC followed by function call,
1671 // for a regular property use keyed CallIC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001672 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001673 if (prop->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001674 VisitForValue(prop->key(), kAccumulator);
1675 // Record source code position for IC call.
1676 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001677 __ pop(r1); // We do not need to keep the receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001678
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001679 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1680 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001681 // Push result (function).
1682 __ push(r0);
1683 // Push Global receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001684 __ ldr(r1, CodeGenerator::GlobalObject());
1685 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001686 __ push(r1);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001687 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001688 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001689 EmitKeyedCallWithIC(expr, prop->key(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001690 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001691 }
1692 } else {
1693 // Call to some other expression. If the expression is an anonymous
1694 // function literal not called in a loop, mark it as one that should
1695 // also use the fast code generator.
1696 FunctionLiteral* lit = fun->AsFunctionLiteral();
1697 if (lit != NULL &&
1698 lit->name()->Equals(Heap::empty_string()) &&
1699 loop_depth() == 0) {
1700 lit->set_try_full_codegen(true);
1701 }
1702 VisitForValue(fun, kStack);
1703 // Load global receiver object.
1704 __ ldr(r1, CodeGenerator::GlobalObject());
1705 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1706 __ push(r1);
1707 // Emit function call.
1708 EmitCallWithStub(expr);
1709 }
1710}
1711
1712
1713void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1714 Comment cmnt(masm_, "[ CallNew");
1715 // According to ECMA-262, section 11.2.2, page 44, the function
1716 // expression in new calls must be evaluated before the
1717 // arguments.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001718
1719 // Push constructor on the stack. If it's not a function it's used as
1720 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
1721 // ignored.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001722 VisitForValue(expr->expression(), kStack);
1723
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001724 // Push the arguments ("left-to-right") on the stack.
1725 ZoneList<Expression*>* args = expr->arguments();
1726 int arg_count = args->length();
1727 for (int i = 0; i < arg_count; i++) {
1728 VisitForValue(args->at(i), kStack);
1729 }
1730
1731 // Call the construct call builtin that handles allocation and
1732 // constructor invocation.
1733 SetSourcePosition(expr->position());
1734
ricow@chromium.org65fae842010-08-25 15:26:24 +00001735 // Load function and argument count into r1 and r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001736 __ mov(r0, Operand(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001737 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001738
1739 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1740 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001741 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001742}
1743
1744
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001745void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1746 ASSERT(args->length() == 1);
1747
1748 VisitForValue(args->at(0), kAccumulator);
1749
1750 Label materialize_true, materialize_false;
1751 Label* if_true = NULL;
1752 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001753 Label* fall_through = NULL;
1754 PrepareTest(&materialize_true, &materialize_false,
1755 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001756
1757 __ BranchOnSmi(r0, if_true);
1758 __ b(if_false);
1759
1760 Apply(context_, if_true, if_false);
1761}
1762
1763
1764void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1765 ASSERT(args->length() == 1);
1766
1767 VisitForValue(args->at(0), kAccumulator);
1768
1769 Label materialize_true, materialize_false;
1770 Label* if_true = NULL;
1771 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001772 Label* fall_through = NULL;
1773 PrepareTest(&materialize_true, &materialize_false,
1774 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001775
1776 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001777 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001778
1779 Apply(context_, if_true, if_false);
1780}
1781
1782
1783void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1784 ASSERT(args->length() == 1);
1785
1786 VisitForValue(args->at(0), kAccumulator);
1787
1788 Label materialize_true, materialize_false;
1789 Label* if_true = NULL;
1790 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001791 Label* fall_through = NULL;
1792 PrepareTest(&materialize_true, &materialize_false,
1793 &if_true, &if_false, &fall_through);
1794
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001795 __ BranchOnSmi(r0, if_false);
1796 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1797 __ cmp(r0, ip);
1798 __ b(eq, if_true);
1799 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1800 // Undetectable objects behave like undefined when tested with typeof.
1801 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
1802 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1803 __ b(ne, if_false);
1804 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1805 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
1806 __ b(lt, if_false);
1807 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001808 Split(le, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001809
1810 Apply(context_, if_true, if_false);
1811}
1812
1813
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001814void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
1815 ASSERT(args->length() == 1);
1816
1817 VisitForValue(args->at(0), kAccumulator);
1818
1819 Label materialize_true, materialize_false;
1820 Label* if_true = NULL;
1821 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001822 Label* fall_through = NULL;
1823 PrepareTest(&materialize_true, &materialize_false,
1824 &if_true, &if_false, &fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001825
1826 __ BranchOnSmi(r0, if_false);
1827 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001828 Split(ge, if_true, if_false, fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001829
1830 Apply(context_, if_true, if_false);
1831}
1832
1833
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001834void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
1835 ASSERT(args->length() == 1);
1836
1837 VisitForValue(args->at(0), kAccumulator);
1838
1839 Label materialize_true, materialize_false;
1840 Label* if_true = NULL;
1841 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001842 Label* fall_through = NULL;
1843 PrepareTest(&materialize_true, &materialize_false,
1844 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001845
1846 __ BranchOnSmi(r0, if_false);
1847 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1848 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
1849 __ tst(r1, Operand(1 << Map::kIsUndetectable));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001850 Split(ne, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001851
1852 Apply(context_, if_true, if_false);
1853}
1854
1855
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001856void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
1857 ZoneList<Expression*>* args) {
1858
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;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001866 Label* fall_through = NULL;
1867 PrepareTest(&materialize_true, &materialize_false,
1868 &if_true, &if_false, &fall_through);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001869
1870 // Just indicate false, as %_IsStringWrapperSafeForDefaultValueOf() is only
1871 // used in a few functions in runtime.js which should not normally be hit by
1872 // this compiler.
1873 __ jmp(if_false);
1874 Apply(context_, if_true, if_false);
1875}
1876
1877
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001878void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
1879 ASSERT(args->length() == 1);
1880
1881 VisitForValue(args->at(0), kAccumulator);
1882
1883 Label materialize_true, materialize_false;
1884 Label* if_true = NULL;
1885 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001886 Label* fall_through = NULL;
1887 PrepareTest(&materialize_true, &materialize_false,
1888 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001889
1890 __ BranchOnSmi(r0, if_false);
1891 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001892 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001893
1894 Apply(context_, if_true, if_false);
1895}
1896
1897
1898void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
1899 ASSERT(args->length() == 1);
1900
1901 VisitForValue(args->at(0), kAccumulator);
1902
1903 Label materialize_true, materialize_false;
1904 Label* if_true = NULL;
1905 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001906 Label* fall_through = NULL;
1907 PrepareTest(&materialize_true, &materialize_false,
1908 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001909
1910 __ BranchOnSmi(r0, if_false);
1911 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001912 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001913
1914 Apply(context_, if_true, if_false);
1915}
1916
1917
1918void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
1919 ASSERT(args->length() == 1);
1920
1921 VisitForValue(args->at(0), kAccumulator);
1922
1923 Label materialize_true, materialize_false;
1924 Label* if_true = NULL;
1925 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001926 Label* fall_through = NULL;
1927 PrepareTest(&materialize_true, &materialize_false,
1928 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001929
1930 __ BranchOnSmi(r0, if_false);
1931 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001932 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001933
1934 Apply(context_, if_true, if_false);
1935}
1936
1937
1938
1939void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
1940 ASSERT(args->length() == 0);
1941
1942 Label materialize_true, materialize_false;
1943 Label* if_true = NULL;
1944 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001945 Label* fall_through = NULL;
1946 PrepareTest(&materialize_true, &materialize_false,
1947 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001948
1949 // Get the frame pointer for the calling frame.
1950 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1951
1952 // Skip the arguments adaptor frame if it exists.
1953 Label check_frame_marker;
1954 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
1955 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1956 __ b(ne, &check_frame_marker);
1957 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
1958
1959 // Check the marker in the calling frame.
1960 __ bind(&check_frame_marker);
1961 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
1962 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001963 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001964
1965 Apply(context_, if_true, if_false);
1966}
1967
1968
1969void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
1970 ASSERT(args->length() == 2);
1971
1972 // Load the two objects into registers and perform the comparison.
1973 VisitForValue(args->at(0), kStack);
1974 VisitForValue(args->at(1), kAccumulator);
1975
1976 Label materialize_true, materialize_false;
1977 Label* if_true = NULL;
1978 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001979 Label* fall_through = NULL;
1980 PrepareTest(&materialize_true, &materialize_false,
1981 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001982
1983 __ pop(r1);
1984 __ cmp(r0, r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001985 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001986
1987 Apply(context_, if_true, if_false);
1988}
1989
1990
1991void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
1992 ASSERT(args->length() == 1);
1993
1994 // ArgumentsAccessStub expects the key in edx and the formal
1995 // parameter count in eax.
1996 VisitForValue(args->at(0), kAccumulator);
1997 __ mov(r1, r0);
1998 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
1999 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2000 __ CallStub(&stub);
2001 Apply(context_, r0);
2002}
2003
2004
2005void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
2006 ASSERT(args->length() == 0);
2007
2008 Label exit;
2009 // Get the number of formal parameters.
2010 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2011
2012 // Check if the calling frame is an arguments adaptor frame.
2013 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2014 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
2015 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2016 __ b(ne, &exit);
2017
2018 // Arguments adaptor case: Read the arguments length from the
2019 // adaptor frame.
2020 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2021
2022 __ bind(&exit);
2023 Apply(context_, r0);
2024}
2025
2026
2027void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
2028 ASSERT(args->length() == 1);
2029 Label done, null, function, non_function_constructor;
2030
2031 VisitForValue(args->at(0), kAccumulator);
2032
2033 // If the object is a smi, we return null.
2034 __ BranchOnSmi(r0, &null);
2035
2036 // Check that the object is a JS object but take special care of JS
2037 // functions to make sure they have 'Function' as their class.
2038 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2039 __ b(lt, &null);
2040
2041 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2042 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2043 // LAST_JS_OBJECT_TYPE.
2044 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2045 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2046 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2047 __ b(eq, &function);
2048
2049 // Check if the constructor in the map is a function.
2050 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2051 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2052 __ b(ne, &non_function_constructor);
2053
2054 // r0 now contains the constructor function. Grab the
2055 // instance class name from there.
2056 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2057 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2058 __ b(&done);
2059
2060 // Functions have class 'Function'.
2061 __ bind(&function);
2062 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2063 __ jmp(&done);
2064
2065 // Objects with a non-function constructor have class 'Object'.
2066 __ bind(&non_function_constructor);
2067 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2068 __ jmp(&done);
2069
2070 // Non-JS objects have class null.
2071 __ bind(&null);
2072 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2073
2074 // All done.
2075 __ bind(&done);
2076
2077 Apply(context_, r0);
2078}
2079
2080
2081void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2082 // Conditionally generate a log call.
2083 // Args:
2084 // 0 (literal string): The type of logging (corresponds to the flags).
2085 // This is used to determine whether or not to generate the log call.
2086 // 1 (string): Format string. Access the string at argument index 2
2087 // with '%2s' (see Logger::LogRuntime for all the formats).
2088 // 2 (array): Arguments to the format string.
2089 ASSERT_EQ(args->length(), 3);
2090#ifdef ENABLE_LOGGING_AND_PROFILING
2091 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2092 VisitForValue(args->at(1), kStack);
2093 VisitForValue(args->at(2), kStack);
2094 __ CallRuntime(Runtime::kLog, 2);
2095 }
2096#endif
2097 // Finally, we're expected to leave a value on the top of the stack.
2098 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2099 Apply(context_, r0);
2100}
2101
2102
2103void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2104 ASSERT(args->length() == 0);
2105
2106 Label slow_allocate_heapnumber;
2107 Label heapnumber_allocated;
2108
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002109 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
2110 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002111 __ jmp(&heapnumber_allocated);
2112
2113 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002114 // Allocate a heap number.
2115 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002116 __ mov(r4, Operand(r0));
2117
2118 __ bind(&heapnumber_allocated);
2119
2120 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
2121 // by computing:
2122 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2123 if (CpuFeatures::IsSupported(VFP3)) {
2124 __ PrepareCallCFunction(0, r1);
2125 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
2126
2127 CpuFeatures::Scope scope(VFP3);
2128 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2129 // Create this constant using mov/orr to avoid PC relative load.
2130 __ mov(r1, Operand(0x41000000));
2131 __ orr(r1, r1, Operand(0x300000));
2132 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
2133 __ vmov(d7, r0, r1);
2134 // Move 0x4130000000000000 to VFP.
2135 __ mov(r0, Operand(0));
2136 __ vmov(d8, r0, r1);
2137 // Subtract and store the result in the heap number.
2138 __ vsub(d7, d7, d8);
2139 __ sub(r0, r4, Operand(kHeapObjectTag));
2140 __ vstr(d7, r0, HeapNumber::kValueOffset);
2141 __ mov(r0, r4);
2142 } else {
2143 __ mov(r0, Operand(r4));
2144 __ PrepareCallCFunction(1, r1);
2145 __ CallCFunction(
2146 ExternalReference::fill_heap_number_with_random_function(), 1);
2147 }
2148
2149 Apply(context_, r0);
2150}
2151
2152
2153void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
2154 // Load the arguments on the stack and call the stub.
2155 SubStringStub stub;
2156 ASSERT(args->length() == 3);
2157 VisitForValue(args->at(0), kStack);
2158 VisitForValue(args->at(1), kStack);
2159 VisitForValue(args->at(2), kStack);
2160 __ CallStub(&stub);
2161 Apply(context_, r0);
2162}
2163
2164
2165void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
2166 // Load the arguments on the stack and call the stub.
2167 RegExpExecStub stub;
2168 ASSERT(args->length() == 4);
2169 VisitForValue(args->at(0), kStack);
2170 VisitForValue(args->at(1), kStack);
2171 VisitForValue(args->at(2), kStack);
2172 VisitForValue(args->at(3), kStack);
2173 __ CallStub(&stub);
2174 Apply(context_, r0);
2175}
2176
2177
2178void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
2179 ASSERT(args->length() == 1);
2180
2181 VisitForValue(args->at(0), kAccumulator); // Load the object.
2182
2183 Label done;
2184 // If the object is a smi return the object.
2185 __ BranchOnSmi(r0, &done);
2186 // If the object is not a value type, return the object.
2187 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
2188 __ b(ne, &done);
2189 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2190
2191 __ bind(&done);
2192 Apply(context_, r0);
2193}
2194
2195
2196void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
2197 // Load the arguments on the stack and call the runtime function.
2198 ASSERT(args->length() == 2);
2199 VisitForValue(args->at(0), kStack);
2200 VisitForValue(args->at(1), kStack);
2201 __ CallRuntime(Runtime::kMath_pow, 2);
2202 Apply(context_, r0);
2203}
2204
2205
2206void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
2207 ASSERT(args->length() == 2);
2208
2209 VisitForValue(args->at(0), kStack); // Load the object.
2210 VisitForValue(args->at(1), kAccumulator); // Load the value.
2211 __ pop(r1); // r0 = value. r1 = object.
2212
2213 Label done;
2214 // If the object is a smi, return the value.
2215 __ BranchOnSmi(r1, &done);
2216
2217 // If the object is not a value type, return the value.
2218 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
2219 __ b(ne, &done);
2220
2221 // Store the value.
2222 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2223 // Update the write barrier. Save the value as it will be
2224 // overwritten by the write barrier code and is needed afterward.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002225 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002226
2227 __ bind(&done);
2228 Apply(context_, r0);
2229}
2230
2231
2232void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2233 ASSERT_EQ(args->length(), 1);
2234
2235 // Load the argument on the stack and call the stub.
2236 VisitForValue(args->at(0), kStack);
2237
2238 NumberToStringStub stub;
2239 __ CallStub(&stub);
2240 Apply(context_, r0);
2241}
2242
2243
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002244void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002245 ASSERT(args->length() == 1);
2246
2247 VisitForValue(args->at(0), kAccumulator);
2248
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002249 Label done;
2250 StringCharFromCodeGenerator generator(r0, r1);
2251 generator.GenerateFast(masm_);
2252 __ jmp(&done);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002253
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002254 NopRuntimeCallHelper call_helper;
2255 generator.GenerateSlow(masm_, call_helper);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002256
2257 __ bind(&done);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002258 Apply(context_, r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002259}
2260
2261
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002262void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
2263 ASSERT(args->length() == 2);
2264
2265 VisitForValue(args->at(0), kStack);
2266 VisitForValue(args->at(1), kAccumulator);
2267
2268 Register object = r1;
2269 Register index = r0;
2270 Register scratch = r2;
2271 Register result = r3;
2272
2273 __ pop(object);
2274
2275 Label need_conversion;
2276 Label index_out_of_range;
2277 Label done;
2278 StringCharCodeAtGenerator generator(object,
2279 index,
2280 scratch,
2281 result,
2282 &need_conversion,
2283 &need_conversion,
2284 &index_out_of_range,
2285 STRING_INDEX_IS_NUMBER);
2286 generator.GenerateFast(masm_);
2287 __ jmp(&done);
2288
2289 __ bind(&index_out_of_range);
2290 // When the index is out of range, the spec requires us to return
2291 // NaN.
2292 __ LoadRoot(result, Heap::kNanValueRootIndex);
2293 __ jmp(&done);
2294
2295 __ bind(&need_conversion);
2296 // Load the undefined value into the result register, which will
2297 // trigger conversion.
2298 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2299 __ jmp(&done);
2300
2301 NopRuntimeCallHelper call_helper;
2302 generator.GenerateSlow(masm_, call_helper);
2303
2304 __ bind(&done);
2305 Apply(context_, result);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002306}
2307
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002308
2309void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
2310 ASSERT(args->length() == 2);
2311
2312 VisitForValue(args->at(0), kStack);
2313 VisitForValue(args->at(1), kAccumulator);
2314
2315 Register object = r1;
2316 Register index = r0;
2317 Register scratch1 = r2;
2318 Register scratch2 = r3;
2319 Register result = r0;
2320
2321 __ pop(object);
2322
2323 Label need_conversion;
2324 Label index_out_of_range;
2325 Label done;
2326 StringCharAtGenerator generator(object,
2327 index,
2328 scratch1,
2329 scratch2,
2330 result,
2331 &need_conversion,
2332 &need_conversion,
2333 &index_out_of_range,
2334 STRING_INDEX_IS_NUMBER);
2335 generator.GenerateFast(masm_);
2336 __ jmp(&done);
2337
2338 __ bind(&index_out_of_range);
2339 // When the index is out of range, the spec requires us to return
2340 // the empty string.
2341 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2342 __ jmp(&done);
2343
2344 __ bind(&need_conversion);
2345 // Move smi zero into the result register, which will trigger
2346 // conversion.
2347 __ mov(result, Operand(Smi::FromInt(0)));
2348 __ jmp(&done);
2349
2350 NopRuntimeCallHelper call_helper;
2351 generator.GenerateSlow(masm_, call_helper);
2352
2353 __ bind(&done);
2354 Apply(context_, result);
2355}
2356
2357
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002358void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2359 ASSERT_EQ(2, args->length());
2360
2361 VisitForValue(args->at(0), kStack);
2362 VisitForValue(args->at(1), kStack);
2363
2364 StringAddStub stub(NO_STRING_ADD_FLAGS);
2365 __ CallStub(&stub);
2366 Apply(context_, r0);
2367}
2368
2369
2370void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
2371 ASSERT_EQ(2, args->length());
2372
2373 VisitForValue(args->at(0), kStack);
2374 VisitForValue(args->at(1), kStack);
2375
2376 StringCompareStub stub;
2377 __ CallStub(&stub);
2378 Apply(context_, r0);
2379}
2380
2381
2382void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
2383 // Load the argument on the stack and call the runtime.
2384 ASSERT(args->length() == 1);
2385 VisitForValue(args->at(0), kStack);
2386 __ CallRuntime(Runtime::kMath_sin, 1);
2387 Apply(context_, r0);
2388}
2389
2390
2391void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
2392 // Load the argument on the stack and call the runtime.
2393 ASSERT(args->length() == 1);
2394 VisitForValue(args->at(0), kStack);
2395 __ CallRuntime(Runtime::kMath_cos, 1);
2396 Apply(context_, r0);
2397}
2398
2399
2400void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2401 // Load the argument on the stack and call the runtime function.
2402 ASSERT(args->length() == 1);
2403 VisitForValue(args->at(0), kStack);
2404 __ CallRuntime(Runtime::kMath_sqrt, 1);
2405 Apply(context_, r0);
2406}
2407
2408
2409void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2410 ASSERT(args->length() >= 2);
2411
2412 int arg_count = args->length() - 2; // For receiver and function.
2413 VisitForValue(args->at(0), kStack); // Receiver.
2414 for (int i = 0; i < arg_count; i++) {
2415 VisitForValue(args->at(i + 1), kStack);
2416 }
2417 VisitForValue(args->at(arg_count + 1), kAccumulator); // Function.
2418
2419 // InvokeFunction requires function in r1. Move it in there.
2420 if (!result_register().is(r1)) __ mov(r1, result_register());
2421 ParameterCount count(arg_count);
2422 __ InvokeFunction(r1, count, CALL_FUNCTION);
2423 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2424 Apply(context_, r0);
2425}
2426
2427
2428void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2429 ASSERT(args->length() == 3);
2430 VisitForValue(args->at(0), kStack);
2431 VisitForValue(args->at(1), kStack);
2432 VisitForValue(args->at(2), kStack);
2433 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
2434 Apply(context_, r0);
2435}
2436
2437
2438void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2439 ASSERT(args->length() == 3);
2440 VisitForValue(args->at(0), kStack);
2441 VisitForValue(args->at(1), kStack);
2442 VisitForValue(args->at(2), kStack);
2443 __ CallRuntime(Runtime::kSwapElements, 3);
2444 Apply(context_, r0);
2445}
2446
2447
2448void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2449 ASSERT_EQ(2, args->length());
2450
2451 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2452 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2453
2454 Handle<FixedArray> jsfunction_result_caches(
2455 Top::global_context()->jsfunction_result_caches());
2456 if (jsfunction_result_caches->length() <= cache_id) {
2457 __ Abort("Attempt to use undefined cache.");
2458 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2459 Apply(context_, r0);
2460 return;
2461 }
2462
2463 VisitForValue(args->at(1), kAccumulator);
2464
2465 Register key = r0;
2466 Register cache = r1;
2467 __ ldr(cache, CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX));
2468 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
2469 __ ldr(cache,
2470 CodeGenerator::ContextOperand(
2471 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
2472 __ ldr(cache,
2473 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2474
2475
2476 Label done, not_found;
2477 // tmp now holds finger offset as a smi.
2478 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2479 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2480 // r2 now holds finger offset as a smi.
2481 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2482 // r3 now points to the start of fixed array elements.
2483 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2484 // Note side effect of PreIndex: r3 now points to the key of the pair.
2485 __ cmp(key, r2);
2486 __ b(ne, &not_found);
2487
2488 __ ldr(r0, MemOperand(r3, kPointerSize));
2489 __ b(&done);
2490
2491 __ bind(&not_found);
2492 // Call runtime to perform the lookup.
2493 __ Push(cache, key);
2494 __ CallRuntime(Runtime::kGetFromCache, 2);
2495
2496 __ bind(&done);
2497 Apply(context_, r0);
2498}
2499
2500
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002501void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
2502 ASSERT_EQ(2, args->length());
2503
2504 Register right = r0;
2505 Register left = r1;
2506 Register tmp = r2;
2507 Register tmp2 = r3;
2508
2509 VisitForValue(args->at(0), kStack);
2510 VisitForValue(args->at(1), kAccumulator);
2511 __ pop(left);
2512
2513 Label done, fail, ok;
2514 __ cmp(left, Operand(right));
2515 __ b(eq, &ok);
2516 // Fail if either is a non-HeapObject.
2517 __ and_(tmp, left, Operand(right));
2518 __ tst(tmp, Operand(kSmiTagMask));
2519 __ b(eq, &fail);
2520 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
2521 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
2522 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
2523 __ b(ne, &fail);
2524 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
2525 __ cmp(tmp, Operand(tmp2));
2526 __ b(ne, &fail);
2527 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
2528 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
2529 __ cmp(tmp, tmp2);
2530 __ b(eq, &ok);
2531 __ bind(&fail);
2532 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
2533 __ jmp(&done);
2534 __ bind(&ok);
2535 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
2536 __ bind(&done);
2537
2538 Apply(context_, r0);
2539}
2540
2541
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002542void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
2543 VisitForValue(args->at(0), kAccumulator);
2544
2545 Label materialize_true, materialize_false;
2546 Label* if_true = NULL;
2547 Label* if_false = NULL;
2548 Label* fall_through = NULL;
2549 PrepareTest(&materialize_true, &materialize_false,
2550 &if_true, &if_false, &fall_through);
2551
2552 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2553 __ tst(r0, Operand(String::kContainsCachedArrayIndexMask));
2554
2555 __ b(eq, if_true);
2556 __ b(if_false);
2557
2558 Apply(context_, if_true, if_false);
2559}
2560
2561
2562void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
2563 ASSERT(args->length() == 1);
2564 VisitForValue(args->at(0), kAccumulator);
2565 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2566 __ IndexFromHash(r0, r0);
2567 Apply(context_, r0);
2568}
2569
2570
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002571void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002572 Handle<String> name = expr->name();
2573 if (name->length() > 0 && name->Get(0) == '_') {
2574 Comment cmnt(masm_, "[ InlineRuntimeCall");
2575 EmitInlineRuntimeCall(expr);
2576 return;
2577 }
2578
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002579 Comment cmnt(masm_, "[ CallRuntime");
2580 ZoneList<Expression*>* args = expr->arguments();
2581
2582 if (expr->is_jsruntime()) {
2583 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002584 __ ldr(r0, CodeGenerator::GlobalObject());
2585 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002586 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002587 }
2588
2589 // Push the arguments ("left-to-right").
2590 int arg_count = args->length();
2591 for (int i = 0; i < arg_count; i++) {
2592 VisitForValue(args->at(i), kStack);
2593 }
2594
2595 if (expr->is_jsruntime()) {
2596 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002597 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002598 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2599 NOT_IN_LOOP);
2600 __ Call(ic, RelocInfo::CODE_TARGET);
2601 // Restore context register.
2602 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002603 } else {
2604 // Call the C runtime function.
2605 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002606 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002607 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002608}
2609
2610
2611void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2612 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002613 case Token::DELETE: {
2614 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2615 Property* prop = expr->expression()->AsProperty();
2616 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2617 if (prop == NULL && var == NULL) {
2618 // Result of deleting non-property, non-variable reference is true.
2619 // The subexpression may have side effects.
2620 VisitForEffect(expr->expression());
2621 Apply(context_, true);
2622 } else if (var != NULL &&
2623 !var->is_global() &&
2624 var->slot() != NULL &&
2625 var->slot()->type() != Slot::LOOKUP) {
2626 // Result of deleting non-global, non-dynamic variables is false.
2627 // The subexpression does not have side effects.
2628 Apply(context_, false);
2629 } else {
2630 // Property or variable reference. Call the delete builtin with
2631 // object and property name as arguments.
2632 if (prop != NULL) {
2633 VisitForValue(prop->obj(), kStack);
2634 VisitForValue(prop->key(), kStack);
2635 } else if (var->is_global()) {
2636 __ ldr(r1, CodeGenerator::GlobalObject());
2637 __ mov(r0, Operand(var->name()));
2638 __ Push(r1, r0);
2639 } else {
2640 // Non-global variable. Call the runtime to look up the context
2641 // where the variable was introduced.
2642 __ push(context_register());
2643 __ mov(r2, Operand(var->name()));
2644 __ push(r2);
2645 __ CallRuntime(Runtime::kLookupContext, 2);
2646 __ push(r0);
2647 __ mov(r2, Operand(var->name()));
2648 __ push(r2);
2649 }
2650 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
2651 Apply(context_, r0);
2652 }
2653 break;
2654 }
2655
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002656 case Token::VOID: {
2657 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2658 VisitForEffect(expr->expression());
2659 switch (context_) {
2660 case Expression::kUninitialized:
2661 UNREACHABLE();
2662 break;
2663 case Expression::kEffect:
2664 break;
2665 case Expression::kValue:
2666 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2667 switch (location_) {
2668 case kAccumulator:
2669 break;
2670 case kStack:
2671 __ push(result_register());
2672 break;
2673 }
2674 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002675 case Expression::kTest:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002676 __ jmp(false_label_);
2677 break;
2678 }
2679 break;
2680 }
2681
2682 case Token::NOT: {
2683 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002684 Label materialize_true, materialize_false;
2685 Label* if_true = NULL;
2686 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002687 Label* fall_through = NULL;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002688
2689 // Notice that the labels are swapped.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002690 PrepareTest(&materialize_true, &materialize_false,
2691 &if_false, &if_true, &fall_through);
2692 VisitForControl(expr->expression(), if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002693 Apply(context_, if_false, if_true); // Labels swapped.
2694 break;
2695 }
2696
2697 case Token::TYPEOF: {
2698 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002699 VisitForTypeofValue(expr->expression(), kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002700 __ CallRuntime(Runtime::kTypeof, 1);
2701 Apply(context_, r0);
2702 break;
2703 }
2704
2705 case Token::ADD: {
2706 Comment cmt(masm_, "[ UnaryOperation (ADD)");
2707 VisitForValue(expr->expression(), kAccumulator);
2708 Label no_conversion;
2709 __ tst(result_register(), Operand(kSmiTagMask));
2710 __ b(eq, &no_conversion);
2711 __ push(r0);
2712 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2713 __ bind(&no_conversion);
2714 Apply(context_, result_register());
2715 break;
2716 }
2717
2718 case Token::SUB: {
2719 Comment cmt(masm_, "[ UnaryOperation (SUB)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002720 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002721 UnaryOverwriteMode overwrite =
2722 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002723 GenericUnaryOpStub stub(Token::SUB, overwrite);
2724 // GenericUnaryOpStub expects the argument to be in the
2725 // accumulator register r0.
2726 VisitForValue(expr->expression(), kAccumulator);
2727 __ CallStub(&stub);
2728 Apply(context_, r0);
2729 break;
2730 }
2731
2732 case Token::BIT_NOT: {
2733 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002734 // The generic unary operation stub expects the argument to be
2735 // in the accumulator register r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002736 VisitForValue(expr->expression(), kAccumulator);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002737 Label done;
2738 if (ShouldInlineSmiCase(expr->op())) {
2739 Label call_stub;
2740 __ BranchOnNotSmi(r0, &call_stub);
2741 __ mvn(r0, Operand(r0));
2742 // Bit-clear inverted smi-tag.
2743 __ bic(r0, r0, Operand(kSmiTagMask));
2744 __ b(&done);
2745 __ bind(&call_stub);
2746 }
2747 bool overwrite = expr->expression()->ResultOverwriteAllowed();
2748 UnaryOverwriteMode mode =
2749 overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
2750 GenericUnaryOpStub stub(Token::BIT_NOT, mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002751 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002752 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002753 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002754 break;
2755 }
2756
2757 default:
2758 UNREACHABLE();
2759 }
2760}
2761
2762
2763void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2764 Comment cmnt(masm_, "[ CountOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002765 SetSourcePosition(expr->position());
2766
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002767 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2768 // as the left-hand side.
2769 if (!expr->expression()->IsValidLeftHandSide()) {
2770 VisitForEffect(expr->expression());
2771 return;
2772 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002773
2774 // Expression can only be a property, a global or a (parameter or local)
2775 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2776 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2777 LhsKind assign_type = VARIABLE;
2778 Property* prop = expr->expression()->AsProperty();
2779 // In case of a property we use the uninitialized expression context
2780 // of the key to detect a named property.
2781 if (prop != NULL) {
2782 assign_type =
2783 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2784 }
2785
2786 // Evaluate expression and get value.
2787 if (assign_type == VARIABLE) {
2788 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
2789 Location saved_location = location_;
2790 location_ = kAccumulator;
2791 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
2792 Expression::kValue);
2793 location_ = saved_location;
2794 } else {
2795 // Reserve space for result of postfix operation.
2796 if (expr->is_postfix() && context_ != Expression::kEffect) {
2797 __ mov(ip, Operand(Smi::FromInt(0)));
2798 __ push(ip);
2799 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002800 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002801 // Put the object both on the stack and in the accumulator.
2802 VisitForValue(prop->obj(), kAccumulator);
2803 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002804 EmitNamedPropertyLoad(prop);
2805 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002806 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002807 VisitForValue(prop->key(), kAccumulator);
2808 __ ldr(r1, MemOperand(sp, 0));
2809 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002810 EmitKeyedPropertyLoad(prop);
2811 }
2812 }
2813
2814 // Call ToNumber only if operand is not a smi.
2815 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002816 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002817 __ push(r0);
2818 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2819 __ bind(&no_conversion);
2820
2821 // Save result for postfix expressions.
2822 if (expr->is_postfix()) {
2823 switch (context_) {
2824 case Expression::kUninitialized:
2825 UNREACHABLE();
2826 case Expression::kEffect:
2827 // Do not save result.
2828 break;
2829 case Expression::kValue:
2830 case Expression::kTest:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002831 // Save the result on the stack. If we have a named or keyed property
2832 // we store the result under the receiver that is currently on top
2833 // of the stack.
2834 switch (assign_type) {
2835 case VARIABLE:
2836 __ push(r0);
2837 break;
2838 case NAMED_PROPERTY:
2839 __ str(r0, MemOperand(sp, kPointerSize));
2840 break;
2841 case KEYED_PROPERTY:
2842 __ str(r0, MemOperand(sp, 2 * kPointerSize));
2843 break;
2844 }
2845 break;
2846 }
2847 }
2848
2849
2850 // Inline smi case if we are in a loop.
2851 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002852 int count_value = expr->op() == Token::INC ? 1 : -1;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002853 if (ShouldInlineSmiCase(expr->op())) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002854 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002855 __ b(vs, &stub_call);
2856 // We could eliminate this smi check if we split the code at
2857 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002858 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002859 __ bind(&stub_call);
2860 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002861 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002862 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002863 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00002864 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002865 __ CallStub(&stub);
2866 __ bind(&done);
2867
2868 // Store the value returned in r0.
2869 switch (assign_type) {
2870 case VARIABLE:
2871 if (expr->is_postfix()) {
2872 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002873 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002874 Expression::kEffect);
2875 // For all contexts except kEffect: We have the result on
2876 // top of the stack.
2877 if (context_ != Expression::kEffect) {
2878 ApplyTOS(context_);
2879 }
2880 } else {
2881 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002882 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002883 context_);
2884 }
2885 break;
2886 case NAMED_PROPERTY: {
2887 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00002888 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002889 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2890 __ Call(ic, RelocInfo::CODE_TARGET);
2891 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002892 if (context_ != Expression::kEffect) {
2893 ApplyTOS(context_);
2894 }
2895 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00002896 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002897 }
2898 break;
2899 }
2900 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002901 __ pop(r1); // Key.
2902 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002903 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
2904 __ Call(ic, RelocInfo::CODE_TARGET);
2905 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002906 if (context_ != Expression::kEffect) {
2907 ApplyTOS(context_);
2908 }
2909 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002910 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002911 }
2912 break;
2913 }
2914 }
2915}
2916
2917
ricow@chromium.org65fae842010-08-25 15:26:24 +00002918void FullCodeGenerator::VisitForTypeofValue(Expression* expr, Location where) {
2919 VariableProxy* proxy = expr->AsVariableProxy();
2920 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
2921 Comment cmnt(masm_, "Global variable");
2922 __ ldr(r0, CodeGenerator::GlobalObject());
2923 __ mov(r2, Operand(proxy->name()));
2924 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2925 // Use a regular load, not a contextual load, to avoid a reference
2926 // error.
2927 __ Call(ic, RelocInfo::CODE_TARGET);
2928 if (where == kStack) __ push(r0);
2929 } else if (proxy != NULL &&
2930 proxy->var()->slot() != NULL &&
2931 proxy->var()->slot()->type() == Slot::LOOKUP) {
2932 __ mov(r0, Operand(proxy->name()));
2933 __ Push(cp, r0);
2934 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
2935 if (where == kStack) __ push(r0);
2936 } else {
2937 // This expression cannot throw a reference error at the top level.
2938 VisitForValue(expr, where);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002939 }
2940}
2941
2942
ricow@chromium.org65fae842010-08-25 15:26:24 +00002943bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
2944 Expression* left,
2945 Expression* right,
2946 Label* if_true,
2947 Label* if_false,
2948 Label* fall_through) {
2949 if (op != Token::EQ && op != Token::EQ_STRICT) return false;
2950
2951 // Check for the pattern: typeof <expression> == <string literal>.
2952 Literal* right_literal = right->AsLiteral();
2953 if (right_literal == NULL) return false;
2954 Handle<Object> right_literal_value = right_literal->handle();
2955 if (!right_literal_value->IsString()) return false;
2956 UnaryOperation* left_unary = left->AsUnaryOperation();
2957 if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
2958 Handle<String> check = Handle<String>::cast(right_literal_value);
2959
2960 VisitForTypeofValue(left_unary->expression(), kAccumulator);
2961 if (check->Equals(Heap::number_symbol())) {
2962 __ tst(r0, Operand(kSmiTagMask));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002963 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002964 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2965 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
2966 __ cmp(r0, ip);
2967 Split(eq, if_true, if_false, fall_through);
2968 } else if (check->Equals(Heap::string_symbol())) {
2969 __ tst(r0, Operand(kSmiTagMask));
2970 __ b(eq, if_false);
2971 // Check for undetectable objects => false.
2972 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2973 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
2974 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
2975 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
2976 __ b(eq, if_false);
2977 __ ldrb(r1, FieldMemOperand(r0, Map::kInstanceTypeOffset));
2978 __ cmp(r1, Operand(FIRST_NONSTRING_TYPE));
2979 Split(lt, if_true, if_false, fall_through);
2980 } else if (check->Equals(Heap::boolean_symbol())) {
2981 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
2982 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002983 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002984 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
2985 __ cmp(r0, ip);
2986 Split(eq, if_true, if_false, fall_through);
2987 } else if (check->Equals(Heap::undefined_symbol())) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002988 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002989 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002990 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002991 __ tst(r0, Operand(kSmiTagMask));
2992 __ b(eq, if_false);
2993 // Check for undetectable objects => true.
2994 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2995 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
2996 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
2997 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
2998 Split(eq, if_true, if_false, fall_through);
2999 } else if (check->Equals(Heap::function_symbol())) {
3000 __ tst(r0, Operand(kSmiTagMask));
3001 __ b(eq, if_false);
3002 __ CompareObjectType(r0, r1, r0, JS_FUNCTION_TYPE);
3003 __ b(eq, if_true);
3004 // Regular expressions => 'function' (they are callable).
3005 __ CompareInstanceType(r1, r0, JS_REGEXP_TYPE);
3006 Split(eq, if_true, if_false, fall_through);
3007 } else if (check->Equals(Heap::object_symbol())) {
3008 __ tst(r0, Operand(kSmiTagMask));
3009 __ b(eq, if_false);
3010 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3011 __ cmp(r0, ip);
3012 __ b(eq, if_true);
3013 // Regular expressions => 'function', not 'object'.
3014 __ CompareObjectType(r0, r1, r0, JS_REGEXP_TYPE);
3015 __ b(eq, if_false);
3016 // Check for undetectable objects => false.
3017 __ ldrb(r0, FieldMemOperand(r1, Map::kBitFieldOffset));
3018 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3019 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
3020 __ b(eq, if_false);
3021 // Check for JS objects => true.
3022 __ ldrb(r0, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3023 __ cmp(r0, Operand(FIRST_JS_OBJECT_TYPE));
3024 __ b(lt, if_false);
3025 __ cmp(r0, Operand(LAST_JS_OBJECT_TYPE));
3026 Split(le, if_true, if_false, fall_through);
3027 } else {
3028 if (if_false != fall_through) __ jmp(if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003029 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003030
3031 return true;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003032}
3033
3034
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003035void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
3036 Comment cmnt(masm_, "[ CompareOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00003037 SetSourcePosition(expr->position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003038
3039 // Always perform the comparison for its control flow. Pack the result
3040 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003041
3042 Label materialize_true, materialize_false;
3043 Label* if_true = NULL;
3044 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003045 Label* fall_through = NULL;
3046 PrepareTest(&materialize_true, &materialize_false,
3047 &if_true, &if_false, &fall_through);
3048
3049 // First we try a fast inlined version of the compare when one of
3050 // the operands is a literal.
3051 Token::Value op = expr->op();
3052 Expression* left = expr->left();
3053 Expression* right = expr->right();
3054 if (TryLiteralCompare(op, left, right, if_true, if_false, fall_through)) {
3055 Apply(context_, if_true, if_false);
3056 return;
3057 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003058
3059 VisitForValue(expr->left(), kStack);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003060 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003061 case Token::IN:
3062 VisitForValue(expr->right(), kStack);
3063 __ InvokeBuiltin(Builtins::IN, CALL_JS);
3064 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3065 __ cmp(r0, ip);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003066 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003067 break;
3068
3069 case Token::INSTANCEOF: {
3070 VisitForValue(expr->right(), kStack);
3071 InstanceofStub stub;
3072 __ CallStub(&stub);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003073 // The stub returns 0 for true.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003074 __ tst(r0, r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003075 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003076 break;
3077 }
3078
3079 default: {
3080 VisitForValue(expr->right(), kAccumulator);
3081 Condition cc = eq;
3082 bool strict = false;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003083 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003084 case Token::EQ_STRICT:
3085 strict = true;
3086 // Fall through
ricow@chromium.org65fae842010-08-25 15:26:24 +00003087 case Token::EQ:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003088 cc = eq;
3089 __ pop(r1);
3090 break;
3091 case Token::LT:
3092 cc = lt;
3093 __ pop(r1);
3094 break;
3095 case Token::GT:
3096 // Reverse left and right sides to obtain ECMA-262 conversion order.
3097 cc = lt;
3098 __ mov(r1, result_register());
3099 __ pop(r0);
3100 break;
3101 case Token::LTE:
3102 // Reverse left and right sides to obtain ECMA-262 conversion order.
3103 cc = ge;
3104 __ mov(r1, result_register());
3105 __ pop(r0);
3106 break;
3107 case Token::GTE:
3108 cc = ge;
3109 __ pop(r1);
3110 break;
3111 case Token::IN:
3112 case Token::INSTANCEOF:
3113 default:
3114 UNREACHABLE();
3115 }
3116
ricow@chromium.org65fae842010-08-25 15:26:24 +00003117 if (ShouldInlineSmiCase(op)) {
3118 Label slow_case;
3119 __ orr(r2, r0, Operand(r1));
3120 __ BranchOnNotSmi(r2, &slow_case);
3121 __ cmp(r1, r0);
3122 Split(cc, if_true, if_false, NULL);
3123 __ bind(&slow_case);
3124 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003125
ager@chromium.orgb5737492010-07-15 09:29:43 +00003126 CompareStub stub(cc, strict, kBothCouldBeNaN, true, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003127 __ CallStub(&stub);
3128 __ cmp(r0, Operand(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003129 Split(cc, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003130 }
3131 }
3132
3133 // Convert the result of the comparison into one expected for this
3134 // expression's context.
3135 Apply(context_, if_true, if_false);
3136}
3137
3138
ricow@chromium.org65fae842010-08-25 15:26:24 +00003139void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
3140 Comment cmnt(masm_, "[ CompareToNull");
3141 Label materialize_true, materialize_false;
3142 Label* if_true = NULL;
3143 Label* if_false = NULL;
3144 Label* fall_through = NULL;
3145 PrepareTest(&materialize_true, &materialize_false,
3146 &if_true, &if_false, &fall_through);
3147
3148 VisitForValue(expr->expression(), kAccumulator);
3149 __ LoadRoot(r1, Heap::kNullValueRootIndex);
3150 __ cmp(r0, r1);
3151 if (expr->is_strict()) {
3152 Split(eq, if_true, if_false, fall_through);
3153 } else {
3154 __ b(eq, if_true);
3155 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
3156 __ cmp(r0, r1);
3157 __ b(eq, if_true);
3158 __ tst(r0, Operand(kSmiTagMask));
3159 __ b(eq, if_false);
3160 // It can be an undetectable object.
3161 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3162 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3163 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3164 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3165 Split(eq, if_true, if_false, fall_through);
3166 }
3167 Apply(context_, if_true, if_false);
3168}
3169
3170
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003171void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3172 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3173 Apply(context_, r0);
3174}
3175
3176
3177Register FullCodeGenerator::result_register() { return r0; }
3178
3179
3180Register FullCodeGenerator::context_register() { return cp; }
3181
3182
3183void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3184 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3185 __ str(value, MemOperand(fp, frame_offset));
3186}
3187
3188
3189void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
3190 __ ldr(dst, CodeGenerator::ContextOperand(cp, context_index));
3191}
3192
3193
3194// ----------------------------------------------------------------------------
3195// Non-local control flow support.
3196
3197void FullCodeGenerator::EnterFinallyBlock() {
3198 ASSERT(!result_register().is(r1));
3199 // Store result register while executing finally block.
3200 __ push(result_register());
3201 // Cook return address in link register to stack (smi encoded Code* delta)
3202 __ sub(r1, lr, Operand(masm_->CodeObject()));
3203 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3204 ASSERT_EQ(0, kSmiTag);
3205 __ add(r1, r1, Operand(r1)); // Convert to smi.
3206 __ push(r1);
3207}
3208
3209
3210void FullCodeGenerator::ExitFinallyBlock() {
3211 ASSERT(!result_register().is(r1));
3212 // Restore result register from stack.
3213 __ pop(r1);
3214 // Uncook return address and return.
3215 __ pop(result_register());
3216 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3217 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3218 __ add(pc, r1, Operand(masm_->CodeObject()));
3219}
3220
3221
3222#undef __
3223
3224} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003225
3226#endif // V8_TARGET_ARCH_ARM