blob: 201742efec74a2c7e98d50c030645f794155be42 [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
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002406 // Record call targets in unoptimized code, but not in the snapshot.
2407 CallFunctionFlags flags;
2408 if (!Serializer::enabled()) {
2409 flags = RECORD_CALL_TARGET;
2410 Handle<Object> uninitialized =
2411 TypeFeedbackCells::UninitializedSentinel(isolate());
2412 Handle<JSGlobalPropertyCell> cell =
2413 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
2414 RecordTypeFeedbackCell(expr->id(), cell);
2415 __ li(a2, Operand(cell));
2416 } else {
2417 flags = NO_CALL_FUNCTION_FLAGS;
2418 }
2419
2420 CallConstructStub stub(flags);
2421 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002422 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002423}
2424
2425
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002426void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2427 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002428 ASSERT(args->length() == 1);
2429
2430 VisitForAccumulatorValue(args->at(0));
2431
2432 Label materialize_true, materialize_false;
2433 Label* if_true = NULL;
2434 Label* if_false = NULL;
2435 Label* fall_through = NULL;
2436 context()->PrepareTest(&materialize_true, &materialize_false,
2437 &if_true, &if_false, &fall_through);
2438
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002439 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002440 __ And(t0, v0, Operand(kSmiTagMask));
2441 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2442
2443 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002444}
2445
2446
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002447void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2448 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002449 ASSERT(args->length() == 1);
2450
2451 VisitForAccumulatorValue(args->at(0));
2452
2453 Label materialize_true, materialize_false;
2454 Label* if_true = NULL;
2455 Label* if_false = NULL;
2456 Label* fall_through = NULL;
2457 context()->PrepareTest(&materialize_true, &materialize_false,
2458 &if_true, &if_false, &fall_through);
2459
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002460 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002461 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2462 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2463
2464 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002465}
2466
2467
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002468void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2469 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002470 ASSERT(args->length() == 1);
2471
2472 VisitForAccumulatorValue(args->at(0));
2473
2474 Label materialize_true, materialize_false;
2475 Label* if_true = NULL;
2476 Label* if_false = NULL;
2477 Label* fall_through = NULL;
2478 context()->PrepareTest(&materialize_true, &materialize_false,
2479 &if_true, &if_false, &fall_through);
2480
2481 __ JumpIfSmi(v0, if_false);
2482 __ LoadRoot(at, Heap::kNullValueRootIndex);
2483 __ Branch(if_true, eq, v0, Operand(at));
2484 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2485 // Undetectable objects behave like undefined when tested with typeof.
2486 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2487 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2488 __ Branch(if_false, ne, at, Operand(zero_reg));
2489 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002490 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002491 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002492 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2493 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002494
2495 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002496}
2497
2498
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002499void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2500 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002501 ASSERT(args->length() == 1);
2502
2503 VisitForAccumulatorValue(args->at(0));
2504
2505 Label materialize_true, materialize_false;
2506 Label* if_true = NULL;
2507 Label* if_false = NULL;
2508 Label* fall_through = NULL;
2509 context()->PrepareTest(&materialize_true, &materialize_false,
2510 &if_true, &if_false, &fall_through);
2511
2512 __ JumpIfSmi(v0, if_false);
2513 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002514 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002515 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002516 if_true, if_false, fall_through);
2517
2518 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002519}
2520
2521
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002522void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2523 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002524 ASSERT(args->length() == 1);
2525
2526 VisitForAccumulatorValue(args->at(0));
2527
2528 Label materialize_true, materialize_false;
2529 Label* if_true = NULL;
2530 Label* if_false = NULL;
2531 Label* fall_through = NULL;
2532 context()->PrepareTest(&materialize_true, &materialize_false,
2533 &if_true, &if_false, &fall_through);
2534
2535 __ JumpIfSmi(v0, if_false);
2536 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2537 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2538 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002539 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002540 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2541
2542 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002543}
2544
2545
2546void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002547 CallRuntime* expr) {
2548 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002549 ASSERT(args->length() == 1);
2550
2551 VisitForAccumulatorValue(args->at(0));
2552
2553 Label materialize_true, materialize_false;
2554 Label* if_true = NULL;
2555 Label* if_false = NULL;
2556 Label* fall_through = NULL;
2557 context()->PrepareTest(&materialize_true, &materialize_false,
2558 &if_true, &if_false, &fall_through);
2559
2560 if (FLAG_debug_code) __ AbortIfSmi(v0);
2561
2562 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2563 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2564 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2565 __ Branch(if_true, ne, t0, Operand(zero_reg));
2566
2567 // Check for fast case object. Generate false result for slow case object.
2568 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2569 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2570 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2571 __ Branch(if_false, eq, a2, Operand(t0));
2572
2573 // Look for valueOf symbol in the descriptor array, and indicate false if
2574 // found. The type is not checked, so if it is a transition it is a false
2575 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002576 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002577 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2578 // t0: descriptor array
2579 // a3: length of descriptor array
2580 // Calculate the end of the descriptor array.
2581 STATIC_ASSERT(kSmiTag == 0);
2582 STATIC_ASSERT(kSmiTagSize == 1);
2583 STATIC_ASSERT(kPointerSize == 4);
2584 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2585 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2586 __ Addu(a2, a2, t1);
2587
2588 // Calculate location of the first key name.
2589 __ Addu(t0,
2590 t0,
2591 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2592 DescriptorArray::kFirstIndex * kPointerSize));
2593 // Loop through all the keys in the descriptor array. If one of these is the
2594 // symbol valueOf the result is false.
2595 Label entry, loop;
2596 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2597 // used in the loop below.
2598 __ li(t2, Operand(FACTORY->value_of_symbol()));
2599 __ jmp(&entry);
2600 __ bind(&loop);
2601 __ lw(a3, MemOperand(t0, 0));
2602 __ Branch(if_false, eq, a3, Operand(t2));
2603 __ Addu(t0, t0, Operand(kPointerSize));
2604 __ bind(&entry);
2605 __ Branch(&loop, ne, t0, Operand(a2));
2606
2607 // If a valueOf property is not found on the object check that it's
2608 // prototype is the un-modified String prototype. If not result is false.
2609 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2610 __ JumpIfSmi(a2, if_false);
2611 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2612 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2613 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2614 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2615 __ Branch(if_false, ne, a2, Operand(a3));
2616
2617 // Set the bit in the map to indicate that it has been checked safe for
2618 // default valueOf and set true result.
2619 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2620 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2621 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2622 __ jmp(if_true);
2623
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002624 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002625 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002626}
2627
2628
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002629void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2630 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002631 ASSERT(args->length() == 1);
2632
2633 VisitForAccumulatorValue(args->at(0));
2634
2635 Label materialize_true, materialize_false;
2636 Label* if_true = NULL;
2637 Label* if_false = NULL;
2638 Label* fall_through = NULL;
2639 context()->PrepareTest(&materialize_true, &materialize_false,
2640 &if_true, &if_false, &fall_through);
2641
2642 __ JumpIfSmi(v0, if_false);
2643 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002644 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002645 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2646 __ Branch(if_false);
2647
2648 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002649}
2650
2651
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002652void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2653 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002654 ASSERT(args->length() == 1);
2655
2656 VisitForAccumulatorValue(args->at(0));
2657
2658 Label materialize_true, materialize_false;
2659 Label* if_true = NULL;
2660 Label* if_false = NULL;
2661 Label* fall_through = NULL;
2662 context()->PrepareTest(&materialize_true, &materialize_false,
2663 &if_true, &if_false, &fall_through);
2664
2665 __ JumpIfSmi(v0, if_false);
2666 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002667 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002668 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2669 if_true, if_false, fall_through);
2670
2671 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002672}
2673
2674
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002675void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2676 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002677 ASSERT(args->length() == 1);
2678
2679 VisitForAccumulatorValue(args->at(0));
2680
2681 Label materialize_true, materialize_false;
2682 Label* if_true = NULL;
2683 Label* if_false = NULL;
2684 Label* fall_through = NULL;
2685 context()->PrepareTest(&materialize_true, &materialize_false,
2686 &if_true, &if_false, &fall_through);
2687
2688 __ JumpIfSmi(v0, if_false);
2689 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002690 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002691 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2692
2693 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002694}
2695
2696
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002697void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2698 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002699
2700 Label materialize_true, materialize_false;
2701 Label* if_true = NULL;
2702 Label* if_false = NULL;
2703 Label* fall_through = NULL;
2704 context()->PrepareTest(&materialize_true, &materialize_false,
2705 &if_true, &if_false, &fall_through);
2706
2707 // Get the frame pointer for the calling frame.
2708 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2709
2710 // Skip the arguments adaptor frame if it exists.
2711 Label check_frame_marker;
2712 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2713 __ Branch(&check_frame_marker, ne,
2714 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2715 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2716
2717 // Check the marker in the calling frame.
2718 __ bind(&check_frame_marker);
2719 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002720 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002721 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2722 if_true, if_false, fall_through);
2723
2724 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002725}
2726
2727
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002728void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2729 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002730 ASSERT(args->length() == 2);
2731
2732 // Load the two objects into registers and perform the comparison.
2733 VisitForStackValue(args->at(0));
2734 VisitForAccumulatorValue(args->at(1));
2735
2736 Label materialize_true, materialize_false;
2737 Label* if_true = NULL;
2738 Label* if_false = NULL;
2739 Label* fall_through = NULL;
2740 context()->PrepareTest(&materialize_true, &materialize_false,
2741 &if_true, &if_false, &fall_through);
2742
2743 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002744 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002745 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2746
2747 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002748}
2749
2750
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002751void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2752 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002753 ASSERT(args->length() == 1);
2754
2755 // ArgumentsAccessStub expects the key in a1 and the formal
2756 // parameter count in a0.
2757 VisitForAccumulatorValue(args->at(0));
2758 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002759 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002760 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2761 __ CallStub(&stub);
2762 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002763}
2764
2765
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002766void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2767 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002768 Label exit;
2769 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002770 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002771
2772 // Check if the calling frame is an arguments adaptor frame.
2773 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2774 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2775 __ Branch(&exit, ne, a3,
2776 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2777
2778 // Arguments adaptor case: Read the arguments length from the
2779 // adaptor frame.
2780 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2781
2782 __ bind(&exit);
2783 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002784}
2785
2786
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002787void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2788 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002789 ASSERT(args->length() == 1);
2790 Label done, null, function, non_function_constructor;
2791
2792 VisitForAccumulatorValue(args->at(0));
2793
2794 // If the object is a smi, we return null.
2795 __ JumpIfSmi(v0, &null);
2796
2797 // Check that the object is a JS object but take special care of JS
2798 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002799 // Assume that there are only two callable types, and one of them is at
2800 // either end of the type range for JS object types. Saves extra comparisons.
2801 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002802 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002803 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002804
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002805 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2806 FIRST_SPEC_OBJECT_TYPE + 1);
2807 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002808
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002809 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2810 LAST_SPEC_OBJECT_TYPE - 1);
2811 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2812 // Assume that there is no larger type.
2813 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2814
2815 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002816 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2817 __ GetObjectType(v0, a1, a1);
2818 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2819
2820 // v0 now contains the constructor function. Grab the
2821 // instance class name from there.
2822 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2823 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2824 __ Branch(&done);
2825
2826 // Functions have class 'Function'.
2827 __ bind(&function);
2828 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2829 __ jmp(&done);
2830
2831 // Objects with a non-function constructor have class 'Object'.
2832 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002833 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002834 __ jmp(&done);
2835
2836 // Non-JS objects have class null.
2837 __ bind(&null);
2838 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2839
2840 // All done.
2841 __ bind(&done);
2842
2843 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002844}
2845
2846
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002847void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002848 // Conditionally generate a log call.
2849 // Args:
2850 // 0 (literal string): The type of logging (corresponds to the flags).
2851 // This is used to determine whether or not to generate the log call.
2852 // 1 (string): Format string. Access the string at argument index 2
2853 // with '%2s' (see Logger::LogRuntime for all the formats).
2854 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002855 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002856 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002857 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2858 VisitForStackValue(args->at(1));
2859 VisitForStackValue(args->at(2));
2860 __ CallRuntime(Runtime::kLog, 2);
2861 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002862
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002863 // Finally, we're expected to leave a value on the top of the stack.
2864 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2865 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002866}
2867
2868
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002869void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
2870 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002871 Label slow_allocate_heapnumber;
2872 Label heapnumber_allocated;
2873
2874 // Save the new heap number in callee-saved register s0, since
2875 // we call out to external C code below.
2876 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2877 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2878 __ jmp(&heapnumber_allocated);
2879
2880 __ bind(&slow_allocate_heapnumber);
2881
2882 // Allocate a heap number.
2883 __ CallRuntime(Runtime::kNumberAlloc, 0);
2884 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2885
2886 __ bind(&heapnumber_allocated);
2887
2888 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2889 // by computing:
2890 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2891 if (CpuFeatures::IsSupported(FPU)) {
2892 __ PrepareCallCFunction(1, a0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002893 __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
2894 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002895 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2896
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002897 CpuFeatures::Scope scope(FPU);
2898 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2899 __ li(a1, Operand(0x41300000));
2900 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002901 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002902 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002903 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002904 // Subtract and store the result in the heap number.
2905 __ sub_d(f0, f12, f14);
2906 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2907 __ mov(v0, s0);
2908 } else {
2909 __ PrepareCallCFunction(2, a0);
2910 __ mov(a0, s0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002911 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
2912 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002913 __ CallCFunction(
2914 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2915 }
2916
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::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002922 // Load the arguments on the stack and call the stub.
2923 SubStringStub 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() == 3);
2926 VisitForStackValue(args->at(0));
2927 VisitForStackValue(args->at(1));
2928 VisitForStackValue(args->at(2));
2929 __ CallStub(&stub);
2930 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002931}
2932
2933
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002934void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002935 // Load the arguments on the stack and call the stub.
2936 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002937 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002938 ASSERT(args->length() == 4);
2939 VisitForStackValue(args->at(0));
2940 VisitForStackValue(args->at(1));
2941 VisitForStackValue(args->at(2));
2942 VisitForStackValue(args->at(3));
2943 __ CallStub(&stub);
2944 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002945}
2946
2947
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002948void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
2949 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002950 ASSERT(args->length() == 1);
2951
2952 VisitForAccumulatorValue(args->at(0)); // Load the object.
2953
2954 Label done;
2955 // If the object is a smi return the object.
2956 __ JumpIfSmi(v0, &done);
2957 // If the object is not a value type, return the object.
2958 __ GetObjectType(v0, a1, a1);
2959 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2960
2961 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2962
2963 __ bind(&done);
2964 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002965}
2966
2967
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002968void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002969 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002970 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002971 ASSERT(args->length() == 2);
2972 VisitForStackValue(args->at(0));
2973 VisitForStackValue(args->at(1));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002974 if (CpuFeatures::IsSupported(FPU)) {
2975 MathPowStub stub(MathPowStub::ON_STACK);
2976 __ CallStub(&stub);
2977 } else {
2978 __ CallRuntime(Runtime::kMath_pow, 2);
2979 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002980 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002981}
2982
2983
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002984void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
2985 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002986 ASSERT(args->length() == 2);
2987
2988 VisitForStackValue(args->at(0)); // Load the object.
2989 VisitForAccumulatorValue(args->at(1)); // Load the value.
2990 __ pop(a1); // v0 = value. a1 = object.
2991
2992 Label done;
2993 // If the object is a smi, return the value.
2994 __ JumpIfSmi(a1, &done);
2995
2996 // If the object is not a value type, return the value.
2997 __ GetObjectType(a1, a2, a2);
2998 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2999
3000 // Store the value.
3001 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
3002 // Update the write barrier. Save the value as it will be
3003 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003004 __ mov(a2, v0);
3005 __ RecordWriteField(
3006 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003007
3008 __ bind(&done);
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::EmitNumberToString(CallRuntime* expr) {
3014 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003015 ASSERT_EQ(args->length(), 1);
3016
3017 // Load the argument on the stack and call the stub.
3018 VisitForStackValue(args->at(0));
3019
3020 NumberToStringStub stub;
3021 __ CallStub(&stub);
3022 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003023}
3024
3025
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003026void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3027 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003028 ASSERT(args->length() == 1);
3029
3030 VisitForAccumulatorValue(args->at(0));
3031
3032 Label done;
3033 StringCharFromCodeGenerator generator(v0, a1);
3034 generator.GenerateFast(masm_);
3035 __ jmp(&done);
3036
3037 NopRuntimeCallHelper call_helper;
3038 generator.GenerateSlow(masm_, call_helper);
3039
3040 __ bind(&done);
3041 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003042}
3043
3044
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003045void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3046 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003047 ASSERT(args->length() == 2);
3048
3049 VisitForStackValue(args->at(0));
3050 VisitForAccumulatorValue(args->at(1));
3051 __ mov(a0, result_register());
3052
3053 Register object = a1;
3054 Register index = a0;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003055 Register result = v0;
3056
3057 __ pop(object);
3058
3059 Label need_conversion;
3060 Label index_out_of_range;
3061 Label done;
3062 StringCharCodeAtGenerator generator(object,
3063 index,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003064 result,
3065 &need_conversion,
3066 &need_conversion,
3067 &index_out_of_range,
3068 STRING_INDEX_IS_NUMBER);
3069 generator.GenerateFast(masm_);
3070 __ jmp(&done);
3071
3072 __ bind(&index_out_of_range);
3073 // When the index is out of range, the spec requires us to return
3074 // NaN.
3075 __ LoadRoot(result, Heap::kNanValueRootIndex);
3076 __ jmp(&done);
3077
3078 __ bind(&need_conversion);
3079 // Load the undefined value into the result register, which will
3080 // trigger conversion.
3081 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3082 __ jmp(&done);
3083
3084 NopRuntimeCallHelper call_helper;
3085 generator.GenerateSlow(masm_, call_helper);
3086
3087 __ bind(&done);
3088 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003089}
3090
3091
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003092void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3093 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003094 ASSERT(args->length() == 2);
3095
3096 VisitForStackValue(args->at(0));
3097 VisitForAccumulatorValue(args->at(1));
3098 __ mov(a0, result_register());
3099
3100 Register object = a1;
3101 Register index = a0;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003102 Register scratch = a3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003103 Register result = v0;
3104
3105 __ pop(object);
3106
3107 Label need_conversion;
3108 Label index_out_of_range;
3109 Label done;
3110 StringCharAtGenerator generator(object,
3111 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003112 scratch,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003113 result,
3114 &need_conversion,
3115 &need_conversion,
3116 &index_out_of_range,
3117 STRING_INDEX_IS_NUMBER);
3118 generator.GenerateFast(masm_);
3119 __ jmp(&done);
3120
3121 __ bind(&index_out_of_range);
3122 // When the index is out of range, the spec requires us to return
3123 // the empty string.
3124 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3125 __ jmp(&done);
3126
3127 __ bind(&need_conversion);
3128 // Move smi zero into the result register, which will trigger
3129 // conversion.
3130 __ li(result, Operand(Smi::FromInt(0)));
3131 __ jmp(&done);
3132
3133 NopRuntimeCallHelper call_helper;
3134 generator.GenerateSlow(masm_, call_helper);
3135
3136 __ bind(&done);
3137 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003138}
3139
3140
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003141void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3142 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003143 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003144 VisitForStackValue(args->at(0));
3145 VisitForStackValue(args->at(1));
3146
3147 StringAddStub stub(NO_STRING_ADD_FLAGS);
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::EmitStringCompare(CallRuntime* expr) {
3154 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003155 ASSERT_EQ(2, args->length());
3156
3157 VisitForStackValue(args->at(0));
3158 VisitForStackValue(args->at(1));
3159
3160 StringCompareStub stub;
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::EmitMathSin(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::SIN,
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
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003179void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003180 // Load the argument on the stack and call the stub.
3181 TranscendentalCacheStub stub(TranscendentalCache::COS,
3182 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003183 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003184 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);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003189}
3190
3191
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003192void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3193 // Load the argument on the stack and call the stub.
3194 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3195 TranscendentalCacheStub::TAGGED);
3196 ZoneList<Expression*>* args = expr->arguments();
3197 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::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003206 // Load the argument on the stack and call the stub.
3207 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3208 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003209 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003210 ASSERT(args->length() == 1);
3211 VisitForStackValue(args->at(0));
3212 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3213 __ CallStub(&stub);
3214 context()->Plug(v0);
3215}
3216
3217
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003218void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003219 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003220 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003221 ASSERT(args->length() == 1);
3222 VisitForStackValue(args->at(0));
3223 __ CallRuntime(Runtime::kMath_sqrt, 1);
3224 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003225}
3226
3227
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003228void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3229 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003230 ASSERT(args->length() >= 2);
3231
3232 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3233 for (int i = 0; i < arg_count + 1; i++) {
3234 VisitForStackValue(args->at(i));
3235 }
3236 VisitForAccumulatorValue(args->last()); // Function.
3237
danno@chromium.orgc612e022011-11-10 11:38:15 +00003238 // Check for proxy.
3239 Label proxy, done;
3240 __ GetObjectType(v0, a1, a1);
3241 __ Branch(&proxy, eq, a1, Operand(JS_FUNCTION_PROXY_TYPE));
3242
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003243 // InvokeFunction requires the function in a1. Move it in there.
3244 __ mov(a1, result_register());
3245 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003246 __ InvokeFunction(a1, count, CALL_FUNCTION,
3247 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003248 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003249 __ jmp(&done);
3250
3251 __ bind(&proxy);
3252 __ push(v0);
3253 __ CallRuntime(Runtime::kCall, args->length());
3254 __ bind(&done);
3255
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003256 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003257}
3258
3259
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003260void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003261 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003262 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003263 ASSERT(args->length() == 3);
3264 VisitForStackValue(args->at(0));
3265 VisitForStackValue(args->at(1));
3266 VisitForStackValue(args->at(2));
3267 __ CallStub(&stub);
3268 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003269}
3270
3271
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003272void FullCodeGenerator::EmitSwapElements(CallRuntime* expr) {
3273 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003274 ASSERT(args->length() == 3);
3275 VisitForStackValue(args->at(0));
3276 VisitForStackValue(args->at(1));
3277 VisitForStackValue(args->at(2));
3278 Label done;
3279 Label slow_case;
3280 Register object = a0;
3281 Register index1 = a1;
3282 Register index2 = a2;
3283 Register elements = a3;
3284 Register scratch1 = t0;
3285 Register scratch2 = t1;
3286
3287 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3288 // Fetch the map and check if array is in fast case.
3289 // Check that object doesn't require security checks and
3290 // has no indexed interceptor.
3291 __ GetObjectType(object, scratch1, scratch2);
3292 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3293 // Map is now in scratch1.
3294
3295 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3296 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3297 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3298
3299 // Check the object's elements are in fast case and writable.
3300 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3301 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3302 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3303 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3304
3305 // Check that both indices are smis.
3306 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3307 __ lw(index2, MemOperand(sp, 0));
3308 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3309
3310 // Check that both indices are valid.
3311 Label not_hi;
3312 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3313 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3314 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3315 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3316 __ bind(&not_hi);
3317
3318 // Bring the address of the elements into index1 and index2.
3319 __ Addu(scratch1, elements,
3320 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3321 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3322 __ Addu(index1, scratch1, index1);
3323 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3324 __ Addu(index2, scratch1, index2);
3325
3326 // Swap elements.
3327 __ lw(scratch1, MemOperand(index1, 0));
3328 __ lw(scratch2, MemOperand(index2, 0));
3329 __ sw(scratch1, MemOperand(index2, 0));
3330 __ sw(scratch2, MemOperand(index1, 0));
3331
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003332 Label no_remembered_set;
3333 __ CheckPageFlag(elements,
3334 scratch1,
3335 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3336 ne,
3337 &no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003338 // Possible optimization: do a check that both values are Smis
3339 // (or them and test against Smi mask).
3340
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003341 // We are swapping two objects in an array and the incremental marker never
3342 // pauses in the middle of scanning a single object. Therefore the
3343 // incremental marker is not disturbed, so we don't need to call the
3344 // RecordWrite stub that notifies the incremental marker.
3345 __ RememberedSetHelper(elements,
3346 index1,
3347 scratch2,
3348 kDontSaveFPRegs,
3349 MacroAssembler::kFallThroughAtEnd);
3350 __ RememberedSetHelper(elements,
3351 index2,
3352 scratch2,
3353 kDontSaveFPRegs,
3354 MacroAssembler::kFallThroughAtEnd);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003355
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003356 __ bind(&no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003357 // We are done. Drop elements from the stack, and return undefined.
3358 __ Drop(3);
3359 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3360 __ jmp(&done);
3361
3362 __ bind(&slow_case);
3363 __ CallRuntime(Runtime::kSwapElements, 3);
3364
3365 __ bind(&done);
3366 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003367}
3368
3369
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003370void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3371 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003372 ASSERT_EQ(2, args->length());
3373
3374 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3375 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3376
3377 Handle<FixedArray> jsfunction_result_caches(
3378 isolate()->global_context()->jsfunction_result_caches());
3379 if (jsfunction_result_caches->length() <= cache_id) {
3380 __ Abort("Attempt to use undefined cache.");
3381 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3382 context()->Plug(v0);
3383 return;
3384 }
3385
3386 VisitForAccumulatorValue(args->at(1));
3387
3388 Register key = v0;
3389 Register cache = a1;
3390 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3391 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3392 __ lw(cache,
3393 ContextOperand(
3394 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3395 __ lw(cache,
3396 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3397
3398
3399 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003400 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003401 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3402 // a2 now holds finger offset as a smi.
3403 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3404 // a3 now points to the start of fixed array elements.
3405 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3406 __ addu(a3, a3, at);
3407 // a3 now points to key of indexed element of cache.
3408 __ lw(a2, MemOperand(a3));
3409 __ Branch(&not_found, ne, key, Operand(a2));
3410
3411 __ lw(v0, MemOperand(a3, kPointerSize));
3412 __ Branch(&done);
3413
3414 __ bind(&not_found);
3415 // Call runtime to perform the lookup.
3416 __ Push(cache, key);
3417 __ CallRuntime(Runtime::kGetFromCache, 2);
3418
3419 __ bind(&done);
3420 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003421}
3422
3423
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003424void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3425 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003426 ASSERT_EQ(2, args->length());
3427
3428 Register right = v0;
3429 Register left = a1;
3430 Register tmp = a2;
3431 Register tmp2 = a3;
3432
3433 VisitForStackValue(args->at(0));
3434 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3435 __ pop(left);
3436
3437 Label done, fail, ok;
3438 __ Branch(&ok, eq, left, Operand(right));
3439 // Fail if either is a non-HeapObject.
3440 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003441 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003442 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3443 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3444 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3445 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3446 __ Branch(&fail, ne, tmp, Operand(tmp2));
3447 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3448 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3449 __ Branch(&ok, eq, tmp, Operand(tmp2));
3450 __ bind(&fail);
3451 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3452 __ jmp(&done);
3453 __ bind(&ok);
3454 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3455 __ bind(&done);
3456
3457 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003458}
3459
3460
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003461void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3462 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003463 VisitForAccumulatorValue(args->at(0));
3464
3465 Label materialize_true, materialize_false;
3466 Label* if_true = NULL;
3467 Label* if_false = NULL;
3468 Label* fall_through = NULL;
3469 context()->PrepareTest(&materialize_true, &materialize_false,
3470 &if_true, &if_false, &fall_through);
3471
3472 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3473 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3474
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003475 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003476 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3477
3478 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003479}
3480
3481
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003482void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3483 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003484 ASSERT(args->length() == 1);
3485 VisitForAccumulatorValue(args->at(0));
3486
3487 if (FLAG_debug_code) {
3488 __ AbortIfNotString(v0);
3489 }
3490
3491 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3492 __ IndexFromHash(v0, v0);
3493
3494 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003495}
3496
3497
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003498void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003499 Label bailout, done, one_char_separator, long_separator,
3500 non_trivial_array, not_size_one_array, loop,
3501 empty_separator_loop, one_char_separator_loop,
3502 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003503 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003504 ASSERT(args->length() == 2);
3505 VisitForStackValue(args->at(1));
3506 VisitForAccumulatorValue(args->at(0));
3507
3508 // All aliases of the same register have disjoint lifetimes.
3509 Register array = v0;
3510 Register elements = no_reg; // Will be v0.
3511 Register result = no_reg; // Will be v0.
3512 Register separator = a1;
3513 Register array_length = a2;
3514 Register result_pos = no_reg; // Will be a2.
3515 Register string_length = a3;
3516 Register string = t0;
3517 Register element = t1;
3518 Register elements_end = t2;
3519 Register scratch1 = t3;
3520 Register scratch2 = t5;
3521 Register scratch3 = t4;
3522 Register scratch4 = v1;
3523
3524 // Separator operand is on the stack.
3525 __ pop(separator);
3526
3527 // Check that the array is a JSArray.
3528 __ JumpIfSmi(array, &bailout);
3529 __ GetObjectType(array, scratch1, scratch2);
3530 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3531
3532 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003533 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003534
3535 // If the array has length zero, return the empty string.
3536 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3537 __ SmiUntag(array_length);
3538 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3539 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3540 __ Branch(&done);
3541
3542 __ bind(&non_trivial_array);
3543
3544 // Get the FixedArray containing array's elements.
3545 elements = array;
3546 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3547 array = no_reg; // End of array's live range.
3548
3549 // Check that all array elements are sequential ASCII strings, and
3550 // accumulate the sum of their lengths, as a smi-encoded value.
3551 __ mov(string_length, zero_reg);
3552 __ Addu(element,
3553 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3554 __ sll(elements_end, array_length, kPointerSizeLog2);
3555 __ Addu(elements_end, element, elements_end);
3556 // Loop condition: while (element < elements_end).
3557 // Live values in registers:
3558 // elements: Fixed array of strings.
3559 // array_length: Length of the fixed array of strings (not smi)
3560 // separator: Separator string
3561 // string_length: Accumulated sum of string lengths (smi).
3562 // element: Current array element.
3563 // elements_end: Array end.
3564 if (FLAG_debug_code) {
3565 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3566 array_length, Operand(zero_reg));
3567 }
3568 __ bind(&loop);
3569 __ lw(string, MemOperand(element));
3570 __ Addu(element, element, kPointerSize);
3571 __ JumpIfSmi(string, &bailout);
3572 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3573 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3574 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3575 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3576 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3577 __ BranchOnOverflow(&bailout, scratch3);
3578 __ Branch(&loop, lt, element, Operand(elements_end));
3579
3580 // If array_length is 1, return elements[0], a string.
3581 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3582 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3583 __ Branch(&done);
3584
3585 __ bind(&not_size_one_array);
3586
3587 // Live values in registers:
3588 // separator: Separator string
3589 // array_length: Length of the array.
3590 // string_length: Sum of string lengths (smi).
3591 // elements: FixedArray of strings.
3592
3593 // Check that the separator is a flat ASCII string.
3594 __ JumpIfSmi(separator, &bailout);
3595 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3596 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3597 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3598
3599 // Add (separator length times array_length) - separator length to the
3600 // string_length to get the length of the result string. array_length is not
3601 // smi but the other values are, so the result is a smi.
3602 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3603 __ Subu(string_length, string_length, Operand(scratch1));
3604 __ Mult(array_length, scratch1);
3605 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3606 // zero.
3607 __ mfhi(scratch2);
3608 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3609 __ mflo(scratch2);
3610 __ And(scratch3, scratch2, Operand(0x80000000));
3611 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3612 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3613 __ BranchOnOverflow(&bailout, scratch3);
3614 __ SmiUntag(string_length);
3615
3616 // Get first element in the array to free up the elements register to be used
3617 // for the result.
3618 __ Addu(element,
3619 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3620 result = elements; // End of live range for elements.
3621 elements = no_reg;
3622 // Live values in registers:
3623 // element: First array element
3624 // separator: Separator string
3625 // string_length: Length of result string (not smi)
3626 // array_length: Length of the array.
3627 __ AllocateAsciiString(result,
3628 string_length,
3629 scratch1,
3630 scratch2,
3631 elements_end,
3632 &bailout);
3633 // Prepare for looping. Set up elements_end to end of the array. Set
3634 // result_pos to the position of the result where to write the first
3635 // character.
3636 __ sll(elements_end, array_length, kPointerSizeLog2);
3637 __ Addu(elements_end, element, elements_end);
3638 result_pos = array_length; // End of live range for array_length.
3639 array_length = no_reg;
3640 __ Addu(result_pos,
3641 result,
3642 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3643
3644 // Check the length of the separator.
3645 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3646 __ li(at, Operand(Smi::FromInt(1)));
3647 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3648 __ Branch(&long_separator, gt, scratch1, Operand(at));
3649
3650 // Empty separator case.
3651 __ bind(&empty_separator_loop);
3652 // Live values in registers:
3653 // result_pos: the position to which we are currently copying characters.
3654 // element: Current array element.
3655 // elements_end: Array end.
3656
3657 // Copy next array element to the result.
3658 __ lw(string, MemOperand(element));
3659 __ Addu(element, element, kPointerSize);
3660 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3661 __ SmiUntag(string_length);
3662 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3663 __ CopyBytes(string, result_pos, string_length, scratch1);
3664 // End while (element < elements_end).
3665 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3666 ASSERT(result.is(v0));
3667 __ Branch(&done);
3668
3669 // One-character separator case.
3670 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003671 // Replace separator with its ASCII character value.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003672 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3673 // Jump into the loop after the code that copies the separator, so the first
3674 // element is not preceded by a separator.
3675 __ jmp(&one_char_separator_loop_entry);
3676
3677 __ bind(&one_char_separator_loop);
3678 // Live values in registers:
3679 // result_pos: the position to which we are currently copying characters.
3680 // element: Current array element.
3681 // elements_end: Array end.
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003682 // separator: Single separator ASCII char (in lower byte).
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003683
3684 // Copy the separator character to the result.
3685 __ sb(separator, MemOperand(result_pos));
3686 __ Addu(result_pos, result_pos, 1);
3687
3688 // Copy next array element to the result.
3689 __ bind(&one_char_separator_loop_entry);
3690 __ lw(string, MemOperand(element));
3691 __ Addu(element, element, kPointerSize);
3692 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3693 __ SmiUntag(string_length);
3694 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3695 __ CopyBytes(string, result_pos, string_length, scratch1);
3696 // End while (element < elements_end).
3697 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3698 ASSERT(result.is(v0));
3699 __ Branch(&done);
3700
3701 // Long separator case (separator is more than one character). Entry is at the
3702 // label long_separator below.
3703 __ bind(&long_separator_loop);
3704 // Live values in registers:
3705 // result_pos: the position to which we are currently copying characters.
3706 // element: Current array element.
3707 // elements_end: Array end.
3708 // separator: Separator string.
3709
3710 // Copy the separator to the result.
3711 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3712 __ SmiUntag(string_length);
3713 __ Addu(string,
3714 separator,
3715 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3716 __ CopyBytes(string, result_pos, string_length, scratch1);
3717
3718 __ bind(&long_separator);
3719 __ lw(string, MemOperand(element));
3720 __ Addu(element, element, kPointerSize);
3721 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3722 __ SmiUntag(string_length);
3723 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3724 __ CopyBytes(string, result_pos, string_length, scratch1);
3725 // End while (element < elements_end).
3726 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3727 ASSERT(result.is(v0));
3728 __ Branch(&done);
3729
3730 __ bind(&bailout);
3731 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3732 __ bind(&done);
3733 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003734}
3735
3736
ager@chromium.org5c838252010-02-19 08:53:10 +00003737void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003738 Handle<String> name = expr->name();
3739 if (name->length() > 0 && name->Get(0) == '_') {
3740 Comment cmnt(masm_, "[ InlineRuntimeCall");
3741 EmitInlineRuntimeCall(expr);
3742 return;
3743 }
3744
3745 Comment cmnt(masm_, "[ CallRuntime");
3746 ZoneList<Expression*>* args = expr->arguments();
3747
3748 if (expr->is_jsruntime()) {
3749 // Prepare for calling JS runtime function.
3750 __ lw(a0, GlobalObjectOperand());
3751 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3752 __ push(a0);
3753 }
3754
3755 // Push the arguments ("left-to-right").
3756 int arg_count = args->length();
3757 for (int i = 0; i < arg_count; i++) {
3758 VisitForStackValue(args->at(i));
3759 }
3760
3761 if (expr->is_jsruntime()) {
3762 // Call the JS runtime function.
3763 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003764 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003765 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003766 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003767 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003768 // Restore context register.
3769 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3770 } else {
3771 // Call the C runtime function.
3772 __ CallRuntime(expr->function(), arg_count);
3773 }
3774 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003775}
3776
3777
3778void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003779 switch (expr->op()) {
3780 case Token::DELETE: {
3781 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003782 Property* property = expr->expression()->AsProperty();
3783 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003784
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003785 if (property != NULL) {
3786 VisitForStackValue(property->obj());
3787 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003788 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
3789 ? kNonStrictMode : kStrictMode;
3790 __ li(a1, Operand(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003791 __ push(a1);
3792 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3793 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003794 } else if (proxy != NULL) {
3795 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003796 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003797 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003798 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003799 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003800 __ lw(a2, GlobalObjectOperand());
3801 __ li(a1, Operand(var->name()));
3802 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3803 __ Push(a2, a1, a0);
3804 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3805 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003806 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003807 // Result of deleting non-global, non-dynamic variables is false.
3808 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003809 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003810 } else {
3811 // Non-global variable. Call the runtime to try to delete from the
3812 // context where the variable was introduced.
3813 __ push(context_register());
3814 __ li(a2, Operand(var->name()));
3815 __ push(a2);
3816 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3817 context()->Plug(v0);
3818 }
3819 } else {
3820 // Result of deleting non-property, non-variable reference is true.
3821 // The subexpression may have side effects.
3822 VisitForEffect(expr->expression());
3823 context()->Plug(true);
3824 }
3825 break;
3826 }
3827
3828 case Token::VOID: {
3829 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3830 VisitForEffect(expr->expression());
3831 context()->Plug(Heap::kUndefinedValueRootIndex);
3832 break;
3833 }
3834
3835 case Token::NOT: {
3836 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3837 if (context()->IsEffect()) {
3838 // Unary NOT has no side effects so it's only necessary to visit the
3839 // subexpression. Match the optimizing compiler by not branching.
3840 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003841 } else if (context()->IsTest()) {
3842 const TestContext* test = TestContext::cast(context());
3843 // The labels are swapped for the recursive call.
3844 VisitForControl(expr->expression(),
3845 test->false_label(),
3846 test->true_label(),
3847 test->fall_through());
3848 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003849 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003850 // We handle value contexts explicitly rather than simply visiting
3851 // for control and plugging the control flow into the context,
3852 // because we need to prepare a pair of extra administrative AST ids
3853 // for the optimizing compiler.
3854 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3855 Label materialize_true, materialize_false, done;
3856 VisitForControl(expr->expression(),
3857 &materialize_false,
3858 &materialize_true,
3859 &materialize_true);
3860 __ bind(&materialize_true);
3861 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3862 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3863 if (context()->IsStackValue()) __ push(v0);
3864 __ jmp(&done);
3865 __ bind(&materialize_false);
3866 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3867 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3868 if (context()->IsStackValue()) __ push(v0);
3869 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003870 }
3871 break;
3872 }
3873
3874 case Token::TYPEOF: {
3875 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3876 { StackValueContext context(this);
3877 VisitForTypeofValue(expr->expression());
3878 }
3879 __ CallRuntime(Runtime::kTypeof, 1);
3880 context()->Plug(v0);
3881 break;
3882 }
3883
3884 case Token::ADD: {
3885 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3886 VisitForAccumulatorValue(expr->expression());
3887 Label no_conversion;
3888 __ JumpIfSmi(result_register(), &no_conversion);
3889 __ mov(a0, result_register());
3890 ToNumberStub convert_stub;
3891 __ CallStub(&convert_stub);
3892 __ bind(&no_conversion);
3893 context()->Plug(result_register());
3894 break;
3895 }
3896
3897 case Token::SUB:
3898 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3899 break;
3900
3901 case Token::BIT_NOT:
3902 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3903 break;
3904
3905 default:
3906 UNREACHABLE();
3907 }
3908}
3909
3910
3911void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3912 const char* comment) {
3913 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3914 Comment cmt(masm_, comment);
3915 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3916 UnaryOverwriteMode overwrite =
3917 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003918 UnaryOpStub stub(expr->op(), overwrite);
3919 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003920 VisitForAccumulatorValue(expr->expression());
3921 SetSourcePosition(expr->position());
3922 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003923 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003924 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003925}
3926
3927
3928void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003929 Comment cmnt(masm_, "[ CountOperation");
3930 SetSourcePosition(expr->position());
3931
3932 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3933 // as the left-hand side.
3934 if (!expr->expression()->IsValidLeftHandSide()) {
3935 VisitForEffect(expr->expression());
3936 return;
3937 }
3938
3939 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003940 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003941 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3942 LhsKind assign_type = VARIABLE;
3943 Property* prop = expr->expression()->AsProperty();
3944 // In case of a property we use the uninitialized expression context
3945 // of the key to detect a named property.
3946 if (prop != NULL) {
3947 assign_type =
3948 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3949 }
3950
3951 // Evaluate expression and get value.
3952 if (assign_type == VARIABLE) {
3953 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3954 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003955 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003956 } else {
3957 // Reserve space for result of postfix operation.
3958 if (expr->is_postfix() && !context()->IsEffect()) {
3959 __ li(at, Operand(Smi::FromInt(0)));
3960 __ push(at);
3961 }
3962 if (assign_type == NAMED_PROPERTY) {
3963 // Put the object both on the stack and in the accumulator.
3964 VisitForAccumulatorValue(prop->obj());
3965 __ push(v0);
3966 EmitNamedPropertyLoad(prop);
3967 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003968 VisitForStackValue(prop->obj());
3969 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003970 __ lw(a1, MemOperand(sp, 0));
3971 __ push(v0);
3972 EmitKeyedPropertyLoad(prop);
3973 }
3974 }
3975
3976 // We need a second deoptimization point after loading the value
3977 // in case evaluating the property load my have a side effect.
3978 if (assign_type == VARIABLE) {
3979 PrepareForBailout(expr->expression(), TOS_REG);
3980 } else {
3981 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3982 }
3983
3984 // Call ToNumber only if operand is not a smi.
3985 Label no_conversion;
3986 __ JumpIfSmi(v0, &no_conversion);
3987 __ mov(a0, v0);
3988 ToNumberStub convert_stub;
3989 __ CallStub(&convert_stub);
3990 __ bind(&no_conversion);
3991
3992 // Save result for postfix expressions.
3993 if (expr->is_postfix()) {
3994 if (!context()->IsEffect()) {
3995 // Save the result on the stack. If we have a named or keyed property
3996 // we store the result under the receiver that is currently on top
3997 // of the stack.
3998 switch (assign_type) {
3999 case VARIABLE:
4000 __ push(v0);
4001 break;
4002 case NAMED_PROPERTY:
4003 __ sw(v0, MemOperand(sp, kPointerSize));
4004 break;
4005 case KEYED_PROPERTY:
4006 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4007 break;
4008 }
4009 }
4010 }
4011 __ mov(a0, result_register());
4012
4013 // Inline smi case if we are in a loop.
4014 Label stub_call, done;
4015 JumpPatchSite patch_site(masm_);
4016
4017 int count_value = expr->op() == Token::INC ? 1 : -1;
4018 __ li(a1, Operand(Smi::FromInt(count_value)));
4019
4020 if (ShouldInlineSmiCase(expr->op())) {
4021 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
4022 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
4023
4024 // We could eliminate this smi check if we split the code at
4025 // the first smi check before calling ToNumber.
4026 patch_site.EmitJumpIfSmi(v0, &done);
4027 __ bind(&stub_call);
4028 }
4029
4030 // Record position before stub call.
4031 SetSourcePosition(expr->position());
4032
danno@chromium.org40cb8782011-05-25 07:58:50 +00004033 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004034 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004035 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004036 __ bind(&done);
4037
4038 // Store the value returned in v0.
4039 switch (assign_type) {
4040 case VARIABLE:
4041 if (expr->is_postfix()) {
4042 { EffectContext context(this);
4043 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4044 Token::ASSIGN);
4045 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4046 context.Plug(v0);
4047 }
4048 // For all contexts except EffectConstant we have the result on
4049 // top of the stack.
4050 if (!context()->IsEffect()) {
4051 context()->PlugTOS();
4052 }
4053 } else {
4054 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4055 Token::ASSIGN);
4056 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4057 context()->Plug(v0);
4058 }
4059 break;
4060 case NAMED_PROPERTY: {
4061 __ mov(a0, result_register()); // Value.
4062 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4063 __ pop(a1); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004064 Handle<Code> ic = is_classic_mode()
4065 ? isolate()->builtins()->StoreIC_Initialize()
4066 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004067 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004068 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4069 if (expr->is_postfix()) {
4070 if (!context()->IsEffect()) {
4071 context()->PlugTOS();
4072 }
4073 } else {
4074 context()->Plug(v0);
4075 }
4076 break;
4077 }
4078 case KEYED_PROPERTY: {
4079 __ mov(a0, result_register()); // Value.
4080 __ pop(a1); // Key.
4081 __ pop(a2); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004082 Handle<Code> ic = is_classic_mode()
4083 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4084 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004085 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004086 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4087 if (expr->is_postfix()) {
4088 if (!context()->IsEffect()) {
4089 context()->PlugTOS();
4090 }
4091 } else {
4092 context()->Plug(v0);
4093 }
4094 break;
4095 }
4096 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004097}
4098
4099
lrn@chromium.org7516f052011-03-30 08:52:27 +00004100void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004101 ASSERT(!context()->IsEffect());
4102 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004103 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004104 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004105 Comment cmnt(masm_, "Global variable");
4106 __ lw(a0, GlobalObjectOperand());
4107 __ li(a2, Operand(proxy->name()));
4108 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4109 // Use a regular load, not a contextual load, to avoid a reference
4110 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004111 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004112 PrepareForBailout(expr, TOS_REG);
4113 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004114 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004115 Label done, slow;
4116
4117 // Generate code for loading from variables potentially shadowed
4118 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004119 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004120
4121 __ bind(&slow);
4122 __ li(a0, Operand(proxy->name()));
4123 __ Push(cp, a0);
4124 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4125 PrepareForBailout(expr, TOS_REG);
4126 __ bind(&done);
4127
4128 context()->Plug(v0);
4129 } else {
4130 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004131 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004132 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004133}
4134
ager@chromium.org04921a82011-06-27 13:21:41 +00004135void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004136 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004137 Handle<String> check) {
4138 Label materialize_true, materialize_false;
4139 Label* if_true = NULL;
4140 Label* if_false = NULL;
4141 Label* fall_through = NULL;
4142 context()->PrepareTest(&materialize_true, &materialize_false,
4143 &if_true, &if_false, &fall_through);
4144
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004145 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004146 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004147 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004148 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004149
4150 if (check->Equals(isolate()->heap()->number_symbol())) {
4151 __ JumpIfSmi(v0, if_true);
4152 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4153 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4154 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4155 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4156 __ JumpIfSmi(v0, if_false);
4157 // Check for undetectable objects => false.
4158 __ GetObjectType(v0, v0, a1);
4159 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4160 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4161 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4162 Split(eq, a1, Operand(zero_reg),
4163 if_true, if_false, fall_through);
4164 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4165 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4166 __ Branch(if_true, eq, v0, Operand(at));
4167 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4168 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004169 } else if (FLAG_harmony_typeof &&
4170 check->Equals(isolate()->heap()->null_symbol())) {
4171 __ LoadRoot(at, Heap::kNullValueRootIndex);
4172 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004173 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4174 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4175 __ Branch(if_true, eq, v0, Operand(at));
4176 __ JumpIfSmi(v0, if_false);
4177 // Check for undetectable objects => true.
4178 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4179 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4180 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4181 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4182 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4183 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004184 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4185 __ GetObjectType(v0, v0, a1);
4186 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4187 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4188 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004189 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4190 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004191 if (!FLAG_harmony_typeof) {
4192 __ LoadRoot(at, Heap::kNullValueRootIndex);
4193 __ Branch(if_true, eq, v0, Operand(at));
4194 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004195 // Check for JS objects => true.
4196 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004197 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004198 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004199 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004200 // Check for undetectable objects => false.
4201 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4202 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4203 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4204 } else {
4205 if (if_false != fall_through) __ jmp(if_false);
4206 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004207 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004208}
4209
4210
ager@chromium.org5c838252010-02-19 08:53:10 +00004211void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004212 Comment cmnt(masm_, "[ CompareOperation");
4213 SetSourcePosition(expr->position());
4214
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004215 // First we try a fast inlined version of the compare when one of
4216 // the operands is a literal.
4217 if (TryLiteralCompare(expr)) return;
4218
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004219 // Always perform the comparison for its control flow. Pack the result
4220 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004221 Label materialize_true, materialize_false;
4222 Label* if_true = NULL;
4223 Label* if_false = NULL;
4224 Label* fall_through = NULL;
4225 context()->PrepareTest(&materialize_true, &materialize_false,
4226 &if_true, &if_false, &fall_through);
4227
ager@chromium.org04921a82011-06-27 13:21:41 +00004228 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004229 VisitForStackValue(expr->left());
4230 switch (op) {
4231 case Token::IN:
4232 VisitForStackValue(expr->right());
4233 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004234 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004235 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4236 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4237 break;
4238
4239 case Token::INSTANCEOF: {
4240 VisitForStackValue(expr->right());
4241 InstanceofStub stub(InstanceofStub::kNoFlags);
4242 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004243 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004244 // The stub returns 0 for true.
4245 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4246 break;
4247 }
4248
4249 default: {
4250 VisitForAccumulatorValue(expr->right());
4251 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004252 switch (op) {
4253 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004254 case Token::EQ:
4255 cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004256 break;
4257 case Token::LT:
4258 cc = lt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004259 break;
4260 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004261 cc = gt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004262 break;
4263 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004264 cc = le;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004265 break;
4266 case Token::GTE:
4267 cc = ge;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004268 break;
4269 case Token::IN:
4270 case Token::INSTANCEOF:
4271 default:
4272 UNREACHABLE();
4273 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004274 __ mov(a0, result_register());
4275 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004276
4277 bool inline_smi_code = ShouldInlineSmiCase(op);
4278 JumpPatchSite patch_site(masm_);
4279 if (inline_smi_code) {
4280 Label slow_case;
4281 __ Or(a2, a0, Operand(a1));
4282 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4283 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4284 __ bind(&slow_case);
4285 }
4286 // Record position and call the compare IC.
4287 SetSourcePosition(expr->position());
4288 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004289 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004290 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004291 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004292 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4293 }
4294 }
4295
4296 // Convert the result of the comparison into one expected for this
4297 // expression's context.
4298 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004299}
4300
4301
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004302void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4303 Expression* sub_expr,
4304 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004305 Label materialize_true, materialize_false;
4306 Label* if_true = NULL;
4307 Label* if_false = NULL;
4308 Label* fall_through = NULL;
4309 context()->PrepareTest(&materialize_true, &materialize_false,
4310 &if_true, &if_false, &fall_through);
4311
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004312 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004313 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004314 Heap::RootListIndex nil_value = nil == kNullValue ?
4315 Heap::kNullValueRootIndex :
4316 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004317 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004318 __ LoadRoot(a1, nil_value);
4319 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004320 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4321 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004322 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4323 Heap::kUndefinedValueRootIndex :
4324 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004325 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004326 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004327 __ Branch(if_true, eq, a0, Operand(a1));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004328 __ JumpIfSmi(a0, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004329 // It can be an undetectable object.
4330 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4331 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4332 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4333 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4334 }
4335 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004336}
4337
4338
ager@chromium.org5c838252010-02-19 08:53:10 +00004339void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004340 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4341 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004342}
4343
4344
lrn@chromium.org7516f052011-03-30 08:52:27 +00004345Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004346 return v0;
4347}
ager@chromium.org5c838252010-02-19 08:53:10 +00004348
4349
lrn@chromium.org7516f052011-03-30 08:52:27 +00004350Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004351 return cp;
4352}
4353
4354
ager@chromium.org5c838252010-02-19 08:53:10 +00004355void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004356 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4357 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004358}
4359
4360
4361void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004362 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004363}
4364
4365
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004366void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4367 Scope* declaration_scope = scope()->DeclarationScope();
4368 if (declaration_scope->is_global_scope()) {
4369 // Contexts nested in the global context have a canonical empty function
4370 // as their closure, not the anonymous closure containing the global
4371 // code. Pass a smi sentinel and let the runtime look up the empty
4372 // function.
4373 __ li(at, Operand(Smi::FromInt(0)));
4374 } else if (declaration_scope->is_eval_scope()) {
4375 // Contexts created by a call to eval have the same closure as the
4376 // context calling eval, not the anonymous closure containing the eval
4377 // code. Fetch it from the context.
4378 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4379 } else {
4380 ASSERT(declaration_scope->is_function_scope());
4381 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4382 }
4383 __ push(at);
4384}
4385
4386
ager@chromium.org5c838252010-02-19 08:53:10 +00004387// ----------------------------------------------------------------------------
4388// Non-local control flow support.
4389
4390void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004391 ASSERT(!result_register().is(a1));
4392 // Store result register while executing finally block.
4393 __ push(result_register());
4394 // Cook return address in link register to stack (smi encoded Code* delta).
4395 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4396 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004397 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004398 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4399 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004400}
4401
4402
4403void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004404 ASSERT(!result_register().is(a1));
4405 // Restore result register from stack.
4406 __ pop(a1);
4407 // Uncook return address and return.
4408 __ pop(result_register());
4409 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4410 __ sra(a1, a1, 1); // Un-smi-tag value.
4411 __ Addu(at, a1, Operand(masm_->CodeObject()));
4412 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004413}
4414
4415
4416#undef __
4417
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004418#define __ ACCESS_MASM(masm())
4419
4420FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4421 int* stack_depth,
4422 int* context_length) {
4423 // The macros used here must preserve the result register.
4424
4425 // Because the handler block contains the context of the finally
4426 // code, we can restore it directly from there for the finally code
4427 // rather than iteratively unwinding contexts via their previous
4428 // links.
4429 __ Drop(*stack_depth); // Down to the handler block.
4430 if (*context_length > 0) {
4431 // Restore the context to its dedicated register and the stack.
4432 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4433 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4434 }
4435 __ PopTryHandler();
4436 __ Call(finally_entry_);
4437
4438 *stack_depth = 0;
4439 *context_length = 0;
4440 return previous_;
4441}
4442
4443
4444#undef __
4445
ager@chromium.org5c838252010-02-19 08:53:10 +00004446} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004447
4448#endif // V8_TARGET_ARCH_MIPS