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