blob: f5d121785da42e96b6efa6083f642b52b41ff569 [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);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000496 return ContextOperand(scratch, slot->index());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000497 }
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.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000560 __ ldr(r1, ContextOperand(cp, Context::FCONTEXT_INDEX));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000561 __ cmp(r1, cp);
562 __ Check(eq, "Unexpected declaration in current context.");
563 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000564 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000565 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000566 __ str(ip, ContextOperand(cp, slot->index()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000567 // No write barrier since the_hole_value is in old space.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000568 } else if (function != NULL) {
569 VisitForValue(function, kAccumulator);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000570 __ str(result_register(), ContextOperand(cp, slot->index()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000571 int offset = Context::SlotOffset(slot->index());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000572 // We know that we have written a function, which is not a smi.
573 __ mov(r1, Operand(cp));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000574 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000575 }
576 break;
577
578 case Slot::LOOKUP: {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000579 __ mov(r2, Operand(variable->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000580 // Declaration nodes are always introduced in one of two modes.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000581 ASSERT(mode == Variable::VAR ||
582 mode == Variable::CONST);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000583 PropertyAttributes attr =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000584 (mode == Variable::VAR) ? NONE : READ_ONLY;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000585 __ mov(r1, Operand(Smi::FromInt(attr)));
586 // Push initial value, if any.
587 // Note: For variables we must not push an initial value (such as
588 // 'undefined') because we may have a (legal) redeclaration and we
589 // must not destroy the current value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000590 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000591 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000592 __ Push(cp, r2, r1, r0);
593 } else if (function != NULL) {
594 __ Push(cp, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000595 // Push initial value for function declaration.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000596 VisitForValue(function, kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000597 } else {
598 __ mov(r0, Operand(Smi::FromInt(0))); // No initial value!
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000599 __ Push(cp, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000600 }
601 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
602 break;
603 }
604 }
605
606 } else if (prop != NULL) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000607 if (function != NULL || mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000608 // We are declaring a function or constant that rewrites to a
609 // property. Use (keyed) IC to set the initial value.
610 VisitForValue(prop->obj(), kStack);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000611 if (function != NULL) {
612 VisitForValue(prop->key(), kStack);
613 VisitForValue(function, kAccumulator);
614 __ pop(r1); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000615 } else {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000616 VisitForValue(prop->key(), kAccumulator);
617 __ mov(r1, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000618 __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex);
619 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000620 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000621
622 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000623 EmitCallIC(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000624 // Value in r0 is ignored (declarations are statements).
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000625 }
626 }
627}
628
629
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000630void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
631 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
632}
633
634
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000635void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
636 // Call the runtime to declare the globals.
637 // The context is the first argument.
638 __ mov(r1, Operand(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000639 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000640 __ Push(cp, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000641 __ CallRuntime(Runtime::kDeclareGlobals, 3);
642 // Return value is ignored.
643}
644
645
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000646void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000647 Comment cmnt(masm_, "[ SwitchStatement");
648 Breakable nested_statement(this, stmt);
649 SetStatementPosition(stmt);
650 // Keep the switch value on the stack until a case matches.
651 VisitForValue(stmt->tag(), kStack);
652
653 ZoneList<CaseClause*>* clauses = stmt->cases();
654 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
655
656 Label next_test; // Recycled for each test.
657 // Compile all the tests with branches to their bodies.
658 for (int i = 0; i < clauses->length(); i++) {
659 CaseClause* clause = clauses->at(i);
660 // The default is not a test, but remember it as final fall through.
661 if (clause->is_default()) {
662 default_clause = clause;
663 continue;
664 }
665
666 Comment cmnt(masm_, "[ Case comparison");
667 __ bind(&next_test);
668 next_test.Unuse();
669
670 // Compile the label expression.
671 VisitForValue(clause->label(), kAccumulator);
672
ricow@chromium.org65fae842010-08-25 15:26:24 +0000673 // Perform the comparison as if via '==='.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000674 __ ldr(r1, MemOperand(sp, 0)); // Switch value.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000675 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
676 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000677 Label slow_case;
678 __ orr(r2, r1, r0);
679 __ tst(r2, Operand(kSmiTagMask));
680 __ b(ne, &slow_case);
681 __ cmp(r1, r0);
682 __ b(ne, &next_test);
683 __ Drop(1); // Switch value is no longer needed.
684 __ b(clause->body_target()->entry_label());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000685 __ bind(&slow_case);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000686 }
687
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000688 CompareFlags flags = inline_smi_code
689 ? NO_SMI_COMPARE_IN_STUB
690 : NO_COMPARE_FLAGS;
691 CompareStub stub(eq, true, flags, r1, r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000692 __ CallStub(&stub);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000693 __ cmp(r0, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000694 __ b(ne, &next_test);
695 __ Drop(1); // Switch value is no longer needed.
696 __ b(clause->body_target()->entry_label());
697 }
698
699 // Discard the test value and jump to the default if present, otherwise to
700 // the end of the statement.
701 __ bind(&next_test);
702 __ Drop(1); // Switch value is no longer needed.
703 if (default_clause == NULL) {
704 __ b(nested_statement.break_target());
705 } else {
706 __ b(default_clause->body_target()->entry_label());
707 }
708
709 // Compile all the case bodies.
710 for (int i = 0; i < clauses->length(); i++) {
711 Comment cmnt(masm_, "[ Case body");
712 CaseClause* clause = clauses->at(i);
713 __ bind(clause->body_target()->entry_label());
714 VisitStatements(clause->statements());
715 }
716
717 __ bind(nested_statement.break_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000718}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000719
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000720
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000721void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000722 Comment cmnt(masm_, "[ ForInStatement");
723 SetStatementPosition(stmt);
724
725 Label loop, exit;
726 ForIn loop_statement(this, stmt);
727 increment_loop_depth();
728
729 // Get the object to enumerate over. Both SpiderMonkey and JSC
730 // ignore null and undefined in contrast to the specification; see
731 // ECMA-262 section 12.6.4.
732 VisitForValue(stmt->enumerable(), kAccumulator);
733 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
734 __ cmp(r0, ip);
735 __ b(eq, &exit);
736 __ LoadRoot(ip, Heap::kNullValueRootIndex);
737 __ cmp(r0, ip);
738 __ b(eq, &exit);
739
740 // Convert the object to a JS object.
741 Label convert, done_convert;
742 __ BranchOnSmi(r0, &convert);
743 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
744 __ b(hs, &done_convert);
745 __ bind(&convert);
746 __ push(r0);
747 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
748 __ bind(&done_convert);
749 __ push(r0);
750
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000751 // BUG(867): Check cache validity in generated code. This is a fast
752 // case for the JSObject::IsSimpleEnum cache validity checks. If we
753 // cannot guarantee cache validity, call the runtime system to check
754 // cache validity or get the property names in a fixed array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000755
756 // Get the set of properties to enumerate.
757 __ push(r0); // Duplicate the enumerable object on the stack.
758 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
759
760 // If we got a map from the runtime call, we can do a fast
761 // modification check. Otherwise, we got a fixed array, and we have
762 // to do a slow check.
763 Label fixed_array;
764 __ mov(r2, r0);
765 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
766 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
767 __ cmp(r1, ip);
768 __ b(ne, &fixed_array);
769
770 // We got a map in register r0. Get the enumeration cache from it.
771 __ ldr(r1, FieldMemOperand(r0, Map::kInstanceDescriptorsOffset));
772 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
773 __ ldr(r2, FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
774
775 // Setup the four remaining stack slots.
776 __ push(r0); // Map.
777 __ ldr(r1, FieldMemOperand(r2, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000778 __ mov(r0, Operand(Smi::FromInt(0)));
779 // Push enumeration cache, enumeration cache length (as smi) and zero.
780 __ Push(r2, r1, r0);
781 __ jmp(&loop);
782
783 // We got a fixed array in register r0. Iterate through that.
784 __ bind(&fixed_array);
785 __ mov(r1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
786 __ Push(r1, r0);
787 __ ldr(r1, FieldMemOperand(r0, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000788 __ mov(r0, Operand(Smi::FromInt(0)));
789 __ Push(r1, r0); // Fixed array length (as smi) and initial index.
790
791 // Generate code for doing the condition check.
792 __ bind(&loop);
793 // Load the current count to r0, load the length to r1.
794 __ Ldrd(r0, r1, MemOperand(sp, 0 * kPointerSize));
795 __ cmp(r0, r1); // Compare to the array length.
796 __ b(hs, loop_statement.break_target());
797
798 // Get the current entry of the array into register r3.
799 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
800 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
801 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
802
803 // Get the expected map from the stack or a zero map in the
804 // permanent slow case into register r2.
805 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
806
807 // Check if the expected map still matches that of the enumerable.
808 // If not, we have to filter the key.
809 Label update_each;
810 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
811 __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
812 __ cmp(r4, Operand(r2));
813 __ b(eq, &update_each);
814
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000815 // Convert the entry to a string or (smi) 0 if it isn't a property
816 // any more. If the property has been removed while iterating, we
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000817 // just skip it.
818 __ push(r1); // Enumerable.
819 __ push(r3); // Current entry.
820 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000821 __ mov(r3, Operand(r0), SetCC);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000822 __ b(eq, loop_statement.continue_target());
823
824 // Update the 'each' property or variable from the possibly filtered
825 // entry in register r3.
826 __ bind(&update_each);
827 __ mov(result_register(), r3);
828 // Perform the assignment as if via '='.
829 EmitAssignment(stmt->each());
830
831 // Generate code for the body of the loop.
832 Label stack_limit_hit, stack_check_done;
833 Visit(stmt->body());
834
835 __ StackLimitCheck(&stack_limit_hit);
836 __ bind(&stack_check_done);
837
838 // Generate code for the going to the next element by incrementing
839 // the index (smi) stored on top of the stack.
840 __ bind(loop_statement.continue_target());
841 __ pop(r0);
842 __ add(r0, r0, Operand(Smi::FromInt(1)));
843 __ push(r0);
844 __ b(&loop);
845
846 // Slow case for the stack limit check.
847 StackCheckStub stack_check_stub;
848 __ bind(&stack_limit_hit);
849 __ CallStub(&stack_check_stub);
850 __ b(&stack_check_done);
851
852 // Remove the pointers stored on the stack.
853 __ bind(loop_statement.break_target());
854 __ Drop(5);
855
856 // Exit and decrement the loop depth.
857 __ bind(&exit);
858 decrement_loop_depth();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000859}
860
861
862void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info) {
863 // Use the fast case closure allocation code that allocates in new
864 // space for nested functions that don't need literals cloning.
865 if (scope()->is_function_scope() && info->num_literals() == 0) {
866 FastNewClosureStub stub;
867 __ mov(r0, Operand(info));
868 __ push(r0);
869 __ CallStub(&stub);
870 } else {
871 __ mov(r0, Operand(info));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000872 __ Push(cp, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000873 __ CallRuntime(Runtime::kNewClosure, 2);
874 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000875 Apply(context_, r0);
876}
877
878
879void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
880 Comment cmnt(masm_, "[ VariableProxy");
881 EmitVariableLoad(expr->var(), context_);
882}
883
884
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000885MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(
886 Slot* slot,
887 Label* slow) {
888 ASSERT(slot->type() == Slot::CONTEXT);
889 Register current = cp;
890 Register next = r3;
891 Register temp = r4;
892
893 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
894 if (s->num_heap_slots() > 0) {
895 if (s->calls_eval()) {
896 // Check that extension is NULL.
897 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
898 __ tst(temp, temp);
899 __ b(ne, slow);
900 }
901 __ ldr(next, ContextOperand(current, Context::CLOSURE_INDEX));
902 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
903 // Walk the rest of the chain without clobbering cp.
904 current = next;
905 }
906 }
907 // Check that last extension is NULL.
908 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
909 __ tst(temp, temp);
910 __ b(ne, slow);
911 __ ldr(temp, ContextOperand(current, Context::FCONTEXT_INDEX));
912 return ContextOperand(temp, slot->index());
913}
914
915
916void FullCodeGenerator::EmitDynamicLoadFromSlotFastCase(
917 Slot* slot,
918 TypeofState typeof_state,
919 Label* slow,
920 Label* done) {
921 // Generate fast-case code for variables that might be shadowed by
922 // eval-introduced variables. Eval is used a lot without
923 // introducing variables. In those cases, we do not want to
924 // perform a runtime call for all variables in the scope
925 // containing the eval.
926 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
927 EmitLoadGlobalSlotCheckExtensions(slot, typeof_state, slow);
928 __ jmp(done);
929 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
930 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
931 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
932 if (potential_slot != NULL) {
933 // Generate fast case for locals that rewrite to slots.
934 __ ldr(r0, ContextSlotOperandCheckExtensions(potential_slot, slow));
935 if (potential_slot->var()->mode() == Variable::CONST) {
936 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
937 __ cmp(r0, ip);
938 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
939 }
940 __ jmp(done);
941 } else if (rewrite != NULL) {
942 // Generate fast case for calls of an argument function.
943 Property* property = rewrite->AsProperty();
944 if (property != NULL) {
945 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
946 Literal* key_literal = property->key()->AsLiteral();
947 if (obj_proxy != NULL &&
948 key_literal != NULL &&
949 obj_proxy->IsArguments() &&
950 key_literal->handle()->IsSmi()) {
951 // Load arguments object if there are no eval-introduced
952 // variables. Then load the argument from the arguments
953 // object using keyed load.
954 __ ldr(r1,
955 ContextSlotOperandCheckExtensions(obj_proxy->var()->slot(),
956 slow));
957 __ mov(r0, Operand(key_literal->handle()));
958 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000959 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000960 __ jmp(done);
961 }
962 }
963 }
964 }
965}
966
967
968void FullCodeGenerator::EmitLoadGlobalSlotCheckExtensions(
969 Slot* slot,
970 TypeofState typeof_state,
971 Label* slow) {
972 Register current = cp;
973 Register next = r1;
974 Register temp = r2;
975
976 Scope* s = scope();
977 while (s != NULL) {
978 if (s->num_heap_slots() > 0) {
979 if (s->calls_eval()) {
980 // Check that extension is NULL.
981 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
982 __ tst(temp, temp);
983 __ b(ne, slow);
984 }
985 // Load next context in chain.
986 __ ldr(next, ContextOperand(current, Context::CLOSURE_INDEX));
987 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
988 // Walk the rest of the chain without clobbering cp.
989 current = next;
990 }
991 // If no outer scope calls eval, we do not need to check more
992 // context extensions.
993 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
994 s = s->outer_scope();
995 }
996
997 if (s->is_eval_scope()) {
998 Label loop, fast;
999 if (!current.is(next)) {
1000 __ Move(next, current);
1001 }
1002 __ bind(&loop);
1003 // Terminate at global context.
1004 __ ldr(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1005 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1006 __ cmp(temp, ip);
1007 __ b(eq, &fast);
1008 // Check that extension is NULL.
1009 __ ldr(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1010 __ tst(temp, temp);
1011 __ b(ne, slow);
1012 // Load next context in chain.
1013 __ ldr(next, ContextOperand(next, Context::CLOSURE_INDEX));
1014 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
1015 __ b(&loop);
1016 __ bind(&fast);
1017 }
1018
1019 __ ldr(r0, CodeGenerator::GlobalObject());
1020 __ mov(r2, Operand(slot->var()->name()));
1021 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1022 ? RelocInfo::CODE_TARGET
1023 : RelocInfo::CODE_TARGET_CONTEXT;
1024 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001025 EmitCallIC(ic, mode);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001026}
1027
1028
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001029void FullCodeGenerator::EmitVariableLoad(Variable* var,
1030 Expression::Context context) {
1031 // Four cases: non-this global variables, lookup slots, all other
1032 // types of slots, and parameters that rewrite to explicit property
1033 // accesses on the arguments object.
1034 Slot* slot = var->slot();
1035 Property* property = var->AsProperty();
1036
1037 if (var->is_global() && !var->is_this()) {
1038 Comment cmnt(masm_, "Global variable");
1039 // Use inline caching. Variable name is passed in r2 and the global
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001040 // object (receiver) in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001041 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001042 __ mov(r2, Operand(var->name()));
1043 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001044 EmitCallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001045 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001046
1047 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001048 Label done, slow;
1049
1050 // Generate code for loading from variables potentially shadowed
1051 // by eval-introduced variables.
1052 EmitDynamicLoadFromSlotFastCase(slot, NOT_INSIDE_TYPEOF, &slow, &done);
1053
1054 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001055 Comment cmnt(masm_, "Lookup slot");
1056 __ mov(r1, Operand(var->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001057 __ Push(cp, r1); // Context and name.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001058 __ CallRuntime(Runtime::kLoadContextSlot, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001059 __ bind(&done);
1060
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001061 Apply(context, r0);
1062
1063 } else if (slot != NULL) {
1064 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1065 ? "Context slot"
1066 : "Stack slot");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001067 if (var->mode() == Variable::CONST) {
1068 // Constants may be the hole value if they have not been initialized.
1069 // Unhole them.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001070 MemOperand slot_operand = EmitSlotSearch(slot, r0);
1071 __ ldr(r0, slot_operand);
1072 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1073 __ cmp(r0, ip);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001074 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001075 Apply(context, r0);
1076 } else {
1077 Apply(context, slot);
1078 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001079 } else {
1080 Comment cmnt(masm_, "Rewritten parameter");
1081 ASSERT_NOT_NULL(property);
1082 // Rewritten parameter accesses are of the form "slot[literal]".
1083
1084 // Assert that the object is in a slot.
1085 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
1086 ASSERT_NOT_NULL(object_var);
1087 Slot* object_slot = object_var->slot();
1088 ASSERT_NOT_NULL(object_slot);
1089
1090 // Load the object.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001091 Move(r1, object_slot);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001092
1093 // Assert that the key is a smi.
1094 Literal* key_literal = property->key()->AsLiteral();
1095 ASSERT_NOT_NULL(key_literal);
1096 ASSERT(key_literal->handle()->IsSmi());
1097
1098 // Load the key.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001099 __ mov(r0, Operand(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001100
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001101 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001102 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001103 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001104 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001105 }
1106}
1107
1108
1109void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1110 Comment cmnt(masm_, "[ RegExpLiteral");
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001111 Label materialized;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001112 // Registers will be used as follows:
1113 // r4 = JS function, literals array
1114 // r3 = literal index
1115 // r2 = RegExp pattern
1116 // r1 = RegExp flags
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001117 // r0 = temp + materialized value (RegExp literal)
ricow@chromium.org65fae842010-08-25 15:26:24 +00001118 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1119 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001120 int literal_offset =
ricow@chromium.org65fae842010-08-25 15:26:24 +00001121 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001122 __ ldr(r0, FieldMemOperand(r4, literal_offset));
1123 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1124 __ cmp(r0, ip);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001125 __ b(ne, &materialized);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001126
1127 // Create regexp literal using runtime function.
1128 // Result will be in r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001129 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
1130 __ mov(r2, Operand(expr->pattern()));
1131 __ mov(r1, Operand(expr->flags()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001132 __ Push(r4, r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001133 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001134
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001135 __ bind(&materialized);
1136 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1137 __ push(r0);
1138 __ mov(r0, Operand(Smi::FromInt(size)));
1139 __ push(r0);
1140 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001141
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001142 // After this, registers are used as follows:
1143 // r0: Newly allocated regexp.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001144 // r1: Materialized regexp.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001145 // r2: temp.
1146 __ pop(r1);
1147 __ CopyFields(r0, r1, r2.bit(), size / kPointerSize);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001148 Apply(context_, r0);
1149}
1150
1151
1152void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1153 Comment cmnt(masm_, "[ ObjectLiteral");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001154 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1155 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1156 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1157 __ mov(r1, Operand(expr->constant_properties()));
1158 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001159 __ Push(r3, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001160 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001161 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001162 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001163 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001164 }
1165
1166 // If result_saved is true the result is on top of the stack. If
1167 // result_saved is false the result is in r0.
1168 bool result_saved = false;
1169
1170 for (int i = 0; i < expr->properties()->length(); i++) {
1171 ObjectLiteral::Property* property = expr->properties()->at(i);
1172 if (property->IsCompileTimeValue()) continue;
1173
1174 Literal* key = property->key();
1175 Expression* value = property->value();
1176 if (!result_saved) {
1177 __ push(r0); // Save result on stack
1178 result_saved = true;
1179 }
1180 switch (property->kind()) {
1181 case ObjectLiteral::Property::CONSTANT:
1182 UNREACHABLE();
1183 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1184 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1185 // Fall through.
1186 case ObjectLiteral::Property::COMPUTED:
1187 if (key->handle()->IsSymbol()) {
1188 VisitForValue(value, kAccumulator);
1189 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001190 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001191 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001192 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001193 break;
1194 }
1195 // Fall through.
1196 case ObjectLiteral::Property::PROTOTYPE:
1197 // Duplicate receiver on stack.
1198 __ ldr(r0, MemOperand(sp));
1199 __ push(r0);
1200 VisitForValue(key, kStack);
1201 VisitForValue(value, kStack);
1202 __ CallRuntime(Runtime::kSetProperty, 3);
1203 break;
1204 case ObjectLiteral::Property::GETTER:
1205 case ObjectLiteral::Property::SETTER:
1206 // Duplicate receiver on stack.
1207 __ ldr(r0, MemOperand(sp));
1208 __ push(r0);
1209 VisitForValue(key, kStack);
1210 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1211 Smi::FromInt(1) :
1212 Smi::FromInt(0)));
1213 __ push(r1);
1214 VisitForValue(value, kStack);
1215 __ CallRuntime(Runtime::kDefineAccessor, 4);
1216 break;
1217 }
1218 }
1219
1220 if (result_saved) {
1221 ApplyTOS(context_);
1222 } else {
1223 Apply(context_, r0);
1224 }
1225}
1226
1227
1228void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1229 Comment cmnt(masm_, "[ ArrayLiteral");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001230
1231 ZoneList<Expression*>* subexprs = expr->values();
1232 int length = subexprs->length();
1233
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001234 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1235 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1236 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1237 __ mov(r1, Operand(expr->constant_elements()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001238 __ Push(r3, r2, r1);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001239 if (expr->constant_elements()->map() == Heap::fixed_cow_array_map()) {
1240 FastCloneShallowArrayStub stub(
1241 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1242 __ CallStub(&stub);
1243 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
1244 } else if (expr->depth() > 1) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001245 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001246 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001247 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001248 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001249 FastCloneShallowArrayStub stub(
1250 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001251 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001252 }
1253
1254 bool result_saved = false; // Is the result saved to the stack?
1255
1256 // Emit code to evaluate all the non-constant subexpressions and to store
1257 // them into the newly cloned array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001258 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001259 Expression* subexpr = subexprs->at(i);
1260 // If the subexpression is a literal or a simple materialized literal it
1261 // is already set in the cloned array.
1262 if (subexpr->AsLiteral() != NULL ||
1263 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1264 continue;
1265 }
1266
1267 if (!result_saved) {
1268 __ push(r0);
1269 result_saved = true;
1270 }
1271 VisitForValue(subexpr, kAccumulator);
1272
1273 // Store the subexpression value in the array's elements.
1274 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
1275 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1276 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1277 __ str(result_register(), FieldMemOperand(r1, offset));
1278
1279 // Update the write barrier for the array store with r0 as the scratch
1280 // register.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001281 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001282 }
1283
1284 if (result_saved) {
1285 ApplyTOS(context_);
1286 } else {
1287 Apply(context_, r0);
1288 }
1289}
1290
1291
ager@chromium.org5c838252010-02-19 08:53:10 +00001292void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1293 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001294 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1295 // on the left-hand side.
1296 if (!expr->target()->IsValidLeftHandSide()) {
1297 VisitForEffect(expr->target());
1298 return;
1299 }
1300
ager@chromium.org5c838252010-02-19 08:53:10 +00001301 // Left-hand side can only be a property, a global or a (parameter or local)
1302 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1303 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1304 LhsKind assign_type = VARIABLE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001305 Property* property = expr->target()->AsProperty();
1306 if (property != NULL) {
1307 assign_type = (property->key()->IsPropertyName())
1308 ? NAMED_PROPERTY
1309 : KEYED_PROPERTY;
ager@chromium.org5c838252010-02-19 08:53:10 +00001310 }
1311
1312 // Evaluate LHS expression.
1313 switch (assign_type) {
1314 case VARIABLE:
1315 // Nothing to do here.
1316 break;
1317 case NAMED_PROPERTY:
1318 if (expr->is_compound()) {
1319 // We need the receiver both on the stack and in the accumulator.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001320 VisitForValue(property->obj(), kAccumulator);
ager@chromium.org5c838252010-02-19 08:53:10 +00001321 __ push(result_register());
1322 } else {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001323 VisitForValue(property->obj(), kStack);
ager@chromium.org5c838252010-02-19 08:53:10 +00001324 }
1325 break;
1326 case KEYED_PROPERTY:
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001327 if (expr->is_compound()) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001328 VisitForValue(property->obj(), kStack);
1329 VisitForValue(property->key(), kAccumulator);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001330 __ ldr(r1, MemOperand(sp, 0));
1331 __ push(r0);
1332 } else {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001333 VisitForValue(property->obj(), kStack);
1334 VisitForValue(property->key(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001335 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001336 break;
1337 }
1338
ager@chromium.org5c838252010-02-19 08:53:10 +00001339 if (expr->is_compound()) {
1340 Location saved_location = location_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001341 location_ = kAccumulator;
ager@chromium.org5c838252010-02-19 08:53:10 +00001342 switch (assign_type) {
1343 case VARIABLE:
1344 EmitVariableLoad(expr->target()->AsVariableProxy()->var(),
1345 Expression::kValue);
1346 break;
1347 case NAMED_PROPERTY:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001348 EmitNamedPropertyLoad(property);
ager@chromium.org5c838252010-02-19 08:53:10 +00001349 break;
1350 case KEYED_PROPERTY:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001351 EmitKeyedPropertyLoad(property);
ager@chromium.org5c838252010-02-19 08:53:10 +00001352 break;
1353 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001354
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001355 Token::Value op = expr->binary_op();
1356 ConstantOperand constant = ShouldInlineSmiCase(op)
1357 ? GetConstantOperand(op, expr->target(), expr->value())
1358 : kNoConstants;
1359 ASSERT(constant == kRightConstant || constant == kNoConstants);
1360 if (constant == kNoConstants) {
1361 __ push(r0); // Left operand goes on the stack.
1362 VisitForValue(expr->value(), kAccumulator);
1363 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001364
ricow@chromium.org65fae842010-08-25 15:26:24 +00001365 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1366 ? OVERWRITE_RIGHT
1367 : NO_OVERWRITE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001368 SetSourcePosition(expr->position() + 1);
1369 if (ShouldInlineSmiCase(op)) {
1370 EmitInlineSmiBinaryOp(expr,
1371 op,
1372 Expression::kValue,
1373 mode,
1374 expr->target(),
1375 expr->value(),
1376 constant);
1377 } else {
1378 EmitBinaryOp(op, Expression::kValue, mode);
1379 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001380 location_ = saved_location;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001381
1382 } else {
1383 VisitForValue(expr->value(), kAccumulator);
ager@chromium.org5c838252010-02-19 08:53:10 +00001384 }
1385
1386 // Record source position before possible IC call.
1387 SetSourcePosition(expr->position());
1388
1389 // Store the value.
1390 switch (assign_type) {
1391 case VARIABLE:
1392 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001393 expr->op(),
ager@chromium.org5c838252010-02-19 08:53:10 +00001394 context_);
1395 break;
1396 case NAMED_PROPERTY:
1397 EmitNamedPropertyAssignment(expr);
1398 break;
1399 case KEYED_PROPERTY:
1400 EmitKeyedPropertyAssignment(expr);
1401 break;
1402 }
1403}
1404
1405
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001406void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1407 SetSourcePosition(prop->position());
1408 Literal* key = prop->key()->AsLiteral();
1409 __ mov(r2, Operand(key->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001410 // Call load IC. It has arguments receiver and property name r0 and r2.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001411 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001412 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001413}
1414
1415
1416void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1417 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001418 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001419 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001420 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001421}
1422
1423
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001424void FullCodeGenerator::EmitInlineSmiBinaryOp(Expression* expr,
1425 Token::Value op,
1426 Expression::Context context,
1427 OverwriteMode mode,
1428 Expression* left,
1429 Expression* right,
1430 ConstantOperand constant) {
1431 ASSERT(constant == kNoConstants); // Only handled case.
1432 EmitBinaryOp(op, context, mode);
1433}
1434
1435
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001436void FullCodeGenerator::EmitBinaryOp(Token::Value op,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001437 Expression::Context context,
1438 OverwriteMode mode) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001439 __ pop(r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001440 GenericBinaryOpStub stub(op, mode, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001441 __ CallStub(&stub);
1442 Apply(context, r0);
1443}
1444
1445
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001446void FullCodeGenerator::EmitAssignment(Expression* expr) {
1447 // Invalid left-hand sides are rewritten to have a 'throw
1448 // ReferenceError' on the left-hand side.
1449 if (!expr->IsValidLeftHandSide()) {
1450 VisitForEffect(expr);
1451 return;
1452 }
1453
1454 // Left-hand side can only be a property, a global or a (parameter or local)
1455 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1456 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1457 LhsKind assign_type = VARIABLE;
1458 Property* prop = expr->AsProperty();
1459 if (prop != NULL) {
1460 assign_type = (prop->key()->IsPropertyName())
1461 ? NAMED_PROPERTY
1462 : KEYED_PROPERTY;
1463 }
1464
1465 switch (assign_type) {
1466 case VARIABLE: {
1467 Variable* var = expr->AsVariableProxy()->var();
1468 EmitVariableAssignment(var, Token::ASSIGN, Expression::kEffect);
1469 break;
1470 }
1471 case NAMED_PROPERTY: {
1472 __ push(r0); // Preserve value.
1473 VisitForValue(prop->obj(), kAccumulator);
1474 __ mov(r1, r0);
1475 __ pop(r0); // Restore value.
1476 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
1477 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001478 EmitCallIC(ic, RelocInfo::CODE_TARGET);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001479 break;
1480 }
1481 case KEYED_PROPERTY: {
1482 __ push(r0); // Preserve value.
1483 VisitForValue(prop->obj(), kStack);
1484 VisitForValue(prop->key(), kAccumulator);
1485 __ mov(r1, r0);
1486 __ pop(r2);
1487 __ pop(r0); // Restore value.
1488 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001489 EmitCallIC(ic, RelocInfo::CODE_TARGET);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001490 break;
1491 }
1492 }
1493}
1494
1495
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001496void FullCodeGenerator::EmitVariableAssignment(Variable* var,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001497 Token::Value op,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001498 Expression::Context context) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001499 // Left-hand sides that rewrite to explicit property accesses do not reach
1500 // here.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001501 ASSERT(var != NULL);
1502 ASSERT(var->is_global() || var->slot() != NULL);
1503
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001504 if (var->is_global()) {
1505 ASSERT(!var->is_this());
1506 // Assignment to a global variable. Use inline caching for the
1507 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001508 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001509 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001510 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001511 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001512 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001513
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001514 } else if (var->mode() != Variable::CONST || op == Token::INIT_CONST) {
1515 // Perform the assignment for non-const variables and for initialization
1516 // of const variables. Const assignments are simply skipped.
1517 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001518 Slot* slot = var->slot();
1519 switch (slot->type()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001520 case Slot::PARAMETER:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001521 case Slot::LOCAL:
1522 if (op == Token::INIT_CONST) {
1523 // Detect const reinitialization by checking for the hole value.
1524 __ ldr(r1, MemOperand(fp, SlotOffset(slot)));
1525 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1526 __ cmp(r1, ip);
1527 __ b(ne, &done);
1528 }
1529 // Perform the assignment.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001530 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1531 break;
1532
1533 case Slot::CONTEXT: {
1534 MemOperand target = EmitSlotSearch(slot, r1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001535 if (op == Token::INIT_CONST) {
1536 // Detect const reinitialization by checking for the hole value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001537 __ ldr(r2, target);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001538 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001539 __ cmp(r2, ip);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001540 __ b(ne, &done);
1541 }
1542 // Perform the assignment and issue the write barrier.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001543 __ str(result_register(), target);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001544 // RecordWrite may destroy all its register arguments.
1545 __ mov(r3, result_register());
1546 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001547 __ RecordWrite(r1, Operand(offset), r2, r3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001548 break;
1549 }
1550
1551 case Slot::LOOKUP:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001552 // Call the runtime for the assignment. The runtime will ignore
1553 // const reinitialization.
1554 __ push(r0); // Value.
1555 __ mov(r0, Operand(slot->var()->name()));
1556 __ Push(cp, r0); // Context and name.
1557 if (op == Token::INIT_CONST) {
1558 // The runtime will ignore const redeclaration.
1559 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1560 } else {
1561 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1562 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001563 break;
1564 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001565 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001566 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001567
ager@chromium.org5c838252010-02-19 08:53:10 +00001568 Apply(context, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001569}
1570
1571
1572void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1573 // Assignment to a property, using a named store IC.
1574 Property* prop = expr->target()->AsProperty();
1575 ASSERT(prop != NULL);
1576 ASSERT(prop->key()->AsLiteral() != NULL);
1577
1578 // If the assignment starts a block of assignments to the same object,
1579 // change to slow case to avoid the quadratic behavior of repeatedly
1580 // adding fast properties.
1581 if (expr->starts_initialization_block()) {
1582 __ push(result_register());
1583 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1584 __ push(ip);
1585 __ CallRuntime(Runtime::kToSlowProperties, 1);
1586 __ pop(result_register());
1587 }
1588
1589 // Record source code position before IC call.
1590 SetSourcePosition(expr->position());
1591 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001592 // Load receiver to r1. Leave a copy in the stack if needed for turning the
1593 // receiver into fast case.
ager@chromium.org5c838252010-02-19 08:53:10 +00001594 if (expr->ends_initialization_block()) {
1595 __ ldr(r1, MemOperand(sp));
1596 } else {
1597 __ pop(r1);
1598 }
1599
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001600 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001601 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001602
1603 // If the assignment ends an initialization block, revert to fast case.
1604 if (expr->ends_initialization_block()) {
1605 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001606 // Receiver is under the result value.
1607 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001608 __ push(ip);
1609 __ CallRuntime(Runtime::kToFastProperties, 1);
1610 __ pop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001611 DropAndApply(1, context_, r0);
1612 } else {
1613 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001614 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001615}
1616
1617
1618void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1619 // Assignment to a property, using a keyed store IC.
1620
1621 // If the assignment starts a block of assignments to the same object,
1622 // change to slow case to avoid the quadratic behavior of repeatedly
1623 // adding fast properties.
1624 if (expr->starts_initialization_block()) {
1625 __ push(result_register());
1626 // Receiver is now under the key and value.
1627 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1628 __ push(ip);
1629 __ CallRuntime(Runtime::kToSlowProperties, 1);
1630 __ pop(result_register());
1631 }
1632
1633 // Record source code position before IC call.
1634 SetSourcePosition(expr->position());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001635 __ pop(r1); // Key.
1636 // Load receiver to r2. Leave a copy in the stack if needed for turning the
1637 // receiver into fast case.
1638 if (expr->ends_initialization_block()) {
1639 __ ldr(r2, MemOperand(sp));
1640 } else {
1641 __ pop(r2);
1642 }
1643
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001644 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001645 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001646
1647 // If the assignment ends an initialization block, revert to fast case.
1648 if (expr->ends_initialization_block()) {
1649 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001650 // Receiver is under the result value.
1651 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001652 __ push(ip);
1653 __ CallRuntime(Runtime::kToFastProperties, 1);
1654 __ pop(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001655 DropAndApply(1, context_, r0);
1656 } else {
1657 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001658 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001659}
1660
1661
1662void FullCodeGenerator::VisitProperty(Property* expr) {
1663 Comment cmnt(masm_, "[ Property");
1664 Expression* key = expr->key();
1665
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001666 if (key->IsPropertyName()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001667 VisitForValue(expr->obj(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001668 EmitNamedPropertyLoad(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001669 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001670 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001671 VisitForValue(expr->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001672 VisitForValue(expr->key(), kAccumulator);
1673 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001674 EmitKeyedPropertyLoad(expr);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001675 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001676 }
1677}
1678
1679void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001680 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001681 RelocInfo::Mode mode) {
1682 // Code common for calls using the IC.
1683 ZoneList<Expression*>* args = expr->arguments();
1684 int arg_count = args->length();
1685 for (int i = 0; i < arg_count; i++) {
1686 VisitForValue(args->at(i), kStack);
1687 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001688 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001689 // Record source position for debugger.
1690 SetSourcePosition(expr->position());
1691 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001692 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1693 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001694 EmitCallIC(ic, mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001695 // Restore context register.
1696 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001697 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001698}
1699
1700
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001701void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
1702 Expression* key,
1703 RelocInfo::Mode mode) {
1704 // Code common for calls using the IC.
1705 ZoneList<Expression*>* args = expr->arguments();
1706 int arg_count = args->length();
1707 for (int i = 0; i < arg_count; i++) {
1708 VisitForValue(args->at(i), kStack);
1709 }
1710 VisitForValue(key, kAccumulator);
1711 __ mov(r2, r0);
1712 // Record source position for debugger.
1713 SetSourcePosition(expr->position());
1714 // Call the IC initialization code.
1715 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1716 Handle<Code> ic = CodeGenerator::ComputeKeyedCallInitialize(arg_count,
1717 in_loop);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001718 EmitCallIC(ic, mode);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001719 // Restore context register.
1720 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1721 Apply(context_, r0);
1722}
1723
1724
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001725void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1726 // Code common for calls using the call stub.
1727 ZoneList<Expression*>* args = expr->arguments();
1728 int arg_count = args->length();
1729 for (int i = 0; i < arg_count; i++) {
1730 VisitForValue(args->at(i), kStack);
1731 }
1732 // Record source position for debugger.
1733 SetSourcePosition(expr->position());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001734 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1735 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001736 __ CallStub(&stub);
1737 // Restore context register.
1738 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001739 DropAndApply(1, context_, r0);
1740}
1741
1742
1743void FullCodeGenerator::VisitCall(Call* expr) {
1744 Comment cmnt(masm_, "[ Call");
1745 Expression* fun = expr->expression();
1746 Variable* var = fun->AsVariableProxy()->AsVariable();
1747
1748 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001749 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1750 // resolve the function we need to call and the receiver of the
1751 // call. Then we call the resolved function using the given
1752 // arguments.
1753 VisitForValue(fun, kStack);
1754 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1755 __ push(r2); // Reserved receiver slot.
1756
1757 // Push the arguments.
1758 ZoneList<Expression*>* args = expr->arguments();
1759 int arg_count = args->length();
1760 for (int i = 0; i < arg_count; i++) {
1761 VisitForValue(args->at(i), kStack);
1762 }
1763
1764 // Push copy of the function - found below the arguments.
1765 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1766 __ push(r1);
1767
1768 // Push copy of the first argument or undefined if it doesn't exist.
1769 if (arg_count > 0) {
1770 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1771 __ push(r1);
1772 } else {
1773 __ push(r2);
1774 }
1775
1776 // Push the receiver of the enclosing function and do runtime call.
1777 __ ldr(r1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1778 __ push(r1);
1779 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1780
1781 // The runtime call returns a pair of values in r0 (function) and
1782 // r1 (receiver). Touch up the stack with the right values.
1783 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1784 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
1785
1786 // Record source position for debugger.
1787 SetSourcePosition(expr->position());
1788 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1789 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1790 __ CallStub(&stub);
1791 // Restore context register.
1792 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1793 DropAndApply(1, context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001794 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001795 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001796 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001797 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001798 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1799 } else if (var != NULL && var->slot() != NULL &&
1800 var->slot()->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001801 // Call to a lookup slot (dynamically introduced variable).
1802 Label slow, done;
1803
1804 // Generate code for loading from variables potentially shadowed
1805 // by eval-introduced variables.
1806 EmitDynamicLoadFromSlotFastCase(var->slot(),
1807 NOT_INSIDE_TYPEOF,
1808 &slow,
1809 &done);
1810
1811 __ bind(&slow);
1812 // Call the runtime to find the function to call (returned in eax)
1813 // and the object holding it (returned in edx).
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001814 __ push(context_register());
1815 __ mov(r2, Operand(var->name()));
1816 __ push(r2);
1817 __ CallRuntime(Runtime::kLoadContextSlot, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001818 __ Push(r0, r1); // Function, receiver.
1819
1820 // If fast case code has been generated, emit code to push the
1821 // function and receiver and have the slow path jump around this
1822 // code.
1823 if (done.is_linked()) {
1824 Label call;
1825 __ b(&call);
1826 __ bind(&done);
1827 // Push function.
1828 __ push(r0);
1829 // Push global receiver.
1830 __ ldr(r1, CodeGenerator::GlobalObject());
1831 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1832 __ push(r1);
1833 __ bind(&call);
1834 }
1835
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001836 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001837 } else if (fun->AsProperty() != NULL) {
1838 // Call to an object property.
1839 Property* prop = fun->AsProperty();
1840 Literal* key = prop->key()->AsLiteral();
1841 if (key != NULL && key->handle()->IsSymbol()) {
1842 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001843 VisitForValue(prop->obj(), kStack);
1844 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1845 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001846 // Call to a keyed property.
1847 // For a synthetic property use keyed load IC followed by function call,
1848 // for a regular property use keyed CallIC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001849 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001850 if (prop->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001851 VisitForValue(prop->key(), kAccumulator);
1852 // Record source code position for IC call.
1853 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001854 __ pop(r1); // We do not need to keep the receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001855
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001856 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001857 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001858 __ ldr(r1, CodeGenerator::GlobalObject());
1859 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001860 __ Push(r0, r1); // Function, receiver.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001861 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001862 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001863 EmitKeyedCallWithIC(expr, prop->key(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001864 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001865 }
1866 } else {
1867 // Call to some other expression. If the expression is an anonymous
1868 // function literal not called in a loop, mark it as one that should
1869 // also use the fast code generator.
1870 FunctionLiteral* lit = fun->AsFunctionLiteral();
1871 if (lit != NULL &&
1872 lit->name()->Equals(Heap::empty_string()) &&
1873 loop_depth() == 0) {
1874 lit->set_try_full_codegen(true);
1875 }
1876 VisitForValue(fun, kStack);
1877 // Load global receiver object.
1878 __ ldr(r1, CodeGenerator::GlobalObject());
1879 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1880 __ push(r1);
1881 // Emit function call.
1882 EmitCallWithStub(expr);
1883 }
1884}
1885
1886
1887void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1888 Comment cmnt(masm_, "[ CallNew");
1889 // According to ECMA-262, section 11.2.2, page 44, the function
1890 // expression in new calls must be evaluated before the
1891 // arguments.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001892
1893 // Push constructor on the stack. If it's not a function it's used as
1894 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
1895 // ignored.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001896 VisitForValue(expr->expression(), kStack);
1897
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001898 // Push the arguments ("left-to-right") on the stack.
1899 ZoneList<Expression*>* args = expr->arguments();
1900 int arg_count = args->length();
1901 for (int i = 0; i < arg_count; i++) {
1902 VisitForValue(args->at(i), kStack);
1903 }
1904
1905 // Call the construct call builtin that handles allocation and
1906 // constructor invocation.
1907 SetSourcePosition(expr->position());
1908
ricow@chromium.org65fae842010-08-25 15:26:24 +00001909 // Load function and argument count into r1 and r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001910 __ mov(r0, Operand(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001911 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001912
1913 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1914 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001915 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001916}
1917
1918
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001919void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1920 ASSERT(args->length() == 1);
1921
1922 VisitForValue(args->at(0), kAccumulator);
1923
1924 Label materialize_true, materialize_false;
1925 Label* if_true = NULL;
1926 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001927 Label* fall_through = NULL;
1928 PrepareTest(&materialize_true, &materialize_false,
1929 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001930
1931 __ BranchOnSmi(r0, if_true);
1932 __ b(if_false);
1933
1934 Apply(context_, if_true, if_false);
1935}
1936
1937
1938void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1939 ASSERT(args->length() == 1);
1940
1941 VisitForValue(args->at(0), kAccumulator);
1942
1943 Label materialize_true, materialize_false;
1944 Label* if_true = NULL;
1945 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001946 Label* fall_through = NULL;
1947 PrepareTest(&materialize_true, &materialize_false,
1948 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001949
1950 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001951 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001952
1953 Apply(context_, if_true, if_false);
1954}
1955
1956
1957void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1958 ASSERT(args->length() == 1);
1959
1960 VisitForValue(args->at(0), kAccumulator);
1961
1962 Label materialize_true, materialize_false;
1963 Label* if_true = NULL;
1964 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001965 Label* fall_through = NULL;
1966 PrepareTest(&materialize_true, &materialize_false,
1967 &if_true, &if_false, &fall_through);
1968
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001969 __ BranchOnSmi(r0, if_false);
1970 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1971 __ cmp(r0, ip);
1972 __ b(eq, if_true);
1973 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1974 // Undetectable objects behave like undefined when tested with typeof.
1975 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
1976 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1977 __ b(ne, if_false);
1978 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1979 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
1980 __ b(lt, if_false);
1981 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001982 Split(le, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001983
1984 Apply(context_, if_true, if_false);
1985}
1986
1987
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001988void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
1989 ASSERT(args->length() == 1);
1990
1991 VisitForValue(args->at(0), kAccumulator);
1992
1993 Label materialize_true, materialize_false;
1994 Label* if_true = NULL;
1995 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001996 Label* fall_through = NULL;
1997 PrepareTest(&materialize_true, &materialize_false,
1998 &if_true, &if_false, &fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001999
2000 __ BranchOnSmi(r0, if_false);
2001 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002002 Split(ge, if_true, if_false, fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002003
2004 Apply(context_, if_true, if_false);
2005}
2006
2007
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002008void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
2009 ASSERT(args->length() == 1);
2010
2011 VisitForValue(args->at(0), kAccumulator);
2012
2013 Label materialize_true, materialize_false;
2014 Label* if_true = NULL;
2015 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002016 Label* fall_through = NULL;
2017 PrepareTest(&materialize_true, &materialize_false,
2018 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002019
2020 __ BranchOnSmi(r0, if_false);
2021 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2022 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
2023 __ tst(r1, Operand(1 << Map::kIsUndetectable));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002024 Split(ne, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002025
2026 Apply(context_, if_true, if_false);
2027}
2028
2029
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002030void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2031 ZoneList<Expression*>* args) {
2032
2033 ASSERT(args->length() == 1);
2034
2035 VisitForValue(args->at(0), kAccumulator);
2036
2037 Label materialize_true, materialize_false;
2038 Label* if_true = NULL;
2039 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002040 Label* fall_through = NULL;
2041 PrepareTest(&materialize_true, &materialize_false,
2042 &if_true, &if_false, &fall_through);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002043
2044 // Just indicate false, as %_IsStringWrapperSafeForDefaultValueOf() is only
2045 // used in a few functions in runtime.js which should not normally be hit by
2046 // this compiler.
2047 __ jmp(if_false);
2048 Apply(context_, if_true, if_false);
2049}
2050
2051
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002052void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
2053 ASSERT(args->length() == 1);
2054
2055 VisitForValue(args->at(0), kAccumulator);
2056
2057 Label materialize_true, materialize_false;
2058 Label* if_true = NULL;
2059 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002060 Label* fall_through = NULL;
2061 PrepareTest(&materialize_true, &materialize_false,
2062 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002063
2064 __ BranchOnSmi(r0, if_false);
2065 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002066 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002067
2068 Apply(context_, if_true, if_false);
2069}
2070
2071
2072void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
2073 ASSERT(args->length() == 1);
2074
2075 VisitForValue(args->at(0), kAccumulator);
2076
2077 Label materialize_true, materialize_false;
2078 Label* if_true = NULL;
2079 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002080 Label* fall_through = NULL;
2081 PrepareTest(&materialize_true, &materialize_false,
2082 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002083
2084 __ BranchOnSmi(r0, if_false);
2085 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002086 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002087
2088 Apply(context_, if_true, if_false);
2089}
2090
2091
2092void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
2093 ASSERT(args->length() == 1);
2094
2095 VisitForValue(args->at(0), kAccumulator);
2096
2097 Label materialize_true, materialize_false;
2098 Label* if_true = NULL;
2099 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002100 Label* fall_through = NULL;
2101 PrepareTest(&materialize_true, &materialize_false,
2102 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002103
2104 __ BranchOnSmi(r0, if_false);
2105 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002106 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002107
2108 Apply(context_, if_true, if_false);
2109}
2110
2111
2112
2113void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
2114 ASSERT(args->length() == 0);
2115
2116 Label materialize_true, materialize_false;
2117 Label* if_true = NULL;
2118 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002119 Label* fall_through = NULL;
2120 PrepareTest(&materialize_true, &materialize_false,
2121 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002122
2123 // Get the frame pointer for the calling frame.
2124 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2125
2126 // Skip the arguments adaptor frame if it exists.
2127 Label check_frame_marker;
2128 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
2129 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2130 __ b(ne, &check_frame_marker);
2131 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
2132
2133 // Check the marker in the calling frame.
2134 __ bind(&check_frame_marker);
2135 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
2136 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002137 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002138
2139 Apply(context_, if_true, if_false);
2140}
2141
2142
2143void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
2144 ASSERT(args->length() == 2);
2145
2146 // Load the two objects into registers and perform the comparison.
2147 VisitForValue(args->at(0), kStack);
2148 VisitForValue(args->at(1), kAccumulator);
2149
2150 Label materialize_true, materialize_false;
2151 Label* if_true = NULL;
2152 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002153 Label* fall_through = NULL;
2154 PrepareTest(&materialize_true, &materialize_false,
2155 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002156
2157 __ pop(r1);
2158 __ cmp(r0, r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002159 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002160
2161 Apply(context_, if_true, if_false);
2162}
2163
2164
2165void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
2166 ASSERT(args->length() == 1);
2167
2168 // ArgumentsAccessStub expects the key in edx and the formal
2169 // parameter count in eax.
2170 VisitForValue(args->at(0), kAccumulator);
2171 __ mov(r1, r0);
2172 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2173 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2174 __ CallStub(&stub);
2175 Apply(context_, r0);
2176}
2177
2178
2179void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
2180 ASSERT(args->length() == 0);
2181
2182 Label exit;
2183 // Get the number of formal parameters.
2184 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2185
2186 // Check if the calling frame is an arguments adaptor frame.
2187 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2188 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
2189 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2190 __ b(ne, &exit);
2191
2192 // Arguments adaptor case: Read the arguments length from the
2193 // adaptor frame.
2194 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2195
2196 __ bind(&exit);
2197 Apply(context_, r0);
2198}
2199
2200
2201void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
2202 ASSERT(args->length() == 1);
2203 Label done, null, function, non_function_constructor;
2204
2205 VisitForValue(args->at(0), kAccumulator);
2206
2207 // If the object is a smi, we return null.
2208 __ BranchOnSmi(r0, &null);
2209
2210 // Check that the object is a JS object but take special care of JS
2211 // functions to make sure they have 'Function' as their class.
2212 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2213 __ b(lt, &null);
2214
2215 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2216 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2217 // LAST_JS_OBJECT_TYPE.
2218 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2219 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2220 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2221 __ b(eq, &function);
2222
2223 // Check if the constructor in the map is a function.
2224 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2225 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2226 __ b(ne, &non_function_constructor);
2227
2228 // r0 now contains the constructor function. Grab the
2229 // instance class name from there.
2230 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2231 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2232 __ b(&done);
2233
2234 // Functions have class 'Function'.
2235 __ bind(&function);
2236 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2237 __ jmp(&done);
2238
2239 // Objects with a non-function constructor have class 'Object'.
2240 __ bind(&non_function_constructor);
2241 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2242 __ jmp(&done);
2243
2244 // Non-JS objects have class null.
2245 __ bind(&null);
2246 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2247
2248 // All done.
2249 __ bind(&done);
2250
2251 Apply(context_, r0);
2252}
2253
2254
2255void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2256 // Conditionally generate a log call.
2257 // Args:
2258 // 0 (literal string): The type of logging (corresponds to the flags).
2259 // This is used to determine whether or not to generate the log call.
2260 // 1 (string): Format string. Access the string at argument index 2
2261 // with '%2s' (see Logger::LogRuntime for all the formats).
2262 // 2 (array): Arguments to the format string.
2263 ASSERT_EQ(args->length(), 3);
2264#ifdef ENABLE_LOGGING_AND_PROFILING
2265 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2266 VisitForValue(args->at(1), kStack);
2267 VisitForValue(args->at(2), kStack);
2268 __ CallRuntime(Runtime::kLog, 2);
2269 }
2270#endif
2271 // Finally, we're expected to leave a value on the top of the stack.
2272 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2273 Apply(context_, r0);
2274}
2275
2276
2277void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2278 ASSERT(args->length() == 0);
2279
2280 Label slow_allocate_heapnumber;
2281 Label heapnumber_allocated;
2282
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002283 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
2284 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002285 __ jmp(&heapnumber_allocated);
2286
2287 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002288 // Allocate a heap number.
2289 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002290 __ mov(r4, Operand(r0));
2291
2292 __ bind(&heapnumber_allocated);
2293
2294 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
2295 // by computing:
2296 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2297 if (CpuFeatures::IsSupported(VFP3)) {
2298 __ PrepareCallCFunction(0, r1);
2299 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
2300
2301 CpuFeatures::Scope scope(VFP3);
2302 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2303 // Create this constant using mov/orr to avoid PC relative load.
2304 __ mov(r1, Operand(0x41000000));
2305 __ orr(r1, r1, Operand(0x300000));
2306 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
2307 __ vmov(d7, r0, r1);
2308 // Move 0x4130000000000000 to VFP.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00002309 __ mov(r0, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002310 __ vmov(d8, r0, r1);
2311 // Subtract and store the result in the heap number.
2312 __ vsub(d7, d7, d8);
2313 __ sub(r0, r4, Operand(kHeapObjectTag));
2314 __ vstr(d7, r0, HeapNumber::kValueOffset);
2315 __ mov(r0, r4);
2316 } else {
2317 __ mov(r0, Operand(r4));
2318 __ PrepareCallCFunction(1, r1);
2319 __ CallCFunction(
2320 ExternalReference::fill_heap_number_with_random_function(), 1);
2321 }
2322
2323 Apply(context_, r0);
2324}
2325
2326
2327void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
2328 // Load the arguments on the stack and call the stub.
2329 SubStringStub stub;
2330 ASSERT(args->length() == 3);
2331 VisitForValue(args->at(0), kStack);
2332 VisitForValue(args->at(1), kStack);
2333 VisitForValue(args->at(2), kStack);
2334 __ CallStub(&stub);
2335 Apply(context_, r0);
2336}
2337
2338
2339void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
2340 // Load the arguments on the stack and call the stub.
2341 RegExpExecStub stub;
2342 ASSERT(args->length() == 4);
2343 VisitForValue(args->at(0), kStack);
2344 VisitForValue(args->at(1), kStack);
2345 VisitForValue(args->at(2), kStack);
2346 VisitForValue(args->at(3), kStack);
2347 __ CallStub(&stub);
2348 Apply(context_, r0);
2349}
2350
2351
2352void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
2353 ASSERT(args->length() == 1);
2354
2355 VisitForValue(args->at(0), kAccumulator); // Load the object.
2356
2357 Label done;
2358 // If the object is a smi return the object.
2359 __ BranchOnSmi(r0, &done);
2360 // If the object is not a value type, return the object.
2361 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
2362 __ b(ne, &done);
2363 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2364
2365 __ bind(&done);
2366 Apply(context_, r0);
2367}
2368
2369
2370void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
2371 // Load the arguments on the stack and call the runtime function.
2372 ASSERT(args->length() == 2);
2373 VisitForValue(args->at(0), kStack);
2374 VisitForValue(args->at(1), kStack);
2375 __ CallRuntime(Runtime::kMath_pow, 2);
2376 Apply(context_, r0);
2377}
2378
2379
2380void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
2381 ASSERT(args->length() == 2);
2382
2383 VisitForValue(args->at(0), kStack); // Load the object.
2384 VisitForValue(args->at(1), kAccumulator); // Load the value.
2385 __ pop(r1); // r0 = value. r1 = object.
2386
2387 Label done;
2388 // If the object is a smi, return the value.
2389 __ BranchOnSmi(r1, &done);
2390
2391 // If the object is not a value type, return the value.
2392 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
2393 __ b(ne, &done);
2394
2395 // Store the value.
2396 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2397 // Update the write barrier. Save the value as it will be
2398 // overwritten by the write barrier code and is needed afterward.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002399 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002400
2401 __ bind(&done);
2402 Apply(context_, r0);
2403}
2404
2405
2406void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2407 ASSERT_EQ(args->length(), 1);
2408
2409 // Load the argument on the stack and call the stub.
2410 VisitForValue(args->at(0), kStack);
2411
2412 NumberToStringStub stub;
2413 __ CallStub(&stub);
2414 Apply(context_, r0);
2415}
2416
2417
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002418void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002419 ASSERT(args->length() == 1);
2420
2421 VisitForValue(args->at(0), kAccumulator);
2422
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002423 Label done;
2424 StringCharFromCodeGenerator generator(r0, r1);
2425 generator.GenerateFast(masm_);
2426 __ jmp(&done);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002427
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002428 NopRuntimeCallHelper call_helper;
2429 generator.GenerateSlow(masm_, call_helper);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002430
2431 __ bind(&done);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002432 Apply(context_, r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002433}
2434
2435
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002436void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
2437 ASSERT(args->length() == 2);
2438
2439 VisitForValue(args->at(0), kStack);
2440 VisitForValue(args->at(1), kAccumulator);
2441
2442 Register object = r1;
2443 Register index = r0;
2444 Register scratch = r2;
2445 Register result = r3;
2446
2447 __ pop(object);
2448
2449 Label need_conversion;
2450 Label index_out_of_range;
2451 Label done;
2452 StringCharCodeAtGenerator generator(object,
2453 index,
2454 scratch,
2455 result,
2456 &need_conversion,
2457 &need_conversion,
2458 &index_out_of_range,
2459 STRING_INDEX_IS_NUMBER);
2460 generator.GenerateFast(masm_);
2461 __ jmp(&done);
2462
2463 __ bind(&index_out_of_range);
2464 // When the index is out of range, the spec requires us to return
2465 // NaN.
2466 __ LoadRoot(result, Heap::kNanValueRootIndex);
2467 __ jmp(&done);
2468
2469 __ bind(&need_conversion);
2470 // Load the undefined value into the result register, which will
2471 // trigger conversion.
2472 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2473 __ jmp(&done);
2474
2475 NopRuntimeCallHelper call_helper;
2476 generator.GenerateSlow(masm_, call_helper);
2477
2478 __ bind(&done);
2479 Apply(context_, result);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002480}
2481
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002482
2483void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
2484 ASSERT(args->length() == 2);
2485
2486 VisitForValue(args->at(0), kStack);
2487 VisitForValue(args->at(1), kAccumulator);
2488
2489 Register object = r1;
2490 Register index = r0;
2491 Register scratch1 = r2;
2492 Register scratch2 = r3;
2493 Register result = r0;
2494
2495 __ pop(object);
2496
2497 Label need_conversion;
2498 Label index_out_of_range;
2499 Label done;
2500 StringCharAtGenerator generator(object,
2501 index,
2502 scratch1,
2503 scratch2,
2504 result,
2505 &need_conversion,
2506 &need_conversion,
2507 &index_out_of_range,
2508 STRING_INDEX_IS_NUMBER);
2509 generator.GenerateFast(masm_);
2510 __ jmp(&done);
2511
2512 __ bind(&index_out_of_range);
2513 // When the index is out of range, the spec requires us to return
2514 // the empty string.
2515 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2516 __ jmp(&done);
2517
2518 __ bind(&need_conversion);
2519 // Move smi zero into the result register, which will trigger
2520 // conversion.
2521 __ mov(result, Operand(Smi::FromInt(0)));
2522 __ jmp(&done);
2523
2524 NopRuntimeCallHelper call_helper;
2525 generator.GenerateSlow(masm_, call_helper);
2526
2527 __ bind(&done);
2528 Apply(context_, result);
2529}
2530
2531
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002532void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2533 ASSERT_EQ(2, args->length());
2534
2535 VisitForValue(args->at(0), kStack);
2536 VisitForValue(args->at(1), kStack);
2537
2538 StringAddStub stub(NO_STRING_ADD_FLAGS);
2539 __ CallStub(&stub);
2540 Apply(context_, r0);
2541}
2542
2543
2544void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
2545 ASSERT_EQ(2, args->length());
2546
2547 VisitForValue(args->at(0), kStack);
2548 VisitForValue(args->at(1), kStack);
2549
2550 StringCompareStub stub;
2551 __ CallStub(&stub);
2552 Apply(context_, r0);
2553}
2554
2555
2556void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
2557 // Load the argument on the stack and call the runtime.
2558 ASSERT(args->length() == 1);
2559 VisitForValue(args->at(0), kStack);
2560 __ CallRuntime(Runtime::kMath_sin, 1);
2561 Apply(context_, r0);
2562}
2563
2564
2565void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
2566 // Load the argument on the stack and call the runtime.
2567 ASSERT(args->length() == 1);
2568 VisitForValue(args->at(0), kStack);
2569 __ CallRuntime(Runtime::kMath_cos, 1);
2570 Apply(context_, r0);
2571}
2572
2573
2574void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2575 // Load the argument on the stack and call the runtime function.
2576 ASSERT(args->length() == 1);
2577 VisitForValue(args->at(0), kStack);
2578 __ CallRuntime(Runtime::kMath_sqrt, 1);
2579 Apply(context_, r0);
2580}
2581
2582
2583void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2584 ASSERT(args->length() >= 2);
2585
2586 int arg_count = args->length() - 2; // For receiver and function.
2587 VisitForValue(args->at(0), kStack); // Receiver.
2588 for (int i = 0; i < arg_count; i++) {
2589 VisitForValue(args->at(i + 1), kStack);
2590 }
2591 VisitForValue(args->at(arg_count + 1), kAccumulator); // Function.
2592
2593 // InvokeFunction requires function in r1. Move it in there.
2594 if (!result_register().is(r1)) __ mov(r1, result_register());
2595 ParameterCount count(arg_count);
2596 __ InvokeFunction(r1, count, CALL_FUNCTION);
2597 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2598 Apply(context_, r0);
2599}
2600
2601
2602void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2603 ASSERT(args->length() == 3);
2604 VisitForValue(args->at(0), kStack);
2605 VisitForValue(args->at(1), kStack);
2606 VisitForValue(args->at(2), kStack);
2607 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
2608 Apply(context_, r0);
2609}
2610
2611
2612void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2613 ASSERT(args->length() == 3);
2614 VisitForValue(args->at(0), kStack);
2615 VisitForValue(args->at(1), kStack);
2616 VisitForValue(args->at(2), kStack);
2617 __ CallRuntime(Runtime::kSwapElements, 3);
2618 Apply(context_, r0);
2619}
2620
2621
2622void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2623 ASSERT_EQ(2, args->length());
2624
2625 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2626 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2627
2628 Handle<FixedArray> jsfunction_result_caches(
2629 Top::global_context()->jsfunction_result_caches());
2630 if (jsfunction_result_caches->length() <= cache_id) {
2631 __ Abort("Attempt to use undefined cache.");
2632 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2633 Apply(context_, r0);
2634 return;
2635 }
2636
2637 VisitForValue(args->at(1), kAccumulator);
2638
2639 Register key = r0;
2640 Register cache = r1;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002641 __ ldr(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002642 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002643 __ ldr(cache, ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002644 __ ldr(cache,
2645 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2646
2647
2648 Label done, not_found;
2649 // tmp now holds finger offset as a smi.
2650 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2651 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2652 // r2 now holds finger offset as a smi.
2653 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2654 // r3 now points to the start of fixed array elements.
2655 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2656 // Note side effect of PreIndex: r3 now points to the key of the pair.
2657 __ cmp(key, r2);
2658 __ b(ne, &not_found);
2659
2660 __ ldr(r0, MemOperand(r3, kPointerSize));
2661 __ b(&done);
2662
2663 __ bind(&not_found);
2664 // Call runtime to perform the lookup.
2665 __ Push(cache, key);
2666 __ CallRuntime(Runtime::kGetFromCache, 2);
2667
2668 __ bind(&done);
2669 Apply(context_, r0);
2670}
2671
2672
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002673void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
2674 ASSERT_EQ(2, args->length());
2675
2676 Register right = r0;
2677 Register left = r1;
2678 Register tmp = r2;
2679 Register tmp2 = r3;
2680
2681 VisitForValue(args->at(0), kStack);
2682 VisitForValue(args->at(1), kAccumulator);
2683 __ pop(left);
2684
2685 Label done, fail, ok;
2686 __ cmp(left, Operand(right));
2687 __ b(eq, &ok);
2688 // Fail if either is a non-HeapObject.
2689 __ and_(tmp, left, Operand(right));
2690 __ tst(tmp, Operand(kSmiTagMask));
2691 __ b(eq, &fail);
2692 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
2693 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
2694 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
2695 __ b(ne, &fail);
2696 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
2697 __ cmp(tmp, Operand(tmp2));
2698 __ b(ne, &fail);
2699 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
2700 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
2701 __ cmp(tmp, tmp2);
2702 __ b(eq, &ok);
2703 __ bind(&fail);
2704 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
2705 __ jmp(&done);
2706 __ bind(&ok);
2707 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
2708 __ bind(&done);
2709
2710 Apply(context_, r0);
2711}
2712
2713
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002714void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
2715 VisitForValue(args->at(0), kAccumulator);
2716
2717 Label materialize_true, materialize_false;
2718 Label* if_true = NULL;
2719 Label* if_false = NULL;
2720 Label* fall_through = NULL;
2721 PrepareTest(&materialize_true, &materialize_false,
2722 &if_true, &if_false, &fall_through);
2723
2724 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2725 __ tst(r0, Operand(String::kContainsCachedArrayIndexMask));
2726
2727 __ b(eq, if_true);
2728 __ b(if_false);
2729
2730 Apply(context_, if_true, if_false);
2731}
2732
2733
2734void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
2735 ASSERT(args->length() == 1);
2736 VisitForValue(args->at(0), kAccumulator);
2737 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2738 __ IndexFromHash(r0, r0);
2739 Apply(context_, r0);
2740}
2741
2742
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002743void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002744 Handle<String> name = expr->name();
2745 if (name->length() > 0 && name->Get(0) == '_') {
2746 Comment cmnt(masm_, "[ InlineRuntimeCall");
2747 EmitInlineRuntimeCall(expr);
2748 return;
2749 }
2750
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002751 Comment cmnt(masm_, "[ CallRuntime");
2752 ZoneList<Expression*>* args = expr->arguments();
2753
2754 if (expr->is_jsruntime()) {
2755 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002756 __ ldr(r0, CodeGenerator::GlobalObject());
2757 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002758 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002759 }
2760
2761 // Push the arguments ("left-to-right").
2762 int arg_count = args->length();
2763 for (int i = 0; i < arg_count; i++) {
2764 VisitForValue(args->at(i), kStack);
2765 }
2766
2767 if (expr->is_jsruntime()) {
2768 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002769 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002770 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2771 NOT_IN_LOOP);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002772 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002773 // Restore context register.
2774 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002775 } else {
2776 // Call the C runtime function.
2777 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002778 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002779 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002780}
2781
2782
2783void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2784 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002785 case Token::DELETE: {
2786 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2787 Property* prop = expr->expression()->AsProperty();
2788 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2789 if (prop == NULL && var == NULL) {
2790 // Result of deleting non-property, non-variable reference is true.
2791 // The subexpression may have side effects.
2792 VisitForEffect(expr->expression());
2793 Apply(context_, true);
2794 } else if (var != NULL &&
2795 !var->is_global() &&
2796 var->slot() != NULL &&
2797 var->slot()->type() != Slot::LOOKUP) {
2798 // Result of deleting non-global, non-dynamic variables is false.
2799 // The subexpression does not have side effects.
2800 Apply(context_, false);
2801 } else {
2802 // Property or variable reference. Call the delete builtin with
2803 // object and property name as arguments.
2804 if (prop != NULL) {
2805 VisitForValue(prop->obj(), kStack);
2806 VisitForValue(prop->key(), kStack);
2807 } else if (var->is_global()) {
2808 __ ldr(r1, CodeGenerator::GlobalObject());
2809 __ mov(r0, Operand(var->name()));
2810 __ Push(r1, r0);
2811 } else {
2812 // Non-global variable. Call the runtime to look up the context
2813 // where the variable was introduced.
2814 __ push(context_register());
2815 __ mov(r2, Operand(var->name()));
2816 __ push(r2);
2817 __ CallRuntime(Runtime::kLookupContext, 2);
2818 __ push(r0);
2819 __ mov(r2, Operand(var->name()));
2820 __ push(r2);
2821 }
2822 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
2823 Apply(context_, r0);
2824 }
2825 break;
2826 }
2827
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002828 case Token::VOID: {
2829 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2830 VisitForEffect(expr->expression());
2831 switch (context_) {
2832 case Expression::kUninitialized:
2833 UNREACHABLE();
2834 break;
2835 case Expression::kEffect:
2836 break;
2837 case Expression::kValue:
2838 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2839 switch (location_) {
2840 case kAccumulator:
2841 break;
2842 case kStack:
2843 __ push(result_register());
2844 break;
2845 }
2846 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002847 case Expression::kTest:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002848 __ jmp(false_label_);
2849 break;
2850 }
2851 break;
2852 }
2853
2854 case Token::NOT: {
2855 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002856 Label materialize_true, materialize_false;
2857 Label* if_true = NULL;
2858 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002859 Label* fall_through = NULL;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002860
2861 // Notice that the labels are swapped.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002862 PrepareTest(&materialize_true, &materialize_false,
2863 &if_false, &if_true, &fall_through);
2864 VisitForControl(expr->expression(), if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002865 Apply(context_, if_false, if_true); // Labels swapped.
2866 break;
2867 }
2868
2869 case Token::TYPEOF: {
2870 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002871 VisitForTypeofValue(expr->expression(), kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002872 __ CallRuntime(Runtime::kTypeof, 1);
2873 Apply(context_, r0);
2874 break;
2875 }
2876
2877 case Token::ADD: {
2878 Comment cmt(masm_, "[ UnaryOperation (ADD)");
2879 VisitForValue(expr->expression(), kAccumulator);
2880 Label no_conversion;
2881 __ tst(result_register(), Operand(kSmiTagMask));
2882 __ b(eq, &no_conversion);
2883 __ push(r0);
2884 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2885 __ bind(&no_conversion);
2886 Apply(context_, result_register());
2887 break;
2888 }
2889
2890 case Token::SUB: {
2891 Comment cmt(masm_, "[ UnaryOperation (SUB)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002892 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002893 UnaryOverwriteMode overwrite =
2894 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002895 GenericUnaryOpStub stub(Token::SUB,
2896 overwrite,
2897 NO_UNARY_FLAGS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002898 // GenericUnaryOpStub expects the argument to be in the
2899 // accumulator register r0.
2900 VisitForValue(expr->expression(), kAccumulator);
2901 __ CallStub(&stub);
2902 Apply(context_, r0);
2903 break;
2904 }
2905
2906 case Token::BIT_NOT: {
2907 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002908 // The generic unary operation stub expects the argument to be
2909 // in the accumulator register r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002910 VisitForValue(expr->expression(), kAccumulator);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002911 Label done;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002912 bool inline_smi_code = ShouldInlineSmiCase(expr->op());
2913 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002914 Label call_stub;
2915 __ BranchOnNotSmi(r0, &call_stub);
2916 __ mvn(r0, Operand(r0));
2917 // Bit-clear inverted smi-tag.
2918 __ bic(r0, r0, Operand(kSmiTagMask));
2919 __ b(&done);
2920 __ bind(&call_stub);
2921 }
2922 bool overwrite = expr->expression()->ResultOverwriteAllowed();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002923 UnaryOpFlags flags = inline_smi_code
2924 ? NO_UNARY_SMI_CODE_IN_STUB
2925 : NO_UNARY_FLAGS;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002926 UnaryOverwriteMode mode =
2927 overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002928 GenericUnaryOpStub stub(Token::BIT_NOT, mode, flags);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002929 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002930 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002931 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002932 break;
2933 }
2934
2935 default:
2936 UNREACHABLE();
2937 }
2938}
2939
2940
2941void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2942 Comment cmnt(masm_, "[ CountOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002943 SetSourcePosition(expr->position());
2944
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002945 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2946 // as the left-hand side.
2947 if (!expr->expression()->IsValidLeftHandSide()) {
2948 VisitForEffect(expr->expression());
2949 return;
2950 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002951
2952 // Expression can only be a property, a global or a (parameter or local)
2953 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2954 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2955 LhsKind assign_type = VARIABLE;
2956 Property* prop = expr->expression()->AsProperty();
2957 // In case of a property we use the uninitialized expression context
2958 // of the key to detect a named property.
2959 if (prop != NULL) {
2960 assign_type =
2961 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2962 }
2963
2964 // Evaluate expression and get value.
2965 if (assign_type == VARIABLE) {
2966 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
2967 Location saved_location = location_;
2968 location_ = kAccumulator;
2969 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
2970 Expression::kValue);
2971 location_ = saved_location;
2972 } else {
2973 // Reserve space for result of postfix operation.
2974 if (expr->is_postfix() && context_ != Expression::kEffect) {
2975 __ mov(ip, Operand(Smi::FromInt(0)));
2976 __ push(ip);
2977 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002978 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002979 // Put the object both on the stack and in the accumulator.
2980 VisitForValue(prop->obj(), kAccumulator);
2981 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002982 EmitNamedPropertyLoad(prop);
2983 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002984 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002985 VisitForValue(prop->key(), kAccumulator);
2986 __ ldr(r1, MemOperand(sp, 0));
2987 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002988 EmitKeyedPropertyLoad(prop);
2989 }
2990 }
2991
2992 // Call ToNumber only if operand is not a smi.
2993 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002994 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002995 __ push(r0);
2996 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2997 __ bind(&no_conversion);
2998
2999 // Save result for postfix expressions.
3000 if (expr->is_postfix()) {
3001 switch (context_) {
3002 case Expression::kUninitialized:
3003 UNREACHABLE();
3004 case Expression::kEffect:
3005 // Do not save result.
3006 break;
3007 case Expression::kValue:
3008 case Expression::kTest:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003009 // Save the result on the stack. If we have a named or keyed property
3010 // we store the result under the receiver that is currently on top
3011 // of the stack.
3012 switch (assign_type) {
3013 case VARIABLE:
3014 __ push(r0);
3015 break;
3016 case NAMED_PROPERTY:
3017 __ str(r0, MemOperand(sp, kPointerSize));
3018 break;
3019 case KEYED_PROPERTY:
3020 __ str(r0, MemOperand(sp, 2 * kPointerSize));
3021 break;
3022 }
3023 break;
3024 }
3025 }
3026
3027
3028 // Inline smi case if we are in a loop.
3029 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003030 int count_value = expr->op() == Token::INC ? 1 : -1;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003031 if (ShouldInlineSmiCase(expr->op())) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003032 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003033 __ b(vs, &stub_call);
3034 // We could eliminate this smi check if we split the code at
3035 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003036 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003037 __ bind(&stub_call);
3038 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003039 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003040 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003041 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00003042 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003043 __ CallStub(&stub);
3044 __ bind(&done);
3045
3046 // Store the value returned in r0.
3047 switch (assign_type) {
3048 case VARIABLE:
3049 if (expr->is_postfix()) {
3050 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003051 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003052 Expression::kEffect);
3053 // For all contexts except kEffect: We have the result on
3054 // top of the stack.
3055 if (context_ != Expression::kEffect) {
3056 ApplyTOS(context_);
3057 }
3058 } else {
3059 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003060 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003061 context_);
3062 }
3063 break;
3064 case NAMED_PROPERTY: {
3065 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003066 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003067 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003068 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003069 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003070 if (context_ != Expression::kEffect) {
3071 ApplyTOS(context_);
3072 }
3073 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00003074 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003075 }
3076 break;
3077 }
3078 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003079 __ pop(r1); // Key.
3080 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003081 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003082 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003083 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003084 if (context_ != Expression::kEffect) {
3085 ApplyTOS(context_);
3086 }
3087 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003088 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003089 }
3090 break;
3091 }
3092 }
3093}
3094
3095
ricow@chromium.org65fae842010-08-25 15:26:24 +00003096void FullCodeGenerator::VisitForTypeofValue(Expression* expr, Location where) {
3097 VariableProxy* proxy = expr->AsVariableProxy();
3098 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
3099 Comment cmnt(masm_, "Global variable");
3100 __ ldr(r0, CodeGenerator::GlobalObject());
3101 __ mov(r2, Operand(proxy->name()));
3102 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
3103 // Use a regular load, not a contextual load, to avoid a reference
3104 // error.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003105 EmitCallIC(ic, RelocInfo::CODE_TARGET);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003106 if (where == kStack) __ push(r0);
3107 } else if (proxy != NULL &&
3108 proxy->var()->slot() != NULL &&
3109 proxy->var()->slot()->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003110 Label done, slow;
3111
3112 // Generate code for loading from variables potentially shadowed
3113 // by eval-introduced variables.
3114 Slot* slot = proxy->var()->slot();
3115 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
3116
3117 __ bind(&slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003118 __ mov(r0, Operand(proxy->name()));
3119 __ Push(cp, r0);
3120 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003121 __ bind(&done);
3122
ricow@chromium.org65fae842010-08-25 15:26:24 +00003123 if (where == kStack) __ push(r0);
3124 } else {
3125 // This expression cannot throw a reference error at the top level.
3126 VisitForValue(expr, where);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003127 }
3128}
3129
3130
ricow@chromium.org65fae842010-08-25 15:26:24 +00003131bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
3132 Expression* left,
3133 Expression* right,
3134 Label* if_true,
3135 Label* if_false,
3136 Label* fall_through) {
3137 if (op != Token::EQ && op != Token::EQ_STRICT) return false;
3138
3139 // Check for the pattern: typeof <expression> == <string literal>.
3140 Literal* right_literal = right->AsLiteral();
3141 if (right_literal == NULL) return false;
3142 Handle<Object> right_literal_value = right_literal->handle();
3143 if (!right_literal_value->IsString()) return false;
3144 UnaryOperation* left_unary = left->AsUnaryOperation();
3145 if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
3146 Handle<String> check = Handle<String>::cast(right_literal_value);
3147
3148 VisitForTypeofValue(left_unary->expression(), kAccumulator);
3149 if (check->Equals(Heap::number_symbol())) {
3150 __ tst(r0, Operand(kSmiTagMask));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003151 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003152 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3153 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
3154 __ cmp(r0, ip);
3155 Split(eq, if_true, if_false, fall_through);
3156 } else if (check->Equals(Heap::string_symbol())) {
3157 __ tst(r0, Operand(kSmiTagMask));
3158 __ b(eq, if_false);
3159 // Check for undetectable objects => false.
3160 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3161 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
3162 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3163 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3164 __ b(eq, if_false);
3165 __ ldrb(r1, FieldMemOperand(r0, Map::kInstanceTypeOffset));
3166 __ cmp(r1, Operand(FIRST_NONSTRING_TYPE));
3167 Split(lt, if_true, if_false, fall_through);
3168 } else if (check->Equals(Heap::boolean_symbol())) {
3169 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3170 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003171 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003172 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
3173 __ cmp(r0, ip);
3174 Split(eq, if_true, if_false, fall_through);
3175 } else if (check->Equals(Heap::undefined_symbol())) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003176 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003177 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003178 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003179 __ tst(r0, Operand(kSmiTagMask));
3180 __ b(eq, if_false);
3181 // Check for undetectable objects => true.
3182 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3183 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
3184 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3185 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3186 Split(eq, if_true, if_false, fall_through);
3187 } else if (check->Equals(Heap::function_symbol())) {
3188 __ tst(r0, Operand(kSmiTagMask));
3189 __ b(eq, if_false);
3190 __ CompareObjectType(r0, r1, r0, JS_FUNCTION_TYPE);
3191 __ b(eq, if_true);
3192 // Regular expressions => 'function' (they are callable).
3193 __ CompareInstanceType(r1, r0, JS_REGEXP_TYPE);
3194 Split(eq, if_true, if_false, fall_through);
3195 } else if (check->Equals(Heap::object_symbol())) {
3196 __ tst(r0, Operand(kSmiTagMask));
3197 __ b(eq, if_false);
3198 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3199 __ cmp(r0, ip);
3200 __ b(eq, if_true);
3201 // Regular expressions => 'function', not 'object'.
3202 __ CompareObjectType(r0, r1, r0, JS_REGEXP_TYPE);
3203 __ b(eq, if_false);
3204 // Check for undetectable objects => false.
3205 __ ldrb(r0, FieldMemOperand(r1, Map::kBitFieldOffset));
3206 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3207 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
3208 __ b(eq, if_false);
3209 // Check for JS objects => true.
3210 __ ldrb(r0, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3211 __ cmp(r0, Operand(FIRST_JS_OBJECT_TYPE));
3212 __ b(lt, if_false);
3213 __ cmp(r0, Operand(LAST_JS_OBJECT_TYPE));
3214 Split(le, if_true, if_false, fall_through);
3215 } else {
3216 if (if_false != fall_through) __ jmp(if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003217 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003218
3219 return true;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003220}
3221
3222
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003223void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
3224 Comment cmnt(masm_, "[ CompareOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00003225 SetSourcePosition(expr->position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003226
3227 // Always perform the comparison for its control flow. Pack the result
3228 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003229
3230 Label materialize_true, materialize_false;
3231 Label* if_true = NULL;
3232 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003233 Label* fall_through = NULL;
3234 PrepareTest(&materialize_true, &materialize_false,
3235 &if_true, &if_false, &fall_through);
3236
3237 // First we try a fast inlined version of the compare when one of
3238 // the operands is a literal.
3239 Token::Value op = expr->op();
3240 Expression* left = expr->left();
3241 Expression* right = expr->right();
3242 if (TryLiteralCompare(op, left, right, if_true, if_false, fall_through)) {
3243 Apply(context_, if_true, if_false);
3244 return;
3245 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003246
3247 VisitForValue(expr->left(), kStack);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003248 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003249 case Token::IN:
3250 VisitForValue(expr->right(), kStack);
3251 __ InvokeBuiltin(Builtins::IN, CALL_JS);
3252 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3253 __ cmp(r0, ip);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003254 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003255 break;
3256
3257 case Token::INSTANCEOF: {
3258 VisitForValue(expr->right(), kStack);
3259 InstanceofStub stub;
3260 __ CallStub(&stub);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003261 // The stub returns 0 for true.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003262 __ tst(r0, r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003263 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003264 break;
3265 }
3266
3267 default: {
3268 VisitForValue(expr->right(), kAccumulator);
3269 Condition cc = eq;
3270 bool strict = false;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003271 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003272 case Token::EQ_STRICT:
3273 strict = true;
3274 // Fall through
ricow@chromium.org65fae842010-08-25 15:26:24 +00003275 case Token::EQ:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003276 cc = eq;
3277 __ pop(r1);
3278 break;
3279 case Token::LT:
3280 cc = lt;
3281 __ pop(r1);
3282 break;
3283 case Token::GT:
3284 // Reverse left and right sides to obtain ECMA-262 conversion order.
3285 cc = lt;
3286 __ mov(r1, result_register());
3287 __ pop(r0);
3288 break;
3289 case Token::LTE:
3290 // Reverse left and right sides to obtain ECMA-262 conversion order.
3291 cc = ge;
3292 __ mov(r1, result_register());
3293 __ pop(r0);
3294 break;
3295 case Token::GTE:
3296 cc = ge;
3297 __ pop(r1);
3298 break;
3299 case Token::IN:
3300 case Token::INSTANCEOF:
3301 default:
3302 UNREACHABLE();
3303 }
3304
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003305 bool inline_smi_code = ShouldInlineSmiCase(op);
3306 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003307 Label slow_case;
3308 __ orr(r2, r0, Operand(r1));
3309 __ BranchOnNotSmi(r2, &slow_case);
3310 __ cmp(r1, r0);
3311 Split(cc, if_true, if_false, NULL);
3312 __ bind(&slow_case);
3313 }
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003314 CompareFlags flags = inline_smi_code
3315 ? NO_SMI_COMPARE_IN_STUB
3316 : NO_COMPARE_FLAGS;
3317 CompareStub stub(cc, strict, flags, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003318 __ CallStub(&stub);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00003319 __ cmp(r0, Operand(0, RelocInfo::NONE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003320 Split(cc, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003321 }
3322 }
3323
3324 // Convert the result of the comparison into one expected for this
3325 // expression's context.
3326 Apply(context_, if_true, if_false);
3327}
3328
3329
ricow@chromium.org65fae842010-08-25 15:26:24 +00003330void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
3331 Comment cmnt(masm_, "[ CompareToNull");
3332 Label materialize_true, materialize_false;
3333 Label* if_true = NULL;
3334 Label* if_false = NULL;
3335 Label* fall_through = NULL;
3336 PrepareTest(&materialize_true, &materialize_false,
3337 &if_true, &if_false, &fall_through);
3338
3339 VisitForValue(expr->expression(), kAccumulator);
3340 __ LoadRoot(r1, Heap::kNullValueRootIndex);
3341 __ cmp(r0, r1);
3342 if (expr->is_strict()) {
3343 Split(eq, if_true, if_false, fall_through);
3344 } else {
3345 __ b(eq, if_true);
3346 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
3347 __ cmp(r0, r1);
3348 __ b(eq, if_true);
3349 __ tst(r0, Operand(kSmiTagMask));
3350 __ b(eq, if_false);
3351 // It can be an undetectable object.
3352 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3353 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3354 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3355 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3356 Split(eq, if_true, if_false, fall_through);
3357 }
3358 Apply(context_, if_true, if_false);
3359}
3360
3361
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003362void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3363 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3364 Apply(context_, r0);
3365}
3366
3367
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003368Register FullCodeGenerator::result_register() {
3369 return r0;
3370}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003371
3372
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003373Register FullCodeGenerator::context_register() {
3374 return cp;
3375}
3376
3377
3378void FullCodeGenerator::EmitCallIC(Handle<Code> ic, RelocInfo::Mode mode) {
3379 ASSERT(mode == RelocInfo::CODE_TARGET ||
3380 mode == RelocInfo::CODE_TARGET_CONTEXT);
3381 __ Call(ic, mode);
3382}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003383
3384
3385void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3386 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3387 __ str(value, MemOperand(fp, frame_offset));
3388}
3389
3390
3391void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003392 __ ldr(dst, ContextOperand(cp, context_index));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003393}
3394
3395
3396// ----------------------------------------------------------------------------
3397// Non-local control flow support.
3398
3399void FullCodeGenerator::EnterFinallyBlock() {
3400 ASSERT(!result_register().is(r1));
3401 // Store result register while executing finally block.
3402 __ push(result_register());
3403 // Cook return address in link register to stack (smi encoded Code* delta)
3404 __ sub(r1, lr, Operand(masm_->CodeObject()));
3405 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3406 ASSERT_EQ(0, kSmiTag);
3407 __ add(r1, r1, Operand(r1)); // Convert to smi.
3408 __ push(r1);
3409}
3410
3411
3412void FullCodeGenerator::ExitFinallyBlock() {
3413 ASSERT(!result_register().is(r1));
3414 // Restore result register from stack.
3415 __ pop(r1);
3416 // Uncook return address and return.
3417 __ pop(result_register());
3418 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3419 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3420 __ add(pc, r1, Operand(masm_->CodeObject()));
3421}
3422
3423
3424#undef __
3425
3426} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003427
3428#endif // V8_TARGET_ARCH_ARM