blob: 9fc0c096bbb1f5c8941e3a8bb46874733c7aa6a4 [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000030#if defined(V8_TARGET_ARCH_ARM)
31
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000032#include "code-stubs.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000033#include "codegen-inl.h"
34#include "compiler.h"
35#include "debug.h"
36#include "full-codegen.h"
37#include "parser.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000038#include "scopes.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000039
40namespace v8 {
41namespace internal {
42
43#define __ ACCESS_MASM(masm_)
44
45// Generate code for a JS function. On entry to the function the receiver
46// and arguments have been pushed on the stack left to right. The actual
47// argument count matches the formal parameter count expected by the
48// function.
49//
50// The live registers are:
51// o r1: the JS function object being called (ie, ourselves)
52// o cp: our context
53// o fp: our caller's frame pointer
54// o sp: stack pointer
55// o lr: return address
56//
57// The function builds a JS frame. Please see JavaScriptFrameConstants in
58// frames-arm.h for its layout.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000059void FullCodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.org5c838252010-02-19 08:53:10 +000060 ASSERT(info_ == NULL);
61 info_ = info;
62 SetFunctionPosition(function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000063 Comment cmnt(masm_, "[ function compiled by full code generator");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000064
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000065 int locals_count = scope()->num_stack_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000066
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000067 __ Push(lr, fp, cp, r1);
68 if (locals_count > 0) {
69 // Load undefined value here, so the value is ready for the loop
70 // below.
71 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
72 }
73 // Adjust fp to point to caller's fp.
74 __ add(fp, sp, Operand(2 * kPointerSize));
75
76 { Comment cmnt(masm_, "[ Allocate locals");
77 for (int i = 0; i < locals_count; i++) {
78 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000079 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000080 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000081
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000082 bool function_in_register = true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000083
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000084 // Possibly allocate a local context.
85 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
86 if (heap_slots > 0) {
87 Comment cmnt(masm_, "[ Allocate local context");
88 // Argument to NewContext is the function, which is in r1.
89 __ push(r1);
90 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
91 FastNewContextStub stub(heap_slots);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000092 __ CallStub(&stub);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000093 } else {
94 __ CallRuntime(Runtime::kNewContext, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000095 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000096 function_in_register = false;
97 // Context is returned in both r0 and cp. It replaces the context
98 // passed to us. It's saved in the stack and kept live in cp.
99 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
100 // Copy any necessary parameters into the context.
101 int num_parameters = scope()->num_parameters();
102 for (int i = 0; i < num_parameters; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000103 Slot* slot = scope()->parameter(i)->AsSlot();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000104 if (slot != NULL && slot->type() == Slot::CONTEXT) {
105 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
106 (num_parameters - 1 - i) * kPointerSize;
107 // Load parameter from stack.
108 __ ldr(r0, MemOperand(fp, parameter_offset));
109 // Store it in the context.
110 __ mov(r1, Operand(Context::SlotOffset(slot->index())));
111 __ str(r0, MemOperand(cp, r1));
112 // Update the write barrier. This clobbers all involved
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
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000121 Variable* arguments = scope()->arguments();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000122 if (arguments != NULL) {
123 // Function uses arguments object.
124 Comment cmnt(masm_, "[ Allocate arguments object");
125 if (!function_in_register) {
126 // Load this again, if it's used by the local context below.
127 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
128 } else {
129 __ mov(r3, r1);
130 }
131 // Receiver is just before the parameters on the caller's stack.
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);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000146 Move(arguments->AsSlot(), r0, r1, r2);
147 Slot* dot_arguments_slot = scope()->arguments_shadow()->AsSlot();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000148 Move(dot_arguments_slot, r3, r1, r2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000149 }
150
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000151 { Comment cmnt(masm_, "[ Declarations");
152 // For named function expressions, declare the function name as a
153 // constant.
154 if (scope()->is_function_scope() && scope()->function() != NULL) {
155 EmitDeclaration(scope()->function(), Variable::CONST, NULL);
156 }
157 // Visit all the explicit declarations unless there is an illegal
158 // redeclaration.
159 if (scope()->HasIllegalRedeclaration()) {
160 scope()->VisitIllegalRedeclaration(this);
161 } else {
162 VisitDeclarations(scope()->declarations());
163 }
164 }
165
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000166 // Check the stack for overflow or break request.
167 // Put the lr setup instruction in the delay slot. The kInstrSize is
168 // added to the implicit 8 byte offset that always applies to operations
169 // with pc and gives a return address 12 bytes down.
170 { Comment cmnt(masm_, "[ Stack check");
171 __ LoadRoot(r2, Heap::kStackLimitRootIndex);
172 __ add(lr, pc, Operand(Assembler::kInstrSize));
173 __ cmp(sp, Operand(r2));
174 StackCheckStub stub;
175 __ mov(pc,
176 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
177 RelocInfo::CODE_TARGET),
178 LeaveCC,
179 lo);
180 }
181
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000182 if (FLAG_trace) {
183 __ CallRuntime(Runtime::kTraceEnter, 0);
184 }
185
186 { Comment cmnt(masm_, "[ Body");
187 ASSERT(loop_depth() == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000188 VisitStatements(function()->body());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000189 ASSERT(loop_depth() == 0);
190 }
191
192 { Comment cmnt(masm_, "[ return <undefined>;");
193 // Emit a 'return undefined' in case control fell off the end of the
194 // body.
195 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
196 }
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000197 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000198}
199
200
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000201void FullCodeGenerator::EmitReturnSequence() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000202 Comment cmnt(masm_, "[ Return sequence");
203 if (return_label_.is_bound()) {
204 __ b(&return_label_);
205 } else {
206 __ bind(&return_label_);
207 if (FLAG_trace) {
208 // Push the return value on the stack as the parameter.
209 // Runtime::TraceExit returns its parameter in r0.
210 __ push(r0);
211 __ CallRuntime(Runtime::kTraceExit, 1);
212 }
213
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000214#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000215 // Add a label for checking the size of the code used for returning.
216 Label check_exit_codesize;
217 masm_->bind(&check_exit_codesize);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000218#endif
219 // Make sure that the constant pool is not emitted inside of the return
220 // sequence.
221 { Assembler::BlockConstPoolScope block_const_pool(masm_);
222 // Here we use masm_-> instead of the __ macro to avoid the code coverage
223 // tool from instrumenting as we rely on the code size here.
224 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000225 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000226 __ RecordJSReturn();
227 masm_->mov(sp, fp);
228 masm_->ldm(ia_w, sp, fp.bit() | lr.bit());
229 masm_->add(sp, sp, Operand(sp_delta));
230 masm_->Jump(lr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000231 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000232
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000233#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000234 // Check that the size of the code used for returning matches what is
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000235 // expected by the debugger. If the sp_delts above cannot be encoded in the
236 // add instruction the add will generate two instructions.
237 int return_sequence_length =
238 masm_->InstructionsGeneratedSince(&check_exit_codesize);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000239 CHECK(return_sequence_length ==
240 Assembler::kJSReturnSequenceInstructions ||
241 return_sequence_length ==
242 Assembler::kJSReturnSequenceInstructions + 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000243#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000244 }
245}
246
247
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000248FullCodeGenerator::ConstantOperand FullCodeGenerator::GetConstantOperand(
249 Token::Value op, Expression* left, Expression* right) {
250 ASSERT(ShouldInlineSmiCase(op));
251 return kNoConstants;
252}
253
254
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000255void FullCodeGenerator::EffectContext::Plug(Slot* slot) const {
256}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000257
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000258
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000259void FullCodeGenerator::AccumulatorValueContext::Plug(Slot* slot) const {
260 codegen()->Move(result_register(), slot);
261}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000262
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000263
264void FullCodeGenerator::StackValueContext::Plug(Slot* slot) const {
265 codegen()->Move(result_register(), slot);
266 __ push(result_register());
267}
268
269
270void FullCodeGenerator::TestContext::Plug(Slot* slot) const {
271 // For simplicity we always test the accumulator register.
272 codegen()->Move(result_register(), slot);
273 codegen()->DoTest(true_label_, false_label_, fall_through_);
274}
275
276
277void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
278}
279
280
281void FullCodeGenerator::AccumulatorValueContext::Plug(
282 Heap::RootListIndex index) const {
283 __ LoadRoot(result_register(), index);
284}
285
286
287void FullCodeGenerator::StackValueContext::Plug(
288 Heap::RootListIndex index) const {
289 __ LoadRoot(result_register(), index);
290 __ push(result_register());
291}
292
293
294void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
295 if (index == Heap::kUndefinedValueRootIndex ||
296 index == Heap::kNullValueRootIndex ||
297 index == Heap::kFalseValueRootIndex) {
298 __ b(false_label_);
299 } else if (index == Heap::kTrueValueRootIndex) {
300 __ b(true_label_);
301 } else {
302 __ LoadRoot(result_register(), index);
303 codegen()->DoTest(true_label_, false_label_, fall_through_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000304 }
305}
306
307
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000308void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
309}
310
311
312void FullCodeGenerator::AccumulatorValueContext::Plug(
313 Handle<Object> lit) const {
314 __ mov(result_register(), Operand(lit));
315}
316
317
318void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
319 // Immediates can be pushed directly.
320 __ mov(result_register(), Operand(lit));
321 __ push(result_register());
322}
323
324
325void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
326 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
327 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
328 __ b(false_label_);
329 } else if (lit->IsTrue() || lit->IsJSObject()) {
330 __ b(true_label_);
331 } else if (lit->IsString()) {
332 if (String::cast(*lit)->length() == 0) {
333 __ b(false_label_);
334 } else {
335 __ b(true_label_);
336 }
337 } else if (lit->IsSmi()) {
338 if (Smi::cast(*lit)->value() == 0) {
339 __ b(false_label_);
340 } else {
341 __ b(true_label_);
342 }
343 } else {
344 // For simplicity we always test the accumulator register.
345 __ mov(result_register(), Operand(lit));
346 codegen()->DoTest(true_label_, false_label_, fall_through_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000347 }
348}
349
350
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000351void FullCodeGenerator::EffectContext::DropAndPlug(int count,
352 Register reg) const {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000353 ASSERT(count > 0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000354 __ Drop(count);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000355}
356
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000357
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000358void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
359 int count,
360 Register reg) const {
361 ASSERT(count > 0);
362 __ Drop(count);
363 __ Move(result_register(), reg);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000364}
365
366
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000367void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
368 Register reg) const {
369 ASSERT(count > 0);
370 if (count > 1) __ Drop(count - 1);
371 __ str(reg, MemOperand(sp, 0));
372}
373
374
375void FullCodeGenerator::TestContext::DropAndPlug(int count,
376 Register reg) const {
377 ASSERT(count > 0);
378 // For simplicity we always test the accumulator register.
379 __ Drop(count);
380 __ Move(result_register(), reg);
381 codegen()->DoTest(true_label_, false_label_, fall_through_);
382}
383
384
385void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
386 Label* materialize_false) const {
387 ASSERT_EQ(materialize_true, materialize_false);
388 __ bind(materialize_true);
389}
390
391
392void FullCodeGenerator::AccumulatorValueContext::Plug(
393 Label* materialize_true,
394 Label* materialize_false) const {
395 Label done;
396 __ bind(materialize_true);
397 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
398 __ jmp(&done);
399 __ bind(materialize_false);
400 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
401 __ bind(&done);
402}
403
404
405void FullCodeGenerator::StackValueContext::Plug(
406 Label* materialize_true,
407 Label* materialize_false) const {
408 Label done;
409 __ bind(materialize_true);
410 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
411 __ push(ip);
412 __ jmp(&done);
413 __ bind(materialize_false);
414 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
415 __ push(ip);
416 __ bind(&done);
417}
418
419
420void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
421 Label* materialize_false) const {
422 ASSERT(materialize_false == false_label_);
423 ASSERT(materialize_true == true_label_);
424}
425
426
427void FullCodeGenerator::EffectContext::Plug(bool flag) const {
428}
429
430
431void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
432 Heap::RootListIndex value_root_index =
433 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
434 __ LoadRoot(result_register(), value_root_index);
435}
436
437
438void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
439 Heap::RootListIndex value_root_index =
440 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
441 __ LoadRoot(ip, value_root_index);
442 __ push(ip);
443}
444
445
446void FullCodeGenerator::TestContext::Plug(bool flag) const {
447 if (flag) {
448 if (true_label_ != fall_through_) __ b(true_label_);
449 } else {
450 if (false_label_ != fall_through_) __ b(false_label_);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000451 }
452}
453
454
ricow@chromium.org65fae842010-08-25 15:26:24 +0000455void FullCodeGenerator::DoTest(Label* if_true,
456 Label* if_false,
457 Label* fall_through) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000458 // Call the runtime to find the boolean value of the source and then
459 // translate it into control flow to the pair of labels.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000460 __ push(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000461 __ CallRuntime(Runtime::kToBool, 1);
462 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
463 __ cmp(r0, ip);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000464 Split(eq, if_true, if_false, fall_through);
465}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000466
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000467
ricow@chromium.org65fae842010-08-25 15:26:24 +0000468void FullCodeGenerator::Split(Condition cc,
469 Label* if_true,
470 Label* if_false,
471 Label* fall_through) {
472 if (if_false == fall_through) {
473 __ b(cc, if_true);
474 } else if (if_true == fall_through) {
475 __ b(NegateCondition(cc), if_false);
476 } else {
477 __ b(cc, if_true);
478 __ b(if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000479 }
480}
481
482
483MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
484 switch (slot->type()) {
485 case Slot::PARAMETER:
486 case Slot::LOCAL:
487 return MemOperand(fp, SlotOffset(slot));
488 case Slot::CONTEXT: {
489 int context_chain_length =
ager@chromium.org5c838252010-02-19 08:53:10 +0000490 scope()->ContextChainLength(slot->var()->scope());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000491 __ LoadContext(scratch, context_chain_length);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000492 return ContextOperand(scratch, slot->index());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000493 }
494 case Slot::LOOKUP:
495 UNREACHABLE();
496 }
497 UNREACHABLE();
498 return MemOperand(r0, 0);
499}
500
501
502void FullCodeGenerator::Move(Register destination, Slot* source) {
503 // Use destination as scratch.
504 MemOperand slot_operand = EmitSlotSearch(source, destination);
505 __ ldr(destination, slot_operand);
506}
507
508
509void FullCodeGenerator::Move(Slot* dst,
510 Register src,
511 Register scratch1,
512 Register scratch2) {
513 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
514 ASSERT(!scratch1.is(src) && !scratch2.is(src));
515 MemOperand location = EmitSlotSearch(dst, scratch1);
516 __ str(src, location);
517 // Emit the write barrier code if the location is in the heap.
518 if (dst->type() == Slot::CONTEXT) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000519 __ RecordWrite(scratch1,
520 Operand(Context::SlotOffset(dst->index())),
521 scratch2,
522 src);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000523 }
524}
525
526
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000527void FullCodeGenerator::EmitDeclaration(Variable* variable,
528 Variable::Mode mode,
529 FunctionLiteral* function) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000530 Comment cmnt(masm_, "[ Declaration");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000531 ASSERT(variable != NULL); // Must have been resolved.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000532 Slot* slot = variable->AsSlot();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000533 Property* prop = variable->AsProperty();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000534
535 if (slot != NULL) {
536 switch (slot->type()) {
537 case Slot::PARAMETER:
538 case Slot::LOCAL:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000539 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000540 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
541 __ str(ip, MemOperand(fp, SlotOffset(slot)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000542 } else if (function != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000543 VisitForAccumulatorValue(function);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000544 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
545 }
546 break;
547
548 case Slot::CONTEXT:
549 // We bypass the general EmitSlotSearch because we know more about
550 // this specific context.
551
552 // The variable in the decl always resides in the current context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000553 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000554 if (FLAG_debug_code) {
555 // Check if we have the correct context pointer.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000556 __ ldr(r1, ContextOperand(cp, Context::FCONTEXT_INDEX));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000557 __ cmp(r1, cp);
558 __ Check(eq, "Unexpected declaration in current context.");
559 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000560 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000561 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000562 __ str(ip, ContextOperand(cp, slot->index()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000563 // No write barrier since the_hole_value is in old space.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000564 } else if (function != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000565 VisitForAccumulatorValue(function);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000566 __ str(result_register(), ContextOperand(cp, slot->index()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000567 int offset = Context::SlotOffset(slot->index());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000568 // We know that we have written a function, which is not a smi.
569 __ mov(r1, Operand(cp));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000570 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000571 }
572 break;
573
574 case Slot::LOOKUP: {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000575 __ mov(r2, Operand(variable->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000576 // Declaration nodes are always introduced in one of two modes.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000577 ASSERT(mode == Variable::VAR ||
578 mode == Variable::CONST);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000579 PropertyAttributes attr =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000580 (mode == Variable::VAR) ? NONE : READ_ONLY;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000581 __ mov(r1, Operand(Smi::FromInt(attr)));
582 // Push initial value, if any.
583 // Note: For variables we must not push an initial value (such as
584 // 'undefined') because we may have a (legal) redeclaration and we
585 // must not destroy the current value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000586 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000587 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000588 __ Push(cp, r2, r1, r0);
589 } else if (function != NULL) {
590 __ Push(cp, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000591 // Push initial value for function declaration.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000592 VisitForStackValue(function);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000593 } else {
594 __ mov(r0, Operand(Smi::FromInt(0))); // No initial value!
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000595 __ Push(cp, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000596 }
597 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
598 break;
599 }
600 }
601
602 } else if (prop != NULL) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000603 if (function != NULL || mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000604 // We are declaring a function or constant that rewrites to a
605 // property. Use (keyed) IC to set the initial value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000606 VisitForStackValue(prop->obj());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000607 if (function != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000608 VisitForStackValue(prop->key());
609 VisitForAccumulatorValue(function);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000610 __ pop(r1); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000611 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000612 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000613 __ mov(r1, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000614 __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex);
615 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000616 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000617
618 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000619 EmitCallIC(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000620 // Value in r0 is ignored (declarations are statements).
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000621 }
622 }
623}
624
625
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000626void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
627 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
628}
629
630
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000631void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
632 // Call the runtime to declare the globals.
633 // The context is the first argument.
634 __ mov(r1, Operand(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000635 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000636 __ Push(cp, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000637 __ CallRuntime(Runtime::kDeclareGlobals, 3);
638 // Return value is ignored.
639}
640
641
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000642void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000643 Comment cmnt(masm_, "[ SwitchStatement");
644 Breakable nested_statement(this, stmt);
645 SetStatementPosition(stmt);
646 // Keep the switch value on the stack until a case matches.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000647 VisitForStackValue(stmt->tag());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000648
649 ZoneList<CaseClause*>* clauses = stmt->cases();
650 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
651
652 Label next_test; // Recycled for each test.
653 // Compile all the tests with branches to their bodies.
654 for (int i = 0; i < clauses->length(); i++) {
655 CaseClause* clause = clauses->at(i);
656 // The default is not a test, but remember it as final fall through.
657 if (clause->is_default()) {
658 default_clause = clause;
659 continue;
660 }
661
662 Comment cmnt(masm_, "[ Case comparison");
663 __ bind(&next_test);
664 next_test.Unuse();
665
666 // Compile the label expression.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000667 VisitForAccumulatorValue(clause->label());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000668
ricow@chromium.org65fae842010-08-25 15:26:24 +0000669 // Perform the comparison as if via '==='.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000670 __ ldr(r1, MemOperand(sp, 0)); // Switch value.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000671 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
672 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000673 Label slow_case;
674 __ orr(r2, r1, r0);
675 __ tst(r2, Operand(kSmiTagMask));
676 __ b(ne, &slow_case);
677 __ cmp(r1, r0);
678 __ b(ne, &next_test);
679 __ Drop(1); // Switch value is no longer needed.
680 __ b(clause->body_target()->entry_label());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000681 __ bind(&slow_case);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000682 }
683
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000684 CompareFlags flags = inline_smi_code
685 ? NO_SMI_COMPARE_IN_STUB
686 : NO_COMPARE_FLAGS;
687 CompareStub stub(eq, true, flags, r1, r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000688 __ CallStub(&stub);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000689 __ cmp(r0, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000690 __ b(ne, &next_test);
691 __ Drop(1); // Switch value is no longer needed.
692 __ b(clause->body_target()->entry_label());
693 }
694
695 // Discard the test value and jump to the default if present, otherwise to
696 // the end of the statement.
697 __ bind(&next_test);
698 __ Drop(1); // Switch value is no longer needed.
699 if (default_clause == NULL) {
700 __ b(nested_statement.break_target());
701 } else {
702 __ b(default_clause->body_target()->entry_label());
703 }
704
705 // Compile all the case bodies.
706 for (int i = 0; i < clauses->length(); i++) {
707 Comment cmnt(masm_, "[ Case body");
708 CaseClause* clause = clauses->at(i);
709 __ bind(clause->body_target()->entry_label());
710 VisitStatements(clause->statements());
711 }
712
713 __ bind(nested_statement.break_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000714}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000715
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000716
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000717void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000718 Comment cmnt(masm_, "[ ForInStatement");
719 SetStatementPosition(stmt);
720
721 Label loop, exit;
722 ForIn loop_statement(this, stmt);
723 increment_loop_depth();
724
725 // Get the object to enumerate over. Both SpiderMonkey and JSC
726 // ignore null and undefined in contrast to the specification; see
727 // ECMA-262 section 12.6.4.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000728 VisitForAccumulatorValue(stmt->enumerable());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000729 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
730 __ cmp(r0, ip);
731 __ b(eq, &exit);
732 __ LoadRoot(ip, Heap::kNullValueRootIndex);
733 __ cmp(r0, ip);
734 __ b(eq, &exit);
735
736 // Convert the object to a JS object.
737 Label convert, done_convert;
738 __ BranchOnSmi(r0, &convert);
739 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
740 __ b(hs, &done_convert);
741 __ bind(&convert);
742 __ push(r0);
743 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
744 __ bind(&done_convert);
745 __ push(r0);
746
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000747 // BUG(867): Check cache validity in generated code. This is a fast
748 // case for the JSObject::IsSimpleEnum cache validity checks. If we
749 // cannot guarantee cache validity, call the runtime system to check
750 // cache validity or get the property names in a fixed array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000751
752 // Get the set of properties to enumerate.
753 __ push(r0); // Duplicate the enumerable object on the stack.
754 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
755
756 // If we got a map from the runtime call, we can do a fast
757 // modification check. Otherwise, we got a fixed array, and we have
758 // to do a slow check.
759 Label fixed_array;
760 __ mov(r2, r0);
761 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
762 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
763 __ cmp(r1, ip);
764 __ b(ne, &fixed_array);
765
766 // We got a map in register r0. Get the enumeration cache from it.
767 __ ldr(r1, FieldMemOperand(r0, Map::kInstanceDescriptorsOffset));
768 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
769 __ ldr(r2, FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
770
771 // Setup the four remaining stack slots.
772 __ push(r0); // Map.
773 __ ldr(r1, FieldMemOperand(r2, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000774 __ mov(r0, Operand(Smi::FromInt(0)));
775 // Push enumeration cache, enumeration cache length (as smi) and zero.
776 __ Push(r2, r1, r0);
777 __ jmp(&loop);
778
779 // We got a fixed array in register r0. Iterate through that.
780 __ bind(&fixed_array);
781 __ mov(r1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
782 __ Push(r1, r0);
783 __ ldr(r1, FieldMemOperand(r0, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000784 __ mov(r0, Operand(Smi::FromInt(0)));
785 __ Push(r1, r0); // Fixed array length (as smi) and initial index.
786
787 // Generate code for doing the condition check.
788 __ bind(&loop);
789 // Load the current count to r0, load the length to r1.
790 __ Ldrd(r0, r1, MemOperand(sp, 0 * kPointerSize));
791 __ cmp(r0, r1); // Compare to the array length.
792 __ b(hs, loop_statement.break_target());
793
794 // Get the current entry of the array into register r3.
795 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
796 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
797 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
798
799 // Get the expected map from the stack or a zero map in the
800 // permanent slow case into register r2.
801 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
802
803 // Check if the expected map still matches that of the enumerable.
804 // If not, we have to filter the key.
805 Label update_each;
806 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
807 __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
808 __ cmp(r4, Operand(r2));
809 __ b(eq, &update_each);
810
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000811 // Convert the entry to a string or (smi) 0 if it isn't a property
812 // any more. If the property has been removed while iterating, we
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000813 // just skip it.
814 __ push(r1); // Enumerable.
815 __ push(r3); // Current entry.
816 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000817 __ mov(r3, Operand(r0), SetCC);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000818 __ b(eq, loop_statement.continue_target());
819
820 // Update the 'each' property or variable from the possibly filtered
821 // entry in register r3.
822 __ bind(&update_each);
823 __ mov(result_register(), r3);
824 // Perform the assignment as if via '='.
825 EmitAssignment(stmt->each());
826
827 // Generate code for the body of the loop.
828 Label stack_limit_hit, stack_check_done;
829 Visit(stmt->body());
830
831 __ StackLimitCheck(&stack_limit_hit);
832 __ bind(&stack_check_done);
833
834 // Generate code for the going to the next element by incrementing
835 // the index (smi) stored on top of the stack.
836 __ bind(loop_statement.continue_target());
837 __ pop(r0);
838 __ add(r0, r0, Operand(Smi::FromInt(1)));
839 __ push(r0);
840 __ b(&loop);
841
842 // Slow case for the stack limit check.
843 StackCheckStub stack_check_stub;
844 __ bind(&stack_limit_hit);
845 __ CallStub(&stack_check_stub);
846 __ b(&stack_check_done);
847
848 // Remove the pointers stored on the stack.
849 __ bind(loop_statement.break_target());
850 __ Drop(5);
851
852 // Exit and decrement the loop depth.
853 __ bind(&exit);
854 decrement_loop_depth();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000855}
856
857
858void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info) {
859 // Use the fast case closure allocation code that allocates in new
860 // space for nested functions that don't need literals cloning.
861 if (scope()->is_function_scope() && info->num_literals() == 0) {
862 FastNewClosureStub stub;
863 __ mov(r0, Operand(info));
864 __ push(r0);
865 __ CallStub(&stub);
866 } else {
867 __ mov(r0, Operand(info));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000868 __ Push(cp, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000869 __ CallRuntime(Runtime::kNewClosure, 2);
870 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000871 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000872}
873
874
875void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
876 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000877 EmitVariableLoad(expr->var());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000878}
879
880
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000881MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(
882 Slot* slot,
883 Label* slow) {
884 ASSERT(slot->type() == Slot::CONTEXT);
885 Register current = cp;
886 Register next = r3;
887 Register temp = r4;
888
889 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
890 if (s->num_heap_slots() > 0) {
891 if (s->calls_eval()) {
892 // Check that extension is NULL.
893 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
894 __ tst(temp, temp);
895 __ b(ne, slow);
896 }
897 __ ldr(next, ContextOperand(current, Context::CLOSURE_INDEX));
898 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
899 // Walk the rest of the chain without clobbering cp.
900 current = next;
901 }
902 }
903 // Check that last extension is NULL.
904 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
905 __ tst(temp, temp);
906 __ b(ne, slow);
907 __ ldr(temp, ContextOperand(current, Context::FCONTEXT_INDEX));
908 return ContextOperand(temp, slot->index());
909}
910
911
912void FullCodeGenerator::EmitDynamicLoadFromSlotFastCase(
913 Slot* slot,
914 TypeofState typeof_state,
915 Label* slow,
916 Label* done) {
917 // Generate fast-case code for variables that might be shadowed by
918 // eval-introduced variables. Eval is used a lot without
919 // introducing variables. In those cases, we do not want to
920 // perform a runtime call for all variables in the scope
921 // containing the eval.
922 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
923 EmitLoadGlobalSlotCheckExtensions(slot, typeof_state, slow);
924 __ jmp(done);
925 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000926 Slot* potential_slot = slot->var()->local_if_not_shadowed()->AsSlot();
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000927 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
928 if (potential_slot != NULL) {
929 // Generate fast case for locals that rewrite to slots.
930 __ ldr(r0, ContextSlotOperandCheckExtensions(potential_slot, slow));
931 if (potential_slot->var()->mode() == Variable::CONST) {
932 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
933 __ cmp(r0, ip);
934 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
935 }
936 __ jmp(done);
937 } else if (rewrite != NULL) {
938 // Generate fast case for calls of an argument function.
939 Property* property = rewrite->AsProperty();
940 if (property != NULL) {
941 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
942 Literal* key_literal = property->key()->AsLiteral();
943 if (obj_proxy != NULL &&
944 key_literal != NULL &&
945 obj_proxy->IsArguments() &&
946 key_literal->handle()->IsSmi()) {
947 // Load arguments object if there are no eval-introduced
948 // variables. Then load the argument from the arguments
949 // object using keyed load.
950 __ ldr(r1,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000951 ContextSlotOperandCheckExtensions(obj_proxy->var()->AsSlot(),
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000952 slow));
953 __ mov(r0, Operand(key_literal->handle()));
954 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000955 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000956 __ jmp(done);
957 }
958 }
959 }
960 }
961}
962
963
964void FullCodeGenerator::EmitLoadGlobalSlotCheckExtensions(
965 Slot* slot,
966 TypeofState typeof_state,
967 Label* slow) {
968 Register current = cp;
969 Register next = r1;
970 Register temp = r2;
971
972 Scope* s = scope();
973 while (s != NULL) {
974 if (s->num_heap_slots() > 0) {
975 if (s->calls_eval()) {
976 // Check that extension is NULL.
977 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
978 __ tst(temp, temp);
979 __ b(ne, slow);
980 }
981 // Load next context in chain.
982 __ ldr(next, ContextOperand(current, Context::CLOSURE_INDEX));
983 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
984 // Walk the rest of the chain without clobbering cp.
985 current = next;
986 }
987 // If no outer scope calls eval, we do not need to check more
988 // context extensions.
989 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
990 s = s->outer_scope();
991 }
992
993 if (s->is_eval_scope()) {
994 Label loop, fast;
995 if (!current.is(next)) {
996 __ Move(next, current);
997 }
998 __ bind(&loop);
999 // Terminate at global context.
1000 __ ldr(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1001 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1002 __ cmp(temp, ip);
1003 __ b(eq, &fast);
1004 // Check that extension is NULL.
1005 __ ldr(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1006 __ tst(temp, temp);
1007 __ b(ne, slow);
1008 // Load next context in chain.
1009 __ ldr(next, ContextOperand(next, Context::CLOSURE_INDEX));
1010 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
1011 __ b(&loop);
1012 __ bind(&fast);
1013 }
1014
1015 __ ldr(r0, CodeGenerator::GlobalObject());
1016 __ mov(r2, Operand(slot->var()->name()));
1017 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1018 ? RelocInfo::CODE_TARGET
1019 : RelocInfo::CODE_TARGET_CONTEXT;
1020 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001021 EmitCallIC(ic, mode);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001022}
1023
1024
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001025void FullCodeGenerator::EmitVariableLoad(Variable* var) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001026 // Four cases: non-this global variables, lookup slots, all other
1027 // types of slots, and parameters that rewrite to explicit property
1028 // accesses on the arguments object.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001029 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001030 Property* property = var->AsProperty();
1031
1032 if (var->is_global() && !var->is_this()) {
1033 Comment cmnt(masm_, "Global variable");
1034 // Use inline caching. Variable name is passed in r2 and the global
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001035 // object (receiver) in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001036 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001037 __ mov(r2, Operand(var->name()));
1038 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001039 EmitCallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001040 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001041
1042 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001043 Label done, slow;
1044
1045 // Generate code for loading from variables potentially shadowed
1046 // by eval-introduced variables.
1047 EmitDynamicLoadFromSlotFastCase(slot, NOT_INSIDE_TYPEOF, &slow, &done);
1048
1049 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001050 Comment cmnt(masm_, "Lookup slot");
1051 __ mov(r1, Operand(var->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001052 __ Push(cp, r1); // Context and name.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001053 __ CallRuntime(Runtime::kLoadContextSlot, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001054 __ bind(&done);
1055
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001056 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001057
1058 } else if (slot != NULL) {
1059 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1060 ? "Context slot"
1061 : "Stack slot");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001062 if (var->mode() == Variable::CONST) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001063 // Constants may be the hole value if they have not been initialized.
1064 // Unhole them.
1065 MemOperand slot_operand = EmitSlotSearch(slot, r0);
1066 __ ldr(r0, slot_operand);
1067 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1068 __ cmp(r0, ip);
1069 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
1070 context()->Plug(r0);
1071 } else {
1072 context()->Plug(slot);
1073 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001074 } else {
1075 Comment cmnt(masm_, "Rewritten parameter");
1076 ASSERT_NOT_NULL(property);
1077 // Rewritten parameter accesses are of the form "slot[literal]".
1078
1079 // Assert that the object is in a slot.
1080 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
1081 ASSERT_NOT_NULL(object_var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001082 Slot* object_slot = object_var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001083 ASSERT_NOT_NULL(object_slot);
1084
1085 // Load the object.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001086 Move(r1, object_slot);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001087
1088 // Assert that the key is a smi.
1089 Literal* key_literal = property->key()->AsLiteral();
1090 ASSERT_NOT_NULL(key_literal);
1091 ASSERT(key_literal->handle()->IsSmi());
1092
1093 // Load the key.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001094 __ mov(r0, Operand(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001095
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001096 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001097 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001098 EmitCallIC(ic, RelocInfo::CODE_TARGET);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001099 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001100 }
1101}
1102
1103
1104void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1105 Comment cmnt(masm_, "[ RegExpLiteral");
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001106 Label materialized;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001107 // Registers will be used as follows:
1108 // r4 = JS function, literals array
1109 // r3 = literal index
1110 // r2 = RegExp pattern
1111 // r1 = RegExp flags
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001112 // r0 = temp + materialized value (RegExp literal)
ricow@chromium.org65fae842010-08-25 15:26:24 +00001113 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1114 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001115 int literal_offset =
ricow@chromium.org65fae842010-08-25 15:26:24 +00001116 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001117 __ ldr(r0, FieldMemOperand(r4, literal_offset));
1118 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1119 __ cmp(r0, ip);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001120 __ b(ne, &materialized);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001121
1122 // Create regexp literal using runtime function.
1123 // Result will be in r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001124 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
1125 __ mov(r2, Operand(expr->pattern()));
1126 __ mov(r1, Operand(expr->flags()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001127 __ Push(r4, r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001128 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001129
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001130 __ bind(&materialized);
1131 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1132 __ push(r0);
1133 __ mov(r0, Operand(Smi::FromInt(size)));
1134 __ push(r0);
1135 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001136
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001137 // After this, registers are used as follows:
1138 // r0: Newly allocated regexp.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001139 // r1: Materialized regexp.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001140 // r2: temp.
1141 __ pop(r1);
1142 __ CopyFields(r0, r1, r2.bit(), size / kPointerSize);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001143 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001144}
1145
1146
1147void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1148 Comment cmnt(masm_, "[ ObjectLiteral");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001149 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1150 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1151 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1152 __ mov(r1, Operand(expr->constant_properties()));
1153 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001154 __ Push(r3, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001155 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001156 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001157 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001158 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001159 }
1160
1161 // If result_saved is true the result is on top of the stack. If
1162 // result_saved is false the result is in r0.
1163 bool result_saved = false;
1164
1165 for (int i = 0; i < expr->properties()->length(); i++) {
1166 ObjectLiteral::Property* property = expr->properties()->at(i);
1167 if (property->IsCompileTimeValue()) continue;
1168
1169 Literal* key = property->key();
1170 Expression* value = property->value();
1171 if (!result_saved) {
1172 __ push(r0); // Save result on stack
1173 result_saved = true;
1174 }
1175 switch (property->kind()) {
1176 case ObjectLiteral::Property::CONSTANT:
1177 UNREACHABLE();
1178 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1179 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1180 // Fall through.
1181 case ObjectLiteral::Property::COMPUTED:
1182 if (key->handle()->IsSymbol()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001183 VisitForAccumulatorValue(value);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001184 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001185 __ ldr(r1, MemOperand(sp));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001186 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001187 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001188 break;
1189 }
1190 // Fall through.
1191 case ObjectLiteral::Property::PROTOTYPE:
1192 // Duplicate receiver on stack.
1193 __ ldr(r0, MemOperand(sp));
1194 __ push(r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001195 VisitForStackValue(key);
1196 VisitForStackValue(value);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001197 __ CallRuntime(Runtime::kSetProperty, 3);
1198 break;
1199 case ObjectLiteral::Property::GETTER:
1200 case ObjectLiteral::Property::SETTER:
1201 // Duplicate receiver on stack.
1202 __ ldr(r0, MemOperand(sp));
1203 __ push(r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001204 VisitForStackValue(key);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001205 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1206 Smi::FromInt(1) :
1207 Smi::FromInt(0)));
1208 __ push(r1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001209 VisitForStackValue(value);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001210 __ CallRuntime(Runtime::kDefineAccessor, 4);
1211 break;
1212 }
1213 }
1214
1215 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001216 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001217 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001218 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001219 }
1220}
1221
1222
1223void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1224 Comment cmnt(masm_, "[ ArrayLiteral");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001225
1226 ZoneList<Expression*>* subexprs = expr->values();
1227 int length = subexprs->length();
1228
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001229 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1230 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1231 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1232 __ mov(r1, Operand(expr->constant_elements()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001233 __ Push(r3, r2, r1);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001234 if (expr->constant_elements()->map() == Heap::fixed_cow_array_map()) {
1235 FastCloneShallowArrayStub stub(
1236 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1237 __ CallStub(&stub);
1238 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
1239 } else if (expr->depth() > 1) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001240 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001241 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001242 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001243 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001244 FastCloneShallowArrayStub stub(
1245 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001246 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001247 }
1248
1249 bool result_saved = false; // Is the result saved to the stack?
1250
1251 // Emit code to evaluate all the non-constant subexpressions and to store
1252 // them into the newly cloned array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001253 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001254 Expression* subexpr = subexprs->at(i);
1255 // If the subexpression is a literal or a simple materialized literal it
1256 // is already set in the cloned array.
1257 if (subexpr->AsLiteral() != NULL ||
1258 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1259 continue;
1260 }
1261
1262 if (!result_saved) {
1263 __ push(r0);
1264 result_saved = true;
1265 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001266 VisitForAccumulatorValue(subexpr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001267
1268 // Store the subexpression value in the array's elements.
1269 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
1270 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1271 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1272 __ str(result_register(), FieldMemOperand(r1, offset));
1273
1274 // Update the write barrier for the array store with r0 as the scratch
1275 // register.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001276 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001277 }
1278
1279 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001280 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001281 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001282 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001283 }
1284}
1285
1286
ager@chromium.org5c838252010-02-19 08:53:10 +00001287void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1288 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001289 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1290 // on the left-hand side.
1291 if (!expr->target()->IsValidLeftHandSide()) {
1292 VisitForEffect(expr->target());
1293 return;
1294 }
1295
ager@chromium.org5c838252010-02-19 08:53:10 +00001296 // Left-hand side can only be a property, a global or a (parameter or local)
1297 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1298 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1299 LhsKind assign_type = VARIABLE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001300 Property* property = expr->target()->AsProperty();
1301 if (property != NULL) {
1302 assign_type = (property->key()->IsPropertyName())
1303 ? NAMED_PROPERTY
1304 : KEYED_PROPERTY;
ager@chromium.org5c838252010-02-19 08:53:10 +00001305 }
1306
1307 // Evaluate LHS expression.
1308 switch (assign_type) {
1309 case VARIABLE:
1310 // Nothing to do here.
1311 break;
1312 case NAMED_PROPERTY:
1313 if (expr->is_compound()) {
1314 // We need the receiver both on the stack and in the accumulator.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001315 VisitForAccumulatorValue(property->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00001316 __ push(result_register());
1317 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001318 VisitForStackValue(property->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00001319 }
1320 break;
1321 case KEYED_PROPERTY:
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001322 if (expr->is_compound()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001323 VisitForStackValue(property->obj());
1324 VisitForAccumulatorValue(property->key());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001325 __ ldr(r1, MemOperand(sp, 0));
1326 __ push(r0);
1327 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001328 VisitForStackValue(property->obj());
1329 VisitForStackValue(property->key());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001330 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001331 break;
1332 }
1333
ager@chromium.org5c838252010-02-19 08:53:10 +00001334 if (expr->is_compound()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001335 { AccumulatorValueContext context(this);
1336 switch (assign_type) {
1337 case VARIABLE:
1338 EmitVariableLoad(expr->target()->AsVariableProxy()->var());
1339 break;
1340 case NAMED_PROPERTY:
1341 EmitNamedPropertyLoad(property);
1342 break;
1343 case KEYED_PROPERTY:
1344 EmitKeyedPropertyLoad(property);
1345 break;
1346 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001347 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001348
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001349 Token::Value op = expr->binary_op();
1350 ConstantOperand constant = ShouldInlineSmiCase(op)
1351 ? GetConstantOperand(op, expr->target(), expr->value())
1352 : kNoConstants;
1353 ASSERT(constant == kRightConstant || constant == kNoConstants);
1354 if (constant == kNoConstants) {
1355 __ push(r0); // Left operand goes on the stack.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001356 VisitForAccumulatorValue(expr->value());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001357 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001358
ricow@chromium.org65fae842010-08-25 15:26:24 +00001359 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1360 ? OVERWRITE_RIGHT
1361 : NO_OVERWRITE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001362 SetSourcePosition(expr->position() + 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001363 AccumulatorValueContext context(this);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001364 if (ShouldInlineSmiCase(op)) {
1365 EmitInlineSmiBinaryOp(expr,
1366 op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001367 mode,
1368 expr->target(),
1369 expr->value(),
1370 constant);
1371 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001372 EmitBinaryOp(op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001373 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001374 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001375 VisitForAccumulatorValue(expr->value());
ager@chromium.org5c838252010-02-19 08:53:10 +00001376 }
1377
1378 // Record source position before possible IC call.
1379 SetSourcePosition(expr->position());
1380
1381 // Store the value.
1382 switch (assign_type) {
1383 case VARIABLE:
1384 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001385 expr->op());
ager@chromium.org5c838252010-02-19 08:53:10 +00001386 break;
1387 case NAMED_PROPERTY:
1388 EmitNamedPropertyAssignment(expr);
1389 break;
1390 case KEYED_PROPERTY:
1391 EmitKeyedPropertyAssignment(expr);
1392 break;
1393 }
1394}
1395
1396
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001397void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1398 SetSourcePosition(prop->position());
1399 Literal* key = prop->key()->AsLiteral();
1400 __ mov(r2, Operand(key->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001401 // Call load IC. It has arguments receiver and property name r0 and r2.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001402 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001403 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001404}
1405
1406
1407void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1408 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001409 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001410 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001411 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001412}
1413
1414
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001415void FullCodeGenerator::EmitInlineSmiBinaryOp(Expression* expr,
1416 Token::Value op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001417 OverwriteMode mode,
1418 Expression* left,
1419 Expression* right,
1420 ConstantOperand constant) {
1421 ASSERT(constant == kNoConstants); // Only handled case.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001422 EmitBinaryOp(op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001423}
1424
1425
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001426void FullCodeGenerator::EmitBinaryOp(Token::Value op,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001427 OverwriteMode mode) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001428 __ pop(r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001429 GenericBinaryOpStub stub(op, mode, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001430 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001431 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001432}
1433
1434
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001435void FullCodeGenerator::EmitAssignment(Expression* expr) {
1436 // Invalid left-hand sides are rewritten to have a 'throw
1437 // ReferenceError' on the left-hand side.
1438 if (!expr->IsValidLeftHandSide()) {
1439 VisitForEffect(expr);
1440 return;
1441 }
1442
1443 // Left-hand side can only be a property, a global or a (parameter or local)
1444 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1445 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1446 LhsKind assign_type = VARIABLE;
1447 Property* prop = expr->AsProperty();
1448 if (prop != NULL) {
1449 assign_type = (prop->key()->IsPropertyName())
1450 ? NAMED_PROPERTY
1451 : KEYED_PROPERTY;
1452 }
1453
1454 switch (assign_type) {
1455 case VARIABLE: {
1456 Variable* var = expr->AsVariableProxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001457 EffectContext context(this);
1458 EmitVariableAssignment(var, Token::ASSIGN);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001459 break;
1460 }
1461 case NAMED_PROPERTY: {
1462 __ push(r0); // Preserve value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001463 VisitForAccumulatorValue(prop->obj());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001464 __ mov(r1, r0);
1465 __ pop(r0); // Restore value.
1466 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
1467 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001468 EmitCallIC(ic, RelocInfo::CODE_TARGET);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001469 break;
1470 }
1471 case KEYED_PROPERTY: {
1472 __ push(r0); // Preserve value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001473 VisitForStackValue(prop->obj());
1474 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001475 __ mov(r1, r0);
1476 __ pop(r2);
1477 __ pop(r0); // Restore value.
1478 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001479 EmitCallIC(ic, RelocInfo::CODE_TARGET);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001480 break;
1481 }
1482 }
1483}
1484
1485
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001486void FullCodeGenerator::EmitVariableAssignment(Variable* var,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001487 Token::Value op) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001488 // Left-hand sides that rewrite to explicit property accesses do not reach
1489 // here.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001490 ASSERT(var != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001491 ASSERT(var->is_global() || var->AsSlot() != NULL);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001492
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001493 if (var->is_global()) {
1494 ASSERT(!var->is_this());
1495 // Assignment to a global variable. Use inline caching for the
1496 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001497 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001498 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001499 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001500 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001501 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001502
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001503 } else if (var->mode() != Variable::CONST || op == Token::INIT_CONST) {
1504 // Perform the assignment for non-const variables and for initialization
1505 // of const variables. Const assignments are simply skipped.
1506 Label done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001507 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001508 switch (slot->type()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001509 case Slot::PARAMETER:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001510 case Slot::LOCAL:
1511 if (op == Token::INIT_CONST) {
1512 // Detect const reinitialization by checking for the hole value.
1513 __ ldr(r1, MemOperand(fp, SlotOffset(slot)));
1514 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1515 __ cmp(r1, ip);
1516 __ b(ne, &done);
1517 }
1518 // Perform the assignment.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001519 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1520 break;
1521
1522 case Slot::CONTEXT: {
1523 MemOperand target = EmitSlotSearch(slot, r1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001524 if (op == Token::INIT_CONST) {
1525 // Detect const reinitialization by checking for the hole value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001526 __ ldr(r2, target);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001527 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001528 __ cmp(r2, ip);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001529 __ b(ne, &done);
1530 }
1531 // Perform the assignment and issue the write barrier.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001532 __ str(result_register(), target);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001533 // RecordWrite may destroy all its register arguments.
1534 __ mov(r3, result_register());
1535 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001536 __ RecordWrite(r1, Operand(offset), r2, r3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001537 break;
1538 }
1539
1540 case Slot::LOOKUP:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001541 // Call the runtime for the assignment. The runtime will ignore
1542 // const reinitialization.
1543 __ push(r0); // Value.
1544 __ mov(r0, Operand(slot->var()->name()));
1545 __ Push(cp, r0); // Context and name.
1546 if (op == Token::INIT_CONST) {
1547 // The runtime will ignore const redeclaration.
1548 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1549 } else {
1550 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1551 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001552 break;
1553 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001554 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001555 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001556
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001557 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001558}
1559
1560
1561void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1562 // Assignment to a property, using a named store IC.
1563 Property* prop = expr->target()->AsProperty();
1564 ASSERT(prop != NULL);
1565 ASSERT(prop->key()->AsLiteral() != NULL);
1566
1567 // If the assignment starts a block of assignments to the same object,
1568 // change to slow case to avoid the quadratic behavior of repeatedly
1569 // adding fast properties.
1570 if (expr->starts_initialization_block()) {
1571 __ push(result_register());
1572 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1573 __ push(ip);
1574 __ CallRuntime(Runtime::kToSlowProperties, 1);
1575 __ pop(result_register());
1576 }
1577
1578 // Record source code position before IC call.
1579 SetSourcePosition(expr->position());
1580 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001581 // Load receiver to r1. Leave a copy in the stack if needed for turning the
1582 // receiver into fast case.
ager@chromium.org5c838252010-02-19 08:53:10 +00001583 if (expr->ends_initialization_block()) {
1584 __ ldr(r1, MemOperand(sp));
1585 } else {
1586 __ pop(r1);
1587 }
1588
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001589 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001590 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001591
1592 // If the assignment ends an initialization block, revert to fast case.
1593 if (expr->ends_initialization_block()) {
1594 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001595 // Receiver is under the result value.
1596 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001597 __ push(ip);
1598 __ CallRuntime(Runtime::kToFastProperties, 1);
1599 __ pop(r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001600 context()->DropAndPlug(1, r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001601 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001602 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001603 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001604}
1605
1606
1607void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1608 // Assignment to a property, using a keyed store IC.
1609
1610 // If the assignment starts a block of assignments to the same object,
1611 // change to slow case to avoid the quadratic behavior of repeatedly
1612 // adding fast properties.
1613 if (expr->starts_initialization_block()) {
1614 __ push(result_register());
1615 // Receiver is now under the key and value.
1616 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1617 __ push(ip);
1618 __ CallRuntime(Runtime::kToSlowProperties, 1);
1619 __ pop(result_register());
1620 }
1621
1622 // Record source code position before IC call.
1623 SetSourcePosition(expr->position());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001624 __ pop(r1); // Key.
1625 // Load receiver to r2. Leave a copy in the stack if needed for turning the
1626 // receiver into fast case.
1627 if (expr->ends_initialization_block()) {
1628 __ ldr(r2, MemOperand(sp));
1629 } else {
1630 __ pop(r2);
1631 }
1632
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001633 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001634 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001635
1636 // If the assignment ends an initialization block, revert to fast case.
1637 if (expr->ends_initialization_block()) {
1638 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001639 // Receiver is under the result value.
1640 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001641 __ push(ip);
1642 __ CallRuntime(Runtime::kToFastProperties, 1);
1643 __ pop(r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001644 context()->DropAndPlug(1, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001645 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001646 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001647 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001648}
1649
1650
1651void FullCodeGenerator::VisitProperty(Property* expr) {
1652 Comment cmnt(masm_, "[ Property");
1653 Expression* key = expr->key();
1654
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001655 if (key->IsPropertyName()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001656 VisitForAccumulatorValue(expr->obj());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001657 EmitNamedPropertyLoad(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001658 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001659 VisitForStackValue(expr->obj());
1660 VisitForAccumulatorValue(expr->key());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001661 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001662 EmitKeyedPropertyLoad(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001663 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001664 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001665}
1666
1667void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001668 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001669 RelocInfo::Mode mode) {
1670 // Code common for calls using the IC.
1671 ZoneList<Expression*>* args = expr->arguments();
1672 int arg_count = args->length();
1673 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001674 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001675 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001676 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001677 // Record source position for debugger.
1678 SetSourcePosition(expr->position());
1679 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001680 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1681 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001682 EmitCallIC(ic, mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001683 // Restore context register.
1684 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001685 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001686}
1687
1688
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001689void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
1690 Expression* key,
1691 RelocInfo::Mode mode) {
1692 // Code common for calls using the IC.
1693 ZoneList<Expression*>* args = expr->arguments();
1694 int arg_count = args->length();
1695 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001696 VisitForStackValue(args->at(i));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001697 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001698 VisitForAccumulatorValue(key);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001699 __ mov(r2, r0);
1700 // Record source position for debugger.
1701 SetSourcePosition(expr->position());
1702 // Call the IC initialization code.
1703 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1704 Handle<Code> ic = CodeGenerator::ComputeKeyedCallInitialize(arg_count,
1705 in_loop);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001706 EmitCallIC(ic, mode);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001707 // Restore context register.
1708 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001709 context()->Plug(r0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001710}
1711
1712
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001713void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1714 // Code common for calls using the call stub.
1715 ZoneList<Expression*>* args = expr->arguments();
1716 int arg_count = args->length();
1717 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001718 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001719 }
1720 // Record source position for debugger.
1721 SetSourcePosition(expr->position());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001722 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1723 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001724 __ CallStub(&stub);
1725 // Restore context register.
1726 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001727 context()->DropAndPlug(1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001728}
1729
1730
1731void FullCodeGenerator::VisitCall(Call* expr) {
1732 Comment cmnt(masm_, "[ Call");
1733 Expression* fun = expr->expression();
1734 Variable* var = fun->AsVariableProxy()->AsVariable();
1735
1736 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001737 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1738 // resolve the function we need to call and the receiver of the
1739 // call. Then we call the resolved function using the given
1740 // arguments.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001741 VisitForStackValue(fun);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001742 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1743 __ push(r2); // Reserved receiver slot.
1744
1745 // Push the arguments.
1746 ZoneList<Expression*>* args = expr->arguments();
1747 int arg_count = args->length();
1748 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001749 VisitForStackValue(args->at(i));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001750 }
1751
1752 // Push copy of the function - found below the arguments.
1753 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1754 __ push(r1);
1755
1756 // Push copy of the first argument or undefined if it doesn't exist.
1757 if (arg_count > 0) {
1758 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1759 __ push(r1);
1760 } else {
1761 __ push(r2);
1762 }
1763
1764 // Push the receiver of the enclosing function and do runtime call.
1765 __ ldr(r1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1766 __ push(r1);
1767 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1768
1769 // The runtime call returns a pair of values in r0 (function) and
1770 // r1 (receiver). Touch up the stack with the right values.
1771 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1772 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
1773
1774 // Record source position for debugger.
1775 SetSourcePosition(expr->position());
1776 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1777 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1778 __ CallStub(&stub);
1779 // Restore context register.
1780 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001781 context()->DropAndPlug(1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001782 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001783 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001784 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001785 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001786 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001787 } else if (var != NULL && var->AsSlot() != NULL &&
1788 var->AsSlot()->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001789 // Call to a lookup slot (dynamically introduced variable).
1790 Label slow, done;
1791
1792 // Generate code for loading from variables potentially shadowed
1793 // by eval-introduced variables.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001794 EmitDynamicLoadFromSlotFastCase(var->AsSlot(),
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001795 NOT_INSIDE_TYPEOF,
1796 &slow,
1797 &done);
1798
1799 __ bind(&slow);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001800 // Call the runtime to find the function to call (returned in r0)
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001801 // and the object holding it (returned in edx).
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001802 __ push(context_register());
1803 __ mov(r2, Operand(var->name()));
1804 __ push(r2);
1805 __ CallRuntime(Runtime::kLoadContextSlot, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001806 __ Push(r0, r1); // Function, receiver.
1807
1808 // If fast case code has been generated, emit code to push the
1809 // function and receiver and have the slow path jump around this
1810 // code.
1811 if (done.is_linked()) {
1812 Label call;
1813 __ b(&call);
1814 __ bind(&done);
1815 // Push function.
1816 __ push(r0);
1817 // Push global receiver.
1818 __ ldr(r1, CodeGenerator::GlobalObject());
1819 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1820 __ push(r1);
1821 __ bind(&call);
1822 }
1823
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001824 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001825 } else if (fun->AsProperty() != NULL) {
1826 // Call to an object property.
1827 Property* prop = fun->AsProperty();
1828 Literal* key = prop->key()->AsLiteral();
1829 if (key != NULL && key->handle()->IsSymbol()) {
1830 // Call to a named property, use call IC.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001831 VisitForStackValue(prop->obj());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001832 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1833 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001834 // Call to a keyed property.
1835 // For a synthetic property use keyed load IC followed by function call,
1836 // for a regular property use keyed CallIC.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001837 VisitForStackValue(prop->obj());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001838 if (prop->is_synthetic()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001839 VisitForAccumulatorValue(prop->key());
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001840 // Record source code position for IC call.
1841 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001842 __ pop(r1); // We do not need to keep the receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001843
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001844 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001845 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001846 __ ldr(r1, CodeGenerator::GlobalObject());
1847 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001848 __ Push(r0, r1); // Function, receiver.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001849 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001850 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001851 EmitKeyedCallWithIC(expr, prop->key(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001852 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001853 }
1854 } else {
1855 // Call to some other expression. If the expression is an anonymous
1856 // function literal not called in a loop, mark it as one that should
1857 // also use the fast code generator.
1858 FunctionLiteral* lit = fun->AsFunctionLiteral();
1859 if (lit != NULL &&
1860 lit->name()->Equals(Heap::empty_string()) &&
1861 loop_depth() == 0) {
1862 lit->set_try_full_codegen(true);
1863 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001864 VisitForStackValue(fun);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001865 // Load global receiver object.
1866 __ ldr(r1, CodeGenerator::GlobalObject());
1867 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1868 __ push(r1);
1869 // Emit function call.
1870 EmitCallWithStub(expr);
1871 }
1872}
1873
1874
1875void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1876 Comment cmnt(masm_, "[ CallNew");
1877 // According to ECMA-262, section 11.2.2, page 44, the function
1878 // expression in new calls must be evaluated before the
1879 // arguments.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001880
1881 // Push constructor on the stack. If it's not a function it's used as
1882 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
1883 // ignored.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001884 VisitForStackValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001885
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001886 // Push the arguments ("left-to-right") on the stack.
1887 ZoneList<Expression*>* args = expr->arguments();
1888 int arg_count = args->length();
1889 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001890 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001891 }
1892
1893 // Call the construct call builtin that handles allocation and
1894 // constructor invocation.
1895 SetSourcePosition(expr->position());
1896
ricow@chromium.org65fae842010-08-25 15:26:24 +00001897 // Load function and argument count into r1 and r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001898 __ mov(r0, Operand(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001899 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001900
1901 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1902 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001903 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001904}
1905
1906
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001907void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1908 ASSERT(args->length() == 1);
1909
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001910 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001911
1912 Label materialize_true, materialize_false;
1913 Label* if_true = NULL;
1914 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001915 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001916 context()->PrepareTest(&materialize_true, &materialize_false,
1917 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001918
1919 __ BranchOnSmi(r0, if_true);
1920 __ b(if_false);
1921
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001922 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001923}
1924
1925
1926void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1927 ASSERT(args->length() == 1);
1928
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001929 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001930
1931 Label materialize_true, materialize_false;
1932 Label* if_true = NULL;
1933 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001934 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001935 context()->PrepareTest(&materialize_true, &materialize_false,
1936 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001937
1938 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001939 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001940
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001941 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001942}
1943
1944
1945void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1946 ASSERT(args->length() == 1);
1947
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001948 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001949
1950 Label materialize_true, materialize_false;
1951 Label* if_true = NULL;
1952 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001953 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001954 context()->PrepareTest(&materialize_true, &materialize_false,
1955 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001956
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001957 __ BranchOnSmi(r0, if_false);
1958 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1959 __ cmp(r0, ip);
1960 __ b(eq, if_true);
1961 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1962 // Undetectable objects behave like undefined when tested with typeof.
1963 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
1964 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1965 __ b(ne, if_false);
1966 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1967 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
1968 __ b(lt, if_false);
1969 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001970 Split(le, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001971
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001972 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001973}
1974
1975
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001976void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
1977 ASSERT(args->length() == 1);
1978
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001979 VisitForAccumulatorValue(args->at(0));
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001980
1981 Label materialize_true, materialize_false;
1982 Label* if_true = NULL;
1983 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001984 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001985 context()->PrepareTest(&materialize_true, &materialize_false,
1986 &if_true, &if_false, &fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001987
1988 __ BranchOnSmi(r0, if_false);
1989 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001990 Split(ge, if_true, if_false, fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001991
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001992 context()->Plug(if_true, if_false);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001993}
1994
1995
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001996void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
1997 ASSERT(args->length() == 1);
1998
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001999 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002000
2001 Label materialize_true, materialize_false;
2002 Label* if_true = NULL;
2003 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002004 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002005 context()->PrepareTest(&materialize_true, &materialize_false,
2006 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002007
2008 __ BranchOnSmi(r0, if_false);
2009 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2010 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
2011 __ tst(r1, Operand(1 << Map::kIsUndetectable));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002012 Split(ne, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002013
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002014 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002015}
2016
2017
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002018void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2019 ZoneList<Expression*>* args) {
2020
2021 ASSERT(args->length() == 1);
2022
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002023 VisitForAccumulatorValue(args->at(0));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002024
2025 Label materialize_true, materialize_false;
2026 Label* if_true = NULL;
2027 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002028 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002029 context()->PrepareTest(&materialize_true, &materialize_false,
2030 &if_true, &if_false, &fall_through);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002031
2032 // Just indicate false, as %_IsStringWrapperSafeForDefaultValueOf() is only
2033 // used in a few functions in runtime.js which should not normally be hit by
2034 // this compiler.
2035 __ jmp(if_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002036 context()->Plug(if_true, if_false);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002037}
2038
2039
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002040void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
2041 ASSERT(args->length() == 1);
2042
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002043 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002044
2045 Label materialize_true, materialize_false;
2046 Label* if_true = NULL;
2047 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002048 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002049 context()->PrepareTest(&materialize_true, &materialize_false,
2050 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002051
2052 __ BranchOnSmi(r0, if_false);
2053 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002054 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002055
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002056 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002057}
2058
2059
2060void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
2061 ASSERT(args->length() == 1);
2062
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002063 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002064
2065 Label materialize_true, materialize_false;
2066 Label* if_true = NULL;
2067 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002068 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002069 context()->PrepareTest(&materialize_true, &materialize_false,
2070 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002071
2072 __ BranchOnSmi(r0, if_false);
2073 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002074 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002075
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002076 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002077}
2078
2079
2080void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
2081 ASSERT(args->length() == 1);
2082
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002083 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002084
2085 Label materialize_true, materialize_false;
2086 Label* if_true = NULL;
2087 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002088 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002089 context()->PrepareTest(&materialize_true, &materialize_false,
2090 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002091
2092 __ BranchOnSmi(r0, if_false);
2093 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002094 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002095
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002096 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002097}
2098
2099
2100
2101void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
2102 ASSERT(args->length() == 0);
2103
2104 Label materialize_true, materialize_false;
2105 Label* if_true = NULL;
2106 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002107 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002108 context()->PrepareTest(&materialize_true, &materialize_false,
2109 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002110
2111 // Get the frame pointer for the calling frame.
2112 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2113
2114 // Skip the arguments adaptor frame if it exists.
2115 Label check_frame_marker;
2116 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
2117 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2118 __ b(ne, &check_frame_marker);
2119 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
2120
2121 // Check the marker in the calling frame.
2122 __ bind(&check_frame_marker);
2123 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
2124 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002125 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002126
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002127 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002128}
2129
2130
2131void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
2132 ASSERT(args->length() == 2);
2133
2134 // Load the two objects into registers and perform the comparison.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002135 VisitForStackValue(args->at(0));
2136 VisitForAccumulatorValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002137
2138 Label materialize_true, materialize_false;
2139 Label* if_true = NULL;
2140 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002141 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002142 context()->PrepareTest(&materialize_true, &materialize_false,
2143 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002144
2145 __ pop(r1);
2146 __ cmp(r0, r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002147 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002148
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002149 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002150}
2151
2152
2153void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
2154 ASSERT(args->length() == 1);
2155
2156 // ArgumentsAccessStub expects the key in edx and the formal
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002157 // parameter count in r0.
2158 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002159 __ mov(r1, r0);
2160 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2161 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2162 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002163 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002164}
2165
2166
2167void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
2168 ASSERT(args->length() == 0);
2169
2170 Label exit;
2171 // Get the number of formal parameters.
2172 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2173
2174 // Check if the calling frame is an arguments adaptor frame.
2175 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2176 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
2177 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2178 __ b(ne, &exit);
2179
2180 // Arguments adaptor case: Read the arguments length from the
2181 // adaptor frame.
2182 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2183
2184 __ bind(&exit);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002185 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002186}
2187
2188
2189void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
2190 ASSERT(args->length() == 1);
2191 Label done, null, function, non_function_constructor;
2192
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002193 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002194
2195 // If the object is a smi, we return null.
2196 __ BranchOnSmi(r0, &null);
2197
2198 // Check that the object is a JS object but take special care of JS
2199 // functions to make sure they have 'Function' as their class.
2200 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2201 __ b(lt, &null);
2202
2203 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2204 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2205 // LAST_JS_OBJECT_TYPE.
2206 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2207 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2208 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2209 __ b(eq, &function);
2210
2211 // Check if the constructor in the map is a function.
2212 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2213 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2214 __ b(ne, &non_function_constructor);
2215
2216 // r0 now contains the constructor function. Grab the
2217 // instance class name from there.
2218 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2219 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2220 __ b(&done);
2221
2222 // Functions have class 'Function'.
2223 __ bind(&function);
2224 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2225 __ jmp(&done);
2226
2227 // Objects with a non-function constructor have class 'Object'.
2228 __ bind(&non_function_constructor);
2229 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2230 __ jmp(&done);
2231
2232 // Non-JS objects have class null.
2233 __ bind(&null);
2234 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2235
2236 // All done.
2237 __ bind(&done);
2238
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002239 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002240}
2241
2242
2243void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2244 // Conditionally generate a log call.
2245 // Args:
2246 // 0 (literal string): The type of logging (corresponds to the flags).
2247 // This is used to determine whether or not to generate the log call.
2248 // 1 (string): Format string. Access the string at argument index 2
2249 // with '%2s' (see Logger::LogRuntime for all the formats).
2250 // 2 (array): Arguments to the format string.
2251 ASSERT_EQ(args->length(), 3);
2252#ifdef ENABLE_LOGGING_AND_PROFILING
2253 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002254 VisitForStackValue(args->at(1));
2255 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002256 __ CallRuntime(Runtime::kLog, 2);
2257 }
2258#endif
2259 // Finally, we're expected to leave a value on the top of the stack.
2260 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002261 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002262}
2263
2264
2265void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2266 ASSERT(args->length() == 0);
2267
2268 Label slow_allocate_heapnumber;
2269 Label heapnumber_allocated;
2270
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002271 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
2272 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002273 __ jmp(&heapnumber_allocated);
2274
2275 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002276 // Allocate a heap number.
2277 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002278 __ mov(r4, Operand(r0));
2279
2280 __ bind(&heapnumber_allocated);
2281
2282 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
2283 // by computing:
2284 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2285 if (CpuFeatures::IsSupported(VFP3)) {
2286 __ PrepareCallCFunction(0, r1);
2287 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
2288
2289 CpuFeatures::Scope scope(VFP3);
2290 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2291 // Create this constant using mov/orr to avoid PC relative load.
2292 __ mov(r1, Operand(0x41000000));
2293 __ orr(r1, r1, Operand(0x300000));
2294 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
2295 __ vmov(d7, r0, r1);
2296 // Move 0x4130000000000000 to VFP.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00002297 __ mov(r0, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002298 __ vmov(d8, r0, r1);
2299 // Subtract and store the result in the heap number.
2300 __ vsub(d7, d7, d8);
2301 __ sub(r0, r4, Operand(kHeapObjectTag));
2302 __ vstr(d7, r0, HeapNumber::kValueOffset);
2303 __ mov(r0, r4);
2304 } else {
2305 __ mov(r0, Operand(r4));
2306 __ PrepareCallCFunction(1, r1);
2307 __ CallCFunction(
2308 ExternalReference::fill_heap_number_with_random_function(), 1);
2309 }
2310
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002311 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002312}
2313
2314
2315void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
2316 // Load the arguments on the stack and call the stub.
2317 SubStringStub stub;
2318 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002319 VisitForStackValue(args->at(0));
2320 VisitForStackValue(args->at(1));
2321 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002322 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002323 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002324}
2325
2326
2327void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
2328 // Load the arguments on the stack and call the stub.
2329 RegExpExecStub stub;
2330 ASSERT(args->length() == 4);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002331 VisitForStackValue(args->at(0));
2332 VisitForStackValue(args->at(1));
2333 VisitForStackValue(args->at(2));
2334 VisitForStackValue(args->at(3));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002335 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002336 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002337}
2338
2339
2340void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
2341 ASSERT(args->length() == 1);
2342
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002343 VisitForAccumulatorValue(args->at(0)); // Load the object.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002344
2345 Label done;
2346 // If the object is a smi return the object.
2347 __ BranchOnSmi(r0, &done);
2348 // If the object is not a value type, return the object.
2349 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
2350 __ b(ne, &done);
2351 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2352
2353 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002354 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002355}
2356
2357
2358void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
2359 // Load the arguments on the stack and call the runtime function.
2360 ASSERT(args->length() == 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002361 VisitForStackValue(args->at(0));
2362 VisitForStackValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002363 __ CallRuntime(Runtime::kMath_pow, 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002364 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002365}
2366
2367
2368void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
2369 ASSERT(args->length() == 2);
2370
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002371 VisitForStackValue(args->at(0)); // Load the object.
2372 VisitForAccumulatorValue(args->at(1)); // Load the value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002373 __ pop(r1); // r0 = value. r1 = object.
2374
2375 Label done;
2376 // If the object is a smi, return the value.
2377 __ BranchOnSmi(r1, &done);
2378
2379 // If the object is not a value type, return the value.
2380 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
2381 __ b(ne, &done);
2382
2383 // Store the value.
2384 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2385 // Update the write barrier. Save the value as it will be
2386 // overwritten by the write barrier code and is needed afterward.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002387 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002388
2389 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002390 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002391}
2392
2393
2394void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2395 ASSERT_EQ(args->length(), 1);
2396
2397 // Load the argument on the stack and call the stub.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002398 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002399
2400 NumberToStringStub stub;
2401 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002402 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002403}
2404
2405
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002406void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002407 ASSERT(args->length() == 1);
2408
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002409 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002410
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002411 Label done;
2412 StringCharFromCodeGenerator generator(r0, r1);
2413 generator.GenerateFast(masm_);
2414 __ jmp(&done);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002415
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002416 NopRuntimeCallHelper call_helper;
2417 generator.GenerateSlow(masm_, call_helper);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002418
2419 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002420 context()->Plug(r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002421}
2422
2423
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002424void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
2425 ASSERT(args->length() == 2);
2426
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002427 VisitForStackValue(args->at(0));
2428 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002429
2430 Register object = r1;
2431 Register index = r0;
2432 Register scratch = r2;
2433 Register result = r3;
2434
2435 __ pop(object);
2436
2437 Label need_conversion;
2438 Label index_out_of_range;
2439 Label done;
2440 StringCharCodeAtGenerator generator(object,
2441 index,
2442 scratch,
2443 result,
2444 &need_conversion,
2445 &need_conversion,
2446 &index_out_of_range,
2447 STRING_INDEX_IS_NUMBER);
2448 generator.GenerateFast(masm_);
2449 __ jmp(&done);
2450
2451 __ bind(&index_out_of_range);
2452 // When the index is out of range, the spec requires us to return
2453 // NaN.
2454 __ LoadRoot(result, Heap::kNanValueRootIndex);
2455 __ jmp(&done);
2456
2457 __ bind(&need_conversion);
2458 // Load the undefined value into the result register, which will
2459 // trigger conversion.
2460 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2461 __ jmp(&done);
2462
2463 NopRuntimeCallHelper call_helper;
2464 generator.GenerateSlow(masm_, call_helper);
2465
2466 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002467 context()->Plug(result);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002468}
2469
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002470
2471void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
2472 ASSERT(args->length() == 2);
2473
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002474 VisitForStackValue(args->at(0));
2475 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002476
2477 Register object = r1;
2478 Register index = r0;
2479 Register scratch1 = r2;
2480 Register scratch2 = r3;
2481 Register result = r0;
2482
2483 __ pop(object);
2484
2485 Label need_conversion;
2486 Label index_out_of_range;
2487 Label done;
2488 StringCharAtGenerator generator(object,
2489 index,
2490 scratch1,
2491 scratch2,
2492 result,
2493 &need_conversion,
2494 &need_conversion,
2495 &index_out_of_range,
2496 STRING_INDEX_IS_NUMBER);
2497 generator.GenerateFast(masm_);
2498 __ jmp(&done);
2499
2500 __ bind(&index_out_of_range);
2501 // When the index is out of range, the spec requires us to return
2502 // the empty string.
2503 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2504 __ jmp(&done);
2505
2506 __ bind(&need_conversion);
2507 // Move smi zero into the result register, which will trigger
2508 // conversion.
2509 __ mov(result, Operand(Smi::FromInt(0)));
2510 __ jmp(&done);
2511
2512 NopRuntimeCallHelper call_helper;
2513 generator.GenerateSlow(masm_, call_helper);
2514
2515 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002516 context()->Plug(result);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002517}
2518
2519
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002520void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2521 ASSERT_EQ(2, args->length());
2522
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002523 VisitForStackValue(args->at(0));
2524 VisitForStackValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002525
2526 StringAddStub stub(NO_STRING_ADD_FLAGS);
2527 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002528 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002529}
2530
2531
2532void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
2533 ASSERT_EQ(2, args->length());
2534
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002535 VisitForStackValue(args->at(0));
2536 VisitForStackValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002537
2538 StringCompareStub stub;
2539 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002540 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002541}
2542
2543
2544void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
2545 // Load the argument on the stack and call the runtime.
2546 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002547 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002548 __ CallRuntime(Runtime::kMath_sin, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002549 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002550}
2551
2552
2553void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
2554 // Load the argument on the stack and call the runtime.
2555 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002556 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002557 __ CallRuntime(Runtime::kMath_cos, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002558 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002559}
2560
2561
2562void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2563 // Load the argument on the stack and call the runtime function.
2564 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002565 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002566 __ CallRuntime(Runtime::kMath_sqrt, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002567 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002568}
2569
2570
2571void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2572 ASSERT(args->length() >= 2);
2573
2574 int arg_count = args->length() - 2; // For receiver and function.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002575 VisitForStackValue(args->at(0)); // Receiver.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002576 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002577 VisitForStackValue(args->at(i + 1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002578 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002579 VisitForAccumulatorValue(args->at(arg_count + 1)); // Function.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002580
2581 // InvokeFunction requires function in r1. Move it in there.
2582 if (!result_register().is(r1)) __ mov(r1, result_register());
2583 ParameterCount count(arg_count);
2584 __ InvokeFunction(r1, count, CALL_FUNCTION);
2585 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002586 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002587}
2588
2589
2590void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2591 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002592 VisitForStackValue(args->at(0));
2593 VisitForStackValue(args->at(1));
2594 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002595 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002596 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002597}
2598
2599
2600void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2601 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002602 VisitForStackValue(args->at(0));
2603 VisitForStackValue(args->at(1));
2604 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002605 __ CallRuntime(Runtime::kSwapElements, 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002606 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002607}
2608
2609
2610void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2611 ASSERT_EQ(2, args->length());
2612
2613 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2614 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2615
2616 Handle<FixedArray> jsfunction_result_caches(
2617 Top::global_context()->jsfunction_result_caches());
2618 if (jsfunction_result_caches->length() <= cache_id) {
2619 __ Abort("Attempt to use undefined cache.");
2620 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002621 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002622 return;
2623 }
2624
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002625 VisitForAccumulatorValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002626
2627 Register key = r0;
2628 Register cache = r1;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002629 __ ldr(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002630 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002631 __ ldr(cache, ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002632 __ ldr(cache,
2633 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2634
2635
2636 Label done, not_found;
2637 // tmp now holds finger offset as a smi.
2638 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2639 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2640 // r2 now holds finger offset as a smi.
2641 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2642 // r3 now points to the start of fixed array elements.
2643 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2644 // Note side effect of PreIndex: r3 now points to the key of the pair.
2645 __ cmp(key, r2);
2646 __ b(ne, &not_found);
2647
2648 __ ldr(r0, MemOperand(r3, kPointerSize));
2649 __ b(&done);
2650
2651 __ bind(&not_found);
2652 // Call runtime to perform the lookup.
2653 __ Push(cache, key);
2654 __ CallRuntime(Runtime::kGetFromCache, 2);
2655
2656 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002657 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002658}
2659
2660
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002661void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
2662 ASSERT_EQ(2, args->length());
2663
2664 Register right = r0;
2665 Register left = r1;
2666 Register tmp = r2;
2667 Register tmp2 = r3;
2668
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002669 VisitForStackValue(args->at(0));
2670 VisitForAccumulatorValue(args->at(1));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002671 __ pop(left);
2672
2673 Label done, fail, ok;
2674 __ cmp(left, Operand(right));
2675 __ b(eq, &ok);
2676 // Fail if either is a non-HeapObject.
2677 __ and_(tmp, left, Operand(right));
2678 __ tst(tmp, Operand(kSmiTagMask));
2679 __ b(eq, &fail);
2680 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
2681 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
2682 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
2683 __ b(ne, &fail);
2684 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
2685 __ cmp(tmp, Operand(tmp2));
2686 __ b(ne, &fail);
2687 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
2688 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
2689 __ cmp(tmp, tmp2);
2690 __ b(eq, &ok);
2691 __ bind(&fail);
2692 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
2693 __ jmp(&done);
2694 __ bind(&ok);
2695 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
2696 __ bind(&done);
2697
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002698 context()->Plug(r0);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002699}
2700
2701
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002702void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002703 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002704
2705 Label materialize_true, materialize_false;
2706 Label* if_true = NULL;
2707 Label* if_false = NULL;
2708 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002709 context()->PrepareTest(&materialize_true, &materialize_false,
2710 &if_true, &if_false, &fall_through);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002711
2712 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2713 __ tst(r0, Operand(String::kContainsCachedArrayIndexMask));
2714
2715 __ b(eq, if_true);
2716 __ b(if_false);
2717
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002718 context()->Plug(if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002719}
2720
2721
2722void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
2723 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002724 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002725 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2726 __ IndexFromHash(r0, r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002727 context()->Plug(r0);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002728}
2729
2730
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002731void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002732 Handle<String> name = expr->name();
2733 if (name->length() > 0 && name->Get(0) == '_') {
2734 Comment cmnt(masm_, "[ InlineRuntimeCall");
2735 EmitInlineRuntimeCall(expr);
2736 return;
2737 }
2738
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002739 Comment cmnt(masm_, "[ CallRuntime");
2740 ZoneList<Expression*>* args = expr->arguments();
2741
2742 if (expr->is_jsruntime()) {
2743 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002744 __ ldr(r0, CodeGenerator::GlobalObject());
2745 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002746 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002747 }
2748
2749 // Push the arguments ("left-to-right").
2750 int arg_count = args->length();
2751 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002752 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002753 }
2754
2755 if (expr->is_jsruntime()) {
2756 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002757 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002758 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2759 NOT_IN_LOOP);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002760 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002761 // Restore context register.
2762 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002763 } else {
2764 // Call the C runtime function.
2765 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002766 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002767 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002768}
2769
2770
2771void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2772 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002773 case Token::DELETE: {
2774 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2775 Property* prop = expr->expression()->AsProperty();
2776 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2777 if (prop == NULL && var == NULL) {
2778 // Result of deleting non-property, non-variable reference is true.
2779 // The subexpression may have side effects.
2780 VisitForEffect(expr->expression());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002781 context()->Plug(true);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002782 } else if (var != NULL &&
2783 !var->is_global() &&
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002784 var->AsSlot() != NULL &&
2785 var->AsSlot()->type() != Slot::LOOKUP) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002786 // Result of deleting non-global, non-dynamic variables is false.
2787 // The subexpression does not have side effects.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002788 context()->Plug(false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002789 } else {
2790 // Property or variable reference. Call the delete builtin with
2791 // object and property name as arguments.
2792 if (prop != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002793 VisitForStackValue(prop->obj());
2794 VisitForStackValue(prop->key());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002795 } else if (var->is_global()) {
2796 __ ldr(r1, CodeGenerator::GlobalObject());
2797 __ mov(r0, Operand(var->name()));
2798 __ Push(r1, r0);
2799 } else {
2800 // Non-global variable. Call the runtime to look up the context
2801 // where the variable was introduced.
2802 __ push(context_register());
2803 __ mov(r2, Operand(var->name()));
2804 __ push(r2);
2805 __ CallRuntime(Runtime::kLookupContext, 2);
2806 __ push(r0);
2807 __ mov(r2, Operand(var->name()));
2808 __ push(r2);
2809 }
2810 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002811 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002812 }
2813 break;
2814 }
2815
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002816 case Token::VOID: {
2817 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2818 VisitForEffect(expr->expression());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002819 context()->Plug(Heap::kUndefinedValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002820 break;
2821 }
2822
2823 case Token::NOT: {
2824 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002825 Label materialize_true, materialize_false;
2826 Label* if_true = NULL;
2827 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002828 Label* fall_through = NULL;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002829
2830 // Notice that the labels are swapped.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002831 context()->PrepareTest(&materialize_true, &materialize_false,
2832 &if_false, &if_true, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002833 VisitForControl(expr->expression(), if_true, if_false, fall_through);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002834 context()->Plug(if_false, if_true); // Labels swapped.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002835 break;
2836 }
2837
2838 case Token::TYPEOF: {
2839 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002840 { StackValueContext context(this);
2841 VisitForTypeofValue(expr->expression());
2842 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002843 __ CallRuntime(Runtime::kTypeof, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002844 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002845 break;
2846 }
2847
2848 case Token::ADD: {
2849 Comment cmt(masm_, "[ UnaryOperation (ADD)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002850 VisitForAccumulatorValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002851 Label no_conversion;
2852 __ tst(result_register(), Operand(kSmiTagMask));
2853 __ b(eq, &no_conversion);
2854 __ push(r0);
2855 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2856 __ bind(&no_conversion);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002857 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002858 break;
2859 }
2860
2861 case Token::SUB: {
2862 Comment cmt(masm_, "[ UnaryOperation (SUB)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002863 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002864 UnaryOverwriteMode overwrite =
2865 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002866 GenericUnaryOpStub stub(Token::SUB,
2867 overwrite,
2868 NO_UNARY_FLAGS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002869 // GenericUnaryOpStub expects the argument to be in the
2870 // accumulator register r0.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002871 VisitForAccumulatorValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002872 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002873 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002874 break;
2875 }
2876
2877 case Token::BIT_NOT: {
2878 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002879 // The generic unary operation stub expects the argument to be
2880 // in the accumulator register r0.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002881 VisitForAccumulatorValue(expr->expression());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002882 Label done;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002883 bool inline_smi_code = ShouldInlineSmiCase(expr->op());
2884 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002885 Label call_stub;
2886 __ BranchOnNotSmi(r0, &call_stub);
2887 __ mvn(r0, Operand(r0));
2888 // Bit-clear inverted smi-tag.
2889 __ bic(r0, r0, Operand(kSmiTagMask));
2890 __ b(&done);
2891 __ bind(&call_stub);
2892 }
2893 bool overwrite = expr->expression()->ResultOverwriteAllowed();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002894 UnaryOpFlags flags = inline_smi_code
2895 ? NO_UNARY_SMI_CODE_IN_STUB
2896 : NO_UNARY_FLAGS;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002897 UnaryOverwriteMode mode =
2898 overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002899 GenericUnaryOpStub stub(Token::BIT_NOT, mode, flags);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002900 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002901 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002902 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002903 break;
2904 }
2905
2906 default:
2907 UNREACHABLE();
2908 }
2909}
2910
2911
2912void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2913 Comment cmnt(masm_, "[ CountOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002914 SetSourcePosition(expr->position());
2915
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002916 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2917 // as the left-hand side.
2918 if (!expr->expression()->IsValidLeftHandSide()) {
2919 VisitForEffect(expr->expression());
2920 return;
2921 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002922
2923 // Expression can only be a property, a global or a (parameter or local)
2924 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2925 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2926 LhsKind assign_type = VARIABLE;
2927 Property* prop = expr->expression()->AsProperty();
2928 // In case of a property we use the uninitialized expression context
2929 // of the key to detect a named property.
2930 if (prop != NULL) {
2931 assign_type =
2932 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2933 }
2934
2935 // Evaluate expression and get value.
2936 if (assign_type == VARIABLE) {
2937 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002938 AccumulatorValueContext context(this);
2939 EmitVariableLoad(expr->expression()->AsVariableProxy()->var());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002940 } else {
2941 // Reserve space for result of postfix operation.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002942 if (expr->is_postfix() && !context()->IsEffect()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002943 __ mov(ip, Operand(Smi::FromInt(0)));
2944 __ push(ip);
2945 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002946 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002947 // Put the object both on the stack and in the accumulator.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002948 VisitForAccumulatorValue(prop->obj());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002949 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002950 EmitNamedPropertyLoad(prop);
2951 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002952 VisitForStackValue(prop->obj());
2953 VisitForAccumulatorValue(prop->key());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002954 __ ldr(r1, MemOperand(sp, 0));
2955 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002956 EmitKeyedPropertyLoad(prop);
2957 }
2958 }
2959
2960 // Call ToNumber only if operand is not a smi.
2961 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002962 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002963 __ push(r0);
2964 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2965 __ bind(&no_conversion);
2966
2967 // Save result for postfix expressions.
2968 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002969 if (!context()->IsEffect()) {
2970 // Save the result on the stack. If we have a named or keyed property
2971 // we store the result under the receiver that is currently on top
2972 // of the stack.
2973 switch (assign_type) {
2974 case VARIABLE:
2975 __ push(r0);
2976 break;
2977 case NAMED_PROPERTY:
2978 __ str(r0, MemOperand(sp, kPointerSize));
2979 break;
2980 case KEYED_PROPERTY:
2981 __ str(r0, MemOperand(sp, 2 * kPointerSize));
2982 break;
2983 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002984 }
2985 }
2986
2987
2988 // Inline smi case if we are in a loop.
2989 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002990 int count_value = expr->op() == Token::INC ? 1 : -1;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002991 if (ShouldInlineSmiCase(expr->op())) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002992 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002993 __ b(vs, &stub_call);
2994 // We could eliminate this smi check if we split the code at
2995 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002996 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002997 __ bind(&stub_call);
2998 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002999 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003000 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003001 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00003002 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003003 __ CallStub(&stub);
3004 __ bind(&done);
3005
3006 // Store the value returned in r0.
3007 switch (assign_type) {
3008 case VARIABLE:
3009 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003010 { EffectContext context(this);
3011 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3012 Token::ASSIGN);
3013 }
3014 // For all contexts except EffectConstant We have the result on
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003015 // top of the stack.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003016 if (!context()->IsEffect()) {
3017 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003018 }
3019 } else {
3020 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003021 Token::ASSIGN);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003022 }
3023 break;
3024 case NAMED_PROPERTY: {
3025 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003026 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003027 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003028 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003029 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003030 if (!context()->IsEffect()) {
3031 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003032 }
3033 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003034 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003035 }
3036 break;
3037 }
3038 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003039 __ pop(r1); // Key.
3040 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003041 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003042 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003043 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003044 if (!context()->IsEffect()) {
3045 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003046 }
3047 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003048 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003049 }
3050 break;
3051 }
3052 }
3053}
3054
3055
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003056void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
3057 ASSERT(!context()->IsEffect());
3058 ASSERT(!context()->IsTest());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003059 VariableProxy* proxy = expr->AsVariableProxy();
3060 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
3061 Comment cmnt(masm_, "Global variable");
3062 __ ldr(r0, CodeGenerator::GlobalObject());
3063 __ mov(r2, Operand(proxy->name()));
3064 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
3065 // Use a regular load, not a contextual load, to avoid a reference
3066 // error.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003067 EmitCallIC(ic, RelocInfo::CODE_TARGET);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003068 context()->Plug(r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003069 } else if (proxy != NULL &&
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003070 proxy->var()->AsSlot() != NULL &&
3071 proxy->var()->AsSlot()->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003072 Label done, slow;
3073
3074 // Generate code for loading from variables potentially shadowed
3075 // by eval-introduced variables.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003076 Slot* slot = proxy->var()->AsSlot();
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003077 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
3078
3079 __ bind(&slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003080 __ mov(r0, Operand(proxy->name()));
3081 __ Push(cp, r0);
3082 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003083 __ bind(&done);
3084
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003085 context()->Plug(r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003086 } else {
3087 // This expression cannot throw a reference error at the top level.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003088 Visit(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003089 }
3090}
3091
3092
ricow@chromium.org65fae842010-08-25 15:26:24 +00003093bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
3094 Expression* left,
3095 Expression* right,
3096 Label* if_true,
3097 Label* if_false,
3098 Label* fall_through) {
3099 if (op != Token::EQ && op != Token::EQ_STRICT) return false;
3100
3101 // Check for the pattern: typeof <expression> == <string literal>.
3102 Literal* right_literal = right->AsLiteral();
3103 if (right_literal == NULL) return false;
3104 Handle<Object> right_literal_value = right_literal->handle();
3105 if (!right_literal_value->IsString()) return false;
3106 UnaryOperation* left_unary = left->AsUnaryOperation();
3107 if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
3108 Handle<String> check = Handle<String>::cast(right_literal_value);
3109
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003110 { AccumulatorValueContext context(this);
3111 VisitForTypeofValue(left_unary->expression());
3112 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003113 if (check->Equals(Heap::number_symbol())) {
3114 __ tst(r0, Operand(kSmiTagMask));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003115 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003116 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3117 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
3118 __ cmp(r0, ip);
3119 Split(eq, if_true, if_false, fall_through);
3120 } else if (check->Equals(Heap::string_symbol())) {
3121 __ tst(r0, Operand(kSmiTagMask));
3122 __ b(eq, if_false);
3123 // Check for undetectable objects => false.
3124 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3125 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
3126 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3127 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3128 __ b(eq, if_false);
3129 __ ldrb(r1, FieldMemOperand(r0, Map::kInstanceTypeOffset));
3130 __ cmp(r1, Operand(FIRST_NONSTRING_TYPE));
3131 Split(lt, if_true, if_false, fall_through);
3132 } else if (check->Equals(Heap::boolean_symbol())) {
3133 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3134 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003135 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003136 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
3137 __ cmp(r0, ip);
3138 Split(eq, if_true, if_false, fall_through);
3139 } else if (check->Equals(Heap::undefined_symbol())) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003140 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003141 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003142 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003143 __ tst(r0, Operand(kSmiTagMask));
3144 __ b(eq, if_false);
3145 // Check for undetectable objects => true.
3146 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3147 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
3148 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3149 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3150 Split(eq, if_true, if_false, fall_through);
3151 } else if (check->Equals(Heap::function_symbol())) {
3152 __ tst(r0, Operand(kSmiTagMask));
3153 __ b(eq, if_false);
3154 __ CompareObjectType(r0, r1, r0, JS_FUNCTION_TYPE);
3155 __ b(eq, if_true);
3156 // Regular expressions => 'function' (they are callable).
3157 __ CompareInstanceType(r1, r0, JS_REGEXP_TYPE);
3158 Split(eq, if_true, if_false, fall_through);
3159 } else if (check->Equals(Heap::object_symbol())) {
3160 __ tst(r0, Operand(kSmiTagMask));
3161 __ b(eq, if_false);
3162 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3163 __ cmp(r0, ip);
3164 __ b(eq, if_true);
3165 // Regular expressions => 'function', not 'object'.
3166 __ CompareObjectType(r0, r1, r0, JS_REGEXP_TYPE);
3167 __ b(eq, if_false);
3168 // Check for undetectable objects => false.
3169 __ ldrb(r0, FieldMemOperand(r1, Map::kBitFieldOffset));
3170 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3171 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
3172 __ b(eq, if_false);
3173 // Check for JS objects => true.
3174 __ ldrb(r0, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3175 __ cmp(r0, Operand(FIRST_JS_OBJECT_TYPE));
3176 __ b(lt, if_false);
3177 __ cmp(r0, Operand(LAST_JS_OBJECT_TYPE));
3178 Split(le, if_true, if_false, fall_through);
3179 } else {
3180 if (if_false != fall_through) __ jmp(if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003181 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003182
3183 return true;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003184}
3185
3186
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003187void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
3188 Comment cmnt(masm_, "[ CompareOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00003189 SetSourcePosition(expr->position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003190
3191 // Always perform the comparison for its control flow. Pack the result
3192 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003193
3194 Label materialize_true, materialize_false;
3195 Label* if_true = NULL;
3196 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003197 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003198 context()->PrepareTest(&materialize_true, &materialize_false,
3199 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003200
3201 // First we try a fast inlined version of the compare when one of
3202 // the operands is a literal.
3203 Token::Value op = expr->op();
3204 Expression* left = expr->left();
3205 Expression* right = expr->right();
3206 if (TryLiteralCompare(op, left, right, if_true, if_false, fall_through)) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003207 context()->Plug(if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003208 return;
3209 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003210
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003211 VisitForStackValue(expr->left());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003212 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003213 case Token::IN:
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003214 VisitForStackValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003215 __ InvokeBuiltin(Builtins::IN, CALL_JS);
3216 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3217 __ cmp(r0, ip);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003218 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003219 break;
3220
3221 case Token::INSTANCEOF: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003222 VisitForStackValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003223 InstanceofStub stub;
3224 __ CallStub(&stub);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003225 // The stub returns 0 for true.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003226 __ tst(r0, r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003227 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003228 break;
3229 }
3230
3231 default: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003232 VisitForAccumulatorValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003233 Condition cc = eq;
3234 bool strict = false;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003235 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003236 case Token::EQ_STRICT:
3237 strict = true;
3238 // Fall through
ricow@chromium.org65fae842010-08-25 15:26:24 +00003239 case Token::EQ:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003240 cc = eq;
3241 __ pop(r1);
3242 break;
3243 case Token::LT:
3244 cc = lt;
3245 __ pop(r1);
3246 break;
3247 case Token::GT:
3248 // Reverse left and right sides to obtain ECMA-262 conversion order.
3249 cc = lt;
3250 __ mov(r1, result_register());
3251 __ pop(r0);
3252 break;
3253 case Token::LTE:
3254 // Reverse left and right sides to obtain ECMA-262 conversion order.
3255 cc = ge;
3256 __ mov(r1, result_register());
3257 __ pop(r0);
3258 break;
3259 case Token::GTE:
3260 cc = ge;
3261 __ pop(r1);
3262 break;
3263 case Token::IN:
3264 case Token::INSTANCEOF:
3265 default:
3266 UNREACHABLE();
3267 }
3268
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003269 bool inline_smi_code = ShouldInlineSmiCase(op);
3270 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003271 Label slow_case;
3272 __ orr(r2, r0, Operand(r1));
3273 __ BranchOnNotSmi(r2, &slow_case);
3274 __ cmp(r1, r0);
3275 Split(cc, if_true, if_false, NULL);
3276 __ bind(&slow_case);
3277 }
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003278 CompareFlags flags = inline_smi_code
3279 ? NO_SMI_COMPARE_IN_STUB
3280 : NO_COMPARE_FLAGS;
3281 CompareStub stub(cc, strict, flags, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003282 __ CallStub(&stub);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00003283 __ cmp(r0, Operand(0, RelocInfo::NONE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003284 Split(cc, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003285 }
3286 }
3287
3288 // Convert the result of the comparison into one expected for this
3289 // expression's context.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003290 context()->Plug(if_true, if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003291}
3292
3293
ricow@chromium.org65fae842010-08-25 15:26:24 +00003294void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
3295 Comment cmnt(masm_, "[ CompareToNull");
3296 Label materialize_true, materialize_false;
3297 Label* if_true = NULL;
3298 Label* if_false = NULL;
3299 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003300 context()->PrepareTest(&materialize_true, &materialize_false,
3301 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003302
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003303 VisitForAccumulatorValue(expr->expression());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003304 __ LoadRoot(r1, Heap::kNullValueRootIndex);
3305 __ cmp(r0, r1);
3306 if (expr->is_strict()) {
3307 Split(eq, if_true, if_false, fall_through);
3308 } else {
3309 __ b(eq, if_true);
3310 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
3311 __ cmp(r0, r1);
3312 __ b(eq, if_true);
3313 __ tst(r0, Operand(kSmiTagMask));
3314 __ b(eq, if_false);
3315 // It can be an undetectable object.
3316 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3317 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3318 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3319 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3320 Split(eq, if_true, if_false, fall_through);
3321 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003322 context()->Plug(if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003323}
3324
3325
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003326void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3327 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003328 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003329}
3330
3331
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003332Register FullCodeGenerator::result_register() {
3333 return r0;
3334}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003335
3336
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003337Register FullCodeGenerator::context_register() {
3338 return cp;
3339}
3340
3341
3342void FullCodeGenerator::EmitCallIC(Handle<Code> ic, RelocInfo::Mode mode) {
3343 ASSERT(mode == RelocInfo::CODE_TARGET ||
3344 mode == RelocInfo::CODE_TARGET_CONTEXT);
3345 __ Call(ic, mode);
3346}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003347
3348
3349void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3350 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3351 __ str(value, MemOperand(fp, frame_offset));
3352}
3353
3354
3355void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003356 __ ldr(dst, ContextOperand(cp, context_index));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003357}
3358
3359
3360// ----------------------------------------------------------------------------
3361// Non-local control flow support.
3362
3363void FullCodeGenerator::EnterFinallyBlock() {
3364 ASSERT(!result_register().is(r1));
3365 // Store result register while executing finally block.
3366 __ push(result_register());
3367 // Cook return address in link register to stack (smi encoded Code* delta)
3368 __ sub(r1, lr, Operand(masm_->CodeObject()));
3369 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3370 ASSERT_EQ(0, kSmiTag);
3371 __ add(r1, r1, Operand(r1)); // Convert to smi.
3372 __ push(r1);
3373}
3374
3375
3376void FullCodeGenerator::ExitFinallyBlock() {
3377 ASSERT(!result_register().is(r1));
3378 // Restore result register from stack.
3379 __ pop(r1);
3380 // Uncook return address and return.
3381 __ pop(result_register());
3382 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3383 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3384 __ add(pc, r1, Operand(masm_->CodeObject()));
3385}
3386
3387
3388#undef __
3389
3390} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003391
3392#endif // V8_TARGET_ARCH_ARM