blob: c5b69cc2f0873d13346affd394a00e8853e64e01 [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();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000140 SetFunctionPosition(function());
141 Comment cmnt(masm_, "[ function compiled by full code generator");
142
143#ifdef DEBUG
144 if (strlen(FLAG_stop_at) > 0 &&
145 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
146 __ stop("stop-at");
147 }
148#endif
149
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000150 // Strict mode functions and builtins need to replace the receiver
151 // with undefined when called as functions (without an explicit
152 // receiver object). t1 is zero for method calls and non-zero for
153 // function calls.
154 if (info->is_strict_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000155 Label ok;
156 __ Branch(&ok, eq, t1, Operand(zero_reg));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000157 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000158 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
159 __ sw(a2, MemOperand(sp, receiver_offset));
160 __ bind(&ok);
161 }
162
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000163 // Open a frame scope to indicate that there is a frame on the stack. The
164 // MANUAL indicates that the scope shouldn't actually generate code to set up
165 // the frame (that is done below).
166 FrameScope frame_scope(masm_, StackFrame::MANUAL);
167
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000168 int locals_count = info->scope()->num_stack_slots();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000169
170 __ Push(ra, fp, cp, a1);
171 if (locals_count > 0) {
172 // Load undefined value here, so the value is ready for the loop
173 // below.
174 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
175 }
176 // Adjust fp to point to caller's fp.
177 __ Addu(fp, sp, Operand(2 * kPointerSize));
178
179 { Comment cmnt(masm_, "[ Allocate locals");
180 for (int i = 0; i < locals_count; i++) {
181 __ push(at);
182 }
183 }
184
185 bool function_in_register = true;
186
187 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000188 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000189 if (heap_slots > 0) {
190 Comment cmnt(masm_, "[ Allocate local context");
191 // Argument to NewContext is the function, which is in a1.
192 __ push(a1);
193 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
194 FastNewContextStub stub(heap_slots);
195 __ CallStub(&stub);
196 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000197 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000198 }
199 function_in_register = false;
200 // Context is returned in both v0 and cp. It replaces the context
201 // passed to us. It's saved in the stack and kept live in cp.
202 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
203 // Copy any necessary parameters into the context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000204 int num_parameters = info->scope()->num_parameters();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000205 for (int i = 0; i < num_parameters; i++) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000206 Variable* var = scope()->parameter(i);
207 if (var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000208 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
209 (num_parameters - 1 - i) * kPointerSize;
210 // Load parameter from stack.
211 __ lw(a0, MemOperand(fp, parameter_offset));
212 // Store it in the context.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000213 MemOperand target = ContextOperand(cp, var->index());
214 __ sw(a0, target);
215
216 // Update the write barrier.
217 __ RecordWriteContextSlot(
218 cp, target.offset(), a0, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000219 }
220 }
221 }
222
223 Variable* arguments = scope()->arguments();
224 if (arguments != NULL) {
225 // Function uses arguments object.
226 Comment cmnt(masm_, "[ Allocate arguments object");
227 if (!function_in_register) {
228 // Load this again, if it's used by the local context below.
229 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
230 } else {
231 __ mov(a3, a1);
232 }
233 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000234 int num_parameters = info->scope()->num_parameters();
235 int offset = num_parameters * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000236 __ Addu(a2, fp,
237 Operand(StandardFrameConstants::kCallerSPOffset + offset));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000238 __ li(a1, Operand(Smi::FromInt(num_parameters)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000239 __ Push(a3, a2, a1);
240
241 // Arguments to ArgumentsAccessStub:
242 // function, receiver address, parameter count.
243 // The stub will rewrite receiever and parameter count if the previous
244 // stack frame was an arguments adapter frame.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000245 ArgumentsAccessStub::Type type;
246 if (is_strict_mode()) {
247 type = ArgumentsAccessStub::NEW_STRICT;
248 } else if (function()->has_duplicate_parameters()) {
249 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
250 } else {
251 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
252 }
253 ArgumentsAccessStub stub(type);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000254 __ CallStub(&stub);
255
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000256 SetVar(arguments, v0, a1, a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000257 }
258
259 if (FLAG_trace) {
260 __ CallRuntime(Runtime::kTraceEnter, 0);
261 }
262
263 // Visit the declarations and body unless there is an illegal
264 // redeclaration.
265 if (scope()->HasIllegalRedeclaration()) {
266 Comment cmnt(masm_, "[ Declarations");
267 scope()->VisitIllegalRedeclaration(this);
268
269 } else {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000270 PrepareForBailoutForId(AstNode::kFunctionEntryId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000271 { Comment cmnt(masm_, "[ Declarations");
272 // For named function expressions, declare the function name as a
273 // constant.
274 if (scope()->is_function_scope() && scope()->function() != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000275 int ignored = 0;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000276 VariableProxy* proxy = scope()->function();
277 ASSERT(proxy->var()->mode() == CONST ||
278 proxy->var()->mode() == CONST_HARMONY);
279 EmitDeclaration(proxy, proxy->var()->mode(), NULL, &ignored);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000280 }
281 VisitDeclarations(scope()->declarations());
282 }
283
284 { Comment cmnt(masm_, "[ Stack check");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000285 PrepareForBailoutForId(AstNode::kDeclarationsId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000286 Label ok;
287 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
288 __ Branch(&ok, hs, sp, Operand(t0));
289 StackCheckStub stub;
290 __ CallStub(&stub);
291 __ bind(&ok);
292 }
293
294 { Comment cmnt(masm_, "[ Body");
295 ASSERT(loop_depth() == 0);
296 VisitStatements(function()->body());
297 ASSERT(loop_depth() == 0);
298 }
299 }
300
301 // Always emit a 'return undefined' in case control fell off the end of
302 // the body.
303 { Comment cmnt(masm_, "[ return <undefined>;");
304 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
305 }
306 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000307}
308
309
310void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000311 ASSERT(Smi::FromInt(0) == 0);
312 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000313}
314
315
316void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000317 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
318 // to make sure it is constant. Branch may emit a skip-or-jump sequence
319 // instead of the normal Branch. It seems that the "skip" part of that
320 // sequence is about as long as this Branch would be so it is safe to ignore
321 // that.
322 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000323 Comment cmnt(masm_, "[ Stack check");
324 Label ok;
325 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000326 __ sltu(at, sp, t0);
327 __ beq(at, zero_reg, &ok);
328 // CallStub will emit a li t9, ... first, so it is safe to use the delay slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000329 StackCheckStub stub;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000330 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000331 // Record a mapping of this PC offset to the OSR id. This is used to find
332 // the AST id from the unoptimized code in order to use it as a key into
333 // the deoptimization input data found in the optimized code.
334 RecordStackCheck(stmt->OsrEntryId());
335
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000336 __ bind(&ok);
337 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
338 // Record a mapping of the OSR id to this PC. This is used if the OSR
339 // entry becomes the target of a bailout. We don't expect it to be, but
340 // we want it to work if it is.
341 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000342}
343
344
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000345void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000346 Comment cmnt(masm_, "[ Return sequence");
347 if (return_label_.is_bound()) {
348 __ Branch(&return_label_);
349 } else {
350 __ bind(&return_label_);
351 if (FLAG_trace) {
352 // Push the return value on the stack as the parameter.
353 // Runtime::TraceExit returns its parameter in v0.
354 __ push(v0);
355 __ CallRuntime(Runtime::kTraceExit, 1);
356 }
357
358#ifdef DEBUG
359 // Add a label for checking the size of the code used for returning.
360 Label check_exit_codesize;
361 masm_->bind(&check_exit_codesize);
362#endif
363 // Make sure that the constant pool is not emitted inside of the return
364 // sequence.
365 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
366 // Here we use masm_-> instead of the __ macro to avoid the code coverage
367 // tool from instrumenting as we rely on the code size here.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000368 int32_t sp_delta = (info_->scope()->num_parameters() + 1) * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000369 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
370 __ RecordJSReturn();
371 masm_->mov(sp, fp);
372 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
373 masm_->Addu(sp, sp, Operand(sp_delta));
374 masm_->Jump(ra);
375 }
376
377#ifdef DEBUG
378 // Check that the size of the code used for returning is large enough
379 // for the debugger's requirements.
380 ASSERT(Assembler::kJSReturnSequenceInstructions <=
381 masm_->InstructionsGeneratedSince(&check_exit_codesize));
382#endif
383 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000384}
385
386
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000387void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
388 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
ager@chromium.org5c838252010-02-19 08:53:10 +0000389}
390
391
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000392void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
393 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
394 codegen()->GetVar(result_register(), var);
ager@chromium.org5c838252010-02-19 08:53:10 +0000395}
396
397
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000398void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
399 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
400 codegen()->GetVar(result_register(), var);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000401 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000402}
403
404
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000405void FullCodeGenerator::TestContext::Plug(Variable* var) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000406 // For simplicity we always test the accumulator register.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000407 codegen()->GetVar(result_register(), var);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000408 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000409 codegen()->DoTest(this);
ager@chromium.org5c838252010-02-19 08:53:10 +0000410}
411
412
lrn@chromium.org7516f052011-03-30 08:52:27 +0000413void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000414}
415
416
lrn@chromium.org7516f052011-03-30 08:52:27 +0000417void FullCodeGenerator::AccumulatorValueContext::Plug(
418 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000419 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000420}
421
422
423void FullCodeGenerator::StackValueContext::Plug(
424 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000425 __ LoadRoot(result_register(), index);
426 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000427}
428
429
430void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000431 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000432 true,
433 true_label_,
434 false_label_);
435 if (index == Heap::kUndefinedValueRootIndex ||
436 index == Heap::kNullValueRootIndex ||
437 index == Heap::kFalseValueRootIndex) {
438 if (false_label_ != fall_through_) __ Branch(false_label_);
439 } else if (index == Heap::kTrueValueRootIndex) {
440 if (true_label_ != fall_through_) __ Branch(true_label_);
441 } else {
442 __ LoadRoot(result_register(), index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000443 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000444 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000445}
446
447
448void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000449}
450
451
452void FullCodeGenerator::AccumulatorValueContext::Plug(
453 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000454 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000455}
456
457
458void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000459 // Immediates cannot be pushed directly.
460 __ li(result_register(), Operand(lit));
461 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000462}
463
464
465void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000466 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000467 true,
468 true_label_,
469 false_label_);
470 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
471 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
472 if (false_label_ != fall_through_) __ Branch(false_label_);
473 } else if (lit->IsTrue() || lit->IsJSObject()) {
474 if (true_label_ != fall_through_) __ Branch(true_label_);
475 } else if (lit->IsString()) {
476 if (String::cast(*lit)->length() == 0) {
477 if (false_label_ != fall_through_) __ Branch(false_label_);
478 } else {
479 if (true_label_ != fall_through_) __ Branch(true_label_);
480 }
481 } else if (lit->IsSmi()) {
482 if (Smi::cast(*lit)->value() == 0) {
483 if (false_label_ != fall_through_) __ Branch(false_label_);
484 } else {
485 if (true_label_ != fall_through_) __ Branch(true_label_);
486 }
487 } else {
488 // For simplicity we always test the accumulator register.
489 __ li(result_register(), Operand(lit));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000490 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000491 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000492}
493
494
495void FullCodeGenerator::EffectContext::DropAndPlug(int count,
496 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000497 ASSERT(count > 0);
498 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000499}
500
501
502void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
503 int count,
504 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000505 ASSERT(count > 0);
506 __ Drop(count);
507 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000508}
509
510
511void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
512 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000513 ASSERT(count > 0);
514 if (count > 1) __ Drop(count - 1);
515 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000516}
517
518
519void FullCodeGenerator::TestContext::DropAndPlug(int count,
520 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000521 ASSERT(count > 0);
522 // For simplicity we always test the accumulator register.
523 __ Drop(count);
524 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000525 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000526 codegen()->DoTest(this);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000527}
528
529
530void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
531 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000532 ASSERT(materialize_true == materialize_false);
533 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000534}
535
536
537void FullCodeGenerator::AccumulatorValueContext::Plug(
538 Label* materialize_true,
539 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000540 Label done;
541 __ bind(materialize_true);
542 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
543 __ Branch(&done);
544 __ bind(materialize_false);
545 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
546 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000547}
548
549
550void FullCodeGenerator::StackValueContext::Plug(
551 Label* materialize_true,
552 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000553 Label done;
554 __ bind(materialize_true);
555 __ LoadRoot(at, Heap::kTrueValueRootIndex);
556 __ push(at);
557 __ Branch(&done);
558 __ bind(materialize_false);
559 __ LoadRoot(at, Heap::kFalseValueRootIndex);
560 __ push(at);
561 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000562}
563
564
565void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
566 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000567 ASSERT(materialize_true == true_label_);
568 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000569}
570
571
572void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000573}
574
575
576void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000577 Heap::RootListIndex value_root_index =
578 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
579 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000580}
581
582
583void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000584 Heap::RootListIndex value_root_index =
585 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
586 __ LoadRoot(at, value_root_index);
587 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000588}
589
590
591void FullCodeGenerator::TestContext::Plug(bool flag) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000592 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000593 true,
594 true_label_,
595 false_label_);
596 if (flag) {
597 if (true_label_ != fall_through_) __ Branch(true_label_);
598 } else {
599 if (false_label_ != fall_through_) __ Branch(false_label_);
600 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000601}
602
603
whesse@chromium.org7b260152011-06-20 15:33:18 +0000604void FullCodeGenerator::DoTest(Expression* condition,
605 Label* if_true,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000606 Label* if_false,
607 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000608 if (CpuFeatures::IsSupported(FPU)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000609 ToBooleanStub stub(result_register());
610 __ CallStub(&stub);
611 __ mov(at, zero_reg);
612 } else {
613 // Call the runtime to find the boolean value of the source and then
614 // translate it into control flow to the pair of labels.
615 __ push(result_register());
616 __ CallRuntime(Runtime::kToBool, 1);
617 __ LoadRoot(at, Heap::kFalseValueRootIndex);
618 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000619 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000620}
621
622
lrn@chromium.org7516f052011-03-30 08:52:27 +0000623void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000624 Register lhs,
625 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000626 Label* if_true,
627 Label* if_false,
628 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000629 if (if_false == fall_through) {
630 __ Branch(if_true, cc, lhs, rhs);
631 } else if (if_true == fall_through) {
632 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
633 } else {
634 __ Branch(if_true, cc, lhs, rhs);
635 __ Branch(if_false);
636 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000637}
638
639
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000640MemOperand FullCodeGenerator::StackOperand(Variable* var) {
641 ASSERT(var->IsStackAllocated());
642 // Offset is negative because higher indexes are at lower addresses.
643 int offset = -var->index() * kPointerSize;
644 // Adjust by a (parameter or local) base offset.
645 if (var->IsParameter()) {
646 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
647 } else {
648 offset += JavaScriptFrameConstants::kLocal0Offset;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000649 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000650 return MemOperand(fp, offset);
ager@chromium.org5c838252010-02-19 08:53:10 +0000651}
652
653
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000654MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
655 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
656 if (var->IsContextSlot()) {
657 int context_chain_length = scope()->ContextChainLength(var->scope());
658 __ LoadContext(scratch, context_chain_length);
659 return ContextOperand(scratch, var->index());
660 } else {
661 return StackOperand(var);
662 }
663}
664
665
666void FullCodeGenerator::GetVar(Register dest, Variable* var) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000667 // Use destination as scratch.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000668 MemOperand location = VarOperand(var, dest);
669 __ lw(dest, location);
670}
671
672
673void FullCodeGenerator::SetVar(Variable* var,
674 Register src,
675 Register scratch0,
676 Register scratch1) {
677 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
678 ASSERT(!scratch0.is(src));
679 ASSERT(!scratch0.is(scratch1));
680 ASSERT(!scratch1.is(src));
681 MemOperand location = VarOperand(var, scratch0);
682 __ sw(src, location);
683 // Emit the write barrier code if the location is in the heap.
684 if (var->IsContextSlot()) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000685 __ RecordWriteContextSlot(scratch0,
686 location.offset(),
687 src,
688 scratch1,
689 kRAHasBeenSaved,
690 kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000691 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000692}
693
694
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000695void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000696 bool should_normalize,
697 Label* if_true,
698 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000699 // Only prepare for bailouts before splits if we're in a test
700 // context. Otherwise, we let the Visit function deal with the
701 // preparation to avoid preparing with the same AST id twice.
702 if (!context()->IsTest() || !info_->IsOptimizable()) return;
703
704 Label skip;
705 if (should_normalize) __ Branch(&skip);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000706 PrepareForBailout(expr, TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000707 if (should_normalize) {
708 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
709 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
710 __ bind(&skip);
711 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000712}
713
714
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000715void FullCodeGenerator::EmitDeclaration(VariableProxy* proxy,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000716 VariableMode mode,
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000717 FunctionLiteral* function,
718 int* global_count) {
719 // If it was not possible to allocate the variable at compile time, we
720 // need to "declare" it at runtime to make sure it actually exists in the
721 // local context.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000722 Variable* variable = proxy->var();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000723 bool binding_needs_init = (function == NULL) &&
724 (mode == CONST || mode == CONST_HARMONY || mode == LET);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000725 switch (variable->location()) {
726 case Variable::UNALLOCATED:
727 ++(*global_count);
728 break;
729
730 case Variable::PARAMETER:
731 case Variable::LOCAL:
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000732 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000733 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000734 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000735 __ sw(result_register(), StackOperand(variable));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000736 } else if (binding_needs_init) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000737 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000738 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000739 __ sw(t0, StackOperand(variable));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000740 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000741 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000742
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000743 case Variable::CONTEXT:
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000744 // The variable in the decl always resides in the current function
745 // context.
746 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
747 if (FLAG_debug_code) {
748 // Check that we're not inside a with or catch context.
749 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
750 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
751 __ Check(ne, "Declaration in with context.",
752 a1, Operand(t0));
753 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
754 __ Check(ne, "Declaration in catch context.",
755 a1, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000756 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000757 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000758 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000759 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000760 __ sw(result_register(), ContextOperand(cp, variable->index()));
761 int offset = Context::SlotOffset(variable->index());
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000762 // We know that we have written a function, which is not a smi.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000763 __ RecordWriteContextSlot(cp,
764 offset,
765 result_register(),
766 a2,
767 kRAHasBeenSaved,
768 kDontSaveFPRegs,
769 EMIT_REMEMBERED_SET,
770 OMIT_SMI_CHECK);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000771 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000772 } else if (binding_needs_init) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000773 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000774 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000775 __ sw(at, ContextOperand(cp, variable->index()));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000776 // No write barrier since the_hole_value is in old space.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000777 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000778 }
779 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000780
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000781 case Variable::LOOKUP: {
782 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000783 __ li(a2, Operand(variable->name()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000784 // Declaration nodes are always introduced in one of four modes.
785 ASSERT(mode == VAR ||
786 mode == CONST ||
787 mode == CONST_HARMONY ||
788 mode == LET);
789 PropertyAttributes attr = (mode == CONST || mode == CONST_HARMONY)
790 ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000791 __ li(a1, Operand(Smi::FromInt(attr)));
792 // Push initial value, if any.
793 // Note: For variables we must not push an initial value (such as
794 // 'undefined') because we may have a (legal) redeclaration and we
795 // must not destroy the current value.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000796 if (function != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000797 __ Push(cp, a2, a1);
798 // Push initial value for function declaration.
799 VisitForStackValue(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000800 } else if (binding_needs_init) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000801 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
802 __ Push(cp, a2, a1, a0);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000803 } else {
804 ASSERT(Smi::FromInt(0) == 0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000805 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000806 __ Push(cp, a2, a1, a0);
807 }
808 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
809 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000810 }
811 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000812}
813
814
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000815void FullCodeGenerator::VisitDeclaration(Declaration* decl) { }
ager@chromium.org5c838252010-02-19 08:53:10 +0000816
817
818void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000819 // Call the runtime to declare the globals.
820 // The context is the first argument.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000821 __ li(a1, Operand(pairs));
822 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
823 __ Push(cp, a1, a0);
824 __ CallRuntime(Runtime::kDeclareGlobals, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000825 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000826}
827
828
lrn@chromium.org7516f052011-03-30 08:52:27 +0000829void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000830 Comment cmnt(masm_, "[ SwitchStatement");
831 Breakable nested_statement(this, stmt);
832 SetStatementPosition(stmt);
833
834 // Keep the switch value on the stack until a case matches.
835 VisitForStackValue(stmt->tag());
836 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
837
838 ZoneList<CaseClause*>* clauses = stmt->cases();
839 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
840
841 Label next_test; // Recycled for each test.
842 // Compile all the tests with branches to their bodies.
843 for (int i = 0; i < clauses->length(); i++) {
844 CaseClause* clause = clauses->at(i);
845 clause->body_target()->Unuse();
846
847 // The default is not a test, but remember it as final fall through.
848 if (clause->is_default()) {
849 default_clause = clause;
850 continue;
851 }
852
853 Comment cmnt(masm_, "[ Case comparison");
854 __ bind(&next_test);
855 next_test.Unuse();
856
857 // Compile the label expression.
858 VisitForAccumulatorValue(clause->label());
859 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
860
861 // Perform the comparison as if via '==='.
862 __ lw(a1, MemOperand(sp, 0)); // Switch value.
863 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
864 JumpPatchSite patch_site(masm_);
865 if (inline_smi_code) {
866 Label slow_case;
867 __ or_(a2, a1, a0);
868 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
869
870 __ Branch(&next_test, ne, a1, Operand(a0));
871 __ Drop(1); // Switch value is no longer needed.
872 __ Branch(clause->body_target());
873
874 __ bind(&slow_case);
875 }
876
877 // Record position before stub call for type feedback.
878 SetSourcePosition(clause->position());
879 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000880 __ Call(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000881 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +0000882
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000883 __ Branch(&next_test, ne, v0, Operand(zero_reg));
884 __ Drop(1); // Switch value is no longer needed.
885 __ Branch(clause->body_target());
886 }
887
888 // Discard the test value and jump to the default if present, otherwise to
889 // the end of the statement.
890 __ bind(&next_test);
891 __ Drop(1); // Switch value is no longer needed.
892 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000893 __ Branch(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000894 } else {
895 __ Branch(default_clause->body_target());
896 }
897
898 // Compile all the case bodies.
899 for (int i = 0; i < clauses->length(); i++) {
900 Comment cmnt(masm_, "[ Case body");
901 CaseClause* clause = clauses->at(i);
902 __ bind(clause->body_target());
903 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
904 VisitStatements(clause->statements());
905 }
906
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000907 __ bind(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000908 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000909}
910
911
912void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000913 Comment cmnt(masm_, "[ ForInStatement");
914 SetStatementPosition(stmt);
915
916 Label loop, exit;
917 ForIn loop_statement(this, stmt);
918 increment_loop_depth();
919
920 // Get the object to enumerate over. Both SpiderMonkey and JSC
921 // ignore null and undefined in contrast to the specification; see
922 // ECMA-262 section 12.6.4.
923 VisitForAccumulatorValue(stmt->enumerable());
924 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
925 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
926 __ Branch(&exit, eq, a0, Operand(at));
927 Register null_value = t1;
928 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
929 __ Branch(&exit, eq, a0, Operand(null_value));
930
931 // Convert the object to a JS object.
932 Label convert, done_convert;
933 __ JumpIfSmi(a0, &convert);
934 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000935 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000936 __ bind(&convert);
937 __ push(a0);
938 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
939 __ mov(a0, v0);
940 __ bind(&done_convert);
941 __ push(a0);
942
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000943 // Check for proxies.
944 Label call_runtime;
945 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
946 __ GetObjectType(a0, a1, a1);
947 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
948
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000949 // Check cache validity in generated code. This is a fast case for
950 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
951 // guarantee cache validity, call the runtime system to check cache
952 // validity or get the property names in a fixed array.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000953 Label next;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000954 // Preload a couple of values used in the loop.
955 Register empty_fixed_array_value = t2;
956 __ LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
957 Register empty_descriptor_array_value = t3;
958 __ LoadRoot(empty_descriptor_array_value,
959 Heap::kEmptyDescriptorArrayRootIndex);
960 __ mov(a1, a0);
961 __ bind(&next);
962
963 // Check that there are no elements. Register a1 contains the
964 // current JS object we've reached through the prototype chain.
965 __ lw(a2, FieldMemOperand(a1, JSObject::kElementsOffset));
966 __ Branch(&call_runtime, ne, a2, Operand(empty_fixed_array_value));
967
968 // Check that instance descriptors are not empty so that we can
969 // check for an enum cache. Leave the map in a2 for the subsequent
970 // prototype load.
971 __ lw(a2, FieldMemOperand(a1, HeapObject::kMapOffset));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000972 __ lw(a3, FieldMemOperand(a2, Map::kInstanceDescriptorsOrBitField3Offset));
973 __ JumpIfSmi(a3, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000974
975 // Check that there is an enum cache in the non-empty instance
976 // descriptors (a3). This is the case if the next enumeration
977 // index field does not contain a smi.
978 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumerationIndexOffset));
979 __ JumpIfSmi(a3, &call_runtime);
980
981 // For all objects but the receiver, check that the cache is empty.
982 Label check_prototype;
983 __ Branch(&check_prototype, eq, a1, Operand(a0));
984 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumCacheBridgeCacheOffset));
985 __ Branch(&call_runtime, ne, a3, Operand(empty_fixed_array_value));
986
987 // Load the prototype from the map and loop if non-null.
988 __ bind(&check_prototype);
989 __ lw(a1, FieldMemOperand(a2, Map::kPrototypeOffset));
990 __ Branch(&next, ne, a1, Operand(null_value));
991
992 // The enum cache is valid. Load the map of the object being
993 // iterated over and use the cache for the iteration.
994 Label use_cache;
995 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
996 __ Branch(&use_cache);
997
998 // Get the set of properties to enumerate.
999 __ bind(&call_runtime);
1000 __ push(a0); // Duplicate the enumerable object on the stack.
1001 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1002
1003 // If we got a map from the runtime call, we can do a fast
1004 // modification check. Otherwise, we got a fixed array, and we have
1005 // to do a slow check.
1006 Label fixed_array;
1007 __ mov(a2, v0);
1008 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
1009 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1010 __ Branch(&fixed_array, ne, a1, Operand(at));
1011
1012 // We got a map in register v0. Get the enumeration cache from it.
1013 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001014 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001015 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
1016 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1017
1018 // Setup the four remaining stack slots.
1019 __ push(v0); // Map.
1020 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
1021 __ li(a0, Operand(Smi::FromInt(0)));
1022 // Push enumeration cache, enumeration cache length (as smi) and zero.
1023 __ Push(a2, a1, a0);
1024 __ jmp(&loop);
1025
1026 // We got a fixed array in register v0. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001027 Label non_proxy;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001028 __ bind(&fixed_array);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001029 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1030 __ lw(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1031 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1032 __ GetObjectType(a2, a3, a3);
1033 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1034 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1035 __ bind(&non_proxy);
1036 __ Push(a1, v0); // Smi and array
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001037 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1038 __ li(a0, Operand(Smi::FromInt(0)));
1039 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1040
1041 // Generate code for doing the condition check.
1042 __ bind(&loop);
1043 // Load the current count to a0, load the length to a1.
1044 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1045 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001046 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001047
1048 // Get the current entry of the array into register a3.
1049 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1050 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1051 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1052 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1053 __ lw(a3, MemOperand(t0)); // Current entry.
1054
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001055 // Get the expected map from the stack or a smi in the
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001056 // permanent slow case into register a2.
1057 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1058
1059 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001060 // If not, we may have to filter the key.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001061 Label update_each;
1062 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1063 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1064 __ Branch(&update_each, eq, t0, Operand(a2));
1065
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001066 // For proxies, no filtering is done.
1067 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1068 ASSERT_EQ(Smi::FromInt(0), 0);
1069 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1070
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001071 // Convert the entry to a string or (smi) 0 if it isn't a property
1072 // any more. If the property has been removed while iterating, we
1073 // just skip it.
1074 __ push(a1); // Enumerable.
1075 __ push(a3); // Current entry.
1076 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1077 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001078 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001079
1080 // Update the 'each' property or variable from the possibly filtered
1081 // entry in register a3.
1082 __ bind(&update_each);
1083 __ mov(result_register(), a3);
1084 // Perform the assignment as if via '='.
1085 { EffectContext context(this);
1086 EmitAssignment(stmt->each(), stmt->AssignmentId());
1087 }
1088
1089 // Generate code for the body of the loop.
1090 Visit(stmt->body());
1091
1092 // Generate code for the going to the next element by incrementing
1093 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001094 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001095 __ pop(a0);
1096 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1097 __ push(a0);
1098
1099 EmitStackCheck(stmt);
1100 __ Branch(&loop);
1101
1102 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001103 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001104 __ Drop(5);
1105
1106 // Exit and decrement the loop depth.
1107 __ bind(&exit);
1108 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001109}
1110
1111
1112void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1113 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001114 // Use the fast case closure allocation code that allocates in new
1115 // space for nested functions that don't need literals cloning. If
1116 // we're running with the --always-opt or the --prepare-always-opt
1117 // flag, we need to use the runtime function so that the new function
1118 // we are creating here gets a chance to have its code optimized and
1119 // doesn't just get a copy of the existing unoptimized code.
1120 if (!FLAG_always_opt &&
1121 !FLAG_prepare_always_opt &&
1122 !pretenure &&
1123 scope()->is_function_scope() &&
1124 info->num_literals() == 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001125 FastNewClosureStub stub(info->strict_mode_flag());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001126 __ li(a0, Operand(info));
1127 __ push(a0);
1128 __ CallStub(&stub);
1129 } else {
1130 __ li(a0, Operand(info));
1131 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1132 : Heap::kFalseValueRootIndex);
1133 __ Push(cp, a0, a1);
1134 __ CallRuntime(Runtime::kNewClosure, 3);
1135 }
1136 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001137}
1138
1139
1140void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001141 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001142 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001143}
1144
1145
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001146void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1147 TypeofState typeof_state,
1148 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001149 Register current = cp;
1150 Register next = a1;
1151 Register temp = a2;
1152
1153 Scope* s = scope();
1154 while (s != NULL) {
1155 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001156 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001157 // Check that extension is NULL.
1158 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1159 __ Branch(slow, ne, temp, Operand(zero_reg));
1160 }
1161 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001162 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001163 // Walk the rest of the chain without clobbering cp.
1164 current = next;
1165 }
1166 // If no outer scope calls eval, we do not need to check more
1167 // context extensions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001168 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001169 s = s->outer_scope();
1170 }
1171
1172 if (s->is_eval_scope()) {
1173 Label loop, fast;
1174 if (!current.is(next)) {
1175 __ Move(next, current);
1176 }
1177 __ bind(&loop);
1178 // Terminate at global context.
1179 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1180 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1181 __ Branch(&fast, eq, temp, Operand(t0));
1182 // Check that extension is NULL.
1183 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1184 __ Branch(slow, ne, temp, Operand(zero_reg));
1185 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001186 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001187 __ Branch(&loop);
1188 __ bind(&fast);
1189 }
1190
1191 __ lw(a0, GlobalObjectOperand());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001192 __ li(a2, Operand(var->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001193 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1194 ? RelocInfo::CODE_TARGET
1195 : RelocInfo::CODE_TARGET_CONTEXT;
1196 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001197 __ Call(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001198}
1199
1200
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001201MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1202 Label* slow) {
1203 ASSERT(var->IsContextSlot());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001204 Register context = cp;
1205 Register next = a3;
1206 Register temp = t0;
1207
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001208 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001209 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001210 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001211 // Check that extension is NULL.
1212 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1213 __ Branch(slow, ne, temp, Operand(zero_reg));
1214 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001215 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001216 // Walk the rest of the chain without clobbering cp.
1217 context = next;
1218 }
1219 }
1220 // Check that last extension is NULL.
1221 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1222 __ Branch(slow, ne, temp, Operand(zero_reg));
1223
1224 // This function is used only for loads, not stores, so it's safe to
1225 // return an cp-based operand (the write barrier cannot be allowed to
1226 // destroy the cp register).
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001227 return ContextOperand(context, var->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001228}
1229
1230
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001231void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1232 TypeofState typeof_state,
1233 Label* slow,
1234 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001235 // Generate fast-case code for variables that might be shadowed by
1236 // eval-introduced variables. Eval is used a lot without
1237 // introducing variables. In those cases, we do not want to
1238 // perform a runtime call for all variables in the scope
1239 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001240 if (var->mode() == DYNAMIC_GLOBAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001241 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001242 __ Branch(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001243 } else if (var->mode() == DYNAMIC_LOCAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001244 Variable* local = var->local_if_not_shadowed();
1245 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001246 if (local->mode() == CONST ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001247 local->mode() == CONST_HARMONY ||
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001248 local->mode() == LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001249 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1250 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001251 if (local->mode() == CONST) {
1252 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1253 __ movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001254 } else { // LET || CONST_HARMONY
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001255 __ Branch(done, ne, at, Operand(zero_reg));
1256 __ li(a0, Operand(var->name()));
1257 __ push(a0);
1258 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1259 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001260 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001261 __ Branch(done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001262 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001263}
1264
1265
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001266void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1267 // Record position before possible IC call.
1268 SetSourcePosition(proxy->position());
1269 Variable* var = proxy->var();
1270
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001271 // Three cases: global variables, lookup variables, and all other types of
1272 // variables.
1273 switch (var->location()) {
1274 case Variable::UNALLOCATED: {
1275 Comment cmnt(masm_, "Global variable");
1276 // Use inline caching. Variable name is passed in a2 and the global
1277 // object (receiver) in a0.
1278 __ lw(a0, GlobalObjectOperand());
1279 __ li(a2, Operand(var->name()));
1280 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1281 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
1282 context()->Plug(v0);
1283 break;
1284 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001285
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001286 case Variable::PARAMETER:
1287 case Variable::LOCAL:
1288 case Variable::CONTEXT: {
1289 Comment cmnt(masm_, var->IsContextSlot()
1290 ? "Context variable"
1291 : "Stack variable");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001292 if (!var->binding_needs_init()) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001293 context()->Plug(var);
1294 } else {
1295 // Let and const need a read barrier.
1296 GetVar(v0, var);
1297 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1298 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001299 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1300 // Throw a reference error when using an uninitialized let/const
1301 // binding in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001302 Label done;
1303 __ Branch(&done, ne, at, Operand(zero_reg));
1304 __ li(a0, Operand(var->name()));
1305 __ push(a0);
1306 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1307 __ bind(&done);
1308 } else {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001309 // Uninitalized const bindings outside of harmony mode are unholed.
1310 ASSERT(var->mode() == CONST);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001311 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1312 __ movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1313 }
1314 context()->Plug(v0);
1315 }
1316 break;
1317 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001318
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001319 case Variable::LOOKUP: {
1320 Label done, slow;
1321 // Generate code for loading from variables potentially shadowed
1322 // by eval-introduced variables.
1323 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1324 __ bind(&slow);
1325 Comment cmnt(masm_, "Lookup variable");
1326 __ li(a1, Operand(var->name()));
1327 __ Push(cp, a1); // Context and name.
1328 __ CallRuntime(Runtime::kLoadContextSlot, 2);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001329 __ bind(&done);
1330 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001331 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001332 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001333}
1334
1335
1336void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001337 Comment cmnt(masm_, "[ RegExpLiteral");
1338 Label materialized;
1339 // Registers will be used as follows:
1340 // t1 = materialized value (RegExp literal)
1341 // t0 = JS function, literals array
1342 // a3 = literal index
1343 // a2 = RegExp pattern
1344 // a1 = RegExp flags
1345 // a0 = RegExp literal clone
1346 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1347 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1348 int literal_offset =
1349 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1350 __ lw(t1, FieldMemOperand(t0, literal_offset));
1351 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1352 __ Branch(&materialized, ne, t1, Operand(at));
1353
1354 // Create regexp literal using runtime function.
1355 // Result will be in v0.
1356 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1357 __ li(a2, Operand(expr->pattern()));
1358 __ li(a1, Operand(expr->flags()));
1359 __ Push(t0, a3, a2, a1);
1360 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1361 __ mov(t1, v0);
1362
1363 __ bind(&materialized);
1364 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1365 Label allocated, runtime_allocate;
1366 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1367 __ jmp(&allocated);
1368
1369 __ bind(&runtime_allocate);
1370 __ push(t1);
1371 __ li(a0, Operand(Smi::FromInt(size)));
1372 __ push(a0);
1373 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1374 __ pop(t1);
1375
1376 __ bind(&allocated);
1377
1378 // After this, registers are used as follows:
1379 // v0: Newly allocated regexp.
1380 // t1: Materialized regexp.
1381 // a2: temp.
1382 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1383 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001384}
1385
1386
1387void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001388 Comment cmnt(masm_, "[ ObjectLiteral");
1389 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1390 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1391 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1392 __ li(a1, Operand(expr->constant_properties()));
1393 int flags = expr->fast_elements()
1394 ? ObjectLiteral::kFastElements
1395 : ObjectLiteral::kNoFlags;
1396 flags |= expr->has_function()
1397 ? ObjectLiteral::kHasFunction
1398 : ObjectLiteral::kNoFlags;
1399 __ li(a0, Operand(Smi::FromInt(flags)));
1400 __ Push(a3, a2, a1, a0);
1401 if (expr->depth() > 1) {
1402 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1403 } else {
1404 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
1405 }
1406
1407 // If result_saved is true the result is on top of the stack. If
1408 // result_saved is false the result is in v0.
1409 bool result_saved = false;
1410
1411 // Mark all computed expressions that are bound to a key that
1412 // is shadowed by a later occurrence of the same key. For the
1413 // marked expressions, no store code is emitted.
1414 expr->CalculateEmitStore();
1415
1416 for (int i = 0; i < expr->properties()->length(); i++) {
1417 ObjectLiteral::Property* property = expr->properties()->at(i);
1418 if (property->IsCompileTimeValue()) continue;
1419
1420 Literal* key = property->key();
1421 Expression* value = property->value();
1422 if (!result_saved) {
1423 __ push(v0); // Save result on stack.
1424 result_saved = true;
1425 }
1426 switch (property->kind()) {
1427 case ObjectLiteral::Property::CONSTANT:
1428 UNREACHABLE();
1429 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1430 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1431 // Fall through.
1432 case ObjectLiteral::Property::COMPUTED:
1433 if (key->handle()->IsSymbol()) {
1434 if (property->emit_store()) {
1435 VisitForAccumulatorValue(value);
1436 __ mov(a0, result_register());
1437 __ li(a2, Operand(key->handle()));
1438 __ lw(a1, MemOperand(sp));
1439 Handle<Code> ic = is_strict_mode()
1440 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1441 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001442 __ Call(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001443 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1444 } else {
1445 VisitForEffect(value);
1446 }
1447 break;
1448 }
1449 // Fall through.
1450 case ObjectLiteral::Property::PROTOTYPE:
1451 // Duplicate receiver on stack.
1452 __ lw(a0, MemOperand(sp));
1453 __ push(a0);
1454 VisitForStackValue(key);
1455 VisitForStackValue(value);
1456 if (property->emit_store()) {
1457 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1458 __ push(a0);
1459 __ CallRuntime(Runtime::kSetProperty, 4);
1460 } else {
1461 __ Drop(3);
1462 }
1463 break;
1464 case ObjectLiteral::Property::GETTER:
1465 case ObjectLiteral::Property::SETTER:
1466 // Duplicate receiver on stack.
1467 __ lw(a0, MemOperand(sp));
1468 __ push(a0);
1469 VisitForStackValue(key);
1470 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1471 Smi::FromInt(1) :
1472 Smi::FromInt(0)));
1473 __ push(a1);
1474 VisitForStackValue(value);
1475 __ CallRuntime(Runtime::kDefineAccessor, 4);
1476 break;
1477 }
1478 }
1479
1480 if (expr->has_function()) {
1481 ASSERT(result_saved);
1482 __ lw(a0, MemOperand(sp));
1483 __ push(a0);
1484 __ CallRuntime(Runtime::kToFastProperties, 1);
1485 }
1486
1487 if (result_saved) {
1488 context()->PlugTOS();
1489 } else {
1490 context()->Plug(v0);
1491 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001492}
1493
1494
1495void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001496 Comment cmnt(masm_, "[ ArrayLiteral");
1497
1498 ZoneList<Expression*>* subexprs = expr->values();
1499 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001500
1501 Handle<FixedArray> constant_elements = expr->constant_elements();
1502 ASSERT_EQ(2, constant_elements->length());
1503 ElementsKind constant_elements_kind =
1504 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
1505 Handle<FixedArrayBase> constant_elements_values(
1506 FixedArrayBase::cast(constant_elements->get(1)));
1507
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001508 __ mov(a0, result_register());
1509 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1510 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1511 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001512 __ li(a1, Operand(constant_elements));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001513 __ Push(a3, a2, a1);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001514 if (constant_elements_values->map() ==
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001515 isolate()->heap()->fixed_cow_array_map()) {
1516 FastCloneShallowArrayStub stub(
1517 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1518 __ CallStub(&stub);
1519 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1520 1, a1, a2);
1521 } else if (expr->depth() > 1) {
1522 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1523 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1524 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1525 } else {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001526 ASSERT(constant_elements_kind == FAST_ELEMENTS ||
1527 constant_elements_kind == FAST_SMI_ONLY_ELEMENTS ||
1528 FLAG_smi_only_arrays);
1529 FastCloneShallowArrayStub::Mode mode =
1530 constant_elements_kind == FAST_DOUBLE_ELEMENTS
1531 ? FastCloneShallowArrayStub::CLONE_DOUBLE_ELEMENTS
1532 : FastCloneShallowArrayStub::CLONE_ELEMENTS;
1533 FastCloneShallowArrayStub stub(mode, length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001534 __ CallStub(&stub);
1535 }
1536
1537 bool result_saved = false; // Is the result saved to the stack?
1538
1539 // Emit code to evaluate all the non-constant subexpressions and to store
1540 // them into the newly cloned array.
1541 for (int i = 0; i < length; i++) {
1542 Expression* subexpr = subexprs->at(i);
1543 // If the subexpression is a literal or a simple materialized literal it
1544 // is already set in the cloned array.
1545 if (subexpr->AsLiteral() != NULL ||
1546 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1547 continue;
1548 }
1549
1550 if (!result_saved) {
1551 __ push(v0);
1552 result_saved = true;
1553 }
1554 VisitForAccumulatorValue(subexpr);
1555
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001556 __ lw(t6, MemOperand(sp)); // Copy of array literal.
1557 __ lw(a1, FieldMemOperand(t6, JSObject::kElementsOffset));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001558 __ lw(a2, FieldMemOperand(t6, JSObject::kMapOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001559 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001560
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001561 Label element_done;
1562 Label double_elements;
1563 Label smi_element;
1564 Label slow_elements;
1565 Label fast_elements;
1566 __ CheckFastElements(a2, a3, &double_elements);
1567
1568 // FAST_SMI_ONLY_ELEMENTS or FAST_ELEMENTS
1569 __ JumpIfSmi(result_register(), &smi_element);
1570 __ CheckFastSmiOnlyElements(a2, a3, &fast_elements);
1571
1572 // Store into the array literal requires a elements transition. Call into
1573 // the runtime.
1574 __ bind(&slow_elements);
1575 __ push(t6); // Copy of array literal.
1576 __ li(a1, Operand(Smi::FromInt(i)));
1577 __ li(a2, Operand(Smi::FromInt(NONE))); // PropertyAttributes
1578 __ li(a3, Operand(Smi::FromInt(strict_mode_flag()))); // Strict mode.
1579 __ Push(a1, result_register(), a2, a3);
1580 __ CallRuntime(Runtime::kSetProperty, 5);
1581 __ Branch(&element_done);
1582
1583 // Array literal has ElementsKind of FAST_DOUBLE_ELEMENTS.
1584 __ bind(&double_elements);
1585 __ li(a3, Operand(Smi::FromInt(i)));
1586 __ StoreNumberToDoubleElements(result_register(), a3, t6, a1, t0, t1, t5,
1587 t3, &slow_elements);
1588 __ Branch(&element_done);
1589
1590 // Array literal has ElementsKind of FAST_ELEMENTS and value is an object.
1591 __ bind(&fast_elements);
1592 __ sw(result_register(), FieldMemOperand(a1, offset));
1593 // Update the write barrier for the array store.
1594
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001595 __ RecordWriteField(
1596 a1, offset, result_register(), a2, kRAHasBeenSaved, kDontSaveFPRegs,
1597 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001598 __ Branch(&element_done);
1599
1600 // Array literal has ElementsKind of FAST_SMI_ONLY_ELEMENTS or
1601 // FAST_ELEMENTS, and value is Smi.
1602 __ bind(&smi_element);
1603 __ sw(result_register(), FieldMemOperand(a1, offset));
1604 // Fall through
1605
1606 __ bind(&element_done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001607
1608 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1609 }
1610
1611 if (result_saved) {
1612 context()->PlugTOS();
1613 } else {
1614 context()->Plug(v0);
1615 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001616}
1617
1618
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001619void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001620 Comment cmnt(masm_, "[ Assignment");
1621 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1622 // on the left-hand side.
1623 if (!expr->target()->IsValidLeftHandSide()) {
1624 VisitForEffect(expr->target());
1625 return;
1626 }
1627
1628 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001629 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001630 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1631 LhsKind assign_type = VARIABLE;
1632 Property* property = expr->target()->AsProperty();
1633 if (property != NULL) {
1634 assign_type = (property->key()->IsPropertyName())
1635 ? NAMED_PROPERTY
1636 : KEYED_PROPERTY;
1637 }
1638
1639 // Evaluate LHS expression.
1640 switch (assign_type) {
1641 case VARIABLE:
1642 // Nothing to do here.
1643 break;
1644 case NAMED_PROPERTY:
1645 if (expr->is_compound()) {
1646 // We need the receiver both on the stack and in the accumulator.
1647 VisitForAccumulatorValue(property->obj());
1648 __ push(result_register());
1649 } else {
1650 VisitForStackValue(property->obj());
1651 }
1652 break;
1653 case KEYED_PROPERTY:
1654 // We need the key and receiver on both the stack and in v0 and a1.
1655 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001656 VisitForStackValue(property->obj());
1657 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001658 __ lw(a1, MemOperand(sp, 0));
1659 __ push(v0);
1660 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001661 VisitForStackValue(property->obj());
1662 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001663 }
1664 break;
1665 }
1666
1667 // For compound assignments we need another deoptimization point after the
1668 // variable/property load.
1669 if (expr->is_compound()) {
1670 { AccumulatorValueContext context(this);
1671 switch (assign_type) {
1672 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001673 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001674 PrepareForBailout(expr->target(), TOS_REG);
1675 break;
1676 case NAMED_PROPERTY:
1677 EmitNamedPropertyLoad(property);
1678 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1679 break;
1680 case KEYED_PROPERTY:
1681 EmitKeyedPropertyLoad(property);
1682 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1683 break;
1684 }
1685 }
1686
1687 Token::Value op = expr->binary_op();
1688 __ push(v0); // Left operand goes on the stack.
1689 VisitForAccumulatorValue(expr->value());
1690
1691 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1692 ? OVERWRITE_RIGHT
1693 : NO_OVERWRITE;
1694 SetSourcePosition(expr->position() + 1);
1695 AccumulatorValueContext context(this);
1696 if (ShouldInlineSmiCase(op)) {
1697 EmitInlineSmiBinaryOp(expr->binary_operation(),
1698 op,
1699 mode,
1700 expr->target(),
1701 expr->value());
1702 } else {
1703 EmitBinaryOp(expr->binary_operation(), op, mode);
1704 }
1705
1706 // Deoptimization point in case the binary operation may have side effects.
1707 PrepareForBailout(expr->binary_operation(), TOS_REG);
1708 } else {
1709 VisitForAccumulatorValue(expr->value());
1710 }
1711
1712 // Record source position before possible IC call.
1713 SetSourcePosition(expr->position());
1714
1715 // Store the value.
1716 switch (assign_type) {
1717 case VARIABLE:
1718 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1719 expr->op());
1720 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1721 context()->Plug(v0);
1722 break;
1723 case NAMED_PROPERTY:
1724 EmitNamedPropertyAssignment(expr);
1725 break;
1726 case KEYED_PROPERTY:
1727 EmitKeyedPropertyAssignment(expr);
1728 break;
1729 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001730}
1731
1732
ager@chromium.org5c838252010-02-19 08:53:10 +00001733void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001734 SetSourcePosition(prop->position());
1735 Literal* key = prop->key()->AsLiteral();
1736 __ mov(a0, result_register());
1737 __ li(a2, Operand(key->handle()));
1738 // Call load IC. It has arguments receiver and property name a0 and a2.
1739 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001740 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001741}
1742
1743
1744void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001745 SetSourcePosition(prop->position());
1746 __ mov(a0, result_register());
1747 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1748 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001749 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001750}
1751
1752
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001753void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001754 Token::Value op,
1755 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001756 Expression* left_expr,
1757 Expression* right_expr) {
1758 Label done, smi_case, stub_call;
1759
1760 Register scratch1 = a2;
1761 Register scratch2 = a3;
1762
1763 // Get the arguments.
1764 Register left = a1;
1765 Register right = a0;
1766 __ pop(left);
1767 __ mov(a0, result_register());
1768
1769 // Perform combined smi check on both operands.
1770 __ Or(scratch1, left, Operand(right));
1771 STATIC_ASSERT(kSmiTag == 0);
1772 JumpPatchSite patch_site(masm_);
1773 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1774
1775 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001776 BinaryOpStub stub(op, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001777 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001778 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001779 __ jmp(&done);
1780
1781 __ bind(&smi_case);
1782 // Smi case. This code works the same way as the smi-smi case in the type
1783 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001784 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001785 switch (op) {
1786 case Token::SAR:
1787 __ Branch(&stub_call);
1788 __ GetLeastBitsFromSmi(scratch1, right, 5);
1789 __ srav(right, left, scratch1);
1790 __ And(v0, right, Operand(~kSmiTagMask));
1791 break;
1792 case Token::SHL: {
1793 __ Branch(&stub_call);
1794 __ SmiUntag(scratch1, left);
1795 __ GetLeastBitsFromSmi(scratch2, right, 5);
1796 __ sllv(scratch1, scratch1, scratch2);
1797 __ Addu(scratch2, scratch1, Operand(0x40000000));
1798 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1799 __ SmiTag(v0, scratch1);
1800 break;
1801 }
1802 case Token::SHR: {
1803 __ Branch(&stub_call);
1804 __ SmiUntag(scratch1, left);
1805 __ GetLeastBitsFromSmi(scratch2, right, 5);
1806 __ srlv(scratch1, scratch1, scratch2);
1807 __ And(scratch2, scratch1, 0xc0000000);
1808 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1809 __ SmiTag(v0, scratch1);
1810 break;
1811 }
1812 case Token::ADD:
1813 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1814 __ BranchOnOverflow(&stub_call, scratch1);
1815 break;
1816 case Token::SUB:
1817 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1818 __ BranchOnOverflow(&stub_call, scratch1);
1819 break;
1820 case Token::MUL: {
1821 __ SmiUntag(scratch1, right);
1822 __ Mult(left, scratch1);
1823 __ mflo(scratch1);
1824 __ mfhi(scratch2);
1825 __ sra(scratch1, scratch1, 31);
1826 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1827 __ mflo(v0);
1828 __ Branch(&done, ne, v0, Operand(zero_reg));
1829 __ Addu(scratch2, right, left);
1830 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1831 ASSERT(Smi::FromInt(0) == 0);
1832 __ mov(v0, zero_reg);
1833 break;
1834 }
1835 case Token::BIT_OR:
1836 __ Or(v0, left, Operand(right));
1837 break;
1838 case Token::BIT_AND:
1839 __ And(v0, left, Operand(right));
1840 break;
1841 case Token::BIT_XOR:
1842 __ Xor(v0, left, Operand(right));
1843 break;
1844 default:
1845 UNREACHABLE();
1846 }
1847
1848 __ bind(&done);
1849 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001850}
1851
1852
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001853void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1854 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001855 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001856 __ mov(a0, result_register());
1857 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001858 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001859 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001860 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001861 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001862 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001863}
1864
1865
1866void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001867 // Invalid left-hand sides are rewritten to have a 'throw
1868 // ReferenceError' on the left-hand side.
1869 if (!expr->IsValidLeftHandSide()) {
1870 VisitForEffect(expr);
1871 return;
1872 }
1873
1874 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001875 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001876 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1877 LhsKind assign_type = VARIABLE;
1878 Property* prop = expr->AsProperty();
1879 if (prop != NULL) {
1880 assign_type = (prop->key()->IsPropertyName())
1881 ? NAMED_PROPERTY
1882 : KEYED_PROPERTY;
1883 }
1884
1885 switch (assign_type) {
1886 case VARIABLE: {
1887 Variable* var = expr->AsVariableProxy()->var();
1888 EffectContext context(this);
1889 EmitVariableAssignment(var, Token::ASSIGN);
1890 break;
1891 }
1892 case NAMED_PROPERTY: {
1893 __ push(result_register()); // Preserve value.
1894 VisitForAccumulatorValue(prop->obj());
1895 __ mov(a1, result_register());
1896 __ pop(a0); // Restore value.
1897 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1898 Handle<Code> ic = is_strict_mode()
1899 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1900 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001901 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001902 break;
1903 }
1904 case KEYED_PROPERTY: {
1905 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001906 VisitForStackValue(prop->obj());
1907 VisitForAccumulatorValue(prop->key());
1908 __ mov(a1, result_register());
1909 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001910 __ pop(a0); // Restore value.
1911 Handle<Code> ic = is_strict_mode()
1912 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
1913 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001914 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001915 break;
1916 }
1917 }
1918 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1919 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001920}
1921
1922
1923void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001924 Token::Value op) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001925 if (var->IsUnallocated()) {
1926 // Global var, const, or let.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001927 __ mov(a0, result_register());
1928 __ li(a2, Operand(var->name()));
1929 __ lw(a1, GlobalObjectOperand());
1930 Handle<Code> ic = is_strict_mode()
1931 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1932 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001933 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001934
1935 } else if (op == Token::INIT_CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001936 // Const initializers need a write barrier.
1937 ASSERT(!var->IsParameter()); // No const parameters.
1938 if (var->IsStackLocal()) {
1939 Label skip;
1940 __ lw(a1, StackOperand(var));
1941 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1942 __ Branch(&skip, ne, a1, Operand(t0));
1943 __ sw(result_register(), StackOperand(var));
1944 __ bind(&skip);
1945 } else {
1946 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
1947 // Like var declarations, const declarations are hoisted to function
1948 // scope. However, unlike var initializers, const initializers are
1949 // able to drill a hole to that function context, even from inside a
1950 // 'with' context. We thus bypass the normal static scope lookup for
1951 // var->IsContextSlot().
1952 __ push(v0);
1953 __ li(a0, Operand(var->name()));
1954 __ Push(cp, a0); // Context and name.
1955 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001956 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001957
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001958 } else if (var->mode() == LET && op != Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001959 // Non-initializing assignment to let variable needs a write barrier.
1960 if (var->IsLookupSlot()) {
1961 __ push(v0); // Value.
1962 __ li(a1, Operand(var->name()));
1963 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1964 __ Push(cp, a1, a0); // Context, name, strict mode.
1965 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1966 } else {
1967 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
1968 Label assign;
1969 MemOperand location = VarOperand(var, a1);
1970 __ lw(a3, location);
1971 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1972 __ Branch(&assign, ne, a3, Operand(t0));
1973 __ li(a3, Operand(var->name()));
1974 __ push(a3);
1975 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1976 // Perform the assignment.
1977 __ bind(&assign);
1978 __ sw(result_register(), location);
1979 if (var->IsContextSlot()) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001980 // RecordWrite may destroy all its register arguments.
1981 __ mov(a3, result_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001982 int offset = Context::SlotOffset(var->index());
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001983 __ RecordWriteContextSlot(
1984 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001985 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001986 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001987
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001988 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
1989 // Assignment to var or initializing assignment to let/const
1990 // in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001991 if (var->IsStackAllocated() || var->IsContextSlot()) {
1992 MemOperand location = VarOperand(var, a1);
1993 if (FLAG_debug_code && op == Token::INIT_LET) {
1994 // Check for an uninitialized let binding.
1995 __ lw(a2, location);
1996 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1997 __ Check(eq, "Let binding re-initialization.", a2, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001998 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001999 // Perform the assignment.
2000 __ sw(v0, location);
2001 if (var->IsContextSlot()) {
2002 __ mov(a3, v0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002003 int offset = Context::SlotOffset(var->index());
2004 __ RecordWriteContextSlot(
2005 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002006 }
2007 } else {
2008 ASSERT(var->IsLookupSlot());
2009 __ push(v0); // Value.
2010 __ li(a1, Operand(var->name()));
2011 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
2012 __ Push(cp, a1, a0); // Context, name, strict mode.
2013 __ CallRuntime(Runtime::kStoreContextSlot, 4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002014 }
2015 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002016 // Non-initializing assignments to consts are ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +00002017}
2018
2019
2020void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002021 // Assignment to a property, using a named store IC.
2022 Property* prop = expr->target()->AsProperty();
2023 ASSERT(prop != NULL);
2024 ASSERT(prop->key()->AsLiteral() != NULL);
2025
2026 // If the assignment starts a block of assignments to the same object,
2027 // change to slow case to avoid the quadratic behavior of repeatedly
2028 // adding fast properties.
2029 if (expr->starts_initialization_block()) {
2030 __ push(result_register());
2031 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
2032 __ push(t0);
2033 __ CallRuntime(Runtime::kToSlowProperties, 1);
2034 __ pop(result_register());
2035 }
2036
2037 // Record source code position before IC call.
2038 SetSourcePosition(expr->position());
2039 __ mov(a0, result_register()); // Load the value.
2040 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
2041 // Load receiver to a1. Leave a copy in the stack if needed for turning the
2042 // receiver into fast case.
2043 if (expr->ends_initialization_block()) {
2044 __ lw(a1, MemOperand(sp));
2045 } else {
2046 __ pop(a1);
2047 }
2048
2049 Handle<Code> ic = is_strict_mode()
2050 ? isolate()->builtins()->StoreIC_Initialize_Strict()
2051 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002052 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002053
2054 // If the assignment ends an initialization block, revert to fast case.
2055 if (expr->ends_initialization_block()) {
2056 __ push(v0); // Result of assignment, saved even if not needed.
2057 // Receiver is under the result value.
2058 __ lw(t0, MemOperand(sp, kPointerSize));
2059 __ push(t0);
2060 __ CallRuntime(Runtime::kToFastProperties, 1);
2061 __ pop(v0);
2062 __ Drop(1);
2063 }
2064 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2065 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002066}
2067
2068
2069void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002070 // Assignment to a property, using a keyed store IC.
2071
2072 // If the assignment starts a block of assignments to the same object,
2073 // change to slow case to avoid the quadratic behavior of repeatedly
2074 // adding fast properties.
2075 if (expr->starts_initialization_block()) {
2076 __ push(result_register());
2077 // Receiver is now under the key and value.
2078 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2079 __ push(t0);
2080 __ CallRuntime(Runtime::kToSlowProperties, 1);
2081 __ pop(result_register());
2082 }
2083
2084 // Record source code position before IC call.
2085 SetSourcePosition(expr->position());
2086 // Call keyed store IC.
2087 // The arguments are:
2088 // - a0 is the value,
2089 // - a1 is the key,
2090 // - a2 is the receiver.
2091 __ mov(a0, result_register());
2092 __ pop(a1); // Key.
2093 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2094 // receiver into fast case.
2095 if (expr->ends_initialization_block()) {
2096 __ lw(a2, MemOperand(sp));
2097 } else {
2098 __ pop(a2);
2099 }
2100
2101 Handle<Code> ic = is_strict_mode()
2102 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
2103 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002104 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002105
2106 // If the assignment ends an initialization block, revert to fast case.
2107 if (expr->ends_initialization_block()) {
2108 __ push(v0); // Result of assignment, saved even if not needed.
2109 // Receiver is under the result value.
2110 __ lw(t0, MemOperand(sp, kPointerSize));
2111 __ push(t0);
2112 __ CallRuntime(Runtime::kToFastProperties, 1);
2113 __ pop(v0);
2114 __ Drop(1);
2115 }
2116 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2117 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002118}
2119
2120
2121void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002122 Comment cmnt(masm_, "[ Property");
2123 Expression* key = expr->key();
2124
2125 if (key->IsPropertyName()) {
2126 VisitForAccumulatorValue(expr->obj());
2127 EmitNamedPropertyLoad(expr);
2128 context()->Plug(v0);
2129 } else {
2130 VisitForStackValue(expr->obj());
2131 VisitForAccumulatorValue(expr->key());
2132 __ pop(a1);
2133 EmitKeyedPropertyLoad(expr);
2134 context()->Plug(v0);
2135 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002136}
2137
lrn@chromium.org7516f052011-03-30 08:52:27 +00002138
ager@chromium.org5c838252010-02-19 08:53:10 +00002139void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002140 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002141 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002142 // Code common for calls using the IC.
2143 ZoneList<Expression*>* args = expr->arguments();
2144 int arg_count = args->length();
2145 { PreservePositionScope scope(masm()->positions_recorder());
2146 for (int i = 0; i < arg_count; i++) {
2147 VisitForStackValue(args->at(i));
2148 }
2149 __ li(a2, Operand(name));
2150 }
2151 // Record source position for debugger.
2152 SetSourcePosition(expr->position());
2153 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002154 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002155 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002156 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002157 RecordJSReturnSite(expr);
2158 // Restore context register.
2159 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2160 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002161}
2162
2163
lrn@chromium.org7516f052011-03-30 08:52:27 +00002164void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002165 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002166 // Load the key.
2167 VisitForAccumulatorValue(key);
2168
2169 // Swap the name of the function and the receiver on the stack to follow
2170 // the calling convention for call ICs.
2171 __ pop(a1);
2172 __ push(v0);
2173 __ push(a1);
2174
2175 // Code common for calls using the IC.
2176 ZoneList<Expression*>* args = expr->arguments();
2177 int arg_count = args->length();
2178 { PreservePositionScope scope(masm()->positions_recorder());
2179 for (int i = 0; i < arg_count; i++) {
2180 VisitForStackValue(args->at(i));
2181 }
2182 }
2183 // Record source position for debugger.
2184 SetSourcePosition(expr->position());
2185 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002186 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002187 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002188 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002189 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002190 RecordJSReturnSite(expr);
2191 // Restore context register.
2192 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2193 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002194}
2195
2196
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002197void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002198 // Code common for calls using the call stub.
2199 ZoneList<Expression*>* args = expr->arguments();
2200 int arg_count = args->length();
2201 { PreservePositionScope scope(masm()->positions_recorder());
2202 for (int i = 0; i < arg_count; i++) {
2203 VisitForStackValue(args->at(i));
2204 }
2205 }
2206 // Record source position for debugger.
2207 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002208 CallFunctionStub stub(arg_count, flags);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002209 __ CallStub(&stub);
2210 RecordJSReturnSite(expr);
2211 // Restore context register.
2212 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2213 context()->DropAndPlug(1, v0);
2214}
2215
2216
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002217void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002218 // Push copy of the first argument or undefined if it doesn't exist.
2219 if (arg_count > 0) {
2220 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2221 } else {
2222 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2223 }
2224 __ push(a1);
2225
2226 // Push the receiver of the enclosing function and do runtime call.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002227 int receiver_offset = 2 + info_->scope()->num_parameters();
2228 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002229 __ push(a1);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002230 // Push the strict mode flag. In harmony mode every eval call
2231 // is a strict mode eval call.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002232 StrictModeFlag strict_mode =
2233 FLAG_harmony_scoping ? kStrictMode : strict_mode_flag();
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002234 __ li(a1, Operand(Smi::FromInt(strict_mode)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002235 __ push(a1);
2236
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002237 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 4);
ager@chromium.org5c838252010-02-19 08:53:10 +00002238}
2239
2240
2241void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002242#ifdef DEBUG
2243 // We want to verify that RecordJSReturnSite gets called on all paths
2244 // through this function. Avoid early returns.
2245 expr->return_is_recorded_ = false;
2246#endif
2247
2248 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002249 Expression* callee = expr->expression();
2250 VariableProxy* proxy = callee->AsVariableProxy();
2251 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002252
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002253 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002254 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2255 // resolve the function we need to call and the receiver of the
2256 // call. Then we call the resolved function using the given
2257 // arguments.
2258 ZoneList<Expression*>* args = expr->arguments();
2259 int arg_count = args->length();
2260
2261 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002262 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002263 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2264 __ push(a2); // Reserved receiver slot.
2265
2266 // Push the arguments.
2267 for (int i = 0; i < arg_count; i++) {
2268 VisitForStackValue(args->at(i));
2269 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002270
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002271 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002272 // resolve eval.
2273 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2274 __ push(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002275 EmitResolvePossiblyDirectEval(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002276
2277 // The runtime call returns a pair of values in v0 (function) and
2278 // v1 (receiver). Touch up the stack with the right values.
2279 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2280 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2281 }
2282 // Record source position for debugger.
2283 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002284 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002285 __ CallStub(&stub);
2286 RecordJSReturnSite(expr);
2287 // Restore context register.
2288 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2289 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002290 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002291 // Push global object as receiver for the call IC.
2292 __ lw(a0, GlobalObjectOperand());
2293 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002294 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2295 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002296 // Call to a lookup slot (dynamically introduced variable).
2297 Label slow, done;
2298
2299 { PreservePositionScope scope(masm()->positions_recorder());
2300 // Generate code for loading from variables potentially shadowed
2301 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002302 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002303 }
2304
2305 __ bind(&slow);
2306 // Call the runtime to find the function to call (returned in v0)
2307 // and the object holding it (returned in v1).
2308 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002309 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002310 __ push(a2);
2311 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2312 __ Push(v0, v1); // Function, receiver.
2313
2314 // If fast case code has been generated, emit code to push the
2315 // function and receiver and have the slow path jump around this
2316 // code.
2317 if (done.is_linked()) {
2318 Label call;
2319 __ Branch(&call);
2320 __ bind(&done);
2321 // Push function.
2322 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002323 // The receiver is implicitly the global receiver. Indicate this
2324 // by passing the hole to the call function stub.
2325 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002326 __ push(a1);
2327 __ bind(&call);
2328 }
2329
danno@chromium.org40cb8782011-05-25 07:58:50 +00002330 // The receiver is either the global receiver or an object found
2331 // by LoadContextSlot. That object could be the hole if the
2332 // receiver is implicitly the global object.
2333 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002334 } else if (property != NULL) {
2335 { PreservePositionScope scope(masm()->positions_recorder());
2336 VisitForStackValue(property->obj());
2337 }
2338 if (property->key()->IsPropertyName()) {
2339 EmitCallWithIC(expr,
2340 property->key()->AsLiteral()->handle(),
2341 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002342 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002343 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002344 }
2345 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002346 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002347 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002348 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002349 }
2350 // Load global receiver object.
2351 __ lw(a1, GlobalObjectOperand());
2352 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2353 __ push(a1);
2354 // Emit function call.
2355 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2356 }
2357
2358#ifdef DEBUG
2359 // RecordJSReturnSite should have been called.
2360 ASSERT(expr->return_is_recorded_);
2361#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002362}
2363
2364
2365void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002366 Comment cmnt(masm_, "[ CallNew");
2367 // According to ECMA-262, section 11.2.2, page 44, the function
2368 // expression in new calls must be evaluated before the
2369 // arguments.
2370
2371 // Push constructor on the stack. If it's not a function it's used as
2372 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2373 // ignored.
2374 VisitForStackValue(expr->expression());
2375
2376 // Push the arguments ("left-to-right") on the stack.
2377 ZoneList<Expression*>* args = expr->arguments();
2378 int arg_count = args->length();
2379 for (int i = 0; i < arg_count; i++) {
2380 VisitForStackValue(args->at(i));
2381 }
2382
2383 // Call the construct call builtin that handles allocation and
2384 // constructor invocation.
2385 SetSourcePosition(expr->position());
2386
2387 // Load function and argument count into a1 and a0.
2388 __ li(a0, Operand(arg_count));
2389 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2390
2391 Handle<Code> construct_builtin =
2392 isolate()->builtins()->JSConstructCall();
2393 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2394 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002395}
2396
2397
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002398void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2399 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002400 ASSERT(args->length() == 1);
2401
2402 VisitForAccumulatorValue(args->at(0));
2403
2404 Label materialize_true, materialize_false;
2405 Label* if_true = NULL;
2406 Label* if_false = NULL;
2407 Label* fall_through = NULL;
2408 context()->PrepareTest(&materialize_true, &materialize_false,
2409 &if_true, &if_false, &fall_through);
2410
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002411 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002412 __ And(t0, v0, Operand(kSmiTagMask));
2413 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2414
2415 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002416}
2417
2418
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002419void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2420 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002421 ASSERT(args->length() == 1);
2422
2423 VisitForAccumulatorValue(args->at(0));
2424
2425 Label materialize_true, materialize_false;
2426 Label* if_true = NULL;
2427 Label* if_false = NULL;
2428 Label* fall_through = NULL;
2429 context()->PrepareTest(&materialize_true, &materialize_false,
2430 &if_true, &if_false, &fall_through);
2431
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002432 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002433 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2434 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2435
2436 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002437}
2438
2439
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002440void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2441 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002442 ASSERT(args->length() == 1);
2443
2444 VisitForAccumulatorValue(args->at(0));
2445
2446 Label materialize_true, materialize_false;
2447 Label* if_true = NULL;
2448 Label* if_false = NULL;
2449 Label* fall_through = NULL;
2450 context()->PrepareTest(&materialize_true, &materialize_false,
2451 &if_true, &if_false, &fall_through);
2452
2453 __ JumpIfSmi(v0, if_false);
2454 __ LoadRoot(at, Heap::kNullValueRootIndex);
2455 __ Branch(if_true, eq, v0, Operand(at));
2456 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2457 // Undetectable objects behave like undefined when tested with typeof.
2458 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2459 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2460 __ Branch(if_false, ne, at, Operand(zero_reg));
2461 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002462 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002463 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002464 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2465 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002466
2467 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002468}
2469
2470
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002471void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2472 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002473 ASSERT(args->length() == 1);
2474
2475 VisitForAccumulatorValue(args->at(0));
2476
2477 Label materialize_true, materialize_false;
2478 Label* if_true = NULL;
2479 Label* if_false = NULL;
2480 Label* fall_through = NULL;
2481 context()->PrepareTest(&materialize_true, &materialize_false,
2482 &if_true, &if_false, &fall_through);
2483
2484 __ JumpIfSmi(v0, if_false);
2485 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002486 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002487 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002488 if_true, if_false, fall_through);
2489
2490 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002491}
2492
2493
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002494void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2495 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002496 ASSERT(args->length() == 1);
2497
2498 VisitForAccumulatorValue(args->at(0));
2499
2500 Label materialize_true, materialize_false;
2501 Label* if_true = NULL;
2502 Label* if_false = NULL;
2503 Label* fall_through = NULL;
2504 context()->PrepareTest(&materialize_true, &materialize_false,
2505 &if_true, &if_false, &fall_through);
2506
2507 __ JumpIfSmi(v0, if_false);
2508 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2509 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2510 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002511 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002512 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2513
2514 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002515}
2516
2517
2518void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002519 CallRuntime* expr) {
2520 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002521 ASSERT(args->length() == 1);
2522
2523 VisitForAccumulatorValue(args->at(0));
2524
2525 Label materialize_true, materialize_false;
2526 Label* if_true = NULL;
2527 Label* if_false = NULL;
2528 Label* fall_through = NULL;
2529 context()->PrepareTest(&materialize_true, &materialize_false,
2530 &if_true, &if_false, &fall_through);
2531
2532 if (FLAG_debug_code) __ AbortIfSmi(v0);
2533
2534 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2535 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2536 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2537 __ Branch(if_true, ne, t0, Operand(zero_reg));
2538
2539 // Check for fast case object. Generate false result for slow case object.
2540 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2541 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2542 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2543 __ Branch(if_false, eq, a2, Operand(t0));
2544
2545 // Look for valueOf symbol in the descriptor array, and indicate false if
2546 // found. The type is not checked, so if it is a transition it is a false
2547 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002548 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002549 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2550 // t0: descriptor array
2551 // a3: length of descriptor array
2552 // Calculate the end of the descriptor array.
2553 STATIC_ASSERT(kSmiTag == 0);
2554 STATIC_ASSERT(kSmiTagSize == 1);
2555 STATIC_ASSERT(kPointerSize == 4);
2556 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2557 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2558 __ Addu(a2, a2, t1);
2559
2560 // Calculate location of the first key name.
2561 __ Addu(t0,
2562 t0,
2563 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2564 DescriptorArray::kFirstIndex * kPointerSize));
2565 // Loop through all the keys in the descriptor array. If one of these is the
2566 // symbol valueOf the result is false.
2567 Label entry, loop;
2568 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2569 // used in the loop below.
2570 __ li(t2, Operand(FACTORY->value_of_symbol()));
2571 __ jmp(&entry);
2572 __ bind(&loop);
2573 __ lw(a3, MemOperand(t0, 0));
2574 __ Branch(if_false, eq, a3, Operand(t2));
2575 __ Addu(t0, t0, Operand(kPointerSize));
2576 __ bind(&entry);
2577 __ Branch(&loop, ne, t0, Operand(a2));
2578
2579 // If a valueOf property is not found on the object check that it's
2580 // prototype is the un-modified String prototype. If not result is false.
2581 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2582 __ JumpIfSmi(a2, if_false);
2583 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2584 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2585 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2586 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2587 __ Branch(if_false, ne, a2, Operand(a3));
2588
2589 // Set the bit in the map to indicate that it has been checked safe for
2590 // default valueOf and set true result.
2591 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2592 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2593 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2594 __ jmp(if_true);
2595
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002596 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002597 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002598}
2599
2600
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002601void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2602 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002603 ASSERT(args->length() == 1);
2604
2605 VisitForAccumulatorValue(args->at(0));
2606
2607 Label materialize_true, materialize_false;
2608 Label* if_true = NULL;
2609 Label* if_false = NULL;
2610 Label* fall_through = NULL;
2611 context()->PrepareTest(&materialize_true, &materialize_false,
2612 &if_true, &if_false, &fall_through);
2613
2614 __ JumpIfSmi(v0, if_false);
2615 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002616 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002617 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2618 __ Branch(if_false);
2619
2620 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002621}
2622
2623
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002624void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2625 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002626 ASSERT(args->length() == 1);
2627
2628 VisitForAccumulatorValue(args->at(0));
2629
2630 Label materialize_true, materialize_false;
2631 Label* if_true = NULL;
2632 Label* if_false = NULL;
2633 Label* fall_through = NULL;
2634 context()->PrepareTest(&materialize_true, &materialize_false,
2635 &if_true, &if_false, &fall_through);
2636
2637 __ JumpIfSmi(v0, if_false);
2638 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002639 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002640 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2641 if_true, if_false, fall_through);
2642
2643 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002644}
2645
2646
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002647void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2648 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002649 ASSERT(args->length() == 1);
2650
2651 VisitForAccumulatorValue(args->at(0));
2652
2653 Label materialize_true, materialize_false;
2654 Label* if_true = NULL;
2655 Label* if_false = NULL;
2656 Label* fall_through = NULL;
2657 context()->PrepareTest(&materialize_true, &materialize_false,
2658 &if_true, &if_false, &fall_through);
2659
2660 __ JumpIfSmi(v0, if_false);
2661 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002662 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002663 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2664
2665 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002666}
2667
2668
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002669void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2670 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002671
2672 Label materialize_true, materialize_false;
2673 Label* if_true = NULL;
2674 Label* if_false = NULL;
2675 Label* fall_through = NULL;
2676 context()->PrepareTest(&materialize_true, &materialize_false,
2677 &if_true, &if_false, &fall_through);
2678
2679 // Get the frame pointer for the calling frame.
2680 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2681
2682 // Skip the arguments adaptor frame if it exists.
2683 Label check_frame_marker;
2684 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2685 __ Branch(&check_frame_marker, ne,
2686 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2687 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2688
2689 // Check the marker in the calling frame.
2690 __ bind(&check_frame_marker);
2691 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002692 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002693 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2694 if_true, if_false, fall_through);
2695
2696 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002697}
2698
2699
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002700void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2701 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002702 ASSERT(args->length() == 2);
2703
2704 // Load the two objects into registers and perform the comparison.
2705 VisitForStackValue(args->at(0));
2706 VisitForAccumulatorValue(args->at(1));
2707
2708 Label materialize_true, materialize_false;
2709 Label* if_true = NULL;
2710 Label* if_false = NULL;
2711 Label* fall_through = NULL;
2712 context()->PrepareTest(&materialize_true, &materialize_false,
2713 &if_true, &if_false, &fall_through);
2714
2715 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002716 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002717 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2718
2719 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002720}
2721
2722
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002723void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2724 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002725 ASSERT(args->length() == 1);
2726
2727 // ArgumentsAccessStub expects the key in a1 and the formal
2728 // parameter count in a0.
2729 VisitForAccumulatorValue(args->at(0));
2730 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002731 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002732 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2733 __ CallStub(&stub);
2734 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002735}
2736
2737
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002738void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2739 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002740 Label exit;
2741 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002742 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002743
2744 // Check if the calling frame is an arguments adaptor frame.
2745 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2746 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2747 __ Branch(&exit, ne, a3,
2748 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2749
2750 // Arguments adaptor case: Read the arguments length from the
2751 // adaptor frame.
2752 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2753
2754 __ bind(&exit);
2755 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002756}
2757
2758
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002759void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2760 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002761 ASSERT(args->length() == 1);
2762 Label done, null, function, non_function_constructor;
2763
2764 VisitForAccumulatorValue(args->at(0));
2765
2766 // If the object is a smi, we return null.
2767 __ JumpIfSmi(v0, &null);
2768
2769 // Check that the object is a JS object but take special care of JS
2770 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002771 // Assume that there are only two callable types, and one of them is at
2772 // either end of the type range for JS object types. Saves extra comparisons.
2773 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002774 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002775 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002776
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002777 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2778 FIRST_SPEC_OBJECT_TYPE + 1);
2779 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002780
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002781 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2782 LAST_SPEC_OBJECT_TYPE - 1);
2783 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2784 // Assume that there is no larger type.
2785 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2786
2787 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002788 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2789 __ GetObjectType(v0, a1, a1);
2790 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2791
2792 // v0 now contains the constructor function. Grab the
2793 // instance class name from there.
2794 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2795 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2796 __ Branch(&done);
2797
2798 // Functions have class 'Function'.
2799 __ bind(&function);
2800 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2801 __ jmp(&done);
2802
2803 // Objects with a non-function constructor have class 'Object'.
2804 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002805 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002806 __ jmp(&done);
2807
2808 // Non-JS objects have class null.
2809 __ bind(&null);
2810 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2811
2812 // All done.
2813 __ bind(&done);
2814
2815 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002816}
2817
2818
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002819void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002820 // Conditionally generate a log call.
2821 // Args:
2822 // 0 (literal string): The type of logging (corresponds to the flags).
2823 // This is used to determine whether or not to generate the log call.
2824 // 1 (string): Format string. Access the string at argument index 2
2825 // with '%2s' (see Logger::LogRuntime for all the formats).
2826 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002827 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002828 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002829 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2830 VisitForStackValue(args->at(1));
2831 VisitForStackValue(args->at(2));
2832 __ CallRuntime(Runtime::kLog, 2);
2833 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002834
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002835 // Finally, we're expected to leave a value on the top of the stack.
2836 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2837 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002838}
2839
2840
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002841void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
2842 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002843 Label slow_allocate_heapnumber;
2844 Label heapnumber_allocated;
2845
2846 // Save the new heap number in callee-saved register s0, since
2847 // we call out to external C code below.
2848 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2849 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2850 __ jmp(&heapnumber_allocated);
2851
2852 __ bind(&slow_allocate_heapnumber);
2853
2854 // Allocate a heap number.
2855 __ CallRuntime(Runtime::kNumberAlloc, 0);
2856 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2857
2858 __ bind(&heapnumber_allocated);
2859
2860 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2861 // by computing:
2862 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2863 if (CpuFeatures::IsSupported(FPU)) {
2864 __ PrepareCallCFunction(1, a0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002865 __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
2866 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002867 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2868
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002869 CpuFeatures::Scope scope(FPU);
2870 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2871 __ li(a1, Operand(0x41300000));
2872 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002873 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002874 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002875 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002876 // Subtract and store the result in the heap number.
2877 __ sub_d(f0, f12, f14);
2878 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2879 __ mov(v0, s0);
2880 } else {
2881 __ PrepareCallCFunction(2, a0);
2882 __ mov(a0, s0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002883 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
2884 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002885 __ CallCFunction(
2886 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2887 }
2888
2889 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002890}
2891
2892
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002893void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002894 // Load the arguments on the stack and call the stub.
2895 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002896 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002897 ASSERT(args->length() == 3);
2898 VisitForStackValue(args->at(0));
2899 VisitForStackValue(args->at(1));
2900 VisitForStackValue(args->at(2));
2901 __ CallStub(&stub);
2902 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002903}
2904
2905
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002906void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002907 // Load the arguments on the stack and call the stub.
2908 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002909 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002910 ASSERT(args->length() == 4);
2911 VisitForStackValue(args->at(0));
2912 VisitForStackValue(args->at(1));
2913 VisitForStackValue(args->at(2));
2914 VisitForStackValue(args->at(3));
2915 __ CallStub(&stub);
2916 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002917}
2918
2919
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002920void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
2921 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002922 ASSERT(args->length() == 1);
2923
2924 VisitForAccumulatorValue(args->at(0)); // Load the object.
2925
2926 Label done;
2927 // If the object is a smi return the object.
2928 __ JumpIfSmi(v0, &done);
2929 // If the object is not a value type, return the object.
2930 __ GetObjectType(v0, a1, a1);
2931 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2932
2933 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2934
2935 __ bind(&done);
2936 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002937}
2938
2939
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002940void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002941 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002942 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002943 ASSERT(args->length() == 2);
2944 VisitForStackValue(args->at(0));
2945 VisitForStackValue(args->at(1));
2946 MathPowStub stub;
2947 __ CallStub(&stub);
2948 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002949}
2950
2951
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002952void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
2953 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002954 ASSERT(args->length() == 2);
2955
2956 VisitForStackValue(args->at(0)); // Load the object.
2957 VisitForAccumulatorValue(args->at(1)); // Load the value.
2958 __ pop(a1); // v0 = value. a1 = object.
2959
2960 Label done;
2961 // If the object is a smi, return the value.
2962 __ JumpIfSmi(a1, &done);
2963
2964 // If the object is not a value type, return the value.
2965 __ GetObjectType(a1, a2, a2);
2966 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2967
2968 // Store the value.
2969 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2970 // Update the write barrier. Save the value as it will be
2971 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002972 __ mov(a2, v0);
2973 __ RecordWriteField(
2974 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002975
2976 __ bind(&done);
2977 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002978}
2979
2980
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002981void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
2982 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002983 ASSERT_EQ(args->length(), 1);
2984
2985 // Load the argument on the stack and call the stub.
2986 VisitForStackValue(args->at(0));
2987
2988 NumberToStringStub stub;
2989 __ CallStub(&stub);
2990 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002991}
2992
2993
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002994void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
2995 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002996 ASSERT(args->length() == 1);
2997
2998 VisitForAccumulatorValue(args->at(0));
2999
3000 Label done;
3001 StringCharFromCodeGenerator generator(v0, a1);
3002 generator.GenerateFast(masm_);
3003 __ jmp(&done);
3004
3005 NopRuntimeCallHelper call_helper;
3006 generator.GenerateSlow(masm_, call_helper);
3007
3008 __ bind(&done);
3009 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003010}
3011
3012
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003013void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3014 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003015 ASSERT(args->length() == 2);
3016
3017 VisitForStackValue(args->at(0));
3018 VisitForAccumulatorValue(args->at(1));
3019 __ mov(a0, result_register());
3020
3021 Register object = a1;
3022 Register index = a0;
3023 Register scratch = a2;
3024 Register result = v0;
3025
3026 __ pop(object);
3027
3028 Label need_conversion;
3029 Label index_out_of_range;
3030 Label done;
3031 StringCharCodeAtGenerator generator(object,
3032 index,
3033 scratch,
3034 result,
3035 &need_conversion,
3036 &need_conversion,
3037 &index_out_of_range,
3038 STRING_INDEX_IS_NUMBER);
3039 generator.GenerateFast(masm_);
3040 __ jmp(&done);
3041
3042 __ bind(&index_out_of_range);
3043 // When the index is out of range, the spec requires us to return
3044 // NaN.
3045 __ LoadRoot(result, Heap::kNanValueRootIndex);
3046 __ jmp(&done);
3047
3048 __ bind(&need_conversion);
3049 // Load the undefined value into the result register, which will
3050 // trigger conversion.
3051 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3052 __ jmp(&done);
3053
3054 NopRuntimeCallHelper call_helper;
3055 generator.GenerateSlow(masm_, call_helper);
3056
3057 __ bind(&done);
3058 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003059}
3060
3061
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003062void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3063 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003064 ASSERT(args->length() == 2);
3065
3066 VisitForStackValue(args->at(0));
3067 VisitForAccumulatorValue(args->at(1));
3068 __ mov(a0, result_register());
3069
3070 Register object = a1;
3071 Register index = a0;
3072 Register scratch1 = a2;
3073 Register scratch2 = a3;
3074 Register result = v0;
3075
3076 __ pop(object);
3077
3078 Label need_conversion;
3079 Label index_out_of_range;
3080 Label done;
3081 StringCharAtGenerator generator(object,
3082 index,
3083 scratch1,
3084 scratch2,
3085 result,
3086 &need_conversion,
3087 &need_conversion,
3088 &index_out_of_range,
3089 STRING_INDEX_IS_NUMBER);
3090 generator.GenerateFast(masm_);
3091 __ jmp(&done);
3092
3093 __ bind(&index_out_of_range);
3094 // When the index is out of range, the spec requires us to return
3095 // the empty string.
3096 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3097 __ jmp(&done);
3098
3099 __ bind(&need_conversion);
3100 // Move smi zero into the result register, which will trigger
3101 // conversion.
3102 __ li(result, Operand(Smi::FromInt(0)));
3103 __ jmp(&done);
3104
3105 NopRuntimeCallHelper call_helper;
3106 generator.GenerateSlow(masm_, call_helper);
3107
3108 __ bind(&done);
3109 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003110}
3111
3112
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003113void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3114 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003115 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003116 VisitForStackValue(args->at(0));
3117 VisitForStackValue(args->at(1));
3118
3119 StringAddStub stub(NO_STRING_ADD_FLAGS);
3120 __ CallStub(&stub);
3121 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003122}
3123
3124
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003125void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3126 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003127 ASSERT_EQ(2, args->length());
3128
3129 VisitForStackValue(args->at(0));
3130 VisitForStackValue(args->at(1));
3131
3132 StringCompareStub stub;
3133 __ CallStub(&stub);
3134 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003135}
3136
3137
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003138void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003139 // Load the argument on the stack and call the stub.
3140 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3141 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003142 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003143 ASSERT(args->length() == 1);
3144 VisitForStackValue(args->at(0));
3145 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3146 __ CallStub(&stub);
3147 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003148}
3149
3150
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003151void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003152 // Load the argument on the stack and call the stub.
3153 TranscendentalCacheStub stub(TranscendentalCache::COS,
3154 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003155 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003156 ASSERT(args->length() == 1);
3157 VisitForStackValue(args->at(0));
3158 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3159 __ CallStub(&stub);
3160 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003161}
3162
3163
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003164void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003165 // Load the argument on the stack and call the stub.
3166 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3167 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003168 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003169 ASSERT(args->length() == 1);
3170 VisitForStackValue(args->at(0));
3171 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3172 __ CallStub(&stub);
3173 context()->Plug(v0);
3174}
3175
3176
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003177void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003178 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003179 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003180 ASSERT(args->length() == 1);
3181 VisitForStackValue(args->at(0));
3182 __ CallRuntime(Runtime::kMath_sqrt, 1);
3183 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003184}
3185
3186
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003187void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3188 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003189 ASSERT(args->length() >= 2);
3190
3191 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3192 for (int i = 0; i < arg_count + 1; i++) {
3193 VisitForStackValue(args->at(i));
3194 }
3195 VisitForAccumulatorValue(args->last()); // Function.
3196
3197 // InvokeFunction requires the function in a1. Move it in there.
3198 __ mov(a1, result_register());
3199 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003200 __ InvokeFunction(a1, count, CALL_FUNCTION,
3201 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003202 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3203 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003204}
3205
3206
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003207void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003208 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003209 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003210 ASSERT(args->length() == 3);
3211 VisitForStackValue(args->at(0));
3212 VisitForStackValue(args->at(1));
3213 VisitForStackValue(args->at(2));
3214 __ CallStub(&stub);
3215 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003216}
3217
3218
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003219void FullCodeGenerator::EmitSwapElements(CallRuntime* expr) {
3220 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003221 ASSERT(args->length() == 3);
3222 VisitForStackValue(args->at(0));
3223 VisitForStackValue(args->at(1));
3224 VisitForStackValue(args->at(2));
3225 Label done;
3226 Label slow_case;
3227 Register object = a0;
3228 Register index1 = a1;
3229 Register index2 = a2;
3230 Register elements = a3;
3231 Register scratch1 = t0;
3232 Register scratch2 = t1;
3233
3234 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3235 // Fetch the map and check if array is in fast case.
3236 // Check that object doesn't require security checks and
3237 // has no indexed interceptor.
3238 __ GetObjectType(object, scratch1, scratch2);
3239 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3240 // Map is now in scratch1.
3241
3242 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3243 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3244 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3245
3246 // Check the object's elements are in fast case and writable.
3247 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3248 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3249 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3250 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3251
3252 // Check that both indices are smis.
3253 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3254 __ lw(index2, MemOperand(sp, 0));
3255 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3256
3257 // Check that both indices are valid.
3258 Label not_hi;
3259 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3260 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3261 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3262 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3263 __ bind(&not_hi);
3264
3265 // Bring the address of the elements into index1 and index2.
3266 __ Addu(scratch1, elements,
3267 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3268 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3269 __ Addu(index1, scratch1, index1);
3270 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3271 __ Addu(index2, scratch1, index2);
3272
3273 // Swap elements.
3274 __ lw(scratch1, MemOperand(index1, 0));
3275 __ lw(scratch2, MemOperand(index2, 0));
3276 __ sw(scratch1, MemOperand(index2, 0));
3277 __ sw(scratch2, MemOperand(index1, 0));
3278
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003279 Label no_remembered_set;
3280 __ CheckPageFlag(elements,
3281 scratch1,
3282 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3283 ne,
3284 &no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003285 // Possible optimization: do a check that both values are Smis
3286 // (or them and test against Smi mask).
3287
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003288 // We are swapping two objects in an array and the incremental marker never
3289 // pauses in the middle of scanning a single object. Therefore the
3290 // incremental marker is not disturbed, so we don't need to call the
3291 // RecordWrite stub that notifies the incremental marker.
3292 __ RememberedSetHelper(elements,
3293 index1,
3294 scratch2,
3295 kDontSaveFPRegs,
3296 MacroAssembler::kFallThroughAtEnd);
3297 __ RememberedSetHelper(elements,
3298 index2,
3299 scratch2,
3300 kDontSaveFPRegs,
3301 MacroAssembler::kFallThroughAtEnd);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003302
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003303 __ bind(&no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003304 // We are done. Drop elements from the stack, and return undefined.
3305 __ Drop(3);
3306 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3307 __ jmp(&done);
3308
3309 __ bind(&slow_case);
3310 __ CallRuntime(Runtime::kSwapElements, 3);
3311
3312 __ bind(&done);
3313 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003314}
3315
3316
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003317void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3318 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003319 ASSERT_EQ(2, args->length());
3320
3321 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3322 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3323
3324 Handle<FixedArray> jsfunction_result_caches(
3325 isolate()->global_context()->jsfunction_result_caches());
3326 if (jsfunction_result_caches->length() <= cache_id) {
3327 __ Abort("Attempt to use undefined cache.");
3328 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3329 context()->Plug(v0);
3330 return;
3331 }
3332
3333 VisitForAccumulatorValue(args->at(1));
3334
3335 Register key = v0;
3336 Register cache = a1;
3337 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3338 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3339 __ lw(cache,
3340 ContextOperand(
3341 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3342 __ lw(cache,
3343 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3344
3345
3346 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003347 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003348 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3349 // a2 now holds finger offset as a smi.
3350 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3351 // a3 now points to the start of fixed array elements.
3352 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3353 __ addu(a3, a3, at);
3354 // a3 now points to key of indexed element of cache.
3355 __ lw(a2, MemOperand(a3));
3356 __ Branch(&not_found, ne, key, Operand(a2));
3357
3358 __ lw(v0, MemOperand(a3, kPointerSize));
3359 __ Branch(&done);
3360
3361 __ bind(&not_found);
3362 // Call runtime to perform the lookup.
3363 __ Push(cache, key);
3364 __ CallRuntime(Runtime::kGetFromCache, 2);
3365
3366 __ bind(&done);
3367 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003368}
3369
3370
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003371void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3372 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003373 ASSERT_EQ(2, args->length());
3374
3375 Register right = v0;
3376 Register left = a1;
3377 Register tmp = a2;
3378 Register tmp2 = a3;
3379
3380 VisitForStackValue(args->at(0));
3381 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3382 __ pop(left);
3383
3384 Label done, fail, ok;
3385 __ Branch(&ok, eq, left, Operand(right));
3386 // Fail if either is a non-HeapObject.
3387 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003388 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003389 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3390 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3391 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3392 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3393 __ Branch(&fail, ne, tmp, Operand(tmp2));
3394 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3395 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3396 __ Branch(&ok, eq, tmp, Operand(tmp2));
3397 __ bind(&fail);
3398 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3399 __ jmp(&done);
3400 __ bind(&ok);
3401 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3402 __ bind(&done);
3403
3404 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003405}
3406
3407
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003408void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3409 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003410 VisitForAccumulatorValue(args->at(0));
3411
3412 Label materialize_true, materialize_false;
3413 Label* if_true = NULL;
3414 Label* if_false = NULL;
3415 Label* fall_through = NULL;
3416 context()->PrepareTest(&materialize_true, &materialize_false,
3417 &if_true, &if_false, &fall_through);
3418
3419 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3420 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3421
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003422 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003423 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3424
3425 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003426}
3427
3428
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003429void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3430 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003431 ASSERT(args->length() == 1);
3432 VisitForAccumulatorValue(args->at(0));
3433
3434 if (FLAG_debug_code) {
3435 __ AbortIfNotString(v0);
3436 }
3437
3438 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3439 __ IndexFromHash(v0, v0);
3440
3441 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003442}
3443
3444
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003445void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003446 Label bailout, done, one_char_separator, long_separator,
3447 non_trivial_array, not_size_one_array, loop,
3448 empty_separator_loop, one_char_separator_loop,
3449 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003450 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003451 ASSERT(args->length() == 2);
3452 VisitForStackValue(args->at(1));
3453 VisitForAccumulatorValue(args->at(0));
3454
3455 // All aliases of the same register have disjoint lifetimes.
3456 Register array = v0;
3457 Register elements = no_reg; // Will be v0.
3458 Register result = no_reg; // Will be v0.
3459 Register separator = a1;
3460 Register array_length = a2;
3461 Register result_pos = no_reg; // Will be a2.
3462 Register string_length = a3;
3463 Register string = t0;
3464 Register element = t1;
3465 Register elements_end = t2;
3466 Register scratch1 = t3;
3467 Register scratch2 = t5;
3468 Register scratch3 = t4;
3469 Register scratch4 = v1;
3470
3471 // Separator operand is on the stack.
3472 __ pop(separator);
3473
3474 // Check that the array is a JSArray.
3475 __ JumpIfSmi(array, &bailout);
3476 __ GetObjectType(array, scratch1, scratch2);
3477 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3478
3479 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003480 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003481
3482 // If the array has length zero, return the empty string.
3483 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3484 __ SmiUntag(array_length);
3485 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3486 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3487 __ Branch(&done);
3488
3489 __ bind(&non_trivial_array);
3490
3491 // Get the FixedArray containing array's elements.
3492 elements = array;
3493 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3494 array = no_reg; // End of array's live range.
3495
3496 // Check that all array elements are sequential ASCII strings, and
3497 // accumulate the sum of their lengths, as a smi-encoded value.
3498 __ mov(string_length, zero_reg);
3499 __ Addu(element,
3500 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3501 __ sll(elements_end, array_length, kPointerSizeLog2);
3502 __ Addu(elements_end, element, elements_end);
3503 // Loop condition: while (element < elements_end).
3504 // Live values in registers:
3505 // elements: Fixed array of strings.
3506 // array_length: Length of the fixed array of strings (not smi)
3507 // separator: Separator string
3508 // string_length: Accumulated sum of string lengths (smi).
3509 // element: Current array element.
3510 // elements_end: Array end.
3511 if (FLAG_debug_code) {
3512 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3513 array_length, Operand(zero_reg));
3514 }
3515 __ bind(&loop);
3516 __ lw(string, MemOperand(element));
3517 __ Addu(element, element, kPointerSize);
3518 __ JumpIfSmi(string, &bailout);
3519 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3520 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3521 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3522 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3523 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3524 __ BranchOnOverflow(&bailout, scratch3);
3525 __ Branch(&loop, lt, element, Operand(elements_end));
3526
3527 // If array_length is 1, return elements[0], a string.
3528 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3529 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3530 __ Branch(&done);
3531
3532 __ bind(&not_size_one_array);
3533
3534 // Live values in registers:
3535 // separator: Separator string
3536 // array_length: Length of the array.
3537 // string_length: Sum of string lengths (smi).
3538 // elements: FixedArray of strings.
3539
3540 // Check that the separator is a flat ASCII string.
3541 __ JumpIfSmi(separator, &bailout);
3542 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3543 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3544 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3545
3546 // Add (separator length times array_length) - separator length to the
3547 // string_length to get the length of the result string. array_length is not
3548 // smi but the other values are, so the result is a smi.
3549 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3550 __ Subu(string_length, string_length, Operand(scratch1));
3551 __ Mult(array_length, scratch1);
3552 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3553 // zero.
3554 __ mfhi(scratch2);
3555 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3556 __ mflo(scratch2);
3557 __ And(scratch3, scratch2, Operand(0x80000000));
3558 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3559 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3560 __ BranchOnOverflow(&bailout, scratch3);
3561 __ SmiUntag(string_length);
3562
3563 // Get first element in the array to free up the elements register to be used
3564 // for the result.
3565 __ Addu(element,
3566 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3567 result = elements; // End of live range for elements.
3568 elements = no_reg;
3569 // Live values in registers:
3570 // element: First array element
3571 // separator: Separator string
3572 // string_length: Length of result string (not smi)
3573 // array_length: Length of the array.
3574 __ AllocateAsciiString(result,
3575 string_length,
3576 scratch1,
3577 scratch2,
3578 elements_end,
3579 &bailout);
3580 // Prepare for looping. Set up elements_end to end of the array. Set
3581 // result_pos to the position of the result where to write the first
3582 // character.
3583 __ sll(elements_end, array_length, kPointerSizeLog2);
3584 __ Addu(elements_end, element, elements_end);
3585 result_pos = array_length; // End of live range for array_length.
3586 array_length = no_reg;
3587 __ Addu(result_pos,
3588 result,
3589 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3590
3591 // Check the length of the separator.
3592 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3593 __ li(at, Operand(Smi::FromInt(1)));
3594 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3595 __ Branch(&long_separator, gt, scratch1, Operand(at));
3596
3597 // Empty separator case.
3598 __ bind(&empty_separator_loop);
3599 // Live values in registers:
3600 // result_pos: the position to which we are currently copying characters.
3601 // element: Current array element.
3602 // elements_end: Array end.
3603
3604 // Copy next array element to the result.
3605 __ lw(string, MemOperand(element));
3606 __ Addu(element, element, kPointerSize);
3607 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3608 __ SmiUntag(string_length);
3609 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3610 __ CopyBytes(string, result_pos, string_length, scratch1);
3611 // End while (element < elements_end).
3612 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3613 ASSERT(result.is(v0));
3614 __ Branch(&done);
3615
3616 // One-character separator case.
3617 __ bind(&one_char_separator);
3618 // Replace separator with its ascii character value.
3619 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3620 // Jump into the loop after the code that copies the separator, so the first
3621 // element is not preceded by a separator.
3622 __ jmp(&one_char_separator_loop_entry);
3623
3624 __ bind(&one_char_separator_loop);
3625 // Live values in registers:
3626 // result_pos: the position to which we are currently copying characters.
3627 // element: Current array element.
3628 // elements_end: Array end.
3629 // separator: Single separator ascii char (in lower byte).
3630
3631 // Copy the separator character to the result.
3632 __ sb(separator, MemOperand(result_pos));
3633 __ Addu(result_pos, result_pos, 1);
3634
3635 // Copy next array element to the result.
3636 __ bind(&one_char_separator_loop_entry);
3637 __ lw(string, MemOperand(element));
3638 __ Addu(element, element, kPointerSize);
3639 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3640 __ SmiUntag(string_length);
3641 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3642 __ CopyBytes(string, result_pos, string_length, scratch1);
3643 // End while (element < elements_end).
3644 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3645 ASSERT(result.is(v0));
3646 __ Branch(&done);
3647
3648 // Long separator case (separator is more than one character). Entry is at the
3649 // label long_separator below.
3650 __ bind(&long_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 // separator: Separator string.
3656
3657 // Copy the separator to the result.
3658 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3659 __ SmiUntag(string_length);
3660 __ Addu(string,
3661 separator,
3662 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3663 __ CopyBytes(string, result_pos, string_length, scratch1);
3664
3665 __ bind(&long_separator);
3666 __ lw(string, MemOperand(element));
3667 __ Addu(element, element, kPointerSize);
3668 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3669 __ SmiUntag(string_length);
3670 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3671 __ CopyBytes(string, result_pos, string_length, scratch1);
3672 // End while (element < elements_end).
3673 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3674 ASSERT(result.is(v0));
3675 __ Branch(&done);
3676
3677 __ bind(&bailout);
3678 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3679 __ bind(&done);
3680 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003681}
3682
3683
ager@chromium.org5c838252010-02-19 08:53:10 +00003684void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003685 Handle<String> name = expr->name();
3686 if (name->length() > 0 && name->Get(0) == '_') {
3687 Comment cmnt(masm_, "[ InlineRuntimeCall");
3688 EmitInlineRuntimeCall(expr);
3689 return;
3690 }
3691
3692 Comment cmnt(masm_, "[ CallRuntime");
3693 ZoneList<Expression*>* args = expr->arguments();
3694
3695 if (expr->is_jsruntime()) {
3696 // Prepare for calling JS runtime function.
3697 __ lw(a0, GlobalObjectOperand());
3698 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3699 __ push(a0);
3700 }
3701
3702 // Push the arguments ("left-to-right").
3703 int arg_count = args->length();
3704 for (int i = 0; i < arg_count; i++) {
3705 VisitForStackValue(args->at(i));
3706 }
3707
3708 if (expr->is_jsruntime()) {
3709 // Call the JS runtime function.
3710 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003711 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003712 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003713 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003714 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003715 // Restore context register.
3716 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3717 } else {
3718 // Call the C runtime function.
3719 __ CallRuntime(expr->function(), arg_count);
3720 }
3721 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003722}
3723
3724
3725void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003726 switch (expr->op()) {
3727 case Token::DELETE: {
3728 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003729 Property* property = expr->expression()->AsProperty();
3730 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003731
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003732 if (property != NULL) {
3733 VisitForStackValue(property->obj());
3734 VisitForStackValue(property->key());
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003735 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3736 __ push(a1);
3737 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3738 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003739 } else if (proxy != NULL) {
3740 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003741 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003742 // but "delete this" is allowed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003743 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003744 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003745 __ lw(a2, GlobalObjectOperand());
3746 __ li(a1, Operand(var->name()));
3747 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3748 __ Push(a2, a1, a0);
3749 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3750 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003751 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003752 // Result of deleting non-global, non-dynamic variables is false.
3753 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003754 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003755 } else {
3756 // Non-global variable. Call the runtime to try to delete from the
3757 // context where the variable was introduced.
3758 __ push(context_register());
3759 __ li(a2, Operand(var->name()));
3760 __ push(a2);
3761 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3762 context()->Plug(v0);
3763 }
3764 } else {
3765 // Result of deleting non-property, non-variable reference is true.
3766 // The subexpression may have side effects.
3767 VisitForEffect(expr->expression());
3768 context()->Plug(true);
3769 }
3770 break;
3771 }
3772
3773 case Token::VOID: {
3774 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3775 VisitForEffect(expr->expression());
3776 context()->Plug(Heap::kUndefinedValueRootIndex);
3777 break;
3778 }
3779
3780 case Token::NOT: {
3781 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3782 if (context()->IsEffect()) {
3783 // Unary NOT has no side effects so it's only necessary to visit the
3784 // subexpression. Match the optimizing compiler by not branching.
3785 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003786 } else if (context()->IsTest()) {
3787 const TestContext* test = TestContext::cast(context());
3788 // The labels are swapped for the recursive call.
3789 VisitForControl(expr->expression(),
3790 test->false_label(),
3791 test->true_label(),
3792 test->fall_through());
3793 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003794 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003795 // We handle value contexts explicitly rather than simply visiting
3796 // for control and plugging the control flow into the context,
3797 // because we need to prepare a pair of extra administrative AST ids
3798 // for the optimizing compiler.
3799 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3800 Label materialize_true, materialize_false, done;
3801 VisitForControl(expr->expression(),
3802 &materialize_false,
3803 &materialize_true,
3804 &materialize_true);
3805 __ bind(&materialize_true);
3806 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3807 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3808 if (context()->IsStackValue()) __ push(v0);
3809 __ jmp(&done);
3810 __ bind(&materialize_false);
3811 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3812 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3813 if (context()->IsStackValue()) __ push(v0);
3814 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003815 }
3816 break;
3817 }
3818
3819 case Token::TYPEOF: {
3820 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3821 { StackValueContext context(this);
3822 VisitForTypeofValue(expr->expression());
3823 }
3824 __ CallRuntime(Runtime::kTypeof, 1);
3825 context()->Plug(v0);
3826 break;
3827 }
3828
3829 case Token::ADD: {
3830 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3831 VisitForAccumulatorValue(expr->expression());
3832 Label no_conversion;
3833 __ JumpIfSmi(result_register(), &no_conversion);
3834 __ mov(a0, result_register());
3835 ToNumberStub convert_stub;
3836 __ CallStub(&convert_stub);
3837 __ bind(&no_conversion);
3838 context()->Plug(result_register());
3839 break;
3840 }
3841
3842 case Token::SUB:
3843 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3844 break;
3845
3846 case Token::BIT_NOT:
3847 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3848 break;
3849
3850 default:
3851 UNREACHABLE();
3852 }
3853}
3854
3855
3856void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3857 const char* comment) {
3858 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3859 Comment cmt(masm_, comment);
3860 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3861 UnaryOverwriteMode overwrite =
3862 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003863 UnaryOpStub stub(expr->op(), overwrite);
3864 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003865 VisitForAccumulatorValue(expr->expression());
3866 SetSourcePosition(expr->position());
3867 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003868 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003869 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003870}
3871
3872
3873void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003874 Comment cmnt(masm_, "[ CountOperation");
3875 SetSourcePosition(expr->position());
3876
3877 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3878 // as the left-hand side.
3879 if (!expr->expression()->IsValidLeftHandSide()) {
3880 VisitForEffect(expr->expression());
3881 return;
3882 }
3883
3884 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003885 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003886 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3887 LhsKind assign_type = VARIABLE;
3888 Property* prop = expr->expression()->AsProperty();
3889 // In case of a property we use the uninitialized expression context
3890 // of the key to detect a named property.
3891 if (prop != NULL) {
3892 assign_type =
3893 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3894 }
3895
3896 // Evaluate expression and get value.
3897 if (assign_type == VARIABLE) {
3898 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3899 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003900 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003901 } else {
3902 // Reserve space for result of postfix operation.
3903 if (expr->is_postfix() && !context()->IsEffect()) {
3904 __ li(at, Operand(Smi::FromInt(0)));
3905 __ push(at);
3906 }
3907 if (assign_type == NAMED_PROPERTY) {
3908 // Put the object both on the stack and in the accumulator.
3909 VisitForAccumulatorValue(prop->obj());
3910 __ push(v0);
3911 EmitNamedPropertyLoad(prop);
3912 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003913 VisitForStackValue(prop->obj());
3914 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003915 __ lw(a1, MemOperand(sp, 0));
3916 __ push(v0);
3917 EmitKeyedPropertyLoad(prop);
3918 }
3919 }
3920
3921 // We need a second deoptimization point after loading the value
3922 // in case evaluating the property load my have a side effect.
3923 if (assign_type == VARIABLE) {
3924 PrepareForBailout(expr->expression(), TOS_REG);
3925 } else {
3926 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3927 }
3928
3929 // Call ToNumber only if operand is not a smi.
3930 Label no_conversion;
3931 __ JumpIfSmi(v0, &no_conversion);
3932 __ mov(a0, v0);
3933 ToNumberStub convert_stub;
3934 __ CallStub(&convert_stub);
3935 __ bind(&no_conversion);
3936
3937 // Save result for postfix expressions.
3938 if (expr->is_postfix()) {
3939 if (!context()->IsEffect()) {
3940 // Save the result on the stack. If we have a named or keyed property
3941 // we store the result under the receiver that is currently on top
3942 // of the stack.
3943 switch (assign_type) {
3944 case VARIABLE:
3945 __ push(v0);
3946 break;
3947 case NAMED_PROPERTY:
3948 __ sw(v0, MemOperand(sp, kPointerSize));
3949 break;
3950 case KEYED_PROPERTY:
3951 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3952 break;
3953 }
3954 }
3955 }
3956 __ mov(a0, result_register());
3957
3958 // Inline smi case if we are in a loop.
3959 Label stub_call, done;
3960 JumpPatchSite patch_site(masm_);
3961
3962 int count_value = expr->op() == Token::INC ? 1 : -1;
3963 __ li(a1, Operand(Smi::FromInt(count_value)));
3964
3965 if (ShouldInlineSmiCase(expr->op())) {
3966 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
3967 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
3968
3969 // We could eliminate this smi check if we split the code at
3970 // the first smi check before calling ToNumber.
3971 patch_site.EmitJumpIfSmi(v0, &done);
3972 __ bind(&stub_call);
3973 }
3974
3975 // Record position before stub call.
3976 SetSourcePosition(expr->position());
3977
danno@chromium.org40cb8782011-05-25 07:58:50 +00003978 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003979 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003980 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003981 __ bind(&done);
3982
3983 // Store the value returned in v0.
3984 switch (assign_type) {
3985 case VARIABLE:
3986 if (expr->is_postfix()) {
3987 { EffectContext context(this);
3988 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3989 Token::ASSIGN);
3990 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3991 context.Plug(v0);
3992 }
3993 // For all contexts except EffectConstant we have the result on
3994 // top of the stack.
3995 if (!context()->IsEffect()) {
3996 context()->PlugTOS();
3997 }
3998 } else {
3999 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4000 Token::ASSIGN);
4001 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4002 context()->Plug(v0);
4003 }
4004 break;
4005 case NAMED_PROPERTY: {
4006 __ mov(a0, result_register()); // Value.
4007 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4008 __ pop(a1); // Receiver.
4009 Handle<Code> ic = is_strict_mode()
4010 ? isolate()->builtins()->StoreIC_Initialize_Strict()
4011 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004012 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004013 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4014 if (expr->is_postfix()) {
4015 if (!context()->IsEffect()) {
4016 context()->PlugTOS();
4017 }
4018 } else {
4019 context()->Plug(v0);
4020 }
4021 break;
4022 }
4023 case KEYED_PROPERTY: {
4024 __ mov(a0, result_register()); // Value.
4025 __ pop(a1); // Key.
4026 __ pop(a2); // Receiver.
4027 Handle<Code> ic = is_strict_mode()
4028 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
4029 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004030 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004031 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4032 if (expr->is_postfix()) {
4033 if (!context()->IsEffect()) {
4034 context()->PlugTOS();
4035 }
4036 } else {
4037 context()->Plug(v0);
4038 }
4039 break;
4040 }
4041 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004042}
4043
4044
lrn@chromium.org7516f052011-03-30 08:52:27 +00004045void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004046 ASSERT(!context()->IsEffect());
4047 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004048 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004049 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004050 Comment cmnt(masm_, "Global variable");
4051 __ lw(a0, GlobalObjectOperand());
4052 __ li(a2, Operand(proxy->name()));
4053 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4054 // Use a regular load, not a contextual load, to avoid a reference
4055 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004056 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004057 PrepareForBailout(expr, TOS_REG);
4058 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004059 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004060 Label done, slow;
4061
4062 // Generate code for loading from variables potentially shadowed
4063 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004064 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004065
4066 __ bind(&slow);
4067 __ li(a0, Operand(proxy->name()));
4068 __ Push(cp, a0);
4069 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4070 PrepareForBailout(expr, TOS_REG);
4071 __ bind(&done);
4072
4073 context()->Plug(v0);
4074 } else {
4075 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004076 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004077 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004078}
4079
ager@chromium.org04921a82011-06-27 13:21:41 +00004080void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004081 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004082 Handle<String> check) {
4083 Label materialize_true, materialize_false;
4084 Label* if_true = NULL;
4085 Label* if_false = NULL;
4086 Label* fall_through = NULL;
4087 context()->PrepareTest(&materialize_true, &materialize_false,
4088 &if_true, &if_false, &fall_through);
4089
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004090 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004091 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004092 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004093 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004094
4095 if (check->Equals(isolate()->heap()->number_symbol())) {
4096 __ JumpIfSmi(v0, if_true);
4097 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4098 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4099 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4100 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4101 __ JumpIfSmi(v0, if_false);
4102 // Check for undetectable objects => false.
4103 __ GetObjectType(v0, v0, a1);
4104 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4105 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4106 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4107 Split(eq, a1, Operand(zero_reg),
4108 if_true, if_false, fall_through);
4109 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4110 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4111 __ Branch(if_true, eq, v0, Operand(at));
4112 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4113 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004114 } else if (FLAG_harmony_typeof &&
4115 check->Equals(isolate()->heap()->null_symbol())) {
4116 __ LoadRoot(at, Heap::kNullValueRootIndex);
4117 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004118 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4119 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4120 __ Branch(if_true, eq, v0, Operand(at));
4121 __ JumpIfSmi(v0, if_false);
4122 // Check for undetectable objects => true.
4123 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4124 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4125 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4126 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4127 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4128 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004129 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4130 __ GetObjectType(v0, v0, a1);
4131 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4132 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4133 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004134 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4135 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004136 if (!FLAG_harmony_typeof) {
4137 __ LoadRoot(at, Heap::kNullValueRootIndex);
4138 __ Branch(if_true, eq, v0, Operand(at));
4139 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004140 // Check for JS objects => true.
4141 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004142 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004143 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004144 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004145 // Check for undetectable objects => false.
4146 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4147 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4148 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4149 } else {
4150 if (if_false != fall_through) __ jmp(if_false);
4151 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004152 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004153}
4154
4155
ager@chromium.org5c838252010-02-19 08:53:10 +00004156void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004157 Comment cmnt(masm_, "[ CompareOperation");
4158 SetSourcePosition(expr->position());
4159
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004160 // First we try a fast inlined version of the compare when one of
4161 // the operands is a literal.
4162 if (TryLiteralCompare(expr)) return;
4163
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004164 // Always perform the comparison for its control flow. Pack the result
4165 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004166 Label materialize_true, materialize_false;
4167 Label* if_true = NULL;
4168 Label* if_false = NULL;
4169 Label* fall_through = NULL;
4170 context()->PrepareTest(&materialize_true, &materialize_false,
4171 &if_true, &if_false, &fall_through);
4172
ager@chromium.org04921a82011-06-27 13:21:41 +00004173 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004174 VisitForStackValue(expr->left());
4175 switch (op) {
4176 case Token::IN:
4177 VisitForStackValue(expr->right());
4178 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004179 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004180 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4181 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4182 break;
4183
4184 case Token::INSTANCEOF: {
4185 VisitForStackValue(expr->right());
4186 InstanceofStub stub(InstanceofStub::kNoFlags);
4187 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004188 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004189 // The stub returns 0 for true.
4190 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4191 break;
4192 }
4193
4194 default: {
4195 VisitForAccumulatorValue(expr->right());
4196 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004197 switch (op) {
4198 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004199 case Token::EQ:
4200 cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004201 break;
4202 case Token::LT:
4203 cc = lt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004204 break;
4205 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004206 cc = gt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004207 break;
4208 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004209 cc = le;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004210 break;
4211 case Token::GTE:
4212 cc = ge;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004213 break;
4214 case Token::IN:
4215 case Token::INSTANCEOF:
4216 default:
4217 UNREACHABLE();
4218 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004219 __ mov(a0, result_register());
4220 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004221
4222 bool inline_smi_code = ShouldInlineSmiCase(op);
4223 JumpPatchSite patch_site(masm_);
4224 if (inline_smi_code) {
4225 Label slow_case;
4226 __ Or(a2, a0, Operand(a1));
4227 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4228 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4229 __ bind(&slow_case);
4230 }
4231 // Record position and call the compare IC.
4232 SetSourcePosition(expr->position());
4233 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004234 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004235 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004236 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004237 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4238 }
4239 }
4240
4241 // Convert the result of the comparison into one expected for this
4242 // expression's context.
4243 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004244}
4245
4246
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004247void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4248 Expression* sub_expr,
4249 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004250 Label materialize_true, materialize_false;
4251 Label* if_true = NULL;
4252 Label* if_false = NULL;
4253 Label* fall_through = NULL;
4254 context()->PrepareTest(&materialize_true, &materialize_false,
4255 &if_true, &if_false, &fall_through);
4256
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004257 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004258 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004259 Heap::RootListIndex nil_value = nil == kNullValue ?
4260 Heap::kNullValueRootIndex :
4261 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004262 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004263 __ LoadRoot(a1, nil_value);
4264 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004265 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4266 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004267 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4268 Heap::kUndefinedValueRootIndex :
4269 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004270 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004271 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004272 __ Branch(if_true, eq, a0, Operand(a1));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004273 __ JumpIfSmi(a0, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004274 // It can be an undetectable object.
4275 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4276 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4277 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4278 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4279 }
4280 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004281}
4282
4283
ager@chromium.org5c838252010-02-19 08:53:10 +00004284void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004285 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4286 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004287}
4288
4289
lrn@chromium.org7516f052011-03-30 08:52:27 +00004290Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004291 return v0;
4292}
ager@chromium.org5c838252010-02-19 08:53:10 +00004293
4294
lrn@chromium.org7516f052011-03-30 08:52:27 +00004295Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004296 return cp;
4297}
4298
4299
ager@chromium.org5c838252010-02-19 08:53:10 +00004300void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004301 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4302 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004303}
4304
4305
4306void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004307 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004308}
4309
4310
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004311void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4312 Scope* declaration_scope = scope()->DeclarationScope();
4313 if (declaration_scope->is_global_scope()) {
4314 // Contexts nested in the global context have a canonical empty function
4315 // as their closure, not the anonymous closure containing the global
4316 // code. Pass a smi sentinel and let the runtime look up the empty
4317 // function.
4318 __ li(at, Operand(Smi::FromInt(0)));
4319 } else if (declaration_scope->is_eval_scope()) {
4320 // Contexts created by a call to eval have the same closure as the
4321 // context calling eval, not the anonymous closure containing the eval
4322 // code. Fetch it from the context.
4323 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4324 } else {
4325 ASSERT(declaration_scope->is_function_scope());
4326 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4327 }
4328 __ push(at);
4329}
4330
4331
ager@chromium.org5c838252010-02-19 08:53:10 +00004332// ----------------------------------------------------------------------------
4333// Non-local control flow support.
4334
4335void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004336 ASSERT(!result_register().is(a1));
4337 // Store result register while executing finally block.
4338 __ push(result_register());
4339 // Cook return address in link register to stack (smi encoded Code* delta).
4340 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4341 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004342 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004343 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4344 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004345}
4346
4347
4348void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004349 ASSERT(!result_register().is(a1));
4350 // Restore result register from stack.
4351 __ pop(a1);
4352 // Uncook return address and return.
4353 __ pop(result_register());
4354 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4355 __ sra(a1, a1, 1); // Un-smi-tag value.
4356 __ Addu(at, a1, Operand(masm_->CodeObject()));
4357 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004358}
4359
4360
4361#undef __
4362
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004363#define __ ACCESS_MASM(masm())
4364
4365FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4366 int* stack_depth,
4367 int* context_length) {
4368 // The macros used here must preserve the result register.
4369
4370 // Because the handler block contains the context of the finally
4371 // code, we can restore it directly from there for the finally code
4372 // rather than iteratively unwinding contexts via their previous
4373 // links.
4374 __ Drop(*stack_depth); // Down to the handler block.
4375 if (*context_length > 0) {
4376 // Restore the context to its dedicated register and the stack.
4377 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4378 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4379 }
4380 __ PopTryHandler();
4381 __ Call(finally_entry_);
4382
4383 *stack_depth = 0;
4384 *context_length = 0;
4385 return previous_;
4386}
4387
4388
4389#undef __
4390
ager@chromium.org5c838252010-02-19 08:53:10 +00004391} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004392
4393#endif // V8_TARGET_ARCH_MIPS