blob: 201e6b8e1c54b1e79bce578b1613463578ea93cc [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
58// A patch site is a location in the code which it is possible to patch. This
59// class has a number of methods to emit the code which is patchable and the
60// method EmitPatchInfo to record a marker back to the patchable code. This
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000061// marker is a andi zero_reg, rx, #yyyy instruction, and rx * 0x0000ffff + yyyy
62// (raw 16 bit immediate value is used) is the delta from the pc to the first
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000063// instruction of the patchable code.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000064// The marker instruction is effectively a NOP (dest is zero_reg) and will
65// never be emitted by normal code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000066class JumpPatchSite BASE_EMBEDDED {
67 public:
68 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
69#ifdef DEBUG
70 info_emitted_ = false;
71#endif
72 }
73
74 ~JumpPatchSite() {
75 ASSERT(patch_site_.is_bound() == info_emitted_);
76 }
77
78 // When initially emitting this ensure that a jump is always generated to skip
79 // the inlined smi code.
80 void EmitJumpIfNotSmi(Register reg, Label* target) {
81 ASSERT(!patch_site_.is_bound() && !info_emitted_);
82 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
83 __ bind(&patch_site_);
84 __ andi(at, reg, 0);
85 // Always taken before patched.
86 __ Branch(target, eq, at, Operand(zero_reg));
87 }
88
89 // When initially emitting this ensure that a jump is never generated to skip
90 // the inlined smi code.
91 void EmitJumpIfSmi(Register reg, Label* target) {
92 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
93 ASSERT(!patch_site_.is_bound() && !info_emitted_);
94 __ bind(&patch_site_);
95 __ andi(at, reg, 0);
96 // Never taken before patched.
97 __ Branch(target, ne, at, Operand(zero_reg));
98 }
99
100 void EmitPatchInfo() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000101 if (patch_site_.is_bound()) {
102 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
103 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000104 __ andi(zero_reg, reg, delta_to_patch_site % kImm16Mask);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000105#ifdef DEBUG
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000106 info_emitted_ = true;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000107#endif
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000108 } else {
109 __ nop(); // Signals no inlined code.
110 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000111 }
112
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000113 private:
114 MacroAssembler* masm_;
115 Label patch_site_;
116#ifdef DEBUG
117 bool info_emitted_;
118#endif
119};
120
121
lrn@chromium.org7516f052011-03-30 08:52:27 +0000122// Generate code for a JS function. On entry to the function the receiver
123// and arguments have been pushed on the stack left to right. The actual
124// argument count matches the formal parameter count expected by the
125// function.
126//
127// The live registers are:
128// o a1: the JS function object being called (ie, ourselves)
129// o cp: our context
130// o fp: our caller's frame pointer
131// o sp: stack pointer
132// o ra: return address
133//
134// The function builds a JS frame. Please see JavaScriptFrameConstants in
135// frames-mips.h for its layout.
136void FullCodeGenerator::Generate(CompilationInfo* info) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000137 ASSERT(info_ == NULL);
138 info_ = info;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000139 scope_ = info->scope();
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +0000140 handler_table_ =
141 isolate()->factory()->NewFixedArray(function()->handler_count(), TENURED);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000142 SetFunctionPosition(function());
143 Comment cmnt(masm_, "[ function compiled by full code generator");
144
145#ifdef DEBUG
146 if (strlen(FLAG_stop_at) > 0 &&
147 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
148 __ stop("stop-at");
149 }
150#endif
151
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000152 // Strict mode functions and builtins need to replace the receiver
153 // with undefined when called as functions (without an explicit
154 // receiver object). t1 is zero for method calls and non-zero for
155 // function calls.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000156 if (!info->is_classic_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000157 Label ok;
158 __ Branch(&ok, eq, t1, Operand(zero_reg));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000159 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000160 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
161 __ sw(a2, MemOperand(sp, receiver_offset));
162 __ bind(&ok);
163 }
164
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000165 // Open a frame scope to indicate that there is a frame on the stack. The
166 // MANUAL indicates that the scope shouldn't actually generate code to set up
167 // the frame (that is done below).
168 FrameScope frame_scope(masm_, StackFrame::MANUAL);
169
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000170 int locals_count = info->scope()->num_stack_slots();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000171
172 __ Push(ra, fp, cp, a1);
173 if (locals_count > 0) {
174 // Load undefined value here, so the value is ready for the loop
175 // below.
176 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
177 }
178 // Adjust fp to point to caller's fp.
179 __ Addu(fp, sp, Operand(2 * kPointerSize));
180
181 { Comment cmnt(masm_, "[ Allocate locals");
182 for (int i = 0; i < locals_count; i++) {
183 __ push(at);
184 }
185 }
186
187 bool function_in_register = true;
188
189 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000190 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000191 if (heap_slots > 0) {
192 Comment cmnt(masm_, "[ Allocate local context");
193 // Argument to NewContext is the function, which is in a1.
194 __ push(a1);
195 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
196 FastNewContextStub stub(heap_slots);
197 __ CallStub(&stub);
198 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000199 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000200 }
201 function_in_register = false;
202 // Context is returned in both v0 and cp. It replaces the context
203 // passed to us. It's saved in the stack and kept live in cp.
204 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
205 // Copy any necessary parameters into the context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000206 int num_parameters = info->scope()->num_parameters();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000207 for (int i = 0; i < num_parameters; i++) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000208 Variable* var = scope()->parameter(i);
209 if (var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000210 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
211 (num_parameters - 1 - i) * kPointerSize;
212 // Load parameter from stack.
213 __ lw(a0, MemOperand(fp, parameter_offset));
214 // Store it in the context.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000215 MemOperand target = ContextOperand(cp, var->index());
216 __ sw(a0, target);
217
218 // Update the write barrier.
219 __ RecordWriteContextSlot(
220 cp, target.offset(), a0, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000221 }
222 }
223 }
224
225 Variable* arguments = scope()->arguments();
226 if (arguments != NULL) {
227 // Function uses arguments object.
228 Comment cmnt(masm_, "[ Allocate arguments object");
229 if (!function_in_register) {
230 // Load this again, if it's used by the local context below.
231 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
232 } else {
233 __ mov(a3, a1);
234 }
235 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000236 int num_parameters = info->scope()->num_parameters();
237 int offset = num_parameters * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000238 __ Addu(a2, fp,
239 Operand(StandardFrameConstants::kCallerSPOffset + offset));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000240 __ li(a1, Operand(Smi::FromInt(num_parameters)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000241 __ Push(a3, a2, a1);
242
243 // Arguments to ArgumentsAccessStub:
244 // function, receiver address, parameter count.
245 // The stub will rewrite receiever and parameter count if the previous
246 // stack frame was an arguments adapter frame.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000247 ArgumentsAccessStub::Type type;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000248 if (!is_classic_mode()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000249 type = ArgumentsAccessStub::NEW_STRICT;
250 } else if (function()->has_duplicate_parameters()) {
251 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
252 } else {
253 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
254 }
255 ArgumentsAccessStub stub(type);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000256 __ CallStub(&stub);
257
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000258 SetVar(arguments, v0, a1, a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000259 }
260
261 if (FLAG_trace) {
262 __ CallRuntime(Runtime::kTraceEnter, 0);
263 }
264
265 // Visit the declarations and body unless there is an illegal
266 // redeclaration.
267 if (scope()->HasIllegalRedeclaration()) {
268 Comment cmnt(masm_, "[ Declarations");
269 scope()->VisitIllegalRedeclaration(this);
270
271 } else {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000272 PrepareForBailoutForId(AstNode::kFunctionEntryId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000273 { Comment cmnt(masm_, "[ Declarations");
274 // For named function expressions, declare the function name as a
275 // constant.
276 if (scope()->is_function_scope() && scope()->function() != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000277 int ignored = 0;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000278 VariableProxy* proxy = scope()->function();
279 ASSERT(proxy->var()->mode() == CONST ||
280 proxy->var()->mode() == CONST_HARMONY);
281 EmitDeclaration(proxy, proxy->var()->mode(), NULL, &ignored);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000282 }
283 VisitDeclarations(scope()->declarations());
284 }
285
286 { Comment cmnt(masm_, "[ Stack check");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000287 PrepareForBailoutForId(AstNode::kDeclarationsId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000288 Label ok;
289 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
290 __ Branch(&ok, hs, sp, Operand(t0));
291 StackCheckStub stub;
292 __ CallStub(&stub);
293 __ bind(&ok);
294 }
295
296 { Comment cmnt(masm_, "[ Body");
297 ASSERT(loop_depth() == 0);
298 VisitStatements(function()->body());
299 ASSERT(loop_depth() == 0);
300 }
301 }
302
303 // Always emit a 'return undefined' in case control fell off the end of
304 // the body.
305 { Comment cmnt(masm_, "[ return <undefined>;");
306 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
307 }
308 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000309}
310
311
312void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000313 ASSERT(Smi::FromInt(0) == 0);
314 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000315}
316
317
318void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000319 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
320 // to make sure it is constant. Branch may emit a skip-or-jump sequence
321 // instead of the normal Branch. It seems that the "skip" part of that
322 // sequence is about as long as this Branch would be so it is safe to ignore
323 // that.
324 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000325 Comment cmnt(masm_, "[ Stack check");
326 Label ok;
327 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000328 __ sltu(at, sp, t0);
329 __ beq(at, zero_reg, &ok);
330 // CallStub will emit a li t9, ... first, so it is safe to use the delay slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000331 StackCheckStub stub;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000332 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000333 // Record a mapping of this PC offset to the OSR id. This is used to find
334 // the AST id from the unoptimized code in order to use it as a key into
335 // the deoptimization input data found in the optimized code.
336 RecordStackCheck(stmt->OsrEntryId());
337
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000338 __ bind(&ok);
339 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
340 // Record a mapping of the OSR id to this PC. This is used if the OSR
341 // entry becomes the target of a bailout. We don't expect it to be, but
342 // we want it to work if it is.
343 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000344}
345
346
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000347void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000348 Comment cmnt(masm_, "[ Return sequence");
349 if (return_label_.is_bound()) {
350 __ Branch(&return_label_);
351 } else {
352 __ bind(&return_label_);
353 if (FLAG_trace) {
354 // Push the return value on the stack as the parameter.
355 // Runtime::TraceExit returns its parameter in v0.
356 __ push(v0);
357 __ CallRuntime(Runtime::kTraceExit, 1);
358 }
359
360#ifdef DEBUG
361 // Add a label for checking the size of the code used for returning.
362 Label check_exit_codesize;
363 masm_->bind(&check_exit_codesize);
364#endif
365 // Make sure that the constant pool is not emitted inside of the return
366 // sequence.
367 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
368 // Here we use masm_-> instead of the __ macro to avoid the code coverage
369 // tool from instrumenting as we rely on the code size here.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000370 int32_t sp_delta = (info_->scope()->num_parameters() + 1) * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000371 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
372 __ RecordJSReturn();
373 masm_->mov(sp, fp);
374 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
375 masm_->Addu(sp, sp, Operand(sp_delta));
376 masm_->Jump(ra);
377 }
378
379#ifdef DEBUG
380 // Check that the size of the code used for returning is large enough
381 // for the debugger's requirements.
382 ASSERT(Assembler::kJSReturnSequenceInstructions <=
383 masm_->InstructionsGeneratedSince(&check_exit_codesize));
384#endif
385 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000386}
387
388
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000389void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
390 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
ager@chromium.org5c838252010-02-19 08:53:10 +0000391}
392
393
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000394void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
395 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
396 codegen()->GetVar(result_register(), var);
ager@chromium.org5c838252010-02-19 08:53:10 +0000397}
398
399
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000400void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
401 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
402 codegen()->GetVar(result_register(), var);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000403 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000404}
405
406
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000407void FullCodeGenerator::TestContext::Plug(Variable* var) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000408 // For simplicity we always test the accumulator register.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000409 codegen()->GetVar(result_register(), var);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000410 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000411 codegen()->DoTest(this);
ager@chromium.org5c838252010-02-19 08:53:10 +0000412}
413
414
lrn@chromium.org7516f052011-03-30 08:52:27 +0000415void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000416}
417
418
lrn@chromium.org7516f052011-03-30 08:52:27 +0000419void FullCodeGenerator::AccumulatorValueContext::Plug(
420 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000421 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000422}
423
424
425void FullCodeGenerator::StackValueContext::Plug(
426 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000427 __ LoadRoot(result_register(), index);
428 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000429}
430
431
432void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000433 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000434 true,
435 true_label_,
436 false_label_);
437 if (index == Heap::kUndefinedValueRootIndex ||
438 index == Heap::kNullValueRootIndex ||
439 index == Heap::kFalseValueRootIndex) {
440 if (false_label_ != fall_through_) __ Branch(false_label_);
441 } else if (index == Heap::kTrueValueRootIndex) {
442 if (true_label_ != fall_through_) __ Branch(true_label_);
443 } else {
444 __ LoadRoot(result_register(), index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000445 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000446 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000447}
448
449
450void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000451}
452
453
454void FullCodeGenerator::AccumulatorValueContext::Plug(
455 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000456 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000457}
458
459
460void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000461 // Immediates cannot be pushed directly.
462 __ li(result_register(), Operand(lit));
463 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000464}
465
466
467void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000468 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000469 true,
470 true_label_,
471 false_label_);
472 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
473 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
474 if (false_label_ != fall_through_) __ Branch(false_label_);
475 } else if (lit->IsTrue() || lit->IsJSObject()) {
476 if (true_label_ != fall_through_) __ Branch(true_label_);
477 } else if (lit->IsString()) {
478 if (String::cast(*lit)->length() == 0) {
479 if (false_label_ != fall_through_) __ Branch(false_label_);
480 } else {
481 if (true_label_ != fall_through_) __ Branch(true_label_);
482 }
483 } else if (lit->IsSmi()) {
484 if (Smi::cast(*lit)->value() == 0) {
485 if (false_label_ != fall_through_) __ Branch(false_label_);
486 } else {
487 if (true_label_ != fall_through_) __ Branch(true_label_);
488 }
489 } else {
490 // For simplicity we always test the accumulator register.
491 __ li(result_register(), Operand(lit));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000492 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000493 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000494}
495
496
497void FullCodeGenerator::EffectContext::DropAndPlug(int count,
498 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000499 ASSERT(count > 0);
500 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000501}
502
503
504void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
505 int count,
506 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000507 ASSERT(count > 0);
508 __ Drop(count);
509 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000510}
511
512
513void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
514 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000515 ASSERT(count > 0);
516 if (count > 1) __ Drop(count - 1);
517 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000518}
519
520
521void FullCodeGenerator::TestContext::DropAndPlug(int count,
522 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000523 ASSERT(count > 0);
524 // For simplicity we always test the accumulator register.
525 __ Drop(count);
526 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000527 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000528 codegen()->DoTest(this);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000529}
530
531
532void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
533 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000534 ASSERT(materialize_true == materialize_false);
535 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000536}
537
538
539void FullCodeGenerator::AccumulatorValueContext::Plug(
540 Label* materialize_true,
541 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000542 Label done;
543 __ bind(materialize_true);
544 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
545 __ Branch(&done);
546 __ bind(materialize_false);
547 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
548 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000549}
550
551
552void FullCodeGenerator::StackValueContext::Plug(
553 Label* materialize_true,
554 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000555 Label done;
556 __ bind(materialize_true);
557 __ LoadRoot(at, Heap::kTrueValueRootIndex);
558 __ push(at);
559 __ Branch(&done);
560 __ bind(materialize_false);
561 __ LoadRoot(at, Heap::kFalseValueRootIndex);
562 __ push(at);
563 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000564}
565
566
567void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
568 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000569 ASSERT(materialize_true == true_label_);
570 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000571}
572
573
574void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000575}
576
577
578void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000579 Heap::RootListIndex value_root_index =
580 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
581 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000582}
583
584
585void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000586 Heap::RootListIndex value_root_index =
587 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
588 __ LoadRoot(at, value_root_index);
589 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000590}
591
592
593void FullCodeGenerator::TestContext::Plug(bool flag) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000594 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000595 true,
596 true_label_,
597 false_label_);
598 if (flag) {
599 if (true_label_ != fall_through_) __ Branch(true_label_);
600 } else {
601 if (false_label_ != fall_through_) __ Branch(false_label_);
602 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000603}
604
605
whesse@chromium.org7b260152011-06-20 15:33:18 +0000606void FullCodeGenerator::DoTest(Expression* condition,
607 Label* if_true,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000608 Label* if_false,
609 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000610 if (CpuFeatures::IsSupported(FPU)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000611 ToBooleanStub stub(result_register());
612 __ CallStub(&stub);
613 __ mov(at, zero_reg);
614 } else {
615 // Call the runtime to find the boolean value of the source and then
616 // translate it into control flow to the pair of labels.
617 __ push(result_register());
618 __ CallRuntime(Runtime::kToBool, 1);
619 __ LoadRoot(at, Heap::kFalseValueRootIndex);
620 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000621 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000622}
623
624
lrn@chromium.org7516f052011-03-30 08:52:27 +0000625void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000626 Register lhs,
627 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000628 Label* if_true,
629 Label* if_false,
630 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000631 if (if_false == fall_through) {
632 __ Branch(if_true, cc, lhs, rhs);
633 } else if (if_true == fall_through) {
634 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
635 } else {
636 __ Branch(if_true, cc, lhs, rhs);
637 __ Branch(if_false);
638 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000639}
640
641
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000642MemOperand FullCodeGenerator::StackOperand(Variable* var) {
643 ASSERT(var->IsStackAllocated());
644 // Offset is negative because higher indexes are at lower addresses.
645 int offset = -var->index() * kPointerSize;
646 // Adjust by a (parameter or local) base offset.
647 if (var->IsParameter()) {
648 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
649 } else {
650 offset += JavaScriptFrameConstants::kLocal0Offset;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000651 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000652 return MemOperand(fp, offset);
ager@chromium.org5c838252010-02-19 08:53:10 +0000653}
654
655
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000656MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
657 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
658 if (var->IsContextSlot()) {
659 int context_chain_length = scope()->ContextChainLength(var->scope());
660 __ LoadContext(scratch, context_chain_length);
661 return ContextOperand(scratch, var->index());
662 } else {
663 return StackOperand(var);
664 }
665}
666
667
668void FullCodeGenerator::GetVar(Register dest, Variable* var) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000669 // Use destination as scratch.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000670 MemOperand location = VarOperand(var, dest);
671 __ lw(dest, location);
672}
673
674
675void FullCodeGenerator::SetVar(Variable* var,
676 Register src,
677 Register scratch0,
678 Register scratch1) {
679 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
680 ASSERT(!scratch0.is(src));
681 ASSERT(!scratch0.is(scratch1));
682 ASSERT(!scratch1.is(src));
683 MemOperand location = VarOperand(var, scratch0);
684 __ sw(src, location);
685 // Emit the write barrier code if the location is in the heap.
686 if (var->IsContextSlot()) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000687 __ RecordWriteContextSlot(scratch0,
688 location.offset(),
689 src,
690 scratch1,
691 kRAHasBeenSaved,
692 kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000693 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000694}
695
696
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000697void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000698 bool should_normalize,
699 Label* if_true,
700 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000701 // Only prepare for bailouts before splits if we're in a test
702 // context. Otherwise, we let the Visit function deal with the
703 // preparation to avoid preparing with the same AST id twice.
704 if (!context()->IsTest() || !info_->IsOptimizable()) return;
705
706 Label skip;
707 if (should_normalize) __ Branch(&skip);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000708 PrepareForBailout(expr, TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000709 if (should_normalize) {
710 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
711 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
712 __ bind(&skip);
713 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000714}
715
716
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000717void FullCodeGenerator::EmitDeclaration(VariableProxy* proxy,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000718 VariableMode mode,
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000719 FunctionLiteral* function,
720 int* global_count) {
721 // If it was not possible to allocate the variable at compile time, we
722 // need to "declare" it at runtime to make sure it actually exists in the
723 // local context.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000724 Variable* variable = proxy->var();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000725 bool binding_needs_init = (function == NULL) &&
726 (mode == CONST || mode == CONST_HARMONY || mode == LET);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000727 switch (variable->location()) {
728 case Variable::UNALLOCATED:
729 ++(*global_count);
730 break;
731
732 case Variable::PARAMETER:
733 case Variable::LOCAL:
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000734 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000735 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000736 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000737 __ sw(result_register(), StackOperand(variable));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000738 } else if (binding_needs_init) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000739 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000740 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000741 __ sw(t0, StackOperand(variable));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000742 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000743 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000744
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000745 case Variable::CONTEXT:
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000746 // The variable in the decl always resides in the current function
747 // context.
748 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
749 if (FLAG_debug_code) {
750 // Check that we're not inside a with or catch context.
751 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
752 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
753 __ Check(ne, "Declaration in with context.",
754 a1, Operand(t0));
755 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
756 __ Check(ne, "Declaration in catch context.",
757 a1, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000758 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000759 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000760 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000761 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000762 __ sw(result_register(), ContextOperand(cp, variable->index()));
763 int offset = Context::SlotOffset(variable->index());
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000764 // We know that we have written a function, which is not a smi.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000765 __ RecordWriteContextSlot(cp,
766 offset,
767 result_register(),
768 a2,
769 kRAHasBeenSaved,
770 kDontSaveFPRegs,
771 EMIT_REMEMBERED_SET,
772 OMIT_SMI_CHECK);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000773 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000774 } else if (binding_needs_init) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000775 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000776 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000777 __ sw(at, ContextOperand(cp, variable->index()));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000778 // No write barrier since the_hole_value is in old space.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000779 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000780 }
781 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000782
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000783 case Variable::LOOKUP: {
784 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000785 __ li(a2, Operand(variable->name()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000786 // Declaration nodes are always introduced in one of four modes.
787 ASSERT(mode == VAR ||
788 mode == CONST ||
789 mode == CONST_HARMONY ||
790 mode == LET);
791 PropertyAttributes attr = (mode == CONST || mode == CONST_HARMONY)
792 ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000793 __ li(a1, Operand(Smi::FromInt(attr)));
794 // Push initial value, if any.
795 // Note: For variables we must not push an initial value (such as
796 // 'undefined') because we may have a (legal) redeclaration and we
797 // must not destroy the current value.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000798 if (function != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000799 __ Push(cp, a2, a1);
800 // Push initial value for function declaration.
801 VisitForStackValue(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000802 } else if (binding_needs_init) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000803 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
804 __ Push(cp, a2, a1, a0);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000805 } else {
806 ASSERT(Smi::FromInt(0) == 0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000807 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000808 __ Push(cp, a2, a1, a0);
809 }
810 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
811 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000812 }
813 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000814}
815
816
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000817void FullCodeGenerator::VisitDeclaration(Declaration* decl) { }
ager@chromium.org5c838252010-02-19 08:53:10 +0000818
819
820void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000821 // Call the runtime to declare the globals.
822 // The context is the first argument.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000823 __ li(a1, Operand(pairs));
824 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
825 __ Push(cp, a1, a0);
826 __ CallRuntime(Runtime::kDeclareGlobals, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000827 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000828}
829
830
lrn@chromium.org7516f052011-03-30 08:52:27 +0000831void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000832 Comment cmnt(masm_, "[ SwitchStatement");
833 Breakable nested_statement(this, stmt);
834 SetStatementPosition(stmt);
835
836 // Keep the switch value on the stack until a case matches.
837 VisitForStackValue(stmt->tag());
838 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
839
840 ZoneList<CaseClause*>* clauses = stmt->cases();
841 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
842
843 Label next_test; // Recycled for each test.
844 // Compile all the tests with branches to their bodies.
845 for (int i = 0; i < clauses->length(); i++) {
846 CaseClause* clause = clauses->at(i);
847 clause->body_target()->Unuse();
848
849 // The default is not a test, but remember it as final fall through.
850 if (clause->is_default()) {
851 default_clause = clause;
852 continue;
853 }
854
855 Comment cmnt(masm_, "[ Case comparison");
856 __ bind(&next_test);
857 next_test.Unuse();
858
859 // Compile the label expression.
860 VisitForAccumulatorValue(clause->label());
861 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
862
863 // Perform the comparison as if via '==='.
864 __ lw(a1, MemOperand(sp, 0)); // Switch value.
865 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
866 JumpPatchSite patch_site(masm_);
867 if (inline_smi_code) {
868 Label slow_case;
869 __ or_(a2, a1, a0);
870 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
871
872 __ Branch(&next_test, ne, a1, Operand(a0));
873 __ Drop(1); // Switch value is no longer needed.
874 __ Branch(clause->body_target());
875
876 __ bind(&slow_case);
877 }
878
879 // Record position before stub call for type feedback.
880 SetSourcePosition(clause->position());
881 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000882 __ Call(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000883 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +0000884
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000885 __ Branch(&next_test, ne, v0, Operand(zero_reg));
886 __ Drop(1); // Switch value is no longer needed.
887 __ Branch(clause->body_target());
888 }
889
890 // Discard the test value and jump to the default if present, otherwise to
891 // the end of the statement.
892 __ bind(&next_test);
893 __ Drop(1); // Switch value is no longer needed.
894 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000895 __ Branch(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000896 } else {
897 __ Branch(default_clause->body_target());
898 }
899
900 // Compile all the case bodies.
901 for (int i = 0; i < clauses->length(); i++) {
902 Comment cmnt(masm_, "[ Case body");
903 CaseClause* clause = clauses->at(i);
904 __ bind(clause->body_target());
905 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
906 VisitStatements(clause->statements());
907 }
908
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000909 __ bind(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000910 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000911}
912
913
914void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000915 Comment cmnt(masm_, "[ ForInStatement");
916 SetStatementPosition(stmt);
917
918 Label loop, exit;
919 ForIn loop_statement(this, stmt);
920 increment_loop_depth();
921
922 // Get the object to enumerate over. Both SpiderMonkey and JSC
923 // ignore null and undefined in contrast to the specification; see
924 // ECMA-262 section 12.6.4.
925 VisitForAccumulatorValue(stmt->enumerable());
926 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
927 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
928 __ Branch(&exit, eq, a0, Operand(at));
929 Register null_value = t1;
930 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
931 __ Branch(&exit, eq, a0, Operand(null_value));
932
933 // Convert the object to a JS object.
934 Label convert, done_convert;
935 __ JumpIfSmi(a0, &convert);
936 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000937 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000938 __ bind(&convert);
939 __ push(a0);
940 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
941 __ mov(a0, v0);
942 __ bind(&done_convert);
943 __ push(a0);
944
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000945 // Check for proxies.
946 Label call_runtime;
947 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
948 __ GetObjectType(a0, a1, a1);
949 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
950
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000951 // Check cache validity in generated code. This is a fast case for
952 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
953 // guarantee cache validity, call the runtime system to check cache
954 // validity or get the property names in a fixed array.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000955 Label next;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000956 // Preload a couple of values used in the loop.
957 Register empty_fixed_array_value = t2;
958 __ LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
959 Register empty_descriptor_array_value = t3;
960 __ LoadRoot(empty_descriptor_array_value,
961 Heap::kEmptyDescriptorArrayRootIndex);
962 __ mov(a1, a0);
963 __ bind(&next);
964
965 // Check that there are no elements. Register a1 contains the
966 // current JS object we've reached through the prototype chain.
967 __ lw(a2, FieldMemOperand(a1, JSObject::kElementsOffset));
968 __ Branch(&call_runtime, ne, a2, Operand(empty_fixed_array_value));
969
970 // Check that instance descriptors are not empty so that we can
971 // check for an enum cache. Leave the map in a2 for the subsequent
972 // prototype load.
973 __ lw(a2, FieldMemOperand(a1, HeapObject::kMapOffset));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000974 __ lw(a3, FieldMemOperand(a2, Map::kInstanceDescriptorsOrBitField3Offset));
975 __ JumpIfSmi(a3, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000976
977 // Check that there is an enum cache in the non-empty instance
978 // descriptors (a3). This is the case if the next enumeration
979 // index field does not contain a smi.
980 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumerationIndexOffset));
981 __ JumpIfSmi(a3, &call_runtime);
982
983 // For all objects but the receiver, check that the cache is empty.
984 Label check_prototype;
985 __ Branch(&check_prototype, eq, a1, Operand(a0));
986 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumCacheBridgeCacheOffset));
987 __ Branch(&call_runtime, ne, a3, Operand(empty_fixed_array_value));
988
989 // Load the prototype from the map and loop if non-null.
990 __ bind(&check_prototype);
991 __ lw(a1, FieldMemOperand(a2, Map::kPrototypeOffset));
992 __ Branch(&next, ne, a1, Operand(null_value));
993
994 // The enum cache is valid. Load the map of the object being
995 // iterated over and use the cache for the iteration.
996 Label use_cache;
997 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
998 __ Branch(&use_cache);
999
1000 // Get the set of properties to enumerate.
1001 __ bind(&call_runtime);
1002 __ push(a0); // Duplicate the enumerable object on the stack.
1003 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1004
1005 // If we got a map from the runtime call, we can do a fast
1006 // modification check. Otherwise, we got a fixed array, and we have
1007 // to do a slow check.
1008 Label fixed_array;
1009 __ mov(a2, v0);
1010 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
1011 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1012 __ Branch(&fixed_array, ne, a1, Operand(at));
1013
1014 // We got a map in register v0. Get the enumeration cache from it.
1015 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001016 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001017 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
1018 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1019
1020 // Setup the four remaining stack slots.
1021 __ push(v0); // Map.
1022 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
1023 __ li(a0, Operand(Smi::FromInt(0)));
1024 // Push enumeration cache, enumeration cache length (as smi) and zero.
1025 __ Push(a2, a1, a0);
1026 __ jmp(&loop);
1027
1028 // We got a fixed array in register v0. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001029 Label non_proxy;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001030 __ bind(&fixed_array);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001031 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1032 __ lw(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1033 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1034 __ GetObjectType(a2, a3, a3);
1035 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1036 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1037 __ bind(&non_proxy);
1038 __ Push(a1, v0); // Smi and array
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001039 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1040 __ li(a0, Operand(Smi::FromInt(0)));
1041 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1042
1043 // Generate code for doing the condition check.
1044 __ bind(&loop);
1045 // Load the current count to a0, load the length to a1.
1046 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1047 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001048 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001049
1050 // Get the current entry of the array into register a3.
1051 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1052 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1053 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1054 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1055 __ lw(a3, MemOperand(t0)); // Current entry.
1056
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001057 // Get the expected map from the stack or a smi in the
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001058 // permanent slow case into register a2.
1059 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1060
1061 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001062 // If not, we may have to filter the key.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001063 Label update_each;
1064 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1065 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1066 __ Branch(&update_each, eq, t0, Operand(a2));
1067
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001068 // For proxies, no filtering is done.
1069 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1070 ASSERT_EQ(Smi::FromInt(0), 0);
1071 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1072
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001073 // Convert the entry to a string or (smi) 0 if it isn't a property
1074 // any more. If the property has been removed while iterating, we
1075 // just skip it.
1076 __ push(a1); // Enumerable.
1077 __ push(a3); // Current entry.
1078 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1079 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001080 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001081
1082 // Update the 'each' property or variable from the possibly filtered
1083 // entry in register a3.
1084 __ bind(&update_each);
1085 __ mov(result_register(), a3);
1086 // Perform the assignment as if via '='.
1087 { EffectContext context(this);
1088 EmitAssignment(stmt->each(), stmt->AssignmentId());
1089 }
1090
1091 // Generate code for the body of the loop.
1092 Visit(stmt->body());
1093
1094 // Generate code for the going to the next element by incrementing
1095 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001096 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001097 __ pop(a0);
1098 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1099 __ push(a0);
1100
1101 EmitStackCheck(stmt);
1102 __ Branch(&loop);
1103
1104 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001105 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001106 __ Drop(5);
1107
1108 // Exit and decrement the loop depth.
1109 __ bind(&exit);
1110 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001111}
1112
1113
1114void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1115 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001116 // Use the fast case closure allocation code that allocates in new
1117 // space for nested functions that don't need literals cloning. If
1118 // we're running with the --always-opt or the --prepare-always-opt
1119 // flag, we need to use the runtime function so that the new function
1120 // we are creating here gets a chance to have its code optimized and
1121 // doesn't just get a copy of the existing unoptimized code.
1122 if (!FLAG_always_opt &&
1123 !FLAG_prepare_always_opt &&
1124 !pretenure &&
1125 scope()->is_function_scope() &&
1126 info->num_literals() == 0) {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001127 FastNewClosureStub stub(info->language_mode());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001128 __ li(a0, Operand(info));
1129 __ push(a0);
1130 __ CallStub(&stub);
1131 } else {
1132 __ li(a0, Operand(info));
1133 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1134 : Heap::kFalseValueRootIndex);
1135 __ Push(cp, a0, a1);
1136 __ CallRuntime(Runtime::kNewClosure, 3);
1137 }
1138 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001139}
1140
1141
1142void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001143 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001144 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001145}
1146
1147
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001148void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1149 TypeofState typeof_state,
1150 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001151 Register current = cp;
1152 Register next = a1;
1153 Register temp = a2;
1154
1155 Scope* s = scope();
1156 while (s != NULL) {
1157 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001158 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001159 // Check that extension is NULL.
1160 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1161 __ Branch(slow, ne, temp, Operand(zero_reg));
1162 }
1163 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001164 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001165 // Walk the rest of the chain without clobbering cp.
1166 current = next;
1167 }
1168 // If no outer scope calls eval, we do not need to check more
1169 // context extensions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001170 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001171 s = s->outer_scope();
1172 }
1173
1174 if (s->is_eval_scope()) {
1175 Label loop, fast;
1176 if (!current.is(next)) {
1177 __ Move(next, current);
1178 }
1179 __ bind(&loop);
1180 // Terminate at global context.
1181 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1182 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1183 __ Branch(&fast, eq, temp, Operand(t0));
1184 // Check that extension is NULL.
1185 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1186 __ Branch(slow, ne, temp, Operand(zero_reg));
1187 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001188 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001189 __ Branch(&loop);
1190 __ bind(&fast);
1191 }
1192
1193 __ lw(a0, GlobalObjectOperand());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001194 __ li(a2, Operand(var->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001195 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1196 ? RelocInfo::CODE_TARGET
1197 : RelocInfo::CODE_TARGET_CONTEXT;
1198 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001199 __ Call(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001200}
1201
1202
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001203MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1204 Label* slow) {
1205 ASSERT(var->IsContextSlot());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001206 Register context = cp;
1207 Register next = a3;
1208 Register temp = t0;
1209
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001210 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001211 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001212 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001213 // Check that extension is NULL.
1214 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1215 __ Branch(slow, ne, temp, Operand(zero_reg));
1216 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001217 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001218 // Walk the rest of the chain without clobbering cp.
1219 context = next;
1220 }
1221 }
1222 // Check that last extension is NULL.
1223 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1224 __ Branch(slow, ne, temp, Operand(zero_reg));
1225
1226 // This function is used only for loads, not stores, so it's safe to
1227 // return an cp-based operand (the write barrier cannot be allowed to
1228 // destroy the cp register).
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001229 return ContextOperand(context, var->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001230}
1231
1232
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001233void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1234 TypeofState typeof_state,
1235 Label* slow,
1236 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001237 // Generate fast-case code for variables that might be shadowed by
1238 // eval-introduced variables. Eval is used a lot without
1239 // introducing variables. In those cases, we do not want to
1240 // perform a runtime call for all variables in the scope
1241 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001242 if (var->mode() == DYNAMIC_GLOBAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001243 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001244 __ Branch(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001245 } else if (var->mode() == DYNAMIC_LOCAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001246 Variable* local = var->local_if_not_shadowed();
1247 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001248 if (local->mode() == CONST ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001249 local->mode() == CONST_HARMONY ||
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001250 local->mode() == LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001251 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1252 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001253 if (local->mode() == CONST) {
1254 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1255 __ movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001256 } else { // LET || CONST_HARMONY
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001257 __ Branch(done, ne, at, Operand(zero_reg));
1258 __ li(a0, Operand(var->name()));
1259 __ push(a0);
1260 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1261 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001262 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001263 __ Branch(done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001264 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001265}
1266
1267
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001268void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1269 // Record position before possible IC call.
1270 SetSourcePosition(proxy->position());
1271 Variable* var = proxy->var();
1272
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001273 // Three cases: global variables, lookup variables, and all other types of
1274 // variables.
1275 switch (var->location()) {
1276 case Variable::UNALLOCATED: {
1277 Comment cmnt(masm_, "Global variable");
1278 // Use inline caching. Variable name is passed in a2 and the global
1279 // object (receiver) in a0.
1280 __ lw(a0, GlobalObjectOperand());
1281 __ li(a2, Operand(var->name()));
1282 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1283 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
1284 context()->Plug(v0);
1285 break;
1286 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001287
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001288 case Variable::PARAMETER:
1289 case Variable::LOCAL:
1290 case Variable::CONTEXT: {
1291 Comment cmnt(masm_, var->IsContextSlot()
1292 ? "Context variable"
1293 : "Stack variable");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001294 if (var->binding_needs_init()) {
1295 // var->scope() may be NULL when the proxy is located in eval code and
1296 // refers to a potential outside binding. Currently those bindings are
1297 // always looked up dynamically, i.e. in that case
1298 // var->location() == LOOKUP.
1299 // always holds.
1300 ASSERT(var->scope() != NULL);
1301
1302 // Check if the binding really needs an initialization check. The check
1303 // can be skipped in the following situation: we have a LET or CONST
1304 // binding in harmony mode, both the Variable and the VariableProxy have
1305 // the same declaration scope (i.e. they are both in global code, in the
1306 // same function or in the same eval code) and the VariableProxy is in
1307 // the source physically located after the initializer of the variable.
1308 //
1309 // We cannot skip any initialization checks for CONST in non-harmony
1310 // mode because const variables may be declared but never initialized:
1311 // if (false) { const x; }; var y = x;
1312 //
1313 // The condition on the declaration scopes is a conservative check for
1314 // nested functions that access a binding and are called before the
1315 // binding is initialized:
1316 // function() { f(); let x = 1; function f() { x = 2; } }
1317 //
1318 bool skip_init_check;
1319 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1320 skip_init_check = false;
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001321 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001322 // Check that we always have valid source position.
1323 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1324 ASSERT(proxy->position() != RelocInfo::kNoPosition);
1325 skip_init_check = var->mode() != CONST &&
1326 var->initializer_position() < proxy->position();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001327 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001328
1329 if (!skip_init_check) {
1330 // Let and const need a read barrier.
1331 GetVar(v0, var);
1332 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1333 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1334 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1335 // Throw a reference error when using an uninitialized let/const
1336 // binding in harmony mode.
1337 Label done;
1338 __ Branch(&done, ne, at, Operand(zero_reg));
1339 __ li(a0, Operand(var->name()));
1340 __ push(a0);
1341 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1342 __ bind(&done);
1343 } else {
1344 // Uninitalized const bindings outside of harmony mode are unholed.
1345 ASSERT(var->mode() == CONST);
1346 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1347 __ movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1348 }
1349 context()->Plug(v0);
1350 break;
1351 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001352 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001353 context()->Plug(var);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001354 break;
1355 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001356
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001357 case Variable::LOOKUP: {
1358 Label done, slow;
1359 // Generate code for loading from variables potentially shadowed
1360 // by eval-introduced variables.
1361 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1362 __ bind(&slow);
1363 Comment cmnt(masm_, "Lookup variable");
1364 __ li(a1, Operand(var->name()));
1365 __ Push(cp, a1); // Context and name.
1366 __ CallRuntime(Runtime::kLoadContextSlot, 2);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001367 __ bind(&done);
1368 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001369 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001370 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001371}
1372
1373
1374void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001375 Comment cmnt(masm_, "[ RegExpLiteral");
1376 Label materialized;
1377 // Registers will be used as follows:
1378 // t1 = materialized value (RegExp literal)
1379 // t0 = JS function, literals array
1380 // a3 = literal index
1381 // a2 = RegExp pattern
1382 // a1 = RegExp flags
1383 // a0 = RegExp literal clone
1384 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1385 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1386 int literal_offset =
1387 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1388 __ lw(t1, FieldMemOperand(t0, literal_offset));
1389 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1390 __ Branch(&materialized, ne, t1, Operand(at));
1391
1392 // Create regexp literal using runtime function.
1393 // Result will be in v0.
1394 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1395 __ li(a2, Operand(expr->pattern()));
1396 __ li(a1, Operand(expr->flags()));
1397 __ Push(t0, a3, a2, a1);
1398 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1399 __ mov(t1, v0);
1400
1401 __ bind(&materialized);
1402 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1403 Label allocated, runtime_allocate;
1404 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1405 __ jmp(&allocated);
1406
1407 __ bind(&runtime_allocate);
1408 __ push(t1);
1409 __ li(a0, Operand(Smi::FromInt(size)));
1410 __ push(a0);
1411 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1412 __ pop(t1);
1413
1414 __ bind(&allocated);
1415
1416 // After this, registers are used as follows:
1417 // v0: Newly allocated regexp.
1418 // t1: Materialized regexp.
1419 // a2: temp.
1420 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1421 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001422}
1423
1424
1425void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001426 Comment cmnt(masm_, "[ ObjectLiteral");
1427 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1428 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1429 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1430 __ li(a1, Operand(expr->constant_properties()));
1431 int flags = expr->fast_elements()
1432 ? ObjectLiteral::kFastElements
1433 : ObjectLiteral::kNoFlags;
1434 flags |= expr->has_function()
1435 ? ObjectLiteral::kHasFunction
1436 : ObjectLiteral::kNoFlags;
1437 __ li(a0, Operand(Smi::FromInt(flags)));
1438 __ Push(a3, a2, a1, a0);
1439 if (expr->depth() > 1) {
1440 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1441 } else {
1442 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
1443 }
1444
1445 // If result_saved is true the result is on top of the stack. If
1446 // result_saved is false the result is in v0.
1447 bool result_saved = false;
1448
1449 // Mark all computed expressions that are bound to a key that
1450 // is shadowed by a later occurrence of the same key. For the
1451 // marked expressions, no store code is emitted.
1452 expr->CalculateEmitStore();
1453
1454 for (int i = 0; i < expr->properties()->length(); i++) {
1455 ObjectLiteral::Property* property = expr->properties()->at(i);
1456 if (property->IsCompileTimeValue()) continue;
1457
1458 Literal* key = property->key();
1459 Expression* value = property->value();
1460 if (!result_saved) {
1461 __ push(v0); // Save result on stack.
1462 result_saved = true;
1463 }
1464 switch (property->kind()) {
1465 case ObjectLiteral::Property::CONSTANT:
1466 UNREACHABLE();
1467 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1468 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1469 // Fall through.
1470 case ObjectLiteral::Property::COMPUTED:
1471 if (key->handle()->IsSymbol()) {
1472 if (property->emit_store()) {
1473 VisitForAccumulatorValue(value);
1474 __ mov(a0, result_register());
1475 __ li(a2, Operand(key->handle()));
1476 __ lw(a1, MemOperand(sp));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001477 Handle<Code> ic = is_classic_mode()
1478 ? isolate()->builtins()->StoreIC_Initialize()
1479 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001480 __ Call(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001481 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1482 } else {
1483 VisitForEffect(value);
1484 }
1485 break;
1486 }
1487 // Fall through.
1488 case ObjectLiteral::Property::PROTOTYPE:
1489 // Duplicate receiver on stack.
1490 __ lw(a0, MemOperand(sp));
1491 __ push(a0);
1492 VisitForStackValue(key);
1493 VisitForStackValue(value);
1494 if (property->emit_store()) {
1495 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1496 __ push(a0);
1497 __ CallRuntime(Runtime::kSetProperty, 4);
1498 } else {
1499 __ Drop(3);
1500 }
1501 break;
1502 case ObjectLiteral::Property::GETTER:
1503 case ObjectLiteral::Property::SETTER:
1504 // Duplicate receiver on stack.
1505 __ lw(a0, MemOperand(sp));
1506 __ push(a0);
1507 VisitForStackValue(key);
1508 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1509 Smi::FromInt(1) :
1510 Smi::FromInt(0)));
1511 __ push(a1);
1512 VisitForStackValue(value);
1513 __ CallRuntime(Runtime::kDefineAccessor, 4);
1514 break;
1515 }
1516 }
1517
1518 if (expr->has_function()) {
1519 ASSERT(result_saved);
1520 __ lw(a0, MemOperand(sp));
1521 __ push(a0);
1522 __ CallRuntime(Runtime::kToFastProperties, 1);
1523 }
1524
1525 if (result_saved) {
1526 context()->PlugTOS();
1527 } else {
1528 context()->Plug(v0);
1529 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001530}
1531
1532
1533void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001534 Comment cmnt(masm_, "[ ArrayLiteral");
1535
1536 ZoneList<Expression*>* subexprs = expr->values();
1537 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001538
1539 Handle<FixedArray> constant_elements = expr->constant_elements();
1540 ASSERT_EQ(2, constant_elements->length());
1541 ElementsKind constant_elements_kind =
1542 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
1543 Handle<FixedArrayBase> constant_elements_values(
1544 FixedArrayBase::cast(constant_elements->get(1)));
1545
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001546 __ mov(a0, result_register());
1547 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1548 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1549 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001550 __ li(a1, Operand(constant_elements));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001551 __ Push(a3, a2, a1);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001552 if (constant_elements_values->map() ==
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001553 isolate()->heap()->fixed_cow_array_map()) {
1554 FastCloneShallowArrayStub stub(
1555 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1556 __ CallStub(&stub);
1557 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1558 1, a1, a2);
1559 } else if (expr->depth() > 1) {
1560 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1561 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1562 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1563 } else {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001564 ASSERT(constant_elements_kind == FAST_ELEMENTS ||
1565 constant_elements_kind == FAST_SMI_ONLY_ELEMENTS ||
1566 FLAG_smi_only_arrays);
1567 FastCloneShallowArrayStub::Mode mode =
1568 constant_elements_kind == FAST_DOUBLE_ELEMENTS
1569 ? FastCloneShallowArrayStub::CLONE_DOUBLE_ELEMENTS
1570 : FastCloneShallowArrayStub::CLONE_ELEMENTS;
1571 FastCloneShallowArrayStub stub(mode, length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001572 __ CallStub(&stub);
1573 }
1574
1575 bool result_saved = false; // Is the result saved to the stack?
1576
1577 // Emit code to evaluate all the non-constant subexpressions and to store
1578 // them into the newly cloned array.
1579 for (int i = 0; i < length; i++) {
1580 Expression* subexpr = subexprs->at(i);
1581 // If the subexpression is a literal or a simple materialized literal it
1582 // is already set in the cloned array.
1583 if (subexpr->AsLiteral() != NULL ||
1584 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1585 continue;
1586 }
1587
1588 if (!result_saved) {
1589 __ push(v0);
1590 result_saved = true;
1591 }
1592 VisitForAccumulatorValue(subexpr);
1593
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001594 __ lw(t6, MemOperand(sp)); // Copy of array literal.
1595 __ lw(a1, FieldMemOperand(t6, JSObject::kElementsOffset));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001596 __ lw(a2, FieldMemOperand(t6, JSObject::kMapOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001597 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001598
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001599 Label element_done;
1600 Label double_elements;
1601 Label smi_element;
1602 Label slow_elements;
1603 Label fast_elements;
1604 __ CheckFastElements(a2, a3, &double_elements);
1605
1606 // FAST_SMI_ONLY_ELEMENTS or FAST_ELEMENTS
1607 __ JumpIfSmi(result_register(), &smi_element);
1608 __ CheckFastSmiOnlyElements(a2, a3, &fast_elements);
1609
1610 // Store into the array literal requires a elements transition. Call into
1611 // the runtime.
1612 __ bind(&slow_elements);
1613 __ push(t6); // Copy of array literal.
1614 __ li(a1, Operand(Smi::FromInt(i)));
1615 __ li(a2, Operand(Smi::FromInt(NONE))); // PropertyAttributes
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001616 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
1617 ? kNonStrictMode : kStrictMode;
1618 __ li(a3, Operand(Smi::FromInt(strict_mode_flag))); // Strict mode.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001619 __ Push(a1, result_register(), a2, a3);
1620 __ CallRuntime(Runtime::kSetProperty, 5);
1621 __ Branch(&element_done);
1622
1623 // Array literal has ElementsKind of FAST_DOUBLE_ELEMENTS.
1624 __ bind(&double_elements);
1625 __ li(a3, Operand(Smi::FromInt(i)));
1626 __ StoreNumberToDoubleElements(result_register(), a3, t6, a1, t0, t1, t5,
1627 t3, &slow_elements);
1628 __ Branch(&element_done);
1629
1630 // Array literal has ElementsKind of FAST_ELEMENTS and value is an object.
1631 __ bind(&fast_elements);
1632 __ sw(result_register(), FieldMemOperand(a1, offset));
1633 // Update the write barrier for the array store.
1634
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001635 __ RecordWriteField(
1636 a1, offset, result_register(), a2, kRAHasBeenSaved, kDontSaveFPRegs,
1637 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001638 __ Branch(&element_done);
1639
1640 // Array literal has ElementsKind of FAST_SMI_ONLY_ELEMENTS or
1641 // FAST_ELEMENTS, and value is Smi.
1642 __ bind(&smi_element);
1643 __ sw(result_register(), FieldMemOperand(a1, offset));
1644 // Fall through
1645
1646 __ bind(&element_done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001647
1648 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1649 }
1650
1651 if (result_saved) {
1652 context()->PlugTOS();
1653 } else {
1654 context()->Plug(v0);
1655 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001656}
1657
1658
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001659void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001660 Comment cmnt(masm_, "[ Assignment");
1661 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1662 // on the left-hand side.
1663 if (!expr->target()->IsValidLeftHandSide()) {
1664 VisitForEffect(expr->target());
1665 return;
1666 }
1667
1668 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001669 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001670 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1671 LhsKind assign_type = VARIABLE;
1672 Property* property = expr->target()->AsProperty();
1673 if (property != NULL) {
1674 assign_type = (property->key()->IsPropertyName())
1675 ? NAMED_PROPERTY
1676 : KEYED_PROPERTY;
1677 }
1678
1679 // Evaluate LHS expression.
1680 switch (assign_type) {
1681 case VARIABLE:
1682 // Nothing to do here.
1683 break;
1684 case NAMED_PROPERTY:
1685 if (expr->is_compound()) {
1686 // We need the receiver both on the stack and in the accumulator.
1687 VisitForAccumulatorValue(property->obj());
1688 __ push(result_register());
1689 } else {
1690 VisitForStackValue(property->obj());
1691 }
1692 break;
1693 case KEYED_PROPERTY:
1694 // We need the key and receiver on both the stack and in v0 and a1.
1695 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001696 VisitForStackValue(property->obj());
1697 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001698 __ lw(a1, MemOperand(sp, 0));
1699 __ push(v0);
1700 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001701 VisitForStackValue(property->obj());
1702 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001703 }
1704 break;
1705 }
1706
1707 // For compound assignments we need another deoptimization point after the
1708 // variable/property load.
1709 if (expr->is_compound()) {
1710 { AccumulatorValueContext context(this);
1711 switch (assign_type) {
1712 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001713 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001714 PrepareForBailout(expr->target(), TOS_REG);
1715 break;
1716 case NAMED_PROPERTY:
1717 EmitNamedPropertyLoad(property);
1718 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1719 break;
1720 case KEYED_PROPERTY:
1721 EmitKeyedPropertyLoad(property);
1722 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1723 break;
1724 }
1725 }
1726
1727 Token::Value op = expr->binary_op();
1728 __ push(v0); // Left operand goes on the stack.
1729 VisitForAccumulatorValue(expr->value());
1730
1731 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1732 ? OVERWRITE_RIGHT
1733 : NO_OVERWRITE;
1734 SetSourcePosition(expr->position() + 1);
1735 AccumulatorValueContext context(this);
1736 if (ShouldInlineSmiCase(op)) {
1737 EmitInlineSmiBinaryOp(expr->binary_operation(),
1738 op,
1739 mode,
1740 expr->target(),
1741 expr->value());
1742 } else {
1743 EmitBinaryOp(expr->binary_operation(), op, mode);
1744 }
1745
1746 // Deoptimization point in case the binary operation may have side effects.
1747 PrepareForBailout(expr->binary_operation(), TOS_REG);
1748 } else {
1749 VisitForAccumulatorValue(expr->value());
1750 }
1751
1752 // Record source position before possible IC call.
1753 SetSourcePosition(expr->position());
1754
1755 // Store the value.
1756 switch (assign_type) {
1757 case VARIABLE:
1758 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1759 expr->op());
1760 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1761 context()->Plug(v0);
1762 break;
1763 case NAMED_PROPERTY:
1764 EmitNamedPropertyAssignment(expr);
1765 break;
1766 case KEYED_PROPERTY:
1767 EmitKeyedPropertyAssignment(expr);
1768 break;
1769 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001770}
1771
1772
ager@chromium.org5c838252010-02-19 08:53:10 +00001773void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001774 SetSourcePosition(prop->position());
1775 Literal* key = prop->key()->AsLiteral();
1776 __ mov(a0, result_register());
1777 __ li(a2, Operand(key->handle()));
1778 // Call load IC. It has arguments receiver and property name a0 and a2.
1779 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001780 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001781}
1782
1783
1784void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001785 SetSourcePosition(prop->position());
1786 __ mov(a0, result_register());
1787 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1788 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001789 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001790}
1791
1792
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001793void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001794 Token::Value op,
1795 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001796 Expression* left_expr,
1797 Expression* right_expr) {
1798 Label done, smi_case, stub_call;
1799
1800 Register scratch1 = a2;
1801 Register scratch2 = a3;
1802
1803 // Get the arguments.
1804 Register left = a1;
1805 Register right = a0;
1806 __ pop(left);
1807 __ mov(a0, result_register());
1808
1809 // Perform combined smi check on both operands.
1810 __ Or(scratch1, left, Operand(right));
1811 STATIC_ASSERT(kSmiTag == 0);
1812 JumpPatchSite patch_site(masm_);
1813 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1814
1815 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001816 BinaryOpStub stub(op, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001817 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001818 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001819 __ jmp(&done);
1820
1821 __ bind(&smi_case);
1822 // Smi case. This code works the same way as the smi-smi case in the type
1823 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001824 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001825 switch (op) {
1826 case Token::SAR:
1827 __ Branch(&stub_call);
1828 __ GetLeastBitsFromSmi(scratch1, right, 5);
1829 __ srav(right, left, scratch1);
1830 __ And(v0, right, Operand(~kSmiTagMask));
1831 break;
1832 case Token::SHL: {
1833 __ Branch(&stub_call);
1834 __ SmiUntag(scratch1, left);
1835 __ GetLeastBitsFromSmi(scratch2, right, 5);
1836 __ sllv(scratch1, scratch1, scratch2);
1837 __ Addu(scratch2, scratch1, Operand(0x40000000));
1838 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1839 __ SmiTag(v0, scratch1);
1840 break;
1841 }
1842 case Token::SHR: {
1843 __ Branch(&stub_call);
1844 __ SmiUntag(scratch1, left);
1845 __ GetLeastBitsFromSmi(scratch2, right, 5);
1846 __ srlv(scratch1, scratch1, scratch2);
1847 __ And(scratch2, scratch1, 0xc0000000);
1848 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1849 __ SmiTag(v0, scratch1);
1850 break;
1851 }
1852 case Token::ADD:
1853 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1854 __ BranchOnOverflow(&stub_call, scratch1);
1855 break;
1856 case Token::SUB:
1857 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1858 __ BranchOnOverflow(&stub_call, scratch1);
1859 break;
1860 case Token::MUL: {
1861 __ SmiUntag(scratch1, right);
1862 __ Mult(left, scratch1);
1863 __ mflo(scratch1);
1864 __ mfhi(scratch2);
1865 __ sra(scratch1, scratch1, 31);
1866 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1867 __ mflo(v0);
1868 __ Branch(&done, ne, v0, Operand(zero_reg));
1869 __ Addu(scratch2, right, left);
1870 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1871 ASSERT(Smi::FromInt(0) == 0);
1872 __ mov(v0, zero_reg);
1873 break;
1874 }
1875 case Token::BIT_OR:
1876 __ Or(v0, left, Operand(right));
1877 break;
1878 case Token::BIT_AND:
1879 __ And(v0, left, Operand(right));
1880 break;
1881 case Token::BIT_XOR:
1882 __ Xor(v0, left, Operand(right));
1883 break;
1884 default:
1885 UNREACHABLE();
1886 }
1887
1888 __ bind(&done);
1889 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001890}
1891
1892
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001893void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1894 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001895 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001896 __ mov(a0, result_register());
1897 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001898 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001899 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001900 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001901 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001902 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001903}
1904
1905
1906void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001907 // Invalid left-hand sides are rewritten to have a 'throw
1908 // ReferenceError' on the left-hand side.
1909 if (!expr->IsValidLeftHandSide()) {
1910 VisitForEffect(expr);
1911 return;
1912 }
1913
1914 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001915 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001916 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1917 LhsKind assign_type = VARIABLE;
1918 Property* prop = expr->AsProperty();
1919 if (prop != NULL) {
1920 assign_type = (prop->key()->IsPropertyName())
1921 ? NAMED_PROPERTY
1922 : KEYED_PROPERTY;
1923 }
1924
1925 switch (assign_type) {
1926 case VARIABLE: {
1927 Variable* var = expr->AsVariableProxy()->var();
1928 EffectContext context(this);
1929 EmitVariableAssignment(var, Token::ASSIGN);
1930 break;
1931 }
1932 case NAMED_PROPERTY: {
1933 __ push(result_register()); // Preserve value.
1934 VisitForAccumulatorValue(prop->obj());
1935 __ mov(a1, result_register());
1936 __ pop(a0); // Restore value.
1937 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001938 Handle<Code> ic = is_classic_mode()
1939 ? isolate()->builtins()->StoreIC_Initialize()
1940 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001941 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001942 break;
1943 }
1944 case KEYED_PROPERTY: {
1945 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001946 VisitForStackValue(prop->obj());
1947 VisitForAccumulatorValue(prop->key());
1948 __ mov(a1, result_register());
1949 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001950 __ pop(a0); // Restore value.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001951 Handle<Code> ic = is_classic_mode()
1952 ? isolate()->builtins()->KeyedStoreIC_Initialize()
1953 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001954 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001955 break;
1956 }
1957 }
1958 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1959 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001960}
1961
1962
1963void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001964 Token::Value op) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001965 if (var->IsUnallocated()) {
1966 // Global var, const, or let.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001967 __ mov(a0, result_register());
1968 __ li(a2, Operand(var->name()));
1969 __ lw(a1, GlobalObjectOperand());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001970 Handle<Code> ic = is_classic_mode()
1971 ? isolate()->builtins()->StoreIC_Initialize()
1972 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001973 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001974
1975 } else if (op == Token::INIT_CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001976 // Const initializers need a write barrier.
1977 ASSERT(!var->IsParameter()); // No const parameters.
1978 if (var->IsStackLocal()) {
1979 Label skip;
1980 __ lw(a1, StackOperand(var));
1981 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1982 __ Branch(&skip, ne, a1, Operand(t0));
1983 __ sw(result_register(), StackOperand(var));
1984 __ bind(&skip);
1985 } else {
1986 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
1987 // Like var declarations, const declarations are hoisted to function
1988 // scope. However, unlike var initializers, const initializers are
1989 // able to drill a hole to that function context, even from inside a
1990 // 'with' context. We thus bypass the normal static scope lookup for
1991 // var->IsContextSlot().
1992 __ push(v0);
1993 __ li(a0, Operand(var->name()));
1994 __ Push(cp, a0); // Context and name.
1995 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001996 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001997
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001998 } else if (var->mode() == LET && op != Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001999 // Non-initializing assignment to let variable needs a write barrier.
2000 if (var->IsLookupSlot()) {
2001 __ push(v0); // Value.
2002 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002003 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002004 __ Push(cp, a1, a0); // Context, name, strict mode.
2005 __ CallRuntime(Runtime::kStoreContextSlot, 4);
2006 } else {
2007 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
2008 Label assign;
2009 MemOperand location = VarOperand(var, a1);
2010 __ lw(a3, location);
2011 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2012 __ Branch(&assign, ne, a3, Operand(t0));
2013 __ li(a3, Operand(var->name()));
2014 __ push(a3);
2015 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2016 // Perform the assignment.
2017 __ bind(&assign);
2018 __ sw(result_register(), location);
2019 if (var->IsContextSlot()) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002020 // RecordWrite may destroy all its register arguments.
2021 __ mov(a3, result_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002022 int offset = Context::SlotOffset(var->index());
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002023 __ RecordWriteContextSlot(
2024 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002025 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002026 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002027
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002028 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2029 // Assignment to var or initializing assignment to let/const
2030 // in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002031 if (var->IsStackAllocated() || var->IsContextSlot()) {
2032 MemOperand location = VarOperand(var, a1);
2033 if (FLAG_debug_code && op == Token::INIT_LET) {
2034 // Check for an uninitialized let binding.
2035 __ lw(a2, location);
2036 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2037 __ Check(eq, "Let binding re-initialization.", a2, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002038 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002039 // Perform the assignment.
2040 __ sw(v0, location);
2041 if (var->IsContextSlot()) {
2042 __ mov(a3, v0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002043 int offset = Context::SlotOffset(var->index());
2044 __ RecordWriteContextSlot(
2045 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002046 }
2047 } else {
2048 ASSERT(var->IsLookupSlot());
2049 __ push(v0); // Value.
2050 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002051 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002052 __ Push(cp, a1, a0); // Context, name, strict mode.
2053 __ CallRuntime(Runtime::kStoreContextSlot, 4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002054 }
2055 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002056 // Non-initializing assignments to consts are ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +00002057}
2058
2059
2060void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002061 // Assignment to a property, using a named store IC.
2062 Property* prop = expr->target()->AsProperty();
2063 ASSERT(prop != NULL);
2064 ASSERT(prop->key()->AsLiteral() != NULL);
2065
2066 // If the assignment starts a block of assignments to the same object,
2067 // change to slow case to avoid the quadratic behavior of repeatedly
2068 // adding fast properties.
2069 if (expr->starts_initialization_block()) {
2070 __ push(result_register());
2071 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
2072 __ push(t0);
2073 __ CallRuntime(Runtime::kToSlowProperties, 1);
2074 __ pop(result_register());
2075 }
2076
2077 // Record source code position before IC call.
2078 SetSourcePosition(expr->position());
2079 __ mov(a0, result_register()); // Load the value.
2080 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
2081 // Load receiver to a1. Leave a copy in the stack if needed for turning the
2082 // receiver into fast case.
2083 if (expr->ends_initialization_block()) {
2084 __ lw(a1, MemOperand(sp));
2085 } else {
2086 __ pop(a1);
2087 }
2088
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002089 Handle<Code> ic = is_classic_mode()
2090 ? isolate()->builtins()->StoreIC_Initialize()
2091 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002092 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002093
2094 // If the assignment ends an initialization block, revert to fast case.
2095 if (expr->ends_initialization_block()) {
2096 __ push(v0); // Result of assignment, saved even if not needed.
2097 // Receiver is under the result value.
2098 __ lw(t0, MemOperand(sp, kPointerSize));
2099 __ push(t0);
2100 __ CallRuntime(Runtime::kToFastProperties, 1);
2101 __ pop(v0);
2102 __ Drop(1);
2103 }
2104 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2105 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002106}
2107
2108
2109void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002110 // Assignment to a property, using a keyed store IC.
2111
2112 // If the assignment starts a block of assignments to the same object,
2113 // change to slow case to avoid the quadratic behavior of repeatedly
2114 // adding fast properties.
2115 if (expr->starts_initialization_block()) {
2116 __ push(result_register());
2117 // Receiver is now under the key and value.
2118 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2119 __ push(t0);
2120 __ CallRuntime(Runtime::kToSlowProperties, 1);
2121 __ pop(result_register());
2122 }
2123
2124 // Record source code position before IC call.
2125 SetSourcePosition(expr->position());
2126 // Call keyed store IC.
2127 // The arguments are:
2128 // - a0 is the value,
2129 // - a1 is the key,
2130 // - a2 is the receiver.
2131 __ mov(a0, result_register());
2132 __ pop(a1); // Key.
2133 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2134 // receiver into fast case.
2135 if (expr->ends_initialization_block()) {
2136 __ lw(a2, MemOperand(sp));
2137 } else {
2138 __ pop(a2);
2139 }
2140
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002141 Handle<Code> ic = is_classic_mode()
2142 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2143 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002144 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002145
2146 // If the assignment ends an initialization block, revert to fast case.
2147 if (expr->ends_initialization_block()) {
2148 __ push(v0); // Result of assignment, saved even if not needed.
2149 // Receiver is under the result value.
2150 __ lw(t0, MemOperand(sp, kPointerSize));
2151 __ push(t0);
2152 __ CallRuntime(Runtime::kToFastProperties, 1);
2153 __ pop(v0);
2154 __ Drop(1);
2155 }
2156 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2157 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002158}
2159
2160
2161void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002162 Comment cmnt(masm_, "[ Property");
2163 Expression* key = expr->key();
2164
2165 if (key->IsPropertyName()) {
2166 VisitForAccumulatorValue(expr->obj());
2167 EmitNamedPropertyLoad(expr);
2168 context()->Plug(v0);
2169 } else {
2170 VisitForStackValue(expr->obj());
2171 VisitForAccumulatorValue(expr->key());
2172 __ pop(a1);
2173 EmitKeyedPropertyLoad(expr);
2174 context()->Plug(v0);
2175 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002176}
2177
lrn@chromium.org7516f052011-03-30 08:52:27 +00002178
ager@chromium.org5c838252010-02-19 08:53:10 +00002179void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002180 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002181 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002182 // Code common for calls using the IC.
2183 ZoneList<Expression*>* args = expr->arguments();
2184 int arg_count = args->length();
2185 { PreservePositionScope scope(masm()->positions_recorder());
2186 for (int i = 0; i < arg_count; i++) {
2187 VisitForStackValue(args->at(i));
2188 }
2189 __ li(a2, Operand(name));
2190 }
2191 // Record source position for debugger.
2192 SetSourcePosition(expr->position());
2193 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002194 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002195 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002196 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002197 RecordJSReturnSite(expr);
2198 // Restore context register.
2199 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2200 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002201}
2202
2203
lrn@chromium.org7516f052011-03-30 08:52:27 +00002204void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002205 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002206 // Load the key.
2207 VisitForAccumulatorValue(key);
2208
2209 // Swap the name of the function and the receiver on the stack to follow
2210 // the calling convention for call ICs.
2211 __ pop(a1);
2212 __ push(v0);
2213 __ push(a1);
2214
2215 // Code common for calls using the IC.
2216 ZoneList<Expression*>* args = expr->arguments();
2217 int arg_count = args->length();
2218 { PreservePositionScope scope(masm()->positions_recorder());
2219 for (int i = 0; i < arg_count; i++) {
2220 VisitForStackValue(args->at(i));
2221 }
2222 }
2223 // Record source position for debugger.
2224 SetSourcePosition(expr->position());
2225 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002226 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002227 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002228 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002229 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002230 RecordJSReturnSite(expr);
2231 // Restore context register.
2232 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2233 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002234}
2235
2236
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002237void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002238 // Code common for calls using the call stub.
2239 ZoneList<Expression*>* args = expr->arguments();
2240 int arg_count = args->length();
2241 { PreservePositionScope scope(masm()->positions_recorder());
2242 for (int i = 0; i < arg_count; i++) {
2243 VisitForStackValue(args->at(i));
2244 }
2245 }
2246 // Record source position for debugger.
2247 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002248 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002249 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002250 __ CallStub(&stub);
2251 RecordJSReturnSite(expr);
2252 // Restore context register.
2253 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2254 context()->DropAndPlug(1, v0);
2255}
2256
2257
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002258void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002259 // Push copy of the first argument or undefined if it doesn't exist.
2260 if (arg_count > 0) {
2261 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2262 } else {
2263 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2264 }
2265 __ push(a1);
2266
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002267 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002268 int receiver_offset = 2 + info_->scope()->num_parameters();
2269 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002270 __ push(a1);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002271 // Push the language mode.
2272 __ li(a1, Operand(Smi::FromInt(language_mode())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002273 __ push(a1);
2274
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002275 // Push the start position of the scope the calls resides in.
2276 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
2277 __ push(a1);
2278
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002279 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002280 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org5c838252010-02-19 08:53:10 +00002281}
2282
2283
2284void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002285#ifdef DEBUG
2286 // We want to verify that RecordJSReturnSite gets called on all paths
2287 // through this function. Avoid early returns.
2288 expr->return_is_recorded_ = false;
2289#endif
2290
2291 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002292 Expression* callee = expr->expression();
2293 VariableProxy* proxy = callee->AsVariableProxy();
2294 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002295
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002296 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002297 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2298 // resolve the function we need to call and the receiver of the
2299 // call. Then we call the resolved function using the given
2300 // arguments.
2301 ZoneList<Expression*>* args = expr->arguments();
2302 int arg_count = args->length();
2303
2304 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002305 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002306 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2307 __ push(a2); // Reserved receiver slot.
2308
2309 // Push the arguments.
2310 for (int i = 0; i < arg_count; i++) {
2311 VisitForStackValue(args->at(i));
2312 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002313
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002314 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002315 // resolve eval.
2316 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2317 __ push(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002318 EmitResolvePossiblyDirectEval(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002319
2320 // The runtime call returns a pair of values in v0 (function) and
2321 // v1 (receiver). Touch up the stack with the right values.
2322 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2323 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2324 }
2325 // Record source position for debugger.
2326 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002327 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002328 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002329 __ CallStub(&stub);
2330 RecordJSReturnSite(expr);
2331 // Restore context register.
2332 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2333 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002334 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002335 // Push global object as receiver for the call IC.
2336 __ lw(a0, GlobalObjectOperand());
2337 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002338 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2339 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002340 // Call to a lookup slot (dynamically introduced variable).
2341 Label slow, done;
2342
2343 { PreservePositionScope scope(masm()->positions_recorder());
2344 // Generate code for loading from variables potentially shadowed
2345 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002346 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002347 }
2348
2349 __ bind(&slow);
2350 // Call the runtime to find the function to call (returned in v0)
2351 // and the object holding it (returned in v1).
2352 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002353 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002354 __ push(a2);
2355 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2356 __ Push(v0, v1); // Function, receiver.
2357
2358 // If fast case code has been generated, emit code to push the
2359 // function and receiver and have the slow path jump around this
2360 // code.
2361 if (done.is_linked()) {
2362 Label call;
2363 __ Branch(&call);
2364 __ bind(&done);
2365 // Push function.
2366 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002367 // The receiver is implicitly the global receiver. Indicate this
2368 // by passing the hole to the call function stub.
2369 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002370 __ push(a1);
2371 __ bind(&call);
2372 }
2373
danno@chromium.org40cb8782011-05-25 07:58:50 +00002374 // The receiver is either the global receiver or an object found
2375 // by LoadContextSlot. That object could be the hole if the
2376 // receiver is implicitly the global object.
2377 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002378 } else if (property != NULL) {
2379 { PreservePositionScope scope(masm()->positions_recorder());
2380 VisitForStackValue(property->obj());
2381 }
2382 if (property->key()->IsPropertyName()) {
2383 EmitCallWithIC(expr,
2384 property->key()->AsLiteral()->handle(),
2385 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002386 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002387 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002388 }
2389 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002390 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002391 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002392 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002393 }
2394 // Load global receiver object.
2395 __ lw(a1, GlobalObjectOperand());
2396 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2397 __ push(a1);
2398 // Emit function call.
2399 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2400 }
2401
2402#ifdef DEBUG
2403 // RecordJSReturnSite should have been called.
2404 ASSERT(expr->return_is_recorded_);
2405#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002406}
2407
2408
2409void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002410 Comment cmnt(masm_, "[ CallNew");
2411 // According to ECMA-262, section 11.2.2, page 44, the function
2412 // expression in new calls must be evaluated before the
2413 // arguments.
2414
2415 // Push constructor on the stack. If it's not a function it's used as
2416 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2417 // ignored.
2418 VisitForStackValue(expr->expression());
2419
2420 // Push the arguments ("left-to-right") on the stack.
2421 ZoneList<Expression*>* args = expr->arguments();
2422 int arg_count = args->length();
2423 for (int i = 0; i < arg_count; i++) {
2424 VisitForStackValue(args->at(i));
2425 }
2426
2427 // Call the construct call builtin that handles allocation and
2428 // constructor invocation.
2429 SetSourcePosition(expr->position());
2430
2431 // Load function and argument count into a1 and a0.
2432 __ li(a0, Operand(arg_count));
2433 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2434
2435 Handle<Code> construct_builtin =
2436 isolate()->builtins()->JSConstructCall();
2437 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2438 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002439}
2440
2441
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002442void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2443 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002444 ASSERT(args->length() == 1);
2445
2446 VisitForAccumulatorValue(args->at(0));
2447
2448 Label materialize_true, materialize_false;
2449 Label* if_true = NULL;
2450 Label* if_false = NULL;
2451 Label* fall_through = NULL;
2452 context()->PrepareTest(&materialize_true, &materialize_false,
2453 &if_true, &if_false, &fall_through);
2454
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002455 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002456 __ And(t0, v0, Operand(kSmiTagMask));
2457 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2458
2459 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002460}
2461
2462
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002463void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2464 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002465 ASSERT(args->length() == 1);
2466
2467 VisitForAccumulatorValue(args->at(0));
2468
2469 Label materialize_true, materialize_false;
2470 Label* if_true = NULL;
2471 Label* if_false = NULL;
2472 Label* fall_through = NULL;
2473 context()->PrepareTest(&materialize_true, &materialize_false,
2474 &if_true, &if_false, &fall_through);
2475
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002476 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002477 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2478 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2479
2480 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002481}
2482
2483
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002484void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2485 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002486 ASSERT(args->length() == 1);
2487
2488 VisitForAccumulatorValue(args->at(0));
2489
2490 Label materialize_true, materialize_false;
2491 Label* if_true = NULL;
2492 Label* if_false = NULL;
2493 Label* fall_through = NULL;
2494 context()->PrepareTest(&materialize_true, &materialize_false,
2495 &if_true, &if_false, &fall_through);
2496
2497 __ JumpIfSmi(v0, if_false);
2498 __ LoadRoot(at, Heap::kNullValueRootIndex);
2499 __ Branch(if_true, eq, v0, Operand(at));
2500 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2501 // Undetectable objects behave like undefined when tested with typeof.
2502 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2503 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2504 __ Branch(if_false, ne, at, Operand(zero_reg));
2505 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002506 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002507 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002508 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2509 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002510
2511 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002512}
2513
2514
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002515void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2516 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002517 ASSERT(args->length() == 1);
2518
2519 VisitForAccumulatorValue(args->at(0));
2520
2521 Label materialize_true, materialize_false;
2522 Label* if_true = NULL;
2523 Label* if_false = NULL;
2524 Label* fall_through = NULL;
2525 context()->PrepareTest(&materialize_true, &materialize_false,
2526 &if_true, &if_false, &fall_through);
2527
2528 __ JumpIfSmi(v0, if_false);
2529 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002530 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002531 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002532 if_true, if_false, fall_through);
2533
2534 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002535}
2536
2537
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002538void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2539 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002540 ASSERT(args->length() == 1);
2541
2542 VisitForAccumulatorValue(args->at(0));
2543
2544 Label materialize_true, materialize_false;
2545 Label* if_true = NULL;
2546 Label* if_false = NULL;
2547 Label* fall_through = NULL;
2548 context()->PrepareTest(&materialize_true, &materialize_false,
2549 &if_true, &if_false, &fall_through);
2550
2551 __ JumpIfSmi(v0, if_false);
2552 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2553 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2554 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002555 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002556 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2557
2558 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002559}
2560
2561
2562void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002563 CallRuntime* expr) {
2564 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002565 ASSERT(args->length() == 1);
2566
2567 VisitForAccumulatorValue(args->at(0));
2568
2569 Label materialize_true, materialize_false;
2570 Label* if_true = NULL;
2571 Label* if_false = NULL;
2572 Label* fall_through = NULL;
2573 context()->PrepareTest(&materialize_true, &materialize_false,
2574 &if_true, &if_false, &fall_through);
2575
2576 if (FLAG_debug_code) __ AbortIfSmi(v0);
2577
2578 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2579 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2580 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2581 __ Branch(if_true, ne, t0, Operand(zero_reg));
2582
2583 // Check for fast case object. Generate false result for slow case object.
2584 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2585 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2586 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2587 __ Branch(if_false, eq, a2, Operand(t0));
2588
2589 // Look for valueOf symbol in the descriptor array, and indicate false if
2590 // found. The type is not checked, so if it is a transition it is a false
2591 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002592 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002593 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2594 // t0: descriptor array
2595 // a3: length of descriptor array
2596 // Calculate the end of the descriptor array.
2597 STATIC_ASSERT(kSmiTag == 0);
2598 STATIC_ASSERT(kSmiTagSize == 1);
2599 STATIC_ASSERT(kPointerSize == 4);
2600 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2601 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2602 __ Addu(a2, a2, t1);
2603
2604 // Calculate location of the first key name.
2605 __ Addu(t0,
2606 t0,
2607 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2608 DescriptorArray::kFirstIndex * kPointerSize));
2609 // Loop through all the keys in the descriptor array. If one of these is the
2610 // symbol valueOf the result is false.
2611 Label entry, loop;
2612 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2613 // used in the loop below.
2614 __ li(t2, Operand(FACTORY->value_of_symbol()));
2615 __ jmp(&entry);
2616 __ bind(&loop);
2617 __ lw(a3, MemOperand(t0, 0));
2618 __ Branch(if_false, eq, a3, Operand(t2));
2619 __ Addu(t0, t0, Operand(kPointerSize));
2620 __ bind(&entry);
2621 __ Branch(&loop, ne, t0, Operand(a2));
2622
2623 // If a valueOf property is not found on the object check that it's
2624 // prototype is the un-modified String prototype. If not result is false.
2625 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2626 __ JumpIfSmi(a2, if_false);
2627 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2628 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2629 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2630 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2631 __ Branch(if_false, ne, a2, Operand(a3));
2632
2633 // Set the bit in the map to indicate that it has been checked safe for
2634 // default valueOf and set true result.
2635 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2636 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2637 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2638 __ jmp(if_true);
2639
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002640 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002641 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002642}
2643
2644
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002645void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2646 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002647 ASSERT(args->length() == 1);
2648
2649 VisitForAccumulatorValue(args->at(0));
2650
2651 Label materialize_true, materialize_false;
2652 Label* if_true = NULL;
2653 Label* if_false = NULL;
2654 Label* fall_through = NULL;
2655 context()->PrepareTest(&materialize_true, &materialize_false,
2656 &if_true, &if_false, &fall_through);
2657
2658 __ JumpIfSmi(v0, if_false);
2659 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002660 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002661 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2662 __ Branch(if_false);
2663
2664 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002665}
2666
2667
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002668void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2669 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002670 ASSERT(args->length() == 1);
2671
2672 VisitForAccumulatorValue(args->at(0));
2673
2674 Label materialize_true, materialize_false;
2675 Label* if_true = NULL;
2676 Label* if_false = NULL;
2677 Label* fall_through = NULL;
2678 context()->PrepareTest(&materialize_true, &materialize_false,
2679 &if_true, &if_false, &fall_through);
2680
2681 __ JumpIfSmi(v0, if_false);
2682 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002683 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002684 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2685 if_true, if_false, fall_through);
2686
2687 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002688}
2689
2690
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002691void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2692 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002693 ASSERT(args->length() == 1);
2694
2695 VisitForAccumulatorValue(args->at(0));
2696
2697 Label materialize_true, materialize_false;
2698 Label* if_true = NULL;
2699 Label* if_false = NULL;
2700 Label* fall_through = NULL;
2701 context()->PrepareTest(&materialize_true, &materialize_false,
2702 &if_true, &if_false, &fall_through);
2703
2704 __ JumpIfSmi(v0, if_false);
2705 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002706 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002707 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2708
2709 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002710}
2711
2712
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002713void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2714 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002715
2716 Label materialize_true, materialize_false;
2717 Label* if_true = NULL;
2718 Label* if_false = NULL;
2719 Label* fall_through = NULL;
2720 context()->PrepareTest(&materialize_true, &materialize_false,
2721 &if_true, &if_false, &fall_through);
2722
2723 // Get the frame pointer for the calling frame.
2724 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2725
2726 // Skip the arguments adaptor frame if it exists.
2727 Label check_frame_marker;
2728 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2729 __ Branch(&check_frame_marker, ne,
2730 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2731 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2732
2733 // Check the marker in the calling frame.
2734 __ bind(&check_frame_marker);
2735 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002736 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002737 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2738 if_true, if_false, fall_through);
2739
2740 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002741}
2742
2743
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002744void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2745 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002746 ASSERT(args->length() == 2);
2747
2748 // Load the two objects into registers and perform the comparison.
2749 VisitForStackValue(args->at(0));
2750 VisitForAccumulatorValue(args->at(1));
2751
2752 Label materialize_true, materialize_false;
2753 Label* if_true = NULL;
2754 Label* if_false = NULL;
2755 Label* fall_through = NULL;
2756 context()->PrepareTest(&materialize_true, &materialize_false,
2757 &if_true, &if_false, &fall_through);
2758
2759 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002760 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002761 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2762
2763 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002764}
2765
2766
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002767void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2768 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002769 ASSERT(args->length() == 1);
2770
2771 // ArgumentsAccessStub expects the key in a1 and the formal
2772 // parameter count in a0.
2773 VisitForAccumulatorValue(args->at(0));
2774 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002775 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002776 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2777 __ CallStub(&stub);
2778 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002779}
2780
2781
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002782void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2783 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002784 Label exit;
2785 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002786 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002787
2788 // Check if the calling frame is an arguments adaptor frame.
2789 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2790 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2791 __ Branch(&exit, ne, a3,
2792 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2793
2794 // Arguments adaptor case: Read the arguments length from the
2795 // adaptor frame.
2796 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2797
2798 __ bind(&exit);
2799 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002800}
2801
2802
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002803void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2804 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002805 ASSERT(args->length() == 1);
2806 Label done, null, function, non_function_constructor;
2807
2808 VisitForAccumulatorValue(args->at(0));
2809
2810 // If the object is a smi, we return null.
2811 __ JumpIfSmi(v0, &null);
2812
2813 // Check that the object is a JS object but take special care of JS
2814 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002815 // Assume that there are only two callable types, and one of them is at
2816 // either end of the type range for JS object types. Saves extra comparisons.
2817 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002818 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002819 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002820
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002821 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2822 FIRST_SPEC_OBJECT_TYPE + 1);
2823 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002824
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002825 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2826 LAST_SPEC_OBJECT_TYPE - 1);
2827 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2828 // Assume that there is no larger type.
2829 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2830
2831 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002832 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2833 __ GetObjectType(v0, a1, a1);
2834 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2835
2836 // v0 now contains the constructor function. Grab the
2837 // instance class name from there.
2838 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2839 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2840 __ Branch(&done);
2841
2842 // Functions have class 'Function'.
2843 __ bind(&function);
2844 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2845 __ jmp(&done);
2846
2847 // Objects with a non-function constructor have class 'Object'.
2848 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002849 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002850 __ jmp(&done);
2851
2852 // Non-JS objects have class null.
2853 __ bind(&null);
2854 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2855
2856 // All done.
2857 __ bind(&done);
2858
2859 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002860}
2861
2862
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002863void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002864 // Conditionally generate a log call.
2865 // Args:
2866 // 0 (literal string): The type of logging (corresponds to the flags).
2867 // This is used to determine whether or not to generate the log call.
2868 // 1 (string): Format string. Access the string at argument index 2
2869 // with '%2s' (see Logger::LogRuntime for all the formats).
2870 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002871 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002872 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002873 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2874 VisitForStackValue(args->at(1));
2875 VisitForStackValue(args->at(2));
2876 __ CallRuntime(Runtime::kLog, 2);
2877 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002878
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002879 // Finally, we're expected to leave a value on the top of the stack.
2880 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2881 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002882}
2883
2884
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002885void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
2886 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002887 Label slow_allocate_heapnumber;
2888 Label heapnumber_allocated;
2889
2890 // Save the new heap number in callee-saved register s0, since
2891 // we call out to external C code below.
2892 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2893 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2894 __ jmp(&heapnumber_allocated);
2895
2896 __ bind(&slow_allocate_heapnumber);
2897
2898 // Allocate a heap number.
2899 __ CallRuntime(Runtime::kNumberAlloc, 0);
2900 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2901
2902 __ bind(&heapnumber_allocated);
2903
2904 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2905 // by computing:
2906 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2907 if (CpuFeatures::IsSupported(FPU)) {
2908 __ PrepareCallCFunction(1, a0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002909 __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
2910 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002911 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2912
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002913 CpuFeatures::Scope scope(FPU);
2914 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2915 __ li(a1, Operand(0x41300000));
2916 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002917 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002918 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002919 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002920 // Subtract and store the result in the heap number.
2921 __ sub_d(f0, f12, f14);
2922 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2923 __ mov(v0, s0);
2924 } else {
2925 __ PrepareCallCFunction(2, a0);
2926 __ mov(a0, s0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002927 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
2928 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002929 __ CallCFunction(
2930 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2931 }
2932
2933 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002934}
2935
2936
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002937void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002938 // Load the arguments on the stack and call the stub.
2939 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002940 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002941 ASSERT(args->length() == 3);
2942 VisitForStackValue(args->at(0));
2943 VisitForStackValue(args->at(1));
2944 VisitForStackValue(args->at(2));
2945 __ CallStub(&stub);
2946 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002947}
2948
2949
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002950void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002951 // Load the arguments on the stack and call the stub.
2952 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002953 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002954 ASSERT(args->length() == 4);
2955 VisitForStackValue(args->at(0));
2956 VisitForStackValue(args->at(1));
2957 VisitForStackValue(args->at(2));
2958 VisitForStackValue(args->at(3));
2959 __ CallStub(&stub);
2960 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002961}
2962
2963
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002964void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
2965 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002966 ASSERT(args->length() == 1);
2967
2968 VisitForAccumulatorValue(args->at(0)); // Load the object.
2969
2970 Label done;
2971 // If the object is a smi return the object.
2972 __ JumpIfSmi(v0, &done);
2973 // If the object is not a value type, return the object.
2974 __ GetObjectType(v0, a1, a1);
2975 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2976
2977 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2978
2979 __ bind(&done);
2980 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002981}
2982
2983
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002984void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002985 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002986 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002987 ASSERT(args->length() == 2);
2988 VisitForStackValue(args->at(0));
2989 VisitForStackValue(args->at(1));
2990 MathPowStub stub;
2991 __ CallStub(&stub);
2992 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002993}
2994
2995
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002996void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
2997 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002998 ASSERT(args->length() == 2);
2999
3000 VisitForStackValue(args->at(0)); // Load the object.
3001 VisitForAccumulatorValue(args->at(1)); // Load the value.
3002 __ pop(a1); // v0 = value. a1 = object.
3003
3004 Label done;
3005 // If the object is a smi, return the value.
3006 __ JumpIfSmi(a1, &done);
3007
3008 // If the object is not a value type, return the value.
3009 __ GetObjectType(a1, a2, a2);
3010 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
3011
3012 // Store the value.
3013 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
3014 // Update the write barrier. Save the value as it will be
3015 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003016 __ mov(a2, v0);
3017 __ RecordWriteField(
3018 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003019
3020 __ bind(&done);
3021 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003022}
3023
3024
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003025void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3026 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003027 ASSERT_EQ(args->length(), 1);
3028
3029 // Load the argument on the stack and call the stub.
3030 VisitForStackValue(args->at(0));
3031
3032 NumberToStringStub stub;
3033 __ CallStub(&stub);
3034 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003035}
3036
3037
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003038void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3039 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003040 ASSERT(args->length() == 1);
3041
3042 VisitForAccumulatorValue(args->at(0));
3043
3044 Label done;
3045 StringCharFromCodeGenerator generator(v0, a1);
3046 generator.GenerateFast(masm_);
3047 __ jmp(&done);
3048
3049 NopRuntimeCallHelper call_helper;
3050 generator.GenerateSlow(masm_, call_helper);
3051
3052 __ bind(&done);
3053 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003054}
3055
3056
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003057void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3058 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003059 ASSERT(args->length() == 2);
3060
3061 VisitForStackValue(args->at(0));
3062 VisitForAccumulatorValue(args->at(1));
3063 __ mov(a0, result_register());
3064
3065 Register object = a1;
3066 Register index = a0;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003067 Register result = v0;
3068
3069 __ pop(object);
3070
3071 Label need_conversion;
3072 Label index_out_of_range;
3073 Label done;
3074 StringCharCodeAtGenerator generator(object,
3075 index,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003076 result,
3077 &need_conversion,
3078 &need_conversion,
3079 &index_out_of_range,
3080 STRING_INDEX_IS_NUMBER);
3081 generator.GenerateFast(masm_);
3082 __ jmp(&done);
3083
3084 __ bind(&index_out_of_range);
3085 // When the index is out of range, the spec requires us to return
3086 // NaN.
3087 __ LoadRoot(result, Heap::kNanValueRootIndex);
3088 __ jmp(&done);
3089
3090 __ bind(&need_conversion);
3091 // Load the undefined value into the result register, which will
3092 // trigger conversion.
3093 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3094 __ jmp(&done);
3095
3096 NopRuntimeCallHelper call_helper;
3097 generator.GenerateSlow(masm_, call_helper);
3098
3099 __ bind(&done);
3100 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003101}
3102
3103
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003104void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3105 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003106 ASSERT(args->length() == 2);
3107
3108 VisitForStackValue(args->at(0));
3109 VisitForAccumulatorValue(args->at(1));
3110 __ mov(a0, result_register());
3111
3112 Register object = a1;
3113 Register index = a0;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003114 Register scratch = a3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003115 Register result = v0;
3116
3117 __ pop(object);
3118
3119 Label need_conversion;
3120 Label index_out_of_range;
3121 Label done;
3122 StringCharAtGenerator generator(object,
3123 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003124 scratch,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003125 result,
3126 &need_conversion,
3127 &need_conversion,
3128 &index_out_of_range,
3129 STRING_INDEX_IS_NUMBER);
3130 generator.GenerateFast(masm_);
3131 __ jmp(&done);
3132
3133 __ bind(&index_out_of_range);
3134 // When the index is out of range, the spec requires us to return
3135 // the empty string.
3136 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3137 __ jmp(&done);
3138
3139 __ bind(&need_conversion);
3140 // Move smi zero into the result register, which will trigger
3141 // conversion.
3142 __ li(result, Operand(Smi::FromInt(0)));
3143 __ jmp(&done);
3144
3145 NopRuntimeCallHelper call_helper;
3146 generator.GenerateSlow(masm_, call_helper);
3147
3148 __ bind(&done);
3149 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003150}
3151
3152
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003153void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3154 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003155 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003156 VisitForStackValue(args->at(0));
3157 VisitForStackValue(args->at(1));
3158
3159 StringAddStub stub(NO_STRING_ADD_FLAGS);
3160 __ CallStub(&stub);
3161 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003162}
3163
3164
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003165void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3166 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003167 ASSERT_EQ(2, args->length());
3168
3169 VisitForStackValue(args->at(0));
3170 VisitForStackValue(args->at(1));
3171
3172 StringCompareStub stub;
3173 __ CallStub(&stub);
3174 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003175}
3176
3177
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003178void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003179 // Load the argument on the stack and call the stub.
3180 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3181 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003182 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003183 ASSERT(args->length() == 1);
3184 VisitForStackValue(args->at(0));
3185 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3186 __ CallStub(&stub);
3187 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003188}
3189
3190
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003191void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003192 // Load the argument on the stack and call the stub.
3193 TranscendentalCacheStub stub(TranscendentalCache::COS,
3194 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003195 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003196 ASSERT(args->length() == 1);
3197 VisitForStackValue(args->at(0));
3198 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3199 __ CallStub(&stub);
3200 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003201}
3202
3203
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003204void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3205 // Load the argument on the stack and call the stub.
3206 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3207 TranscendentalCacheStub::TAGGED);
3208 ZoneList<Expression*>* args = expr->arguments();
3209 ASSERT(args->length() == 1);
3210 VisitForStackValue(args->at(0));
3211 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3212 __ CallStub(&stub);
3213 context()->Plug(v0);
3214}
3215
3216
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003217void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003218 // Load the argument on the stack and call the stub.
3219 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3220 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003221 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003222 ASSERT(args->length() == 1);
3223 VisitForStackValue(args->at(0));
3224 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3225 __ CallStub(&stub);
3226 context()->Plug(v0);
3227}
3228
3229
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003230void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003231 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003232 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003233 ASSERT(args->length() == 1);
3234 VisitForStackValue(args->at(0));
3235 __ CallRuntime(Runtime::kMath_sqrt, 1);
3236 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003237}
3238
3239
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003240void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3241 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003242 ASSERT(args->length() >= 2);
3243
3244 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3245 for (int i = 0; i < arg_count + 1; i++) {
3246 VisitForStackValue(args->at(i));
3247 }
3248 VisitForAccumulatorValue(args->last()); // Function.
3249
danno@chromium.orgc612e022011-11-10 11:38:15 +00003250 // Check for proxy.
3251 Label proxy, done;
3252 __ GetObjectType(v0, a1, a1);
3253 __ Branch(&proxy, eq, a1, Operand(JS_FUNCTION_PROXY_TYPE));
3254
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003255 // InvokeFunction requires the function in a1. Move it in there.
3256 __ mov(a1, result_register());
3257 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003258 __ InvokeFunction(a1, count, CALL_FUNCTION,
3259 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003260 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003261 __ jmp(&done);
3262
3263 __ bind(&proxy);
3264 __ push(v0);
3265 __ CallRuntime(Runtime::kCall, args->length());
3266 __ bind(&done);
3267
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003268 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003269}
3270
3271
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003272void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003273 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003274 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003275 ASSERT(args->length() == 3);
3276 VisitForStackValue(args->at(0));
3277 VisitForStackValue(args->at(1));
3278 VisitForStackValue(args->at(2));
3279 __ CallStub(&stub);
3280 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003281}
3282
3283
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003284void FullCodeGenerator::EmitSwapElements(CallRuntime* expr) {
3285 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003286 ASSERT(args->length() == 3);
3287 VisitForStackValue(args->at(0));
3288 VisitForStackValue(args->at(1));
3289 VisitForStackValue(args->at(2));
3290 Label done;
3291 Label slow_case;
3292 Register object = a0;
3293 Register index1 = a1;
3294 Register index2 = a2;
3295 Register elements = a3;
3296 Register scratch1 = t0;
3297 Register scratch2 = t1;
3298
3299 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3300 // Fetch the map and check if array is in fast case.
3301 // Check that object doesn't require security checks and
3302 // has no indexed interceptor.
3303 __ GetObjectType(object, scratch1, scratch2);
3304 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3305 // Map is now in scratch1.
3306
3307 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3308 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3309 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3310
3311 // Check the object's elements are in fast case and writable.
3312 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3313 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3314 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3315 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3316
3317 // Check that both indices are smis.
3318 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3319 __ lw(index2, MemOperand(sp, 0));
3320 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3321
3322 // Check that both indices are valid.
3323 Label not_hi;
3324 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3325 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3326 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3327 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3328 __ bind(&not_hi);
3329
3330 // Bring the address of the elements into index1 and index2.
3331 __ Addu(scratch1, elements,
3332 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3333 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3334 __ Addu(index1, scratch1, index1);
3335 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3336 __ Addu(index2, scratch1, index2);
3337
3338 // Swap elements.
3339 __ lw(scratch1, MemOperand(index1, 0));
3340 __ lw(scratch2, MemOperand(index2, 0));
3341 __ sw(scratch1, MemOperand(index2, 0));
3342 __ sw(scratch2, MemOperand(index1, 0));
3343
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003344 Label no_remembered_set;
3345 __ CheckPageFlag(elements,
3346 scratch1,
3347 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3348 ne,
3349 &no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003350 // Possible optimization: do a check that both values are Smis
3351 // (or them and test against Smi mask).
3352
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003353 // We are swapping two objects in an array and the incremental marker never
3354 // pauses in the middle of scanning a single object. Therefore the
3355 // incremental marker is not disturbed, so we don't need to call the
3356 // RecordWrite stub that notifies the incremental marker.
3357 __ RememberedSetHelper(elements,
3358 index1,
3359 scratch2,
3360 kDontSaveFPRegs,
3361 MacroAssembler::kFallThroughAtEnd);
3362 __ RememberedSetHelper(elements,
3363 index2,
3364 scratch2,
3365 kDontSaveFPRegs,
3366 MacroAssembler::kFallThroughAtEnd);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003367
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003368 __ bind(&no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003369 // We are done. Drop elements from the stack, and return undefined.
3370 __ Drop(3);
3371 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3372 __ jmp(&done);
3373
3374 __ bind(&slow_case);
3375 __ CallRuntime(Runtime::kSwapElements, 3);
3376
3377 __ bind(&done);
3378 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003379}
3380
3381
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003382void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3383 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003384 ASSERT_EQ(2, args->length());
3385
3386 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3387 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3388
3389 Handle<FixedArray> jsfunction_result_caches(
3390 isolate()->global_context()->jsfunction_result_caches());
3391 if (jsfunction_result_caches->length() <= cache_id) {
3392 __ Abort("Attempt to use undefined cache.");
3393 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3394 context()->Plug(v0);
3395 return;
3396 }
3397
3398 VisitForAccumulatorValue(args->at(1));
3399
3400 Register key = v0;
3401 Register cache = a1;
3402 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3403 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3404 __ lw(cache,
3405 ContextOperand(
3406 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3407 __ lw(cache,
3408 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3409
3410
3411 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003412 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003413 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3414 // a2 now holds finger offset as a smi.
3415 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3416 // a3 now points to the start of fixed array elements.
3417 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3418 __ addu(a3, a3, at);
3419 // a3 now points to key of indexed element of cache.
3420 __ lw(a2, MemOperand(a3));
3421 __ Branch(&not_found, ne, key, Operand(a2));
3422
3423 __ lw(v0, MemOperand(a3, kPointerSize));
3424 __ Branch(&done);
3425
3426 __ bind(&not_found);
3427 // Call runtime to perform the lookup.
3428 __ Push(cache, key);
3429 __ CallRuntime(Runtime::kGetFromCache, 2);
3430
3431 __ bind(&done);
3432 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003433}
3434
3435
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003436void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3437 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003438 ASSERT_EQ(2, args->length());
3439
3440 Register right = v0;
3441 Register left = a1;
3442 Register tmp = a2;
3443 Register tmp2 = a3;
3444
3445 VisitForStackValue(args->at(0));
3446 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3447 __ pop(left);
3448
3449 Label done, fail, ok;
3450 __ Branch(&ok, eq, left, Operand(right));
3451 // Fail if either is a non-HeapObject.
3452 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003453 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003454 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3455 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3456 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3457 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3458 __ Branch(&fail, ne, tmp, Operand(tmp2));
3459 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3460 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3461 __ Branch(&ok, eq, tmp, Operand(tmp2));
3462 __ bind(&fail);
3463 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3464 __ jmp(&done);
3465 __ bind(&ok);
3466 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3467 __ bind(&done);
3468
3469 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003470}
3471
3472
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003473void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3474 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003475 VisitForAccumulatorValue(args->at(0));
3476
3477 Label materialize_true, materialize_false;
3478 Label* if_true = NULL;
3479 Label* if_false = NULL;
3480 Label* fall_through = NULL;
3481 context()->PrepareTest(&materialize_true, &materialize_false,
3482 &if_true, &if_false, &fall_through);
3483
3484 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3485 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3486
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003487 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003488 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3489
3490 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003491}
3492
3493
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003494void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3495 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003496 ASSERT(args->length() == 1);
3497 VisitForAccumulatorValue(args->at(0));
3498
3499 if (FLAG_debug_code) {
3500 __ AbortIfNotString(v0);
3501 }
3502
3503 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3504 __ IndexFromHash(v0, v0);
3505
3506 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003507}
3508
3509
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003510void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003511 Label bailout, done, one_char_separator, long_separator,
3512 non_trivial_array, not_size_one_array, loop,
3513 empty_separator_loop, one_char_separator_loop,
3514 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003515 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003516 ASSERT(args->length() == 2);
3517 VisitForStackValue(args->at(1));
3518 VisitForAccumulatorValue(args->at(0));
3519
3520 // All aliases of the same register have disjoint lifetimes.
3521 Register array = v0;
3522 Register elements = no_reg; // Will be v0.
3523 Register result = no_reg; // Will be v0.
3524 Register separator = a1;
3525 Register array_length = a2;
3526 Register result_pos = no_reg; // Will be a2.
3527 Register string_length = a3;
3528 Register string = t0;
3529 Register element = t1;
3530 Register elements_end = t2;
3531 Register scratch1 = t3;
3532 Register scratch2 = t5;
3533 Register scratch3 = t4;
3534 Register scratch4 = v1;
3535
3536 // Separator operand is on the stack.
3537 __ pop(separator);
3538
3539 // Check that the array is a JSArray.
3540 __ JumpIfSmi(array, &bailout);
3541 __ GetObjectType(array, scratch1, scratch2);
3542 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3543
3544 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003545 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003546
3547 // If the array has length zero, return the empty string.
3548 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3549 __ SmiUntag(array_length);
3550 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3551 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3552 __ Branch(&done);
3553
3554 __ bind(&non_trivial_array);
3555
3556 // Get the FixedArray containing array's elements.
3557 elements = array;
3558 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3559 array = no_reg; // End of array's live range.
3560
3561 // Check that all array elements are sequential ASCII strings, and
3562 // accumulate the sum of their lengths, as a smi-encoded value.
3563 __ mov(string_length, zero_reg);
3564 __ Addu(element,
3565 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3566 __ sll(elements_end, array_length, kPointerSizeLog2);
3567 __ Addu(elements_end, element, elements_end);
3568 // Loop condition: while (element < elements_end).
3569 // Live values in registers:
3570 // elements: Fixed array of strings.
3571 // array_length: Length of the fixed array of strings (not smi)
3572 // separator: Separator string
3573 // string_length: Accumulated sum of string lengths (smi).
3574 // element: Current array element.
3575 // elements_end: Array end.
3576 if (FLAG_debug_code) {
3577 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3578 array_length, Operand(zero_reg));
3579 }
3580 __ bind(&loop);
3581 __ lw(string, MemOperand(element));
3582 __ Addu(element, element, kPointerSize);
3583 __ JumpIfSmi(string, &bailout);
3584 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3585 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3586 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3587 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3588 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3589 __ BranchOnOverflow(&bailout, scratch3);
3590 __ Branch(&loop, lt, element, Operand(elements_end));
3591
3592 // If array_length is 1, return elements[0], a string.
3593 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3594 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3595 __ Branch(&done);
3596
3597 __ bind(&not_size_one_array);
3598
3599 // Live values in registers:
3600 // separator: Separator string
3601 // array_length: Length of the array.
3602 // string_length: Sum of string lengths (smi).
3603 // elements: FixedArray of strings.
3604
3605 // Check that the separator is a flat ASCII string.
3606 __ JumpIfSmi(separator, &bailout);
3607 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3608 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3609 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3610
3611 // Add (separator length times array_length) - separator length to the
3612 // string_length to get the length of the result string. array_length is not
3613 // smi but the other values are, so the result is a smi.
3614 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3615 __ Subu(string_length, string_length, Operand(scratch1));
3616 __ Mult(array_length, scratch1);
3617 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3618 // zero.
3619 __ mfhi(scratch2);
3620 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3621 __ mflo(scratch2);
3622 __ And(scratch3, scratch2, Operand(0x80000000));
3623 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3624 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3625 __ BranchOnOverflow(&bailout, scratch3);
3626 __ SmiUntag(string_length);
3627
3628 // Get first element in the array to free up the elements register to be used
3629 // for the result.
3630 __ Addu(element,
3631 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3632 result = elements; // End of live range for elements.
3633 elements = no_reg;
3634 // Live values in registers:
3635 // element: First array element
3636 // separator: Separator string
3637 // string_length: Length of result string (not smi)
3638 // array_length: Length of the array.
3639 __ AllocateAsciiString(result,
3640 string_length,
3641 scratch1,
3642 scratch2,
3643 elements_end,
3644 &bailout);
3645 // Prepare for looping. Set up elements_end to end of the array. Set
3646 // result_pos to the position of the result where to write the first
3647 // character.
3648 __ sll(elements_end, array_length, kPointerSizeLog2);
3649 __ Addu(elements_end, element, elements_end);
3650 result_pos = array_length; // End of live range for array_length.
3651 array_length = no_reg;
3652 __ Addu(result_pos,
3653 result,
3654 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3655
3656 // Check the length of the separator.
3657 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3658 __ li(at, Operand(Smi::FromInt(1)));
3659 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3660 __ Branch(&long_separator, gt, scratch1, Operand(at));
3661
3662 // Empty separator case.
3663 __ bind(&empty_separator_loop);
3664 // Live values in registers:
3665 // result_pos: the position to which we are currently copying characters.
3666 // element: Current array element.
3667 // elements_end: Array end.
3668
3669 // Copy next array element to the result.
3670 __ lw(string, MemOperand(element));
3671 __ Addu(element, element, kPointerSize);
3672 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3673 __ SmiUntag(string_length);
3674 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3675 __ CopyBytes(string, result_pos, string_length, scratch1);
3676 // End while (element < elements_end).
3677 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3678 ASSERT(result.is(v0));
3679 __ Branch(&done);
3680
3681 // One-character separator case.
3682 __ bind(&one_char_separator);
3683 // Replace separator with its ascii character value.
3684 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3685 // Jump into the loop after the code that copies the separator, so the first
3686 // element is not preceded by a separator.
3687 __ jmp(&one_char_separator_loop_entry);
3688
3689 __ bind(&one_char_separator_loop);
3690 // Live values in registers:
3691 // result_pos: the position to which we are currently copying characters.
3692 // element: Current array element.
3693 // elements_end: Array end.
3694 // separator: Single separator ascii char (in lower byte).
3695
3696 // Copy the separator character to the result.
3697 __ sb(separator, MemOperand(result_pos));
3698 __ Addu(result_pos, result_pos, 1);
3699
3700 // Copy next array element to the result.
3701 __ bind(&one_char_separator_loop_entry);
3702 __ lw(string, MemOperand(element));
3703 __ Addu(element, element, kPointerSize);
3704 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3705 __ SmiUntag(string_length);
3706 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3707 __ CopyBytes(string, result_pos, string_length, scratch1);
3708 // End while (element < elements_end).
3709 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3710 ASSERT(result.is(v0));
3711 __ Branch(&done);
3712
3713 // Long separator case (separator is more than one character). Entry is at the
3714 // label long_separator below.
3715 __ bind(&long_separator_loop);
3716 // Live values in registers:
3717 // result_pos: the position to which we are currently copying characters.
3718 // element: Current array element.
3719 // elements_end: Array end.
3720 // separator: Separator string.
3721
3722 // Copy the separator to the result.
3723 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3724 __ SmiUntag(string_length);
3725 __ Addu(string,
3726 separator,
3727 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3728 __ CopyBytes(string, result_pos, string_length, scratch1);
3729
3730 __ bind(&long_separator);
3731 __ lw(string, MemOperand(element));
3732 __ Addu(element, element, kPointerSize);
3733 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3734 __ SmiUntag(string_length);
3735 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3736 __ CopyBytes(string, result_pos, string_length, scratch1);
3737 // End while (element < elements_end).
3738 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3739 ASSERT(result.is(v0));
3740 __ Branch(&done);
3741
3742 __ bind(&bailout);
3743 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3744 __ bind(&done);
3745 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003746}
3747
3748
ager@chromium.org5c838252010-02-19 08:53:10 +00003749void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003750 Handle<String> name = expr->name();
3751 if (name->length() > 0 && name->Get(0) == '_') {
3752 Comment cmnt(masm_, "[ InlineRuntimeCall");
3753 EmitInlineRuntimeCall(expr);
3754 return;
3755 }
3756
3757 Comment cmnt(masm_, "[ CallRuntime");
3758 ZoneList<Expression*>* args = expr->arguments();
3759
3760 if (expr->is_jsruntime()) {
3761 // Prepare for calling JS runtime function.
3762 __ lw(a0, GlobalObjectOperand());
3763 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3764 __ push(a0);
3765 }
3766
3767 // Push the arguments ("left-to-right").
3768 int arg_count = args->length();
3769 for (int i = 0; i < arg_count; i++) {
3770 VisitForStackValue(args->at(i));
3771 }
3772
3773 if (expr->is_jsruntime()) {
3774 // Call the JS runtime function.
3775 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003776 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003777 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003778 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003779 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003780 // Restore context register.
3781 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3782 } else {
3783 // Call the C runtime function.
3784 __ CallRuntime(expr->function(), arg_count);
3785 }
3786 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003787}
3788
3789
3790void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003791 switch (expr->op()) {
3792 case Token::DELETE: {
3793 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003794 Property* property = expr->expression()->AsProperty();
3795 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003796
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003797 if (property != NULL) {
3798 VisitForStackValue(property->obj());
3799 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003800 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
3801 ? kNonStrictMode : kStrictMode;
3802 __ li(a1, Operand(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003803 __ push(a1);
3804 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3805 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003806 } else if (proxy != NULL) {
3807 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003808 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003809 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003810 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003811 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003812 __ lw(a2, GlobalObjectOperand());
3813 __ li(a1, Operand(var->name()));
3814 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3815 __ Push(a2, a1, a0);
3816 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3817 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003818 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003819 // Result of deleting non-global, non-dynamic variables is false.
3820 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003821 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003822 } else {
3823 // Non-global variable. Call the runtime to try to delete from the
3824 // context where the variable was introduced.
3825 __ push(context_register());
3826 __ li(a2, Operand(var->name()));
3827 __ push(a2);
3828 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3829 context()->Plug(v0);
3830 }
3831 } else {
3832 // Result of deleting non-property, non-variable reference is true.
3833 // The subexpression may have side effects.
3834 VisitForEffect(expr->expression());
3835 context()->Plug(true);
3836 }
3837 break;
3838 }
3839
3840 case Token::VOID: {
3841 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3842 VisitForEffect(expr->expression());
3843 context()->Plug(Heap::kUndefinedValueRootIndex);
3844 break;
3845 }
3846
3847 case Token::NOT: {
3848 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3849 if (context()->IsEffect()) {
3850 // Unary NOT has no side effects so it's only necessary to visit the
3851 // subexpression. Match the optimizing compiler by not branching.
3852 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003853 } else if (context()->IsTest()) {
3854 const TestContext* test = TestContext::cast(context());
3855 // The labels are swapped for the recursive call.
3856 VisitForControl(expr->expression(),
3857 test->false_label(),
3858 test->true_label(),
3859 test->fall_through());
3860 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003861 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003862 // We handle value contexts explicitly rather than simply visiting
3863 // for control and plugging the control flow into the context,
3864 // because we need to prepare a pair of extra administrative AST ids
3865 // for the optimizing compiler.
3866 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3867 Label materialize_true, materialize_false, done;
3868 VisitForControl(expr->expression(),
3869 &materialize_false,
3870 &materialize_true,
3871 &materialize_true);
3872 __ bind(&materialize_true);
3873 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3874 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3875 if (context()->IsStackValue()) __ push(v0);
3876 __ jmp(&done);
3877 __ bind(&materialize_false);
3878 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3879 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3880 if (context()->IsStackValue()) __ push(v0);
3881 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003882 }
3883 break;
3884 }
3885
3886 case Token::TYPEOF: {
3887 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3888 { StackValueContext context(this);
3889 VisitForTypeofValue(expr->expression());
3890 }
3891 __ CallRuntime(Runtime::kTypeof, 1);
3892 context()->Plug(v0);
3893 break;
3894 }
3895
3896 case Token::ADD: {
3897 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3898 VisitForAccumulatorValue(expr->expression());
3899 Label no_conversion;
3900 __ JumpIfSmi(result_register(), &no_conversion);
3901 __ mov(a0, result_register());
3902 ToNumberStub convert_stub;
3903 __ CallStub(&convert_stub);
3904 __ bind(&no_conversion);
3905 context()->Plug(result_register());
3906 break;
3907 }
3908
3909 case Token::SUB:
3910 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3911 break;
3912
3913 case Token::BIT_NOT:
3914 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3915 break;
3916
3917 default:
3918 UNREACHABLE();
3919 }
3920}
3921
3922
3923void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3924 const char* comment) {
3925 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3926 Comment cmt(masm_, comment);
3927 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3928 UnaryOverwriteMode overwrite =
3929 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003930 UnaryOpStub stub(expr->op(), overwrite);
3931 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003932 VisitForAccumulatorValue(expr->expression());
3933 SetSourcePosition(expr->position());
3934 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003935 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003936 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003937}
3938
3939
3940void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003941 Comment cmnt(masm_, "[ CountOperation");
3942 SetSourcePosition(expr->position());
3943
3944 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3945 // as the left-hand side.
3946 if (!expr->expression()->IsValidLeftHandSide()) {
3947 VisitForEffect(expr->expression());
3948 return;
3949 }
3950
3951 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003952 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003953 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3954 LhsKind assign_type = VARIABLE;
3955 Property* prop = expr->expression()->AsProperty();
3956 // In case of a property we use the uninitialized expression context
3957 // of the key to detect a named property.
3958 if (prop != NULL) {
3959 assign_type =
3960 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3961 }
3962
3963 // Evaluate expression and get value.
3964 if (assign_type == VARIABLE) {
3965 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3966 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003967 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003968 } else {
3969 // Reserve space for result of postfix operation.
3970 if (expr->is_postfix() && !context()->IsEffect()) {
3971 __ li(at, Operand(Smi::FromInt(0)));
3972 __ push(at);
3973 }
3974 if (assign_type == NAMED_PROPERTY) {
3975 // Put the object both on the stack and in the accumulator.
3976 VisitForAccumulatorValue(prop->obj());
3977 __ push(v0);
3978 EmitNamedPropertyLoad(prop);
3979 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003980 VisitForStackValue(prop->obj());
3981 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003982 __ lw(a1, MemOperand(sp, 0));
3983 __ push(v0);
3984 EmitKeyedPropertyLoad(prop);
3985 }
3986 }
3987
3988 // We need a second deoptimization point after loading the value
3989 // in case evaluating the property load my have a side effect.
3990 if (assign_type == VARIABLE) {
3991 PrepareForBailout(expr->expression(), TOS_REG);
3992 } else {
3993 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3994 }
3995
3996 // Call ToNumber only if operand is not a smi.
3997 Label no_conversion;
3998 __ JumpIfSmi(v0, &no_conversion);
3999 __ mov(a0, v0);
4000 ToNumberStub convert_stub;
4001 __ CallStub(&convert_stub);
4002 __ bind(&no_conversion);
4003
4004 // Save result for postfix expressions.
4005 if (expr->is_postfix()) {
4006 if (!context()->IsEffect()) {
4007 // Save the result on the stack. If we have a named or keyed property
4008 // we store the result under the receiver that is currently on top
4009 // of the stack.
4010 switch (assign_type) {
4011 case VARIABLE:
4012 __ push(v0);
4013 break;
4014 case NAMED_PROPERTY:
4015 __ sw(v0, MemOperand(sp, kPointerSize));
4016 break;
4017 case KEYED_PROPERTY:
4018 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4019 break;
4020 }
4021 }
4022 }
4023 __ mov(a0, result_register());
4024
4025 // Inline smi case if we are in a loop.
4026 Label stub_call, done;
4027 JumpPatchSite patch_site(masm_);
4028
4029 int count_value = expr->op() == Token::INC ? 1 : -1;
4030 __ li(a1, Operand(Smi::FromInt(count_value)));
4031
4032 if (ShouldInlineSmiCase(expr->op())) {
4033 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
4034 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
4035
4036 // We could eliminate this smi check if we split the code at
4037 // the first smi check before calling ToNumber.
4038 patch_site.EmitJumpIfSmi(v0, &done);
4039 __ bind(&stub_call);
4040 }
4041
4042 // Record position before stub call.
4043 SetSourcePosition(expr->position());
4044
danno@chromium.org40cb8782011-05-25 07:58:50 +00004045 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004046 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004047 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004048 __ bind(&done);
4049
4050 // Store the value returned in v0.
4051 switch (assign_type) {
4052 case VARIABLE:
4053 if (expr->is_postfix()) {
4054 { EffectContext context(this);
4055 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4056 Token::ASSIGN);
4057 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4058 context.Plug(v0);
4059 }
4060 // For all contexts except EffectConstant we have the result on
4061 // top of the stack.
4062 if (!context()->IsEffect()) {
4063 context()->PlugTOS();
4064 }
4065 } else {
4066 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4067 Token::ASSIGN);
4068 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4069 context()->Plug(v0);
4070 }
4071 break;
4072 case NAMED_PROPERTY: {
4073 __ mov(a0, result_register()); // Value.
4074 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4075 __ pop(a1); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004076 Handle<Code> ic = is_classic_mode()
4077 ? isolate()->builtins()->StoreIC_Initialize()
4078 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004079 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004080 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4081 if (expr->is_postfix()) {
4082 if (!context()->IsEffect()) {
4083 context()->PlugTOS();
4084 }
4085 } else {
4086 context()->Plug(v0);
4087 }
4088 break;
4089 }
4090 case KEYED_PROPERTY: {
4091 __ mov(a0, result_register()); // Value.
4092 __ pop(a1); // Key.
4093 __ pop(a2); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004094 Handle<Code> ic = is_classic_mode()
4095 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4096 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004097 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004098 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4099 if (expr->is_postfix()) {
4100 if (!context()->IsEffect()) {
4101 context()->PlugTOS();
4102 }
4103 } else {
4104 context()->Plug(v0);
4105 }
4106 break;
4107 }
4108 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004109}
4110
4111
lrn@chromium.org7516f052011-03-30 08:52:27 +00004112void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004113 ASSERT(!context()->IsEffect());
4114 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004115 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004116 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004117 Comment cmnt(masm_, "Global variable");
4118 __ lw(a0, GlobalObjectOperand());
4119 __ li(a2, Operand(proxy->name()));
4120 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4121 // Use a regular load, not a contextual load, to avoid a reference
4122 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004123 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004124 PrepareForBailout(expr, TOS_REG);
4125 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004126 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004127 Label done, slow;
4128
4129 // Generate code for loading from variables potentially shadowed
4130 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004131 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004132
4133 __ bind(&slow);
4134 __ li(a0, Operand(proxy->name()));
4135 __ Push(cp, a0);
4136 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4137 PrepareForBailout(expr, TOS_REG);
4138 __ bind(&done);
4139
4140 context()->Plug(v0);
4141 } else {
4142 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004143 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004144 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004145}
4146
ager@chromium.org04921a82011-06-27 13:21:41 +00004147void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004148 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004149 Handle<String> check) {
4150 Label materialize_true, materialize_false;
4151 Label* if_true = NULL;
4152 Label* if_false = NULL;
4153 Label* fall_through = NULL;
4154 context()->PrepareTest(&materialize_true, &materialize_false,
4155 &if_true, &if_false, &fall_through);
4156
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004157 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004158 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004159 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004160 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004161
4162 if (check->Equals(isolate()->heap()->number_symbol())) {
4163 __ JumpIfSmi(v0, if_true);
4164 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4165 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4166 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4167 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4168 __ JumpIfSmi(v0, if_false);
4169 // Check for undetectable objects => false.
4170 __ GetObjectType(v0, v0, a1);
4171 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4172 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4173 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4174 Split(eq, a1, Operand(zero_reg),
4175 if_true, if_false, fall_through);
4176 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4177 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4178 __ Branch(if_true, eq, v0, Operand(at));
4179 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4180 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004181 } else if (FLAG_harmony_typeof &&
4182 check->Equals(isolate()->heap()->null_symbol())) {
4183 __ LoadRoot(at, Heap::kNullValueRootIndex);
4184 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004185 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4186 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4187 __ Branch(if_true, eq, v0, Operand(at));
4188 __ JumpIfSmi(v0, if_false);
4189 // Check for undetectable objects => true.
4190 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4191 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4192 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4193 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4194 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4195 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004196 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4197 __ GetObjectType(v0, v0, a1);
4198 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4199 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4200 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004201 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4202 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004203 if (!FLAG_harmony_typeof) {
4204 __ LoadRoot(at, Heap::kNullValueRootIndex);
4205 __ Branch(if_true, eq, v0, Operand(at));
4206 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004207 // Check for JS objects => true.
4208 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004209 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004210 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004211 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004212 // Check for undetectable objects => false.
4213 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4214 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4215 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4216 } else {
4217 if (if_false != fall_through) __ jmp(if_false);
4218 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004219 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004220}
4221
4222
ager@chromium.org5c838252010-02-19 08:53:10 +00004223void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004224 Comment cmnt(masm_, "[ CompareOperation");
4225 SetSourcePosition(expr->position());
4226
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004227 // First we try a fast inlined version of the compare when one of
4228 // the operands is a literal.
4229 if (TryLiteralCompare(expr)) return;
4230
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004231 // Always perform the comparison for its control flow. Pack the result
4232 // into the expression's context after the comparison is performed.
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
ager@chromium.org04921a82011-06-27 13:21:41 +00004240 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004241 VisitForStackValue(expr->left());
4242 switch (op) {
4243 case Token::IN:
4244 VisitForStackValue(expr->right());
4245 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004246 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004247 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4248 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4249 break;
4250
4251 case Token::INSTANCEOF: {
4252 VisitForStackValue(expr->right());
4253 InstanceofStub stub(InstanceofStub::kNoFlags);
4254 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004255 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004256 // The stub returns 0 for true.
4257 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4258 break;
4259 }
4260
4261 default: {
4262 VisitForAccumulatorValue(expr->right());
4263 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004264 switch (op) {
4265 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004266 case Token::EQ:
4267 cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004268 break;
4269 case Token::LT:
4270 cc = lt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004271 break;
4272 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004273 cc = gt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004274 break;
4275 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004276 cc = le;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004277 break;
4278 case Token::GTE:
4279 cc = ge;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004280 break;
4281 case Token::IN:
4282 case Token::INSTANCEOF:
4283 default:
4284 UNREACHABLE();
4285 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004286 __ mov(a0, result_register());
4287 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004288
4289 bool inline_smi_code = ShouldInlineSmiCase(op);
4290 JumpPatchSite patch_site(masm_);
4291 if (inline_smi_code) {
4292 Label slow_case;
4293 __ Or(a2, a0, Operand(a1));
4294 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4295 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4296 __ bind(&slow_case);
4297 }
4298 // Record position and call the compare IC.
4299 SetSourcePosition(expr->position());
4300 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004301 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004302 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004303 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004304 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4305 }
4306 }
4307
4308 // Convert the result of the comparison into one expected for this
4309 // expression's context.
4310 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004311}
4312
4313
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004314void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4315 Expression* sub_expr,
4316 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004317 Label materialize_true, materialize_false;
4318 Label* if_true = NULL;
4319 Label* if_false = NULL;
4320 Label* fall_through = NULL;
4321 context()->PrepareTest(&materialize_true, &materialize_false,
4322 &if_true, &if_false, &fall_through);
4323
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004324 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004325 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004326 Heap::RootListIndex nil_value = nil == kNullValue ?
4327 Heap::kNullValueRootIndex :
4328 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004329 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004330 __ LoadRoot(a1, nil_value);
4331 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004332 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4333 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004334 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4335 Heap::kUndefinedValueRootIndex :
4336 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004337 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004338 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004339 __ Branch(if_true, eq, a0, Operand(a1));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004340 __ JumpIfSmi(a0, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004341 // It can be an undetectable object.
4342 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4343 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4344 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4345 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4346 }
4347 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004348}
4349
4350
ager@chromium.org5c838252010-02-19 08:53:10 +00004351void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004352 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4353 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004354}
4355
4356
lrn@chromium.org7516f052011-03-30 08:52:27 +00004357Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004358 return v0;
4359}
ager@chromium.org5c838252010-02-19 08:53:10 +00004360
4361
lrn@chromium.org7516f052011-03-30 08:52:27 +00004362Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004363 return cp;
4364}
4365
4366
ager@chromium.org5c838252010-02-19 08:53:10 +00004367void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004368 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4369 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004370}
4371
4372
4373void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004374 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004375}
4376
4377
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004378void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4379 Scope* declaration_scope = scope()->DeclarationScope();
4380 if (declaration_scope->is_global_scope()) {
4381 // Contexts nested in the global context have a canonical empty function
4382 // as their closure, not the anonymous closure containing the global
4383 // code. Pass a smi sentinel and let the runtime look up the empty
4384 // function.
4385 __ li(at, Operand(Smi::FromInt(0)));
4386 } else if (declaration_scope->is_eval_scope()) {
4387 // Contexts created by a call to eval have the same closure as the
4388 // context calling eval, not the anonymous closure containing the eval
4389 // code. Fetch it from the context.
4390 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4391 } else {
4392 ASSERT(declaration_scope->is_function_scope());
4393 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4394 }
4395 __ push(at);
4396}
4397
4398
ager@chromium.org5c838252010-02-19 08:53:10 +00004399// ----------------------------------------------------------------------------
4400// Non-local control flow support.
4401
4402void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004403 ASSERT(!result_register().is(a1));
4404 // Store result register while executing finally block.
4405 __ push(result_register());
4406 // Cook return address in link register to stack (smi encoded Code* delta).
4407 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4408 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004409 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004410 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4411 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004412}
4413
4414
4415void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004416 ASSERT(!result_register().is(a1));
4417 // Restore result register from stack.
4418 __ pop(a1);
4419 // Uncook return address and return.
4420 __ pop(result_register());
4421 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4422 __ sra(a1, a1, 1); // Un-smi-tag value.
4423 __ Addu(at, a1, Operand(masm_->CodeObject()));
4424 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004425}
4426
4427
4428#undef __
4429
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004430#define __ ACCESS_MASM(masm())
4431
4432FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4433 int* stack_depth,
4434 int* context_length) {
4435 // The macros used here must preserve the result register.
4436
4437 // Because the handler block contains the context of the finally
4438 // code, we can restore it directly from there for the finally code
4439 // rather than iteratively unwinding contexts via their previous
4440 // links.
4441 __ Drop(*stack_depth); // Down to the handler block.
4442 if (*context_length > 0) {
4443 // Restore the context to its dedicated register and the stack.
4444 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4445 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4446 }
4447 __ PopTryHandler();
4448 __ Call(finally_entry_);
4449
4450 *stack_depth = 0;
4451 *context_length = 0;
4452 return previous_;
4453}
4454
4455
4456#undef __
4457
ager@chromium.org5c838252010-02-19 08:53:10 +00004458} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004459
4460#endif // V8_TARGET_ARCH_MIPS