blob: 61fbd764159204d3cc7ac052fd7af63872482b37 [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.org65fae842010-08-25 15:26:24 +000032#include "code-stubs-arm.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
249void FullCodeGenerator::Apply(Expression::Context context, Register reg) {
250 switch (context) {
251 case Expression::kUninitialized:
252 UNREACHABLE();
253
254 case Expression::kEffect:
255 // Nothing to do.
256 break;
257
258 case Expression::kValue:
259 // Move value into place.
260 switch (location_) {
261 case kAccumulator:
262 if (!reg.is(result_register())) __ mov(result_register(), reg);
263 break;
264 case kStack:
265 __ push(reg);
266 break;
267 }
268 break;
269
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000270 case Expression::kTest:
ricow@chromium.org65fae842010-08-25 15:26:24 +0000271 // For simplicity we always test the accumulator register.
272 if (!reg.is(result_register())) __ mov(result_register(), reg);
273 DoTest(true_label_, false_label_, fall_through_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000274 break;
275 }
276}
277
278
279void FullCodeGenerator::Apply(Expression::Context context, Slot* slot) {
280 switch (context) {
281 case Expression::kUninitialized:
282 UNREACHABLE();
283 case Expression::kEffect:
284 // Nothing to do.
285 break;
286 case Expression::kValue:
287 case Expression::kTest:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000288 // On ARM we have to move the value into a register to do anything
289 // with it.
290 Move(result_register(), slot);
291 Apply(context, result_register());
292 break;
293 }
294}
295
296
297void FullCodeGenerator::Apply(Expression::Context context, Literal* lit) {
298 switch (context) {
299 case Expression::kUninitialized:
300 UNREACHABLE();
301 case Expression::kEffect:
302 break;
303 // Nothing to do.
304 case Expression::kValue:
305 case Expression::kTest:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000306 // On ARM we have to move the value into a register to do anything
307 // with it.
308 __ mov(result_register(), Operand(lit->handle()));
309 Apply(context, result_register());
310 break;
311 }
312}
313
314
315void FullCodeGenerator::ApplyTOS(Expression::Context context) {
316 switch (context) {
317 case Expression::kUninitialized:
318 UNREACHABLE();
319
320 case Expression::kEffect:
321 __ Drop(1);
322 break;
323
324 case Expression::kValue:
325 switch (location_) {
326 case kAccumulator:
327 __ pop(result_register());
328 break;
329 case kStack:
330 break;
331 }
332 break;
333
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000334 case Expression::kTest:
ricow@chromium.org65fae842010-08-25 15:26:24 +0000335 __ pop(result_register());
336 DoTest(true_label_, false_label_, fall_through_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000337 break;
338 }
339}
340
341
342void FullCodeGenerator::DropAndApply(int count,
343 Expression::Context context,
344 Register reg) {
345 ASSERT(count > 0);
346 ASSERT(!reg.is(sp));
347 switch (context) {
348 case Expression::kUninitialized:
349 UNREACHABLE();
350
351 case Expression::kEffect:
352 __ Drop(count);
353 break;
354
355 case Expression::kValue:
356 switch (location_) {
357 case kAccumulator:
358 __ Drop(count);
359 if (!reg.is(result_register())) __ mov(result_register(), reg);
360 break;
361 case kStack:
362 if (count > 1) __ Drop(count - 1);
363 __ str(reg, MemOperand(sp));
364 break;
365 }
366 break;
367
368 case Expression::kTest:
ricow@chromium.org65fae842010-08-25 15:26:24 +0000369 __ Drop(count);
370 if (!reg.is(result_register())) __ mov(result_register(), reg);
371 DoTest(true_label_, false_label_, fall_through_);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000372 break;
373 }
374}
375
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000376
377void FullCodeGenerator::Apply(Expression::Context context,
378 Label* materialize_true,
379 Label* materialize_false) {
380 switch (context) {
381 case Expression::kUninitialized:
382
383 case Expression::kEffect:
384 ASSERT_EQ(materialize_true, materialize_false);
385 __ bind(materialize_true);
386 break;
387
388 case Expression::kValue: {
389 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000390 switch (location_) {
391 case kAccumulator:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000392 __ bind(materialize_true);
393 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
394 __ jmp(&done);
395 __ bind(materialize_false);
396 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000397 break;
398 case kStack:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000399 __ bind(materialize_true);
400 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
401 __ push(ip);
402 __ jmp(&done);
403 __ bind(materialize_false);
404 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
405 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000406 break;
407 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000408 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000409 break;
410 }
411
412 case Expression::kTest:
413 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000414 }
415}
416
417
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000418// Convert constant control flow (true or false) to the result expected for
419// a given expression context.
420void FullCodeGenerator::Apply(Expression::Context context, bool flag) {
421 switch (context) {
422 case Expression::kUninitialized:
423 UNREACHABLE();
424 break;
425 case Expression::kEffect:
426 break;
427 case Expression::kValue: {
428 Heap::RootListIndex value_root_index =
429 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
430 switch (location_) {
431 case kAccumulator:
432 __ LoadRoot(result_register(), value_root_index);
433 break;
434 case kStack:
435 __ LoadRoot(ip, value_root_index);
436 __ push(ip);
437 break;
438 }
439 break;
440 }
441 case Expression::kTest:
ricow@chromium.org65fae842010-08-25 15:26:24 +0000442 if (flag) {
443 if (true_label_ != fall_through_) __ b(true_label_);
444 } else {
445 if (false_label_ != fall_through_) __ b(false_label_);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000446 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000447 break;
448 }
449}
450
451
ricow@chromium.org65fae842010-08-25 15:26:24 +0000452void FullCodeGenerator::DoTest(Label* if_true,
453 Label* if_false,
454 Label* fall_through) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000455 // Call the runtime to find the boolean value of the source and then
456 // translate it into control flow to the pair of labels.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000457 __ push(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000458 __ CallRuntime(Runtime::kToBool, 1);
459 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
460 __ cmp(r0, ip);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000461 Split(eq, if_true, if_false, fall_through);
462}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000463
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000464
ricow@chromium.org65fae842010-08-25 15:26:24 +0000465void FullCodeGenerator::Split(Condition cc,
466 Label* if_true,
467 Label* if_false,
468 Label* fall_through) {
469 if (if_false == fall_through) {
470 __ b(cc, if_true);
471 } else if (if_true == fall_through) {
472 __ b(NegateCondition(cc), if_false);
473 } else {
474 __ b(cc, if_true);
475 __ b(if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000476 }
477}
478
479
480MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
481 switch (slot->type()) {
482 case Slot::PARAMETER:
483 case Slot::LOCAL:
484 return MemOperand(fp, SlotOffset(slot));
485 case Slot::CONTEXT: {
486 int context_chain_length =
ager@chromium.org5c838252010-02-19 08:53:10 +0000487 scope()->ContextChainLength(slot->var()->scope());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000488 __ LoadContext(scratch, context_chain_length);
489 return CodeGenerator::ContextOperand(scratch, slot->index());
490 }
491 case Slot::LOOKUP:
492 UNREACHABLE();
493 }
494 UNREACHABLE();
495 return MemOperand(r0, 0);
496}
497
498
499void FullCodeGenerator::Move(Register destination, Slot* source) {
500 // Use destination as scratch.
501 MemOperand slot_operand = EmitSlotSearch(source, destination);
502 __ ldr(destination, slot_operand);
503}
504
505
506void FullCodeGenerator::Move(Slot* dst,
507 Register src,
508 Register scratch1,
509 Register scratch2) {
510 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
511 ASSERT(!scratch1.is(src) && !scratch2.is(src));
512 MemOperand location = EmitSlotSearch(dst, scratch1);
513 __ str(src, location);
514 // Emit the write barrier code if the location is in the heap.
515 if (dst->type() == Slot::CONTEXT) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000516 __ RecordWrite(scratch1,
517 Operand(Context::SlotOffset(dst->index())),
518 scratch2,
519 src);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000520 }
521}
522
523
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000524void FullCodeGenerator::EmitDeclaration(Variable* variable,
525 Variable::Mode mode,
526 FunctionLiteral* function) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000527 Comment cmnt(masm_, "[ Declaration");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000528 ASSERT(variable != NULL); // Must have been resolved.
529 Slot* slot = variable->slot();
530 Property* prop = variable->AsProperty();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000531
532 if (slot != NULL) {
533 switch (slot->type()) {
534 case Slot::PARAMETER:
535 case Slot::LOCAL:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000536 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000537 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
538 __ str(ip, MemOperand(fp, SlotOffset(slot)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000539 } else if (function != NULL) {
540 VisitForValue(function, kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000541 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
542 }
543 break;
544
545 case Slot::CONTEXT:
546 // We bypass the general EmitSlotSearch because we know more about
547 // this specific context.
548
549 // The variable in the decl always resides in the current context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000550 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000551 if (FLAG_debug_code) {
552 // Check if we have the correct context pointer.
553 __ ldr(r1,
554 CodeGenerator::ContextOperand(cp, Context::FCONTEXT_INDEX));
555 __ cmp(r1, cp);
556 __ Check(eq, "Unexpected declaration in current context.");
557 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000558 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000559 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
560 __ str(ip, CodeGenerator::ContextOperand(cp, slot->index()));
561 // No write barrier since the_hole_value is in old space.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000562 } else if (function != NULL) {
563 VisitForValue(function, kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000564 __ str(result_register(),
565 CodeGenerator::ContextOperand(cp, slot->index()));
566 int offset = Context::SlotOffset(slot->index());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000567 // We know that we have written a function, which is not a smi.
568 __ mov(r1, Operand(cp));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000569 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000570 }
571 break;
572
573 case Slot::LOOKUP: {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000574 __ mov(r2, Operand(variable->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000575 // Declaration nodes are always introduced in one of two modes.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000576 ASSERT(mode == Variable::VAR ||
577 mode == Variable::CONST);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000578 PropertyAttributes attr =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000579 (mode == Variable::VAR) ? NONE : READ_ONLY;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000580 __ mov(r1, Operand(Smi::FromInt(attr)));
581 // Push initial value, if any.
582 // Note: For variables we must not push an initial value (such as
583 // 'undefined') because we may have a (legal) redeclaration and we
584 // must not destroy the current value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000585 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000586 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000587 __ Push(cp, r2, r1, r0);
588 } else if (function != NULL) {
589 __ Push(cp, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000590 // Push initial value for function declaration.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000591 VisitForValue(function, kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000592 } else {
593 __ mov(r0, Operand(Smi::FromInt(0))); // No initial value!
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000594 __ Push(cp, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000595 }
596 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
597 break;
598 }
599 }
600
601 } else if (prop != NULL) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000602 if (function != NULL || mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000603 // We are declaring a function or constant that rewrites to a
604 // property. Use (keyed) IC to set the initial value.
605 VisitForValue(prop->obj(), kStack);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000606 if (function != NULL) {
607 VisitForValue(prop->key(), kStack);
608 VisitForValue(function, kAccumulator);
609 __ pop(r1); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000610 } else {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000611 VisitForValue(prop->key(), kAccumulator);
612 __ mov(r1, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000613 __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex);
614 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000615 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000616
617 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
618 __ Call(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000619 // Value in r0 is ignored (declarations are statements).
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000620 }
621 }
622}
623
624
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000625void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
626 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
627}
628
629
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000630void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
631 // Call the runtime to declare the globals.
632 // The context is the first argument.
633 __ mov(r1, Operand(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000634 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000635 __ Push(cp, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000636 __ CallRuntime(Runtime::kDeclareGlobals, 3);
637 // Return value is ignored.
638}
639
640
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000641void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000642 Comment cmnt(masm_, "[ SwitchStatement");
643 Breakable nested_statement(this, stmt);
644 SetStatementPosition(stmt);
645 // Keep the switch value on the stack until a case matches.
646 VisitForValue(stmt->tag(), kStack);
647
648 ZoneList<CaseClause*>* clauses = stmt->cases();
649 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
650
651 Label next_test; // Recycled for each test.
652 // Compile all the tests with branches to their bodies.
653 for (int i = 0; i < clauses->length(); i++) {
654 CaseClause* clause = clauses->at(i);
655 // The default is not a test, but remember it as final fall through.
656 if (clause->is_default()) {
657 default_clause = clause;
658 continue;
659 }
660
661 Comment cmnt(masm_, "[ Case comparison");
662 __ bind(&next_test);
663 next_test.Unuse();
664
665 // Compile the label expression.
666 VisitForValue(clause->label(), kAccumulator);
667
ricow@chromium.org65fae842010-08-25 15:26:24 +0000668 // Perform the comparison as if via '==='.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000669 __ ldr(r1, MemOperand(sp, 0)); // Switch value.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000670 if (ShouldInlineSmiCase(Token::EQ_STRICT)) {
671 Label slow_case;
672 __ orr(r2, r1, r0);
673 __ tst(r2, Operand(kSmiTagMask));
674 __ b(ne, &slow_case);
675 __ cmp(r1, r0);
676 __ b(ne, &next_test);
677 __ Drop(1); // Switch value is no longer needed.
678 __ b(clause->body_target()->entry_label());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000679 __ bind(&slow_case);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000680 }
681
ager@chromium.orgb5737492010-07-15 09:29:43 +0000682 CompareStub stub(eq, true, kBothCouldBeNaN, true, r1, r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000683 __ CallStub(&stub);
ager@chromium.orgb5737492010-07-15 09:29:43 +0000684 __ cmp(r0, Operand(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000685 __ b(ne, &next_test);
686 __ Drop(1); // Switch value is no longer needed.
687 __ b(clause->body_target()->entry_label());
688 }
689
690 // Discard the test value and jump to the default if present, otherwise to
691 // the end of the statement.
692 __ bind(&next_test);
693 __ Drop(1); // Switch value is no longer needed.
694 if (default_clause == NULL) {
695 __ b(nested_statement.break_target());
696 } else {
697 __ b(default_clause->body_target()->entry_label());
698 }
699
700 // Compile all the case bodies.
701 for (int i = 0; i < clauses->length(); i++) {
702 Comment cmnt(masm_, "[ Case body");
703 CaseClause* clause = clauses->at(i);
704 __ bind(clause->body_target()->entry_label());
705 VisitStatements(clause->statements());
706 }
707
708 __ bind(nested_statement.break_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000709}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000710
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000711
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000712void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000713 Comment cmnt(masm_, "[ ForInStatement");
714 SetStatementPosition(stmt);
715
716 Label loop, exit;
717 ForIn loop_statement(this, stmt);
718 increment_loop_depth();
719
720 // Get the object to enumerate over. Both SpiderMonkey and JSC
721 // ignore null and undefined in contrast to the specification; see
722 // ECMA-262 section 12.6.4.
723 VisitForValue(stmt->enumerable(), kAccumulator);
724 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
725 __ cmp(r0, ip);
726 __ b(eq, &exit);
727 __ LoadRoot(ip, Heap::kNullValueRootIndex);
728 __ cmp(r0, ip);
729 __ b(eq, &exit);
730
731 // Convert the object to a JS object.
732 Label convert, done_convert;
733 __ BranchOnSmi(r0, &convert);
734 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
735 __ b(hs, &done_convert);
736 __ bind(&convert);
737 __ push(r0);
738 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
739 __ bind(&done_convert);
740 __ push(r0);
741
742 // TODO(kasperl): Check cache validity in generated code. This is a
743 // fast case for the JSObject::IsSimpleEnum cache validity
744 // checks. If we cannot guarantee cache validity, call the runtime
745 // system to check cache validity or get the property names in a
746 // fixed array.
747
748 // Get the set of properties to enumerate.
749 __ push(r0); // Duplicate the enumerable object on the stack.
750 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
751
752 // If we got a map from the runtime call, we can do a fast
753 // modification check. Otherwise, we got a fixed array, and we have
754 // to do a slow check.
755 Label fixed_array;
756 __ mov(r2, r0);
757 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
758 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
759 __ cmp(r1, ip);
760 __ b(ne, &fixed_array);
761
762 // We got a map in register r0. Get the enumeration cache from it.
763 __ ldr(r1, FieldMemOperand(r0, Map::kInstanceDescriptorsOffset));
764 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
765 __ ldr(r2, FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
766
767 // Setup the four remaining stack slots.
768 __ push(r0); // Map.
769 __ ldr(r1, FieldMemOperand(r2, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000770 __ mov(r0, Operand(Smi::FromInt(0)));
771 // Push enumeration cache, enumeration cache length (as smi) and zero.
772 __ Push(r2, r1, r0);
773 __ jmp(&loop);
774
775 // We got a fixed array in register r0. Iterate through that.
776 __ bind(&fixed_array);
777 __ mov(r1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
778 __ Push(r1, r0);
779 __ ldr(r1, FieldMemOperand(r0, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000780 __ mov(r0, Operand(Smi::FromInt(0)));
781 __ Push(r1, r0); // Fixed array length (as smi) and initial index.
782
783 // Generate code for doing the condition check.
784 __ bind(&loop);
785 // Load the current count to r0, load the length to r1.
786 __ Ldrd(r0, r1, MemOperand(sp, 0 * kPointerSize));
787 __ cmp(r0, r1); // Compare to the array length.
788 __ b(hs, loop_statement.break_target());
789
790 // Get the current entry of the array into register r3.
791 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
792 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
793 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
794
795 // Get the expected map from the stack or a zero map in the
796 // permanent slow case into register r2.
797 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
798
799 // Check if the expected map still matches that of the enumerable.
800 // If not, we have to filter the key.
801 Label update_each;
802 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
803 __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
804 __ cmp(r4, Operand(r2));
805 __ b(eq, &update_each);
806
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000807 // Convert the entry to a string or (smi) 0 if it isn't a property
808 // any more. If the property has been removed while iterating, we
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000809 // just skip it.
810 __ push(r1); // Enumerable.
811 __ push(r3); // Current entry.
812 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000813 __ mov(r3, Operand(r0), SetCC);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000814 __ b(eq, loop_statement.continue_target());
815
816 // Update the 'each' property or variable from the possibly filtered
817 // entry in register r3.
818 __ bind(&update_each);
819 __ mov(result_register(), r3);
820 // Perform the assignment as if via '='.
821 EmitAssignment(stmt->each());
822
823 // Generate code for the body of the loop.
824 Label stack_limit_hit, stack_check_done;
825 Visit(stmt->body());
826
827 __ StackLimitCheck(&stack_limit_hit);
828 __ bind(&stack_check_done);
829
830 // Generate code for the going to the next element by incrementing
831 // the index (smi) stored on top of the stack.
832 __ bind(loop_statement.continue_target());
833 __ pop(r0);
834 __ add(r0, r0, Operand(Smi::FromInt(1)));
835 __ push(r0);
836 __ b(&loop);
837
838 // Slow case for the stack limit check.
839 StackCheckStub stack_check_stub;
840 __ bind(&stack_limit_hit);
841 __ CallStub(&stack_check_stub);
842 __ b(&stack_check_done);
843
844 // Remove the pointers stored on the stack.
845 __ bind(loop_statement.break_target());
846 __ Drop(5);
847
848 // Exit and decrement the loop depth.
849 __ bind(&exit);
850 decrement_loop_depth();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000851}
852
853
854void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info) {
855 // Use the fast case closure allocation code that allocates in new
856 // space for nested functions that don't need literals cloning.
857 if (scope()->is_function_scope() && info->num_literals() == 0) {
858 FastNewClosureStub stub;
859 __ mov(r0, Operand(info));
860 __ push(r0);
861 __ CallStub(&stub);
862 } else {
863 __ mov(r0, Operand(info));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000864 __ Push(cp, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000865 __ CallRuntime(Runtime::kNewClosure, 2);
866 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000867 Apply(context_, r0);
868}
869
870
871void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
872 Comment cmnt(masm_, "[ VariableProxy");
873 EmitVariableLoad(expr->var(), context_);
874}
875
876
877void FullCodeGenerator::EmitVariableLoad(Variable* var,
878 Expression::Context context) {
879 // Four cases: non-this global variables, lookup slots, all other
880 // types of slots, and parameters that rewrite to explicit property
881 // accesses on the arguments object.
882 Slot* slot = var->slot();
883 Property* property = var->AsProperty();
884
885 if (var->is_global() && !var->is_this()) {
886 Comment cmnt(masm_, "Global variable");
887 // Use inline caching. Variable name is passed in r2 and the global
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000888 // object (receiver) in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000889 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000890 __ mov(r2, Operand(var->name()));
891 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
892 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000893 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000894
895 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
896 Comment cmnt(masm_, "Lookup slot");
897 __ mov(r1, Operand(var->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000898 __ Push(cp, r1); // Context and name.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000899 __ CallRuntime(Runtime::kLoadContextSlot, 2);
900 Apply(context, r0);
901
902 } else if (slot != NULL) {
903 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
904 ? "Context slot"
905 : "Stack slot");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000906 if (var->mode() == Variable::CONST) {
907 // Constants may be the hole value if they have not been initialized.
908 // Unhole them.
909 Label done;
910 MemOperand slot_operand = EmitSlotSearch(slot, r0);
911 __ ldr(r0, slot_operand);
912 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
913 __ cmp(r0, ip);
914 __ b(ne, &done);
915 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
916 __ bind(&done);
917 Apply(context, r0);
918 } else {
919 Apply(context, slot);
920 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000921 } else {
922 Comment cmnt(masm_, "Rewritten parameter");
923 ASSERT_NOT_NULL(property);
924 // Rewritten parameter accesses are of the form "slot[literal]".
925
926 // Assert that the object is in a slot.
927 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
928 ASSERT_NOT_NULL(object_var);
929 Slot* object_slot = object_var->slot();
930 ASSERT_NOT_NULL(object_slot);
931
932 // Load the object.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000933 Move(r1, object_slot);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000934
935 // Assert that the key is a smi.
936 Literal* key_literal = property->key()->AsLiteral();
937 ASSERT_NOT_NULL(key_literal);
938 ASSERT(key_literal->handle()->IsSmi());
939
940 // Load the key.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000941 __ mov(r0, Operand(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000942
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000943 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000944 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
945 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000946 Apply(context, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000947 }
948}
949
950
951void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
952 Comment cmnt(masm_, "[ RegExpLiteral");
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000953 Label materialized;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000954 // Registers will be used as follows:
955 // r4 = JS function, literals array
956 // r3 = literal index
957 // r2 = RegExp pattern
958 // r1 = RegExp flags
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000959 // r0 = temp + materialized value (RegExp literal)
ricow@chromium.org65fae842010-08-25 15:26:24 +0000960 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
961 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000962 int literal_offset =
ricow@chromium.org65fae842010-08-25 15:26:24 +0000963 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000964 __ ldr(r0, FieldMemOperand(r4, literal_offset));
965 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
966 __ cmp(r0, ip);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000967 __ b(ne, &materialized);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000968
969 // Create regexp literal using runtime function.
970 // Result will be in r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000971 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
972 __ mov(r2, Operand(expr->pattern()));
973 __ mov(r1, Operand(expr->flags()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000974 __ Push(r4, r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000975 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000976
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000977 __ bind(&materialized);
978 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
979 __ push(r0);
980 __ mov(r0, Operand(Smi::FromInt(size)));
981 __ push(r0);
982 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000983
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000984 // After this, registers are used as follows:
985 // r0: Newly allocated regexp.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000986 // r1: Materialized regexp.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000987 // r2: temp.
988 __ pop(r1);
989 __ CopyFields(r0, r1, r2.bit(), size / kPointerSize);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000990 Apply(context_, r0);
991}
992
993
994void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
995 Comment cmnt(masm_, "[ ObjectLiteral");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000996 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
997 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
998 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
999 __ mov(r1, Operand(expr->constant_properties()));
1000 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001001 __ Push(r3, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001002 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001003 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001004 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001005 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001006 }
1007
1008 // If result_saved is true the result is on top of the stack. If
1009 // result_saved is false the result is in r0.
1010 bool result_saved = false;
1011
1012 for (int i = 0; i < expr->properties()->length(); i++) {
1013 ObjectLiteral::Property* property = expr->properties()->at(i);
1014 if (property->IsCompileTimeValue()) continue;
1015
1016 Literal* key = property->key();
1017 Expression* value = property->value();
1018 if (!result_saved) {
1019 __ push(r0); // Save result on stack
1020 result_saved = true;
1021 }
1022 switch (property->kind()) {
1023 case ObjectLiteral::Property::CONSTANT:
1024 UNREACHABLE();
1025 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1026 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1027 // Fall through.
1028 case ObjectLiteral::Property::COMPUTED:
1029 if (key->handle()->IsSymbol()) {
1030 VisitForValue(value, kAccumulator);
1031 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001032 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001033 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1034 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001035 break;
1036 }
1037 // Fall through.
1038 case ObjectLiteral::Property::PROTOTYPE:
1039 // Duplicate receiver on stack.
1040 __ ldr(r0, MemOperand(sp));
1041 __ push(r0);
1042 VisitForValue(key, kStack);
1043 VisitForValue(value, kStack);
1044 __ CallRuntime(Runtime::kSetProperty, 3);
1045 break;
1046 case ObjectLiteral::Property::GETTER:
1047 case ObjectLiteral::Property::SETTER:
1048 // Duplicate receiver on stack.
1049 __ ldr(r0, MemOperand(sp));
1050 __ push(r0);
1051 VisitForValue(key, kStack);
1052 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1053 Smi::FromInt(1) :
1054 Smi::FromInt(0)));
1055 __ push(r1);
1056 VisitForValue(value, kStack);
1057 __ CallRuntime(Runtime::kDefineAccessor, 4);
1058 break;
1059 }
1060 }
1061
1062 if (result_saved) {
1063 ApplyTOS(context_);
1064 } else {
1065 Apply(context_, r0);
1066 }
1067}
1068
1069
1070void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1071 Comment cmnt(masm_, "[ ArrayLiteral");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001072
1073 ZoneList<Expression*>* subexprs = expr->values();
1074 int length = subexprs->length();
1075
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001076 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1077 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1078 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1079 __ mov(r1, Operand(expr->constant_elements()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001080 __ Push(r3, r2, r1);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001081 if (expr->constant_elements()->map() == Heap::fixed_cow_array_map()) {
1082 FastCloneShallowArrayStub stub(
1083 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1084 __ CallStub(&stub);
1085 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
1086 } else if (expr->depth() > 1) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001087 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001088 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001089 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001090 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001091 FastCloneShallowArrayStub stub(
1092 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001093 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001094 }
1095
1096 bool result_saved = false; // Is the result saved to the stack?
1097
1098 // Emit code to evaluate all the non-constant subexpressions and to store
1099 // them into the newly cloned array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001100 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001101 Expression* subexpr = subexprs->at(i);
1102 // If the subexpression is a literal or a simple materialized literal it
1103 // is already set in the cloned array.
1104 if (subexpr->AsLiteral() != NULL ||
1105 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1106 continue;
1107 }
1108
1109 if (!result_saved) {
1110 __ push(r0);
1111 result_saved = true;
1112 }
1113 VisitForValue(subexpr, kAccumulator);
1114
1115 // Store the subexpression value in the array's elements.
1116 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
1117 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1118 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1119 __ str(result_register(), FieldMemOperand(r1, offset));
1120
1121 // Update the write barrier for the array store with r0 as the scratch
1122 // register.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001123 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001124 }
1125
1126 if (result_saved) {
1127 ApplyTOS(context_);
1128 } else {
1129 Apply(context_, r0);
1130 }
1131}
1132
1133
ager@chromium.org5c838252010-02-19 08:53:10 +00001134void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1135 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001136 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1137 // on the left-hand side.
1138 if (!expr->target()->IsValidLeftHandSide()) {
1139 VisitForEffect(expr->target());
1140 return;
1141 }
1142
ager@chromium.org5c838252010-02-19 08:53:10 +00001143 // Left-hand side can only be a property, a global or a (parameter or local)
1144 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1145 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1146 LhsKind assign_type = VARIABLE;
1147 Property* prop = expr->target()->AsProperty();
1148 if (prop != NULL) {
1149 assign_type =
1150 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
1151 }
1152
1153 // Evaluate LHS expression.
1154 switch (assign_type) {
1155 case VARIABLE:
1156 // Nothing to do here.
1157 break;
1158 case NAMED_PROPERTY:
1159 if (expr->is_compound()) {
1160 // We need the receiver both on the stack and in the accumulator.
1161 VisitForValue(prop->obj(), kAccumulator);
1162 __ push(result_register());
1163 } else {
1164 VisitForValue(prop->obj(), kStack);
1165 }
1166 break;
1167 case KEYED_PROPERTY:
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001168 // We need the key and receiver on both the stack and in r0 and r1.
1169 if (expr->is_compound()) {
1170 VisitForValue(prop->obj(), kStack);
1171 VisitForValue(prop->key(), kAccumulator);
1172 __ ldr(r1, MemOperand(sp, 0));
1173 __ push(r0);
1174 } else {
1175 VisitForValue(prop->obj(), kStack);
1176 VisitForValue(prop->key(), kStack);
1177 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001178 break;
1179 }
1180
1181 // If we have a compound assignment: Get value of LHS expression and
1182 // store in on top of the stack.
1183 if (expr->is_compound()) {
1184 Location saved_location = location_;
1185 location_ = kStack;
1186 switch (assign_type) {
1187 case VARIABLE:
1188 EmitVariableLoad(expr->target()->AsVariableProxy()->var(),
1189 Expression::kValue);
1190 break;
1191 case NAMED_PROPERTY:
1192 EmitNamedPropertyLoad(prop);
1193 __ push(result_register());
1194 break;
1195 case KEYED_PROPERTY:
1196 EmitKeyedPropertyLoad(prop);
1197 __ push(result_register());
1198 break;
1199 }
1200 location_ = saved_location;
1201 }
1202
1203 // Evaluate RHS expression.
1204 Expression* rhs = expr->value();
1205 VisitForValue(rhs, kAccumulator);
1206
1207 // If we have a compound assignment: Apply operator.
1208 if (expr->is_compound()) {
1209 Location saved_location = location_;
1210 location_ = kAccumulator;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001211 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1212 ? OVERWRITE_RIGHT
1213 : NO_OVERWRITE;
1214 EmitBinaryOp(expr->binary_op(), Expression::kValue, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001215 location_ = saved_location;
1216 }
1217
1218 // Record source position before possible IC call.
1219 SetSourcePosition(expr->position());
1220
1221 // Store the value.
1222 switch (assign_type) {
1223 case VARIABLE:
1224 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001225 expr->op(),
ager@chromium.org5c838252010-02-19 08:53:10 +00001226 context_);
1227 break;
1228 case NAMED_PROPERTY:
1229 EmitNamedPropertyAssignment(expr);
1230 break;
1231 case KEYED_PROPERTY:
1232 EmitKeyedPropertyAssignment(expr);
1233 break;
1234 }
1235}
1236
1237
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001238void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1239 SetSourcePosition(prop->position());
1240 Literal* key = prop->key()->AsLiteral();
1241 __ mov(r2, Operand(key->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001242 // Call load IC. It has arguments receiver and property name r0 and r2.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001243 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1244 __ Call(ic, RelocInfo::CODE_TARGET);
1245}
1246
1247
1248void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1249 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001250 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001251 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1252 __ Call(ic, RelocInfo::CODE_TARGET);
1253}
1254
1255
1256void FullCodeGenerator::EmitBinaryOp(Token::Value op,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001257 Expression::Context context,
1258 OverwriteMode mode) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001259 __ pop(r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001260 GenericBinaryOpStub stub(op, mode, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001261 __ CallStub(&stub);
1262 Apply(context, r0);
1263}
1264
1265
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001266void FullCodeGenerator::EmitAssignment(Expression* expr) {
1267 // Invalid left-hand sides are rewritten to have a 'throw
1268 // ReferenceError' on the left-hand side.
1269 if (!expr->IsValidLeftHandSide()) {
1270 VisitForEffect(expr);
1271 return;
1272 }
1273
1274 // Left-hand side can only be a property, a global or a (parameter or local)
1275 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1276 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1277 LhsKind assign_type = VARIABLE;
1278 Property* prop = expr->AsProperty();
1279 if (prop != NULL) {
1280 assign_type = (prop->key()->IsPropertyName())
1281 ? NAMED_PROPERTY
1282 : KEYED_PROPERTY;
1283 }
1284
1285 switch (assign_type) {
1286 case VARIABLE: {
1287 Variable* var = expr->AsVariableProxy()->var();
1288 EmitVariableAssignment(var, Token::ASSIGN, Expression::kEffect);
1289 break;
1290 }
1291 case NAMED_PROPERTY: {
1292 __ push(r0); // Preserve value.
1293 VisitForValue(prop->obj(), kAccumulator);
1294 __ mov(r1, r0);
1295 __ pop(r0); // Restore value.
1296 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
1297 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1298 __ Call(ic, RelocInfo::CODE_TARGET);
1299 break;
1300 }
1301 case KEYED_PROPERTY: {
1302 __ push(r0); // Preserve value.
1303 VisitForValue(prop->obj(), kStack);
1304 VisitForValue(prop->key(), kAccumulator);
1305 __ mov(r1, r0);
1306 __ pop(r2);
1307 __ pop(r0); // Restore value.
1308 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1309 __ Call(ic, RelocInfo::CODE_TARGET);
1310 break;
1311 }
1312 }
1313}
1314
1315
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001316void FullCodeGenerator::EmitVariableAssignment(Variable* var,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001317 Token::Value op,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001318 Expression::Context context) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001319 // Left-hand sides that rewrite to explicit property accesses do not reach
1320 // here.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001321 ASSERT(var != NULL);
1322 ASSERT(var->is_global() || var->slot() != NULL);
1323
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001324 if (var->is_global()) {
1325 ASSERT(!var->is_this());
1326 // Assignment to a global variable. Use inline caching for the
1327 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001328 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001329 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001330 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001331 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1332 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001333
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001334 } else if (var->mode() != Variable::CONST || op == Token::INIT_CONST) {
1335 // Perform the assignment for non-const variables and for initialization
1336 // of const variables. Const assignments are simply skipped.
1337 Label done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001338 Slot* slot = var->slot();
1339 switch (slot->type()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001340 case Slot::PARAMETER:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001341 case Slot::LOCAL:
1342 if (op == Token::INIT_CONST) {
1343 // Detect const reinitialization by checking for the hole value.
1344 __ ldr(r1, MemOperand(fp, SlotOffset(slot)));
1345 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1346 __ cmp(r1, ip);
1347 __ b(ne, &done);
1348 }
1349 // Perform the assignment.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001350 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1351 break;
1352
1353 case Slot::CONTEXT: {
1354 MemOperand target = EmitSlotSearch(slot, r1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001355 if (op == Token::INIT_CONST) {
1356 // Detect const reinitialization by checking for the hole value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001357 __ ldr(r2, target);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001358 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001359 __ cmp(r2, ip);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001360 __ b(ne, &done);
1361 }
1362 // Perform the assignment and issue the write barrier.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001363 __ str(result_register(), target);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001364 // RecordWrite may destroy all its register arguments.
1365 __ mov(r3, result_register());
1366 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001367 __ RecordWrite(r1, Operand(offset), r2, r3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001368 break;
1369 }
1370
1371 case Slot::LOOKUP:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001372 // Call the runtime for the assignment. The runtime will ignore
1373 // const reinitialization.
1374 __ push(r0); // Value.
1375 __ mov(r0, Operand(slot->var()->name()));
1376 __ Push(cp, r0); // Context and name.
1377 if (op == Token::INIT_CONST) {
1378 // The runtime will ignore const redeclaration.
1379 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1380 } else {
1381 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1382 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001383 break;
1384 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001385 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001386 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001387
ager@chromium.org5c838252010-02-19 08:53:10 +00001388 Apply(context, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001389}
1390
1391
1392void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1393 // Assignment to a property, using a named store IC.
1394 Property* prop = expr->target()->AsProperty();
1395 ASSERT(prop != NULL);
1396 ASSERT(prop->key()->AsLiteral() != NULL);
1397
1398 // If the assignment starts a block of assignments to the same object,
1399 // change to slow case to avoid the quadratic behavior of repeatedly
1400 // adding fast properties.
1401 if (expr->starts_initialization_block()) {
1402 __ push(result_register());
1403 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1404 __ push(ip);
1405 __ CallRuntime(Runtime::kToSlowProperties, 1);
1406 __ pop(result_register());
1407 }
1408
1409 // Record source code position before IC call.
1410 SetSourcePosition(expr->position());
1411 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001412 // Load receiver to r1. Leave a copy in the stack if needed for turning the
1413 // receiver into fast case.
ager@chromium.org5c838252010-02-19 08:53:10 +00001414 if (expr->ends_initialization_block()) {
1415 __ ldr(r1, MemOperand(sp));
1416 } else {
1417 __ pop(r1);
1418 }
1419
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001420 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1421 __ Call(ic, RelocInfo::CODE_TARGET);
1422
1423 // If the assignment ends an initialization block, revert to fast case.
1424 if (expr->ends_initialization_block()) {
1425 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001426 // Receiver is under the result value.
1427 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001428 __ push(ip);
1429 __ CallRuntime(Runtime::kToFastProperties, 1);
1430 __ pop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001431 DropAndApply(1, context_, r0);
1432 } else {
1433 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001434 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001435}
1436
1437
1438void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1439 // Assignment to a property, using a keyed store IC.
1440
1441 // If the assignment starts a block of assignments to the same object,
1442 // change to slow case to avoid the quadratic behavior of repeatedly
1443 // adding fast properties.
1444 if (expr->starts_initialization_block()) {
1445 __ push(result_register());
1446 // Receiver is now under the key and value.
1447 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1448 __ push(ip);
1449 __ CallRuntime(Runtime::kToSlowProperties, 1);
1450 __ pop(result_register());
1451 }
1452
1453 // Record source code position before IC call.
1454 SetSourcePosition(expr->position());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001455 __ pop(r1); // Key.
1456 // Load receiver to r2. Leave a copy in the stack if needed for turning the
1457 // receiver into fast case.
1458 if (expr->ends_initialization_block()) {
1459 __ ldr(r2, MemOperand(sp));
1460 } else {
1461 __ pop(r2);
1462 }
1463
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001464 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1465 __ Call(ic, RelocInfo::CODE_TARGET);
1466
1467 // If the assignment ends an initialization block, revert to fast case.
1468 if (expr->ends_initialization_block()) {
1469 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001470 // Receiver is under the result value.
1471 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001472 __ push(ip);
1473 __ CallRuntime(Runtime::kToFastProperties, 1);
1474 __ pop(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001475 DropAndApply(1, context_, r0);
1476 } else {
1477 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001478 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001479}
1480
1481
1482void FullCodeGenerator::VisitProperty(Property* expr) {
1483 Comment cmnt(masm_, "[ Property");
1484 Expression* key = expr->key();
1485
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001486 if (key->IsPropertyName()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001487 VisitForValue(expr->obj(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001488 EmitNamedPropertyLoad(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001489 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001490 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001491 VisitForValue(expr->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001492 VisitForValue(expr->key(), kAccumulator);
1493 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001494 EmitKeyedPropertyLoad(expr);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001495 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001496 }
1497}
1498
1499void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001500 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001501 RelocInfo::Mode mode) {
1502 // Code common for calls using the IC.
1503 ZoneList<Expression*>* args = expr->arguments();
1504 int arg_count = args->length();
1505 for (int i = 0; i < arg_count; i++) {
1506 VisitForValue(args->at(i), kStack);
1507 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001508 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001509 // Record source position for debugger.
1510 SetSourcePosition(expr->position());
1511 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001512 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1513 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001514 __ Call(ic, mode);
1515 // Restore context register.
1516 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001517 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001518}
1519
1520
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001521void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
1522 Expression* key,
1523 RelocInfo::Mode mode) {
1524 // Code common for calls using the IC.
1525 ZoneList<Expression*>* args = expr->arguments();
1526 int arg_count = args->length();
1527 for (int i = 0; i < arg_count; i++) {
1528 VisitForValue(args->at(i), kStack);
1529 }
1530 VisitForValue(key, kAccumulator);
1531 __ mov(r2, r0);
1532 // Record source position for debugger.
1533 SetSourcePosition(expr->position());
1534 // Call the IC initialization code.
1535 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1536 Handle<Code> ic = CodeGenerator::ComputeKeyedCallInitialize(arg_count,
1537 in_loop);
1538 __ Call(ic, mode);
1539 // Restore context register.
1540 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1541 Apply(context_, r0);
1542}
1543
1544
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001545void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1546 // Code common for calls using the call stub.
1547 ZoneList<Expression*>* args = expr->arguments();
1548 int arg_count = args->length();
1549 for (int i = 0; i < arg_count; i++) {
1550 VisitForValue(args->at(i), kStack);
1551 }
1552 // Record source position for debugger.
1553 SetSourcePosition(expr->position());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001554 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1555 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001556 __ CallStub(&stub);
1557 // Restore context register.
1558 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001559 DropAndApply(1, context_, r0);
1560}
1561
1562
1563void FullCodeGenerator::VisitCall(Call* expr) {
1564 Comment cmnt(masm_, "[ Call");
1565 Expression* fun = expr->expression();
1566 Variable* var = fun->AsVariableProxy()->AsVariable();
1567
1568 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001569 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1570 // resolve the function we need to call and the receiver of the
1571 // call. Then we call the resolved function using the given
1572 // arguments.
1573 VisitForValue(fun, kStack);
1574 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1575 __ push(r2); // Reserved receiver slot.
1576
1577 // Push the arguments.
1578 ZoneList<Expression*>* args = expr->arguments();
1579 int arg_count = args->length();
1580 for (int i = 0; i < arg_count; i++) {
1581 VisitForValue(args->at(i), kStack);
1582 }
1583
1584 // Push copy of the function - found below the arguments.
1585 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1586 __ push(r1);
1587
1588 // Push copy of the first argument or undefined if it doesn't exist.
1589 if (arg_count > 0) {
1590 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1591 __ push(r1);
1592 } else {
1593 __ push(r2);
1594 }
1595
1596 // Push the receiver of the enclosing function and do runtime call.
1597 __ ldr(r1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1598 __ push(r1);
1599 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1600
1601 // The runtime call returns a pair of values in r0 (function) and
1602 // r1 (receiver). Touch up the stack with the right values.
1603 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1604 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
1605
1606 // Record source position for debugger.
1607 SetSourcePosition(expr->position());
1608 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1609 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1610 __ CallStub(&stub);
1611 // Restore context register.
1612 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1613 DropAndApply(1, context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001614 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001615 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001616 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001617 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001618 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1619 } else if (var != NULL && var->slot() != NULL &&
1620 var->slot()->type() == Slot::LOOKUP) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001621 // Call to a lookup slot (dynamically introduced variable). Call the
1622 // runtime to find the function to call (returned in eax) and the object
1623 // holding it (returned in edx).
1624 __ push(context_register());
1625 __ mov(r2, Operand(var->name()));
1626 __ push(r2);
1627 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1628 __ push(r0); // Function.
1629 __ push(r1); // Receiver.
1630 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001631 } else if (fun->AsProperty() != NULL) {
1632 // Call to an object property.
1633 Property* prop = fun->AsProperty();
1634 Literal* key = prop->key()->AsLiteral();
1635 if (key != NULL && key->handle()->IsSymbol()) {
1636 // Call to a named property, use call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001637 VisitForValue(prop->obj(), kStack);
1638 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1639 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001640 // Call to a keyed property.
1641 // For a synthetic property use keyed load IC followed by function call,
1642 // for a regular property use keyed CallIC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001643 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001644 if (prop->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001645 VisitForValue(prop->key(), kAccumulator);
1646 // Record source code position for IC call.
1647 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001648 __ pop(r1); // We do not need to keep the receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001649
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001650 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1651 __ Call(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001652 // Push result (function).
1653 __ push(r0);
1654 // Push Global receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001655 __ ldr(r1, CodeGenerator::GlobalObject());
1656 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001657 __ push(r1);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001658 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001659 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001660 EmitKeyedCallWithIC(expr, prop->key(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001661 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001662 }
1663 } else {
1664 // Call to some other expression. If the expression is an anonymous
1665 // function literal not called in a loop, mark it as one that should
1666 // also use the fast code generator.
1667 FunctionLiteral* lit = fun->AsFunctionLiteral();
1668 if (lit != NULL &&
1669 lit->name()->Equals(Heap::empty_string()) &&
1670 loop_depth() == 0) {
1671 lit->set_try_full_codegen(true);
1672 }
1673 VisitForValue(fun, kStack);
1674 // Load global receiver object.
1675 __ ldr(r1, CodeGenerator::GlobalObject());
1676 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1677 __ push(r1);
1678 // Emit function call.
1679 EmitCallWithStub(expr);
1680 }
1681}
1682
1683
1684void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1685 Comment cmnt(masm_, "[ CallNew");
1686 // According to ECMA-262, section 11.2.2, page 44, the function
1687 // expression in new calls must be evaluated before the
1688 // arguments.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001689
1690 // Push constructor on the stack. If it's not a function it's used as
1691 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
1692 // ignored.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001693 VisitForValue(expr->expression(), kStack);
1694
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001695 // Push the arguments ("left-to-right") on the stack.
1696 ZoneList<Expression*>* args = expr->arguments();
1697 int arg_count = args->length();
1698 for (int i = 0; i < arg_count; i++) {
1699 VisitForValue(args->at(i), kStack);
1700 }
1701
1702 // Call the construct call builtin that handles allocation and
1703 // constructor invocation.
1704 SetSourcePosition(expr->position());
1705
ricow@chromium.org65fae842010-08-25 15:26:24 +00001706 // Load function and argument count into r1 and r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001707 __ mov(r0, Operand(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001708 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001709
1710 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1711 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001712 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001713}
1714
1715
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001716void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1717 ASSERT(args->length() == 1);
1718
1719 VisitForValue(args->at(0), kAccumulator);
1720
1721 Label materialize_true, materialize_false;
1722 Label* if_true = NULL;
1723 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001724 Label* fall_through = NULL;
1725 PrepareTest(&materialize_true, &materialize_false,
1726 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001727
1728 __ BranchOnSmi(r0, if_true);
1729 __ b(if_false);
1730
1731 Apply(context_, if_true, if_false);
1732}
1733
1734
1735void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1736 ASSERT(args->length() == 1);
1737
1738 VisitForValue(args->at(0), kAccumulator);
1739
1740 Label materialize_true, materialize_false;
1741 Label* if_true = NULL;
1742 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001743 Label* fall_through = NULL;
1744 PrepareTest(&materialize_true, &materialize_false,
1745 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001746
1747 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001748 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001749
1750 Apply(context_, if_true, if_false);
1751}
1752
1753
1754void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1755 ASSERT(args->length() == 1);
1756
1757 VisitForValue(args->at(0), kAccumulator);
1758
1759 Label materialize_true, materialize_false;
1760 Label* if_true = NULL;
1761 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001762 Label* fall_through = NULL;
1763 PrepareTest(&materialize_true, &materialize_false,
1764 &if_true, &if_false, &fall_through);
1765
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001766 __ BranchOnSmi(r0, if_false);
1767 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1768 __ cmp(r0, ip);
1769 __ b(eq, if_true);
1770 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1771 // Undetectable objects behave like undefined when tested with typeof.
1772 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
1773 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1774 __ b(ne, if_false);
1775 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1776 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
1777 __ b(lt, if_false);
1778 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001779 Split(le, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001780
1781 Apply(context_, if_true, if_false);
1782}
1783
1784
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001785void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
1786 ASSERT(args->length() == 1);
1787
1788 VisitForValue(args->at(0), kAccumulator);
1789
1790 Label materialize_true, materialize_false;
1791 Label* if_true = NULL;
1792 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001793 Label* fall_through = NULL;
1794 PrepareTest(&materialize_true, &materialize_false,
1795 &if_true, &if_false, &fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001796
1797 __ BranchOnSmi(r0, if_false);
1798 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001799 Split(ge, if_true, if_false, fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001800
1801 Apply(context_, if_true, if_false);
1802}
1803
1804
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001805void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
1806 ASSERT(args->length() == 1);
1807
1808 VisitForValue(args->at(0), kAccumulator);
1809
1810 Label materialize_true, materialize_false;
1811 Label* if_true = NULL;
1812 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001813 Label* fall_through = NULL;
1814 PrepareTest(&materialize_true, &materialize_false,
1815 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001816
1817 __ BranchOnSmi(r0, if_false);
1818 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1819 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
1820 __ tst(r1, Operand(1 << Map::kIsUndetectable));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001821 Split(ne, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001822
1823 Apply(context_, if_true, if_false);
1824}
1825
1826
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001827void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
1828 ZoneList<Expression*>* args) {
1829
1830 ASSERT(args->length() == 1);
1831
1832 VisitForValue(args->at(0), kAccumulator);
1833
1834 Label materialize_true, materialize_false;
1835 Label* if_true = NULL;
1836 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001837 Label* fall_through = NULL;
1838 PrepareTest(&materialize_true, &materialize_false,
1839 &if_true, &if_false, &fall_through);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001840
1841 // Just indicate false, as %_IsStringWrapperSafeForDefaultValueOf() is only
1842 // used in a few functions in runtime.js which should not normally be hit by
1843 // this compiler.
1844 __ jmp(if_false);
1845 Apply(context_, if_true, if_false);
1846}
1847
1848
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001849void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
1850 ASSERT(args->length() == 1);
1851
1852 VisitForValue(args->at(0), kAccumulator);
1853
1854 Label materialize_true, materialize_false;
1855 Label* if_true = NULL;
1856 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001857 Label* fall_through = NULL;
1858 PrepareTest(&materialize_true, &materialize_false,
1859 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001860
1861 __ BranchOnSmi(r0, if_false);
1862 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001863 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001864
1865 Apply(context_, if_true, if_false);
1866}
1867
1868
1869void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
1870 ASSERT(args->length() == 1);
1871
1872 VisitForValue(args->at(0), kAccumulator);
1873
1874 Label materialize_true, materialize_false;
1875 Label* if_true = NULL;
1876 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001877 Label* fall_through = NULL;
1878 PrepareTest(&materialize_true, &materialize_false,
1879 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001880
1881 __ BranchOnSmi(r0, if_false);
1882 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001883 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001884
1885 Apply(context_, if_true, if_false);
1886}
1887
1888
1889void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
1890 ASSERT(args->length() == 1);
1891
1892 VisitForValue(args->at(0), kAccumulator);
1893
1894 Label materialize_true, materialize_false;
1895 Label* if_true = NULL;
1896 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001897 Label* fall_through = NULL;
1898 PrepareTest(&materialize_true, &materialize_false,
1899 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001900
1901 __ BranchOnSmi(r0, if_false);
1902 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001903 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001904
1905 Apply(context_, if_true, if_false);
1906}
1907
1908
1909
1910void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
1911 ASSERT(args->length() == 0);
1912
1913 Label materialize_true, materialize_false;
1914 Label* if_true = NULL;
1915 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001916 Label* fall_through = NULL;
1917 PrepareTest(&materialize_true, &materialize_false,
1918 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001919
1920 // Get the frame pointer for the calling frame.
1921 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1922
1923 // Skip the arguments adaptor frame if it exists.
1924 Label check_frame_marker;
1925 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
1926 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1927 __ b(ne, &check_frame_marker);
1928 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
1929
1930 // Check the marker in the calling frame.
1931 __ bind(&check_frame_marker);
1932 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
1933 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001934 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001935
1936 Apply(context_, if_true, if_false);
1937}
1938
1939
1940void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
1941 ASSERT(args->length() == 2);
1942
1943 // Load the two objects into registers and perform the comparison.
1944 VisitForValue(args->at(0), kStack);
1945 VisitForValue(args->at(1), kAccumulator);
1946
1947 Label materialize_true, materialize_false;
1948 Label* if_true = NULL;
1949 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001950 Label* fall_through = NULL;
1951 PrepareTest(&materialize_true, &materialize_false,
1952 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001953
1954 __ pop(r1);
1955 __ cmp(r0, r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001956 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001957
1958 Apply(context_, if_true, if_false);
1959}
1960
1961
1962void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
1963 ASSERT(args->length() == 1);
1964
1965 // ArgumentsAccessStub expects the key in edx and the formal
1966 // parameter count in eax.
1967 VisitForValue(args->at(0), kAccumulator);
1968 __ mov(r1, r0);
1969 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
1970 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
1971 __ CallStub(&stub);
1972 Apply(context_, r0);
1973}
1974
1975
1976void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
1977 ASSERT(args->length() == 0);
1978
1979 Label exit;
1980 // Get the number of formal parameters.
1981 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
1982
1983 // Check if the calling frame is an arguments adaptor frame.
1984 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1985 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1986 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1987 __ b(ne, &exit);
1988
1989 // Arguments adaptor case: Read the arguments length from the
1990 // adaptor frame.
1991 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1992
1993 __ bind(&exit);
1994 Apply(context_, r0);
1995}
1996
1997
1998void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
1999 ASSERT(args->length() == 1);
2000 Label done, null, function, non_function_constructor;
2001
2002 VisitForValue(args->at(0), kAccumulator);
2003
2004 // If the object is a smi, we return null.
2005 __ BranchOnSmi(r0, &null);
2006
2007 // Check that the object is a JS object but take special care of JS
2008 // functions to make sure they have 'Function' as their class.
2009 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2010 __ b(lt, &null);
2011
2012 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2013 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2014 // LAST_JS_OBJECT_TYPE.
2015 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2016 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2017 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2018 __ b(eq, &function);
2019
2020 // Check if the constructor in the map is a function.
2021 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2022 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2023 __ b(ne, &non_function_constructor);
2024
2025 // r0 now contains the constructor function. Grab the
2026 // instance class name from there.
2027 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2028 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2029 __ b(&done);
2030
2031 // Functions have class 'Function'.
2032 __ bind(&function);
2033 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2034 __ jmp(&done);
2035
2036 // Objects with a non-function constructor have class 'Object'.
2037 __ bind(&non_function_constructor);
2038 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2039 __ jmp(&done);
2040
2041 // Non-JS objects have class null.
2042 __ bind(&null);
2043 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2044
2045 // All done.
2046 __ bind(&done);
2047
2048 Apply(context_, r0);
2049}
2050
2051
2052void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2053 // Conditionally generate a log call.
2054 // Args:
2055 // 0 (literal string): The type of logging (corresponds to the flags).
2056 // This is used to determine whether or not to generate the log call.
2057 // 1 (string): Format string. Access the string at argument index 2
2058 // with '%2s' (see Logger::LogRuntime for all the formats).
2059 // 2 (array): Arguments to the format string.
2060 ASSERT_EQ(args->length(), 3);
2061#ifdef ENABLE_LOGGING_AND_PROFILING
2062 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2063 VisitForValue(args->at(1), kStack);
2064 VisitForValue(args->at(2), kStack);
2065 __ CallRuntime(Runtime::kLog, 2);
2066 }
2067#endif
2068 // Finally, we're expected to leave a value on the top of the stack.
2069 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2070 Apply(context_, r0);
2071}
2072
2073
2074void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2075 ASSERT(args->length() == 0);
2076
2077 Label slow_allocate_heapnumber;
2078 Label heapnumber_allocated;
2079
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002080 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
2081 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002082 __ jmp(&heapnumber_allocated);
2083
2084 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002085 // Allocate a heap number.
2086 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002087 __ mov(r4, Operand(r0));
2088
2089 __ bind(&heapnumber_allocated);
2090
2091 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
2092 // by computing:
2093 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2094 if (CpuFeatures::IsSupported(VFP3)) {
2095 __ PrepareCallCFunction(0, r1);
2096 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
2097
2098 CpuFeatures::Scope scope(VFP3);
2099 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2100 // Create this constant using mov/orr to avoid PC relative load.
2101 __ mov(r1, Operand(0x41000000));
2102 __ orr(r1, r1, Operand(0x300000));
2103 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
2104 __ vmov(d7, r0, r1);
2105 // Move 0x4130000000000000 to VFP.
2106 __ mov(r0, Operand(0));
2107 __ vmov(d8, r0, r1);
2108 // Subtract and store the result in the heap number.
2109 __ vsub(d7, d7, d8);
2110 __ sub(r0, r4, Operand(kHeapObjectTag));
2111 __ vstr(d7, r0, HeapNumber::kValueOffset);
2112 __ mov(r0, r4);
2113 } else {
2114 __ mov(r0, Operand(r4));
2115 __ PrepareCallCFunction(1, r1);
2116 __ CallCFunction(
2117 ExternalReference::fill_heap_number_with_random_function(), 1);
2118 }
2119
2120 Apply(context_, r0);
2121}
2122
2123
2124void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
2125 // Load the arguments on the stack and call the stub.
2126 SubStringStub stub;
2127 ASSERT(args->length() == 3);
2128 VisitForValue(args->at(0), kStack);
2129 VisitForValue(args->at(1), kStack);
2130 VisitForValue(args->at(2), kStack);
2131 __ CallStub(&stub);
2132 Apply(context_, r0);
2133}
2134
2135
2136void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
2137 // Load the arguments on the stack and call the stub.
2138 RegExpExecStub stub;
2139 ASSERT(args->length() == 4);
2140 VisitForValue(args->at(0), kStack);
2141 VisitForValue(args->at(1), kStack);
2142 VisitForValue(args->at(2), kStack);
2143 VisitForValue(args->at(3), kStack);
2144 __ CallStub(&stub);
2145 Apply(context_, r0);
2146}
2147
2148
2149void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
2150 ASSERT(args->length() == 1);
2151
2152 VisitForValue(args->at(0), kAccumulator); // Load the object.
2153
2154 Label done;
2155 // If the object is a smi return the object.
2156 __ BranchOnSmi(r0, &done);
2157 // If the object is not a value type, return the object.
2158 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
2159 __ b(ne, &done);
2160 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2161
2162 __ bind(&done);
2163 Apply(context_, r0);
2164}
2165
2166
2167void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
2168 // Load the arguments on the stack and call the runtime function.
2169 ASSERT(args->length() == 2);
2170 VisitForValue(args->at(0), kStack);
2171 VisitForValue(args->at(1), kStack);
2172 __ CallRuntime(Runtime::kMath_pow, 2);
2173 Apply(context_, r0);
2174}
2175
2176
2177void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
2178 ASSERT(args->length() == 2);
2179
2180 VisitForValue(args->at(0), kStack); // Load the object.
2181 VisitForValue(args->at(1), kAccumulator); // Load the value.
2182 __ pop(r1); // r0 = value. r1 = object.
2183
2184 Label done;
2185 // If the object is a smi, return the value.
2186 __ BranchOnSmi(r1, &done);
2187
2188 // If the object is not a value type, return the value.
2189 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
2190 __ b(ne, &done);
2191
2192 // Store the value.
2193 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2194 // Update the write barrier. Save the value as it will be
2195 // overwritten by the write barrier code and is needed afterward.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002196 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002197
2198 __ bind(&done);
2199 Apply(context_, r0);
2200}
2201
2202
2203void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2204 ASSERT_EQ(args->length(), 1);
2205
2206 // Load the argument on the stack and call the stub.
2207 VisitForValue(args->at(0), kStack);
2208
2209 NumberToStringStub stub;
2210 __ CallStub(&stub);
2211 Apply(context_, r0);
2212}
2213
2214
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002215void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002216 ASSERT(args->length() == 1);
2217
2218 VisitForValue(args->at(0), kAccumulator);
2219
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002220 Label done;
2221 StringCharFromCodeGenerator generator(r0, r1);
2222 generator.GenerateFast(masm_);
2223 __ jmp(&done);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002224
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002225 NopRuntimeCallHelper call_helper;
2226 generator.GenerateSlow(masm_, call_helper);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002227
2228 __ bind(&done);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002229 Apply(context_, r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002230}
2231
2232
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002233void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
2234 ASSERT(args->length() == 2);
2235
2236 VisitForValue(args->at(0), kStack);
2237 VisitForValue(args->at(1), kAccumulator);
2238
2239 Register object = r1;
2240 Register index = r0;
2241 Register scratch = r2;
2242 Register result = r3;
2243
2244 __ pop(object);
2245
2246 Label need_conversion;
2247 Label index_out_of_range;
2248 Label done;
2249 StringCharCodeAtGenerator generator(object,
2250 index,
2251 scratch,
2252 result,
2253 &need_conversion,
2254 &need_conversion,
2255 &index_out_of_range,
2256 STRING_INDEX_IS_NUMBER);
2257 generator.GenerateFast(masm_);
2258 __ jmp(&done);
2259
2260 __ bind(&index_out_of_range);
2261 // When the index is out of range, the spec requires us to return
2262 // NaN.
2263 __ LoadRoot(result, Heap::kNanValueRootIndex);
2264 __ jmp(&done);
2265
2266 __ bind(&need_conversion);
2267 // Load the undefined value into the result register, which will
2268 // trigger conversion.
2269 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2270 __ jmp(&done);
2271
2272 NopRuntimeCallHelper call_helper;
2273 generator.GenerateSlow(masm_, call_helper);
2274
2275 __ bind(&done);
2276 Apply(context_, result);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002277}
2278
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002279
2280void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
2281 ASSERT(args->length() == 2);
2282
2283 VisitForValue(args->at(0), kStack);
2284 VisitForValue(args->at(1), kAccumulator);
2285
2286 Register object = r1;
2287 Register index = r0;
2288 Register scratch1 = r2;
2289 Register scratch2 = r3;
2290 Register result = r0;
2291
2292 __ pop(object);
2293
2294 Label need_conversion;
2295 Label index_out_of_range;
2296 Label done;
2297 StringCharAtGenerator generator(object,
2298 index,
2299 scratch1,
2300 scratch2,
2301 result,
2302 &need_conversion,
2303 &need_conversion,
2304 &index_out_of_range,
2305 STRING_INDEX_IS_NUMBER);
2306 generator.GenerateFast(masm_);
2307 __ jmp(&done);
2308
2309 __ bind(&index_out_of_range);
2310 // When the index is out of range, the spec requires us to return
2311 // the empty string.
2312 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2313 __ jmp(&done);
2314
2315 __ bind(&need_conversion);
2316 // Move smi zero into the result register, which will trigger
2317 // conversion.
2318 __ mov(result, Operand(Smi::FromInt(0)));
2319 __ jmp(&done);
2320
2321 NopRuntimeCallHelper call_helper;
2322 generator.GenerateSlow(masm_, call_helper);
2323
2324 __ bind(&done);
2325 Apply(context_, result);
2326}
2327
2328
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002329void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2330 ASSERT_EQ(2, args->length());
2331
2332 VisitForValue(args->at(0), kStack);
2333 VisitForValue(args->at(1), kStack);
2334
2335 StringAddStub stub(NO_STRING_ADD_FLAGS);
2336 __ CallStub(&stub);
2337 Apply(context_, r0);
2338}
2339
2340
2341void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
2342 ASSERT_EQ(2, args->length());
2343
2344 VisitForValue(args->at(0), kStack);
2345 VisitForValue(args->at(1), kStack);
2346
2347 StringCompareStub stub;
2348 __ CallStub(&stub);
2349 Apply(context_, r0);
2350}
2351
2352
2353void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
2354 // Load the argument on the stack and call the runtime.
2355 ASSERT(args->length() == 1);
2356 VisitForValue(args->at(0), kStack);
2357 __ CallRuntime(Runtime::kMath_sin, 1);
2358 Apply(context_, r0);
2359}
2360
2361
2362void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
2363 // Load the argument on the stack and call the runtime.
2364 ASSERT(args->length() == 1);
2365 VisitForValue(args->at(0), kStack);
2366 __ CallRuntime(Runtime::kMath_cos, 1);
2367 Apply(context_, r0);
2368}
2369
2370
2371void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2372 // Load the argument on the stack and call the runtime function.
2373 ASSERT(args->length() == 1);
2374 VisitForValue(args->at(0), kStack);
2375 __ CallRuntime(Runtime::kMath_sqrt, 1);
2376 Apply(context_, r0);
2377}
2378
2379
2380void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2381 ASSERT(args->length() >= 2);
2382
2383 int arg_count = args->length() - 2; // For receiver and function.
2384 VisitForValue(args->at(0), kStack); // Receiver.
2385 for (int i = 0; i < arg_count; i++) {
2386 VisitForValue(args->at(i + 1), kStack);
2387 }
2388 VisitForValue(args->at(arg_count + 1), kAccumulator); // Function.
2389
2390 // InvokeFunction requires function in r1. Move it in there.
2391 if (!result_register().is(r1)) __ mov(r1, result_register());
2392 ParameterCount count(arg_count);
2393 __ InvokeFunction(r1, count, CALL_FUNCTION);
2394 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2395 Apply(context_, r0);
2396}
2397
2398
2399void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2400 ASSERT(args->length() == 3);
2401 VisitForValue(args->at(0), kStack);
2402 VisitForValue(args->at(1), kStack);
2403 VisitForValue(args->at(2), kStack);
2404 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
2405 Apply(context_, r0);
2406}
2407
2408
2409void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2410 ASSERT(args->length() == 3);
2411 VisitForValue(args->at(0), kStack);
2412 VisitForValue(args->at(1), kStack);
2413 VisitForValue(args->at(2), kStack);
2414 __ CallRuntime(Runtime::kSwapElements, 3);
2415 Apply(context_, r0);
2416}
2417
2418
2419void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2420 ASSERT_EQ(2, args->length());
2421
2422 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2423 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2424
2425 Handle<FixedArray> jsfunction_result_caches(
2426 Top::global_context()->jsfunction_result_caches());
2427 if (jsfunction_result_caches->length() <= cache_id) {
2428 __ Abort("Attempt to use undefined cache.");
2429 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2430 Apply(context_, r0);
2431 return;
2432 }
2433
2434 VisitForValue(args->at(1), kAccumulator);
2435
2436 Register key = r0;
2437 Register cache = r1;
2438 __ ldr(cache, CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX));
2439 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
2440 __ ldr(cache,
2441 CodeGenerator::ContextOperand(
2442 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
2443 __ ldr(cache,
2444 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2445
2446
2447 Label done, not_found;
2448 // tmp now holds finger offset as a smi.
2449 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2450 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2451 // r2 now holds finger offset as a smi.
2452 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2453 // r3 now points to the start of fixed array elements.
2454 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2455 // Note side effect of PreIndex: r3 now points to the key of the pair.
2456 __ cmp(key, r2);
2457 __ b(ne, &not_found);
2458
2459 __ ldr(r0, MemOperand(r3, kPointerSize));
2460 __ b(&done);
2461
2462 __ bind(&not_found);
2463 // Call runtime to perform the lookup.
2464 __ Push(cache, key);
2465 __ CallRuntime(Runtime::kGetFromCache, 2);
2466
2467 __ bind(&done);
2468 Apply(context_, r0);
2469}
2470
2471
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002472void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
2473 ASSERT_EQ(2, args->length());
2474
2475 Register right = r0;
2476 Register left = r1;
2477 Register tmp = r2;
2478 Register tmp2 = r3;
2479
2480 VisitForValue(args->at(0), kStack);
2481 VisitForValue(args->at(1), kAccumulator);
2482 __ pop(left);
2483
2484 Label done, fail, ok;
2485 __ cmp(left, Operand(right));
2486 __ b(eq, &ok);
2487 // Fail if either is a non-HeapObject.
2488 __ and_(tmp, left, Operand(right));
2489 __ tst(tmp, Operand(kSmiTagMask));
2490 __ b(eq, &fail);
2491 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
2492 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
2493 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
2494 __ b(ne, &fail);
2495 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
2496 __ cmp(tmp, Operand(tmp2));
2497 __ b(ne, &fail);
2498 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
2499 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
2500 __ cmp(tmp, tmp2);
2501 __ b(eq, &ok);
2502 __ bind(&fail);
2503 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
2504 __ jmp(&done);
2505 __ bind(&ok);
2506 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
2507 __ bind(&done);
2508
2509 Apply(context_, r0);
2510}
2511
2512
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002513void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002514 Handle<String> name = expr->name();
2515 if (name->length() > 0 && name->Get(0) == '_') {
2516 Comment cmnt(masm_, "[ InlineRuntimeCall");
2517 EmitInlineRuntimeCall(expr);
2518 return;
2519 }
2520
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002521 Comment cmnt(masm_, "[ CallRuntime");
2522 ZoneList<Expression*>* args = expr->arguments();
2523
2524 if (expr->is_jsruntime()) {
2525 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002526 __ ldr(r0, CodeGenerator::GlobalObject());
2527 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002528 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002529 }
2530
2531 // Push the arguments ("left-to-right").
2532 int arg_count = args->length();
2533 for (int i = 0; i < arg_count; i++) {
2534 VisitForValue(args->at(i), kStack);
2535 }
2536
2537 if (expr->is_jsruntime()) {
2538 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002539 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002540 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2541 NOT_IN_LOOP);
2542 __ Call(ic, RelocInfo::CODE_TARGET);
2543 // Restore context register.
2544 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002545 } else {
2546 // Call the C runtime function.
2547 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002548 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002549 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002550}
2551
2552
2553void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2554 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002555 case Token::DELETE: {
2556 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2557 Property* prop = expr->expression()->AsProperty();
2558 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2559 if (prop == NULL && var == NULL) {
2560 // Result of deleting non-property, non-variable reference is true.
2561 // The subexpression may have side effects.
2562 VisitForEffect(expr->expression());
2563 Apply(context_, true);
2564 } else if (var != NULL &&
2565 !var->is_global() &&
2566 var->slot() != NULL &&
2567 var->slot()->type() != Slot::LOOKUP) {
2568 // Result of deleting non-global, non-dynamic variables is false.
2569 // The subexpression does not have side effects.
2570 Apply(context_, false);
2571 } else {
2572 // Property or variable reference. Call the delete builtin with
2573 // object and property name as arguments.
2574 if (prop != NULL) {
2575 VisitForValue(prop->obj(), kStack);
2576 VisitForValue(prop->key(), kStack);
2577 } else if (var->is_global()) {
2578 __ ldr(r1, CodeGenerator::GlobalObject());
2579 __ mov(r0, Operand(var->name()));
2580 __ Push(r1, r0);
2581 } else {
2582 // Non-global variable. Call the runtime to look up the context
2583 // where the variable was introduced.
2584 __ push(context_register());
2585 __ mov(r2, Operand(var->name()));
2586 __ push(r2);
2587 __ CallRuntime(Runtime::kLookupContext, 2);
2588 __ push(r0);
2589 __ mov(r2, Operand(var->name()));
2590 __ push(r2);
2591 }
2592 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
2593 Apply(context_, r0);
2594 }
2595 break;
2596 }
2597
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002598 case Token::VOID: {
2599 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2600 VisitForEffect(expr->expression());
2601 switch (context_) {
2602 case Expression::kUninitialized:
2603 UNREACHABLE();
2604 break;
2605 case Expression::kEffect:
2606 break;
2607 case Expression::kValue:
2608 __ LoadRoot(result_register(), Heap::kUndefinedValueRootIndex);
2609 switch (location_) {
2610 case kAccumulator:
2611 break;
2612 case kStack:
2613 __ push(result_register());
2614 break;
2615 }
2616 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002617 case Expression::kTest:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002618 __ jmp(false_label_);
2619 break;
2620 }
2621 break;
2622 }
2623
2624 case Token::NOT: {
2625 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002626 Label materialize_true, materialize_false;
2627 Label* if_true = NULL;
2628 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002629 Label* fall_through = NULL;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002630
2631 // Notice that the labels are swapped.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002632 PrepareTest(&materialize_true, &materialize_false,
2633 &if_false, &if_true, &fall_through);
2634 VisitForControl(expr->expression(), if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002635 Apply(context_, if_false, if_true); // Labels swapped.
2636 break;
2637 }
2638
2639 case Token::TYPEOF: {
2640 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002641 VisitForTypeofValue(expr->expression(), kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002642 __ CallRuntime(Runtime::kTypeof, 1);
2643 Apply(context_, r0);
2644 break;
2645 }
2646
2647 case Token::ADD: {
2648 Comment cmt(masm_, "[ UnaryOperation (ADD)");
2649 VisitForValue(expr->expression(), kAccumulator);
2650 Label no_conversion;
2651 __ tst(result_register(), Operand(kSmiTagMask));
2652 __ b(eq, &no_conversion);
2653 __ push(r0);
2654 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2655 __ bind(&no_conversion);
2656 Apply(context_, result_register());
2657 break;
2658 }
2659
2660 case Token::SUB: {
2661 Comment cmt(masm_, "[ UnaryOperation (SUB)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002662 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002663 UnaryOverwriteMode overwrite =
2664 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002665 GenericUnaryOpStub stub(Token::SUB, overwrite);
2666 // GenericUnaryOpStub expects the argument to be in the
2667 // accumulator register r0.
2668 VisitForValue(expr->expression(), kAccumulator);
2669 __ CallStub(&stub);
2670 Apply(context_, r0);
2671 break;
2672 }
2673
2674 case Token::BIT_NOT: {
2675 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002676 // The generic unary operation stub expects the argument to be
2677 // in the accumulator register r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002678 VisitForValue(expr->expression(), kAccumulator);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002679 Label done;
2680 if (ShouldInlineSmiCase(expr->op())) {
2681 Label call_stub;
2682 __ BranchOnNotSmi(r0, &call_stub);
2683 __ mvn(r0, Operand(r0));
2684 // Bit-clear inverted smi-tag.
2685 __ bic(r0, r0, Operand(kSmiTagMask));
2686 __ b(&done);
2687 __ bind(&call_stub);
2688 }
2689 bool overwrite = expr->expression()->ResultOverwriteAllowed();
2690 UnaryOverwriteMode mode =
2691 overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
2692 GenericUnaryOpStub stub(Token::BIT_NOT, mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002693 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002694 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002695 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002696 break;
2697 }
2698
2699 default:
2700 UNREACHABLE();
2701 }
2702}
2703
2704
2705void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2706 Comment cmnt(masm_, "[ CountOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002707 SetSourcePosition(expr->position());
2708
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002709 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2710 // as the left-hand side.
2711 if (!expr->expression()->IsValidLeftHandSide()) {
2712 VisitForEffect(expr->expression());
2713 return;
2714 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002715
2716 // Expression can only be a property, a global or a (parameter or local)
2717 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2718 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2719 LhsKind assign_type = VARIABLE;
2720 Property* prop = expr->expression()->AsProperty();
2721 // In case of a property we use the uninitialized expression context
2722 // of the key to detect a named property.
2723 if (prop != NULL) {
2724 assign_type =
2725 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2726 }
2727
2728 // Evaluate expression and get value.
2729 if (assign_type == VARIABLE) {
2730 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
2731 Location saved_location = location_;
2732 location_ = kAccumulator;
2733 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
2734 Expression::kValue);
2735 location_ = saved_location;
2736 } else {
2737 // Reserve space for result of postfix operation.
2738 if (expr->is_postfix() && context_ != Expression::kEffect) {
2739 __ mov(ip, Operand(Smi::FromInt(0)));
2740 __ push(ip);
2741 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002742 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002743 // Put the object both on the stack and in the accumulator.
2744 VisitForValue(prop->obj(), kAccumulator);
2745 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002746 EmitNamedPropertyLoad(prop);
2747 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002748 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002749 VisitForValue(prop->key(), kAccumulator);
2750 __ ldr(r1, MemOperand(sp, 0));
2751 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002752 EmitKeyedPropertyLoad(prop);
2753 }
2754 }
2755
2756 // Call ToNumber only if operand is not a smi.
2757 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002758 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002759 __ push(r0);
2760 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2761 __ bind(&no_conversion);
2762
2763 // Save result for postfix expressions.
2764 if (expr->is_postfix()) {
2765 switch (context_) {
2766 case Expression::kUninitialized:
2767 UNREACHABLE();
2768 case Expression::kEffect:
2769 // Do not save result.
2770 break;
2771 case Expression::kValue:
2772 case Expression::kTest:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002773 // Save the result on the stack. If we have a named or keyed property
2774 // we store the result under the receiver that is currently on top
2775 // of the stack.
2776 switch (assign_type) {
2777 case VARIABLE:
2778 __ push(r0);
2779 break;
2780 case NAMED_PROPERTY:
2781 __ str(r0, MemOperand(sp, kPointerSize));
2782 break;
2783 case KEYED_PROPERTY:
2784 __ str(r0, MemOperand(sp, 2 * kPointerSize));
2785 break;
2786 }
2787 break;
2788 }
2789 }
2790
2791
2792 // Inline smi case if we are in a loop.
2793 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002794 int count_value = expr->op() == Token::INC ? 1 : -1;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002795 if (ShouldInlineSmiCase(expr->op())) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002796 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002797 __ b(vs, &stub_call);
2798 // We could eliminate this smi check if we split the code at
2799 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002800 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002801 __ bind(&stub_call);
2802 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002803 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002804 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002805 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00002806 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002807 __ CallStub(&stub);
2808 __ bind(&done);
2809
2810 // Store the value returned in r0.
2811 switch (assign_type) {
2812 case VARIABLE:
2813 if (expr->is_postfix()) {
2814 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002815 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002816 Expression::kEffect);
2817 // For all contexts except kEffect: We have the result on
2818 // top of the stack.
2819 if (context_ != Expression::kEffect) {
2820 ApplyTOS(context_);
2821 }
2822 } else {
2823 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002824 Token::ASSIGN,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002825 context_);
2826 }
2827 break;
2828 case NAMED_PROPERTY: {
2829 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00002830 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002831 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2832 __ Call(ic, RelocInfo::CODE_TARGET);
2833 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002834 if (context_ != Expression::kEffect) {
2835 ApplyTOS(context_);
2836 }
2837 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00002838 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002839 }
2840 break;
2841 }
2842 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002843 __ pop(r1); // Key.
2844 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002845 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
2846 __ Call(ic, RelocInfo::CODE_TARGET);
2847 if (expr->is_postfix()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002848 if (context_ != Expression::kEffect) {
2849 ApplyTOS(context_);
2850 }
2851 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002852 Apply(context_, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002853 }
2854 break;
2855 }
2856 }
2857}
2858
2859
ricow@chromium.org65fae842010-08-25 15:26:24 +00002860void FullCodeGenerator::VisitForTypeofValue(Expression* expr, Location where) {
2861 VariableProxy* proxy = expr->AsVariableProxy();
2862 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
2863 Comment cmnt(masm_, "Global variable");
2864 __ ldr(r0, CodeGenerator::GlobalObject());
2865 __ mov(r2, Operand(proxy->name()));
2866 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2867 // Use a regular load, not a contextual load, to avoid a reference
2868 // error.
2869 __ Call(ic, RelocInfo::CODE_TARGET);
2870 if (where == kStack) __ push(r0);
2871 } else if (proxy != NULL &&
2872 proxy->var()->slot() != NULL &&
2873 proxy->var()->slot()->type() == Slot::LOOKUP) {
2874 __ mov(r0, Operand(proxy->name()));
2875 __ Push(cp, r0);
2876 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
2877 if (where == kStack) __ push(r0);
2878 } else {
2879 // This expression cannot throw a reference error at the top level.
2880 VisitForValue(expr, where);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002881 }
2882}
2883
2884
ricow@chromium.org65fae842010-08-25 15:26:24 +00002885bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
2886 Expression* left,
2887 Expression* right,
2888 Label* if_true,
2889 Label* if_false,
2890 Label* fall_through) {
2891 if (op != Token::EQ && op != Token::EQ_STRICT) return false;
2892
2893 // Check for the pattern: typeof <expression> == <string literal>.
2894 Literal* right_literal = right->AsLiteral();
2895 if (right_literal == NULL) return false;
2896 Handle<Object> right_literal_value = right_literal->handle();
2897 if (!right_literal_value->IsString()) return false;
2898 UnaryOperation* left_unary = left->AsUnaryOperation();
2899 if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
2900 Handle<String> check = Handle<String>::cast(right_literal_value);
2901
2902 VisitForTypeofValue(left_unary->expression(), kAccumulator);
2903 if (check->Equals(Heap::number_symbol())) {
2904 __ tst(r0, Operand(kSmiTagMask));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002905 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002906 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2907 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
2908 __ cmp(r0, ip);
2909 Split(eq, if_true, if_false, fall_through);
2910 } else if (check->Equals(Heap::string_symbol())) {
2911 __ tst(r0, Operand(kSmiTagMask));
2912 __ b(eq, if_false);
2913 // Check for undetectable objects => false.
2914 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2915 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
2916 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
2917 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
2918 __ b(eq, if_false);
2919 __ ldrb(r1, FieldMemOperand(r0, Map::kInstanceTypeOffset));
2920 __ cmp(r1, Operand(FIRST_NONSTRING_TYPE));
2921 Split(lt, if_true, if_false, fall_through);
2922 } else if (check->Equals(Heap::boolean_symbol())) {
2923 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
2924 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002925 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002926 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
2927 __ cmp(r0, ip);
2928 Split(eq, if_true, if_false, fall_through);
2929 } else if (check->Equals(Heap::undefined_symbol())) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002930 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002931 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002932 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002933 __ tst(r0, Operand(kSmiTagMask));
2934 __ b(eq, if_false);
2935 // Check for undetectable objects => true.
2936 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2937 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
2938 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
2939 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
2940 Split(eq, if_true, if_false, fall_through);
2941 } else if (check->Equals(Heap::function_symbol())) {
2942 __ tst(r0, Operand(kSmiTagMask));
2943 __ b(eq, if_false);
2944 __ CompareObjectType(r0, r1, r0, JS_FUNCTION_TYPE);
2945 __ b(eq, if_true);
2946 // Regular expressions => 'function' (they are callable).
2947 __ CompareInstanceType(r1, r0, JS_REGEXP_TYPE);
2948 Split(eq, if_true, if_false, fall_through);
2949 } else if (check->Equals(Heap::object_symbol())) {
2950 __ tst(r0, Operand(kSmiTagMask));
2951 __ b(eq, if_false);
2952 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2953 __ cmp(r0, ip);
2954 __ b(eq, if_true);
2955 // Regular expressions => 'function', not 'object'.
2956 __ CompareObjectType(r0, r1, r0, JS_REGEXP_TYPE);
2957 __ b(eq, if_false);
2958 // Check for undetectable objects => false.
2959 __ ldrb(r0, FieldMemOperand(r1, Map::kBitFieldOffset));
2960 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
2961 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
2962 __ b(eq, if_false);
2963 // Check for JS objects => true.
2964 __ ldrb(r0, FieldMemOperand(r1, Map::kInstanceTypeOffset));
2965 __ cmp(r0, Operand(FIRST_JS_OBJECT_TYPE));
2966 __ b(lt, if_false);
2967 __ cmp(r0, Operand(LAST_JS_OBJECT_TYPE));
2968 Split(le, if_true, if_false, fall_through);
2969 } else {
2970 if (if_false != fall_through) __ jmp(if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002971 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002972
2973 return true;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002974}
2975
2976
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002977void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
2978 Comment cmnt(masm_, "[ CompareOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002979 SetSourcePosition(expr->position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002980
2981 // Always perform the comparison for its control flow. Pack the result
2982 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002983
2984 Label materialize_true, materialize_false;
2985 Label* if_true = NULL;
2986 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002987 Label* fall_through = NULL;
2988 PrepareTest(&materialize_true, &materialize_false,
2989 &if_true, &if_false, &fall_through);
2990
2991 // First we try a fast inlined version of the compare when one of
2992 // the operands is a literal.
2993 Token::Value op = expr->op();
2994 Expression* left = expr->left();
2995 Expression* right = expr->right();
2996 if (TryLiteralCompare(op, left, right, if_true, if_false, fall_through)) {
2997 Apply(context_, if_true, if_false);
2998 return;
2999 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003000
3001 VisitForValue(expr->left(), kStack);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003002 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003003 case Token::IN:
3004 VisitForValue(expr->right(), kStack);
3005 __ InvokeBuiltin(Builtins::IN, CALL_JS);
3006 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3007 __ cmp(r0, ip);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003008 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003009 break;
3010
3011 case Token::INSTANCEOF: {
3012 VisitForValue(expr->right(), kStack);
3013 InstanceofStub stub;
3014 __ CallStub(&stub);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003015 // The stub returns 0 for true.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003016 __ tst(r0, r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003017 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003018 break;
3019 }
3020
3021 default: {
3022 VisitForValue(expr->right(), kAccumulator);
3023 Condition cc = eq;
3024 bool strict = false;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003025 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003026 case Token::EQ_STRICT:
3027 strict = true;
3028 // Fall through
ricow@chromium.org65fae842010-08-25 15:26:24 +00003029 case Token::EQ:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003030 cc = eq;
3031 __ pop(r1);
3032 break;
3033 case Token::LT:
3034 cc = lt;
3035 __ pop(r1);
3036 break;
3037 case Token::GT:
3038 // Reverse left and right sides to obtain ECMA-262 conversion order.
3039 cc = lt;
3040 __ mov(r1, result_register());
3041 __ pop(r0);
3042 break;
3043 case Token::LTE:
3044 // Reverse left and right sides to obtain ECMA-262 conversion order.
3045 cc = ge;
3046 __ mov(r1, result_register());
3047 __ pop(r0);
3048 break;
3049 case Token::GTE:
3050 cc = ge;
3051 __ pop(r1);
3052 break;
3053 case Token::IN:
3054 case Token::INSTANCEOF:
3055 default:
3056 UNREACHABLE();
3057 }
3058
ricow@chromium.org65fae842010-08-25 15:26:24 +00003059 if (ShouldInlineSmiCase(op)) {
3060 Label slow_case;
3061 __ orr(r2, r0, Operand(r1));
3062 __ BranchOnNotSmi(r2, &slow_case);
3063 __ cmp(r1, r0);
3064 Split(cc, if_true, if_false, NULL);
3065 __ bind(&slow_case);
3066 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003067
ager@chromium.orgb5737492010-07-15 09:29:43 +00003068 CompareStub stub(cc, strict, kBothCouldBeNaN, true, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003069 __ CallStub(&stub);
3070 __ cmp(r0, Operand(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003071 Split(cc, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003072 }
3073 }
3074
3075 // Convert the result of the comparison into one expected for this
3076 // expression's context.
3077 Apply(context_, if_true, if_false);
3078}
3079
3080
ricow@chromium.org65fae842010-08-25 15:26:24 +00003081void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
3082 Comment cmnt(masm_, "[ CompareToNull");
3083 Label materialize_true, materialize_false;
3084 Label* if_true = NULL;
3085 Label* if_false = NULL;
3086 Label* fall_through = NULL;
3087 PrepareTest(&materialize_true, &materialize_false,
3088 &if_true, &if_false, &fall_through);
3089
3090 VisitForValue(expr->expression(), kAccumulator);
3091 __ LoadRoot(r1, Heap::kNullValueRootIndex);
3092 __ cmp(r0, r1);
3093 if (expr->is_strict()) {
3094 Split(eq, if_true, if_false, fall_through);
3095 } else {
3096 __ b(eq, if_true);
3097 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
3098 __ cmp(r0, r1);
3099 __ b(eq, if_true);
3100 __ tst(r0, Operand(kSmiTagMask));
3101 __ b(eq, if_false);
3102 // It can be an undetectable object.
3103 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3104 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3105 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3106 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3107 Split(eq, if_true, if_false, fall_through);
3108 }
3109 Apply(context_, if_true, if_false);
3110}
3111
3112
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003113void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3114 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3115 Apply(context_, r0);
3116}
3117
3118
3119Register FullCodeGenerator::result_register() { return r0; }
3120
3121
3122Register FullCodeGenerator::context_register() { return cp; }
3123
3124
3125void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3126 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3127 __ str(value, MemOperand(fp, frame_offset));
3128}
3129
3130
3131void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
3132 __ ldr(dst, CodeGenerator::ContextOperand(cp, context_index));
3133}
3134
3135
3136// ----------------------------------------------------------------------------
3137// Non-local control flow support.
3138
3139void FullCodeGenerator::EnterFinallyBlock() {
3140 ASSERT(!result_register().is(r1));
3141 // Store result register while executing finally block.
3142 __ push(result_register());
3143 // Cook return address in link register to stack (smi encoded Code* delta)
3144 __ sub(r1, lr, Operand(masm_->CodeObject()));
3145 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3146 ASSERT_EQ(0, kSmiTag);
3147 __ add(r1, r1, Operand(r1)); // Convert to smi.
3148 __ push(r1);
3149}
3150
3151
3152void FullCodeGenerator::ExitFinallyBlock() {
3153 ASSERT(!result_register().is(r1));
3154 // Restore result register from stack.
3155 __ pop(r1);
3156 // Uncook return address and return.
3157 __ pop(result_register());
3158 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3159 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3160 __ add(pc, r1, Operand(masm_->CodeObject()));
3161}
3162
3163
3164#undef __
3165
3166} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003167
3168#endif // V8_TARGET_ARCH_ARM