blob: 73b3984974a80e4bba4a407d37445e686b2df4bc [file] [log] [blame]
lrn@chromium.org7516f052011-03-30 08:52:27 +00001// Copyright 2011 the V8 project authors. All rights reserved.
ager@chromium.org5c838252010-02-19 08:53:10 +00002// 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_MIPS)
31
lrn@chromium.org7516f052011-03-30 08:52:27 +000032// Note on Mips implementation:
33//
34// The result_register() for mips is the 'v0' register, which is defined
35// by the ABI to contain function return values. However, the first
36// parameter to a function is defined to be 'a0'. So there are many
37// places where we have to move a previous result in v0 to a0 for the
38// next call: mov(a0, v0). This is not needed on the other architectures.
39
40#include "code-stubs.h"
karlklose@chromium.org83a47282011-05-11 11:54:09 +000041#include "codegen.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000042#include "compiler.h"
43#include "debug.h"
44#include "full-codegen.h"
45#include "parser.h"
lrn@chromium.org7516f052011-03-30 08:52:27 +000046#include "scopes.h"
47#include "stub-cache.h"
48
49#include "mips/code-stubs-mips.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000050
51namespace v8 {
52namespace internal {
53
54#define __ ACCESS_MASM(masm_)
55
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000056
danno@chromium.org40cb8782011-05-25 07:58:50 +000057static unsigned GetPropertyId(Property* property) {
58 if (property->is_synthetic()) return AstNode::kNoNumber;
59 return property->id();
60}
61
62
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000063// A patch site is a location in the code which it is possible to patch. This
64// class has a number of methods to emit the code which is patchable and the
65// method EmitPatchInfo to record a marker back to the patchable code. This
66// marker is a andi at, rx, #yyy instruction, and x * 0x0000ffff + yyy (raw 16
67// bit immediate value is used) is the delta from the pc to the first
68// instruction of the patchable code.
69class JumpPatchSite BASE_EMBEDDED {
70 public:
71 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
72#ifdef DEBUG
73 info_emitted_ = false;
74#endif
75 }
76
77 ~JumpPatchSite() {
78 ASSERT(patch_site_.is_bound() == info_emitted_);
79 }
80
81 // When initially emitting this ensure that a jump is always generated to skip
82 // the inlined smi code.
83 void EmitJumpIfNotSmi(Register reg, Label* target) {
84 ASSERT(!patch_site_.is_bound() && !info_emitted_);
85 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
86 __ bind(&patch_site_);
87 __ andi(at, reg, 0);
88 // Always taken before patched.
89 __ Branch(target, eq, at, Operand(zero_reg));
90 }
91
92 // When initially emitting this ensure that a jump is never generated to skip
93 // the inlined smi code.
94 void EmitJumpIfSmi(Register reg, Label* target) {
95 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
96 ASSERT(!patch_site_.is_bound() && !info_emitted_);
97 __ bind(&patch_site_);
98 __ andi(at, reg, 0);
99 // Never taken before patched.
100 __ Branch(target, ne, at, Operand(zero_reg));
101 }
102
103 void EmitPatchInfo() {
104 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
105 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
106 __ andi(at, reg, delta_to_patch_site % kImm16Mask);
107#ifdef DEBUG
108 info_emitted_ = true;
109#endif
110 }
111
112 bool is_bound() const { return patch_site_.is_bound(); }
113
114 private:
115 MacroAssembler* masm_;
116 Label patch_site_;
117#ifdef DEBUG
118 bool info_emitted_;
119#endif
120};
121
122
lrn@chromium.org7516f052011-03-30 08:52:27 +0000123// Generate code for a JS function. On entry to the function the receiver
124// and arguments have been pushed on the stack left to right. The actual
125// argument count matches the formal parameter count expected by the
126// function.
127//
128// The live registers are:
129// o a1: the JS function object being called (ie, ourselves)
130// o cp: our context
131// o fp: our caller's frame pointer
132// o sp: stack pointer
133// o ra: return address
134//
135// The function builds a JS frame. Please see JavaScriptFrameConstants in
136// frames-mips.h for its layout.
137void FullCodeGenerator::Generate(CompilationInfo* info) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000138 ASSERT(info_ == NULL);
139 info_ = info;
140 SetFunctionPosition(function());
141 Comment cmnt(masm_, "[ function compiled by full code generator");
142
143#ifdef DEBUG
144 if (strlen(FLAG_stop_at) > 0 &&
145 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
146 __ stop("stop-at");
147 }
148#endif
149
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000150 // Strict mode functions and builtins need to replace the receiver
151 // with undefined when called as functions (without an explicit
152 // receiver object). t1 is zero for method calls and non-zero for
153 // function calls.
154 if (info->is_strict_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000155 Label ok;
156 __ Branch(&ok, eq, t1, Operand(zero_reg));
157 int receiver_offset = scope()->num_parameters() * kPointerSize;
158 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
159 __ sw(a2, MemOperand(sp, receiver_offset));
160 __ bind(&ok);
161 }
162
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000163 int locals_count = scope()->num_stack_slots();
164
165 __ Push(ra, fp, cp, a1);
166 if (locals_count > 0) {
167 // Load undefined value here, so the value is ready for the loop
168 // below.
169 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
170 }
171 // Adjust fp to point to caller's fp.
172 __ Addu(fp, sp, Operand(2 * kPointerSize));
173
174 { Comment cmnt(masm_, "[ Allocate locals");
175 for (int i = 0; i < locals_count; i++) {
176 __ push(at);
177 }
178 }
179
180 bool function_in_register = true;
181
182 // Possibly allocate a local context.
183 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
184 if (heap_slots > 0) {
185 Comment cmnt(masm_, "[ Allocate local context");
186 // Argument to NewContext is the function, which is in a1.
187 __ push(a1);
188 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
189 FastNewContextStub stub(heap_slots);
190 __ CallStub(&stub);
191 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000192 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000193 }
194 function_in_register = false;
195 // Context is returned in both v0 and cp. It replaces the context
196 // passed to us. It's saved in the stack and kept live in cp.
197 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
198 // Copy any necessary parameters into the context.
199 int num_parameters = scope()->num_parameters();
200 for (int i = 0; i < num_parameters; i++) {
201 Slot* slot = scope()->parameter(i)->AsSlot();
202 if (slot != NULL && slot->type() == Slot::CONTEXT) {
203 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
204 (num_parameters - 1 - i) * kPointerSize;
205 // Load parameter from stack.
206 __ lw(a0, MemOperand(fp, parameter_offset));
207 // Store it in the context.
208 __ li(a1, Operand(Context::SlotOffset(slot->index())));
209 __ addu(a2, cp, a1);
210 __ sw(a0, MemOperand(a2, 0));
211 // Update the write barrier. This clobbers all involved
212 // registers, so we have to use two more registers to avoid
213 // clobbering cp.
214 __ mov(a2, cp);
215 __ RecordWrite(a2, a1, a3);
216 }
217 }
218 }
219
220 Variable* arguments = scope()->arguments();
221 if (arguments != NULL) {
222 // Function uses arguments object.
223 Comment cmnt(masm_, "[ Allocate arguments object");
224 if (!function_in_register) {
225 // Load this again, if it's used by the local context below.
226 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
227 } else {
228 __ mov(a3, a1);
229 }
230 // Receiver is just before the parameters on the caller's stack.
231 int offset = scope()->num_parameters() * kPointerSize;
232 __ Addu(a2, fp,
233 Operand(StandardFrameConstants::kCallerSPOffset + offset));
234 __ li(a1, Operand(Smi::FromInt(scope()->num_parameters())));
235 __ Push(a3, a2, a1);
236
237 // Arguments to ArgumentsAccessStub:
238 // function, receiver address, parameter count.
239 // The stub will rewrite receiever and parameter count if the previous
240 // stack frame was an arguments adapter frame.
241 ArgumentsAccessStub stub(
242 is_strict_mode() ? ArgumentsAccessStub::NEW_STRICT
243 : ArgumentsAccessStub::NEW_NON_STRICT);
244 __ CallStub(&stub);
245
246 Variable* arguments_shadow = scope()->arguments_shadow();
247 if (arguments_shadow != NULL) {
248 // Duplicate the value; move-to-slot operation might clobber registers.
249 __ mov(a3, v0);
250 Move(arguments_shadow->AsSlot(), a3, a1, a2);
251 }
252 Move(arguments->AsSlot(), v0, a1, a2);
253 }
254
255 if (FLAG_trace) {
256 __ CallRuntime(Runtime::kTraceEnter, 0);
257 }
258
259 // Visit the declarations and body unless there is an illegal
260 // redeclaration.
261 if (scope()->HasIllegalRedeclaration()) {
262 Comment cmnt(masm_, "[ Declarations");
263 scope()->VisitIllegalRedeclaration(this);
264
265 } else {
266 { Comment cmnt(masm_, "[ Declarations");
267 // For named function expressions, declare the function name as a
268 // constant.
269 if (scope()->is_function_scope() && scope()->function() != NULL) {
270 EmitDeclaration(scope()->function(), Variable::CONST, NULL);
271 }
272 VisitDeclarations(scope()->declarations());
273 }
274
275 { Comment cmnt(masm_, "[ Stack check");
276 PrepareForBailoutForId(AstNode::kFunctionEntryId, NO_REGISTERS);
277 Label ok;
278 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
279 __ Branch(&ok, hs, sp, Operand(t0));
280 StackCheckStub stub;
281 __ CallStub(&stub);
282 __ bind(&ok);
283 }
284
285 { Comment cmnt(masm_, "[ Body");
286 ASSERT(loop_depth() == 0);
287 VisitStatements(function()->body());
288 ASSERT(loop_depth() == 0);
289 }
290 }
291
292 // Always emit a 'return undefined' in case control fell off the end of
293 // the body.
294 { Comment cmnt(masm_, "[ return <undefined>;");
295 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
296 }
297 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000298}
299
300
301void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000302 ASSERT(Smi::FromInt(0) == 0);
303 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000304}
305
306
307void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000308 Comment cmnt(masm_, "[ Stack check");
309 Label ok;
310 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
311 __ Branch(&ok, hs, sp, Operand(t0));
312 StackCheckStub stub;
313 // Record a mapping of this PC offset to the OSR id. This is used to find
314 // the AST id from the unoptimized code in order to use it as a key into
315 // the deoptimization input data found in the optimized code.
316 RecordStackCheck(stmt->OsrEntryId());
317
318 __ CallStub(&stub);
319 __ bind(&ok);
320 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
321 // Record a mapping of the OSR id to this PC. This is used if the OSR
322 // entry becomes the target of a bailout. We don't expect it to be, but
323 // we want it to work if it is.
324 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000325}
326
327
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000328void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000329 Comment cmnt(masm_, "[ Return sequence");
330 if (return_label_.is_bound()) {
331 __ Branch(&return_label_);
332 } else {
333 __ bind(&return_label_);
334 if (FLAG_trace) {
335 // Push the return value on the stack as the parameter.
336 // Runtime::TraceExit returns its parameter in v0.
337 __ push(v0);
338 __ CallRuntime(Runtime::kTraceExit, 1);
339 }
340
341#ifdef DEBUG
342 // Add a label for checking the size of the code used for returning.
343 Label check_exit_codesize;
344 masm_->bind(&check_exit_codesize);
345#endif
346 // Make sure that the constant pool is not emitted inside of the return
347 // sequence.
348 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
349 // Here we use masm_-> instead of the __ macro to avoid the code coverage
350 // tool from instrumenting as we rely on the code size here.
351 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
352 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
353 __ RecordJSReturn();
354 masm_->mov(sp, fp);
355 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
356 masm_->Addu(sp, sp, Operand(sp_delta));
357 masm_->Jump(ra);
358 }
359
360#ifdef DEBUG
361 // Check that the size of the code used for returning is large enough
362 // for the debugger's requirements.
363 ASSERT(Assembler::kJSReturnSequenceInstructions <=
364 masm_->InstructionsGeneratedSince(&check_exit_codesize));
365#endif
366 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000367}
368
369
lrn@chromium.org7516f052011-03-30 08:52:27 +0000370void FullCodeGenerator::EffectContext::Plug(Slot* slot) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000371}
372
373
lrn@chromium.org7516f052011-03-30 08:52:27 +0000374void FullCodeGenerator::AccumulatorValueContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000375 codegen()->Move(result_register(), slot);
ager@chromium.org5c838252010-02-19 08:53:10 +0000376}
377
378
lrn@chromium.org7516f052011-03-30 08:52:27 +0000379void FullCodeGenerator::StackValueContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000380 codegen()->Move(result_register(), slot);
381 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000382}
383
384
lrn@chromium.org7516f052011-03-30 08:52:27 +0000385void FullCodeGenerator::TestContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000386 // For simplicity we always test the accumulator register.
387 codegen()->Move(result_register(), slot);
388 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000389 codegen()->DoTest(this);
ager@chromium.org5c838252010-02-19 08:53:10 +0000390}
391
392
lrn@chromium.org7516f052011-03-30 08:52:27 +0000393void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000394}
395
396
lrn@chromium.org7516f052011-03-30 08:52:27 +0000397void FullCodeGenerator::AccumulatorValueContext::Plug(
398 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000399 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000400}
401
402
403void FullCodeGenerator::StackValueContext::Plug(
404 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000405 __ LoadRoot(result_register(), index);
406 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000407}
408
409
410void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000411 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
412 true,
413 true_label_,
414 false_label_);
415 if (index == Heap::kUndefinedValueRootIndex ||
416 index == Heap::kNullValueRootIndex ||
417 index == Heap::kFalseValueRootIndex) {
418 if (false_label_ != fall_through_) __ Branch(false_label_);
419 } else if (index == Heap::kTrueValueRootIndex) {
420 if (true_label_ != fall_through_) __ Branch(true_label_);
421 } else {
422 __ LoadRoot(result_register(), index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000423 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000424 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000425}
426
427
428void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000429}
430
431
432void FullCodeGenerator::AccumulatorValueContext::Plug(
433 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000434 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000435}
436
437
438void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000439 // Immediates cannot be pushed directly.
440 __ li(result_register(), Operand(lit));
441 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000442}
443
444
445void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000446 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
447 true,
448 true_label_,
449 false_label_);
450 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
451 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
452 if (false_label_ != fall_through_) __ Branch(false_label_);
453 } else if (lit->IsTrue() || lit->IsJSObject()) {
454 if (true_label_ != fall_through_) __ Branch(true_label_);
455 } else if (lit->IsString()) {
456 if (String::cast(*lit)->length() == 0) {
457 if (false_label_ != fall_through_) __ Branch(false_label_);
458 } else {
459 if (true_label_ != fall_through_) __ Branch(true_label_);
460 }
461 } else if (lit->IsSmi()) {
462 if (Smi::cast(*lit)->value() == 0) {
463 if (false_label_ != fall_through_) __ Branch(false_label_);
464 } else {
465 if (true_label_ != fall_through_) __ Branch(true_label_);
466 }
467 } else {
468 // For simplicity we always test the accumulator register.
469 __ li(result_register(), Operand(lit));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000470 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000471 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000472}
473
474
475void FullCodeGenerator::EffectContext::DropAndPlug(int count,
476 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000477 ASSERT(count > 0);
478 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000479}
480
481
482void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
483 int count,
484 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000485 ASSERT(count > 0);
486 __ Drop(count);
487 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000488}
489
490
491void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
492 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000493 ASSERT(count > 0);
494 if (count > 1) __ Drop(count - 1);
495 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000496}
497
498
499void FullCodeGenerator::TestContext::DropAndPlug(int count,
500 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000501 ASSERT(count > 0);
502 // For simplicity we always test the accumulator register.
503 __ Drop(count);
504 __ Move(result_register(), reg);
505 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000506 codegen()->DoTest(this);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000507}
508
509
510void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
511 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000512 ASSERT(materialize_true == materialize_false);
513 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000514}
515
516
517void FullCodeGenerator::AccumulatorValueContext::Plug(
518 Label* materialize_true,
519 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000520 Label done;
521 __ bind(materialize_true);
522 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
523 __ Branch(&done);
524 __ bind(materialize_false);
525 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
526 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000527}
528
529
530void FullCodeGenerator::StackValueContext::Plug(
531 Label* materialize_true,
532 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000533 Label done;
534 __ bind(materialize_true);
535 __ LoadRoot(at, Heap::kTrueValueRootIndex);
536 __ push(at);
537 __ Branch(&done);
538 __ bind(materialize_false);
539 __ LoadRoot(at, Heap::kFalseValueRootIndex);
540 __ push(at);
541 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000542}
543
544
545void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
546 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000547 ASSERT(materialize_true == true_label_);
548 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000549}
550
551
552void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000553}
554
555
556void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000557 Heap::RootListIndex value_root_index =
558 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
559 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000560}
561
562
563void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000564 Heap::RootListIndex value_root_index =
565 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
566 __ LoadRoot(at, value_root_index);
567 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000568}
569
570
571void FullCodeGenerator::TestContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000572 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
573 true,
574 true_label_,
575 false_label_);
576 if (flag) {
577 if (true_label_ != fall_through_) __ Branch(true_label_);
578 } else {
579 if (false_label_ != fall_through_) __ Branch(false_label_);
580 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000581}
582
583
whesse@chromium.org7b260152011-06-20 15:33:18 +0000584void FullCodeGenerator::DoTest(Expression* condition,
585 Label* if_true,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000586 Label* if_false,
587 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000588 if (CpuFeatures::IsSupported(FPU)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000589 ToBooleanStub stub(result_register());
590 __ CallStub(&stub);
591 __ mov(at, zero_reg);
592 } else {
593 // Call the runtime to find the boolean value of the source and then
594 // translate it into control flow to the pair of labels.
595 __ push(result_register());
596 __ CallRuntime(Runtime::kToBool, 1);
597 __ LoadRoot(at, Heap::kFalseValueRootIndex);
598 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000599 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000600}
601
602
lrn@chromium.org7516f052011-03-30 08:52:27 +0000603void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000604 Register lhs,
605 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000606 Label* if_true,
607 Label* if_false,
608 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000609 if (if_false == fall_through) {
610 __ Branch(if_true, cc, lhs, rhs);
611 } else if (if_true == fall_through) {
612 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
613 } else {
614 __ Branch(if_true, cc, lhs, rhs);
615 __ Branch(if_false);
616 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000617}
618
619
620MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000621 switch (slot->type()) {
622 case Slot::PARAMETER:
623 case Slot::LOCAL:
624 return MemOperand(fp, SlotOffset(slot));
625 case Slot::CONTEXT: {
626 int context_chain_length =
627 scope()->ContextChainLength(slot->var()->scope());
628 __ LoadContext(scratch, context_chain_length);
629 return ContextOperand(scratch, slot->index());
630 }
631 case Slot::LOOKUP:
632 UNREACHABLE();
633 }
634 UNREACHABLE();
635 return MemOperand(v0, 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000636}
637
638
639void FullCodeGenerator::Move(Register destination, Slot* source) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000640 // Use destination as scratch.
641 MemOperand slot_operand = EmitSlotSearch(source, destination);
642 __ lw(destination, slot_operand);
ager@chromium.org5c838252010-02-19 08:53:10 +0000643}
644
645
lrn@chromium.org7516f052011-03-30 08:52:27 +0000646void FullCodeGenerator::PrepareForBailoutBeforeSplit(State state,
647 bool should_normalize,
648 Label* if_true,
649 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000650 // Only prepare for bailouts before splits if we're in a test
651 // context. Otherwise, we let the Visit function deal with the
652 // preparation to avoid preparing with the same AST id twice.
653 if (!context()->IsTest() || !info_->IsOptimizable()) return;
654
655 Label skip;
656 if (should_normalize) __ Branch(&skip);
657
658 ForwardBailoutStack* current = forward_bailout_stack_;
659 while (current != NULL) {
660 PrepareForBailout(current->expr(), state);
661 current = current->parent();
662 }
663
664 if (should_normalize) {
665 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
666 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
667 __ bind(&skip);
668 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000669}
670
671
ager@chromium.org5c838252010-02-19 08:53:10 +0000672void FullCodeGenerator::Move(Slot* dst,
673 Register src,
674 Register scratch1,
675 Register scratch2) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000676 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
677 ASSERT(!scratch1.is(src) && !scratch2.is(src));
678 MemOperand location = EmitSlotSearch(dst, scratch1);
679 __ sw(src, location);
680 // Emit the write barrier code if the location is in the heap.
681 if (dst->type() == Slot::CONTEXT) {
682 __ RecordWrite(scratch1,
683 Operand(Context::SlotOffset(dst->index())),
684 scratch2,
685 src);
686 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000687}
688
689
lrn@chromium.org7516f052011-03-30 08:52:27 +0000690void FullCodeGenerator::EmitDeclaration(Variable* variable,
691 Variable::Mode mode,
692 FunctionLiteral* function) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000693 Comment cmnt(masm_, "[ Declaration");
694 ASSERT(variable != NULL); // Must have been resolved.
695 Slot* slot = variable->AsSlot();
696 Property* prop = variable->AsProperty();
697
698 if (slot != NULL) {
699 switch (slot->type()) {
700 case Slot::PARAMETER:
701 case Slot::LOCAL:
702 if (mode == Variable::CONST) {
703 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
704 __ sw(t0, MemOperand(fp, SlotOffset(slot)));
705 } else if (function != NULL) {
706 VisitForAccumulatorValue(function);
707 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
708 }
709 break;
710
711 case Slot::CONTEXT:
712 // We bypass the general EmitSlotSearch because we know more about
713 // this specific context.
714
715 // The variable in the decl always resides in the current function
716 // context.
717 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
718 if (FLAG_debug_code) {
719 // Check that we're not inside a 'with'.
720 __ lw(a1, ContextOperand(cp, Context::FCONTEXT_INDEX));
721 __ Check(eq, "Unexpected declaration in current context.",
722 a1, Operand(cp));
723 }
724 if (mode == Variable::CONST) {
725 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
726 __ sw(at, ContextOperand(cp, slot->index()));
727 // No write barrier since the_hole_value is in old space.
728 } else if (function != NULL) {
729 VisitForAccumulatorValue(function);
730 __ sw(result_register(), ContextOperand(cp, slot->index()));
731 int offset = Context::SlotOffset(slot->index());
732 // We know that we have written a function, which is not a smi.
733 __ mov(a1, cp);
734 __ RecordWrite(a1, Operand(offset), a2, result_register());
735 }
736 break;
737
738 case Slot::LOOKUP: {
739 __ li(a2, Operand(variable->name()));
740 // Declaration nodes are always introduced in one of two modes.
741 ASSERT(mode == Variable::VAR ||
742 mode == Variable::CONST);
743 PropertyAttributes attr =
744 (mode == Variable::VAR) ? NONE : READ_ONLY;
745 __ li(a1, Operand(Smi::FromInt(attr)));
746 // Push initial value, if any.
747 // Note: For variables we must not push an initial value (such as
748 // 'undefined') because we may have a (legal) redeclaration and we
749 // must not destroy the current value.
750 if (mode == Variable::CONST) {
751 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
752 __ Push(cp, a2, a1, a0);
753 } else if (function != NULL) {
754 __ Push(cp, a2, a1);
755 // Push initial value for function declaration.
756 VisitForStackValue(function);
757 } else {
758 ASSERT(Smi::FromInt(0) == 0);
759 // No initial value!
760 __ mov(a0, zero_reg); // Operand(Smi::FromInt(0)));
761 __ Push(cp, a2, a1, a0);
762 }
763 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
764 break;
765 }
766 }
767
768 } else if (prop != NULL) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000769 // A const declaration aliasing a parameter is an illegal redeclaration.
770 ASSERT(mode != Variable::CONST);
771 if (function != NULL) {
772 // We are declaring a function that rewrites to a property.
773 // Use (keyed) IC to set the initial value. We cannot visit the
774 // rewrite because it's shared and we risk recording duplicate AST
775 // IDs for bailouts from optimized code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000776 ASSERT(prop->obj()->AsVariableProxy() != NULL);
777 { AccumulatorValueContext for_object(this);
778 EmitVariableLoad(prop->obj()->AsVariableProxy()->var());
779 }
danno@chromium.org40cb8782011-05-25 07:58:50 +0000780
781 __ push(result_register());
782 VisitForAccumulatorValue(function);
783 __ mov(a0, result_register());
784 __ pop(a2);
785
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000786 ASSERT(prop->key()->AsLiteral() != NULL &&
787 prop->key()->AsLiteral()->handle()->IsSmi());
788 __ li(a1, Operand(prop->key()->AsLiteral()->handle()));
789
790 Handle<Code> ic = is_strict_mode()
791 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
792 : isolate()->builtins()->KeyedStoreIC_Initialize();
793 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
794 // Value in v0 is ignored (declarations are statements).
795 }
796 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000797}
798
799
ager@chromium.org5c838252010-02-19 08:53:10 +0000800void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000801 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
ager@chromium.org5c838252010-02-19 08:53:10 +0000802}
803
804
805void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000806 // Call the runtime to declare the globals.
807 // The context is the first argument.
808 __ li(a2, Operand(pairs));
809 __ li(a1, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
810 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
811 __ Push(cp, a2, a1, a0);
812 __ CallRuntime(Runtime::kDeclareGlobals, 4);
813 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000814}
815
816
lrn@chromium.org7516f052011-03-30 08:52:27 +0000817void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000818 Comment cmnt(masm_, "[ SwitchStatement");
819 Breakable nested_statement(this, stmt);
820 SetStatementPosition(stmt);
821
822 // Keep the switch value on the stack until a case matches.
823 VisitForStackValue(stmt->tag());
824 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
825
826 ZoneList<CaseClause*>* clauses = stmt->cases();
827 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
828
829 Label next_test; // Recycled for each test.
830 // Compile all the tests with branches to their bodies.
831 for (int i = 0; i < clauses->length(); i++) {
832 CaseClause* clause = clauses->at(i);
833 clause->body_target()->Unuse();
834
835 // The default is not a test, but remember it as final fall through.
836 if (clause->is_default()) {
837 default_clause = clause;
838 continue;
839 }
840
841 Comment cmnt(masm_, "[ Case comparison");
842 __ bind(&next_test);
843 next_test.Unuse();
844
845 // Compile the label expression.
846 VisitForAccumulatorValue(clause->label());
847 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
848
849 // Perform the comparison as if via '==='.
850 __ lw(a1, MemOperand(sp, 0)); // Switch value.
851 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
852 JumpPatchSite patch_site(masm_);
853 if (inline_smi_code) {
854 Label slow_case;
855 __ or_(a2, a1, a0);
856 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
857
858 __ Branch(&next_test, ne, a1, Operand(a0));
859 __ Drop(1); // Switch value is no longer needed.
860 __ Branch(clause->body_target());
861
862 __ bind(&slow_case);
863 }
864
865 // Record position before stub call for type feedback.
866 SetSourcePosition(clause->position());
867 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
868 EmitCallIC(ic, &patch_site, clause->CompareId());
danno@chromium.org40cb8782011-05-25 07:58:50 +0000869
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000870 __ Branch(&next_test, ne, v0, Operand(zero_reg));
871 __ Drop(1); // Switch value is no longer needed.
872 __ Branch(clause->body_target());
873 }
874
875 // Discard the test value and jump to the default if present, otherwise to
876 // the end of the statement.
877 __ bind(&next_test);
878 __ Drop(1); // Switch value is no longer needed.
879 if (default_clause == NULL) {
880 __ Branch(nested_statement.break_target());
881 } else {
882 __ Branch(default_clause->body_target());
883 }
884
885 // Compile all the case bodies.
886 for (int i = 0; i < clauses->length(); i++) {
887 Comment cmnt(masm_, "[ Case body");
888 CaseClause* clause = clauses->at(i);
889 __ bind(clause->body_target());
890 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
891 VisitStatements(clause->statements());
892 }
893
894 __ bind(nested_statement.break_target());
895 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000896}
897
898
899void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000900 Comment cmnt(masm_, "[ ForInStatement");
901 SetStatementPosition(stmt);
902
903 Label loop, exit;
904 ForIn loop_statement(this, stmt);
905 increment_loop_depth();
906
907 // Get the object to enumerate over. Both SpiderMonkey and JSC
908 // ignore null and undefined in contrast to the specification; see
909 // ECMA-262 section 12.6.4.
910 VisitForAccumulatorValue(stmt->enumerable());
911 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
912 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
913 __ Branch(&exit, eq, a0, Operand(at));
914 Register null_value = t1;
915 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
916 __ Branch(&exit, eq, a0, Operand(null_value));
917
918 // Convert the object to a JS object.
919 Label convert, done_convert;
920 __ JumpIfSmi(a0, &convert);
921 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000922 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000923 __ bind(&convert);
924 __ push(a0);
925 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
926 __ mov(a0, v0);
927 __ bind(&done_convert);
928 __ push(a0);
929
930 // Check cache validity in generated code. This is a fast case for
931 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
932 // guarantee cache validity, call the runtime system to check cache
933 // validity or get the property names in a fixed array.
934 Label next, call_runtime;
935 // Preload a couple of values used in the loop.
936 Register empty_fixed_array_value = t2;
937 __ LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
938 Register empty_descriptor_array_value = t3;
939 __ LoadRoot(empty_descriptor_array_value,
940 Heap::kEmptyDescriptorArrayRootIndex);
941 __ mov(a1, a0);
942 __ bind(&next);
943
944 // Check that there are no elements. Register a1 contains the
945 // current JS object we've reached through the prototype chain.
946 __ lw(a2, FieldMemOperand(a1, JSObject::kElementsOffset));
947 __ Branch(&call_runtime, ne, a2, Operand(empty_fixed_array_value));
948
949 // Check that instance descriptors are not empty so that we can
950 // check for an enum cache. Leave the map in a2 for the subsequent
951 // prototype load.
952 __ lw(a2, FieldMemOperand(a1, HeapObject::kMapOffset));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000953 __ lw(a3, FieldMemOperand(a2, Map::kInstanceDescriptorsOrBitField3Offset));
954 __ JumpIfSmi(a3, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000955
956 // Check that there is an enum cache in the non-empty instance
957 // descriptors (a3). This is the case if the next enumeration
958 // index field does not contain a smi.
959 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumerationIndexOffset));
960 __ JumpIfSmi(a3, &call_runtime);
961
962 // For all objects but the receiver, check that the cache is empty.
963 Label check_prototype;
964 __ Branch(&check_prototype, eq, a1, Operand(a0));
965 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumCacheBridgeCacheOffset));
966 __ Branch(&call_runtime, ne, a3, Operand(empty_fixed_array_value));
967
968 // Load the prototype from the map and loop if non-null.
969 __ bind(&check_prototype);
970 __ lw(a1, FieldMemOperand(a2, Map::kPrototypeOffset));
971 __ Branch(&next, ne, a1, Operand(null_value));
972
973 // The enum cache is valid. Load the map of the object being
974 // iterated over and use the cache for the iteration.
975 Label use_cache;
976 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
977 __ Branch(&use_cache);
978
979 // Get the set of properties to enumerate.
980 __ bind(&call_runtime);
981 __ push(a0); // Duplicate the enumerable object on the stack.
982 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
983
984 // If we got a map from the runtime call, we can do a fast
985 // modification check. Otherwise, we got a fixed array, and we have
986 // to do a slow check.
987 Label fixed_array;
988 __ mov(a2, v0);
989 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
990 __ LoadRoot(at, Heap::kMetaMapRootIndex);
991 __ Branch(&fixed_array, ne, a1, Operand(at));
992
993 // We got a map in register v0. Get the enumeration cache from it.
994 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000995 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000996 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
997 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
998
999 // Setup the four remaining stack slots.
1000 __ push(v0); // Map.
1001 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
1002 __ li(a0, Operand(Smi::FromInt(0)));
1003 // Push enumeration cache, enumeration cache length (as smi) and zero.
1004 __ Push(a2, a1, a0);
1005 __ jmp(&loop);
1006
1007 // We got a fixed array in register v0. Iterate through that.
1008 __ bind(&fixed_array);
1009 __ li(a1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
1010 __ Push(a1, v0);
1011 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1012 __ li(a0, Operand(Smi::FromInt(0)));
1013 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1014
1015 // Generate code for doing the condition check.
1016 __ bind(&loop);
1017 // Load the current count to a0, load the length to a1.
1018 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1019 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
1020 __ Branch(loop_statement.break_target(), hs, a0, Operand(a1));
1021
1022 // Get the current entry of the array into register a3.
1023 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1024 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1025 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1026 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1027 __ lw(a3, MemOperand(t0)); // Current entry.
1028
1029 // Get the expected map from the stack or a zero map in the
1030 // permanent slow case into register a2.
1031 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1032
1033 // Check if the expected map still matches that of the enumerable.
1034 // If not, we have to filter the key.
1035 Label update_each;
1036 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1037 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1038 __ Branch(&update_each, eq, t0, Operand(a2));
1039
1040 // Convert the entry to a string or (smi) 0 if it isn't a property
1041 // any more. If the property has been removed while iterating, we
1042 // just skip it.
1043 __ push(a1); // Enumerable.
1044 __ push(a3); // Current entry.
1045 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1046 __ mov(a3, result_register());
1047 __ Branch(loop_statement.continue_target(), eq, a3, Operand(zero_reg));
1048
1049 // Update the 'each' property or variable from the possibly filtered
1050 // entry in register a3.
1051 __ bind(&update_each);
1052 __ mov(result_register(), a3);
1053 // Perform the assignment as if via '='.
1054 { EffectContext context(this);
1055 EmitAssignment(stmt->each(), stmt->AssignmentId());
1056 }
1057
1058 // Generate code for the body of the loop.
1059 Visit(stmt->body());
1060
1061 // Generate code for the going to the next element by incrementing
1062 // the index (smi) stored on top of the stack.
1063 __ bind(loop_statement.continue_target());
1064 __ pop(a0);
1065 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1066 __ push(a0);
1067
1068 EmitStackCheck(stmt);
1069 __ Branch(&loop);
1070
1071 // Remove the pointers stored on the stack.
1072 __ bind(loop_statement.break_target());
1073 __ Drop(5);
1074
1075 // Exit and decrement the loop depth.
1076 __ bind(&exit);
1077 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001078}
1079
1080
1081void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1082 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001083 // Use the fast case closure allocation code that allocates in new
1084 // space for nested functions that don't need literals cloning. If
1085 // we're running with the --always-opt or the --prepare-always-opt
1086 // flag, we need to use the runtime function so that the new function
1087 // we are creating here gets a chance to have its code optimized and
1088 // doesn't just get a copy of the existing unoptimized code.
1089 if (!FLAG_always_opt &&
1090 !FLAG_prepare_always_opt &&
1091 !pretenure &&
1092 scope()->is_function_scope() &&
1093 info->num_literals() == 0) {
1094 FastNewClosureStub stub(info->strict_mode() ? kStrictMode : kNonStrictMode);
1095 __ li(a0, Operand(info));
1096 __ push(a0);
1097 __ CallStub(&stub);
1098 } else {
1099 __ li(a0, Operand(info));
1100 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1101 : Heap::kFalseValueRootIndex);
1102 __ Push(cp, a0, a1);
1103 __ CallRuntime(Runtime::kNewClosure, 3);
1104 }
1105 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001106}
1107
1108
1109void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001110 Comment cmnt(masm_, "[ VariableProxy");
1111 EmitVariableLoad(expr->var());
1112}
1113
1114
1115void FullCodeGenerator::EmitLoadGlobalSlotCheckExtensions(
1116 Slot* slot,
1117 TypeofState typeof_state,
1118 Label* slow) {
1119 Register current = cp;
1120 Register next = a1;
1121 Register temp = a2;
1122
1123 Scope* s = scope();
1124 while (s != NULL) {
1125 if (s->num_heap_slots() > 0) {
1126 if (s->calls_eval()) {
1127 // Check that extension is NULL.
1128 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1129 __ Branch(slow, ne, temp, Operand(zero_reg));
1130 }
1131 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001132 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001133 // Walk the rest of the chain without clobbering cp.
1134 current = next;
1135 }
1136 // If no outer scope calls eval, we do not need to check more
1137 // context extensions.
1138 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
1139 s = s->outer_scope();
1140 }
1141
1142 if (s->is_eval_scope()) {
1143 Label loop, fast;
1144 if (!current.is(next)) {
1145 __ Move(next, current);
1146 }
1147 __ bind(&loop);
1148 // Terminate at global context.
1149 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1150 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1151 __ Branch(&fast, eq, temp, Operand(t0));
1152 // Check that extension is NULL.
1153 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1154 __ Branch(slow, ne, temp, Operand(zero_reg));
1155 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001156 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001157 __ Branch(&loop);
1158 __ bind(&fast);
1159 }
1160
1161 __ lw(a0, GlobalObjectOperand());
1162 __ li(a2, Operand(slot->var()->name()));
1163 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1164 ? RelocInfo::CODE_TARGET
1165 : RelocInfo::CODE_TARGET_CONTEXT;
1166 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1167 EmitCallIC(ic, mode, AstNode::kNoNumber);
ager@chromium.org5c838252010-02-19 08:53:10 +00001168}
1169
1170
lrn@chromium.org7516f052011-03-30 08:52:27 +00001171MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(
1172 Slot* slot,
1173 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001174 ASSERT(slot->type() == Slot::CONTEXT);
1175 Register context = cp;
1176 Register next = a3;
1177 Register temp = t0;
1178
1179 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
1180 if (s->num_heap_slots() > 0) {
1181 if (s->calls_eval()) {
1182 // Check that extension is NULL.
1183 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1184 __ Branch(slow, ne, temp, Operand(zero_reg));
1185 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001186 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001187 // Walk the rest of the chain without clobbering cp.
1188 context = next;
1189 }
1190 }
1191 // Check that last extension is NULL.
1192 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1193 __ Branch(slow, ne, temp, Operand(zero_reg));
1194
1195 // This function is used only for loads, not stores, so it's safe to
1196 // return an cp-based operand (the write barrier cannot be allowed to
1197 // destroy the cp register).
1198 return ContextOperand(context, slot->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001199}
1200
1201
1202void FullCodeGenerator::EmitDynamicLoadFromSlotFastCase(
1203 Slot* slot,
1204 TypeofState typeof_state,
1205 Label* slow,
1206 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001207 // Generate fast-case code for variables that might be shadowed by
1208 // eval-introduced variables. Eval is used a lot without
1209 // introducing variables. In those cases, we do not want to
1210 // perform a runtime call for all variables in the scope
1211 // containing the eval.
1212 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
1213 EmitLoadGlobalSlotCheckExtensions(slot, typeof_state, slow);
1214 __ Branch(done);
1215 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
1216 Slot* potential_slot = slot->var()->local_if_not_shadowed()->AsSlot();
1217 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
1218 if (potential_slot != NULL) {
1219 // Generate fast case for locals that rewrite to slots.
1220 __ lw(v0, ContextSlotOperandCheckExtensions(potential_slot, slow));
1221 if (potential_slot->var()->mode() == Variable::CONST) {
1222 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1223 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1224 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1225 __ movz(v0, a0, at); // Conditional move.
1226 }
1227 __ Branch(done);
1228 } else if (rewrite != NULL) {
1229 // Generate fast case for calls of an argument function.
1230 Property* property = rewrite->AsProperty();
1231 if (property != NULL) {
1232 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
1233 Literal* key_literal = property->key()->AsLiteral();
1234 if (obj_proxy != NULL &&
1235 key_literal != NULL &&
1236 obj_proxy->IsArguments() &&
1237 key_literal->handle()->IsSmi()) {
1238 // Load arguments object if there are no eval-introduced
1239 // variables. Then load the argument from the arguments
1240 // object using keyed load.
1241 __ lw(a1,
1242 ContextSlotOperandCheckExtensions(obj_proxy->var()->AsSlot(),
1243 slow));
1244 __ li(a0, Operand(key_literal->handle()));
1245 Handle<Code> ic =
1246 isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001247 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(property));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001248 __ Branch(done);
1249 }
1250 }
1251 }
1252 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001253}
1254
1255
1256void FullCodeGenerator::EmitVariableLoad(Variable* var) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001257 // Four cases: non-this global variables, lookup slots, all other
1258 // types of slots, and parameters that rewrite to explicit property
1259 // accesses on the arguments object.
1260 Slot* slot = var->AsSlot();
1261 Property* property = var->AsProperty();
1262
1263 if (var->is_global() && !var->is_this()) {
1264 Comment cmnt(masm_, "Global variable");
1265 // Use inline caching. Variable name is passed in a2 and the global
1266 // object (receiver) in a0.
1267 __ lw(a0, GlobalObjectOperand());
1268 __ li(a2, Operand(var->name()));
1269 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1270 EmitCallIC(ic, RelocInfo::CODE_TARGET_CONTEXT, AstNode::kNoNumber);
1271 context()->Plug(v0);
1272
1273 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
1274 Label done, slow;
1275
1276 // Generate code for loading from variables potentially shadowed
1277 // by eval-introduced variables.
1278 EmitDynamicLoadFromSlotFastCase(slot, NOT_INSIDE_TYPEOF, &slow, &done);
1279
1280 __ bind(&slow);
1281 Comment cmnt(masm_, "Lookup slot");
1282 __ li(a1, Operand(var->name()));
1283 __ Push(cp, a1); // Context and name.
1284 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1285 __ bind(&done);
1286
1287 context()->Plug(v0);
1288
1289 } else if (slot != NULL) {
1290 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1291 ? "Context slot"
1292 : "Stack slot");
1293 if (var->mode() == Variable::CONST) {
1294 // Constants may be the hole value if they have not been initialized.
1295 // Unhole them.
1296 MemOperand slot_operand = EmitSlotSearch(slot, a0);
1297 __ lw(v0, slot_operand);
1298 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1299 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1300 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1301 __ movz(v0, a0, at); // Conditional move.
1302 context()->Plug(v0);
1303 } else {
1304 context()->Plug(slot);
1305 }
1306 } else {
1307 Comment cmnt(masm_, "Rewritten parameter");
1308 ASSERT_NOT_NULL(property);
1309 // Rewritten parameter accesses are of the form "slot[literal]".
1310 // Assert that the object is in a slot.
1311 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
1312 ASSERT_NOT_NULL(object_var);
1313 Slot* object_slot = object_var->AsSlot();
1314 ASSERT_NOT_NULL(object_slot);
1315
1316 // Load the object.
1317 Move(a1, object_slot);
1318
1319 // Assert that the key is a smi.
1320 Literal* key_literal = property->key()->AsLiteral();
1321 ASSERT_NOT_NULL(key_literal);
1322 ASSERT(key_literal->handle()->IsSmi());
1323
1324 // Load the key.
1325 __ li(a0, Operand(key_literal->handle()));
1326
1327 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1328 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001329 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(property));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001330 context()->Plug(v0);
1331 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001332}
1333
1334
1335void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001336 Comment cmnt(masm_, "[ RegExpLiteral");
1337 Label materialized;
1338 // Registers will be used as follows:
1339 // t1 = materialized value (RegExp literal)
1340 // t0 = JS function, literals array
1341 // a3 = literal index
1342 // a2 = RegExp pattern
1343 // a1 = RegExp flags
1344 // a0 = RegExp literal clone
1345 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1346 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1347 int literal_offset =
1348 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1349 __ lw(t1, FieldMemOperand(t0, literal_offset));
1350 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1351 __ Branch(&materialized, ne, t1, Operand(at));
1352
1353 // Create regexp literal using runtime function.
1354 // Result will be in v0.
1355 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1356 __ li(a2, Operand(expr->pattern()));
1357 __ li(a1, Operand(expr->flags()));
1358 __ Push(t0, a3, a2, a1);
1359 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1360 __ mov(t1, v0);
1361
1362 __ bind(&materialized);
1363 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1364 Label allocated, runtime_allocate;
1365 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1366 __ jmp(&allocated);
1367
1368 __ bind(&runtime_allocate);
1369 __ push(t1);
1370 __ li(a0, Operand(Smi::FromInt(size)));
1371 __ push(a0);
1372 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1373 __ pop(t1);
1374
1375 __ bind(&allocated);
1376
1377 // After this, registers are used as follows:
1378 // v0: Newly allocated regexp.
1379 // t1: Materialized regexp.
1380 // a2: temp.
1381 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1382 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001383}
1384
1385
1386void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001387 Comment cmnt(masm_, "[ ObjectLiteral");
1388 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1389 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1390 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1391 __ li(a1, Operand(expr->constant_properties()));
1392 int flags = expr->fast_elements()
1393 ? ObjectLiteral::kFastElements
1394 : ObjectLiteral::kNoFlags;
1395 flags |= expr->has_function()
1396 ? ObjectLiteral::kHasFunction
1397 : ObjectLiteral::kNoFlags;
1398 __ li(a0, Operand(Smi::FromInt(flags)));
1399 __ Push(a3, a2, a1, a0);
1400 if (expr->depth() > 1) {
1401 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1402 } else {
1403 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
1404 }
1405
1406 // If result_saved is true the result is on top of the stack. If
1407 // result_saved is false the result is in v0.
1408 bool result_saved = false;
1409
1410 // Mark all computed expressions that are bound to a key that
1411 // is shadowed by a later occurrence of the same key. For the
1412 // marked expressions, no store code is emitted.
1413 expr->CalculateEmitStore();
1414
1415 for (int i = 0; i < expr->properties()->length(); i++) {
1416 ObjectLiteral::Property* property = expr->properties()->at(i);
1417 if (property->IsCompileTimeValue()) continue;
1418
1419 Literal* key = property->key();
1420 Expression* value = property->value();
1421 if (!result_saved) {
1422 __ push(v0); // Save result on stack.
1423 result_saved = true;
1424 }
1425 switch (property->kind()) {
1426 case ObjectLiteral::Property::CONSTANT:
1427 UNREACHABLE();
1428 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1429 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1430 // Fall through.
1431 case ObjectLiteral::Property::COMPUTED:
1432 if (key->handle()->IsSymbol()) {
1433 if (property->emit_store()) {
1434 VisitForAccumulatorValue(value);
1435 __ mov(a0, result_register());
1436 __ li(a2, Operand(key->handle()));
1437 __ lw(a1, MemOperand(sp));
1438 Handle<Code> ic = is_strict_mode()
1439 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1440 : isolate()->builtins()->StoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001441 EmitCallIC(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001442 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1443 } else {
1444 VisitForEffect(value);
1445 }
1446 break;
1447 }
1448 // Fall through.
1449 case ObjectLiteral::Property::PROTOTYPE:
1450 // Duplicate receiver on stack.
1451 __ lw(a0, MemOperand(sp));
1452 __ push(a0);
1453 VisitForStackValue(key);
1454 VisitForStackValue(value);
1455 if (property->emit_store()) {
1456 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1457 __ push(a0);
1458 __ CallRuntime(Runtime::kSetProperty, 4);
1459 } else {
1460 __ Drop(3);
1461 }
1462 break;
1463 case ObjectLiteral::Property::GETTER:
1464 case ObjectLiteral::Property::SETTER:
1465 // Duplicate receiver on stack.
1466 __ lw(a0, MemOperand(sp));
1467 __ push(a0);
1468 VisitForStackValue(key);
1469 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1470 Smi::FromInt(1) :
1471 Smi::FromInt(0)));
1472 __ push(a1);
1473 VisitForStackValue(value);
1474 __ CallRuntime(Runtime::kDefineAccessor, 4);
1475 break;
1476 }
1477 }
1478
1479 if (expr->has_function()) {
1480 ASSERT(result_saved);
1481 __ lw(a0, MemOperand(sp));
1482 __ push(a0);
1483 __ CallRuntime(Runtime::kToFastProperties, 1);
1484 }
1485
1486 if (result_saved) {
1487 context()->PlugTOS();
1488 } else {
1489 context()->Plug(v0);
1490 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001491}
1492
1493
1494void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001495 Comment cmnt(masm_, "[ ArrayLiteral");
1496
1497 ZoneList<Expression*>* subexprs = expr->values();
1498 int length = subexprs->length();
1499 __ mov(a0, result_register());
1500 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1501 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1502 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1503 __ li(a1, Operand(expr->constant_elements()));
1504 __ Push(a3, a2, a1);
1505 if (expr->constant_elements()->map() ==
1506 isolate()->heap()->fixed_cow_array_map()) {
1507 FastCloneShallowArrayStub stub(
1508 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1509 __ CallStub(&stub);
1510 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1511 1, a1, a2);
1512 } else if (expr->depth() > 1) {
1513 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1514 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1515 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1516 } else {
1517 FastCloneShallowArrayStub stub(
1518 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
1519 __ CallStub(&stub);
1520 }
1521
1522 bool result_saved = false; // Is the result saved to the stack?
1523
1524 // Emit code to evaluate all the non-constant subexpressions and to store
1525 // them into the newly cloned array.
1526 for (int i = 0; i < length; i++) {
1527 Expression* subexpr = subexprs->at(i);
1528 // If the subexpression is a literal or a simple materialized literal it
1529 // is already set in the cloned array.
1530 if (subexpr->AsLiteral() != NULL ||
1531 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1532 continue;
1533 }
1534
1535 if (!result_saved) {
1536 __ push(v0);
1537 result_saved = true;
1538 }
1539 VisitForAccumulatorValue(subexpr);
1540
1541 // Store the subexpression value in the array's elements.
1542 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1543 __ lw(a1, FieldMemOperand(a1, JSObject::kElementsOffset));
1544 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1545 __ sw(result_register(), FieldMemOperand(a1, offset));
1546
1547 // Update the write barrier for the array store with v0 as the scratch
1548 // register.
1549 __ li(a2, Operand(offset));
1550 // TODO(PJ): double check this RecordWrite call.
1551 __ RecordWrite(a1, a2, result_register());
1552
1553 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1554 }
1555
1556 if (result_saved) {
1557 context()->PlugTOS();
1558 } else {
1559 context()->Plug(v0);
1560 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001561}
1562
1563
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001564void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001565 Comment cmnt(masm_, "[ Assignment");
1566 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1567 // on the left-hand side.
1568 if (!expr->target()->IsValidLeftHandSide()) {
1569 VisitForEffect(expr->target());
1570 return;
1571 }
1572
1573 // Left-hand side can only be a property, a global or a (parameter or local)
1574 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1575 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1576 LhsKind assign_type = VARIABLE;
1577 Property* property = expr->target()->AsProperty();
1578 if (property != NULL) {
1579 assign_type = (property->key()->IsPropertyName())
1580 ? NAMED_PROPERTY
1581 : KEYED_PROPERTY;
1582 }
1583
1584 // Evaluate LHS expression.
1585 switch (assign_type) {
1586 case VARIABLE:
1587 // Nothing to do here.
1588 break;
1589 case NAMED_PROPERTY:
1590 if (expr->is_compound()) {
1591 // We need the receiver both on the stack and in the accumulator.
1592 VisitForAccumulatorValue(property->obj());
1593 __ push(result_register());
1594 } else {
1595 VisitForStackValue(property->obj());
1596 }
1597 break;
1598 case KEYED_PROPERTY:
1599 // We need the key and receiver on both the stack and in v0 and a1.
1600 if (expr->is_compound()) {
1601 if (property->is_arguments_access()) {
1602 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
1603 __ lw(v0, EmitSlotSearch(obj_proxy->var()->AsSlot(), v0));
1604 __ push(v0);
1605 __ li(v0, Operand(property->key()->AsLiteral()->handle()));
1606 } else {
1607 VisitForStackValue(property->obj());
1608 VisitForAccumulatorValue(property->key());
1609 }
1610 __ lw(a1, MemOperand(sp, 0));
1611 __ push(v0);
1612 } else {
1613 if (property->is_arguments_access()) {
1614 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
1615 __ lw(a1, EmitSlotSearch(obj_proxy->var()->AsSlot(), v0));
1616 __ li(v0, Operand(property->key()->AsLiteral()->handle()));
1617 __ Push(a1, v0);
1618 } else {
1619 VisitForStackValue(property->obj());
1620 VisitForStackValue(property->key());
1621 }
1622 }
1623 break;
1624 }
1625
1626 // For compound assignments we need another deoptimization point after the
1627 // variable/property load.
1628 if (expr->is_compound()) {
1629 { AccumulatorValueContext context(this);
1630 switch (assign_type) {
1631 case VARIABLE:
1632 EmitVariableLoad(expr->target()->AsVariableProxy()->var());
1633 PrepareForBailout(expr->target(), TOS_REG);
1634 break;
1635 case NAMED_PROPERTY:
1636 EmitNamedPropertyLoad(property);
1637 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1638 break;
1639 case KEYED_PROPERTY:
1640 EmitKeyedPropertyLoad(property);
1641 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1642 break;
1643 }
1644 }
1645
1646 Token::Value op = expr->binary_op();
1647 __ push(v0); // Left operand goes on the stack.
1648 VisitForAccumulatorValue(expr->value());
1649
1650 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1651 ? OVERWRITE_RIGHT
1652 : NO_OVERWRITE;
1653 SetSourcePosition(expr->position() + 1);
1654 AccumulatorValueContext context(this);
1655 if (ShouldInlineSmiCase(op)) {
1656 EmitInlineSmiBinaryOp(expr->binary_operation(),
1657 op,
1658 mode,
1659 expr->target(),
1660 expr->value());
1661 } else {
1662 EmitBinaryOp(expr->binary_operation(), op, mode);
1663 }
1664
1665 // Deoptimization point in case the binary operation may have side effects.
1666 PrepareForBailout(expr->binary_operation(), TOS_REG);
1667 } else {
1668 VisitForAccumulatorValue(expr->value());
1669 }
1670
1671 // Record source position before possible IC call.
1672 SetSourcePosition(expr->position());
1673
1674 // Store the value.
1675 switch (assign_type) {
1676 case VARIABLE:
1677 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1678 expr->op());
1679 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1680 context()->Plug(v0);
1681 break;
1682 case NAMED_PROPERTY:
1683 EmitNamedPropertyAssignment(expr);
1684 break;
1685 case KEYED_PROPERTY:
1686 EmitKeyedPropertyAssignment(expr);
1687 break;
1688 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001689}
1690
1691
ager@chromium.org5c838252010-02-19 08:53:10 +00001692void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001693 SetSourcePosition(prop->position());
1694 Literal* key = prop->key()->AsLiteral();
1695 __ mov(a0, result_register());
1696 __ li(a2, Operand(key->handle()));
1697 // Call load IC. It has arguments receiver and property name a0 and a2.
1698 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001699 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001700}
1701
1702
1703void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001704 SetSourcePosition(prop->position());
1705 __ mov(a0, result_register());
1706 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1707 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001708 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001709}
1710
1711
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001712void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001713 Token::Value op,
1714 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001715 Expression* left_expr,
1716 Expression* right_expr) {
1717 Label done, smi_case, stub_call;
1718
1719 Register scratch1 = a2;
1720 Register scratch2 = a3;
1721
1722 // Get the arguments.
1723 Register left = a1;
1724 Register right = a0;
1725 __ pop(left);
1726 __ mov(a0, result_register());
1727
1728 // Perform combined smi check on both operands.
1729 __ Or(scratch1, left, Operand(right));
1730 STATIC_ASSERT(kSmiTag == 0);
1731 JumpPatchSite patch_site(masm_);
1732 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1733
1734 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001735 BinaryOpStub stub(op, mode);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001736 EmitCallIC(stub.GetCode(), &patch_site, expr->id());
1737 __ jmp(&done);
1738
1739 __ bind(&smi_case);
1740 // Smi case. This code works the same way as the smi-smi case in the type
1741 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001742 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001743 switch (op) {
1744 case Token::SAR:
1745 __ Branch(&stub_call);
1746 __ GetLeastBitsFromSmi(scratch1, right, 5);
1747 __ srav(right, left, scratch1);
1748 __ And(v0, right, Operand(~kSmiTagMask));
1749 break;
1750 case Token::SHL: {
1751 __ Branch(&stub_call);
1752 __ SmiUntag(scratch1, left);
1753 __ GetLeastBitsFromSmi(scratch2, right, 5);
1754 __ sllv(scratch1, scratch1, scratch2);
1755 __ Addu(scratch2, scratch1, Operand(0x40000000));
1756 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1757 __ SmiTag(v0, scratch1);
1758 break;
1759 }
1760 case Token::SHR: {
1761 __ Branch(&stub_call);
1762 __ SmiUntag(scratch1, left);
1763 __ GetLeastBitsFromSmi(scratch2, right, 5);
1764 __ srlv(scratch1, scratch1, scratch2);
1765 __ And(scratch2, scratch1, 0xc0000000);
1766 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1767 __ SmiTag(v0, scratch1);
1768 break;
1769 }
1770 case Token::ADD:
1771 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1772 __ BranchOnOverflow(&stub_call, scratch1);
1773 break;
1774 case Token::SUB:
1775 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1776 __ BranchOnOverflow(&stub_call, scratch1);
1777 break;
1778 case Token::MUL: {
1779 __ SmiUntag(scratch1, right);
1780 __ Mult(left, scratch1);
1781 __ mflo(scratch1);
1782 __ mfhi(scratch2);
1783 __ sra(scratch1, scratch1, 31);
1784 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1785 __ mflo(v0);
1786 __ Branch(&done, ne, v0, Operand(zero_reg));
1787 __ Addu(scratch2, right, left);
1788 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1789 ASSERT(Smi::FromInt(0) == 0);
1790 __ mov(v0, zero_reg);
1791 break;
1792 }
1793 case Token::BIT_OR:
1794 __ Or(v0, left, Operand(right));
1795 break;
1796 case Token::BIT_AND:
1797 __ And(v0, left, Operand(right));
1798 break;
1799 case Token::BIT_XOR:
1800 __ Xor(v0, left, Operand(right));
1801 break;
1802 default:
1803 UNREACHABLE();
1804 }
1805
1806 __ bind(&done);
1807 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001808}
1809
1810
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001811void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1812 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001813 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001814 __ mov(a0, result_register());
1815 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001816 BinaryOpStub stub(op, mode);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001817 EmitCallIC(stub.GetCode(), NULL, expr->id());
1818 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001819}
1820
1821
1822void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001823 // Invalid left-hand sides are rewritten to have a 'throw
1824 // ReferenceError' on the left-hand side.
1825 if (!expr->IsValidLeftHandSide()) {
1826 VisitForEffect(expr);
1827 return;
1828 }
1829
1830 // Left-hand side can only be a property, a global or a (parameter or local)
1831 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1832 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1833 LhsKind assign_type = VARIABLE;
1834 Property* prop = expr->AsProperty();
1835 if (prop != NULL) {
1836 assign_type = (prop->key()->IsPropertyName())
1837 ? NAMED_PROPERTY
1838 : KEYED_PROPERTY;
1839 }
1840
1841 switch (assign_type) {
1842 case VARIABLE: {
1843 Variable* var = expr->AsVariableProxy()->var();
1844 EffectContext context(this);
1845 EmitVariableAssignment(var, Token::ASSIGN);
1846 break;
1847 }
1848 case NAMED_PROPERTY: {
1849 __ push(result_register()); // Preserve value.
1850 VisitForAccumulatorValue(prop->obj());
1851 __ mov(a1, result_register());
1852 __ pop(a0); // Restore value.
1853 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1854 Handle<Code> ic = is_strict_mode()
1855 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1856 : isolate()->builtins()->StoreIC_Initialize();
1857 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
1858 break;
1859 }
1860 case KEYED_PROPERTY: {
1861 __ push(result_register()); // Preserve value.
1862 if (prop->is_synthetic()) {
1863 ASSERT(prop->obj()->AsVariableProxy() != NULL);
1864 ASSERT(prop->key()->AsLiteral() != NULL);
1865 { AccumulatorValueContext for_object(this);
1866 EmitVariableLoad(prop->obj()->AsVariableProxy()->var());
1867 }
1868 __ mov(a2, result_register());
1869 __ li(a1, Operand(prop->key()->AsLiteral()->handle()));
1870 } else {
1871 VisitForStackValue(prop->obj());
1872 VisitForAccumulatorValue(prop->key());
1873 __ mov(a1, result_register());
1874 __ pop(a2);
1875 }
1876 __ pop(a0); // Restore value.
1877 Handle<Code> ic = is_strict_mode()
1878 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
1879 : isolate()->builtins()->KeyedStoreIC_Initialize();
1880 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
1881 break;
1882 }
1883 }
1884 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1885 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001886}
1887
1888
1889void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001890 Token::Value op) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001891 // Left-hand sides that rewrite to explicit property accesses do not reach
1892 // here.
1893 ASSERT(var != NULL);
1894 ASSERT(var->is_global() || var->AsSlot() != NULL);
1895
1896 if (var->is_global()) {
1897 ASSERT(!var->is_this());
1898 // Assignment to a global variable. Use inline caching for the
1899 // assignment. Right-hand-side value is passed in a0, variable name in
1900 // a2, and the global object in a1.
1901 __ mov(a0, result_register());
1902 __ li(a2, Operand(var->name()));
1903 __ lw(a1, GlobalObjectOperand());
1904 Handle<Code> ic = is_strict_mode()
1905 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1906 : isolate()->builtins()->StoreIC_Initialize();
1907 EmitCallIC(ic, RelocInfo::CODE_TARGET_CONTEXT, AstNode::kNoNumber);
1908
1909 } else if (op == Token::INIT_CONST) {
1910 // Like var declarations, const declarations are hoisted to function
1911 // scope. However, unlike var initializers, const initializers are able
1912 // to drill a hole to that function context, even from inside a 'with'
1913 // context. We thus bypass the normal static scope lookup.
1914 Slot* slot = var->AsSlot();
1915 Label skip;
1916 switch (slot->type()) {
1917 case Slot::PARAMETER:
1918 // No const parameters.
1919 UNREACHABLE();
1920 break;
1921 case Slot::LOCAL:
1922 // Detect const reinitialization by checking for the hole value.
1923 __ lw(a1, MemOperand(fp, SlotOffset(slot)));
1924 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1925 __ Branch(&skip, ne, a1, Operand(t0));
1926 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1927 break;
1928 case Slot::CONTEXT: {
1929 __ lw(a1, ContextOperand(cp, Context::FCONTEXT_INDEX));
1930 __ lw(a2, ContextOperand(a1, slot->index()));
1931 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1932 __ Branch(&skip, ne, a2, Operand(t0));
1933 __ sw(result_register(), ContextOperand(a1, slot->index()));
1934 int offset = Context::SlotOffset(slot->index());
1935 __ mov(a3, result_register()); // Preserve the stored value in v0.
1936 __ RecordWrite(a1, Operand(offset), a3, a2);
1937 break;
1938 }
1939 case Slot::LOOKUP:
1940 __ push(result_register());
1941 __ li(a0, Operand(slot->var()->name()));
1942 __ Push(cp, a0); // Context and name.
1943 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1944 break;
1945 }
1946 __ bind(&skip);
1947
1948 } else if (var->mode() != Variable::CONST) {
1949 // Perform the assignment for non-const variables. Const assignments
1950 // are simply skipped.
1951 Slot* slot = var->AsSlot();
1952 switch (slot->type()) {
1953 case Slot::PARAMETER:
1954 case Slot::LOCAL:
1955 // Perform the assignment.
1956 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1957 break;
1958
1959 case Slot::CONTEXT: {
1960 MemOperand target = EmitSlotSearch(slot, a1);
1961 // Perform the assignment and issue the write barrier.
1962 __ sw(result_register(), target);
1963 // RecordWrite may destroy all its register arguments.
1964 __ mov(a3, result_register());
1965 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
1966 __ RecordWrite(a1, Operand(offset), a2, a3);
1967 break;
1968 }
1969
1970 case Slot::LOOKUP:
1971 // Call the runtime for the assignment.
1972 __ push(v0); // Value.
1973 __ li(a1, Operand(slot->var()->name()));
1974 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1975 __ Push(cp, a1, a0); // Context, name, strict mode.
1976 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1977 break;
1978 }
1979 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001980}
1981
1982
1983void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001984 // Assignment to a property, using a named store IC.
1985 Property* prop = expr->target()->AsProperty();
1986 ASSERT(prop != NULL);
1987 ASSERT(prop->key()->AsLiteral() != NULL);
1988
1989 // If the assignment starts a block of assignments to the same object,
1990 // change to slow case to avoid the quadratic behavior of repeatedly
1991 // adding fast properties.
1992 if (expr->starts_initialization_block()) {
1993 __ push(result_register());
1994 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1995 __ push(t0);
1996 __ CallRuntime(Runtime::kToSlowProperties, 1);
1997 __ pop(result_register());
1998 }
1999
2000 // Record source code position before IC call.
2001 SetSourcePosition(expr->position());
2002 __ mov(a0, result_register()); // Load the value.
2003 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
2004 // Load receiver to a1. Leave a copy in the stack if needed for turning the
2005 // receiver into fast case.
2006 if (expr->ends_initialization_block()) {
2007 __ lw(a1, MemOperand(sp));
2008 } else {
2009 __ pop(a1);
2010 }
2011
2012 Handle<Code> ic = is_strict_mode()
2013 ? isolate()->builtins()->StoreIC_Initialize_Strict()
2014 : isolate()->builtins()->StoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00002015 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002016
2017 // If the assignment ends an initialization block, revert to fast case.
2018 if (expr->ends_initialization_block()) {
2019 __ push(v0); // Result of assignment, saved even if not needed.
2020 // Receiver is under the result value.
2021 __ lw(t0, MemOperand(sp, kPointerSize));
2022 __ push(t0);
2023 __ CallRuntime(Runtime::kToFastProperties, 1);
2024 __ pop(v0);
2025 __ Drop(1);
2026 }
2027 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2028 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002029}
2030
2031
2032void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002033 // Assignment to a property, using a keyed store IC.
2034
2035 // If the assignment starts a block of assignments to the same object,
2036 // change to slow case to avoid the quadratic behavior of repeatedly
2037 // adding fast properties.
2038 if (expr->starts_initialization_block()) {
2039 __ push(result_register());
2040 // Receiver is now under the key and value.
2041 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2042 __ push(t0);
2043 __ CallRuntime(Runtime::kToSlowProperties, 1);
2044 __ pop(result_register());
2045 }
2046
2047 // Record source code position before IC call.
2048 SetSourcePosition(expr->position());
2049 // Call keyed store IC.
2050 // The arguments are:
2051 // - a0 is the value,
2052 // - a1 is the key,
2053 // - a2 is the receiver.
2054 __ mov(a0, result_register());
2055 __ pop(a1); // Key.
2056 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2057 // receiver into fast case.
2058 if (expr->ends_initialization_block()) {
2059 __ lw(a2, MemOperand(sp));
2060 } else {
2061 __ pop(a2);
2062 }
2063
2064 Handle<Code> ic = is_strict_mode()
2065 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
2066 : isolate()->builtins()->KeyedStoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00002067 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002068
2069 // If the assignment ends an initialization block, revert to fast case.
2070 if (expr->ends_initialization_block()) {
2071 __ push(v0); // Result of assignment, saved even if not needed.
2072 // Receiver is under the result value.
2073 __ lw(t0, MemOperand(sp, kPointerSize));
2074 __ push(t0);
2075 __ CallRuntime(Runtime::kToFastProperties, 1);
2076 __ pop(v0);
2077 __ Drop(1);
2078 }
2079 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2080 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002081}
2082
2083
2084void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002085 Comment cmnt(masm_, "[ Property");
2086 Expression* key = expr->key();
2087
2088 if (key->IsPropertyName()) {
2089 VisitForAccumulatorValue(expr->obj());
2090 EmitNamedPropertyLoad(expr);
2091 context()->Plug(v0);
2092 } else {
2093 VisitForStackValue(expr->obj());
2094 VisitForAccumulatorValue(expr->key());
2095 __ pop(a1);
2096 EmitKeyedPropertyLoad(expr);
2097 context()->Plug(v0);
2098 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002099}
2100
lrn@chromium.org7516f052011-03-30 08:52:27 +00002101
ager@chromium.org5c838252010-02-19 08:53:10 +00002102void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002103 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002104 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002105 // Code common for calls using the IC.
2106 ZoneList<Expression*>* args = expr->arguments();
2107 int arg_count = args->length();
2108 { PreservePositionScope scope(masm()->positions_recorder());
2109 for (int i = 0; i < arg_count; i++) {
2110 VisitForStackValue(args->at(i));
2111 }
2112 __ li(a2, Operand(name));
2113 }
2114 // Record source position for debugger.
2115 SetSourcePosition(expr->position());
2116 // Call the IC initialization code.
2117 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2118 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00002119 isolate()->stub_cache()->ComputeCallInitialize(arg_count, in_loop, mode);
2120 EmitCallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002121 RecordJSReturnSite(expr);
2122 // Restore context register.
2123 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2124 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002125}
2126
2127
lrn@chromium.org7516f052011-03-30 08:52:27 +00002128void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002129 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002130 // Load the key.
2131 VisitForAccumulatorValue(key);
2132
2133 // Swap the name of the function and the receiver on the stack to follow
2134 // the calling convention for call ICs.
2135 __ pop(a1);
2136 __ push(v0);
2137 __ push(a1);
2138
2139 // Code common for calls using the IC.
2140 ZoneList<Expression*>* args = expr->arguments();
2141 int arg_count = args->length();
2142 { PreservePositionScope scope(masm()->positions_recorder());
2143 for (int i = 0; i < arg_count; i++) {
2144 VisitForStackValue(args->at(i));
2145 }
2146 }
2147 // Record source position for debugger.
2148 SetSourcePosition(expr->position());
2149 // Call the IC initialization code.
2150 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2151 Handle<Code> ic =
2152 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count, in_loop);
2153 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002154 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002155 RecordJSReturnSite(expr);
2156 // Restore context register.
2157 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2158 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002159}
2160
2161
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002162void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002163 // Code common for calls using the call stub.
2164 ZoneList<Expression*>* args = expr->arguments();
2165 int arg_count = args->length();
2166 { PreservePositionScope scope(masm()->positions_recorder());
2167 for (int i = 0; i < arg_count; i++) {
2168 VisitForStackValue(args->at(i));
2169 }
2170 }
2171 // Record source position for debugger.
2172 SetSourcePosition(expr->position());
2173 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2174 CallFunctionStub stub(arg_count, in_loop, flags);
2175 __ CallStub(&stub);
2176 RecordJSReturnSite(expr);
2177 // Restore context register.
2178 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2179 context()->DropAndPlug(1, v0);
2180}
2181
2182
2183void FullCodeGenerator::EmitResolvePossiblyDirectEval(ResolveEvalFlag flag,
2184 int arg_count) {
2185 // Push copy of the first argument or undefined if it doesn't exist.
2186 if (arg_count > 0) {
2187 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2188 } else {
2189 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2190 }
2191 __ push(a1);
2192
2193 // Push the receiver of the enclosing function and do runtime call.
2194 __ lw(a1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
2195 __ push(a1);
2196 // Push the strict mode flag.
2197 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
2198 __ push(a1);
2199
2200 __ CallRuntime(flag == SKIP_CONTEXT_LOOKUP
2201 ? Runtime::kResolvePossiblyDirectEvalNoLookup
2202 : Runtime::kResolvePossiblyDirectEval, 4);
ager@chromium.org5c838252010-02-19 08:53:10 +00002203}
2204
2205
2206void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002207#ifdef DEBUG
2208 // We want to verify that RecordJSReturnSite gets called on all paths
2209 // through this function. Avoid early returns.
2210 expr->return_is_recorded_ = false;
2211#endif
2212
2213 Comment cmnt(masm_, "[ Call");
2214 Expression* fun = expr->expression();
2215 Variable* var = fun->AsVariableProxy()->AsVariable();
2216
2217 if (var != NULL && var->is_possibly_eval()) {
2218 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2219 // resolve the function we need to call and the receiver of the
2220 // call. Then we call the resolved function using the given
2221 // arguments.
2222 ZoneList<Expression*>* args = expr->arguments();
2223 int arg_count = args->length();
2224
2225 { PreservePositionScope pos_scope(masm()->positions_recorder());
2226 VisitForStackValue(fun);
2227 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2228 __ push(a2); // Reserved receiver slot.
2229
2230 // Push the arguments.
2231 for (int i = 0; i < arg_count; i++) {
2232 VisitForStackValue(args->at(i));
2233 }
2234 // If we know that eval can only be shadowed by eval-introduced
2235 // variables we attempt to load the global eval function directly
2236 // in generated code. If we succeed, there is no need to perform a
2237 // context lookup in the runtime system.
2238 Label done;
2239 if (var->AsSlot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
2240 Label slow;
2241 EmitLoadGlobalSlotCheckExtensions(var->AsSlot(),
2242 NOT_INSIDE_TYPEOF,
2243 &slow);
2244 // Push the function and resolve eval.
2245 __ push(v0);
2246 EmitResolvePossiblyDirectEval(SKIP_CONTEXT_LOOKUP, arg_count);
2247 __ jmp(&done);
2248 __ bind(&slow);
2249 }
2250
2251 // Push copy of the function (found below the arguments) and
2252 // resolve eval.
2253 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2254 __ push(a1);
2255 EmitResolvePossiblyDirectEval(PERFORM_CONTEXT_LOOKUP, arg_count);
2256 if (done.is_linked()) {
2257 __ bind(&done);
2258 }
2259
2260 // The runtime call returns a pair of values in v0 (function) and
2261 // v1 (receiver). Touch up the stack with the right values.
2262 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2263 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2264 }
2265 // Record source position for debugger.
2266 SetSourcePosition(expr->position());
2267 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002268 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002269 __ CallStub(&stub);
2270 RecordJSReturnSite(expr);
2271 // Restore context register.
2272 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2273 context()->DropAndPlug(1, v0);
2274 } else if (var != NULL && !var->is_this() && var->is_global()) {
2275 // Push global object as receiver for the call IC.
2276 __ lw(a0, GlobalObjectOperand());
2277 __ push(a0);
2278 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
2279 } else if (var != NULL && var->AsSlot() != NULL &&
2280 var->AsSlot()->type() == Slot::LOOKUP) {
2281 // Call to a lookup slot (dynamically introduced variable).
2282 Label slow, done;
2283
2284 { PreservePositionScope scope(masm()->positions_recorder());
2285 // Generate code for loading from variables potentially shadowed
2286 // by eval-introduced variables.
2287 EmitDynamicLoadFromSlotFastCase(var->AsSlot(),
2288 NOT_INSIDE_TYPEOF,
2289 &slow,
2290 &done);
2291 }
2292
2293 __ bind(&slow);
2294 // Call the runtime to find the function to call (returned in v0)
2295 // and the object holding it (returned in v1).
2296 __ push(context_register());
2297 __ li(a2, Operand(var->name()));
2298 __ push(a2);
2299 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2300 __ Push(v0, v1); // Function, receiver.
2301
2302 // If fast case code has been generated, emit code to push the
2303 // function and receiver and have the slow path jump around this
2304 // code.
2305 if (done.is_linked()) {
2306 Label call;
2307 __ Branch(&call);
2308 __ bind(&done);
2309 // Push function.
2310 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002311 // The receiver is implicitly the global receiver. Indicate this
2312 // by passing the hole to the call function stub.
2313 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002314 __ push(a1);
2315 __ bind(&call);
2316 }
2317
danno@chromium.org40cb8782011-05-25 07:58:50 +00002318 // The receiver is either the global receiver or an object found
2319 // by LoadContextSlot. That object could be the hole if the
2320 // receiver is implicitly the global object.
2321 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002322 } else if (fun->AsProperty() != NULL) {
2323 // Call to an object property.
2324 Property* prop = fun->AsProperty();
2325 Literal* key = prop->key()->AsLiteral();
2326 if (key != NULL && key->handle()->IsSymbol()) {
2327 // Call to a named property, use call IC.
2328 { PreservePositionScope scope(masm()->positions_recorder());
2329 VisitForStackValue(prop->obj());
2330 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00002331 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002332 } else {
2333 // Call to a keyed property.
2334 // For a synthetic property use keyed load IC followed by function call,
2335 // for a regular property use keyed EmitCallIC.
2336 if (prop->is_synthetic()) {
2337 // Do not visit the object and key subexpressions (they are shared
2338 // by all occurrences of the same rewritten parameter).
2339 ASSERT(prop->obj()->AsVariableProxy() != NULL);
2340 ASSERT(prop->obj()->AsVariableProxy()->var()->AsSlot() != NULL);
2341 Slot* slot = prop->obj()->AsVariableProxy()->var()->AsSlot();
2342 MemOperand operand = EmitSlotSearch(slot, a1);
2343 __ lw(a1, operand);
2344
2345 ASSERT(prop->key()->AsLiteral() != NULL);
2346 ASSERT(prop->key()->AsLiteral()->handle()->IsSmi());
2347 __ li(a0, Operand(prop->key()->AsLiteral()->handle()));
2348
2349 // Record source code position for IC call.
2350 SetSourcePosition(prop->position());
2351
2352 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00002353 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002354 __ lw(a1, GlobalObjectOperand());
2355 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2356 __ Push(v0, a1); // Function, receiver.
2357 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2358 } else {
2359 { PreservePositionScope scope(masm()->positions_recorder());
2360 VisitForStackValue(prop->obj());
2361 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00002362 EmitKeyedCallWithIC(expr, prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002363 }
2364 }
2365 } else {
2366 { PreservePositionScope scope(masm()->positions_recorder());
2367 VisitForStackValue(fun);
2368 }
2369 // Load global receiver object.
2370 __ lw(a1, GlobalObjectOperand());
2371 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2372 __ push(a1);
2373 // Emit function call.
2374 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2375 }
2376
2377#ifdef DEBUG
2378 // RecordJSReturnSite should have been called.
2379 ASSERT(expr->return_is_recorded_);
2380#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002381}
2382
2383
2384void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002385 Comment cmnt(masm_, "[ CallNew");
2386 // According to ECMA-262, section 11.2.2, page 44, the function
2387 // expression in new calls must be evaluated before the
2388 // arguments.
2389
2390 // Push constructor on the stack. If it's not a function it's used as
2391 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2392 // ignored.
2393 VisitForStackValue(expr->expression());
2394
2395 // Push the arguments ("left-to-right") on the stack.
2396 ZoneList<Expression*>* args = expr->arguments();
2397 int arg_count = args->length();
2398 for (int i = 0; i < arg_count; i++) {
2399 VisitForStackValue(args->at(i));
2400 }
2401
2402 // Call the construct call builtin that handles allocation and
2403 // constructor invocation.
2404 SetSourcePosition(expr->position());
2405
2406 // Load function and argument count into a1 and a0.
2407 __ li(a0, Operand(arg_count));
2408 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2409
2410 Handle<Code> construct_builtin =
2411 isolate()->builtins()->JSConstructCall();
2412 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2413 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002414}
2415
2416
lrn@chromium.org7516f052011-03-30 08:52:27 +00002417void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002418 ASSERT(args->length() == 1);
2419
2420 VisitForAccumulatorValue(args->at(0));
2421
2422 Label materialize_true, materialize_false;
2423 Label* if_true = NULL;
2424 Label* if_false = NULL;
2425 Label* fall_through = NULL;
2426 context()->PrepareTest(&materialize_true, &materialize_false,
2427 &if_true, &if_false, &fall_through);
2428
2429 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2430 __ And(t0, v0, Operand(kSmiTagMask));
2431 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2432
2433 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002434}
2435
2436
2437void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002438 ASSERT(args->length() == 1);
2439
2440 VisitForAccumulatorValue(args->at(0));
2441
2442 Label materialize_true, materialize_false;
2443 Label* if_true = NULL;
2444 Label* if_false = NULL;
2445 Label* fall_through = NULL;
2446 context()->PrepareTest(&materialize_true, &materialize_false,
2447 &if_true, &if_false, &fall_through);
2448
2449 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2450 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2451 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2452
2453 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002454}
2455
2456
2457void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002458 ASSERT(args->length() == 1);
2459
2460 VisitForAccumulatorValue(args->at(0));
2461
2462 Label materialize_true, materialize_false;
2463 Label* if_true = NULL;
2464 Label* if_false = NULL;
2465 Label* fall_through = NULL;
2466 context()->PrepareTest(&materialize_true, &materialize_false,
2467 &if_true, &if_false, &fall_through);
2468
2469 __ JumpIfSmi(v0, if_false);
2470 __ LoadRoot(at, Heap::kNullValueRootIndex);
2471 __ Branch(if_true, eq, v0, Operand(at));
2472 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2473 // Undetectable objects behave like undefined when tested with typeof.
2474 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2475 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2476 __ Branch(if_false, ne, at, Operand(zero_reg));
2477 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002478 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002479 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002480 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2481 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002482
2483 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002484}
2485
2486
2487void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002488 ASSERT(args->length() == 1);
2489
2490 VisitForAccumulatorValue(args->at(0));
2491
2492 Label materialize_true, materialize_false;
2493 Label* if_true = NULL;
2494 Label* if_false = NULL;
2495 Label* fall_through = NULL;
2496 context()->PrepareTest(&materialize_true, &materialize_false,
2497 &if_true, &if_false, &fall_through);
2498
2499 __ JumpIfSmi(v0, if_false);
2500 __ GetObjectType(v0, a1, a1);
2501 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002502 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002503 if_true, if_false, fall_through);
2504
2505 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002506}
2507
2508
2509void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002510 ASSERT(args->length() == 1);
2511
2512 VisitForAccumulatorValue(args->at(0));
2513
2514 Label materialize_true, materialize_false;
2515 Label* if_true = NULL;
2516 Label* if_false = NULL;
2517 Label* fall_through = NULL;
2518 context()->PrepareTest(&materialize_true, &materialize_false,
2519 &if_true, &if_false, &fall_through);
2520
2521 __ JumpIfSmi(v0, if_false);
2522 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2523 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2524 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2525 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2526 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2527
2528 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002529}
2530
2531
2532void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2533 ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002534
2535 ASSERT(args->length() == 1);
2536
2537 VisitForAccumulatorValue(args->at(0));
2538
2539 Label materialize_true, materialize_false;
2540 Label* if_true = NULL;
2541 Label* if_false = NULL;
2542 Label* fall_through = NULL;
2543 context()->PrepareTest(&materialize_true, &materialize_false,
2544 &if_true, &if_false, &fall_through);
2545
2546 if (FLAG_debug_code) __ AbortIfSmi(v0);
2547
2548 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2549 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2550 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2551 __ Branch(if_true, ne, t0, Operand(zero_reg));
2552
2553 // Check for fast case object. Generate false result for slow case object.
2554 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2555 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2556 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2557 __ Branch(if_false, eq, a2, Operand(t0));
2558
2559 // Look for valueOf symbol in the descriptor array, and indicate false if
2560 // found. The type is not checked, so if it is a transition it is a false
2561 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002562 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002563 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2564 // t0: descriptor array
2565 // a3: length of descriptor array
2566 // Calculate the end of the descriptor array.
2567 STATIC_ASSERT(kSmiTag == 0);
2568 STATIC_ASSERT(kSmiTagSize == 1);
2569 STATIC_ASSERT(kPointerSize == 4);
2570 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2571 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2572 __ Addu(a2, a2, t1);
2573
2574 // Calculate location of the first key name.
2575 __ Addu(t0,
2576 t0,
2577 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2578 DescriptorArray::kFirstIndex * kPointerSize));
2579 // Loop through all the keys in the descriptor array. If one of these is the
2580 // symbol valueOf the result is false.
2581 Label entry, loop;
2582 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2583 // used in the loop below.
2584 __ li(t2, Operand(FACTORY->value_of_symbol()));
2585 __ jmp(&entry);
2586 __ bind(&loop);
2587 __ lw(a3, MemOperand(t0, 0));
2588 __ Branch(if_false, eq, a3, Operand(t2));
2589 __ Addu(t0, t0, Operand(kPointerSize));
2590 __ bind(&entry);
2591 __ Branch(&loop, ne, t0, Operand(a2));
2592
2593 // If a valueOf property is not found on the object check that it's
2594 // prototype is the un-modified String prototype. If not result is false.
2595 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2596 __ JumpIfSmi(a2, if_false);
2597 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2598 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2599 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2600 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2601 __ Branch(if_false, ne, a2, Operand(a3));
2602
2603 // Set the bit in the map to indicate that it has been checked safe for
2604 // default valueOf and set true result.
2605 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2606 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2607 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2608 __ jmp(if_true);
2609
2610 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2611 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002612}
2613
2614
2615void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002616 ASSERT(args->length() == 1);
2617
2618 VisitForAccumulatorValue(args->at(0));
2619
2620 Label materialize_true, materialize_false;
2621 Label* if_true = NULL;
2622 Label* if_false = NULL;
2623 Label* fall_through = NULL;
2624 context()->PrepareTest(&materialize_true, &materialize_false,
2625 &if_true, &if_false, &fall_through);
2626
2627 __ JumpIfSmi(v0, if_false);
2628 __ GetObjectType(v0, a1, a2);
2629 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2630 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2631 __ Branch(if_false);
2632
2633 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002634}
2635
2636
2637void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002638 ASSERT(args->length() == 1);
2639
2640 VisitForAccumulatorValue(args->at(0));
2641
2642 Label materialize_true, materialize_false;
2643 Label* if_true = NULL;
2644 Label* if_false = NULL;
2645 Label* fall_through = NULL;
2646 context()->PrepareTest(&materialize_true, &materialize_false,
2647 &if_true, &if_false, &fall_through);
2648
2649 __ JumpIfSmi(v0, if_false);
2650 __ GetObjectType(v0, a1, a1);
2651 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2652 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2653 if_true, if_false, fall_through);
2654
2655 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002656}
2657
2658
2659void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002660 ASSERT(args->length() == 1);
2661
2662 VisitForAccumulatorValue(args->at(0));
2663
2664 Label materialize_true, materialize_false;
2665 Label* if_true = NULL;
2666 Label* if_false = NULL;
2667 Label* fall_through = NULL;
2668 context()->PrepareTest(&materialize_true, &materialize_false,
2669 &if_true, &if_false, &fall_through);
2670
2671 __ JumpIfSmi(v0, if_false);
2672 __ GetObjectType(v0, a1, a1);
2673 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2674 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2675
2676 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002677}
2678
2679
2680void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002681 ASSERT(args->length() == 0);
2682
2683 Label materialize_true, materialize_false;
2684 Label* if_true = NULL;
2685 Label* if_false = NULL;
2686 Label* fall_through = NULL;
2687 context()->PrepareTest(&materialize_true, &materialize_false,
2688 &if_true, &if_false, &fall_through);
2689
2690 // Get the frame pointer for the calling frame.
2691 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2692
2693 // Skip the arguments adaptor frame if it exists.
2694 Label check_frame_marker;
2695 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2696 __ Branch(&check_frame_marker, ne,
2697 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2698 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2699
2700 // Check the marker in the calling frame.
2701 __ bind(&check_frame_marker);
2702 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
2703 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2704 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2705 if_true, if_false, fall_through);
2706
2707 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002708}
2709
2710
2711void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002712 ASSERT(args->length() == 2);
2713
2714 // Load the two objects into registers and perform the comparison.
2715 VisitForStackValue(args->at(0));
2716 VisitForAccumulatorValue(args->at(1));
2717
2718 Label materialize_true, materialize_false;
2719 Label* if_true = NULL;
2720 Label* if_false = NULL;
2721 Label* fall_through = NULL;
2722 context()->PrepareTest(&materialize_true, &materialize_false,
2723 &if_true, &if_false, &fall_through);
2724
2725 __ pop(a1);
2726 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2727 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2728
2729 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002730}
2731
2732
2733void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002734 ASSERT(args->length() == 1);
2735
2736 // ArgumentsAccessStub expects the key in a1 and the formal
2737 // parameter count in a0.
2738 VisitForAccumulatorValue(args->at(0));
2739 __ mov(a1, v0);
2740 __ li(a0, Operand(Smi::FromInt(scope()->num_parameters())));
2741 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2742 __ CallStub(&stub);
2743 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002744}
2745
2746
2747void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002748 ASSERT(args->length() == 0);
2749
2750 Label exit;
2751 // Get the number of formal parameters.
2752 __ li(v0, Operand(Smi::FromInt(scope()->num_parameters())));
2753
2754 // Check if the calling frame is an arguments adaptor frame.
2755 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2756 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2757 __ Branch(&exit, ne, a3,
2758 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2759
2760 // Arguments adaptor case: Read the arguments length from the
2761 // adaptor frame.
2762 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2763
2764 __ bind(&exit);
2765 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002766}
2767
2768
2769void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002770 ASSERT(args->length() == 1);
2771 Label done, null, function, non_function_constructor;
2772
2773 VisitForAccumulatorValue(args->at(0));
2774
2775 // If the object is a smi, we return null.
2776 __ JumpIfSmi(v0, &null);
2777
2778 // Check that the object is a JS object but take special care of JS
2779 // functions to make sure they have 'Function' as their class.
2780 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002781 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002782
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002783 // As long as LAST_CALLABLE_SPEC_OBJECT_TYPE is the last instance type, and
2784 // FIRST_CALLABLE_SPEC_OBJECT_TYPE comes right after
2785 // LAST_NONCALLABLE_SPEC_OBJECT_TYPE, we can avoid checking for the latter.
2786 STATIC_ASSERT(LAST_TYPE == LAST_CALLABLE_SPEC_OBJECT_TYPE);
2787 STATIC_ASSERT(FIRST_CALLABLE_SPEC_OBJECT_TYPE ==
2788 LAST_NONCALLABLE_SPEC_OBJECT_TYPE + 1);
2789 __ Branch(&function, ge, a1, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002790
2791 // Check if the constructor in the map is a function.
2792 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2793 __ GetObjectType(v0, a1, a1);
2794 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2795
2796 // v0 now contains the constructor function. Grab the
2797 // instance class name from there.
2798 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2799 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2800 __ Branch(&done);
2801
2802 // Functions have class 'Function'.
2803 __ bind(&function);
2804 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2805 __ jmp(&done);
2806
2807 // Objects with a non-function constructor have class 'Object'.
2808 __ bind(&non_function_constructor);
2809 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2810 __ jmp(&done);
2811
2812 // Non-JS objects have class null.
2813 __ bind(&null);
2814 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2815
2816 // All done.
2817 __ bind(&done);
2818
2819 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002820}
2821
2822
2823void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002824 // Conditionally generate a log call.
2825 // Args:
2826 // 0 (literal string): The type of logging (corresponds to the flags).
2827 // This is used to determine whether or not to generate the log call.
2828 // 1 (string): Format string. Access the string at argument index 2
2829 // with '%2s' (see Logger::LogRuntime for all the formats).
2830 // 2 (array): Arguments to the format string.
2831 ASSERT_EQ(args->length(), 3);
2832#ifdef ENABLE_LOGGING_AND_PROFILING
2833 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2834 VisitForStackValue(args->at(1));
2835 VisitForStackValue(args->at(2));
2836 __ CallRuntime(Runtime::kLog, 2);
2837 }
2838#endif
2839 // Finally, we're expected to leave a value on the top of the stack.
2840 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2841 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002842}
2843
2844
2845void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002846 ASSERT(args->length() == 0);
2847
2848 Label slow_allocate_heapnumber;
2849 Label heapnumber_allocated;
2850
2851 // Save the new heap number in callee-saved register s0, since
2852 // we call out to external C code below.
2853 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2854 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2855 __ jmp(&heapnumber_allocated);
2856
2857 __ bind(&slow_allocate_heapnumber);
2858
2859 // Allocate a heap number.
2860 __ CallRuntime(Runtime::kNumberAlloc, 0);
2861 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2862
2863 __ bind(&heapnumber_allocated);
2864
2865 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2866 // by computing:
2867 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2868 if (CpuFeatures::IsSupported(FPU)) {
2869 __ PrepareCallCFunction(1, a0);
2870 __ li(a0, Operand(ExternalReference::isolate_address()));
2871 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2872
2873
2874 CpuFeatures::Scope scope(FPU);
2875 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2876 __ li(a1, Operand(0x41300000));
2877 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002878 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002879 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002880 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002881 // Subtract and store the result in the heap number.
2882 __ sub_d(f0, f12, f14);
2883 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2884 __ mov(v0, s0);
2885 } else {
2886 __ PrepareCallCFunction(2, a0);
2887 __ mov(a0, s0);
2888 __ li(a1, Operand(ExternalReference::isolate_address()));
2889 __ CallCFunction(
2890 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2891 }
2892
2893 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002894}
2895
2896
2897void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002898 // Load the arguments on the stack and call the stub.
2899 SubStringStub stub;
2900 ASSERT(args->length() == 3);
2901 VisitForStackValue(args->at(0));
2902 VisitForStackValue(args->at(1));
2903 VisitForStackValue(args->at(2));
2904 __ CallStub(&stub);
2905 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002906}
2907
2908
2909void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002910 // Load the arguments on the stack and call the stub.
2911 RegExpExecStub stub;
2912 ASSERT(args->length() == 4);
2913 VisitForStackValue(args->at(0));
2914 VisitForStackValue(args->at(1));
2915 VisitForStackValue(args->at(2));
2916 VisitForStackValue(args->at(3));
2917 __ CallStub(&stub);
2918 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002919}
2920
2921
2922void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002923 ASSERT(args->length() == 1);
2924
2925 VisitForAccumulatorValue(args->at(0)); // Load the object.
2926
2927 Label done;
2928 // If the object is a smi return the object.
2929 __ JumpIfSmi(v0, &done);
2930 // If the object is not a value type, return the object.
2931 __ GetObjectType(v0, a1, a1);
2932 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2933
2934 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2935
2936 __ bind(&done);
2937 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002938}
2939
2940
2941void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002942 // Load the arguments on the stack and call the runtime function.
2943 ASSERT(args->length() == 2);
2944 VisitForStackValue(args->at(0));
2945 VisitForStackValue(args->at(1));
2946 MathPowStub stub;
2947 __ CallStub(&stub);
2948 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002949}
2950
2951
2952void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002953 ASSERT(args->length() == 2);
2954
2955 VisitForStackValue(args->at(0)); // Load the object.
2956 VisitForAccumulatorValue(args->at(1)); // Load the value.
2957 __ pop(a1); // v0 = value. a1 = object.
2958
2959 Label done;
2960 // If the object is a smi, return the value.
2961 __ JumpIfSmi(a1, &done);
2962
2963 // If the object is not a value type, return the value.
2964 __ GetObjectType(a1, a2, a2);
2965 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2966
2967 // Store the value.
2968 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2969 // Update the write barrier. Save the value as it will be
2970 // overwritten by the write barrier code and is needed afterward.
2971 __ RecordWrite(a1, Operand(JSValue::kValueOffset - kHeapObjectTag), a2, a3);
2972
2973 __ bind(&done);
2974 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002975}
2976
2977
2978void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002979 ASSERT_EQ(args->length(), 1);
2980
2981 // Load the argument on the stack and call the stub.
2982 VisitForStackValue(args->at(0));
2983
2984 NumberToStringStub stub;
2985 __ CallStub(&stub);
2986 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002987}
2988
2989
2990void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002991 ASSERT(args->length() == 1);
2992
2993 VisitForAccumulatorValue(args->at(0));
2994
2995 Label done;
2996 StringCharFromCodeGenerator generator(v0, a1);
2997 generator.GenerateFast(masm_);
2998 __ jmp(&done);
2999
3000 NopRuntimeCallHelper call_helper;
3001 generator.GenerateSlow(masm_, call_helper);
3002
3003 __ bind(&done);
3004 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003005}
3006
3007
3008void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003009 ASSERT(args->length() == 2);
3010
3011 VisitForStackValue(args->at(0));
3012 VisitForAccumulatorValue(args->at(1));
3013 __ mov(a0, result_register());
3014
3015 Register object = a1;
3016 Register index = a0;
3017 Register scratch = a2;
3018 Register result = v0;
3019
3020 __ pop(object);
3021
3022 Label need_conversion;
3023 Label index_out_of_range;
3024 Label done;
3025 StringCharCodeAtGenerator generator(object,
3026 index,
3027 scratch,
3028 result,
3029 &need_conversion,
3030 &need_conversion,
3031 &index_out_of_range,
3032 STRING_INDEX_IS_NUMBER);
3033 generator.GenerateFast(masm_);
3034 __ jmp(&done);
3035
3036 __ bind(&index_out_of_range);
3037 // When the index is out of range, the spec requires us to return
3038 // NaN.
3039 __ LoadRoot(result, Heap::kNanValueRootIndex);
3040 __ jmp(&done);
3041
3042 __ bind(&need_conversion);
3043 // Load the undefined value into the result register, which will
3044 // trigger conversion.
3045 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3046 __ jmp(&done);
3047
3048 NopRuntimeCallHelper call_helper;
3049 generator.GenerateSlow(masm_, call_helper);
3050
3051 __ bind(&done);
3052 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003053}
3054
3055
3056void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003057 ASSERT(args->length() == 2);
3058
3059 VisitForStackValue(args->at(0));
3060 VisitForAccumulatorValue(args->at(1));
3061 __ mov(a0, result_register());
3062
3063 Register object = a1;
3064 Register index = a0;
3065 Register scratch1 = a2;
3066 Register scratch2 = a3;
3067 Register result = v0;
3068
3069 __ pop(object);
3070
3071 Label need_conversion;
3072 Label index_out_of_range;
3073 Label done;
3074 StringCharAtGenerator generator(object,
3075 index,
3076 scratch1,
3077 scratch2,
3078 result,
3079 &need_conversion,
3080 &need_conversion,
3081 &index_out_of_range,
3082 STRING_INDEX_IS_NUMBER);
3083 generator.GenerateFast(masm_);
3084 __ jmp(&done);
3085
3086 __ bind(&index_out_of_range);
3087 // When the index is out of range, the spec requires us to return
3088 // the empty string.
3089 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3090 __ jmp(&done);
3091
3092 __ bind(&need_conversion);
3093 // Move smi zero into the result register, which will trigger
3094 // conversion.
3095 __ li(result, Operand(Smi::FromInt(0)));
3096 __ jmp(&done);
3097
3098 NopRuntimeCallHelper call_helper;
3099 generator.GenerateSlow(masm_, call_helper);
3100
3101 __ bind(&done);
3102 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003103}
3104
3105
3106void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003107 ASSERT_EQ(2, args->length());
3108
3109 VisitForStackValue(args->at(0));
3110 VisitForStackValue(args->at(1));
3111
3112 StringAddStub stub(NO_STRING_ADD_FLAGS);
3113 __ CallStub(&stub);
3114 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003115}
3116
3117
3118void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003119 ASSERT_EQ(2, args->length());
3120
3121 VisitForStackValue(args->at(0));
3122 VisitForStackValue(args->at(1));
3123
3124 StringCompareStub stub;
3125 __ CallStub(&stub);
3126 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003127}
3128
3129
3130void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003131 // Load the argument on the stack and call the stub.
3132 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3133 TranscendentalCacheStub::TAGGED);
3134 ASSERT(args->length() == 1);
3135 VisitForStackValue(args->at(0));
3136 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3137 __ CallStub(&stub);
3138 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003139}
3140
3141
3142void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003143 // Load the argument on the stack and call the stub.
3144 TranscendentalCacheStub stub(TranscendentalCache::COS,
3145 TranscendentalCacheStub::TAGGED);
3146 ASSERT(args->length() == 1);
3147 VisitForStackValue(args->at(0));
3148 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3149 __ CallStub(&stub);
3150 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003151}
3152
3153
3154void FullCodeGenerator::EmitMathLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003155 // Load the argument on the stack and call the stub.
3156 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3157 TranscendentalCacheStub::TAGGED);
3158 ASSERT(args->length() == 1);
3159 VisitForStackValue(args->at(0));
3160 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3161 __ CallStub(&stub);
3162 context()->Plug(v0);
3163}
3164
3165
3166void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
3167 // Load the argument on the stack and call the runtime function.
3168 ASSERT(args->length() == 1);
3169 VisitForStackValue(args->at(0));
3170 __ CallRuntime(Runtime::kMath_sqrt, 1);
3171 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003172}
3173
3174
3175void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003176 ASSERT(args->length() >= 2);
3177
3178 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3179 for (int i = 0; i < arg_count + 1; i++) {
3180 VisitForStackValue(args->at(i));
3181 }
3182 VisitForAccumulatorValue(args->last()); // Function.
3183
3184 // InvokeFunction requires the function in a1. Move it in there.
3185 __ mov(a1, result_register());
3186 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003187 __ InvokeFunction(a1, count, CALL_FUNCTION,
3188 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003189 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3190 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003191}
3192
3193
3194void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003195 RegExpConstructResultStub stub;
3196 ASSERT(args->length() == 3);
3197 VisitForStackValue(args->at(0));
3198 VisitForStackValue(args->at(1));
3199 VisitForStackValue(args->at(2));
3200 __ CallStub(&stub);
3201 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003202}
3203
3204
3205void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003206 ASSERT(args->length() == 3);
3207 VisitForStackValue(args->at(0));
3208 VisitForStackValue(args->at(1));
3209 VisitForStackValue(args->at(2));
3210 Label done;
3211 Label slow_case;
3212 Register object = a0;
3213 Register index1 = a1;
3214 Register index2 = a2;
3215 Register elements = a3;
3216 Register scratch1 = t0;
3217 Register scratch2 = t1;
3218
3219 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3220 // Fetch the map and check if array is in fast case.
3221 // Check that object doesn't require security checks and
3222 // has no indexed interceptor.
3223 __ GetObjectType(object, scratch1, scratch2);
3224 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3225 // Map is now in scratch1.
3226
3227 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3228 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3229 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3230
3231 // Check the object's elements are in fast case and writable.
3232 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3233 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3234 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3235 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3236
3237 // Check that both indices are smis.
3238 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3239 __ lw(index2, MemOperand(sp, 0));
3240 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3241
3242 // Check that both indices are valid.
3243 Label not_hi;
3244 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3245 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3246 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3247 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3248 __ bind(&not_hi);
3249
3250 // Bring the address of the elements into index1 and index2.
3251 __ Addu(scratch1, elements,
3252 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3253 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3254 __ Addu(index1, scratch1, index1);
3255 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3256 __ Addu(index2, scratch1, index2);
3257
3258 // Swap elements.
3259 __ lw(scratch1, MemOperand(index1, 0));
3260 __ lw(scratch2, MemOperand(index2, 0));
3261 __ sw(scratch1, MemOperand(index2, 0));
3262 __ sw(scratch2, MemOperand(index1, 0));
3263
3264 Label new_space;
3265 __ InNewSpace(elements, scratch1, eq, &new_space);
3266 // Possible optimization: do a check that both values are Smis
3267 // (or them and test against Smi mask).
3268
3269 __ mov(scratch1, elements);
3270 __ RecordWriteHelper(elements, index1, scratch2);
3271 __ RecordWriteHelper(scratch1, index2, scratch2); // scratch1 holds elements.
3272
3273 __ bind(&new_space);
3274 // We are done. Drop elements from the stack, and return undefined.
3275 __ Drop(3);
3276 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3277 __ jmp(&done);
3278
3279 __ bind(&slow_case);
3280 __ CallRuntime(Runtime::kSwapElements, 3);
3281
3282 __ bind(&done);
3283 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003284}
3285
3286
3287void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003288 ASSERT_EQ(2, args->length());
3289
3290 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3291 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3292
3293 Handle<FixedArray> jsfunction_result_caches(
3294 isolate()->global_context()->jsfunction_result_caches());
3295 if (jsfunction_result_caches->length() <= cache_id) {
3296 __ Abort("Attempt to use undefined cache.");
3297 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3298 context()->Plug(v0);
3299 return;
3300 }
3301
3302 VisitForAccumulatorValue(args->at(1));
3303
3304 Register key = v0;
3305 Register cache = a1;
3306 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3307 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3308 __ lw(cache,
3309 ContextOperand(
3310 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3311 __ lw(cache,
3312 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3313
3314
3315 Label done, not_found;
3316 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
3317 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3318 // a2 now holds finger offset as a smi.
3319 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3320 // a3 now points to the start of fixed array elements.
3321 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3322 __ addu(a3, a3, at);
3323 // a3 now points to key of indexed element of cache.
3324 __ lw(a2, MemOperand(a3));
3325 __ Branch(&not_found, ne, key, Operand(a2));
3326
3327 __ lw(v0, MemOperand(a3, kPointerSize));
3328 __ Branch(&done);
3329
3330 __ bind(&not_found);
3331 // Call runtime to perform the lookup.
3332 __ Push(cache, key);
3333 __ CallRuntime(Runtime::kGetFromCache, 2);
3334
3335 __ bind(&done);
3336 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003337}
3338
3339
3340void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003341 ASSERT_EQ(2, args->length());
3342
3343 Register right = v0;
3344 Register left = a1;
3345 Register tmp = a2;
3346 Register tmp2 = a3;
3347
3348 VisitForStackValue(args->at(0));
3349 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3350 __ pop(left);
3351
3352 Label done, fail, ok;
3353 __ Branch(&ok, eq, left, Operand(right));
3354 // Fail if either is a non-HeapObject.
3355 __ And(tmp, left, Operand(right));
3356 __ And(at, tmp, Operand(kSmiTagMask));
3357 __ Branch(&fail, eq, at, Operand(zero_reg));
3358 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3359 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3360 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3361 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3362 __ Branch(&fail, ne, tmp, Operand(tmp2));
3363 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3364 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3365 __ Branch(&ok, eq, tmp, Operand(tmp2));
3366 __ bind(&fail);
3367 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3368 __ jmp(&done);
3369 __ bind(&ok);
3370 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3371 __ bind(&done);
3372
3373 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003374}
3375
3376
3377void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003378 VisitForAccumulatorValue(args->at(0));
3379
3380 Label materialize_true, materialize_false;
3381 Label* if_true = NULL;
3382 Label* if_false = NULL;
3383 Label* fall_through = NULL;
3384 context()->PrepareTest(&materialize_true, &materialize_false,
3385 &if_true, &if_false, &fall_through);
3386
3387 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3388 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3389
3390 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3391 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3392
3393 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003394}
3395
3396
3397void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003398 ASSERT(args->length() == 1);
3399 VisitForAccumulatorValue(args->at(0));
3400
3401 if (FLAG_debug_code) {
3402 __ AbortIfNotString(v0);
3403 }
3404
3405 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3406 __ IndexFromHash(v0, v0);
3407
3408 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003409}
3410
3411
3412void FullCodeGenerator::EmitFastAsciiArrayJoin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003413 Label bailout, done, one_char_separator, long_separator,
3414 non_trivial_array, not_size_one_array, loop,
3415 empty_separator_loop, one_char_separator_loop,
3416 one_char_separator_loop_entry, long_separator_loop;
3417
3418 ASSERT(args->length() == 2);
3419 VisitForStackValue(args->at(1));
3420 VisitForAccumulatorValue(args->at(0));
3421
3422 // All aliases of the same register have disjoint lifetimes.
3423 Register array = v0;
3424 Register elements = no_reg; // Will be v0.
3425 Register result = no_reg; // Will be v0.
3426 Register separator = a1;
3427 Register array_length = a2;
3428 Register result_pos = no_reg; // Will be a2.
3429 Register string_length = a3;
3430 Register string = t0;
3431 Register element = t1;
3432 Register elements_end = t2;
3433 Register scratch1 = t3;
3434 Register scratch2 = t5;
3435 Register scratch3 = t4;
3436 Register scratch4 = v1;
3437
3438 // Separator operand is on the stack.
3439 __ pop(separator);
3440
3441 // Check that the array is a JSArray.
3442 __ JumpIfSmi(array, &bailout);
3443 __ GetObjectType(array, scratch1, scratch2);
3444 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3445
3446 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003447 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003448
3449 // If the array has length zero, return the empty string.
3450 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3451 __ SmiUntag(array_length);
3452 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3453 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3454 __ Branch(&done);
3455
3456 __ bind(&non_trivial_array);
3457
3458 // Get the FixedArray containing array's elements.
3459 elements = array;
3460 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3461 array = no_reg; // End of array's live range.
3462
3463 // Check that all array elements are sequential ASCII strings, and
3464 // accumulate the sum of their lengths, as a smi-encoded value.
3465 __ mov(string_length, zero_reg);
3466 __ Addu(element,
3467 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3468 __ sll(elements_end, array_length, kPointerSizeLog2);
3469 __ Addu(elements_end, element, elements_end);
3470 // Loop condition: while (element < elements_end).
3471 // Live values in registers:
3472 // elements: Fixed array of strings.
3473 // array_length: Length of the fixed array of strings (not smi)
3474 // separator: Separator string
3475 // string_length: Accumulated sum of string lengths (smi).
3476 // element: Current array element.
3477 // elements_end: Array end.
3478 if (FLAG_debug_code) {
3479 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3480 array_length, Operand(zero_reg));
3481 }
3482 __ bind(&loop);
3483 __ lw(string, MemOperand(element));
3484 __ Addu(element, element, kPointerSize);
3485 __ JumpIfSmi(string, &bailout);
3486 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3487 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3488 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3489 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3490 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3491 __ BranchOnOverflow(&bailout, scratch3);
3492 __ Branch(&loop, lt, element, Operand(elements_end));
3493
3494 // If array_length is 1, return elements[0], a string.
3495 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3496 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3497 __ Branch(&done);
3498
3499 __ bind(&not_size_one_array);
3500
3501 // Live values in registers:
3502 // separator: Separator string
3503 // array_length: Length of the array.
3504 // string_length: Sum of string lengths (smi).
3505 // elements: FixedArray of strings.
3506
3507 // Check that the separator is a flat ASCII string.
3508 __ JumpIfSmi(separator, &bailout);
3509 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3510 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3511 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3512
3513 // Add (separator length times array_length) - separator length to the
3514 // string_length to get the length of the result string. array_length is not
3515 // smi but the other values are, so the result is a smi.
3516 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3517 __ Subu(string_length, string_length, Operand(scratch1));
3518 __ Mult(array_length, scratch1);
3519 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3520 // zero.
3521 __ mfhi(scratch2);
3522 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3523 __ mflo(scratch2);
3524 __ And(scratch3, scratch2, Operand(0x80000000));
3525 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3526 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3527 __ BranchOnOverflow(&bailout, scratch3);
3528 __ SmiUntag(string_length);
3529
3530 // Get first element in the array to free up the elements register to be used
3531 // for the result.
3532 __ Addu(element,
3533 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3534 result = elements; // End of live range for elements.
3535 elements = no_reg;
3536 // Live values in registers:
3537 // element: First array element
3538 // separator: Separator string
3539 // string_length: Length of result string (not smi)
3540 // array_length: Length of the array.
3541 __ AllocateAsciiString(result,
3542 string_length,
3543 scratch1,
3544 scratch2,
3545 elements_end,
3546 &bailout);
3547 // Prepare for looping. Set up elements_end to end of the array. Set
3548 // result_pos to the position of the result where to write the first
3549 // character.
3550 __ sll(elements_end, array_length, kPointerSizeLog2);
3551 __ Addu(elements_end, element, elements_end);
3552 result_pos = array_length; // End of live range for array_length.
3553 array_length = no_reg;
3554 __ Addu(result_pos,
3555 result,
3556 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3557
3558 // Check the length of the separator.
3559 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3560 __ li(at, Operand(Smi::FromInt(1)));
3561 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3562 __ Branch(&long_separator, gt, scratch1, Operand(at));
3563
3564 // Empty separator case.
3565 __ bind(&empty_separator_loop);
3566 // Live values in registers:
3567 // result_pos: the position to which we are currently copying characters.
3568 // element: Current array element.
3569 // elements_end: Array end.
3570
3571 // Copy next array element to the result.
3572 __ lw(string, MemOperand(element));
3573 __ Addu(element, element, kPointerSize);
3574 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3575 __ SmiUntag(string_length);
3576 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3577 __ CopyBytes(string, result_pos, string_length, scratch1);
3578 // End while (element < elements_end).
3579 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3580 ASSERT(result.is(v0));
3581 __ Branch(&done);
3582
3583 // One-character separator case.
3584 __ bind(&one_char_separator);
3585 // Replace separator with its ascii character value.
3586 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3587 // Jump into the loop after the code that copies the separator, so the first
3588 // element is not preceded by a separator.
3589 __ jmp(&one_char_separator_loop_entry);
3590
3591 __ bind(&one_char_separator_loop);
3592 // Live values in registers:
3593 // result_pos: the position to which we are currently copying characters.
3594 // element: Current array element.
3595 // elements_end: Array end.
3596 // separator: Single separator ascii char (in lower byte).
3597
3598 // Copy the separator character to the result.
3599 __ sb(separator, MemOperand(result_pos));
3600 __ Addu(result_pos, result_pos, 1);
3601
3602 // Copy next array element to the result.
3603 __ bind(&one_char_separator_loop_entry);
3604 __ lw(string, MemOperand(element));
3605 __ Addu(element, element, kPointerSize);
3606 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3607 __ SmiUntag(string_length);
3608 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3609 __ CopyBytes(string, result_pos, string_length, scratch1);
3610 // End while (element < elements_end).
3611 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3612 ASSERT(result.is(v0));
3613 __ Branch(&done);
3614
3615 // Long separator case (separator is more than one character). Entry is at the
3616 // label long_separator below.
3617 __ bind(&long_separator_loop);
3618 // Live values in registers:
3619 // result_pos: the position to which we are currently copying characters.
3620 // element: Current array element.
3621 // elements_end: Array end.
3622 // separator: Separator string.
3623
3624 // Copy the separator to the result.
3625 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3626 __ SmiUntag(string_length);
3627 __ Addu(string,
3628 separator,
3629 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3630 __ CopyBytes(string, result_pos, string_length, scratch1);
3631
3632 __ bind(&long_separator);
3633 __ lw(string, MemOperand(element));
3634 __ Addu(element, element, kPointerSize);
3635 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3636 __ SmiUntag(string_length);
3637 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3638 __ CopyBytes(string, result_pos, string_length, scratch1);
3639 // End while (element < elements_end).
3640 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3641 ASSERT(result.is(v0));
3642 __ Branch(&done);
3643
3644 __ bind(&bailout);
3645 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3646 __ bind(&done);
3647 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003648}
3649
3650
ager@chromium.org5c838252010-02-19 08:53:10 +00003651void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003652 Handle<String> name = expr->name();
3653 if (name->length() > 0 && name->Get(0) == '_') {
3654 Comment cmnt(masm_, "[ InlineRuntimeCall");
3655 EmitInlineRuntimeCall(expr);
3656 return;
3657 }
3658
3659 Comment cmnt(masm_, "[ CallRuntime");
3660 ZoneList<Expression*>* args = expr->arguments();
3661
3662 if (expr->is_jsruntime()) {
3663 // Prepare for calling JS runtime function.
3664 __ lw(a0, GlobalObjectOperand());
3665 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3666 __ push(a0);
3667 }
3668
3669 // Push the arguments ("left-to-right").
3670 int arg_count = args->length();
3671 for (int i = 0; i < arg_count; i++) {
3672 VisitForStackValue(args->at(i));
3673 }
3674
3675 if (expr->is_jsruntime()) {
3676 // Call the JS runtime function.
3677 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003678 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003679 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00003680 isolate()->stub_cache()->ComputeCallInitialize(arg_count,
3681 NOT_IN_LOOP,
3682 mode);
3683 EmitCallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003684 // Restore context register.
3685 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3686 } else {
3687 // Call the C runtime function.
3688 __ CallRuntime(expr->function(), arg_count);
3689 }
3690 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003691}
3692
3693
3694void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003695 switch (expr->op()) {
3696 case Token::DELETE: {
3697 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
3698 Property* prop = expr->expression()->AsProperty();
3699 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
3700
3701 if (prop != NULL) {
3702 if (prop->is_synthetic()) {
3703 // Result of deleting parameters is false, even when they rewrite
3704 // to accesses on the arguments object.
3705 context()->Plug(false);
3706 } else {
3707 VisitForStackValue(prop->obj());
3708 VisitForStackValue(prop->key());
3709 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3710 __ push(a1);
3711 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3712 context()->Plug(v0);
3713 }
3714 } else if (var != NULL) {
3715 // Delete of an unqualified identifier is disallowed in strict mode
3716 // but "delete this" is.
3717 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
3718 if (var->is_global()) {
3719 __ lw(a2, GlobalObjectOperand());
3720 __ li(a1, Operand(var->name()));
3721 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3722 __ Push(a2, a1, a0);
3723 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3724 context()->Plug(v0);
3725 } else if (var->AsSlot() != NULL &&
3726 var->AsSlot()->type() != Slot::LOOKUP) {
3727 // Result of deleting non-global, non-dynamic variables is false.
3728 // The subexpression does not have side effects.
3729 context()->Plug(false);
3730 } else {
3731 // Non-global variable. Call the runtime to try to delete from the
3732 // context where the variable was introduced.
3733 __ push(context_register());
3734 __ li(a2, Operand(var->name()));
3735 __ push(a2);
3736 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3737 context()->Plug(v0);
3738 }
3739 } else {
3740 // Result of deleting non-property, non-variable reference is true.
3741 // The subexpression may have side effects.
3742 VisitForEffect(expr->expression());
3743 context()->Plug(true);
3744 }
3745 break;
3746 }
3747
3748 case Token::VOID: {
3749 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3750 VisitForEffect(expr->expression());
3751 context()->Plug(Heap::kUndefinedValueRootIndex);
3752 break;
3753 }
3754
3755 case Token::NOT: {
3756 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3757 if (context()->IsEffect()) {
3758 // Unary NOT has no side effects so it's only necessary to visit the
3759 // subexpression. Match the optimizing compiler by not branching.
3760 VisitForEffect(expr->expression());
3761 } else {
3762 Label materialize_true, materialize_false;
3763 Label* if_true = NULL;
3764 Label* if_false = NULL;
3765 Label* fall_through = NULL;
3766
3767 // Notice that the labels are swapped.
3768 context()->PrepareTest(&materialize_true, &materialize_false,
3769 &if_false, &if_true, &fall_through);
3770 if (context()->IsTest()) ForwardBailoutToChild(expr);
3771 VisitForControl(expr->expression(), if_true, if_false, fall_through);
3772 context()->Plug(if_false, if_true); // Labels swapped.
3773 }
3774 break;
3775 }
3776
3777 case Token::TYPEOF: {
3778 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3779 { StackValueContext context(this);
3780 VisitForTypeofValue(expr->expression());
3781 }
3782 __ CallRuntime(Runtime::kTypeof, 1);
3783 context()->Plug(v0);
3784 break;
3785 }
3786
3787 case Token::ADD: {
3788 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3789 VisitForAccumulatorValue(expr->expression());
3790 Label no_conversion;
3791 __ JumpIfSmi(result_register(), &no_conversion);
3792 __ mov(a0, result_register());
3793 ToNumberStub convert_stub;
3794 __ CallStub(&convert_stub);
3795 __ bind(&no_conversion);
3796 context()->Plug(result_register());
3797 break;
3798 }
3799
3800 case Token::SUB:
3801 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3802 break;
3803
3804 case Token::BIT_NOT:
3805 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3806 break;
3807
3808 default:
3809 UNREACHABLE();
3810 }
3811}
3812
3813
3814void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3815 const char* comment) {
3816 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3817 Comment cmt(masm_, comment);
3818 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3819 UnaryOverwriteMode overwrite =
3820 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003821 UnaryOpStub stub(expr->op(), overwrite);
3822 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003823 VisitForAccumulatorValue(expr->expression());
3824 SetSourcePosition(expr->position());
3825 __ mov(a0, result_register());
3826 EmitCallIC(stub.GetCode(), NULL, expr->id());
3827 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003828}
3829
3830
3831void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003832 Comment cmnt(masm_, "[ CountOperation");
3833 SetSourcePosition(expr->position());
3834
3835 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3836 // as the left-hand side.
3837 if (!expr->expression()->IsValidLeftHandSide()) {
3838 VisitForEffect(expr->expression());
3839 return;
3840 }
3841
3842 // Expression can only be a property, a global or a (parameter or local)
3843 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
3844 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3845 LhsKind assign_type = VARIABLE;
3846 Property* prop = expr->expression()->AsProperty();
3847 // In case of a property we use the uninitialized expression context
3848 // of the key to detect a named property.
3849 if (prop != NULL) {
3850 assign_type =
3851 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3852 }
3853
3854 // Evaluate expression and get value.
3855 if (assign_type == VARIABLE) {
3856 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3857 AccumulatorValueContext context(this);
3858 EmitVariableLoad(expr->expression()->AsVariableProxy()->var());
3859 } else {
3860 // Reserve space for result of postfix operation.
3861 if (expr->is_postfix() && !context()->IsEffect()) {
3862 __ li(at, Operand(Smi::FromInt(0)));
3863 __ push(at);
3864 }
3865 if (assign_type == NAMED_PROPERTY) {
3866 // Put the object both on the stack and in the accumulator.
3867 VisitForAccumulatorValue(prop->obj());
3868 __ push(v0);
3869 EmitNamedPropertyLoad(prop);
3870 } else {
3871 if (prop->is_arguments_access()) {
3872 VariableProxy* obj_proxy = prop->obj()->AsVariableProxy();
3873 __ lw(v0, EmitSlotSearch(obj_proxy->var()->AsSlot(), v0));
3874 __ push(v0);
3875 __ li(v0, Operand(prop->key()->AsLiteral()->handle()));
3876 } else {
3877 VisitForStackValue(prop->obj());
3878 VisitForAccumulatorValue(prop->key());
3879 }
3880 __ lw(a1, MemOperand(sp, 0));
3881 __ push(v0);
3882 EmitKeyedPropertyLoad(prop);
3883 }
3884 }
3885
3886 // We need a second deoptimization point after loading the value
3887 // in case evaluating the property load my have a side effect.
3888 if (assign_type == VARIABLE) {
3889 PrepareForBailout(expr->expression(), TOS_REG);
3890 } else {
3891 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3892 }
3893
3894 // Call ToNumber only if operand is not a smi.
3895 Label no_conversion;
3896 __ JumpIfSmi(v0, &no_conversion);
3897 __ mov(a0, v0);
3898 ToNumberStub convert_stub;
3899 __ CallStub(&convert_stub);
3900 __ bind(&no_conversion);
3901
3902 // Save result for postfix expressions.
3903 if (expr->is_postfix()) {
3904 if (!context()->IsEffect()) {
3905 // Save the result on the stack. If we have a named or keyed property
3906 // we store the result under the receiver that is currently on top
3907 // of the stack.
3908 switch (assign_type) {
3909 case VARIABLE:
3910 __ push(v0);
3911 break;
3912 case NAMED_PROPERTY:
3913 __ sw(v0, MemOperand(sp, kPointerSize));
3914 break;
3915 case KEYED_PROPERTY:
3916 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3917 break;
3918 }
3919 }
3920 }
3921 __ mov(a0, result_register());
3922
3923 // Inline smi case if we are in a loop.
3924 Label stub_call, done;
3925 JumpPatchSite patch_site(masm_);
3926
3927 int count_value = expr->op() == Token::INC ? 1 : -1;
3928 __ li(a1, Operand(Smi::FromInt(count_value)));
3929
3930 if (ShouldInlineSmiCase(expr->op())) {
3931 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
3932 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
3933
3934 // We could eliminate this smi check if we split the code at
3935 // the first smi check before calling ToNumber.
3936 patch_site.EmitJumpIfSmi(v0, &done);
3937 __ bind(&stub_call);
3938 }
3939
3940 // Record position before stub call.
3941 SetSourcePosition(expr->position());
3942
danno@chromium.org40cb8782011-05-25 07:58:50 +00003943 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003944 EmitCallIC(stub.GetCode(), &patch_site, expr->CountId());
3945 __ bind(&done);
3946
3947 // Store the value returned in v0.
3948 switch (assign_type) {
3949 case VARIABLE:
3950 if (expr->is_postfix()) {
3951 { EffectContext context(this);
3952 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3953 Token::ASSIGN);
3954 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3955 context.Plug(v0);
3956 }
3957 // For all contexts except EffectConstant we have the result on
3958 // top of the stack.
3959 if (!context()->IsEffect()) {
3960 context()->PlugTOS();
3961 }
3962 } else {
3963 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3964 Token::ASSIGN);
3965 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3966 context()->Plug(v0);
3967 }
3968 break;
3969 case NAMED_PROPERTY: {
3970 __ mov(a0, result_register()); // Value.
3971 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
3972 __ pop(a1); // Receiver.
3973 Handle<Code> ic = is_strict_mode()
3974 ? isolate()->builtins()->StoreIC_Initialize_Strict()
3975 : isolate()->builtins()->StoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00003976 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003977 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3978 if (expr->is_postfix()) {
3979 if (!context()->IsEffect()) {
3980 context()->PlugTOS();
3981 }
3982 } else {
3983 context()->Plug(v0);
3984 }
3985 break;
3986 }
3987 case KEYED_PROPERTY: {
3988 __ mov(a0, result_register()); // Value.
3989 __ pop(a1); // Key.
3990 __ pop(a2); // Receiver.
3991 Handle<Code> ic = is_strict_mode()
3992 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
3993 : isolate()->builtins()->KeyedStoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00003994 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003995 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3996 if (expr->is_postfix()) {
3997 if (!context()->IsEffect()) {
3998 context()->PlugTOS();
3999 }
4000 } else {
4001 context()->Plug(v0);
4002 }
4003 break;
4004 }
4005 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004006}
4007
4008
lrn@chromium.org7516f052011-03-30 08:52:27 +00004009void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004010 VariableProxy* proxy = expr->AsVariableProxy();
4011 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
4012 Comment cmnt(masm_, "Global variable");
4013 __ lw(a0, GlobalObjectOperand());
4014 __ li(a2, Operand(proxy->name()));
4015 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4016 // Use a regular load, not a contextual load, to avoid a reference
4017 // error.
4018 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
4019 PrepareForBailout(expr, TOS_REG);
4020 context()->Plug(v0);
4021 } else if (proxy != NULL &&
4022 proxy->var()->AsSlot() != NULL &&
4023 proxy->var()->AsSlot()->type() == Slot::LOOKUP) {
4024 Label done, slow;
4025
4026 // Generate code for loading from variables potentially shadowed
4027 // by eval-introduced variables.
4028 Slot* slot = proxy->var()->AsSlot();
4029 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
4030
4031 __ bind(&slow);
4032 __ li(a0, Operand(proxy->name()));
4033 __ Push(cp, a0);
4034 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4035 PrepareForBailout(expr, TOS_REG);
4036 __ bind(&done);
4037
4038 context()->Plug(v0);
4039 } else {
4040 // This expression cannot throw a reference error at the top level.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004041 VisitInCurrentContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004042 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004043}
4044
4045
lrn@chromium.org7516f052011-03-30 08:52:27 +00004046bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
4047 Expression* left,
4048 Expression* right,
4049 Label* if_true,
4050 Label* if_false,
4051 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004052 if (op != Token::EQ && op != Token::EQ_STRICT) return false;
4053
4054 // Check for the pattern: typeof <expression> == <string literal>.
4055 Literal* right_literal = right->AsLiteral();
4056 if (right_literal == NULL) return false;
4057 Handle<Object> right_literal_value = right_literal->handle();
4058 if (!right_literal_value->IsString()) return false;
4059 UnaryOperation* left_unary = left->AsUnaryOperation();
4060 if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
4061 Handle<String> check = Handle<String>::cast(right_literal_value);
4062
4063 { AccumulatorValueContext context(this);
4064 VisitForTypeofValue(left_unary->expression());
4065 }
4066 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4067
4068 if (check->Equals(isolate()->heap()->number_symbol())) {
4069 __ JumpIfSmi(v0, if_true);
4070 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4071 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4072 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4073 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4074 __ JumpIfSmi(v0, if_false);
4075 // Check for undetectable objects => false.
4076 __ GetObjectType(v0, v0, a1);
4077 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4078 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4079 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4080 Split(eq, a1, Operand(zero_reg),
4081 if_true, if_false, fall_through);
4082 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4083 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4084 __ Branch(if_true, eq, v0, Operand(at));
4085 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4086 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4087 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4088 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4089 __ Branch(if_true, eq, v0, Operand(at));
4090 __ JumpIfSmi(v0, if_false);
4091 // Check for undetectable objects => true.
4092 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4093 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4094 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4095 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4096 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4097 __ JumpIfSmi(v0, if_false);
4098 __ GetObjectType(v0, a1, v0); // Leave map in a1.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004099 Split(ge, v0, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004100 if_true, if_false, fall_through);
4101
4102 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4103 __ JumpIfSmi(v0, if_false);
4104 __ LoadRoot(at, Heap::kNullValueRootIndex);
4105 __ Branch(if_true, eq, v0, Operand(at));
4106 // Check for JS objects => true.
4107 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004108 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004109 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004110 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004111 // Check for undetectable objects => false.
4112 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4113 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4114 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4115 } else {
4116 if (if_false != fall_through) __ jmp(if_false);
4117 }
4118
4119 return true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00004120}
4121
4122
ager@chromium.org5c838252010-02-19 08:53:10 +00004123void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004124 Comment cmnt(masm_, "[ CompareOperation");
4125 SetSourcePosition(expr->position());
4126
4127 // Always perform the comparison for its control flow. Pack the result
4128 // into the expression's context after the comparison is performed.
4129
4130 Label materialize_true, materialize_false;
4131 Label* if_true = NULL;
4132 Label* if_false = NULL;
4133 Label* fall_through = NULL;
4134 context()->PrepareTest(&materialize_true, &materialize_false,
4135 &if_true, &if_false, &fall_through);
4136
4137 // First we try a fast inlined version of the compare when one of
4138 // the operands is a literal.
4139 Token::Value op = expr->op();
4140 Expression* left = expr->left();
4141 Expression* right = expr->right();
4142 if (TryLiteralCompare(op, left, right, if_true, if_false, fall_through)) {
4143 context()->Plug(if_true, if_false);
4144 return;
4145 }
4146
4147 VisitForStackValue(expr->left());
4148 switch (op) {
4149 case Token::IN:
4150 VisitForStackValue(expr->right());
4151 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
4152 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
4153 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4154 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4155 break;
4156
4157 case Token::INSTANCEOF: {
4158 VisitForStackValue(expr->right());
4159 InstanceofStub stub(InstanceofStub::kNoFlags);
4160 __ CallStub(&stub);
4161 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4162 // The stub returns 0 for true.
4163 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4164 break;
4165 }
4166
4167 default: {
4168 VisitForAccumulatorValue(expr->right());
4169 Condition cc = eq;
4170 bool strict = false;
4171 switch (op) {
4172 case Token::EQ_STRICT:
4173 strict = true;
4174 // Fall through.
4175 case Token::EQ:
4176 cc = eq;
4177 __ mov(a0, result_register());
4178 __ pop(a1);
4179 break;
4180 case Token::LT:
4181 cc = lt;
4182 __ mov(a0, result_register());
4183 __ pop(a1);
4184 break;
4185 case Token::GT:
4186 // Reverse left and right sides to obtain ECMA-262 conversion order.
4187 cc = lt;
4188 __ mov(a1, result_register());
4189 __ pop(a0);
4190 break;
4191 case Token::LTE:
4192 // Reverse left and right sides to obtain ECMA-262 conversion order.
4193 cc = ge;
4194 __ mov(a1, result_register());
4195 __ pop(a0);
4196 break;
4197 case Token::GTE:
4198 cc = ge;
4199 __ mov(a0, result_register());
4200 __ pop(a1);
4201 break;
4202 case Token::IN:
4203 case Token::INSTANCEOF:
4204 default:
4205 UNREACHABLE();
4206 }
4207
4208 bool inline_smi_code = ShouldInlineSmiCase(op);
4209 JumpPatchSite patch_site(masm_);
4210 if (inline_smi_code) {
4211 Label slow_case;
4212 __ Or(a2, a0, Operand(a1));
4213 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4214 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4215 __ bind(&slow_case);
4216 }
4217 // Record position and call the compare IC.
4218 SetSourcePosition(expr->position());
4219 Handle<Code> ic = CompareIC::GetUninitialized(op);
4220 EmitCallIC(ic, &patch_site, expr->id());
4221 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4222 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4223 }
4224 }
4225
4226 // Convert the result of the comparison into one expected for this
4227 // expression's context.
4228 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004229}
4230
4231
lrn@chromium.org7516f052011-03-30 08:52:27 +00004232void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004233 Comment cmnt(masm_, "[ CompareToNull");
4234 Label materialize_true, materialize_false;
4235 Label* if_true = NULL;
4236 Label* if_false = NULL;
4237 Label* fall_through = NULL;
4238 context()->PrepareTest(&materialize_true, &materialize_false,
4239 &if_true, &if_false, &fall_through);
4240
4241 VisitForAccumulatorValue(expr->expression());
4242 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4243 __ mov(a0, result_register());
4244 __ LoadRoot(a1, Heap::kNullValueRootIndex);
4245 if (expr->is_strict()) {
4246 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4247 } else {
4248 __ Branch(if_true, eq, a0, Operand(a1));
4249 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
4250 __ Branch(if_true, eq, a0, Operand(a1));
4251 __ And(at, a0, Operand(kSmiTagMask));
4252 __ Branch(if_false, eq, at, Operand(zero_reg));
4253 // It can be an undetectable object.
4254 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4255 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4256 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4257 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4258 }
4259 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004260}
4261
4262
ager@chromium.org5c838252010-02-19 08:53:10 +00004263void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004264 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4265 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004266}
4267
4268
lrn@chromium.org7516f052011-03-30 08:52:27 +00004269Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004270 return v0;
4271}
ager@chromium.org5c838252010-02-19 08:53:10 +00004272
4273
lrn@chromium.org7516f052011-03-30 08:52:27 +00004274Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004275 return cp;
4276}
4277
4278
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004279void FullCodeGenerator::EmitCallIC(Handle<Code> ic,
4280 RelocInfo::Mode mode,
4281 unsigned ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004282 ASSERT(mode == RelocInfo::CODE_TARGET ||
danno@chromium.org40cb8782011-05-25 07:58:50 +00004283 mode == RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004284 Counters* counters = isolate()->counters();
4285 switch (ic->kind()) {
4286 case Code::LOAD_IC:
4287 __ IncrementCounter(counters->named_load_full(), 1, a1, a2);
4288 break;
4289 case Code::KEYED_LOAD_IC:
4290 __ IncrementCounter(counters->keyed_load_full(), 1, a1, a2);
4291 break;
4292 case Code::STORE_IC:
4293 __ IncrementCounter(counters->named_store_full(), 1, a1, a2);
4294 break;
4295 case Code::KEYED_STORE_IC:
4296 __ IncrementCounter(counters->keyed_store_full(), 1, a1, a2);
4297 default:
4298 break;
4299 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00004300 if (ast_id == kNoASTId || mode == RelocInfo::CODE_TARGET_CONTEXT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004301 __ Call(ic, mode);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004302 } else {
4303 ASSERT(mode == RelocInfo::CODE_TARGET);
4304 mode = RelocInfo::CODE_TARGET_WITH_ID;
4305 __ CallWithAstId(ic, mode, ast_id);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004306 }
4307}
4308
4309
4310void FullCodeGenerator::EmitCallIC(Handle<Code> ic,
4311 JumpPatchSite* patch_site,
4312 unsigned ast_id) {
4313 Counters* counters = isolate()->counters();
4314 switch (ic->kind()) {
4315 case Code::LOAD_IC:
4316 __ IncrementCounter(counters->named_load_full(), 1, a1, a2);
4317 break;
4318 case Code::KEYED_LOAD_IC:
4319 __ IncrementCounter(counters->keyed_load_full(), 1, a1, a2);
4320 break;
4321 case Code::STORE_IC:
4322 __ IncrementCounter(counters->named_store_full(), 1, a1, a2);
4323 break;
4324 case Code::KEYED_STORE_IC:
4325 __ IncrementCounter(counters->keyed_store_full(), 1, a1, a2);
4326 default:
4327 break;
4328 }
4329
danno@chromium.org40cb8782011-05-25 07:58:50 +00004330 if (ast_id == kNoASTId) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004331 __ Call(ic, RelocInfo::CODE_TARGET);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004332 } else {
4333 __ CallWithAstId(ic, RelocInfo::CODE_TARGET_WITH_ID, ast_id);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004334 }
4335 if (patch_site != NULL && patch_site->is_bound()) {
4336 patch_site->EmitPatchInfo();
4337 } else {
4338 __ nop(); // Signals no inlined code.
4339 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00004340}
ager@chromium.org5c838252010-02-19 08:53:10 +00004341
4342
4343void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004344 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4345 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004346}
4347
4348
4349void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004350 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004351}
4352
4353
4354// ----------------------------------------------------------------------------
4355// Non-local control flow support.
4356
4357void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004358 ASSERT(!result_register().is(a1));
4359 // Store result register while executing finally block.
4360 __ push(result_register());
4361 // Cook return address in link register to stack (smi encoded Code* delta).
4362 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4363 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4364 ASSERT_EQ(0, kSmiTag);
4365 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4366 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004367}
4368
4369
4370void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004371 ASSERT(!result_register().is(a1));
4372 // Restore result register from stack.
4373 __ pop(a1);
4374 // Uncook return address and return.
4375 __ pop(result_register());
4376 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4377 __ sra(a1, a1, 1); // Un-smi-tag value.
4378 __ Addu(at, a1, Operand(masm_->CodeObject()));
4379 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004380}
4381
4382
4383#undef __
4384
4385} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004386
4387#endif // V8_TARGET_ARCH_MIPS