blob: a4b87664bb10a1629d8b304a7f4a536e8dfaa641 [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");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001292 if (var->binding_needs_init()) {
1293 // var->scope() may be NULL when the proxy is located in eval code and
1294 // refers to a potential outside binding. Currently those bindings are
1295 // always looked up dynamically, i.e. in that case
1296 // var->location() == LOOKUP.
1297 // always holds.
1298 ASSERT(var->scope() != NULL);
1299
1300 // Check if the binding really needs an initialization check. The check
1301 // can be skipped in the following situation: we have a LET or CONST
1302 // binding in harmony mode, both the Variable and the VariableProxy have
1303 // the same declaration scope (i.e. they are both in global code, in the
1304 // same function or in the same eval code) and the VariableProxy is in
1305 // the source physically located after the initializer of the variable.
1306 //
1307 // We cannot skip any initialization checks for CONST in non-harmony
1308 // mode because const variables may be declared but never initialized:
1309 // if (false) { const x; }; var y = x;
1310 //
1311 // The condition on the declaration scopes is a conservative check for
1312 // nested functions that access a binding and are called before the
1313 // binding is initialized:
1314 // function() { f(); let x = 1; function f() { x = 2; } }
1315 //
1316 bool skip_init_check;
1317 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1318 skip_init_check = false;
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001319 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001320 // Check that we always have valid source position.
1321 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1322 ASSERT(proxy->position() != RelocInfo::kNoPosition);
1323 skip_init_check = var->mode() != CONST &&
1324 var->initializer_position() < proxy->position();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001325 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001326
1327 if (!skip_init_check) {
1328 // Let and const need a read barrier.
1329 GetVar(v0, var);
1330 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1331 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1332 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1333 // Throw a reference error when using an uninitialized let/const
1334 // binding in harmony mode.
1335 Label done;
1336 __ Branch(&done, ne, at, Operand(zero_reg));
1337 __ li(a0, Operand(var->name()));
1338 __ push(a0);
1339 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1340 __ bind(&done);
1341 } else {
1342 // Uninitalized const bindings outside of harmony mode are unholed.
1343 ASSERT(var->mode() == CONST);
1344 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1345 __ movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1346 }
1347 context()->Plug(v0);
1348 break;
1349 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001350 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001351 context()->Plug(var);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001352 break;
1353 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001354
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001355 case Variable::LOOKUP: {
1356 Label done, slow;
1357 // Generate code for loading from variables potentially shadowed
1358 // by eval-introduced variables.
1359 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1360 __ bind(&slow);
1361 Comment cmnt(masm_, "Lookup variable");
1362 __ li(a1, Operand(var->name()));
1363 __ Push(cp, a1); // Context and name.
1364 __ CallRuntime(Runtime::kLoadContextSlot, 2);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001365 __ bind(&done);
1366 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001367 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001368 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001369}
1370
1371
1372void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001373 Comment cmnt(masm_, "[ RegExpLiteral");
1374 Label materialized;
1375 // Registers will be used as follows:
1376 // t1 = materialized value (RegExp literal)
1377 // t0 = JS function, literals array
1378 // a3 = literal index
1379 // a2 = RegExp pattern
1380 // a1 = RegExp flags
1381 // a0 = RegExp literal clone
1382 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1383 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1384 int literal_offset =
1385 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1386 __ lw(t1, FieldMemOperand(t0, literal_offset));
1387 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1388 __ Branch(&materialized, ne, t1, Operand(at));
1389
1390 // Create regexp literal using runtime function.
1391 // Result will be in v0.
1392 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1393 __ li(a2, Operand(expr->pattern()));
1394 __ li(a1, Operand(expr->flags()));
1395 __ Push(t0, a3, a2, a1);
1396 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1397 __ mov(t1, v0);
1398
1399 __ bind(&materialized);
1400 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1401 Label allocated, runtime_allocate;
1402 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1403 __ jmp(&allocated);
1404
1405 __ bind(&runtime_allocate);
1406 __ push(t1);
1407 __ li(a0, Operand(Smi::FromInt(size)));
1408 __ push(a0);
1409 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1410 __ pop(t1);
1411
1412 __ bind(&allocated);
1413
1414 // After this, registers are used as follows:
1415 // v0: Newly allocated regexp.
1416 // t1: Materialized regexp.
1417 // a2: temp.
1418 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1419 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001420}
1421
1422
1423void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001424 Comment cmnt(masm_, "[ ObjectLiteral");
1425 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1426 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1427 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1428 __ li(a1, Operand(expr->constant_properties()));
1429 int flags = expr->fast_elements()
1430 ? ObjectLiteral::kFastElements
1431 : ObjectLiteral::kNoFlags;
1432 flags |= expr->has_function()
1433 ? ObjectLiteral::kHasFunction
1434 : ObjectLiteral::kNoFlags;
1435 __ li(a0, Operand(Smi::FromInt(flags)));
1436 __ Push(a3, a2, a1, a0);
1437 if (expr->depth() > 1) {
1438 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1439 } else {
1440 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
1441 }
1442
1443 // If result_saved is true the result is on top of the stack. If
1444 // result_saved is false the result is in v0.
1445 bool result_saved = false;
1446
1447 // Mark all computed expressions that are bound to a key that
1448 // is shadowed by a later occurrence of the same key. For the
1449 // marked expressions, no store code is emitted.
1450 expr->CalculateEmitStore();
1451
1452 for (int i = 0; i < expr->properties()->length(); i++) {
1453 ObjectLiteral::Property* property = expr->properties()->at(i);
1454 if (property->IsCompileTimeValue()) continue;
1455
1456 Literal* key = property->key();
1457 Expression* value = property->value();
1458 if (!result_saved) {
1459 __ push(v0); // Save result on stack.
1460 result_saved = true;
1461 }
1462 switch (property->kind()) {
1463 case ObjectLiteral::Property::CONSTANT:
1464 UNREACHABLE();
1465 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1466 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1467 // Fall through.
1468 case ObjectLiteral::Property::COMPUTED:
1469 if (key->handle()->IsSymbol()) {
1470 if (property->emit_store()) {
1471 VisitForAccumulatorValue(value);
1472 __ mov(a0, result_register());
1473 __ li(a2, Operand(key->handle()));
1474 __ lw(a1, MemOperand(sp));
1475 Handle<Code> ic = is_strict_mode()
1476 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1477 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001478 __ Call(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001479 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1480 } else {
1481 VisitForEffect(value);
1482 }
1483 break;
1484 }
1485 // Fall through.
1486 case ObjectLiteral::Property::PROTOTYPE:
1487 // Duplicate receiver on stack.
1488 __ lw(a0, MemOperand(sp));
1489 __ push(a0);
1490 VisitForStackValue(key);
1491 VisitForStackValue(value);
1492 if (property->emit_store()) {
1493 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1494 __ push(a0);
1495 __ CallRuntime(Runtime::kSetProperty, 4);
1496 } else {
1497 __ Drop(3);
1498 }
1499 break;
1500 case ObjectLiteral::Property::GETTER:
1501 case ObjectLiteral::Property::SETTER:
1502 // Duplicate receiver on stack.
1503 __ lw(a0, MemOperand(sp));
1504 __ push(a0);
1505 VisitForStackValue(key);
1506 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1507 Smi::FromInt(1) :
1508 Smi::FromInt(0)));
1509 __ push(a1);
1510 VisitForStackValue(value);
1511 __ CallRuntime(Runtime::kDefineAccessor, 4);
1512 break;
1513 }
1514 }
1515
1516 if (expr->has_function()) {
1517 ASSERT(result_saved);
1518 __ lw(a0, MemOperand(sp));
1519 __ push(a0);
1520 __ CallRuntime(Runtime::kToFastProperties, 1);
1521 }
1522
1523 if (result_saved) {
1524 context()->PlugTOS();
1525 } else {
1526 context()->Plug(v0);
1527 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001528}
1529
1530
1531void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001532 Comment cmnt(masm_, "[ ArrayLiteral");
1533
1534 ZoneList<Expression*>* subexprs = expr->values();
1535 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001536
1537 Handle<FixedArray> constant_elements = expr->constant_elements();
1538 ASSERT_EQ(2, constant_elements->length());
1539 ElementsKind constant_elements_kind =
1540 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
1541 Handle<FixedArrayBase> constant_elements_values(
1542 FixedArrayBase::cast(constant_elements->get(1)));
1543
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001544 __ mov(a0, result_register());
1545 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1546 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1547 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001548 __ li(a1, Operand(constant_elements));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001549 __ Push(a3, a2, a1);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001550 if (constant_elements_values->map() ==
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001551 isolate()->heap()->fixed_cow_array_map()) {
1552 FastCloneShallowArrayStub stub(
1553 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1554 __ CallStub(&stub);
1555 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1556 1, a1, a2);
1557 } else if (expr->depth() > 1) {
1558 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1559 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1560 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1561 } else {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001562 ASSERT(constant_elements_kind == FAST_ELEMENTS ||
1563 constant_elements_kind == FAST_SMI_ONLY_ELEMENTS ||
1564 FLAG_smi_only_arrays);
1565 FastCloneShallowArrayStub::Mode mode =
1566 constant_elements_kind == FAST_DOUBLE_ELEMENTS
1567 ? FastCloneShallowArrayStub::CLONE_DOUBLE_ELEMENTS
1568 : FastCloneShallowArrayStub::CLONE_ELEMENTS;
1569 FastCloneShallowArrayStub stub(mode, length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001570 __ CallStub(&stub);
1571 }
1572
1573 bool result_saved = false; // Is the result saved to the stack?
1574
1575 // Emit code to evaluate all the non-constant subexpressions and to store
1576 // them into the newly cloned array.
1577 for (int i = 0; i < length; i++) {
1578 Expression* subexpr = subexprs->at(i);
1579 // If the subexpression is a literal or a simple materialized literal it
1580 // is already set in the cloned array.
1581 if (subexpr->AsLiteral() != NULL ||
1582 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1583 continue;
1584 }
1585
1586 if (!result_saved) {
1587 __ push(v0);
1588 result_saved = true;
1589 }
1590 VisitForAccumulatorValue(subexpr);
1591
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001592 __ lw(t6, MemOperand(sp)); // Copy of array literal.
1593 __ lw(a1, FieldMemOperand(t6, JSObject::kElementsOffset));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001594 __ lw(a2, FieldMemOperand(t6, JSObject::kMapOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001595 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001596
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001597 Label element_done;
1598 Label double_elements;
1599 Label smi_element;
1600 Label slow_elements;
1601 Label fast_elements;
1602 __ CheckFastElements(a2, a3, &double_elements);
1603
1604 // FAST_SMI_ONLY_ELEMENTS or FAST_ELEMENTS
1605 __ JumpIfSmi(result_register(), &smi_element);
1606 __ CheckFastSmiOnlyElements(a2, a3, &fast_elements);
1607
1608 // Store into the array literal requires a elements transition. Call into
1609 // the runtime.
1610 __ bind(&slow_elements);
1611 __ push(t6); // Copy of array literal.
1612 __ li(a1, Operand(Smi::FromInt(i)));
1613 __ li(a2, Operand(Smi::FromInt(NONE))); // PropertyAttributes
1614 __ li(a3, Operand(Smi::FromInt(strict_mode_flag()))); // Strict mode.
1615 __ Push(a1, result_register(), a2, a3);
1616 __ CallRuntime(Runtime::kSetProperty, 5);
1617 __ Branch(&element_done);
1618
1619 // Array literal has ElementsKind of FAST_DOUBLE_ELEMENTS.
1620 __ bind(&double_elements);
1621 __ li(a3, Operand(Smi::FromInt(i)));
1622 __ StoreNumberToDoubleElements(result_register(), a3, t6, a1, t0, t1, t5,
1623 t3, &slow_elements);
1624 __ Branch(&element_done);
1625
1626 // Array literal has ElementsKind of FAST_ELEMENTS and value is an object.
1627 __ bind(&fast_elements);
1628 __ sw(result_register(), FieldMemOperand(a1, offset));
1629 // Update the write barrier for the array store.
1630
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001631 __ RecordWriteField(
1632 a1, offset, result_register(), a2, kRAHasBeenSaved, kDontSaveFPRegs,
1633 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001634 __ Branch(&element_done);
1635
1636 // Array literal has ElementsKind of FAST_SMI_ONLY_ELEMENTS or
1637 // FAST_ELEMENTS, and value is Smi.
1638 __ bind(&smi_element);
1639 __ sw(result_register(), FieldMemOperand(a1, offset));
1640 // Fall through
1641
1642 __ bind(&element_done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001643
1644 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1645 }
1646
1647 if (result_saved) {
1648 context()->PlugTOS();
1649 } else {
1650 context()->Plug(v0);
1651 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001652}
1653
1654
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001655void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001656 Comment cmnt(masm_, "[ Assignment");
1657 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1658 // on the left-hand side.
1659 if (!expr->target()->IsValidLeftHandSide()) {
1660 VisitForEffect(expr->target());
1661 return;
1662 }
1663
1664 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001665 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001666 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1667 LhsKind assign_type = VARIABLE;
1668 Property* property = expr->target()->AsProperty();
1669 if (property != NULL) {
1670 assign_type = (property->key()->IsPropertyName())
1671 ? NAMED_PROPERTY
1672 : KEYED_PROPERTY;
1673 }
1674
1675 // Evaluate LHS expression.
1676 switch (assign_type) {
1677 case VARIABLE:
1678 // Nothing to do here.
1679 break;
1680 case NAMED_PROPERTY:
1681 if (expr->is_compound()) {
1682 // We need the receiver both on the stack and in the accumulator.
1683 VisitForAccumulatorValue(property->obj());
1684 __ push(result_register());
1685 } else {
1686 VisitForStackValue(property->obj());
1687 }
1688 break;
1689 case KEYED_PROPERTY:
1690 // We need the key and receiver on both the stack and in v0 and a1.
1691 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001692 VisitForStackValue(property->obj());
1693 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001694 __ lw(a1, MemOperand(sp, 0));
1695 __ push(v0);
1696 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001697 VisitForStackValue(property->obj());
1698 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001699 }
1700 break;
1701 }
1702
1703 // For compound assignments we need another deoptimization point after the
1704 // variable/property load.
1705 if (expr->is_compound()) {
1706 { AccumulatorValueContext context(this);
1707 switch (assign_type) {
1708 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001709 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001710 PrepareForBailout(expr->target(), TOS_REG);
1711 break;
1712 case NAMED_PROPERTY:
1713 EmitNamedPropertyLoad(property);
1714 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1715 break;
1716 case KEYED_PROPERTY:
1717 EmitKeyedPropertyLoad(property);
1718 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1719 break;
1720 }
1721 }
1722
1723 Token::Value op = expr->binary_op();
1724 __ push(v0); // Left operand goes on the stack.
1725 VisitForAccumulatorValue(expr->value());
1726
1727 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1728 ? OVERWRITE_RIGHT
1729 : NO_OVERWRITE;
1730 SetSourcePosition(expr->position() + 1);
1731 AccumulatorValueContext context(this);
1732 if (ShouldInlineSmiCase(op)) {
1733 EmitInlineSmiBinaryOp(expr->binary_operation(),
1734 op,
1735 mode,
1736 expr->target(),
1737 expr->value());
1738 } else {
1739 EmitBinaryOp(expr->binary_operation(), op, mode);
1740 }
1741
1742 // Deoptimization point in case the binary operation may have side effects.
1743 PrepareForBailout(expr->binary_operation(), TOS_REG);
1744 } else {
1745 VisitForAccumulatorValue(expr->value());
1746 }
1747
1748 // Record source position before possible IC call.
1749 SetSourcePosition(expr->position());
1750
1751 // Store the value.
1752 switch (assign_type) {
1753 case VARIABLE:
1754 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1755 expr->op());
1756 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1757 context()->Plug(v0);
1758 break;
1759 case NAMED_PROPERTY:
1760 EmitNamedPropertyAssignment(expr);
1761 break;
1762 case KEYED_PROPERTY:
1763 EmitKeyedPropertyAssignment(expr);
1764 break;
1765 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001766}
1767
1768
ager@chromium.org5c838252010-02-19 08:53:10 +00001769void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001770 SetSourcePosition(prop->position());
1771 Literal* key = prop->key()->AsLiteral();
1772 __ mov(a0, result_register());
1773 __ li(a2, Operand(key->handle()));
1774 // Call load IC. It has arguments receiver and property name a0 and a2.
1775 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001776 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001777}
1778
1779
1780void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001781 SetSourcePosition(prop->position());
1782 __ mov(a0, result_register());
1783 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1784 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001785 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001786}
1787
1788
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001789void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001790 Token::Value op,
1791 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001792 Expression* left_expr,
1793 Expression* right_expr) {
1794 Label done, smi_case, stub_call;
1795
1796 Register scratch1 = a2;
1797 Register scratch2 = a3;
1798
1799 // Get the arguments.
1800 Register left = a1;
1801 Register right = a0;
1802 __ pop(left);
1803 __ mov(a0, result_register());
1804
1805 // Perform combined smi check on both operands.
1806 __ Or(scratch1, left, Operand(right));
1807 STATIC_ASSERT(kSmiTag == 0);
1808 JumpPatchSite patch_site(masm_);
1809 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1810
1811 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001812 BinaryOpStub stub(op, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001813 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001814 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001815 __ jmp(&done);
1816
1817 __ bind(&smi_case);
1818 // Smi case. This code works the same way as the smi-smi case in the type
1819 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001820 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001821 switch (op) {
1822 case Token::SAR:
1823 __ Branch(&stub_call);
1824 __ GetLeastBitsFromSmi(scratch1, right, 5);
1825 __ srav(right, left, scratch1);
1826 __ And(v0, right, Operand(~kSmiTagMask));
1827 break;
1828 case Token::SHL: {
1829 __ Branch(&stub_call);
1830 __ SmiUntag(scratch1, left);
1831 __ GetLeastBitsFromSmi(scratch2, right, 5);
1832 __ sllv(scratch1, scratch1, scratch2);
1833 __ Addu(scratch2, scratch1, Operand(0x40000000));
1834 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1835 __ SmiTag(v0, scratch1);
1836 break;
1837 }
1838 case Token::SHR: {
1839 __ Branch(&stub_call);
1840 __ SmiUntag(scratch1, left);
1841 __ GetLeastBitsFromSmi(scratch2, right, 5);
1842 __ srlv(scratch1, scratch1, scratch2);
1843 __ And(scratch2, scratch1, 0xc0000000);
1844 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1845 __ SmiTag(v0, scratch1);
1846 break;
1847 }
1848 case Token::ADD:
1849 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1850 __ BranchOnOverflow(&stub_call, scratch1);
1851 break;
1852 case Token::SUB:
1853 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1854 __ BranchOnOverflow(&stub_call, scratch1);
1855 break;
1856 case Token::MUL: {
1857 __ SmiUntag(scratch1, right);
1858 __ Mult(left, scratch1);
1859 __ mflo(scratch1);
1860 __ mfhi(scratch2);
1861 __ sra(scratch1, scratch1, 31);
1862 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1863 __ mflo(v0);
1864 __ Branch(&done, ne, v0, Operand(zero_reg));
1865 __ Addu(scratch2, right, left);
1866 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1867 ASSERT(Smi::FromInt(0) == 0);
1868 __ mov(v0, zero_reg);
1869 break;
1870 }
1871 case Token::BIT_OR:
1872 __ Or(v0, left, Operand(right));
1873 break;
1874 case Token::BIT_AND:
1875 __ And(v0, left, Operand(right));
1876 break;
1877 case Token::BIT_XOR:
1878 __ Xor(v0, left, Operand(right));
1879 break;
1880 default:
1881 UNREACHABLE();
1882 }
1883
1884 __ bind(&done);
1885 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001886}
1887
1888
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001889void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1890 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001891 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001892 __ mov(a0, result_register());
1893 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001894 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001895 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001896 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001897 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001898 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001899}
1900
1901
1902void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001903 // Invalid left-hand sides are rewritten to have a 'throw
1904 // ReferenceError' on the left-hand side.
1905 if (!expr->IsValidLeftHandSide()) {
1906 VisitForEffect(expr);
1907 return;
1908 }
1909
1910 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001911 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001912 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1913 LhsKind assign_type = VARIABLE;
1914 Property* prop = expr->AsProperty();
1915 if (prop != NULL) {
1916 assign_type = (prop->key()->IsPropertyName())
1917 ? NAMED_PROPERTY
1918 : KEYED_PROPERTY;
1919 }
1920
1921 switch (assign_type) {
1922 case VARIABLE: {
1923 Variable* var = expr->AsVariableProxy()->var();
1924 EffectContext context(this);
1925 EmitVariableAssignment(var, Token::ASSIGN);
1926 break;
1927 }
1928 case NAMED_PROPERTY: {
1929 __ push(result_register()); // Preserve value.
1930 VisitForAccumulatorValue(prop->obj());
1931 __ mov(a1, result_register());
1932 __ pop(a0); // Restore value.
1933 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1934 Handle<Code> ic = is_strict_mode()
1935 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1936 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001937 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001938 break;
1939 }
1940 case KEYED_PROPERTY: {
1941 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001942 VisitForStackValue(prop->obj());
1943 VisitForAccumulatorValue(prop->key());
1944 __ mov(a1, result_register());
1945 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001946 __ pop(a0); // Restore value.
1947 Handle<Code> ic = is_strict_mode()
1948 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
1949 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001950 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001951 break;
1952 }
1953 }
1954 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1955 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001956}
1957
1958
1959void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001960 Token::Value op) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001961 if (var->IsUnallocated()) {
1962 // Global var, const, or let.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001963 __ mov(a0, result_register());
1964 __ li(a2, Operand(var->name()));
1965 __ lw(a1, GlobalObjectOperand());
1966 Handle<Code> ic = is_strict_mode()
1967 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1968 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001969 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001970
1971 } else if (op == Token::INIT_CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001972 // Const initializers need a write barrier.
1973 ASSERT(!var->IsParameter()); // No const parameters.
1974 if (var->IsStackLocal()) {
1975 Label skip;
1976 __ lw(a1, StackOperand(var));
1977 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1978 __ Branch(&skip, ne, a1, Operand(t0));
1979 __ sw(result_register(), StackOperand(var));
1980 __ bind(&skip);
1981 } else {
1982 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
1983 // Like var declarations, const declarations are hoisted to function
1984 // scope. However, unlike var initializers, const initializers are
1985 // able to drill a hole to that function context, even from inside a
1986 // 'with' context. We thus bypass the normal static scope lookup for
1987 // var->IsContextSlot().
1988 __ push(v0);
1989 __ li(a0, Operand(var->name()));
1990 __ Push(cp, a0); // Context and name.
1991 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001992 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001993
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001994 } else if (var->mode() == LET && op != Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001995 // Non-initializing assignment to let variable needs a write barrier.
1996 if (var->IsLookupSlot()) {
1997 __ push(v0); // Value.
1998 __ li(a1, Operand(var->name()));
1999 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
2000 __ Push(cp, a1, a0); // Context, name, strict mode.
2001 __ CallRuntime(Runtime::kStoreContextSlot, 4);
2002 } else {
2003 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
2004 Label assign;
2005 MemOperand location = VarOperand(var, a1);
2006 __ lw(a3, location);
2007 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2008 __ Branch(&assign, ne, a3, Operand(t0));
2009 __ li(a3, Operand(var->name()));
2010 __ push(a3);
2011 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2012 // Perform the assignment.
2013 __ bind(&assign);
2014 __ sw(result_register(), location);
2015 if (var->IsContextSlot()) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002016 // RecordWrite may destroy all its register arguments.
2017 __ mov(a3, result_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002018 int offset = Context::SlotOffset(var->index());
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002019 __ RecordWriteContextSlot(
2020 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002021 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002022 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002023
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002024 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2025 // Assignment to var or initializing assignment to let/const
2026 // in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002027 if (var->IsStackAllocated() || var->IsContextSlot()) {
2028 MemOperand location = VarOperand(var, a1);
2029 if (FLAG_debug_code && op == Token::INIT_LET) {
2030 // Check for an uninitialized let binding.
2031 __ lw(a2, location);
2032 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2033 __ Check(eq, "Let binding re-initialization.", a2, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002034 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002035 // Perform the assignment.
2036 __ sw(v0, location);
2037 if (var->IsContextSlot()) {
2038 __ mov(a3, v0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002039 int offset = Context::SlotOffset(var->index());
2040 __ RecordWriteContextSlot(
2041 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002042 }
2043 } else {
2044 ASSERT(var->IsLookupSlot());
2045 __ push(v0); // Value.
2046 __ li(a1, Operand(var->name()));
2047 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
2048 __ Push(cp, a1, a0); // Context, name, strict mode.
2049 __ CallRuntime(Runtime::kStoreContextSlot, 4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002050 }
2051 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002052 // Non-initializing assignments to consts are ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +00002053}
2054
2055
2056void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002057 // Assignment to a property, using a named store IC.
2058 Property* prop = expr->target()->AsProperty();
2059 ASSERT(prop != NULL);
2060 ASSERT(prop->key()->AsLiteral() != NULL);
2061
2062 // If the assignment starts a block of assignments to the same object,
2063 // change to slow case to avoid the quadratic behavior of repeatedly
2064 // adding fast properties.
2065 if (expr->starts_initialization_block()) {
2066 __ push(result_register());
2067 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
2068 __ push(t0);
2069 __ CallRuntime(Runtime::kToSlowProperties, 1);
2070 __ pop(result_register());
2071 }
2072
2073 // Record source code position before IC call.
2074 SetSourcePosition(expr->position());
2075 __ mov(a0, result_register()); // Load the value.
2076 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
2077 // Load receiver to a1. Leave a copy in the stack if needed for turning the
2078 // receiver into fast case.
2079 if (expr->ends_initialization_block()) {
2080 __ lw(a1, MemOperand(sp));
2081 } else {
2082 __ pop(a1);
2083 }
2084
2085 Handle<Code> ic = is_strict_mode()
2086 ? isolate()->builtins()->StoreIC_Initialize_Strict()
2087 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002088 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002089
2090 // If the assignment ends an initialization block, revert to fast case.
2091 if (expr->ends_initialization_block()) {
2092 __ push(v0); // Result of assignment, saved even if not needed.
2093 // Receiver is under the result value.
2094 __ lw(t0, MemOperand(sp, kPointerSize));
2095 __ push(t0);
2096 __ CallRuntime(Runtime::kToFastProperties, 1);
2097 __ pop(v0);
2098 __ Drop(1);
2099 }
2100 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2101 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002102}
2103
2104
2105void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002106 // Assignment to a property, using a keyed store IC.
2107
2108 // If the assignment starts a block of assignments to the same object,
2109 // change to slow case to avoid the quadratic behavior of repeatedly
2110 // adding fast properties.
2111 if (expr->starts_initialization_block()) {
2112 __ push(result_register());
2113 // Receiver is now under the key and value.
2114 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2115 __ push(t0);
2116 __ CallRuntime(Runtime::kToSlowProperties, 1);
2117 __ pop(result_register());
2118 }
2119
2120 // Record source code position before IC call.
2121 SetSourcePosition(expr->position());
2122 // Call keyed store IC.
2123 // The arguments are:
2124 // - a0 is the value,
2125 // - a1 is the key,
2126 // - a2 is the receiver.
2127 __ mov(a0, result_register());
2128 __ pop(a1); // Key.
2129 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2130 // receiver into fast case.
2131 if (expr->ends_initialization_block()) {
2132 __ lw(a2, MemOperand(sp));
2133 } else {
2134 __ pop(a2);
2135 }
2136
2137 Handle<Code> ic = is_strict_mode()
2138 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
2139 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002140 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002141
2142 // If the assignment ends an initialization block, revert to fast case.
2143 if (expr->ends_initialization_block()) {
2144 __ push(v0); // Result of assignment, saved even if not needed.
2145 // Receiver is under the result value.
2146 __ lw(t0, MemOperand(sp, kPointerSize));
2147 __ push(t0);
2148 __ CallRuntime(Runtime::kToFastProperties, 1);
2149 __ pop(v0);
2150 __ Drop(1);
2151 }
2152 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2153 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002154}
2155
2156
2157void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002158 Comment cmnt(masm_, "[ Property");
2159 Expression* key = expr->key();
2160
2161 if (key->IsPropertyName()) {
2162 VisitForAccumulatorValue(expr->obj());
2163 EmitNamedPropertyLoad(expr);
2164 context()->Plug(v0);
2165 } else {
2166 VisitForStackValue(expr->obj());
2167 VisitForAccumulatorValue(expr->key());
2168 __ pop(a1);
2169 EmitKeyedPropertyLoad(expr);
2170 context()->Plug(v0);
2171 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002172}
2173
lrn@chromium.org7516f052011-03-30 08:52:27 +00002174
ager@chromium.org5c838252010-02-19 08:53:10 +00002175void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002176 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002177 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002178 // Code common for calls using the IC.
2179 ZoneList<Expression*>* args = expr->arguments();
2180 int arg_count = args->length();
2181 { PreservePositionScope scope(masm()->positions_recorder());
2182 for (int i = 0; i < arg_count; i++) {
2183 VisitForStackValue(args->at(i));
2184 }
2185 __ li(a2, Operand(name));
2186 }
2187 // Record source position for debugger.
2188 SetSourcePosition(expr->position());
2189 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002190 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002191 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002192 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002193 RecordJSReturnSite(expr);
2194 // Restore context register.
2195 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2196 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002197}
2198
2199
lrn@chromium.org7516f052011-03-30 08:52:27 +00002200void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002201 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002202 // Load the key.
2203 VisitForAccumulatorValue(key);
2204
2205 // Swap the name of the function and the receiver on the stack to follow
2206 // the calling convention for call ICs.
2207 __ pop(a1);
2208 __ push(v0);
2209 __ push(a1);
2210
2211 // Code common for calls using the IC.
2212 ZoneList<Expression*>* args = expr->arguments();
2213 int arg_count = args->length();
2214 { PreservePositionScope scope(masm()->positions_recorder());
2215 for (int i = 0; i < arg_count; i++) {
2216 VisitForStackValue(args->at(i));
2217 }
2218 }
2219 // Record source position for debugger.
2220 SetSourcePosition(expr->position());
2221 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002222 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002223 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002224 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002225 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002226 RecordJSReturnSite(expr);
2227 // Restore context register.
2228 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2229 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002230}
2231
2232
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002233void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002234 // Code common for calls using the call stub.
2235 ZoneList<Expression*>* args = expr->arguments();
2236 int arg_count = args->length();
2237 { PreservePositionScope scope(masm()->positions_recorder());
2238 for (int i = 0; i < arg_count; i++) {
2239 VisitForStackValue(args->at(i));
2240 }
2241 }
2242 // Record source position for debugger.
2243 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002244 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002245 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002246 __ CallStub(&stub);
2247 RecordJSReturnSite(expr);
2248 // Restore context register.
2249 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2250 context()->DropAndPlug(1, v0);
2251}
2252
2253
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002254void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002255 // Push copy of the first argument or undefined if it doesn't exist.
2256 if (arg_count > 0) {
2257 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2258 } else {
2259 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2260 }
2261 __ push(a1);
2262
2263 // Push the receiver of the enclosing function and do runtime call.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002264 int receiver_offset = 2 + info_->scope()->num_parameters();
2265 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002266 __ push(a1);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002267 // Push the strict mode flag. In harmony mode every eval call
2268 // is a strict mode eval call.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002269 StrictModeFlag strict_mode =
2270 FLAG_harmony_scoping ? kStrictMode : strict_mode_flag();
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002271 __ li(a1, Operand(Smi::FromInt(strict_mode)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002272 __ push(a1);
2273
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002274 // Push the start position of the scope the calls resides in.
2275 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
2276 __ push(a1);
2277
2278 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org5c838252010-02-19 08:53:10 +00002279}
2280
2281
2282void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002283#ifdef DEBUG
2284 // We want to verify that RecordJSReturnSite gets called on all paths
2285 // through this function. Avoid early returns.
2286 expr->return_is_recorded_ = false;
2287#endif
2288
2289 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002290 Expression* callee = expr->expression();
2291 VariableProxy* proxy = callee->AsVariableProxy();
2292 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002293
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002294 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002295 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2296 // resolve the function we need to call and the receiver of the
2297 // call. Then we call the resolved function using the given
2298 // arguments.
2299 ZoneList<Expression*>* args = expr->arguments();
2300 int arg_count = args->length();
2301
2302 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002303 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002304 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2305 __ push(a2); // Reserved receiver slot.
2306
2307 // Push the arguments.
2308 for (int i = 0; i < arg_count; i++) {
2309 VisitForStackValue(args->at(i));
2310 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002311
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002312 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002313 // resolve eval.
2314 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2315 __ push(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002316 EmitResolvePossiblyDirectEval(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002317
2318 // The runtime call returns a pair of values in v0 (function) and
2319 // v1 (receiver). Touch up the stack with the right values.
2320 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2321 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2322 }
2323 // Record source position for debugger.
2324 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002325 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002326 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002327 __ CallStub(&stub);
2328 RecordJSReturnSite(expr);
2329 // Restore context register.
2330 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2331 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002332 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002333 // Push global object as receiver for the call IC.
2334 __ lw(a0, GlobalObjectOperand());
2335 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002336 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2337 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002338 // Call to a lookup slot (dynamically introduced variable).
2339 Label slow, done;
2340
2341 { PreservePositionScope scope(masm()->positions_recorder());
2342 // Generate code for loading from variables potentially shadowed
2343 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002344 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002345 }
2346
2347 __ bind(&slow);
2348 // Call the runtime to find the function to call (returned in v0)
2349 // and the object holding it (returned in v1).
2350 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002351 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002352 __ push(a2);
2353 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2354 __ Push(v0, v1); // Function, receiver.
2355
2356 // If fast case code has been generated, emit code to push the
2357 // function and receiver and have the slow path jump around this
2358 // code.
2359 if (done.is_linked()) {
2360 Label call;
2361 __ Branch(&call);
2362 __ bind(&done);
2363 // Push function.
2364 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002365 // The receiver is implicitly the global receiver. Indicate this
2366 // by passing the hole to the call function stub.
2367 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002368 __ push(a1);
2369 __ bind(&call);
2370 }
2371
danno@chromium.org40cb8782011-05-25 07:58:50 +00002372 // The receiver is either the global receiver or an object found
2373 // by LoadContextSlot. That object could be the hole if the
2374 // receiver is implicitly the global object.
2375 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002376 } else if (property != NULL) {
2377 { PreservePositionScope scope(masm()->positions_recorder());
2378 VisitForStackValue(property->obj());
2379 }
2380 if (property->key()->IsPropertyName()) {
2381 EmitCallWithIC(expr,
2382 property->key()->AsLiteral()->handle(),
2383 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002384 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002385 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002386 }
2387 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002388 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002389 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002390 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002391 }
2392 // Load global receiver object.
2393 __ lw(a1, GlobalObjectOperand());
2394 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2395 __ push(a1);
2396 // Emit function call.
2397 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2398 }
2399
2400#ifdef DEBUG
2401 // RecordJSReturnSite should have been called.
2402 ASSERT(expr->return_is_recorded_);
2403#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002404}
2405
2406
2407void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002408 Comment cmnt(masm_, "[ CallNew");
2409 // According to ECMA-262, section 11.2.2, page 44, the function
2410 // expression in new calls must be evaluated before the
2411 // arguments.
2412
2413 // Push constructor on the stack. If it's not a function it's used as
2414 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2415 // ignored.
2416 VisitForStackValue(expr->expression());
2417
2418 // Push the arguments ("left-to-right") on the stack.
2419 ZoneList<Expression*>* args = expr->arguments();
2420 int arg_count = args->length();
2421 for (int i = 0; i < arg_count; i++) {
2422 VisitForStackValue(args->at(i));
2423 }
2424
2425 // Call the construct call builtin that handles allocation and
2426 // constructor invocation.
2427 SetSourcePosition(expr->position());
2428
2429 // Load function and argument count into a1 and a0.
2430 __ li(a0, Operand(arg_count));
2431 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2432
2433 Handle<Code> construct_builtin =
2434 isolate()->builtins()->JSConstructCall();
2435 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2436 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002437}
2438
2439
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002440void FullCodeGenerator::EmitIsSmi(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
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002453 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002454 __ And(t0, v0, Operand(kSmiTagMask));
2455 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2456
2457 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002458}
2459
2460
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002461void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2462 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002463 ASSERT(args->length() == 1);
2464
2465 VisitForAccumulatorValue(args->at(0));
2466
2467 Label materialize_true, materialize_false;
2468 Label* if_true = NULL;
2469 Label* if_false = NULL;
2470 Label* fall_through = NULL;
2471 context()->PrepareTest(&materialize_true, &materialize_false,
2472 &if_true, &if_false, &fall_through);
2473
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002474 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002475 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2476 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2477
2478 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002479}
2480
2481
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002482void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2483 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002484 ASSERT(args->length() == 1);
2485
2486 VisitForAccumulatorValue(args->at(0));
2487
2488 Label materialize_true, materialize_false;
2489 Label* if_true = NULL;
2490 Label* if_false = NULL;
2491 Label* fall_through = NULL;
2492 context()->PrepareTest(&materialize_true, &materialize_false,
2493 &if_true, &if_false, &fall_through);
2494
2495 __ JumpIfSmi(v0, if_false);
2496 __ LoadRoot(at, Heap::kNullValueRootIndex);
2497 __ Branch(if_true, eq, v0, Operand(at));
2498 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2499 // Undetectable objects behave like undefined when tested with typeof.
2500 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2501 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2502 __ Branch(if_false, ne, at, Operand(zero_reg));
2503 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002504 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002505 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002506 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2507 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002508
2509 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002510}
2511
2512
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002513void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2514 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002515 ASSERT(args->length() == 1);
2516
2517 VisitForAccumulatorValue(args->at(0));
2518
2519 Label materialize_true, materialize_false;
2520 Label* if_true = NULL;
2521 Label* if_false = NULL;
2522 Label* fall_through = NULL;
2523 context()->PrepareTest(&materialize_true, &materialize_false,
2524 &if_true, &if_false, &fall_through);
2525
2526 __ JumpIfSmi(v0, if_false);
2527 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002528 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002529 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002530 if_true, if_false, fall_through);
2531
2532 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002533}
2534
2535
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002536void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2537 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002538 ASSERT(args->length() == 1);
2539
2540 VisitForAccumulatorValue(args->at(0));
2541
2542 Label materialize_true, materialize_false;
2543 Label* if_true = NULL;
2544 Label* if_false = NULL;
2545 Label* fall_through = NULL;
2546 context()->PrepareTest(&materialize_true, &materialize_false,
2547 &if_true, &if_false, &fall_through);
2548
2549 __ JumpIfSmi(v0, if_false);
2550 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2551 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2552 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002553 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002554 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2555
2556 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002557}
2558
2559
2560void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002561 CallRuntime* expr) {
2562 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002563 ASSERT(args->length() == 1);
2564
2565 VisitForAccumulatorValue(args->at(0));
2566
2567 Label materialize_true, materialize_false;
2568 Label* if_true = NULL;
2569 Label* if_false = NULL;
2570 Label* fall_through = NULL;
2571 context()->PrepareTest(&materialize_true, &materialize_false,
2572 &if_true, &if_false, &fall_through);
2573
2574 if (FLAG_debug_code) __ AbortIfSmi(v0);
2575
2576 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2577 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2578 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2579 __ Branch(if_true, ne, t0, Operand(zero_reg));
2580
2581 // Check for fast case object. Generate false result for slow case object.
2582 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2583 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2584 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2585 __ Branch(if_false, eq, a2, Operand(t0));
2586
2587 // Look for valueOf symbol in the descriptor array, and indicate false if
2588 // found. The type is not checked, so if it is a transition it is a false
2589 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002590 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002591 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2592 // t0: descriptor array
2593 // a3: length of descriptor array
2594 // Calculate the end of the descriptor array.
2595 STATIC_ASSERT(kSmiTag == 0);
2596 STATIC_ASSERT(kSmiTagSize == 1);
2597 STATIC_ASSERT(kPointerSize == 4);
2598 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2599 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2600 __ Addu(a2, a2, t1);
2601
2602 // Calculate location of the first key name.
2603 __ Addu(t0,
2604 t0,
2605 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2606 DescriptorArray::kFirstIndex * kPointerSize));
2607 // Loop through all the keys in the descriptor array. If one of these is the
2608 // symbol valueOf the result is false.
2609 Label entry, loop;
2610 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2611 // used in the loop below.
2612 __ li(t2, Operand(FACTORY->value_of_symbol()));
2613 __ jmp(&entry);
2614 __ bind(&loop);
2615 __ lw(a3, MemOperand(t0, 0));
2616 __ Branch(if_false, eq, a3, Operand(t2));
2617 __ Addu(t0, t0, Operand(kPointerSize));
2618 __ bind(&entry);
2619 __ Branch(&loop, ne, t0, Operand(a2));
2620
2621 // If a valueOf property is not found on the object check that it's
2622 // prototype is the un-modified String prototype. If not result is false.
2623 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2624 __ JumpIfSmi(a2, if_false);
2625 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2626 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2627 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2628 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2629 __ Branch(if_false, ne, a2, Operand(a3));
2630
2631 // Set the bit in the map to indicate that it has been checked safe for
2632 // default valueOf and set true result.
2633 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2634 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2635 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2636 __ jmp(if_true);
2637
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002638 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002639 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002640}
2641
2642
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002643void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2644 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002645 ASSERT(args->length() == 1);
2646
2647 VisitForAccumulatorValue(args->at(0));
2648
2649 Label materialize_true, materialize_false;
2650 Label* if_true = NULL;
2651 Label* if_false = NULL;
2652 Label* fall_through = NULL;
2653 context()->PrepareTest(&materialize_true, &materialize_false,
2654 &if_true, &if_false, &fall_through);
2655
2656 __ JumpIfSmi(v0, if_false);
2657 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002658 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002659 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2660 __ Branch(if_false);
2661
2662 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002663}
2664
2665
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002666void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2667 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002668 ASSERT(args->length() == 1);
2669
2670 VisitForAccumulatorValue(args->at(0));
2671
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 __ JumpIfSmi(v0, if_false);
2680 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002681 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002682 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2683 if_true, if_false, fall_through);
2684
2685 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002686}
2687
2688
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002689void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2690 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002691 ASSERT(args->length() == 1);
2692
2693 VisitForAccumulatorValue(args->at(0));
2694
2695 Label materialize_true, materialize_false;
2696 Label* if_true = NULL;
2697 Label* if_false = NULL;
2698 Label* fall_through = NULL;
2699 context()->PrepareTest(&materialize_true, &materialize_false,
2700 &if_true, &if_false, &fall_through);
2701
2702 __ JumpIfSmi(v0, if_false);
2703 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002704 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002705 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2706
2707 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002708}
2709
2710
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002711void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2712 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002713
2714 Label materialize_true, materialize_false;
2715 Label* if_true = NULL;
2716 Label* if_false = NULL;
2717 Label* fall_through = NULL;
2718 context()->PrepareTest(&materialize_true, &materialize_false,
2719 &if_true, &if_false, &fall_through);
2720
2721 // Get the frame pointer for the calling frame.
2722 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2723
2724 // Skip the arguments adaptor frame if it exists.
2725 Label check_frame_marker;
2726 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2727 __ Branch(&check_frame_marker, ne,
2728 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2729 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2730
2731 // Check the marker in the calling frame.
2732 __ bind(&check_frame_marker);
2733 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002734 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002735 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2736 if_true, if_false, fall_through);
2737
2738 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002739}
2740
2741
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002742void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2743 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002744 ASSERT(args->length() == 2);
2745
2746 // Load the two objects into registers and perform the comparison.
2747 VisitForStackValue(args->at(0));
2748 VisitForAccumulatorValue(args->at(1));
2749
2750 Label materialize_true, materialize_false;
2751 Label* if_true = NULL;
2752 Label* if_false = NULL;
2753 Label* fall_through = NULL;
2754 context()->PrepareTest(&materialize_true, &materialize_false,
2755 &if_true, &if_false, &fall_through);
2756
2757 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002758 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002759 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2760
2761 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002762}
2763
2764
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002765void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2766 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002767 ASSERT(args->length() == 1);
2768
2769 // ArgumentsAccessStub expects the key in a1 and the formal
2770 // parameter count in a0.
2771 VisitForAccumulatorValue(args->at(0));
2772 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002773 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002774 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2775 __ CallStub(&stub);
2776 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002777}
2778
2779
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002780void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2781 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002782 Label exit;
2783 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002784 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002785
2786 // Check if the calling frame is an arguments adaptor frame.
2787 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2788 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2789 __ Branch(&exit, ne, a3,
2790 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2791
2792 // Arguments adaptor case: Read the arguments length from the
2793 // adaptor frame.
2794 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2795
2796 __ bind(&exit);
2797 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002798}
2799
2800
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002801void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2802 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002803 ASSERT(args->length() == 1);
2804 Label done, null, function, non_function_constructor;
2805
2806 VisitForAccumulatorValue(args->at(0));
2807
2808 // If the object is a smi, we return null.
2809 __ JumpIfSmi(v0, &null);
2810
2811 // Check that the object is a JS object but take special care of JS
2812 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002813 // Assume that there are only two callable types, and one of them is at
2814 // either end of the type range for JS object types. Saves extra comparisons.
2815 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002816 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002817 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002818
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002819 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2820 FIRST_SPEC_OBJECT_TYPE + 1);
2821 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002822
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002823 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2824 LAST_SPEC_OBJECT_TYPE - 1);
2825 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2826 // Assume that there is no larger type.
2827 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2828
2829 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002830 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2831 __ GetObjectType(v0, a1, a1);
2832 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2833
2834 // v0 now contains the constructor function. Grab the
2835 // instance class name from there.
2836 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2837 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2838 __ Branch(&done);
2839
2840 // Functions have class 'Function'.
2841 __ bind(&function);
2842 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2843 __ jmp(&done);
2844
2845 // Objects with a non-function constructor have class 'Object'.
2846 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002847 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002848 __ jmp(&done);
2849
2850 // Non-JS objects have class null.
2851 __ bind(&null);
2852 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2853
2854 // All done.
2855 __ bind(&done);
2856
2857 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002858}
2859
2860
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002861void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002862 // Conditionally generate a log call.
2863 // Args:
2864 // 0 (literal string): The type of logging (corresponds to the flags).
2865 // This is used to determine whether or not to generate the log call.
2866 // 1 (string): Format string. Access the string at argument index 2
2867 // with '%2s' (see Logger::LogRuntime for all the formats).
2868 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002869 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002870 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002871 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2872 VisitForStackValue(args->at(1));
2873 VisitForStackValue(args->at(2));
2874 __ CallRuntime(Runtime::kLog, 2);
2875 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002876
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002877 // Finally, we're expected to leave a value on the top of the stack.
2878 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2879 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002880}
2881
2882
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002883void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
2884 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002885 Label slow_allocate_heapnumber;
2886 Label heapnumber_allocated;
2887
2888 // Save the new heap number in callee-saved register s0, since
2889 // we call out to external C code below.
2890 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2891 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2892 __ jmp(&heapnumber_allocated);
2893
2894 __ bind(&slow_allocate_heapnumber);
2895
2896 // Allocate a heap number.
2897 __ CallRuntime(Runtime::kNumberAlloc, 0);
2898 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2899
2900 __ bind(&heapnumber_allocated);
2901
2902 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2903 // by computing:
2904 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2905 if (CpuFeatures::IsSupported(FPU)) {
2906 __ PrepareCallCFunction(1, a0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002907 __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
2908 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002909 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2910
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002911 CpuFeatures::Scope scope(FPU);
2912 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2913 __ li(a1, Operand(0x41300000));
2914 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002915 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002916 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002917 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002918 // Subtract and store the result in the heap number.
2919 __ sub_d(f0, f12, f14);
2920 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2921 __ mov(v0, s0);
2922 } else {
2923 __ PrepareCallCFunction(2, a0);
2924 __ mov(a0, s0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002925 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
2926 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002927 __ CallCFunction(
2928 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2929 }
2930
2931 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002932}
2933
2934
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002935void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002936 // Load the arguments on the stack and call the stub.
2937 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002938 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002939 ASSERT(args->length() == 3);
2940 VisitForStackValue(args->at(0));
2941 VisitForStackValue(args->at(1));
2942 VisitForStackValue(args->at(2));
2943 __ CallStub(&stub);
2944 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002945}
2946
2947
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002948void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002949 // Load the arguments on the stack and call the stub.
2950 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002951 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002952 ASSERT(args->length() == 4);
2953 VisitForStackValue(args->at(0));
2954 VisitForStackValue(args->at(1));
2955 VisitForStackValue(args->at(2));
2956 VisitForStackValue(args->at(3));
2957 __ CallStub(&stub);
2958 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002959}
2960
2961
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002962void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
2963 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002964 ASSERT(args->length() == 1);
2965
2966 VisitForAccumulatorValue(args->at(0)); // Load the object.
2967
2968 Label done;
2969 // If the object is a smi return the object.
2970 __ JumpIfSmi(v0, &done);
2971 // If the object is not a value type, return the object.
2972 __ GetObjectType(v0, a1, a1);
2973 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2974
2975 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2976
2977 __ bind(&done);
2978 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002979}
2980
2981
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002982void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002983 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002984 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002985 ASSERT(args->length() == 2);
2986 VisitForStackValue(args->at(0));
2987 VisitForStackValue(args->at(1));
2988 MathPowStub 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::EmitSetValueOf(CallRuntime* expr) {
2995 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002996 ASSERT(args->length() == 2);
2997
2998 VisitForStackValue(args->at(0)); // Load the object.
2999 VisitForAccumulatorValue(args->at(1)); // Load the value.
3000 __ pop(a1); // v0 = value. a1 = object.
3001
3002 Label done;
3003 // If the object is a smi, return the value.
3004 __ JumpIfSmi(a1, &done);
3005
3006 // If the object is not a value type, return the value.
3007 __ GetObjectType(a1, a2, a2);
3008 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
3009
3010 // Store the value.
3011 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
3012 // Update the write barrier. Save the value as it will be
3013 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003014 __ mov(a2, v0);
3015 __ RecordWriteField(
3016 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003017
3018 __ bind(&done);
3019 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003020}
3021
3022
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003023void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3024 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003025 ASSERT_EQ(args->length(), 1);
3026
3027 // Load the argument on the stack and call the stub.
3028 VisitForStackValue(args->at(0));
3029
3030 NumberToStringStub stub;
3031 __ CallStub(&stub);
3032 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003033}
3034
3035
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003036void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3037 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003038 ASSERT(args->length() == 1);
3039
3040 VisitForAccumulatorValue(args->at(0));
3041
3042 Label done;
3043 StringCharFromCodeGenerator generator(v0, a1);
3044 generator.GenerateFast(masm_);
3045 __ jmp(&done);
3046
3047 NopRuntimeCallHelper call_helper;
3048 generator.GenerateSlow(masm_, call_helper);
3049
3050 __ bind(&done);
3051 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003052}
3053
3054
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003055void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3056 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003057 ASSERT(args->length() == 2);
3058
3059 VisitForStackValue(args->at(0));
3060 VisitForAccumulatorValue(args->at(1));
3061 __ mov(a0, result_register());
3062
3063 Register object = a1;
3064 Register index = a0;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003065 Register result = v0;
3066
3067 __ pop(object);
3068
3069 Label need_conversion;
3070 Label index_out_of_range;
3071 Label done;
3072 StringCharCodeAtGenerator generator(object,
3073 index,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003074 result,
3075 &need_conversion,
3076 &need_conversion,
3077 &index_out_of_range,
3078 STRING_INDEX_IS_NUMBER);
3079 generator.GenerateFast(masm_);
3080 __ jmp(&done);
3081
3082 __ bind(&index_out_of_range);
3083 // When the index is out of range, the spec requires us to return
3084 // NaN.
3085 __ LoadRoot(result, Heap::kNanValueRootIndex);
3086 __ jmp(&done);
3087
3088 __ bind(&need_conversion);
3089 // Load the undefined value into the result register, which will
3090 // trigger conversion.
3091 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3092 __ jmp(&done);
3093
3094 NopRuntimeCallHelper call_helper;
3095 generator.GenerateSlow(masm_, call_helper);
3096
3097 __ bind(&done);
3098 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003099}
3100
3101
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003102void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3103 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003104 ASSERT(args->length() == 2);
3105
3106 VisitForStackValue(args->at(0));
3107 VisitForAccumulatorValue(args->at(1));
3108 __ mov(a0, result_register());
3109
3110 Register object = a1;
3111 Register index = a0;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003112 Register scratch = a3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003113 Register result = v0;
3114
3115 __ pop(object);
3116
3117 Label need_conversion;
3118 Label index_out_of_range;
3119 Label done;
3120 StringCharAtGenerator generator(object,
3121 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003122 scratch,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003123 result,
3124 &need_conversion,
3125 &need_conversion,
3126 &index_out_of_range,
3127 STRING_INDEX_IS_NUMBER);
3128 generator.GenerateFast(masm_);
3129 __ jmp(&done);
3130
3131 __ bind(&index_out_of_range);
3132 // When the index is out of range, the spec requires us to return
3133 // the empty string.
3134 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3135 __ jmp(&done);
3136
3137 __ bind(&need_conversion);
3138 // Move smi zero into the result register, which will trigger
3139 // conversion.
3140 __ li(result, Operand(Smi::FromInt(0)));
3141 __ jmp(&done);
3142
3143 NopRuntimeCallHelper call_helper;
3144 generator.GenerateSlow(masm_, call_helper);
3145
3146 __ bind(&done);
3147 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003148}
3149
3150
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003151void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3152 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003153 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003154 VisitForStackValue(args->at(0));
3155 VisitForStackValue(args->at(1));
3156
3157 StringAddStub stub(NO_STRING_ADD_FLAGS);
3158 __ CallStub(&stub);
3159 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003160}
3161
3162
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003163void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3164 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003165 ASSERT_EQ(2, args->length());
3166
3167 VisitForStackValue(args->at(0));
3168 VisitForStackValue(args->at(1));
3169
3170 StringCompareStub stub;
3171 __ CallStub(&stub);
3172 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003173}
3174
3175
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003176void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003177 // Load the argument on the stack and call the stub.
3178 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3179 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003180 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003181 ASSERT(args->length() == 1);
3182 VisitForStackValue(args->at(0));
3183 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3184 __ CallStub(&stub);
3185 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003186}
3187
3188
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003189void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003190 // Load the argument on the stack and call the stub.
3191 TranscendentalCacheStub stub(TranscendentalCache::COS,
3192 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003193 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003194 ASSERT(args->length() == 1);
3195 VisitForStackValue(args->at(0));
3196 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3197 __ CallStub(&stub);
3198 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003199}
3200
3201
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003202void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003203 // Load the argument on the stack and call the stub.
3204 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3205 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003206 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003207 ASSERT(args->length() == 1);
3208 VisitForStackValue(args->at(0));
3209 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3210 __ CallStub(&stub);
3211 context()->Plug(v0);
3212}
3213
3214
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003215void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003216 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003217 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003218 ASSERT(args->length() == 1);
3219 VisitForStackValue(args->at(0));
3220 __ CallRuntime(Runtime::kMath_sqrt, 1);
3221 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003222}
3223
3224
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003225void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3226 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003227 ASSERT(args->length() >= 2);
3228
3229 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3230 for (int i = 0; i < arg_count + 1; i++) {
3231 VisitForStackValue(args->at(i));
3232 }
3233 VisitForAccumulatorValue(args->last()); // Function.
3234
danno@chromium.orgc612e022011-11-10 11:38:15 +00003235 // Check for proxy.
3236 Label proxy, done;
3237 __ GetObjectType(v0, a1, a1);
3238 __ Branch(&proxy, eq, a1, Operand(JS_FUNCTION_PROXY_TYPE));
3239
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003240 // InvokeFunction requires the function in a1. Move it in there.
3241 __ mov(a1, result_register());
3242 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003243 __ InvokeFunction(a1, count, CALL_FUNCTION,
3244 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003245 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003246 __ jmp(&done);
3247
3248 __ bind(&proxy);
3249 __ push(v0);
3250 __ CallRuntime(Runtime::kCall, args->length());
3251 __ bind(&done);
3252
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003253 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003254}
3255
3256
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003257void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003258 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003259 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003260 ASSERT(args->length() == 3);
3261 VisitForStackValue(args->at(0));
3262 VisitForStackValue(args->at(1));
3263 VisitForStackValue(args->at(2));
3264 __ CallStub(&stub);
3265 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003266}
3267
3268
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003269void FullCodeGenerator::EmitSwapElements(CallRuntime* expr) {
3270 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003271 ASSERT(args->length() == 3);
3272 VisitForStackValue(args->at(0));
3273 VisitForStackValue(args->at(1));
3274 VisitForStackValue(args->at(2));
3275 Label done;
3276 Label slow_case;
3277 Register object = a0;
3278 Register index1 = a1;
3279 Register index2 = a2;
3280 Register elements = a3;
3281 Register scratch1 = t0;
3282 Register scratch2 = t1;
3283
3284 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3285 // Fetch the map and check if array is in fast case.
3286 // Check that object doesn't require security checks and
3287 // has no indexed interceptor.
3288 __ GetObjectType(object, scratch1, scratch2);
3289 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3290 // Map is now in scratch1.
3291
3292 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3293 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3294 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3295
3296 // Check the object's elements are in fast case and writable.
3297 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3298 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3299 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3300 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3301
3302 // Check that both indices are smis.
3303 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3304 __ lw(index2, MemOperand(sp, 0));
3305 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3306
3307 // Check that both indices are valid.
3308 Label not_hi;
3309 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3310 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3311 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3312 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3313 __ bind(&not_hi);
3314
3315 // Bring the address of the elements into index1 and index2.
3316 __ Addu(scratch1, elements,
3317 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3318 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3319 __ Addu(index1, scratch1, index1);
3320 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3321 __ Addu(index2, scratch1, index2);
3322
3323 // Swap elements.
3324 __ lw(scratch1, MemOperand(index1, 0));
3325 __ lw(scratch2, MemOperand(index2, 0));
3326 __ sw(scratch1, MemOperand(index2, 0));
3327 __ sw(scratch2, MemOperand(index1, 0));
3328
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003329 Label no_remembered_set;
3330 __ CheckPageFlag(elements,
3331 scratch1,
3332 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3333 ne,
3334 &no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003335 // Possible optimization: do a check that both values are Smis
3336 // (or them and test against Smi mask).
3337
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003338 // We are swapping two objects in an array and the incremental marker never
3339 // pauses in the middle of scanning a single object. Therefore the
3340 // incremental marker is not disturbed, so we don't need to call the
3341 // RecordWrite stub that notifies the incremental marker.
3342 __ RememberedSetHelper(elements,
3343 index1,
3344 scratch2,
3345 kDontSaveFPRegs,
3346 MacroAssembler::kFallThroughAtEnd);
3347 __ RememberedSetHelper(elements,
3348 index2,
3349 scratch2,
3350 kDontSaveFPRegs,
3351 MacroAssembler::kFallThroughAtEnd);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003352
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003353 __ bind(&no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003354 // We are done. Drop elements from the stack, and return undefined.
3355 __ Drop(3);
3356 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3357 __ jmp(&done);
3358
3359 __ bind(&slow_case);
3360 __ CallRuntime(Runtime::kSwapElements, 3);
3361
3362 __ bind(&done);
3363 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003364}
3365
3366
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003367void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3368 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003369 ASSERT_EQ(2, args->length());
3370
3371 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3372 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3373
3374 Handle<FixedArray> jsfunction_result_caches(
3375 isolate()->global_context()->jsfunction_result_caches());
3376 if (jsfunction_result_caches->length() <= cache_id) {
3377 __ Abort("Attempt to use undefined cache.");
3378 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3379 context()->Plug(v0);
3380 return;
3381 }
3382
3383 VisitForAccumulatorValue(args->at(1));
3384
3385 Register key = v0;
3386 Register cache = a1;
3387 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3388 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3389 __ lw(cache,
3390 ContextOperand(
3391 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3392 __ lw(cache,
3393 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3394
3395
3396 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003397 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003398 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3399 // a2 now holds finger offset as a smi.
3400 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3401 // a3 now points to the start of fixed array elements.
3402 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3403 __ addu(a3, a3, at);
3404 // a3 now points to key of indexed element of cache.
3405 __ lw(a2, MemOperand(a3));
3406 __ Branch(&not_found, ne, key, Operand(a2));
3407
3408 __ lw(v0, MemOperand(a3, kPointerSize));
3409 __ Branch(&done);
3410
3411 __ bind(&not_found);
3412 // Call runtime to perform the lookup.
3413 __ Push(cache, key);
3414 __ CallRuntime(Runtime::kGetFromCache, 2);
3415
3416 __ bind(&done);
3417 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003418}
3419
3420
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003421void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3422 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003423 ASSERT_EQ(2, args->length());
3424
3425 Register right = v0;
3426 Register left = a1;
3427 Register tmp = a2;
3428 Register tmp2 = a3;
3429
3430 VisitForStackValue(args->at(0));
3431 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3432 __ pop(left);
3433
3434 Label done, fail, ok;
3435 __ Branch(&ok, eq, left, Operand(right));
3436 // Fail if either is a non-HeapObject.
3437 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003438 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003439 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3440 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3441 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3442 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3443 __ Branch(&fail, ne, tmp, Operand(tmp2));
3444 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3445 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3446 __ Branch(&ok, eq, tmp, Operand(tmp2));
3447 __ bind(&fail);
3448 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3449 __ jmp(&done);
3450 __ bind(&ok);
3451 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3452 __ bind(&done);
3453
3454 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003455}
3456
3457
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003458void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3459 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003460 VisitForAccumulatorValue(args->at(0));
3461
3462 Label materialize_true, materialize_false;
3463 Label* if_true = NULL;
3464 Label* if_false = NULL;
3465 Label* fall_through = NULL;
3466 context()->PrepareTest(&materialize_true, &materialize_false,
3467 &if_true, &if_false, &fall_through);
3468
3469 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3470 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3471
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003472 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003473 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3474
3475 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003476}
3477
3478
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003479void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3480 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003481 ASSERT(args->length() == 1);
3482 VisitForAccumulatorValue(args->at(0));
3483
3484 if (FLAG_debug_code) {
3485 __ AbortIfNotString(v0);
3486 }
3487
3488 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3489 __ IndexFromHash(v0, v0);
3490
3491 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003492}
3493
3494
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003495void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003496 Label bailout, done, one_char_separator, long_separator,
3497 non_trivial_array, not_size_one_array, loop,
3498 empty_separator_loop, one_char_separator_loop,
3499 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003500 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003501 ASSERT(args->length() == 2);
3502 VisitForStackValue(args->at(1));
3503 VisitForAccumulatorValue(args->at(0));
3504
3505 // All aliases of the same register have disjoint lifetimes.
3506 Register array = v0;
3507 Register elements = no_reg; // Will be v0.
3508 Register result = no_reg; // Will be v0.
3509 Register separator = a1;
3510 Register array_length = a2;
3511 Register result_pos = no_reg; // Will be a2.
3512 Register string_length = a3;
3513 Register string = t0;
3514 Register element = t1;
3515 Register elements_end = t2;
3516 Register scratch1 = t3;
3517 Register scratch2 = t5;
3518 Register scratch3 = t4;
3519 Register scratch4 = v1;
3520
3521 // Separator operand is on the stack.
3522 __ pop(separator);
3523
3524 // Check that the array is a JSArray.
3525 __ JumpIfSmi(array, &bailout);
3526 __ GetObjectType(array, scratch1, scratch2);
3527 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3528
3529 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003530 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003531
3532 // If the array has length zero, return the empty string.
3533 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3534 __ SmiUntag(array_length);
3535 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3536 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3537 __ Branch(&done);
3538
3539 __ bind(&non_trivial_array);
3540
3541 // Get the FixedArray containing array's elements.
3542 elements = array;
3543 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3544 array = no_reg; // End of array's live range.
3545
3546 // Check that all array elements are sequential ASCII strings, and
3547 // accumulate the sum of their lengths, as a smi-encoded value.
3548 __ mov(string_length, zero_reg);
3549 __ Addu(element,
3550 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3551 __ sll(elements_end, array_length, kPointerSizeLog2);
3552 __ Addu(elements_end, element, elements_end);
3553 // Loop condition: while (element < elements_end).
3554 // Live values in registers:
3555 // elements: Fixed array of strings.
3556 // array_length: Length of the fixed array of strings (not smi)
3557 // separator: Separator string
3558 // string_length: Accumulated sum of string lengths (smi).
3559 // element: Current array element.
3560 // elements_end: Array end.
3561 if (FLAG_debug_code) {
3562 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3563 array_length, Operand(zero_reg));
3564 }
3565 __ bind(&loop);
3566 __ lw(string, MemOperand(element));
3567 __ Addu(element, element, kPointerSize);
3568 __ JumpIfSmi(string, &bailout);
3569 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3570 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3571 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3572 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3573 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3574 __ BranchOnOverflow(&bailout, scratch3);
3575 __ Branch(&loop, lt, element, Operand(elements_end));
3576
3577 // If array_length is 1, return elements[0], a string.
3578 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3579 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3580 __ Branch(&done);
3581
3582 __ bind(&not_size_one_array);
3583
3584 // Live values in registers:
3585 // separator: Separator string
3586 // array_length: Length of the array.
3587 // string_length: Sum of string lengths (smi).
3588 // elements: FixedArray of strings.
3589
3590 // Check that the separator is a flat ASCII string.
3591 __ JumpIfSmi(separator, &bailout);
3592 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3593 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3594 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3595
3596 // Add (separator length times array_length) - separator length to the
3597 // string_length to get the length of the result string. array_length is not
3598 // smi but the other values are, so the result is a smi.
3599 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3600 __ Subu(string_length, string_length, Operand(scratch1));
3601 __ Mult(array_length, scratch1);
3602 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3603 // zero.
3604 __ mfhi(scratch2);
3605 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3606 __ mflo(scratch2);
3607 __ And(scratch3, scratch2, Operand(0x80000000));
3608 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3609 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3610 __ BranchOnOverflow(&bailout, scratch3);
3611 __ SmiUntag(string_length);
3612
3613 // Get first element in the array to free up the elements register to be used
3614 // for the result.
3615 __ Addu(element,
3616 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3617 result = elements; // End of live range for elements.
3618 elements = no_reg;
3619 // Live values in registers:
3620 // element: First array element
3621 // separator: Separator string
3622 // string_length: Length of result string (not smi)
3623 // array_length: Length of the array.
3624 __ AllocateAsciiString(result,
3625 string_length,
3626 scratch1,
3627 scratch2,
3628 elements_end,
3629 &bailout);
3630 // Prepare for looping. Set up elements_end to end of the array. Set
3631 // result_pos to the position of the result where to write the first
3632 // character.
3633 __ sll(elements_end, array_length, kPointerSizeLog2);
3634 __ Addu(elements_end, element, elements_end);
3635 result_pos = array_length; // End of live range for array_length.
3636 array_length = no_reg;
3637 __ Addu(result_pos,
3638 result,
3639 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3640
3641 // Check the length of the separator.
3642 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3643 __ li(at, Operand(Smi::FromInt(1)));
3644 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3645 __ Branch(&long_separator, gt, scratch1, Operand(at));
3646
3647 // Empty separator case.
3648 __ bind(&empty_separator_loop);
3649 // Live values in registers:
3650 // result_pos: the position to which we are currently copying characters.
3651 // element: Current array element.
3652 // elements_end: Array end.
3653
3654 // Copy next array element to the result.
3655 __ lw(string, MemOperand(element));
3656 __ Addu(element, element, kPointerSize);
3657 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3658 __ SmiUntag(string_length);
3659 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3660 __ CopyBytes(string, result_pos, string_length, scratch1);
3661 // End while (element < elements_end).
3662 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3663 ASSERT(result.is(v0));
3664 __ Branch(&done);
3665
3666 // One-character separator case.
3667 __ bind(&one_char_separator);
3668 // Replace separator with its ascii character value.
3669 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3670 // Jump into the loop after the code that copies the separator, so the first
3671 // element is not preceded by a separator.
3672 __ jmp(&one_char_separator_loop_entry);
3673
3674 __ bind(&one_char_separator_loop);
3675 // Live values in registers:
3676 // result_pos: the position to which we are currently copying characters.
3677 // element: Current array element.
3678 // elements_end: Array end.
3679 // separator: Single separator ascii char (in lower byte).
3680
3681 // Copy the separator character to the result.
3682 __ sb(separator, MemOperand(result_pos));
3683 __ Addu(result_pos, result_pos, 1);
3684
3685 // Copy next array element to the result.
3686 __ bind(&one_char_separator_loop_entry);
3687 __ lw(string, MemOperand(element));
3688 __ Addu(element, element, kPointerSize);
3689 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3690 __ SmiUntag(string_length);
3691 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3692 __ CopyBytes(string, result_pos, string_length, scratch1);
3693 // End while (element < elements_end).
3694 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3695 ASSERT(result.is(v0));
3696 __ Branch(&done);
3697
3698 // Long separator case (separator is more than one character). Entry is at the
3699 // label long_separator below.
3700 __ bind(&long_separator_loop);
3701 // Live values in registers:
3702 // result_pos: the position to which we are currently copying characters.
3703 // element: Current array element.
3704 // elements_end: Array end.
3705 // separator: Separator string.
3706
3707 // Copy the separator to the result.
3708 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3709 __ SmiUntag(string_length);
3710 __ Addu(string,
3711 separator,
3712 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3713 __ CopyBytes(string, result_pos, string_length, scratch1);
3714
3715 __ bind(&long_separator);
3716 __ lw(string, MemOperand(element));
3717 __ Addu(element, element, kPointerSize);
3718 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3719 __ SmiUntag(string_length);
3720 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3721 __ CopyBytes(string, result_pos, string_length, scratch1);
3722 // End while (element < elements_end).
3723 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3724 ASSERT(result.is(v0));
3725 __ Branch(&done);
3726
3727 __ bind(&bailout);
3728 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3729 __ bind(&done);
3730 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003731}
3732
3733
ager@chromium.org5c838252010-02-19 08:53:10 +00003734void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003735 Handle<String> name = expr->name();
3736 if (name->length() > 0 && name->Get(0) == '_') {
3737 Comment cmnt(masm_, "[ InlineRuntimeCall");
3738 EmitInlineRuntimeCall(expr);
3739 return;
3740 }
3741
3742 Comment cmnt(masm_, "[ CallRuntime");
3743 ZoneList<Expression*>* args = expr->arguments();
3744
3745 if (expr->is_jsruntime()) {
3746 // Prepare for calling JS runtime function.
3747 __ lw(a0, GlobalObjectOperand());
3748 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3749 __ push(a0);
3750 }
3751
3752 // Push the arguments ("left-to-right").
3753 int arg_count = args->length();
3754 for (int i = 0; i < arg_count; i++) {
3755 VisitForStackValue(args->at(i));
3756 }
3757
3758 if (expr->is_jsruntime()) {
3759 // Call the JS runtime function.
3760 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003761 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003762 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003763 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003764 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003765 // Restore context register.
3766 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3767 } else {
3768 // Call the C runtime function.
3769 __ CallRuntime(expr->function(), arg_count);
3770 }
3771 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003772}
3773
3774
3775void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003776 switch (expr->op()) {
3777 case Token::DELETE: {
3778 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003779 Property* property = expr->expression()->AsProperty();
3780 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003781
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003782 if (property != NULL) {
3783 VisitForStackValue(property->obj());
3784 VisitForStackValue(property->key());
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003785 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3786 __ push(a1);
3787 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3788 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003789 } else if (proxy != NULL) {
3790 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003791 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003792 // but "delete this" is allowed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003793 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003794 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003795 __ lw(a2, GlobalObjectOperand());
3796 __ li(a1, Operand(var->name()));
3797 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3798 __ Push(a2, a1, a0);
3799 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3800 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003801 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003802 // Result of deleting non-global, non-dynamic variables is false.
3803 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003804 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003805 } else {
3806 // Non-global variable. Call the runtime to try to delete from the
3807 // context where the variable was introduced.
3808 __ push(context_register());
3809 __ li(a2, Operand(var->name()));
3810 __ push(a2);
3811 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3812 context()->Plug(v0);
3813 }
3814 } else {
3815 // Result of deleting non-property, non-variable reference is true.
3816 // The subexpression may have side effects.
3817 VisitForEffect(expr->expression());
3818 context()->Plug(true);
3819 }
3820 break;
3821 }
3822
3823 case Token::VOID: {
3824 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3825 VisitForEffect(expr->expression());
3826 context()->Plug(Heap::kUndefinedValueRootIndex);
3827 break;
3828 }
3829
3830 case Token::NOT: {
3831 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3832 if (context()->IsEffect()) {
3833 // Unary NOT has no side effects so it's only necessary to visit the
3834 // subexpression. Match the optimizing compiler by not branching.
3835 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003836 } else if (context()->IsTest()) {
3837 const TestContext* test = TestContext::cast(context());
3838 // The labels are swapped for the recursive call.
3839 VisitForControl(expr->expression(),
3840 test->false_label(),
3841 test->true_label(),
3842 test->fall_through());
3843 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003844 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003845 // We handle value contexts explicitly rather than simply visiting
3846 // for control and plugging the control flow into the context,
3847 // because we need to prepare a pair of extra administrative AST ids
3848 // for the optimizing compiler.
3849 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3850 Label materialize_true, materialize_false, done;
3851 VisitForControl(expr->expression(),
3852 &materialize_false,
3853 &materialize_true,
3854 &materialize_true);
3855 __ bind(&materialize_true);
3856 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3857 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3858 if (context()->IsStackValue()) __ push(v0);
3859 __ jmp(&done);
3860 __ bind(&materialize_false);
3861 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3862 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3863 if (context()->IsStackValue()) __ push(v0);
3864 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003865 }
3866 break;
3867 }
3868
3869 case Token::TYPEOF: {
3870 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3871 { StackValueContext context(this);
3872 VisitForTypeofValue(expr->expression());
3873 }
3874 __ CallRuntime(Runtime::kTypeof, 1);
3875 context()->Plug(v0);
3876 break;
3877 }
3878
3879 case Token::ADD: {
3880 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3881 VisitForAccumulatorValue(expr->expression());
3882 Label no_conversion;
3883 __ JumpIfSmi(result_register(), &no_conversion);
3884 __ mov(a0, result_register());
3885 ToNumberStub convert_stub;
3886 __ CallStub(&convert_stub);
3887 __ bind(&no_conversion);
3888 context()->Plug(result_register());
3889 break;
3890 }
3891
3892 case Token::SUB:
3893 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3894 break;
3895
3896 case Token::BIT_NOT:
3897 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3898 break;
3899
3900 default:
3901 UNREACHABLE();
3902 }
3903}
3904
3905
3906void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3907 const char* comment) {
3908 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3909 Comment cmt(masm_, comment);
3910 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3911 UnaryOverwriteMode overwrite =
3912 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003913 UnaryOpStub stub(expr->op(), overwrite);
3914 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003915 VisitForAccumulatorValue(expr->expression());
3916 SetSourcePosition(expr->position());
3917 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003918 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003919 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003920}
3921
3922
3923void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003924 Comment cmnt(masm_, "[ CountOperation");
3925 SetSourcePosition(expr->position());
3926
3927 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3928 // as the left-hand side.
3929 if (!expr->expression()->IsValidLeftHandSide()) {
3930 VisitForEffect(expr->expression());
3931 return;
3932 }
3933
3934 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003935 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003936 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3937 LhsKind assign_type = VARIABLE;
3938 Property* prop = expr->expression()->AsProperty();
3939 // In case of a property we use the uninitialized expression context
3940 // of the key to detect a named property.
3941 if (prop != NULL) {
3942 assign_type =
3943 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3944 }
3945
3946 // Evaluate expression and get value.
3947 if (assign_type == VARIABLE) {
3948 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3949 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003950 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003951 } else {
3952 // Reserve space for result of postfix operation.
3953 if (expr->is_postfix() && !context()->IsEffect()) {
3954 __ li(at, Operand(Smi::FromInt(0)));
3955 __ push(at);
3956 }
3957 if (assign_type == NAMED_PROPERTY) {
3958 // Put the object both on the stack and in the accumulator.
3959 VisitForAccumulatorValue(prop->obj());
3960 __ push(v0);
3961 EmitNamedPropertyLoad(prop);
3962 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003963 VisitForStackValue(prop->obj());
3964 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003965 __ lw(a1, MemOperand(sp, 0));
3966 __ push(v0);
3967 EmitKeyedPropertyLoad(prop);
3968 }
3969 }
3970
3971 // We need a second deoptimization point after loading the value
3972 // in case evaluating the property load my have a side effect.
3973 if (assign_type == VARIABLE) {
3974 PrepareForBailout(expr->expression(), TOS_REG);
3975 } else {
3976 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3977 }
3978
3979 // Call ToNumber only if operand is not a smi.
3980 Label no_conversion;
3981 __ JumpIfSmi(v0, &no_conversion);
3982 __ mov(a0, v0);
3983 ToNumberStub convert_stub;
3984 __ CallStub(&convert_stub);
3985 __ bind(&no_conversion);
3986
3987 // Save result for postfix expressions.
3988 if (expr->is_postfix()) {
3989 if (!context()->IsEffect()) {
3990 // Save the result on the stack. If we have a named or keyed property
3991 // we store the result under the receiver that is currently on top
3992 // of the stack.
3993 switch (assign_type) {
3994 case VARIABLE:
3995 __ push(v0);
3996 break;
3997 case NAMED_PROPERTY:
3998 __ sw(v0, MemOperand(sp, kPointerSize));
3999 break;
4000 case KEYED_PROPERTY:
4001 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4002 break;
4003 }
4004 }
4005 }
4006 __ mov(a0, result_register());
4007
4008 // Inline smi case if we are in a loop.
4009 Label stub_call, done;
4010 JumpPatchSite patch_site(masm_);
4011
4012 int count_value = expr->op() == Token::INC ? 1 : -1;
4013 __ li(a1, Operand(Smi::FromInt(count_value)));
4014
4015 if (ShouldInlineSmiCase(expr->op())) {
4016 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
4017 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
4018
4019 // We could eliminate this smi check if we split the code at
4020 // the first smi check before calling ToNumber.
4021 patch_site.EmitJumpIfSmi(v0, &done);
4022 __ bind(&stub_call);
4023 }
4024
4025 // Record position before stub call.
4026 SetSourcePosition(expr->position());
4027
danno@chromium.org40cb8782011-05-25 07:58:50 +00004028 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004029 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004030 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004031 __ bind(&done);
4032
4033 // Store the value returned in v0.
4034 switch (assign_type) {
4035 case VARIABLE:
4036 if (expr->is_postfix()) {
4037 { EffectContext context(this);
4038 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4039 Token::ASSIGN);
4040 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4041 context.Plug(v0);
4042 }
4043 // For all contexts except EffectConstant we have the result on
4044 // top of the stack.
4045 if (!context()->IsEffect()) {
4046 context()->PlugTOS();
4047 }
4048 } else {
4049 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4050 Token::ASSIGN);
4051 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4052 context()->Plug(v0);
4053 }
4054 break;
4055 case NAMED_PROPERTY: {
4056 __ mov(a0, result_register()); // Value.
4057 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4058 __ pop(a1); // Receiver.
4059 Handle<Code> ic = is_strict_mode()
4060 ? isolate()->builtins()->StoreIC_Initialize_Strict()
4061 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004062 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004063 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4064 if (expr->is_postfix()) {
4065 if (!context()->IsEffect()) {
4066 context()->PlugTOS();
4067 }
4068 } else {
4069 context()->Plug(v0);
4070 }
4071 break;
4072 }
4073 case KEYED_PROPERTY: {
4074 __ mov(a0, result_register()); // Value.
4075 __ pop(a1); // Key.
4076 __ pop(a2); // Receiver.
4077 Handle<Code> ic = is_strict_mode()
4078 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
4079 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004080 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004081 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4082 if (expr->is_postfix()) {
4083 if (!context()->IsEffect()) {
4084 context()->PlugTOS();
4085 }
4086 } else {
4087 context()->Plug(v0);
4088 }
4089 break;
4090 }
4091 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004092}
4093
4094
lrn@chromium.org7516f052011-03-30 08:52:27 +00004095void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004096 ASSERT(!context()->IsEffect());
4097 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004098 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004099 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004100 Comment cmnt(masm_, "Global variable");
4101 __ lw(a0, GlobalObjectOperand());
4102 __ li(a2, Operand(proxy->name()));
4103 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4104 // Use a regular load, not a contextual load, to avoid a reference
4105 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004106 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004107 PrepareForBailout(expr, TOS_REG);
4108 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004109 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004110 Label done, slow;
4111
4112 // Generate code for loading from variables potentially shadowed
4113 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004114 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004115
4116 __ bind(&slow);
4117 __ li(a0, Operand(proxy->name()));
4118 __ Push(cp, a0);
4119 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4120 PrepareForBailout(expr, TOS_REG);
4121 __ bind(&done);
4122
4123 context()->Plug(v0);
4124 } else {
4125 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004126 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004127 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004128}
4129
ager@chromium.org04921a82011-06-27 13:21:41 +00004130void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004131 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004132 Handle<String> check) {
4133 Label materialize_true, materialize_false;
4134 Label* if_true = NULL;
4135 Label* if_false = NULL;
4136 Label* fall_through = NULL;
4137 context()->PrepareTest(&materialize_true, &materialize_false,
4138 &if_true, &if_false, &fall_through);
4139
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004140 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004141 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004142 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004143 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004144
4145 if (check->Equals(isolate()->heap()->number_symbol())) {
4146 __ JumpIfSmi(v0, if_true);
4147 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4148 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4149 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4150 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4151 __ JumpIfSmi(v0, if_false);
4152 // Check for undetectable objects => false.
4153 __ GetObjectType(v0, v0, a1);
4154 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4155 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4156 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4157 Split(eq, a1, Operand(zero_reg),
4158 if_true, if_false, fall_through);
4159 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4160 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4161 __ Branch(if_true, eq, v0, Operand(at));
4162 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4163 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004164 } else if (FLAG_harmony_typeof &&
4165 check->Equals(isolate()->heap()->null_symbol())) {
4166 __ LoadRoot(at, Heap::kNullValueRootIndex);
4167 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004168 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4169 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4170 __ Branch(if_true, eq, v0, Operand(at));
4171 __ JumpIfSmi(v0, if_false);
4172 // Check for undetectable objects => true.
4173 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4174 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4175 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4176 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4177 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4178 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004179 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4180 __ GetObjectType(v0, v0, a1);
4181 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4182 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4183 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004184 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4185 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004186 if (!FLAG_harmony_typeof) {
4187 __ LoadRoot(at, Heap::kNullValueRootIndex);
4188 __ Branch(if_true, eq, v0, Operand(at));
4189 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004190 // Check for JS objects => true.
4191 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004192 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004193 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004194 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004195 // Check for undetectable objects => false.
4196 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4197 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4198 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4199 } else {
4200 if (if_false != fall_through) __ jmp(if_false);
4201 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004202 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004203}
4204
4205
ager@chromium.org5c838252010-02-19 08:53:10 +00004206void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004207 Comment cmnt(masm_, "[ CompareOperation");
4208 SetSourcePosition(expr->position());
4209
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004210 // First we try a fast inlined version of the compare when one of
4211 // the operands is a literal.
4212 if (TryLiteralCompare(expr)) return;
4213
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004214 // Always perform the comparison for its control flow. Pack the result
4215 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004216 Label materialize_true, materialize_false;
4217 Label* if_true = NULL;
4218 Label* if_false = NULL;
4219 Label* fall_through = NULL;
4220 context()->PrepareTest(&materialize_true, &materialize_false,
4221 &if_true, &if_false, &fall_through);
4222
ager@chromium.org04921a82011-06-27 13:21:41 +00004223 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004224 VisitForStackValue(expr->left());
4225 switch (op) {
4226 case Token::IN:
4227 VisitForStackValue(expr->right());
4228 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004229 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004230 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4231 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4232 break;
4233
4234 case Token::INSTANCEOF: {
4235 VisitForStackValue(expr->right());
4236 InstanceofStub stub(InstanceofStub::kNoFlags);
4237 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004238 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004239 // The stub returns 0 for true.
4240 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4241 break;
4242 }
4243
4244 default: {
4245 VisitForAccumulatorValue(expr->right());
4246 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004247 switch (op) {
4248 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004249 case Token::EQ:
4250 cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004251 break;
4252 case Token::LT:
4253 cc = lt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004254 break;
4255 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004256 cc = gt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004257 break;
4258 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004259 cc = le;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004260 break;
4261 case Token::GTE:
4262 cc = ge;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004263 break;
4264 case Token::IN:
4265 case Token::INSTANCEOF:
4266 default:
4267 UNREACHABLE();
4268 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004269 __ mov(a0, result_register());
4270 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004271
4272 bool inline_smi_code = ShouldInlineSmiCase(op);
4273 JumpPatchSite patch_site(masm_);
4274 if (inline_smi_code) {
4275 Label slow_case;
4276 __ Or(a2, a0, Operand(a1));
4277 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4278 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4279 __ bind(&slow_case);
4280 }
4281 // Record position and call the compare IC.
4282 SetSourcePosition(expr->position());
4283 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004284 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004285 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004286 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004287 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4288 }
4289 }
4290
4291 // Convert the result of the comparison into one expected for this
4292 // expression's context.
4293 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004294}
4295
4296
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004297void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4298 Expression* sub_expr,
4299 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004300 Label materialize_true, materialize_false;
4301 Label* if_true = NULL;
4302 Label* if_false = NULL;
4303 Label* fall_through = NULL;
4304 context()->PrepareTest(&materialize_true, &materialize_false,
4305 &if_true, &if_false, &fall_through);
4306
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004307 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004308 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004309 Heap::RootListIndex nil_value = nil == kNullValue ?
4310 Heap::kNullValueRootIndex :
4311 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004312 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004313 __ LoadRoot(a1, nil_value);
4314 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004315 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4316 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004317 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4318 Heap::kUndefinedValueRootIndex :
4319 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004320 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004321 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004322 __ Branch(if_true, eq, a0, Operand(a1));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004323 __ JumpIfSmi(a0, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004324 // It can be an undetectable object.
4325 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4326 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4327 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4328 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4329 }
4330 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004331}
4332
4333
ager@chromium.org5c838252010-02-19 08:53:10 +00004334void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004335 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4336 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004337}
4338
4339
lrn@chromium.org7516f052011-03-30 08:52:27 +00004340Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004341 return v0;
4342}
ager@chromium.org5c838252010-02-19 08:53:10 +00004343
4344
lrn@chromium.org7516f052011-03-30 08:52:27 +00004345Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004346 return cp;
4347}
4348
4349
ager@chromium.org5c838252010-02-19 08:53:10 +00004350void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004351 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4352 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004353}
4354
4355
4356void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004357 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004358}
4359
4360
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004361void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4362 Scope* declaration_scope = scope()->DeclarationScope();
4363 if (declaration_scope->is_global_scope()) {
4364 // Contexts nested in the global context have a canonical empty function
4365 // as their closure, not the anonymous closure containing the global
4366 // code. Pass a smi sentinel and let the runtime look up the empty
4367 // function.
4368 __ li(at, Operand(Smi::FromInt(0)));
4369 } else if (declaration_scope->is_eval_scope()) {
4370 // Contexts created by a call to eval have the same closure as the
4371 // context calling eval, not the anonymous closure containing the eval
4372 // code. Fetch it from the context.
4373 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4374 } else {
4375 ASSERT(declaration_scope->is_function_scope());
4376 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4377 }
4378 __ push(at);
4379}
4380
4381
ager@chromium.org5c838252010-02-19 08:53:10 +00004382// ----------------------------------------------------------------------------
4383// Non-local control flow support.
4384
4385void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004386 ASSERT(!result_register().is(a1));
4387 // Store result register while executing finally block.
4388 __ push(result_register());
4389 // Cook return address in link register to stack (smi encoded Code* delta).
4390 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4391 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004392 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004393 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4394 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004395}
4396
4397
4398void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004399 ASSERT(!result_register().is(a1));
4400 // Restore result register from stack.
4401 __ pop(a1);
4402 // Uncook return address and return.
4403 __ pop(result_register());
4404 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4405 __ sra(a1, a1, 1); // Un-smi-tag value.
4406 __ Addu(at, a1, Operand(masm_->CodeObject()));
4407 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004408}
4409
4410
4411#undef __
4412
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004413#define __ ACCESS_MASM(masm())
4414
4415FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4416 int* stack_depth,
4417 int* context_length) {
4418 // The macros used here must preserve the result register.
4419
4420 // Because the handler block contains the context of the finally
4421 // code, we can restore it directly from there for the finally code
4422 // rather than iteratively unwinding contexts via their previous
4423 // links.
4424 __ Drop(*stack_depth); // Down to the handler block.
4425 if (*context_length > 0) {
4426 // Restore the context to its dedicated register and the stack.
4427 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4428 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4429 }
4430 __ PopTryHandler();
4431 __ Call(finally_entry_);
4432
4433 *stack_depth = 0;
4434 *context_length = 0;
4435 return previous_;
4436}
4437
4438
4439#undef __
4440
ager@chromium.org5c838252010-02-19 08:53:10 +00004441} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004442
4443#endif // V8_TARGET_ARCH_MIPS