blob: 48c176acdba7961a4648bf19c3c64fc9acd0041c [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"
ager@chromium.org5c838252010-02-19 08:53:10 +000050
51namespace v8 {
52namespace internal {
53
54#define __ ACCESS_MASM(masm_)
55
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000056
danno@chromium.org40cb8782011-05-25 07:58:50 +000057static unsigned GetPropertyId(Property* property) {
danno@chromium.org40cb8782011-05-25 07:58:50 +000058 return property->id();
59}
60
61
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000062// A patch site is a location in the code which it is possible to patch. This
63// class has a number of methods to emit the code which is patchable and the
64// method EmitPatchInfo to record a marker back to the patchable code. This
65// marker is a andi at, rx, #yyy instruction, and x * 0x0000ffff + yyy (raw 16
66// bit immediate value is used) is the delta from the pc to the first
67// instruction of the patchable code.
68class JumpPatchSite BASE_EMBEDDED {
69 public:
70 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
71#ifdef DEBUG
72 info_emitted_ = false;
73#endif
74 }
75
76 ~JumpPatchSite() {
77 ASSERT(patch_site_.is_bound() == info_emitted_);
78 }
79
80 // When initially emitting this ensure that a jump is always generated to skip
81 // the inlined smi code.
82 void EmitJumpIfNotSmi(Register reg, Label* target) {
83 ASSERT(!patch_site_.is_bound() && !info_emitted_);
84 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
85 __ bind(&patch_site_);
86 __ andi(at, reg, 0);
87 // Always taken before patched.
88 __ Branch(target, eq, at, Operand(zero_reg));
89 }
90
91 // When initially emitting this ensure that a jump is never generated to skip
92 // the inlined smi code.
93 void EmitJumpIfSmi(Register reg, Label* target) {
94 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
95 ASSERT(!patch_site_.is_bound() && !info_emitted_);
96 __ bind(&patch_site_);
97 __ andi(at, reg, 0);
98 // Never taken before patched.
99 __ Branch(target, ne, at, Operand(zero_reg));
100 }
101
102 void EmitPatchInfo() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000103 if (patch_site_.is_bound()) {
104 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
105 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
106 __ andi(at, reg, delta_to_patch_site % kImm16Mask);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000107#ifdef DEBUG
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000108 info_emitted_ = true;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000109#endif
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000110 } else {
111 __ nop(); // Signals no inlined code.
112 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000113 }
114
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000115 private:
116 MacroAssembler* masm_;
117 Label patch_site_;
118#ifdef DEBUG
119 bool info_emitted_;
120#endif
121};
122
123
lrn@chromium.org7516f052011-03-30 08:52:27 +0000124// Generate code for a JS function. On entry to the function the receiver
125// and arguments have been pushed on the stack left to right. The actual
126// argument count matches the formal parameter count expected by the
127// function.
128//
129// The live registers are:
130// o a1: the JS function object being called (ie, ourselves)
131// o cp: our context
132// o fp: our caller's frame pointer
133// o sp: stack pointer
134// o ra: return address
135//
136// The function builds a JS frame. Please see JavaScriptFrameConstants in
137// frames-mips.h for its layout.
138void FullCodeGenerator::Generate(CompilationInfo* info) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000139 ASSERT(info_ == NULL);
140 info_ = info;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000141 scope_ = info->scope();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000142 SetFunctionPosition(function());
143 Comment cmnt(masm_, "[ function compiled by full code generator");
144
145#ifdef DEBUG
146 if (strlen(FLAG_stop_at) > 0 &&
147 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
148 __ stop("stop-at");
149 }
150#endif
151
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000152 // Strict mode functions and builtins need to replace the receiver
153 // with undefined when called as functions (without an explicit
154 // receiver object). t1 is zero for method calls and non-zero for
155 // function calls.
156 if (info->is_strict_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000157 Label ok;
158 __ Branch(&ok, eq, t1, Operand(zero_reg));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000159 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000160 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
161 __ sw(a2, MemOperand(sp, receiver_offset));
162 __ bind(&ok);
163 }
164
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000165 int locals_count = info->scope()->num_stack_slots();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000166
167 __ Push(ra, fp, cp, a1);
168 if (locals_count > 0) {
169 // Load undefined value here, so the value is ready for the loop
170 // below.
171 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
172 }
173 // Adjust fp to point to caller's fp.
174 __ Addu(fp, sp, Operand(2 * kPointerSize));
175
176 { Comment cmnt(masm_, "[ Allocate locals");
177 for (int i = 0; i < locals_count; i++) {
178 __ push(at);
179 }
180 }
181
182 bool function_in_register = true;
183
184 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000185 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000186 if (heap_slots > 0) {
187 Comment cmnt(masm_, "[ Allocate local context");
188 // Argument to NewContext is the function, which is in a1.
189 __ push(a1);
190 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
191 FastNewContextStub stub(heap_slots);
192 __ CallStub(&stub);
193 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000194 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000195 }
196 function_in_register = false;
197 // Context is returned in both v0 and cp. It replaces the context
198 // passed to us. It's saved in the stack and kept live in cp.
199 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
200 // Copy any necessary parameters into the context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000201 int num_parameters = info->scope()->num_parameters();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000202 for (int i = 0; i < num_parameters; i++) {
203 Slot* slot = scope()->parameter(i)->AsSlot();
204 if (slot != NULL && slot->type() == Slot::CONTEXT) {
205 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
206 (num_parameters - 1 - i) * kPointerSize;
207 // Load parameter from stack.
208 __ lw(a0, MemOperand(fp, parameter_offset));
209 // Store it in the context.
210 __ li(a1, Operand(Context::SlotOffset(slot->index())));
211 __ addu(a2, cp, a1);
212 __ sw(a0, MemOperand(a2, 0));
213 // Update the write barrier. This clobbers all involved
214 // registers, so we have to use two more registers to avoid
215 // clobbering cp.
216 __ mov(a2, cp);
217 __ RecordWrite(a2, a1, a3);
218 }
219 }
220 }
221
222 Variable* arguments = scope()->arguments();
223 if (arguments != NULL) {
224 // Function uses arguments object.
225 Comment cmnt(masm_, "[ Allocate arguments object");
226 if (!function_in_register) {
227 // Load this again, if it's used by the local context below.
228 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
229 } else {
230 __ mov(a3, a1);
231 }
232 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000233 int num_parameters = info->scope()->num_parameters();
234 int offset = num_parameters * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000235 __ Addu(a2, fp,
236 Operand(StandardFrameConstants::kCallerSPOffset + offset));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000237 __ li(a1, Operand(Smi::FromInt(num_parameters)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000238 __ Push(a3, a2, a1);
239
240 // Arguments to ArgumentsAccessStub:
241 // function, receiver address, parameter count.
242 // The stub will rewrite receiever and parameter count if the previous
243 // stack frame was an arguments adapter frame.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000244 ArgumentsAccessStub::Type type;
245 if (is_strict_mode()) {
246 type = ArgumentsAccessStub::NEW_STRICT;
247 } else if (function()->has_duplicate_parameters()) {
248 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
249 } else {
250 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
251 }
252 ArgumentsAccessStub stub(type);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000253 __ CallStub(&stub);
254
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000255 Move(arguments->AsSlot(), v0, a1, a2);
256 }
257
258 if (FLAG_trace) {
259 __ CallRuntime(Runtime::kTraceEnter, 0);
260 }
261
262 // Visit the declarations and body unless there is an illegal
263 // redeclaration.
264 if (scope()->HasIllegalRedeclaration()) {
265 Comment cmnt(masm_, "[ Declarations");
266 scope()->VisitIllegalRedeclaration(this);
267
268 } else {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000269 PrepareForBailoutForId(AstNode::kFunctionEntryId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000270 { Comment cmnt(masm_, "[ Declarations");
271 // For named function expressions, declare the function name as a
272 // constant.
273 if (scope()->is_function_scope() && scope()->function() != NULL) {
274 EmitDeclaration(scope()->function(), Variable::CONST, NULL);
275 }
276 VisitDeclarations(scope()->declarations());
277 }
278
279 { Comment cmnt(masm_, "[ Stack check");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000280 PrepareForBailoutForId(AstNode::kDeclarationsId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000281 Label ok;
282 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
283 __ Branch(&ok, hs, sp, Operand(t0));
284 StackCheckStub stub;
285 __ CallStub(&stub);
286 __ bind(&ok);
287 }
288
289 { Comment cmnt(masm_, "[ Body");
290 ASSERT(loop_depth() == 0);
291 VisitStatements(function()->body());
292 ASSERT(loop_depth() == 0);
293 }
294 }
295
296 // Always emit a 'return undefined' in case control fell off the end of
297 // the body.
298 { Comment cmnt(masm_, "[ return <undefined>;");
299 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
300 }
301 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000302}
303
304
305void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000306 ASSERT(Smi::FromInt(0) == 0);
307 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000308}
309
310
311void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000312 Comment cmnt(masm_, "[ Stack check");
313 Label ok;
314 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
315 __ Branch(&ok, hs, sp, Operand(t0));
316 StackCheckStub stub;
317 // Record a mapping of this PC offset to the OSR id. This is used to find
318 // the AST id from the unoptimized code in order to use it as a key into
319 // the deoptimization input data found in the optimized code.
320 RecordStackCheck(stmt->OsrEntryId());
321
322 __ CallStub(&stub);
323 __ bind(&ok);
324 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
325 // Record a mapping of the OSR id to this PC. This is used if the OSR
326 // entry becomes the target of a bailout. We don't expect it to be, but
327 // we want it to work if it is.
328 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000329}
330
331
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000332void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000333 Comment cmnt(masm_, "[ Return sequence");
334 if (return_label_.is_bound()) {
335 __ Branch(&return_label_);
336 } else {
337 __ bind(&return_label_);
338 if (FLAG_trace) {
339 // Push the return value on the stack as the parameter.
340 // Runtime::TraceExit returns its parameter in v0.
341 __ push(v0);
342 __ CallRuntime(Runtime::kTraceExit, 1);
343 }
344
345#ifdef DEBUG
346 // Add a label for checking the size of the code used for returning.
347 Label check_exit_codesize;
348 masm_->bind(&check_exit_codesize);
349#endif
350 // Make sure that the constant pool is not emitted inside of the return
351 // sequence.
352 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
353 // Here we use masm_-> instead of the __ macro to avoid the code coverage
354 // tool from instrumenting as we rely on the code size here.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000355 int32_t sp_delta = (info_->scope()->num_parameters() + 1) * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000356 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
357 __ RecordJSReturn();
358 masm_->mov(sp, fp);
359 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
360 masm_->Addu(sp, sp, Operand(sp_delta));
361 masm_->Jump(ra);
362 }
363
364#ifdef DEBUG
365 // Check that the size of the code used for returning is large enough
366 // for the debugger's requirements.
367 ASSERT(Assembler::kJSReturnSequenceInstructions <=
368 masm_->InstructionsGeneratedSince(&check_exit_codesize));
369#endif
370 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000371}
372
373
lrn@chromium.org7516f052011-03-30 08:52:27 +0000374void FullCodeGenerator::EffectContext::Plug(Slot* slot) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000375}
376
377
lrn@chromium.org7516f052011-03-30 08:52:27 +0000378void FullCodeGenerator::AccumulatorValueContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000379 codegen()->Move(result_register(), slot);
ager@chromium.org5c838252010-02-19 08:53:10 +0000380}
381
382
lrn@chromium.org7516f052011-03-30 08:52:27 +0000383void FullCodeGenerator::StackValueContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000384 codegen()->Move(result_register(), slot);
385 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000386}
387
388
lrn@chromium.org7516f052011-03-30 08:52:27 +0000389void FullCodeGenerator::TestContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000390 // For simplicity we always test the accumulator register.
391 codegen()->Move(result_register(), slot);
392 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000393 codegen()->DoTest(this);
ager@chromium.org5c838252010-02-19 08:53:10 +0000394}
395
396
lrn@chromium.org7516f052011-03-30 08:52:27 +0000397void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000398}
399
400
lrn@chromium.org7516f052011-03-30 08:52:27 +0000401void FullCodeGenerator::AccumulatorValueContext::Plug(
402 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000403 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000404}
405
406
407void FullCodeGenerator::StackValueContext::Plug(
408 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000409 __ LoadRoot(result_register(), index);
410 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000411}
412
413
414void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000415 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
416 true,
417 true_label_,
418 false_label_);
419 if (index == Heap::kUndefinedValueRootIndex ||
420 index == Heap::kNullValueRootIndex ||
421 index == Heap::kFalseValueRootIndex) {
422 if (false_label_ != fall_through_) __ Branch(false_label_);
423 } else if (index == Heap::kTrueValueRootIndex) {
424 if (true_label_ != fall_through_) __ Branch(true_label_);
425 } else {
426 __ LoadRoot(result_register(), index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000427 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000428 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000429}
430
431
432void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000433}
434
435
436void FullCodeGenerator::AccumulatorValueContext::Plug(
437 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000438 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000439}
440
441
442void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000443 // Immediates cannot be pushed directly.
444 __ li(result_register(), Operand(lit));
445 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000446}
447
448
449void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000450 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
451 true,
452 true_label_,
453 false_label_);
454 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
455 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
456 if (false_label_ != fall_through_) __ Branch(false_label_);
457 } else if (lit->IsTrue() || lit->IsJSObject()) {
458 if (true_label_ != fall_through_) __ Branch(true_label_);
459 } else if (lit->IsString()) {
460 if (String::cast(*lit)->length() == 0) {
461 if (false_label_ != fall_through_) __ Branch(false_label_);
462 } else {
463 if (true_label_ != fall_through_) __ Branch(true_label_);
464 }
465 } else if (lit->IsSmi()) {
466 if (Smi::cast(*lit)->value() == 0) {
467 if (false_label_ != fall_through_) __ Branch(false_label_);
468 } else {
469 if (true_label_ != fall_through_) __ Branch(true_label_);
470 }
471 } else {
472 // For simplicity we always test the accumulator register.
473 __ li(result_register(), Operand(lit));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000474 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000475 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000476}
477
478
479void FullCodeGenerator::EffectContext::DropAndPlug(int count,
480 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000481 ASSERT(count > 0);
482 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000483}
484
485
486void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
487 int count,
488 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000489 ASSERT(count > 0);
490 __ Drop(count);
491 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000492}
493
494
495void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
496 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000497 ASSERT(count > 0);
498 if (count > 1) __ Drop(count - 1);
499 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000500}
501
502
503void FullCodeGenerator::TestContext::DropAndPlug(int count,
504 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000505 ASSERT(count > 0);
506 // For simplicity we always test the accumulator register.
507 __ Drop(count);
508 __ Move(result_register(), reg);
509 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000510 codegen()->DoTest(this);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000511}
512
513
514void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
515 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000516 ASSERT(materialize_true == materialize_false);
517 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000518}
519
520
521void FullCodeGenerator::AccumulatorValueContext::Plug(
522 Label* materialize_true,
523 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000524 Label done;
525 __ bind(materialize_true);
526 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
527 __ Branch(&done);
528 __ bind(materialize_false);
529 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
530 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000531}
532
533
534void FullCodeGenerator::StackValueContext::Plug(
535 Label* materialize_true,
536 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000537 Label done;
538 __ bind(materialize_true);
539 __ LoadRoot(at, Heap::kTrueValueRootIndex);
540 __ push(at);
541 __ Branch(&done);
542 __ bind(materialize_false);
543 __ LoadRoot(at, Heap::kFalseValueRootIndex);
544 __ push(at);
545 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000546}
547
548
549void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
550 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000551 ASSERT(materialize_true == true_label_);
552 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000553}
554
555
556void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000557}
558
559
560void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000561 Heap::RootListIndex value_root_index =
562 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
563 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000564}
565
566
567void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000568 Heap::RootListIndex value_root_index =
569 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
570 __ LoadRoot(at, value_root_index);
571 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000572}
573
574
575void FullCodeGenerator::TestContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000576 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
577 true,
578 true_label_,
579 false_label_);
580 if (flag) {
581 if (true_label_ != fall_through_) __ Branch(true_label_);
582 } else {
583 if (false_label_ != fall_through_) __ Branch(false_label_);
584 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000585}
586
587
whesse@chromium.org7b260152011-06-20 15:33:18 +0000588void FullCodeGenerator::DoTest(Expression* condition,
589 Label* if_true,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000590 Label* if_false,
591 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000592 if (CpuFeatures::IsSupported(FPU)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000593 ToBooleanStub stub(result_register());
594 __ CallStub(&stub);
595 __ mov(at, zero_reg);
596 } else {
597 // Call the runtime to find the boolean value of the source and then
598 // translate it into control flow to the pair of labels.
599 __ push(result_register());
600 __ CallRuntime(Runtime::kToBool, 1);
601 __ LoadRoot(at, Heap::kFalseValueRootIndex);
602 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000603 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000604}
605
606
lrn@chromium.org7516f052011-03-30 08:52:27 +0000607void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000608 Register lhs,
609 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000610 Label* if_true,
611 Label* if_false,
612 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000613 if (if_false == fall_through) {
614 __ Branch(if_true, cc, lhs, rhs);
615 } else if (if_true == fall_through) {
616 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
617 } else {
618 __ Branch(if_true, cc, lhs, rhs);
619 __ Branch(if_false);
620 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000621}
622
623
624MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000625 switch (slot->type()) {
626 case Slot::PARAMETER:
627 case Slot::LOCAL:
628 return MemOperand(fp, SlotOffset(slot));
629 case Slot::CONTEXT: {
630 int context_chain_length =
631 scope()->ContextChainLength(slot->var()->scope());
632 __ LoadContext(scratch, context_chain_length);
633 return ContextOperand(scratch, slot->index());
634 }
635 case Slot::LOOKUP:
636 UNREACHABLE();
637 }
638 UNREACHABLE();
639 return MemOperand(v0, 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000640}
641
642
643void FullCodeGenerator::Move(Register destination, Slot* source) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000644 // Use destination as scratch.
645 MemOperand slot_operand = EmitSlotSearch(source, destination);
646 __ lw(destination, slot_operand);
ager@chromium.org5c838252010-02-19 08:53:10 +0000647}
648
649
lrn@chromium.org7516f052011-03-30 08:52:27 +0000650void FullCodeGenerator::PrepareForBailoutBeforeSplit(State state,
651 bool should_normalize,
652 Label* if_true,
653 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000654 // Only prepare for bailouts before splits if we're in a test
655 // context. Otherwise, we let the Visit function deal with the
656 // preparation to avoid preparing with the same AST id twice.
657 if (!context()->IsTest() || !info_->IsOptimizable()) return;
658
659 Label skip;
660 if (should_normalize) __ Branch(&skip);
661
662 ForwardBailoutStack* current = forward_bailout_stack_;
663 while (current != NULL) {
664 PrepareForBailout(current->expr(), state);
665 current = current->parent();
666 }
667
668 if (should_normalize) {
669 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
670 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
671 __ bind(&skip);
672 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000673}
674
675
ager@chromium.org5c838252010-02-19 08:53:10 +0000676void FullCodeGenerator::Move(Slot* dst,
677 Register src,
678 Register scratch1,
679 Register scratch2) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000680 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
681 ASSERT(!scratch1.is(src) && !scratch2.is(src));
682 MemOperand location = EmitSlotSearch(dst, scratch1);
683 __ sw(src, location);
684 // Emit the write barrier code if the location is in the heap.
685 if (dst->type() == Slot::CONTEXT) {
686 __ RecordWrite(scratch1,
687 Operand(Context::SlotOffset(dst->index())),
688 scratch2,
689 src);
690 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000691}
692
693
lrn@chromium.org7516f052011-03-30 08:52:27 +0000694void FullCodeGenerator::EmitDeclaration(Variable* variable,
695 Variable::Mode mode,
696 FunctionLiteral* function) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000697 Comment cmnt(masm_, "[ Declaration");
698 ASSERT(variable != NULL); // Must have been resolved.
699 Slot* slot = variable->AsSlot();
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000700 ASSERT(slot != NULL);
701 switch (slot->type()) {
702 case Slot::PARAMETER:
703 case Slot::LOCAL:
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000704 if (function != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000705 VisitForAccumulatorValue(function);
706 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000707 } else if (mode == Variable::CONST || mode == Variable::LET) {
708 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
709 __ sw(t0, MemOperand(fp, SlotOffset(slot)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000710 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000711 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000712
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000713 case Slot::CONTEXT:
714 // We bypass the general EmitSlotSearch because we know more about
715 // this specific context.
716
717 // The variable in the decl always resides in the current function
718 // context.
719 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
720 if (FLAG_debug_code) {
721 // Check that we're not inside a with or catch context.
722 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
723 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
724 __ Check(ne, "Declaration in with context.",
725 a1, Operand(t0));
726 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
727 __ Check(ne, "Declaration in catch context.",
728 a1, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000729 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000730 if (function != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000731 VisitForAccumulatorValue(function);
732 __ sw(result_register(), ContextOperand(cp, slot->index()));
733 int offset = Context::SlotOffset(slot->index());
734 // We know that we have written a function, which is not a smi.
735 __ mov(a1, cp);
736 __ RecordWrite(a1, Operand(offset), a2, result_register());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000737 } else if (mode == Variable::CONST || mode == Variable::LET) {
738 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
739 __ sw(at, ContextOperand(cp, slot->index()));
740 // No write barrier since the_hole_value is in old space.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000741 }
742 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000743
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000744 case Slot::LOOKUP: {
745 __ li(a2, Operand(variable->name()));
746 // Declaration nodes are always introduced in one of two modes.
747 ASSERT(mode == Variable::VAR ||
748 mode == Variable::CONST ||
749 mode == Variable::LET);
750 PropertyAttributes attr = (mode == Variable::CONST) ? READ_ONLY : NONE;
751 __ li(a1, Operand(Smi::FromInt(attr)));
752 // Push initial value, if any.
753 // Note: For variables we must not push an initial value (such as
754 // 'undefined') because we may have a (legal) redeclaration and we
755 // must not destroy the current value.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000756 if (function != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000757 __ Push(cp, a2, a1);
758 // Push initial value for function declaration.
759 VisitForStackValue(function);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000760 } else if (mode == Variable::CONST || mode == Variable::LET) {
761 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
762 __ Push(cp, a2, a1, a0);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000763 } else {
764 ASSERT(Smi::FromInt(0) == 0);
765 // No initial value!
766 __ mov(a0, zero_reg); // Operand(Smi::FromInt(0)));
767 __ Push(cp, a2, a1, a0);
768 }
769 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
770 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000771 }
772 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000773}
774
775
ager@chromium.org5c838252010-02-19 08:53:10 +0000776void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000777 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
ager@chromium.org5c838252010-02-19 08:53:10 +0000778}
779
780
781void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000782 // Call the runtime to declare the globals.
783 // The context is the first argument.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000784 __ li(a1, Operand(pairs));
785 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
786 __ Push(cp, a1, a0);
787 __ CallRuntime(Runtime::kDeclareGlobals, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000788 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000789}
790
791
lrn@chromium.org7516f052011-03-30 08:52:27 +0000792void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000793 Comment cmnt(masm_, "[ SwitchStatement");
794 Breakable nested_statement(this, stmt);
795 SetStatementPosition(stmt);
796
797 // Keep the switch value on the stack until a case matches.
798 VisitForStackValue(stmt->tag());
799 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
800
801 ZoneList<CaseClause*>* clauses = stmt->cases();
802 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
803
804 Label next_test; // Recycled for each test.
805 // Compile all the tests with branches to their bodies.
806 for (int i = 0; i < clauses->length(); i++) {
807 CaseClause* clause = clauses->at(i);
808 clause->body_target()->Unuse();
809
810 // The default is not a test, but remember it as final fall through.
811 if (clause->is_default()) {
812 default_clause = clause;
813 continue;
814 }
815
816 Comment cmnt(masm_, "[ Case comparison");
817 __ bind(&next_test);
818 next_test.Unuse();
819
820 // Compile the label expression.
821 VisitForAccumulatorValue(clause->label());
822 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
823
824 // Perform the comparison as if via '==='.
825 __ lw(a1, MemOperand(sp, 0)); // Switch value.
826 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
827 JumpPatchSite patch_site(masm_);
828 if (inline_smi_code) {
829 Label slow_case;
830 __ or_(a2, a1, a0);
831 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
832
833 __ Branch(&next_test, ne, a1, Operand(a0));
834 __ Drop(1); // Switch value is no longer needed.
835 __ Branch(clause->body_target());
836
837 __ bind(&slow_case);
838 }
839
840 // Record position before stub call for type feedback.
841 SetSourcePosition(clause->position());
842 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000843 __ Call(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000844 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +0000845
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000846 __ Branch(&next_test, ne, v0, Operand(zero_reg));
847 __ Drop(1); // Switch value is no longer needed.
848 __ Branch(clause->body_target());
849 }
850
851 // Discard the test value and jump to the default if present, otherwise to
852 // the end of the statement.
853 __ bind(&next_test);
854 __ Drop(1); // Switch value is no longer needed.
855 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000856 __ Branch(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000857 } else {
858 __ Branch(default_clause->body_target());
859 }
860
861 // Compile all the case bodies.
862 for (int i = 0; i < clauses->length(); i++) {
863 Comment cmnt(masm_, "[ Case body");
864 CaseClause* clause = clauses->at(i);
865 __ bind(clause->body_target());
866 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
867 VisitStatements(clause->statements());
868 }
869
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000870 __ bind(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000871 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000872}
873
874
875void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000876 Comment cmnt(masm_, "[ ForInStatement");
877 SetStatementPosition(stmt);
878
879 Label loop, exit;
880 ForIn loop_statement(this, stmt);
881 increment_loop_depth();
882
883 // Get the object to enumerate over. Both SpiderMonkey and JSC
884 // ignore null and undefined in contrast to the specification; see
885 // ECMA-262 section 12.6.4.
886 VisitForAccumulatorValue(stmt->enumerable());
887 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
888 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
889 __ Branch(&exit, eq, a0, Operand(at));
890 Register null_value = t1;
891 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
892 __ Branch(&exit, eq, a0, Operand(null_value));
893
894 // Convert the object to a JS object.
895 Label convert, done_convert;
896 __ JumpIfSmi(a0, &convert);
897 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000898 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000899 __ bind(&convert);
900 __ push(a0);
901 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
902 __ mov(a0, v0);
903 __ bind(&done_convert);
904 __ push(a0);
905
906 // Check cache validity in generated code. This is a fast case for
907 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
908 // guarantee cache validity, call the runtime system to check cache
909 // validity or get the property names in a fixed array.
910 Label next, call_runtime;
911 // Preload a couple of values used in the loop.
912 Register empty_fixed_array_value = t2;
913 __ LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
914 Register empty_descriptor_array_value = t3;
915 __ LoadRoot(empty_descriptor_array_value,
916 Heap::kEmptyDescriptorArrayRootIndex);
917 __ mov(a1, a0);
918 __ bind(&next);
919
920 // Check that there are no elements. Register a1 contains the
921 // current JS object we've reached through the prototype chain.
922 __ lw(a2, FieldMemOperand(a1, JSObject::kElementsOffset));
923 __ Branch(&call_runtime, ne, a2, Operand(empty_fixed_array_value));
924
925 // Check that instance descriptors are not empty so that we can
926 // check for an enum cache. Leave the map in a2 for the subsequent
927 // prototype load.
928 __ lw(a2, FieldMemOperand(a1, HeapObject::kMapOffset));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000929 __ lw(a3, FieldMemOperand(a2, Map::kInstanceDescriptorsOrBitField3Offset));
930 __ JumpIfSmi(a3, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000931
932 // Check that there is an enum cache in the non-empty instance
933 // descriptors (a3). This is the case if the next enumeration
934 // index field does not contain a smi.
935 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumerationIndexOffset));
936 __ JumpIfSmi(a3, &call_runtime);
937
938 // For all objects but the receiver, check that the cache is empty.
939 Label check_prototype;
940 __ Branch(&check_prototype, eq, a1, Operand(a0));
941 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumCacheBridgeCacheOffset));
942 __ Branch(&call_runtime, ne, a3, Operand(empty_fixed_array_value));
943
944 // Load the prototype from the map and loop if non-null.
945 __ bind(&check_prototype);
946 __ lw(a1, FieldMemOperand(a2, Map::kPrototypeOffset));
947 __ Branch(&next, ne, a1, Operand(null_value));
948
949 // The enum cache is valid. Load the map of the object being
950 // iterated over and use the cache for the iteration.
951 Label use_cache;
952 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
953 __ Branch(&use_cache);
954
955 // Get the set of properties to enumerate.
956 __ bind(&call_runtime);
957 __ push(a0); // Duplicate the enumerable object on the stack.
958 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
959
960 // If we got a map from the runtime call, we can do a fast
961 // modification check. Otherwise, we got a fixed array, and we have
962 // to do a slow check.
963 Label fixed_array;
964 __ mov(a2, v0);
965 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
966 __ LoadRoot(at, Heap::kMetaMapRootIndex);
967 __ Branch(&fixed_array, ne, a1, Operand(at));
968
969 // We got a map in register v0. Get the enumeration cache from it.
970 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000971 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000972 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
973 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
974
975 // Setup the four remaining stack slots.
976 __ push(v0); // Map.
977 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
978 __ li(a0, Operand(Smi::FromInt(0)));
979 // Push enumeration cache, enumeration cache length (as smi) and zero.
980 __ Push(a2, a1, a0);
981 __ jmp(&loop);
982
983 // We got a fixed array in register v0. Iterate through that.
984 __ bind(&fixed_array);
985 __ li(a1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
986 __ Push(a1, v0);
987 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
988 __ li(a0, Operand(Smi::FromInt(0)));
989 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
990
991 // Generate code for doing the condition check.
992 __ bind(&loop);
993 // Load the current count to a0, load the length to a1.
994 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
995 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000996 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000997
998 // Get the current entry of the array into register a3.
999 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1000 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1001 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1002 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1003 __ lw(a3, MemOperand(t0)); // Current entry.
1004
1005 // Get the expected map from the stack or a zero map in the
1006 // permanent slow case into register a2.
1007 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1008
1009 // Check if the expected map still matches that of the enumerable.
1010 // If not, we have to filter the key.
1011 Label update_each;
1012 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1013 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1014 __ Branch(&update_each, eq, t0, Operand(a2));
1015
1016 // Convert the entry to a string or (smi) 0 if it isn't a property
1017 // any more. If the property has been removed while iterating, we
1018 // just skip it.
1019 __ push(a1); // Enumerable.
1020 __ push(a3); // Current entry.
1021 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1022 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001023 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001024
1025 // Update the 'each' property or variable from the possibly filtered
1026 // entry in register a3.
1027 __ bind(&update_each);
1028 __ mov(result_register(), a3);
1029 // Perform the assignment as if via '='.
1030 { EffectContext context(this);
1031 EmitAssignment(stmt->each(), stmt->AssignmentId());
1032 }
1033
1034 // Generate code for the body of the loop.
1035 Visit(stmt->body());
1036
1037 // Generate code for the going to the next element by incrementing
1038 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001039 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001040 __ pop(a0);
1041 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1042 __ push(a0);
1043
1044 EmitStackCheck(stmt);
1045 __ Branch(&loop);
1046
1047 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001048 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001049 __ Drop(5);
1050
1051 // Exit and decrement the loop depth.
1052 __ bind(&exit);
1053 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001054}
1055
1056
1057void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1058 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001059 // Use the fast case closure allocation code that allocates in new
1060 // space for nested functions that don't need literals cloning. If
1061 // we're running with the --always-opt or the --prepare-always-opt
1062 // flag, we need to use the runtime function so that the new function
1063 // we are creating here gets a chance to have its code optimized and
1064 // doesn't just get a copy of the existing unoptimized code.
1065 if (!FLAG_always_opt &&
1066 !FLAG_prepare_always_opt &&
1067 !pretenure &&
1068 scope()->is_function_scope() &&
1069 info->num_literals() == 0) {
1070 FastNewClosureStub stub(info->strict_mode() ? kStrictMode : kNonStrictMode);
1071 __ li(a0, Operand(info));
1072 __ push(a0);
1073 __ CallStub(&stub);
1074 } else {
1075 __ li(a0, Operand(info));
1076 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1077 : Heap::kFalseValueRootIndex);
1078 __ Push(cp, a0, a1);
1079 __ CallRuntime(Runtime::kNewClosure, 3);
1080 }
1081 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001082}
1083
1084
1085void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001086 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001087 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001088}
1089
1090
1091void FullCodeGenerator::EmitLoadGlobalSlotCheckExtensions(
1092 Slot* slot,
1093 TypeofState typeof_state,
1094 Label* slow) {
1095 Register current = cp;
1096 Register next = a1;
1097 Register temp = a2;
1098
1099 Scope* s = scope();
1100 while (s != NULL) {
1101 if (s->num_heap_slots() > 0) {
1102 if (s->calls_eval()) {
1103 // Check that extension is NULL.
1104 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1105 __ Branch(slow, ne, temp, Operand(zero_reg));
1106 }
1107 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001108 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001109 // Walk the rest of the chain without clobbering cp.
1110 current = next;
1111 }
1112 // If no outer scope calls eval, we do not need to check more
1113 // context extensions.
1114 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
1115 s = s->outer_scope();
1116 }
1117
1118 if (s->is_eval_scope()) {
1119 Label loop, fast;
1120 if (!current.is(next)) {
1121 __ Move(next, current);
1122 }
1123 __ bind(&loop);
1124 // Terminate at global context.
1125 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1126 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1127 __ Branch(&fast, eq, temp, Operand(t0));
1128 // Check that extension is NULL.
1129 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1130 __ Branch(slow, ne, temp, Operand(zero_reg));
1131 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001132 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001133 __ Branch(&loop);
1134 __ bind(&fast);
1135 }
1136
1137 __ lw(a0, GlobalObjectOperand());
1138 __ li(a2, Operand(slot->var()->name()));
1139 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1140 ? RelocInfo::CODE_TARGET
1141 : RelocInfo::CODE_TARGET_CONTEXT;
1142 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001143 __ Call(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001144}
1145
1146
lrn@chromium.org7516f052011-03-30 08:52:27 +00001147MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(
1148 Slot* slot,
1149 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001150 ASSERT(slot->type() == Slot::CONTEXT);
1151 Register context = cp;
1152 Register next = a3;
1153 Register temp = t0;
1154
1155 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
1156 if (s->num_heap_slots() > 0) {
1157 if (s->calls_eval()) {
1158 // Check that extension is NULL.
1159 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1160 __ Branch(slow, ne, temp, Operand(zero_reg));
1161 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001162 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001163 // Walk the rest of the chain without clobbering cp.
1164 context = next;
1165 }
1166 }
1167 // Check that last extension is NULL.
1168 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1169 __ Branch(slow, ne, temp, Operand(zero_reg));
1170
1171 // This function is used only for loads, not stores, so it's safe to
1172 // return an cp-based operand (the write barrier cannot be allowed to
1173 // destroy the cp register).
1174 return ContextOperand(context, slot->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001175}
1176
1177
1178void FullCodeGenerator::EmitDynamicLoadFromSlotFastCase(
1179 Slot* slot,
1180 TypeofState typeof_state,
1181 Label* slow,
1182 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001183 // Generate fast-case code for variables that might be shadowed by
1184 // eval-introduced variables. Eval is used a lot without
1185 // introducing variables. In those cases, we do not want to
1186 // perform a runtime call for all variables in the scope
1187 // containing the eval.
1188 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
1189 EmitLoadGlobalSlotCheckExtensions(slot, typeof_state, slow);
1190 __ Branch(done);
1191 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
1192 Slot* potential_slot = slot->var()->local_if_not_shadowed()->AsSlot();
1193 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
1194 if (potential_slot != NULL) {
1195 // Generate fast case for locals that rewrite to slots.
1196 __ lw(v0, ContextSlotOperandCheckExtensions(potential_slot, slow));
1197 if (potential_slot->var()->mode() == Variable::CONST) {
1198 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1199 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1200 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1201 __ movz(v0, a0, at); // Conditional move.
1202 }
1203 __ Branch(done);
1204 } else if (rewrite != NULL) {
1205 // Generate fast case for calls of an argument function.
1206 Property* property = rewrite->AsProperty();
1207 if (property != NULL) {
1208 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
1209 Literal* key_literal = property->key()->AsLiteral();
1210 if (obj_proxy != NULL &&
1211 key_literal != NULL &&
1212 obj_proxy->IsArguments() &&
1213 key_literal->handle()->IsSmi()) {
1214 // Load arguments object if there are no eval-introduced
1215 // variables. Then load the argument from the arguments
1216 // object using keyed load.
1217 __ lw(a1,
1218 ContextSlotOperandCheckExtensions(obj_proxy->var()->AsSlot(),
1219 slow));
1220 __ li(a0, Operand(key_literal->handle()));
1221 Handle<Code> ic =
1222 isolate()->builtins()->KeyedLoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001223 __ Call(ic, RelocInfo::CODE_TARGET, GetPropertyId(property));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001224 __ Branch(done);
1225 }
1226 }
1227 }
1228 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001229}
1230
1231
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001232void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1233 // Record position before possible IC call.
1234 SetSourcePosition(proxy->position());
1235 Variable* var = proxy->var();
1236
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001237 // Three cases: non-this global variables, lookup slots, and all other
1238 // types of slots.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001239 Slot* slot = var->AsSlot();
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001240 ASSERT((var->is_global() && !var->is_this()) == (slot == NULL));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001241
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001242 if (slot == NULL) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001243 Comment cmnt(masm_, "Global variable");
1244 // Use inline caching. Variable name is passed in a2 and the global
1245 // object (receiver) in a0.
1246 __ lw(a0, GlobalObjectOperand());
1247 __ li(a2, Operand(var->name()));
1248 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001249 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001250 context()->Plug(v0);
1251
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001252 } else if (slot->type() == Slot::LOOKUP) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001253 Label done, slow;
1254
1255 // Generate code for loading from variables potentially shadowed
1256 // by eval-introduced variables.
1257 EmitDynamicLoadFromSlotFastCase(slot, NOT_INSIDE_TYPEOF, &slow, &done);
1258
1259 __ bind(&slow);
1260 Comment cmnt(masm_, "Lookup slot");
1261 __ li(a1, Operand(var->name()));
1262 __ Push(cp, a1); // Context and name.
1263 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1264 __ bind(&done);
1265
1266 context()->Plug(v0);
1267
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001268 } else {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001269 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1270 ? "Context slot"
1271 : "Stack slot");
1272 if (var->mode() == Variable::CONST) {
1273 // Constants may be the hole value if they have not been initialized.
1274 // Unhole them.
1275 MemOperand slot_operand = EmitSlotSearch(slot, a0);
1276 __ lw(v0, slot_operand);
1277 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1278 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1279 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1280 __ movz(v0, a0, at); // Conditional move.
1281 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001282 } else if (var->mode() == Variable::LET) {
1283 // Let bindings may be the hole value if they have not been initialized.
1284 // Throw a type error in this case.
1285 Label done;
1286 MemOperand slot_operand = EmitSlotSearch(slot, a0);
1287 __ lw(v0, slot_operand);
1288 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
1289 __ Branch(&done, ne, v0, Operand(a1));
1290 __ li(v0, Operand(var->name()));
1291 __ push(v0);
1292 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1293 __ bind(&done);
1294 context()->Plug(v0);
1295 } else {
1296 context()->Plug(slot);
1297 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001298 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001299}
1300
1301
1302void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001303 Comment cmnt(masm_, "[ RegExpLiteral");
1304 Label materialized;
1305 // Registers will be used as follows:
1306 // t1 = materialized value (RegExp literal)
1307 // t0 = JS function, literals array
1308 // a3 = literal index
1309 // a2 = RegExp pattern
1310 // a1 = RegExp flags
1311 // a0 = RegExp literal clone
1312 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1313 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1314 int literal_offset =
1315 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1316 __ lw(t1, FieldMemOperand(t0, literal_offset));
1317 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1318 __ Branch(&materialized, ne, t1, Operand(at));
1319
1320 // Create regexp literal using runtime function.
1321 // Result will be in v0.
1322 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1323 __ li(a2, Operand(expr->pattern()));
1324 __ li(a1, Operand(expr->flags()));
1325 __ Push(t0, a3, a2, a1);
1326 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1327 __ mov(t1, v0);
1328
1329 __ bind(&materialized);
1330 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1331 Label allocated, runtime_allocate;
1332 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1333 __ jmp(&allocated);
1334
1335 __ bind(&runtime_allocate);
1336 __ push(t1);
1337 __ li(a0, Operand(Smi::FromInt(size)));
1338 __ push(a0);
1339 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1340 __ pop(t1);
1341
1342 __ bind(&allocated);
1343
1344 // After this, registers are used as follows:
1345 // v0: Newly allocated regexp.
1346 // t1: Materialized regexp.
1347 // a2: temp.
1348 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1349 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001350}
1351
1352
1353void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001354 Comment cmnt(masm_, "[ ObjectLiteral");
1355 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1356 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1357 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1358 __ li(a1, Operand(expr->constant_properties()));
1359 int flags = expr->fast_elements()
1360 ? ObjectLiteral::kFastElements
1361 : ObjectLiteral::kNoFlags;
1362 flags |= expr->has_function()
1363 ? ObjectLiteral::kHasFunction
1364 : ObjectLiteral::kNoFlags;
1365 __ li(a0, Operand(Smi::FromInt(flags)));
1366 __ Push(a3, a2, a1, a0);
1367 if (expr->depth() > 1) {
1368 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1369 } else {
1370 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
1371 }
1372
1373 // If result_saved is true the result is on top of the stack. If
1374 // result_saved is false the result is in v0.
1375 bool result_saved = false;
1376
1377 // Mark all computed expressions that are bound to a key that
1378 // is shadowed by a later occurrence of the same key. For the
1379 // marked expressions, no store code is emitted.
1380 expr->CalculateEmitStore();
1381
1382 for (int i = 0; i < expr->properties()->length(); i++) {
1383 ObjectLiteral::Property* property = expr->properties()->at(i);
1384 if (property->IsCompileTimeValue()) continue;
1385
1386 Literal* key = property->key();
1387 Expression* value = property->value();
1388 if (!result_saved) {
1389 __ push(v0); // Save result on stack.
1390 result_saved = true;
1391 }
1392 switch (property->kind()) {
1393 case ObjectLiteral::Property::CONSTANT:
1394 UNREACHABLE();
1395 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1396 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1397 // Fall through.
1398 case ObjectLiteral::Property::COMPUTED:
1399 if (key->handle()->IsSymbol()) {
1400 if (property->emit_store()) {
1401 VisitForAccumulatorValue(value);
1402 __ mov(a0, result_register());
1403 __ li(a2, Operand(key->handle()));
1404 __ lw(a1, MemOperand(sp));
1405 Handle<Code> ic = is_strict_mode()
1406 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1407 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001408 __ Call(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001409 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1410 } else {
1411 VisitForEffect(value);
1412 }
1413 break;
1414 }
1415 // Fall through.
1416 case ObjectLiteral::Property::PROTOTYPE:
1417 // Duplicate receiver on stack.
1418 __ lw(a0, MemOperand(sp));
1419 __ push(a0);
1420 VisitForStackValue(key);
1421 VisitForStackValue(value);
1422 if (property->emit_store()) {
1423 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1424 __ push(a0);
1425 __ CallRuntime(Runtime::kSetProperty, 4);
1426 } else {
1427 __ Drop(3);
1428 }
1429 break;
1430 case ObjectLiteral::Property::GETTER:
1431 case ObjectLiteral::Property::SETTER:
1432 // Duplicate receiver on stack.
1433 __ lw(a0, MemOperand(sp));
1434 __ push(a0);
1435 VisitForStackValue(key);
1436 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1437 Smi::FromInt(1) :
1438 Smi::FromInt(0)));
1439 __ push(a1);
1440 VisitForStackValue(value);
1441 __ CallRuntime(Runtime::kDefineAccessor, 4);
1442 break;
1443 }
1444 }
1445
1446 if (expr->has_function()) {
1447 ASSERT(result_saved);
1448 __ lw(a0, MemOperand(sp));
1449 __ push(a0);
1450 __ CallRuntime(Runtime::kToFastProperties, 1);
1451 }
1452
1453 if (result_saved) {
1454 context()->PlugTOS();
1455 } else {
1456 context()->Plug(v0);
1457 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001458}
1459
1460
1461void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001462 Comment cmnt(masm_, "[ ArrayLiteral");
1463
1464 ZoneList<Expression*>* subexprs = expr->values();
1465 int length = subexprs->length();
1466 __ mov(a0, result_register());
1467 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1468 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1469 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1470 __ li(a1, Operand(expr->constant_elements()));
1471 __ Push(a3, a2, a1);
1472 if (expr->constant_elements()->map() ==
1473 isolate()->heap()->fixed_cow_array_map()) {
1474 FastCloneShallowArrayStub stub(
1475 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1476 __ CallStub(&stub);
1477 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1478 1, a1, a2);
1479 } else if (expr->depth() > 1) {
1480 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1481 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1482 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1483 } else {
1484 FastCloneShallowArrayStub stub(
1485 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
1486 __ CallStub(&stub);
1487 }
1488
1489 bool result_saved = false; // Is the result saved to the stack?
1490
1491 // Emit code to evaluate all the non-constant subexpressions and to store
1492 // them into the newly cloned array.
1493 for (int i = 0; i < length; i++) {
1494 Expression* subexpr = subexprs->at(i);
1495 // If the subexpression is a literal or a simple materialized literal it
1496 // is already set in the cloned array.
1497 if (subexpr->AsLiteral() != NULL ||
1498 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1499 continue;
1500 }
1501
1502 if (!result_saved) {
1503 __ push(v0);
1504 result_saved = true;
1505 }
1506 VisitForAccumulatorValue(subexpr);
1507
1508 // Store the subexpression value in the array's elements.
1509 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1510 __ lw(a1, FieldMemOperand(a1, JSObject::kElementsOffset));
1511 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1512 __ sw(result_register(), FieldMemOperand(a1, offset));
1513
1514 // Update the write barrier for the array store with v0 as the scratch
1515 // register.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001516 __ RecordWrite(a1, Operand(offset), a2, result_register());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001517
1518 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1519 }
1520
1521 if (result_saved) {
1522 context()->PlugTOS();
1523 } else {
1524 context()->Plug(v0);
1525 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001526}
1527
1528
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001529void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001530 Comment cmnt(masm_, "[ Assignment");
1531 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1532 // on the left-hand side.
1533 if (!expr->target()->IsValidLeftHandSide()) {
1534 VisitForEffect(expr->target());
1535 return;
1536 }
1537
1538 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001539 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001540 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1541 LhsKind assign_type = VARIABLE;
1542 Property* property = expr->target()->AsProperty();
1543 if (property != NULL) {
1544 assign_type = (property->key()->IsPropertyName())
1545 ? NAMED_PROPERTY
1546 : KEYED_PROPERTY;
1547 }
1548
1549 // Evaluate LHS expression.
1550 switch (assign_type) {
1551 case VARIABLE:
1552 // Nothing to do here.
1553 break;
1554 case NAMED_PROPERTY:
1555 if (expr->is_compound()) {
1556 // We need the receiver both on the stack and in the accumulator.
1557 VisitForAccumulatorValue(property->obj());
1558 __ push(result_register());
1559 } else {
1560 VisitForStackValue(property->obj());
1561 }
1562 break;
1563 case KEYED_PROPERTY:
1564 // We need the key and receiver on both the stack and in v0 and a1.
1565 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001566 VisitForStackValue(property->obj());
1567 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001568 __ lw(a1, MemOperand(sp, 0));
1569 __ push(v0);
1570 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001571 VisitForStackValue(property->obj());
1572 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001573 }
1574 break;
1575 }
1576
1577 // For compound assignments we need another deoptimization point after the
1578 // variable/property load.
1579 if (expr->is_compound()) {
1580 { AccumulatorValueContext context(this);
1581 switch (assign_type) {
1582 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001583 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001584 PrepareForBailout(expr->target(), TOS_REG);
1585 break;
1586 case NAMED_PROPERTY:
1587 EmitNamedPropertyLoad(property);
1588 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1589 break;
1590 case KEYED_PROPERTY:
1591 EmitKeyedPropertyLoad(property);
1592 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1593 break;
1594 }
1595 }
1596
1597 Token::Value op = expr->binary_op();
1598 __ push(v0); // Left operand goes on the stack.
1599 VisitForAccumulatorValue(expr->value());
1600
1601 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1602 ? OVERWRITE_RIGHT
1603 : NO_OVERWRITE;
1604 SetSourcePosition(expr->position() + 1);
1605 AccumulatorValueContext context(this);
1606 if (ShouldInlineSmiCase(op)) {
1607 EmitInlineSmiBinaryOp(expr->binary_operation(),
1608 op,
1609 mode,
1610 expr->target(),
1611 expr->value());
1612 } else {
1613 EmitBinaryOp(expr->binary_operation(), op, mode);
1614 }
1615
1616 // Deoptimization point in case the binary operation may have side effects.
1617 PrepareForBailout(expr->binary_operation(), TOS_REG);
1618 } else {
1619 VisitForAccumulatorValue(expr->value());
1620 }
1621
1622 // Record source position before possible IC call.
1623 SetSourcePosition(expr->position());
1624
1625 // Store the value.
1626 switch (assign_type) {
1627 case VARIABLE:
1628 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1629 expr->op());
1630 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1631 context()->Plug(v0);
1632 break;
1633 case NAMED_PROPERTY:
1634 EmitNamedPropertyAssignment(expr);
1635 break;
1636 case KEYED_PROPERTY:
1637 EmitKeyedPropertyAssignment(expr);
1638 break;
1639 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001640}
1641
1642
ager@chromium.org5c838252010-02-19 08:53:10 +00001643void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001644 SetSourcePosition(prop->position());
1645 Literal* key = prop->key()->AsLiteral();
1646 __ mov(a0, result_register());
1647 __ li(a2, Operand(key->handle()));
1648 // Call load IC. It has arguments receiver and property name a0 and a2.
1649 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001650 __ Call(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001651}
1652
1653
1654void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001655 SetSourcePosition(prop->position());
1656 __ mov(a0, result_register());
1657 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1658 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001659 __ Call(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001660}
1661
1662
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001663void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001664 Token::Value op,
1665 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001666 Expression* left_expr,
1667 Expression* right_expr) {
1668 Label done, smi_case, stub_call;
1669
1670 Register scratch1 = a2;
1671 Register scratch2 = a3;
1672
1673 // Get the arguments.
1674 Register left = a1;
1675 Register right = a0;
1676 __ pop(left);
1677 __ mov(a0, result_register());
1678
1679 // Perform combined smi check on both operands.
1680 __ Or(scratch1, left, Operand(right));
1681 STATIC_ASSERT(kSmiTag == 0);
1682 JumpPatchSite patch_site(masm_);
1683 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1684
1685 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001686 BinaryOpStub stub(op, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001687 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001688 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001689 __ jmp(&done);
1690
1691 __ bind(&smi_case);
1692 // Smi case. This code works the same way as the smi-smi case in the type
1693 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001694 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001695 switch (op) {
1696 case Token::SAR:
1697 __ Branch(&stub_call);
1698 __ GetLeastBitsFromSmi(scratch1, right, 5);
1699 __ srav(right, left, scratch1);
1700 __ And(v0, right, Operand(~kSmiTagMask));
1701 break;
1702 case Token::SHL: {
1703 __ Branch(&stub_call);
1704 __ SmiUntag(scratch1, left);
1705 __ GetLeastBitsFromSmi(scratch2, right, 5);
1706 __ sllv(scratch1, scratch1, scratch2);
1707 __ Addu(scratch2, scratch1, Operand(0x40000000));
1708 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1709 __ SmiTag(v0, scratch1);
1710 break;
1711 }
1712 case Token::SHR: {
1713 __ Branch(&stub_call);
1714 __ SmiUntag(scratch1, left);
1715 __ GetLeastBitsFromSmi(scratch2, right, 5);
1716 __ srlv(scratch1, scratch1, scratch2);
1717 __ And(scratch2, scratch1, 0xc0000000);
1718 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1719 __ SmiTag(v0, scratch1);
1720 break;
1721 }
1722 case Token::ADD:
1723 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1724 __ BranchOnOverflow(&stub_call, scratch1);
1725 break;
1726 case Token::SUB:
1727 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1728 __ BranchOnOverflow(&stub_call, scratch1);
1729 break;
1730 case Token::MUL: {
1731 __ SmiUntag(scratch1, right);
1732 __ Mult(left, scratch1);
1733 __ mflo(scratch1);
1734 __ mfhi(scratch2);
1735 __ sra(scratch1, scratch1, 31);
1736 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1737 __ mflo(v0);
1738 __ Branch(&done, ne, v0, Operand(zero_reg));
1739 __ Addu(scratch2, right, left);
1740 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1741 ASSERT(Smi::FromInt(0) == 0);
1742 __ mov(v0, zero_reg);
1743 break;
1744 }
1745 case Token::BIT_OR:
1746 __ Or(v0, left, Operand(right));
1747 break;
1748 case Token::BIT_AND:
1749 __ And(v0, left, Operand(right));
1750 break;
1751 case Token::BIT_XOR:
1752 __ Xor(v0, left, Operand(right));
1753 break;
1754 default:
1755 UNREACHABLE();
1756 }
1757
1758 __ bind(&done);
1759 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001760}
1761
1762
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001763void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1764 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001765 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001766 __ mov(a0, result_register());
1767 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001768 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001769 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001770 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001771 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001772 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001773}
1774
1775
1776void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001777 // Invalid left-hand sides are rewritten to have a 'throw
1778 // ReferenceError' on the left-hand side.
1779 if (!expr->IsValidLeftHandSide()) {
1780 VisitForEffect(expr);
1781 return;
1782 }
1783
1784 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001785 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001786 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1787 LhsKind assign_type = VARIABLE;
1788 Property* prop = expr->AsProperty();
1789 if (prop != NULL) {
1790 assign_type = (prop->key()->IsPropertyName())
1791 ? NAMED_PROPERTY
1792 : KEYED_PROPERTY;
1793 }
1794
1795 switch (assign_type) {
1796 case VARIABLE: {
1797 Variable* var = expr->AsVariableProxy()->var();
1798 EffectContext context(this);
1799 EmitVariableAssignment(var, Token::ASSIGN);
1800 break;
1801 }
1802 case NAMED_PROPERTY: {
1803 __ push(result_register()); // Preserve value.
1804 VisitForAccumulatorValue(prop->obj());
1805 __ mov(a1, result_register());
1806 __ pop(a0); // Restore value.
1807 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1808 Handle<Code> ic = is_strict_mode()
1809 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1810 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001811 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001812 break;
1813 }
1814 case KEYED_PROPERTY: {
1815 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001816 VisitForStackValue(prop->obj());
1817 VisitForAccumulatorValue(prop->key());
1818 __ mov(a1, result_register());
1819 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001820 __ pop(a0); // Restore value.
1821 Handle<Code> ic = is_strict_mode()
1822 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
1823 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001824 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001825 break;
1826 }
1827 }
1828 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1829 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001830}
1831
1832
1833void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001834 Token::Value op) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001835 ASSERT(var != NULL);
1836 ASSERT(var->is_global() || var->AsSlot() != NULL);
1837
1838 if (var->is_global()) {
1839 ASSERT(!var->is_this());
1840 // Assignment to a global variable. Use inline caching for the
1841 // assignment. Right-hand-side value is passed in a0, variable name in
1842 // a2, and the global object in a1.
1843 __ mov(a0, result_register());
1844 __ li(a2, Operand(var->name()));
1845 __ lw(a1, GlobalObjectOperand());
1846 Handle<Code> ic = is_strict_mode()
1847 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1848 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001849 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001850
1851 } else if (op == Token::INIT_CONST) {
1852 // Like var declarations, const declarations are hoisted to function
1853 // scope. However, unlike var initializers, const initializers are able
1854 // to drill a hole to that function context, even from inside a 'with'
1855 // context. We thus bypass the normal static scope lookup.
1856 Slot* slot = var->AsSlot();
1857 Label skip;
1858 switch (slot->type()) {
1859 case Slot::PARAMETER:
1860 // No const parameters.
1861 UNREACHABLE();
1862 break;
1863 case Slot::LOCAL:
1864 // Detect const reinitialization by checking for the hole value.
1865 __ lw(a1, MemOperand(fp, SlotOffset(slot)));
1866 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1867 __ Branch(&skip, ne, a1, Operand(t0));
1868 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1869 break;
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001870 case Slot::CONTEXT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001871 case Slot::LOOKUP:
1872 __ push(result_register());
1873 __ li(a0, Operand(slot->var()->name()));
1874 __ Push(cp, a0); // Context and name.
1875 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1876 break;
1877 }
1878 __ bind(&skip);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001879 } else if (var->mode() == Variable::LET && op != Token::INIT_LET) {
1880 // Perform the assignment for non-const variables. Const assignments
1881 // are simply skipped.
1882 Slot* slot = var->AsSlot();
1883 switch (slot->type()) {
1884 case Slot::PARAMETER:
1885 case Slot::LOCAL: {
1886 Label assign;
1887 // Check for an initialized let binding.
1888 __ lw(a1, MemOperand(fp, SlotOffset(slot)));
1889 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1890 __ Branch(&assign, ne, a1, Operand(t0));
1891 __ li(a1, Operand(var->name()));
1892 __ push(a1);
1893 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1894 // Perform the assignment.
1895 __ bind(&assign);
1896 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1897 break;
1898 }
1899 case Slot::CONTEXT: {
1900 // Let variables may be the hole value if they have not been
1901 // initialized. Throw a type error in this case.
1902 Label assign;
1903 MemOperand target = EmitSlotSearch(slot, a1);
1904 // Check for an initialized let binding.
1905 __ lw(a3, target);
1906 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1907 __ Branch(&assign, ne, a3, Operand(t0));
1908 __ li(a3, Operand(var->name()));
1909 __ push(a3);
1910 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1911 // Perform the assignment.
1912 __ bind(&assign);
1913 __ sw(result_register(), target);
1914 // RecordWrite may destroy all its register arguments.
1915 __ mov(a3, result_register());
1916 int offset = Context::SlotOffset(slot->index());
1917 __ RecordWrite(a1, Operand(offset), a2, a3);
1918 break;
1919 }
1920 case Slot::LOOKUP:
1921 // Call the runtime for the assignment.
1922 __ push(v0); // Value.
1923 __ li(a1, Operand(slot->var()->name()));
1924 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1925 __ Push(cp, a1, a0); // Context, name, strict mode.
1926 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1927 break;
1928 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001929
1930 } else if (var->mode() != Variable::CONST) {
1931 // Perform the assignment for non-const variables. Const assignments
1932 // are simply skipped.
1933 Slot* slot = var->AsSlot();
1934 switch (slot->type()) {
1935 case Slot::PARAMETER:
1936 case Slot::LOCAL:
1937 // Perform the assignment.
1938 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1939 break;
1940
1941 case Slot::CONTEXT: {
1942 MemOperand target = EmitSlotSearch(slot, a1);
1943 // Perform the assignment and issue the write barrier.
1944 __ sw(result_register(), target);
1945 // RecordWrite may destroy all its register arguments.
1946 __ mov(a3, result_register());
1947 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
1948 __ RecordWrite(a1, Operand(offset), a2, a3);
1949 break;
1950 }
1951
1952 case Slot::LOOKUP:
1953 // Call the runtime for the assignment.
1954 __ push(v0); // Value.
1955 __ li(a1, Operand(slot->var()->name()));
1956 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1957 __ Push(cp, a1, a0); // Context, name, strict mode.
1958 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1959 break;
1960 }
1961 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001962}
1963
1964
1965void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001966 // Assignment to a property, using a named store IC.
1967 Property* prop = expr->target()->AsProperty();
1968 ASSERT(prop != NULL);
1969 ASSERT(prop->key()->AsLiteral() != NULL);
1970
1971 // If the assignment starts a block of assignments to the same object,
1972 // change to slow case to avoid the quadratic behavior of repeatedly
1973 // adding fast properties.
1974 if (expr->starts_initialization_block()) {
1975 __ push(result_register());
1976 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1977 __ push(t0);
1978 __ CallRuntime(Runtime::kToSlowProperties, 1);
1979 __ pop(result_register());
1980 }
1981
1982 // Record source code position before IC call.
1983 SetSourcePosition(expr->position());
1984 __ mov(a0, result_register()); // Load the value.
1985 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1986 // Load receiver to a1. Leave a copy in the stack if needed for turning the
1987 // receiver into fast case.
1988 if (expr->ends_initialization_block()) {
1989 __ lw(a1, MemOperand(sp));
1990 } else {
1991 __ pop(a1);
1992 }
1993
1994 Handle<Code> ic = is_strict_mode()
1995 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1996 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001997 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001998
1999 // If the assignment ends an initialization block, revert to fast case.
2000 if (expr->ends_initialization_block()) {
2001 __ push(v0); // Result of assignment, saved even if not needed.
2002 // Receiver is under the result value.
2003 __ lw(t0, MemOperand(sp, kPointerSize));
2004 __ push(t0);
2005 __ CallRuntime(Runtime::kToFastProperties, 1);
2006 __ pop(v0);
2007 __ Drop(1);
2008 }
2009 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2010 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002011}
2012
2013
2014void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002015 // Assignment to a property, using a keyed store IC.
2016
2017 // If the assignment starts a block of assignments to the same object,
2018 // change to slow case to avoid the quadratic behavior of repeatedly
2019 // adding fast properties.
2020 if (expr->starts_initialization_block()) {
2021 __ push(result_register());
2022 // Receiver is now under the key and value.
2023 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2024 __ push(t0);
2025 __ CallRuntime(Runtime::kToSlowProperties, 1);
2026 __ pop(result_register());
2027 }
2028
2029 // Record source code position before IC call.
2030 SetSourcePosition(expr->position());
2031 // Call keyed store IC.
2032 // The arguments are:
2033 // - a0 is the value,
2034 // - a1 is the key,
2035 // - a2 is the receiver.
2036 __ mov(a0, result_register());
2037 __ pop(a1); // Key.
2038 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2039 // receiver into fast case.
2040 if (expr->ends_initialization_block()) {
2041 __ lw(a2, MemOperand(sp));
2042 } else {
2043 __ pop(a2);
2044 }
2045
2046 Handle<Code> ic = is_strict_mode()
2047 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
2048 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002049 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002050
2051 // If the assignment ends an initialization block, revert to fast case.
2052 if (expr->ends_initialization_block()) {
2053 __ push(v0); // Result of assignment, saved even if not needed.
2054 // Receiver is under the result value.
2055 __ lw(t0, MemOperand(sp, kPointerSize));
2056 __ push(t0);
2057 __ CallRuntime(Runtime::kToFastProperties, 1);
2058 __ pop(v0);
2059 __ Drop(1);
2060 }
2061 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2062 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002063}
2064
2065
2066void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002067 Comment cmnt(masm_, "[ Property");
2068 Expression* key = expr->key();
2069
2070 if (key->IsPropertyName()) {
2071 VisitForAccumulatorValue(expr->obj());
2072 EmitNamedPropertyLoad(expr);
2073 context()->Plug(v0);
2074 } else {
2075 VisitForStackValue(expr->obj());
2076 VisitForAccumulatorValue(expr->key());
2077 __ pop(a1);
2078 EmitKeyedPropertyLoad(expr);
2079 context()->Plug(v0);
2080 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002081}
2082
lrn@chromium.org7516f052011-03-30 08:52:27 +00002083
ager@chromium.org5c838252010-02-19 08:53:10 +00002084void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002085 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002086 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002087 // Code common for calls using the IC.
2088 ZoneList<Expression*>* args = expr->arguments();
2089 int arg_count = args->length();
2090 { PreservePositionScope scope(masm()->positions_recorder());
2091 for (int i = 0; i < arg_count; i++) {
2092 VisitForStackValue(args->at(i));
2093 }
2094 __ li(a2, Operand(name));
2095 }
2096 // Record source position for debugger.
2097 SetSourcePosition(expr->position());
2098 // Call the IC initialization code.
2099 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2100 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00002101 isolate()->stub_cache()->ComputeCallInitialize(arg_count, in_loop, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002102 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002103 RecordJSReturnSite(expr);
2104 // Restore context register.
2105 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2106 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002107}
2108
2109
lrn@chromium.org7516f052011-03-30 08:52:27 +00002110void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002111 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002112 // Load the key.
2113 VisitForAccumulatorValue(key);
2114
2115 // Swap the name of the function and the receiver on the stack to follow
2116 // the calling convention for call ICs.
2117 __ pop(a1);
2118 __ push(v0);
2119 __ push(a1);
2120
2121 // Code common for calls using the IC.
2122 ZoneList<Expression*>* args = expr->arguments();
2123 int arg_count = args->length();
2124 { PreservePositionScope scope(masm()->positions_recorder());
2125 for (int i = 0; i < arg_count; i++) {
2126 VisitForStackValue(args->at(i));
2127 }
2128 }
2129 // Record source position for debugger.
2130 SetSourcePosition(expr->position());
2131 // Call the IC initialization code.
2132 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2133 Handle<Code> ic =
2134 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count, in_loop);
2135 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002136 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002137 RecordJSReturnSite(expr);
2138 // Restore context register.
2139 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2140 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002141}
2142
2143
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002144void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002145 // Code common for calls using the call stub.
2146 ZoneList<Expression*>* args = expr->arguments();
2147 int arg_count = args->length();
2148 { PreservePositionScope scope(masm()->positions_recorder());
2149 for (int i = 0; i < arg_count; i++) {
2150 VisitForStackValue(args->at(i));
2151 }
2152 }
2153 // Record source position for debugger.
2154 SetSourcePosition(expr->position());
2155 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2156 CallFunctionStub stub(arg_count, in_loop, flags);
2157 __ CallStub(&stub);
2158 RecordJSReturnSite(expr);
2159 // Restore context register.
2160 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2161 context()->DropAndPlug(1, v0);
2162}
2163
2164
2165void FullCodeGenerator::EmitResolvePossiblyDirectEval(ResolveEvalFlag flag,
2166 int arg_count) {
2167 // Push copy of the first argument or undefined if it doesn't exist.
2168 if (arg_count > 0) {
2169 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2170 } else {
2171 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2172 }
2173 __ push(a1);
2174
2175 // Push the receiver of the enclosing function and do runtime call.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002176 int receiver_offset = 2 + info_->scope()->num_parameters();
2177 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002178 __ push(a1);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002179 // Push the strict mode flag. In harmony mode every eval call
2180 // is a strict mode eval call.
2181 StrictModeFlag strict_mode = strict_mode_flag();
2182 if (FLAG_harmony_block_scoping) {
2183 strict_mode = kStrictMode;
2184 }
2185 __ li(a1, Operand(Smi::FromInt(strict_mode)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002186 __ push(a1);
2187
2188 __ CallRuntime(flag == SKIP_CONTEXT_LOOKUP
2189 ? Runtime::kResolvePossiblyDirectEvalNoLookup
2190 : Runtime::kResolvePossiblyDirectEval, 4);
ager@chromium.org5c838252010-02-19 08:53:10 +00002191}
2192
2193
2194void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002195#ifdef DEBUG
2196 // We want to verify that RecordJSReturnSite gets called on all paths
2197 // through this function. Avoid early returns.
2198 expr->return_is_recorded_ = false;
2199#endif
2200
2201 Comment cmnt(masm_, "[ Call");
2202 Expression* fun = expr->expression();
2203 Variable* var = fun->AsVariableProxy()->AsVariable();
2204
2205 if (var != NULL && var->is_possibly_eval()) {
2206 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2207 // resolve the function we need to call and the receiver of the
2208 // call. Then we call the resolved function using the given
2209 // arguments.
2210 ZoneList<Expression*>* args = expr->arguments();
2211 int arg_count = args->length();
2212
2213 { PreservePositionScope pos_scope(masm()->positions_recorder());
2214 VisitForStackValue(fun);
2215 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2216 __ push(a2); // Reserved receiver slot.
2217
2218 // Push the arguments.
2219 for (int i = 0; i < arg_count; i++) {
2220 VisitForStackValue(args->at(i));
2221 }
2222 // If we know that eval can only be shadowed by eval-introduced
2223 // variables we attempt to load the global eval function directly
2224 // in generated code. If we succeed, there is no need to perform a
2225 // context lookup in the runtime system.
2226 Label done;
2227 if (var->AsSlot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
2228 Label slow;
2229 EmitLoadGlobalSlotCheckExtensions(var->AsSlot(),
2230 NOT_INSIDE_TYPEOF,
2231 &slow);
2232 // Push the function and resolve eval.
2233 __ push(v0);
2234 EmitResolvePossiblyDirectEval(SKIP_CONTEXT_LOOKUP, arg_count);
2235 __ jmp(&done);
2236 __ bind(&slow);
2237 }
2238
2239 // Push copy of the function (found below the arguments) and
2240 // resolve eval.
2241 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2242 __ push(a1);
2243 EmitResolvePossiblyDirectEval(PERFORM_CONTEXT_LOOKUP, arg_count);
2244 if (done.is_linked()) {
2245 __ bind(&done);
2246 }
2247
2248 // The runtime call returns a pair of values in v0 (function) and
2249 // v1 (receiver). Touch up the stack with the right values.
2250 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2251 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2252 }
2253 // Record source position for debugger.
2254 SetSourcePosition(expr->position());
2255 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002256 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002257 __ CallStub(&stub);
2258 RecordJSReturnSite(expr);
2259 // Restore context register.
2260 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2261 context()->DropAndPlug(1, v0);
2262 } else if (var != NULL && !var->is_this() && var->is_global()) {
2263 // Push global object as receiver for the call IC.
2264 __ lw(a0, GlobalObjectOperand());
2265 __ push(a0);
2266 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
2267 } else if (var != NULL && var->AsSlot() != NULL &&
2268 var->AsSlot()->type() == Slot::LOOKUP) {
2269 // Call to a lookup slot (dynamically introduced variable).
2270 Label slow, done;
2271
2272 { PreservePositionScope scope(masm()->positions_recorder());
2273 // Generate code for loading from variables potentially shadowed
2274 // by eval-introduced variables.
2275 EmitDynamicLoadFromSlotFastCase(var->AsSlot(),
2276 NOT_INSIDE_TYPEOF,
2277 &slow,
2278 &done);
2279 }
2280
2281 __ bind(&slow);
2282 // Call the runtime to find the function to call (returned in v0)
2283 // and the object holding it (returned in v1).
2284 __ push(context_register());
2285 __ li(a2, Operand(var->name()));
2286 __ push(a2);
2287 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2288 __ Push(v0, v1); // Function, receiver.
2289
2290 // If fast case code has been generated, emit code to push the
2291 // function and receiver and have the slow path jump around this
2292 // code.
2293 if (done.is_linked()) {
2294 Label call;
2295 __ Branch(&call);
2296 __ bind(&done);
2297 // Push function.
2298 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002299 // The receiver is implicitly the global receiver. Indicate this
2300 // by passing the hole to the call function stub.
2301 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002302 __ push(a1);
2303 __ bind(&call);
2304 }
2305
danno@chromium.org40cb8782011-05-25 07:58:50 +00002306 // The receiver is either the global receiver or an object found
2307 // by LoadContextSlot. That object could be the hole if the
2308 // receiver is implicitly the global object.
2309 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002310 } else if (fun->AsProperty() != NULL) {
2311 // Call to an object property.
2312 Property* prop = fun->AsProperty();
2313 Literal* key = prop->key()->AsLiteral();
2314 if (key != NULL && key->handle()->IsSymbol()) {
2315 // Call to a named property, use call IC.
2316 { PreservePositionScope scope(masm()->positions_recorder());
2317 VisitForStackValue(prop->obj());
2318 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00002319 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002320 } else {
2321 // Call to a keyed property.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002322 { PreservePositionScope scope(masm()->positions_recorder());
2323 VisitForStackValue(prop->obj());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002324 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002325 EmitKeyedCallWithIC(expr, prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002326 }
2327 } else {
2328 { PreservePositionScope scope(masm()->positions_recorder());
2329 VisitForStackValue(fun);
2330 }
2331 // Load global receiver object.
2332 __ lw(a1, GlobalObjectOperand());
2333 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2334 __ push(a1);
2335 // Emit function call.
2336 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2337 }
2338
2339#ifdef DEBUG
2340 // RecordJSReturnSite should have been called.
2341 ASSERT(expr->return_is_recorded_);
2342#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002343}
2344
2345
2346void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002347 Comment cmnt(masm_, "[ CallNew");
2348 // According to ECMA-262, section 11.2.2, page 44, the function
2349 // expression in new calls must be evaluated before the
2350 // arguments.
2351
2352 // Push constructor on the stack. If it's not a function it's used as
2353 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2354 // ignored.
2355 VisitForStackValue(expr->expression());
2356
2357 // Push the arguments ("left-to-right") on the stack.
2358 ZoneList<Expression*>* args = expr->arguments();
2359 int arg_count = args->length();
2360 for (int i = 0; i < arg_count; i++) {
2361 VisitForStackValue(args->at(i));
2362 }
2363
2364 // Call the construct call builtin that handles allocation and
2365 // constructor invocation.
2366 SetSourcePosition(expr->position());
2367
2368 // Load function and argument count into a1 and a0.
2369 __ li(a0, Operand(arg_count));
2370 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2371
2372 Handle<Code> construct_builtin =
2373 isolate()->builtins()->JSConstructCall();
2374 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2375 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002376}
2377
2378
lrn@chromium.org7516f052011-03-30 08:52:27 +00002379void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002380 ASSERT(args->length() == 1);
2381
2382 VisitForAccumulatorValue(args->at(0));
2383
2384 Label materialize_true, materialize_false;
2385 Label* if_true = NULL;
2386 Label* if_false = NULL;
2387 Label* fall_through = NULL;
2388 context()->PrepareTest(&materialize_true, &materialize_false,
2389 &if_true, &if_false, &fall_through);
2390
2391 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2392 __ And(t0, v0, Operand(kSmiTagMask));
2393 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2394
2395 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002396}
2397
2398
2399void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002400 ASSERT(args->length() == 1);
2401
2402 VisitForAccumulatorValue(args->at(0));
2403
2404 Label materialize_true, materialize_false;
2405 Label* if_true = NULL;
2406 Label* if_false = NULL;
2407 Label* fall_through = NULL;
2408 context()->PrepareTest(&materialize_true, &materialize_false,
2409 &if_true, &if_false, &fall_through);
2410
2411 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2412 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2413 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2414
2415 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002416}
2417
2418
2419void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002420 ASSERT(args->length() == 1);
2421
2422 VisitForAccumulatorValue(args->at(0));
2423
2424 Label materialize_true, materialize_false;
2425 Label* if_true = NULL;
2426 Label* if_false = NULL;
2427 Label* fall_through = NULL;
2428 context()->PrepareTest(&materialize_true, &materialize_false,
2429 &if_true, &if_false, &fall_through);
2430
2431 __ JumpIfSmi(v0, if_false);
2432 __ LoadRoot(at, Heap::kNullValueRootIndex);
2433 __ Branch(if_true, eq, v0, Operand(at));
2434 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2435 // Undetectable objects behave like undefined when tested with typeof.
2436 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2437 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2438 __ Branch(if_false, ne, at, Operand(zero_reg));
2439 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002440 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002441 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002442 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2443 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002444
2445 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002446}
2447
2448
2449void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002450 ASSERT(args->length() == 1);
2451
2452 VisitForAccumulatorValue(args->at(0));
2453
2454 Label materialize_true, materialize_false;
2455 Label* if_true = NULL;
2456 Label* if_false = NULL;
2457 Label* fall_through = NULL;
2458 context()->PrepareTest(&materialize_true, &materialize_false,
2459 &if_true, &if_false, &fall_through);
2460
2461 __ JumpIfSmi(v0, if_false);
2462 __ GetObjectType(v0, a1, a1);
2463 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002464 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002465 if_true, if_false, fall_through);
2466
2467 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002468}
2469
2470
2471void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002472 ASSERT(args->length() == 1);
2473
2474 VisitForAccumulatorValue(args->at(0));
2475
2476 Label materialize_true, materialize_false;
2477 Label* if_true = NULL;
2478 Label* if_false = NULL;
2479 Label* fall_through = NULL;
2480 context()->PrepareTest(&materialize_true, &materialize_false,
2481 &if_true, &if_false, &fall_through);
2482
2483 __ JumpIfSmi(v0, if_false);
2484 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2485 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2486 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2487 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2488 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2489
2490 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002491}
2492
2493
2494void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2495 ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002496
2497 ASSERT(args->length() == 1);
2498
2499 VisitForAccumulatorValue(args->at(0));
2500
2501 Label materialize_true, materialize_false;
2502 Label* if_true = NULL;
2503 Label* if_false = NULL;
2504 Label* fall_through = NULL;
2505 context()->PrepareTest(&materialize_true, &materialize_false,
2506 &if_true, &if_false, &fall_through);
2507
2508 if (FLAG_debug_code) __ AbortIfSmi(v0);
2509
2510 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2511 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2512 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2513 __ Branch(if_true, ne, t0, Operand(zero_reg));
2514
2515 // Check for fast case object. Generate false result for slow case object.
2516 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2517 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2518 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2519 __ Branch(if_false, eq, a2, Operand(t0));
2520
2521 // Look for valueOf symbol in the descriptor array, and indicate false if
2522 // found. The type is not checked, so if it is a transition it is a false
2523 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002524 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002525 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2526 // t0: descriptor array
2527 // a3: length of descriptor array
2528 // Calculate the end of the descriptor array.
2529 STATIC_ASSERT(kSmiTag == 0);
2530 STATIC_ASSERT(kSmiTagSize == 1);
2531 STATIC_ASSERT(kPointerSize == 4);
2532 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2533 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2534 __ Addu(a2, a2, t1);
2535
2536 // Calculate location of the first key name.
2537 __ Addu(t0,
2538 t0,
2539 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2540 DescriptorArray::kFirstIndex * kPointerSize));
2541 // Loop through all the keys in the descriptor array. If one of these is the
2542 // symbol valueOf the result is false.
2543 Label entry, loop;
2544 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2545 // used in the loop below.
2546 __ li(t2, Operand(FACTORY->value_of_symbol()));
2547 __ jmp(&entry);
2548 __ bind(&loop);
2549 __ lw(a3, MemOperand(t0, 0));
2550 __ Branch(if_false, eq, a3, Operand(t2));
2551 __ Addu(t0, t0, Operand(kPointerSize));
2552 __ bind(&entry);
2553 __ Branch(&loop, ne, t0, Operand(a2));
2554
2555 // If a valueOf property is not found on the object check that it's
2556 // prototype is the un-modified String prototype. If not result is false.
2557 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2558 __ JumpIfSmi(a2, if_false);
2559 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2560 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2561 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2562 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2563 __ Branch(if_false, ne, a2, Operand(a3));
2564
2565 // Set the bit in the map to indicate that it has been checked safe for
2566 // default valueOf and set true result.
2567 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2568 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2569 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2570 __ jmp(if_true);
2571
2572 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2573 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002574}
2575
2576
2577void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002578 ASSERT(args->length() == 1);
2579
2580 VisitForAccumulatorValue(args->at(0));
2581
2582 Label materialize_true, materialize_false;
2583 Label* if_true = NULL;
2584 Label* if_false = NULL;
2585 Label* fall_through = NULL;
2586 context()->PrepareTest(&materialize_true, &materialize_false,
2587 &if_true, &if_false, &fall_through);
2588
2589 __ JumpIfSmi(v0, if_false);
2590 __ GetObjectType(v0, a1, a2);
2591 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2592 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2593 __ Branch(if_false);
2594
2595 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002596}
2597
2598
2599void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002600 ASSERT(args->length() == 1);
2601
2602 VisitForAccumulatorValue(args->at(0));
2603
2604 Label materialize_true, materialize_false;
2605 Label* if_true = NULL;
2606 Label* if_false = NULL;
2607 Label* fall_through = NULL;
2608 context()->PrepareTest(&materialize_true, &materialize_false,
2609 &if_true, &if_false, &fall_through);
2610
2611 __ JumpIfSmi(v0, if_false);
2612 __ GetObjectType(v0, a1, a1);
2613 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2614 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2615 if_true, if_false, fall_through);
2616
2617 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002618}
2619
2620
2621void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002622 ASSERT(args->length() == 1);
2623
2624 VisitForAccumulatorValue(args->at(0));
2625
2626 Label materialize_true, materialize_false;
2627 Label* if_true = NULL;
2628 Label* if_false = NULL;
2629 Label* fall_through = NULL;
2630 context()->PrepareTest(&materialize_true, &materialize_false,
2631 &if_true, &if_false, &fall_through);
2632
2633 __ JumpIfSmi(v0, if_false);
2634 __ GetObjectType(v0, a1, a1);
2635 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2636 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2637
2638 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002639}
2640
2641
2642void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002643 ASSERT(args->length() == 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 // Get the frame pointer for the calling frame.
2653 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2654
2655 // Skip the arguments adaptor frame if it exists.
2656 Label check_frame_marker;
2657 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2658 __ Branch(&check_frame_marker, ne,
2659 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2660 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2661
2662 // Check the marker in the calling frame.
2663 __ bind(&check_frame_marker);
2664 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
2665 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2666 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2667 if_true, if_false, fall_through);
2668
2669 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002670}
2671
2672
2673void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002674 ASSERT(args->length() == 2);
2675
2676 // Load the two objects into registers and perform the comparison.
2677 VisitForStackValue(args->at(0));
2678 VisitForAccumulatorValue(args->at(1));
2679
2680 Label materialize_true, materialize_false;
2681 Label* if_true = NULL;
2682 Label* if_false = NULL;
2683 Label* fall_through = NULL;
2684 context()->PrepareTest(&materialize_true, &materialize_false,
2685 &if_true, &if_false, &fall_through);
2686
2687 __ pop(a1);
2688 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2689 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2690
2691 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002692}
2693
2694
2695void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002696 ASSERT(args->length() == 1);
2697
2698 // ArgumentsAccessStub expects the key in a1 and the formal
2699 // parameter count in a0.
2700 VisitForAccumulatorValue(args->at(0));
2701 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002702 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002703 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2704 __ CallStub(&stub);
2705 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002706}
2707
2708
2709void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002710 ASSERT(args->length() == 0);
2711
2712 Label exit;
2713 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002714 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002715
2716 // Check if the calling frame is an arguments adaptor frame.
2717 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2718 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2719 __ Branch(&exit, ne, a3,
2720 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2721
2722 // Arguments adaptor case: Read the arguments length from the
2723 // adaptor frame.
2724 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2725
2726 __ bind(&exit);
2727 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002728}
2729
2730
2731void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002732 ASSERT(args->length() == 1);
2733 Label done, null, function, non_function_constructor;
2734
2735 VisitForAccumulatorValue(args->at(0));
2736
2737 // If the object is a smi, we return null.
2738 __ JumpIfSmi(v0, &null);
2739
2740 // Check that the object is a JS object but take special care of JS
2741 // functions to make sure they have 'Function' as their class.
2742 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002743 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002744
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002745 // As long as LAST_CALLABLE_SPEC_OBJECT_TYPE is the last instance type, and
2746 // FIRST_CALLABLE_SPEC_OBJECT_TYPE comes right after
2747 // LAST_NONCALLABLE_SPEC_OBJECT_TYPE, we can avoid checking for the latter.
2748 STATIC_ASSERT(LAST_TYPE == LAST_CALLABLE_SPEC_OBJECT_TYPE);
2749 STATIC_ASSERT(FIRST_CALLABLE_SPEC_OBJECT_TYPE ==
2750 LAST_NONCALLABLE_SPEC_OBJECT_TYPE + 1);
2751 __ Branch(&function, ge, a1, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002752
2753 // Check if the constructor in the map is a function.
2754 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2755 __ GetObjectType(v0, a1, a1);
2756 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2757
2758 // v0 now contains the constructor function. Grab the
2759 // instance class name from there.
2760 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2761 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2762 __ Branch(&done);
2763
2764 // Functions have class 'Function'.
2765 __ bind(&function);
2766 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2767 __ jmp(&done);
2768
2769 // Objects with a non-function constructor have class 'Object'.
2770 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002771 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002772 __ jmp(&done);
2773
2774 // Non-JS objects have class null.
2775 __ bind(&null);
2776 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2777
2778 // All done.
2779 __ bind(&done);
2780
2781 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002782}
2783
2784
2785void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002786 // Conditionally generate a log call.
2787 // Args:
2788 // 0 (literal string): The type of logging (corresponds to the flags).
2789 // This is used to determine whether or not to generate the log call.
2790 // 1 (string): Format string. Access the string at argument index 2
2791 // with '%2s' (see Logger::LogRuntime for all the formats).
2792 // 2 (array): Arguments to the format string.
2793 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002794 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2795 VisitForStackValue(args->at(1));
2796 VisitForStackValue(args->at(2));
2797 __ CallRuntime(Runtime::kLog, 2);
2798 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002799
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002800 // Finally, we're expected to leave a value on the top of the stack.
2801 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2802 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002803}
2804
2805
2806void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002807 ASSERT(args->length() == 0);
2808
2809 Label slow_allocate_heapnumber;
2810 Label heapnumber_allocated;
2811
2812 // Save the new heap number in callee-saved register s0, since
2813 // we call out to external C code below.
2814 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2815 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2816 __ jmp(&heapnumber_allocated);
2817
2818 __ bind(&slow_allocate_heapnumber);
2819
2820 // Allocate a heap number.
2821 __ CallRuntime(Runtime::kNumberAlloc, 0);
2822 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2823
2824 __ bind(&heapnumber_allocated);
2825
2826 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2827 // by computing:
2828 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2829 if (CpuFeatures::IsSupported(FPU)) {
2830 __ PrepareCallCFunction(1, a0);
2831 __ li(a0, Operand(ExternalReference::isolate_address()));
2832 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2833
2834
2835 CpuFeatures::Scope scope(FPU);
2836 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2837 __ li(a1, Operand(0x41300000));
2838 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002839 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002840 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002841 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002842 // Subtract and store the result in the heap number.
2843 __ sub_d(f0, f12, f14);
2844 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2845 __ mov(v0, s0);
2846 } else {
2847 __ PrepareCallCFunction(2, a0);
2848 __ mov(a0, s0);
2849 __ li(a1, Operand(ExternalReference::isolate_address()));
2850 __ CallCFunction(
2851 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2852 }
2853
2854 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002855}
2856
2857
2858void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002859 // Load the arguments on the stack and call the stub.
2860 SubStringStub stub;
2861 ASSERT(args->length() == 3);
2862 VisitForStackValue(args->at(0));
2863 VisitForStackValue(args->at(1));
2864 VisitForStackValue(args->at(2));
2865 __ CallStub(&stub);
2866 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002867}
2868
2869
2870void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002871 // Load the arguments on the stack and call the stub.
2872 RegExpExecStub stub;
2873 ASSERT(args->length() == 4);
2874 VisitForStackValue(args->at(0));
2875 VisitForStackValue(args->at(1));
2876 VisitForStackValue(args->at(2));
2877 VisitForStackValue(args->at(3));
2878 __ CallStub(&stub);
2879 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002880}
2881
2882
2883void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002884 ASSERT(args->length() == 1);
2885
2886 VisitForAccumulatorValue(args->at(0)); // Load the object.
2887
2888 Label done;
2889 // If the object is a smi return the object.
2890 __ JumpIfSmi(v0, &done);
2891 // If the object is not a value type, return the object.
2892 __ GetObjectType(v0, a1, a1);
2893 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2894
2895 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2896
2897 __ bind(&done);
2898 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002899}
2900
2901
2902void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002903 // Load the arguments on the stack and call the runtime function.
2904 ASSERT(args->length() == 2);
2905 VisitForStackValue(args->at(0));
2906 VisitForStackValue(args->at(1));
2907 MathPowStub stub;
2908 __ CallStub(&stub);
2909 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002910}
2911
2912
2913void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002914 ASSERT(args->length() == 2);
2915
2916 VisitForStackValue(args->at(0)); // Load the object.
2917 VisitForAccumulatorValue(args->at(1)); // Load the value.
2918 __ pop(a1); // v0 = value. a1 = object.
2919
2920 Label done;
2921 // If the object is a smi, return the value.
2922 __ JumpIfSmi(a1, &done);
2923
2924 // If the object is not a value type, return the value.
2925 __ GetObjectType(a1, a2, a2);
2926 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2927
2928 // Store the value.
2929 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2930 // Update the write barrier. Save the value as it will be
2931 // overwritten by the write barrier code and is needed afterward.
2932 __ RecordWrite(a1, Operand(JSValue::kValueOffset - kHeapObjectTag), a2, a3);
2933
2934 __ bind(&done);
2935 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002936}
2937
2938
2939void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002940 ASSERT_EQ(args->length(), 1);
2941
2942 // Load the argument on the stack and call the stub.
2943 VisitForStackValue(args->at(0));
2944
2945 NumberToStringStub stub;
2946 __ CallStub(&stub);
2947 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002948}
2949
2950
2951void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002952 ASSERT(args->length() == 1);
2953
2954 VisitForAccumulatorValue(args->at(0));
2955
2956 Label done;
2957 StringCharFromCodeGenerator generator(v0, a1);
2958 generator.GenerateFast(masm_);
2959 __ jmp(&done);
2960
2961 NopRuntimeCallHelper call_helper;
2962 generator.GenerateSlow(masm_, call_helper);
2963
2964 __ bind(&done);
2965 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002966}
2967
2968
2969void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002970 ASSERT(args->length() == 2);
2971
2972 VisitForStackValue(args->at(0));
2973 VisitForAccumulatorValue(args->at(1));
2974 __ mov(a0, result_register());
2975
2976 Register object = a1;
2977 Register index = a0;
2978 Register scratch = a2;
2979 Register result = v0;
2980
2981 __ pop(object);
2982
2983 Label need_conversion;
2984 Label index_out_of_range;
2985 Label done;
2986 StringCharCodeAtGenerator generator(object,
2987 index,
2988 scratch,
2989 result,
2990 &need_conversion,
2991 &need_conversion,
2992 &index_out_of_range,
2993 STRING_INDEX_IS_NUMBER);
2994 generator.GenerateFast(masm_);
2995 __ jmp(&done);
2996
2997 __ bind(&index_out_of_range);
2998 // When the index is out of range, the spec requires us to return
2999 // NaN.
3000 __ LoadRoot(result, Heap::kNanValueRootIndex);
3001 __ jmp(&done);
3002
3003 __ bind(&need_conversion);
3004 // Load the undefined value into the result register, which will
3005 // trigger conversion.
3006 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3007 __ jmp(&done);
3008
3009 NopRuntimeCallHelper call_helper;
3010 generator.GenerateSlow(masm_, call_helper);
3011
3012 __ bind(&done);
3013 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003014}
3015
3016
3017void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003018 ASSERT(args->length() == 2);
3019
3020 VisitForStackValue(args->at(0));
3021 VisitForAccumulatorValue(args->at(1));
3022 __ mov(a0, result_register());
3023
3024 Register object = a1;
3025 Register index = a0;
3026 Register scratch1 = a2;
3027 Register scratch2 = a3;
3028 Register result = v0;
3029
3030 __ pop(object);
3031
3032 Label need_conversion;
3033 Label index_out_of_range;
3034 Label done;
3035 StringCharAtGenerator generator(object,
3036 index,
3037 scratch1,
3038 scratch2,
3039 result,
3040 &need_conversion,
3041 &need_conversion,
3042 &index_out_of_range,
3043 STRING_INDEX_IS_NUMBER);
3044 generator.GenerateFast(masm_);
3045 __ jmp(&done);
3046
3047 __ bind(&index_out_of_range);
3048 // When the index is out of range, the spec requires us to return
3049 // the empty string.
3050 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3051 __ jmp(&done);
3052
3053 __ bind(&need_conversion);
3054 // Move smi zero into the result register, which will trigger
3055 // conversion.
3056 __ li(result, Operand(Smi::FromInt(0)));
3057 __ jmp(&done);
3058
3059 NopRuntimeCallHelper call_helper;
3060 generator.GenerateSlow(masm_, call_helper);
3061
3062 __ bind(&done);
3063 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003064}
3065
3066
3067void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003068 ASSERT_EQ(2, args->length());
3069
3070 VisitForStackValue(args->at(0));
3071 VisitForStackValue(args->at(1));
3072
3073 StringAddStub stub(NO_STRING_ADD_FLAGS);
3074 __ CallStub(&stub);
3075 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003076}
3077
3078
3079void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003080 ASSERT_EQ(2, args->length());
3081
3082 VisitForStackValue(args->at(0));
3083 VisitForStackValue(args->at(1));
3084
3085 StringCompareStub stub;
3086 __ CallStub(&stub);
3087 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003088}
3089
3090
3091void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003092 // Load the argument on the stack and call the stub.
3093 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3094 TranscendentalCacheStub::TAGGED);
3095 ASSERT(args->length() == 1);
3096 VisitForStackValue(args->at(0));
3097 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3098 __ CallStub(&stub);
3099 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003100}
3101
3102
3103void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003104 // Load the argument on the stack and call the stub.
3105 TranscendentalCacheStub stub(TranscendentalCache::COS,
3106 TranscendentalCacheStub::TAGGED);
3107 ASSERT(args->length() == 1);
3108 VisitForStackValue(args->at(0));
3109 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3110 __ CallStub(&stub);
3111 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003112}
3113
3114
3115void FullCodeGenerator::EmitMathLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003116 // Load the argument on the stack and call the stub.
3117 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3118 TranscendentalCacheStub::TAGGED);
3119 ASSERT(args->length() == 1);
3120 VisitForStackValue(args->at(0));
3121 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3122 __ CallStub(&stub);
3123 context()->Plug(v0);
3124}
3125
3126
3127void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
3128 // Load the argument on the stack and call the runtime function.
3129 ASSERT(args->length() == 1);
3130 VisitForStackValue(args->at(0));
3131 __ CallRuntime(Runtime::kMath_sqrt, 1);
3132 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003133}
3134
3135
3136void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003137 ASSERT(args->length() >= 2);
3138
3139 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3140 for (int i = 0; i < arg_count + 1; i++) {
3141 VisitForStackValue(args->at(i));
3142 }
3143 VisitForAccumulatorValue(args->last()); // Function.
3144
3145 // InvokeFunction requires the function in a1. Move it in there.
3146 __ mov(a1, result_register());
3147 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003148 __ InvokeFunction(a1, count, CALL_FUNCTION,
3149 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003150 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3151 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003152}
3153
3154
3155void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003156 RegExpConstructResultStub stub;
3157 ASSERT(args->length() == 3);
3158 VisitForStackValue(args->at(0));
3159 VisitForStackValue(args->at(1));
3160 VisitForStackValue(args->at(2));
3161 __ CallStub(&stub);
3162 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003163}
3164
3165
3166void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003167 ASSERT(args->length() == 3);
3168 VisitForStackValue(args->at(0));
3169 VisitForStackValue(args->at(1));
3170 VisitForStackValue(args->at(2));
3171 Label done;
3172 Label slow_case;
3173 Register object = a0;
3174 Register index1 = a1;
3175 Register index2 = a2;
3176 Register elements = a3;
3177 Register scratch1 = t0;
3178 Register scratch2 = t1;
3179
3180 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3181 // Fetch the map and check if array is in fast case.
3182 // Check that object doesn't require security checks and
3183 // has no indexed interceptor.
3184 __ GetObjectType(object, scratch1, scratch2);
3185 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3186 // Map is now in scratch1.
3187
3188 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3189 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3190 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3191
3192 // Check the object's elements are in fast case and writable.
3193 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3194 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3195 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3196 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3197
3198 // Check that both indices are smis.
3199 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3200 __ lw(index2, MemOperand(sp, 0));
3201 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3202
3203 // Check that both indices are valid.
3204 Label not_hi;
3205 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3206 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3207 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3208 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3209 __ bind(&not_hi);
3210
3211 // Bring the address of the elements into index1 and index2.
3212 __ Addu(scratch1, elements,
3213 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3214 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3215 __ Addu(index1, scratch1, index1);
3216 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3217 __ Addu(index2, scratch1, index2);
3218
3219 // Swap elements.
3220 __ lw(scratch1, MemOperand(index1, 0));
3221 __ lw(scratch2, MemOperand(index2, 0));
3222 __ sw(scratch1, MemOperand(index2, 0));
3223 __ sw(scratch2, MemOperand(index1, 0));
3224
3225 Label new_space;
3226 __ InNewSpace(elements, scratch1, eq, &new_space);
3227 // Possible optimization: do a check that both values are Smis
3228 // (or them and test against Smi mask).
3229
3230 __ mov(scratch1, elements);
3231 __ RecordWriteHelper(elements, index1, scratch2);
3232 __ RecordWriteHelper(scratch1, index2, scratch2); // scratch1 holds elements.
3233
3234 __ bind(&new_space);
3235 // We are done. Drop elements from the stack, and return undefined.
3236 __ Drop(3);
3237 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3238 __ jmp(&done);
3239
3240 __ bind(&slow_case);
3241 __ CallRuntime(Runtime::kSwapElements, 3);
3242
3243 __ bind(&done);
3244 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003245}
3246
3247
3248void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003249 ASSERT_EQ(2, args->length());
3250
3251 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3252 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3253
3254 Handle<FixedArray> jsfunction_result_caches(
3255 isolate()->global_context()->jsfunction_result_caches());
3256 if (jsfunction_result_caches->length() <= cache_id) {
3257 __ Abort("Attempt to use undefined cache.");
3258 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3259 context()->Plug(v0);
3260 return;
3261 }
3262
3263 VisitForAccumulatorValue(args->at(1));
3264
3265 Register key = v0;
3266 Register cache = a1;
3267 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3268 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3269 __ lw(cache,
3270 ContextOperand(
3271 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3272 __ lw(cache,
3273 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3274
3275
3276 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003277 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003278 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3279 // a2 now holds finger offset as a smi.
3280 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3281 // a3 now points to the start of fixed array elements.
3282 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3283 __ addu(a3, a3, at);
3284 // a3 now points to key of indexed element of cache.
3285 __ lw(a2, MemOperand(a3));
3286 __ Branch(&not_found, ne, key, Operand(a2));
3287
3288 __ lw(v0, MemOperand(a3, kPointerSize));
3289 __ Branch(&done);
3290
3291 __ bind(&not_found);
3292 // Call runtime to perform the lookup.
3293 __ Push(cache, key);
3294 __ CallRuntime(Runtime::kGetFromCache, 2);
3295
3296 __ bind(&done);
3297 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003298}
3299
3300
3301void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003302 ASSERT_EQ(2, args->length());
3303
3304 Register right = v0;
3305 Register left = a1;
3306 Register tmp = a2;
3307 Register tmp2 = a3;
3308
3309 VisitForStackValue(args->at(0));
3310 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3311 __ pop(left);
3312
3313 Label done, fail, ok;
3314 __ Branch(&ok, eq, left, Operand(right));
3315 // Fail if either is a non-HeapObject.
3316 __ And(tmp, left, Operand(right));
3317 __ And(at, tmp, Operand(kSmiTagMask));
3318 __ Branch(&fail, eq, at, Operand(zero_reg));
3319 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3320 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3321 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3322 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3323 __ Branch(&fail, ne, tmp, Operand(tmp2));
3324 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3325 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3326 __ Branch(&ok, eq, tmp, Operand(tmp2));
3327 __ bind(&fail);
3328 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3329 __ jmp(&done);
3330 __ bind(&ok);
3331 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3332 __ bind(&done);
3333
3334 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003335}
3336
3337
3338void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003339 VisitForAccumulatorValue(args->at(0));
3340
3341 Label materialize_true, materialize_false;
3342 Label* if_true = NULL;
3343 Label* if_false = NULL;
3344 Label* fall_through = NULL;
3345 context()->PrepareTest(&materialize_true, &materialize_false,
3346 &if_true, &if_false, &fall_through);
3347
3348 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3349 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3350
3351 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3352 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3353
3354 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003355}
3356
3357
3358void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003359 ASSERT(args->length() == 1);
3360 VisitForAccumulatorValue(args->at(0));
3361
3362 if (FLAG_debug_code) {
3363 __ AbortIfNotString(v0);
3364 }
3365
3366 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3367 __ IndexFromHash(v0, v0);
3368
3369 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003370}
3371
3372
3373void FullCodeGenerator::EmitFastAsciiArrayJoin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003374 Label bailout, done, one_char_separator, long_separator,
3375 non_trivial_array, not_size_one_array, loop,
3376 empty_separator_loop, one_char_separator_loop,
3377 one_char_separator_loop_entry, long_separator_loop;
3378
3379 ASSERT(args->length() == 2);
3380 VisitForStackValue(args->at(1));
3381 VisitForAccumulatorValue(args->at(0));
3382
3383 // All aliases of the same register have disjoint lifetimes.
3384 Register array = v0;
3385 Register elements = no_reg; // Will be v0.
3386 Register result = no_reg; // Will be v0.
3387 Register separator = a1;
3388 Register array_length = a2;
3389 Register result_pos = no_reg; // Will be a2.
3390 Register string_length = a3;
3391 Register string = t0;
3392 Register element = t1;
3393 Register elements_end = t2;
3394 Register scratch1 = t3;
3395 Register scratch2 = t5;
3396 Register scratch3 = t4;
3397 Register scratch4 = v1;
3398
3399 // Separator operand is on the stack.
3400 __ pop(separator);
3401
3402 // Check that the array is a JSArray.
3403 __ JumpIfSmi(array, &bailout);
3404 __ GetObjectType(array, scratch1, scratch2);
3405 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3406
3407 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003408 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003409
3410 // If the array has length zero, return the empty string.
3411 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3412 __ SmiUntag(array_length);
3413 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3414 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3415 __ Branch(&done);
3416
3417 __ bind(&non_trivial_array);
3418
3419 // Get the FixedArray containing array's elements.
3420 elements = array;
3421 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3422 array = no_reg; // End of array's live range.
3423
3424 // Check that all array elements are sequential ASCII strings, and
3425 // accumulate the sum of their lengths, as a smi-encoded value.
3426 __ mov(string_length, zero_reg);
3427 __ Addu(element,
3428 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3429 __ sll(elements_end, array_length, kPointerSizeLog2);
3430 __ Addu(elements_end, element, elements_end);
3431 // Loop condition: while (element < elements_end).
3432 // Live values in registers:
3433 // elements: Fixed array of strings.
3434 // array_length: Length of the fixed array of strings (not smi)
3435 // separator: Separator string
3436 // string_length: Accumulated sum of string lengths (smi).
3437 // element: Current array element.
3438 // elements_end: Array end.
3439 if (FLAG_debug_code) {
3440 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3441 array_length, Operand(zero_reg));
3442 }
3443 __ bind(&loop);
3444 __ lw(string, MemOperand(element));
3445 __ Addu(element, element, kPointerSize);
3446 __ JumpIfSmi(string, &bailout);
3447 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3448 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3449 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3450 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3451 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3452 __ BranchOnOverflow(&bailout, scratch3);
3453 __ Branch(&loop, lt, element, Operand(elements_end));
3454
3455 // If array_length is 1, return elements[0], a string.
3456 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3457 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3458 __ Branch(&done);
3459
3460 __ bind(&not_size_one_array);
3461
3462 // Live values in registers:
3463 // separator: Separator string
3464 // array_length: Length of the array.
3465 // string_length: Sum of string lengths (smi).
3466 // elements: FixedArray of strings.
3467
3468 // Check that the separator is a flat ASCII string.
3469 __ JumpIfSmi(separator, &bailout);
3470 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3471 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3472 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3473
3474 // Add (separator length times array_length) - separator length to the
3475 // string_length to get the length of the result string. array_length is not
3476 // smi but the other values are, so the result is a smi.
3477 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3478 __ Subu(string_length, string_length, Operand(scratch1));
3479 __ Mult(array_length, scratch1);
3480 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3481 // zero.
3482 __ mfhi(scratch2);
3483 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3484 __ mflo(scratch2);
3485 __ And(scratch3, scratch2, Operand(0x80000000));
3486 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3487 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3488 __ BranchOnOverflow(&bailout, scratch3);
3489 __ SmiUntag(string_length);
3490
3491 // Get first element in the array to free up the elements register to be used
3492 // for the result.
3493 __ Addu(element,
3494 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3495 result = elements; // End of live range for elements.
3496 elements = no_reg;
3497 // Live values in registers:
3498 // element: First array element
3499 // separator: Separator string
3500 // string_length: Length of result string (not smi)
3501 // array_length: Length of the array.
3502 __ AllocateAsciiString(result,
3503 string_length,
3504 scratch1,
3505 scratch2,
3506 elements_end,
3507 &bailout);
3508 // Prepare for looping. Set up elements_end to end of the array. Set
3509 // result_pos to the position of the result where to write the first
3510 // character.
3511 __ sll(elements_end, array_length, kPointerSizeLog2);
3512 __ Addu(elements_end, element, elements_end);
3513 result_pos = array_length; // End of live range for array_length.
3514 array_length = no_reg;
3515 __ Addu(result_pos,
3516 result,
3517 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3518
3519 // Check the length of the separator.
3520 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3521 __ li(at, Operand(Smi::FromInt(1)));
3522 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3523 __ Branch(&long_separator, gt, scratch1, Operand(at));
3524
3525 // Empty separator case.
3526 __ bind(&empty_separator_loop);
3527 // Live values in registers:
3528 // result_pos: the position to which we are currently copying characters.
3529 // element: Current array element.
3530 // elements_end: Array end.
3531
3532 // Copy next array element to the result.
3533 __ lw(string, MemOperand(element));
3534 __ Addu(element, element, kPointerSize);
3535 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3536 __ SmiUntag(string_length);
3537 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3538 __ CopyBytes(string, result_pos, string_length, scratch1);
3539 // End while (element < elements_end).
3540 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3541 ASSERT(result.is(v0));
3542 __ Branch(&done);
3543
3544 // One-character separator case.
3545 __ bind(&one_char_separator);
3546 // Replace separator with its ascii character value.
3547 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3548 // Jump into the loop after the code that copies the separator, so the first
3549 // element is not preceded by a separator.
3550 __ jmp(&one_char_separator_loop_entry);
3551
3552 __ bind(&one_char_separator_loop);
3553 // Live values in registers:
3554 // result_pos: the position to which we are currently copying characters.
3555 // element: Current array element.
3556 // elements_end: Array end.
3557 // separator: Single separator ascii char (in lower byte).
3558
3559 // Copy the separator character to the result.
3560 __ sb(separator, MemOperand(result_pos));
3561 __ Addu(result_pos, result_pos, 1);
3562
3563 // Copy next array element to the result.
3564 __ bind(&one_char_separator_loop_entry);
3565 __ lw(string, MemOperand(element));
3566 __ Addu(element, element, kPointerSize);
3567 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3568 __ SmiUntag(string_length);
3569 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3570 __ CopyBytes(string, result_pos, string_length, scratch1);
3571 // End while (element < elements_end).
3572 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3573 ASSERT(result.is(v0));
3574 __ Branch(&done);
3575
3576 // Long separator case (separator is more than one character). Entry is at the
3577 // label long_separator below.
3578 __ bind(&long_separator_loop);
3579 // Live values in registers:
3580 // result_pos: the position to which we are currently copying characters.
3581 // element: Current array element.
3582 // elements_end: Array end.
3583 // separator: Separator string.
3584
3585 // Copy the separator to the result.
3586 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3587 __ SmiUntag(string_length);
3588 __ Addu(string,
3589 separator,
3590 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3591 __ CopyBytes(string, result_pos, string_length, scratch1);
3592
3593 __ bind(&long_separator);
3594 __ lw(string, MemOperand(element));
3595 __ Addu(element, element, kPointerSize);
3596 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3597 __ SmiUntag(string_length);
3598 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3599 __ CopyBytes(string, result_pos, string_length, scratch1);
3600 // End while (element < elements_end).
3601 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3602 ASSERT(result.is(v0));
3603 __ Branch(&done);
3604
3605 __ bind(&bailout);
3606 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3607 __ bind(&done);
3608 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003609}
3610
3611
ager@chromium.org5c838252010-02-19 08:53:10 +00003612void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003613 Handle<String> name = expr->name();
3614 if (name->length() > 0 && name->Get(0) == '_') {
3615 Comment cmnt(masm_, "[ InlineRuntimeCall");
3616 EmitInlineRuntimeCall(expr);
3617 return;
3618 }
3619
3620 Comment cmnt(masm_, "[ CallRuntime");
3621 ZoneList<Expression*>* args = expr->arguments();
3622
3623 if (expr->is_jsruntime()) {
3624 // Prepare for calling JS runtime function.
3625 __ lw(a0, GlobalObjectOperand());
3626 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3627 __ push(a0);
3628 }
3629
3630 // Push the arguments ("left-to-right").
3631 int arg_count = args->length();
3632 for (int i = 0; i < arg_count; i++) {
3633 VisitForStackValue(args->at(i));
3634 }
3635
3636 if (expr->is_jsruntime()) {
3637 // Call the JS runtime function.
3638 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003639 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003640 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00003641 isolate()->stub_cache()->ComputeCallInitialize(arg_count,
3642 NOT_IN_LOOP,
3643 mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003644 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003645 // Restore context register.
3646 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3647 } else {
3648 // Call the C runtime function.
3649 __ CallRuntime(expr->function(), arg_count);
3650 }
3651 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003652}
3653
3654
3655void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003656 switch (expr->op()) {
3657 case Token::DELETE: {
3658 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
3659 Property* prop = expr->expression()->AsProperty();
3660 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
3661
3662 if (prop != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003663 VisitForStackValue(prop->obj());
3664 VisitForStackValue(prop->key());
3665 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3666 __ push(a1);
3667 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3668 context()->Plug(v0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003669 } else if (var != NULL) {
3670 // Delete of an unqualified identifier is disallowed in strict mode
3671 // but "delete this" is.
3672 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
3673 if (var->is_global()) {
3674 __ lw(a2, GlobalObjectOperand());
3675 __ li(a1, Operand(var->name()));
3676 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3677 __ Push(a2, a1, a0);
3678 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3679 context()->Plug(v0);
3680 } else if (var->AsSlot() != NULL &&
3681 var->AsSlot()->type() != Slot::LOOKUP) {
3682 // Result of deleting non-global, non-dynamic variables is false.
3683 // The subexpression does not have side effects.
3684 context()->Plug(false);
3685 } else {
3686 // Non-global variable. Call the runtime to try to delete from the
3687 // context where the variable was introduced.
3688 __ push(context_register());
3689 __ li(a2, Operand(var->name()));
3690 __ push(a2);
3691 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3692 context()->Plug(v0);
3693 }
3694 } else {
3695 // Result of deleting non-property, non-variable reference is true.
3696 // The subexpression may have side effects.
3697 VisitForEffect(expr->expression());
3698 context()->Plug(true);
3699 }
3700 break;
3701 }
3702
3703 case Token::VOID: {
3704 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3705 VisitForEffect(expr->expression());
3706 context()->Plug(Heap::kUndefinedValueRootIndex);
3707 break;
3708 }
3709
3710 case Token::NOT: {
3711 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3712 if (context()->IsEffect()) {
3713 // Unary NOT has no side effects so it's only necessary to visit the
3714 // subexpression. Match the optimizing compiler by not branching.
3715 VisitForEffect(expr->expression());
3716 } else {
3717 Label materialize_true, materialize_false;
3718 Label* if_true = NULL;
3719 Label* if_false = NULL;
3720 Label* fall_through = NULL;
3721
3722 // Notice that the labels are swapped.
3723 context()->PrepareTest(&materialize_true, &materialize_false,
3724 &if_false, &if_true, &fall_through);
3725 if (context()->IsTest()) ForwardBailoutToChild(expr);
3726 VisitForControl(expr->expression(), if_true, if_false, fall_through);
3727 context()->Plug(if_false, if_true); // Labels swapped.
3728 }
3729 break;
3730 }
3731
3732 case Token::TYPEOF: {
3733 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3734 { StackValueContext context(this);
3735 VisitForTypeofValue(expr->expression());
3736 }
3737 __ CallRuntime(Runtime::kTypeof, 1);
3738 context()->Plug(v0);
3739 break;
3740 }
3741
3742 case Token::ADD: {
3743 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3744 VisitForAccumulatorValue(expr->expression());
3745 Label no_conversion;
3746 __ JumpIfSmi(result_register(), &no_conversion);
3747 __ mov(a0, result_register());
3748 ToNumberStub convert_stub;
3749 __ CallStub(&convert_stub);
3750 __ bind(&no_conversion);
3751 context()->Plug(result_register());
3752 break;
3753 }
3754
3755 case Token::SUB:
3756 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3757 break;
3758
3759 case Token::BIT_NOT:
3760 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3761 break;
3762
3763 default:
3764 UNREACHABLE();
3765 }
3766}
3767
3768
3769void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3770 const char* comment) {
3771 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3772 Comment cmt(masm_, comment);
3773 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3774 UnaryOverwriteMode overwrite =
3775 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003776 UnaryOpStub stub(expr->op(), overwrite);
3777 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003778 VisitForAccumulatorValue(expr->expression());
3779 SetSourcePosition(expr->position());
3780 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003781 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003782 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003783}
3784
3785
3786void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003787 Comment cmnt(masm_, "[ CountOperation");
3788 SetSourcePosition(expr->position());
3789
3790 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3791 // as the left-hand side.
3792 if (!expr->expression()->IsValidLeftHandSide()) {
3793 VisitForEffect(expr->expression());
3794 return;
3795 }
3796
3797 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003798 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003799 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3800 LhsKind assign_type = VARIABLE;
3801 Property* prop = expr->expression()->AsProperty();
3802 // In case of a property we use the uninitialized expression context
3803 // of the key to detect a named property.
3804 if (prop != NULL) {
3805 assign_type =
3806 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3807 }
3808
3809 // Evaluate expression and get value.
3810 if (assign_type == VARIABLE) {
3811 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3812 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003813 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003814 } else {
3815 // Reserve space for result of postfix operation.
3816 if (expr->is_postfix() && !context()->IsEffect()) {
3817 __ li(at, Operand(Smi::FromInt(0)));
3818 __ push(at);
3819 }
3820 if (assign_type == NAMED_PROPERTY) {
3821 // Put the object both on the stack and in the accumulator.
3822 VisitForAccumulatorValue(prop->obj());
3823 __ push(v0);
3824 EmitNamedPropertyLoad(prop);
3825 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003826 VisitForStackValue(prop->obj());
3827 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003828 __ lw(a1, MemOperand(sp, 0));
3829 __ push(v0);
3830 EmitKeyedPropertyLoad(prop);
3831 }
3832 }
3833
3834 // We need a second deoptimization point after loading the value
3835 // in case evaluating the property load my have a side effect.
3836 if (assign_type == VARIABLE) {
3837 PrepareForBailout(expr->expression(), TOS_REG);
3838 } else {
3839 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3840 }
3841
3842 // Call ToNumber only if operand is not a smi.
3843 Label no_conversion;
3844 __ JumpIfSmi(v0, &no_conversion);
3845 __ mov(a0, v0);
3846 ToNumberStub convert_stub;
3847 __ CallStub(&convert_stub);
3848 __ bind(&no_conversion);
3849
3850 // Save result for postfix expressions.
3851 if (expr->is_postfix()) {
3852 if (!context()->IsEffect()) {
3853 // Save the result on the stack. If we have a named or keyed property
3854 // we store the result under the receiver that is currently on top
3855 // of the stack.
3856 switch (assign_type) {
3857 case VARIABLE:
3858 __ push(v0);
3859 break;
3860 case NAMED_PROPERTY:
3861 __ sw(v0, MemOperand(sp, kPointerSize));
3862 break;
3863 case KEYED_PROPERTY:
3864 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3865 break;
3866 }
3867 }
3868 }
3869 __ mov(a0, result_register());
3870
3871 // Inline smi case if we are in a loop.
3872 Label stub_call, done;
3873 JumpPatchSite patch_site(masm_);
3874
3875 int count_value = expr->op() == Token::INC ? 1 : -1;
3876 __ li(a1, Operand(Smi::FromInt(count_value)));
3877
3878 if (ShouldInlineSmiCase(expr->op())) {
3879 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
3880 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
3881
3882 // We could eliminate this smi check if we split the code at
3883 // the first smi check before calling ToNumber.
3884 patch_site.EmitJumpIfSmi(v0, &done);
3885 __ bind(&stub_call);
3886 }
3887
3888 // Record position before stub call.
3889 SetSourcePosition(expr->position());
3890
danno@chromium.org40cb8782011-05-25 07:58:50 +00003891 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003892 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003893 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003894 __ bind(&done);
3895
3896 // Store the value returned in v0.
3897 switch (assign_type) {
3898 case VARIABLE:
3899 if (expr->is_postfix()) {
3900 { EffectContext context(this);
3901 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3902 Token::ASSIGN);
3903 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3904 context.Plug(v0);
3905 }
3906 // For all contexts except EffectConstant we have the result on
3907 // top of the stack.
3908 if (!context()->IsEffect()) {
3909 context()->PlugTOS();
3910 }
3911 } else {
3912 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3913 Token::ASSIGN);
3914 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3915 context()->Plug(v0);
3916 }
3917 break;
3918 case NAMED_PROPERTY: {
3919 __ mov(a0, result_register()); // Value.
3920 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
3921 __ pop(a1); // Receiver.
3922 Handle<Code> ic = is_strict_mode()
3923 ? isolate()->builtins()->StoreIC_Initialize_Strict()
3924 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003925 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003926 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3927 if (expr->is_postfix()) {
3928 if (!context()->IsEffect()) {
3929 context()->PlugTOS();
3930 }
3931 } else {
3932 context()->Plug(v0);
3933 }
3934 break;
3935 }
3936 case KEYED_PROPERTY: {
3937 __ mov(a0, result_register()); // Value.
3938 __ pop(a1); // Key.
3939 __ pop(a2); // Receiver.
3940 Handle<Code> ic = is_strict_mode()
3941 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
3942 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003943 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003944 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3945 if (expr->is_postfix()) {
3946 if (!context()->IsEffect()) {
3947 context()->PlugTOS();
3948 }
3949 } else {
3950 context()->Plug(v0);
3951 }
3952 break;
3953 }
3954 }
ager@chromium.org5c838252010-02-19 08:53:10 +00003955}
3956
3957
lrn@chromium.org7516f052011-03-30 08:52:27 +00003958void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003959 VariableProxy* proxy = expr->AsVariableProxy();
3960 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
3961 Comment cmnt(masm_, "Global variable");
3962 __ lw(a0, GlobalObjectOperand());
3963 __ li(a2, Operand(proxy->name()));
3964 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
3965 // Use a regular load, not a contextual load, to avoid a reference
3966 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003967 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003968 PrepareForBailout(expr, TOS_REG);
3969 context()->Plug(v0);
3970 } else if (proxy != NULL &&
3971 proxy->var()->AsSlot() != NULL &&
3972 proxy->var()->AsSlot()->type() == Slot::LOOKUP) {
3973 Label done, slow;
3974
3975 // Generate code for loading from variables potentially shadowed
3976 // by eval-introduced variables.
3977 Slot* slot = proxy->var()->AsSlot();
3978 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
3979
3980 __ bind(&slow);
3981 __ li(a0, Operand(proxy->name()));
3982 __ Push(cp, a0);
3983 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
3984 PrepareForBailout(expr, TOS_REG);
3985 __ bind(&done);
3986
3987 context()->Plug(v0);
3988 } else {
3989 // This expression cannot throw a reference error at the top level.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003990 VisitInCurrentContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003991 }
ager@chromium.org5c838252010-02-19 08:53:10 +00003992}
3993
ager@chromium.org04921a82011-06-27 13:21:41 +00003994void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
3995 Handle<String> check,
3996 Label* if_true,
3997 Label* if_false,
3998 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003999 { AccumulatorValueContext context(this);
ager@chromium.org04921a82011-06-27 13:21:41 +00004000 VisitForTypeofValue(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004001 }
4002 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4003
4004 if (check->Equals(isolate()->heap()->number_symbol())) {
4005 __ JumpIfSmi(v0, if_true);
4006 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4007 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4008 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4009 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4010 __ JumpIfSmi(v0, if_false);
4011 // Check for undetectable objects => false.
4012 __ GetObjectType(v0, v0, a1);
4013 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4014 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4015 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4016 Split(eq, a1, Operand(zero_reg),
4017 if_true, if_false, fall_through);
4018 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4019 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4020 __ Branch(if_true, eq, v0, Operand(at));
4021 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4022 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004023 } else if (FLAG_harmony_typeof &&
4024 check->Equals(isolate()->heap()->null_symbol())) {
4025 __ LoadRoot(at, Heap::kNullValueRootIndex);
4026 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004027 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4028 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4029 __ Branch(if_true, eq, v0, Operand(at));
4030 __ JumpIfSmi(v0, if_false);
4031 // Check for undetectable objects => true.
4032 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4033 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4034 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4035 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4036 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4037 __ JumpIfSmi(v0, if_false);
4038 __ GetObjectType(v0, a1, v0); // Leave map in a1.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004039 Split(ge, v0, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004040 if_true, if_false, fall_through);
4041
4042 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4043 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004044 if (!FLAG_harmony_typeof) {
4045 __ LoadRoot(at, Heap::kNullValueRootIndex);
4046 __ Branch(if_true, eq, v0, Operand(at));
4047 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004048 // Check for JS objects => true.
4049 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004050 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004051 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004052 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004053 // Check for undetectable objects => false.
4054 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4055 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4056 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4057 } else {
4058 if (if_false != fall_through) __ jmp(if_false);
4059 }
ager@chromium.org04921a82011-06-27 13:21:41 +00004060}
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004061
ager@chromium.org04921a82011-06-27 13:21:41 +00004062
4063void FullCodeGenerator::EmitLiteralCompareUndefined(Expression* expr,
4064 Label* if_true,
4065 Label* if_false,
4066 Label* fall_through) {
4067 VisitForAccumulatorValue(expr);
4068 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4069
4070 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4071 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004072}
4073
4074
ager@chromium.org5c838252010-02-19 08:53:10 +00004075void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004076 Comment cmnt(masm_, "[ CompareOperation");
4077 SetSourcePosition(expr->position());
4078
4079 // Always perform the comparison for its control flow. Pack the result
4080 // into the expression's context after the comparison is performed.
4081
4082 Label materialize_true, materialize_false;
4083 Label* if_true = NULL;
4084 Label* if_false = NULL;
4085 Label* fall_through = NULL;
4086 context()->PrepareTest(&materialize_true, &materialize_false,
4087 &if_true, &if_false, &fall_through);
4088
4089 // First we try a fast inlined version of the compare when one of
4090 // the operands is a literal.
ager@chromium.org04921a82011-06-27 13:21:41 +00004091 if (TryLiteralCompare(expr, if_true, if_false, fall_through)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004092 context()->Plug(if_true, if_false);
4093 return;
4094 }
4095
ager@chromium.org04921a82011-06-27 13:21:41 +00004096 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004097 VisitForStackValue(expr->left());
4098 switch (op) {
4099 case Token::IN:
4100 VisitForStackValue(expr->right());
4101 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
4102 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
4103 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4104 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4105 break;
4106
4107 case Token::INSTANCEOF: {
4108 VisitForStackValue(expr->right());
4109 InstanceofStub stub(InstanceofStub::kNoFlags);
4110 __ CallStub(&stub);
4111 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4112 // The stub returns 0 for true.
4113 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4114 break;
4115 }
4116
4117 default: {
4118 VisitForAccumulatorValue(expr->right());
4119 Condition cc = eq;
4120 bool strict = false;
4121 switch (op) {
4122 case Token::EQ_STRICT:
4123 strict = true;
4124 // Fall through.
4125 case Token::EQ:
4126 cc = eq;
4127 __ mov(a0, result_register());
4128 __ pop(a1);
4129 break;
4130 case Token::LT:
4131 cc = lt;
4132 __ mov(a0, result_register());
4133 __ pop(a1);
4134 break;
4135 case Token::GT:
4136 // Reverse left and right sides to obtain ECMA-262 conversion order.
4137 cc = lt;
4138 __ mov(a1, result_register());
4139 __ pop(a0);
4140 break;
4141 case Token::LTE:
4142 // Reverse left and right sides to obtain ECMA-262 conversion order.
4143 cc = ge;
4144 __ mov(a1, result_register());
4145 __ pop(a0);
4146 break;
4147 case Token::GTE:
4148 cc = ge;
4149 __ mov(a0, result_register());
4150 __ pop(a1);
4151 break;
4152 case Token::IN:
4153 case Token::INSTANCEOF:
4154 default:
4155 UNREACHABLE();
4156 }
4157
4158 bool inline_smi_code = ShouldInlineSmiCase(op);
4159 JumpPatchSite patch_site(masm_);
4160 if (inline_smi_code) {
4161 Label slow_case;
4162 __ Or(a2, a0, Operand(a1));
4163 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4164 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4165 __ bind(&slow_case);
4166 }
4167 // Record position and call the compare IC.
4168 SetSourcePosition(expr->position());
4169 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004170 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004171 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004172 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4173 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4174 }
4175 }
4176
4177 // Convert the result of the comparison into one expected for this
4178 // expression's context.
4179 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004180}
4181
4182
lrn@chromium.org7516f052011-03-30 08:52:27 +00004183void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004184 Comment cmnt(masm_, "[ CompareToNull");
4185 Label materialize_true, materialize_false;
4186 Label* if_true = NULL;
4187 Label* if_false = NULL;
4188 Label* fall_through = NULL;
4189 context()->PrepareTest(&materialize_true, &materialize_false,
4190 &if_true, &if_false, &fall_through);
4191
4192 VisitForAccumulatorValue(expr->expression());
4193 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4194 __ mov(a0, result_register());
4195 __ LoadRoot(a1, Heap::kNullValueRootIndex);
4196 if (expr->is_strict()) {
4197 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4198 } else {
4199 __ Branch(if_true, eq, a0, Operand(a1));
4200 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
4201 __ Branch(if_true, eq, a0, Operand(a1));
4202 __ And(at, a0, Operand(kSmiTagMask));
4203 __ Branch(if_false, eq, at, Operand(zero_reg));
4204 // It can be an undetectable object.
4205 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4206 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4207 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4208 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4209 }
4210 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004211}
4212
4213
ager@chromium.org5c838252010-02-19 08:53:10 +00004214void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004215 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4216 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004217}
4218
4219
lrn@chromium.org7516f052011-03-30 08:52:27 +00004220Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004221 return v0;
4222}
ager@chromium.org5c838252010-02-19 08:53:10 +00004223
4224
lrn@chromium.org7516f052011-03-30 08:52:27 +00004225Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004226 return cp;
4227}
4228
4229
ager@chromium.org5c838252010-02-19 08:53:10 +00004230void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004231 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4232 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004233}
4234
4235
4236void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004237 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004238}
4239
4240
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004241void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4242 Scope* declaration_scope = scope()->DeclarationScope();
4243 if (declaration_scope->is_global_scope()) {
4244 // Contexts nested in the global context have a canonical empty function
4245 // as their closure, not the anonymous closure containing the global
4246 // code. Pass a smi sentinel and let the runtime look up the empty
4247 // function.
4248 __ li(at, Operand(Smi::FromInt(0)));
4249 } else if (declaration_scope->is_eval_scope()) {
4250 // Contexts created by a call to eval have the same closure as the
4251 // context calling eval, not the anonymous closure containing the eval
4252 // code. Fetch it from the context.
4253 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4254 } else {
4255 ASSERT(declaration_scope->is_function_scope());
4256 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4257 }
4258 __ push(at);
4259}
4260
4261
ager@chromium.org5c838252010-02-19 08:53:10 +00004262// ----------------------------------------------------------------------------
4263// Non-local control flow support.
4264
4265void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004266 ASSERT(!result_register().is(a1));
4267 // Store result register while executing finally block.
4268 __ push(result_register());
4269 // Cook return address in link register to stack (smi encoded Code* delta).
4270 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4271 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004272 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004273 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4274 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004275}
4276
4277
4278void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004279 ASSERT(!result_register().is(a1));
4280 // Restore result register from stack.
4281 __ pop(a1);
4282 // Uncook return address and return.
4283 __ pop(result_register());
4284 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4285 __ sra(a1, a1, 1); // Un-smi-tag value.
4286 __ Addu(at, a1, Operand(masm_->CodeObject()));
4287 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004288}
4289
4290
4291#undef __
4292
4293} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004294
4295#endif // V8_TARGET_ARCH_MIPS