blob: da3be4c23e62c0165bfe6b79f97f19d6486361ac [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:
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000128// o a1: the JS function object being called (i.e. ourselves)
lrn@chromium.org7516f052011-03-30 08:52:27 +0000129// 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
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001020 // Set up the four remaining stack slots.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001021 __ 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");
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001427 Handle<FixedArray> constant_properties = expr->constant_properties();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001428 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1429 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1430 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001431 __ li(a1, Operand(constant_properties));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001432 int flags = expr->fast_elements()
1433 ? ObjectLiteral::kFastElements
1434 : ObjectLiteral::kNoFlags;
1435 flags |= expr->has_function()
1436 ? ObjectLiteral::kHasFunction
1437 : ObjectLiteral::kNoFlags;
1438 __ li(a0, Operand(Smi::FromInt(flags)));
1439 __ Push(a3, a2, a1, a0);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001440 int properties_count = constant_properties->length() / 2;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001441 if (expr->depth() > 1) {
1442 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001443 } else if (flags != ObjectLiteral::kFastElements ||
1444 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001445 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001446 } else {
1447 FastCloneShallowObjectStub stub(properties_count);
1448 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001449 }
1450
1451 // If result_saved is true the result is on top of the stack. If
1452 // result_saved is false the result is in v0.
1453 bool result_saved = false;
1454
1455 // Mark all computed expressions that are bound to a key that
1456 // is shadowed by a later occurrence of the same key. For the
1457 // marked expressions, no store code is emitted.
1458 expr->CalculateEmitStore();
1459
1460 for (int i = 0; i < expr->properties()->length(); i++) {
1461 ObjectLiteral::Property* property = expr->properties()->at(i);
1462 if (property->IsCompileTimeValue()) continue;
1463
1464 Literal* key = property->key();
1465 Expression* value = property->value();
1466 if (!result_saved) {
1467 __ push(v0); // Save result on stack.
1468 result_saved = true;
1469 }
1470 switch (property->kind()) {
1471 case ObjectLiteral::Property::CONSTANT:
1472 UNREACHABLE();
1473 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1474 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1475 // Fall through.
1476 case ObjectLiteral::Property::COMPUTED:
1477 if (key->handle()->IsSymbol()) {
1478 if (property->emit_store()) {
1479 VisitForAccumulatorValue(value);
1480 __ mov(a0, result_register());
1481 __ li(a2, Operand(key->handle()));
1482 __ lw(a1, MemOperand(sp));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001483 Handle<Code> ic = is_classic_mode()
1484 ? isolate()->builtins()->StoreIC_Initialize()
1485 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001486 __ Call(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001487 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1488 } else {
1489 VisitForEffect(value);
1490 }
1491 break;
1492 }
1493 // Fall through.
1494 case ObjectLiteral::Property::PROTOTYPE:
1495 // Duplicate receiver on stack.
1496 __ lw(a0, MemOperand(sp));
1497 __ push(a0);
1498 VisitForStackValue(key);
1499 VisitForStackValue(value);
1500 if (property->emit_store()) {
1501 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1502 __ push(a0);
1503 __ CallRuntime(Runtime::kSetProperty, 4);
1504 } else {
1505 __ Drop(3);
1506 }
1507 break;
1508 case ObjectLiteral::Property::GETTER:
1509 case ObjectLiteral::Property::SETTER:
1510 // Duplicate receiver on stack.
1511 __ lw(a0, MemOperand(sp));
1512 __ push(a0);
1513 VisitForStackValue(key);
1514 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1515 Smi::FromInt(1) :
1516 Smi::FromInt(0)));
1517 __ push(a1);
1518 VisitForStackValue(value);
1519 __ CallRuntime(Runtime::kDefineAccessor, 4);
1520 break;
1521 }
1522 }
1523
1524 if (expr->has_function()) {
1525 ASSERT(result_saved);
1526 __ lw(a0, MemOperand(sp));
1527 __ push(a0);
1528 __ CallRuntime(Runtime::kToFastProperties, 1);
1529 }
1530
1531 if (result_saved) {
1532 context()->PlugTOS();
1533 } else {
1534 context()->Plug(v0);
1535 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001536}
1537
1538
1539void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001540 Comment cmnt(masm_, "[ ArrayLiteral");
1541
1542 ZoneList<Expression*>* subexprs = expr->values();
1543 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001544
1545 Handle<FixedArray> constant_elements = expr->constant_elements();
1546 ASSERT_EQ(2, constant_elements->length());
1547 ElementsKind constant_elements_kind =
1548 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001549 bool has_fast_elements = constant_elements_kind == FAST_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001550 Handle<FixedArrayBase> constant_elements_values(
1551 FixedArrayBase::cast(constant_elements->get(1)));
1552
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001553 __ mov(a0, result_register());
1554 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1555 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1556 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001557 __ li(a1, Operand(constant_elements));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001558 __ Push(a3, a2, a1);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001559 if (has_fast_elements && constant_elements_values->map() ==
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001560 isolate()->heap()->fixed_cow_array_map()) {
1561 FastCloneShallowArrayStub stub(
1562 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1563 __ CallStub(&stub);
1564 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1565 1, a1, a2);
1566 } else if (expr->depth() > 1) {
1567 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1568 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1569 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1570 } else {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001571 ASSERT(constant_elements_kind == FAST_ELEMENTS ||
1572 constant_elements_kind == FAST_SMI_ONLY_ELEMENTS ||
1573 FLAG_smi_only_arrays);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001574 FastCloneShallowArrayStub::Mode mode = has_fast_elements
1575 ? FastCloneShallowArrayStub::CLONE_ELEMENTS
1576 : FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001577 FastCloneShallowArrayStub stub(mode, length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001578 __ CallStub(&stub);
1579 }
1580
1581 bool result_saved = false; // Is the result saved to the stack?
1582
1583 // Emit code to evaluate all the non-constant subexpressions and to store
1584 // them into the newly cloned array.
1585 for (int i = 0; i < length; i++) {
1586 Expression* subexpr = subexprs->at(i);
1587 // If the subexpression is a literal or a simple materialized literal it
1588 // is already set in the cloned array.
1589 if (subexpr->AsLiteral() != NULL ||
1590 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1591 continue;
1592 }
1593
1594 if (!result_saved) {
1595 __ push(v0);
1596 result_saved = true;
1597 }
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001598
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001599 VisitForAccumulatorValue(subexpr);
1600
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001601 if (constant_elements_kind == FAST_ELEMENTS) {
1602 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1603 __ lw(t2, MemOperand(sp)); // Copy of array literal.
1604 __ lw(a1, FieldMemOperand(t2, JSObject::kElementsOffset));
1605 __ sw(result_register(), FieldMemOperand(a1, offset));
1606 // Update the write barrier for the array store.
1607 __ RecordWriteField(a1, offset, result_register(), a2,
1608 kRAHasBeenSaved, kDontSaveFPRegs,
1609 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1610 } else {
1611 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1612 __ lw(a2, FieldMemOperand(a1, JSObject::kMapOffset));
1613 __ li(a3, Operand(Smi::FromInt(i)));
1614 __ li(t0, Operand(Smi::FromInt(expr->literal_index())));
1615 __ mov(a0, result_register());
1616 StoreArrayLiteralElementStub stub;
1617 __ CallStub(&stub);
1618 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001619
1620 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1621 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001622 if (result_saved) {
1623 context()->PlugTOS();
1624 } else {
1625 context()->Plug(v0);
1626 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001627}
1628
1629
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001630void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001631 Comment cmnt(masm_, "[ Assignment");
1632 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1633 // on the left-hand side.
1634 if (!expr->target()->IsValidLeftHandSide()) {
1635 VisitForEffect(expr->target());
1636 return;
1637 }
1638
1639 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001640 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001641 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1642 LhsKind assign_type = VARIABLE;
1643 Property* property = expr->target()->AsProperty();
1644 if (property != NULL) {
1645 assign_type = (property->key()->IsPropertyName())
1646 ? NAMED_PROPERTY
1647 : KEYED_PROPERTY;
1648 }
1649
1650 // Evaluate LHS expression.
1651 switch (assign_type) {
1652 case VARIABLE:
1653 // Nothing to do here.
1654 break;
1655 case NAMED_PROPERTY:
1656 if (expr->is_compound()) {
1657 // We need the receiver both on the stack and in the accumulator.
1658 VisitForAccumulatorValue(property->obj());
1659 __ push(result_register());
1660 } else {
1661 VisitForStackValue(property->obj());
1662 }
1663 break;
1664 case KEYED_PROPERTY:
1665 // We need the key and receiver on both the stack and in v0 and a1.
1666 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001667 VisitForStackValue(property->obj());
1668 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001669 __ lw(a1, MemOperand(sp, 0));
1670 __ push(v0);
1671 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001672 VisitForStackValue(property->obj());
1673 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001674 }
1675 break;
1676 }
1677
1678 // For compound assignments we need another deoptimization point after the
1679 // variable/property load.
1680 if (expr->is_compound()) {
1681 { AccumulatorValueContext context(this);
1682 switch (assign_type) {
1683 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001684 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001685 PrepareForBailout(expr->target(), TOS_REG);
1686 break;
1687 case NAMED_PROPERTY:
1688 EmitNamedPropertyLoad(property);
1689 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1690 break;
1691 case KEYED_PROPERTY:
1692 EmitKeyedPropertyLoad(property);
1693 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1694 break;
1695 }
1696 }
1697
1698 Token::Value op = expr->binary_op();
1699 __ push(v0); // Left operand goes on the stack.
1700 VisitForAccumulatorValue(expr->value());
1701
1702 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1703 ? OVERWRITE_RIGHT
1704 : NO_OVERWRITE;
1705 SetSourcePosition(expr->position() + 1);
1706 AccumulatorValueContext context(this);
1707 if (ShouldInlineSmiCase(op)) {
1708 EmitInlineSmiBinaryOp(expr->binary_operation(),
1709 op,
1710 mode,
1711 expr->target(),
1712 expr->value());
1713 } else {
1714 EmitBinaryOp(expr->binary_operation(), op, mode);
1715 }
1716
1717 // Deoptimization point in case the binary operation may have side effects.
1718 PrepareForBailout(expr->binary_operation(), TOS_REG);
1719 } else {
1720 VisitForAccumulatorValue(expr->value());
1721 }
1722
1723 // Record source position before possible IC call.
1724 SetSourcePosition(expr->position());
1725
1726 // Store the value.
1727 switch (assign_type) {
1728 case VARIABLE:
1729 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1730 expr->op());
1731 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1732 context()->Plug(v0);
1733 break;
1734 case NAMED_PROPERTY:
1735 EmitNamedPropertyAssignment(expr);
1736 break;
1737 case KEYED_PROPERTY:
1738 EmitKeyedPropertyAssignment(expr);
1739 break;
1740 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001741}
1742
1743
ager@chromium.org5c838252010-02-19 08:53:10 +00001744void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001745 SetSourcePosition(prop->position());
1746 Literal* key = prop->key()->AsLiteral();
1747 __ mov(a0, result_register());
1748 __ li(a2, Operand(key->handle()));
1749 // Call load IC. It has arguments receiver and property name a0 and a2.
1750 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001751 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001752}
1753
1754
1755void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001756 SetSourcePosition(prop->position());
1757 __ mov(a0, result_register());
1758 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1759 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001760 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001761}
1762
1763
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001764void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001765 Token::Value op,
1766 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001767 Expression* left_expr,
1768 Expression* right_expr) {
1769 Label done, smi_case, stub_call;
1770
1771 Register scratch1 = a2;
1772 Register scratch2 = a3;
1773
1774 // Get the arguments.
1775 Register left = a1;
1776 Register right = a0;
1777 __ pop(left);
1778 __ mov(a0, result_register());
1779
1780 // Perform combined smi check on both operands.
1781 __ Or(scratch1, left, Operand(right));
1782 STATIC_ASSERT(kSmiTag == 0);
1783 JumpPatchSite patch_site(masm_);
1784 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1785
1786 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001787 BinaryOpStub stub(op, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001788 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001789 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001790 __ jmp(&done);
1791
1792 __ bind(&smi_case);
1793 // Smi case. This code works the same way as the smi-smi case in the type
1794 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001795 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001796 switch (op) {
1797 case Token::SAR:
1798 __ Branch(&stub_call);
1799 __ GetLeastBitsFromSmi(scratch1, right, 5);
1800 __ srav(right, left, scratch1);
1801 __ And(v0, right, Operand(~kSmiTagMask));
1802 break;
1803 case Token::SHL: {
1804 __ Branch(&stub_call);
1805 __ SmiUntag(scratch1, left);
1806 __ GetLeastBitsFromSmi(scratch2, right, 5);
1807 __ sllv(scratch1, scratch1, scratch2);
1808 __ Addu(scratch2, scratch1, Operand(0x40000000));
1809 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1810 __ SmiTag(v0, scratch1);
1811 break;
1812 }
1813 case Token::SHR: {
1814 __ Branch(&stub_call);
1815 __ SmiUntag(scratch1, left);
1816 __ GetLeastBitsFromSmi(scratch2, right, 5);
1817 __ srlv(scratch1, scratch1, scratch2);
1818 __ And(scratch2, scratch1, 0xc0000000);
1819 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1820 __ SmiTag(v0, scratch1);
1821 break;
1822 }
1823 case Token::ADD:
1824 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1825 __ BranchOnOverflow(&stub_call, scratch1);
1826 break;
1827 case Token::SUB:
1828 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1829 __ BranchOnOverflow(&stub_call, scratch1);
1830 break;
1831 case Token::MUL: {
1832 __ SmiUntag(scratch1, right);
1833 __ Mult(left, scratch1);
1834 __ mflo(scratch1);
1835 __ mfhi(scratch2);
1836 __ sra(scratch1, scratch1, 31);
1837 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1838 __ mflo(v0);
1839 __ Branch(&done, ne, v0, Operand(zero_reg));
1840 __ Addu(scratch2, right, left);
1841 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1842 ASSERT(Smi::FromInt(0) == 0);
1843 __ mov(v0, zero_reg);
1844 break;
1845 }
1846 case Token::BIT_OR:
1847 __ Or(v0, left, Operand(right));
1848 break;
1849 case Token::BIT_AND:
1850 __ And(v0, left, Operand(right));
1851 break;
1852 case Token::BIT_XOR:
1853 __ Xor(v0, left, Operand(right));
1854 break;
1855 default:
1856 UNREACHABLE();
1857 }
1858
1859 __ bind(&done);
1860 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001861}
1862
1863
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001864void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1865 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001866 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001867 __ mov(a0, result_register());
1868 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001869 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001870 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001871 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001872 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001873 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001874}
1875
1876
1877void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001878 // Invalid left-hand sides are rewritten to have a 'throw
1879 // ReferenceError' on the left-hand side.
1880 if (!expr->IsValidLeftHandSide()) {
1881 VisitForEffect(expr);
1882 return;
1883 }
1884
1885 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001886 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001887 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1888 LhsKind assign_type = VARIABLE;
1889 Property* prop = expr->AsProperty();
1890 if (prop != NULL) {
1891 assign_type = (prop->key()->IsPropertyName())
1892 ? NAMED_PROPERTY
1893 : KEYED_PROPERTY;
1894 }
1895
1896 switch (assign_type) {
1897 case VARIABLE: {
1898 Variable* var = expr->AsVariableProxy()->var();
1899 EffectContext context(this);
1900 EmitVariableAssignment(var, Token::ASSIGN);
1901 break;
1902 }
1903 case NAMED_PROPERTY: {
1904 __ push(result_register()); // Preserve value.
1905 VisitForAccumulatorValue(prop->obj());
1906 __ mov(a1, result_register());
1907 __ pop(a0); // Restore value.
1908 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001909 Handle<Code> ic = is_classic_mode()
1910 ? isolate()->builtins()->StoreIC_Initialize()
1911 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001912 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001913 break;
1914 }
1915 case KEYED_PROPERTY: {
1916 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001917 VisitForStackValue(prop->obj());
1918 VisitForAccumulatorValue(prop->key());
1919 __ mov(a1, result_register());
1920 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001921 __ pop(a0); // Restore value.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001922 Handle<Code> ic = is_classic_mode()
1923 ? isolate()->builtins()->KeyedStoreIC_Initialize()
1924 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001925 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001926 break;
1927 }
1928 }
1929 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1930 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001931}
1932
1933
1934void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001935 Token::Value op) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001936 if (var->IsUnallocated()) {
1937 // Global var, const, or let.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001938 __ mov(a0, result_register());
1939 __ li(a2, Operand(var->name()));
1940 __ lw(a1, GlobalObjectOperand());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001941 Handle<Code> ic = is_classic_mode()
1942 ? isolate()->builtins()->StoreIC_Initialize()
1943 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001944 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001945
1946 } else if (op == Token::INIT_CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001947 // Const initializers need a write barrier.
1948 ASSERT(!var->IsParameter()); // No const parameters.
1949 if (var->IsStackLocal()) {
1950 Label skip;
1951 __ lw(a1, StackOperand(var));
1952 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1953 __ Branch(&skip, ne, a1, Operand(t0));
1954 __ sw(result_register(), StackOperand(var));
1955 __ bind(&skip);
1956 } else {
1957 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
1958 // Like var declarations, const declarations are hoisted to function
1959 // scope. However, unlike var initializers, const initializers are
1960 // able to drill a hole to that function context, even from inside a
1961 // 'with' context. We thus bypass the normal static scope lookup for
1962 // var->IsContextSlot().
1963 __ push(v0);
1964 __ li(a0, Operand(var->name()));
1965 __ Push(cp, a0); // Context and name.
1966 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001967 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001968
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001969 } else if (var->mode() == LET && op != Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001970 // Non-initializing assignment to let variable needs a write barrier.
1971 if (var->IsLookupSlot()) {
1972 __ push(v0); // Value.
1973 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001974 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001975 __ Push(cp, a1, a0); // Context, name, strict mode.
1976 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1977 } else {
1978 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
1979 Label assign;
1980 MemOperand location = VarOperand(var, a1);
1981 __ lw(a3, location);
1982 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1983 __ Branch(&assign, ne, a3, Operand(t0));
1984 __ li(a3, Operand(var->name()));
1985 __ push(a3);
1986 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1987 // Perform the assignment.
1988 __ bind(&assign);
1989 __ sw(result_register(), location);
1990 if (var->IsContextSlot()) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001991 // RecordWrite may destroy all its register arguments.
1992 __ mov(a3, result_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001993 int offset = Context::SlotOffset(var->index());
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001994 __ RecordWriteContextSlot(
1995 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001996 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001997 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001998
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001999 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2000 // Assignment to var or initializing assignment to let/const
2001 // in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002002 if (var->IsStackAllocated() || var->IsContextSlot()) {
2003 MemOperand location = VarOperand(var, a1);
2004 if (FLAG_debug_code && op == Token::INIT_LET) {
2005 // Check for an uninitialized let binding.
2006 __ lw(a2, location);
2007 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2008 __ Check(eq, "Let binding re-initialization.", a2, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002009 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002010 // Perform the assignment.
2011 __ sw(v0, location);
2012 if (var->IsContextSlot()) {
2013 __ mov(a3, v0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002014 int offset = Context::SlotOffset(var->index());
2015 __ RecordWriteContextSlot(
2016 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002017 }
2018 } else {
2019 ASSERT(var->IsLookupSlot());
2020 __ push(v0); // Value.
2021 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002022 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002023 __ Push(cp, a1, a0); // Context, name, strict mode.
2024 __ CallRuntime(Runtime::kStoreContextSlot, 4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002025 }
2026 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002027 // Non-initializing assignments to consts are ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +00002028}
2029
2030
2031void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002032 // Assignment to a property, using a named store IC.
2033 Property* prop = expr->target()->AsProperty();
2034 ASSERT(prop != NULL);
2035 ASSERT(prop->key()->AsLiteral() != NULL);
2036
2037 // If the assignment starts a block of assignments to the same object,
2038 // change to slow case to avoid the quadratic behavior of repeatedly
2039 // adding fast properties.
2040 if (expr->starts_initialization_block()) {
2041 __ push(result_register());
2042 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
2043 __ push(t0);
2044 __ CallRuntime(Runtime::kToSlowProperties, 1);
2045 __ pop(result_register());
2046 }
2047
2048 // Record source code position before IC call.
2049 SetSourcePosition(expr->position());
2050 __ mov(a0, result_register()); // Load the value.
2051 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
2052 // Load receiver to a1. Leave a copy in the stack if needed for turning the
2053 // receiver into fast case.
2054 if (expr->ends_initialization_block()) {
2055 __ lw(a1, MemOperand(sp));
2056 } else {
2057 __ pop(a1);
2058 }
2059
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002060 Handle<Code> ic = is_classic_mode()
2061 ? isolate()->builtins()->StoreIC_Initialize()
2062 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002063 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002064
2065 // If the assignment ends an initialization block, revert to fast case.
2066 if (expr->ends_initialization_block()) {
2067 __ push(v0); // Result of assignment, saved even if not needed.
2068 // Receiver is under the result value.
2069 __ lw(t0, MemOperand(sp, kPointerSize));
2070 __ push(t0);
2071 __ CallRuntime(Runtime::kToFastProperties, 1);
2072 __ pop(v0);
2073 __ Drop(1);
2074 }
2075 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2076 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002077}
2078
2079
2080void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002081 // Assignment to a property, using a keyed store IC.
2082
2083 // If the assignment starts a block of assignments to the same object,
2084 // change to slow case to avoid the quadratic behavior of repeatedly
2085 // adding fast properties.
2086 if (expr->starts_initialization_block()) {
2087 __ push(result_register());
2088 // Receiver is now under the key and value.
2089 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2090 __ push(t0);
2091 __ CallRuntime(Runtime::kToSlowProperties, 1);
2092 __ pop(result_register());
2093 }
2094
2095 // Record source code position before IC call.
2096 SetSourcePosition(expr->position());
2097 // Call keyed store IC.
2098 // The arguments are:
2099 // - a0 is the value,
2100 // - a1 is the key,
2101 // - a2 is the receiver.
2102 __ mov(a0, result_register());
2103 __ pop(a1); // Key.
2104 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2105 // receiver into fast case.
2106 if (expr->ends_initialization_block()) {
2107 __ lw(a2, MemOperand(sp));
2108 } else {
2109 __ pop(a2);
2110 }
2111
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002112 Handle<Code> ic = is_classic_mode()
2113 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2114 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002115 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002116
2117 // If the assignment ends an initialization block, revert to fast case.
2118 if (expr->ends_initialization_block()) {
2119 __ push(v0); // Result of assignment, saved even if not needed.
2120 // Receiver is under the result value.
2121 __ lw(t0, MemOperand(sp, kPointerSize));
2122 __ push(t0);
2123 __ CallRuntime(Runtime::kToFastProperties, 1);
2124 __ pop(v0);
2125 __ Drop(1);
2126 }
2127 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2128 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002129}
2130
2131
2132void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002133 Comment cmnt(masm_, "[ Property");
2134 Expression* key = expr->key();
2135
2136 if (key->IsPropertyName()) {
2137 VisitForAccumulatorValue(expr->obj());
2138 EmitNamedPropertyLoad(expr);
2139 context()->Plug(v0);
2140 } else {
2141 VisitForStackValue(expr->obj());
2142 VisitForAccumulatorValue(expr->key());
2143 __ pop(a1);
2144 EmitKeyedPropertyLoad(expr);
2145 context()->Plug(v0);
2146 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002147}
2148
lrn@chromium.org7516f052011-03-30 08:52:27 +00002149
ager@chromium.org5c838252010-02-19 08:53:10 +00002150void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002151 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002152 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002153 // Code common for calls using the IC.
2154 ZoneList<Expression*>* args = expr->arguments();
2155 int arg_count = args->length();
2156 { PreservePositionScope scope(masm()->positions_recorder());
2157 for (int i = 0; i < arg_count; i++) {
2158 VisitForStackValue(args->at(i));
2159 }
2160 __ li(a2, Operand(name));
2161 }
2162 // Record source position for debugger.
2163 SetSourcePosition(expr->position());
2164 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002165 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002166 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002167 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002168 RecordJSReturnSite(expr);
2169 // Restore context register.
2170 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2171 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002172}
2173
2174
lrn@chromium.org7516f052011-03-30 08:52:27 +00002175void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002176 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002177 // Load the key.
2178 VisitForAccumulatorValue(key);
2179
2180 // Swap the name of the function and the receiver on the stack to follow
2181 // the calling convention for call ICs.
2182 __ pop(a1);
2183 __ push(v0);
2184 __ push(a1);
2185
2186 // Code common for calls using the IC.
2187 ZoneList<Expression*>* args = expr->arguments();
2188 int arg_count = args->length();
2189 { PreservePositionScope scope(masm()->positions_recorder());
2190 for (int i = 0; i < arg_count; i++) {
2191 VisitForStackValue(args->at(i));
2192 }
2193 }
2194 // Record source position for debugger.
2195 SetSourcePosition(expr->position());
2196 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002197 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002198 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002199 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002200 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002201 RecordJSReturnSite(expr);
2202 // Restore context register.
2203 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2204 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002205}
2206
2207
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002208void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002209 // Code common for calls using the call stub.
2210 ZoneList<Expression*>* args = expr->arguments();
2211 int arg_count = args->length();
2212 { PreservePositionScope scope(masm()->positions_recorder());
2213 for (int i = 0; i < arg_count; i++) {
2214 VisitForStackValue(args->at(i));
2215 }
2216 }
2217 // Record source position for debugger.
2218 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002219 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002220 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002221 __ CallStub(&stub);
2222 RecordJSReturnSite(expr);
2223 // Restore context register.
2224 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2225 context()->DropAndPlug(1, v0);
2226}
2227
2228
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002229void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002230 // Push copy of the first argument or undefined if it doesn't exist.
2231 if (arg_count > 0) {
2232 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2233 } else {
2234 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2235 }
2236 __ push(a1);
2237
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002238 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002239 int receiver_offset = 2 + info_->scope()->num_parameters();
2240 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002241 __ push(a1);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002242 // Push the language mode.
2243 __ li(a1, Operand(Smi::FromInt(language_mode())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002244 __ push(a1);
2245
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002246 // Push the start position of the scope the calls resides in.
2247 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
2248 __ push(a1);
2249
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002250 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002251 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org5c838252010-02-19 08:53:10 +00002252}
2253
2254
2255void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002256#ifdef DEBUG
2257 // We want to verify that RecordJSReturnSite gets called on all paths
2258 // through this function. Avoid early returns.
2259 expr->return_is_recorded_ = false;
2260#endif
2261
2262 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002263 Expression* callee = expr->expression();
2264 VariableProxy* proxy = callee->AsVariableProxy();
2265 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002266
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002267 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002268 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2269 // resolve the function we need to call and the receiver of the
2270 // call. Then we call the resolved function using the given
2271 // arguments.
2272 ZoneList<Expression*>* args = expr->arguments();
2273 int arg_count = args->length();
2274
2275 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002276 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002277 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2278 __ push(a2); // Reserved receiver slot.
2279
2280 // Push the arguments.
2281 for (int i = 0; i < arg_count; i++) {
2282 VisitForStackValue(args->at(i));
2283 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002284
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002285 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002286 // resolve eval.
2287 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2288 __ push(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002289 EmitResolvePossiblyDirectEval(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002290
2291 // The runtime call returns a pair of values in v0 (function) and
2292 // v1 (receiver). Touch up the stack with the right values.
2293 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2294 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2295 }
2296 // Record source position for debugger.
2297 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002298 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002299 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002300 __ CallStub(&stub);
2301 RecordJSReturnSite(expr);
2302 // Restore context register.
2303 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2304 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002305 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002306 // Push global object as receiver for the call IC.
2307 __ lw(a0, GlobalObjectOperand());
2308 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002309 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2310 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002311 // Call to a lookup slot (dynamically introduced variable).
2312 Label slow, done;
2313
2314 { PreservePositionScope scope(masm()->positions_recorder());
2315 // Generate code for loading from variables potentially shadowed
2316 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002317 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002318 }
2319
2320 __ bind(&slow);
2321 // Call the runtime to find the function to call (returned in v0)
2322 // and the object holding it (returned in v1).
2323 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002324 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002325 __ push(a2);
2326 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2327 __ Push(v0, v1); // Function, receiver.
2328
2329 // If fast case code has been generated, emit code to push the
2330 // function and receiver and have the slow path jump around this
2331 // code.
2332 if (done.is_linked()) {
2333 Label call;
2334 __ Branch(&call);
2335 __ bind(&done);
2336 // Push function.
2337 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002338 // The receiver is implicitly the global receiver. Indicate this
2339 // by passing the hole to the call function stub.
2340 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002341 __ push(a1);
2342 __ bind(&call);
2343 }
2344
danno@chromium.org40cb8782011-05-25 07:58:50 +00002345 // The receiver is either the global receiver or an object found
2346 // by LoadContextSlot. That object could be the hole if the
2347 // receiver is implicitly the global object.
2348 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002349 } else if (property != NULL) {
2350 { PreservePositionScope scope(masm()->positions_recorder());
2351 VisitForStackValue(property->obj());
2352 }
2353 if (property->key()->IsPropertyName()) {
2354 EmitCallWithIC(expr,
2355 property->key()->AsLiteral()->handle(),
2356 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002357 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002358 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002359 }
2360 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002361 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002362 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002363 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002364 }
2365 // Load global receiver object.
2366 __ lw(a1, GlobalObjectOperand());
2367 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2368 __ push(a1);
2369 // Emit function call.
2370 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2371 }
2372
2373#ifdef DEBUG
2374 // RecordJSReturnSite should have been called.
2375 ASSERT(expr->return_is_recorded_);
2376#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002377}
2378
2379
2380void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002381 Comment cmnt(masm_, "[ CallNew");
2382 // According to ECMA-262, section 11.2.2, page 44, the function
2383 // expression in new calls must be evaluated before the
2384 // arguments.
2385
2386 // Push constructor on the stack. If it's not a function it's used as
2387 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2388 // ignored.
2389 VisitForStackValue(expr->expression());
2390
2391 // Push the arguments ("left-to-right") on the stack.
2392 ZoneList<Expression*>* args = expr->arguments();
2393 int arg_count = args->length();
2394 for (int i = 0; i < arg_count; i++) {
2395 VisitForStackValue(args->at(i));
2396 }
2397
2398 // Call the construct call builtin that handles allocation and
2399 // constructor invocation.
2400 SetSourcePosition(expr->position());
2401
2402 // Load function and argument count into a1 and a0.
2403 __ li(a0, Operand(arg_count));
2404 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2405
2406 Handle<Code> construct_builtin =
2407 isolate()->builtins()->JSConstructCall();
2408 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2409 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002410}
2411
2412
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002413void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2414 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002415 ASSERT(args->length() == 1);
2416
2417 VisitForAccumulatorValue(args->at(0));
2418
2419 Label materialize_true, materialize_false;
2420 Label* if_true = NULL;
2421 Label* if_false = NULL;
2422 Label* fall_through = NULL;
2423 context()->PrepareTest(&materialize_true, &materialize_false,
2424 &if_true, &if_false, &fall_through);
2425
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002426 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002427 __ And(t0, v0, Operand(kSmiTagMask));
2428 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2429
2430 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002431}
2432
2433
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002434void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2435 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002436 ASSERT(args->length() == 1);
2437
2438 VisitForAccumulatorValue(args->at(0));
2439
2440 Label materialize_true, materialize_false;
2441 Label* if_true = NULL;
2442 Label* if_false = NULL;
2443 Label* fall_through = NULL;
2444 context()->PrepareTest(&materialize_true, &materialize_false,
2445 &if_true, &if_false, &fall_through);
2446
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002447 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002448 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2449 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2450
2451 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002452}
2453
2454
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002455void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2456 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002457 ASSERT(args->length() == 1);
2458
2459 VisitForAccumulatorValue(args->at(0));
2460
2461 Label materialize_true, materialize_false;
2462 Label* if_true = NULL;
2463 Label* if_false = NULL;
2464 Label* fall_through = NULL;
2465 context()->PrepareTest(&materialize_true, &materialize_false,
2466 &if_true, &if_false, &fall_through);
2467
2468 __ JumpIfSmi(v0, if_false);
2469 __ LoadRoot(at, Heap::kNullValueRootIndex);
2470 __ Branch(if_true, eq, v0, Operand(at));
2471 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2472 // Undetectable objects behave like undefined when tested with typeof.
2473 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2474 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2475 __ Branch(if_false, ne, at, Operand(zero_reg));
2476 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002477 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002478 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002479 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2480 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002481
2482 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002483}
2484
2485
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002486void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2487 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002488 ASSERT(args->length() == 1);
2489
2490 VisitForAccumulatorValue(args->at(0));
2491
2492 Label materialize_true, materialize_false;
2493 Label* if_true = NULL;
2494 Label* if_false = NULL;
2495 Label* fall_through = NULL;
2496 context()->PrepareTest(&materialize_true, &materialize_false,
2497 &if_true, &if_false, &fall_through);
2498
2499 __ JumpIfSmi(v0, if_false);
2500 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002501 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002502 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002503 if_true, if_false, fall_through);
2504
2505 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002506}
2507
2508
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002509void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2510 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002511 ASSERT(args->length() == 1);
2512
2513 VisitForAccumulatorValue(args->at(0));
2514
2515 Label materialize_true, materialize_false;
2516 Label* if_true = NULL;
2517 Label* if_false = NULL;
2518 Label* fall_through = NULL;
2519 context()->PrepareTest(&materialize_true, &materialize_false,
2520 &if_true, &if_false, &fall_through);
2521
2522 __ JumpIfSmi(v0, if_false);
2523 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2524 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2525 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002526 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002527 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2528
2529 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002530}
2531
2532
2533void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002534 CallRuntime* expr) {
2535 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002536 ASSERT(args->length() == 1);
2537
2538 VisitForAccumulatorValue(args->at(0));
2539
2540 Label materialize_true, materialize_false;
2541 Label* if_true = NULL;
2542 Label* if_false = NULL;
2543 Label* fall_through = NULL;
2544 context()->PrepareTest(&materialize_true, &materialize_false,
2545 &if_true, &if_false, &fall_through);
2546
2547 if (FLAG_debug_code) __ AbortIfSmi(v0);
2548
2549 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2550 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2551 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2552 __ Branch(if_true, ne, t0, Operand(zero_reg));
2553
2554 // Check for fast case object. Generate false result for slow case object.
2555 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2556 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2557 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2558 __ Branch(if_false, eq, a2, Operand(t0));
2559
2560 // Look for valueOf symbol in the descriptor array, and indicate false if
2561 // found. The type is not checked, so if it is a transition it is a false
2562 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002563 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002564 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2565 // t0: descriptor array
2566 // a3: length of descriptor array
2567 // Calculate the end of the descriptor array.
2568 STATIC_ASSERT(kSmiTag == 0);
2569 STATIC_ASSERT(kSmiTagSize == 1);
2570 STATIC_ASSERT(kPointerSize == 4);
2571 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2572 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2573 __ Addu(a2, a2, t1);
2574
2575 // Calculate location of the first key name.
2576 __ Addu(t0,
2577 t0,
2578 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2579 DescriptorArray::kFirstIndex * kPointerSize));
2580 // Loop through all the keys in the descriptor array. If one of these is the
2581 // symbol valueOf the result is false.
2582 Label entry, loop;
2583 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2584 // used in the loop below.
2585 __ li(t2, Operand(FACTORY->value_of_symbol()));
2586 __ jmp(&entry);
2587 __ bind(&loop);
2588 __ lw(a3, MemOperand(t0, 0));
2589 __ Branch(if_false, eq, a3, Operand(t2));
2590 __ Addu(t0, t0, Operand(kPointerSize));
2591 __ bind(&entry);
2592 __ Branch(&loop, ne, t0, Operand(a2));
2593
2594 // If a valueOf property is not found on the object check that it's
2595 // prototype is the un-modified String prototype. If not result is false.
2596 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2597 __ JumpIfSmi(a2, if_false);
2598 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2599 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2600 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2601 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2602 __ Branch(if_false, ne, a2, Operand(a3));
2603
2604 // Set the bit in the map to indicate that it has been checked safe for
2605 // default valueOf and set true result.
2606 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2607 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2608 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2609 __ jmp(if_true);
2610
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002611 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002612 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002613}
2614
2615
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002616void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2617 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002618 ASSERT(args->length() == 1);
2619
2620 VisitForAccumulatorValue(args->at(0));
2621
2622 Label materialize_true, materialize_false;
2623 Label* if_true = NULL;
2624 Label* if_false = NULL;
2625 Label* fall_through = NULL;
2626 context()->PrepareTest(&materialize_true, &materialize_false,
2627 &if_true, &if_false, &fall_through);
2628
2629 __ JumpIfSmi(v0, if_false);
2630 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002631 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002632 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2633 __ Branch(if_false);
2634
2635 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002636}
2637
2638
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002639void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2640 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002641 ASSERT(args->length() == 1);
2642
2643 VisitForAccumulatorValue(args->at(0));
2644
2645 Label materialize_true, materialize_false;
2646 Label* if_true = NULL;
2647 Label* if_false = NULL;
2648 Label* fall_through = NULL;
2649 context()->PrepareTest(&materialize_true, &materialize_false,
2650 &if_true, &if_false, &fall_through);
2651
2652 __ JumpIfSmi(v0, if_false);
2653 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002654 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002655 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2656 if_true, if_false, fall_through);
2657
2658 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002659}
2660
2661
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002662void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2663 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002664 ASSERT(args->length() == 1);
2665
2666 VisitForAccumulatorValue(args->at(0));
2667
2668 Label materialize_true, materialize_false;
2669 Label* if_true = NULL;
2670 Label* if_false = NULL;
2671 Label* fall_through = NULL;
2672 context()->PrepareTest(&materialize_true, &materialize_false,
2673 &if_true, &if_false, &fall_through);
2674
2675 __ JumpIfSmi(v0, if_false);
2676 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002677 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002678 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2679
2680 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002681}
2682
2683
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002684void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2685 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002686
2687 Label materialize_true, materialize_false;
2688 Label* if_true = NULL;
2689 Label* if_false = NULL;
2690 Label* fall_through = NULL;
2691 context()->PrepareTest(&materialize_true, &materialize_false,
2692 &if_true, &if_false, &fall_through);
2693
2694 // Get the frame pointer for the calling frame.
2695 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2696
2697 // Skip the arguments adaptor frame if it exists.
2698 Label check_frame_marker;
2699 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2700 __ Branch(&check_frame_marker, ne,
2701 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2702 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2703
2704 // Check the marker in the calling frame.
2705 __ bind(&check_frame_marker);
2706 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002707 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002708 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2709 if_true, if_false, fall_through);
2710
2711 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002712}
2713
2714
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002715void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2716 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002717 ASSERT(args->length() == 2);
2718
2719 // Load the two objects into registers and perform the comparison.
2720 VisitForStackValue(args->at(0));
2721 VisitForAccumulatorValue(args->at(1));
2722
2723 Label materialize_true, materialize_false;
2724 Label* if_true = NULL;
2725 Label* if_false = NULL;
2726 Label* fall_through = NULL;
2727 context()->PrepareTest(&materialize_true, &materialize_false,
2728 &if_true, &if_false, &fall_through);
2729
2730 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002731 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002732 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2733
2734 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002735}
2736
2737
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002738void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2739 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002740 ASSERT(args->length() == 1);
2741
2742 // ArgumentsAccessStub expects the key in a1 and the formal
2743 // parameter count in a0.
2744 VisitForAccumulatorValue(args->at(0));
2745 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002746 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002747 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2748 __ CallStub(&stub);
2749 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002750}
2751
2752
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002753void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2754 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002755 Label exit;
2756 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002757 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002758
2759 // Check if the calling frame is an arguments adaptor frame.
2760 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2761 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2762 __ Branch(&exit, ne, a3,
2763 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2764
2765 // Arguments adaptor case: Read the arguments length from the
2766 // adaptor frame.
2767 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2768
2769 __ bind(&exit);
2770 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002771}
2772
2773
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002774void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2775 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002776 ASSERT(args->length() == 1);
2777 Label done, null, function, non_function_constructor;
2778
2779 VisitForAccumulatorValue(args->at(0));
2780
2781 // If the object is a smi, we return null.
2782 __ JumpIfSmi(v0, &null);
2783
2784 // Check that the object is a JS object but take special care of JS
2785 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002786 // Assume that there are only two callable types, and one of them is at
2787 // either end of the type range for JS object types. Saves extra comparisons.
2788 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002789 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002790 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002791
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002792 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2793 FIRST_SPEC_OBJECT_TYPE + 1);
2794 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002795
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002796 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2797 LAST_SPEC_OBJECT_TYPE - 1);
2798 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2799 // Assume that there is no larger type.
2800 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2801
2802 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002803 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2804 __ GetObjectType(v0, a1, a1);
2805 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2806
2807 // v0 now contains the constructor function. Grab the
2808 // instance class name from there.
2809 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2810 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2811 __ Branch(&done);
2812
2813 // Functions have class 'Function'.
2814 __ bind(&function);
2815 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2816 __ jmp(&done);
2817
2818 // Objects with a non-function constructor have class 'Object'.
2819 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002820 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002821 __ jmp(&done);
2822
2823 // Non-JS objects have class null.
2824 __ bind(&null);
2825 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2826
2827 // All done.
2828 __ bind(&done);
2829
2830 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002831}
2832
2833
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002834void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002835 // Conditionally generate a log call.
2836 // Args:
2837 // 0 (literal string): The type of logging (corresponds to the flags).
2838 // This is used to determine whether or not to generate the log call.
2839 // 1 (string): Format string. Access the string at argument index 2
2840 // with '%2s' (see Logger::LogRuntime for all the formats).
2841 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002842 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002843 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002844 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2845 VisitForStackValue(args->at(1));
2846 VisitForStackValue(args->at(2));
2847 __ CallRuntime(Runtime::kLog, 2);
2848 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002849
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002850 // Finally, we're expected to leave a value on the top of the stack.
2851 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2852 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002853}
2854
2855
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002856void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
2857 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002858 Label slow_allocate_heapnumber;
2859 Label heapnumber_allocated;
2860
2861 // Save the new heap number in callee-saved register s0, since
2862 // we call out to external C code below.
2863 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2864 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2865 __ jmp(&heapnumber_allocated);
2866
2867 __ bind(&slow_allocate_heapnumber);
2868
2869 // Allocate a heap number.
2870 __ CallRuntime(Runtime::kNumberAlloc, 0);
2871 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2872
2873 __ bind(&heapnumber_allocated);
2874
2875 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2876 // by computing:
2877 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2878 if (CpuFeatures::IsSupported(FPU)) {
2879 __ PrepareCallCFunction(1, a0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002880 __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
2881 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002882 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2883
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002884 CpuFeatures::Scope scope(FPU);
2885 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2886 __ li(a1, Operand(0x41300000));
2887 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002888 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002889 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002890 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002891 // Subtract and store the result in the heap number.
2892 __ sub_d(f0, f12, f14);
2893 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2894 __ mov(v0, s0);
2895 } else {
2896 __ PrepareCallCFunction(2, a0);
2897 __ mov(a0, s0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002898 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
2899 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002900 __ CallCFunction(
2901 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2902 }
2903
2904 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002905}
2906
2907
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002908void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002909 // Load the arguments on the stack and call the stub.
2910 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002911 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002912 ASSERT(args->length() == 3);
2913 VisitForStackValue(args->at(0));
2914 VisitForStackValue(args->at(1));
2915 VisitForStackValue(args->at(2));
2916 __ CallStub(&stub);
2917 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002918}
2919
2920
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002921void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002922 // Load the arguments on the stack and call the stub.
2923 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002924 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002925 ASSERT(args->length() == 4);
2926 VisitForStackValue(args->at(0));
2927 VisitForStackValue(args->at(1));
2928 VisitForStackValue(args->at(2));
2929 VisitForStackValue(args->at(3));
2930 __ CallStub(&stub);
2931 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002932}
2933
2934
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002935void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
2936 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002937 ASSERT(args->length() == 1);
2938
2939 VisitForAccumulatorValue(args->at(0)); // Load the object.
2940
2941 Label done;
2942 // If the object is a smi return the object.
2943 __ JumpIfSmi(v0, &done);
2944 // If the object is not a value type, return the object.
2945 __ GetObjectType(v0, a1, a1);
2946 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2947
2948 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2949
2950 __ bind(&done);
2951 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002952}
2953
2954
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002955void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002956 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002957 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002958 ASSERT(args->length() == 2);
2959 VisitForStackValue(args->at(0));
2960 VisitForStackValue(args->at(1));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002961 if (CpuFeatures::IsSupported(FPU)) {
2962 MathPowStub stub(MathPowStub::ON_STACK);
2963 __ CallStub(&stub);
2964 } else {
2965 __ CallRuntime(Runtime::kMath_pow, 2);
2966 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002967 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002968}
2969
2970
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002971void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
2972 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002973 ASSERT(args->length() == 2);
2974
2975 VisitForStackValue(args->at(0)); // Load the object.
2976 VisitForAccumulatorValue(args->at(1)); // Load the value.
2977 __ pop(a1); // v0 = value. a1 = object.
2978
2979 Label done;
2980 // If the object is a smi, return the value.
2981 __ JumpIfSmi(a1, &done);
2982
2983 // If the object is not a value type, return the value.
2984 __ GetObjectType(a1, a2, a2);
2985 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2986
2987 // Store the value.
2988 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2989 // Update the write barrier. Save the value as it will be
2990 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002991 __ mov(a2, v0);
2992 __ RecordWriteField(
2993 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002994
2995 __ bind(&done);
2996 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002997}
2998
2999
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003000void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3001 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003002 ASSERT_EQ(args->length(), 1);
3003
3004 // Load the argument on the stack and call the stub.
3005 VisitForStackValue(args->at(0));
3006
3007 NumberToStringStub stub;
3008 __ CallStub(&stub);
3009 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003010}
3011
3012
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003013void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3014 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003015 ASSERT(args->length() == 1);
3016
3017 VisitForAccumulatorValue(args->at(0));
3018
3019 Label done;
3020 StringCharFromCodeGenerator generator(v0, a1);
3021 generator.GenerateFast(masm_);
3022 __ jmp(&done);
3023
3024 NopRuntimeCallHelper call_helper;
3025 generator.GenerateSlow(masm_, call_helper);
3026
3027 __ bind(&done);
3028 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003029}
3030
3031
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003032void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3033 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003034 ASSERT(args->length() == 2);
3035
3036 VisitForStackValue(args->at(0));
3037 VisitForAccumulatorValue(args->at(1));
3038 __ mov(a0, result_register());
3039
3040 Register object = a1;
3041 Register index = a0;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003042 Register result = v0;
3043
3044 __ pop(object);
3045
3046 Label need_conversion;
3047 Label index_out_of_range;
3048 Label done;
3049 StringCharCodeAtGenerator generator(object,
3050 index,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003051 result,
3052 &need_conversion,
3053 &need_conversion,
3054 &index_out_of_range,
3055 STRING_INDEX_IS_NUMBER);
3056 generator.GenerateFast(masm_);
3057 __ jmp(&done);
3058
3059 __ bind(&index_out_of_range);
3060 // When the index is out of range, the spec requires us to return
3061 // NaN.
3062 __ LoadRoot(result, Heap::kNanValueRootIndex);
3063 __ jmp(&done);
3064
3065 __ bind(&need_conversion);
3066 // Load the undefined value into the result register, which will
3067 // trigger conversion.
3068 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3069 __ jmp(&done);
3070
3071 NopRuntimeCallHelper call_helper;
3072 generator.GenerateSlow(masm_, call_helper);
3073
3074 __ bind(&done);
3075 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003076}
3077
3078
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003079void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3080 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003081 ASSERT(args->length() == 2);
3082
3083 VisitForStackValue(args->at(0));
3084 VisitForAccumulatorValue(args->at(1));
3085 __ mov(a0, result_register());
3086
3087 Register object = a1;
3088 Register index = a0;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003089 Register scratch = a3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003090 Register result = v0;
3091
3092 __ pop(object);
3093
3094 Label need_conversion;
3095 Label index_out_of_range;
3096 Label done;
3097 StringCharAtGenerator generator(object,
3098 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003099 scratch,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003100 result,
3101 &need_conversion,
3102 &need_conversion,
3103 &index_out_of_range,
3104 STRING_INDEX_IS_NUMBER);
3105 generator.GenerateFast(masm_);
3106 __ jmp(&done);
3107
3108 __ bind(&index_out_of_range);
3109 // When the index is out of range, the spec requires us to return
3110 // the empty string.
3111 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3112 __ jmp(&done);
3113
3114 __ bind(&need_conversion);
3115 // Move smi zero into the result register, which will trigger
3116 // conversion.
3117 __ li(result, Operand(Smi::FromInt(0)));
3118 __ jmp(&done);
3119
3120 NopRuntimeCallHelper call_helper;
3121 generator.GenerateSlow(masm_, call_helper);
3122
3123 __ bind(&done);
3124 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003125}
3126
3127
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003128void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3129 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003130 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003131 VisitForStackValue(args->at(0));
3132 VisitForStackValue(args->at(1));
3133
3134 StringAddStub stub(NO_STRING_ADD_FLAGS);
3135 __ CallStub(&stub);
3136 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003137}
3138
3139
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003140void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3141 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003142 ASSERT_EQ(2, args->length());
3143
3144 VisitForStackValue(args->at(0));
3145 VisitForStackValue(args->at(1));
3146
3147 StringCompareStub stub;
3148 __ CallStub(&stub);
3149 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003150}
3151
3152
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003153void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003154 // Load the argument on the stack and call the stub.
3155 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3156 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003157 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003158 ASSERT(args->length() == 1);
3159 VisitForStackValue(args->at(0));
3160 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3161 __ CallStub(&stub);
3162 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003163}
3164
3165
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003166void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003167 // Load the argument on the stack and call the stub.
3168 TranscendentalCacheStub stub(TranscendentalCache::COS,
3169 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003170 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003171 ASSERT(args->length() == 1);
3172 VisitForStackValue(args->at(0));
3173 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3174 __ CallStub(&stub);
3175 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003176}
3177
3178
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003179void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3180 // Load the argument on the stack and call the stub.
3181 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3182 TranscendentalCacheStub::TAGGED);
3183 ZoneList<Expression*>* args = expr->arguments();
3184 ASSERT(args->length() == 1);
3185 VisitForStackValue(args->at(0));
3186 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3187 __ CallStub(&stub);
3188 context()->Plug(v0);
3189}
3190
3191
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003192void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003193 // Load the argument on the stack and call the stub.
3194 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3195 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003196 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003197 ASSERT(args->length() == 1);
3198 VisitForStackValue(args->at(0));
3199 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3200 __ CallStub(&stub);
3201 context()->Plug(v0);
3202}
3203
3204
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003205void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003206 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003207 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003208 ASSERT(args->length() == 1);
3209 VisitForStackValue(args->at(0));
3210 __ CallRuntime(Runtime::kMath_sqrt, 1);
3211 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003212}
3213
3214
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003215void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3216 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003217 ASSERT(args->length() >= 2);
3218
3219 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3220 for (int i = 0; i < arg_count + 1; i++) {
3221 VisitForStackValue(args->at(i));
3222 }
3223 VisitForAccumulatorValue(args->last()); // Function.
3224
danno@chromium.orgc612e022011-11-10 11:38:15 +00003225 // Check for proxy.
3226 Label proxy, done;
3227 __ GetObjectType(v0, a1, a1);
3228 __ Branch(&proxy, eq, a1, Operand(JS_FUNCTION_PROXY_TYPE));
3229
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003230 // InvokeFunction requires the function in a1. Move it in there.
3231 __ mov(a1, result_register());
3232 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003233 __ InvokeFunction(a1, count, CALL_FUNCTION,
3234 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003235 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003236 __ jmp(&done);
3237
3238 __ bind(&proxy);
3239 __ push(v0);
3240 __ CallRuntime(Runtime::kCall, args->length());
3241 __ bind(&done);
3242
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003243 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003244}
3245
3246
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003247void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003248 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003249 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003250 ASSERT(args->length() == 3);
3251 VisitForStackValue(args->at(0));
3252 VisitForStackValue(args->at(1));
3253 VisitForStackValue(args->at(2));
3254 __ CallStub(&stub);
3255 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003256}
3257
3258
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003259void FullCodeGenerator::EmitSwapElements(CallRuntime* expr) {
3260 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003261 ASSERT(args->length() == 3);
3262 VisitForStackValue(args->at(0));
3263 VisitForStackValue(args->at(1));
3264 VisitForStackValue(args->at(2));
3265 Label done;
3266 Label slow_case;
3267 Register object = a0;
3268 Register index1 = a1;
3269 Register index2 = a2;
3270 Register elements = a3;
3271 Register scratch1 = t0;
3272 Register scratch2 = t1;
3273
3274 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3275 // Fetch the map and check if array is in fast case.
3276 // Check that object doesn't require security checks and
3277 // has no indexed interceptor.
3278 __ GetObjectType(object, scratch1, scratch2);
3279 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3280 // Map is now in scratch1.
3281
3282 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3283 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3284 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3285
3286 // Check the object's elements are in fast case and writable.
3287 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3288 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3289 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3290 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3291
3292 // Check that both indices are smis.
3293 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3294 __ lw(index2, MemOperand(sp, 0));
3295 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3296
3297 // Check that both indices are valid.
3298 Label not_hi;
3299 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3300 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3301 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3302 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3303 __ bind(&not_hi);
3304
3305 // Bring the address of the elements into index1 and index2.
3306 __ Addu(scratch1, elements,
3307 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3308 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3309 __ Addu(index1, scratch1, index1);
3310 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3311 __ Addu(index2, scratch1, index2);
3312
3313 // Swap elements.
3314 __ lw(scratch1, MemOperand(index1, 0));
3315 __ lw(scratch2, MemOperand(index2, 0));
3316 __ sw(scratch1, MemOperand(index2, 0));
3317 __ sw(scratch2, MemOperand(index1, 0));
3318
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003319 Label no_remembered_set;
3320 __ CheckPageFlag(elements,
3321 scratch1,
3322 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3323 ne,
3324 &no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003325 // Possible optimization: do a check that both values are Smis
3326 // (or them and test against Smi mask).
3327
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003328 // We are swapping two objects in an array and the incremental marker never
3329 // pauses in the middle of scanning a single object. Therefore the
3330 // incremental marker is not disturbed, so we don't need to call the
3331 // RecordWrite stub that notifies the incremental marker.
3332 __ RememberedSetHelper(elements,
3333 index1,
3334 scratch2,
3335 kDontSaveFPRegs,
3336 MacroAssembler::kFallThroughAtEnd);
3337 __ RememberedSetHelper(elements,
3338 index2,
3339 scratch2,
3340 kDontSaveFPRegs,
3341 MacroAssembler::kFallThroughAtEnd);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003342
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003343 __ bind(&no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003344 // We are done. Drop elements from the stack, and return undefined.
3345 __ Drop(3);
3346 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3347 __ jmp(&done);
3348
3349 __ bind(&slow_case);
3350 __ CallRuntime(Runtime::kSwapElements, 3);
3351
3352 __ bind(&done);
3353 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003354}
3355
3356
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003357void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3358 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003359 ASSERT_EQ(2, args->length());
3360
3361 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3362 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3363
3364 Handle<FixedArray> jsfunction_result_caches(
3365 isolate()->global_context()->jsfunction_result_caches());
3366 if (jsfunction_result_caches->length() <= cache_id) {
3367 __ Abort("Attempt to use undefined cache.");
3368 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3369 context()->Plug(v0);
3370 return;
3371 }
3372
3373 VisitForAccumulatorValue(args->at(1));
3374
3375 Register key = v0;
3376 Register cache = a1;
3377 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3378 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3379 __ lw(cache,
3380 ContextOperand(
3381 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3382 __ lw(cache,
3383 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3384
3385
3386 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003387 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003388 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3389 // a2 now holds finger offset as a smi.
3390 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3391 // a3 now points to the start of fixed array elements.
3392 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3393 __ addu(a3, a3, at);
3394 // a3 now points to key of indexed element of cache.
3395 __ lw(a2, MemOperand(a3));
3396 __ Branch(&not_found, ne, key, Operand(a2));
3397
3398 __ lw(v0, MemOperand(a3, kPointerSize));
3399 __ Branch(&done);
3400
3401 __ bind(&not_found);
3402 // Call runtime to perform the lookup.
3403 __ Push(cache, key);
3404 __ CallRuntime(Runtime::kGetFromCache, 2);
3405
3406 __ bind(&done);
3407 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003408}
3409
3410
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003411void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3412 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003413 ASSERT_EQ(2, args->length());
3414
3415 Register right = v0;
3416 Register left = a1;
3417 Register tmp = a2;
3418 Register tmp2 = a3;
3419
3420 VisitForStackValue(args->at(0));
3421 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3422 __ pop(left);
3423
3424 Label done, fail, ok;
3425 __ Branch(&ok, eq, left, Operand(right));
3426 // Fail if either is a non-HeapObject.
3427 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003428 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003429 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3430 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3431 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3432 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3433 __ Branch(&fail, ne, tmp, Operand(tmp2));
3434 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3435 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3436 __ Branch(&ok, eq, tmp, Operand(tmp2));
3437 __ bind(&fail);
3438 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3439 __ jmp(&done);
3440 __ bind(&ok);
3441 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3442 __ bind(&done);
3443
3444 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003445}
3446
3447
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003448void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3449 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003450 VisitForAccumulatorValue(args->at(0));
3451
3452 Label materialize_true, materialize_false;
3453 Label* if_true = NULL;
3454 Label* if_false = NULL;
3455 Label* fall_through = NULL;
3456 context()->PrepareTest(&materialize_true, &materialize_false,
3457 &if_true, &if_false, &fall_through);
3458
3459 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3460 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3461
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003462 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003463 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3464
3465 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003466}
3467
3468
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003469void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3470 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003471 ASSERT(args->length() == 1);
3472 VisitForAccumulatorValue(args->at(0));
3473
3474 if (FLAG_debug_code) {
3475 __ AbortIfNotString(v0);
3476 }
3477
3478 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3479 __ IndexFromHash(v0, v0);
3480
3481 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003482}
3483
3484
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003485void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003486 Label bailout, done, one_char_separator, long_separator,
3487 non_trivial_array, not_size_one_array, loop,
3488 empty_separator_loop, one_char_separator_loop,
3489 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003490 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003491 ASSERT(args->length() == 2);
3492 VisitForStackValue(args->at(1));
3493 VisitForAccumulatorValue(args->at(0));
3494
3495 // All aliases of the same register have disjoint lifetimes.
3496 Register array = v0;
3497 Register elements = no_reg; // Will be v0.
3498 Register result = no_reg; // Will be v0.
3499 Register separator = a1;
3500 Register array_length = a2;
3501 Register result_pos = no_reg; // Will be a2.
3502 Register string_length = a3;
3503 Register string = t0;
3504 Register element = t1;
3505 Register elements_end = t2;
3506 Register scratch1 = t3;
3507 Register scratch2 = t5;
3508 Register scratch3 = t4;
3509 Register scratch4 = v1;
3510
3511 // Separator operand is on the stack.
3512 __ pop(separator);
3513
3514 // Check that the array is a JSArray.
3515 __ JumpIfSmi(array, &bailout);
3516 __ GetObjectType(array, scratch1, scratch2);
3517 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3518
3519 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003520 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003521
3522 // If the array has length zero, return the empty string.
3523 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3524 __ SmiUntag(array_length);
3525 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3526 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3527 __ Branch(&done);
3528
3529 __ bind(&non_trivial_array);
3530
3531 // Get the FixedArray containing array's elements.
3532 elements = array;
3533 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3534 array = no_reg; // End of array's live range.
3535
3536 // Check that all array elements are sequential ASCII strings, and
3537 // accumulate the sum of their lengths, as a smi-encoded value.
3538 __ mov(string_length, zero_reg);
3539 __ Addu(element,
3540 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3541 __ sll(elements_end, array_length, kPointerSizeLog2);
3542 __ Addu(elements_end, element, elements_end);
3543 // Loop condition: while (element < elements_end).
3544 // Live values in registers:
3545 // elements: Fixed array of strings.
3546 // array_length: Length of the fixed array of strings (not smi)
3547 // separator: Separator string
3548 // string_length: Accumulated sum of string lengths (smi).
3549 // element: Current array element.
3550 // elements_end: Array end.
3551 if (FLAG_debug_code) {
3552 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3553 array_length, Operand(zero_reg));
3554 }
3555 __ bind(&loop);
3556 __ lw(string, MemOperand(element));
3557 __ Addu(element, element, kPointerSize);
3558 __ JumpIfSmi(string, &bailout);
3559 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3560 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3561 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3562 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3563 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3564 __ BranchOnOverflow(&bailout, scratch3);
3565 __ Branch(&loop, lt, element, Operand(elements_end));
3566
3567 // If array_length is 1, return elements[0], a string.
3568 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3569 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3570 __ Branch(&done);
3571
3572 __ bind(&not_size_one_array);
3573
3574 // Live values in registers:
3575 // separator: Separator string
3576 // array_length: Length of the array.
3577 // string_length: Sum of string lengths (smi).
3578 // elements: FixedArray of strings.
3579
3580 // Check that the separator is a flat ASCII string.
3581 __ JumpIfSmi(separator, &bailout);
3582 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3583 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3584 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3585
3586 // Add (separator length times array_length) - separator length to the
3587 // string_length to get the length of the result string. array_length is not
3588 // smi but the other values are, so the result is a smi.
3589 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3590 __ Subu(string_length, string_length, Operand(scratch1));
3591 __ Mult(array_length, scratch1);
3592 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3593 // zero.
3594 __ mfhi(scratch2);
3595 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3596 __ mflo(scratch2);
3597 __ And(scratch3, scratch2, Operand(0x80000000));
3598 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3599 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3600 __ BranchOnOverflow(&bailout, scratch3);
3601 __ SmiUntag(string_length);
3602
3603 // Get first element in the array to free up the elements register to be used
3604 // for the result.
3605 __ Addu(element,
3606 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3607 result = elements; // End of live range for elements.
3608 elements = no_reg;
3609 // Live values in registers:
3610 // element: First array element
3611 // separator: Separator string
3612 // string_length: Length of result string (not smi)
3613 // array_length: Length of the array.
3614 __ AllocateAsciiString(result,
3615 string_length,
3616 scratch1,
3617 scratch2,
3618 elements_end,
3619 &bailout);
3620 // Prepare for looping. Set up elements_end to end of the array. Set
3621 // result_pos to the position of the result where to write the first
3622 // character.
3623 __ sll(elements_end, array_length, kPointerSizeLog2);
3624 __ Addu(elements_end, element, elements_end);
3625 result_pos = array_length; // End of live range for array_length.
3626 array_length = no_reg;
3627 __ Addu(result_pos,
3628 result,
3629 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3630
3631 // Check the length of the separator.
3632 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3633 __ li(at, Operand(Smi::FromInt(1)));
3634 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3635 __ Branch(&long_separator, gt, scratch1, Operand(at));
3636
3637 // Empty separator case.
3638 __ bind(&empty_separator_loop);
3639 // Live values in registers:
3640 // result_pos: the position to which we are currently copying characters.
3641 // element: Current array element.
3642 // elements_end: Array end.
3643
3644 // Copy next array element to the result.
3645 __ lw(string, MemOperand(element));
3646 __ Addu(element, element, kPointerSize);
3647 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3648 __ SmiUntag(string_length);
3649 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3650 __ CopyBytes(string, result_pos, string_length, scratch1);
3651 // End while (element < elements_end).
3652 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3653 ASSERT(result.is(v0));
3654 __ Branch(&done);
3655
3656 // One-character separator case.
3657 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003658 // Replace separator with its ASCII character value.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003659 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3660 // Jump into the loop after the code that copies the separator, so the first
3661 // element is not preceded by a separator.
3662 __ jmp(&one_char_separator_loop_entry);
3663
3664 __ bind(&one_char_separator_loop);
3665 // Live values in registers:
3666 // result_pos: the position to which we are currently copying characters.
3667 // element: Current array element.
3668 // elements_end: Array end.
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003669 // separator: Single separator ASCII char (in lower byte).
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003670
3671 // Copy the separator character to the result.
3672 __ sb(separator, MemOperand(result_pos));
3673 __ Addu(result_pos, result_pos, 1);
3674
3675 // Copy next array element to the result.
3676 __ bind(&one_char_separator_loop_entry);
3677 __ lw(string, MemOperand(element));
3678 __ Addu(element, element, kPointerSize);
3679 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3680 __ SmiUntag(string_length);
3681 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3682 __ CopyBytes(string, result_pos, string_length, scratch1);
3683 // End while (element < elements_end).
3684 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3685 ASSERT(result.is(v0));
3686 __ Branch(&done);
3687
3688 // Long separator case (separator is more than one character). Entry is at the
3689 // label long_separator below.
3690 __ bind(&long_separator_loop);
3691 // Live values in registers:
3692 // result_pos: the position to which we are currently copying characters.
3693 // element: Current array element.
3694 // elements_end: Array end.
3695 // separator: Separator string.
3696
3697 // Copy the separator to the result.
3698 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3699 __ SmiUntag(string_length);
3700 __ Addu(string,
3701 separator,
3702 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3703 __ CopyBytes(string, result_pos, string_length, scratch1);
3704
3705 __ bind(&long_separator);
3706 __ lw(string, MemOperand(element));
3707 __ Addu(element, element, kPointerSize);
3708 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3709 __ SmiUntag(string_length);
3710 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3711 __ CopyBytes(string, result_pos, string_length, scratch1);
3712 // End while (element < elements_end).
3713 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3714 ASSERT(result.is(v0));
3715 __ Branch(&done);
3716
3717 __ bind(&bailout);
3718 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3719 __ bind(&done);
3720 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003721}
3722
3723
ager@chromium.org5c838252010-02-19 08:53:10 +00003724void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003725 Handle<String> name = expr->name();
3726 if (name->length() > 0 && name->Get(0) == '_') {
3727 Comment cmnt(masm_, "[ InlineRuntimeCall");
3728 EmitInlineRuntimeCall(expr);
3729 return;
3730 }
3731
3732 Comment cmnt(masm_, "[ CallRuntime");
3733 ZoneList<Expression*>* args = expr->arguments();
3734
3735 if (expr->is_jsruntime()) {
3736 // Prepare for calling JS runtime function.
3737 __ lw(a0, GlobalObjectOperand());
3738 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3739 __ push(a0);
3740 }
3741
3742 // Push the arguments ("left-to-right").
3743 int arg_count = args->length();
3744 for (int i = 0; i < arg_count; i++) {
3745 VisitForStackValue(args->at(i));
3746 }
3747
3748 if (expr->is_jsruntime()) {
3749 // Call the JS runtime function.
3750 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003751 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003752 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003753 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003754 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003755 // Restore context register.
3756 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3757 } else {
3758 // Call the C runtime function.
3759 __ CallRuntime(expr->function(), arg_count);
3760 }
3761 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003762}
3763
3764
3765void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003766 switch (expr->op()) {
3767 case Token::DELETE: {
3768 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003769 Property* property = expr->expression()->AsProperty();
3770 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003771
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003772 if (property != NULL) {
3773 VisitForStackValue(property->obj());
3774 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003775 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
3776 ? kNonStrictMode : kStrictMode;
3777 __ li(a1, Operand(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003778 __ push(a1);
3779 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3780 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003781 } else if (proxy != NULL) {
3782 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003783 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003784 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003785 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003786 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003787 __ lw(a2, GlobalObjectOperand());
3788 __ li(a1, Operand(var->name()));
3789 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3790 __ Push(a2, a1, a0);
3791 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3792 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003793 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003794 // Result of deleting non-global, non-dynamic variables is false.
3795 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003796 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003797 } else {
3798 // Non-global variable. Call the runtime to try to delete from the
3799 // context where the variable was introduced.
3800 __ push(context_register());
3801 __ li(a2, Operand(var->name()));
3802 __ push(a2);
3803 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3804 context()->Plug(v0);
3805 }
3806 } else {
3807 // Result of deleting non-property, non-variable reference is true.
3808 // The subexpression may have side effects.
3809 VisitForEffect(expr->expression());
3810 context()->Plug(true);
3811 }
3812 break;
3813 }
3814
3815 case Token::VOID: {
3816 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3817 VisitForEffect(expr->expression());
3818 context()->Plug(Heap::kUndefinedValueRootIndex);
3819 break;
3820 }
3821
3822 case Token::NOT: {
3823 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3824 if (context()->IsEffect()) {
3825 // Unary NOT has no side effects so it's only necessary to visit the
3826 // subexpression. Match the optimizing compiler by not branching.
3827 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003828 } else if (context()->IsTest()) {
3829 const TestContext* test = TestContext::cast(context());
3830 // The labels are swapped for the recursive call.
3831 VisitForControl(expr->expression(),
3832 test->false_label(),
3833 test->true_label(),
3834 test->fall_through());
3835 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003836 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003837 // We handle value contexts explicitly rather than simply visiting
3838 // for control and plugging the control flow into the context,
3839 // because we need to prepare a pair of extra administrative AST ids
3840 // for the optimizing compiler.
3841 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3842 Label materialize_true, materialize_false, done;
3843 VisitForControl(expr->expression(),
3844 &materialize_false,
3845 &materialize_true,
3846 &materialize_true);
3847 __ bind(&materialize_true);
3848 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3849 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3850 if (context()->IsStackValue()) __ push(v0);
3851 __ jmp(&done);
3852 __ bind(&materialize_false);
3853 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3854 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3855 if (context()->IsStackValue()) __ push(v0);
3856 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003857 }
3858 break;
3859 }
3860
3861 case Token::TYPEOF: {
3862 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3863 { StackValueContext context(this);
3864 VisitForTypeofValue(expr->expression());
3865 }
3866 __ CallRuntime(Runtime::kTypeof, 1);
3867 context()->Plug(v0);
3868 break;
3869 }
3870
3871 case Token::ADD: {
3872 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3873 VisitForAccumulatorValue(expr->expression());
3874 Label no_conversion;
3875 __ JumpIfSmi(result_register(), &no_conversion);
3876 __ mov(a0, result_register());
3877 ToNumberStub convert_stub;
3878 __ CallStub(&convert_stub);
3879 __ bind(&no_conversion);
3880 context()->Plug(result_register());
3881 break;
3882 }
3883
3884 case Token::SUB:
3885 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3886 break;
3887
3888 case Token::BIT_NOT:
3889 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3890 break;
3891
3892 default:
3893 UNREACHABLE();
3894 }
3895}
3896
3897
3898void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3899 const char* comment) {
3900 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3901 Comment cmt(masm_, comment);
3902 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3903 UnaryOverwriteMode overwrite =
3904 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003905 UnaryOpStub stub(expr->op(), overwrite);
3906 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003907 VisitForAccumulatorValue(expr->expression());
3908 SetSourcePosition(expr->position());
3909 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003910 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003911 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003912}
3913
3914
3915void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003916 Comment cmnt(masm_, "[ CountOperation");
3917 SetSourcePosition(expr->position());
3918
3919 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3920 // as the left-hand side.
3921 if (!expr->expression()->IsValidLeftHandSide()) {
3922 VisitForEffect(expr->expression());
3923 return;
3924 }
3925
3926 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003927 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003928 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3929 LhsKind assign_type = VARIABLE;
3930 Property* prop = expr->expression()->AsProperty();
3931 // In case of a property we use the uninitialized expression context
3932 // of the key to detect a named property.
3933 if (prop != NULL) {
3934 assign_type =
3935 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3936 }
3937
3938 // Evaluate expression and get value.
3939 if (assign_type == VARIABLE) {
3940 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3941 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003942 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003943 } else {
3944 // Reserve space for result of postfix operation.
3945 if (expr->is_postfix() && !context()->IsEffect()) {
3946 __ li(at, Operand(Smi::FromInt(0)));
3947 __ push(at);
3948 }
3949 if (assign_type == NAMED_PROPERTY) {
3950 // Put the object both on the stack and in the accumulator.
3951 VisitForAccumulatorValue(prop->obj());
3952 __ push(v0);
3953 EmitNamedPropertyLoad(prop);
3954 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003955 VisitForStackValue(prop->obj());
3956 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003957 __ lw(a1, MemOperand(sp, 0));
3958 __ push(v0);
3959 EmitKeyedPropertyLoad(prop);
3960 }
3961 }
3962
3963 // We need a second deoptimization point after loading the value
3964 // in case evaluating the property load my have a side effect.
3965 if (assign_type == VARIABLE) {
3966 PrepareForBailout(expr->expression(), TOS_REG);
3967 } else {
3968 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3969 }
3970
3971 // Call ToNumber only if operand is not a smi.
3972 Label no_conversion;
3973 __ JumpIfSmi(v0, &no_conversion);
3974 __ mov(a0, v0);
3975 ToNumberStub convert_stub;
3976 __ CallStub(&convert_stub);
3977 __ bind(&no_conversion);
3978
3979 // Save result for postfix expressions.
3980 if (expr->is_postfix()) {
3981 if (!context()->IsEffect()) {
3982 // Save the result on the stack. If we have a named or keyed property
3983 // we store the result under the receiver that is currently on top
3984 // of the stack.
3985 switch (assign_type) {
3986 case VARIABLE:
3987 __ push(v0);
3988 break;
3989 case NAMED_PROPERTY:
3990 __ sw(v0, MemOperand(sp, kPointerSize));
3991 break;
3992 case KEYED_PROPERTY:
3993 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3994 break;
3995 }
3996 }
3997 }
3998 __ mov(a0, result_register());
3999
4000 // Inline smi case if we are in a loop.
4001 Label stub_call, done;
4002 JumpPatchSite patch_site(masm_);
4003
4004 int count_value = expr->op() == Token::INC ? 1 : -1;
4005 __ li(a1, Operand(Smi::FromInt(count_value)));
4006
4007 if (ShouldInlineSmiCase(expr->op())) {
4008 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
4009 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
4010
4011 // We could eliminate this smi check if we split the code at
4012 // the first smi check before calling ToNumber.
4013 patch_site.EmitJumpIfSmi(v0, &done);
4014 __ bind(&stub_call);
4015 }
4016
4017 // Record position before stub call.
4018 SetSourcePosition(expr->position());
4019
danno@chromium.org40cb8782011-05-25 07:58:50 +00004020 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004021 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004022 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004023 __ bind(&done);
4024
4025 // Store the value returned in v0.
4026 switch (assign_type) {
4027 case VARIABLE:
4028 if (expr->is_postfix()) {
4029 { EffectContext context(this);
4030 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4031 Token::ASSIGN);
4032 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4033 context.Plug(v0);
4034 }
4035 // For all contexts except EffectConstant we have the result on
4036 // top of the stack.
4037 if (!context()->IsEffect()) {
4038 context()->PlugTOS();
4039 }
4040 } else {
4041 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4042 Token::ASSIGN);
4043 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4044 context()->Plug(v0);
4045 }
4046 break;
4047 case NAMED_PROPERTY: {
4048 __ mov(a0, result_register()); // Value.
4049 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4050 __ pop(a1); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004051 Handle<Code> ic = is_classic_mode()
4052 ? isolate()->builtins()->StoreIC_Initialize()
4053 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004054 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004055 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4056 if (expr->is_postfix()) {
4057 if (!context()->IsEffect()) {
4058 context()->PlugTOS();
4059 }
4060 } else {
4061 context()->Plug(v0);
4062 }
4063 break;
4064 }
4065 case KEYED_PROPERTY: {
4066 __ mov(a0, result_register()); // Value.
4067 __ pop(a1); // Key.
4068 __ pop(a2); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004069 Handle<Code> ic = is_classic_mode()
4070 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4071 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004072 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004073 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4074 if (expr->is_postfix()) {
4075 if (!context()->IsEffect()) {
4076 context()->PlugTOS();
4077 }
4078 } else {
4079 context()->Plug(v0);
4080 }
4081 break;
4082 }
4083 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004084}
4085
4086
lrn@chromium.org7516f052011-03-30 08:52:27 +00004087void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004088 ASSERT(!context()->IsEffect());
4089 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004090 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004091 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004092 Comment cmnt(masm_, "Global variable");
4093 __ lw(a0, GlobalObjectOperand());
4094 __ li(a2, Operand(proxy->name()));
4095 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4096 // Use a regular load, not a contextual load, to avoid a reference
4097 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004098 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004099 PrepareForBailout(expr, TOS_REG);
4100 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004101 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004102 Label done, slow;
4103
4104 // Generate code for loading from variables potentially shadowed
4105 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004106 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004107
4108 __ bind(&slow);
4109 __ li(a0, Operand(proxy->name()));
4110 __ Push(cp, a0);
4111 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4112 PrepareForBailout(expr, TOS_REG);
4113 __ bind(&done);
4114
4115 context()->Plug(v0);
4116 } else {
4117 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004118 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004119 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004120}
4121
ager@chromium.org04921a82011-06-27 13:21:41 +00004122void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004123 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004124 Handle<String> check) {
4125 Label materialize_true, materialize_false;
4126 Label* if_true = NULL;
4127 Label* if_false = NULL;
4128 Label* fall_through = NULL;
4129 context()->PrepareTest(&materialize_true, &materialize_false,
4130 &if_true, &if_false, &fall_through);
4131
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004132 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004133 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004134 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004135 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004136
4137 if (check->Equals(isolate()->heap()->number_symbol())) {
4138 __ JumpIfSmi(v0, if_true);
4139 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4140 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4141 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4142 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4143 __ JumpIfSmi(v0, if_false);
4144 // Check for undetectable objects => false.
4145 __ GetObjectType(v0, v0, a1);
4146 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4147 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4148 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4149 Split(eq, a1, Operand(zero_reg),
4150 if_true, if_false, fall_through);
4151 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4152 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4153 __ Branch(if_true, eq, v0, Operand(at));
4154 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4155 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004156 } else if (FLAG_harmony_typeof &&
4157 check->Equals(isolate()->heap()->null_symbol())) {
4158 __ LoadRoot(at, Heap::kNullValueRootIndex);
4159 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004160 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4161 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4162 __ Branch(if_true, eq, v0, Operand(at));
4163 __ JumpIfSmi(v0, if_false);
4164 // Check for undetectable objects => true.
4165 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4166 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4167 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4168 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4169 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4170 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004171 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4172 __ GetObjectType(v0, v0, a1);
4173 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4174 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4175 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004176 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4177 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004178 if (!FLAG_harmony_typeof) {
4179 __ LoadRoot(at, Heap::kNullValueRootIndex);
4180 __ Branch(if_true, eq, v0, Operand(at));
4181 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004182 // Check for JS objects => true.
4183 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004184 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004185 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004186 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004187 // Check for undetectable objects => false.
4188 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4189 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4190 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4191 } else {
4192 if (if_false != fall_through) __ jmp(if_false);
4193 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004194 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004195}
4196
4197
ager@chromium.org5c838252010-02-19 08:53:10 +00004198void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004199 Comment cmnt(masm_, "[ CompareOperation");
4200 SetSourcePosition(expr->position());
4201
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004202 // First we try a fast inlined version of the compare when one of
4203 // the operands is a literal.
4204 if (TryLiteralCompare(expr)) return;
4205
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004206 // Always perform the comparison for its control flow. Pack the result
4207 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004208 Label materialize_true, materialize_false;
4209 Label* if_true = NULL;
4210 Label* if_false = NULL;
4211 Label* fall_through = NULL;
4212 context()->PrepareTest(&materialize_true, &materialize_false,
4213 &if_true, &if_false, &fall_through);
4214
ager@chromium.org04921a82011-06-27 13:21:41 +00004215 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004216 VisitForStackValue(expr->left());
4217 switch (op) {
4218 case Token::IN:
4219 VisitForStackValue(expr->right());
4220 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004221 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004222 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4223 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4224 break;
4225
4226 case Token::INSTANCEOF: {
4227 VisitForStackValue(expr->right());
4228 InstanceofStub stub(InstanceofStub::kNoFlags);
4229 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004230 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004231 // The stub returns 0 for true.
4232 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4233 break;
4234 }
4235
4236 default: {
4237 VisitForAccumulatorValue(expr->right());
4238 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004239 switch (op) {
4240 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004241 case Token::EQ:
4242 cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004243 break;
4244 case Token::LT:
4245 cc = lt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004246 break;
4247 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004248 cc = gt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004249 break;
4250 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004251 cc = le;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004252 break;
4253 case Token::GTE:
4254 cc = ge;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004255 break;
4256 case Token::IN:
4257 case Token::INSTANCEOF:
4258 default:
4259 UNREACHABLE();
4260 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004261 __ mov(a0, result_register());
4262 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004263
4264 bool inline_smi_code = ShouldInlineSmiCase(op);
4265 JumpPatchSite patch_site(masm_);
4266 if (inline_smi_code) {
4267 Label slow_case;
4268 __ Or(a2, a0, Operand(a1));
4269 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4270 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4271 __ bind(&slow_case);
4272 }
4273 // Record position and call the compare IC.
4274 SetSourcePosition(expr->position());
4275 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004276 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004277 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004278 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004279 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4280 }
4281 }
4282
4283 // Convert the result of the comparison into one expected for this
4284 // expression's context.
4285 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004286}
4287
4288
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004289void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4290 Expression* sub_expr,
4291 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004292 Label materialize_true, materialize_false;
4293 Label* if_true = NULL;
4294 Label* if_false = NULL;
4295 Label* fall_through = NULL;
4296 context()->PrepareTest(&materialize_true, &materialize_false,
4297 &if_true, &if_false, &fall_through);
4298
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004299 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004300 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004301 Heap::RootListIndex nil_value = nil == kNullValue ?
4302 Heap::kNullValueRootIndex :
4303 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004304 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004305 __ LoadRoot(a1, nil_value);
4306 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004307 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4308 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004309 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4310 Heap::kUndefinedValueRootIndex :
4311 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004312 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004313 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004314 __ Branch(if_true, eq, a0, Operand(a1));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004315 __ JumpIfSmi(a0, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004316 // It can be an undetectable object.
4317 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4318 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4319 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4320 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4321 }
4322 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004323}
4324
4325
ager@chromium.org5c838252010-02-19 08:53:10 +00004326void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004327 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4328 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004329}
4330
4331
lrn@chromium.org7516f052011-03-30 08:52:27 +00004332Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004333 return v0;
4334}
ager@chromium.org5c838252010-02-19 08:53:10 +00004335
4336
lrn@chromium.org7516f052011-03-30 08:52:27 +00004337Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004338 return cp;
4339}
4340
4341
ager@chromium.org5c838252010-02-19 08:53:10 +00004342void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004343 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4344 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004345}
4346
4347
4348void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004349 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004350}
4351
4352
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004353void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4354 Scope* declaration_scope = scope()->DeclarationScope();
4355 if (declaration_scope->is_global_scope()) {
4356 // Contexts nested in the global context have a canonical empty function
4357 // as their closure, not the anonymous closure containing the global
4358 // code. Pass a smi sentinel and let the runtime look up the empty
4359 // function.
4360 __ li(at, Operand(Smi::FromInt(0)));
4361 } else if (declaration_scope->is_eval_scope()) {
4362 // Contexts created by a call to eval have the same closure as the
4363 // context calling eval, not the anonymous closure containing the eval
4364 // code. Fetch it from the context.
4365 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4366 } else {
4367 ASSERT(declaration_scope->is_function_scope());
4368 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4369 }
4370 __ push(at);
4371}
4372
4373
ager@chromium.org5c838252010-02-19 08:53:10 +00004374// ----------------------------------------------------------------------------
4375// Non-local control flow support.
4376
4377void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004378 ASSERT(!result_register().is(a1));
4379 // Store result register while executing finally block.
4380 __ push(result_register());
4381 // Cook return address in link register to stack (smi encoded Code* delta).
4382 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4383 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004384 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004385 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4386 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004387}
4388
4389
4390void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004391 ASSERT(!result_register().is(a1));
4392 // Restore result register from stack.
4393 __ pop(a1);
4394 // Uncook return address and return.
4395 __ pop(result_register());
4396 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4397 __ sra(a1, a1, 1); // Un-smi-tag value.
4398 __ Addu(at, a1, Operand(masm_->CodeObject()));
4399 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004400}
4401
4402
4403#undef __
4404
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004405#define __ ACCESS_MASM(masm())
4406
4407FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4408 int* stack_depth,
4409 int* context_length) {
4410 // The macros used here must preserve the result register.
4411
4412 // Because the handler block contains the context of the finally
4413 // code, we can restore it directly from there for the finally code
4414 // rather than iteratively unwinding contexts via their previous
4415 // links.
4416 __ Drop(*stack_depth); // Down to the handler block.
4417 if (*context_length > 0) {
4418 // Restore the context to its dedicated register and the stack.
4419 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4420 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4421 }
4422 __ PopTryHandler();
4423 __ Call(finally_entry_);
4424
4425 *stack_depth = 0;
4426 *context_length = 0;
4427 return previous_;
4428}
4429
4430
4431#undef __
4432
ager@chromium.org5c838252010-02-19 08:53:10 +00004433} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004434
4435#endif // V8_TARGET_ARCH_MIPS