blob: 2f989bc6f3ebeff2e66e7841735e4e3ea5058b41 [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"
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +000050#include "mips/macro-assembler-mips.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000051
52namespace v8 {
53namespace internal {
54
55#define __ ACCESS_MASM(masm_)
56
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000057
danno@chromium.org40cb8782011-05-25 07:58:50 +000058static unsigned GetPropertyId(Property* property) {
danno@chromium.org40cb8782011-05-25 07:58:50 +000059 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
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000066// marker is a andi zero_reg, rx, #yyyy instruction, and rx * 0x0000ffff + yyyy
67// (raw 16 bit immediate value is used) is the delta from the pc to the first
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000068// instruction of the patchable code.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000069// The marker instruction is effectively a NOP (dest is zero_reg) and will
70// never be emitted by normal code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000071class JumpPatchSite BASE_EMBEDDED {
72 public:
73 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
74#ifdef DEBUG
75 info_emitted_ = false;
76#endif
77 }
78
79 ~JumpPatchSite() {
80 ASSERT(patch_site_.is_bound() == info_emitted_);
81 }
82
83 // When initially emitting this ensure that a jump is always generated to skip
84 // the inlined smi code.
85 void EmitJumpIfNotSmi(Register reg, Label* target) {
86 ASSERT(!patch_site_.is_bound() && !info_emitted_);
87 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
88 __ bind(&patch_site_);
89 __ andi(at, reg, 0);
90 // Always taken before patched.
91 __ Branch(target, eq, at, Operand(zero_reg));
92 }
93
94 // When initially emitting this ensure that a jump is never generated to skip
95 // the inlined smi code.
96 void EmitJumpIfSmi(Register reg, Label* target) {
97 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
98 ASSERT(!patch_site_.is_bound() && !info_emitted_);
99 __ bind(&patch_site_);
100 __ andi(at, reg, 0);
101 // Never taken before patched.
102 __ Branch(target, ne, at, Operand(zero_reg));
103 }
104
105 void EmitPatchInfo() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000106 if (patch_site_.is_bound()) {
107 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
108 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000109 __ andi(zero_reg, reg, delta_to_patch_site % kImm16Mask);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000110#ifdef DEBUG
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000111 info_emitted_ = true;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000112#endif
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000113 } else {
114 __ nop(); // Signals no inlined code.
115 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000116 }
117
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000118 private:
119 MacroAssembler* masm_;
120 Label patch_site_;
121#ifdef DEBUG
122 bool info_emitted_;
123#endif
124};
125
126
lrn@chromium.org7516f052011-03-30 08:52:27 +0000127// Generate code for a JS function. On entry to the function the receiver
128// and arguments have been pushed on the stack left to right. The actual
129// argument count matches the formal parameter count expected by the
130// function.
131//
132// The live registers are:
133// o a1: the JS function object being called (ie, ourselves)
134// o cp: our context
135// o fp: our caller's frame pointer
136// o sp: stack pointer
137// o ra: return address
138//
139// The function builds a JS frame. Please see JavaScriptFrameConstants in
140// frames-mips.h for its layout.
141void FullCodeGenerator::Generate(CompilationInfo* info) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000142 ASSERT(info_ == NULL);
143 info_ = info;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000144 scope_ = info->scope();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000145 SetFunctionPosition(function());
146 Comment cmnt(masm_, "[ function compiled by full code generator");
147
148#ifdef DEBUG
149 if (strlen(FLAG_stop_at) > 0 &&
150 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
151 __ stop("stop-at");
152 }
153#endif
154
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000155 // Strict mode functions and builtins need to replace the receiver
156 // with undefined when called as functions (without an explicit
157 // receiver object). t1 is zero for method calls and non-zero for
158 // function calls.
159 if (info->is_strict_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000160 Label ok;
161 __ Branch(&ok, eq, t1, Operand(zero_reg));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000162 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000163 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
164 __ sw(a2, MemOperand(sp, receiver_offset));
165 __ bind(&ok);
166 }
167
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000168 // Open a frame scope to indicate that there is a frame on the stack. The
169 // MANUAL indicates that the scope shouldn't actually generate code to set up
170 // the frame (that is done below).
171 FrameScope frame_scope(masm_, StackFrame::MANUAL);
172
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000173 int locals_count = info->scope()->num_stack_slots();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000174
175 __ Push(ra, fp, cp, a1);
176 if (locals_count > 0) {
177 // Load undefined value here, so the value is ready for the loop
178 // below.
179 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
180 }
181 // Adjust fp to point to caller's fp.
182 __ Addu(fp, sp, Operand(2 * kPointerSize));
183
184 { Comment cmnt(masm_, "[ Allocate locals");
185 for (int i = 0; i < locals_count; i++) {
186 __ push(at);
187 }
188 }
189
190 bool function_in_register = true;
191
192 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000193 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000194 if (heap_slots > 0) {
195 Comment cmnt(masm_, "[ Allocate local context");
196 // Argument to NewContext is the function, which is in a1.
197 __ push(a1);
198 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
199 FastNewContextStub stub(heap_slots);
200 __ CallStub(&stub);
201 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000202 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000203 }
204 function_in_register = false;
205 // Context is returned in both v0 and cp. It replaces the context
206 // passed to us. It's saved in the stack and kept live in cp.
207 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
208 // Copy any necessary parameters into the context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000209 int num_parameters = info->scope()->num_parameters();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000210 for (int i = 0; i < num_parameters; i++) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000211 Variable* var = scope()->parameter(i);
212 if (var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000213 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
214 (num_parameters - 1 - i) * kPointerSize;
215 // Load parameter from stack.
216 __ lw(a0, MemOperand(fp, parameter_offset));
217 // Store it in the context.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000218 MemOperand target = ContextOperand(cp, var->index());
219 __ sw(a0, target);
220
221 // Update the write barrier.
222 __ RecordWriteContextSlot(
223 cp, target.offset(), a0, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000224 }
225 }
226 }
227
228 Variable* arguments = scope()->arguments();
229 if (arguments != NULL) {
230 // Function uses arguments object.
231 Comment cmnt(masm_, "[ Allocate arguments object");
232 if (!function_in_register) {
233 // Load this again, if it's used by the local context below.
234 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
235 } else {
236 __ mov(a3, a1);
237 }
238 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000239 int num_parameters = info->scope()->num_parameters();
240 int offset = num_parameters * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000241 __ Addu(a2, fp,
242 Operand(StandardFrameConstants::kCallerSPOffset + offset));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000243 __ li(a1, Operand(Smi::FromInt(num_parameters)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000244 __ Push(a3, a2, a1);
245
246 // Arguments to ArgumentsAccessStub:
247 // function, receiver address, parameter count.
248 // The stub will rewrite receiever and parameter count if the previous
249 // stack frame was an arguments adapter frame.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000250 ArgumentsAccessStub::Type type;
251 if (is_strict_mode()) {
252 type = ArgumentsAccessStub::NEW_STRICT;
253 } else if (function()->has_duplicate_parameters()) {
254 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
255 } else {
256 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
257 }
258 ArgumentsAccessStub stub(type);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000259 __ CallStub(&stub);
260
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000261 SetVar(arguments, v0, a1, a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000262 }
263
264 if (FLAG_trace) {
265 __ CallRuntime(Runtime::kTraceEnter, 0);
266 }
267
268 // Visit the declarations and body unless there is an illegal
269 // redeclaration.
270 if (scope()->HasIllegalRedeclaration()) {
271 Comment cmnt(masm_, "[ Declarations");
272 scope()->VisitIllegalRedeclaration(this);
273
274 } else {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000275 PrepareForBailoutForId(AstNode::kFunctionEntryId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000276 { Comment cmnt(masm_, "[ Declarations");
277 // For named function expressions, declare the function name as a
278 // constant.
279 if (scope()->is_function_scope() && scope()->function() != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000280 int ignored = 0;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000281 VariableProxy* proxy = scope()->function();
282 ASSERT(proxy->var()->mode() == CONST ||
283 proxy->var()->mode() == CONST_HARMONY);
284 EmitDeclaration(proxy, proxy->var()->mode(), NULL, &ignored);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000285 }
286 VisitDeclarations(scope()->declarations());
287 }
288
289 { Comment cmnt(masm_, "[ Stack check");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000290 PrepareForBailoutForId(AstNode::kDeclarationsId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000291 Label ok;
292 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
293 __ Branch(&ok, hs, sp, Operand(t0));
294 StackCheckStub stub;
295 __ CallStub(&stub);
296 __ bind(&ok);
297 }
298
299 { Comment cmnt(masm_, "[ Body");
300 ASSERT(loop_depth() == 0);
301 VisitStatements(function()->body());
302 ASSERT(loop_depth() == 0);
303 }
304 }
305
306 // Always emit a 'return undefined' in case control fell off the end of
307 // the body.
308 { Comment cmnt(masm_, "[ return <undefined>;");
309 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
310 }
311 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000312}
313
314
315void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000316 ASSERT(Smi::FromInt(0) == 0);
317 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000318}
319
320
321void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000322 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
323 // to make sure it is constant. Branch may emit a skip-or-jump sequence
324 // instead of the normal Branch. It seems that the "skip" part of that
325 // sequence is about as long as this Branch would be so it is safe to ignore
326 // that.
327 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000328 Comment cmnt(masm_, "[ Stack check");
329 Label ok;
330 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000331 __ sltu(at, sp, t0);
332 __ beq(at, zero_reg, &ok);
333 // CallStub will emit a li t9, ... first, so it is safe to use the delay slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000334 StackCheckStub stub;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000335 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000336 // Record a mapping of this PC offset to the OSR id. This is used to find
337 // the AST id from the unoptimized code in order to use it as a key into
338 // the deoptimization input data found in the optimized code.
339 RecordStackCheck(stmt->OsrEntryId());
340
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000341 __ bind(&ok);
342 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
343 // Record a mapping of the OSR id to this PC. This is used if the OSR
344 // entry becomes the target of a bailout. We don't expect it to be, but
345 // we want it to work if it is.
346 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000347}
348
349
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000350void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000351 Comment cmnt(masm_, "[ Return sequence");
352 if (return_label_.is_bound()) {
353 __ Branch(&return_label_);
354 } else {
355 __ bind(&return_label_);
356 if (FLAG_trace) {
357 // Push the return value on the stack as the parameter.
358 // Runtime::TraceExit returns its parameter in v0.
359 __ push(v0);
360 __ CallRuntime(Runtime::kTraceExit, 1);
361 }
362
363#ifdef DEBUG
364 // Add a label for checking the size of the code used for returning.
365 Label check_exit_codesize;
366 masm_->bind(&check_exit_codesize);
367#endif
368 // Make sure that the constant pool is not emitted inside of the return
369 // sequence.
370 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
371 // Here we use masm_-> instead of the __ macro to avoid the code coverage
372 // tool from instrumenting as we rely on the code size here.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000373 int32_t sp_delta = (info_->scope()->num_parameters() + 1) * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000374 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
375 __ RecordJSReturn();
376 masm_->mov(sp, fp);
377 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
378 masm_->Addu(sp, sp, Operand(sp_delta));
379 masm_->Jump(ra);
380 }
381
382#ifdef DEBUG
383 // Check that the size of the code used for returning is large enough
384 // for the debugger's requirements.
385 ASSERT(Assembler::kJSReturnSequenceInstructions <=
386 masm_->InstructionsGeneratedSince(&check_exit_codesize));
387#endif
388 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000389}
390
391
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000392void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
393 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
ager@chromium.org5c838252010-02-19 08:53:10 +0000394}
395
396
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000397void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
398 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
399 codegen()->GetVar(result_register(), var);
ager@chromium.org5c838252010-02-19 08:53:10 +0000400}
401
402
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000403void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
404 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
405 codegen()->GetVar(result_register(), var);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000406 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000407}
408
409
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000410void FullCodeGenerator::TestContext::Plug(Variable* var) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000411 // For simplicity we always test the accumulator register.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000412 codegen()->GetVar(result_register(), var);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000413 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000414 codegen()->DoTest(this);
ager@chromium.org5c838252010-02-19 08:53:10 +0000415}
416
417
lrn@chromium.org7516f052011-03-30 08:52:27 +0000418void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000419}
420
421
lrn@chromium.org7516f052011-03-30 08:52:27 +0000422void FullCodeGenerator::AccumulatorValueContext::Plug(
423 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000424 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000425}
426
427
428void FullCodeGenerator::StackValueContext::Plug(
429 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000430 __ LoadRoot(result_register(), index);
431 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000432}
433
434
435void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000436 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
437 true,
438 true_label_,
439 false_label_);
440 if (index == Heap::kUndefinedValueRootIndex ||
441 index == Heap::kNullValueRootIndex ||
442 index == Heap::kFalseValueRootIndex) {
443 if (false_label_ != fall_through_) __ Branch(false_label_);
444 } else if (index == Heap::kTrueValueRootIndex) {
445 if (true_label_ != fall_through_) __ Branch(true_label_);
446 } else {
447 __ LoadRoot(result_register(), index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000448 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000449 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000450}
451
452
453void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000454}
455
456
457void FullCodeGenerator::AccumulatorValueContext::Plug(
458 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000459 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000460}
461
462
463void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000464 // Immediates cannot be pushed directly.
465 __ li(result_register(), Operand(lit));
466 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000467}
468
469
470void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000471 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
472 true,
473 true_label_,
474 false_label_);
475 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
476 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
477 if (false_label_ != fall_through_) __ Branch(false_label_);
478 } else if (lit->IsTrue() || lit->IsJSObject()) {
479 if (true_label_ != fall_through_) __ Branch(true_label_);
480 } else if (lit->IsString()) {
481 if (String::cast(*lit)->length() == 0) {
482 if (false_label_ != fall_through_) __ Branch(false_label_);
483 } else {
484 if (true_label_ != fall_through_) __ Branch(true_label_);
485 }
486 } else if (lit->IsSmi()) {
487 if (Smi::cast(*lit)->value() == 0) {
488 if (false_label_ != fall_through_) __ Branch(false_label_);
489 } else {
490 if (true_label_ != fall_through_) __ Branch(true_label_);
491 }
492 } else {
493 // For simplicity we always test the accumulator register.
494 __ li(result_register(), Operand(lit));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000495 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000496 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000497}
498
499
500void FullCodeGenerator::EffectContext::DropAndPlug(int count,
501 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000502 ASSERT(count > 0);
503 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000504}
505
506
507void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
508 int count,
509 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000510 ASSERT(count > 0);
511 __ Drop(count);
512 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000513}
514
515
516void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
517 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000518 ASSERT(count > 0);
519 if (count > 1) __ Drop(count - 1);
520 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000521}
522
523
524void FullCodeGenerator::TestContext::DropAndPlug(int count,
525 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000526 ASSERT(count > 0);
527 // For simplicity we always test the accumulator register.
528 __ Drop(count);
529 __ Move(result_register(), reg);
530 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000531 codegen()->DoTest(this);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000532}
533
534
535void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
536 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000537 ASSERT(materialize_true == materialize_false);
538 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000539}
540
541
542void FullCodeGenerator::AccumulatorValueContext::Plug(
543 Label* materialize_true,
544 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000545 Label done;
546 __ bind(materialize_true);
547 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
548 __ Branch(&done);
549 __ bind(materialize_false);
550 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
551 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000552}
553
554
555void FullCodeGenerator::StackValueContext::Plug(
556 Label* materialize_true,
557 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000558 Label done;
559 __ bind(materialize_true);
560 __ LoadRoot(at, Heap::kTrueValueRootIndex);
561 __ push(at);
562 __ Branch(&done);
563 __ bind(materialize_false);
564 __ LoadRoot(at, Heap::kFalseValueRootIndex);
565 __ push(at);
566 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000567}
568
569
570void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
571 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000572 ASSERT(materialize_true == true_label_);
573 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000574}
575
576
577void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000578}
579
580
581void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000582 Heap::RootListIndex value_root_index =
583 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
584 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000585}
586
587
588void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000589 Heap::RootListIndex value_root_index =
590 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
591 __ LoadRoot(at, value_root_index);
592 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000593}
594
595
596void FullCodeGenerator::TestContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000597 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
598 true,
599 true_label_,
600 false_label_);
601 if (flag) {
602 if (true_label_ != fall_through_) __ Branch(true_label_);
603 } else {
604 if (false_label_ != fall_through_) __ Branch(false_label_);
605 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000606}
607
608
whesse@chromium.org7b260152011-06-20 15:33:18 +0000609void FullCodeGenerator::DoTest(Expression* condition,
610 Label* if_true,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000611 Label* if_false,
612 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000613 if (CpuFeatures::IsSupported(FPU)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000614 ToBooleanStub stub(result_register());
615 __ CallStub(&stub);
616 __ mov(at, zero_reg);
617 } else {
618 // Call the runtime to find the boolean value of the source and then
619 // translate it into control flow to the pair of labels.
620 __ push(result_register());
621 __ CallRuntime(Runtime::kToBool, 1);
622 __ LoadRoot(at, Heap::kFalseValueRootIndex);
623 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000624 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000625}
626
627
lrn@chromium.org7516f052011-03-30 08:52:27 +0000628void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000629 Register lhs,
630 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000631 Label* if_true,
632 Label* if_false,
633 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000634 if (if_false == fall_through) {
635 __ Branch(if_true, cc, lhs, rhs);
636 } else if (if_true == fall_through) {
637 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
638 } else {
639 __ Branch(if_true, cc, lhs, rhs);
640 __ Branch(if_false);
641 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000642}
643
644
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000645MemOperand FullCodeGenerator::StackOperand(Variable* var) {
646 ASSERT(var->IsStackAllocated());
647 // Offset is negative because higher indexes are at lower addresses.
648 int offset = -var->index() * kPointerSize;
649 // Adjust by a (parameter or local) base offset.
650 if (var->IsParameter()) {
651 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
652 } else {
653 offset += JavaScriptFrameConstants::kLocal0Offset;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000654 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000655 return MemOperand(fp, offset);
ager@chromium.org5c838252010-02-19 08:53:10 +0000656}
657
658
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000659MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
660 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
661 if (var->IsContextSlot()) {
662 int context_chain_length = scope()->ContextChainLength(var->scope());
663 __ LoadContext(scratch, context_chain_length);
664 return ContextOperand(scratch, var->index());
665 } else {
666 return StackOperand(var);
667 }
668}
669
670
671void FullCodeGenerator::GetVar(Register dest, Variable* var) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000672 // Use destination as scratch.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000673 MemOperand location = VarOperand(var, dest);
674 __ lw(dest, location);
675}
676
677
678void FullCodeGenerator::SetVar(Variable* var,
679 Register src,
680 Register scratch0,
681 Register scratch1) {
682 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
683 ASSERT(!scratch0.is(src));
684 ASSERT(!scratch0.is(scratch1));
685 ASSERT(!scratch1.is(src));
686 MemOperand location = VarOperand(var, scratch0);
687 __ sw(src, location);
688 // Emit the write barrier code if the location is in the heap.
689 if (var->IsContextSlot()) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000690 __ RecordWriteContextSlot(scratch0,
691 location.offset(),
692 src,
693 scratch1,
694 kRAHasBeenSaved,
695 kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000696 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000697}
698
699
lrn@chromium.org7516f052011-03-30 08:52:27 +0000700void FullCodeGenerator::PrepareForBailoutBeforeSplit(State state,
701 bool should_normalize,
702 Label* if_true,
703 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000704 // Only prepare for bailouts before splits if we're in a test
705 // context. Otherwise, we let the Visit function deal with the
706 // preparation to avoid preparing with the same AST id twice.
707 if (!context()->IsTest() || !info_->IsOptimizable()) return;
708
709 Label skip;
710 if (should_normalize) __ Branch(&skip);
711
712 ForwardBailoutStack* current = forward_bailout_stack_;
713 while (current != NULL) {
714 PrepareForBailout(current->expr(), state);
715 current = current->parent();
716 }
717
718 if (should_normalize) {
719 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
720 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
721 __ bind(&skip);
722 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000723}
724
725
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000726void FullCodeGenerator::EmitDeclaration(VariableProxy* proxy,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000727 VariableMode mode,
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000728 FunctionLiteral* function,
729 int* global_count) {
730 // If it was not possible to allocate the variable at compile time, we
731 // need to "declare" it at runtime to make sure it actually exists in the
732 // local context.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000733 Variable* variable = proxy->var();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000734 bool binding_needs_init =
735 mode == CONST || mode == CONST_HARMONY || mode == LET;
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000736 switch (variable->location()) {
737 case Variable::UNALLOCATED:
738 ++(*global_count);
739 break;
740
741 case Variable::PARAMETER:
742 case Variable::LOCAL:
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000743 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000744 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000745 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000746 __ sw(result_register(), StackOperand(variable));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000747 } else if (binding_needs_init) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000748 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000749 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000750 __ sw(t0, StackOperand(variable));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000751 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000752 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000753
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000754 case Variable::CONTEXT:
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000755 // The variable in the decl always resides in the current function
756 // context.
757 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
758 if (FLAG_debug_code) {
759 // Check that we're not inside a with or catch context.
760 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
761 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
762 __ Check(ne, "Declaration in with context.",
763 a1, Operand(t0));
764 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
765 __ Check(ne, "Declaration in catch context.",
766 a1, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000767 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000768 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000769 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000770 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000771 __ sw(result_register(), ContextOperand(cp, variable->index()));
772 int offset = Context::SlotOffset(variable->index());
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000773 // We know that we have written a function, which is not a smi.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000774 __ RecordWriteContextSlot(cp,
775 offset,
776 result_register(),
777 a2,
778 kRAHasBeenSaved,
779 kDontSaveFPRegs,
780 EMIT_REMEMBERED_SET,
781 OMIT_SMI_CHECK);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000782 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000783 } else if (binding_needs_init) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000784 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000785 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000786 __ sw(at, ContextOperand(cp, variable->index()));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000787 // No write barrier since the_hole_value is in old space.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000788 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000789 }
790 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000791
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000792 case Variable::LOOKUP: {
793 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000794 __ li(a2, Operand(variable->name()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000795 // Declaration nodes are always introduced in one of four modes.
796 ASSERT(mode == VAR ||
797 mode == CONST ||
798 mode == CONST_HARMONY ||
799 mode == LET);
800 PropertyAttributes attr = (mode == CONST || mode == CONST_HARMONY)
801 ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000802 __ li(a1, Operand(Smi::FromInt(attr)));
803 // Push initial value, if any.
804 // Note: For variables we must not push an initial value (such as
805 // 'undefined') because we may have a (legal) redeclaration and we
806 // must not destroy the current value.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000807 if (function != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000808 __ Push(cp, a2, a1);
809 // Push initial value for function declaration.
810 VisitForStackValue(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000811 } else if (binding_needs_init) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000812 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
813 __ Push(cp, a2, a1, a0);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000814 } else {
815 ASSERT(Smi::FromInt(0) == 0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000816 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000817 __ Push(cp, a2, a1, a0);
818 }
819 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
820 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000821 }
822 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000823}
824
825
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000826void FullCodeGenerator::VisitDeclaration(Declaration* decl) { }
ager@chromium.org5c838252010-02-19 08:53:10 +0000827
828
829void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000830 // Call the runtime to declare the globals.
831 // The context is the first argument.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000832 __ li(a1, Operand(pairs));
833 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
834 __ Push(cp, a1, a0);
835 __ CallRuntime(Runtime::kDeclareGlobals, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000836 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000837}
838
839
lrn@chromium.org7516f052011-03-30 08:52:27 +0000840void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000841 Comment cmnt(masm_, "[ SwitchStatement");
842 Breakable nested_statement(this, stmt);
843 SetStatementPosition(stmt);
844
845 // Keep the switch value on the stack until a case matches.
846 VisitForStackValue(stmt->tag());
847 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
848
849 ZoneList<CaseClause*>* clauses = stmt->cases();
850 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
851
852 Label next_test; // Recycled for each test.
853 // Compile all the tests with branches to their bodies.
854 for (int i = 0; i < clauses->length(); i++) {
855 CaseClause* clause = clauses->at(i);
856 clause->body_target()->Unuse();
857
858 // The default is not a test, but remember it as final fall through.
859 if (clause->is_default()) {
860 default_clause = clause;
861 continue;
862 }
863
864 Comment cmnt(masm_, "[ Case comparison");
865 __ bind(&next_test);
866 next_test.Unuse();
867
868 // Compile the label expression.
869 VisitForAccumulatorValue(clause->label());
870 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
871
872 // Perform the comparison as if via '==='.
873 __ lw(a1, MemOperand(sp, 0)); // Switch value.
874 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
875 JumpPatchSite patch_site(masm_);
876 if (inline_smi_code) {
877 Label slow_case;
878 __ or_(a2, a1, a0);
879 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
880
881 __ Branch(&next_test, ne, a1, Operand(a0));
882 __ Drop(1); // Switch value is no longer needed.
883 __ Branch(clause->body_target());
884
885 __ bind(&slow_case);
886 }
887
888 // Record position before stub call for type feedback.
889 SetSourcePosition(clause->position());
890 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000891 __ Call(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000892 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +0000893
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000894 __ Branch(&next_test, ne, v0, Operand(zero_reg));
895 __ Drop(1); // Switch value is no longer needed.
896 __ Branch(clause->body_target());
897 }
898
899 // Discard the test value and jump to the default if present, otherwise to
900 // the end of the statement.
901 __ bind(&next_test);
902 __ Drop(1); // Switch value is no longer needed.
903 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000904 __ Branch(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000905 } else {
906 __ Branch(default_clause->body_target());
907 }
908
909 // Compile all the case bodies.
910 for (int i = 0; i < clauses->length(); i++) {
911 Comment cmnt(masm_, "[ Case body");
912 CaseClause* clause = clauses->at(i);
913 __ bind(clause->body_target());
914 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
915 VisitStatements(clause->statements());
916 }
917
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000918 __ bind(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000919 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000920}
921
922
923void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000924 Comment cmnt(masm_, "[ ForInStatement");
925 SetStatementPosition(stmt);
926
927 Label loop, exit;
928 ForIn loop_statement(this, stmt);
929 increment_loop_depth();
930
931 // Get the object to enumerate over. Both SpiderMonkey and JSC
932 // ignore null and undefined in contrast to the specification; see
933 // ECMA-262 section 12.6.4.
934 VisitForAccumulatorValue(stmt->enumerable());
935 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
936 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
937 __ Branch(&exit, eq, a0, Operand(at));
938 Register null_value = t1;
939 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
940 __ Branch(&exit, eq, a0, Operand(null_value));
941
942 // Convert the object to a JS object.
943 Label convert, done_convert;
944 __ JumpIfSmi(a0, &convert);
945 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000946 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000947 __ bind(&convert);
948 __ push(a0);
949 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
950 __ mov(a0, v0);
951 __ bind(&done_convert);
952 __ push(a0);
953
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000954 // Check for proxies.
955 Label call_runtime;
956 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
957 __ GetObjectType(a0, a1, a1);
958 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
959
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000960 // Check cache validity in generated code. This is a fast case for
961 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
962 // guarantee cache validity, call the runtime system to check cache
963 // validity or get the property names in a fixed array.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000964 Label next;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000965 // Preload a couple of values used in the loop.
966 Register empty_fixed_array_value = t2;
967 __ LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
968 Register empty_descriptor_array_value = t3;
969 __ LoadRoot(empty_descriptor_array_value,
970 Heap::kEmptyDescriptorArrayRootIndex);
971 __ mov(a1, a0);
972 __ bind(&next);
973
974 // Check that there are no elements. Register a1 contains the
975 // current JS object we've reached through the prototype chain.
976 __ lw(a2, FieldMemOperand(a1, JSObject::kElementsOffset));
977 __ Branch(&call_runtime, ne, a2, Operand(empty_fixed_array_value));
978
979 // Check that instance descriptors are not empty so that we can
980 // check for an enum cache. Leave the map in a2 for the subsequent
981 // prototype load.
982 __ lw(a2, FieldMemOperand(a1, HeapObject::kMapOffset));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000983 __ lw(a3, FieldMemOperand(a2, Map::kInstanceDescriptorsOrBitField3Offset));
984 __ JumpIfSmi(a3, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000985
986 // Check that there is an enum cache in the non-empty instance
987 // descriptors (a3). This is the case if the next enumeration
988 // index field does not contain a smi.
989 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumerationIndexOffset));
990 __ JumpIfSmi(a3, &call_runtime);
991
992 // For all objects but the receiver, check that the cache is empty.
993 Label check_prototype;
994 __ Branch(&check_prototype, eq, a1, Operand(a0));
995 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumCacheBridgeCacheOffset));
996 __ Branch(&call_runtime, ne, a3, Operand(empty_fixed_array_value));
997
998 // Load the prototype from the map and loop if non-null.
999 __ bind(&check_prototype);
1000 __ lw(a1, FieldMemOperand(a2, Map::kPrototypeOffset));
1001 __ Branch(&next, ne, a1, Operand(null_value));
1002
1003 // The enum cache is valid. Load the map of the object being
1004 // iterated over and use the cache for the iteration.
1005 Label use_cache;
1006 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
1007 __ Branch(&use_cache);
1008
1009 // Get the set of properties to enumerate.
1010 __ bind(&call_runtime);
1011 __ push(a0); // Duplicate the enumerable object on the stack.
1012 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1013
1014 // If we got a map from the runtime call, we can do a fast
1015 // modification check. Otherwise, we got a fixed array, and we have
1016 // to do a slow check.
1017 Label fixed_array;
1018 __ mov(a2, v0);
1019 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
1020 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1021 __ Branch(&fixed_array, ne, a1, Operand(at));
1022
1023 // We got a map in register v0. Get the enumeration cache from it.
1024 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001025 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001026 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
1027 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1028
1029 // Setup the four remaining stack slots.
1030 __ push(v0); // Map.
1031 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
1032 __ li(a0, Operand(Smi::FromInt(0)));
1033 // Push enumeration cache, enumeration cache length (as smi) and zero.
1034 __ Push(a2, a1, a0);
1035 __ jmp(&loop);
1036
1037 // We got a fixed array in register v0. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001038 Label non_proxy;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001039 __ bind(&fixed_array);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001040 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1041 __ lw(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1042 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1043 __ GetObjectType(a2, a3, a3);
1044 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1045 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1046 __ bind(&non_proxy);
1047 __ Push(a1, v0); // Smi and array
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001048 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1049 __ li(a0, Operand(Smi::FromInt(0)));
1050 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1051
1052 // Generate code for doing the condition check.
1053 __ bind(&loop);
1054 // Load the current count to a0, load the length to a1.
1055 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1056 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001057 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001058
1059 // Get the current entry of the array into register a3.
1060 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1061 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1062 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1063 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1064 __ lw(a3, MemOperand(t0)); // Current entry.
1065
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001066 // Get the expected map from the stack or a smi in the
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001067 // permanent slow case into register a2.
1068 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1069
1070 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001071 // If not, we may have to filter the key.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001072 Label update_each;
1073 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1074 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1075 __ Branch(&update_each, eq, t0, Operand(a2));
1076
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001077 // For proxies, no filtering is done.
1078 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1079 ASSERT_EQ(Smi::FromInt(0), 0);
1080 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1081
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001082 // Convert the entry to a string or (smi) 0 if it isn't a property
1083 // any more. If the property has been removed while iterating, we
1084 // just skip it.
1085 __ push(a1); // Enumerable.
1086 __ push(a3); // Current entry.
1087 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1088 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001089 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001090
1091 // Update the 'each' property or variable from the possibly filtered
1092 // entry in register a3.
1093 __ bind(&update_each);
1094 __ mov(result_register(), a3);
1095 // Perform the assignment as if via '='.
1096 { EffectContext context(this);
1097 EmitAssignment(stmt->each(), stmt->AssignmentId());
1098 }
1099
1100 // Generate code for the body of the loop.
1101 Visit(stmt->body());
1102
1103 // Generate code for the going to the next element by incrementing
1104 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001105 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001106 __ pop(a0);
1107 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1108 __ push(a0);
1109
1110 EmitStackCheck(stmt);
1111 __ Branch(&loop);
1112
1113 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001114 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001115 __ Drop(5);
1116
1117 // Exit and decrement the loop depth.
1118 __ bind(&exit);
1119 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001120}
1121
1122
1123void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1124 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001125 // Use the fast case closure allocation code that allocates in new
1126 // space for nested functions that don't need literals cloning. If
1127 // we're running with the --always-opt or the --prepare-always-opt
1128 // flag, we need to use the runtime function so that the new function
1129 // we are creating here gets a chance to have its code optimized and
1130 // doesn't just get a copy of the existing unoptimized code.
1131 if (!FLAG_always_opt &&
1132 !FLAG_prepare_always_opt &&
1133 !pretenure &&
1134 scope()->is_function_scope() &&
1135 info->num_literals() == 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001136 FastNewClosureStub stub(info->strict_mode_flag());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001137 __ li(a0, Operand(info));
1138 __ push(a0);
1139 __ CallStub(&stub);
1140 } else {
1141 __ li(a0, Operand(info));
1142 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1143 : Heap::kFalseValueRootIndex);
1144 __ Push(cp, a0, a1);
1145 __ CallRuntime(Runtime::kNewClosure, 3);
1146 }
1147 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001148}
1149
1150
1151void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001152 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001153 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001154}
1155
1156
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001157void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1158 TypeofState typeof_state,
1159 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001160 Register current = cp;
1161 Register next = a1;
1162 Register temp = a2;
1163
1164 Scope* s = scope();
1165 while (s != NULL) {
1166 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001167 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001168 // Check that extension is NULL.
1169 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1170 __ Branch(slow, ne, temp, Operand(zero_reg));
1171 }
1172 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001173 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001174 // Walk the rest of the chain without clobbering cp.
1175 current = next;
1176 }
1177 // If no outer scope calls eval, we do not need to check more
1178 // context extensions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001179 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001180 s = s->outer_scope();
1181 }
1182
1183 if (s->is_eval_scope()) {
1184 Label loop, fast;
1185 if (!current.is(next)) {
1186 __ Move(next, current);
1187 }
1188 __ bind(&loop);
1189 // Terminate at global context.
1190 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1191 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1192 __ Branch(&fast, eq, temp, Operand(t0));
1193 // Check that extension is NULL.
1194 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1195 __ Branch(slow, ne, temp, Operand(zero_reg));
1196 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001197 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001198 __ Branch(&loop);
1199 __ bind(&fast);
1200 }
1201
1202 __ lw(a0, GlobalObjectOperand());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001203 __ li(a2, Operand(var->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001204 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1205 ? RelocInfo::CODE_TARGET
1206 : RelocInfo::CODE_TARGET_CONTEXT;
1207 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001208 __ Call(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001209}
1210
1211
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001212MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1213 Label* slow) {
1214 ASSERT(var->IsContextSlot());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001215 Register context = cp;
1216 Register next = a3;
1217 Register temp = t0;
1218
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001219 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001220 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001221 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001222 // Check that extension is NULL.
1223 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1224 __ Branch(slow, ne, temp, Operand(zero_reg));
1225 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001226 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001227 // Walk the rest of the chain without clobbering cp.
1228 context = next;
1229 }
1230 }
1231 // Check that last extension is NULL.
1232 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1233 __ Branch(slow, ne, temp, Operand(zero_reg));
1234
1235 // This function is used only for loads, not stores, so it's safe to
1236 // return an cp-based operand (the write barrier cannot be allowed to
1237 // destroy the cp register).
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001238 return ContextOperand(context, var->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001239}
1240
1241
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001242void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1243 TypeofState typeof_state,
1244 Label* slow,
1245 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001246 // Generate fast-case code for variables that might be shadowed by
1247 // eval-introduced variables. Eval is used a lot without
1248 // introducing variables. In those cases, we do not want to
1249 // perform a runtime call for all variables in the scope
1250 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001251 if (var->mode() == DYNAMIC_GLOBAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001252 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001253 __ Branch(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001254 } else if (var->mode() == DYNAMIC_LOCAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001255 Variable* local = var->local_if_not_shadowed();
1256 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001257 if (local->mode() == CONST ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001258 local->mode() == CONST_HARMONY ||
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001259 local->mode() == LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001260 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1261 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001262 if (local->mode() == CONST) {
1263 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1264 __ movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001265 } else { // LET || CONST_HARMONY
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001266 __ Branch(done, ne, at, Operand(zero_reg));
1267 __ li(a0, Operand(var->name()));
1268 __ push(a0);
1269 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1270 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001271 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001272 __ Branch(done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001273 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001274}
1275
1276
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001277void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1278 // Record position before possible IC call.
1279 SetSourcePosition(proxy->position());
1280 Variable* var = proxy->var();
1281
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001282 // Three cases: global variables, lookup variables, and all other types of
1283 // variables.
1284 switch (var->location()) {
1285 case Variable::UNALLOCATED: {
1286 Comment cmnt(masm_, "Global variable");
1287 // Use inline caching. Variable name is passed in a2 and the global
1288 // object (receiver) in a0.
1289 __ lw(a0, GlobalObjectOperand());
1290 __ li(a2, Operand(var->name()));
1291 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1292 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
1293 context()->Plug(v0);
1294 break;
1295 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001296
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001297 case Variable::PARAMETER:
1298 case Variable::LOCAL:
1299 case Variable::CONTEXT: {
1300 Comment cmnt(masm_, var->IsContextSlot()
1301 ? "Context variable"
1302 : "Stack variable");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001303 if (!var->binding_needs_init()) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001304 context()->Plug(var);
1305 } else {
1306 // Let and const need a read barrier.
1307 GetVar(v0, var);
1308 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1309 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001310 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1311 // Throw a reference error when using an uninitialized let/const
1312 // binding in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001313 Label done;
1314 __ Branch(&done, ne, at, Operand(zero_reg));
1315 __ li(a0, Operand(var->name()));
1316 __ push(a0);
1317 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1318 __ bind(&done);
1319 } else {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001320 // Uninitalized const bindings outside of harmony mode are unholed.
1321 ASSERT(var->mode() == CONST);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001322 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1323 __ movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1324 }
1325 context()->Plug(v0);
1326 }
1327 break;
1328 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001329
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001330 case Variable::LOOKUP: {
1331 Label done, slow;
1332 // Generate code for loading from variables potentially shadowed
1333 // by eval-introduced variables.
1334 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1335 __ bind(&slow);
1336 Comment cmnt(masm_, "Lookup variable");
1337 __ li(a1, Operand(var->name()));
1338 __ Push(cp, a1); // Context and name.
1339 __ CallRuntime(Runtime::kLoadContextSlot, 2);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001340 __ bind(&done);
1341 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001342 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001343 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001344}
1345
1346
1347void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001348 Comment cmnt(masm_, "[ RegExpLiteral");
1349 Label materialized;
1350 // Registers will be used as follows:
1351 // t1 = materialized value (RegExp literal)
1352 // t0 = JS function, literals array
1353 // a3 = literal index
1354 // a2 = RegExp pattern
1355 // a1 = RegExp flags
1356 // a0 = RegExp literal clone
1357 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1358 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1359 int literal_offset =
1360 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1361 __ lw(t1, FieldMemOperand(t0, literal_offset));
1362 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1363 __ Branch(&materialized, ne, t1, Operand(at));
1364
1365 // Create regexp literal using runtime function.
1366 // Result will be in v0.
1367 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1368 __ li(a2, Operand(expr->pattern()));
1369 __ li(a1, Operand(expr->flags()));
1370 __ Push(t0, a3, a2, a1);
1371 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1372 __ mov(t1, v0);
1373
1374 __ bind(&materialized);
1375 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1376 Label allocated, runtime_allocate;
1377 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1378 __ jmp(&allocated);
1379
1380 __ bind(&runtime_allocate);
1381 __ push(t1);
1382 __ li(a0, Operand(Smi::FromInt(size)));
1383 __ push(a0);
1384 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1385 __ pop(t1);
1386
1387 __ bind(&allocated);
1388
1389 // After this, registers are used as follows:
1390 // v0: Newly allocated regexp.
1391 // t1: Materialized regexp.
1392 // a2: temp.
1393 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1394 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001395}
1396
1397
1398void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001399 Comment cmnt(masm_, "[ ObjectLiteral");
1400 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1401 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1402 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1403 __ li(a1, Operand(expr->constant_properties()));
1404 int flags = expr->fast_elements()
1405 ? ObjectLiteral::kFastElements
1406 : ObjectLiteral::kNoFlags;
1407 flags |= expr->has_function()
1408 ? ObjectLiteral::kHasFunction
1409 : ObjectLiteral::kNoFlags;
1410 __ li(a0, Operand(Smi::FromInt(flags)));
1411 __ Push(a3, a2, a1, a0);
1412 if (expr->depth() > 1) {
1413 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1414 } else {
1415 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
1416 }
1417
1418 // If result_saved is true the result is on top of the stack. If
1419 // result_saved is false the result is in v0.
1420 bool result_saved = false;
1421
1422 // Mark all computed expressions that are bound to a key that
1423 // is shadowed by a later occurrence of the same key. For the
1424 // marked expressions, no store code is emitted.
1425 expr->CalculateEmitStore();
1426
1427 for (int i = 0; i < expr->properties()->length(); i++) {
1428 ObjectLiteral::Property* property = expr->properties()->at(i);
1429 if (property->IsCompileTimeValue()) continue;
1430
1431 Literal* key = property->key();
1432 Expression* value = property->value();
1433 if (!result_saved) {
1434 __ push(v0); // Save result on stack.
1435 result_saved = true;
1436 }
1437 switch (property->kind()) {
1438 case ObjectLiteral::Property::CONSTANT:
1439 UNREACHABLE();
1440 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1441 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1442 // Fall through.
1443 case ObjectLiteral::Property::COMPUTED:
1444 if (key->handle()->IsSymbol()) {
1445 if (property->emit_store()) {
1446 VisitForAccumulatorValue(value);
1447 __ mov(a0, result_register());
1448 __ li(a2, Operand(key->handle()));
1449 __ lw(a1, MemOperand(sp));
1450 Handle<Code> ic = is_strict_mode()
1451 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1452 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001453 __ Call(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001454 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1455 } else {
1456 VisitForEffect(value);
1457 }
1458 break;
1459 }
1460 // Fall through.
1461 case ObjectLiteral::Property::PROTOTYPE:
1462 // Duplicate receiver on stack.
1463 __ lw(a0, MemOperand(sp));
1464 __ push(a0);
1465 VisitForStackValue(key);
1466 VisitForStackValue(value);
1467 if (property->emit_store()) {
1468 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1469 __ push(a0);
1470 __ CallRuntime(Runtime::kSetProperty, 4);
1471 } else {
1472 __ Drop(3);
1473 }
1474 break;
1475 case ObjectLiteral::Property::GETTER:
1476 case ObjectLiteral::Property::SETTER:
1477 // Duplicate receiver on stack.
1478 __ lw(a0, MemOperand(sp));
1479 __ push(a0);
1480 VisitForStackValue(key);
1481 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1482 Smi::FromInt(1) :
1483 Smi::FromInt(0)));
1484 __ push(a1);
1485 VisitForStackValue(value);
1486 __ CallRuntime(Runtime::kDefineAccessor, 4);
1487 break;
1488 }
1489 }
1490
1491 if (expr->has_function()) {
1492 ASSERT(result_saved);
1493 __ lw(a0, MemOperand(sp));
1494 __ push(a0);
1495 __ CallRuntime(Runtime::kToFastProperties, 1);
1496 }
1497
1498 if (result_saved) {
1499 context()->PlugTOS();
1500 } else {
1501 context()->Plug(v0);
1502 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001503}
1504
1505
1506void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001507 Comment cmnt(masm_, "[ ArrayLiteral");
1508
1509 ZoneList<Expression*>* subexprs = expr->values();
1510 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001511
1512 Handle<FixedArray> constant_elements = expr->constant_elements();
1513 ASSERT_EQ(2, constant_elements->length());
1514 ElementsKind constant_elements_kind =
1515 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
1516 Handle<FixedArrayBase> constant_elements_values(
1517 FixedArrayBase::cast(constant_elements->get(1)));
1518
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001519 __ mov(a0, result_register());
1520 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1521 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1522 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001523 __ li(a1, Operand(constant_elements));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001524 __ Push(a3, a2, a1);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001525 if (constant_elements_values->map() ==
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001526 isolate()->heap()->fixed_cow_array_map()) {
1527 FastCloneShallowArrayStub stub(
1528 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1529 __ CallStub(&stub);
1530 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1531 1, a1, a2);
1532 } else if (expr->depth() > 1) {
1533 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1534 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1535 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1536 } else {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001537 ASSERT(constant_elements_kind == FAST_ELEMENTS ||
1538 constant_elements_kind == FAST_SMI_ONLY_ELEMENTS ||
1539 FLAG_smi_only_arrays);
1540 FastCloneShallowArrayStub::Mode mode =
1541 constant_elements_kind == FAST_DOUBLE_ELEMENTS
1542 ? FastCloneShallowArrayStub::CLONE_DOUBLE_ELEMENTS
1543 : FastCloneShallowArrayStub::CLONE_ELEMENTS;
1544 FastCloneShallowArrayStub stub(mode, length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001545 __ CallStub(&stub);
1546 }
1547
1548 bool result_saved = false; // Is the result saved to the stack?
1549
1550 // Emit code to evaluate all the non-constant subexpressions and to store
1551 // them into the newly cloned array.
1552 for (int i = 0; i < length; i++) {
1553 Expression* subexpr = subexprs->at(i);
1554 // If the subexpression is a literal or a simple materialized literal it
1555 // is already set in the cloned array.
1556 if (subexpr->AsLiteral() != NULL ||
1557 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1558 continue;
1559 }
1560
1561 if (!result_saved) {
1562 __ push(v0);
1563 result_saved = true;
1564 }
1565 VisitForAccumulatorValue(subexpr);
1566
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001567 __ lw(t6, MemOperand(sp)); // Copy of array literal.
1568 __ lw(a1, FieldMemOperand(t6, JSObject::kElementsOffset));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001569 __ lw(a2, FieldMemOperand(t6, JSObject::kMapOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001570 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001571
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001572 Label element_done;
1573 Label double_elements;
1574 Label smi_element;
1575 Label slow_elements;
1576 Label fast_elements;
1577 __ CheckFastElements(a2, a3, &double_elements);
1578
1579 // FAST_SMI_ONLY_ELEMENTS or FAST_ELEMENTS
1580 __ JumpIfSmi(result_register(), &smi_element);
1581 __ CheckFastSmiOnlyElements(a2, a3, &fast_elements);
1582
1583 // Store into the array literal requires a elements transition. Call into
1584 // the runtime.
1585 __ bind(&slow_elements);
1586 __ push(t6); // Copy of array literal.
1587 __ li(a1, Operand(Smi::FromInt(i)));
1588 __ li(a2, Operand(Smi::FromInt(NONE))); // PropertyAttributes
1589 __ li(a3, Operand(Smi::FromInt(strict_mode_flag()))); // Strict mode.
1590 __ Push(a1, result_register(), a2, a3);
1591 __ CallRuntime(Runtime::kSetProperty, 5);
1592 __ Branch(&element_done);
1593
1594 // Array literal has ElementsKind of FAST_DOUBLE_ELEMENTS.
1595 __ bind(&double_elements);
1596 __ li(a3, Operand(Smi::FromInt(i)));
1597 __ StoreNumberToDoubleElements(result_register(), a3, t6, a1, t0, t1, t5,
1598 t3, &slow_elements);
1599 __ Branch(&element_done);
1600
1601 // Array literal has ElementsKind of FAST_ELEMENTS and value is an object.
1602 __ bind(&fast_elements);
1603 __ sw(result_register(), FieldMemOperand(a1, offset));
1604 // Update the write barrier for the array store.
1605
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001606 __ RecordWriteField(
1607 a1, offset, result_register(), a2, kRAHasBeenSaved, kDontSaveFPRegs,
1608 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001609 __ Branch(&element_done);
1610
1611 // Array literal has ElementsKind of FAST_SMI_ONLY_ELEMENTS or
1612 // FAST_ELEMENTS, and value is Smi.
1613 __ bind(&smi_element);
1614 __ sw(result_register(), FieldMemOperand(a1, offset));
1615 // Fall through
1616
1617 __ bind(&element_done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001618
1619 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1620 }
1621
1622 if (result_saved) {
1623 context()->PlugTOS();
1624 } else {
1625 context()->Plug(v0);
1626 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001627}
1628
1629
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001630void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001631 Comment cmnt(masm_, "[ Assignment");
1632 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1633 // on the left-hand side.
1634 if (!expr->target()->IsValidLeftHandSide()) {
1635 VisitForEffect(expr->target());
1636 return;
1637 }
1638
1639 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001640 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001641 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1642 LhsKind assign_type = VARIABLE;
1643 Property* property = expr->target()->AsProperty();
1644 if (property != NULL) {
1645 assign_type = (property->key()->IsPropertyName())
1646 ? NAMED_PROPERTY
1647 : KEYED_PROPERTY;
1648 }
1649
1650 // Evaluate LHS expression.
1651 switch (assign_type) {
1652 case VARIABLE:
1653 // Nothing to do here.
1654 break;
1655 case NAMED_PROPERTY:
1656 if (expr->is_compound()) {
1657 // We need the receiver both on the stack and in the accumulator.
1658 VisitForAccumulatorValue(property->obj());
1659 __ push(result_register());
1660 } else {
1661 VisitForStackValue(property->obj());
1662 }
1663 break;
1664 case KEYED_PROPERTY:
1665 // We need the key and receiver on both the stack and in v0 and a1.
1666 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001667 VisitForStackValue(property->obj());
1668 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001669 __ lw(a1, MemOperand(sp, 0));
1670 __ push(v0);
1671 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001672 VisitForStackValue(property->obj());
1673 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001674 }
1675 break;
1676 }
1677
1678 // For compound assignments we need another deoptimization point after the
1679 // variable/property load.
1680 if (expr->is_compound()) {
1681 { AccumulatorValueContext context(this);
1682 switch (assign_type) {
1683 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001684 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001685 PrepareForBailout(expr->target(), TOS_REG);
1686 break;
1687 case NAMED_PROPERTY:
1688 EmitNamedPropertyLoad(property);
1689 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1690 break;
1691 case KEYED_PROPERTY:
1692 EmitKeyedPropertyLoad(property);
1693 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1694 break;
1695 }
1696 }
1697
1698 Token::Value op = expr->binary_op();
1699 __ push(v0); // Left operand goes on the stack.
1700 VisitForAccumulatorValue(expr->value());
1701
1702 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1703 ? OVERWRITE_RIGHT
1704 : NO_OVERWRITE;
1705 SetSourcePosition(expr->position() + 1);
1706 AccumulatorValueContext context(this);
1707 if (ShouldInlineSmiCase(op)) {
1708 EmitInlineSmiBinaryOp(expr->binary_operation(),
1709 op,
1710 mode,
1711 expr->target(),
1712 expr->value());
1713 } else {
1714 EmitBinaryOp(expr->binary_operation(), op, mode);
1715 }
1716
1717 // Deoptimization point in case the binary operation may have side effects.
1718 PrepareForBailout(expr->binary_operation(), TOS_REG);
1719 } else {
1720 VisitForAccumulatorValue(expr->value());
1721 }
1722
1723 // Record source position before possible IC call.
1724 SetSourcePosition(expr->position());
1725
1726 // Store the value.
1727 switch (assign_type) {
1728 case VARIABLE:
1729 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1730 expr->op());
1731 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1732 context()->Plug(v0);
1733 break;
1734 case NAMED_PROPERTY:
1735 EmitNamedPropertyAssignment(expr);
1736 break;
1737 case KEYED_PROPERTY:
1738 EmitKeyedPropertyAssignment(expr);
1739 break;
1740 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001741}
1742
1743
ager@chromium.org5c838252010-02-19 08:53:10 +00001744void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001745 SetSourcePosition(prop->position());
1746 Literal* key = prop->key()->AsLiteral();
1747 __ mov(a0, result_register());
1748 __ li(a2, Operand(key->handle()));
1749 // Call load IC. It has arguments receiver and property name a0 and a2.
1750 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001751 __ Call(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001752}
1753
1754
1755void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001756 SetSourcePosition(prop->position());
1757 __ mov(a0, result_register());
1758 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1759 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001760 __ Call(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001761}
1762
1763
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001764void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001765 Token::Value op,
1766 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001767 Expression* left_expr,
1768 Expression* right_expr) {
1769 Label done, smi_case, stub_call;
1770
1771 Register scratch1 = a2;
1772 Register scratch2 = a3;
1773
1774 // Get the arguments.
1775 Register left = a1;
1776 Register right = a0;
1777 __ pop(left);
1778 __ mov(a0, result_register());
1779
1780 // Perform combined smi check on both operands.
1781 __ Or(scratch1, left, Operand(right));
1782 STATIC_ASSERT(kSmiTag == 0);
1783 JumpPatchSite patch_site(masm_);
1784 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1785
1786 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001787 BinaryOpStub stub(op, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001788 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001789 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001790 __ jmp(&done);
1791
1792 __ bind(&smi_case);
1793 // Smi case. This code works the same way as the smi-smi case in the type
1794 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001795 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001796 switch (op) {
1797 case Token::SAR:
1798 __ Branch(&stub_call);
1799 __ GetLeastBitsFromSmi(scratch1, right, 5);
1800 __ srav(right, left, scratch1);
1801 __ And(v0, right, Operand(~kSmiTagMask));
1802 break;
1803 case Token::SHL: {
1804 __ Branch(&stub_call);
1805 __ SmiUntag(scratch1, left);
1806 __ GetLeastBitsFromSmi(scratch2, right, 5);
1807 __ sllv(scratch1, scratch1, scratch2);
1808 __ Addu(scratch2, scratch1, Operand(0x40000000));
1809 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1810 __ SmiTag(v0, scratch1);
1811 break;
1812 }
1813 case Token::SHR: {
1814 __ Branch(&stub_call);
1815 __ SmiUntag(scratch1, left);
1816 __ GetLeastBitsFromSmi(scratch2, right, 5);
1817 __ srlv(scratch1, scratch1, scratch2);
1818 __ And(scratch2, scratch1, 0xc0000000);
1819 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1820 __ SmiTag(v0, scratch1);
1821 break;
1822 }
1823 case Token::ADD:
1824 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1825 __ BranchOnOverflow(&stub_call, scratch1);
1826 break;
1827 case Token::SUB:
1828 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1829 __ BranchOnOverflow(&stub_call, scratch1);
1830 break;
1831 case Token::MUL: {
1832 __ SmiUntag(scratch1, right);
1833 __ Mult(left, scratch1);
1834 __ mflo(scratch1);
1835 __ mfhi(scratch2);
1836 __ sra(scratch1, scratch1, 31);
1837 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1838 __ mflo(v0);
1839 __ Branch(&done, ne, v0, Operand(zero_reg));
1840 __ Addu(scratch2, right, left);
1841 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1842 ASSERT(Smi::FromInt(0) == 0);
1843 __ mov(v0, zero_reg);
1844 break;
1845 }
1846 case Token::BIT_OR:
1847 __ Or(v0, left, Operand(right));
1848 break;
1849 case Token::BIT_AND:
1850 __ And(v0, left, Operand(right));
1851 break;
1852 case Token::BIT_XOR:
1853 __ Xor(v0, left, Operand(right));
1854 break;
1855 default:
1856 UNREACHABLE();
1857 }
1858
1859 __ bind(&done);
1860 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001861}
1862
1863
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001864void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1865 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001866 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001867 __ mov(a0, result_register());
1868 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001869 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001870 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001871 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001872 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001873 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001874}
1875
1876
1877void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001878 // Invalid left-hand sides are rewritten to have a 'throw
1879 // ReferenceError' on the left-hand side.
1880 if (!expr->IsValidLeftHandSide()) {
1881 VisitForEffect(expr);
1882 return;
1883 }
1884
1885 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001886 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001887 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1888 LhsKind assign_type = VARIABLE;
1889 Property* prop = expr->AsProperty();
1890 if (prop != NULL) {
1891 assign_type = (prop->key()->IsPropertyName())
1892 ? NAMED_PROPERTY
1893 : KEYED_PROPERTY;
1894 }
1895
1896 switch (assign_type) {
1897 case VARIABLE: {
1898 Variable* var = expr->AsVariableProxy()->var();
1899 EffectContext context(this);
1900 EmitVariableAssignment(var, Token::ASSIGN);
1901 break;
1902 }
1903 case NAMED_PROPERTY: {
1904 __ push(result_register()); // Preserve value.
1905 VisitForAccumulatorValue(prop->obj());
1906 __ mov(a1, result_register());
1907 __ pop(a0); // Restore value.
1908 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1909 Handle<Code> ic = is_strict_mode()
1910 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1911 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001912 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001913 break;
1914 }
1915 case KEYED_PROPERTY: {
1916 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001917 VisitForStackValue(prop->obj());
1918 VisitForAccumulatorValue(prop->key());
1919 __ mov(a1, result_register());
1920 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001921 __ pop(a0); // Restore value.
1922 Handle<Code> ic = is_strict_mode()
1923 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
1924 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001925 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001926 break;
1927 }
1928 }
1929 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1930 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001931}
1932
1933
1934void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001935 Token::Value op) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001936 if (var->IsUnallocated()) {
1937 // Global var, const, or let.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001938 __ mov(a0, result_register());
1939 __ li(a2, Operand(var->name()));
1940 __ lw(a1, GlobalObjectOperand());
1941 Handle<Code> ic = is_strict_mode()
1942 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1943 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001944 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001945
1946 } else if (op == Token::INIT_CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001947 // Const initializers need a write barrier.
1948 ASSERT(!var->IsParameter()); // No const parameters.
1949 if (var->IsStackLocal()) {
1950 Label skip;
1951 __ lw(a1, StackOperand(var));
1952 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1953 __ Branch(&skip, ne, a1, Operand(t0));
1954 __ sw(result_register(), StackOperand(var));
1955 __ bind(&skip);
1956 } else {
1957 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
1958 // Like var declarations, const declarations are hoisted to function
1959 // scope. However, unlike var initializers, const initializers are
1960 // able to drill a hole to that function context, even from inside a
1961 // 'with' context. We thus bypass the normal static scope lookup for
1962 // var->IsContextSlot().
1963 __ push(v0);
1964 __ li(a0, Operand(var->name()));
1965 __ Push(cp, a0); // Context and name.
1966 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001967 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001968
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001969 } else if (var->mode() == LET && op != Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001970 // Non-initializing assignment to let variable needs a write barrier.
1971 if (var->IsLookupSlot()) {
1972 __ push(v0); // Value.
1973 __ li(a1, Operand(var->name()));
1974 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1975 __ Push(cp, a1, a0); // Context, name, strict mode.
1976 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1977 } else {
1978 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
1979 Label assign;
1980 MemOperand location = VarOperand(var, a1);
1981 __ lw(a3, location);
1982 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1983 __ Branch(&assign, ne, a3, Operand(t0));
1984 __ li(a3, Operand(var->name()));
1985 __ push(a3);
1986 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1987 // Perform the assignment.
1988 __ bind(&assign);
1989 __ sw(result_register(), location);
1990 if (var->IsContextSlot()) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001991 // RecordWrite may destroy all its register arguments.
1992 __ mov(a3, result_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001993 int offset = Context::SlotOffset(var->index());
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001994 __ RecordWriteContextSlot(
1995 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001996 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001997 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001998
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001999 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2000 // Assignment to var or initializing assignment to let/const
2001 // in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002002 if (var->IsStackAllocated() || var->IsContextSlot()) {
2003 MemOperand location = VarOperand(var, a1);
2004 if (FLAG_debug_code && op == Token::INIT_LET) {
2005 // Check for an uninitialized let binding.
2006 __ lw(a2, location);
2007 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2008 __ Check(eq, "Let binding re-initialization.", a2, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002009 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002010 // Perform the assignment.
2011 __ sw(v0, location);
2012 if (var->IsContextSlot()) {
2013 __ mov(a3, v0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002014 int offset = Context::SlotOffset(var->index());
2015 __ RecordWriteContextSlot(
2016 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002017 }
2018 } else {
2019 ASSERT(var->IsLookupSlot());
2020 __ push(v0); // Value.
2021 __ li(a1, Operand(var->name()));
2022 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
2023 __ Push(cp, a1, a0); // Context, name, strict mode.
2024 __ CallRuntime(Runtime::kStoreContextSlot, 4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002025 }
2026 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002027 // Non-initializing assignments to consts are ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +00002028}
2029
2030
2031void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002032 // Assignment to a property, using a named store IC.
2033 Property* prop = expr->target()->AsProperty();
2034 ASSERT(prop != NULL);
2035 ASSERT(prop->key()->AsLiteral() != NULL);
2036
2037 // If the assignment starts a block of assignments to the same object,
2038 // change to slow case to avoid the quadratic behavior of repeatedly
2039 // adding fast properties.
2040 if (expr->starts_initialization_block()) {
2041 __ push(result_register());
2042 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
2043 __ push(t0);
2044 __ CallRuntime(Runtime::kToSlowProperties, 1);
2045 __ pop(result_register());
2046 }
2047
2048 // Record source code position before IC call.
2049 SetSourcePosition(expr->position());
2050 __ mov(a0, result_register()); // Load the value.
2051 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
2052 // Load receiver to a1. Leave a copy in the stack if needed for turning the
2053 // receiver into fast case.
2054 if (expr->ends_initialization_block()) {
2055 __ lw(a1, MemOperand(sp));
2056 } else {
2057 __ pop(a1);
2058 }
2059
2060 Handle<Code> ic = is_strict_mode()
2061 ? isolate()->builtins()->StoreIC_Initialize_Strict()
2062 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002063 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002064
2065 // If the assignment ends an initialization block, revert to fast case.
2066 if (expr->ends_initialization_block()) {
2067 __ push(v0); // Result of assignment, saved even if not needed.
2068 // Receiver is under the result value.
2069 __ lw(t0, MemOperand(sp, kPointerSize));
2070 __ push(t0);
2071 __ CallRuntime(Runtime::kToFastProperties, 1);
2072 __ pop(v0);
2073 __ Drop(1);
2074 }
2075 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2076 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002077}
2078
2079
2080void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002081 // Assignment to a property, using a keyed store IC.
2082
2083 // If the assignment starts a block of assignments to the same object,
2084 // change to slow case to avoid the quadratic behavior of repeatedly
2085 // adding fast properties.
2086 if (expr->starts_initialization_block()) {
2087 __ push(result_register());
2088 // Receiver is now under the key and value.
2089 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2090 __ push(t0);
2091 __ CallRuntime(Runtime::kToSlowProperties, 1);
2092 __ pop(result_register());
2093 }
2094
2095 // Record source code position before IC call.
2096 SetSourcePosition(expr->position());
2097 // Call keyed store IC.
2098 // The arguments are:
2099 // - a0 is the value,
2100 // - a1 is the key,
2101 // - a2 is the receiver.
2102 __ mov(a0, result_register());
2103 __ pop(a1); // Key.
2104 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2105 // receiver into fast case.
2106 if (expr->ends_initialization_block()) {
2107 __ lw(a2, MemOperand(sp));
2108 } else {
2109 __ pop(a2);
2110 }
2111
2112 Handle<Code> ic = is_strict_mode()
2113 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
2114 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002115 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002116
2117 // If the assignment ends an initialization block, revert to fast case.
2118 if (expr->ends_initialization_block()) {
2119 __ push(v0); // Result of assignment, saved even if not needed.
2120 // Receiver is under the result value.
2121 __ lw(t0, MemOperand(sp, kPointerSize));
2122 __ push(t0);
2123 __ CallRuntime(Runtime::kToFastProperties, 1);
2124 __ pop(v0);
2125 __ Drop(1);
2126 }
2127 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2128 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002129}
2130
2131
2132void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002133 Comment cmnt(masm_, "[ Property");
2134 Expression* key = expr->key();
2135
2136 if (key->IsPropertyName()) {
2137 VisitForAccumulatorValue(expr->obj());
2138 EmitNamedPropertyLoad(expr);
2139 context()->Plug(v0);
2140 } else {
2141 VisitForStackValue(expr->obj());
2142 VisitForAccumulatorValue(expr->key());
2143 __ pop(a1);
2144 EmitKeyedPropertyLoad(expr);
2145 context()->Plug(v0);
2146 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002147}
2148
lrn@chromium.org7516f052011-03-30 08:52:27 +00002149
ager@chromium.org5c838252010-02-19 08:53:10 +00002150void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002151 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002152 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002153 // Code common for calls using the IC.
2154 ZoneList<Expression*>* args = expr->arguments();
2155 int arg_count = args->length();
2156 { PreservePositionScope scope(masm()->positions_recorder());
2157 for (int i = 0; i < arg_count; i++) {
2158 VisitForStackValue(args->at(i));
2159 }
2160 __ li(a2, Operand(name));
2161 }
2162 // Record source position for debugger.
2163 SetSourcePosition(expr->position());
2164 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002165 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002166 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002167 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002168 RecordJSReturnSite(expr);
2169 // Restore context register.
2170 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2171 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002172}
2173
2174
lrn@chromium.org7516f052011-03-30 08:52:27 +00002175void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002176 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002177 // Load the key.
2178 VisitForAccumulatorValue(key);
2179
2180 // Swap the name of the function and the receiver on the stack to follow
2181 // the calling convention for call ICs.
2182 __ pop(a1);
2183 __ push(v0);
2184 __ push(a1);
2185
2186 // Code common for calls using the IC.
2187 ZoneList<Expression*>* args = expr->arguments();
2188 int arg_count = args->length();
2189 { PreservePositionScope scope(masm()->positions_recorder());
2190 for (int i = 0; i < arg_count; i++) {
2191 VisitForStackValue(args->at(i));
2192 }
2193 }
2194 // Record source position for debugger.
2195 SetSourcePosition(expr->position());
2196 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002197 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002198 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002199 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002200 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002201 RecordJSReturnSite(expr);
2202 // Restore context register.
2203 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2204 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002205}
2206
2207
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002208void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002209 // Code common for calls using the call stub.
2210 ZoneList<Expression*>* args = expr->arguments();
2211 int arg_count = args->length();
2212 { PreservePositionScope scope(masm()->positions_recorder());
2213 for (int i = 0; i < arg_count; i++) {
2214 VisitForStackValue(args->at(i));
2215 }
2216 }
2217 // Record source position for debugger.
2218 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002219 CallFunctionStub stub(arg_count, flags);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002220 __ CallStub(&stub);
2221 RecordJSReturnSite(expr);
2222 // Restore context register.
2223 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2224 context()->DropAndPlug(1, v0);
2225}
2226
2227
2228void FullCodeGenerator::EmitResolvePossiblyDirectEval(ResolveEvalFlag flag,
2229 int arg_count) {
2230 // Push copy of the first argument or undefined if it doesn't exist.
2231 if (arg_count > 0) {
2232 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2233 } else {
2234 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2235 }
2236 __ push(a1);
2237
2238 // Push the receiver of the enclosing function and do runtime call.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002239 int receiver_offset = 2 + info_->scope()->num_parameters();
2240 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002241 __ push(a1);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002242 // Push the strict mode flag. In harmony mode every eval call
2243 // is a strict mode eval call.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002244 StrictModeFlag strict_mode =
2245 FLAG_harmony_scoping ? kStrictMode : strict_mode_flag();
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002246 __ li(a1, Operand(Smi::FromInt(strict_mode)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002247 __ push(a1);
2248
2249 __ CallRuntime(flag == SKIP_CONTEXT_LOOKUP
2250 ? Runtime::kResolvePossiblyDirectEvalNoLookup
2251 : Runtime::kResolvePossiblyDirectEval, 4);
ager@chromium.org5c838252010-02-19 08:53:10 +00002252}
2253
2254
2255void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002256#ifdef DEBUG
2257 // We want to verify that RecordJSReturnSite gets called on all paths
2258 // through this function. Avoid early returns.
2259 expr->return_is_recorded_ = false;
2260#endif
2261
2262 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002263 Expression* callee = expr->expression();
2264 VariableProxy* proxy = callee->AsVariableProxy();
2265 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002266
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002267 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002268 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2269 // resolve the function we need to call and the receiver of the
2270 // call. Then we call the resolved function using the given
2271 // arguments.
2272 ZoneList<Expression*>* args = expr->arguments();
2273 int arg_count = args->length();
2274
2275 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002276 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002277 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2278 __ push(a2); // Reserved receiver slot.
2279
2280 // Push the arguments.
2281 for (int i = 0; i < arg_count; i++) {
2282 VisitForStackValue(args->at(i));
2283 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002284
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002285 // If we know that eval can only be shadowed by eval-introduced
2286 // variables we attempt to load the global eval function directly
2287 // in generated code. If we succeed, there is no need to perform a
2288 // context lookup in the runtime system.
2289 Label done;
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002290 Variable* var = proxy->var();
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002291 if (!var->IsUnallocated() && var->mode() == DYNAMIC_GLOBAL) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002292 Label slow;
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002293 EmitLoadGlobalCheckExtensions(var, NOT_INSIDE_TYPEOF, &slow);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002294 // Push the function and resolve eval.
2295 __ push(v0);
2296 EmitResolvePossiblyDirectEval(SKIP_CONTEXT_LOOKUP, arg_count);
2297 __ jmp(&done);
2298 __ bind(&slow);
2299 }
2300
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002301 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002302 // resolve eval.
2303 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2304 __ push(a1);
2305 EmitResolvePossiblyDirectEval(PERFORM_CONTEXT_LOOKUP, arg_count);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002306 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002307
2308 // The runtime call returns a pair of values in v0 (function) and
2309 // v1 (receiver). Touch up the stack with the right values.
2310 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2311 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2312 }
2313 // Record source position for debugger.
2314 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002315 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002316 __ CallStub(&stub);
2317 RecordJSReturnSite(expr);
2318 // Restore context register.
2319 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2320 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002321 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002322 // Push global object as receiver for the call IC.
2323 __ lw(a0, GlobalObjectOperand());
2324 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002325 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2326 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002327 // Call to a lookup slot (dynamically introduced variable).
2328 Label slow, done;
2329
2330 { PreservePositionScope scope(masm()->positions_recorder());
2331 // Generate code for loading from variables potentially shadowed
2332 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002333 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002334 }
2335
2336 __ bind(&slow);
2337 // Call the runtime to find the function to call (returned in v0)
2338 // and the object holding it (returned in v1).
2339 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002340 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002341 __ push(a2);
2342 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2343 __ Push(v0, v1); // Function, receiver.
2344
2345 // If fast case code has been generated, emit code to push the
2346 // function and receiver and have the slow path jump around this
2347 // code.
2348 if (done.is_linked()) {
2349 Label call;
2350 __ Branch(&call);
2351 __ bind(&done);
2352 // Push function.
2353 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002354 // The receiver is implicitly the global receiver. Indicate this
2355 // by passing the hole to the call function stub.
2356 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002357 __ push(a1);
2358 __ bind(&call);
2359 }
2360
danno@chromium.org40cb8782011-05-25 07:58:50 +00002361 // The receiver is either the global receiver or an object found
2362 // by LoadContextSlot. That object could be the hole if the
2363 // receiver is implicitly the global object.
2364 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002365 } else if (property != NULL) {
2366 { PreservePositionScope scope(masm()->positions_recorder());
2367 VisitForStackValue(property->obj());
2368 }
2369 if (property->key()->IsPropertyName()) {
2370 EmitCallWithIC(expr,
2371 property->key()->AsLiteral()->handle(),
2372 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002373 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002374 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002375 }
2376 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002377 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002378 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002379 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002380 }
2381 // Load global receiver object.
2382 __ lw(a1, GlobalObjectOperand());
2383 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2384 __ push(a1);
2385 // Emit function call.
2386 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2387 }
2388
2389#ifdef DEBUG
2390 // RecordJSReturnSite should have been called.
2391 ASSERT(expr->return_is_recorded_);
2392#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002393}
2394
2395
2396void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002397 Comment cmnt(masm_, "[ CallNew");
2398 // According to ECMA-262, section 11.2.2, page 44, the function
2399 // expression in new calls must be evaluated before the
2400 // arguments.
2401
2402 // Push constructor on the stack. If it's not a function it's used as
2403 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2404 // ignored.
2405 VisitForStackValue(expr->expression());
2406
2407 // Push the arguments ("left-to-right") on the stack.
2408 ZoneList<Expression*>* args = expr->arguments();
2409 int arg_count = args->length();
2410 for (int i = 0; i < arg_count; i++) {
2411 VisitForStackValue(args->at(i));
2412 }
2413
2414 // Call the construct call builtin that handles allocation and
2415 // constructor invocation.
2416 SetSourcePosition(expr->position());
2417
2418 // Load function and argument count into a1 and a0.
2419 __ li(a0, Operand(arg_count));
2420 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2421
2422 Handle<Code> construct_builtin =
2423 isolate()->builtins()->JSConstructCall();
2424 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2425 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002426}
2427
2428
lrn@chromium.org7516f052011-03-30 08:52:27 +00002429void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002430 ASSERT(args->length() == 1);
2431
2432 VisitForAccumulatorValue(args->at(0));
2433
2434 Label materialize_true, materialize_false;
2435 Label* if_true = NULL;
2436 Label* if_false = NULL;
2437 Label* fall_through = NULL;
2438 context()->PrepareTest(&materialize_true, &materialize_false,
2439 &if_true, &if_false, &fall_through);
2440
2441 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2442 __ And(t0, v0, Operand(kSmiTagMask));
2443 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2444
2445 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002446}
2447
2448
2449void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002450 ASSERT(args->length() == 1);
2451
2452 VisitForAccumulatorValue(args->at(0));
2453
2454 Label materialize_true, materialize_false;
2455 Label* if_true = NULL;
2456 Label* if_false = NULL;
2457 Label* fall_through = NULL;
2458 context()->PrepareTest(&materialize_true, &materialize_false,
2459 &if_true, &if_false, &fall_through);
2460
2461 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2462 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2463 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2464
2465 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002466}
2467
2468
2469void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002470 ASSERT(args->length() == 1);
2471
2472 VisitForAccumulatorValue(args->at(0));
2473
2474 Label materialize_true, materialize_false;
2475 Label* if_true = NULL;
2476 Label* if_false = NULL;
2477 Label* fall_through = NULL;
2478 context()->PrepareTest(&materialize_true, &materialize_false,
2479 &if_true, &if_false, &fall_through);
2480
2481 __ JumpIfSmi(v0, if_false);
2482 __ LoadRoot(at, Heap::kNullValueRootIndex);
2483 __ Branch(if_true, eq, v0, Operand(at));
2484 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2485 // Undetectable objects behave like undefined when tested with typeof.
2486 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2487 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2488 __ Branch(if_false, ne, at, Operand(zero_reg));
2489 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002490 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002491 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002492 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2493 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002494
2495 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002496}
2497
2498
2499void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002500 ASSERT(args->length() == 1);
2501
2502 VisitForAccumulatorValue(args->at(0));
2503
2504 Label materialize_true, materialize_false;
2505 Label* if_true = NULL;
2506 Label* if_false = NULL;
2507 Label* fall_through = NULL;
2508 context()->PrepareTest(&materialize_true, &materialize_false,
2509 &if_true, &if_false, &fall_through);
2510
2511 __ JumpIfSmi(v0, if_false);
2512 __ GetObjectType(v0, a1, a1);
2513 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002514 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002515 if_true, if_false, fall_through);
2516
2517 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002518}
2519
2520
2521void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002522 ASSERT(args->length() == 1);
2523
2524 VisitForAccumulatorValue(args->at(0));
2525
2526 Label materialize_true, materialize_false;
2527 Label* if_true = NULL;
2528 Label* if_false = NULL;
2529 Label* fall_through = NULL;
2530 context()->PrepareTest(&materialize_true, &materialize_false,
2531 &if_true, &if_false, &fall_through);
2532
2533 __ JumpIfSmi(v0, if_false);
2534 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2535 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2536 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2537 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2538 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2539
2540 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002541}
2542
2543
2544void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2545 ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002546
2547 ASSERT(args->length() == 1);
2548
2549 VisitForAccumulatorValue(args->at(0));
2550
2551 Label materialize_true, materialize_false;
2552 Label* if_true = NULL;
2553 Label* if_false = NULL;
2554 Label* fall_through = NULL;
2555 context()->PrepareTest(&materialize_true, &materialize_false,
2556 &if_true, &if_false, &fall_through);
2557
2558 if (FLAG_debug_code) __ AbortIfSmi(v0);
2559
2560 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2561 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2562 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2563 __ Branch(if_true, ne, t0, Operand(zero_reg));
2564
2565 // Check for fast case object. Generate false result for slow case object.
2566 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2567 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2568 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2569 __ Branch(if_false, eq, a2, Operand(t0));
2570
2571 // Look for valueOf symbol in the descriptor array, and indicate false if
2572 // found. The type is not checked, so if it is a transition it is a false
2573 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002574 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002575 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2576 // t0: descriptor array
2577 // a3: length of descriptor array
2578 // Calculate the end of the descriptor array.
2579 STATIC_ASSERT(kSmiTag == 0);
2580 STATIC_ASSERT(kSmiTagSize == 1);
2581 STATIC_ASSERT(kPointerSize == 4);
2582 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2583 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2584 __ Addu(a2, a2, t1);
2585
2586 // Calculate location of the first key name.
2587 __ Addu(t0,
2588 t0,
2589 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2590 DescriptorArray::kFirstIndex * kPointerSize));
2591 // Loop through all the keys in the descriptor array. If one of these is the
2592 // symbol valueOf the result is false.
2593 Label entry, loop;
2594 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2595 // used in the loop below.
2596 __ li(t2, Operand(FACTORY->value_of_symbol()));
2597 __ jmp(&entry);
2598 __ bind(&loop);
2599 __ lw(a3, MemOperand(t0, 0));
2600 __ Branch(if_false, eq, a3, Operand(t2));
2601 __ Addu(t0, t0, Operand(kPointerSize));
2602 __ bind(&entry);
2603 __ Branch(&loop, ne, t0, Operand(a2));
2604
2605 // If a valueOf property is not found on the object check that it's
2606 // prototype is the un-modified String prototype. If not result is false.
2607 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2608 __ JumpIfSmi(a2, if_false);
2609 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2610 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2611 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2612 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2613 __ Branch(if_false, ne, a2, Operand(a3));
2614
2615 // Set the bit in the map to indicate that it has been checked safe for
2616 // default valueOf and set true result.
2617 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2618 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2619 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2620 __ jmp(if_true);
2621
2622 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2623 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002624}
2625
2626
2627void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002628 ASSERT(args->length() == 1);
2629
2630 VisitForAccumulatorValue(args->at(0));
2631
2632 Label materialize_true, materialize_false;
2633 Label* if_true = NULL;
2634 Label* if_false = NULL;
2635 Label* fall_through = NULL;
2636 context()->PrepareTest(&materialize_true, &materialize_false,
2637 &if_true, &if_false, &fall_through);
2638
2639 __ JumpIfSmi(v0, if_false);
2640 __ GetObjectType(v0, a1, a2);
2641 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2642 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2643 __ Branch(if_false);
2644
2645 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002646}
2647
2648
2649void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002650 ASSERT(args->length() == 1);
2651
2652 VisitForAccumulatorValue(args->at(0));
2653
2654 Label materialize_true, materialize_false;
2655 Label* if_true = NULL;
2656 Label* if_false = NULL;
2657 Label* fall_through = NULL;
2658 context()->PrepareTest(&materialize_true, &materialize_false,
2659 &if_true, &if_false, &fall_through);
2660
2661 __ JumpIfSmi(v0, if_false);
2662 __ GetObjectType(v0, a1, a1);
2663 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2664 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2665 if_true, if_false, fall_through);
2666
2667 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002668}
2669
2670
2671void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002672 ASSERT(args->length() == 1);
2673
2674 VisitForAccumulatorValue(args->at(0));
2675
2676 Label materialize_true, materialize_false;
2677 Label* if_true = NULL;
2678 Label* if_false = NULL;
2679 Label* fall_through = NULL;
2680 context()->PrepareTest(&materialize_true, &materialize_false,
2681 &if_true, &if_false, &fall_through);
2682
2683 __ JumpIfSmi(v0, if_false);
2684 __ GetObjectType(v0, a1, a1);
2685 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2686 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2687
2688 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002689}
2690
2691
2692void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002693 ASSERT(args->length() == 0);
2694
2695 Label materialize_true, materialize_false;
2696 Label* if_true = NULL;
2697 Label* if_false = NULL;
2698 Label* fall_through = NULL;
2699 context()->PrepareTest(&materialize_true, &materialize_false,
2700 &if_true, &if_false, &fall_through);
2701
2702 // Get the frame pointer for the calling frame.
2703 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2704
2705 // Skip the arguments adaptor frame if it exists.
2706 Label check_frame_marker;
2707 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2708 __ Branch(&check_frame_marker, ne,
2709 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2710 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2711
2712 // Check the marker in the calling frame.
2713 __ bind(&check_frame_marker);
2714 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
2715 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2716 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2717 if_true, if_false, fall_through);
2718
2719 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002720}
2721
2722
2723void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002724 ASSERT(args->length() == 2);
2725
2726 // Load the two objects into registers and perform the comparison.
2727 VisitForStackValue(args->at(0));
2728 VisitForAccumulatorValue(args->at(1));
2729
2730 Label materialize_true, materialize_false;
2731 Label* if_true = NULL;
2732 Label* if_false = NULL;
2733 Label* fall_through = NULL;
2734 context()->PrepareTest(&materialize_true, &materialize_false,
2735 &if_true, &if_false, &fall_through);
2736
2737 __ pop(a1);
2738 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2739 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2740
2741 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002742}
2743
2744
2745void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002746 ASSERT(args->length() == 1);
2747
2748 // ArgumentsAccessStub expects the key in a1 and the formal
2749 // parameter count in a0.
2750 VisitForAccumulatorValue(args->at(0));
2751 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002752 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002753 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2754 __ CallStub(&stub);
2755 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002756}
2757
2758
2759void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002760 ASSERT(args->length() == 0);
2761
2762 Label exit;
2763 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002764 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002765
2766 // Check if the calling frame is an arguments adaptor frame.
2767 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2768 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2769 __ Branch(&exit, ne, a3,
2770 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2771
2772 // Arguments adaptor case: Read the arguments length from the
2773 // adaptor frame.
2774 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2775
2776 __ bind(&exit);
2777 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002778}
2779
2780
2781void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002782 ASSERT(args->length() == 1);
2783 Label done, null, function, non_function_constructor;
2784
2785 VisitForAccumulatorValue(args->at(0));
2786
2787 // If the object is a smi, we return null.
2788 __ JumpIfSmi(v0, &null);
2789
2790 // Check that the object is a JS object but take special care of JS
2791 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002792 // Assume that there are only two callable types, and one of them is at
2793 // either end of the type range for JS object types. Saves extra comparisons.
2794 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002795 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002796 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002797
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002798 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2799 FIRST_SPEC_OBJECT_TYPE + 1);
2800 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002801
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002802 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2803 LAST_SPEC_OBJECT_TYPE - 1);
2804 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2805 // Assume that there is no larger type.
2806 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2807
2808 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002809 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2810 __ GetObjectType(v0, a1, a1);
2811 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2812
2813 // v0 now contains the constructor function. Grab the
2814 // instance class name from there.
2815 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2816 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2817 __ Branch(&done);
2818
2819 // Functions have class 'Function'.
2820 __ bind(&function);
2821 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2822 __ jmp(&done);
2823
2824 // Objects with a non-function constructor have class 'Object'.
2825 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002826 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002827 __ jmp(&done);
2828
2829 // Non-JS objects have class null.
2830 __ bind(&null);
2831 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2832
2833 // All done.
2834 __ bind(&done);
2835
2836 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002837}
2838
2839
2840void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002841 // Conditionally generate a log call.
2842 // Args:
2843 // 0 (literal string): The type of logging (corresponds to the flags).
2844 // This is used to determine whether or not to generate the log call.
2845 // 1 (string): Format string. Access the string at argument index 2
2846 // with '%2s' (see Logger::LogRuntime for all the formats).
2847 // 2 (array): Arguments to the format string.
2848 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002849 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2850 VisitForStackValue(args->at(1));
2851 VisitForStackValue(args->at(2));
2852 __ CallRuntime(Runtime::kLog, 2);
2853 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002854
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002855 // Finally, we're expected to leave a value on the top of the stack.
2856 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2857 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002858}
2859
2860
2861void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002862 ASSERT(args->length() == 0);
2863
2864 Label slow_allocate_heapnumber;
2865 Label heapnumber_allocated;
2866
2867 // Save the new heap number in callee-saved register s0, since
2868 // we call out to external C code below.
2869 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2870 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2871 __ jmp(&heapnumber_allocated);
2872
2873 __ bind(&slow_allocate_heapnumber);
2874
2875 // Allocate a heap number.
2876 __ CallRuntime(Runtime::kNumberAlloc, 0);
2877 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2878
2879 __ bind(&heapnumber_allocated);
2880
2881 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2882 // by computing:
2883 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2884 if (CpuFeatures::IsSupported(FPU)) {
2885 __ PrepareCallCFunction(1, a0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002886 __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
2887 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002888 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2889
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002890 CpuFeatures::Scope scope(FPU);
2891 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2892 __ li(a1, Operand(0x41300000));
2893 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002894 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002895 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002896 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002897 // Subtract and store the result in the heap number.
2898 __ sub_d(f0, f12, f14);
2899 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2900 __ mov(v0, s0);
2901 } else {
2902 __ PrepareCallCFunction(2, a0);
2903 __ mov(a0, s0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002904 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
2905 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002906 __ CallCFunction(
2907 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2908 }
2909
2910 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002911}
2912
2913
2914void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002915 // Load the arguments on the stack and call the stub.
2916 SubStringStub stub;
2917 ASSERT(args->length() == 3);
2918 VisitForStackValue(args->at(0));
2919 VisitForStackValue(args->at(1));
2920 VisitForStackValue(args->at(2));
2921 __ CallStub(&stub);
2922 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002923}
2924
2925
2926void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002927 // Load the arguments on the stack and call the stub.
2928 RegExpExecStub stub;
2929 ASSERT(args->length() == 4);
2930 VisitForStackValue(args->at(0));
2931 VisitForStackValue(args->at(1));
2932 VisitForStackValue(args->at(2));
2933 VisitForStackValue(args->at(3));
2934 __ CallStub(&stub);
2935 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002936}
2937
2938
2939void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002940 ASSERT(args->length() == 1);
2941
2942 VisitForAccumulatorValue(args->at(0)); // Load the object.
2943
2944 Label done;
2945 // If the object is a smi return the object.
2946 __ JumpIfSmi(v0, &done);
2947 // If the object is not a value type, return the object.
2948 __ GetObjectType(v0, a1, a1);
2949 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2950
2951 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2952
2953 __ bind(&done);
2954 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002955}
2956
2957
2958void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002959 // Load the arguments on the stack and call the runtime function.
2960 ASSERT(args->length() == 2);
2961 VisitForStackValue(args->at(0));
2962 VisitForStackValue(args->at(1));
2963 MathPowStub stub;
2964 __ CallStub(&stub);
2965 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002966}
2967
2968
2969void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002970 ASSERT(args->length() == 2);
2971
2972 VisitForStackValue(args->at(0)); // Load the object.
2973 VisitForAccumulatorValue(args->at(1)); // Load the value.
2974 __ pop(a1); // v0 = value. a1 = object.
2975
2976 Label done;
2977 // If the object is a smi, return the value.
2978 __ JumpIfSmi(a1, &done);
2979
2980 // If the object is not a value type, return the value.
2981 __ GetObjectType(a1, a2, a2);
2982 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2983
2984 // Store the value.
2985 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2986 // Update the write barrier. Save the value as it will be
2987 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002988 __ mov(a2, v0);
2989 __ RecordWriteField(
2990 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002991
2992 __ bind(&done);
2993 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002994}
2995
2996
2997void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002998 ASSERT_EQ(args->length(), 1);
2999
3000 // Load the argument on the stack and call the stub.
3001 VisitForStackValue(args->at(0));
3002
3003 NumberToStringStub stub;
3004 __ CallStub(&stub);
3005 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003006}
3007
3008
3009void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003010 ASSERT(args->length() == 1);
3011
3012 VisitForAccumulatorValue(args->at(0));
3013
3014 Label done;
3015 StringCharFromCodeGenerator generator(v0, a1);
3016 generator.GenerateFast(masm_);
3017 __ jmp(&done);
3018
3019 NopRuntimeCallHelper call_helper;
3020 generator.GenerateSlow(masm_, call_helper);
3021
3022 __ bind(&done);
3023 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003024}
3025
3026
3027void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003028 ASSERT(args->length() == 2);
3029
3030 VisitForStackValue(args->at(0));
3031 VisitForAccumulatorValue(args->at(1));
3032 __ mov(a0, result_register());
3033
3034 Register object = a1;
3035 Register index = a0;
3036 Register scratch = a2;
3037 Register result = v0;
3038
3039 __ pop(object);
3040
3041 Label need_conversion;
3042 Label index_out_of_range;
3043 Label done;
3044 StringCharCodeAtGenerator generator(object,
3045 index,
3046 scratch,
3047 result,
3048 &need_conversion,
3049 &need_conversion,
3050 &index_out_of_range,
3051 STRING_INDEX_IS_NUMBER);
3052 generator.GenerateFast(masm_);
3053 __ jmp(&done);
3054
3055 __ bind(&index_out_of_range);
3056 // When the index is out of range, the spec requires us to return
3057 // NaN.
3058 __ LoadRoot(result, Heap::kNanValueRootIndex);
3059 __ jmp(&done);
3060
3061 __ bind(&need_conversion);
3062 // Load the undefined value into the result register, which will
3063 // trigger conversion.
3064 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3065 __ jmp(&done);
3066
3067 NopRuntimeCallHelper call_helper;
3068 generator.GenerateSlow(masm_, call_helper);
3069
3070 __ bind(&done);
3071 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003072}
3073
3074
3075void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003076 ASSERT(args->length() == 2);
3077
3078 VisitForStackValue(args->at(0));
3079 VisitForAccumulatorValue(args->at(1));
3080 __ mov(a0, result_register());
3081
3082 Register object = a1;
3083 Register index = a0;
3084 Register scratch1 = a2;
3085 Register scratch2 = a3;
3086 Register result = v0;
3087
3088 __ pop(object);
3089
3090 Label need_conversion;
3091 Label index_out_of_range;
3092 Label done;
3093 StringCharAtGenerator generator(object,
3094 index,
3095 scratch1,
3096 scratch2,
3097 result,
3098 &need_conversion,
3099 &need_conversion,
3100 &index_out_of_range,
3101 STRING_INDEX_IS_NUMBER);
3102 generator.GenerateFast(masm_);
3103 __ jmp(&done);
3104
3105 __ bind(&index_out_of_range);
3106 // When the index is out of range, the spec requires us to return
3107 // the empty string.
3108 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3109 __ jmp(&done);
3110
3111 __ bind(&need_conversion);
3112 // Move smi zero into the result register, which will trigger
3113 // conversion.
3114 __ li(result, Operand(Smi::FromInt(0)));
3115 __ jmp(&done);
3116
3117 NopRuntimeCallHelper call_helper;
3118 generator.GenerateSlow(masm_, call_helper);
3119
3120 __ bind(&done);
3121 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003122}
3123
3124
3125void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003126 ASSERT_EQ(2, args->length());
3127
3128 VisitForStackValue(args->at(0));
3129 VisitForStackValue(args->at(1));
3130
3131 StringAddStub stub(NO_STRING_ADD_FLAGS);
3132 __ CallStub(&stub);
3133 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003134}
3135
3136
3137void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003138 ASSERT_EQ(2, args->length());
3139
3140 VisitForStackValue(args->at(0));
3141 VisitForStackValue(args->at(1));
3142
3143 StringCompareStub stub;
3144 __ CallStub(&stub);
3145 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003146}
3147
3148
3149void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003150 // Load the argument on the stack and call the stub.
3151 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3152 TranscendentalCacheStub::TAGGED);
3153 ASSERT(args->length() == 1);
3154 VisitForStackValue(args->at(0));
3155 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3156 __ CallStub(&stub);
3157 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003158}
3159
3160
3161void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003162 // Load the argument on the stack and call the stub.
3163 TranscendentalCacheStub stub(TranscendentalCache::COS,
3164 TranscendentalCacheStub::TAGGED);
3165 ASSERT(args->length() == 1);
3166 VisitForStackValue(args->at(0));
3167 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3168 __ CallStub(&stub);
3169 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003170}
3171
3172
3173void FullCodeGenerator::EmitMathLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003174 // Load the argument on the stack and call the stub.
3175 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3176 TranscendentalCacheStub::TAGGED);
3177 ASSERT(args->length() == 1);
3178 VisitForStackValue(args->at(0));
3179 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3180 __ CallStub(&stub);
3181 context()->Plug(v0);
3182}
3183
3184
3185void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
3186 // Load the argument on the stack and call the runtime function.
3187 ASSERT(args->length() == 1);
3188 VisitForStackValue(args->at(0));
3189 __ CallRuntime(Runtime::kMath_sqrt, 1);
3190 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003191}
3192
3193
3194void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003195 ASSERT(args->length() >= 2);
3196
3197 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3198 for (int i = 0; i < arg_count + 1; i++) {
3199 VisitForStackValue(args->at(i));
3200 }
3201 VisitForAccumulatorValue(args->last()); // Function.
3202
3203 // InvokeFunction requires the function in a1. Move it in there.
3204 __ mov(a1, result_register());
3205 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003206 __ InvokeFunction(a1, count, CALL_FUNCTION,
3207 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003208 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3209 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003210}
3211
3212
3213void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003214 RegExpConstructResultStub stub;
3215 ASSERT(args->length() == 3);
3216 VisitForStackValue(args->at(0));
3217 VisitForStackValue(args->at(1));
3218 VisitForStackValue(args->at(2));
3219 __ CallStub(&stub);
3220 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003221}
3222
3223
3224void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003225 ASSERT(args->length() == 3);
3226 VisitForStackValue(args->at(0));
3227 VisitForStackValue(args->at(1));
3228 VisitForStackValue(args->at(2));
3229 Label done;
3230 Label slow_case;
3231 Register object = a0;
3232 Register index1 = a1;
3233 Register index2 = a2;
3234 Register elements = a3;
3235 Register scratch1 = t0;
3236 Register scratch2 = t1;
3237
3238 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3239 // Fetch the map and check if array is in fast case.
3240 // Check that object doesn't require security checks and
3241 // has no indexed interceptor.
3242 __ GetObjectType(object, scratch1, scratch2);
3243 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3244 // Map is now in scratch1.
3245
3246 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3247 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3248 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3249
3250 // Check the object's elements are in fast case and writable.
3251 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3252 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3253 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3254 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3255
3256 // Check that both indices are smis.
3257 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3258 __ lw(index2, MemOperand(sp, 0));
3259 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3260
3261 // Check that both indices are valid.
3262 Label not_hi;
3263 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3264 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3265 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3266 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3267 __ bind(&not_hi);
3268
3269 // Bring the address of the elements into index1 and index2.
3270 __ Addu(scratch1, elements,
3271 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3272 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3273 __ Addu(index1, scratch1, index1);
3274 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3275 __ Addu(index2, scratch1, index2);
3276
3277 // Swap elements.
3278 __ lw(scratch1, MemOperand(index1, 0));
3279 __ lw(scratch2, MemOperand(index2, 0));
3280 __ sw(scratch1, MemOperand(index2, 0));
3281 __ sw(scratch2, MemOperand(index1, 0));
3282
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003283 Label no_remembered_set;
3284 __ CheckPageFlag(elements,
3285 scratch1,
3286 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3287 ne,
3288 &no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003289 // Possible optimization: do a check that both values are Smis
3290 // (or them and test against Smi mask).
3291
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003292 // We are swapping two objects in an array and the incremental marker never
3293 // pauses in the middle of scanning a single object. Therefore the
3294 // incremental marker is not disturbed, so we don't need to call the
3295 // RecordWrite stub that notifies the incremental marker.
3296 __ RememberedSetHelper(elements,
3297 index1,
3298 scratch2,
3299 kDontSaveFPRegs,
3300 MacroAssembler::kFallThroughAtEnd);
3301 __ RememberedSetHelper(elements,
3302 index2,
3303 scratch2,
3304 kDontSaveFPRegs,
3305 MacroAssembler::kFallThroughAtEnd);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003306
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003307 __ bind(&no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003308 // We are done. Drop elements from the stack, and return undefined.
3309 __ Drop(3);
3310 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3311 __ jmp(&done);
3312
3313 __ bind(&slow_case);
3314 __ CallRuntime(Runtime::kSwapElements, 3);
3315
3316 __ bind(&done);
3317 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003318}
3319
3320
3321void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003322 ASSERT_EQ(2, args->length());
3323
3324 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3325 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3326
3327 Handle<FixedArray> jsfunction_result_caches(
3328 isolate()->global_context()->jsfunction_result_caches());
3329 if (jsfunction_result_caches->length() <= cache_id) {
3330 __ Abort("Attempt to use undefined cache.");
3331 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3332 context()->Plug(v0);
3333 return;
3334 }
3335
3336 VisitForAccumulatorValue(args->at(1));
3337
3338 Register key = v0;
3339 Register cache = a1;
3340 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3341 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3342 __ lw(cache,
3343 ContextOperand(
3344 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3345 __ lw(cache,
3346 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3347
3348
3349 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003350 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003351 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3352 // a2 now holds finger offset as a smi.
3353 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3354 // a3 now points to the start of fixed array elements.
3355 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3356 __ addu(a3, a3, at);
3357 // a3 now points to key of indexed element of cache.
3358 __ lw(a2, MemOperand(a3));
3359 __ Branch(&not_found, ne, key, Operand(a2));
3360
3361 __ lw(v0, MemOperand(a3, kPointerSize));
3362 __ Branch(&done);
3363
3364 __ bind(&not_found);
3365 // Call runtime to perform the lookup.
3366 __ Push(cache, key);
3367 __ CallRuntime(Runtime::kGetFromCache, 2);
3368
3369 __ bind(&done);
3370 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003371}
3372
3373
3374void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003375 ASSERT_EQ(2, args->length());
3376
3377 Register right = v0;
3378 Register left = a1;
3379 Register tmp = a2;
3380 Register tmp2 = a3;
3381
3382 VisitForStackValue(args->at(0));
3383 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3384 __ pop(left);
3385
3386 Label done, fail, ok;
3387 __ Branch(&ok, eq, left, Operand(right));
3388 // Fail if either is a non-HeapObject.
3389 __ And(tmp, left, Operand(right));
3390 __ And(at, tmp, Operand(kSmiTagMask));
3391 __ Branch(&fail, eq, at, Operand(zero_reg));
3392 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3393 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3394 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3395 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3396 __ Branch(&fail, ne, tmp, Operand(tmp2));
3397 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3398 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3399 __ Branch(&ok, eq, tmp, Operand(tmp2));
3400 __ bind(&fail);
3401 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3402 __ jmp(&done);
3403 __ bind(&ok);
3404 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3405 __ bind(&done);
3406
3407 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003408}
3409
3410
3411void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003412 VisitForAccumulatorValue(args->at(0));
3413
3414 Label materialize_true, materialize_false;
3415 Label* if_true = NULL;
3416 Label* if_false = NULL;
3417 Label* fall_through = NULL;
3418 context()->PrepareTest(&materialize_true, &materialize_false,
3419 &if_true, &if_false, &fall_through);
3420
3421 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3422 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3423
3424 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3425 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3426
3427 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003428}
3429
3430
3431void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003432 ASSERT(args->length() == 1);
3433 VisitForAccumulatorValue(args->at(0));
3434
3435 if (FLAG_debug_code) {
3436 __ AbortIfNotString(v0);
3437 }
3438
3439 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3440 __ IndexFromHash(v0, v0);
3441
3442 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003443}
3444
3445
3446void FullCodeGenerator::EmitFastAsciiArrayJoin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003447 Label bailout, done, one_char_separator, long_separator,
3448 non_trivial_array, not_size_one_array, loop,
3449 empty_separator_loop, one_char_separator_loop,
3450 one_char_separator_loop_entry, long_separator_loop;
3451
3452 ASSERT(args->length() == 2);
3453 VisitForStackValue(args->at(1));
3454 VisitForAccumulatorValue(args->at(0));
3455
3456 // All aliases of the same register have disjoint lifetimes.
3457 Register array = v0;
3458 Register elements = no_reg; // Will be v0.
3459 Register result = no_reg; // Will be v0.
3460 Register separator = a1;
3461 Register array_length = a2;
3462 Register result_pos = no_reg; // Will be a2.
3463 Register string_length = a3;
3464 Register string = t0;
3465 Register element = t1;
3466 Register elements_end = t2;
3467 Register scratch1 = t3;
3468 Register scratch2 = t5;
3469 Register scratch3 = t4;
3470 Register scratch4 = v1;
3471
3472 // Separator operand is on the stack.
3473 __ pop(separator);
3474
3475 // Check that the array is a JSArray.
3476 __ JumpIfSmi(array, &bailout);
3477 __ GetObjectType(array, scratch1, scratch2);
3478 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3479
3480 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003481 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003482
3483 // If the array has length zero, return the empty string.
3484 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3485 __ SmiUntag(array_length);
3486 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3487 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3488 __ Branch(&done);
3489
3490 __ bind(&non_trivial_array);
3491
3492 // Get the FixedArray containing array's elements.
3493 elements = array;
3494 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3495 array = no_reg; // End of array's live range.
3496
3497 // Check that all array elements are sequential ASCII strings, and
3498 // accumulate the sum of their lengths, as a smi-encoded value.
3499 __ mov(string_length, zero_reg);
3500 __ Addu(element,
3501 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3502 __ sll(elements_end, array_length, kPointerSizeLog2);
3503 __ Addu(elements_end, element, elements_end);
3504 // Loop condition: while (element < elements_end).
3505 // Live values in registers:
3506 // elements: Fixed array of strings.
3507 // array_length: Length of the fixed array of strings (not smi)
3508 // separator: Separator string
3509 // string_length: Accumulated sum of string lengths (smi).
3510 // element: Current array element.
3511 // elements_end: Array end.
3512 if (FLAG_debug_code) {
3513 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3514 array_length, Operand(zero_reg));
3515 }
3516 __ bind(&loop);
3517 __ lw(string, MemOperand(element));
3518 __ Addu(element, element, kPointerSize);
3519 __ JumpIfSmi(string, &bailout);
3520 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3521 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3522 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3523 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3524 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3525 __ BranchOnOverflow(&bailout, scratch3);
3526 __ Branch(&loop, lt, element, Operand(elements_end));
3527
3528 // If array_length is 1, return elements[0], a string.
3529 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3530 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3531 __ Branch(&done);
3532
3533 __ bind(&not_size_one_array);
3534
3535 // Live values in registers:
3536 // separator: Separator string
3537 // array_length: Length of the array.
3538 // string_length: Sum of string lengths (smi).
3539 // elements: FixedArray of strings.
3540
3541 // Check that the separator is a flat ASCII string.
3542 __ JumpIfSmi(separator, &bailout);
3543 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3544 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3545 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3546
3547 // Add (separator length times array_length) - separator length to the
3548 // string_length to get the length of the result string. array_length is not
3549 // smi but the other values are, so the result is a smi.
3550 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3551 __ Subu(string_length, string_length, Operand(scratch1));
3552 __ Mult(array_length, scratch1);
3553 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3554 // zero.
3555 __ mfhi(scratch2);
3556 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3557 __ mflo(scratch2);
3558 __ And(scratch3, scratch2, Operand(0x80000000));
3559 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3560 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3561 __ BranchOnOverflow(&bailout, scratch3);
3562 __ SmiUntag(string_length);
3563
3564 // Get first element in the array to free up the elements register to be used
3565 // for the result.
3566 __ Addu(element,
3567 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3568 result = elements; // End of live range for elements.
3569 elements = no_reg;
3570 // Live values in registers:
3571 // element: First array element
3572 // separator: Separator string
3573 // string_length: Length of result string (not smi)
3574 // array_length: Length of the array.
3575 __ AllocateAsciiString(result,
3576 string_length,
3577 scratch1,
3578 scratch2,
3579 elements_end,
3580 &bailout);
3581 // Prepare for looping. Set up elements_end to end of the array. Set
3582 // result_pos to the position of the result where to write the first
3583 // character.
3584 __ sll(elements_end, array_length, kPointerSizeLog2);
3585 __ Addu(elements_end, element, elements_end);
3586 result_pos = array_length; // End of live range for array_length.
3587 array_length = no_reg;
3588 __ Addu(result_pos,
3589 result,
3590 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3591
3592 // Check the length of the separator.
3593 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3594 __ li(at, Operand(Smi::FromInt(1)));
3595 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3596 __ Branch(&long_separator, gt, scratch1, Operand(at));
3597
3598 // Empty separator case.
3599 __ bind(&empty_separator_loop);
3600 // Live values in registers:
3601 // result_pos: the position to which we are currently copying characters.
3602 // element: Current array element.
3603 // elements_end: Array end.
3604
3605 // Copy next array element to the result.
3606 __ lw(string, MemOperand(element));
3607 __ Addu(element, element, kPointerSize);
3608 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3609 __ SmiUntag(string_length);
3610 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3611 __ CopyBytes(string, result_pos, string_length, scratch1);
3612 // End while (element < elements_end).
3613 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3614 ASSERT(result.is(v0));
3615 __ Branch(&done);
3616
3617 // One-character separator case.
3618 __ bind(&one_char_separator);
3619 // Replace separator with its ascii character value.
3620 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3621 // Jump into the loop after the code that copies the separator, so the first
3622 // element is not preceded by a separator.
3623 __ jmp(&one_char_separator_loop_entry);
3624
3625 __ bind(&one_char_separator_loop);
3626 // Live values in registers:
3627 // result_pos: the position to which we are currently copying characters.
3628 // element: Current array element.
3629 // elements_end: Array end.
3630 // separator: Single separator ascii char (in lower byte).
3631
3632 // Copy the separator character to the result.
3633 __ sb(separator, MemOperand(result_pos));
3634 __ Addu(result_pos, result_pos, 1);
3635
3636 // Copy next array element to the result.
3637 __ bind(&one_char_separator_loop_entry);
3638 __ lw(string, MemOperand(element));
3639 __ Addu(element, element, kPointerSize);
3640 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3641 __ SmiUntag(string_length);
3642 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3643 __ CopyBytes(string, result_pos, string_length, scratch1);
3644 // End while (element < elements_end).
3645 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3646 ASSERT(result.is(v0));
3647 __ Branch(&done);
3648
3649 // Long separator case (separator is more than one character). Entry is at the
3650 // label long_separator below.
3651 __ bind(&long_separator_loop);
3652 // Live values in registers:
3653 // result_pos: the position to which we are currently copying characters.
3654 // element: Current array element.
3655 // elements_end: Array end.
3656 // separator: Separator string.
3657
3658 // Copy the separator to the result.
3659 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3660 __ SmiUntag(string_length);
3661 __ Addu(string,
3662 separator,
3663 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3664 __ CopyBytes(string, result_pos, string_length, scratch1);
3665
3666 __ bind(&long_separator);
3667 __ lw(string, MemOperand(element));
3668 __ Addu(element, element, kPointerSize);
3669 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3670 __ SmiUntag(string_length);
3671 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3672 __ CopyBytes(string, result_pos, string_length, scratch1);
3673 // End while (element < elements_end).
3674 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3675 ASSERT(result.is(v0));
3676 __ Branch(&done);
3677
3678 __ bind(&bailout);
3679 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3680 __ bind(&done);
3681 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003682}
3683
3684
ager@chromium.org5c838252010-02-19 08:53:10 +00003685void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003686 Handle<String> name = expr->name();
3687 if (name->length() > 0 && name->Get(0) == '_') {
3688 Comment cmnt(masm_, "[ InlineRuntimeCall");
3689 EmitInlineRuntimeCall(expr);
3690 return;
3691 }
3692
3693 Comment cmnt(masm_, "[ CallRuntime");
3694 ZoneList<Expression*>* args = expr->arguments();
3695
3696 if (expr->is_jsruntime()) {
3697 // Prepare for calling JS runtime function.
3698 __ lw(a0, GlobalObjectOperand());
3699 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3700 __ push(a0);
3701 }
3702
3703 // Push the arguments ("left-to-right").
3704 int arg_count = args->length();
3705 for (int i = 0; i < arg_count; i++) {
3706 VisitForStackValue(args->at(i));
3707 }
3708
3709 if (expr->is_jsruntime()) {
3710 // Call the JS runtime function.
3711 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003712 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003713 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003714 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003715 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003716 // Restore context register.
3717 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3718 } else {
3719 // Call the C runtime function.
3720 __ CallRuntime(expr->function(), arg_count);
3721 }
3722 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003723}
3724
3725
3726void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003727 switch (expr->op()) {
3728 case Token::DELETE: {
3729 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003730 Property* property = expr->expression()->AsProperty();
3731 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003732
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003733 if (property != NULL) {
3734 VisitForStackValue(property->obj());
3735 VisitForStackValue(property->key());
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003736 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3737 __ push(a1);
3738 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3739 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003740 } else if (proxy != NULL) {
3741 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003742 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003743 // but "delete this" is allowed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003744 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003745 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003746 __ lw(a2, GlobalObjectOperand());
3747 __ li(a1, Operand(var->name()));
3748 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3749 __ Push(a2, a1, a0);
3750 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3751 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003752 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003753 // Result of deleting non-global, non-dynamic variables is false.
3754 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003755 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003756 } else {
3757 // Non-global variable. Call the runtime to try to delete from the
3758 // context where the variable was introduced.
3759 __ push(context_register());
3760 __ li(a2, Operand(var->name()));
3761 __ push(a2);
3762 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3763 context()->Plug(v0);
3764 }
3765 } else {
3766 // Result of deleting non-property, non-variable reference is true.
3767 // The subexpression may have side effects.
3768 VisitForEffect(expr->expression());
3769 context()->Plug(true);
3770 }
3771 break;
3772 }
3773
3774 case Token::VOID: {
3775 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3776 VisitForEffect(expr->expression());
3777 context()->Plug(Heap::kUndefinedValueRootIndex);
3778 break;
3779 }
3780
3781 case Token::NOT: {
3782 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3783 if (context()->IsEffect()) {
3784 // Unary NOT has no side effects so it's only necessary to visit the
3785 // subexpression. Match the optimizing compiler by not branching.
3786 VisitForEffect(expr->expression());
3787 } else {
3788 Label materialize_true, materialize_false;
3789 Label* if_true = NULL;
3790 Label* if_false = NULL;
3791 Label* fall_through = NULL;
3792
3793 // Notice that the labels are swapped.
3794 context()->PrepareTest(&materialize_true, &materialize_false,
3795 &if_false, &if_true, &fall_through);
3796 if (context()->IsTest()) ForwardBailoutToChild(expr);
3797 VisitForControl(expr->expression(), if_true, if_false, fall_through);
3798 context()->Plug(if_false, if_true); // Labels swapped.
3799 }
3800 break;
3801 }
3802
3803 case Token::TYPEOF: {
3804 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3805 { StackValueContext context(this);
3806 VisitForTypeofValue(expr->expression());
3807 }
3808 __ CallRuntime(Runtime::kTypeof, 1);
3809 context()->Plug(v0);
3810 break;
3811 }
3812
3813 case Token::ADD: {
3814 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3815 VisitForAccumulatorValue(expr->expression());
3816 Label no_conversion;
3817 __ JumpIfSmi(result_register(), &no_conversion);
3818 __ mov(a0, result_register());
3819 ToNumberStub convert_stub;
3820 __ CallStub(&convert_stub);
3821 __ bind(&no_conversion);
3822 context()->Plug(result_register());
3823 break;
3824 }
3825
3826 case Token::SUB:
3827 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3828 break;
3829
3830 case Token::BIT_NOT:
3831 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3832 break;
3833
3834 default:
3835 UNREACHABLE();
3836 }
3837}
3838
3839
3840void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3841 const char* comment) {
3842 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3843 Comment cmt(masm_, comment);
3844 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3845 UnaryOverwriteMode overwrite =
3846 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003847 UnaryOpStub stub(expr->op(), overwrite);
3848 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003849 VisitForAccumulatorValue(expr->expression());
3850 SetSourcePosition(expr->position());
3851 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003852 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003853 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003854}
3855
3856
3857void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003858 Comment cmnt(masm_, "[ CountOperation");
3859 SetSourcePosition(expr->position());
3860
3861 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3862 // as the left-hand side.
3863 if (!expr->expression()->IsValidLeftHandSide()) {
3864 VisitForEffect(expr->expression());
3865 return;
3866 }
3867
3868 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003869 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003870 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3871 LhsKind assign_type = VARIABLE;
3872 Property* prop = expr->expression()->AsProperty();
3873 // In case of a property we use the uninitialized expression context
3874 // of the key to detect a named property.
3875 if (prop != NULL) {
3876 assign_type =
3877 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3878 }
3879
3880 // Evaluate expression and get value.
3881 if (assign_type == VARIABLE) {
3882 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3883 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003884 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003885 } else {
3886 // Reserve space for result of postfix operation.
3887 if (expr->is_postfix() && !context()->IsEffect()) {
3888 __ li(at, Operand(Smi::FromInt(0)));
3889 __ push(at);
3890 }
3891 if (assign_type == NAMED_PROPERTY) {
3892 // Put the object both on the stack and in the accumulator.
3893 VisitForAccumulatorValue(prop->obj());
3894 __ push(v0);
3895 EmitNamedPropertyLoad(prop);
3896 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003897 VisitForStackValue(prop->obj());
3898 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003899 __ lw(a1, MemOperand(sp, 0));
3900 __ push(v0);
3901 EmitKeyedPropertyLoad(prop);
3902 }
3903 }
3904
3905 // We need a second deoptimization point after loading the value
3906 // in case evaluating the property load my have a side effect.
3907 if (assign_type == VARIABLE) {
3908 PrepareForBailout(expr->expression(), TOS_REG);
3909 } else {
3910 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3911 }
3912
3913 // Call ToNumber only if operand is not a smi.
3914 Label no_conversion;
3915 __ JumpIfSmi(v0, &no_conversion);
3916 __ mov(a0, v0);
3917 ToNumberStub convert_stub;
3918 __ CallStub(&convert_stub);
3919 __ bind(&no_conversion);
3920
3921 // Save result for postfix expressions.
3922 if (expr->is_postfix()) {
3923 if (!context()->IsEffect()) {
3924 // Save the result on the stack. If we have a named or keyed property
3925 // we store the result under the receiver that is currently on top
3926 // of the stack.
3927 switch (assign_type) {
3928 case VARIABLE:
3929 __ push(v0);
3930 break;
3931 case NAMED_PROPERTY:
3932 __ sw(v0, MemOperand(sp, kPointerSize));
3933 break;
3934 case KEYED_PROPERTY:
3935 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3936 break;
3937 }
3938 }
3939 }
3940 __ mov(a0, result_register());
3941
3942 // Inline smi case if we are in a loop.
3943 Label stub_call, done;
3944 JumpPatchSite patch_site(masm_);
3945
3946 int count_value = expr->op() == Token::INC ? 1 : -1;
3947 __ li(a1, Operand(Smi::FromInt(count_value)));
3948
3949 if (ShouldInlineSmiCase(expr->op())) {
3950 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
3951 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
3952
3953 // We could eliminate this smi check if we split the code at
3954 // the first smi check before calling ToNumber.
3955 patch_site.EmitJumpIfSmi(v0, &done);
3956 __ bind(&stub_call);
3957 }
3958
3959 // Record position before stub call.
3960 SetSourcePosition(expr->position());
3961
danno@chromium.org40cb8782011-05-25 07:58:50 +00003962 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003963 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003964 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003965 __ bind(&done);
3966
3967 // Store the value returned in v0.
3968 switch (assign_type) {
3969 case VARIABLE:
3970 if (expr->is_postfix()) {
3971 { EffectContext context(this);
3972 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3973 Token::ASSIGN);
3974 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3975 context.Plug(v0);
3976 }
3977 // For all contexts except EffectConstant we have the result on
3978 // top of the stack.
3979 if (!context()->IsEffect()) {
3980 context()->PlugTOS();
3981 }
3982 } else {
3983 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3984 Token::ASSIGN);
3985 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3986 context()->Plug(v0);
3987 }
3988 break;
3989 case NAMED_PROPERTY: {
3990 __ mov(a0, result_register()); // Value.
3991 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
3992 __ pop(a1); // Receiver.
3993 Handle<Code> ic = is_strict_mode()
3994 ? isolate()->builtins()->StoreIC_Initialize_Strict()
3995 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003996 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003997 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3998 if (expr->is_postfix()) {
3999 if (!context()->IsEffect()) {
4000 context()->PlugTOS();
4001 }
4002 } else {
4003 context()->Plug(v0);
4004 }
4005 break;
4006 }
4007 case KEYED_PROPERTY: {
4008 __ mov(a0, result_register()); // Value.
4009 __ pop(a1); // Key.
4010 __ pop(a2); // Receiver.
4011 Handle<Code> ic = is_strict_mode()
4012 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
4013 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004014 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004015 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4016 if (expr->is_postfix()) {
4017 if (!context()->IsEffect()) {
4018 context()->PlugTOS();
4019 }
4020 } else {
4021 context()->Plug(v0);
4022 }
4023 break;
4024 }
4025 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004026}
4027
4028
lrn@chromium.org7516f052011-03-30 08:52:27 +00004029void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004030 ASSERT(!context()->IsEffect());
4031 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004032 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004033 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004034 Comment cmnt(masm_, "Global variable");
4035 __ lw(a0, GlobalObjectOperand());
4036 __ li(a2, Operand(proxy->name()));
4037 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4038 // Use a regular load, not a contextual load, to avoid a reference
4039 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004040 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004041 PrepareForBailout(expr, TOS_REG);
4042 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004043 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004044 Label done, slow;
4045
4046 // Generate code for loading from variables potentially shadowed
4047 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004048 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004049
4050 __ bind(&slow);
4051 __ li(a0, Operand(proxy->name()));
4052 __ Push(cp, a0);
4053 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4054 PrepareForBailout(expr, TOS_REG);
4055 __ bind(&done);
4056
4057 context()->Plug(v0);
4058 } else {
4059 // This expression cannot throw a reference error at the top level.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004060 VisitInCurrentContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004061 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004062}
4063
ager@chromium.org04921a82011-06-27 13:21:41 +00004064void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004065 Handle<String> check) {
4066 Label materialize_true, materialize_false;
4067 Label* if_true = NULL;
4068 Label* if_false = NULL;
4069 Label* fall_through = NULL;
4070 context()->PrepareTest(&materialize_true, &materialize_false,
4071 &if_true, &if_false, &fall_through);
4072
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004073 { AccumulatorValueContext context(this);
ager@chromium.org04921a82011-06-27 13:21:41 +00004074 VisitForTypeofValue(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004075 }
4076 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4077
4078 if (check->Equals(isolate()->heap()->number_symbol())) {
4079 __ JumpIfSmi(v0, if_true);
4080 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4081 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4082 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4083 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4084 __ JumpIfSmi(v0, if_false);
4085 // Check for undetectable objects => false.
4086 __ GetObjectType(v0, v0, a1);
4087 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4088 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4089 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4090 Split(eq, a1, Operand(zero_reg),
4091 if_true, if_false, fall_through);
4092 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4093 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4094 __ Branch(if_true, eq, v0, Operand(at));
4095 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4096 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004097 } else if (FLAG_harmony_typeof &&
4098 check->Equals(isolate()->heap()->null_symbol())) {
4099 __ LoadRoot(at, Heap::kNullValueRootIndex);
4100 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004101 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4102 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4103 __ Branch(if_true, eq, v0, Operand(at));
4104 __ JumpIfSmi(v0, if_false);
4105 // Check for undetectable objects => true.
4106 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4107 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4108 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4109 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4110 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4111 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004112 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4113 __ GetObjectType(v0, v0, a1);
4114 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4115 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4116 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004117 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4118 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004119 if (!FLAG_harmony_typeof) {
4120 __ LoadRoot(at, Heap::kNullValueRootIndex);
4121 __ Branch(if_true, eq, v0, Operand(at));
4122 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004123 // Check for JS objects => true.
4124 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004125 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004126 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004127 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004128 // Check for undetectable objects => false.
4129 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4130 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4131 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4132 } else {
4133 if (if_false != fall_through) __ jmp(if_false);
4134 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004135 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004136}
4137
4138
ager@chromium.org5c838252010-02-19 08:53:10 +00004139void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004140 Comment cmnt(masm_, "[ CompareOperation");
4141 SetSourcePosition(expr->position());
4142
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004143 // First we try a fast inlined version of the compare when one of
4144 // the operands is a literal.
4145 if (TryLiteralCompare(expr)) return;
4146
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004147 // Always perform the comparison for its control flow. Pack the result
4148 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004149 Label materialize_true, materialize_false;
4150 Label* if_true = NULL;
4151 Label* if_false = NULL;
4152 Label* fall_through = NULL;
4153 context()->PrepareTest(&materialize_true, &materialize_false,
4154 &if_true, &if_false, &fall_through);
4155
ager@chromium.org04921a82011-06-27 13:21:41 +00004156 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004157 VisitForStackValue(expr->left());
4158 switch (op) {
4159 case Token::IN:
4160 VisitForStackValue(expr->right());
4161 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
4162 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
4163 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4164 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4165 break;
4166
4167 case Token::INSTANCEOF: {
4168 VisitForStackValue(expr->right());
4169 InstanceofStub stub(InstanceofStub::kNoFlags);
4170 __ CallStub(&stub);
4171 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4172 // The stub returns 0 for true.
4173 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4174 break;
4175 }
4176
4177 default: {
4178 VisitForAccumulatorValue(expr->right());
4179 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004180 switch (op) {
4181 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004182 case Token::EQ:
4183 cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004184 break;
4185 case Token::LT:
4186 cc = lt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004187 break;
4188 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004189 cc = gt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004190 break;
4191 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004192 cc = le;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004193 break;
4194 case Token::GTE:
4195 cc = ge;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004196 break;
4197 case Token::IN:
4198 case Token::INSTANCEOF:
4199 default:
4200 UNREACHABLE();
4201 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004202 __ mov(a0, result_register());
4203 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004204
4205 bool inline_smi_code = ShouldInlineSmiCase(op);
4206 JumpPatchSite patch_site(masm_);
4207 if (inline_smi_code) {
4208 Label slow_case;
4209 __ Or(a2, a0, Operand(a1));
4210 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4211 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4212 __ bind(&slow_case);
4213 }
4214 // Record position and call the compare IC.
4215 SetSourcePosition(expr->position());
4216 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004217 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004218 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004219 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4220 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4221 }
4222 }
4223
4224 // Convert the result of the comparison into one expected for this
4225 // expression's context.
4226 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004227}
4228
4229
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004230void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4231 Expression* sub_expr,
4232 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004233 Label materialize_true, materialize_false;
4234 Label* if_true = NULL;
4235 Label* if_false = NULL;
4236 Label* fall_through = NULL;
4237 context()->PrepareTest(&materialize_true, &materialize_false,
4238 &if_true, &if_false, &fall_through);
4239
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004240 VisitForAccumulatorValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004241 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004242 Heap::RootListIndex nil_value = nil == kNullValue ?
4243 Heap::kNullValueRootIndex :
4244 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004245 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004246 __ LoadRoot(a1, nil_value);
4247 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004248 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4249 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004250 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4251 Heap::kUndefinedValueRootIndex :
4252 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004253 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004254 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004255 __ Branch(if_true, eq, a0, Operand(a1));
4256 __ And(at, a0, Operand(kSmiTagMask));
4257 __ Branch(if_false, eq, at, Operand(zero_reg));
4258 // It can be an undetectable object.
4259 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4260 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4261 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4262 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4263 }
4264 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004265}
4266
4267
ager@chromium.org5c838252010-02-19 08:53:10 +00004268void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004269 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4270 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004271}
4272
4273
lrn@chromium.org7516f052011-03-30 08:52:27 +00004274Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004275 return v0;
4276}
ager@chromium.org5c838252010-02-19 08:53:10 +00004277
4278
lrn@chromium.org7516f052011-03-30 08:52:27 +00004279Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004280 return cp;
4281}
4282
4283
ager@chromium.org5c838252010-02-19 08:53:10 +00004284void 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
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004295void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4296 Scope* declaration_scope = scope()->DeclarationScope();
4297 if (declaration_scope->is_global_scope()) {
4298 // Contexts nested in the global context have a canonical empty function
4299 // as their closure, not the anonymous closure containing the global
4300 // code. Pass a smi sentinel and let the runtime look up the empty
4301 // function.
4302 __ li(at, Operand(Smi::FromInt(0)));
4303 } else if (declaration_scope->is_eval_scope()) {
4304 // Contexts created by a call to eval have the same closure as the
4305 // context calling eval, not the anonymous closure containing the eval
4306 // code. Fetch it from the context.
4307 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4308 } else {
4309 ASSERT(declaration_scope->is_function_scope());
4310 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4311 }
4312 __ push(at);
4313}
4314
4315
ager@chromium.org5c838252010-02-19 08:53:10 +00004316// ----------------------------------------------------------------------------
4317// Non-local control flow support.
4318
4319void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004320 ASSERT(!result_register().is(a1));
4321 // Store result register while executing finally block.
4322 __ push(result_register());
4323 // Cook return address in link register to stack (smi encoded Code* delta).
4324 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4325 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004326 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004327 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4328 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004329}
4330
4331
4332void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004333 ASSERT(!result_register().is(a1));
4334 // Restore result register from stack.
4335 __ pop(a1);
4336 // Uncook return address and return.
4337 __ pop(result_register());
4338 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4339 __ sra(a1, a1, 1); // Un-smi-tag value.
4340 __ Addu(at, a1, Operand(masm_->CodeObject()));
4341 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004342}
4343
4344
4345#undef __
4346
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004347#define __ ACCESS_MASM(masm())
4348
4349FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4350 int* stack_depth,
4351 int* context_length) {
4352 // The macros used here must preserve the result register.
4353
4354 // Because the handler block contains the context of the finally
4355 // code, we can restore it directly from there for the finally code
4356 // rather than iteratively unwinding contexts via their previous
4357 // links.
4358 __ Drop(*stack_depth); // Down to the handler block.
4359 if (*context_length > 0) {
4360 // Restore the context to its dedicated register and the stack.
4361 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4362 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4363 }
4364 __ PopTryHandler();
4365 __ Call(finally_entry_);
4366
4367 *stack_depth = 0;
4368 *context_length = 0;
4369 return previous_;
4370}
4371
4372
4373#undef __
4374
ager@chromium.org5c838252010-02-19 08:53:10 +00004375} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004376
4377#endif // V8_TARGET_ARCH_MIPS