blob: 0f8f6d4124a38c76da332348d2e42f5a5edb90a5 [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));
623 __ Call(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.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000675 if (ShouldInlineSmiCase(Token::EQ_STRICT)) {
676 Label slow_case;
677 __ orr(r2, r1, r0);
678 __ tst(r2, Operand(kSmiTagMask));
679 __ b(ne, &slow_case);
680 __ cmp(r1, r0);
681 __ b(ne, &next_test);
682 __ Drop(1); // Switch value is no longer needed.
683 __ b(clause->body_target()->entry_label());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000684 __ bind(&slow_case);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000685 }
686
ager@chromium.orgb5737492010-07-15 09:29:43 +0000687 CompareStub stub(eq, true, kBothCouldBeNaN, true, r1, r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000688 __ CallStub(&stub);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000689 __ cmp(r0, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000690 __ b(ne, &next_test);
691 __ Drop(1); // Switch value is no longer needed.
692 __ b(clause->body_target()->entry_label());
693 }
694
695 // Discard the test value and jump to the default if present, otherwise to
696 // the end of the statement.
697 __ bind(&next_test);
698 __ Drop(1); // Switch value is no longer needed.
699 if (default_clause == NULL) {
700 __ b(nested_statement.break_target());
701 } else {
702 __ b(default_clause->body_target()->entry_label());
703 }
704
705 // Compile all the case bodies.
706 for (int i = 0; i < clauses->length(); i++) {
707 Comment cmnt(masm_, "[ Case body");
708 CaseClause* clause = clauses->at(i);
709 __ bind(clause->body_target()->entry_label());
710 VisitStatements(clause->statements());
711 }
712
713 __ bind(nested_statement.break_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000714}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000715
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000716
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000717void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000718 Comment cmnt(masm_, "[ ForInStatement");
719 SetStatementPosition(stmt);
720
721 Label loop, exit;
722 ForIn loop_statement(this, stmt);
723 increment_loop_depth();
724
725 // Get the object to enumerate over. Both SpiderMonkey and JSC
726 // ignore null and undefined in contrast to the specification; see
727 // ECMA-262 section 12.6.4.
728 VisitForValue(stmt->enumerable(), kAccumulator);
729 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
730 __ cmp(r0, ip);
731 __ b(eq, &exit);
732 __ LoadRoot(ip, Heap::kNullValueRootIndex);
733 __ cmp(r0, ip);
734 __ b(eq, &exit);
735
736 // Convert the object to a JS object.
737 Label convert, done_convert;
738 __ BranchOnSmi(r0, &convert);
739 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
740 __ b(hs, &done_convert);
741 __ bind(&convert);
742 __ push(r0);
743 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
744 __ bind(&done_convert);
745 __ push(r0);
746
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000747 // BUG(867): Check cache validity in generated code. This is a fast
748 // case for the JSObject::IsSimpleEnum cache validity checks. If we
749 // cannot guarantee cache validity, call the runtime system to check
750 // cache validity or get the property names in a fixed array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000751
752 // Get the set of properties to enumerate.
753 __ push(r0); // Duplicate the enumerable object on the stack.
754 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
755
756 // If we got a map from the runtime call, we can do a fast
757 // modification check. Otherwise, we got a fixed array, and we have
758 // to do a slow check.
759 Label fixed_array;
760 __ mov(r2, r0);
761 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
762 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
763 __ cmp(r1, ip);
764 __ b(ne, &fixed_array);
765
766 // We got a map in register r0. Get the enumeration cache from it.
767 __ ldr(r1, FieldMemOperand(r0, Map::kInstanceDescriptorsOffset));
768 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
769 __ ldr(r2, FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
770
771 // Setup the four remaining stack slots.
772 __ push(r0); // Map.
773 __ ldr(r1, FieldMemOperand(r2, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000774 __ mov(r0, Operand(Smi::FromInt(0)));
775 // Push enumeration cache, enumeration cache length (as smi) and zero.
776 __ Push(r2, r1, r0);
777 __ jmp(&loop);
778
779 // We got a fixed array in register r0. Iterate through that.
780 __ bind(&fixed_array);
781 __ mov(r1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
782 __ Push(r1, r0);
783 __ ldr(r1, FieldMemOperand(r0, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000784 __ mov(r0, Operand(Smi::FromInt(0)));
785 __ Push(r1, r0); // Fixed array length (as smi) and initial index.
786
787 // Generate code for doing the condition check.
788 __ bind(&loop);
789 // Load the current count to r0, load the length to r1.
790 __ Ldrd(r0, r1, MemOperand(sp, 0 * kPointerSize));
791 __ cmp(r0, r1); // Compare to the array length.
792 __ b(hs, loop_statement.break_target());
793
794 // Get the current entry of the array into register r3.
795 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
796 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
797 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
798
799 // Get the expected map from the stack or a zero map in the
800 // permanent slow case into register r2.
801 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
802
803 // Check if the expected map still matches that of the enumerable.
804 // If not, we have to filter the key.
805 Label update_each;
806 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
807 __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
808 __ cmp(r4, Operand(r2));
809 __ b(eq, &update_each);
810
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000811 // Convert the entry to a string or (smi) 0 if it isn't a property
812 // any more. If the property has been removed while iterating, we
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000813 // just skip it.
814 __ push(r1); // Enumerable.
815 __ push(r3); // Current entry.
816 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000817 __ mov(r3, Operand(r0), SetCC);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000818 __ b(eq, loop_statement.continue_target());
819
820 // Update the 'each' property or variable from the possibly filtered
821 // entry in register r3.
822 __ bind(&update_each);
823 __ mov(result_register(), r3);
824 // Perform the assignment as if via '='.
825 EmitAssignment(stmt->each());
826
827 // Generate code for the body of the loop.
828 Label stack_limit_hit, stack_check_done;
829 Visit(stmt->body());
830
831 __ StackLimitCheck(&stack_limit_hit);
832 __ bind(&stack_check_done);
833
834 // Generate code for the going to the next element by incrementing
835 // the index (smi) stored on top of the stack.
836 __ bind(loop_statement.continue_target());
837 __ pop(r0);
838 __ add(r0, r0, Operand(Smi::FromInt(1)));
839 __ push(r0);
840 __ b(&loop);
841
842 // Slow case for the stack limit check.
843 StackCheckStub stack_check_stub;
844 __ bind(&stack_limit_hit);
845 __ CallStub(&stack_check_stub);
846 __ b(&stack_check_done);
847
848 // Remove the pointers stored on the stack.
849 __ bind(loop_statement.break_target());
850 __ Drop(5);
851
852 // Exit and decrement the loop depth.
853 __ bind(&exit);
854 decrement_loop_depth();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000855}
856
857
858void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info) {
859 // Use the fast case closure allocation code that allocates in new
860 // space for nested functions that don't need literals cloning.
861 if (scope()->is_function_scope() && info->num_literals() == 0) {
862 FastNewClosureStub stub;
863 __ mov(r0, Operand(info));
864 __ push(r0);
865 __ CallStub(&stub);
866 } else {
867 __ mov(r0, Operand(info));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000868 __ Push(cp, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000869 __ CallRuntime(Runtime::kNewClosure, 2);
870 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000871 Apply(context_, r0);
872}
873
874
875void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
876 Comment cmnt(masm_, "[ VariableProxy");
877 EmitVariableLoad(expr->var(), context_);
878}
879
880
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000881MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(
882 Slot* slot,
883 Label* slow) {
884 ASSERT(slot->type() == Slot::CONTEXT);
885 Register current = cp;
886 Register next = r3;
887 Register temp = r4;
888
889 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
890 if (s->num_heap_slots() > 0) {
891 if (s->calls_eval()) {
892 // Check that extension is NULL.
893 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
894 __ tst(temp, temp);
895 __ b(ne, slow);
896 }
897 __ ldr(next, ContextOperand(current, Context::CLOSURE_INDEX));
898 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
899 // Walk the rest of the chain without clobbering cp.
900 current = next;
901 }
902 }
903 // Check that last extension is NULL.
904 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
905 __ tst(temp, temp);
906 __ b(ne, slow);
907 __ ldr(temp, ContextOperand(current, Context::FCONTEXT_INDEX));
908 return ContextOperand(temp, slot->index());
909}
910
911
912void FullCodeGenerator::EmitDynamicLoadFromSlotFastCase(
913 Slot* slot,
914 TypeofState typeof_state,
915 Label* slow,
916 Label* done) {
917 // Generate fast-case code for variables that might be shadowed by
918 // eval-introduced variables. Eval is used a lot without
919 // introducing variables. In those cases, we do not want to
920 // perform a runtime call for all variables in the scope
921 // containing the eval.
922 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
923 EmitLoadGlobalSlotCheckExtensions(slot, typeof_state, slow);
924 __ jmp(done);
925 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
926 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
927 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
928 if (potential_slot != NULL) {
929 // Generate fast case for locals that rewrite to slots.
930 __ ldr(r0, ContextSlotOperandCheckExtensions(potential_slot, slow));
931 if (potential_slot->var()->mode() == Variable::CONST) {
932 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
933 __ cmp(r0, ip);
934 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
935 }
936 __ jmp(done);
937 } else if (rewrite != NULL) {
938 // Generate fast case for calls of an argument function.
939 Property* property = rewrite->AsProperty();
940 if (property != NULL) {
941 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
942 Literal* key_literal = property->key()->AsLiteral();
943 if (obj_proxy != NULL &&
944 key_literal != NULL &&
945 obj_proxy->IsArguments() &&
946 key_literal->handle()->IsSmi()) {
947 // Load arguments object if there are no eval-introduced
948 // variables. Then load the argument from the arguments
949 // object using keyed load.
950 __ ldr(r1,
951 ContextSlotOperandCheckExtensions(obj_proxy->var()->slot(),
952 slow));
953 __ mov(r0, Operand(key_literal->handle()));
954 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
955 __ Call(ic, RelocInfo::CODE_TARGET);
956 __ jmp(done);
957 }
958 }
959 }
960 }
961}
962
963
964void FullCodeGenerator::EmitLoadGlobalSlotCheckExtensions(
965 Slot* slot,
966 TypeofState typeof_state,
967 Label* slow) {
968 Register current = cp;
969 Register next = r1;
970 Register temp = r2;
971
972 Scope* s = scope();
973 while (s != NULL) {
974 if (s->num_heap_slots() > 0) {
975 if (s->calls_eval()) {
976 // Check that extension is NULL.
977 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
978 __ tst(temp, temp);
979 __ b(ne, slow);
980 }
981 // Load next context in chain.
982 __ ldr(next, ContextOperand(current, Context::CLOSURE_INDEX));
983 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
984 // Walk the rest of the chain without clobbering cp.
985 current = next;
986 }
987 // If no outer scope calls eval, we do not need to check more
988 // context extensions.
989 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
990 s = s->outer_scope();
991 }
992
993 if (s->is_eval_scope()) {
994 Label loop, fast;
995 if (!current.is(next)) {
996 __ Move(next, current);
997 }
998 __ bind(&loop);
999 // Terminate at global context.
1000 __ ldr(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1001 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1002 __ cmp(temp, ip);
1003 __ b(eq, &fast);
1004 // Check that extension is NULL.
1005 __ ldr(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1006 __ tst(temp, temp);
1007 __ b(ne, slow);
1008 // Load next context in chain.
1009 __ ldr(next, ContextOperand(next, Context::CLOSURE_INDEX));
1010 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
1011 __ b(&loop);
1012 __ bind(&fast);
1013 }
1014
1015 __ ldr(r0, CodeGenerator::GlobalObject());
1016 __ mov(r2, Operand(slot->var()->name()));
1017 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1018 ? RelocInfo::CODE_TARGET
1019 : RelocInfo::CODE_TARGET_CONTEXT;
1020 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1021 __ Call(ic, mode);
1022}
1023
1024
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001025void FullCodeGenerator::EmitVariableLoad(Variable* var,
1026 Expression::Context context) {
1027 // Four cases: non-this global variables, lookup slots, all other
1028 // types of slots, and parameters that rewrite to explicit property
1029 // accesses on the arguments object.
1030 Slot* slot = var->slot();
1031 Property* property = var->AsProperty();
1032
1033 if (var->is_global() && !var->is_this()) {
1034 Comment cmnt(masm_, "Global variable");
1035 // Use inline caching. Variable name is passed in r2 and the global
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001036 // object (receiver) in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001037 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001038 __ mov(r2, Operand(var->name()));
1039 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1040 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001041 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001042
1043 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001044 Label done, slow;
1045
1046 // Generate code for loading from variables potentially shadowed
1047 // by eval-introduced variables.
1048 EmitDynamicLoadFromSlotFastCase(slot, NOT_INSIDE_TYPEOF, &slow, &done);
1049
1050 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001051 Comment cmnt(masm_, "Lookup slot");
1052 __ mov(r1, Operand(var->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001053 __ Push(cp, r1); // Context and name.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001054 __ CallRuntime(Runtime::kLoadContextSlot, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001055 __ bind(&done);
1056
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001057 Apply(context, r0);
1058
1059 } else if (slot != NULL) {
1060 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1061 ? "Context slot"
1062 : "Stack slot");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001063 if (var->mode() == Variable::CONST) {
1064 // Constants may be the hole value if they have not been initialized.
1065 // Unhole them.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001066 MemOperand slot_operand = EmitSlotSearch(slot, r0);
1067 __ ldr(r0, slot_operand);
1068 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1069 __ cmp(r0, ip);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001070 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001071 Apply(context, r0);
1072 } else {
1073 Apply(context, slot);
1074 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001075 } else {
1076 Comment cmnt(masm_, "Rewritten parameter");
1077 ASSERT_NOT_NULL(property);
1078 // Rewritten parameter accesses are of the form "slot[literal]".
1079
1080 // Assert that the object is in a slot.
1081 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
1082 ASSERT_NOT_NULL(object_var);
1083 Slot* object_slot = object_var->slot();
1084 ASSERT_NOT_NULL(object_slot);
1085
1086 // Load the object.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001087 Move(r1, object_slot);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001088
1089 // Assert that the key is a smi.
1090 Literal* key_literal = property->key()->AsLiteral();
1091 ASSERT_NOT_NULL(key_literal);
1092 ASSERT(key_literal->handle()->IsSmi());
1093
1094 // Load the key.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001095 __ mov(r0, Operand(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001096
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001097 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001098 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1099 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001100 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001101 }
1102}
1103
1104
1105void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1106 Comment cmnt(masm_, "[ RegExpLiteral");
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001107 Label materialized;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001108 // Registers will be used as follows:
1109 // r4 = JS function, literals array
1110 // r3 = literal index
1111 // r2 = RegExp pattern
1112 // r1 = RegExp flags
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001113 // r0 = temp + materialized value (RegExp literal)
ricow@chromium.org65fae842010-08-25 15:26:24 +00001114 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1115 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001116 int literal_offset =
ricow@chromium.org65fae842010-08-25 15:26:24 +00001117 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001118 __ ldr(r0, FieldMemOperand(r4, literal_offset));
1119 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1120 __ cmp(r0, ip);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001121 __ b(ne, &materialized);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001122
1123 // Create regexp literal using runtime function.
1124 // Result will be in r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001125 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
1126 __ mov(r2, Operand(expr->pattern()));
1127 __ mov(r1, Operand(expr->flags()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001128 __ Push(r4, r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001129 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001130
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001131 __ bind(&materialized);
1132 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1133 __ push(r0);
1134 __ mov(r0, Operand(Smi::FromInt(size)));
1135 __ push(r0);
1136 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001137
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001138 // After this, registers are used as follows:
1139 // r0: Newly allocated regexp.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001140 // r1: Materialized regexp.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001141 // r2: temp.
1142 __ pop(r1);
1143 __ CopyFields(r0, r1, r2.bit(), size / kPointerSize);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001144 Apply(context_, r0);
1145}
1146
1147
1148void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1149 Comment cmnt(masm_, "[ ObjectLiteral");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001150 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1151 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1152 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1153 __ mov(r1, Operand(expr->constant_properties()));
1154 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001155 __ Push(r3, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001156 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001157 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001158 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001159 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001160 }
1161
1162 // If result_saved is true the result is on top of the stack. If
1163 // result_saved is false the result is in r0.
1164 bool result_saved = false;
1165
1166 for (int i = 0; i < expr->properties()->length(); i++) {
1167 ObjectLiteral::Property* property = expr->properties()->at(i);
1168 if (property->IsCompileTimeValue()) continue;
1169
1170 Literal* key = property->key();
1171 Expression* value = property->value();
1172 if (!result_saved) {
1173 __ push(r0); // Save result on stack
1174 result_saved = true;
1175 }
1176 switch (property->kind()) {
1177 case ObjectLiteral::Property::CONSTANT:
1178 UNREACHABLE();
1179 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1180 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1181 // Fall through.
1182 case ObjectLiteral::Property::COMPUTED:
1183 if (key->handle()->IsSymbol()) {
1184 VisitForValue(value, kAccumulator);
1185 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001186 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001187 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1188 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001189 break;
1190 }
1191 // Fall through.
1192 case ObjectLiteral::Property::PROTOTYPE:
1193 // Duplicate receiver on stack.
1194 __ ldr(r0, MemOperand(sp));
1195 __ push(r0);
1196 VisitForValue(key, kStack);
1197 VisitForValue(value, kStack);
1198 __ CallRuntime(Runtime::kSetProperty, 3);
1199 break;
1200 case ObjectLiteral::Property::GETTER:
1201 case ObjectLiteral::Property::SETTER:
1202 // Duplicate receiver on stack.
1203 __ ldr(r0, MemOperand(sp));
1204 __ push(r0);
1205 VisitForValue(key, kStack);
1206 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1207 Smi::FromInt(1) :
1208 Smi::FromInt(0)));
1209 __ push(r1);
1210 VisitForValue(value, kStack);
1211 __ CallRuntime(Runtime::kDefineAccessor, 4);
1212 break;
1213 }
1214 }
1215
1216 if (result_saved) {
1217 ApplyTOS(context_);
1218 } else {
1219 Apply(context_, r0);
1220 }
1221}
1222
1223
1224void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1225 Comment cmnt(masm_, "[ ArrayLiteral");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001226
1227 ZoneList<Expression*>* subexprs = expr->values();
1228 int length = subexprs->length();
1229
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001230 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1231 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1232 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1233 __ mov(r1, Operand(expr->constant_elements()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001234 __ Push(r3, r2, r1);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001235 if (expr->constant_elements()->map() == Heap::fixed_cow_array_map()) {
1236 FastCloneShallowArrayStub stub(
1237 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1238 __ CallStub(&stub);
1239 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
1240 } else if (expr->depth() > 1) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001241 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001242 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001243 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001244 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001245 FastCloneShallowArrayStub stub(
1246 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001247 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001248 }
1249
1250 bool result_saved = false; // Is the result saved to the stack?
1251
1252 // Emit code to evaluate all the non-constant subexpressions and to store
1253 // them into the newly cloned array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001254 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001255 Expression* subexpr = subexprs->at(i);
1256 // If the subexpression is a literal or a simple materialized literal it
1257 // is already set in the cloned array.
1258 if (subexpr->AsLiteral() != NULL ||
1259 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1260 continue;
1261 }
1262
1263 if (!result_saved) {
1264 __ push(r0);
1265 result_saved = true;
1266 }
1267 VisitForValue(subexpr, kAccumulator);
1268
1269 // Store the subexpression value in the array's elements.
1270 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
1271 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1272 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1273 __ str(result_register(), FieldMemOperand(r1, offset));
1274
1275 // Update the write barrier for the array store with r0 as the scratch
1276 // register.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001277 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001278 }
1279
1280 if (result_saved) {
1281 ApplyTOS(context_);
1282 } else {
1283 Apply(context_, r0);
1284 }
1285}
1286
1287
ager@chromium.org5c838252010-02-19 08:53:10 +00001288void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1289 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001290 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1291 // on the left-hand side.
1292 if (!expr->target()->IsValidLeftHandSide()) {
1293 VisitForEffect(expr->target());
1294 return;
1295 }
1296
ager@chromium.org5c838252010-02-19 08:53:10 +00001297 // Left-hand side can only be a property, a global or a (parameter or local)
1298 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1299 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1300 LhsKind assign_type = VARIABLE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001301 Property* property = expr->target()->AsProperty();
1302 if (property != NULL) {
1303 assign_type = (property->key()->IsPropertyName())
1304 ? NAMED_PROPERTY
1305 : KEYED_PROPERTY;
ager@chromium.org5c838252010-02-19 08:53:10 +00001306 }
1307
1308 // Evaluate LHS expression.
1309 switch (assign_type) {
1310 case VARIABLE:
1311 // Nothing to do here.
1312 break;
1313 case NAMED_PROPERTY:
1314 if (expr->is_compound()) {
1315 // We need the receiver both on the stack and in the accumulator.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001316 VisitForValue(property->obj(), kAccumulator);
ager@chromium.org5c838252010-02-19 08:53:10 +00001317 __ push(result_register());
1318 } else {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001319 VisitForValue(property->obj(), kStack);
ager@chromium.org5c838252010-02-19 08:53:10 +00001320 }
1321 break;
1322 case KEYED_PROPERTY:
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001323 if (expr->is_compound()) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001324 VisitForValue(property->obj(), kStack);
1325 VisitForValue(property->key(), kAccumulator);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001326 __ ldr(r1, MemOperand(sp, 0));
1327 __ push(r0);
1328 } else {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001329 VisitForValue(property->obj(), kStack);
1330 VisitForValue(property->key(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001331 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001332 break;
1333 }
1334
ager@chromium.org5c838252010-02-19 08:53:10 +00001335 if (expr->is_compound()) {
1336 Location saved_location = location_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001337 location_ = kAccumulator;
ager@chromium.org5c838252010-02-19 08:53:10 +00001338 switch (assign_type) {
1339 case VARIABLE:
1340 EmitVariableLoad(expr->target()->AsVariableProxy()->var(),
1341 Expression::kValue);
1342 break;
1343 case NAMED_PROPERTY:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001344 EmitNamedPropertyLoad(property);
ager@chromium.org5c838252010-02-19 08:53:10 +00001345 break;
1346 case KEYED_PROPERTY:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001347 EmitKeyedPropertyLoad(property);
ager@chromium.org5c838252010-02-19 08:53:10 +00001348 break;
1349 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001350
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001351 Token::Value op = expr->binary_op();
1352 ConstantOperand constant = ShouldInlineSmiCase(op)
1353 ? GetConstantOperand(op, expr->target(), expr->value())
1354 : kNoConstants;
1355 ASSERT(constant == kRightConstant || constant == kNoConstants);
1356 if (constant == kNoConstants) {
1357 __ push(r0); // Left operand goes on the stack.
1358 VisitForValue(expr->value(), kAccumulator);
1359 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001360
ricow@chromium.org65fae842010-08-25 15:26:24 +00001361 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1362 ? OVERWRITE_RIGHT
1363 : NO_OVERWRITE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001364 SetSourcePosition(expr->position() + 1);
1365 if (ShouldInlineSmiCase(op)) {
1366 EmitInlineSmiBinaryOp(expr,
1367 op,
1368 Expression::kValue,
1369 mode,
1370 expr->target(),
1371 expr->value(),
1372 constant);
1373 } else {
1374 EmitBinaryOp(op, Expression::kValue, mode);
1375 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001376 location_ = saved_location;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001377
1378 } else {
1379 VisitForValue(expr->value(), kAccumulator);
ager@chromium.org5c838252010-02-19 08:53:10 +00001380 }
1381
1382 // Record source position before possible IC call.
1383 SetSourcePosition(expr->position());
1384
1385 // Store the value.
1386 switch (assign_type) {
1387 case VARIABLE:
1388 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001389 expr->op(),
ager@chromium.org5c838252010-02-19 08:53:10 +00001390 context_);
1391 break;
1392 case NAMED_PROPERTY:
1393 EmitNamedPropertyAssignment(expr);
1394 break;
1395 case KEYED_PROPERTY:
1396 EmitKeyedPropertyAssignment(expr);
1397 break;
1398 }
1399}
1400
1401
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001402void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1403 SetSourcePosition(prop->position());
1404 Literal* key = prop->key()->AsLiteral();
1405 __ mov(r2, Operand(key->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001406 // Call load IC. It has arguments receiver and property name r0 and r2.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001407 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1408 __ Call(ic, RelocInfo::CODE_TARGET);
1409}
1410
1411
1412void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1413 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001414 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001415 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1416 __ Call(ic, RelocInfo::CODE_TARGET);
1417}
1418
1419
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001420void FullCodeGenerator::EmitInlineSmiBinaryOp(Expression* expr,
1421 Token::Value op,
1422 Expression::Context context,
1423 OverwriteMode mode,
1424 Expression* left,
1425 Expression* right,
1426 ConstantOperand constant) {
1427 ASSERT(constant == kNoConstants); // Only handled case.
1428 EmitBinaryOp(op, context, mode);
1429}
1430
1431
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001432void FullCodeGenerator::EmitBinaryOp(Token::Value op,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001433 Expression::Context context,
1434 OverwriteMode mode) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001435 __ pop(r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001436 GenericBinaryOpStub stub(op, mode, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001437 __ CallStub(&stub);
1438 Apply(context, r0);
1439}
1440
1441
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001442void FullCodeGenerator::EmitAssignment(Expression* expr) {
1443 // Invalid left-hand sides are rewritten to have a 'throw
1444 // ReferenceError' on the left-hand side.
1445 if (!expr->IsValidLeftHandSide()) {
1446 VisitForEffect(expr);
1447 return;
1448 }
1449
1450 // Left-hand side can only be a property, a global or a (parameter or local)
1451 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1452 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1453 LhsKind assign_type = VARIABLE;
1454 Property* prop = expr->AsProperty();
1455 if (prop != NULL) {
1456 assign_type = (prop->key()->IsPropertyName())
1457 ? NAMED_PROPERTY
1458 : KEYED_PROPERTY;
1459 }
1460
1461 switch (assign_type) {
1462 case VARIABLE: {
1463 Variable* var = expr->AsVariableProxy()->var();
1464 EmitVariableAssignment(var, Token::ASSIGN, Expression::kEffect);
1465 break;
1466 }
1467 case NAMED_PROPERTY: {
1468 __ push(r0); // Preserve value.
1469 VisitForValue(prop->obj(), kAccumulator);
1470 __ mov(r1, r0);
1471 __ pop(r0); // Restore value.
1472 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
1473 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1474 __ Call(ic, RelocInfo::CODE_TARGET);
1475 break;
1476 }
1477 case KEYED_PROPERTY: {
1478 __ push(r0); // Preserve value.
1479 VisitForValue(prop->obj(), kStack);
1480 VisitForValue(prop->key(), kAccumulator);
1481 __ mov(r1, r0);
1482 __ pop(r2);
1483 __ pop(r0); // Restore value.
1484 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1485 __ Call(ic, RelocInfo::CODE_TARGET);
1486 break;
1487 }
1488 }
1489}
1490
1491
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001492void FullCodeGenerator::EmitVariableAssignment(Variable* var,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001493 Token::Value op,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001494 Expression::Context context) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001495 // Left-hand sides that rewrite to explicit property accesses do not reach
1496 // here.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001497 ASSERT(var != NULL);
1498 ASSERT(var->is_global() || var->slot() != NULL);
1499
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001500 if (var->is_global()) {
1501 ASSERT(!var->is_this());
1502 // Assignment to a global variable. Use inline caching for the
1503 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001504 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001505 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001506 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001507 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1508 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001509
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001510 } else if (var->mode() != Variable::CONST || op == Token::INIT_CONST) {
1511 // Perform the assignment for non-const variables and for initialization
1512 // of const variables. Const assignments are simply skipped.
1513 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001514 Slot* slot = var->slot();
1515 switch (slot->type()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001516 case Slot::PARAMETER:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001517 case Slot::LOCAL:
1518 if (op == Token::INIT_CONST) {
1519 // Detect const reinitialization by checking for the hole value.
1520 __ ldr(r1, MemOperand(fp, SlotOffset(slot)));
1521 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1522 __ cmp(r1, ip);
1523 __ b(ne, &done);
1524 }
1525 // Perform the assignment.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001526 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1527 break;
1528
1529 case Slot::CONTEXT: {
1530 MemOperand target = EmitSlotSearch(slot, r1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001531 if (op == Token::INIT_CONST) {
1532 // Detect const reinitialization by checking for the hole value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001533 __ ldr(r2, target);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001534 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001535 __ cmp(r2, ip);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001536 __ b(ne, &done);
1537 }
1538 // Perform the assignment and issue the write barrier.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001539 __ str(result_register(), target);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001540 // RecordWrite may destroy all its register arguments.
1541 __ mov(r3, result_register());
1542 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001543 __ RecordWrite(r1, Operand(offset), r2, r3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001544 break;
1545 }
1546
1547 case Slot::LOOKUP:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001548 // Call the runtime for the assignment. The runtime will ignore
1549 // const reinitialization.
1550 __ push(r0); // Value.
1551 __ mov(r0, Operand(slot->var()->name()));
1552 __ Push(cp, r0); // Context and name.
1553 if (op == Token::INIT_CONST) {
1554 // The runtime will ignore const redeclaration.
1555 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1556 } else {
1557 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1558 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001559 break;
1560 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001561 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001562 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001563
ager@chromium.org5c838252010-02-19 08:53:10 +00001564 Apply(context, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001565}
1566
1567
1568void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1569 // Assignment to a property, using a named store IC.
1570 Property* prop = expr->target()->AsProperty();
1571 ASSERT(prop != NULL);
1572 ASSERT(prop->key()->AsLiteral() != NULL);
1573
1574 // If the assignment starts a block of assignments to the same object,
1575 // change to slow case to avoid the quadratic behavior of repeatedly
1576 // adding fast properties.
1577 if (expr->starts_initialization_block()) {
1578 __ push(result_register());
1579 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1580 __ push(ip);
1581 __ CallRuntime(Runtime::kToSlowProperties, 1);
1582 __ pop(result_register());
1583 }
1584
1585 // Record source code position before IC call.
1586 SetSourcePosition(expr->position());
1587 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001588 // Load receiver to r1. Leave a copy in the stack if needed for turning the
1589 // receiver into fast case.
ager@chromium.org5c838252010-02-19 08:53:10 +00001590 if (expr->ends_initialization_block()) {
1591 __ ldr(r1, MemOperand(sp));
1592 } else {
1593 __ pop(r1);
1594 }
1595
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001596 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1597 __ Call(ic, RelocInfo::CODE_TARGET);
1598
1599 // If the assignment ends an initialization block, revert to fast case.
1600 if (expr->ends_initialization_block()) {
1601 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001602 // Receiver is under the result value.
1603 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001604 __ push(ip);
1605 __ CallRuntime(Runtime::kToFastProperties, 1);
1606 __ pop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001607 DropAndApply(1, context_, r0);
1608 } else {
1609 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001610 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001611}
1612
1613
1614void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1615 // Assignment to a property, using a keyed store IC.
1616
1617 // If the assignment starts a block of assignments to the same object,
1618 // change to slow case to avoid the quadratic behavior of repeatedly
1619 // adding fast properties.
1620 if (expr->starts_initialization_block()) {
1621 __ push(result_register());
1622 // Receiver is now under the key and value.
1623 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1624 __ push(ip);
1625 __ CallRuntime(Runtime::kToSlowProperties, 1);
1626 __ pop(result_register());
1627 }
1628
1629 // Record source code position before IC call.
1630 SetSourcePosition(expr->position());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001631 __ pop(r1); // Key.
1632 // Load receiver to r2. Leave a copy in the stack if needed for turning the
1633 // receiver into fast case.
1634 if (expr->ends_initialization_block()) {
1635 __ ldr(r2, MemOperand(sp));
1636 } else {
1637 __ pop(r2);
1638 }
1639
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001640 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1641 __ Call(ic, RelocInfo::CODE_TARGET);
1642
1643 // If the assignment ends an initialization block, revert to fast case.
1644 if (expr->ends_initialization_block()) {
1645 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001646 // Receiver is under the result value.
1647 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001648 __ push(ip);
1649 __ CallRuntime(Runtime::kToFastProperties, 1);
1650 __ pop(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001651 DropAndApply(1, context_, r0);
1652 } else {
1653 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001654 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001655}
1656
1657
1658void FullCodeGenerator::VisitProperty(Property* expr) {
1659 Comment cmnt(masm_, "[ Property");
1660 Expression* key = expr->key();
1661
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001662 if (key->IsPropertyName()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001663 VisitForValue(expr->obj(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001664 EmitNamedPropertyLoad(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001665 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001666 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001667 VisitForValue(expr->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001668 VisitForValue(expr->key(), kAccumulator);
1669 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001670 EmitKeyedPropertyLoad(expr);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001671 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001672 }
1673}
1674
1675void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001676 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001677 RelocInfo::Mode mode) {
1678 // Code common for calls using the IC.
1679 ZoneList<Expression*>* args = expr->arguments();
1680 int arg_count = args->length();
1681 for (int i = 0; i < arg_count; i++) {
1682 VisitForValue(args->at(i), kStack);
1683 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001684 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001685 // Record source position for debugger.
1686 SetSourcePosition(expr->position());
1687 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001688 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1689 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001690 __ Call(ic, mode);
1691 // Restore context register.
1692 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001693 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001694}
1695
1696
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001697void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
1698 Expression* key,
1699 RelocInfo::Mode mode) {
1700 // Code common for calls using the IC.
1701 ZoneList<Expression*>* args = expr->arguments();
1702 int arg_count = args->length();
1703 for (int i = 0; i < arg_count; i++) {
1704 VisitForValue(args->at(i), kStack);
1705 }
1706 VisitForValue(key, kAccumulator);
1707 __ mov(r2, r0);
1708 // Record source position for debugger.
1709 SetSourcePosition(expr->position());
1710 // Call the IC initialization code.
1711 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1712 Handle<Code> ic = CodeGenerator::ComputeKeyedCallInitialize(arg_count,
1713 in_loop);
1714 __ Call(ic, mode);
1715 // Restore context register.
1716 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1717 Apply(context_, r0);
1718}
1719
1720
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001721void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1722 // Code common for calls using the call stub.
1723 ZoneList<Expression*>* args = expr->arguments();
1724 int arg_count = args->length();
1725 for (int i = 0; i < arg_count; i++) {
1726 VisitForValue(args->at(i), kStack);
1727 }
1728 // Record source position for debugger.
1729 SetSourcePosition(expr->position());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001730 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1731 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001732 __ CallStub(&stub);
1733 // Restore context register.
1734 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001735 DropAndApply(1, context_, r0);
1736}
1737
1738
1739void FullCodeGenerator::VisitCall(Call* expr) {
1740 Comment cmnt(masm_, "[ Call");
1741 Expression* fun = expr->expression();
1742 Variable* var = fun->AsVariableProxy()->AsVariable();
1743
1744 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001745 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1746 // resolve the function we need to call and the receiver of the
1747 // call. Then we call the resolved function using the given
1748 // arguments.
1749 VisitForValue(fun, kStack);
1750 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1751 __ push(r2); // Reserved receiver slot.
1752
1753 // Push the arguments.
1754 ZoneList<Expression*>* args = expr->arguments();
1755 int arg_count = args->length();
1756 for (int i = 0; i < arg_count; i++) {
1757 VisitForValue(args->at(i), kStack);
1758 }
1759
1760 // Push copy of the function - found below the arguments.
1761 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1762 __ push(r1);
1763
1764 // Push copy of the first argument or undefined if it doesn't exist.
1765 if (arg_count > 0) {
1766 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1767 __ push(r1);
1768 } else {
1769 __ push(r2);
1770 }
1771
1772 // Push the receiver of the enclosing function and do runtime call.
1773 __ ldr(r1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1774 __ push(r1);
1775 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1776
1777 // The runtime call returns a pair of values in r0 (function) and
1778 // r1 (receiver). Touch up the stack with the right values.
1779 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1780 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
1781
1782 // Record source position for debugger.
1783 SetSourcePosition(expr->position());
1784 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1785 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1786 __ CallStub(&stub);
1787 // Restore context register.
1788 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1789 DropAndApply(1, context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001790 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001791 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001792 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001793 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001794 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1795 } else if (var != NULL && var->slot() != NULL &&
1796 var->slot()->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001797 // Call to a lookup slot (dynamically introduced variable).
1798 Label slow, done;
1799
1800 // Generate code for loading from variables potentially shadowed
1801 // by eval-introduced variables.
1802 EmitDynamicLoadFromSlotFastCase(var->slot(),
1803 NOT_INSIDE_TYPEOF,
1804 &slow,
1805 &done);
1806
1807 __ bind(&slow);
1808 // Call the runtime to find the function to call (returned in eax)
1809 // and the object holding it (returned in edx).
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001810 __ push(context_register());
1811 __ mov(r2, Operand(var->name()));
1812 __ push(r2);
1813 __ CallRuntime(Runtime::kLoadContextSlot, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001814 __ Push(r0, r1); // Function, receiver.
1815
1816 // If fast case code has been generated, emit code to push the
1817 // function and receiver and have the slow path jump around this
1818 // code.
1819 if (done.is_linked()) {
1820 Label call;
1821 __ b(&call);
1822 __ bind(&done);
1823 // Push function.
1824 __ push(r0);
1825 // Push global receiver.
1826 __ ldr(r1, CodeGenerator::GlobalObject());
1827 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1828 __ push(r1);
1829 __ bind(&call);
1830 }
1831
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001832 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001833 } else if (fun->AsProperty() != NULL) {
1834 // Call to an object property.
1835 Property* prop = fun->AsProperty();
1836 Literal* key = prop->key()->AsLiteral();
1837 if (key != NULL && key->handle()->IsSymbol()) {
1838 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001839 VisitForValue(prop->obj(), kStack);
1840 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1841 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001842 // Call to a keyed property.
1843 // For a synthetic property use keyed load IC followed by function call,
1844 // for a regular property use keyed CallIC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001845 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001846 if (prop->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001847 VisitForValue(prop->key(), kAccumulator);
1848 // Record source code position for IC call.
1849 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001850 __ pop(r1); // We do not need to keep the receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001851
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001852 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1853 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001854 __ ldr(r1, CodeGenerator::GlobalObject());
1855 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001856 __ Push(r0, r1); // Function, receiver.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001857 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001858 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001859 EmitKeyedCallWithIC(expr, prop->key(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001860 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001861 }
1862 } else {
1863 // Call to some other expression. If the expression is an anonymous
1864 // function literal not called in a loop, mark it as one that should
1865 // also use the fast code generator.
1866 FunctionLiteral* lit = fun->AsFunctionLiteral();
1867 if (lit != NULL &&
1868 lit->name()->Equals(Heap::empty_string()) &&
1869 loop_depth() == 0) {
1870 lit->set_try_full_codegen(true);
1871 }
1872 VisitForValue(fun, kStack);
1873 // Load global receiver object.
1874 __ ldr(r1, CodeGenerator::GlobalObject());
1875 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1876 __ push(r1);
1877 // Emit function call.
1878 EmitCallWithStub(expr);
1879 }
1880}
1881
1882
1883void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1884 Comment cmnt(masm_, "[ CallNew");
1885 // According to ECMA-262, section 11.2.2, page 44, the function
1886 // expression in new calls must be evaluated before the
1887 // arguments.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001888
1889 // Push constructor on the stack. If it's not a function it's used as
1890 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
1891 // ignored.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001892 VisitForValue(expr->expression(), kStack);
1893
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001894 // Push the arguments ("left-to-right") on the stack.
1895 ZoneList<Expression*>* args = expr->arguments();
1896 int arg_count = args->length();
1897 for (int i = 0; i < arg_count; i++) {
1898 VisitForValue(args->at(i), kStack);
1899 }
1900
1901 // Call the construct call builtin that handles allocation and
1902 // constructor invocation.
1903 SetSourcePosition(expr->position());
1904
ricow@chromium.org65fae842010-08-25 15:26:24 +00001905 // Load function and argument count into r1 and r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001906 __ mov(r0, Operand(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001907 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001908
1909 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1910 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001911 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001912}
1913
1914
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001915void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1916 ASSERT(args->length() == 1);
1917
1918 VisitForValue(args->at(0), kAccumulator);
1919
1920 Label materialize_true, materialize_false;
1921 Label* if_true = NULL;
1922 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001923 Label* fall_through = NULL;
1924 PrepareTest(&materialize_true, &materialize_false,
1925 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001926
1927 __ BranchOnSmi(r0, if_true);
1928 __ b(if_false);
1929
1930 Apply(context_, if_true, if_false);
1931}
1932
1933
1934void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1935 ASSERT(args->length() == 1);
1936
1937 VisitForValue(args->at(0), kAccumulator);
1938
1939 Label materialize_true, materialize_false;
1940 Label* if_true = NULL;
1941 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001942 Label* fall_through = NULL;
1943 PrepareTest(&materialize_true, &materialize_false,
1944 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001945
1946 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001947 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001948
1949 Apply(context_, if_true, if_false);
1950}
1951
1952
1953void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1954 ASSERT(args->length() == 1);
1955
1956 VisitForValue(args->at(0), kAccumulator);
1957
1958 Label materialize_true, materialize_false;
1959 Label* if_true = NULL;
1960 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001961 Label* fall_through = NULL;
1962 PrepareTest(&materialize_true, &materialize_false,
1963 &if_true, &if_false, &fall_through);
1964
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001965 __ BranchOnSmi(r0, if_false);
1966 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1967 __ cmp(r0, ip);
1968 __ b(eq, if_true);
1969 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1970 // Undetectable objects behave like undefined when tested with typeof.
1971 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
1972 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1973 __ b(ne, if_false);
1974 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1975 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
1976 __ b(lt, if_false);
1977 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001978 Split(le, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001979
1980 Apply(context_, if_true, if_false);
1981}
1982
1983
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001984void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
1985 ASSERT(args->length() == 1);
1986
1987 VisitForValue(args->at(0), kAccumulator);
1988
1989 Label materialize_true, materialize_false;
1990 Label* if_true = NULL;
1991 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001992 Label* fall_through = NULL;
1993 PrepareTest(&materialize_true, &materialize_false,
1994 &if_true, &if_false, &fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001995
1996 __ BranchOnSmi(r0, if_false);
1997 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001998 Split(ge, if_true, if_false, fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001999
2000 Apply(context_, if_true, if_false);
2001}
2002
2003
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002004void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
2005 ASSERT(args->length() == 1);
2006
2007 VisitForValue(args->at(0), kAccumulator);
2008
2009 Label materialize_true, materialize_false;
2010 Label* if_true = NULL;
2011 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002012 Label* fall_through = NULL;
2013 PrepareTest(&materialize_true, &materialize_false,
2014 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002015
2016 __ BranchOnSmi(r0, if_false);
2017 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2018 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
2019 __ tst(r1, Operand(1 << Map::kIsUndetectable));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002020 Split(ne, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002021
2022 Apply(context_, if_true, if_false);
2023}
2024
2025
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002026void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2027 ZoneList<Expression*>* args) {
2028
2029 ASSERT(args->length() == 1);
2030
2031 VisitForValue(args->at(0), kAccumulator);
2032
2033 Label materialize_true, materialize_false;
2034 Label* if_true = NULL;
2035 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002036 Label* fall_through = NULL;
2037 PrepareTest(&materialize_true, &materialize_false,
2038 &if_true, &if_false, &fall_through);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002039
2040 // Just indicate false, as %_IsStringWrapperSafeForDefaultValueOf() is only
2041 // used in a few functions in runtime.js which should not normally be hit by
2042 // this compiler.
2043 __ jmp(if_false);
2044 Apply(context_, if_true, if_false);
2045}
2046
2047
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002048void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
2049 ASSERT(args->length() == 1);
2050
2051 VisitForValue(args->at(0), kAccumulator);
2052
2053 Label materialize_true, materialize_false;
2054 Label* if_true = NULL;
2055 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002056 Label* fall_through = NULL;
2057 PrepareTest(&materialize_true, &materialize_false,
2058 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002059
2060 __ BranchOnSmi(r0, if_false);
2061 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002062 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002063
2064 Apply(context_, if_true, if_false);
2065}
2066
2067
2068void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
2069 ASSERT(args->length() == 1);
2070
2071 VisitForValue(args->at(0), kAccumulator);
2072
2073 Label materialize_true, materialize_false;
2074 Label* if_true = NULL;
2075 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002076 Label* fall_through = NULL;
2077 PrepareTest(&materialize_true, &materialize_false,
2078 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002079
2080 __ BranchOnSmi(r0, if_false);
2081 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002082 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002083
2084 Apply(context_, if_true, if_false);
2085}
2086
2087
2088void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
2089 ASSERT(args->length() == 1);
2090
2091 VisitForValue(args->at(0), kAccumulator);
2092
2093 Label materialize_true, materialize_false;
2094 Label* if_true = NULL;
2095 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002096 Label* fall_through = NULL;
2097 PrepareTest(&materialize_true, &materialize_false,
2098 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002099
2100 __ BranchOnSmi(r0, if_false);
2101 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002102 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002103
2104 Apply(context_, if_true, if_false);
2105}
2106
2107
2108
2109void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
2110 ASSERT(args->length() == 0);
2111
2112 Label materialize_true, materialize_false;
2113 Label* if_true = NULL;
2114 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002115 Label* fall_through = NULL;
2116 PrepareTest(&materialize_true, &materialize_false,
2117 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002118
2119 // Get the frame pointer for the calling frame.
2120 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2121
2122 // Skip the arguments adaptor frame if it exists.
2123 Label check_frame_marker;
2124 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
2125 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2126 __ b(ne, &check_frame_marker);
2127 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
2128
2129 // Check the marker in the calling frame.
2130 __ bind(&check_frame_marker);
2131 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
2132 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002133 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002134
2135 Apply(context_, if_true, if_false);
2136}
2137
2138
2139void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
2140 ASSERT(args->length() == 2);
2141
2142 // Load the two objects into registers and perform the comparison.
2143 VisitForValue(args->at(0), kStack);
2144 VisitForValue(args->at(1), kAccumulator);
2145
2146 Label materialize_true, materialize_false;
2147 Label* if_true = NULL;
2148 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002149 Label* fall_through = NULL;
2150 PrepareTest(&materialize_true, &materialize_false,
2151 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002152
2153 __ pop(r1);
2154 __ cmp(r0, r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002155 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002156
2157 Apply(context_, if_true, if_false);
2158}
2159
2160
2161void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
2162 ASSERT(args->length() == 1);
2163
2164 // ArgumentsAccessStub expects the key in edx and the formal
2165 // parameter count in eax.
2166 VisitForValue(args->at(0), kAccumulator);
2167 __ mov(r1, r0);
2168 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2169 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2170 __ CallStub(&stub);
2171 Apply(context_, r0);
2172}
2173
2174
2175void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
2176 ASSERT(args->length() == 0);
2177
2178 Label exit;
2179 // Get the number of formal parameters.
2180 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2181
2182 // Check if the calling frame is an arguments adaptor frame.
2183 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2184 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
2185 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2186 __ b(ne, &exit);
2187
2188 // Arguments adaptor case: Read the arguments length from the
2189 // adaptor frame.
2190 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2191
2192 __ bind(&exit);
2193 Apply(context_, r0);
2194}
2195
2196
2197void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
2198 ASSERT(args->length() == 1);
2199 Label done, null, function, non_function_constructor;
2200
2201 VisitForValue(args->at(0), kAccumulator);
2202
2203 // If the object is a smi, we return null.
2204 __ BranchOnSmi(r0, &null);
2205
2206 // Check that the object is a JS object but take special care of JS
2207 // functions to make sure they have 'Function' as their class.
2208 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2209 __ b(lt, &null);
2210
2211 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2212 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2213 // LAST_JS_OBJECT_TYPE.
2214 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2215 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2216 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2217 __ b(eq, &function);
2218
2219 // Check if the constructor in the map is a function.
2220 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2221 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2222 __ b(ne, &non_function_constructor);
2223
2224 // r0 now contains the constructor function. Grab the
2225 // instance class name from there.
2226 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2227 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2228 __ b(&done);
2229
2230 // Functions have class 'Function'.
2231 __ bind(&function);
2232 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2233 __ jmp(&done);
2234
2235 // Objects with a non-function constructor have class 'Object'.
2236 __ bind(&non_function_constructor);
2237 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2238 __ jmp(&done);
2239
2240 // Non-JS objects have class null.
2241 __ bind(&null);
2242 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2243
2244 // All done.
2245 __ bind(&done);
2246
2247 Apply(context_, r0);
2248}
2249
2250
2251void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2252 // Conditionally generate a log call.
2253 // Args:
2254 // 0 (literal string): The type of logging (corresponds to the flags).
2255 // This is used to determine whether or not to generate the log call.
2256 // 1 (string): Format string. Access the string at argument index 2
2257 // with '%2s' (see Logger::LogRuntime for all the formats).
2258 // 2 (array): Arguments to the format string.
2259 ASSERT_EQ(args->length(), 3);
2260#ifdef ENABLE_LOGGING_AND_PROFILING
2261 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2262 VisitForValue(args->at(1), kStack);
2263 VisitForValue(args->at(2), kStack);
2264 __ CallRuntime(Runtime::kLog, 2);
2265 }
2266#endif
2267 // Finally, we're expected to leave a value on the top of the stack.
2268 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2269 Apply(context_, r0);
2270}
2271
2272
2273void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2274 ASSERT(args->length() == 0);
2275
2276 Label slow_allocate_heapnumber;
2277 Label heapnumber_allocated;
2278
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002279 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
2280 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002281 __ jmp(&heapnumber_allocated);
2282
2283 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002284 // Allocate a heap number.
2285 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002286 __ mov(r4, Operand(r0));
2287
2288 __ bind(&heapnumber_allocated);
2289
2290 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
2291 // by computing:
2292 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2293 if (CpuFeatures::IsSupported(VFP3)) {
2294 __ PrepareCallCFunction(0, r1);
2295 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
2296
2297 CpuFeatures::Scope scope(VFP3);
2298 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2299 // Create this constant using mov/orr to avoid PC relative load.
2300 __ mov(r1, Operand(0x41000000));
2301 __ orr(r1, r1, Operand(0x300000));
2302 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
2303 __ vmov(d7, r0, r1);
2304 // Move 0x4130000000000000 to VFP.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00002305 __ mov(r0, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002306 __ vmov(d8, r0, r1);
2307 // Subtract and store the result in the heap number.
2308 __ vsub(d7, d7, d8);
2309 __ sub(r0, r4, Operand(kHeapObjectTag));
2310 __ vstr(d7, r0, HeapNumber::kValueOffset);
2311 __ mov(r0, r4);
2312 } else {
2313 __ mov(r0, Operand(r4));
2314 __ PrepareCallCFunction(1, r1);
2315 __ CallCFunction(
2316 ExternalReference::fill_heap_number_with_random_function(), 1);
2317 }
2318
2319 Apply(context_, r0);
2320}
2321
2322
2323void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
2324 // Load the arguments on the stack and call the stub.
2325 SubStringStub stub;
2326 ASSERT(args->length() == 3);
2327 VisitForValue(args->at(0), kStack);
2328 VisitForValue(args->at(1), kStack);
2329 VisitForValue(args->at(2), kStack);
2330 __ CallStub(&stub);
2331 Apply(context_, r0);
2332}
2333
2334
2335void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
2336 // Load the arguments on the stack and call the stub.
2337 RegExpExecStub stub;
2338 ASSERT(args->length() == 4);
2339 VisitForValue(args->at(0), kStack);
2340 VisitForValue(args->at(1), kStack);
2341 VisitForValue(args->at(2), kStack);
2342 VisitForValue(args->at(3), kStack);
2343 __ CallStub(&stub);
2344 Apply(context_, r0);
2345}
2346
2347
2348void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
2349 ASSERT(args->length() == 1);
2350
2351 VisitForValue(args->at(0), kAccumulator); // Load the object.
2352
2353 Label done;
2354 // If the object is a smi return the object.
2355 __ BranchOnSmi(r0, &done);
2356 // If the object is not a value type, return the object.
2357 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
2358 __ b(ne, &done);
2359 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2360
2361 __ bind(&done);
2362 Apply(context_, r0);
2363}
2364
2365
2366void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
2367 // Load the arguments on the stack and call the runtime function.
2368 ASSERT(args->length() == 2);
2369 VisitForValue(args->at(0), kStack);
2370 VisitForValue(args->at(1), kStack);
2371 __ CallRuntime(Runtime::kMath_pow, 2);
2372 Apply(context_, r0);
2373}
2374
2375
2376void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
2377 ASSERT(args->length() == 2);
2378
2379 VisitForValue(args->at(0), kStack); // Load the object.
2380 VisitForValue(args->at(1), kAccumulator); // Load the value.
2381 __ pop(r1); // r0 = value. r1 = object.
2382
2383 Label done;
2384 // If the object is a smi, return the value.
2385 __ BranchOnSmi(r1, &done);
2386
2387 // If the object is not a value type, return the value.
2388 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
2389 __ b(ne, &done);
2390
2391 // Store the value.
2392 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2393 // Update the write barrier. Save the value as it will be
2394 // overwritten by the write barrier code and is needed afterward.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002395 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002396
2397 __ bind(&done);
2398 Apply(context_, r0);
2399}
2400
2401
2402void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2403 ASSERT_EQ(args->length(), 1);
2404
2405 // Load the argument on the stack and call the stub.
2406 VisitForValue(args->at(0), kStack);
2407
2408 NumberToStringStub stub;
2409 __ CallStub(&stub);
2410 Apply(context_, r0);
2411}
2412
2413
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002414void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002415 ASSERT(args->length() == 1);
2416
2417 VisitForValue(args->at(0), kAccumulator);
2418
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002419 Label done;
2420 StringCharFromCodeGenerator generator(r0, r1);
2421 generator.GenerateFast(masm_);
2422 __ jmp(&done);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002423
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002424 NopRuntimeCallHelper call_helper;
2425 generator.GenerateSlow(masm_, call_helper);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002426
2427 __ bind(&done);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002428 Apply(context_, r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002429}
2430
2431
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002432void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
2433 ASSERT(args->length() == 2);
2434
2435 VisitForValue(args->at(0), kStack);
2436 VisitForValue(args->at(1), kAccumulator);
2437
2438 Register object = r1;
2439 Register index = r0;
2440 Register scratch = r2;
2441 Register result = r3;
2442
2443 __ pop(object);
2444
2445 Label need_conversion;
2446 Label index_out_of_range;
2447 Label done;
2448 StringCharCodeAtGenerator generator(object,
2449 index,
2450 scratch,
2451 result,
2452 &need_conversion,
2453 &need_conversion,
2454 &index_out_of_range,
2455 STRING_INDEX_IS_NUMBER);
2456 generator.GenerateFast(masm_);
2457 __ jmp(&done);
2458
2459 __ bind(&index_out_of_range);
2460 // When the index is out of range, the spec requires us to return
2461 // NaN.
2462 __ LoadRoot(result, Heap::kNanValueRootIndex);
2463 __ jmp(&done);
2464
2465 __ bind(&need_conversion);
2466 // Load the undefined value into the result register, which will
2467 // trigger conversion.
2468 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2469 __ jmp(&done);
2470
2471 NopRuntimeCallHelper call_helper;
2472 generator.GenerateSlow(masm_, call_helper);
2473
2474 __ bind(&done);
2475 Apply(context_, result);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002476}
2477
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002478
2479void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
2480 ASSERT(args->length() == 2);
2481
2482 VisitForValue(args->at(0), kStack);
2483 VisitForValue(args->at(1), kAccumulator);
2484
2485 Register object = r1;
2486 Register index = r0;
2487 Register scratch1 = r2;
2488 Register scratch2 = r3;
2489 Register result = r0;
2490
2491 __ pop(object);
2492
2493 Label need_conversion;
2494 Label index_out_of_range;
2495 Label done;
2496 StringCharAtGenerator generator(object,
2497 index,
2498 scratch1,
2499 scratch2,
2500 result,
2501 &need_conversion,
2502 &need_conversion,
2503 &index_out_of_range,
2504 STRING_INDEX_IS_NUMBER);
2505 generator.GenerateFast(masm_);
2506 __ jmp(&done);
2507
2508 __ bind(&index_out_of_range);
2509 // When the index is out of range, the spec requires us to return
2510 // the empty string.
2511 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2512 __ jmp(&done);
2513
2514 __ bind(&need_conversion);
2515 // Move smi zero into the result register, which will trigger
2516 // conversion.
2517 __ mov(result, Operand(Smi::FromInt(0)));
2518 __ jmp(&done);
2519
2520 NopRuntimeCallHelper call_helper;
2521 generator.GenerateSlow(masm_, call_helper);
2522
2523 __ bind(&done);
2524 Apply(context_, result);
2525}
2526
2527
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002528void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2529 ASSERT_EQ(2, args->length());
2530
2531 VisitForValue(args->at(0), kStack);
2532 VisitForValue(args->at(1), kStack);
2533
2534 StringAddStub stub(NO_STRING_ADD_FLAGS);
2535 __ CallStub(&stub);
2536 Apply(context_, r0);
2537}
2538
2539
2540void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
2541 ASSERT_EQ(2, args->length());
2542
2543 VisitForValue(args->at(0), kStack);
2544 VisitForValue(args->at(1), kStack);
2545
2546 StringCompareStub stub;
2547 __ CallStub(&stub);
2548 Apply(context_, r0);
2549}
2550
2551
2552void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
2553 // Load the argument on the stack and call the runtime.
2554 ASSERT(args->length() == 1);
2555 VisitForValue(args->at(0), kStack);
2556 __ CallRuntime(Runtime::kMath_sin, 1);
2557 Apply(context_, r0);
2558}
2559
2560
2561void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
2562 // Load the argument on the stack and call the runtime.
2563 ASSERT(args->length() == 1);
2564 VisitForValue(args->at(0), kStack);
2565 __ CallRuntime(Runtime::kMath_cos, 1);
2566 Apply(context_, r0);
2567}
2568
2569
2570void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2571 // Load the argument on the stack and call the runtime function.
2572 ASSERT(args->length() == 1);
2573 VisitForValue(args->at(0), kStack);
2574 __ CallRuntime(Runtime::kMath_sqrt, 1);
2575 Apply(context_, r0);
2576}
2577
2578
2579void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2580 ASSERT(args->length() >= 2);
2581
2582 int arg_count = args->length() - 2; // For receiver and function.
2583 VisitForValue(args->at(0), kStack); // Receiver.
2584 for (int i = 0; i < arg_count; i++) {
2585 VisitForValue(args->at(i + 1), kStack);
2586 }
2587 VisitForValue(args->at(arg_count + 1), kAccumulator); // Function.
2588
2589 // InvokeFunction requires function in r1. Move it in there.
2590 if (!result_register().is(r1)) __ mov(r1, result_register());
2591 ParameterCount count(arg_count);
2592 __ InvokeFunction(r1, count, CALL_FUNCTION);
2593 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2594 Apply(context_, r0);
2595}
2596
2597
2598void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2599 ASSERT(args->length() == 3);
2600 VisitForValue(args->at(0), kStack);
2601 VisitForValue(args->at(1), kStack);
2602 VisitForValue(args->at(2), kStack);
2603 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
2604 Apply(context_, r0);
2605}
2606
2607
2608void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2609 ASSERT(args->length() == 3);
2610 VisitForValue(args->at(0), kStack);
2611 VisitForValue(args->at(1), kStack);
2612 VisitForValue(args->at(2), kStack);
2613 __ CallRuntime(Runtime::kSwapElements, 3);
2614 Apply(context_, r0);
2615}
2616
2617
2618void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2619 ASSERT_EQ(2, args->length());
2620
2621 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2622 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2623
2624 Handle<FixedArray> jsfunction_result_caches(
2625 Top::global_context()->jsfunction_result_caches());
2626 if (jsfunction_result_caches->length() <= cache_id) {
2627 __ Abort("Attempt to use undefined cache.");
2628 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2629 Apply(context_, r0);
2630 return;
2631 }
2632
2633 VisitForValue(args->at(1), kAccumulator);
2634
2635 Register key = r0;
2636 Register cache = r1;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002637 __ ldr(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002638 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002639 __ ldr(cache, ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002640 __ ldr(cache,
2641 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2642
2643
2644 Label done, not_found;
2645 // tmp now holds finger offset as a smi.
2646 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2647 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2648 // r2 now holds finger offset as a smi.
2649 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2650 // r3 now points to the start of fixed array elements.
2651 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2652 // Note side effect of PreIndex: r3 now points to the key of the pair.
2653 __ cmp(key, r2);
2654 __ b(ne, &not_found);
2655
2656 __ ldr(r0, MemOperand(r3, kPointerSize));
2657 __ b(&done);
2658
2659 __ bind(&not_found);
2660 // Call runtime to perform the lookup.
2661 __ Push(cache, key);
2662 __ CallRuntime(Runtime::kGetFromCache, 2);
2663
2664 __ bind(&done);
2665 Apply(context_, r0);
2666}
2667
2668
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002669void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
2670 ASSERT_EQ(2, args->length());
2671
2672 Register right = r0;
2673 Register left = r1;
2674 Register tmp = r2;
2675 Register tmp2 = r3;
2676
2677 VisitForValue(args->at(0), kStack);
2678 VisitForValue(args->at(1), kAccumulator);
2679 __ pop(left);
2680
2681 Label done, fail, ok;
2682 __ cmp(left, Operand(right));
2683 __ b(eq, &ok);
2684 // Fail if either is a non-HeapObject.
2685 __ and_(tmp, left, Operand(right));
2686 __ tst(tmp, Operand(kSmiTagMask));
2687 __ b(eq, &fail);
2688 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
2689 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
2690 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
2691 __ b(ne, &fail);
2692 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
2693 __ cmp(tmp, Operand(tmp2));
2694 __ b(ne, &fail);
2695 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
2696 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
2697 __ cmp(tmp, tmp2);
2698 __ b(eq, &ok);
2699 __ bind(&fail);
2700 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
2701 __ jmp(&done);
2702 __ bind(&ok);
2703 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
2704 __ bind(&done);
2705
2706 Apply(context_, r0);
2707}
2708
2709
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002710void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
2711 VisitForValue(args->at(0), kAccumulator);
2712
2713 Label materialize_true, materialize_false;
2714 Label* if_true = NULL;
2715 Label* if_false = NULL;
2716 Label* fall_through = NULL;
2717 PrepareTest(&materialize_true, &materialize_false,
2718 &if_true, &if_false, &fall_through);
2719
2720 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2721 __ tst(r0, Operand(String::kContainsCachedArrayIndexMask));
2722
2723 __ b(eq, if_true);
2724 __ b(if_false);
2725
2726 Apply(context_, if_true, if_false);
2727}
2728
2729
2730void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
2731 ASSERT(args->length() == 1);
2732 VisitForValue(args->at(0), kAccumulator);
2733 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2734 __ IndexFromHash(r0, r0);
2735 Apply(context_, r0);
2736}
2737
2738
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002739void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002740 Handle<String> name = expr->name();
2741 if (name->length() > 0 && name->Get(0) == '_') {
2742 Comment cmnt(masm_, "[ InlineRuntimeCall");
2743 EmitInlineRuntimeCall(expr);
2744 return;
2745 }
2746
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002747 Comment cmnt(masm_, "[ CallRuntime");
2748 ZoneList<Expression*>* args = expr->arguments();
2749
2750 if (expr->is_jsruntime()) {
2751 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002752 __ ldr(r0, CodeGenerator::GlobalObject());
2753 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002754 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002755 }
2756
2757 // Push the arguments ("left-to-right").
2758 int arg_count = args->length();
2759 for (int i = 0; i < arg_count; i++) {
2760 VisitForValue(args->at(i), kStack);
2761 }
2762
2763 if (expr->is_jsruntime()) {
2764 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002765 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002766 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2767 NOT_IN_LOOP);
2768 __ Call(ic, RelocInfo::CODE_TARGET);
2769 // Restore context register.
2770 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002771 } else {
2772 // Call the C runtime function.
2773 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002774 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002775 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002776}
2777
2778
2779void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2780 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002781 case Token::DELETE: {
2782 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2783 Property* prop = expr->expression()->AsProperty();
2784 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2785 if (prop == NULL && var == NULL) {
2786 // Result of deleting non-property, non-variable reference is true.
2787 // The subexpression may have side effects.
2788 VisitForEffect(expr->expression());
2789 Apply(context_, true);
2790 } else if (var != NULL &&
2791 !var->is_global() &&
2792 var->slot() != NULL &&
2793 var->slot()->type() != Slot::LOOKUP) {
2794 // Result of deleting non-global, non-dynamic variables is false.
2795 // The subexpression does not have side effects.
2796 Apply(context_, false);
2797 } else {
2798 // Property or variable reference. Call the delete builtin with
2799 // object and property name as arguments.
2800 if (prop != NULL) {
2801 VisitForValue(prop->obj(), kStack);
2802 VisitForValue(prop->key(), kStack);
2803 } else if (var->is_global()) {
2804 __ ldr(r1, CodeGenerator::GlobalObject());
2805 __ mov(r0, Operand(var->name()));
2806 __ Push(r1, r0);
2807 } else {
2808 // Non-global variable. Call the runtime to look up the context
2809 // where the variable was introduced.
2810 __ push(context_register());
2811 __ mov(r2, Operand(var->name()));
2812 __ push(r2);
2813 __ CallRuntime(Runtime::kLookupContext, 2);
2814 __ push(r0);
2815 __ mov(r2, Operand(var->name()));
2816 __ push(r2);
2817 }
2818 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
2819 Apply(context_, r0);
2820 }
2821 break;
2822 }
2823
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002824 case Token::VOID: {
2825 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2826 VisitForEffect(expr->expression());
2827 switch (context_) {
2828 case Expression::kUninitialized:
2829 UNREACHABLE();
2830 break;
2831 case Expression::kEffect:
2832 break;
2833 case Expression::kValue:
2834 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2835 switch (location_) {
2836 case kAccumulator:
2837 break;
2838 case kStack:
2839 __ push(result_register());
2840 break;
2841 }
2842 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002843 case Expression::kTest:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002844 __ jmp(false_label_);
2845 break;
2846 }
2847 break;
2848 }
2849
2850 case Token::NOT: {
2851 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002852 Label materialize_true, materialize_false;
2853 Label* if_true = NULL;
2854 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002855 Label* fall_through = NULL;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002856
2857 // Notice that the labels are swapped.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002858 PrepareTest(&materialize_true, &materialize_false,
2859 &if_false, &if_true, &fall_through);
2860 VisitForControl(expr->expression(), if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002861 Apply(context_, if_false, if_true); // Labels swapped.
2862 break;
2863 }
2864
2865 case Token::TYPEOF: {
2866 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002867 VisitForTypeofValue(expr->expression(), kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002868 __ CallRuntime(Runtime::kTypeof, 1);
2869 Apply(context_, r0);
2870 break;
2871 }
2872
2873 case Token::ADD: {
2874 Comment cmt(masm_, "[ UnaryOperation (ADD)");
2875 VisitForValue(expr->expression(), kAccumulator);
2876 Label no_conversion;
2877 __ tst(result_register(), Operand(kSmiTagMask));
2878 __ b(eq, &no_conversion);
2879 __ push(r0);
2880 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2881 __ bind(&no_conversion);
2882 Apply(context_, result_register());
2883 break;
2884 }
2885
2886 case Token::SUB: {
2887 Comment cmt(masm_, "[ UnaryOperation (SUB)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002888 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002889 UnaryOverwriteMode overwrite =
2890 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002891 GenericUnaryOpStub stub(Token::SUB, overwrite);
2892 // GenericUnaryOpStub expects the argument to be in the
2893 // accumulator register r0.
2894 VisitForValue(expr->expression(), kAccumulator);
2895 __ CallStub(&stub);
2896 Apply(context_, r0);
2897 break;
2898 }
2899
2900 case Token::BIT_NOT: {
2901 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002902 // The generic unary operation stub expects the argument to be
2903 // in the accumulator register r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002904 VisitForValue(expr->expression(), kAccumulator);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002905 Label done;
2906 if (ShouldInlineSmiCase(expr->op())) {
2907 Label call_stub;
2908 __ BranchOnNotSmi(r0, &call_stub);
2909 __ mvn(r0, Operand(r0));
2910 // Bit-clear inverted smi-tag.
2911 __ bic(r0, r0, Operand(kSmiTagMask));
2912 __ b(&done);
2913 __ bind(&call_stub);
2914 }
2915 bool overwrite = expr->expression()->ResultOverwriteAllowed();
2916 UnaryOverwriteMode mode =
2917 overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
2918 GenericUnaryOpStub stub(Token::BIT_NOT, mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002919 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002920 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002921 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002922 break;
2923 }
2924
2925 default:
2926 UNREACHABLE();
2927 }
2928}
2929
2930
2931void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2932 Comment cmnt(masm_, "[ CountOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002933 SetSourcePosition(expr->position());
2934
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002935 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2936 // as the left-hand side.
2937 if (!expr->expression()->IsValidLeftHandSide()) {
2938 VisitForEffect(expr->expression());
2939 return;
2940 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002941
2942 // Expression can only be a property, a global or a (parameter or local)
2943 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2944 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2945 LhsKind assign_type = VARIABLE;
2946 Property* prop = expr->expression()->AsProperty();
2947 // In case of a property we use the uninitialized expression context
2948 // of the key to detect a named property.
2949 if (prop != NULL) {
2950 assign_type =
2951 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2952 }
2953
2954 // Evaluate expression and get value.
2955 if (assign_type == VARIABLE) {
2956 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
2957 Location saved_location = location_;
2958 location_ = kAccumulator;
2959 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
2960 Expression::kValue);
2961 location_ = saved_location;
2962 } else {
2963 // Reserve space for result of postfix operation.
2964 if (expr->is_postfix() && context_ != Expression::kEffect) {
2965 __ mov(ip, Operand(Smi::FromInt(0)));
2966 __ push(ip);
2967 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002968 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002969 // Put the object both on the stack and in the accumulator.
2970 VisitForValue(prop->obj(), kAccumulator);
2971 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002972 EmitNamedPropertyLoad(prop);
2973 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002974 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002975 VisitForValue(prop->key(), kAccumulator);
2976 __ ldr(r1, MemOperand(sp, 0));
2977 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002978 EmitKeyedPropertyLoad(prop);
2979 }
2980 }
2981
2982 // Call ToNumber only if operand is not a smi.
2983 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002984 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002985 __ push(r0);
2986 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2987 __ bind(&no_conversion);
2988
2989 // Save result for postfix expressions.
2990 if (expr->is_postfix()) {
2991 switch (context_) {
2992 case Expression::kUninitialized:
2993 UNREACHABLE();
2994 case Expression::kEffect:
2995 // Do not save result.
2996 break;
2997 case Expression::kValue:
2998 case Expression::kTest:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002999 // Save the result on the stack. If we have a named or keyed property
3000 // we store the result under the receiver that is currently on top
3001 // of the stack.
3002 switch (assign_type) {
3003 case VARIABLE:
3004 __ push(r0);
3005 break;
3006 case NAMED_PROPERTY:
3007 __ str(r0, MemOperand(sp, kPointerSize));
3008 break;
3009 case KEYED_PROPERTY:
3010 __ str(r0, MemOperand(sp, 2 * kPointerSize));
3011 break;
3012 }
3013 break;
3014 }
3015 }
3016
3017
3018 // Inline smi case if we are in a loop.
3019 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003020 int count_value = expr->op() == Token::INC ? 1 : -1;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003021 if (ShouldInlineSmiCase(expr->op())) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003022 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003023 __ b(vs, &stub_call);
3024 // We could eliminate this smi check if we split the code at
3025 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003026 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003027 __ bind(&stub_call);
3028 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003029 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003030 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003031 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00003032 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003033 __ CallStub(&stub);
3034 __ bind(&done);
3035
3036 // Store the value returned in r0.
3037 switch (assign_type) {
3038 case VARIABLE:
3039 if (expr->is_postfix()) {
3040 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003041 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003042 Expression::kEffect);
3043 // For all contexts except kEffect: We have the result on
3044 // top of the stack.
3045 if (context_ != Expression::kEffect) {
3046 ApplyTOS(context_);
3047 }
3048 } else {
3049 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003050 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003051 context_);
3052 }
3053 break;
3054 case NAMED_PROPERTY: {
3055 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003056 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003057 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
3058 __ Call(ic, RelocInfo::CODE_TARGET);
3059 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003060 if (context_ != Expression::kEffect) {
3061 ApplyTOS(context_);
3062 }
3063 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00003064 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003065 }
3066 break;
3067 }
3068 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003069 __ pop(r1); // Key.
3070 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003071 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
3072 __ Call(ic, RelocInfo::CODE_TARGET);
3073 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003074 if (context_ != Expression::kEffect) {
3075 ApplyTOS(context_);
3076 }
3077 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003078 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003079 }
3080 break;
3081 }
3082 }
3083}
3084
3085
ricow@chromium.org65fae842010-08-25 15:26:24 +00003086void FullCodeGenerator::VisitForTypeofValue(Expression* expr, Location where) {
3087 VariableProxy* proxy = expr->AsVariableProxy();
3088 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
3089 Comment cmnt(masm_, "Global variable");
3090 __ ldr(r0, CodeGenerator::GlobalObject());
3091 __ mov(r2, Operand(proxy->name()));
3092 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
3093 // Use a regular load, not a contextual load, to avoid a reference
3094 // error.
3095 __ Call(ic, RelocInfo::CODE_TARGET);
3096 if (where == kStack) __ push(r0);
3097 } else if (proxy != NULL &&
3098 proxy->var()->slot() != NULL &&
3099 proxy->var()->slot()->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003100 Label done, slow;
3101
3102 // Generate code for loading from variables potentially shadowed
3103 // by eval-introduced variables.
3104 Slot* slot = proxy->var()->slot();
3105 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
3106
3107 __ bind(&slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003108 __ mov(r0, Operand(proxy->name()));
3109 __ Push(cp, r0);
3110 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003111 __ bind(&done);
3112
ricow@chromium.org65fae842010-08-25 15:26:24 +00003113 if (where == kStack) __ push(r0);
3114 } else {
3115 // This expression cannot throw a reference error at the top level.
3116 VisitForValue(expr, where);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003117 }
3118}
3119
3120
ricow@chromium.org65fae842010-08-25 15:26:24 +00003121bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
3122 Expression* left,
3123 Expression* right,
3124 Label* if_true,
3125 Label* if_false,
3126 Label* fall_through) {
3127 if (op != Token::EQ && op != Token::EQ_STRICT) return false;
3128
3129 // Check for the pattern: typeof <expression> == <string literal>.
3130 Literal* right_literal = right->AsLiteral();
3131 if (right_literal == NULL) return false;
3132 Handle<Object> right_literal_value = right_literal->handle();
3133 if (!right_literal_value->IsString()) return false;
3134 UnaryOperation* left_unary = left->AsUnaryOperation();
3135 if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
3136 Handle<String> check = Handle<String>::cast(right_literal_value);
3137
3138 VisitForTypeofValue(left_unary->expression(), kAccumulator);
3139 if (check->Equals(Heap::number_symbol())) {
3140 __ tst(r0, Operand(kSmiTagMask));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003141 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003142 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3143 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
3144 __ cmp(r0, ip);
3145 Split(eq, if_true, if_false, fall_through);
3146 } else if (check->Equals(Heap::string_symbol())) {
3147 __ tst(r0, Operand(kSmiTagMask));
3148 __ b(eq, if_false);
3149 // Check for undetectable objects => false.
3150 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3151 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
3152 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3153 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3154 __ b(eq, if_false);
3155 __ ldrb(r1, FieldMemOperand(r0, Map::kInstanceTypeOffset));
3156 __ cmp(r1, Operand(FIRST_NONSTRING_TYPE));
3157 Split(lt, if_true, if_false, fall_through);
3158 } else if (check->Equals(Heap::boolean_symbol())) {
3159 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3160 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003161 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003162 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
3163 __ cmp(r0, ip);
3164 Split(eq, if_true, if_false, fall_through);
3165 } else if (check->Equals(Heap::undefined_symbol())) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003166 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003167 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003168 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003169 __ tst(r0, Operand(kSmiTagMask));
3170 __ b(eq, if_false);
3171 // Check for undetectable objects => true.
3172 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3173 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
3174 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3175 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3176 Split(eq, if_true, if_false, fall_through);
3177 } else if (check->Equals(Heap::function_symbol())) {
3178 __ tst(r0, Operand(kSmiTagMask));
3179 __ b(eq, if_false);
3180 __ CompareObjectType(r0, r1, r0, JS_FUNCTION_TYPE);
3181 __ b(eq, if_true);
3182 // Regular expressions => 'function' (they are callable).
3183 __ CompareInstanceType(r1, r0, JS_REGEXP_TYPE);
3184 Split(eq, if_true, if_false, fall_through);
3185 } else if (check->Equals(Heap::object_symbol())) {
3186 __ tst(r0, Operand(kSmiTagMask));
3187 __ b(eq, if_false);
3188 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3189 __ cmp(r0, ip);
3190 __ b(eq, if_true);
3191 // Regular expressions => 'function', not 'object'.
3192 __ CompareObjectType(r0, r1, r0, JS_REGEXP_TYPE);
3193 __ b(eq, if_false);
3194 // Check for undetectable objects => false.
3195 __ ldrb(r0, FieldMemOperand(r1, Map::kBitFieldOffset));
3196 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3197 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
3198 __ b(eq, if_false);
3199 // Check for JS objects => true.
3200 __ ldrb(r0, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3201 __ cmp(r0, Operand(FIRST_JS_OBJECT_TYPE));
3202 __ b(lt, if_false);
3203 __ cmp(r0, Operand(LAST_JS_OBJECT_TYPE));
3204 Split(le, if_true, if_false, fall_through);
3205 } else {
3206 if (if_false != fall_through) __ jmp(if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003207 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003208
3209 return true;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003210}
3211
3212
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003213void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
3214 Comment cmnt(masm_, "[ CompareOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00003215 SetSourcePosition(expr->position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003216
3217 // Always perform the comparison for its control flow. Pack the result
3218 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003219
3220 Label materialize_true, materialize_false;
3221 Label* if_true = NULL;
3222 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003223 Label* fall_through = NULL;
3224 PrepareTest(&materialize_true, &materialize_false,
3225 &if_true, &if_false, &fall_through);
3226
3227 // First we try a fast inlined version of the compare when one of
3228 // the operands is a literal.
3229 Token::Value op = expr->op();
3230 Expression* left = expr->left();
3231 Expression* right = expr->right();
3232 if (TryLiteralCompare(op, left, right, if_true, if_false, fall_through)) {
3233 Apply(context_, if_true, if_false);
3234 return;
3235 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003236
3237 VisitForValue(expr->left(), kStack);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003238 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003239 case Token::IN:
3240 VisitForValue(expr->right(), kStack);
3241 __ InvokeBuiltin(Builtins::IN, CALL_JS);
3242 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3243 __ cmp(r0, ip);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003244 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003245 break;
3246
3247 case Token::INSTANCEOF: {
3248 VisitForValue(expr->right(), kStack);
3249 InstanceofStub stub;
3250 __ CallStub(&stub);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003251 // The stub returns 0 for true.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003252 __ tst(r0, r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003253 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003254 break;
3255 }
3256
3257 default: {
3258 VisitForValue(expr->right(), kAccumulator);
3259 Condition cc = eq;
3260 bool strict = false;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003261 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003262 case Token::EQ_STRICT:
3263 strict = true;
3264 // Fall through
ricow@chromium.org65fae842010-08-25 15:26:24 +00003265 case Token::EQ:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003266 cc = eq;
3267 __ pop(r1);
3268 break;
3269 case Token::LT:
3270 cc = lt;
3271 __ pop(r1);
3272 break;
3273 case Token::GT:
3274 // Reverse left and right sides to obtain ECMA-262 conversion order.
3275 cc = lt;
3276 __ mov(r1, result_register());
3277 __ pop(r0);
3278 break;
3279 case Token::LTE:
3280 // Reverse left and right sides to obtain ECMA-262 conversion order.
3281 cc = ge;
3282 __ mov(r1, result_register());
3283 __ pop(r0);
3284 break;
3285 case Token::GTE:
3286 cc = ge;
3287 __ pop(r1);
3288 break;
3289 case Token::IN:
3290 case Token::INSTANCEOF:
3291 default:
3292 UNREACHABLE();
3293 }
3294
ricow@chromium.org65fae842010-08-25 15:26:24 +00003295 if (ShouldInlineSmiCase(op)) {
3296 Label slow_case;
3297 __ orr(r2, r0, Operand(r1));
3298 __ BranchOnNotSmi(r2, &slow_case);
3299 __ cmp(r1, r0);
3300 Split(cc, if_true, if_false, NULL);
3301 __ bind(&slow_case);
3302 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003303
ager@chromium.orgb5737492010-07-15 09:29:43 +00003304 CompareStub stub(cc, strict, kBothCouldBeNaN, true, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003305 __ CallStub(&stub);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00003306 __ cmp(r0, Operand(0, RelocInfo::NONE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003307 Split(cc, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003308 }
3309 }
3310
3311 // Convert the result of the comparison into one expected for this
3312 // expression's context.
3313 Apply(context_, if_true, if_false);
3314}
3315
3316
ricow@chromium.org65fae842010-08-25 15:26:24 +00003317void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
3318 Comment cmnt(masm_, "[ CompareToNull");
3319 Label materialize_true, materialize_false;
3320 Label* if_true = NULL;
3321 Label* if_false = NULL;
3322 Label* fall_through = NULL;
3323 PrepareTest(&materialize_true, &materialize_false,
3324 &if_true, &if_false, &fall_through);
3325
3326 VisitForValue(expr->expression(), kAccumulator);
3327 __ LoadRoot(r1, Heap::kNullValueRootIndex);
3328 __ cmp(r0, r1);
3329 if (expr->is_strict()) {
3330 Split(eq, if_true, if_false, fall_through);
3331 } else {
3332 __ b(eq, if_true);
3333 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
3334 __ cmp(r0, r1);
3335 __ b(eq, if_true);
3336 __ tst(r0, Operand(kSmiTagMask));
3337 __ b(eq, if_false);
3338 // It can be an undetectable object.
3339 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3340 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3341 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3342 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3343 Split(eq, if_true, if_false, fall_through);
3344 }
3345 Apply(context_, if_true, if_false);
3346}
3347
3348
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003349void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3350 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3351 Apply(context_, r0);
3352}
3353
3354
3355Register FullCodeGenerator::result_register() { return r0; }
3356
3357
3358Register FullCodeGenerator::context_register() { return cp; }
3359
3360
3361void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3362 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3363 __ str(value, MemOperand(fp, frame_offset));
3364}
3365
3366
3367void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003368 __ ldr(dst, ContextOperand(cp, context_index));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003369}
3370
3371
3372// ----------------------------------------------------------------------------
3373// Non-local control flow support.
3374
3375void FullCodeGenerator::EnterFinallyBlock() {
3376 ASSERT(!result_register().is(r1));
3377 // Store result register while executing finally block.
3378 __ push(result_register());
3379 // Cook return address in link register to stack (smi encoded Code* delta)
3380 __ sub(r1, lr, Operand(masm_->CodeObject()));
3381 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3382 ASSERT_EQ(0, kSmiTag);
3383 __ add(r1, r1, Operand(r1)); // Convert to smi.
3384 __ push(r1);
3385}
3386
3387
3388void FullCodeGenerator::ExitFinallyBlock() {
3389 ASSERT(!result_register().is(r1));
3390 // Restore result register from stack.
3391 __ pop(r1);
3392 // Uncook return address and return.
3393 __ pop(result_register());
3394 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3395 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3396 __ add(pc, r1, Operand(masm_->CodeObject()));
3397}
3398
3399
3400#undef __
3401
3402} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003403
3404#endif // V8_TARGET_ARCH_ARM