blob: bf27d0c7fee1067745e97594ea8d1a2dcf5a5aa0 [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();
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001691 { PreserveStatementPositionScope scope(masm()->positions_recorder());
1692 for (int i = 0; i < arg_count; i++) {
1693 VisitForStackValue(args->at(i));
1694 }
1695 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001696 }
1697 // Record source position for debugger.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001698 SetSourcePosition(expr->position(), FORCED_POSITION);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001699 // Call the IC initialization code.
ager@chromium.org5c838252010-02-19 08:53:10 +00001700 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1701 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001702 EmitCallIC(ic, mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001703 // Restore context register.
1704 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001705 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001706}
1707
1708
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001709void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
1710 Expression* key,
1711 RelocInfo::Mode mode) {
1712 // Code common for calls using the IC.
1713 ZoneList<Expression*>* args = expr->arguments();
1714 int arg_count = args->length();
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001715 { PreserveStatementPositionScope scope(masm()->positions_recorder());
1716 for (int i = 0; i < arg_count; i++) {
1717 VisitForStackValue(args->at(i));
1718 }
1719 VisitForAccumulatorValue(key);
1720 __ mov(r2, r0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001721 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001722 // Record source position for debugger.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001723 SetSourcePosition(expr->position(), FORCED_POSITION);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001724 // Call the IC initialization code.
1725 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1726 Handle<Code> ic = CodeGenerator::ComputeKeyedCallInitialize(arg_count,
1727 in_loop);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001728 EmitCallIC(ic, mode);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001729 // Restore context register.
1730 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001731 context()->Plug(r0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001732}
1733
1734
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001735void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1736 // Code common for calls using the call stub.
1737 ZoneList<Expression*>* args = expr->arguments();
1738 int arg_count = args->length();
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001739 { PreserveStatementPositionScope scope(masm()->positions_recorder());
1740 for (int i = 0; i < arg_count; i++) {
1741 VisitForStackValue(args->at(i));
1742 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001743 }
1744 // Record source position for debugger.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001745 SetSourcePosition(expr->position(), FORCED_POSITION);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001746 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1747 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001748 __ CallStub(&stub);
1749 // Restore context register.
1750 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001751 context()->DropAndPlug(1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001752}
1753
1754
1755void FullCodeGenerator::VisitCall(Call* expr) {
1756 Comment cmnt(masm_, "[ Call");
1757 Expression* fun = expr->expression();
1758 Variable* var = fun->AsVariableProxy()->AsVariable();
1759
1760 if (var != NULL && var->is_possibly_eval()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001761 // In a call to eval, we first call %ResolvePossiblyDirectEval to
1762 // resolve the function we need to call and the receiver of the
1763 // call. Then we call the resolved function using the given
1764 // arguments.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001765 ZoneList<Expression*>* args = expr->arguments();
1766 int arg_count = args->length();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001767
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001768 { PreserveStatementPositionScope pos_scope(masm()->positions_recorder());
1769 VisitForStackValue(fun);
1770 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1771 __ push(r2); // Reserved receiver slot.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001772
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001773 // Push the arguments.
1774 for (int i = 0; i < arg_count; i++) {
1775 VisitForStackValue(args->at(i));
1776 }
1777
1778 // Push copy of the function - found below the arguments.
1779 __ ldr(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001780 __ push(r1);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001781
1782 // Push copy of the first argument or undefined if it doesn't exist.
1783 if (arg_count > 0) {
1784 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
1785 __ push(r1);
1786 } else {
1787 __ push(r2);
1788 }
1789
1790 // Push the receiver of the enclosing function and do runtime call.
1791 __ ldr(r1,
1792 MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
1793 __ push(r1);
1794 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
1795
1796 // The runtime call returns a pair of values in r0 (function) and
1797 // r1 (receiver). Touch up the stack with the right values.
1798 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
1799 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001800 }
1801
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001802 // Record source position for debugger.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001803 SetSourcePosition(expr->position(), FORCED_POSITION);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001804 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1805 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
1806 __ CallStub(&stub);
1807 // Restore context register.
1808 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001809 context()->DropAndPlug(1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001810 } else if (var != NULL && !var->is_this() && var->is_global()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001811 // Push global object as receiver for the call IC.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001812 __ ldr(r0, CodeGenerator::GlobalObject());
ager@chromium.org5c838252010-02-19 08:53:10 +00001813 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001814 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001815 } else if (var != NULL && var->AsSlot() != NULL &&
1816 var->AsSlot()->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001817 // Call to a lookup slot (dynamically introduced variable).
1818 Label slow, done;
1819
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001820 { PreserveStatementPositionScope scope(masm()->positions_recorder());
1821 // Generate code for loading from variables potentially shadowed
1822 // by eval-introduced variables.
1823 EmitDynamicLoadFromSlotFastCase(var->AsSlot(),
1824 NOT_INSIDE_TYPEOF,
1825 &slow,
1826 &done);
1827 }
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001828
1829 __ bind(&slow);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001830 // Call the runtime to find the function to call (returned in r0)
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001831 // and the object holding it (returned in edx).
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001832 __ push(context_register());
1833 __ mov(r2, Operand(var->name()));
1834 __ push(r2);
1835 __ CallRuntime(Runtime::kLoadContextSlot, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001836 __ Push(r0, r1); // Function, receiver.
1837
1838 // If fast case code has been generated, emit code to push the
1839 // function and receiver and have the slow path jump around this
1840 // code.
1841 if (done.is_linked()) {
1842 Label call;
1843 __ b(&call);
1844 __ bind(&done);
1845 // Push function.
1846 __ push(r0);
1847 // Push global receiver.
1848 __ ldr(r1, CodeGenerator::GlobalObject());
1849 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1850 __ push(r1);
1851 __ bind(&call);
1852 }
1853
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001854 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001855 } else if (fun->AsProperty() != NULL) {
1856 // Call to an object property.
1857 Property* prop = fun->AsProperty();
1858 Literal* key = prop->key()->AsLiteral();
1859 if (key != NULL && key->handle()->IsSymbol()) {
1860 // Call to a named property, use call IC.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001861 { PreserveStatementPositionScope scope(masm()->positions_recorder());
1862 VisitForStackValue(prop->obj());
1863 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001864 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1865 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001866 // Call to a keyed property.
1867 // For a synthetic property use keyed load IC followed by function call,
1868 // for a regular property use keyed CallIC.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001869 { PreserveStatementPositionScope scope(masm()->positions_recorder());
1870 VisitForStackValue(prop->obj());
1871 }
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001872 if (prop->is_synthetic()) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001873 { PreserveStatementPositionScope scope(masm()->positions_recorder());
1874 VisitForAccumulatorValue(prop->key());
1875 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001876 // Record source code position for IC call.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001877 SetSourcePosition(prop->position(), FORCED_POSITION);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001878 __ pop(r1); // We do not need to keep the receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001879
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001880 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001881 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001882 __ ldr(r1, CodeGenerator::GlobalObject());
1883 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001884 __ Push(r0, r1); // Function, receiver.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001885 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001886 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001887 EmitKeyedCallWithIC(expr, prop->key(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001888 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001889 }
1890 } else {
1891 // Call to some other expression. If the expression is an anonymous
1892 // function literal not called in a loop, mark it as one that should
1893 // also use the fast code generator.
1894 FunctionLiteral* lit = fun->AsFunctionLiteral();
1895 if (lit != NULL &&
1896 lit->name()->Equals(Heap::empty_string()) &&
1897 loop_depth() == 0) {
1898 lit->set_try_full_codegen(true);
1899 }
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001900
1901 { PreserveStatementPositionScope scope(masm()->positions_recorder());
1902 VisitForStackValue(fun);
1903 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001904 // Load global receiver object.
1905 __ ldr(r1, CodeGenerator::GlobalObject());
1906 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
1907 __ push(r1);
1908 // Emit function call.
1909 EmitCallWithStub(expr);
1910 }
1911}
1912
1913
1914void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1915 Comment cmnt(masm_, "[ CallNew");
1916 // According to ECMA-262, section 11.2.2, page 44, the function
1917 // expression in new calls must be evaluated before the
1918 // arguments.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001919
1920 // Push constructor on the stack. If it's not a function it's used as
1921 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
1922 // ignored.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001923 VisitForStackValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001924
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001925 // Push the arguments ("left-to-right") on the stack.
1926 ZoneList<Expression*>* args = expr->arguments();
1927 int arg_count = args->length();
1928 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001929 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001930 }
1931
1932 // Call the construct call builtin that handles allocation and
1933 // constructor invocation.
1934 SetSourcePosition(expr->position());
1935
ricow@chromium.org65fae842010-08-25 15:26:24 +00001936 // Load function and argument count into r1 and r0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001937 __ mov(r0, Operand(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001938 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001939
1940 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1941 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001942 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001943}
1944
1945
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001946void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
1947 ASSERT(args->length() == 1);
1948
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001949 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001950
1951 Label materialize_true, materialize_false;
1952 Label* if_true = NULL;
1953 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001954 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001955 context()->PrepareTest(&materialize_true, &materialize_false,
1956 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001957
1958 __ BranchOnSmi(r0, if_true);
1959 __ b(if_false);
1960
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001961 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001962}
1963
1964
1965void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
1966 ASSERT(args->length() == 1);
1967
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001968 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001969
1970 Label materialize_true, materialize_false;
1971 Label* if_true = NULL;
1972 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001973 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001974 context()->PrepareTest(&materialize_true, &materialize_false,
1975 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001976
1977 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001978 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001979
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001980 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001981}
1982
1983
1984void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
1985 ASSERT(args->length() == 1);
1986
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001987 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001988
1989 Label materialize_true, materialize_false;
1990 Label* if_true = NULL;
1991 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001992 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001993 context()->PrepareTest(&materialize_true, &materialize_false,
1994 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001995
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001996 __ BranchOnSmi(r0, if_false);
1997 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1998 __ cmp(r0, ip);
1999 __ b(eq, if_true);
2000 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
2001 // Undetectable objects behave like undefined when tested with typeof.
2002 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
2003 __ tst(r1, Operand(1 << Map::kIsUndetectable));
2004 __ b(ne, if_false);
2005 __ ldrb(r1, FieldMemOperand(r2, Map::kInstanceTypeOffset));
2006 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
2007 __ b(lt, if_false);
2008 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002009 Split(le, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002010
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002011 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002012}
2013
2014
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002015void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
2016 ASSERT(args->length() == 1);
2017
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002018 VisitForAccumulatorValue(args->at(0));
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002019
2020 Label materialize_true, materialize_false;
2021 Label* if_true = NULL;
2022 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002023 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002024 context()->PrepareTest(&materialize_true, &materialize_false,
2025 &if_true, &if_false, &fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002026
2027 __ BranchOnSmi(r0, if_false);
2028 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002029 Split(ge, if_true, if_false, fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002030
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002031 context()->Plug(if_true, if_false);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002032}
2033
2034
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002035void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
2036 ASSERT(args->length() == 1);
2037
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002038 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002039
2040 Label materialize_true, materialize_false;
2041 Label* if_true = NULL;
2042 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002043 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002044 context()->PrepareTest(&materialize_true, &materialize_false,
2045 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002046
2047 __ BranchOnSmi(r0, if_false);
2048 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2049 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
2050 __ tst(r1, Operand(1 << Map::kIsUndetectable));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002051 Split(ne, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002052
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002053 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002054}
2055
2056
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002057void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2058 ZoneList<Expression*>* args) {
2059
2060 ASSERT(args->length() == 1);
2061
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002062 VisitForAccumulatorValue(args->at(0));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002063
2064 Label materialize_true, materialize_false;
2065 Label* if_true = NULL;
2066 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002067 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002068 context()->PrepareTest(&materialize_true, &materialize_false,
2069 &if_true, &if_false, &fall_through);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002070
2071 // Just indicate false, as %_IsStringWrapperSafeForDefaultValueOf() is only
2072 // used in a few functions in runtime.js which should not normally be hit by
2073 // this compiler.
2074 __ jmp(if_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002075 context()->Plug(if_true, if_false);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002076}
2077
2078
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002079void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
2080 ASSERT(args->length() == 1);
2081
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002082 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002083
2084 Label materialize_true, materialize_false;
2085 Label* if_true = NULL;
2086 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002087 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002088 context()->PrepareTest(&materialize_true, &materialize_false,
2089 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002090
2091 __ BranchOnSmi(r0, if_false);
2092 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002093 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002094
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002095 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002096}
2097
2098
2099void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
2100 ASSERT(args->length() == 1);
2101
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002102 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002103
2104 Label materialize_true, materialize_false;
2105 Label* if_true = NULL;
2106 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002107 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002108 context()->PrepareTest(&materialize_true, &materialize_false,
2109 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002110
2111 __ BranchOnSmi(r0, if_false);
2112 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002113 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002114
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002115 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002116}
2117
2118
2119void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
2120 ASSERT(args->length() == 1);
2121
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002122 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002123
2124 Label materialize_true, materialize_false;
2125 Label* if_true = NULL;
2126 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002127 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002128 context()->PrepareTest(&materialize_true, &materialize_false,
2129 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002130
2131 __ BranchOnSmi(r0, if_false);
2132 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002133 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002134
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002135 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002136}
2137
2138
2139
2140void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
2141 ASSERT(args->length() == 0);
2142
2143 Label materialize_true, materialize_false;
2144 Label* if_true = NULL;
2145 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002146 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002147 context()->PrepareTest(&materialize_true, &materialize_false,
2148 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002149
2150 // Get the frame pointer for the calling frame.
2151 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2152
2153 // Skip the arguments adaptor frame if it exists.
2154 Label check_frame_marker;
2155 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
2156 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2157 __ b(ne, &check_frame_marker);
2158 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
2159
2160 // Check the marker in the calling frame.
2161 __ bind(&check_frame_marker);
2162 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
2163 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002164 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002165
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002166 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002167}
2168
2169
2170void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
2171 ASSERT(args->length() == 2);
2172
2173 // Load the two objects into registers and perform the comparison.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002174 VisitForStackValue(args->at(0));
2175 VisitForAccumulatorValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002176
2177 Label materialize_true, materialize_false;
2178 Label* if_true = NULL;
2179 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002180 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002181 context()->PrepareTest(&materialize_true, &materialize_false,
2182 &if_true, &if_false, &fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002183
2184 __ pop(r1);
2185 __ cmp(r0, r1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002186 Split(eq, if_true, if_false, fall_through);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002187
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002188 context()->Plug(if_true, if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002189}
2190
2191
2192void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
2193 ASSERT(args->length() == 1);
2194
2195 // ArgumentsAccessStub expects the key in edx and the formal
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002196 // parameter count in r0.
2197 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002198 __ mov(r1, r0);
2199 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2200 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2201 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002202 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002203}
2204
2205
2206void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
2207 ASSERT(args->length() == 0);
2208
2209 Label exit;
2210 // Get the number of formal parameters.
2211 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
2212
2213 // Check if the calling frame is an arguments adaptor frame.
2214 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2215 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
2216 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2217 __ b(ne, &exit);
2218
2219 // Arguments adaptor case: Read the arguments length from the
2220 // adaptor frame.
2221 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2222
2223 __ bind(&exit);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002224 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002225}
2226
2227
2228void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
2229 ASSERT(args->length() == 1);
2230 Label done, null, function, non_function_constructor;
2231
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002232 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002233
2234 // If the object is a smi, we return null.
2235 __ BranchOnSmi(r0, &null);
2236
2237 // Check that the object is a JS object but take special care of JS
2238 // functions to make sure they have 'Function' as their class.
2239 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); // Map is now in r0.
2240 __ b(lt, &null);
2241
2242 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2243 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2244 // LAST_JS_OBJECT_TYPE.
2245 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2246 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2247 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
2248 __ b(eq, &function);
2249
2250 // Check if the constructor in the map is a function.
2251 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
2252 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
2253 __ b(ne, &non_function_constructor);
2254
2255 // r0 now contains the constructor function. Grab the
2256 // instance class name from there.
2257 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
2258 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
2259 __ b(&done);
2260
2261 // Functions have class 'Function'.
2262 __ bind(&function);
2263 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2264 __ jmp(&done);
2265
2266 // Objects with a non-function constructor have class 'Object'.
2267 __ bind(&non_function_constructor);
2268 __ LoadRoot(r0, Heap::kfunction_class_symbolRootIndex);
2269 __ jmp(&done);
2270
2271 // Non-JS objects have class null.
2272 __ bind(&null);
2273 __ LoadRoot(r0, Heap::kNullValueRootIndex);
2274
2275 // All done.
2276 __ bind(&done);
2277
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002278 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002279}
2280
2281
2282void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
2283 // Conditionally generate a log call.
2284 // Args:
2285 // 0 (literal string): The type of logging (corresponds to the flags).
2286 // This is used to determine whether or not to generate the log call.
2287 // 1 (string): Format string. Access the string at argument index 2
2288 // with '%2s' (see Logger::LogRuntime for all the formats).
2289 // 2 (array): Arguments to the format string.
2290 ASSERT_EQ(args->length(), 3);
2291#ifdef ENABLE_LOGGING_AND_PROFILING
2292 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002293 VisitForStackValue(args->at(1));
2294 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002295 __ CallRuntime(Runtime::kLog, 2);
2296 }
2297#endif
2298 // Finally, we're expected to leave a value on the top of the stack.
2299 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002300 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002301}
2302
2303
2304void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
2305 ASSERT(args->length() == 0);
2306
2307 Label slow_allocate_heapnumber;
2308 Label heapnumber_allocated;
2309
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002310 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
2311 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002312 __ jmp(&heapnumber_allocated);
2313
2314 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002315 // Allocate a heap number.
2316 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002317 __ mov(r4, Operand(r0));
2318
2319 __ bind(&heapnumber_allocated);
2320
2321 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
2322 // by computing:
2323 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2324 if (CpuFeatures::IsSupported(VFP3)) {
2325 __ PrepareCallCFunction(0, r1);
2326 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
2327
2328 CpuFeatures::Scope scope(VFP3);
2329 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2330 // Create this constant using mov/orr to avoid PC relative load.
2331 __ mov(r1, Operand(0x41000000));
2332 __ orr(r1, r1, Operand(0x300000));
2333 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
2334 __ vmov(d7, r0, r1);
2335 // Move 0x4130000000000000 to VFP.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00002336 __ mov(r0, Operand(0, RelocInfo::NONE));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002337 __ vmov(d8, r0, r1);
2338 // Subtract and store the result in the heap number.
2339 __ vsub(d7, d7, d8);
2340 __ sub(r0, r4, Operand(kHeapObjectTag));
2341 __ vstr(d7, r0, HeapNumber::kValueOffset);
2342 __ mov(r0, r4);
2343 } else {
2344 __ mov(r0, Operand(r4));
2345 __ PrepareCallCFunction(1, r1);
2346 __ CallCFunction(
2347 ExternalReference::fill_heap_number_with_random_function(), 1);
2348 }
2349
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002350 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002351}
2352
2353
2354void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
2355 // Load the arguments on the stack and call the stub.
2356 SubStringStub stub;
2357 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002358 VisitForStackValue(args->at(0));
2359 VisitForStackValue(args->at(1));
2360 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002361 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002362 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002363}
2364
2365
2366void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
2367 // Load the arguments on the stack and call the stub.
2368 RegExpExecStub stub;
2369 ASSERT(args->length() == 4);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002370 VisitForStackValue(args->at(0));
2371 VisitForStackValue(args->at(1));
2372 VisitForStackValue(args->at(2));
2373 VisitForStackValue(args->at(3));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002374 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002375 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002376}
2377
2378
2379void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
2380 ASSERT(args->length() == 1);
2381
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002382 VisitForAccumulatorValue(args->at(0)); // Load the object.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002383
2384 Label done;
2385 // If the object is a smi return the object.
2386 __ BranchOnSmi(r0, &done);
2387 // If the object is not a value type, return the object.
2388 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
2389 __ b(ne, &done);
2390 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2391
2392 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002393 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002394}
2395
2396
2397void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
2398 // Load the arguments on the stack and call the runtime function.
2399 ASSERT(args->length() == 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002400 VisitForStackValue(args->at(0));
2401 VisitForStackValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002402 __ CallRuntime(Runtime::kMath_pow, 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002403 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002404}
2405
2406
2407void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
2408 ASSERT(args->length() == 2);
2409
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002410 VisitForStackValue(args->at(0)); // Load the object.
2411 VisitForAccumulatorValue(args->at(1)); // Load the value.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002412 __ pop(r1); // r0 = value. r1 = object.
2413
2414 Label done;
2415 // If the object is a smi, return the value.
2416 __ BranchOnSmi(r1, &done);
2417
2418 // If the object is not a value type, return the value.
2419 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
2420 __ b(ne, &done);
2421
2422 // Store the value.
2423 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2424 // Update the write barrier. Save the value as it will be
2425 // overwritten by the write barrier code and is needed afterward.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002426 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002427
2428 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002429 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002430}
2431
2432
2433void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
2434 ASSERT_EQ(args->length(), 1);
2435
2436 // Load the argument on the stack and call the stub.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002437 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002438
2439 NumberToStringStub stub;
2440 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002441 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002442}
2443
2444
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002445void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002446 ASSERT(args->length() == 1);
2447
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002448 VisitForAccumulatorValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002449
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002450 Label done;
2451 StringCharFromCodeGenerator generator(r0, r1);
2452 generator.GenerateFast(masm_);
2453 __ jmp(&done);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002454
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002455 NopRuntimeCallHelper call_helper;
2456 generator.GenerateSlow(masm_, call_helper);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002457
2458 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002459 context()->Plug(r1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002460}
2461
2462
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002463void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
2464 ASSERT(args->length() == 2);
2465
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002466 VisitForStackValue(args->at(0));
2467 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002468
2469 Register object = r1;
2470 Register index = r0;
2471 Register scratch = r2;
2472 Register result = r3;
2473
2474 __ pop(object);
2475
2476 Label need_conversion;
2477 Label index_out_of_range;
2478 Label done;
2479 StringCharCodeAtGenerator generator(object,
2480 index,
2481 scratch,
2482 result,
2483 &need_conversion,
2484 &need_conversion,
2485 &index_out_of_range,
2486 STRING_INDEX_IS_NUMBER);
2487 generator.GenerateFast(masm_);
2488 __ jmp(&done);
2489
2490 __ bind(&index_out_of_range);
2491 // When the index is out of range, the spec requires us to return
2492 // NaN.
2493 __ LoadRoot(result, Heap::kNanValueRootIndex);
2494 __ jmp(&done);
2495
2496 __ bind(&need_conversion);
2497 // Load the undefined value into the result register, which will
2498 // trigger conversion.
2499 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2500 __ jmp(&done);
2501
2502 NopRuntimeCallHelper call_helper;
2503 generator.GenerateSlow(masm_, call_helper);
2504
2505 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002506 context()->Plug(result);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002507}
2508
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002509
2510void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
2511 ASSERT(args->length() == 2);
2512
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002513 VisitForStackValue(args->at(0));
2514 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002515
2516 Register object = r1;
2517 Register index = r0;
2518 Register scratch1 = r2;
2519 Register scratch2 = r3;
2520 Register result = r0;
2521
2522 __ pop(object);
2523
2524 Label need_conversion;
2525 Label index_out_of_range;
2526 Label done;
2527 StringCharAtGenerator generator(object,
2528 index,
2529 scratch1,
2530 scratch2,
2531 result,
2532 &need_conversion,
2533 &need_conversion,
2534 &index_out_of_range,
2535 STRING_INDEX_IS_NUMBER);
2536 generator.GenerateFast(masm_);
2537 __ jmp(&done);
2538
2539 __ bind(&index_out_of_range);
2540 // When the index is out of range, the spec requires us to return
2541 // the empty string.
2542 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2543 __ jmp(&done);
2544
2545 __ bind(&need_conversion);
2546 // Move smi zero into the result register, which will trigger
2547 // conversion.
2548 __ mov(result, Operand(Smi::FromInt(0)));
2549 __ jmp(&done);
2550
2551 NopRuntimeCallHelper call_helper;
2552 generator.GenerateSlow(masm_, call_helper);
2553
2554 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002555 context()->Plug(result);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002556}
2557
2558
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002559void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
2560 ASSERT_EQ(2, args->length());
2561
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002562 VisitForStackValue(args->at(0));
2563 VisitForStackValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002564
2565 StringAddStub stub(NO_STRING_ADD_FLAGS);
2566 __ CallStub(&stub);
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::EmitStringCompare(ZoneList<Expression*>* args) {
2572 ASSERT_EQ(2, args->length());
2573
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002574 VisitForStackValue(args->at(0));
2575 VisitForStackValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002576
2577 StringCompareStub stub;
2578 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002579 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002580}
2581
2582
2583void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
2584 // Load the argument on the stack and call the runtime.
2585 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002586 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002587 __ CallRuntime(Runtime::kMath_sin, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002588 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002589}
2590
2591
2592void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
2593 // Load the argument on the stack and call the runtime.
2594 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002595 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002596 __ CallRuntime(Runtime::kMath_cos, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002597 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002598}
2599
2600
2601void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
2602 // Load the argument on the stack and call the runtime function.
2603 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002604 VisitForStackValue(args->at(0));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002605 __ CallRuntime(Runtime::kMath_sqrt, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002606 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002607}
2608
2609
2610void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
2611 ASSERT(args->length() >= 2);
2612
2613 int arg_count = args->length() - 2; // For receiver and function.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002614 VisitForStackValue(args->at(0)); // Receiver.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002615 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002616 VisitForStackValue(args->at(i + 1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002617 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002618 VisitForAccumulatorValue(args->at(arg_count + 1)); // Function.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002619
2620 // InvokeFunction requires function in r1. Move it in there.
2621 if (!result_register().is(r1)) __ mov(r1, result_register());
2622 ParameterCount count(arg_count);
2623 __ InvokeFunction(r1, count, CALL_FUNCTION);
2624 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002625 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002626}
2627
2628
2629void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
2630 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002631 VisitForStackValue(args->at(0));
2632 VisitForStackValue(args->at(1));
2633 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002634 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002635 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002636}
2637
2638
2639void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
2640 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002641 VisitForStackValue(args->at(0));
2642 VisitForStackValue(args->at(1));
2643 VisitForStackValue(args->at(2));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002644 __ CallRuntime(Runtime::kSwapElements, 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002645 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002646}
2647
2648
2649void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
2650 ASSERT_EQ(2, args->length());
2651
2652 ASSERT_NE(NULL, args->at(0)->AsLiteral());
2653 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
2654
2655 Handle<FixedArray> jsfunction_result_caches(
2656 Top::global_context()->jsfunction_result_caches());
2657 if (jsfunction_result_caches->length() <= cache_id) {
2658 __ Abort("Attempt to use undefined cache.");
2659 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002660 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002661 return;
2662 }
2663
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002664 VisitForAccumulatorValue(args->at(1));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002665
2666 Register key = r0;
2667 Register cache = r1;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002668 __ ldr(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002669 __ ldr(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002670 __ ldr(cache, ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002671 __ ldr(cache,
2672 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
2673
2674
2675 Label done, not_found;
2676 // tmp now holds finger offset as a smi.
2677 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
2678 __ ldr(r2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
2679 // r2 now holds finger offset as a smi.
2680 __ add(r3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2681 // r3 now points to the start of fixed array elements.
2682 __ ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
2683 // Note side effect of PreIndex: r3 now points to the key of the pair.
2684 __ cmp(key, r2);
2685 __ b(ne, &not_found);
2686
2687 __ ldr(r0, MemOperand(r3, kPointerSize));
2688 __ b(&done);
2689
2690 __ bind(&not_found);
2691 // Call runtime to perform the lookup.
2692 __ Push(cache, key);
2693 __ CallRuntime(Runtime::kGetFromCache, 2);
2694
2695 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002696 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002697}
2698
2699
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002700void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
2701 ASSERT_EQ(2, args->length());
2702
2703 Register right = r0;
2704 Register left = r1;
2705 Register tmp = r2;
2706 Register tmp2 = r3;
2707
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002708 VisitForStackValue(args->at(0));
2709 VisitForAccumulatorValue(args->at(1));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002710 __ pop(left);
2711
2712 Label done, fail, ok;
2713 __ cmp(left, Operand(right));
2714 __ b(eq, &ok);
2715 // Fail if either is a non-HeapObject.
2716 __ and_(tmp, left, Operand(right));
2717 __ tst(tmp, Operand(kSmiTagMask));
2718 __ b(eq, &fail);
2719 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
2720 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
2721 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
2722 __ b(ne, &fail);
2723 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
2724 __ cmp(tmp, Operand(tmp2));
2725 __ b(ne, &fail);
2726 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
2727 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
2728 __ cmp(tmp, tmp2);
2729 __ b(eq, &ok);
2730 __ bind(&fail);
2731 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
2732 __ jmp(&done);
2733 __ bind(&ok);
2734 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
2735 __ bind(&done);
2736
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002737 context()->Plug(r0);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002738}
2739
2740
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002741void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002742 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002743
2744 Label materialize_true, materialize_false;
2745 Label* if_true = NULL;
2746 Label* if_false = NULL;
2747 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002748 context()->PrepareTest(&materialize_true, &materialize_false,
2749 &if_true, &if_false, &fall_through);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002750
2751 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2752 __ tst(r0, Operand(String::kContainsCachedArrayIndexMask));
2753
2754 __ b(eq, if_true);
2755 __ b(if_false);
2756
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002757 context()->Plug(if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002758}
2759
2760
2761void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
2762 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002763 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002764 __ ldr(r0, FieldMemOperand(r0, String::kHashFieldOffset));
2765 __ IndexFromHash(r0, r0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002766 context()->Plug(r0);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002767}
2768
2769
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002770void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002771 Handle<String> name = expr->name();
2772 if (name->length() > 0 && name->Get(0) == '_') {
2773 Comment cmnt(masm_, "[ InlineRuntimeCall");
2774 EmitInlineRuntimeCall(expr);
2775 return;
2776 }
2777
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002778 Comment cmnt(masm_, "[ CallRuntime");
2779 ZoneList<Expression*>* args = expr->arguments();
2780
2781 if (expr->is_jsruntime()) {
2782 // Prepare for calling JS runtime function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002783 __ ldr(r0, CodeGenerator::GlobalObject());
2784 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kBuiltinsOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00002785 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002786 }
2787
2788 // Push the arguments ("left-to-right").
2789 int arg_count = args->length();
2790 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002791 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002792 }
2793
2794 if (expr->is_jsruntime()) {
2795 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00002796 __ mov(r2, Operand(expr->name()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002797 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count,
2798 NOT_IN_LOOP);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002799 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002800 // Restore context register.
2801 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002802 } else {
2803 // Call the C runtime function.
2804 __ CallRuntime(expr->function(), arg_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002805 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002806 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002807}
2808
2809
2810void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
2811 switch (expr->op()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002812 case Token::DELETE: {
2813 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
2814 Property* prop = expr->expression()->AsProperty();
2815 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
2816 if (prop == NULL && var == NULL) {
2817 // Result of deleting non-property, non-variable reference is true.
2818 // The subexpression may have side effects.
2819 VisitForEffect(expr->expression());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002820 context()->Plug(true);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002821 } else if (var != NULL &&
2822 !var->is_global() &&
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002823 var->AsSlot() != NULL &&
2824 var->AsSlot()->type() != Slot::LOOKUP) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002825 // Result of deleting non-global, non-dynamic variables is false.
2826 // The subexpression does not have side effects.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002827 context()->Plug(false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002828 } else {
2829 // Property or variable reference. Call the delete builtin with
2830 // object and property name as arguments.
2831 if (prop != NULL) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002832 VisitForStackValue(prop->obj());
2833 VisitForStackValue(prop->key());
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002834 } else if (var->is_global()) {
2835 __ ldr(r1, CodeGenerator::GlobalObject());
2836 __ mov(r0, Operand(var->name()));
2837 __ Push(r1, r0);
2838 } else {
2839 // Non-global variable. Call the runtime to look up the context
2840 // where the variable was introduced.
2841 __ push(context_register());
2842 __ mov(r2, Operand(var->name()));
2843 __ push(r2);
2844 __ CallRuntime(Runtime::kLookupContext, 2);
2845 __ push(r0);
2846 __ mov(r2, Operand(var->name()));
2847 __ push(r2);
2848 }
2849 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002850 context()->Plug(r0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002851 }
2852 break;
2853 }
2854
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002855 case Token::VOID: {
2856 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
2857 VisitForEffect(expr->expression());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002858 context()->Plug(Heap::kUndefinedValueRootIndex);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002859 break;
2860 }
2861
2862 case Token::NOT: {
2863 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002864 Label materialize_true, materialize_false;
2865 Label* if_true = NULL;
2866 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002867 Label* fall_through = NULL;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002868
2869 // Notice that the labels are swapped.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002870 context()->PrepareTest(&materialize_true, &materialize_false,
2871 &if_false, &if_true, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002872 VisitForControl(expr->expression(), if_true, if_false, fall_through);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002873 context()->Plug(if_false, if_true); // Labels swapped.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002874 break;
2875 }
2876
2877 case Token::TYPEOF: {
2878 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002879 { StackValueContext context(this);
2880 VisitForTypeofValue(expr->expression());
2881 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002882 __ CallRuntime(Runtime::kTypeof, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002883 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002884 break;
2885 }
2886
2887 case Token::ADD: {
2888 Comment cmt(masm_, "[ UnaryOperation (ADD)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002889 VisitForAccumulatorValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002890 Label no_conversion;
2891 __ tst(result_register(), Operand(kSmiTagMask));
2892 __ b(eq, &no_conversion);
2893 __ push(r0);
2894 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
2895 __ bind(&no_conversion);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002896 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002897 break;
2898 }
2899
2900 case Token::SUB: {
2901 Comment cmt(masm_, "[ UnaryOperation (SUB)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002902 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002903 UnaryOverwriteMode overwrite =
2904 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002905 GenericUnaryOpStub stub(Token::SUB,
2906 overwrite,
2907 NO_UNARY_FLAGS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002908 // GenericUnaryOpStub expects the argument to be in the
2909 // accumulator register r0.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002910 VisitForAccumulatorValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002911 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002912 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002913 break;
2914 }
2915
2916 case Token::BIT_NOT: {
2917 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002918 // The generic unary operation stub expects the argument to be
2919 // in the accumulator register r0.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002920 VisitForAccumulatorValue(expr->expression());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002921 Label done;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002922 bool inline_smi_code = ShouldInlineSmiCase(expr->op());
2923 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002924 Label call_stub;
2925 __ BranchOnNotSmi(r0, &call_stub);
2926 __ mvn(r0, Operand(r0));
2927 // Bit-clear inverted smi-tag.
2928 __ bic(r0, r0, Operand(kSmiTagMask));
2929 __ b(&done);
2930 __ bind(&call_stub);
2931 }
2932 bool overwrite = expr->expression()->ResultOverwriteAllowed();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002933 UnaryOpFlags flags = inline_smi_code
2934 ? NO_UNARY_SMI_CODE_IN_STUB
2935 : NO_UNARY_FLAGS;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002936 UnaryOverwriteMode mode =
2937 overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002938 GenericUnaryOpStub stub(Token::BIT_NOT, mode, flags);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002939 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002940 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002941 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002942 break;
2943 }
2944
2945 default:
2946 UNREACHABLE();
2947 }
2948}
2949
2950
2951void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
2952 Comment cmnt(masm_, "[ CountOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00002953 SetSourcePosition(expr->position());
2954
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002955 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
2956 // as the left-hand side.
2957 if (!expr->expression()->IsValidLeftHandSide()) {
2958 VisitForEffect(expr->expression());
2959 return;
2960 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002961
2962 // Expression can only be a property, a global or a (parameter or local)
2963 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
2964 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2965 LhsKind assign_type = VARIABLE;
2966 Property* prop = expr->expression()->AsProperty();
2967 // In case of a property we use the uninitialized expression context
2968 // of the key to detect a named property.
2969 if (prop != NULL) {
2970 assign_type =
2971 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
2972 }
2973
2974 // Evaluate expression and get value.
2975 if (assign_type == VARIABLE) {
2976 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002977 AccumulatorValueContext context(this);
2978 EmitVariableLoad(expr->expression()->AsVariableProxy()->var());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002979 } else {
2980 // Reserve space for result of postfix operation.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002981 if (expr->is_postfix() && !context()->IsEffect()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002982 __ mov(ip, Operand(Smi::FromInt(0)));
2983 __ push(ip);
2984 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002985 if (assign_type == NAMED_PROPERTY) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002986 // Put the object both on the stack and in the accumulator.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002987 VisitForAccumulatorValue(prop->obj());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002988 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002989 EmitNamedPropertyLoad(prop);
2990 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002991 VisitForStackValue(prop->obj());
2992 VisitForAccumulatorValue(prop->key());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002993 __ ldr(r1, MemOperand(sp, 0));
2994 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002995 EmitKeyedPropertyLoad(prop);
2996 }
2997 }
2998
2999 // Call ToNumber only if operand is not a smi.
3000 Label no_conversion;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003001 __ BranchOnSmi(r0, &no_conversion);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003002 __ push(r0);
3003 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
3004 __ bind(&no_conversion);
3005
3006 // Save result for postfix expressions.
3007 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003008 if (!context()->IsEffect()) {
3009 // Save the result on the stack. If we have a named or keyed property
3010 // we store the result under the receiver that is currently on top
3011 // of the stack.
3012 switch (assign_type) {
3013 case VARIABLE:
3014 __ push(r0);
3015 break;
3016 case NAMED_PROPERTY:
3017 __ str(r0, MemOperand(sp, kPointerSize));
3018 break;
3019 case KEYED_PROPERTY:
3020 __ str(r0, MemOperand(sp, 2 * kPointerSize));
3021 break;
3022 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003023 }
3024 }
3025
3026
3027 // Inline smi case if we are in a loop.
3028 Label stub_call, done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003029 int count_value = expr->op() == Token::INC ? 1 : -1;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003030 if (ShouldInlineSmiCase(expr->op())) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003031 __ add(r0, r0, Operand(Smi::FromInt(count_value)), SetCC);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003032 __ b(vs, &stub_call);
3033 // We could eliminate this smi check if we split the code at
3034 // the first smi check before calling ToNumber.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003035 __ BranchOnSmi(r0, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003036 __ bind(&stub_call);
3037 // Call stub. Undo operation first.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003038 __ sub(r0, r0, Operand(Smi::FromInt(count_value)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003039 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003040 __ mov(r1, Operand(Smi::FromInt(count_value)));
ager@chromium.org357bf652010-04-12 11:30:10 +00003041 GenericBinaryOpStub stub(Token::ADD, NO_OVERWRITE, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003042 __ CallStub(&stub);
3043 __ bind(&done);
3044
3045 // Store the value returned in r0.
3046 switch (assign_type) {
3047 case VARIABLE:
3048 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003049 { EffectContext context(this);
3050 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3051 Token::ASSIGN);
3052 }
3053 // For all contexts except EffectConstant We have the result on
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003054 // top of the stack.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003055 if (!context()->IsEffect()) {
3056 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003057 }
3058 } else {
3059 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003060 Token::ASSIGN);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003061 }
3062 break;
3063 case NAMED_PROPERTY: {
3064 __ mov(r2, Operand(prop->key()->AsLiteral()->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003065 __ pop(r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003066 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003067 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003068 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003069 if (!context()->IsEffect()) {
3070 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003071 }
3072 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003073 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003074 }
3075 break;
3076 }
3077 case KEYED_PROPERTY: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003078 __ pop(r1); // Key.
3079 __ pop(r2); // Receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003080 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003081 EmitCallIC(ic, RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003082 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003083 if (!context()->IsEffect()) {
3084 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003085 }
3086 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003087 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003088 }
3089 break;
3090 }
3091 }
3092}
3093
3094
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003095void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
3096 ASSERT(!context()->IsEffect());
3097 ASSERT(!context()->IsTest());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003098 VariableProxy* proxy = expr->AsVariableProxy();
3099 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
3100 Comment cmnt(masm_, "Global variable");
3101 __ ldr(r0, CodeGenerator::GlobalObject());
3102 __ mov(r2, Operand(proxy->name()));
3103 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
3104 // Use a regular load, not a contextual load, to avoid a reference
3105 // error.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003106 EmitCallIC(ic, RelocInfo::CODE_TARGET);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003107 context()->Plug(r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003108 } else if (proxy != NULL &&
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003109 proxy->var()->AsSlot() != NULL &&
3110 proxy->var()->AsSlot()->type() == Slot::LOOKUP) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003111 Label done, slow;
3112
3113 // Generate code for loading from variables potentially shadowed
3114 // by eval-introduced variables.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003115 Slot* slot = proxy->var()->AsSlot();
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003116 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
3117
3118 __ bind(&slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003119 __ mov(r0, Operand(proxy->name()));
3120 __ Push(cp, r0);
3121 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003122 __ bind(&done);
3123
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003124 context()->Plug(r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003125 } else {
3126 // This expression cannot throw a reference error at the top level.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003127 Visit(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003128 }
3129}
3130
3131
ricow@chromium.org65fae842010-08-25 15:26:24 +00003132bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
3133 Expression* left,
3134 Expression* right,
3135 Label* if_true,
3136 Label* if_false,
3137 Label* fall_through) {
3138 if (op != Token::EQ && op != Token::EQ_STRICT) return false;
3139
3140 // Check for the pattern: typeof <expression> == <string literal>.
3141 Literal* right_literal = right->AsLiteral();
3142 if (right_literal == NULL) return false;
3143 Handle<Object> right_literal_value = right_literal->handle();
3144 if (!right_literal_value->IsString()) return false;
3145 UnaryOperation* left_unary = left->AsUnaryOperation();
3146 if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
3147 Handle<String> check = Handle<String>::cast(right_literal_value);
3148
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003149 { AccumulatorValueContext context(this);
3150 VisitForTypeofValue(left_unary->expression());
3151 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003152 if (check->Equals(Heap::number_symbol())) {
3153 __ tst(r0, Operand(kSmiTagMask));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003154 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003155 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3156 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
3157 __ cmp(r0, ip);
3158 Split(eq, if_true, if_false, fall_through);
3159 } else if (check->Equals(Heap::string_symbol())) {
3160 __ tst(r0, Operand(kSmiTagMask));
3161 __ b(eq, if_false);
3162 // Check for undetectable objects => false.
3163 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3164 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
3165 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3166 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3167 __ b(eq, if_false);
3168 __ ldrb(r1, FieldMemOperand(r0, Map::kInstanceTypeOffset));
3169 __ cmp(r1, Operand(FIRST_NONSTRING_TYPE));
3170 Split(lt, if_true, if_false, fall_through);
3171 } else if (check->Equals(Heap::boolean_symbol())) {
3172 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3173 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003174 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003175 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
3176 __ cmp(r0, ip);
3177 Split(eq, if_true, if_false, fall_through);
3178 } else if (check->Equals(Heap::undefined_symbol())) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003179 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003180 __ cmp(r0, ip);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003181 __ b(eq, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003182 __ tst(r0, Operand(kSmiTagMask));
3183 __ b(eq, if_false);
3184 // Check for undetectable objects => true.
3185 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3186 __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
3187 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3188 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3189 Split(eq, if_true, if_false, fall_through);
3190 } else if (check->Equals(Heap::function_symbol())) {
3191 __ tst(r0, Operand(kSmiTagMask));
3192 __ b(eq, if_false);
3193 __ CompareObjectType(r0, r1, r0, JS_FUNCTION_TYPE);
3194 __ b(eq, if_true);
3195 // Regular expressions => 'function' (they are callable).
3196 __ CompareInstanceType(r1, r0, JS_REGEXP_TYPE);
3197 Split(eq, if_true, if_false, fall_through);
3198 } else if (check->Equals(Heap::object_symbol())) {
3199 __ tst(r0, Operand(kSmiTagMask));
3200 __ b(eq, if_false);
3201 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3202 __ cmp(r0, ip);
3203 __ b(eq, if_true);
3204 // Regular expressions => 'function', not 'object'.
3205 __ CompareObjectType(r0, r1, r0, JS_REGEXP_TYPE);
3206 __ b(eq, if_false);
3207 // Check for undetectable objects => false.
3208 __ ldrb(r0, FieldMemOperand(r1, Map::kBitFieldOffset));
3209 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3210 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
3211 __ b(eq, if_false);
3212 // Check for JS objects => true.
3213 __ ldrb(r0, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3214 __ cmp(r0, Operand(FIRST_JS_OBJECT_TYPE));
3215 __ b(lt, if_false);
3216 __ cmp(r0, Operand(LAST_JS_OBJECT_TYPE));
3217 Split(le, if_true, if_false, fall_through);
3218 } else {
3219 if (if_false != fall_through) __ jmp(if_false);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003220 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003221
3222 return true;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003223}
3224
3225
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003226void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
3227 Comment cmnt(masm_, "[ CompareOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00003228 SetSourcePosition(expr->position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003229
3230 // Always perform the comparison for its control flow. Pack the result
3231 // into the expression's context after the comparison is performed.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003232
3233 Label materialize_true, materialize_false;
3234 Label* if_true = NULL;
3235 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003236 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003237 context()->PrepareTest(&materialize_true, &materialize_false,
3238 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003239
3240 // First we try a fast inlined version of the compare when one of
3241 // the operands is a literal.
3242 Token::Value op = expr->op();
3243 Expression* left = expr->left();
3244 Expression* right = expr->right();
3245 if (TryLiteralCompare(op, left, right, if_true, if_false, fall_through)) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003246 context()->Plug(if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003247 return;
3248 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003249
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003250 VisitForStackValue(expr->left());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003251 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003252 case Token::IN:
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003253 VisitForStackValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003254 __ InvokeBuiltin(Builtins::IN, CALL_JS);
3255 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3256 __ cmp(r0, ip);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003257 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003258 break;
3259
3260 case Token::INSTANCEOF: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003261 VisitForStackValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003262 InstanceofStub stub;
3263 __ CallStub(&stub);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003264 // The stub returns 0 for true.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003265 __ tst(r0, r0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003266 Split(eq, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003267 break;
3268 }
3269
3270 default: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003271 VisitForAccumulatorValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003272 Condition cc = eq;
3273 bool strict = false;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003274 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003275 case Token::EQ_STRICT:
3276 strict = true;
3277 // Fall through
ricow@chromium.org65fae842010-08-25 15:26:24 +00003278 case Token::EQ:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003279 cc = eq;
3280 __ pop(r1);
3281 break;
3282 case Token::LT:
3283 cc = lt;
3284 __ pop(r1);
3285 break;
3286 case Token::GT:
3287 // Reverse left and right sides to obtain ECMA-262 conversion order.
3288 cc = lt;
3289 __ mov(r1, result_register());
3290 __ pop(r0);
3291 break;
3292 case Token::LTE:
3293 // Reverse left and right sides to obtain ECMA-262 conversion order.
3294 cc = ge;
3295 __ mov(r1, result_register());
3296 __ pop(r0);
3297 break;
3298 case Token::GTE:
3299 cc = ge;
3300 __ pop(r1);
3301 break;
3302 case Token::IN:
3303 case Token::INSTANCEOF:
3304 default:
3305 UNREACHABLE();
3306 }
3307
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003308 bool inline_smi_code = ShouldInlineSmiCase(op);
3309 if (inline_smi_code) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003310 Label slow_case;
3311 __ orr(r2, r0, Operand(r1));
3312 __ BranchOnNotSmi(r2, &slow_case);
3313 __ cmp(r1, r0);
3314 Split(cc, if_true, if_false, NULL);
3315 __ bind(&slow_case);
3316 }
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003317 CompareFlags flags = inline_smi_code
3318 ? NO_SMI_COMPARE_IN_STUB
3319 : NO_COMPARE_FLAGS;
3320 CompareStub stub(cc, strict, flags, r1, r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003321 __ CallStub(&stub);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00003322 __ cmp(r0, Operand(0, RelocInfo::NONE));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003323 Split(cc, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003324 }
3325 }
3326
3327 // Convert the result of the comparison into one expected for this
3328 // expression's context.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003329 context()->Plug(if_true, if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003330}
3331
3332
ricow@chromium.org65fae842010-08-25 15:26:24 +00003333void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
3334 Comment cmnt(masm_, "[ CompareToNull");
3335 Label materialize_true, materialize_false;
3336 Label* if_true = NULL;
3337 Label* if_false = NULL;
3338 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003339 context()->PrepareTest(&materialize_true, &materialize_false,
3340 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003341
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003342 VisitForAccumulatorValue(expr->expression());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003343 __ LoadRoot(r1, Heap::kNullValueRootIndex);
3344 __ cmp(r0, r1);
3345 if (expr->is_strict()) {
3346 Split(eq, if_true, if_false, fall_through);
3347 } else {
3348 __ b(eq, if_true);
3349 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
3350 __ cmp(r0, r1);
3351 __ b(eq, if_true);
3352 __ tst(r0, Operand(kSmiTagMask));
3353 __ b(eq, if_false);
3354 // It can be an undetectable object.
3355 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3356 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3357 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3358 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3359 Split(eq, if_true, if_false, fall_through);
3360 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003361 context()->Plug(if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003362}
3363
3364
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003365void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3366 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003367 context()->Plug(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003368}
3369
3370
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003371Register FullCodeGenerator::result_register() {
3372 return r0;
3373}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003374
3375
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00003376Register FullCodeGenerator::context_register() {
3377 return cp;
3378}
3379
3380
3381void FullCodeGenerator::EmitCallIC(Handle<Code> ic, RelocInfo::Mode mode) {
3382 ASSERT(mode == RelocInfo::CODE_TARGET ||
3383 mode == RelocInfo::CODE_TARGET_CONTEXT);
3384 __ Call(ic, mode);
3385}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003386
3387
3388void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3389 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
3390 __ str(value, MemOperand(fp, frame_offset));
3391}
3392
3393
3394void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003395 __ ldr(dst, ContextOperand(cp, context_index));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003396}
3397
3398
3399// ----------------------------------------------------------------------------
3400// Non-local control flow support.
3401
3402void FullCodeGenerator::EnterFinallyBlock() {
3403 ASSERT(!result_register().is(r1));
3404 // Store result register while executing finally block.
3405 __ push(result_register());
3406 // Cook return address in link register to stack (smi encoded Code* delta)
3407 __ sub(r1, lr, Operand(masm_->CodeObject()));
3408 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3409 ASSERT_EQ(0, kSmiTag);
3410 __ add(r1, r1, Operand(r1)); // Convert to smi.
3411 __ push(r1);
3412}
3413
3414
3415void FullCodeGenerator::ExitFinallyBlock() {
3416 ASSERT(!result_register().is(r1));
3417 // Restore result register from stack.
3418 __ pop(r1);
3419 // Uncook return address and return.
3420 __ pop(result_register());
3421 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
3422 __ mov(r1, Operand(r1, ASR, 1)); // Un-smi-tag value.
3423 __ add(pc, r1, Operand(masm_->CodeObject()));
3424}
3425
3426
3427#undef __
3428
3429} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003430
3431#endif // V8_TARGET_ARCH_ARM