blob: 9935e038f5cdeadbf341e50497ecbf19a17a5a52 [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
vegorov@chromium.org42841962010-10-18 11:18:59 +000065#ifdef DEBUG
66 if (strlen(FLAG_stop_at) > 0 &&
67 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
68 __ stop("stop-at");
69 }
70#endif
71
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000072 int locals_count = scope()->num_stack_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000073
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000074 __ Push(lr, fp, cp, r1);
75 if (locals_count > 0) {
76 // Load undefined value here, so the value is ready for the loop
77 // below.
78 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
79 }
80 // Adjust fp to point to caller's fp.
81 __ add(fp, sp, Operand(2 * kPointerSize));
82
83 { Comment cmnt(masm_, "[ Allocate locals");
84 for (int i = 0; i < locals_count; i++) {
85 __ push(ip);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000086 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000087 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000088
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000089 bool function_in_register = true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000090
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000091 // Possibly allocate a local context.
92 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
93 if (heap_slots > 0) {
94 Comment cmnt(masm_, "[ Allocate local context");
95 // Argument to NewContext is the function, which is in r1.
96 __ push(r1);
97 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
98 FastNewContextStub stub(heap_slots);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000099 __ CallStub(&stub);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000100 } else {
101 __ CallRuntime(Runtime::kNewContext, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000102 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000103 function_in_register = false;
104 // Context is returned in both r0 and cp. It replaces the context
105 // passed to us. It's saved in the stack and kept live in cp.
106 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
107 // Copy any necessary parameters into the context.
108 int num_parameters = scope()->num_parameters();
109 for (int i = 0; i < num_parameters; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000110 Slot* slot = scope()->parameter(i)->AsSlot();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000111 if (slot != NULL && slot->type() == Slot::CONTEXT) {
112 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
113 (num_parameters - 1 - i) * kPointerSize;
114 // Load parameter from stack.
115 __ ldr(r0, MemOperand(fp, parameter_offset));
116 // Store it in the context.
117 __ mov(r1, Operand(Context::SlotOffset(slot->index())));
118 __ str(r0, MemOperand(cp, r1));
119 // Update the write barrier. This clobbers all involved
120 // registers, so we have to use two more registers to avoid
121 // clobbering cp.
122 __ mov(r2, Operand(cp));
123 __ RecordWrite(r2, Operand(r1), r3, r0);
124 }
125 }
126 }
127
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000128 Variable* arguments = scope()->arguments();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000129 if (arguments != NULL) {
130 // Function uses arguments object.
131 Comment cmnt(masm_, "[ Allocate arguments object");
132 if (!function_in_register) {
133 // Load this again, if it's used by the local context below.
134 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
135 } else {
136 __ mov(r3, r1);
137 }
138 // Receiver is just before the parameters on the caller's stack.
139 int offset = scope()->num_parameters() * kPointerSize;
140 __ add(r2, fp,
141 Operand(StandardFrameConstants::kCallerSPOffset + offset));
142 __ mov(r1, Operand(Smi::FromInt(scope()->num_parameters())));
143 __ Push(r3, r2, r1);
144
145 // Arguments to ArgumentsAccessStub:
146 // function, receiver address, parameter count.
147 // The stub will rewrite receiever and parameter count if the previous
148 // stack frame was an arguments adapter frame.
149 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
150 __ CallStub(&stub);
151 // Duplicate the value; move-to-slot operation might clobber registers.
152 __ mov(r3, r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000153 Move(arguments->AsSlot(), r0, r1, r2);
154 Slot* dot_arguments_slot = scope()->arguments_shadow()->AsSlot();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000155 Move(dot_arguments_slot, r3, r1, r2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000156 }
157
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000158 { Comment cmnt(masm_, "[ Declarations");
159 // For named function expressions, declare the function name as a
160 // constant.
161 if (scope()->is_function_scope() && scope()->function() != NULL) {
162 EmitDeclaration(scope()->function(), Variable::CONST, NULL);
163 }
164 // Visit all the explicit declarations unless there is an illegal
165 // redeclaration.
166 if (scope()->HasIllegalRedeclaration()) {
167 scope()->VisitIllegalRedeclaration(this);
168 } else {
169 VisitDeclarations(scope()->declarations());
170 }
171 }
172
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000173 // Check the stack for overflow or break request.
174 // Put the lr setup instruction in the delay slot. The kInstrSize is
175 // added to the implicit 8 byte offset that always applies to operations
176 // with pc and gives a return address 12 bytes down.
177 { Comment cmnt(masm_, "[ Stack check");
178 __ LoadRoot(r2, Heap::kStackLimitRootIndex);
179 __ add(lr, pc, Operand(Assembler::kInstrSize));
180 __ cmp(sp, Operand(r2));
181 StackCheckStub stub;
182 __ mov(pc,
183 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
184 RelocInfo::CODE_TARGET),
185 LeaveCC,
186 lo);
187 }
188
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000189 if (FLAG_trace) {
190 __ CallRuntime(Runtime::kTraceEnter, 0);
191 }
192
193 { Comment cmnt(masm_, "[ Body");
194 ASSERT(loop_depth() == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000195 VisitStatements(function()->body());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000196 ASSERT(loop_depth() == 0);
197 }
198
199 { Comment cmnt(masm_, "[ return <undefined>;");
200 // Emit a 'return undefined' in case control fell off the end of the
201 // body.
202 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
203 }
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000204 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000205}
206
207
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000208void FullCodeGenerator::EmitReturnSequence() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000209 Comment cmnt(masm_, "[ Return sequence");
210 if (return_label_.is_bound()) {
211 __ b(&return_label_);
212 } else {
213 __ bind(&return_label_);
214 if (FLAG_trace) {
215 // Push the return value on the stack as the parameter.
216 // Runtime::TraceExit returns its parameter in r0.
217 __ push(r0);
218 __ CallRuntime(Runtime::kTraceExit, 1);
219 }
220
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000221#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000222 // Add a label for checking the size of the code used for returning.
223 Label check_exit_codesize;
224 masm_->bind(&check_exit_codesize);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000225#endif
226 // Make sure that the constant pool is not emitted inside of the return
227 // sequence.
228 { Assembler::BlockConstPoolScope block_const_pool(masm_);
229 // Here we use masm_-> instead of the __ macro to avoid the code coverage
230 // tool from instrumenting as we rely on the code size here.
231 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000232 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000233 __ RecordJSReturn();
234 masm_->mov(sp, fp);
235 masm_->ldm(ia_w, sp, fp.bit() | lr.bit());
236 masm_->add(sp, sp, Operand(sp_delta));
237 masm_->Jump(lr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000238 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000239
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000240#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000241 // Check that the size of the code used for returning matches what is
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000242 // expected by the debugger. If the sp_delts above cannot be encoded in the
243 // add instruction the add will generate two instructions.
244 int return_sequence_length =
245 masm_->InstructionsGeneratedSince(&check_exit_codesize);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000246 CHECK(return_sequence_length ==
247 Assembler::kJSReturnSequenceInstructions ||
248 return_sequence_length ==
249 Assembler::kJSReturnSequenceInstructions + 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000250#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000251 }
252}
253
254
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000255FullCodeGenerator::ConstantOperand FullCodeGenerator::GetConstantOperand(
256 Token::Value op, Expression* left, Expression* right) {
257 ASSERT(ShouldInlineSmiCase(op));
258 return kNoConstants;
259}
260
261
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000262void FullCodeGenerator::EffectContext::Plug(Slot* slot) const {
263}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000264
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000265
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000266void FullCodeGenerator::AccumulatorValueContext::Plug(Slot* slot) const {
267 codegen()->Move(result_register(), slot);
268}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000269
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000270
271void FullCodeGenerator::StackValueContext::Plug(Slot* slot) const {
272 codegen()->Move(result_register(), slot);
273 __ push(result_register());
274}
275
276
277void FullCodeGenerator::TestContext::Plug(Slot* slot) const {
278 // For simplicity we always test the accumulator register.
279 codegen()->Move(result_register(), slot);
280 codegen()->DoTest(true_label_, false_label_, fall_through_);
281}
282
283
284void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
285}
286
287
288void FullCodeGenerator::AccumulatorValueContext::Plug(
289 Heap::RootListIndex index) const {
290 __ LoadRoot(result_register(), index);
291}
292
293
294void FullCodeGenerator::StackValueContext::Plug(
295 Heap::RootListIndex index) const {
296 __ LoadRoot(result_register(), index);
297 __ push(result_register());
298}
299
300
301void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
302 if (index == Heap::kUndefinedValueRootIndex ||
303 index == Heap::kNullValueRootIndex ||
304 index == Heap::kFalseValueRootIndex) {
305 __ b(false_label_);
306 } else if (index == Heap::kTrueValueRootIndex) {
307 __ b(true_label_);
308 } else {
309 __ LoadRoot(result_register(), index);
310 codegen()->DoTest(true_label_, false_label_, fall_through_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000311 }
312}
313
314
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000315void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
316}
317
318
319void FullCodeGenerator::AccumulatorValueContext::Plug(
320 Handle<Object> lit) const {
321 __ mov(result_register(), Operand(lit));
322}
323
324
325void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
326 // Immediates can be pushed directly.
327 __ mov(result_register(), Operand(lit));
328 __ push(result_register());
329}
330
331
332void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
333 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
334 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
335 __ b(false_label_);
336 } else if (lit->IsTrue() || lit->IsJSObject()) {
337 __ b(true_label_);
338 } else if (lit->IsString()) {
339 if (String::cast(*lit)->length() == 0) {
340 __ b(false_label_);
341 } else {
342 __ b(true_label_);
343 }
344 } else if (lit->IsSmi()) {
345 if (Smi::cast(*lit)->value() == 0) {
346 __ b(false_label_);
347 } else {
348 __ b(true_label_);
349 }
350 } else {
351 // For simplicity we always test the accumulator register.
352 __ mov(result_register(), Operand(lit));
353 codegen()->DoTest(true_label_, false_label_, fall_through_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000354 }
355}
356
357
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000358void FullCodeGenerator::EffectContext::DropAndPlug(int count,
359 Register reg) const {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000360 ASSERT(count > 0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000361 __ Drop(count);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000362}
363
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000364
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000365void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
366 int count,
367 Register reg) const {
368 ASSERT(count > 0);
369 __ Drop(count);
370 __ Move(result_register(), reg);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000371}
372
373
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000374void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
375 Register reg) const {
376 ASSERT(count > 0);
377 if (count > 1) __ Drop(count - 1);
378 __ str(reg, MemOperand(sp, 0));
379}
380
381
382void FullCodeGenerator::TestContext::DropAndPlug(int count,
383 Register reg) const {
384 ASSERT(count > 0);
385 // For simplicity we always test the accumulator register.
386 __ Drop(count);
387 __ Move(result_register(), reg);
388 codegen()->DoTest(true_label_, false_label_, fall_through_);
389}
390
391
392void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
393 Label* materialize_false) const {
394 ASSERT_EQ(materialize_true, materialize_false);
395 __ bind(materialize_true);
396}
397
398
399void FullCodeGenerator::AccumulatorValueContext::Plug(
400 Label* materialize_true,
401 Label* materialize_false) const {
402 Label done;
403 __ bind(materialize_true);
404 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
405 __ jmp(&done);
406 __ bind(materialize_false);
407 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
408 __ bind(&done);
409}
410
411
412void FullCodeGenerator::StackValueContext::Plug(
413 Label* materialize_true,
414 Label* materialize_false) const {
415 Label done;
416 __ bind(materialize_true);
417 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
418 __ push(ip);
419 __ jmp(&done);
420 __ bind(materialize_false);
421 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
422 __ push(ip);
423 __ bind(&done);
424}
425
426
427void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
428 Label* materialize_false) const {
429 ASSERT(materialize_false == false_label_);
430 ASSERT(materialize_true == true_label_);
431}
432
433
434void FullCodeGenerator::EffectContext::Plug(bool flag) const {
435}
436
437
438void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
439 Heap::RootListIndex value_root_index =
440 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
441 __ LoadRoot(result_register(), value_root_index);
442}
443
444
445void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
446 Heap::RootListIndex value_root_index =
447 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
448 __ LoadRoot(ip, value_root_index);
449 __ push(ip);
450}
451
452
453void FullCodeGenerator::TestContext::Plug(bool flag) const {
454 if (flag) {
455 if (true_label_ != fall_through_) __ b(true_label_);
456 } else {
457 if (false_label_ != fall_through_) __ b(false_label_);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000458 }
459}
460
461
ricow@chromium.org65fae842010-08-25 15:26:24 +0000462void FullCodeGenerator::DoTest(Label* if_true,
463 Label* if_false,
464 Label* fall_through) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000465 // Call the runtime to find the boolean value of the source and then
466 // translate it into control flow to the pair of labels.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000467 __ push(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000468 __ CallRuntime(Runtime::kToBool, 1);
469 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
470 __ cmp(r0, ip);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000471 Split(eq, if_true, if_false, fall_through);
472}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000473
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000474
ricow@chromium.org65fae842010-08-25 15:26:24 +0000475void FullCodeGenerator::Split(Condition cc,
476 Label* if_true,
477 Label* if_false,
478 Label* fall_through) {
479 if (if_false == fall_through) {
480 __ b(cc, if_true);
481 } else if (if_true == fall_through) {
482 __ b(NegateCondition(cc), if_false);
483 } else {
484 __ b(cc, if_true);
485 __ b(if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000486 }
487}
488
489
490MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
491 switch (slot->type()) {
492 case Slot::PARAMETER:
493 case Slot::LOCAL:
494 return MemOperand(fp, SlotOffset(slot));
495 case Slot::CONTEXT: {
496 int context_chain_length =
ager@chromium.org5c838252010-02-19 08:53:10 +0000497 scope()->ContextChainLength(slot->var()->scope());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000498 __ LoadContext(scratch, context_chain_length);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000499 return ContextOperand(scratch, slot->index());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000500 }
501 case Slot::LOOKUP:
502 UNREACHABLE();
503 }
504 UNREACHABLE();
505 return MemOperand(r0, 0);
506}
507
508
509void FullCodeGenerator::Move(Register destination, Slot* source) {
510 // Use destination as scratch.
511 MemOperand slot_operand = EmitSlotSearch(source, destination);
512 __ ldr(destination, slot_operand);
513}
514
515
516void FullCodeGenerator::Move(Slot* dst,
517 Register src,
518 Register scratch1,
519 Register scratch2) {
520 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
521 ASSERT(!scratch1.is(src) && !scratch2.is(src));
522 MemOperand location = EmitSlotSearch(dst, scratch1);
523 __ str(src, location);
524 // Emit the write barrier code if the location is in the heap.
525 if (dst->type() == Slot::CONTEXT) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000526 __ RecordWrite(scratch1,
527 Operand(Context::SlotOffset(dst->index())),
528 scratch2,
529 src);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000530 }
531}
532
533
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000534void FullCodeGenerator::EmitDeclaration(Variable* variable,
535 Variable::Mode mode,
536 FunctionLiteral* function) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000537 Comment cmnt(masm_, "[ Declaration");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000538 ASSERT(variable != NULL); // Must have been resolved.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000539 Slot* slot = variable->AsSlot();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000540 Property* prop = variable->AsProperty();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000541
542 if (slot != NULL) {
543 switch (slot->type()) {
544 case Slot::PARAMETER:
545 case Slot::LOCAL:
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000546 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000547 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
548 __ str(ip, MemOperand(fp, SlotOffset(slot)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000549 } else if (function != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000550 VisitForAccumulatorValue(function);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000551 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
552 }
553 break;
554
555 case Slot::CONTEXT:
556 // We bypass the general EmitSlotSearch because we know more about
557 // this specific context.
558
559 // The variable in the decl always resides in the current context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000560 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000561 if (FLAG_debug_code) {
562 // Check if we have the correct context pointer.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000563 __ ldr(r1, ContextOperand(cp, Context::FCONTEXT_INDEX));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000564 __ cmp(r1, cp);
565 __ Check(eq, "Unexpected declaration in current context.");
566 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000567 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000568 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000569 __ str(ip, ContextOperand(cp, slot->index()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000570 // No write barrier since the_hole_value is in old space.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000571 } else if (function != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000572 VisitForAccumulatorValue(function);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000573 __ str(result_register(), ContextOperand(cp, slot->index()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000574 int offset = Context::SlotOffset(slot->index());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000575 // We know that we have written a function, which is not a smi.
576 __ mov(r1, Operand(cp));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000577 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000578 }
579 break;
580
581 case Slot::LOOKUP: {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000582 __ mov(r2, Operand(variable->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000583 // Declaration nodes are always introduced in one of two modes.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000584 ASSERT(mode == Variable::VAR ||
585 mode == Variable::CONST);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000586 PropertyAttributes attr =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000587 (mode == Variable::VAR) ? NONE : READ_ONLY;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000588 __ mov(r1, Operand(Smi::FromInt(attr)));
589 // Push initial value, if any.
590 // Note: For variables we must not push an initial value (such as
591 // 'undefined') because we may have a (legal) redeclaration and we
592 // must not destroy the current value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000593 if (mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000594 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000595 __ Push(cp, r2, r1, r0);
596 } else if (function != NULL) {
597 __ Push(cp, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000598 // Push initial value for function declaration.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000599 VisitForStackValue(function);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000600 } else {
601 __ mov(r0, Operand(Smi::FromInt(0))); // No initial value!
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000602 __ Push(cp, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000603 }
604 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
605 break;
606 }
607 }
608
609 } else if (prop != NULL) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000610 if (function != NULL || mode == Variable::CONST) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000611 // We are declaring a function or constant that rewrites to a
612 // property. Use (keyed) IC to set the initial value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000613 VisitForStackValue(prop->obj());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000614 if (function != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000615 VisitForStackValue(prop->key());
616 VisitForAccumulatorValue(function);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000617 __ pop(r1); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000618 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000619 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000620 __ mov(r1, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000621 __ LoadRoot(result_register(), Heap::kTheHoleValueRootIndex);
622 }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000623 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000624
625 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000626 EmitCallIC(ic, RelocInfo::CODE_TARGET);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000627 // Value in r0 is ignored (declarations are statements).
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000628 }
629 }
630}
631
632
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000633void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
634 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
635}
636
637
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000638void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
639 // Call the runtime to declare the globals.
640 // The context is the first argument.
641 __ mov(r1, Operand(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000642 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000643 __ Push(cp, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000644 __ CallRuntime(Runtime::kDeclareGlobals, 3);
645 // Return value is ignored.
646}
647
648
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000649void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000650 Comment cmnt(masm_, "[ SwitchStatement");
651 Breakable nested_statement(this, stmt);
652 SetStatementPosition(stmt);
653 // Keep the switch value on the stack until a case matches.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000654 VisitForStackValue(stmt->tag());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000655
656 ZoneList<CaseClause*>* clauses = stmt->cases();
657 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
658
659 Label next_test; // Recycled for each test.
660 // Compile all the tests with branches to their bodies.
661 for (int i = 0; i < clauses->length(); i++) {
662 CaseClause* clause = clauses->at(i);
663 // The default is not a test, but remember it as final fall through.
664 if (clause->is_default()) {
665 default_clause = clause;
666 continue;
667 }
668
669 Comment cmnt(masm_, "[ Case comparison");
670 __ bind(&next_test);
671 next_test.Unuse();
672
673 // Compile the label expression.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000674 VisitForAccumulatorValue(clause->label());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000675
ricow@chromium.org65fae842010-08-25 15:26:24 +0000676 // Perform the comparison as if via '==='.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000677 __ ldr(r1, MemOperand(sp, 0)); // Switch value.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000678 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
679 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000680 Label slow_case;
681 __ orr(r2, r1, r0);
682 __ tst(r2, Operand(kSmiTagMask));
683 __ b(ne, &slow_case);
684 __ cmp(r1, r0);
685 __ b(ne, &next_test);
686 __ Drop(1); // Switch value is no longer needed.
687 __ b(clause->body_target()->entry_label());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000688 __ bind(&slow_case);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000689 }
690
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000691 CompareFlags flags = inline_smi_code
692 ? NO_SMI_COMPARE_IN_STUB
693 : NO_COMPARE_FLAGS;
694 CompareStub stub(eq, true, flags, r1, r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000695 __ CallStub(&stub);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000696 __ cmp(r0, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000697 __ b(ne, &next_test);
698 __ Drop(1); // Switch value is no longer needed.
699 __ b(clause->body_target()->entry_label());
700 }
701
702 // Discard the test value and jump to the default if present, otherwise to
703 // the end of the statement.
704 __ bind(&next_test);
705 __ Drop(1); // Switch value is no longer needed.
706 if (default_clause == NULL) {
707 __ b(nested_statement.break_target());
708 } else {
709 __ b(default_clause->body_target()->entry_label());
710 }
711
712 // Compile all the case bodies.
713 for (int i = 0; i < clauses->length(); i++) {
714 Comment cmnt(masm_, "[ Case body");
715 CaseClause* clause = clauses->at(i);
716 __ bind(clause->body_target()->entry_label());
717 VisitStatements(clause->statements());
718 }
719
720 __ bind(nested_statement.break_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000721}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000722
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000723
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000724void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000725 Comment cmnt(masm_, "[ ForInStatement");
726 SetStatementPosition(stmt);
727
728 Label loop, exit;
729 ForIn loop_statement(this, stmt);
730 increment_loop_depth();
731
732 // Get the object to enumerate over. Both SpiderMonkey and JSC
733 // ignore null and undefined in contrast to the specification; see
734 // ECMA-262 section 12.6.4.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000735 VisitForAccumulatorValue(stmt->enumerable());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000736 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
737 __ cmp(r0, ip);
738 __ b(eq, &exit);
739 __ LoadRoot(ip, Heap::kNullValueRootIndex);
740 __ cmp(r0, ip);
741 __ b(eq, &exit);
742
743 // Convert the object to a JS object.
744 Label convert, done_convert;
745 __ BranchOnSmi(r0, &convert);
746 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
747 __ b(hs, &done_convert);
748 __ bind(&convert);
749 __ push(r0);
750 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
751 __ bind(&done_convert);
752 __ push(r0);
753
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000754 // BUG(867): Check cache validity in generated code. This is a fast
755 // case for the JSObject::IsSimpleEnum cache validity checks. If we
756 // cannot guarantee cache validity, call the runtime system to check
757 // cache validity or get the property names in a fixed array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000758
759 // Get the set of properties to enumerate.
760 __ push(r0); // Duplicate the enumerable object on the stack.
761 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
762
763 // If we got a map from the runtime call, we can do a fast
764 // modification check. Otherwise, we got a fixed array, and we have
765 // to do a slow check.
766 Label fixed_array;
767 __ mov(r2, r0);
768 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
769 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
770 __ cmp(r1, ip);
771 __ b(ne, &fixed_array);
772
773 // We got a map in register r0. Get the enumeration cache from it.
774 __ ldr(r1, FieldMemOperand(r0, Map::kInstanceDescriptorsOffset));
775 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
776 __ ldr(r2, FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
777
778 // Setup the four remaining stack slots.
779 __ push(r0); // Map.
780 __ ldr(r1, FieldMemOperand(r2, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000781 __ mov(r0, Operand(Smi::FromInt(0)));
782 // Push enumeration cache, enumeration cache length (as smi) and zero.
783 __ Push(r2, r1, r0);
784 __ jmp(&loop);
785
786 // We got a fixed array in register r0. Iterate through that.
787 __ bind(&fixed_array);
788 __ mov(r1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
789 __ Push(r1, r0);
790 __ ldr(r1, FieldMemOperand(r0, FixedArray::kLengthOffset));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000791 __ mov(r0, Operand(Smi::FromInt(0)));
792 __ Push(r1, r0); // Fixed array length (as smi) and initial index.
793
794 // Generate code for doing the condition check.
795 __ bind(&loop);
796 // Load the current count to r0, load the length to r1.
797 __ Ldrd(r0, r1, MemOperand(sp, 0 * kPointerSize));
798 __ cmp(r0, r1); // Compare to the array length.
799 __ b(hs, loop_statement.break_target());
800
801 // Get the current entry of the array into register r3.
802 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
803 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
804 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
805
806 // Get the expected map from the stack or a zero map in the
807 // permanent slow case into register r2.
808 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
809
810 // Check if the expected map still matches that of the enumerable.
811 // If not, we have to filter the key.
812 Label update_each;
813 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
814 __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
815 __ cmp(r4, Operand(r2));
816 __ b(eq, &update_each);
817
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000818 // Convert the entry to a string or (smi) 0 if it isn't a property
819 // any more. If the property has been removed while iterating, we
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000820 // just skip it.
821 __ push(r1); // Enumerable.
822 __ push(r3); // Current entry.
823 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000824 __ mov(r3, Operand(r0), SetCC);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000825 __ b(eq, loop_statement.continue_target());
826
827 // Update the 'each' property or variable from the possibly filtered
828 // entry in register r3.
829 __ bind(&update_each);
830 __ mov(result_register(), r3);
831 // Perform the assignment as if via '='.
832 EmitAssignment(stmt->each());
833
834 // Generate code for the body of the loop.
835 Label stack_limit_hit, stack_check_done;
836 Visit(stmt->body());
837
838 __ StackLimitCheck(&stack_limit_hit);
839 __ bind(&stack_check_done);
840
841 // Generate code for the going to the next element by incrementing
842 // the index (smi) stored on top of the stack.
843 __ bind(loop_statement.continue_target());
844 __ pop(r0);
845 __ add(r0, r0, Operand(Smi::FromInt(1)));
846 __ push(r0);
847 __ b(&loop);
848
849 // Slow case for the stack limit check.
850 StackCheckStub stack_check_stub;
851 __ bind(&stack_limit_hit);
852 __ CallStub(&stack_check_stub);
853 __ b(&stack_check_done);
854
855 // Remove the pointers stored on the stack.
856 __ bind(loop_statement.break_target());
857 __ Drop(5);
858
859 // Exit and decrement the loop depth.
860 __ bind(&exit);
861 decrement_loop_depth();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000862}
863
864
865void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info) {
866 // Use the fast case closure allocation code that allocates in new
867 // space for nested functions that don't need literals cloning.
868 if (scope()->is_function_scope() && info->num_literals() == 0) {
869 FastNewClosureStub stub;
870 __ mov(r0, Operand(info));
871 __ push(r0);
872 __ CallStub(&stub);
873 } else {
874 __ mov(r0, Operand(info));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000875 __ Push(cp, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000876 __ CallRuntime(Runtime::kNewClosure, 2);
877 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000878 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000879}
880
881
882void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
883 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000884 EmitVariableLoad(expr->var());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000885}
886
887
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000888MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(
889 Slot* slot,
890 Label* slow) {
891 ASSERT(slot->type() == Slot::CONTEXT);
892 Register current = cp;
893 Register next = r3;
894 Register temp = r4;
895
896 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
897 if (s->num_heap_slots() > 0) {
898 if (s->calls_eval()) {
899 // Check that extension is NULL.
900 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
901 __ tst(temp, temp);
902 __ b(ne, slow);
903 }
904 __ ldr(next, ContextOperand(current, Context::CLOSURE_INDEX));
905 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
906 // Walk the rest of the chain without clobbering cp.
907 current = next;
908 }
909 }
910 // Check that last extension is NULL.
911 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
912 __ tst(temp, temp);
913 __ b(ne, slow);
914 __ ldr(temp, ContextOperand(current, Context::FCONTEXT_INDEX));
915 return ContextOperand(temp, slot->index());
916}
917
918
919void FullCodeGenerator::EmitDynamicLoadFromSlotFastCase(
920 Slot* slot,
921 TypeofState typeof_state,
922 Label* slow,
923 Label* done) {
924 // Generate fast-case code for variables that might be shadowed by
925 // eval-introduced variables. Eval is used a lot without
926 // introducing variables. In those cases, we do not want to
927 // perform a runtime call for all variables in the scope
928 // containing the eval.
929 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
930 EmitLoadGlobalSlotCheckExtensions(slot, typeof_state, slow);
931 __ jmp(done);
932 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000933 Slot* potential_slot = slot->var()->local_if_not_shadowed()->AsSlot();
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000934 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
935 if (potential_slot != NULL) {
936 // Generate fast case for locals that rewrite to slots.
937 __ ldr(r0, ContextSlotOperandCheckExtensions(potential_slot, slow));
938 if (potential_slot->var()->mode() == Variable::CONST) {
939 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
940 __ cmp(r0, ip);
941 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
942 }
943 __ jmp(done);
944 } else if (rewrite != NULL) {
945 // Generate fast case for calls of an argument function.
946 Property* property = rewrite->AsProperty();
947 if (property != NULL) {
948 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
949 Literal* key_literal = property->key()->AsLiteral();
950 if (obj_proxy != NULL &&
951 key_literal != NULL &&
952 obj_proxy->IsArguments() &&
953 key_literal->handle()->IsSmi()) {
954 // Load arguments object if there are no eval-introduced
955 // variables. Then load the argument from the arguments
956 // object using keyed load.
957 __ ldr(r1,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000958 ContextSlotOperandCheckExtensions(obj_proxy->var()->AsSlot(),
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000959 slow));
960 __ mov(r0, Operand(key_literal->handle()));
961 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000962 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000963 __ jmp(done);
964 }
965 }
966 }
967 }
968}
969
970
971void FullCodeGenerator::EmitLoadGlobalSlotCheckExtensions(
972 Slot* slot,
973 TypeofState typeof_state,
974 Label* slow) {
975 Register current = cp;
976 Register next = r1;
977 Register temp = r2;
978
979 Scope* s = scope();
980 while (s != NULL) {
981 if (s->num_heap_slots() > 0) {
982 if (s->calls_eval()) {
983 // Check that extension is NULL.
984 __ ldr(temp, ContextOperand(current, Context::EXTENSION_INDEX));
985 __ tst(temp, temp);
986 __ b(ne, slow);
987 }
988 // Load next context in chain.
989 __ ldr(next, ContextOperand(current, Context::CLOSURE_INDEX));
990 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
991 // Walk the rest of the chain without clobbering cp.
992 current = next;
993 }
994 // If no outer scope calls eval, we do not need to check more
995 // context extensions.
996 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
997 s = s->outer_scope();
998 }
999
1000 if (s->is_eval_scope()) {
1001 Label loop, fast;
1002 if (!current.is(next)) {
1003 __ Move(next, current);
1004 }
1005 __ bind(&loop);
1006 // Terminate at global context.
1007 __ ldr(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1008 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1009 __ cmp(temp, ip);
1010 __ b(eq, &fast);
1011 // Check that extension is NULL.
1012 __ ldr(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1013 __ tst(temp, temp);
1014 __ b(ne, slow);
1015 // Load next context in chain.
1016 __ ldr(next, ContextOperand(next, Context::CLOSURE_INDEX));
1017 __ ldr(next, FieldMemOperand(next, JSFunction::kContextOffset));
1018 __ b(&loop);
1019 __ bind(&fast);
1020 }
1021
1022 __ ldr(r0, CodeGenerator::GlobalObject());
1023 __ mov(r2, Operand(slot->var()->name()));
1024 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1025 ? RelocInfo::CODE_TARGET
1026 : RelocInfo::CODE_TARGET_CONTEXT;
1027 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001028 EmitCallIC(ic, mode);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001029}
1030
1031
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001032void FullCodeGenerator::EmitVariableLoad(Variable* var) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001033 // Four cases: non-this global variables, lookup slots, all other
1034 // types of slots, and parameters that rewrite to explicit property
1035 // accesses on the arguments object.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001036 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001037 Property* property = var->AsProperty();
1038
1039 if (var->is_global() && !var->is_this()) {
1040 Comment cmnt(masm_, "Global variable");
1041 // Use inline caching. Variable name is passed in r2 and the global
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001042 // object (receiver) in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001043 __ ldr(r0, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001044 __ mov(r2, Operand(var->name()));
1045 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001046 EmitCallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001047 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001048
1049 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001050 Label done, slow;
1051
1052 // Generate code for loading from variables potentially shadowed
1053 // by eval-introduced variables.
1054 EmitDynamicLoadFromSlotFastCase(slot, NOT_INSIDE_TYPEOF, &slow, &done);
1055
1056 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001057 Comment cmnt(masm_, "Lookup slot");
1058 __ mov(r1, Operand(var->name()));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001059 __ Push(cp, r1); // Context and name.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001060 __ CallRuntime(Runtime::kLoadContextSlot, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001061 __ bind(&done);
1062
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001063 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001064
1065 } else if (slot != NULL) {
1066 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1067 ? "Context slot"
1068 : "Stack slot");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001069 if (var->mode() == Variable::CONST) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001070 // Constants may be the hole value if they have not been initialized.
1071 // Unhole them.
1072 MemOperand slot_operand = EmitSlotSearch(slot, r0);
1073 __ ldr(r0, slot_operand);
1074 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1075 __ cmp(r0, ip);
1076 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
1077 context()->Plug(r0);
1078 } else {
1079 context()->Plug(slot);
1080 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001081 } else {
1082 Comment cmnt(masm_, "Rewritten parameter");
1083 ASSERT_NOT_NULL(property);
1084 // Rewritten parameter accesses are of the form "slot[literal]".
1085
1086 // Assert that the object is in a slot.
1087 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
1088 ASSERT_NOT_NULL(object_var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001089 Slot* object_slot = object_var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001090 ASSERT_NOT_NULL(object_slot);
1091
1092 // Load the object.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001093 Move(r1, object_slot);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001094
1095 // Assert that the key is a smi.
1096 Literal* key_literal = property->key()->AsLiteral();
1097 ASSERT_NOT_NULL(key_literal);
1098 ASSERT(key_literal->handle()->IsSmi());
1099
1100 // Load the key.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001101 __ mov(r0, Operand(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001102
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001103 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001104 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001105 EmitCallIC(ic, RelocInfo::CODE_TARGET);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001106 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001107 }
1108}
1109
1110
1111void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1112 Comment cmnt(masm_, "[ RegExpLiteral");
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001113 Label materialized;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001114 // Registers will be used as follows:
1115 // r4 = JS function, literals array
1116 // r3 = literal index
1117 // r2 = RegExp pattern
1118 // r1 = RegExp flags
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001119 // r0 = temp + materialized value (RegExp literal)
ricow@chromium.org65fae842010-08-25 15:26:24 +00001120 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1121 __ ldr(r4, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001122 int literal_offset =
ricow@chromium.org65fae842010-08-25 15:26:24 +00001123 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001124 __ ldr(r0, FieldMemOperand(r4, literal_offset));
1125 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1126 __ cmp(r0, ip);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001127 __ b(ne, &materialized);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001128
1129 // Create regexp literal using runtime function.
1130 // Result will be in r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001131 __ mov(r3, Operand(Smi::FromInt(expr->literal_index())));
1132 __ mov(r2, Operand(expr->pattern()));
1133 __ mov(r1, Operand(expr->flags()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001134 __ Push(r4, r3, r2, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001135 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001136
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001137 __ bind(&materialized);
1138 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1139 __ push(r0);
1140 __ mov(r0, Operand(Smi::FromInt(size)));
1141 __ push(r0);
1142 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001143
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001144 // After this, registers are used as follows:
1145 // r0: Newly allocated regexp.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001146 // r1: Materialized regexp.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001147 // r2: temp.
1148 __ pop(r1);
1149 __ CopyFields(r0, r1, r2.bit(), size / kPointerSize);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001150 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001151}
1152
1153
1154void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1155 Comment cmnt(masm_, "[ ObjectLiteral");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001156 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1157 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1158 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1159 __ mov(r1, Operand(expr->constant_properties()));
1160 __ mov(r0, Operand(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001161 __ Push(r3, r2, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001162 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001163 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001164 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001165 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001166 }
1167
1168 // If result_saved is true the result is on top of the stack. If
1169 // result_saved is false the result is in r0.
1170 bool result_saved = false;
1171
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001172 // Mark all computed expressions that are bound to a key that
1173 // is shadowed by a later occurrence of the same key. For the
1174 // marked expressions, no store code is emitted.
1175 expr->CalculateEmitStore();
1176
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001177 for (int i = 0; i < expr->properties()->length(); i++) {
1178 ObjectLiteral::Property* property = expr->properties()->at(i);
1179 if (property->IsCompileTimeValue()) continue;
1180
1181 Literal* key = property->key();
1182 Expression* value = property->value();
1183 if (!result_saved) {
1184 __ push(r0); // Save result on stack
1185 result_saved = true;
1186 }
1187 switch (property->kind()) {
1188 case ObjectLiteral::Property::CONSTANT:
1189 UNREACHABLE();
1190 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1191 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1192 // Fall through.
1193 case ObjectLiteral::Property::COMPUTED:
1194 if (key->handle()->IsSymbol()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001195 VisitForAccumulatorValue(value);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001196 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001197 __ ldr(r1, MemOperand(sp));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001198 if (property->emit_store()) {
1199 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1200 EmitCallIC(ic, RelocInfo::CODE_TARGET);
1201 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001202 break;
1203 }
1204 // Fall through.
1205 case ObjectLiteral::Property::PROTOTYPE:
1206 // Duplicate receiver on stack.
1207 __ ldr(r0, MemOperand(sp));
1208 __ push(r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001209 VisitForStackValue(key);
1210 VisitForStackValue(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001211 if (property->emit_store()) {
1212 __ CallRuntime(Runtime::kSetProperty, 3);
1213 } else {
1214 __ Drop(3);
1215 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001216 break;
1217 case ObjectLiteral::Property::GETTER:
1218 case ObjectLiteral::Property::SETTER:
1219 // Duplicate receiver on stack.
1220 __ ldr(r0, MemOperand(sp));
1221 __ push(r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001222 VisitForStackValue(key);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001223 __ mov(r1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1224 Smi::FromInt(1) :
1225 Smi::FromInt(0)));
1226 __ push(r1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001227 VisitForStackValue(value);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001228 __ CallRuntime(Runtime::kDefineAccessor, 4);
1229 break;
1230 }
1231 }
1232
1233 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001234 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001235 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001236 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001237 }
1238}
1239
1240
1241void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1242 Comment cmnt(masm_, "[ ArrayLiteral");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001243
1244 ZoneList<Expression*>* subexprs = expr->values();
1245 int length = subexprs->length();
1246
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001247 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1248 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1249 __ mov(r2, Operand(Smi::FromInt(expr->literal_index())));
1250 __ mov(r1, Operand(expr->constant_elements()));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001251 __ Push(r3, r2, r1);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001252 if (expr->constant_elements()->map() == Heap::fixed_cow_array_map()) {
1253 FastCloneShallowArrayStub stub(
1254 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1255 __ CallStub(&stub);
1256 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
1257 } else if (expr->depth() > 1) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001258 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001259 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001260 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001261 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001262 FastCloneShallowArrayStub stub(
1263 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001264 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001265 }
1266
1267 bool result_saved = false; // Is the result saved to the stack?
1268
1269 // Emit code to evaluate all the non-constant subexpressions and to store
1270 // them into the newly cloned array.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001271 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001272 Expression* subexpr = subexprs->at(i);
1273 // If the subexpression is a literal or a simple materialized literal it
1274 // is already set in the cloned array.
1275 if (subexpr->AsLiteral() != NULL ||
1276 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1277 continue;
1278 }
1279
1280 if (!result_saved) {
1281 __ push(r0);
1282 result_saved = true;
1283 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001284 VisitForAccumulatorValue(subexpr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001285
1286 // Store the subexpression value in the array's elements.
1287 __ ldr(r1, MemOperand(sp)); // Copy of array literal.
1288 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1289 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1290 __ str(result_register(), FieldMemOperand(r1, offset));
1291
1292 // Update the write barrier for the array store with r0 as the scratch
1293 // register.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001294 __ RecordWrite(r1, Operand(offset), r2, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001295 }
1296
1297 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001298 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001299 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001300 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001301 }
1302}
1303
1304
ager@chromium.org5c838252010-02-19 08:53:10 +00001305void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1306 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001307 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1308 // on the left-hand side.
1309 if (!expr->target()->IsValidLeftHandSide()) {
1310 VisitForEffect(expr->target());
1311 return;
1312 }
1313
ager@chromium.org5c838252010-02-19 08:53:10 +00001314 // Left-hand side can only be a property, a global or a (parameter or local)
1315 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1316 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1317 LhsKind assign_type = VARIABLE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001318 Property* property = expr->target()->AsProperty();
1319 if (property != NULL) {
1320 assign_type = (property->key()->IsPropertyName())
1321 ? NAMED_PROPERTY
1322 : KEYED_PROPERTY;
ager@chromium.org5c838252010-02-19 08:53:10 +00001323 }
1324
1325 // Evaluate LHS expression.
1326 switch (assign_type) {
1327 case VARIABLE:
1328 // Nothing to do here.
1329 break;
1330 case NAMED_PROPERTY:
1331 if (expr->is_compound()) {
1332 // We need the receiver both on the stack and in the accumulator.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001333 VisitForAccumulatorValue(property->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00001334 __ push(result_register());
1335 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001336 VisitForStackValue(property->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00001337 }
1338 break;
1339 case KEYED_PROPERTY:
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001340 if (expr->is_compound()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001341 VisitForStackValue(property->obj());
1342 VisitForAccumulatorValue(property->key());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001343 __ ldr(r1, MemOperand(sp, 0));
1344 __ push(r0);
1345 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001346 VisitForStackValue(property->obj());
1347 VisitForStackValue(property->key());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001348 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001349 break;
1350 }
1351
ager@chromium.org5c838252010-02-19 08:53:10 +00001352 if (expr->is_compound()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001353 { AccumulatorValueContext context(this);
1354 switch (assign_type) {
1355 case VARIABLE:
1356 EmitVariableLoad(expr->target()->AsVariableProxy()->var());
1357 break;
1358 case NAMED_PROPERTY:
1359 EmitNamedPropertyLoad(property);
1360 break;
1361 case KEYED_PROPERTY:
1362 EmitKeyedPropertyLoad(property);
1363 break;
1364 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001365 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001366
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001367 Token::Value op = expr->binary_op();
1368 ConstantOperand constant = ShouldInlineSmiCase(op)
1369 ? GetConstantOperand(op, expr->target(), expr->value())
1370 : kNoConstants;
1371 ASSERT(constant == kRightConstant || constant == kNoConstants);
1372 if (constant == kNoConstants) {
1373 __ push(r0); // Left operand goes on the stack.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001374 VisitForAccumulatorValue(expr->value());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001375 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001376
ricow@chromium.org65fae842010-08-25 15:26:24 +00001377 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1378 ? OVERWRITE_RIGHT
1379 : NO_OVERWRITE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001380 SetSourcePosition(expr->position() + 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001381 AccumulatorValueContext context(this);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001382 if (ShouldInlineSmiCase(op)) {
1383 EmitInlineSmiBinaryOp(expr,
1384 op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001385 mode,
1386 expr->target(),
1387 expr->value(),
1388 constant);
1389 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001390 EmitBinaryOp(op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001391 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001392 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001393 VisitForAccumulatorValue(expr->value());
ager@chromium.org5c838252010-02-19 08:53:10 +00001394 }
1395
1396 // Record source position before possible IC call.
1397 SetSourcePosition(expr->position());
1398
1399 // Store the value.
1400 switch (assign_type) {
1401 case VARIABLE:
1402 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001403 expr->op());
ager@chromium.org5c838252010-02-19 08:53:10 +00001404 break;
1405 case NAMED_PROPERTY:
1406 EmitNamedPropertyAssignment(expr);
1407 break;
1408 case KEYED_PROPERTY:
1409 EmitKeyedPropertyAssignment(expr);
1410 break;
1411 }
1412}
1413
1414
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001415void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1416 SetSourcePosition(prop->position());
1417 Literal* key = prop->key()->AsLiteral();
1418 __ mov(r2, Operand(key->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001419 // Call load IC. It has arguments receiver and property name r0 and r2.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001420 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001421 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001422}
1423
1424
1425void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1426 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001427 // Call keyed load IC. It has arguments key and receiver in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001428 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001429 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001430}
1431
1432
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001433void FullCodeGenerator::EmitInlineSmiBinaryOp(Expression* expr,
1434 Token::Value op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001435 OverwriteMode mode,
1436 Expression* left,
1437 Expression* right,
1438 ConstantOperand constant) {
1439 ASSERT(constant == kNoConstants); // Only handled case.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001440 EmitBinaryOp(op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001441}
1442
1443
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001444void FullCodeGenerator::EmitBinaryOp(Token::Value op,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001445 OverwriteMode mode) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001446 __ pop(r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001447 GenericBinaryOpStub stub(op, mode, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001448 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001449 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001450}
1451
1452
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001453void FullCodeGenerator::EmitAssignment(Expression* expr) {
1454 // Invalid left-hand sides are rewritten to have a 'throw
1455 // ReferenceError' on the left-hand side.
1456 if (!expr->IsValidLeftHandSide()) {
1457 VisitForEffect(expr);
1458 return;
1459 }
1460
1461 // Left-hand side can only be a property, a global or a (parameter or local)
1462 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1463 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1464 LhsKind assign_type = VARIABLE;
1465 Property* prop = expr->AsProperty();
1466 if (prop != NULL) {
1467 assign_type = (prop->key()->IsPropertyName())
1468 ? NAMED_PROPERTY
1469 : KEYED_PROPERTY;
1470 }
1471
1472 switch (assign_type) {
1473 case VARIABLE: {
1474 Variable* var = expr->AsVariableProxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001475 EffectContext context(this);
1476 EmitVariableAssignment(var, Token::ASSIGN);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001477 break;
1478 }
1479 case NAMED_PROPERTY: {
1480 __ push(r0); // Preserve value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001481 VisitForAccumulatorValue(prop->obj());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001482 __ mov(r1, r0);
1483 __ pop(r0); // Restore value.
1484 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
1485 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001486 EmitCallIC(ic, RelocInfo::CODE_TARGET);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001487 break;
1488 }
1489 case KEYED_PROPERTY: {
1490 __ push(r0); // Preserve value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001491 VisitForStackValue(prop->obj());
1492 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001493 __ mov(r1, r0);
1494 __ pop(r2);
1495 __ pop(r0); // Restore value.
1496 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001497 EmitCallIC(ic, RelocInfo::CODE_TARGET);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001498 break;
1499 }
1500 }
1501}
1502
1503
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001504void FullCodeGenerator::EmitVariableAssignment(Variable* var,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001505 Token::Value op) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001506 // Left-hand sides that rewrite to explicit property accesses do not reach
1507 // here.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001508 ASSERT(var != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001509 ASSERT(var->is_global() || var->AsSlot() != NULL);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001510
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001511 if (var->is_global()) {
1512 ASSERT(!var->is_this());
1513 // Assignment to a global variable. Use inline caching for the
1514 // assignment. Right-hand-side value is passed in r0, variable name in
ager@chromium.org5c838252010-02-19 08:53:10 +00001515 // r2, and the global object in r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001516 __ mov(r2, Operand(var->name()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001517 __ ldr(r1, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001518 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001519 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001520
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001521 } else if (var->mode() != Variable::CONST || op == Token::INIT_CONST) {
1522 // Perform the assignment for non-const variables and for initialization
1523 // of const variables. Const assignments are simply skipped.
1524 Label done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001525 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001526 switch (slot->type()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001527 case Slot::PARAMETER:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001528 case Slot::LOCAL:
1529 if (op == Token::INIT_CONST) {
1530 // Detect const reinitialization by checking for the hole value.
1531 __ ldr(r1, MemOperand(fp, SlotOffset(slot)));
1532 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1533 __ cmp(r1, ip);
1534 __ b(ne, &done);
1535 }
1536 // Perform the assignment.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001537 __ str(result_register(), MemOperand(fp, SlotOffset(slot)));
1538 break;
1539
1540 case Slot::CONTEXT: {
1541 MemOperand target = EmitSlotSearch(slot, r1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001542 if (op == Token::INIT_CONST) {
1543 // Detect const reinitialization by checking for the hole value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001544 __ ldr(r2, target);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001545 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001546 __ cmp(r2, ip);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001547 __ b(ne, &done);
1548 }
1549 // Perform the assignment and issue the write barrier.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001550 __ str(result_register(), target);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001551 // RecordWrite may destroy all its register arguments.
1552 __ mov(r3, result_register());
1553 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001554 __ RecordWrite(r1, Operand(offset), r2, r3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001555 break;
1556 }
1557
1558 case Slot::LOOKUP:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001559 // Call the runtime for the assignment. The runtime will ignore
1560 // const reinitialization.
1561 __ push(r0); // Value.
1562 __ mov(r0, Operand(slot->var()->name()));
1563 __ Push(cp, r0); // Context and name.
1564 if (op == Token::INIT_CONST) {
1565 // The runtime will ignore const redeclaration.
1566 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1567 } else {
1568 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1569 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001570 break;
1571 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001572 __ bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001573 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001574
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001575 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001576}
1577
1578
1579void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1580 // Assignment to a property, using a named store IC.
1581 Property* prop = expr->target()->AsProperty();
1582 ASSERT(prop != NULL);
1583 ASSERT(prop->key()->AsLiteral() != NULL);
1584
1585 // If the assignment starts a block of assignments to the same object,
1586 // change to slow case to avoid the quadratic behavior of repeatedly
1587 // adding fast properties.
1588 if (expr->starts_initialization_block()) {
1589 __ push(result_register());
1590 __ ldr(ip, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1591 __ push(ip);
1592 __ CallRuntime(Runtime::kToSlowProperties, 1);
1593 __ pop(result_register());
1594 }
1595
1596 // Record source code position before IC call.
1597 SetSourcePosition(expr->position());
1598 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001599 // Load receiver to r1. Leave a copy in the stack if needed for turning the
1600 // receiver into fast case.
ager@chromium.org5c838252010-02-19 08:53:10 +00001601 if (expr->ends_initialization_block()) {
1602 __ ldr(r1, MemOperand(sp));
1603 } else {
1604 __ pop(r1);
1605 }
1606
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001607 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001608 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001609
1610 // If the assignment ends an initialization block, revert to fast case.
1611 if (expr->ends_initialization_block()) {
1612 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001613 // Receiver is under the result value.
1614 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001615 __ push(ip);
1616 __ CallRuntime(Runtime::kToFastProperties, 1);
1617 __ pop(r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001618 context()->DropAndPlug(1, r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001619 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001620 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001621 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001622}
1623
1624
1625void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1626 // Assignment to a property, using a keyed store IC.
1627
1628 // If the assignment starts a block of assignments to the same object,
1629 // change to slow case to avoid the quadratic behavior of repeatedly
1630 // adding fast properties.
1631 if (expr->starts_initialization_block()) {
1632 __ push(result_register());
1633 // Receiver is now under the key and value.
1634 __ ldr(ip, MemOperand(sp, 2 * kPointerSize));
1635 __ push(ip);
1636 __ CallRuntime(Runtime::kToSlowProperties, 1);
1637 __ pop(result_register());
1638 }
1639
1640 // Record source code position before IC call.
1641 SetSourcePosition(expr->position());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001642 __ pop(r1); // Key.
1643 // Load receiver to r2. Leave a copy in the stack if needed for turning the
1644 // receiver into fast case.
1645 if (expr->ends_initialization_block()) {
1646 __ ldr(r2, MemOperand(sp));
1647 } else {
1648 __ pop(r2);
1649 }
1650
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001651 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001652 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001653
1654 // If the assignment ends an initialization block, revert to fast case.
1655 if (expr->ends_initialization_block()) {
1656 __ push(r0); // Result of assignment, saved even if not needed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001657 // Receiver is under the result value.
1658 __ ldr(ip, MemOperand(sp, kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001659 __ push(ip);
1660 __ CallRuntime(Runtime::kToFastProperties, 1);
1661 __ pop(r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001662 context()->DropAndPlug(1, r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001663 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001664 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001665 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001666}
1667
1668
1669void FullCodeGenerator::VisitProperty(Property* expr) {
1670 Comment cmnt(masm_, "[ Property");
1671 Expression* key = expr->key();
1672
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001673 if (key->IsPropertyName()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001674 VisitForAccumulatorValue(expr->obj());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001675 EmitNamedPropertyLoad(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001676 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001677 VisitForStackValue(expr->obj());
1678 VisitForAccumulatorValue(expr->key());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001679 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001680 EmitKeyedPropertyLoad(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001681 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001682 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001683}
1684
1685void FullCodeGenerator::EmitCallWithIC(Call* expr,
ager@chromium.org5c838252010-02-19 08:53:10 +00001686 Handle<Object> name,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001687 RelocInfo::Mode mode) {
1688 // Code common for calls using the IC.
1689 ZoneList<Expression*>* args = expr->arguments();
1690 int arg_count = args->length();
1691 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001692 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001693 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001694 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001695 // Record source position for debugger.
1696 SetSourcePosition(expr->position());
1697 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001698 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1699 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001700 EmitCallIC(ic, mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001701 // Restore context register.
1702 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001703 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001704}
1705
1706
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001707void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
1708 Expression* key,
1709 RelocInfo::Mode mode) {
1710 // Code common for calls using the IC.
1711 ZoneList<Expression*>* args = expr->arguments();
1712 int arg_count = args->length();
1713 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001714 VisitForStackValue(args->at(i));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001715 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001716 VisitForAccumulatorValue(key);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001717 __ mov(r2, r0);
1718 // Record source position for debugger.
1719 SetSourcePosition(expr->position());
1720 // Call the IC initialization code.
1721 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1722 Handle<Code> ic = CodeGenerator::ComputeKeyedCallInitialize(arg_count,
1723 in_loop);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001724 EmitCallIC(ic, mode);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001725 // Restore context register.
1726 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001727 context()->Plug(r0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001728}
1729
1730
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001731void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1732 // Code common for calls using the call stub.
1733 ZoneList<Expression*>* args = expr->arguments();
1734 int arg_count = args->length();
1735 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001736 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001737 }
1738 // Record source position for debugger.
1739 SetSourcePosition(expr->position());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001740 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1741 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001742 __ CallStub(&stub);
1743 // Restore context register.
1744 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001745 context()->DropAndPlug(1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001746}
1747
1748
1749void FullCodeGenerator::VisitCall(Call* expr) {
1750 Comment cmnt(masm_, "[ Call");
1751 Expression* fun = expr->expression();
1752 Variable* var = fun->AsVariableProxy()->AsVariable();
1753
1754 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001755 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1756 // resolve the function we need to call and the receiver of the
1757 // call. Then we call the resolved function using the given
1758 // arguments.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001759 VisitForStackValue(fun);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001760 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1761 __ push(r2); // Reserved receiver slot.
1762
1763 // Push the arguments.
1764 ZoneList<Expression*>* args = expr->arguments();
1765 int arg_count = args->length();
1766 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001767 VisitForStackValue(args->at(i));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001768 }
1769
1770 // Push copy of the function - found below the arguments.
1771 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
1772 __ push(r1);
1773
1774 // Push copy of the first argument or undefined if it doesn't exist.
1775 if (arg_count > 0) {
1776 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1777 __ push(r1);
1778 } else {
1779 __ push(r2);
1780 }
1781
1782 // Push the receiver of the enclosing function and do runtime call.
1783 __ ldr(r1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1784 __ push(r1);
1785 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1786
1787 // The runtime call returns a pair of values in r0 (function) and
1788 // r1 (receiver). Touch up the stack with the right values.
1789 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1790 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
1791
1792 // Record source position for debugger.
1793 SetSourcePosition(expr->position());
1794 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1795 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1796 __ CallStub(&stub);
1797 // Restore context register.
1798 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001799 context()->DropAndPlug(1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001800 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001801 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001802 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001803 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001804 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001805 } else if (var != NULL && var->AsSlot() != NULL &&
1806 var->AsSlot()->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001807 // Call to a lookup slot (dynamically introduced variable).
1808 Label slow, done;
1809
1810 // Generate code for loading from variables potentially shadowed
1811 // by eval-introduced variables.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001812 EmitDynamicLoadFromSlotFastCase(var->AsSlot(),
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001813 NOT_INSIDE_TYPEOF,
1814 &slow,
1815 &done);
1816
1817 __ bind(&slow);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001818 // Call the runtime to find the function to call (returned in r0)
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001819 // and the object holding it (returned in edx).
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001820 __ push(context_register());
1821 __ mov(r2, Operand(var->name()));
1822 __ push(r2);
1823 __ CallRuntime(Runtime::kLoadContextSlot, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001824 __ Push(r0, r1); // Function, receiver.
1825
1826 // If fast case code has been generated, emit code to push the
1827 // function and receiver and have the slow path jump around this
1828 // code.
1829 if (done.is_linked()) {
1830 Label call;
1831 __ b(&call);
1832 __ bind(&done);
1833 // Push function.
1834 __ push(r0);
1835 // Push global receiver.
1836 __ ldr(r1, CodeGenerator::GlobalObject());
1837 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1838 __ push(r1);
1839 __ bind(&call);
1840 }
1841
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001842 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001843 } else if (fun->AsProperty() != NULL) {
1844 // Call to an object property.
1845 Property* prop = fun->AsProperty();
1846 Literal* key = prop->key()->AsLiteral();
1847 if (key != NULL && key->handle()->IsSymbol()) {
1848 // Call to a named property, use call IC.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001849 VisitForStackValue(prop->obj());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001850 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1851 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001852 // Call to a keyed property.
1853 // For a synthetic property use keyed load IC followed by function call,
1854 // for a regular property use keyed CallIC.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001855 VisitForStackValue(prop->obj());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001856 if (prop->is_synthetic()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001857 VisitForAccumulatorValue(prop->key());
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001858 // Record source code position for IC call.
1859 SetSourcePosition(prop->position());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001860 __ pop(r1); // We do not need to keep the receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001861
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001862 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001863 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001864 __ ldr(r1, CodeGenerator::GlobalObject());
1865 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001866 __ Push(r0, r1); // Function, receiver.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001867 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001868 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001869 EmitKeyedCallWithIC(expr, prop->key(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001870 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001871 }
1872 } else {
1873 // Call to some other expression. If the expression is an anonymous
1874 // function literal not called in a loop, mark it as one that should
1875 // also use the fast code generator.
1876 FunctionLiteral* lit = fun->AsFunctionLiteral();
1877 if (lit != NULL &&
1878 lit->name()->Equals(Heap::empty_string()) &&
1879 loop_depth() == 0) {
1880 lit->set_try_full_codegen(true);
1881 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001882 VisitForStackValue(fun);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001883 // Load global receiver object.
1884 __ ldr(r1, CodeGenerator::GlobalObject());
1885 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1886 __ push(r1);
1887 // Emit function call.
1888 EmitCallWithStub(expr);
1889 }
1890}
1891
1892
1893void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1894 Comment cmnt(masm_, "[ CallNew");
1895 // According to ECMA-262, section 11.2.2, page 44, the function
1896 // expression in new calls must be evaluated before the
1897 // arguments.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001898
1899 // Push constructor on the stack. If it's not a function it's used as
1900 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
1901 // ignored.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001902 VisitForStackValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001903
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001904 // Push the arguments ("left-to-right") on the stack.
1905 ZoneList<Expression*>* args = expr->arguments();
1906 int arg_count = args->length();
1907 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001908 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001909 }
1910
1911 // Call the construct call builtin that handles allocation and
1912 // constructor invocation.
1913 SetSourcePosition(expr->position());
1914
ricow@chromium.org65fae842010-08-25 15:26:24 +00001915 // Load function and argument count into r1 and r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001916 __ mov(r0, Operand(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001917 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001918
1919 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1920 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001921 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001922}
1923
1924
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001925void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1926 ASSERT(args->length() == 1);
1927
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001928 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001929
1930 Label materialize_true, materialize_false;
1931 Label* if_true = NULL;
1932 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001933 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001934 context()->PrepareTest(&materialize_true, &materialize_false,
1935 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001936
1937 __ BranchOnSmi(r0, if_true);
1938 __ b(if_false);
1939
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001940 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001941}
1942
1943
1944void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1945 ASSERT(args->length() == 1);
1946
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001947 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001948
1949 Label materialize_true, materialize_false;
1950 Label* if_true = NULL;
1951 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001952 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001953 context()->PrepareTest(&materialize_true, &materialize_false,
1954 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001955
1956 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001957 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001958
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001959 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001960}
1961
1962
1963void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1964 ASSERT(args->length() == 1);
1965
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001966 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001967
1968 Label materialize_true, materialize_false;
1969 Label* if_true = NULL;
1970 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001971 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001972 context()->PrepareTest(&materialize_true, &materialize_false,
1973 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001974
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001975 __ BranchOnSmi(r0, if_false);
1976 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1977 __ cmp(r0, ip);
1978 __ b(eq, if_true);
1979 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1980 // Undetectable objects behave like undefined when tested with typeof.
1981 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
1982 __ tst(r1, Operand(1 << Map::kIsUndetectable));
1983 __ b(ne, if_false);
1984 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1985 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
1986 __ b(lt, if_false);
1987 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001988 Split(le, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001989
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001990 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001991}
1992
1993
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001994void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
1995 ASSERT(args->length() == 1);
1996
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001997 VisitForAccumulatorValue(args->at(0));
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001998
1999 Label materialize_true, materialize_false;
2000 Label* if_true = NULL;
2001 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002002 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002003 context()->PrepareTest(&materialize_true, &materialize_false,
2004 &if_true, &if_false, &fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002005
2006 __ BranchOnSmi(r0, if_false);
2007 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002008 Split(ge, if_true, if_false, fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002009
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002010 context()->Plug(if_true, if_false);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002011}
2012
2013
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002014void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
2015 ASSERT(args->length() == 1);
2016
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002017 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002018
2019 Label materialize_true, materialize_false;
2020 Label* if_true = NULL;
2021 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002022 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002023 context()->PrepareTest(&materialize_true, &materialize_false,
2024 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002025
2026 __ BranchOnSmi(r0, if_false);
2027 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2028 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
2029 __ tst(r1, Operand(1 << Map::kIsUndetectable));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002030 Split(ne, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002031
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002032 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002033}
2034
2035
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002036void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2037 ZoneList<Expression*>* args) {
2038
2039 ASSERT(args->length() == 1);
2040
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002041 VisitForAccumulatorValue(args->at(0));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002042
2043 Label materialize_true, materialize_false;
2044 Label* if_true = NULL;
2045 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002046 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002047 context()->PrepareTest(&materialize_true, &materialize_false,
2048 &if_true, &if_false, &fall_through);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002049
2050 // Just indicate false, as %_IsStringWrapperSafeForDefaultValueOf() is only
2051 // used in a few functions in runtime.js which should not normally be hit by
2052 // this compiler.
2053 __ jmp(if_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002054 context()->Plug(if_true, if_false);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002055}
2056
2057
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002058void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
2059 ASSERT(args->length() == 1);
2060
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002061 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002062
2063 Label materialize_true, materialize_false;
2064 Label* if_true = NULL;
2065 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002066 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002067 context()->PrepareTest(&materialize_true, &materialize_false,
2068 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002069
2070 __ BranchOnSmi(r0, if_false);
2071 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002072 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002073
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002074 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002075}
2076
2077
2078void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
2079 ASSERT(args->length() == 1);
2080
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002081 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002082
2083 Label materialize_true, materialize_false;
2084 Label* if_true = NULL;
2085 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002086 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002087 context()->PrepareTest(&materialize_true, &materialize_false,
2088 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002089
2090 __ BranchOnSmi(r0, if_false);
2091 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002092 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002093
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002094 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002095}
2096
2097
2098void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
2099 ASSERT(args->length() == 1);
2100
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002101 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002102
2103 Label materialize_true, materialize_false;
2104 Label* if_true = NULL;
2105 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002106 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002107 context()->PrepareTest(&materialize_true, &materialize_false,
2108 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002109
2110 __ BranchOnSmi(r0, if_false);
2111 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002112 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002113
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002114 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002115}
2116
2117
2118
2119void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
2120 ASSERT(args->length() == 0);
2121
2122 Label materialize_true, materialize_false;
2123 Label* if_true = NULL;
2124 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002125 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002126 context()->PrepareTest(&materialize_true, &materialize_false,
2127 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002128
2129 // Get the frame pointer for the calling frame.
2130 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2131
2132 // Skip the arguments adaptor frame if it exists.
2133 Label check_frame_marker;
2134 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
2135 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2136 __ b(ne, &check_frame_marker);
2137 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
2138
2139 // Check the marker in the calling frame.
2140 __ bind(&check_frame_marker);
2141 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
2142 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002143 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002144
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002145 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002146}
2147
2148
2149void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
2150 ASSERT(args->length() == 2);
2151
2152 // Load the two objects into registers and perform the comparison.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002153 VisitForStackValue(args->at(0));
2154 VisitForAccumulatorValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002155
2156 Label materialize_true, materialize_false;
2157 Label* if_true = NULL;
2158 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002159 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002160 context()->PrepareTest(&materialize_true, &materialize_false,
2161 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002162
2163 __ pop(r1);
2164 __ cmp(r0, r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002165 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002166
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002167 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002168}
2169
2170
2171void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
2172 ASSERT(args->length() == 1);
2173
2174 // ArgumentsAccessStub expects the key in edx and the formal
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002175 // parameter count in r0.
2176 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002177 __ mov(r1, r0);
2178 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2179 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2180 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002181 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002182}
2183
2184
2185void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
2186 ASSERT(args->length() == 0);
2187
2188 Label exit;
2189 // Get the number of formal parameters.
2190 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2191
2192 // Check if the calling frame is an arguments adaptor frame.
2193 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2194 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
2195 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2196 __ b(ne, &exit);
2197
2198 // Arguments adaptor case: Read the arguments length from the
2199 // adaptor frame.
2200 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2201
2202 __ bind(&exit);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002203 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002204}
2205
2206
2207void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
2208 ASSERT(args->length() == 1);
2209 Label done, null, function, non_function_constructor;
2210
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002211 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002212
2213 // If the object is a smi, we return null.
2214 __ BranchOnSmi(r0, &null);
2215
2216 // Check that the object is a JS object but take special care of JS
2217 // functions to make sure they have 'Function' as their class.
2218 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2219 __ b(lt, &null);
2220
2221 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2222 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2223 // LAST_JS_OBJECT_TYPE.
2224 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2225 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2226 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2227 __ b(eq, &function);
2228
2229 // Check if the constructor in the map is a function.
2230 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2231 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2232 __ b(ne, &non_function_constructor);
2233
2234 // r0 now contains the constructor function. Grab the
2235 // instance class name from there.
2236 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2237 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2238 __ b(&done);
2239
2240 // Functions have class 'Function'.
2241 __ bind(&function);
2242 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2243 __ jmp(&done);
2244
2245 // Objects with a non-function constructor have class 'Object'.
2246 __ bind(&non_function_constructor);
2247 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2248 __ jmp(&done);
2249
2250 // Non-JS objects have class null.
2251 __ bind(&null);
2252 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2253
2254 // All done.
2255 __ bind(&done);
2256
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002257 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002258}
2259
2260
2261void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2262 // Conditionally generate a log call.
2263 // Args:
2264 // 0 (literal string): The type of logging (corresponds to the flags).
2265 // This is used to determine whether or not to generate the log call.
2266 // 1 (string): Format string. Access the string at argument index 2
2267 // with '%2s' (see Logger::LogRuntime for all the formats).
2268 // 2 (array): Arguments to the format string.
2269 ASSERT_EQ(args->length(), 3);
2270#ifdef ENABLE_LOGGING_AND_PROFILING
2271 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002272 VisitForStackValue(args->at(1));
2273 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002274 __ CallRuntime(Runtime::kLog, 2);
2275 }
2276#endif
2277 // Finally, we're expected to leave a value on the top of the stack.
2278 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002279 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002280}
2281
2282
2283void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2284 ASSERT(args->length() == 0);
2285
2286 Label slow_allocate_heapnumber;
2287 Label heapnumber_allocated;
2288
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002289 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
2290 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002291 __ jmp(&heapnumber_allocated);
2292
2293 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002294 // Allocate a heap number.
2295 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002296 __ mov(r4, Operand(r0));
2297
2298 __ bind(&heapnumber_allocated);
2299
2300 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
2301 // by computing:
2302 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2303 if (CpuFeatures::IsSupported(VFP3)) {
2304 __ PrepareCallCFunction(0, r1);
2305 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
2306
2307 CpuFeatures::Scope scope(VFP3);
2308 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2309 // Create this constant using mov/orr to avoid PC relative load.
2310 __ mov(r1, Operand(0x41000000));
2311 __ orr(r1, r1, Operand(0x300000));
2312 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
2313 __ vmov(d7, r0, r1);
2314 // Move 0x4130000000000000 to VFP.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00002315 __ mov(r0, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002316 __ vmov(d8, r0, r1);
2317 // Subtract and store the result in the heap number.
2318 __ vsub(d7, d7, d8);
2319 __ sub(r0, r4, Operand(kHeapObjectTag));
2320 __ vstr(d7, r0, HeapNumber::kValueOffset);
2321 __ mov(r0, r4);
2322 } else {
2323 __ mov(r0, Operand(r4));
2324 __ PrepareCallCFunction(1, r1);
2325 __ CallCFunction(
2326 ExternalReference::fill_heap_number_with_random_function(), 1);
2327 }
2328
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002329 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002330}
2331
2332
2333void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
2334 // Load the arguments on the stack and call the stub.
2335 SubStringStub stub;
2336 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002337 VisitForStackValue(args->at(0));
2338 VisitForStackValue(args->at(1));
2339 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002340 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002341 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002342}
2343
2344
2345void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
2346 // Load the arguments on the stack and call the stub.
2347 RegExpExecStub stub;
2348 ASSERT(args->length() == 4);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002349 VisitForStackValue(args->at(0));
2350 VisitForStackValue(args->at(1));
2351 VisitForStackValue(args->at(2));
2352 VisitForStackValue(args->at(3));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002353 __ CallStub(&stub);
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::EmitValueOf(ZoneList<Expression*>* args) {
2359 ASSERT(args->length() == 1);
2360
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002361 VisitForAccumulatorValue(args->at(0)); // Load the object.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002362
2363 Label done;
2364 // If the object is a smi return the object.
2365 __ BranchOnSmi(r0, &done);
2366 // If the object is not a value type, return the object.
2367 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
2368 __ b(ne, &done);
2369 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2370
2371 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002372 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002373}
2374
2375
2376void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
2377 // Load the arguments on the stack and call the runtime function.
2378 ASSERT(args->length() == 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002379 VisitForStackValue(args->at(0));
2380 VisitForStackValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002381 __ CallRuntime(Runtime::kMath_pow, 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002382 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002383}
2384
2385
2386void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
2387 ASSERT(args->length() == 2);
2388
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002389 VisitForStackValue(args->at(0)); // Load the object.
2390 VisitForAccumulatorValue(args->at(1)); // Load the value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002391 __ pop(r1); // r0 = value. r1 = object.
2392
2393 Label done;
2394 // If the object is a smi, return the value.
2395 __ BranchOnSmi(r1, &done);
2396
2397 // If the object is not a value type, return the value.
2398 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
2399 __ b(ne, &done);
2400
2401 // Store the value.
2402 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2403 // Update the write barrier. Save the value as it will be
2404 // overwritten by the write barrier code and is needed afterward.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002405 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002406
2407 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002408 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002409}
2410
2411
2412void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2413 ASSERT_EQ(args->length(), 1);
2414
2415 // Load the argument on the stack and call the stub.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002416 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002417
2418 NumberToStringStub stub;
2419 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002420 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002421}
2422
2423
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002424void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002425 ASSERT(args->length() == 1);
2426
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002427 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002428
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002429 Label done;
2430 StringCharFromCodeGenerator generator(r0, r1);
2431 generator.GenerateFast(masm_);
2432 __ jmp(&done);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002433
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002434 NopRuntimeCallHelper call_helper;
2435 generator.GenerateSlow(masm_, call_helper);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002436
2437 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002438 context()->Plug(r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002439}
2440
2441
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002442void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
2443 ASSERT(args->length() == 2);
2444
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002445 VisitForStackValue(args->at(0));
2446 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002447
2448 Register object = r1;
2449 Register index = r0;
2450 Register scratch = r2;
2451 Register result = r3;
2452
2453 __ pop(object);
2454
2455 Label need_conversion;
2456 Label index_out_of_range;
2457 Label done;
2458 StringCharCodeAtGenerator generator(object,
2459 index,
2460 scratch,
2461 result,
2462 &need_conversion,
2463 &need_conversion,
2464 &index_out_of_range,
2465 STRING_INDEX_IS_NUMBER);
2466 generator.GenerateFast(masm_);
2467 __ jmp(&done);
2468
2469 __ bind(&index_out_of_range);
2470 // When the index is out of range, the spec requires us to return
2471 // NaN.
2472 __ LoadRoot(result, Heap::kNanValueRootIndex);
2473 __ jmp(&done);
2474
2475 __ bind(&need_conversion);
2476 // Load the undefined value into the result register, which will
2477 // trigger conversion.
2478 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2479 __ jmp(&done);
2480
2481 NopRuntimeCallHelper call_helper;
2482 generator.GenerateSlow(masm_, call_helper);
2483
2484 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002485 context()->Plug(result);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002486}
2487
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002488
2489void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
2490 ASSERT(args->length() == 2);
2491
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002492 VisitForStackValue(args->at(0));
2493 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002494
2495 Register object = r1;
2496 Register index = r0;
2497 Register scratch1 = r2;
2498 Register scratch2 = r3;
2499 Register result = r0;
2500
2501 __ pop(object);
2502
2503 Label need_conversion;
2504 Label index_out_of_range;
2505 Label done;
2506 StringCharAtGenerator generator(object,
2507 index,
2508 scratch1,
2509 scratch2,
2510 result,
2511 &need_conversion,
2512 &need_conversion,
2513 &index_out_of_range,
2514 STRING_INDEX_IS_NUMBER);
2515 generator.GenerateFast(masm_);
2516 __ jmp(&done);
2517
2518 __ bind(&index_out_of_range);
2519 // When the index is out of range, the spec requires us to return
2520 // the empty string.
2521 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2522 __ jmp(&done);
2523
2524 __ bind(&need_conversion);
2525 // Move smi zero into the result register, which will trigger
2526 // conversion.
2527 __ mov(result, Operand(Smi::FromInt(0)));
2528 __ jmp(&done);
2529
2530 NopRuntimeCallHelper call_helper;
2531 generator.GenerateSlow(masm_, call_helper);
2532
2533 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002534 context()->Plug(result);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002535}
2536
2537
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002538void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2539 ASSERT_EQ(2, args->length());
2540
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002541 VisitForStackValue(args->at(0));
2542 VisitForStackValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002543
2544 StringAddStub stub(NO_STRING_ADD_FLAGS);
2545 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002546 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002547}
2548
2549
2550void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
2551 ASSERT_EQ(2, args->length());
2552
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002553 VisitForStackValue(args->at(0));
2554 VisitForStackValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002555
2556 StringCompareStub stub;
2557 __ CallStub(&stub);
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::EmitMathSin(ZoneList<Expression*>* args) {
2563 // Load the argument on the stack and call the runtime.
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_sin, 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::EmitMathCos(ZoneList<Expression*>* args) {
2572 // Load the argument on the stack and call the runtime.
2573 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002574 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002575 __ CallRuntime(Runtime::kMath_cos, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002576 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002577}
2578
2579
2580void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2581 // Load the argument on the stack and call the runtime function.
2582 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002583 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002584 __ CallRuntime(Runtime::kMath_sqrt, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002585 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002586}
2587
2588
2589void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2590 ASSERT(args->length() >= 2);
2591
2592 int arg_count = args->length() - 2; // For receiver and function.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002593 VisitForStackValue(args->at(0)); // Receiver.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002594 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002595 VisitForStackValue(args->at(i + 1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002596 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002597 VisitForAccumulatorValue(args->at(arg_count + 1)); // Function.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002598
2599 // InvokeFunction requires function in r1. Move it in there.
2600 if (!result_register().is(r1)) __ mov(r1, result_register());
2601 ParameterCount count(arg_count);
2602 __ InvokeFunction(r1, count, CALL_FUNCTION);
2603 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002604 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002605}
2606
2607
2608void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2609 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002610 VisitForStackValue(args->at(0));
2611 VisitForStackValue(args->at(1));
2612 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002613 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002614 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002615}
2616
2617
2618void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2619 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002620 VisitForStackValue(args->at(0));
2621 VisitForStackValue(args->at(1));
2622 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002623 __ CallRuntime(Runtime::kSwapElements, 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002624 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002625}
2626
2627
2628void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2629 ASSERT_EQ(2, args->length());
2630
2631 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2632 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2633
2634 Handle<FixedArray> jsfunction_result_caches(
2635 Top::global_context()->jsfunction_result_caches());
2636 if (jsfunction_result_caches->length() <= cache_id) {
2637 __ Abort("Attempt to use undefined cache.");
2638 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002639 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002640 return;
2641 }
2642
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002643 VisitForAccumulatorValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002644
2645 Register key = r0;
2646 Register cache = r1;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002647 __ ldr(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002648 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002649 __ ldr(cache, ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002650 __ ldr(cache,
2651 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2652
2653
2654 Label done, not_found;
2655 // tmp now holds finger offset as a smi.
2656 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2657 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2658 // r2 now holds finger offset as a smi.
2659 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2660 // r3 now points to the start of fixed array elements.
2661 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2662 // Note side effect of PreIndex: r3 now points to the key of the pair.
2663 __ cmp(key, r2);
2664 __ b(ne, &not_found);
2665
2666 __ ldr(r0, MemOperand(r3, kPointerSize));
2667 __ b(&done);
2668
2669 __ bind(&not_found);
2670 // Call runtime to perform the lookup.
2671 __ Push(cache, key);
2672 __ CallRuntime(Runtime::kGetFromCache, 2);
2673
2674 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002675 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002676}
2677
2678
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002679void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
2680 ASSERT_EQ(2, args->length());
2681
2682 Register right = r0;
2683 Register left = r1;
2684 Register tmp = r2;
2685 Register tmp2 = r3;
2686
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002687 VisitForStackValue(args->at(0));
2688 VisitForAccumulatorValue(args->at(1));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002689 __ pop(left);
2690
2691 Label done, fail, ok;
2692 __ cmp(left, Operand(right));
2693 __ b(eq, &ok);
2694 // Fail if either is a non-HeapObject.
2695 __ and_(tmp, left, Operand(right));
2696 __ tst(tmp, Operand(kSmiTagMask));
2697 __ b(eq, &fail);
2698 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
2699 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
2700 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
2701 __ b(ne, &fail);
2702 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
2703 __ cmp(tmp, Operand(tmp2));
2704 __ b(ne, &fail);
2705 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
2706 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
2707 __ cmp(tmp, tmp2);
2708 __ b(eq, &ok);
2709 __ bind(&fail);
2710 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
2711 __ jmp(&done);
2712 __ bind(&ok);
2713 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
2714 __ bind(&done);
2715
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002716 context()->Plug(r0);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002717}
2718
2719
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002720void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002721 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002722
2723 Label materialize_true, materialize_false;
2724 Label* if_true = NULL;
2725 Label* if_false = NULL;
2726 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002727 context()->PrepareTest(&materialize_true, &materialize_false,
2728 &if_true, &if_false, &fall_through);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002729
2730 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2731 __ tst(r0, Operand(String::kContainsCachedArrayIndexMask));
2732
2733 __ b(eq, if_true);
2734 __ b(if_false);
2735
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002736 context()->Plug(if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002737}
2738
2739
2740void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
2741 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002742 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002743 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2744 __ IndexFromHash(r0, r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002745 context()->Plug(r0);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002746}
2747
2748
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002749void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002750 Handle<String> name = expr->name();
2751 if (name->length() > 0 && name->Get(0) == '_') {
2752 Comment cmnt(masm_, "[ InlineRuntimeCall");
2753 EmitInlineRuntimeCall(expr);
2754 return;
2755 }
2756
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002757 Comment cmnt(masm_, "[ CallRuntime");
2758 ZoneList<Expression*>* args = expr->arguments();
2759
2760 if (expr->is_jsruntime()) {
2761 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002762 __ ldr(r0, CodeGenerator::GlobalObject());
2763 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002764 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002765 }
2766
2767 // Push the arguments ("left-to-right").
2768 int arg_count = args->length();
2769 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002770 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002771 }
2772
2773 if (expr->is_jsruntime()) {
2774 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002775 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002776 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2777 NOT_IN_LOOP);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002778 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002779 // Restore context register.
2780 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002781 } else {
2782 // Call the C runtime function.
2783 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002784 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002785 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002786}
2787
2788
2789void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2790 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002791 case Token::DELETE: {
2792 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2793 Property* prop = expr->expression()->AsProperty();
2794 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2795 if (prop == NULL && var == NULL) {
2796 // Result of deleting non-property, non-variable reference is true.
2797 // The subexpression may have side effects.
2798 VisitForEffect(expr->expression());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002799 context()->Plug(true);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002800 } else if (var != NULL &&
2801 !var->is_global() &&
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002802 var->AsSlot() != NULL &&
2803 var->AsSlot()->type() != Slot::LOOKUP) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002804 // Result of deleting non-global, non-dynamic variables is false.
2805 // The subexpression does not have side effects.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002806 context()->Plug(false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002807 } else {
2808 // Property or variable reference. Call the delete builtin with
2809 // object and property name as arguments.
2810 if (prop != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002811 VisitForStackValue(prop->obj());
2812 VisitForStackValue(prop->key());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002813 } else if (var->is_global()) {
2814 __ ldr(r1, CodeGenerator::GlobalObject());
2815 __ mov(r0, Operand(var->name()));
2816 __ Push(r1, r0);
2817 } else {
2818 // Non-global variable. Call the runtime to look up the context
2819 // where the variable was introduced.
2820 __ push(context_register());
2821 __ mov(r2, Operand(var->name()));
2822 __ push(r2);
2823 __ CallRuntime(Runtime::kLookupContext, 2);
2824 __ push(r0);
2825 __ mov(r2, Operand(var->name()));
2826 __ push(r2);
2827 }
2828 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002829 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002830 }
2831 break;
2832 }
2833
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002834 case Token::VOID: {
2835 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2836 VisitForEffect(expr->expression());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002837 context()->Plug(Heap::kUndefinedValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002838 break;
2839 }
2840
2841 case Token::NOT: {
2842 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002843 Label materialize_true, materialize_false;
2844 Label* if_true = NULL;
2845 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002846 Label* fall_through = NULL;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002847
2848 // Notice that the labels are swapped.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002849 context()->PrepareTest(&materialize_true, &materialize_false,
2850 &if_false, &if_true, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002851 VisitForControl(expr->expression(), if_true, if_false, fall_through);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002852 context()->Plug(if_false, if_true); // Labels swapped.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002853 break;
2854 }
2855
2856 case Token::TYPEOF: {
2857 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002858 { StackValueContext context(this);
2859 VisitForTypeofValue(expr->expression());
2860 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002861 __ CallRuntime(Runtime::kTypeof, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002862 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002863 break;
2864 }
2865
2866 case Token::ADD: {
2867 Comment cmt(masm_, "[ UnaryOperation (ADD)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002868 VisitForAccumulatorValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002869 Label no_conversion;
2870 __ tst(result_register(), Operand(kSmiTagMask));
2871 __ b(eq, &no_conversion);
2872 __ push(r0);
2873 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2874 __ bind(&no_conversion);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002875 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002876 break;
2877 }
2878
2879 case Token::SUB: {
2880 Comment cmt(masm_, "[ UnaryOperation (SUB)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002881 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002882 UnaryOverwriteMode overwrite =
2883 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002884 GenericUnaryOpStub stub(Token::SUB,
2885 overwrite,
2886 NO_UNARY_FLAGS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002887 // GenericUnaryOpStub expects the argument to be in the
2888 // accumulator register r0.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002889 VisitForAccumulatorValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002890 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002891 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002892 break;
2893 }
2894
2895 case Token::BIT_NOT: {
2896 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002897 // The generic unary operation stub expects the argument to be
2898 // in the accumulator register r0.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002899 VisitForAccumulatorValue(expr->expression());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002900 Label done;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002901 bool inline_smi_code = ShouldInlineSmiCase(expr->op());
2902 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002903 Label call_stub;
2904 __ BranchOnNotSmi(r0, &call_stub);
2905 __ mvn(r0, Operand(r0));
2906 // Bit-clear inverted smi-tag.
2907 __ bic(r0, r0, Operand(kSmiTagMask));
2908 __ b(&done);
2909 __ bind(&call_stub);
2910 }
2911 bool overwrite = expr->expression()->ResultOverwriteAllowed();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002912 UnaryOpFlags flags = inline_smi_code
2913 ? NO_UNARY_SMI_CODE_IN_STUB
2914 : NO_UNARY_FLAGS;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002915 UnaryOverwriteMode mode =
2916 overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002917 GenericUnaryOpStub stub(Token::BIT_NOT, mode, flags);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002918 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002919 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002920 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002921 break;
2922 }
2923
2924 default:
2925 UNREACHABLE();
2926 }
2927}
2928
2929
2930void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2931 Comment cmnt(masm_, "[ CountOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002932 SetSourcePosition(expr->position());
2933
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002934 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2935 // as the left-hand side.
2936 if (!expr->expression()->IsValidLeftHandSide()) {
2937 VisitForEffect(expr->expression());
2938 return;
2939 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002940
2941 // Expression can only be a property, a global or a (parameter or local)
2942 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2943 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2944 LhsKind assign_type = VARIABLE;
2945 Property* prop = expr->expression()->AsProperty();
2946 // In case of a property we use the uninitialized expression context
2947 // of the key to detect a named property.
2948 if (prop != NULL) {
2949 assign_type =
2950 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2951 }
2952
2953 // Evaluate expression and get value.
2954 if (assign_type == VARIABLE) {
2955 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002956 AccumulatorValueContext context(this);
2957 EmitVariableLoad(expr->expression()->AsVariableProxy()->var());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002958 } else {
2959 // Reserve space for result of postfix operation.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002960 if (expr->is_postfix() && !context()->IsEffect()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002961 __ mov(ip, Operand(Smi::FromInt(0)));
2962 __ push(ip);
2963 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002964 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002965 // Put the object both on the stack and in the accumulator.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002966 VisitForAccumulatorValue(prop->obj());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002967 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002968 EmitNamedPropertyLoad(prop);
2969 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002970 VisitForStackValue(prop->obj());
2971 VisitForAccumulatorValue(prop->key());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002972 __ ldr(r1, MemOperand(sp, 0));
2973 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002974 EmitKeyedPropertyLoad(prop);
2975 }
2976 }
2977
2978 // Call ToNumber only if operand is not a smi.
2979 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002980 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002981 __ push(r0);
2982 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2983 __ bind(&no_conversion);
2984
2985 // Save result for postfix expressions.
2986 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002987 if (!context()->IsEffect()) {
2988 // Save the result on the stack. If we have a named or keyed property
2989 // we store the result under the receiver that is currently on top
2990 // of the stack.
2991 switch (assign_type) {
2992 case VARIABLE:
2993 __ push(r0);
2994 break;
2995 case NAMED_PROPERTY:
2996 __ str(r0, MemOperand(sp, kPointerSize));
2997 break;
2998 case KEYED_PROPERTY:
2999 __ str(r0, MemOperand(sp, 2 * kPointerSize));
3000 break;
3001 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003002 }
3003 }
3004
3005
3006 // Inline smi case if we are in a loop.
3007 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003008 int count_value = expr->op() == Token::INC ? 1 : -1;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003009 if (ShouldInlineSmiCase(expr->op())) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003010 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003011 __ b(vs, &stub_call);
3012 // We could eliminate this smi check if we split the code at
3013 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003014 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003015 __ bind(&stub_call);
3016 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003017 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003018 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003019 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00003020 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003021 __ CallStub(&stub);
3022 __ bind(&done);
3023
3024 // Store the value returned in r0.
3025 switch (assign_type) {
3026 case VARIABLE:
3027 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003028 { EffectContext context(this);
3029 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3030 Token::ASSIGN);
3031 }
3032 // For all contexts except EffectConstant We have the result on
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003033 // top of the stack.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003034 if (!context()->IsEffect()) {
3035 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003036 }
3037 } else {
3038 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003039 Token::ASSIGN);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003040 }
3041 break;
3042 case NAMED_PROPERTY: {
3043 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003044 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003045 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003046 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003047 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003048 if (!context()->IsEffect()) {
3049 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003050 }
3051 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003052 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003053 }
3054 break;
3055 }
3056 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003057 __ pop(r1); // Key.
3058 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003059 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003060 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003061 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003062 if (!context()->IsEffect()) {
3063 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003064 }
3065 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003066 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003067 }
3068 break;
3069 }
3070 }
3071}
3072
3073
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003074void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
3075 ASSERT(!context()->IsEffect());
3076 ASSERT(!context()->IsTest());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003077 VariableProxy* proxy = expr->AsVariableProxy();
3078 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
3079 Comment cmnt(masm_, "Global variable");
3080 __ ldr(r0, CodeGenerator::GlobalObject());
3081 __ mov(r2, Operand(proxy->name()));
3082 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
3083 // Use a regular load, not a contextual load, to avoid a reference
3084 // error.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003085 EmitCallIC(ic, RelocInfo::CODE_TARGET);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003086 context()->Plug(r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003087 } else if (proxy != NULL &&
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003088 proxy->var()->AsSlot() != NULL &&
3089 proxy->var()->AsSlot()->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003090 Label done, slow;
3091
3092 // Generate code for loading from variables potentially shadowed
3093 // by eval-introduced variables.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003094 Slot* slot = proxy->var()->AsSlot();
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003095 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
3096
3097 __ bind(&slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003098 __ mov(r0, Operand(proxy->name()));
3099 __ Push(cp, r0);
3100 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003101 __ bind(&done);
3102
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003103 context()->Plug(r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003104 } else {
3105 // This expression cannot throw a reference error at the top level.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003106 Visit(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003107 }
3108}
3109
3110
ricow@chromium.org65fae842010-08-25 15:26:24 +00003111bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
3112 Expression* left,
3113 Expression* right,
3114 Label* if_true,
3115 Label* if_false,
3116 Label* fall_through) {
3117 if (op != Token::EQ && op != Token::EQ_STRICT) return false;
3118
3119 // Check for the pattern: typeof <expression> == <string literal>.
3120 Literal* right_literal = right->AsLiteral();
3121 if (right_literal == NULL) return false;
3122 Handle<Object> right_literal_value = right_literal->handle();
3123 if (!right_literal_value->IsString()) return false;
3124 UnaryOperation* left_unary = left->AsUnaryOperation();
3125 if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
3126 Handle<String> check = Handle<String>::cast(right_literal_value);
3127
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003128 { AccumulatorValueContext context(this);
3129 VisitForTypeofValue(left_unary->expression());
3130 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003131 if (check->Equals(Heap::number_symbol())) {
3132 __ tst(r0, Operand(kSmiTagMask));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003133 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003134 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3135 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
3136 __ cmp(r0, ip);
3137 Split(eq, if_true, if_false, fall_through);
3138 } else if (check->Equals(Heap::string_symbol())) {
3139 __ tst(r0, Operand(kSmiTagMask));
3140 __ b(eq, if_false);
3141 // Check for undetectable objects => false.
3142 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3143 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
3144 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3145 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3146 __ b(eq, if_false);
3147 __ ldrb(r1, FieldMemOperand(r0, Map::kInstanceTypeOffset));
3148 __ cmp(r1, Operand(FIRST_NONSTRING_TYPE));
3149 Split(lt, if_true, if_false, fall_through);
3150 } else if (check->Equals(Heap::boolean_symbol())) {
3151 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3152 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003153 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003154 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
3155 __ cmp(r0, ip);
3156 Split(eq, if_true, if_false, fall_through);
3157 } else if (check->Equals(Heap::undefined_symbol())) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003158 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003159 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003160 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003161 __ tst(r0, Operand(kSmiTagMask));
3162 __ b(eq, if_false);
3163 // Check for undetectable objects => true.
3164 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3165 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
3166 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3167 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3168 Split(eq, if_true, if_false, fall_through);
3169 } else if (check->Equals(Heap::function_symbol())) {
3170 __ tst(r0, Operand(kSmiTagMask));
3171 __ b(eq, if_false);
3172 __ CompareObjectType(r0, r1, r0, JS_FUNCTION_TYPE);
3173 __ b(eq, if_true);
3174 // Regular expressions => 'function' (they are callable).
3175 __ CompareInstanceType(r1, r0, JS_REGEXP_TYPE);
3176 Split(eq, if_true, if_false, fall_through);
3177 } else if (check->Equals(Heap::object_symbol())) {
3178 __ tst(r0, Operand(kSmiTagMask));
3179 __ b(eq, if_false);
3180 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3181 __ cmp(r0, ip);
3182 __ b(eq, if_true);
3183 // Regular expressions => 'function', not 'object'.
3184 __ CompareObjectType(r0, r1, r0, JS_REGEXP_TYPE);
3185 __ b(eq, if_false);
3186 // Check for undetectable objects => false.
3187 __ ldrb(r0, FieldMemOperand(r1, Map::kBitFieldOffset));
3188 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3189 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
3190 __ b(eq, if_false);
3191 // Check for JS objects => true.
3192 __ ldrb(r0, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3193 __ cmp(r0, Operand(FIRST_JS_OBJECT_TYPE));
3194 __ b(lt, if_false);
3195 __ cmp(r0, Operand(LAST_JS_OBJECT_TYPE));
3196 Split(le, if_true, if_false, fall_through);
3197 } else {
3198 if (if_false != fall_through) __ jmp(if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003199 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003200
3201 return true;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003202}
3203
3204
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003205void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
3206 Comment cmnt(masm_, "[ CompareOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00003207 SetSourcePosition(expr->position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003208
3209 // Always perform the comparison for its control flow. Pack the result
3210 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003211
3212 Label materialize_true, materialize_false;
3213 Label* if_true = NULL;
3214 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003215 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003216 context()->PrepareTest(&materialize_true, &materialize_false,
3217 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003218
3219 // First we try a fast inlined version of the compare when one of
3220 // the operands is a literal.
3221 Token::Value op = expr->op();
3222 Expression* left = expr->left();
3223 Expression* right = expr->right();
3224 if (TryLiteralCompare(op, left, right, if_true, if_false, fall_through)) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003225 context()->Plug(if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003226 return;
3227 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003228
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003229 VisitForStackValue(expr->left());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003230 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003231 case Token::IN:
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003232 VisitForStackValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003233 __ InvokeBuiltin(Builtins::IN, CALL_JS);
3234 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3235 __ cmp(r0, ip);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003236 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003237 break;
3238
3239 case Token::INSTANCEOF: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003240 VisitForStackValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003241 InstanceofStub stub;
3242 __ CallStub(&stub);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003243 // The stub returns 0 for true.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003244 __ tst(r0, r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003245 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003246 break;
3247 }
3248
3249 default: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003250 VisitForAccumulatorValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003251 Condition cc = eq;
3252 bool strict = false;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003253 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003254 case Token::EQ_STRICT:
3255 strict = true;
3256 // Fall through
ricow@chromium.org65fae842010-08-25 15:26:24 +00003257 case Token::EQ:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003258 cc = eq;
3259 __ pop(r1);
3260 break;
3261 case Token::LT:
3262 cc = lt;
3263 __ pop(r1);
3264 break;
3265 case Token::GT:
3266 // Reverse left and right sides to obtain ECMA-262 conversion order.
3267 cc = lt;
3268 __ mov(r1, result_register());
3269 __ pop(r0);
3270 break;
3271 case Token::LTE:
3272 // Reverse left and right sides to obtain ECMA-262 conversion order.
3273 cc = ge;
3274 __ mov(r1, result_register());
3275 __ pop(r0);
3276 break;
3277 case Token::GTE:
3278 cc = ge;
3279 __ pop(r1);
3280 break;
3281 case Token::IN:
3282 case Token::INSTANCEOF:
3283 default:
3284 UNREACHABLE();
3285 }
3286
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003287 bool inline_smi_code = ShouldInlineSmiCase(op);
3288 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003289 Label slow_case;
3290 __ orr(r2, r0, Operand(r1));
3291 __ BranchOnNotSmi(r2, &slow_case);
3292 __ cmp(r1, r0);
3293 Split(cc, if_true, if_false, NULL);
3294 __ bind(&slow_case);
3295 }
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003296 CompareFlags flags = inline_smi_code
3297 ? NO_SMI_COMPARE_IN_STUB
3298 : NO_COMPARE_FLAGS;
3299 CompareStub stub(cc, strict, flags, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003300 __ CallStub(&stub);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00003301 __ cmp(r0, Operand(0, RelocInfo::NONE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003302 Split(cc, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003303 }
3304 }
3305
3306 // Convert the result of the comparison into one expected for this
3307 // expression's context.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003308 context()->Plug(if_true, if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003309}
3310
3311
ricow@chromium.org65fae842010-08-25 15:26:24 +00003312void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
3313 Comment cmnt(masm_, "[ CompareToNull");
3314 Label materialize_true, materialize_false;
3315 Label* if_true = NULL;
3316 Label* if_false = NULL;
3317 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003318 context()->PrepareTest(&materialize_true, &materialize_false,
3319 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003320
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003321 VisitForAccumulatorValue(expr->expression());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003322 __ LoadRoot(r1, Heap::kNullValueRootIndex);
3323 __ cmp(r0, r1);
3324 if (expr->is_strict()) {
3325 Split(eq, if_true, if_false, fall_through);
3326 } else {
3327 __ b(eq, if_true);
3328 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
3329 __ cmp(r0, r1);
3330 __ b(eq, if_true);
3331 __ tst(r0, Operand(kSmiTagMask));
3332 __ b(eq, if_false);
3333 // It can be an undetectable object.
3334 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3335 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3336 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3337 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3338 Split(eq, if_true, if_false, fall_through);
3339 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003340 context()->Plug(if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003341}
3342
3343
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003344void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3345 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003346 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003347}
3348
3349
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003350Register FullCodeGenerator::result_register() {
3351 return r0;
3352}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003353
3354
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003355Register FullCodeGenerator::context_register() {
3356 return cp;
3357}
3358
3359
3360void FullCodeGenerator::EmitCallIC(Handle<Code> ic, RelocInfo::Mode mode) {
3361 ASSERT(mode == RelocInfo::CODE_TARGET ||
3362 mode == RelocInfo::CODE_TARGET_CONTEXT);
3363 __ Call(ic, mode);
3364}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003365
3366
3367void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3368 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3369 __ str(value, MemOperand(fp, frame_offset));
3370}
3371
3372
3373void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003374 __ ldr(dst, ContextOperand(cp, context_index));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003375}
3376
3377
3378// ----------------------------------------------------------------------------
3379// Non-local control flow support.
3380
3381void FullCodeGenerator::EnterFinallyBlock() {
3382 ASSERT(!result_register().is(r1));
3383 // Store result register while executing finally block.
3384 __ push(result_register());
3385 // Cook return address in link register to stack (smi encoded Code* delta)
3386 __ sub(r1, lr, Operand(masm_->CodeObject()));
3387 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3388 ASSERT_EQ(0, kSmiTag);
3389 __ add(r1, r1, Operand(r1)); // Convert to smi.
3390 __ push(r1);
3391}
3392
3393
3394void FullCodeGenerator::ExitFinallyBlock() {
3395 ASSERT(!result_register().is(r1));
3396 // Restore result register from stack.
3397 __ pop(r1);
3398 // Uncook return address and return.
3399 __ pop(result_register());
3400 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3401 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3402 __ add(pc, r1, Operand(masm_->CodeObject()));
3403}
3404
3405
3406#undef __
3407
3408} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003409
3410#endif // V8_TARGET_ARCH_ARM