blob: 1f1780ec2e5bd7b42c5a6a3ccbf155e7c9eb68ec [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.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000241 ArgumentsAccessStub::Type type;
242 if (is_strict_mode()) {
243 type = ArgumentsAccessStub::NEW_STRICT;
244 } else if (function()->has_duplicate_parameters()) {
245 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
246 } else {
247 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
248 }
249 ArgumentsAccessStub stub(type);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000250 __ CallStub(&stub);
251
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000252 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) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001257 // Three cases: non-this global variables, lookup slots, and all other
1258 // types of slots.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001259 Slot* slot = var->AsSlot();
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001260 ASSERT((var->is_global() && !var->is_this()) == (slot == NULL));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001261
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001262 if (slot == NULL) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001263 Comment cmnt(masm_, "Global variable");
1264 // Use inline caching. Variable name is passed in a2 and the global
1265 // object (receiver) in a0.
1266 __ lw(a0, GlobalObjectOperand());
1267 __ li(a2, Operand(var->name()));
1268 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1269 EmitCallIC(ic, RelocInfo::CODE_TARGET_CONTEXT, AstNode::kNoNumber);
1270 context()->Plug(v0);
1271
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001272 } else if (slot->type() == Slot::LOOKUP) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001273 Label done, slow;
1274
1275 // Generate code for loading from variables potentially shadowed
1276 // by eval-introduced variables.
1277 EmitDynamicLoadFromSlotFastCase(slot, NOT_INSIDE_TYPEOF, &slow, &done);
1278
1279 __ bind(&slow);
1280 Comment cmnt(masm_, "Lookup slot");
1281 __ li(a1, Operand(var->name()));
1282 __ Push(cp, a1); // Context and name.
1283 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1284 __ bind(&done);
1285
1286 context()->Plug(v0);
1287
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001288 } else {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001289 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1290 ? "Context slot"
1291 : "Stack slot");
1292 if (var->mode() == Variable::CONST) {
1293 // Constants may be the hole value if they have not been initialized.
1294 // Unhole them.
1295 MemOperand slot_operand = EmitSlotSearch(slot, a0);
1296 __ lw(v0, slot_operand);
1297 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1298 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1299 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1300 __ movz(v0, a0, at); // Conditional move.
1301 context()->Plug(v0);
1302 } else {
1303 context()->Plug(slot);
1304 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001305 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001306}
1307
1308
1309void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001310 Comment cmnt(masm_, "[ RegExpLiteral");
1311 Label materialized;
1312 // Registers will be used as follows:
1313 // t1 = materialized value (RegExp literal)
1314 // t0 = JS function, literals array
1315 // a3 = literal index
1316 // a2 = RegExp pattern
1317 // a1 = RegExp flags
1318 // a0 = RegExp literal clone
1319 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1320 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1321 int literal_offset =
1322 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1323 __ lw(t1, FieldMemOperand(t0, literal_offset));
1324 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1325 __ Branch(&materialized, ne, t1, Operand(at));
1326
1327 // Create regexp literal using runtime function.
1328 // Result will be in v0.
1329 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1330 __ li(a2, Operand(expr->pattern()));
1331 __ li(a1, Operand(expr->flags()));
1332 __ Push(t0, a3, a2, a1);
1333 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1334 __ mov(t1, v0);
1335
1336 __ bind(&materialized);
1337 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1338 Label allocated, runtime_allocate;
1339 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1340 __ jmp(&allocated);
1341
1342 __ bind(&runtime_allocate);
1343 __ push(t1);
1344 __ li(a0, Operand(Smi::FromInt(size)));
1345 __ push(a0);
1346 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1347 __ pop(t1);
1348
1349 __ bind(&allocated);
1350
1351 // After this, registers are used as follows:
1352 // v0: Newly allocated regexp.
1353 // t1: Materialized regexp.
1354 // a2: temp.
1355 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1356 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001357}
1358
1359
1360void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001361 Comment cmnt(masm_, "[ ObjectLiteral");
1362 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1363 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1364 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1365 __ li(a1, Operand(expr->constant_properties()));
1366 int flags = expr->fast_elements()
1367 ? ObjectLiteral::kFastElements
1368 : ObjectLiteral::kNoFlags;
1369 flags |= expr->has_function()
1370 ? ObjectLiteral::kHasFunction
1371 : ObjectLiteral::kNoFlags;
1372 __ li(a0, Operand(Smi::FromInt(flags)));
1373 __ Push(a3, a2, a1, a0);
1374 if (expr->depth() > 1) {
1375 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1376 } else {
1377 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
1378 }
1379
1380 // If result_saved is true the result is on top of the stack. If
1381 // result_saved is false the result is in v0.
1382 bool result_saved = false;
1383
1384 // Mark all computed expressions that are bound to a key that
1385 // is shadowed by a later occurrence of the same key. For the
1386 // marked expressions, no store code is emitted.
1387 expr->CalculateEmitStore();
1388
1389 for (int i = 0; i < expr->properties()->length(); i++) {
1390 ObjectLiteral::Property* property = expr->properties()->at(i);
1391 if (property->IsCompileTimeValue()) continue;
1392
1393 Literal* key = property->key();
1394 Expression* value = property->value();
1395 if (!result_saved) {
1396 __ push(v0); // Save result on stack.
1397 result_saved = true;
1398 }
1399 switch (property->kind()) {
1400 case ObjectLiteral::Property::CONSTANT:
1401 UNREACHABLE();
1402 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1403 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1404 // Fall through.
1405 case ObjectLiteral::Property::COMPUTED:
1406 if (key->handle()->IsSymbol()) {
1407 if (property->emit_store()) {
1408 VisitForAccumulatorValue(value);
1409 __ mov(a0, result_register());
1410 __ li(a2, Operand(key->handle()));
1411 __ lw(a1, MemOperand(sp));
1412 Handle<Code> ic = is_strict_mode()
1413 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1414 : isolate()->builtins()->StoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001415 EmitCallIC(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001416 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1417 } else {
1418 VisitForEffect(value);
1419 }
1420 break;
1421 }
1422 // Fall through.
1423 case ObjectLiteral::Property::PROTOTYPE:
1424 // Duplicate receiver on stack.
1425 __ lw(a0, MemOperand(sp));
1426 __ push(a0);
1427 VisitForStackValue(key);
1428 VisitForStackValue(value);
1429 if (property->emit_store()) {
1430 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1431 __ push(a0);
1432 __ CallRuntime(Runtime::kSetProperty, 4);
1433 } else {
1434 __ Drop(3);
1435 }
1436 break;
1437 case ObjectLiteral::Property::GETTER:
1438 case ObjectLiteral::Property::SETTER:
1439 // Duplicate receiver on stack.
1440 __ lw(a0, MemOperand(sp));
1441 __ push(a0);
1442 VisitForStackValue(key);
1443 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1444 Smi::FromInt(1) :
1445 Smi::FromInt(0)));
1446 __ push(a1);
1447 VisitForStackValue(value);
1448 __ CallRuntime(Runtime::kDefineAccessor, 4);
1449 break;
1450 }
1451 }
1452
1453 if (expr->has_function()) {
1454 ASSERT(result_saved);
1455 __ lw(a0, MemOperand(sp));
1456 __ push(a0);
1457 __ CallRuntime(Runtime::kToFastProperties, 1);
1458 }
1459
1460 if (result_saved) {
1461 context()->PlugTOS();
1462 } else {
1463 context()->Plug(v0);
1464 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001465}
1466
1467
1468void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001469 Comment cmnt(masm_, "[ ArrayLiteral");
1470
1471 ZoneList<Expression*>* subexprs = expr->values();
1472 int length = subexprs->length();
1473 __ mov(a0, result_register());
1474 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1475 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1476 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1477 __ li(a1, Operand(expr->constant_elements()));
1478 __ Push(a3, a2, a1);
1479 if (expr->constant_elements()->map() ==
1480 isolate()->heap()->fixed_cow_array_map()) {
1481 FastCloneShallowArrayStub stub(
1482 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1483 __ CallStub(&stub);
1484 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1485 1, a1, a2);
1486 } else if (expr->depth() > 1) {
1487 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1488 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1489 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1490 } else {
1491 FastCloneShallowArrayStub stub(
1492 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
1493 __ CallStub(&stub);
1494 }
1495
1496 bool result_saved = false; // Is the result saved to the stack?
1497
1498 // Emit code to evaluate all the non-constant subexpressions and to store
1499 // them into the newly cloned array.
1500 for (int i = 0; i < length; i++) {
1501 Expression* subexpr = subexprs->at(i);
1502 // If the subexpression is a literal or a simple materialized literal it
1503 // is already set in the cloned array.
1504 if (subexpr->AsLiteral() != NULL ||
1505 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1506 continue;
1507 }
1508
1509 if (!result_saved) {
1510 __ push(v0);
1511 result_saved = true;
1512 }
1513 VisitForAccumulatorValue(subexpr);
1514
1515 // Store the subexpression value in the array's elements.
1516 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1517 __ lw(a1, FieldMemOperand(a1, JSObject::kElementsOffset));
1518 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1519 __ sw(result_register(), FieldMemOperand(a1, offset));
1520
1521 // Update the write barrier for the array store with v0 as the scratch
1522 // register.
1523 __ li(a2, Operand(offset));
1524 // TODO(PJ): double check this RecordWrite call.
1525 __ RecordWrite(a1, a2, result_register());
1526
1527 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1528 }
1529
1530 if (result_saved) {
1531 context()->PlugTOS();
1532 } else {
1533 context()->Plug(v0);
1534 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001535}
1536
1537
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001538void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001539 Comment cmnt(masm_, "[ Assignment");
1540 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1541 // on the left-hand side.
1542 if (!expr->target()->IsValidLeftHandSide()) {
1543 VisitForEffect(expr->target());
1544 return;
1545 }
1546
1547 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001548 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001549 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1550 LhsKind assign_type = VARIABLE;
1551 Property* property = expr->target()->AsProperty();
1552 if (property != NULL) {
1553 assign_type = (property->key()->IsPropertyName())
1554 ? NAMED_PROPERTY
1555 : KEYED_PROPERTY;
1556 }
1557
1558 // Evaluate LHS expression.
1559 switch (assign_type) {
1560 case VARIABLE:
1561 // Nothing to do here.
1562 break;
1563 case NAMED_PROPERTY:
1564 if (expr->is_compound()) {
1565 // We need the receiver both on the stack and in the accumulator.
1566 VisitForAccumulatorValue(property->obj());
1567 __ push(result_register());
1568 } else {
1569 VisitForStackValue(property->obj());
1570 }
1571 break;
1572 case KEYED_PROPERTY:
1573 // We need the key and receiver on both the stack and in v0 and a1.
1574 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001575 VisitForStackValue(property->obj());
1576 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001577 __ lw(a1, MemOperand(sp, 0));
1578 __ push(v0);
1579 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001580 VisitForStackValue(property->obj());
1581 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001582 }
1583 break;
1584 }
1585
1586 // For compound assignments we need another deoptimization point after the
1587 // variable/property load.
1588 if (expr->is_compound()) {
1589 { AccumulatorValueContext context(this);
1590 switch (assign_type) {
1591 case VARIABLE:
1592 EmitVariableLoad(expr->target()->AsVariableProxy()->var());
1593 PrepareForBailout(expr->target(), TOS_REG);
1594 break;
1595 case NAMED_PROPERTY:
1596 EmitNamedPropertyLoad(property);
1597 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1598 break;
1599 case KEYED_PROPERTY:
1600 EmitKeyedPropertyLoad(property);
1601 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1602 break;
1603 }
1604 }
1605
1606 Token::Value op = expr->binary_op();
1607 __ push(v0); // Left operand goes on the stack.
1608 VisitForAccumulatorValue(expr->value());
1609
1610 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1611 ? OVERWRITE_RIGHT
1612 : NO_OVERWRITE;
1613 SetSourcePosition(expr->position() + 1);
1614 AccumulatorValueContext context(this);
1615 if (ShouldInlineSmiCase(op)) {
1616 EmitInlineSmiBinaryOp(expr->binary_operation(),
1617 op,
1618 mode,
1619 expr->target(),
1620 expr->value());
1621 } else {
1622 EmitBinaryOp(expr->binary_operation(), op, mode);
1623 }
1624
1625 // Deoptimization point in case the binary operation may have side effects.
1626 PrepareForBailout(expr->binary_operation(), TOS_REG);
1627 } else {
1628 VisitForAccumulatorValue(expr->value());
1629 }
1630
1631 // Record source position before possible IC call.
1632 SetSourcePosition(expr->position());
1633
1634 // Store the value.
1635 switch (assign_type) {
1636 case VARIABLE:
1637 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1638 expr->op());
1639 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1640 context()->Plug(v0);
1641 break;
1642 case NAMED_PROPERTY:
1643 EmitNamedPropertyAssignment(expr);
1644 break;
1645 case KEYED_PROPERTY:
1646 EmitKeyedPropertyAssignment(expr);
1647 break;
1648 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001649}
1650
1651
ager@chromium.org5c838252010-02-19 08:53:10 +00001652void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001653 SetSourcePosition(prop->position());
1654 Literal* key = prop->key()->AsLiteral();
1655 __ mov(a0, result_register());
1656 __ li(a2, Operand(key->handle()));
1657 // Call load IC. It has arguments receiver and property name a0 and a2.
1658 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001659 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001660}
1661
1662
1663void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001664 SetSourcePosition(prop->position());
1665 __ mov(a0, result_register());
1666 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1667 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001668 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001669}
1670
1671
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001672void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001673 Token::Value op,
1674 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001675 Expression* left_expr,
1676 Expression* right_expr) {
1677 Label done, smi_case, stub_call;
1678
1679 Register scratch1 = a2;
1680 Register scratch2 = a3;
1681
1682 // Get the arguments.
1683 Register left = a1;
1684 Register right = a0;
1685 __ pop(left);
1686 __ mov(a0, result_register());
1687
1688 // Perform combined smi check on both operands.
1689 __ Or(scratch1, left, Operand(right));
1690 STATIC_ASSERT(kSmiTag == 0);
1691 JumpPatchSite patch_site(masm_);
1692 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1693
1694 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001695 BinaryOpStub stub(op, mode);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001696 EmitCallIC(stub.GetCode(), &patch_site, expr->id());
1697 __ jmp(&done);
1698
1699 __ bind(&smi_case);
1700 // Smi case. This code works the same way as the smi-smi case in the type
1701 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001702 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001703 switch (op) {
1704 case Token::SAR:
1705 __ Branch(&stub_call);
1706 __ GetLeastBitsFromSmi(scratch1, right, 5);
1707 __ srav(right, left, scratch1);
1708 __ And(v0, right, Operand(~kSmiTagMask));
1709 break;
1710 case Token::SHL: {
1711 __ Branch(&stub_call);
1712 __ SmiUntag(scratch1, left);
1713 __ GetLeastBitsFromSmi(scratch2, right, 5);
1714 __ sllv(scratch1, scratch1, scratch2);
1715 __ Addu(scratch2, scratch1, Operand(0x40000000));
1716 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1717 __ SmiTag(v0, scratch1);
1718 break;
1719 }
1720 case Token::SHR: {
1721 __ Branch(&stub_call);
1722 __ SmiUntag(scratch1, left);
1723 __ GetLeastBitsFromSmi(scratch2, right, 5);
1724 __ srlv(scratch1, scratch1, scratch2);
1725 __ And(scratch2, scratch1, 0xc0000000);
1726 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1727 __ SmiTag(v0, scratch1);
1728 break;
1729 }
1730 case Token::ADD:
1731 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1732 __ BranchOnOverflow(&stub_call, scratch1);
1733 break;
1734 case Token::SUB:
1735 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1736 __ BranchOnOverflow(&stub_call, scratch1);
1737 break;
1738 case Token::MUL: {
1739 __ SmiUntag(scratch1, right);
1740 __ Mult(left, scratch1);
1741 __ mflo(scratch1);
1742 __ mfhi(scratch2);
1743 __ sra(scratch1, scratch1, 31);
1744 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1745 __ mflo(v0);
1746 __ Branch(&done, ne, v0, Operand(zero_reg));
1747 __ Addu(scratch2, right, left);
1748 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1749 ASSERT(Smi::FromInt(0) == 0);
1750 __ mov(v0, zero_reg);
1751 break;
1752 }
1753 case Token::BIT_OR:
1754 __ Or(v0, left, Operand(right));
1755 break;
1756 case Token::BIT_AND:
1757 __ And(v0, left, Operand(right));
1758 break;
1759 case Token::BIT_XOR:
1760 __ Xor(v0, left, Operand(right));
1761 break;
1762 default:
1763 UNREACHABLE();
1764 }
1765
1766 __ bind(&done);
1767 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001768}
1769
1770
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001771void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1772 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001773 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001774 __ mov(a0, result_register());
1775 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001776 BinaryOpStub stub(op, mode);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001777 EmitCallIC(stub.GetCode(), NULL, expr->id());
1778 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001779}
1780
1781
1782void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001783 // Invalid left-hand sides are rewritten to have a 'throw
1784 // ReferenceError' on the left-hand side.
1785 if (!expr->IsValidLeftHandSide()) {
1786 VisitForEffect(expr);
1787 return;
1788 }
1789
1790 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001791 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001792 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1793 LhsKind assign_type = VARIABLE;
1794 Property* prop = expr->AsProperty();
1795 if (prop != NULL) {
1796 assign_type = (prop->key()->IsPropertyName())
1797 ? NAMED_PROPERTY
1798 : KEYED_PROPERTY;
1799 }
1800
1801 switch (assign_type) {
1802 case VARIABLE: {
1803 Variable* var = expr->AsVariableProxy()->var();
1804 EffectContext context(this);
1805 EmitVariableAssignment(var, Token::ASSIGN);
1806 break;
1807 }
1808 case NAMED_PROPERTY: {
1809 __ push(result_register()); // Preserve value.
1810 VisitForAccumulatorValue(prop->obj());
1811 __ mov(a1, result_register());
1812 __ pop(a0); // Restore value.
1813 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1814 Handle<Code> ic = is_strict_mode()
1815 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1816 : isolate()->builtins()->StoreIC_Initialize();
1817 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
1818 break;
1819 }
1820 case KEYED_PROPERTY: {
1821 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001822 VisitForStackValue(prop->obj());
1823 VisitForAccumulatorValue(prop->key());
1824 __ mov(a1, result_register());
1825 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001826 __ pop(a0); // Restore value.
1827 Handle<Code> ic = is_strict_mode()
1828 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
1829 : isolate()->builtins()->KeyedStoreIC_Initialize();
1830 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
1831 break;
1832 }
1833 }
1834 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1835 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001836}
1837
1838
1839void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001840 Token::Value op) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001841 ASSERT(var != NULL);
1842 ASSERT(var->is_global() || var->AsSlot() != NULL);
1843
1844 if (var->is_global()) {
1845 ASSERT(!var->is_this());
1846 // Assignment to a global variable. Use inline caching for the
1847 // assignment. Right-hand-side value is passed in a0, variable name in
1848 // a2, and the global object in a1.
1849 __ mov(a0, result_register());
1850 __ li(a2, Operand(var->name()));
1851 __ lw(a1, GlobalObjectOperand());
1852 Handle<Code> ic = is_strict_mode()
1853 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1854 : isolate()->builtins()->StoreIC_Initialize();
1855 EmitCallIC(ic, RelocInfo::CODE_TARGET_CONTEXT, AstNode::kNoNumber);
1856
1857 } else if (op == Token::INIT_CONST) {
1858 // Like var declarations, const declarations are hoisted to function
1859 // scope. However, unlike var initializers, const initializers are able
1860 // to drill a hole to that function context, even from inside a 'with'
1861 // context. We thus bypass the normal static scope lookup.
1862 Slot* slot = var->AsSlot();
1863 Label skip;
1864 switch (slot->type()) {
1865 case Slot::PARAMETER:
1866 // No const parameters.
1867 UNREACHABLE();
1868 break;
1869 case Slot::LOCAL:
1870 // Detect const reinitialization by checking for the hole value.
1871 __ lw(a1, MemOperand(fp, SlotOffset(slot)));
1872 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1873 __ Branch(&skip, ne, a1, Operand(t0));
1874 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1875 break;
1876 case Slot::CONTEXT: {
1877 __ lw(a1, ContextOperand(cp, Context::FCONTEXT_INDEX));
1878 __ lw(a2, ContextOperand(a1, slot->index()));
1879 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1880 __ Branch(&skip, ne, a2, Operand(t0));
1881 __ sw(result_register(), ContextOperand(a1, slot->index()));
1882 int offset = Context::SlotOffset(slot->index());
1883 __ mov(a3, result_register()); // Preserve the stored value in v0.
1884 __ RecordWrite(a1, Operand(offset), a3, a2);
1885 break;
1886 }
1887 case Slot::LOOKUP:
1888 __ push(result_register());
1889 __ li(a0, Operand(slot->var()->name()));
1890 __ Push(cp, a0); // Context and name.
1891 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1892 break;
1893 }
1894 __ bind(&skip);
1895
1896 } else if (var->mode() != Variable::CONST) {
1897 // Perform the assignment for non-const variables. Const assignments
1898 // are simply skipped.
1899 Slot* slot = var->AsSlot();
1900 switch (slot->type()) {
1901 case Slot::PARAMETER:
1902 case Slot::LOCAL:
1903 // Perform the assignment.
1904 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1905 break;
1906
1907 case Slot::CONTEXT: {
1908 MemOperand target = EmitSlotSearch(slot, a1);
1909 // Perform the assignment and issue the write barrier.
1910 __ sw(result_register(), target);
1911 // RecordWrite may destroy all its register arguments.
1912 __ mov(a3, result_register());
1913 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
1914 __ RecordWrite(a1, Operand(offset), a2, a3);
1915 break;
1916 }
1917
1918 case Slot::LOOKUP:
1919 // Call the runtime for the assignment.
1920 __ push(v0); // Value.
1921 __ li(a1, Operand(slot->var()->name()));
1922 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1923 __ Push(cp, a1, a0); // Context, name, strict mode.
1924 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1925 break;
1926 }
1927 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001928}
1929
1930
1931void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001932 // Assignment to a property, using a named store IC.
1933 Property* prop = expr->target()->AsProperty();
1934 ASSERT(prop != NULL);
1935 ASSERT(prop->key()->AsLiteral() != NULL);
1936
1937 // If the assignment starts a block of assignments to the same object,
1938 // change to slow case to avoid the quadratic behavior of repeatedly
1939 // adding fast properties.
1940 if (expr->starts_initialization_block()) {
1941 __ push(result_register());
1942 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1943 __ push(t0);
1944 __ CallRuntime(Runtime::kToSlowProperties, 1);
1945 __ pop(result_register());
1946 }
1947
1948 // Record source code position before IC call.
1949 SetSourcePosition(expr->position());
1950 __ mov(a0, result_register()); // Load the value.
1951 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1952 // Load receiver to a1. Leave a copy in the stack if needed for turning the
1953 // receiver into fast case.
1954 if (expr->ends_initialization_block()) {
1955 __ lw(a1, MemOperand(sp));
1956 } else {
1957 __ pop(a1);
1958 }
1959
1960 Handle<Code> ic = is_strict_mode()
1961 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1962 : isolate()->builtins()->StoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001963 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001964
1965 // If the assignment ends an initialization block, revert to fast case.
1966 if (expr->ends_initialization_block()) {
1967 __ push(v0); // Result of assignment, saved even if not needed.
1968 // Receiver is under the result value.
1969 __ lw(t0, MemOperand(sp, kPointerSize));
1970 __ push(t0);
1971 __ CallRuntime(Runtime::kToFastProperties, 1);
1972 __ pop(v0);
1973 __ Drop(1);
1974 }
1975 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1976 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001977}
1978
1979
1980void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001981 // Assignment to a property, using a keyed store IC.
1982
1983 // If the assignment starts a block of assignments to the same object,
1984 // change to slow case to avoid the quadratic behavior of repeatedly
1985 // adding fast properties.
1986 if (expr->starts_initialization_block()) {
1987 __ push(result_register());
1988 // Receiver is now under the key and value.
1989 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
1990 __ push(t0);
1991 __ CallRuntime(Runtime::kToSlowProperties, 1);
1992 __ pop(result_register());
1993 }
1994
1995 // Record source code position before IC call.
1996 SetSourcePosition(expr->position());
1997 // Call keyed store IC.
1998 // The arguments are:
1999 // - a0 is the value,
2000 // - a1 is the key,
2001 // - a2 is the receiver.
2002 __ mov(a0, result_register());
2003 __ pop(a1); // Key.
2004 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2005 // receiver into fast case.
2006 if (expr->ends_initialization_block()) {
2007 __ lw(a2, MemOperand(sp));
2008 } else {
2009 __ pop(a2);
2010 }
2011
2012 Handle<Code> ic = is_strict_mode()
2013 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
2014 : isolate()->builtins()->KeyedStoreIC_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::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002033 Comment cmnt(masm_, "[ Property");
2034 Expression* key = expr->key();
2035
2036 if (key->IsPropertyName()) {
2037 VisitForAccumulatorValue(expr->obj());
2038 EmitNamedPropertyLoad(expr);
2039 context()->Plug(v0);
2040 } else {
2041 VisitForStackValue(expr->obj());
2042 VisitForAccumulatorValue(expr->key());
2043 __ pop(a1);
2044 EmitKeyedPropertyLoad(expr);
2045 context()->Plug(v0);
2046 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002047}
2048
lrn@chromium.org7516f052011-03-30 08:52:27 +00002049
ager@chromium.org5c838252010-02-19 08:53:10 +00002050void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002051 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002052 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002053 // Code common for calls using the IC.
2054 ZoneList<Expression*>* args = expr->arguments();
2055 int arg_count = args->length();
2056 { PreservePositionScope scope(masm()->positions_recorder());
2057 for (int i = 0; i < arg_count; i++) {
2058 VisitForStackValue(args->at(i));
2059 }
2060 __ li(a2, Operand(name));
2061 }
2062 // Record source position for debugger.
2063 SetSourcePosition(expr->position());
2064 // Call the IC initialization code.
2065 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2066 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00002067 isolate()->stub_cache()->ComputeCallInitialize(arg_count, in_loop, mode);
2068 EmitCallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002069 RecordJSReturnSite(expr);
2070 // Restore context register.
2071 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2072 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002073}
2074
2075
lrn@chromium.org7516f052011-03-30 08:52:27 +00002076void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002077 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002078 // Load the key.
2079 VisitForAccumulatorValue(key);
2080
2081 // Swap the name of the function and the receiver on the stack to follow
2082 // the calling convention for call ICs.
2083 __ pop(a1);
2084 __ push(v0);
2085 __ push(a1);
2086
2087 // Code common for calls using the IC.
2088 ZoneList<Expression*>* args = expr->arguments();
2089 int arg_count = args->length();
2090 { PreservePositionScope scope(masm()->positions_recorder());
2091 for (int i = 0; i < arg_count; i++) {
2092 VisitForStackValue(args->at(i));
2093 }
2094 }
2095 // Record source position for debugger.
2096 SetSourcePosition(expr->position());
2097 // Call the IC initialization code.
2098 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2099 Handle<Code> ic =
2100 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count, in_loop);
2101 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002102 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002103 RecordJSReturnSite(expr);
2104 // Restore context register.
2105 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2106 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002107}
2108
2109
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002110void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002111 // Code common for calls using the call stub.
2112 ZoneList<Expression*>* args = expr->arguments();
2113 int arg_count = args->length();
2114 { PreservePositionScope scope(masm()->positions_recorder());
2115 for (int i = 0; i < arg_count; i++) {
2116 VisitForStackValue(args->at(i));
2117 }
2118 }
2119 // Record source position for debugger.
2120 SetSourcePosition(expr->position());
2121 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2122 CallFunctionStub stub(arg_count, in_loop, flags);
2123 __ CallStub(&stub);
2124 RecordJSReturnSite(expr);
2125 // Restore context register.
2126 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2127 context()->DropAndPlug(1, v0);
2128}
2129
2130
2131void FullCodeGenerator::EmitResolvePossiblyDirectEval(ResolveEvalFlag flag,
2132 int arg_count) {
2133 // Push copy of the first argument or undefined if it doesn't exist.
2134 if (arg_count > 0) {
2135 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2136 } else {
2137 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2138 }
2139 __ push(a1);
2140
2141 // Push the receiver of the enclosing function and do runtime call.
2142 __ lw(a1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
2143 __ push(a1);
2144 // Push the strict mode flag.
2145 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
2146 __ push(a1);
2147
2148 __ CallRuntime(flag == SKIP_CONTEXT_LOOKUP
2149 ? Runtime::kResolvePossiblyDirectEvalNoLookup
2150 : Runtime::kResolvePossiblyDirectEval, 4);
ager@chromium.org5c838252010-02-19 08:53:10 +00002151}
2152
2153
2154void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002155#ifdef DEBUG
2156 // We want to verify that RecordJSReturnSite gets called on all paths
2157 // through this function. Avoid early returns.
2158 expr->return_is_recorded_ = false;
2159#endif
2160
2161 Comment cmnt(masm_, "[ Call");
2162 Expression* fun = expr->expression();
2163 Variable* var = fun->AsVariableProxy()->AsVariable();
2164
2165 if (var != NULL && var->is_possibly_eval()) {
2166 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2167 // resolve the function we need to call and the receiver of the
2168 // call. Then we call the resolved function using the given
2169 // arguments.
2170 ZoneList<Expression*>* args = expr->arguments();
2171 int arg_count = args->length();
2172
2173 { PreservePositionScope pos_scope(masm()->positions_recorder());
2174 VisitForStackValue(fun);
2175 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2176 __ push(a2); // Reserved receiver slot.
2177
2178 // Push the arguments.
2179 for (int i = 0; i < arg_count; i++) {
2180 VisitForStackValue(args->at(i));
2181 }
2182 // If we know that eval can only be shadowed by eval-introduced
2183 // variables we attempt to load the global eval function directly
2184 // in generated code. If we succeed, there is no need to perform a
2185 // context lookup in the runtime system.
2186 Label done;
2187 if (var->AsSlot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
2188 Label slow;
2189 EmitLoadGlobalSlotCheckExtensions(var->AsSlot(),
2190 NOT_INSIDE_TYPEOF,
2191 &slow);
2192 // Push the function and resolve eval.
2193 __ push(v0);
2194 EmitResolvePossiblyDirectEval(SKIP_CONTEXT_LOOKUP, arg_count);
2195 __ jmp(&done);
2196 __ bind(&slow);
2197 }
2198
2199 // Push copy of the function (found below the arguments) and
2200 // resolve eval.
2201 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2202 __ push(a1);
2203 EmitResolvePossiblyDirectEval(PERFORM_CONTEXT_LOOKUP, arg_count);
2204 if (done.is_linked()) {
2205 __ bind(&done);
2206 }
2207
2208 // The runtime call returns a pair of values in v0 (function) and
2209 // v1 (receiver). Touch up the stack with the right values.
2210 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2211 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2212 }
2213 // Record source position for debugger.
2214 SetSourcePosition(expr->position());
2215 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002216 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002217 __ CallStub(&stub);
2218 RecordJSReturnSite(expr);
2219 // Restore context register.
2220 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2221 context()->DropAndPlug(1, v0);
2222 } else if (var != NULL && !var->is_this() && var->is_global()) {
2223 // Push global object as receiver for the call IC.
2224 __ lw(a0, GlobalObjectOperand());
2225 __ push(a0);
2226 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
2227 } else if (var != NULL && var->AsSlot() != NULL &&
2228 var->AsSlot()->type() == Slot::LOOKUP) {
2229 // Call to a lookup slot (dynamically introduced variable).
2230 Label slow, done;
2231
2232 { PreservePositionScope scope(masm()->positions_recorder());
2233 // Generate code for loading from variables potentially shadowed
2234 // by eval-introduced variables.
2235 EmitDynamicLoadFromSlotFastCase(var->AsSlot(),
2236 NOT_INSIDE_TYPEOF,
2237 &slow,
2238 &done);
2239 }
2240
2241 __ bind(&slow);
2242 // Call the runtime to find the function to call (returned in v0)
2243 // and the object holding it (returned in v1).
2244 __ push(context_register());
2245 __ li(a2, Operand(var->name()));
2246 __ push(a2);
2247 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2248 __ Push(v0, v1); // Function, receiver.
2249
2250 // If fast case code has been generated, emit code to push the
2251 // function and receiver and have the slow path jump around this
2252 // code.
2253 if (done.is_linked()) {
2254 Label call;
2255 __ Branch(&call);
2256 __ bind(&done);
2257 // Push function.
2258 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002259 // The receiver is implicitly the global receiver. Indicate this
2260 // by passing the hole to the call function stub.
2261 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002262 __ push(a1);
2263 __ bind(&call);
2264 }
2265
danno@chromium.org40cb8782011-05-25 07:58:50 +00002266 // The receiver is either the global receiver or an object found
2267 // by LoadContextSlot. That object could be the hole if the
2268 // receiver is implicitly the global object.
2269 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002270 } else if (fun->AsProperty() != NULL) {
2271 // Call to an object property.
2272 Property* prop = fun->AsProperty();
2273 Literal* key = prop->key()->AsLiteral();
2274 if (key != NULL && key->handle()->IsSymbol()) {
2275 // Call to a named property, use call IC.
2276 { PreservePositionScope scope(masm()->positions_recorder());
2277 VisitForStackValue(prop->obj());
2278 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00002279 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002280 } else {
2281 // Call to a keyed property.
2282 // For a synthetic property use keyed load IC followed by function call,
2283 // for a regular property use keyed EmitCallIC.
2284 if (prop->is_synthetic()) {
2285 // Do not visit the object and key subexpressions (they are shared
2286 // by all occurrences of the same rewritten parameter).
2287 ASSERT(prop->obj()->AsVariableProxy() != NULL);
2288 ASSERT(prop->obj()->AsVariableProxy()->var()->AsSlot() != NULL);
2289 Slot* slot = prop->obj()->AsVariableProxy()->var()->AsSlot();
2290 MemOperand operand = EmitSlotSearch(slot, a1);
2291 __ lw(a1, operand);
2292
2293 ASSERT(prop->key()->AsLiteral() != NULL);
2294 ASSERT(prop->key()->AsLiteral()->handle()->IsSmi());
2295 __ li(a0, Operand(prop->key()->AsLiteral()->handle()));
2296
2297 // Record source code position for IC call.
2298 SetSourcePosition(prop->position());
2299
2300 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00002301 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002302 __ lw(a1, GlobalObjectOperand());
2303 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2304 __ Push(v0, a1); // Function, receiver.
2305 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2306 } else {
2307 { PreservePositionScope scope(masm()->positions_recorder());
2308 VisitForStackValue(prop->obj());
2309 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00002310 EmitKeyedCallWithIC(expr, prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002311 }
2312 }
2313 } else {
2314 { PreservePositionScope scope(masm()->positions_recorder());
2315 VisitForStackValue(fun);
2316 }
2317 // Load global receiver object.
2318 __ lw(a1, GlobalObjectOperand());
2319 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2320 __ push(a1);
2321 // Emit function call.
2322 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2323 }
2324
2325#ifdef DEBUG
2326 // RecordJSReturnSite should have been called.
2327 ASSERT(expr->return_is_recorded_);
2328#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002329}
2330
2331
2332void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002333 Comment cmnt(masm_, "[ CallNew");
2334 // According to ECMA-262, section 11.2.2, page 44, the function
2335 // expression in new calls must be evaluated before the
2336 // arguments.
2337
2338 // Push constructor on the stack. If it's not a function it's used as
2339 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2340 // ignored.
2341 VisitForStackValue(expr->expression());
2342
2343 // Push the arguments ("left-to-right") on the stack.
2344 ZoneList<Expression*>* args = expr->arguments();
2345 int arg_count = args->length();
2346 for (int i = 0; i < arg_count; i++) {
2347 VisitForStackValue(args->at(i));
2348 }
2349
2350 // Call the construct call builtin that handles allocation and
2351 // constructor invocation.
2352 SetSourcePosition(expr->position());
2353
2354 // Load function and argument count into a1 and a0.
2355 __ li(a0, Operand(arg_count));
2356 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2357
2358 Handle<Code> construct_builtin =
2359 isolate()->builtins()->JSConstructCall();
2360 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2361 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002362}
2363
2364
lrn@chromium.org7516f052011-03-30 08:52:27 +00002365void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002366 ASSERT(args->length() == 1);
2367
2368 VisitForAccumulatorValue(args->at(0));
2369
2370 Label materialize_true, materialize_false;
2371 Label* if_true = NULL;
2372 Label* if_false = NULL;
2373 Label* fall_through = NULL;
2374 context()->PrepareTest(&materialize_true, &materialize_false,
2375 &if_true, &if_false, &fall_through);
2376
2377 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2378 __ And(t0, v0, Operand(kSmiTagMask));
2379 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2380
2381 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002382}
2383
2384
2385void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002386 ASSERT(args->length() == 1);
2387
2388 VisitForAccumulatorValue(args->at(0));
2389
2390 Label materialize_true, materialize_false;
2391 Label* if_true = NULL;
2392 Label* if_false = NULL;
2393 Label* fall_through = NULL;
2394 context()->PrepareTest(&materialize_true, &materialize_false,
2395 &if_true, &if_false, &fall_through);
2396
2397 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2398 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2399 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2400
2401 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002402}
2403
2404
2405void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002406 ASSERT(args->length() == 1);
2407
2408 VisitForAccumulatorValue(args->at(0));
2409
2410 Label materialize_true, materialize_false;
2411 Label* if_true = NULL;
2412 Label* if_false = NULL;
2413 Label* fall_through = NULL;
2414 context()->PrepareTest(&materialize_true, &materialize_false,
2415 &if_true, &if_false, &fall_through);
2416
2417 __ JumpIfSmi(v0, if_false);
2418 __ LoadRoot(at, Heap::kNullValueRootIndex);
2419 __ Branch(if_true, eq, v0, Operand(at));
2420 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2421 // Undetectable objects behave like undefined when tested with typeof.
2422 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2423 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2424 __ Branch(if_false, ne, at, Operand(zero_reg));
2425 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002426 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002427 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002428 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2429 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002430
2431 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002432}
2433
2434
2435void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002436 ASSERT(args->length() == 1);
2437
2438 VisitForAccumulatorValue(args->at(0));
2439
2440 Label materialize_true, materialize_false;
2441 Label* if_true = NULL;
2442 Label* if_false = NULL;
2443 Label* fall_through = NULL;
2444 context()->PrepareTest(&materialize_true, &materialize_false,
2445 &if_true, &if_false, &fall_through);
2446
2447 __ JumpIfSmi(v0, if_false);
2448 __ GetObjectType(v0, a1, a1);
2449 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002450 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002451 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::EmitIsUndetectableObject(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 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2471 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2472 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2473 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2474 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2475
2476 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002477}
2478
2479
2480void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2481 ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002482
2483 ASSERT(args->length() == 1);
2484
2485 VisitForAccumulatorValue(args->at(0));
2486
2487 Label materialize_true, materialize_false;
2488 Label* if_true = NULL;
2489 Label* if_false = NULL;
2490 Label* fall_through = NULL;
2491 context()->PrepareTest(&materialize_true, &materialize_false,
2492 &if_true, &if_false, &fall_through);
2493
2494 if (FLAG_debug_code) __ AbortIfSmi(v0);
2495
2496 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2497 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2498 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2499 __ Branch(if_true, ne, t0, Operand(zero_reg));
2500
2501 // Check for fast case object. Generate false result for slow case object.
2502 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2503 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2504 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2505 __ Branch(if_false, eq, a2, Operand(t0));
2506
2507 // Look for valueOf symbol in the descriptor array, and indicate false if
2508 // found. The type is not checked, so if it is a transition it is a false
2509 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002510 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002511 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2512 // t0: descriptor array
2513 // a3: length of descriptor array
2514 // Calculate the end of the descriptor array.
2515 STATIC_ASSERT(kSmiTag == 0);
2516 STATIC_ASSERT(kSmiTagSize == 1);
2517 STATIC_ASSERT(kPointerSize == 4);
2518 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2519 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2520 __ Addu(a2, a2, t1);
2521
2522 // Calculate location of the first key name.
2523 __ Addu(t0,
2524 t0,
2525 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2526 DescriptorArray::kFirstIndex * kPointerSize));
2527 // Loop through all the keys in the descriptor array. If one of these is the
2528 // symbol valueOf the result is false.
2529 Label entry, loop;
2530 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2531 // used in the loop below.
2532 __ li(t2, Operand(FACTORY->value_of_symbol()));
2533 __ jmp(&entry);
2534 __ bind(&loop);
2535 __ lw(a3, MemOperand(t0, 0));
2536 __ Branch(if_false, eq, a3, Operand(t2));
2537 __ Addu(t0, t0, Operand(kPointerSize));
2538 __ bind(&entry);
2539 __ Branch(&loop, ne, t0, Operand(a2));
2540
2541 // If a valueOf property is not found on the object check that it's
2542 // prototype is the un-modified String prototype. If not result is false.
2543 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2544 __ JumpIfSmi(a2, if_false);
2545 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2546 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2547 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2548 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2549 __ Branch(if_false, ne, a2, Operand(a3));
2550
2551 // Set the bit in the map to indicate that it has been checked safe for
2552 // default valueOf and set true result.
2553 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2554 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2555 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2556 __ jmp(if_true);
2557
2558 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2559 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002560}
2561
2562
2563void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002564 ASSERT(args->length() == 1);
2565
2566 VisitForAccumulatorValue(args->at(0));
2567
2568 Label materialize_true, materialize_false;
2569 Label* if_true = NULL;
2570 Label* if_false = NULL;
2571 Label* fall_through = NULL;
2572 context()->PrepareTest(&materialize_true, &materialize_false,
2573 &if_true, &if_false, &fall_through);
2574
2575 __ JumpIfSmi(v0, if_false);
2576 __ GetObjectType(v0, a1, a2);
2577 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2578 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2579 __ Branch(if_false);
2580
2581 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002582}
2583
2584
2585void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002586 ASSERT(args->length() == 1);
2587
2588 VisitForAccumulatorValue(args->at(0));
2589
2590 Label materialize_true, materialize_false;
2591 Label* if_true = NULL;
2592 Label* if_false = NULL;
2593 Label* fall_through = NULL;
2594 context()->PrepareTest(&materialize_true, &materialize_false,
2595 &if_true, &if_false, &fall_through);
2596
2597 __ JumpIfSmi(v0, if_false);
2598 __ GetObjectType(v0, a1, a1);
2599 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2600 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2601 if_true, if_false, fall_through);
2602
2603 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002604}
2605
2606
2607void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002608 ASSERT(args->length() == 1);
2609
2610 VisitForAccumulatorValue(args->at(0));
2611
2612 Label materialize_true, materialize_false;
2613 Label* if_true = NULL;
2614 Label* if_false = NULL;
2615 Label* fall_through = NULL;
2616 context()->PrepareTest(&materialize_true, &materialize_false,
2617 &if_true, &if_false, &fall_through);
2618
2619 __ JumpIfSmi(v0, if_false);
2620 __ GetObjectType(v0, a1, a1);
2621 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2622 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2623
2624 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002625}
2626
2627
2628void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002629 ASSERT(args->length() == 0);
2630
2631 Label materialize_true, materialize_false;
2632 Label* if_true = NULL;
2633 Label* if_false = NULL;
2634 Label* fall_through = NULL;
2635 context()->PrepareTest(&materialize_true, &materialize_false,
2636 &if_true, &if_false, &fall_through);
2637
2638 // Get the frame pointer for the calling frame.
2639 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2640
2641 // Skip the arguments adaptor frame if it exists.
2642 Label check_frame_marker;
2643 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2644 __ Branch(&check_frame_marker, ne,
2645 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2646 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2647
2648 // Check the marker in the calling frame.
2649 __ bind(&check_frame_marker);
2650 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
2651 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2652 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
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::EmitObjectEquals(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002660 ASSERT(args->length() == 2);
2661
2662 // Load the two objects into registers and perform the comparison.
2663 VisitForStackValue(args->at(0));
2664 VisitForAccumulatorValue(args->at(1));
2665
2666 Label materialize_true, materialize_false;
2667 Label* if_true = NULL;
2668 Label* if_false = NULL;
2669 Label* fall_through = NULL;
2670 context()->PrepareTest(&materialize_true, &materialize_false,
2671 &if_true, &if_false, &fall_through);
2672
2673 __ pop(a1);
2674 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2675 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2676
2677 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002678}
2679
2680
2681void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002682 ASSERT(args->length() == 1);
2683
2684 // ArgumentsAccessStub expects the key in a1 and the formal
2685 // parameter count in a0.
2686 VisitForAccumulatorValue(args->at(0));
2687 __ mov(a1, v0);
2688 __ li(a0, Operand(Smi::FromInt(scope()->num_parameters())));
2689 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2690 __ CallStub(&stub);
2691 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002692}
2693
2694
2695void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002696 ASSERT(args->length() == 0);
2697
2698 Label exit;
2699 // Get the number of formal parameters.
2700 __ li(v0, Operand(Smi::FromInt(scope()->num_parameters())));
2701
2702 // Check if the calling frame is an arguments adaptor frame.
2703 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2704 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2705 __ Branch(&exit, ne, a3,
2706 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2707
2708 // Arguments adaptor case: Read the arguments length from the
2709 // adaptor frame.
2710 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2711
2712 __ bind(&exit);
2713 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002714}
2715
2716
2717void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002718 ASSERT(args->length() == 1);
2719 Label done, null, function, non_function_constructor;
2720
2721 VisitForAccumulatorValue(args->at(0));
2722
2723 // If the object is a smi, we return null.
2724 __ JumpIfSmi(v0, &null);
2725
2726 // Check that the object is a JS object but take special care of JS
2727 // functions to make sure they have 'Function' as their class.
2728 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002729 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002730
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002731 // As long as LAST_CALLABLE_SPEC_OBJECT_TYPE is the last instance type, and
2732 // FIRST_CALLABLE_SPEC_OBJECT_TYPE comes right after
2733 // LAST_NONCALLABLE_SPEC_OBJECT_TYPE, we can avoid checking for the latter.
2734 STATIC_ASSERT(LAST_TYPE == LAST_CALLABLE_SPEC_OBJECT_TYPE);
2735 STATIC_ASSERT(FIRST_CALLABLE_SPEC_OBJECT_TYPE ==
2736 LAST_NONCALLABLE_SPEC_OBJECT_TYPE + 1);
2737 __ Branch(&function, ge, a1, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002738
2739 // Check if the constructor in the map is a function.
2740 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2741 __ GetObjectType(v0, a1, a1);
2742 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2743
2744 // v0 now contains the constructor function. Grab the
2745 // instance class name from there.
2746 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2747 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2748 __ Branch(&done);
2749
2750 // Functions have class 'Function'.
2751 __ bind(&function);
2752 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2753 __ jmp(&done);
2754
2755 // Objects with a non-function constructor have class 'Object'.
2756 __ bind(&non_function_constructor);
2757 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2758 __ jmp(&done);
2759
2760 // Non-JS objects have class null.
2761 __ bind(&null);
2762 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2763
2764 // All done.
2765 __ bind(&done);
2766
2767 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002768}
2769
2770
2771void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002772 // Conditionally generate a log call.
2773 // Args:
2774 // 0 (literal string): The type of logging (corresponds to the flags).
2775 // This is used to determine whether or not to generate the log call.
2776 // 1 (string): Format string. Access the string at argument index 2
2777 // with '%2s' (see Logger::LogRuntime for all the formats).
2778 // 2 (array): Arguments to the format string.
2779 ASSERT_EQ(args->length(), 3);
2780#ifdef ENABLE_LOGGING_AND_PROFILING
2781 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2782 VisitForStackValue(args->at(1));
2783 VisitForStackValue(args->at(2));
2784 __ CallRuntime(Runtime::kLog, 2);
2785 }
2786#endif
2787 // Finally, we're expected to leave a value on the top of the stack.
2788 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2789 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002790}
2791
2792
2793void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002794 ASSERT(args->length() == 0);
2795
2796 Label slow_allocate_heapnumber;
2797 Label heapnumber_allocated;
2798
2799 // Save the new heap number in callee-saved register s0, since
2800 // we call out to external C code below.
2801 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2802 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2803 __ jmp(&heapnumber_allocated);
2804
2805 __ bind(&slow_allocate_heapnumber);
2806
2807 // Allocate a heap number.
2808 __ CallRuntime(Runtime::kNumberAlloc, 0);
2809 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2810
2811 __ bind(&heapnumber_allocated);
2812
2813 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2814 // by computing:
2815 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2816 if (CpuFeatures::IsSupported(FPU)) {
2817 __ PrepareCallCFunction(1, a0);
2818 __ li(a0, Operand(ExternalReference::isolate_address()));
2819 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2820
2821
2822 CpuFeatures::Scope scope(FPU);
2823 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2824 __ li(a1, Operand(0x41300000));
2825 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002826 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002827 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002828 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002829 // Subtract and store the result in the heap number.
2830 __ sub_d(f0, f12, f14);
2831 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2832 __ mov(v0, s0);
2833 } else {
2834 __ PrepareCallCFunction(2, a0);
2835 __ mov(a0, s0);
2836 __ li(a1, Operand(ExternalReference::isolate_address()));
2837 __ CallCFunction(
2838 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2839 }
2840
2841 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002842}
2843
2844
2845void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002846 // Load the arguments on the stack and call the stub.
2847 SubStringStub stub;
2848 ASSERT(args->length() == 3);
2849 VisitForStackValue(args->at(0));
2850 VisitForStackValue(args->at(1));
2851 VisitForStackValue(args->at(2));
2852 __ CallStub(&stub);
2853 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002854}
2855
2856
2857void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002858 // Load the arguments on the stack and call the stub.
2859 RegExpExecStub stub;
2860 ASSERT(args->length() == 4);
2861 VisitForStackValue(args->at(0));
2862 VisitForStackValue(args->at(1));
2863 VisitForStackValue(args->at(2));
2864 VisitForStackValue(args->at(3));
2865 __ CallStub(&stub);
2866 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002867}
2868
2869
2870void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002871 ASSERT(args->length() == 1);
2872
2873 VisitForAccumulatorValue(args->at(0)); // Load the object.
2874
2875 Label done;
2876 // If the object is a smi return the object.
2877 __ JumpIfSmi(v0, &done);
2878 // If the object is not a value type, return the object.
2879 __ GetObjectType(v0, a1, a1);
2880 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2881
2882 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2883
2884 __ bind(&done);
2885 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002886}
2887
2888
2889void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002890 // Load the arguments on the stack and call the runtime function.
2891 ASSERT(args->length() == 2);
2892 VisitForStackValue(args->at(0));
2893 VisitForStackValue(args->at(1));
2894 MathPowStub stub;
2895 __ CallStub(&stub);
2896 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002897}
2898
2899
2900void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002901 ASSERT(args->length() == 2);
2902
2903 VisitForStackValue(args->at(0)); // Load the object.
2904 VisitForAccumulatorValue(args->at(1)); // Load the value.
2905 __ pop(a1); // v0 = value. a1 = object.
2906
2907 Label done;
2908 // If the object is a smi, return the value.
2909 __ JumpIfSmi(a1, &done);
2910
2911 // If the object is not a value type, return the value.
2912 __ GetObjectType(a1, a2, a2);
2913 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2914
2915 // Store the value.
2916 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2917 // Update the write barrier. Save the value as it will be
2918 // overwritten by the write barrier code and is needed afterward.
2919 __ RecordWrite(a1, Operand(JSValue::kValueOffset - kHeapObjectTag), a2, a3);
2920
2921 __ bind(&done);
2922 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002923}
2924
2925
2926void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002927 ASSERT_EQ(args->length(), 1);
2928
2929 // Load the argument on the stack and call the stub.
2930 VisitForStackValue(args->at(0));
2931
2932 NumberToStringStub stub;
2933 __ CallStub(&stub);
2934 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002935}
2936
2937
2938void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002939 ASSERT(args->length() == 1);
2940
2941 VisitForAccumulatorValue(args->at(0));
2942
2943 Label done;
2944 StringCharFromCodeGenerator generator(v0, a1);
2945 generator.GenerateFast(masm_);
2946 __ jmp(&done);
2947
2948 NopRuntimeCallHelper call_helper;
2949 generator.GenerateSlow(masm_, call_helper);
2950
2951 __ bind(&done);
2952 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002953}
2954
2955
2956void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002957 ASSERT(args->length() == 2);
2958
2959 VisitForStackValue(args->at(0));
2960 VisitForAccumulatorValue(args->at(1));
2961 __ mov(a0, result_register());
2962
2963 Register object = a1;
2964 Register index = a0;
2965 Register scratch = a2;
2966 Register result = v0;
2967
2968 __ pop(object);
2969
2970 Label need_conversion;
2971 Label index_out_of_range;
2972 Label done;
2973 StringCharCodeAtGenerator generator(object,
2974 index,
2975 scratch,
2976 result,
2977 &need_conversion,
2978 &need_conversion,
2979 &index_out_of_range,
2980 STRING_INDEX_IS_NUMBER);
2981 generator.GenerateFast(masm_);
2982 __ jmp(&done);
2983
2984 __ bind(&index_out_of_range);
2985 // When the index is out of range, the spec requires us to return
2986 // NaN.
2987 __ LoadRoot(result, Heap::kNanValueRootIndex);
2988 __ jmp(&done);
2989
2990 __ bind(&need_conversion);
2991 // Load the undefined value into the result register, which will
2992 // trigger conversion.
2993 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2994 __ jmp(&done);
2995
2996 NopRuntimeCallHelper call_helper;
2997 generator.GenerateSlow(masm_, call_helper);
2998
2999 __ bind(&done);
3000 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003001}
3002
3003
3004void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003005 ASSERT(args->length() == 2);
3006
3007 VisitForStackValue(args->at(0));
3008 VisitForAccumulatorValue(args->at(1));
3009 __ mov(a0, result_register());
3010
3011 Register object = a1;
3012 Register index = a0;
3013 Register scratch1 = a2;
3014 Register scratch2 = a3;
3015 Register result = v0;
3016
3017 __ pop(object);
3018
3019 Label need_conversion;
3020 Label index_out_of_range;
3021 Label done;
3022 StringCharAtGenerator generator(object,
3023 index,
3024 scratch1,
3025 scratch2,
3026 result,
3027 &need_conversion,
3028 &need_conversion,
3029 &index_out_of_range,
3030 STRING_INDEX_IS_NUMBER);
3031 generator.GenerateFast(masm_);
3032 __ jmp(&done);
3033
3034 __ bind(&index_out_of_range);
3035 // When the index is out of range, the spec requires us to return
3036 // the empty string.
3037 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3038 __ jmp(&done);
3039
3040 __ bind(&need_conversion);
3041 // Move smi zero into the result register, which will trigger
3042 // conversion.
3043 __ li(result, Operand(Smi::FromInt(0)));
3044 __ jmp(&done);
3045
3046 NopRuntimeCallHelper call_helper;
3047 generator.GenerateSlow(masm_, call_helper);
3048
3049 __ bind(&done);
3050 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003051}
3052
3053
3054void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003055 ASSERT_EQ(2, args->length());
3056
3057 VisitForStackValue(args->at(0));
3058 VisitForStackValue(args->at(1));
3059
3060 StringAddStub stub(NO_STRING_ADD_FLAGS);
3061 __ CallStub(&stub);
3062 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003063}
3064
3065
3066void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003067 ASSERT_EQ(2, args->length());
3068
3069 VisitForStackValue(args->at(0));
3070 VisitForStackValue(args->at(1));
3071
3072 StringCompareStub stub;
3073 __ CallStub(&stub);
3074 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003075}
3076
3077
3078void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003079 // Load the argument on the stack and call the stub.
3080 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3081 TranscendentalCacheStub::TAGGED);
3082 ASSERT(args->length() == 1);
3083 VisitForStackValue(args->at(0));
3084 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3085 __ CallStub(&stub);
3086 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003087}
3088
3089
3090void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003091 // Load the argument on the stack and call the stub.
3092 TranscendentalCacheStub stub(TranscendentalCache::COS,
3093 TranscendentalCacheStub::TAGGED);
3094 ASSERT(args->length() == 1);
3095 VisitForStackValue(args->at(0));
3096 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3097 __ CallStub(&stub);
3098 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003099}
3100
3101
3102void FullCodeGenerator::EmitMathLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003103 // Load the argument on the stack and call the stub.
3104 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3105 TranscendentalCacheStub::TAGGED);
3106 ASSERT(args->length() == 1);
3107 VisitForStackValue(args->at(0));
3108 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3109 __ CallStub(&stub);
3110 context()->Plug(v0);
3111}
3112
3113
3114void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
3115 // Load the argument on the stack and call the runtime function.
3116 ASSERT(args->length() == 1);
3117 VisitForStackValue(args->at(0));
3118 __ CallRuntime(Runtime::kMath_sqrt, 1);
3119 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003120}
3121
3122
3123void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003124 ASSERT(args->length() >= 2);
3125
3126 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3127 for (int i = 0; i < arg_count + 1; i++) {
3128 VisitForStackValue(args->at(i));
3129 }
3130 VisitForAccumulatorValue(args->last()); // Function.
3131
3132 // InvokeFunction requires the function in a1. Move it in there.
3133 __ mov(a1, result_register());
3134 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003135 __ InvokeFunction(a1, count, CALL_FUNCTION,
3136 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003137 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3138 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003139}
3140
3141
3142void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003143 RegExpConstructResultStub stub;
3144 ASSERT(args->length() == 3);
3145 VisitForStackValue(args->at(0));
3146 VisitForStackValue(args->at(1));
3147 VisitForStackValue(args->at(2));
3148 __ CallStub(&stub);
3149 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003150}
3151
3152
3153void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003154 ASSERT(args->length() == 3);
3155 VisitForStackValue(args->at(0));
3156 VisitForStackValue(args->at(1));
3157 VisitForStackValue(args->at(2));
3158 Label done;
3159 Label slow_case;
3160 Register object = a0;
3161 Register index1 = a1;
3162 Register index2 = a2;
3163 Register elements = a3;
3164 Register scratch1 = t0;
3165 Register scratch2 = t1;
3166
3167 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3168 // Fetch the map and check if array is in fast case.
3169 // Check that object doesn't require security checks and
3170 // has no indexed interceptor.
3171 __ GetObjectType(object, scratch1, scratch2);
3172 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3173 // Map is now in scratch1.
3174
3175 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3176 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3177 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3178
3179 // Check the object's elements are in fast case and writable.
3180 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3181 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3182 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3183 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3184
3185 // Check that both indices are smis.
3186 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3187 __ lw(index2, MemOperand(sp, 0));
3188 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3189
3190 // Check that both indices are valid.
3191 Label not_hi;
3192 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3193 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3194 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3195 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3196 __ bind(&not_hi);
3197
3198 // Bring the address of the elements into index1 and index2.
3199 __ Addu(scratch1, elements,
3200 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3201 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3202 __ Addu(index1, scratch1, index1);
3203 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3204 __ Addu(index2, scratch1, index2);
3205
3206 // Swap elements.
3207 __ lw(scratch1, MemOperand(index1, 0));
3208 __ lw(scratch2, MemOperand(index2, 0));
3209 __ sw(scratch1, MemOperand(index2, 0));
3210 __ sw(scratch2, MemOperand(index1, 0));
3211
3212 Label new_space;
3213 __ InNewSpace(elements, scratch1, eq, &new_space);
3214 // Possible optimization: do a check that both values are Smis
3215 // (or them and test against Smi mask).
3216
3217 __ mov(scratch1, elements);
3218 __ RecordWriteHelper(elements, index1, scratch2);
3219 __ RecordWriteHelper(scratch1, index2, scratch2); // scratch1 holds elements.
3220
3221 __ bind(&new_space);
3222 // We are done. Drop elements from the stack, and return undefined.
3223 __ Drop(3);
3224 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3225 __ jmp(&done);
3226
3227 __ bind(&slow_case);
3228 __ CallRuntime(Runtime::kSwapElements, 3);
3229
3230 __ bind(&done);
3231 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003232}
3233
3234
3235void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003236 ASSERT_EQ(2, args->length());
3237
3238 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3239 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3240
3241 Handle<FixedArray> jsfunction_result_caches(
3242 isolate()->global_context()->jsfunction_result_caches());
3243 if (jsfunction_result_caches->length() <= cache_id) {
3244 __ Abort("Attempt to use undefined cache.");
3245 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3246 context()->Plug(v0);
3247 return;
3248 }
3249
3250 VisitForAccumulatorValue(args->at(1));
3251
3252 Register key = v0;
3253 Register cache = a1;
3254 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3255 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3256 __ lw(cache,
3257 ContextOperand(
3258 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3259 __ lw(cache,
3260 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3261
3262
3263 Label done, not_found;
3264 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
3265 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3266 // a2 now holds finger offset as a smi.
3267 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3268 // a3 now points to the start of fixed array elements.
3269 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3270 __ addu(a3, a3, at);
3271 // a3 now points to key of indexed element of cache.
3272 __ lw(a2, MemOperand(a3));
3273 __ Branch(&not_found, ne, key, Operand(a2));
3274
3275 __ lw(v0, MemOperand(a3, kPointerSize));
3276 __ Branch(&done);
3277
3278 __ bind(&not_found);
3279 // Call runtime to perform the lookup.
3280 __ Push(cache, key);
3281 __ CallRuntime(Runtime::kGetFromCache, 2);
3282
3283 __ bind(&done);
3284 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003285}
3286
3287
3288void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003289 ASSERT_EQ(2, args->length());
3290
3291 Register right = v0;
3292 Register left = a1;
3293 Register tmp = a2;
3294 Register tmp2 = a3;
3295
3296 VisitForStackValue(args->at(0));
3297 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3298 __ pop(left);
3299
3300 Label done, fail, ok;
3301 __ Branch(&ok, eq, left, Operand(right));
3302 // Fail if either is a non-HeapObject.
3303 __ And(tmp, left, Operand(right));
3304 __ And(at, tmp, Operand(kSmiTagMask));
3305 __ Branch(&fail, eq, at, Operand(zero_reg));
3306 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3307 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3308 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3309 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3310 __ Branch(&fail, ne, tmp, Operand(tmp2));
3311 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3312 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3313 __ Branch(&ok, eq, tmp, Operand(tmp2));
3314 __ bind(&fail);
3315 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3316 __ jmp(&done);
3317 __ bind(&ok);
3318 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3319 __ bind(&done);
3320
3321 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003322}
3323
3324
3325void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003326 VisitForAccumulatorValue(args->at(0));
3327
3328 Label materialize_true, materialize_false;
3329 Label* if_true = NULL;
3330 Label* if_false = NULL;
3331 Label* fall_through = NULL;
3332 context()->PrepareTest(&materialize_true, &materialize_false,
3333 &if_true, &if_false, &fall_through);
3334
3335 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3336 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3337
3338 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3339 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3340
3341 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003342}
3343
3344
3345void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003346 ASSERT(args->length() == 1);
3347 VisitForAccumulatorValue(args->at(0));
3348
3349 if (FLAG_debug_code) {
3350 __ AbortIfNotString(v0);
3351 }
3352
3353 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3354 __ IndexFromHash(v0, v0);
3355
3356 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003357}
3358
3359
3360void FullCodeGenerator::EmitFastAsciiArrayJoin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003361 Label bailout, done, one_char_separator, long_separator,
3362 non_trivial_array, not_size_one_array, loop,
3363 empty_separator_loop, one_char_separator_loop,
3364 one_char_separator_loop_entry, long_separator_loop;
3365
3366 ASSERT(args->length() == 2);
3367 VisitForStackValue(args->at(1));
3368 VisitForAccumulatorValue(args->at(0));
3369
3370 // All aliases of the same register have disjoint lifetimes.
3371 Register array = v0;
3372 Register elements = no_reg; // Will be v0.
3373 Register result = no_reg; // Will be v0.
3374 Register separator = a1;
3375 Register array_length = a2;
3376 Register result_pos = no_reg; // Will be a2.
3377 Register string_length = a3;
3378 Register string = t0;
3379 Register element = t1;
3380 Register elements_end = t2;
3381 Register scratch1 = t3;
3382 Register scratch2 = t5;
3383 Register scratch3 = t4;
3384 Register scratch4 = v1;
3385
3386 // Separator operand is on the stack.
3387 __ pop(separator);
3388
3389 // Check that the array is a JSArray.
3390 __ JumpIfSmi(array, &bailout);
3391 __ GetObjectType(array, scratch1, scratch2);
3392 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3393
3394 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003395 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003396
3397 // If the array has length zero, return the empty string.
3398 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3399 __ SmiUntag(array_length);
3400 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3401 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3402 __ Branch(&done);
3403
3404 __ bind(&non_trivial_array);
3405
3406 // Get the FixedArray containing array's elements.
3407 elements = array;
3408 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3409 array = no_reg; // End of array's live range.
3410
3411 // Check that all array elements are sequential ASCII strings, and
3412 // accumulate the sum of their lengths, as a smi-encoded value.
3413 __ mov(string_length, zero_reg);
3414 __ Addu(element,
3415 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3416 __ sll(elements_end, array_length, kPointerSizeLog2);
3417 __ Addu(elements_end, element, elements_end);
3418 // Loop condition: while (element < elements_end).
3419 // Live values in registers:
3420 // elements: Fixed array of strings.
3421 // array_length: Length of the fixed array of strings (not smi)
3422 // separator: Separator string
3423 // string_length: Accumulated sum of string lengths (smi).
3424 // element: Current array element.
3425 // elements_end: Array end.
3426 if (FLAG_debug_code) {
3427 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3428 array_length, Operand(zero_reg));
3429 }
3430 __ bind(&loop);
3431 __ lw(string, MemOperand(element));
3432 __ Addu(element, element, kPointerSize);
3433 __ JumpIfSmi(string, &bailout);
3434 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3435 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3436 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3437 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3438 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3439 __ BranchOnOverflow(&bailout, scratch3);
3440 __ Branch(&loop, lt, element, Operand(elements_end));
3441
3442 // If array_length is 1, return elements[0], a string.
3443 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3444 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3445 __ Branch(&done);
3446
3447 __ bind(&not_size_one_array);
3448
3449 // Live values in registers:
3450 // separator: Separator string
3451 // array_length: Length of the array.
3452 // string_length: Sum of string lengths (smi).
3453 // elements: FixedArray of strings.
3454
3455 // Check that the separator is a flat ASCII string.
3456 __ JumpIfSmi(separator, &bailout);
3457 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3458 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3459 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3460
3461 // Add (separator length times array_length) - separator length to the
3462 // string_length to get the length of the result string. array_length is not
3463 // smi but the other values are, so the result is a smi.
3464 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3465 __ Subu(string_length, string_length, Operand(scratch1));
3466 __ Mult(array_length, scratch1);
3467 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3468 // zero.
3469 __ mfhi(scratch2);
3470 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3471 __ mflo(scratch2);
3472 __ And(scratch3, scratch2, Operand(0x80000000));
3473 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3474 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3475 __ BranchOnOverflow(&bailout, scratch3);
3476 __ SmiUntag(string_length);
3477
3478 // Get first element in the array to free up the elements register to be used
3479 // for the result.
3480 __ Addu(element,
3481 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3482 result = elements; // End of live range for elements.
3483 elements = no_reg;
3484 // Live values in registers:
3485 // element: First array element
3486 // separator: Separator string
3487 // string_length: Length of result string (not smi)
3488 // array_length: Length of the array.
3489 __ AllocateAsciiString(result,
3490 string_length,
3491 scratch1,
3492 scratch2,
3493 elements_end,
3494 &bailout);
3495 // Prepare for looping. Set up elements_end to end of the array. Set
3496 // result_pos to the position of the result where to write the first
3497 // character.
3498 __ sll(elements_end, array_length, kPointerSizeLog2);
3499 __ Addu(elements_end, element, elements_end);
3500 result_pos = array_length; // End of live range for array_length.
3501 array_length = no_reg;
3502 __ Addu(result_pos,
3503 result,
3504 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3505
3506 // Check the length of the separator.
3507 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3508 __ li(at, Operand(Smi::FromInt(1)));
3509 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3510 __ Branch(&long_separator, gt, scratch1, Operand(at));
3511
3512 // Empty separator case.
3513 __ bind(&empty_separator_loop);
3514 // Live values in registers:
3515 // result_pos: the position to which we are currently copying characters.
3516 // element: Current array element.
3517 // elements_end: Array end.
3518
3519 // Copy next array element to the result.
3520 __ lw(string, MemOperand(element));
3521 __ Addu(element, element, kPointerSize);
3522 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3523 __ SmiUntag(string_length);
3524 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3525 __ CopyBytes(string, result_pos, string_length, scratch1);
3526 // End while (element < elements_end).
3527 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3528 ASSERT(result.is(v0));
3529 __ Branch(&done);
3530
3531 // One-character separator case.
3532 __ bind(&one_char_separator);
3533 // Replace separator with its ascii character value.
3534 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3535 // Jump into the loop after the code that copies the separator, so the first
3536 // element is not preceded by a separator.
3537 __ jmp(&one_char_separator_loop_entry);
3538
3539 __ bind(&one_char_separator_loop);
3540 // Live values in registers:
3541 // result_pos: the position to which we are currently copying characters.
3542 // element: Current array element.
3543 // elements_end: Array end.
3544 // separator: Single separator ascii char (in lower byte).
3545
3546 // Copy the separator character to the result.
3547 __ sb(separator, MemOperand(result_pos));
3548 __ Addu(result_pos, result_pos, 1);
3549
3550 // Copy next array element to the result.
3551 __ bind(&one_char_separator_loop_entry);
3552 __ lw(string, MemOperand(element));
3553 __ Addu(element, element, kPointerSize);
3554 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3555 __ SmiUntag(string_length);
3556 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3557 __ CopyBytes(string, result_pos, string_length, scratch1);
3558 // End while (element < elements_end).
3559 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3560 ASSERT(result.is(v0));
3561 __ Branch(&done);
3562
3563 // Long separator case (separator is more than one character). Entry is at the
3564 // label long_separator below.
3565 __ bind(&long_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 // separator: Separator string.
3571
3572 // Copy the separator to the result.
3573 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3574 __ SmiUntag(string_length);
3575 __ Addu(string,
3576 separator,
3577 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3578 __ CopyBytes(string, result_pos, string_length, scratch1);
3579
3580 __ bind(&long_separator);
3581 __ lw(string, MemOperand(element));
3582 __ Addu(element, element, kPointerSize);
3583 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3584 __ SmiUntag(string_length);
3585 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3586 __ CopyBytes(string, result_pos, string_length, scratch1);
3587 // End while (element < elements_end).
3588 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3589 ASSERT(result.is(v0));
3590 __ Branch(&done);
3591
3592 __ bind(&bailout);
3593 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3594 __ bind(&done);
3595 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003596}
3597
3598
ager@chromium.org5c838252010-02-19 08:53:10 +00003599void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003600 Handle<String> name = expr->name();
3601 if (name->length() > 0 && name->Get(0) == '_') {
3602 Comment cmnt(masm_, "[ InlineRuntimeCall");
3603 EmitInlineRuntimeCall(expr);
3604 return;
3605 }
3606
3607 Comment cmnt(masm_, "[ CallRuntime");
3608 ZoneList<Expression*>* args = expr->arguments();
3609
3610 if (expr->is_jsruntime()) {
3611 // Prepare for calling JS runtime function.
3612 __ lw(a0, GlobalObjectOperand());
3613 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3614 __ push(a0);
3615 }
3616
3617 // Push the arguments ("left-to-right").
3618 int arg_count = args->length();
3619 for (int i = 0; i < arg_count; i++) {
3620 VisitForStackValue(args->at(i));
3621 }
3622
3623 if (expr->is_jsruntime()) {
3624 // Call the JS runtime function.
3625 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003626 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003627 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00003628 isolate()->stub_cache()->ComputeCallInitialize(arg_count,
3629 NOT_IN_LOOP,
3630 mode);
3631 EmitCallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003632 // Restore context register.
3633 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3634 } else {
3635 // Call the C runtime function.
3636 __ CallRuntime(expr->function(), arg_count);
3637 }
3638 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003639}
3640
3641
3642void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003643 switch (expr->op()) {
3644 case Token::DELETE: {
3645 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
3646 Property* prop = expr->expression()->AsProperty();
3647 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
3648
3649 if (prop != NULL) {
3650 if (prop->is_synthetic()) {
3651 // Result of deleting parameters is false, even when they rewrite
3652 // to accesses on the arguments object.
3653 context()->Plug(false);
3654 } else {
3655 VisitForStackValue(prop->obj());
3656 VisitForStackValue(prop->key());
3657 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3658 __ push(a1);
3659 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3660 context()->Plug(v0);
3661 }
3662 } else if (var != NULL) {
3663 // Delete of an unqualified identifier is disallowed in strict mode
3664 // but "delete this" is.
3665 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
3666 if (var->is_global()) {
3667 __ lw(a2, GlobalObjectOperand());
3668 __ li(a1, Operand(var->name()));
3669 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3670 __ Push(a2, a1, a0);
3671 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3672 context()->Plug(v0);
3673 } else if (var->AsSlot() != NULL &&
3674 var->AsSlot()->type() != Slot::LOOKUP) {
3675 // Result of deleting non-global, non-dynamic variables is false.
3676 // The subexpression does not have side effects.
3677 context()->Plug(false);
3678 } else {
3679 // Non-global variable. Call the runtime to try to delete from the
3680 // context where the variable was introduced.
3681 __ push(context_register());
3682 __ li(a2, Operand(var->name()));
3683 __ push(a2);
3684 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3685 context()->Plug(v0);
3686 }
3687 } else {
3688 // Result of deleting non-property, non-variable reference is true.
3689 // The subexpression may have side effects.
3690 VisitForEffect(expr->expression());
3691 context()->Plug(true);
3692 }
3693 break;
3694 }
3695
3696 case Token::VOID: {
3697 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3698 VisitForEffect(expr->expression());
3699 context()->Plug(Heap::kUndefinedValueRootIndex);
3700 break;
3701 }
3702
3703 case Token::NOT: {
3704 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3705 if (context()->IsEffect()) {
3706 // Unary NOT has no side effects so it's only necessary to visit the
3707 // subexpression. Match the optimizing compiler by not branching.
3708 VisitForEffect(expr->expression());
3709 } else {
3710 Label materialize_true, materialize_false;
3711 Label* if_true = NULL;
3712 Label* if_false = NULL;
3713 Label* fall_through = NULL;
3714
3715 // Notice that the labels are swapped.
3716 context()->PrepareTest(&materialize_true, &materialize_false,
3717 &if_false, &if_true, &fall_through);
3718 if (context()->IsTest()) ForwardBailoutToChild(expr);
3719 VisitForControl(expr->expression(), if_true, if_false, fall_through);
3720 context()->Plug(if_false, if_true); // Labels swapped.
3721 }
3722 break;
3723 }
3724
3725 case Token::TYPEOF: {
3726 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3727 { StackValueContext context(this);
3728 VisitForTypeofValue(expr->expression());
3729 }
3730 __ CallRuntime(Runtime::kTypeof, 1);
3731 context()->Plug(v0);
3732 break;
3733 }
3734
3735 case Token::ADD: {
3736 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3737 VisitForAccumulatorValue(expr->expression());
3738 Label no_conversion;
3739 __ JumpIfSmi(result_register(), &no_conversion);
3740 __ mov(a0, result_register());
3741 ToNumberStub convert_stub;
3742 __ CallStub(&convert_stub);
3743 __ bind(&no_conversion);
3744 context()->Plug(result_register());
3745 break;
3746 }
3747
3748 case Token::SUB:
3749 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3750 break;
3751
3752 case Token::BIT_NOT:
3753 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3754 break;
3755
3756 default:
3757 UNREACHABLE();
3758 }
3759}
3760
3761
3762void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3763 const char* comment) {
3764 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3765 Comment cmt(masm_, comment);
3766 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3767 UnaryOverwriteMode overwrite =
3768 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003769 UnaryOpStub stub(expr->op(), overwrite);
3770 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003771 VisitForAccumulatorValue(expr->expression());
3772 SetSourcePosition(expr->position());
3773 __ mov(a0, result_register());
3774 EmitCallIC(stub.GetCode(), NULL, expr->id());
3775 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003776}
3777
3778
3779void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003780 Comment cmnt(masm_, "[ CountOperation");
3781 SetSourcePosition(expr->position());
3782
3783 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3784 // as the left-hand side.
3785 if (!expr->expression()->IsValidLeftHandSide()) {
3786 VisitForEffect(expr->expression());
3787 return;
3788 }
3789
3790 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003791 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003792 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3793 LhsKind assign_type = VARIABLE;
3794 Property* prop = expr->expression()->AsProperty();
3795 // In case of a property we use the uninitialized expression context
3796 // of the key to detect a named property.
3797 if (prop != NULL) {
3798 assign_type =
3799 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3800 }
3801
3802 // Evaluate expression and get value.
3803 if (assign_type == VARIABLE) {
3804 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3805 AccumulatorValueContext context(this);
3806 EmitVariableLoad(expr->expression()->AsVariableProxy()->var());
3807 } else {
3808 // Reserve space for result of postfix operation.
3809 if (expr->is_postfix() && !context()->IsEffect()) {
3810 __ li(at, Operand(Smi::FromInt(0)));
3811 __ push(at);
3812 }
3813 if (assign_type == NAMED_PROPERTY) {
3814 // Put the object both on the stack and in the accumulator.
3815 VisitForAccumulatorValue(prop->obj());
3816 __ push(v0);
3817 EmitNamedPropertyLoad(prop);
3818 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003819 VisitForStackValue(prop->obj());
3820 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003821 __ lw(a1, MemOperand(sp, 0));
3822 __ push(v0);
3823 EmitKeyedPropertyLoad(prop);
3824 }
3825 }
3826
3827 // We need a second deoptimization point after loading the value
3828 // in case evaluating the property load my have a side effect.
3829 if (assign_type == VARIABLE) {
3830 PrepareForBailout(expr->expression(), TOS_REG);
3831 } else {
3832 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3833 }
3834
3835 // Call ToNumber only if operand is not a smi.
3836 Label no_conversion;
3837 __ JumpIfSmi(v0, &no_conversion);
3838 __ mov(a0, v0);
3839 ToNumberStub convert_stub;
3840 __ CallStub(&convert_stub);
3841 __ bind(&no_conversion);
3842
3843 // Save result for postfix expressions.
3844 if (expr->is_postfix()) {
3845 if (!context()->IsEffect()) {
3846 // Save the result on the stack. If we have a named or keyed property
3847 // we store the result under the receiver that is currently on top
3848 // of the stack.
3849 switch (assign_type) {
3850 case VARIABLE:
3851 __ push(v0);
3852 break;
3853 case NAMED_PROPERTY:
3854 __ sw(v0, MemOperand(sp, kPointerSize));
3855 break;
3856 case KEYED_PROPERTY:
3857 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3858 break;
3859 }
3860 }
3861 }
3862 __ mov(a0, result_register());
3863
3864 // Inline smi case if we are in a loop.
3865 Label stub_call, done;
3866 JumpPatchSite patch_site(masm_);
3867
3868 int count_value = expr->op() == Token::INC ? 1 : -1;
3869 __ li(a1, Operand(Smi::FromInt(count_value)));
3870
3871 if (ShouldInlineSmiCase(expr->op())) {
3872 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
3873 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
3874
3875 // We could eliminate this smi check if we split the code at
3876 // the first smi check before calling ToNumber.
3877 patch_site.EmitJumpIfSmi(v0, &done);
3878 __ bind(&stub_call);
3879 }
3880
3881 // Record position before stub call.
3882 SetSourcePosition(expr->position());
3883
danno@chromium.org40cb8782011-05-25 07:58:50 +00003884 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003885 EmitCallIC(stub.GetCode(), &patch_site, expr->CountId());
3886 __ bind(&done);
3887
3888 // Store the value returned in v0.
3889 switch (assign_type) {
3890 case VARIABLE:
3891 if (expr->is_postfix()) {
3892 { EffectContext context(this);
3893 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3894 Token::ASSIGN);
3895 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3896 context.Plug(v0);
3897 }
3898 // For all contexts except EffectConstant we have the result on
3899 // top of the stack.
3900 if (!context()->IsEffect()) {
3901 context()->PlugTOS();
3902 }
3903 } else {
3904 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3905 Token::ASSIGN);
3906 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3907 context()->Plug(v0);
3908 }
3909 break;
3910 case NAMED_PROPERTY: {
3911 __ mov(a0, result_register()); // Value.
3912 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
3913 __ pop(a1); // Receiver.
3914 Handle<Code> ic = is_strict_mode()
3915 ? isolate()->builtins()->StoreIC_Initialize_Strict()
3916 : isolate()->builtins()->StoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00003917 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003918 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3919 if (expr->is_postfix()) {
3920 if (!context()->IsEffect()) {
3921 context()->PlugTOS();
3922 }
3923 } else {
3924 context()->Plug(v0);
3925 }
3926 break;
3927 }
3928 case KEYED_PROPERTY: {
3929 __ mov(a0, result_register()); // Value.
3930 __ pop(a1); // Key.
3931 __ pop(a2); // Receiver.
3932 Handle<Code> ic = is_strict_mode()
3933 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
3934 : isolate()->builtins()->KeyedStoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00003935 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003936 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3937 if (expr->is_postfix()) {
3938 if (!context()->IsEffect()) {
3939 context()->PlugTOS();
3940 }
3941 } else {
3942 context()->Plug(v0);
3943 }
3944 break;
3945 }
3946 }
ager@chromium.org5c838252010-02-19 08:53:10 +00003947}
3948
3949
lrn@chromium.org7516f052011-03-30 08:52:27 +00003950void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003951 VariableProxy* proxy = expr->AsVariableProxy();
3952 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
3953 Comment cmnt(masm_, "Global variable");
3954 __ lw(a0, GlobalObjectOperand());
3955 __ li(a2, Operand(proxy->name()));
3956 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
3957 // Use a regular load, not a contextual load, to avoid a reference
3958 // error.
3959 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
3960 PrepareForBailout(expr, TOS_REG);
3961 context()->Plug(v0);
3962 } else if (proxy != NULL &&
3963 proxy->var()->AsSlot() != NULL &&
3964 proxy->var()->AsSlot()->type() == Slot::LOOKUP) {
3965 Label done, slow;
3966
3967 // Generate code for loading from variables potentially shadowed
3968 // by eval-introduced variables.
3969 Slot* slot = proxy->var()->AsSlot();
3970 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
3971
3972 __ bind(&slow);
3973 __ li(a0, Operand(proxy->name()));
3974 __ Push(cp, a0);
3975 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
3976 PrepareForBailout(expr, TOS_REG);
3977 __ bind(&done);
3978
3979 context()->Plug(v0);
3980 } else {
3981 // This expression cannot throw a reference error at the top level.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003982 VisitInCurrentContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003983 }
ager@chromium.org5c838252010-02-19 08:53:10 +00003984}
3985
3986
lrn@chromium.org7516f052011-03-30 08:52:27 +00003987bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
3988 Expression* left,
3989 Expression* right,
3990 Label* if_true,
3991 Label* if_false,
3992 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003993 if (op != Token::EQ && op != Token::EQ_STRICT) return false;
3994
3995 // Check for the pattern: typeof <expression> == <string literal>.
3996 Literal* right_literal = right->AsLiteral();
3997 if (right_literal == NULL) return false;
3998 Handle<Object> right_literal_value = right_literal->handle();
3999 if (!right_literal_value->IsString()) return false;
4000 UnaryOperation* left_unary = left->AsUnaryOperation();
4001 if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
4002 Handle<String> check = Handle<String>::cast(right_literal_value);
4003
4004 { AccumulatorValueContext context(this);
4005 VisitForTypeofValue(left_unary->expression());
4006 }
4007 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4008
4009 if (check->Equals(isolate()->heap()->number_symbol())) {
4010 __ JumpIfSmi(v0, if_true);
4011 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4012 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4013 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4014 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4015 __ JumpIfSmi(v0, if_false);
4016 // Check for undetectable objects => false.
4017 __ GetObjectType(v0, v0, a1);
4018 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4019 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4020 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4021 Split(eq, a1, Operand(zero_reg),
4022 if_true, if_false, fall_through);
4023 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4024 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4025 __ Branch(if_true, eq, v0, Operand(at));
4026 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4027 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4028 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4029 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4030 __ Branch(if_true, eq, v0, Operand(at));
4031 __ JumpIfSmi(v0, if_false);
4032 // Check for undetectable objects => true.
4033 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4034 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4035 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4036 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4037 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4038 __ JumpIfSmi(v0, if_false);
4039 __ GetObjectType(v0, a1, v0); // Leave map in a1.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004040 Split(ge, v0, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004041 if_true, if_false, fall_through);
4042
4043 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4044 __ JumpIfSmi(v0, if_false);
4045 __ LoadRoot(at, Heap::kNullValueRootIndex);
4046 __ Branch(if_true, eq, v0, Operand(at));
4047 // Check for JS objects => true.
4048 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004049 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004050 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004051 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004052 // Check for undetectable objects => false.
4053 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4054 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4055 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4056 } else {
4057 if (if_false != fall_through) __ jmp(if_false);
4058 }
4059
4060 return true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00004061}
4062
4063
ager@chromium.org5c838252010-02-19 08:53:10 +00004064void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004065 Comment cmnt(masm_, "[ CompareOperation");
4066 SetSourcePosition(expr->position());
4067
4068 // Always perform the comparison for its control flow. Pack the result
4069 // into the expression's context after the comparison is performed.
4070
4071 Label materialize_true, materialize_false;
4072 Label* if_true = NULL;
4073 Label* if_false = NULL;
4074 Label* fall_through = NULL;
4075 context()->PrepareTest(&materialize_true, &materialize_false,
4076 &if_true, &if_false, &fall_through);
4077
4078 // First we try a fast inlined version of the compare when one of
4079 // the operands is a literal.
4080 Token::Value op = expr->op();
4081 Expression* left = expr->left();
4082 Expression* right = expr->right();
4083 if (TryLiteralCompare(op, left, right, if_true, if_false, fall_through)) {
4084 context()->Plug(if_true, if_false);
4085 return;
4086 }
4087
4088 VisitForStackValue(expr->left());
4089 switch (op) {
4090 case Token::IN:
4091 VisitForStackValue(expr->right());
4092 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
4093 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
4094 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4095 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4096 break;
4097
4098 case Token::INSTANCEOF: {
4099 VisitForStackValue(expr->right());
4100 InstanceofStub stub(InstanceofStub::kNoFlags);
4101 __ CallStub(&stub);
4102 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4103 // The stub returns 0 for true.
4104 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4105 break;
4106 }
4107
4108 default: {
4109 VisitForAccumulatorValue(expr->right());
4110 Condition cc = eq;
4111 bool strict = false;
4112 switch (op) {
4113 case Token::EQ_STRICT:
4114 strict = true;
4115 // Fall through.
4116 case Token::EQ:
4117 cc = eq;
4118 __ mov(a0, result_register());
4119 __ pop(a1);
4120 break;
4121 case Token::LT:
4122 cc = lt;
4123 __ mov(a0, result_register());
4124 __ pop(a1);
4125 break;
4126 case Token::GT:
4127 // Reverse left and right sides to obtain ECMA-262 conversion order.
4128 cc = lt;
4129 __ mov(a1, result_register());
4130 __ pop(a0);
4131 break;
4132 case Token::LTE:
4133 // Reverse left and right sides to obtain ECMA-262 conversion order.
4134 cc = ge;
4135 __ mov(a1, result_register());
4136 __ pop(a0);
4137 break;
4138 case Token::GTE:
4139 cc = ge;
4140 __ mov(a0, result_register());
4141 __ pop(a1);
4142 break;
4143 case Token::IN:
4144 case Token::INSTANCEOF:
4145 default:
4146 UNREACHABLE();
4147 }
4148
4149 bool inline_smi_code = ShouldInlineSmiCase(op);
4150 JumpPatchSite patch_site(masm_);
4151 if (inline_smi_code) {
4152 Label slow_case;
4153 __ Or(a2, a0, Operand(a1));
4154 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4155 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4156 __ bind(&slow_case);
4157 }
4158 // Record position and call the compare IC.
4159 SetSourcePosition(expr->position());
4160 Handle<Code> ic = CompareIC::GetUninitialized(op);
4161 EmitCallIC(ic, &patch_site, expr->id());
4162 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4163 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4164 }
4165 }
4166
4167 // Convert the result of the comparison into one expected for this
4168 // expression's context.
4169 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004170}
4171
4172
lrn@chromium.org7516f052011-03-30 08:52:27 +00004173void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004174 Comment cmnt(masm_, "[ CompareToNull");
4175 Label materialize_true, materialize_false;
4176 Label* if_true = NULL;
4177 Label* if_false = NULL;
4178 Label* fall_through = NULL;
4179 context()->PrepareTest(&materialize_true, &materialize_false,
4180 &if_true, &if_false, &fall_through);
4181
4182 VisitForAccumulatorValue(expr->expression());
4183 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4184 __ mov(a0, result_register());
4185 __ LoadRoot(a1, Heap::kNullValueRootIndex);
4186 if (expr->is_strict()) {
4187 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4188 } else {
4189 __ Branch(if_true, eq, a0, Operand(a1));
4190 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
4191 __ Branch(if_true, eq, a0, Operand(a1));
4192 __ And(at, a0, Operand(kSmiTagMask));
4193 __ Branch(if_false, eq, at, Operand(zero_reg));
4194 // It can be an undetectable object.
4195 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4196 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4197 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4198 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4199 }
4200 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004201}
4202
4203
ager@chromium.org5c838252010-02-19 08:53:10 +00004204void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004205 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4206 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004207}
4208
4209
lrn@chromium.org7516f052011-03-30 08:52:27 +00004210Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004211 return v0;
4212}
ager@chromium.org5c838252010-02-19 08:53:10 +00004213
4214
lrn@chromium.org7516f052011-03-30 08:52:27 +00004215Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004216 return cp;
4217}
4218
4219
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004220void FullCodeGenerator::EmitCallIC(Handle<Code> ic,
4221 RelocInfo::Mode mode,
4222 unsigned ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004223 ASSERT(mode == RelocInfo::CODE_TARGET ||
danno@chromium.org40cb8782011-05-25 07:58:50 +00004224 mode == RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004225 Counters* counters = isolate()->counters();
4226 switch (ic->kind()) {
4227 case Code::LOAD_IC:
4228 __ IncrementCounter(counters->named_load_full(), 1, a1, a2);
4229 break;
4230 case Code::KEYED_LOAD_IC:
4231 __ IncrementCounter(counters->keyed_load_full(), 1, a1, a2);
4232 break;
4233 case Code::STORE_IC:
4234 __ IncrementCounter(counters->named_store_full(), 1, a1, a2);
4235 break;
4236 case Code::KEYED_STORE_IC:
4237 __ IncrementCounter(counters->keyed_store_full(), 1, a1, a2);
4238 default:
4239 break;
4240 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00004241 if (ast_id == kNoASTId || mode == RelocInfo::CODE_TARGET_CONTEXT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004242 __ Call(ic, mode);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004243 } else {
4244 ASSERT(mode == RelocInfo::CODE_TARGET);
4245 mode = RelocInfo::CODE_TARGET_WITH_ID;
4246 __ CallWithAstId(ic, mode, ast_id);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004247 }
4248}
4249
4250
4251void FullCodeGenerator::EmitCallIC(Handle<Code> ic,
4252 JumpPatchSite* patch_site,
4253 unsigned ast_id) {
4254 Counters* counters = isolate()->counters();
4255 switch (ic->kind()) {
4256 case Code::LOAD_IC:
4257 __ IncrementCounter(counters->named_load_full(), 1, a1, a2);
4258 break;
4259 case Code::KEYED_LOAD_IC:
4260 __ IncrementCounter(counters->keyed_load_full(), 1, a1, a2);
4261 break;
4262 case Code::STORE_IC:
4263 __ IncrementCounter(counters->named_store_full(), 1, a1, a2);
4264 break;
4265 case Code::KEYED_STORE_IC:
4266 __ IncrementCounter(counters->keyed_store_full(), 1, a1, a2);
4267 default:
4268 break;
4269 }
4270
danno@chromium.org40cb8782011-05-25 07:58:50 +00004271 if (ast_id == kNoASTId) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004272 __ Call(ic, RelocInfo::CODE_TARGET);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004273 } else {
4274 __ CallWithAstId(ic, RelocInfo::CODE_TARGET_WITH_ID, ast_id);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004275 }
4276 if (patch_site != NULL && patch_site->is_bound()) {
4277 patch_site->EmitPatchInfo();
4278 } else {
4279 __ nop(); // Signals no inlined code.
4280 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00004281}
ager@chromium.org5c838252010-02-19 08:53:10 +00004282
4283
4284void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004285 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4286 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004287}
4288
4289
4290void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004291 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004292}
4293
4294
4295// ----------------------------------------------------------------------------
4296// Non-local control flow support.
4297
4298void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004299 ASSERT(!result_register().is(a1));
4300 // Store result register while executing finally block.
4301 __ push(result_register());
4302 // Cook return address in link register to stack (smi encoded Code* delta).
4303 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4304 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4305 ASSERT_EQ(0, kSmiTag);
4306 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4307 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004308}
4309
4310
4311void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004312 ASSERT(!result_register().is(a1));
4313 // Restore result register from stack.
4314 __ pop(a1);
4315 // Uncook return address and return.
4316 __ pop(result_register());
4317 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4318 __ sra(a1, a1, 1); // Un-smi-tag value.
4319 __ Addu(at, a1, Operand(masm_->CodeObject()));
4320 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004321}
4322
4323
4324#undef __
4325
4326} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004327
4328#endif // V8_TARGET_ARCH_MIPS