blob: 555cad9899410bc83d7923ea798d80817362dd60 [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.orgc3b37122011-11-07 10:14:12 +00002274 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 4);
ager@chromium.org5c838252010-02-19 08:53:10 +00002275}
2276
2277
2278void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002279#ifdef DEBUG
2280 // We want to verify that RecordJSReturnSite gets called on all paths
2281 // through this function. Avoid early returns.
2282 expr->return_is_recorded_ = false;
2283#endif
2284
2285 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002286 Expression* callee = expr->expression();
2287 VariableProxy* proxy = callee->AsVariableProxy();
2288 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002289
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002290 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002291 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2292 // resolve the function we need to call and the receiver of the
2293 // call. Then we call the resolved function using the given
2294 // arguments.
2295 ZoneList<Expression*>* args = expr->arguments();
2296 int arg_count = args->length();
2297
2298 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002299 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002300 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2301 __ push(a2); // Reserved receiver slot.
2302
2303 // Push the arguments.
2304 for (int i = 0; i < arg_count; i++) {
2305 VisitForStackValue(args->at(i));
2306 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002307
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002308 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002309 // resolve eval.
2310 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2311 __ push(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002312 EmitResolvePossiblyDirectEval(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002313
2314 // The runtime call returns a pair of values in v0 (function) and
2315 // v1 (receiver). Touch up the stack with the right values.
2316 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2317 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2318 }
2319 // Record source position for debugger.
2320 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002321 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002322 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002323 __ CallStub(&stub);
2324 RecordJSReturnSite(expr);
2325 // Restore context register.
2326 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2327 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002328 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002329 // Push global object as receiver for the call IC.
2330 __ lw(a0, GlobalObjectOperand());
2331 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002332 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2333 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002334 // Call to a lookup slot (dynamically introduced variable).
2335 Label slow, done;
2336
2337 { PreservePositionScope scope(masm()->positions_recorder());
2338 // Generate code for loading from variables potentially shadowed
2339 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002340 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002341 }
2342
2343 __ bind(&slow);
2344 // Call the runtime to find the function to call (returned in v0)
2345 // and the object holding it (returned in v1).
2346 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002347 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002348 __ push(a2);
2349 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2350 __ Push(v0, v1); // Function, receiver.
2351
2352 // If fast case code has been generated, emit code to push the
2353 // function and receiver and have the slow path jump around this
2354 // code.
2355 if (done.is_linked()) {
2356 Label call;
2357 __ Branch(&call);
2358 __ bind(&done);
2359 // Push function.
2360 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002361 // The receiver is implicitly the global receiver. Indicate this
2362 // by passing the hole to the call function stub.
2363 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002364 __ push(a1);
2365 __ bind(&call);
2366 }
2367
danno@chromium.org40cb8782011-05-25 07:58:50 +00002368 // The receiver is either the global receiver or an object found
2369 // by LoadContextSlot. That object could be the hole if the
2370 // receiver is implicitly the global object.
2371 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002372 } else if (property != NULL) {
2373 { PreservePositionScope scope(masm()->positions_recorder());
2374 VisitForStackValue(property->obj());
2375 }
2376 if (property->key()->IsPropertyName()) {
2377 EmitCallWithIC(expr,
2378 property->key()->AsLiteral()->handle(),
2379 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002380 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002381 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002382 }
2383 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002384 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002385 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002386 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002387 }
2388 // Load global receiver object.
2389 __ lw(a1, GlobalObjectOperand());
2390 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2391 __ push(a1);
2392 // Emit function call.
2393 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2394 }
2395
2396#ifdef DEBUG
2397 // RecordJSReturnSite should have been called.
2398 ASSERT(expr->return_is_recorded_);
2399#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002400}
2401
2402
2403void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002404 Comment cmnt(masm_, "[ CallNew");
2405 // According to ECMA-262, section 11.2.2, page 44, the function
2406 // expression in new calls must be evaluated before the
2407 // arguments.
2408
2409 // Push constructor on the stack. If it's not a function it's used as
2410 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2411 // ignored.
2412 VisitForStackValue(expr->expression());
2413
2414 // Push the arguments ("left-to-right") on the stack.
2415 ZoneList<Expression*>* args = expr->arguments();
2416 int arg_count = args->length();
2417 for (int i = 0; i < arg_count; i++) {
2418 VisitForStackValue(args->at(i));
2419 }
2420
2421 // Call the construct call builtin that handles allocation and
2422 // constructor invocation.
2423 SetSourcePosition(expr->position());
2424
2425 // Load function and argument count into a1 and a0.
2426 __ li(a0, Operand(arg_count));
2427 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2428
2429 Handle<Code> construct_builtin =
2430 isolate()->builtins()->JSConstructCall();
2431 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2432 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002433}
2434
2435
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002436void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2437 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002438 ASSERT(args->length() == 1);
2439
2440 VisitForAccumulatorValue(args->at(0));
2441
2442 Label materialize_true, materialize_false;
2443 Label* if_true = NULL;
2444 Label* if_false = NULL;
2445 Label* fall_through = NULL;
2446 context()->PrepareTest(&materialize_true, &materialize_false,
2447 &if_true, &if_false, &fall_through);
2448
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002449 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002450 __ And(t0, v0, Operand(kSmiTagMask));
2451 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2452
2453 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002454}
2455
2456
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002457void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2458 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002459 ASSERT(args->length() == 1);
2460
2461 VisitForAccumulatorValue(args->at(0));
2462
2463 Label materialize_true, materialize_false;
2464 Label* if_true = NULL;
2465 Label* if_false = NULL;
2466 Label* fall_through = NULL;
2467 context()->PrepareTest(&materialize_true, &materialize_false,
2468 &if_true, &if_false, &fall_through);
2469
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002470 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002471 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2472 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2473
2474 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002475}
2476
2477
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002478void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2479 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002480 ASSERT(args->length() == 1);
2481
2482 VisitForAccumulatorValue(args->at(0));
2483
2484 Label materialize_true, materialize_false;
2485 Label* if_true = NULL;
2486 Label* if_false = NULL;
2487 Label* fall_through = NULL;
2488 context()->PrepareTest(&materialize_true, &materialize_false,
2489 &if_true, &if_false, &fall_through);
2490
2491 __ JumpIfSmi(v0, if_false);
2492 __ LoadRoot(at, Heap::kNullValueRootIndex);
2493 __ Branch(if_true, eq, v0, Operand(at));
2494 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2495 // Undetectable objects behave like undefined when tested with typeof.
2496 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2497 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2498 __ Branch(if_false, ne, at, Operand(zero_reg));
2499 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002500 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002501 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002502 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2503 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002504
2505 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002506}
2507
2508
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002509void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2510 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002511 ASSERT(args->length() == 1);
2512
2513 VisitForAccumulatorValue(args->at(0));
2514
2515 Label materialize_true, materialize_false;
2516 Label* if_true = NULL;
2517 Label* if_false = NULL;
2518 Label* fall_through = NULL;
2519 context()->PrepareTest(&materialize_true, &materialize_false,
2520 &if_true, &if_false, &fall_through);
2521
2522 __ JumpIfSmi(v0, if_false);
2523 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002524 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002525 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002526 if_true, if_false, fall_through);
2527
2528 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002529}
2530
2531
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002532void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2533 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002534 ASSERT(args->length() == 1);
2535
2536 VisitForAccumulatorValue(args->at(0));
2537
2538 Label materialize_true, materialize_false;
2539 Label* if_true = NULL;
2540 Label* if_false = NULL;
2541 Label* fall_through = NULL;
2542 context()->PrepareTest(&materialize_true, &materialize_false,
2543 &if_true, &if_false, &fall_through);
2544
2545 __ JumpIfSmi(v0, if_false);
2546 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2547 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2548 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002549 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002550 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2551
2552 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002553}
2554
2555
2556void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002557 CallRuntime* expr) {
2558 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002559 ASSERT(args->length() == 1);
2560
2561 VisitForAccumulatorValue(args->at(0));
2562
2563 Label materialize_true, materialize_false;
2564 Label* if_true = NULL;
2565 Label* if_false = NULL;
2566 Label* fall_through = NULL;
2567 context()->PrepareTest(&materialize_true, &materialize_false,
2568 &if_true, &if_false, &fall_through);
2569
2570 if (FLAG_debug_code) __ AbortIfSmi(v0);
2571
2572 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2573 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2574 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2575 __ Branch(if_true, ne, t0, Operand(zero_reg));
2576
2577 // Check for fast case object. Generate false result for slow case object.
2578 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2579 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2580 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2581 __ Branch(if_false, eq, a2, Operand(t0));
2582
2583 // Look for valueOf symbol in the descriptor array, and indicate false if
2584 // found. The type is not checked, so if it is a transition it is a false
2585 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002586 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002587 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2588 // t0: descriptor array
2589 // a3: length of descriptor array
2590 // Calculate the end of the descriptor array.
2591 STATIC_ASSERT(kSmiTag == 0);
2592 STATIC_ASSERT(kSmiTagSize == 1);
2593 STATIC_ASSERT(kPointerSize == 4);
2594 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2595 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2596 __ Addu(a2, a2, t1);
2597
2598 // Calculate location of the first key name.
2599 __ Addu(t0,
2600 t0,
2601 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2602 DescriptorArray::kFirstIndex * kPointerSize));
2603 // Loop through all the keys in the descriptor array. If one of these is the
2604 // symbol valueOf the result is false.
2605 Label entry, loop;
2606 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2607 // used in the loop below.
2608 __ li(t2, Operand(FACTORY->value_of_symbol()));
2609 __ jmp(&entry);
2610 __ bind(&loop);
2611 __ lw(a3, MemOperand(t0, 0));
2612 __ Branch(if_false, eq, a3, Operand(t2));
2613 __ Addu(t0, t0, Operand(kPointerSize));
2614 __ bind(&entry);
2615 __ Branch(&loop, ne, t0, Operand(a2));
2616
2617 // If a valueOf property is not found on the object check that it's
2618 // prototype is the un-modified String prototype. If not result is false.
2619 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2620 __ JumpIfSmi(a2, if_false);
2621 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2622 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2623 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2624 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2625 __ Branch(if_false, ne, a2, Operand(a3));
2626
2627 // Set the bit in the map to indicate that it has been checked safe for
2628 // default valueOf and set true result.
2629 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2630 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2631 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2632 __ jmp(if_true);
2633
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002634 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002635 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002636}
2637
2638
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002639void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2640 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002641 ASSERT(args->length() == 1);
2642
2643 VisitForAccumulatorValue(args->at(0));
2644
2645 Label materialize_true, materialize_false;
2646 Label* if_true = NULL;
2647 Label* if_false = NULL;
2648 Label* fall_through = NULL;
2649 context()->PrepareTest(&materialize_true, &materialize_false,
2650 &if_true, &if_false, &fall_through);
2651
2652 __ JumpIfSmi(v0, if_false);
2653 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002654 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002655 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2656 __ Branch(if_false);
2657
2658 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002659}
2660
2661
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002662void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2663 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002664 ASSERT(args->length() == 1);
2665
2666 VisitForAccumulatorValue(args->at(0));
2667
2668 Label materialize_true, materialize_false;
2669 Label* if_true = NULL;
2670 Label* if_false = NULL;
2671 Label* fall_through = NULL;
2672 context()->PrepareTest(&materialize_true, &materialize_false,
2673 &if_true, &if_false, &fall_through);
2674
2675 __ JumpIfSmi(v0, if_false);
2676 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002677 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002678 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2679 if_true, if_false, fall_through);
2680
2681 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002682}
2683
2684
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002685void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2686 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002687 ASSERT(args->length() == 1);
2688
2689 VisitForAccumulatorValue(args->at(0));
2690
2691 Label materialize_true, materialize_false;
2692 Label* if_true = NULL;
2693 Label* if_false = NULL;
2694 Label* fall_through = NULL;
2695 context()->PrepareTest(&materialize_true, &materialize_false,
2696 &if_true, &if_false, &fall_through);
2697
2698 __ JumpIfSmi(v0, if_false);
2699 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002700 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002701 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2702
2703 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002704}
2705
2706
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002707void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2708 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002709
2710 Label materialize_true, materialize_false;
2711 Label* if_true = NULL;
2712 Label* if_false = NULL;
2713 Label* fall_through = NULL;
2714 context()->PrepareTest(&materialize_true, &materialize_false,
2715 &if_true, &if_false, &fall_through);
2716
2717 // Get the frame pointer for the calling frame.
2718 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2719
2720 // Skip the arguments adaptor frame if it exists.
2721 Label check_frame_marker;
2722 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2723 __ Branch(&check_frame_marker, ne,
2724 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2725 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2726
2727 // Check the marker in the calling frame.
2728 __ bind(&check_frame_marker);
2729 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002730 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002731 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2732 if_true, if_false, fall_through);
2733
2734 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002735}
2736
2737
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002738void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2739 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002740 ASSERT(args->length() == 2);
2741
2742 // Load the two objects into registers and perform the comparison.
2743 VisitForStackValue(args->at(0));
2744 VisitForAccumulatorValue(args->at(1));
2745
2746 Label materialize_true, materialize_false;
2747 Label* if_true = NULL;
2748 Label* if_false = NULL;
2749 Label* fall_through = NULL;
2750 context()->PrepareTest(&materialize_true, &materialize_false,
2751 &if_true, &if_false, &fall_through);
2752
2753 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002754 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002755 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2756
2757 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002758}
2759
2760
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002761void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2762 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002763 ASSERT(args->length() == 1);
2764
2765 // ArgumentsAccessStub expects the key in a1 and the formal
2766 // parameter count in a0.
2767 VisitForAccumulatorValue(args->at(0));
2768 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002769 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002770 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2771 __ CallStub(&stub);
2772 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002773}
2774
2775
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002776void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2777 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002778 Label exit;
2779 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002780 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002781
2782 // Check if the calling frame is an arguments adaptor frame.
2783 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2784 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2785 __ Branch(&exit, ne, a3,
2786 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2787
2788 // Arguments adaptor case: Read the arguments length from the
2789 // adaptor frame.
2790 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2791
2792 __ bind(&exit);
2793 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002794}
2795
2796
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002797void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2798 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002799 ASSERT(args->length() == 1);
2800 Label done, null, function, non_function_constructor;
2801
2802 VisitForAccumulatorValue(args->at(0));
2803
2804 // If the object is a smi, we return null.
2805 __ JumpIfSmi(v0, &null);
2806
2807 // Check that the object is a JS object but take special care of JS
2808 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002809 // Assume that there are only two callable types, and one of them is at
2810 // either end of the type range for JS object types. Saves extra comparisons.
2811 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002812 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002813 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002814
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002815 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2816 FIRST_SPEC_OBJECT_TYPE + 1);
2817 __ Branch(&function, eq, 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(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2820 LAST_SPEC_OBJECT_TYPE - 1);
2821 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2822 // Assume that there is no larger type.
2823 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2824
2825 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002826 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2827 __ GetObjectType(v0, a1, a1);
2828 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2829
2830 // v0 now contains the constructor function. Grab the
2831 // instance class name from there.
2832 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2833 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2834 __ Branch(&done);
2835
2836 // Functions have class 'Function'.
2837 __ bind(&function);
2838 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2839 __ jmp(&done);
2840
2841 // Objects with a non-function constructor have class 'Object'.
2842 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002843 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002844 __ jmp(&done);
2845
2846 // Non-JS objects have class null.
2847 __ bind(&null);
2848 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2849
2850 // All done.
2851 __ bind(&done);
2852
2853 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002854}
2855
2856
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002857void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002858 // Conditionally generate a log call.
2859 // Args:
2860 // 0 (literal string): The type of logging (corresponds to the flags).
2861 // This is used to determine whether or not to generate the log call.
2862 // 1 (string): Format string. Access the string at argument index 2
2863 // with '%2s' (see Logger::LogRuntime for all the formats).
2864 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002865 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002866 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002867 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2868 VisitForStackValue(args->at(1));
2869 VisitForStackValue(args->at(2));
2870 __ CallRuntime(Runtime::kLog, 2);
2871 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002872
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002873 // Finally, we're expected to leave a value on the top of the stack.
2874 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2875 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002876}
2877
2878
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002879void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
2880 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002881 Label slow_allocate_heapnumber;
2882 Label heapnumber_allocated;
2883
2884 // Save the new heap number in callee-saved register s0, since
2885 // we call out to external C code below.
2886 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2887 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2888 __ jmp(&heapnumber_allocated);
2889
2890 __ bind(&slow_allocate_heapnumber);
2891
2892 // Allocate a heap number.
2893 __ CallRuntime(Runtime::kNumberAlloc, 0);
2894 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2895
2896 __ bind(&heapnumber_allocated);
2897
2898 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2899 // by computing:
2900 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2901 if (CpuFeatures::IsSupported(FPU)) {
2902 __ PrepareCallCFunction(1, a0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002903 __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
2904 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002905 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2906
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002907 CpuFeatures::Scope scope(FPU);
2908 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2909 __ li(a1, Operand(0x41300000));
2910 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002911 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002912 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002913 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002914 // Subtract and store the result in the heap number.
2915 __ sub_d(f0, f12, f14);
2916 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2917 __ mov(v0, s0);
2918 } else {
2919 __ PrepareCallCFunction(2, a0);
2920 __ mov(a0, s0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002921 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
2922 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002923 __ CallCFunction(
2924 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2925 }
2926
2927 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002928}
2929
2930
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002931void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002932 // Load the arguments on the stack and call the stub.
2933 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002934 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002935 ASSERT(args->length() == 3);
2936 VisitForStackValue(args->at(0));
2937 VisitForStackValue(args->at(1));
2938 VisitForStackValue(args->at(2));
2939 __ CallStub(&stub);
2940 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002941}
2942
2943
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002944void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002945 // Load the arguments on the stack and call the stub.
2946 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002947 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002948 ASSERT(args->length() == 4);
2949 VisitForStackValue(args->at(0));
2950 VisitForStackValue(args->at(1));
2951 VisitForStackValue(args->at(2));
2952 VisitForStackValue(args->at(3));
2953 __ CallStub(&stub);
2954 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002955}
2956
2957
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002958void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
2959 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002960 ASSERT(args->length() == 1);
2961
2962 VisitForAccumulatorValue(args->at(0)); // Load the object.
2963
2964 Label done;
2965 // If the object is a smi return the object.
2966 __ JumpIfSmi(v0, &done);
2967 // If the object is not a value type, return the object.
2968 __ GetObjectType(v0, a1, a1);
2969 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2970
2971 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2972
2973 __ bind(&done);
2974 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002975}
2976
2977
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002978void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002979 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002980 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002981 ASSERT(args->length() == 2);
2982 VisitForStackValue(args->at(0));
2983 VisitForStackValue(args->at(1));
2984 MathPowStub stub;
2985 __ CallStub(&stub);
2986 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002987}
2988
2989
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002990void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
2991 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002992 ASSERT(args->length() == 2);
2993
2994 VisitForStackValue(args->at(0)); // Load the object.
2995 VisitForAccumulatorValue(args->at(1)); // Load the value.
2996 __ pop(a1); // v0 = value. a1 = object.
2997
2998 Label done;
2999 // If the object is a smi, return the value.
3000 __ JumpIfSmi(a1, &done);
3001
3002 // If the object is not a value type, return the value.
3003 __ GetObjectType(a1, a2, a2);
3004 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
3005
3006 // Store the value.
3007 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
3008 // Update the write barrier. Save the value as it will be
3009 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003010 __ mov(a2, v0);
3011 __ RecordWriteField(
3012 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003013
3014 __ bind(&done);
3015 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003016}
3017
3018
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003019void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3020 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003021 ASSERT_EQ(args->length(), 1);
3022
3023 // Load the argument on the stack and call the stub.
3024 VisitForStackValue(args->at(0));
3025
3026 NumberToStringStub stub;
3027 __ CallStub(&stub);
3028 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003029}
3030
3031
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003032void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3033 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003034 ASSERT(args->length() == 1);
3035
3036 VisitForAccumulatorValue(args->at(0));
3037
3038 Label done;
3039 StringCharFromCodeGenerator generator(v0, a1);
3040 generator.GenerateFast(masm_);
3041 __ jmp(&done);
3042
3043 NopRuntimeCallHelper call_helper;
3044 generator.GenerateSlow(masm_, call_helper);
3045
3046 __ bind(&done);
3047 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003048}
3049
3050
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003051void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3052 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003053 ASSERT(args->length() == 2);
3054
3055 VisitForStackValue(args->at(0));
3056 VisitForAccumulatorValue(args->at(1));
3057 __ mov(a0, result_register());
3058
3059 Register object = a1;
3060 Register index = a0;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003061 Register result = v0;
3062
3063 __ pop(object);
3064
3065 Label need_conversion;
3066 Label index_out_of_range;
3067 Label done;
3068 StringCharCodeAtGenerator generator(object,
3069 index,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003070 result,
3071 &need_conversion,
3072 &need_conversion,
3073 &index_out_of_range,
3074 STRING_INDEX_IS_NUMBER);
3075 generator.GenerateFast(masm_);
3076 __ jmp(&done);
3077
3078 __ bind(&index_out_of_range);
3079 // When the index is out of range, the spec requires us to return
3080 // NaN.
3081 __ LoadRoot(result, Heap::kNanValueRootIndex);
3082 __ jmp(&done);
3083
3084 __ bind(&need_conversion);
3085 // Load the undefined value into the result register, which will
3086 // trigger conversion.
3087 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3088 __ jmp(&done);
3089
3090 NopRuntimeCallHelper call_helper;
3091 generator.GenerateSlow(masm_, call_helper);
3092
3093 __ bind(&done);
3094 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003095}
3096
3097
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003098void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3099 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003100 ASSERT(args->length() == 2);
3101
3102 VisitForStackValue(args->at(0));
3103 VisitForAccumulatorValue(args->at(1));
3104 __ mov(a0, result_register());
3105
3106 Register object = a1;
3107 Register index = a0;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003108 Register scratch = a3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003109 Register result = v0;
3110
3111 __ pop(object);
3112
3113 Label need_conversion;
3114 Label index_out_of_range;
3115 Label done;
3116 StringCharAtGenerator generator(object,
3117 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003118 scratch,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003119 result,
3120 &need_conversion,
3121 &need_conversion,
3122 &index_out_of_range,
3123 STRING_INDEX_IS_NUMBER);
3124 generator.GenerateFast(masm_);
3125 __ jmp(&done);
3126
3127 __ bind(&index_out_of_range);
3128 // When the index is out of range, the spec requires us to return
3129 // the empty string.
3130 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3131 __ jmp(&done);
3132
3133 __ bind(&need_conversion);
3134 // Move smi zero into the result register, which will trigger
3135 // conversion.
3136 __ li(result, Operand(Smi::FromInt(0)));
3137 __ jmp(&done);
3138
3139 NopRuntimeCallHelper call_helper;
3140 generator.GenerateSlow(masm_, call_helper);
3141
3142 __ bind(&done);
3143 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003144}
3145
3146
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003147void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3148 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003149 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003150 VisitForStackValue(args->at(0));
3151 VisitForStackValue(args->at(1));
3152
3153 StringAddStub stub(NO_STRING_ADD_FLAGS);
3154 __ CallStub(&stub);
3155 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003156}
3157
3158
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003159void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3160 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003161 ASSERT_EQ(2, args->length());
3162
3163 VisitForStackValue(args->at(0));
3164 VisitForStackValue(args->at(1));
3165
3166 StringCompareStub stub;
3167 __ CallStub(&stub);
3168 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003169}
3170
3171
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003172void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003173 // Load the argument on the stack and call the stub.
3174 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3175 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003176 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003177 ASSERT(args->length() == 1);
3178 VisitForStackValue(args->at(0));
3179 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3180 __ CallStub(&stub);
3181 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003182}
3183
3184
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003185void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003186 // Load the argument on the stack and call the stub.
3187 TranscendentalCacheStub stub(TranscendentalCache::COS,
3188 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003189 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003190 ASSERT(args->length() == 1);
3191 VisitForStackValue(args->at(0));
3192 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3193 __ CallStub(&stub);
3194 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003195}
3196
3197
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003198void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003199 // Load the argument on the stack and call the stub.
3200 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3201 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003202 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003203 ASSERT(args->length() == 1);
3204 VisitForStackValue(args->at(0));
3205 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3206 __ CallStub(&stub);
3207 context()->Plug(v0);
3208}
3209
3210
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003211void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003212 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003213 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003214 ASSERT(args->length() == 1);
3215 VisitForStackValue(args->at(0));
3216 __ CallRuntime(Runtime::kMath_sqrt, 1);
3217 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003218}
3219
3220
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003221void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3222 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003223 ASSERT(args->length() >= 2);
3224
3225 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3226 for (int i = 0; i < arg_count + 1; i++) {
3227 VisitForStackValue(args->at(i));
3228 }
3229 VisitForAccumulatorValue(args->last()); // Function.
3230
danno@chromium.orgc612e022011-11-10 11:38:15 +00003231 // Check for proxy.
3232 Label proxy, done;
3233 __ GetObjectType(v0, a1, a1);
3234 __ Branch(&proxy, eq, a1, Operand(JS_FUNCTION_PROXY_TYPE));
3235
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003236 // InvokeFunction requires the function in a1. Move it in there.
3237 __ mov(a1, result_register());
3238 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003239 __ InvokeFunction(a1, count, CALL_FUNCTION,
3240 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003241 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003242 __ jmp(&done);
3243
3244 __ bind(&proxy);
3245 __ push(v0);
3246 __ CallRuntime(Runtime::kCall, args->length());
3247 __ bind(&done);
3248
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003249 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003250}
3251
3252
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003253void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003254 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003255 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003256 ASSERT(args->length() == 3);
3257 VisitForStackValue(args->at(0));
3258 VisitForStackValue(args->at(1));
3259 VisitForStackValue(args->at(2));
3260 __ CallStub(&stub);
3261 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003262}
3263
3264
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003265void FullCodeGenerator::EmitSwapElements(CallRuntime* expr) {
3266 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003267 ASSERT(args->length() == 3);
3268 VisitForStackValue(args->at(0));
3269 VisitForStackValue(args->at(1));
3270 VisitForStackValue(args->at(2));
3271 Label done;
3272 Label slow_case;
3273 Register object = a0;
3274 Register index1 = a1;
3275 Register index2 = a2;
3276 Register elements = a3;
3277 Register scratch1 = t0;
3278 Register scratch2 = t1;
3279
3280 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3281 // Fetch the map and check if array is in fast case.
3282 // Check that object doesn't require security checks and
3283 // has no indexed interceptor.
3284 __ GetObjectType(object, scratch1, scratch2);
3285 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3286 // Map is now in scratch1.
3287
3288 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3289 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3290 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3291
3292 // Check the object's elements are in fast case and writable.
3293 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3294 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3295 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3296 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3297
3298 // Check that both indices are smis.
3299 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3300 __ lw(index2, MemOperand(sp, 0));
3301 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3302
3303 // Check that both indices are valid.
3304 Label not_hi;
3305 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3306 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3307 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3308 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3309 __ bind(&not_hi);
3310
3311 // Bring the address of the elements into index1 and index2.
3312 __ Addu(scratch1, elements,
3313 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3314 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3315 __ Addu(index1, scratch1, index1);
3316 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3317 __ Addu(index2, scratch1, index2);
3318
3319 // Swap elements.
3320 __ lw(scratch1, MemOperand(index1, 0));
3321 __ lw(scratch2, MemOperand(index2, 0));
3322 __ sw(scratch1, MemOperand(index2, 0));
3323 __ sw(scratch2, MemOperand(index1, 0));
3324
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003325 Label no_remembered_set;
3326 __ CheckPageFlag(elements,
3327 scratch1,
3328 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3329 ne,
3330 &no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003331 // Possible optimization: do a check that both values are Smis
3332 // (or them and test against Smi mask).
3333
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003334 // We are swapping two objects in an array and the incremental marker never
3335 // pauses in the middle of scanning a single object. Therefore the
3336 // incremental marker is not disturbed, so we don't need to call the
3337 // RecordWrite stub that notifies the incremental marker.
3338 __ RememberedSetHelper(elements,
3339 index1,
3340 scratch2,
3341 kDontSaveFPRegs,
3342 MacroAssembler::kFallThroughAtEnd);
3343 __ RememberedSetHelper(elements,
3344 index2,
3345 scratch2,
3346 kDontSaveFPRegs,
3347 MacroAssembler::kFallThroughAtEnd);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003348
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003349 __ bind(&no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003350 // We are done. Drop elements from the stack, and return undefined.
3351 __ Drop(3);
3352 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3353 __ jmp(&done);
3354
3355 __ bind(&slow_case);
3356 __ CallRuntime(Runtime::kSwapElements, 3);
3357
3358 __ bind(&done);
3359 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003360}
3361
3362
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003363void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3364 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003365 ASSERT_EQ(2, args->length());
3366
3367 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3368 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3369
3370 Handle<FixedArray> jsfunction_result_caches(
3371 isolate()->global_context()->jsfunction_result_caches());
3372 if (jsfunction_result_caches->length() <= cache_id) {
3373 __ Abort("Attempt to use undefined cache.");
3374 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3375 context()->Plug(v0);
3376 return;
3377 }
3378
3379 VisitForAccumulatorValue(args->at(1));
3380
3381 Register key = v0;
3382 Register cache = a1;
3383 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3384 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3385 __ lw(cache,
3386 ContextOperand(
3387 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3388 __ lw(cache,
3389 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3390
3391
3392 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003393 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003394 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3395 // a2 now holds finger offset as a smi.
3396 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3397 // a3 now points to the start of fixed array elements.
3398 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3399 __ addu(a3, a3, at);
3400 // a3 now points to key of indexed element of cache.
3401 __ lw(a2, MemOperand(a3));
3402 __ Branch(&not_found, ne, key, Operand(a2));
3403
3404 __ lw(v0, MemOperand(a3, kPointerSize));
3405 __ Branch(&done);
3406
3407 __ bind(&not_found);
3408 // Call runtime to perform the lookup.
3409 __ Push(cache, key);
3410 __ CallRuntime(Runtime::kGetFromCache, 2);
3411
3412 __ bind(&done);
3413 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003414}
3415
3416
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003417void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3418 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003419 ASSERT_EQ(2, args->length());
3420
3421 Register right = v0;
3422 Register left = a1;
3423 Register tmp = a2;
3424 Register tmp2 = a3;
3425
3426 VisitForStackValue(args->at(0));
3427 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3428 __ pop(left);
3429
3430 Label done, fail, ok;
3431 __ Branch(&ok, eq, left, Operand(right));
3432 // Fail if either is a non-HeapObject.
3433 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003434 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003435 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3436 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3437 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3438 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3439 __ Branch(&fail, ne, tmp, Operand(tmp2));
3440 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3441 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3442 __ Branch(&ok, eq, tmp, Operand(tmp2));
3443 __ bind(&fail);
3444 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3445 __ jmp(&done);
3446 __ bind(&ok);
3447 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3448 __ bind(&done);
3449
3450 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003451}
3452
3453
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003454void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3455 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003456 VisitForAccumulatorValue(args->at(0));
3457
3458 Label materialize_true, materialize_false;
3459 Label* if_true = NULL;
3460 Label* if_false = NULL;
3461 Label* fall_through = NULL;
3462 context()->PrepareTest(&materialize_true, &materialize_false,
3463 &if_true, &if_false, &fall_through);
3464
3465 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3466 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3467
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003468 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003469 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3470
3471 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003472}
3473
3474
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003475void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3476 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003477 ASSERT(args->length() == 1);
3478 VisitForAccumulatorValue(args->at(0));
3479
3480 if (FLAG_debug_code) {
3481 __ AbortIfNotString(v0);
3482 }
3483
3484 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3485 __ IndexFromHash(v0, v0);
3486
3487 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003488}
3489
3490
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003491void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003492 Label bailout, done, one_char_separator, long_separator,
3493 non_trivial_array, not_size_one_array, loop,
3494 empty_separator_loop, one_char_separator_loop,
3495 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003496 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003497 ASSERT(args->length() == 2);
3498 VisitForStackValue(args->at(1));
3499 VisitForAccumulatorValue(args->at(0));
3500
3501 // All aliases of the same register have disjoint lifetimes.
3502 Register array = v0;
3503 Register elements = no_reg; // Will be v0.
3504 Register result = no_reg; // Will be v0.
3505 Register separator = a1;
3506 Register array_length = a2;
3507 Register result_pos = no_reg; // Will be a2.
3508 Register string_length = a3;
3509 Register string = t0;
3510 Register element = t1;
3511 Register elements_end = t2;
3512 Register scratch1 = t3;
3513 Register scratch2 = t5;
3514 Register scratch3 = t4;
3515 Register scratch4 = v1;
3516
3517 // Separator operand is on the stack.
3518 __ pop(separator);
3519
3520 // Check that the array is a JSArray.
3521 __ JumpIfSmi(array, &bailout);
3522 __ GetObjectType(array, scratch1, scratch2);
3523 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3524
3525 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003526 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003527
3528 // If the array has length zero, return the empty string.
3529 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3530 __ SmiUntag(array_length);
3531 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3532 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3533 __ Branch(&done);
3534
3535 __ bind(&non_trivial_array);
3536
3537 // Get the FixedArray containing array's elements.
3538 elements = array;
3539 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3540 array = no_reg; // End of array's live range.
3541
3542 // Check that all array elements are sequential ASCII strings, and
3543 // accumulate the sum of their lengths, as a smi-encoded value.
3544 __ mov(string_length, zero_reg);
3545 __ Addu(element,
3546 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3547 __ sll(elements_end, array_length, kPointerSizeLog2);
3548 __ Addu(elements_end, element, elements_end);
3549 // Loop condition: while (element < elements_end).
3550 // Live values in registers:
3551 // elements: Fixed array of strings.
3552 // array_length: Length of the fixed array of strings (not smi)
3553 // separator: Separator string
3554 // string_length: Accumulated sum of string lengths (smi).
3555 // element: Current array element.
3556 // elements_end: Array end.
3557 if (FLAG_debug_code) {
3558 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3559 array_length, Operand(zero_reg));
3560 }
3561 __ bind(&loop);
3562 __ lw(string, MemOperand(element));
3563 __ Addu(element, element, kPointerSize);
3564 __ JumpIfSmi(string, &bailout);
3565 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3566 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3567 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3568 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3569 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3570 __ BranchOnOverflow(&bailout, scratch3);
3571 __ Branch(&loop, lt, element, Operand(elements_end));
3572
3573 // If array_length is 1, return elements[0], a string.
3574 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3575 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3576 __ Branch(&done);
3577
3578 __ bind(&not_size_one_array);
3579
3580 // Live values in registers:
3581 // separator: Separator string
3582 // array_length: Length of the array.
3583 // string_length: Sum of string lengths (smi).
3584 // elements: FixedArray of strings.
3585
3586 // Check that the separator is a flat ASCII string.
3587 __ JumpIfSmi(separator, &bailout);
3588 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3589 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3590 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3591
3592 // Add (separator length times array_length) - separator length to the
3593 // string_length to get the length of the result string. array_length is not
3594 // smi but the other values are, so the result is a smi.
3595 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3596 __ Subu(string_length, string_length, Operand(scratch1));
3597 __ Mult(array_length, scratch1);
3598 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3599 // zero.
3600 __ mfhi(scratch2);
3601 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3602 __ mflo(scratch2);
3603 __ And(scratch3, scratch2, Operand(0x80000000));
3604 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3605 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3606 __ BranchOnOverflow(&bailout, scratch3);
3607 __ SmiUntag(string_length);
3608
3609 // Get first element in the array to free up the elements register to be used
3610 // for the result.
3611 __ Addu(element,
3612 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3613 result = elements; // End of live range for elements.
3614 elements = no_reg;
3615 // Live values in registers:
3616 // element: First array element
3617 // separator: Separator string
3618 // string_length: Length of result string (not smi)
3619 // array_length: Length of the array.
3620 __ AllocateAsciiString(result,
3621 string_length,
3622 scratch1,
3623 scratch2,
3624 elements_end,
3625 &bailout);
3626 // Prepare for looping. Set up elements_end to end of the array. Set
3627 // result_pos to the position of the result where to write the first
3628 // character.
3629 __ sll(elements_end, array_length, kPointerSizeLog2);
3630 __ Addu(elements_end, element, elements_end);
3631 result_pos = array_length; // End of live range for array_length.
3632 array_length = no_reg;
3633 __ Addu(result_pos,
3634 result,
3635 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3636
3637 // Check the length of the separator.
3638 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3639 __ li(at, Operand(Smi::FromInt(1)));
3640 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3641 __ Branch(&long_separator, gt, scratch1, Operand(at));
3642
3643 // Empty separator case.
3644 __ bind(&empty_separator_loop);
3645 // Live values in registers:
3646 // result_pos: the position to which we are currently copying characters.
3647 // element: Current array element.
3648 // elements_end: Array end.
3649
3650 // Copy next array element to the result.
3651 __ lw(string, MemOperand(element));
3652 __ Addu(element, element, kPointerSize);
3653 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3654 __ SmiUntag(string_length);
3655 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3656 __ CopyBytes(string, result_pos, string_length, scratch1);
3657 // End while (element < elements_end).
3658 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3659 ASSERT(result.is(v0));
3660 __ Branch(&done);
3661
3662 // One-character separator case.
3663 __ bind(&one_char_separator);
3664 // Replace separator with its ascii character value.
3665 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3666 // Jump into the loop after the code that copies the separator, so the first
3667 // element is not preceded by a separator.
3668 __ jmp(&one_char_separator_loop_entry);
3669
3670 __ bind(&one_char_separator_loop);
3671 // Live values in registers:
3672 // result_pos: the position to which we are currently copying characters.
3673 // element: Current array element.
3674 // elements_end: Array end.
3675 // separator: Single separator ascii char (in lower byte).
3676
3677 // Copy the separator character to the result.
3678 __ sb(separator, MemOperand(result_pos));
3679 __ Addu(result_pos, result_pos, 1);
3680
3681 // Copy next array element to the result.
3682 __ bind(&one_char_separator_loop_entry);
3683 __ lw(string, MemOperand(element));
3684 __ Addu(element, element, kPointerSize);
3685 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3686 __ SmiUntag(string_length);
3687 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3688 __ CopyBytes(string, result_pos, string_length, scratch1);
3689 // End while (element < elements_end).
3690 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3691 ASSERT(result.is(v0));
3692 __ Branch(&done);
3693
3694 // Long separator case (separator is more than one character). Entry is at the
3695 // label long_separator below.
3696 __ bind(&long_separator_loop);
3697 // Live values in registers:
3698 // result_pos: the position to which we are currently copying characters.
3699 // element: Current array element.
3700 // elements_end: Array end.
3701 // separator: Separator string.
3702
3703 // Copy the separator to the result.
3704 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3705 __ SmiUntag(string_length);
3706 __ Addu(string,
3707 separator,
3708 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3709 __ CopyBytes(string, result_pos, string_length, scratch1);
3710
3711 __ bind(&long_separator);
3712 __ lw(string, MemOperand(element));
3713 __ Addu(element, element, kPointerSize);
3714 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3715 __ SmiUntag(string_length);
3716 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3717 __ CopyBytes(string, result_pos, string_length, scratch1);
3718 // End while (element < elements_end).
3719 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3720 ASSERT(result.is(v0));
3721 __ Branch(&done);
3722
3723 __ bind(&bailout);
3724 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3725 __ bind(&done);
3726 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003727}
3728
3729
ager@chromium.org5c838252010-02-19 08:53:10 +00003730void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003731 Handle<String> name = expr->name();
3732 if (name->length() > 0 && name->Get(0) == '_') {
3733 Comment cmnt(masm_, "[ InlineRuntimeCall");
3734 EmitInlineRuntimeCall(expr);
3735 return;
3736 }
3737
3738 Comment cmnt(masm_, "[ CallRuntime");
3739 ZoneList<Expression*>* args = expr->arguments();
3740
3741 if (expr->is_jsruntime()) {
3742 // Prepare for calling JS runtime function.
3743 __ lw(a0, GlobalObjectOperand());
3744 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3745 __ push(a0);
3746 }
3747
3748 // Push the arguments ("left-to-right").
3749 int arg_count = args->length();
3750 for (int i = 0; i < arg_count; i++) {
3751 VisitForStackValue(args->at(i));
3752 }
3753
3754 if (expr->is_jsruntime()) {
3755 // Call the JS runtime function.
3756 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003757 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003758 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003759 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003760 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003761 // Restore context register.
3762 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3763 } else {
3764 // Call the C runtime function.
3765 __ CallRuntime(expr->function(), arg_count);
3766 }
3767 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003768}
3769
3770
3771void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003772 switch (expr->op()) {
3773 case Token::DELETE: {
3774 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003775 Property* property = expr->expression()->AsProperty();
3776 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003777
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003778 if (property != NULL) {
3779 VisitForStackValue(property->obj());
3780 VisitForStackValue(property->key());
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003781 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3782 __ push(a1);
3783 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3784 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003785 } else if (proxy != NULL) {
3786 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003787 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003788 // but "delete this" is allowed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003789 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003790 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003791 __ lw(a2, GlobalObjectOperand());
3792 __ li(a1, Operand(var->name()));
3793 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3794 __ Push(a2, a1, a0);
3795 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3796 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003797 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003798 // Result of deleting non-global, non-dynamic variables is false.
3799 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003800 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003801 } else {
3802 // Non-global variable. Call the runtime to try to delete from the
3803 // context where the variable was introduced.
3804 __ push(context_register());
3805 __ li(a2, Operand(var->name()));
3806 __ push(a2);
3807 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3808 context()->Plug(v0);
3809 }
3810 } else {
3811 // Result of deleting non-property, non-variable reference is true.
3812 // The subexpression may have side effects.
3813 VisitForEffect(expr->expression());
3814 context()->Plug(true);
3815 }
3816 break;
3817 }
3818
3819 case Token::VOID: {
3820 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3821 VisitForEffect(expr->expression());
3822 context()->Plug(Heap::kUndefinedValueRootIndex);
3823 break;
3824 }
3825
3826 case Token::NOT: {
3827 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3828 if (context()->IsEffect()) {
3829 // Unary NOT has no side effects so it's only necessary to visit the
3830 // subexpression. Match the optimizing compiler by not branching.
3831 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003832 } else if (context()->IsTest()) {
3833 const TestContext* test = TestContext::cast(context());
3834 // The labels are swapped for the recursive call.
3835 VisitForControl(expr->expression(),
3836 test->false_label(),
3837 test->true_label(),
3838 test->fall_through());
3839 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003840 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003841 // We handle value contexts explicitly rather than simply visiting
3842 // for control and plugging the control flow into the context,
3843 // because we need to prepare a pair of extra administrative AST ids
3844 // for the optimizing compiler.
3845 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3846 Label materialize_true, materialize_false, done;
3847 VisitForControl(expr->expression(),
3848 &materialize_false,
3849 &materialize_true,
3850 &materialize_true);
3851 __ bind(&materialize_true);
3852 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3853 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3854 if (context()->IsStackValue()) __ push(v0);
3855 __ jmp(&done);
3856 __ bind(&materialize_false);
3857 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3858 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3859 if (context()->IsStackValue()) __ push(v0);
3860 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003861 }
3862 break;
3863 }
3864
3865 case Token::TYPEOF: {
3866 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3867 { StackValueContext context(this);
3868 VisitForTypeofValue(expr->expression());
3869 }
3870 __ CallRuntime(Runtime::kTypeof, 1);
3871 context()->Plug(v0);
3872 break;
3873 }
3874
3875 case Token::ADD: {
3876 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3877 VisitForAccumulatorValue(expr->expression());
3878 Label no_conversion;
3879 __ JumpIfSmi(result_register(), &no_conversion);
3880 __ mov(a0, result_register());
3881 ToNumberStub convert_stub;
3882 __ CallStub(&convert_stub);
3883 __ bind(&no_conversion);
3884 context()->Plug(result_register());
3885 break;
3886 }
3887
3888 case Token::SUB:
3889 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3890 break;
3891
3892 case Token::BIT_NOT:
3893 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3894 break;
3895
3896 default:
3897 UNREACHABLE();
3898 }
3899}
3900
3901
3902void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3903 const char* comment) {
3904 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3905 Comment cmt(masm_, comment);
3906 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3907 UnaryOverwriteMode overwrite =
3908 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003909 UnaryOpStub stub(expr->op(), overwrite);
3910 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003911 VisitForAccumulatorValue(expr->expression());
3912 SetSourcePosition(expr->position());
3913 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003914 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003915 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003916}
3917
3918
3919void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003920 Comment cmnt(masm_, "[ CountOperation");
3921 SetSourcePosition(expr->position());
3922
3923 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3924 // as the left-hand side.
3925 if (!expr->expression()->IsValidLeftHandSide()) {
3926 VisitForEffect(expr->expression());
3927 return;
3928 }
3929
3930 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003931 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003932 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3933 LhsKind assign_type = VARIABLE;
3934 Property* prop = expr->expression()->AsProperty();
3935 // In case of a property we use the uninitialized expression context
3936 // of the key to detect a named property.
3937 if (prop != NULL) {
3938 assign_type =
3939 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3940 }
3941
3942 // Evaluate expression and get value.
3943 if (assign_type == VARIABLE) {
3944 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3945 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003946 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003947 } else {
3948 // Reserve space for result of postfix operation.
3949 if (expr->is_postfix() && !context()->IsEffect()) {
3950 __ li(at, Operand(Smi::FromInt(0)));
3951 __ push(at);
3952 }
3953 if (assign_type == NAMED_PROPERTY) {
3954 // Put the object both on the stack and in the accumulator.
3955 VisitForAccumulatorValue(prop->obj());
3956 __ push(v0);
3957 EmitNamedPropertyLoad(prop);
3958 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003959 VisitForStackValue(prop->obj());
3960 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003961 __ lw(a1, MemOperand(sp, 0));
3962 __ push(v0);
3963 EmitKeyedPropertyLoad(prop);
3964 }
3965 }
3966
3967 // We need a second deoptimization point after loading the value
3968 // in case evaluating the property load my have a side effect.
3969 if (assign_type == VARIABLE) {
3970 PrepareForBailout(expr->expression(), TOS_REG);
3971 } else {
3972 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3973 }
3974
3975 // Call ToNumber only if operand is not a smi.
3976 Label no_conversion;
3977 __ JumpIfSmi(v0, &no_conversion);
3978 __ mov(a0, v0);
3979 ToNumberStub convert_stub;
3980 __ CallStub(&convert_stub);
3981 __ bind(&no_conversion);
3982
3983 // Save result for postfix expressions.
3984 if (expr->is_postfix()) {
3985 if (!context()->IsEffect()) {
3986 // Save the result on the stack. If we have a named or keyed property
3987 // we store the result under the receiver that is currently on top
3988 // of the stack.
3989 switch (assign_type) {
3990 case VARIABLE:
3991 __ push(v0);
3992 break;
3993 case NAMED_PROPERTY:
3994 __ sw(v0, MemOperand(sp, kPointerSize));
3995 break;
3996 case KEYED_PROPERTY:
3997 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3998 break;
3999 }
4000 }
4001 }
4002 __ mov(a0, result_register());
4003
4004 // Inline smi case if we are in a loop.
4005 Label stub_call, done;
4006 JumpPatchSite patch_site(masm_);
4007
4008 int count_value = expr->op() == Token::INC ? 1 : -1;
4009 __ li(a1, Operand(Smi::FromInt(count_value)));
4010
4011 if (ShouldInlineSmiCase(expr->op())) {
4012 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
4013 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
4014
4015 // We could eliminate this smi check if we split the code at
4016 // the first smi check before calling ToNumber.
4017 patch_site.EmitJumpIfSmi(v0, &done);
4018 __ bind(&stub_call);
4019 }
4020
4021 // Record position before stub call.
4022 SetSourcePosition(expr->position());
4023
danno@chromium.org40cb8782011-05-25 07:58:50 +00004024 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004025 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004026 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004027 __ bind(&done);
4028
4029 // Store the value returned in v0.
4030 switch (assign_type) {
4031 case VARIABLE:
4032 if (expr->is_postfix()) {
4033 { EffectContext context(this);
4034 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4035 Token::ASSIGN);
4036 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4037 context.Plug(v0);
4038 }
4039 // For all contexts except EffectConstant we have the result on
4040 // top of the stack.
4041 if (!context()->IsEffect()) {
4042 context()->PlugTOS();
4043 }
4044 } else {
4045 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4046 Token::ASSIGN);
4047 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4048 context()->Plug(v0);
4049 }
4050 break;
4051 case NAMED_PROPERTY: {
4052 __ mov(a0, result_register()); // Value.
4053 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4054 __ pop(a1); // Receiver.
4055 Handle<Code> ic = is_strict_mode()
4056 ? isolate()->builtins()->StoreIC_Initialize_Strict()
4057 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004058 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004059 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4060 if (expr->is_postfix()) {
4061 if (!context()->IsEffect()) {
4062 context()->PlugTOS();
4063 }
4064 } else {
4065 context()->Plug(v0);
4066 }
4067 break;
4068 }
4069 case KEYED_PROPERTY: {
4070 __ mov(a0, result_register()); // Value.
4071 __ pop(a1); // Key.
4072 __ pop(a2); // Receiver.
4073 Handle<Code> ic = is_strict_mode()
4074 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
4075 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004076 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004077 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4078 if (expr->is_postfix()) {
4079 if (!context()->IsEffect()) {
4080 context()->PlugTOS();
4081 }
4082 } else {
4083 context()->Plug(v0);
4084 }
4085 break;
4086 }
4087 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004088}
4089
4090
lrn@chromium.org7516f052011-03-30 08:52:27 +00004091void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004092 ASSERT(!context()->IsEffect());
4093 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004094 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004095 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004096 Comment cmnt(masm_, "Global variable");
4097 __ lw(a0, GlobalObjectOperand());
4098 __ li(a2, Operand(proxy->name()));
4099 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4100 // Use a regular load, not a contextual load, to avoid a reference
4101 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004102 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004103 PrepareForBailout(expr, TOS_REG);
4104 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004105 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004106 Label done, slow;
4107
4108 // Generate code for loading from variables potentially shadowed
4109 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004110 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004111
4112 __ bind(&slow);
4113 __ li(a0, Operand(proxy->name()));
4114 __ Push(cp, a0);
4115 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4116 PrepareForBailout(expr, TOS_REG);
4117 __ bind(&done);
4118
4119 context()->Plug(v0);
4120 } else {
4121 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004122 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004123 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004124}
4125
ager@chromium.org04921a82011-06-27 13:21:41 +00004126void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004127 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004128 Handle<String> check) {
4129 Label materialize_true, materialize_false;
4130 Label* if_true = NULL;
4131 Label* if_false = NULL;
4132 Label* fall_through = NULL;
4133 context()->PrepareTest(&materialize_true, &materialize_false,
4134 &if_true, &if_false, &fall_through);
4135
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004136 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004137 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004138 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004139 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004140
4141 if (check->Equals(isolate()->heap()->number_symbol())) {
4142 __ JumpIfSmi(v0, if_true);
4143 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4144 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4145 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4146 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4147 __ JumpIfSmi(v0, if_false);
4148 // Check for undetectable objects => false.
4149 __ GetObjectType(v0, v0, a1);
4150 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4151 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4152 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4153 Split(eq, a1, Operand(zero_reg),
4154 if_true, if_false, fall_through);
4155 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4156 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4157 __ Branch(if_true, eq, v0, Operand(at));
4158 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4159 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004160 } else if (FLAG_harmony_typeof &&
4161 check->Equals(isolate()->heap()->null_symbol())) {
4162 __ LoadRoot(at, Heap::kNullValueRootIndex);
4163 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004164 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4165 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4166 __ Branch(if_true, eq, v0, Operand(at));
4167 __ JumpIfSmi(v0, if_false);
4168 // Check for undetectable objects => true.
4169 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4170 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4171 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4172 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4173 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4174 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004175 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4176 __ GetObjectType(v0, v0, a1);
4177 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4178 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4179 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004180 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4181 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004182 if (!FLAG_harmony_typeof) {
4183 __ LoadRoot(at, Heap::kNullValueRootIndex);
4184 __ Branch(if_true, eq, v0, Operand(at));
4185 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004186 // Check for JS objects => true.
4187 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004188 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004189 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004190 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004191 // Check for undetectable objects => false.
4192 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4193 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4194 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4195 } else {
4196 if (if_false != fall_through) __ jmp(if_false);
4197 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004198 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004199}
4200
4201
ager@chromium.org5c838252010-02-19 08:53:10 +00004202void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004203 Comment cmnt(masm_, "[ CompareOperation");
4204 SetSourcePosition(expr->position());
4205
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004206 // First we try a fast inlined version of the compare when one of
4207 // the operands is a literal.
4208 if (TryLiteralCompare(expr)) return;
4209
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004210 // Always perform the comparison for its control flow. Pack the result
4211 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004212 Label materialize_true, materialize_false;
4213 Label* if_true = NULL;
4214 Label* if_false = NULL;
4215 Label* fall_through = NULL;
4216 context()->PrepareTest(&materialize_true, &materialize_false,
4217 &if_true, &if_false, &fall_through);
4218
ager@chromium.org04921a82011-06-27 13:21:41 +00004219 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004220 VisitForStackValue(expr->left());
4221 switch (op) {
4222 case Token::IN:
4223 VisitForStackValue(expr->right());
4224 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004225 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004226 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4227 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4228 break;
4229
4230 case Token::INSTANCEOF: {
4231 VisitForStackValue(expr->right());
4232 InstanceofStub stub(InstanceofStub::kNoFlags);
4233 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004234 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004235 // The stub returns 0 for true.
4236 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4237 break;
4238 }
4239
4240 default: {
4241 VisitForAccumulatorValue(expr->right());
4242 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004243 switch (op) {
4244 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004245 case Token::EQ:
4246 cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004247 break;
4248 case Token::LT:
4249 cc = lt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004250 break;
4251 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004252 cc = gt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004253 break;
4254 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004255 cc = le;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004256 break;
4257 case Token::GTE:
4258 cc = ge;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004259 break;
4260 case Token::IN:
4261 case Token::INSTANCEOF:
4262 default:
4263 UNREACHABLE();
4264 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004265 __ mov(a0, result_register());
4266 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004267
4268 bool inline_smi_code = ShouldInlineSmiCase(op);
4269 JumpPatchSite patch_site(masm_);
4270 if (inline_smi_code) {
4271 Label slow_case;
4272 __ Or(a2, a0, Operand(a1));
4273 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4274 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4275 __ bind(&slow_case);
4276 }
4277 // Record position and call the compare IC.
4278 SetSourcePosition(expr->position());
4279 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004280 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004281 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004282 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004283 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4284 }
4285 }
4286
4287 // Convert the result of the comparison into one expected for this
4288 // expression's context.
4289 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004290}
4291
4292
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004293void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4294 Expression* sub_expr,
4295 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004296 Label materialize_true, materialize_false;
4297 Label* if_true = NULL;
4298 Label* if_false = NULL;
4299 Label* fall_through = NULL;
4300 context()->PrepareTest(&materialize_true, &materialize_false,
4301 &if_true, &if_false, &fall_through);
4302
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004303 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004304 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004305 Heap::RootListIndex nil_value = nil == kNullValue ?
4306 Heap::kNullValueRootIndex :
4307 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004308 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004309 __ LoadRoot(a1, nil_value);
4310 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004311 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4312 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004313 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4314 Heap::kUndefinedValueRootIndex :
4315 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004316 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004317 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004318 __ Branch(if_true, eq, a0, Operand(a1));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004319 __ JumpIfSmi(a0, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004320 // It can be an undetectable object.
4321 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4322 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4323 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4324 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4325 }
4326 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004327}
4328
4329
ager@chromium.org5c838252010-02-19 08:53:10 +00004330void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004331 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4332 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004333}
4334
4335
lrn@chromium.org7516f052011-03-30 08:52:27 +00004336Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004337 return v0;
4338}
ager@chromium.org5c838252010-02-19 08:53:10 +00004339
4340
lrn@chromium.org7516f052011-03-30 08:52:27 +00004341Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004342 return cp;
4343}
4344
4345
ager@chromium.org5c838252010-02-19 08:53:10 +00004346void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004347 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4348 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004349}
4350
4351
4352void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004353 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004354}
4355
4356
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004357void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4358 Scope* declaration_scope = scope()->DeclarationScope();
4359 if (declaration_scope->is_global_scope()) {
4360 // Contexts nested in the global context have a canonical empty function
4361 // as their closure, not the anonymous closure containing the global
4362 // code. Pass a smi sentinel and let the runtime look up the empty
4363 // function.
4364 __ li(at, Operand(Smi::FromInt(0)));
4365 } else if (declaration_scope->is_eval_scope()) {
4366 // Contexts created by a call to eval have the same closure as the
4367 // context calling eval, not the anonymous closure containing the eval
4368 // code. Fetch it from the context.
4369 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4370 } else {
4371 ASSERT(declaration_scope->is_function_scope());
4372 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4373 }
4374 __ push(at);
4375}
4376
4377
ager@chromium.org5c838252010-02-19 08:53:10 +00004378// ----------------------------------------------------------------------------
4379// Non-local control flow support.
4380
4381void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004382 ASSERT(!result_register().is(a1));
4383 // Store result register while executing finally block.
4384 __ push(result_register());
4385 // Cook return address in link register to stack (smi encoded Code* delta).
4386 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4387 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004388 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004389 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4390 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004391}
4392
4393
4394void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004395 ASSERT(!result_register().is(a1));
4396 // Restore result register from stack.
4397 __ pop(a1);
4398 // Uncook return address and return.
4399 __ pop(result_register());
4400 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4401 __ sra(a1, a1, 1); // Un-smi-tag value.
4402 __ Addu(at, a1, Operand(masm_->CodeObject()));
4403 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004404}
4405
4406
4407#undef __
4408
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004409#define __ ACCESS_MASM(masm())
4410
4411FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4412 int* stack_depth,
4413 int* context_length) {
4414 // The macros used here must preserve the result register.
4415
4416 // Because the handler block contains the context of the finally
4417 // code, we can restore it directly from there for the finally code
4418 // rather than iteratively unwinding contexts via their previous
4419 // links.
4420 __ Drop(*stack_depth); // Down to the handler block.
4421 if (*context_length > 0) {
4422 // Restore the context to its dedicated register and the stack.
4423 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4424 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4425 }
4426 __ PopTryHandler();
4427 __ Call(finally_entry_);
4428
4429 *stack_depth = 0;
4430 *context_length = 0;
4431 return previous_;
4432}
4433
4434
4435#undef __
4436
ager@chromium.org5c838252010-02-19 08:53:10 +00004437} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004438
4439#endif // V8_TARGET_ARCH_MIPS