blob: 385e57ae663a7dedc389148c768553a72d42fb9b [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++) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000203 Slot* slot = scope()->parameter(i)->rewrite();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000204 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
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000255 Move(arguments->rewrite(), v0, a1, a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000256 }
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:
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000636 case Slot::GLOBAL:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000637 UNREACHABLE();
638 }
639 UNREACHABLE();
640 return MemOperand(v0, 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000641}
642
643
644void FullCodeGenerator::Move(Register destination, Slot* source) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000645 // Use destination as scratch.
646 MemOperand slot_operand = EmitSlotSearch(source, destination);
647 __ lw(destination, slot_operand);
ager@chromium.org5c838252010-02-19 08:53:10 +0000648}
649
650
lrn@chromium.org7516f052011-03-30 08:52:27 +0000651void FullCodeGenerator::PrepareForBailoutBeforeSplit(State state,
652 bool should_normalize,
653 Label* if_true,
654 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000655 // Only prepare for bailouts before splits if we're in a test
656 // context. Otherwise, we let the Visit function deal with the
657 // preparation to avoid preparing with the same AST id twice.
658 if (!context()->IsTest() || !info_->IsOptimizable()) return;
659
660 Label skip;
661 if (should_normalize) __ Branch(&skip);
662
663 ForwardBailoutStack* current = forward_bailout_stack_;
664 while (current != NULL) {
665 PrepareForBailout(current->expr(), state);
666 current = current->parent();
667 }
668
669 if (should_normalize) {
670 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
671 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
672 __ bind(&skip);
673 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000674}
675
676
ager@chromium.org5c838252010-02-19 08:53:10 +0000677void FullCodeGenerator::Move(Slot* dst,
678 Register src,
679 Register scratch1,
680 Register scratch2) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000681 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
682 ASSERT(!scratch1.is(src) && !scratch2.is(src));
683 MemOperand location = EmitSlotSearch(dst, scratch1);
684 __ sw(src, location);
685 // Emit the write barrier code if the location is in the heap.
686 if (dst->type() == Slot::CONTEXT) {
687 __ RecordWrite(scratch1,
688 Operand(Context::SlotOffset(dst->index())),
689 scratch2,
690 src);
691 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000692}
693
694
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000695void FullCodeGenerator::EmitDeclaration(VariableProxy* proxy,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000696 Variable::Mode mode,
697 FunctionLiteral* function) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000698 Comment cmnt(masm_, "[ Declaration");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000699 Variable* variable = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000700 ASSERT(variable != NULL); // Must have been resolved.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000701 Slot* slot = variable->rewrite();
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000702 ASSERT(slot != NULL);
703 switch (slot->type()) {
704 case Slot::PARAMETER:
705 case Slot::LOCAL:
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000706 if (function != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000707 VisitForAccumulatorValue(function);
708 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000709 } else if (mode == Variable::CONST || mode == Variable::LET) {
710 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
711 __ sw(t0, MemOperand(fp, SlotOffset(slot)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000712 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000713 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000714
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000715 case Slot::CONTEXT:
716 // We bypass the general EmitSlotSearch because we know more about
717 // this specific context.
718
719 // The variable in the decl always resides in the current function
720 // context.
721 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
722 if (FLAG_debug_code) {
723 // Check that we're not inside a with or catch context.
724 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
725 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
726 __ Check(ne, "Declaration in with context.",
727 a1, Operand(t0));
728 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
729 __ Check(ne, "Declaration in catch context.",
730 a1, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000731 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000732 if (function != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000733 VisitForAccumulatorValue(function);
734 __ sw(result_register(), ContextOperand(cp, slot->index()));
735 int offset = Context::SlotOffset(slot->index());
736 // We know that we have written a function, which is not a smi.
737 __ mov(a1, cp);
738 __ RecordWrite(a1, Operand(offset), a2, result_register());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000739 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000740 } else if (mode == Variable::CONST || mode == Variable::LET) {
741 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
742 __ sw(at, ContextOperand(cp, slot->index()));
743 // No write barrier since the_hole_value is in old space.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000744 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000745 }
746 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000747
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000748 case Slot::LOOKUP: {
749 __ li(a2, Operand(variable->name()));
750 // Declaration nodes are always introduced in one of two modes.
751 ASSERT(mode == Variable::VAR ||
752 mode == Variable::CONST ||
753 mode == Variable::LET);
754 PropertyAttributes attr = (mode == Variable::CONST) ? READ_ONLY : NONE;
755 __ li(a1, Operand(Smi::FromInt(attr)));
756 // Push initial value, if any.
757 // Note: For variables we must not push an initial value (such as
758 // 'undefined') because we may have a (legal) redeclaration and we
759 // must not destroy the current value.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000760 if (function != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000761 __ Push(cp, a2, a1);
762 // Push initial value for function declaration.
763 VisitForStackValue(function);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000764 } else if (mode == Variable::CONST || mode == Variable::LET) {
765 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
766 __ Push(cp, a2, a1, a0);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000767 } else {
768 ASSERT(Smi::FromInt(0) == 0);
769 // No initial value!
770 __ mov(a0, zero_reg); // Operand(Smi::FromInt(0)));
771 __ Push(cp, a2, a1, a0);
772 }
773 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
774 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000775 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000776
777 case Slot::GLOBAL:
778 UNREACHABLE();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000779 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000780}
781
782
ager@chromium.org5c838252010-02-19 08:53:10 +0000783void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000784 EmitDeclaration(decl->proxy(), decl->mode(), decl->fun());
ager@chromium.org5c838252010-02-19 08:53:10 +0000785}
786
787
788void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000789 // Call the runtime to declare the globals.
790 // The context is the first argument.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000791 __ li(a1, Operand(pairs));
792 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
793 __ Push(cp, a1, a0);
794 __ CallRuntime(Runtime::kDeclareGlobals, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000795 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000796}
797
798
lrn@chromium.org7516f052011-03-30 08:52:27 +0000799void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000800 Comment cmnt(masm_, "[ SwitchStatement");
801 Breakable nested_statement(this, stmt);
802 SetStatementPosition(stmt);
803
804 // Keep the switch value on the stack until a case matches.
805 VisitForStackValue(stmt->tag());
806 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
807
808 ZoneList<CaseClause*>* clauses = stmt->cases();
809 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
810
811 Label next_test; // Recycled for each test.
812 // Compile all the tests with branches to their bodies.
813 for (int i = 0; i < clauses->length(); i++) {
814 CaseClause* clause = clauses->at(i);
815 clause->body_target()->Unuse();
816
817 // The default is not a test, but remember it as final fall through.
818 if (clause->is_default()) {
819 default_clause = clause;
820 continue;
821 }
822
823 Comment cmnt(masm_, "[ Case comparison");
824 __ bind(&next_test);
825 next_test.Unuse();
826
827 // Compile the label expression.
828 VisitForAccumulatorValue(clause->label());
829 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
830
831 // Perform the comparison as if via '==='.
832 __ lw(a1, MemOperand(sp, 0)); // Switch value.
833 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
834 JumpPatchSite patch_site(masm_);
835 if (inline_smi_code) {
836 Label slow_case;
837 __ or_(a2, a1, a0);
838 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
839
840 __ Branch(&next_test, ne, a1, Operand(a0));
841 __ Drop(1); // Switch value is no longer needed.
842 __ Branch(clause->body_target());
843
844 __ bind(&slow_case);
845 }
846
847 // Record position before stub call for type feedback.
848 SetSourcePosition(clause->position());
849 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000850 __ Call(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000851 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +0000852
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000853 __ Branch(&next_test, ne, v0, Operand(zero_reg));
854 __ Drop(1); // Switch value is no longer needed.
855 __ Branch(clause->body_target());
856 }
857
858 // Discard the test value and jump to the default if present, otherwise to
859 // the end of the statement.
860 __ bind(&next_test);
861 __ Drop(1); // Switch value is no longer needed.
862 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000863 __ Branch(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000864 } else {
865 __ Branch(default_clause->body_target());
866 }
867
868 // Compile all the case bodies.
869 for (int i = 0; i < clauses->length(); i++) {
870 Comment cmnt(masm_, "[ Case body");
871 CaseClause* clause = clauses->at(i);
872 __ bind(clause->body_target());
873 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
874 VisitStatements(clause->statements());
875 }
876
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000877 __ bind(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000878 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000879}
880
881
882void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000883 Comment cmnt(masm_, "[ ForInStatement");
884 SetStatementPosition(stmt);
885
886 Label loop, exit;
887 ForIn loop_statement(this, stmt);
888 increment_loop_depth();
889
890 // Get the object to enumerate over. Both SpiderMonkey and JSC
891 // ignore null and undefined in contrast to the specification; see
892 // ECMA-262 section 12.6.4.
893 VisitForAccumulatorValue(stmt->enumerable());
894 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
895 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
896 __ Branch(&exit, eq, a0, Operand(at));
897 Register null_value = t1;
898 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
899 __ Branch(&exit, eq, a0, Operand(null_value));
900
901 // Convert the object to a JS object.
902 Label convert, done_convert;
903 __ JumpIfSmi(a0, &convert);
904 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000905 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000906 __ bind(&convert);
907 __ push(a0);
908 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
909 __ mov(a0, v0);
910 __ bind(&done_convert);
911 __ push(a0);
912
913 // Check cache validity in generated code. This is a fast case for
914 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
915 // guarantee cache validity, call the runtime system to check cache
916 // validity or get the property names in a fixed array.
917 Label next, call_runtime;
918 // Preload a couple of values used in the loop.
919 Register empty_fixed_array_value = t2;
920 __ LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
921 Register empty_descriptor_array_value = t3;
922 __ LoadRoot(empty_descriptor_array_value,
923 Heap::kEmptyDescriptorArrayRootIndex);
924 __ mov(a1, a0);
925 __ bind(&next);
926
927 // Check that there are no elements. Register a1 contains the
928 // current JS object we've reached through the prototype chain.
929 __ lw(a2, FieldMemOperand(a1, JSObject::kElementsOffset));
930 __ Branch(&call_runtime, ne, a2, Operand(empty_fixed_array_value));
931
932 // Check that instance descriptors are not empty so that we can
933 // check for an enum cache. Leave the map in a2 for the subsequent
934 // prototype load.
935 __ lw(a2, FieldMemOperand(a1, HeapObject::kMapOffset));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000936 __ lw(a3, FieldMemOperand(a2, Map::kInstanceDescriptorsOrBitField3Offset));
937 __ JumpIfSmi(a3, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000938
939 // Check that there is an enum cache in the non-empty instance
940 // descriptors (a3). This is the case if the next enumeration
941 // index field does not contain a smi.
942 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumerationIndexOffset));
943 __ JumpIfSmi(a3, &call_runtime);
944
945 // For all objects but the receiver, check that the cache is empty.
946 Label check_prototype;
947 __ Branch(&check_prototype, eq, a1, Operand(a0));
948 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumCacheBridgeCacheOffset));
949 __ Branch(&call_runtime, ne, a3, Operand(empty_fixed_array_value));
950
951 // Load the prototype from the map and loop if non-null.
952 __ bind(&check_prototype);
953 __ lw(a1, FieldMemOperand(a2, Map::kPrototypeOffset));
954 __ Branch(&next, ne, a1, Operand(null_value));
955
956 // The enum cache is valid. Load the map of the object being
957 // iterated over and use the cache for the iteration.
958 Label use_cache;
959 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
960 __ Branch(&use_cache);
961
962 // Get the set of properties to enumerate.
963 __ bind(&call_runtime);
964 __ push(a0); // Duplicate the enumerable object on the stack.
965 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
966
967 // If we got a map from the runtime call, we can do a fast
968 // modification check. Otherwise, we got a fixed array, and we have
969 // to do a slow check.
970 Label fixed_array;
971 __ mov(a2, v0);
972 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
973 __ LoadRoot(at, Heap::kMetaMapRootIndex);
974 __ Branch(&fixed_array, ne, a1, Operand(at));
975
976 // We got a map in register v0. Get the enumeration cache from it.
977 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000978 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000979 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
980 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
981
982 // Setup the four remaining stack slots.
983 __ push(v0); // Map.
984 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
985 __ li(a0, Operand(Smi::FromInt(0)));
986 // Push enumeration cache, enumeration cache length (as smi) and zero.
987 __ Push(a2, a1, a0);
988 __ jmp(&loop);
989
990 // We got a fixed array in register v0. Iterate through that.
991 __ bind(&fixed_array);
992 __ li(a1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
993 __ Push(a1, v0);
994 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
995 __ li(a0, Operand(Smi::FromInt(0)));
996 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
997
998 // Generate code for doing the condition check.
999 __ bind(&loop);
1000 // Load the current count to a0, load the length to a1.
1001 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1002 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001003 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001004
1005 // Get the current entry of the array into register a3.
1006 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1007 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1008 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1009 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1010 __ lw(a3, MemOperand(t0)); // Current entry.
1011
1012 // Get the expected map from the stack or a zero map in the
1013 // permanent slow case into register a2.
1014 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1015
1016 // Check if the expected map still matches that of the enumerable.
1017 // If not, we have to filter the key.
1018 Label update_each;
1019 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1020 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1021 __ Branch(&update_each, eq, t0, Operand(a2));
1022
1023 // Convert the entry to a string or (smi) 0 if it isn't a property
1024 // any more. If the property has been removed while iterating, we
1025 // just skip it.
1026 __ push(a1); // Enumerable.
1027 __ push(a3); // Current entry.
1028 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1029 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001030 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001031
1032 // Update the 'each' property or variable from the possibly filtered
1033 // entry in register a3.
1034 __ bind(&update_each);
1035 __ mov(result_register(), a3);
1036 // Perform the assignment as if via '='.
1037 { EffectContext context(this);
1038 EmitAssignment(stmt->each(), stmt->AssignmentId());
1039 }
1040
1041 // Generate code for the body of the loop.
1042 Visit(stmt->body());
1043
1044 // Generate code for the going to the next element by incrementing
1045 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001046 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001047 __ pop(a0);
1048 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1049 __ push(a0);
1050
1051 EmitStackCheck(stmt);
1052 __ Branch(&loop);
1053
1054 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001055 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001056 __ Drop(5);
1057
1058 // Exit and decrement the loop depth.
1059 __ bind(&exit);
1060 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001061}
1062
1063
1064void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1065 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001066 // Use the fast case closure allocation code that allocates in new
1067 // space for nested functions that don't need literals cloning. If
1068 // we're running with the --always-opt or the --prepare-always-opt
1069 // flag, we need to use the runtime function so that the new function
1070 // we are creating here gets a chance to have its code optimized and
1071 // doesn't just get a copy of the existing unoptimized code.
1072 if (!FLAG_always_opt &&
1073 !FLAG_prepare_always_opt &&
1074 !pretenure &&
1075 scope()->is_function_scope() &&
1076 info->num_literals() == 0) {
1077 FastNewClosureStub stub(info->strict_mode() ? kStrictMode : kNonStrictMode);
1078 __ li(a0, Operand(info));
1079 __ push(a0);
1080 __ CallStub(&stub);
1081 } else {
1082 __ li(a0, Operand(info));
1083 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1084 : Heap::kFalseValueRootIndex);
1085 __ Push(cp, a0, a1);
1086 __ CallRuntime(Runtime::kNewClosure, 3);
1087 }
1088 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001089}
1090
1091
1092void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001093 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001094 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001095}
1096
1097
1098void FullCodeGenerator::EmitLoadGlobalSlotCheckExtensions(
1099 Slot* slot,
1100 TypeofState typeof_state,
1101 Label* slow) {
1102 Register current = cp;
1103 Register next = a1;
1104 Register temp = a2;
1105
1106 Scope* s = scope();
1107 while (s != NULL) {
1108 if (s->num_heap_slots() > 0) {
1109 if (s->calls_eval()) {
1110 // Check that extension is NULL.
1111 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1112 __ Branch(slow, ne, temp, Operand(zero_reg));
1113 }
1114 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001115 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001116 // Walk the rest of the chain without clobbering cp.
1117 current = next;
1118 }
1119 // If no outer scope calls eval, we do not need to check more
1120 // context extensions.
1121 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
1122 s = s->outer_scope();
1123 }
1124
1125 if (s->is_eval_scope()) {
1126 Label loop, fast;
1127 if (!current.is(next)) {
1128 __ Move(next, current);
1129 }
1130 __ bind(&loop);
1131 // Terminate at global context.
1132 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1133 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1134 __ Branch(&fast, eq, temp, Operand(t0));
1135 // Check that extension is NULL.
1136 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1137 __ Branch(slow, ne, temp, Operand(zero_reg));
1138 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001139 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001140 __ Branch(&loop);
1141 __ bind(&fast);
1142 }
1143
1144 __ lw(a0, GlobalObjectOperand());
1145 __ li(a2, Operand(slot->var()->name()));
1146 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1147 ? RelocInfo::CODE_TARGET
1148 : RelocInfo::CODE_TARGET_CONTEXT;
1149 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001150 __ Call(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001151}
1152
1153
lrn@chromium.org7516f052011-03-30 08:52:27 +00001154MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(
1155 Slot* slot,
1156 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001157 ASSERT(slot->type() == Slot::CONTEXT);
1158 Register context = cp;
1159 Register next = a3;
1160 Register temp = t0;
1161
1162 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
1163 if (s->num_heap_slots() > 0) {
1164 if (s->calls_eval()) {
1165 // Check that extension is NULL.
1166 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1167 __ Branch(slow, ne, temp, Operand(zero_reg));
1168 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001169 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001170 // Walk the rest of the chain without clobbering cp.
1171 context = next;
1172 }
1173 }
1174 // Check that last extension is NULL.
1175 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1176 __ Branch(slow, ne, temp, Operand(zero_reg));
1177
1178 // This function is used only for loads, not stores, so it's safe to
1179 // return an cp-based operand (the write barrier cannot be allowed to
1180 // destroy the cp register).
1181 return ContextOperand(context, slot->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001182}
1183
1184
1185void FullCodeGenerator::EmitDynamicLoadFromSlotFastCase(
1186 Slot* slot,
1187 TypeofState typeof_state,
1188 Label* slow,
1189 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001190 // Generate fast-case code for variables that might be shadowed by
1191 // eval-introduced variables. Eval is used a lot without
1192 // introducing variables. In those cases, we do not want to
1193 // perform a runtime call for all variables in the scope
1194 // containing the eval.
1195 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
1196 EmitLoadGlobalSlotCheckExtensions(slot, typeof_state, slow);
1197 __ Branch(done);
1198 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001199 Slot* potential_slot = slot->var()->local_if_not_shadowed()->rewrite();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001200 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
1201 if (potential_slot != NULL) {
1202 // Generate fast case for locals that rewrite to slots.
1203 __ lw(v0, ContextSlotOperandCheckExtensions(potential_slot, slow));
1204 if (potential_slot->var()->mode() == Variable::CONST) {
1205 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1206 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1207 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1208 __ movz(v0, a0, at); // Conditional move.
1209 }
1210 __ Branch(done);
1211 } else if (rewrite != NULL) {
1212 // Generate fast case for calls of an argument function.
1213 Property* property = rewrite->AsProperty();
1214 if (property != NULL) {
1215 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
1216 Literal* key_literal = property->key()->AsLiteral();
1217 if (obj_proxy != NULL &&
1218 key_literal != NULL &&
1219 obj_proxy->IsArguments() &&
1220 key_literal->handle()->IsSmi()) {
1221 // Load arguments object if there are no eval-introduced
1222 // variables. Then load the argument from the arguments
1223 // object using keyed load.
1224 __ lw(a1,
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001225 ContextSlotOperandCheckExtensions(obj_proxy->var()->rewrite(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001226 slow));
1227 __ li(a0, Operand(key_literal->handle()));
1228 Handle<Code> ic =
1229 isolate()->builtins()->KeyedLoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001230 __ Call(ic, RelocInfo::CODE_TARGET, GetPropertyId(property));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001231 __ Branch(done);
1232 }
1233 }
1234 }
1235 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001236}
1237
1238
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001239void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1240 // Record position before possible IC call.
1241 SetSourcePosition(proxy->position());
1242 Variable* var = proxy->var();
1243
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001244 // Three cases: non-this global variables, lookup slots, and all other
1245 // types of slots.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001246 Slot* slot = var->rewrite();
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001247 ASSERT((var->is_global() && !var->is_this()) == (slot == NULL));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001248
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001249 if (slot == NULL) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001250 Comment cmnt(masm_, "Global variable");
1251 // Use inline caching. Variable name is passed in a2 and the global
1252 // object (receiver) in a0.
1253 __ lw(a0, GlobalObjectOperand());
1254 __ li(a2, Operand(var->name()));
1255 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001256 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001257 context()->Plug(v0);
1258
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001259 } else if (slot->type() == Slot::LOOKUP) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001260 Label done, slow;
1261
1262 // Generate code for loading from variables potentially shadowed
1263 // by eval-introduced variables.
1264 EmitDynamicLoadFromSlotFastCase(slot, NOT_INSIDE_TYPEOF, &slow, &done);
1265
1266 __ bind(&slow);
1267 Comment cmnt(masm_, "Lookup slot");
1268 __ li(a1, Operand(var->name()));
1269 __ Push(cp, a1); // Context and name.
1270 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1271 __ bind(&done);
1272
1273 context()->Plug(v0);
1274
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001275 } else {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001276 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1277 ? "Context slot"
1278 : "Stack slot");
1279 if (var->mode() == Variable::CONST) {
1280 // Constants may be the hole value if they have not been initialized.
1281 // Unhole them.
1282 MemOperand slot_operand = EmitSlotSearch(slot, a0);
1283 __ lw(v0, slot_operand);
1284 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1285 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1286 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1287 __ movz(v0, a0, at); // Conditional move.
1288 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001289 } else if (var->mode() == Variable::LET) {
1290 // Let bindings may be the hole value if they have not been initialized.
1291 // Throw a type error in this case.
1292 Label done;
1293 MemOperand slot_operand = EmitSlotSearch(slot, a0);
1294 __ lw(v0, slot_operand);
1295 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
1296 __ Branch(&done, ne, v0, Operand(a1));
1297 __ li(v0, Operand(var->name()));
1298 __ push(v0);
1299 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1300 __ bind(&done);
1301 context()->Plug(v0);
1302 } else {
1303 context()->Plug(slot);
1304 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001305 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001306}
1307
1308
1309void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001310 Comment cmnt(masm_, "[ RegExpLiteral");
1311 Label materialized;
1312 // Registers will be used as follows:
1313 // t1 = materialized value (RegExp literal)
1314 // t0 = JS function, literals array
1315 // a3 = literal index
1316 // a2 = RegExp pattern
1317 // a1 = RegExp flags
1318 // a0 = RegExp literal clone
1319 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1320 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1321 int literal_offset =
1322 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1323 __ lw(t1, FieldMemOperand(t0, literal_offset));
1324 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1325 __ Branch(&materialized, ne, t1, Operand(at));
1326
1327 // Create regexp literal using runtime function.
1328 // Result will be in v0.
1329 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1330 __ li(a2, Operand(expr->pattern()));
1331 __ li(a1, Operand(expr->flags()));
1332 __ Push(t0, a3, a2, a1);
1333 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1334 __ mov(t1, v0);
1335
1336 __ bind(&materialized);
1337 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1338 Label allocated, runtime_allocate;
1339 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1340 __ jmp(&allocated);
1341
1342 __ bind(&runtime_allocate);
1343 __ push(t1);
1344 __ li(a0, Operand(Smi::FromInt(size)));
1345 __ push(a0);
1346 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1347 __ pop(t1);
1348
1349 __ bind(&allocated);
1350
1351 // After this, registers are used as follows:
1352 // v0: Newly allocated regexp.
1353 // t1: Materialized regexp.
1354 // a2: temp.
1355 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1356 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001357}
1358
1359
1360void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001361 Comment cmnt(masm_, "[ ObjectLiteral");
1362 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1363 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1364 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1365 __ li(a1, Operand(expr->constant_properties()));
1366 int flags = expr->fast_elements()
1367 ? ObjectLiteral::kFastElements
1368 : ObjectLiteral::kNoFlags;
1369 flags |= expr->has_function()
1370 ? ObjectLiteral::kHasFunction
1371 : ObjectLiteral::kNoFlags;
1372 __ li(a0, Operand(Smi::FromInt(flags)));
1373 __ Push(a3, a2, a1, a0);
1374 if (expr->depth() > 1) {
1375 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1376 } else {
1377 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
1378 }
1379
1380 // If result_saved is true the result is on top of the stack. If
1381 // result_saved is false the result is in v0.
1382 bool result_saved = false;
1383
1384 // Mark all computed expressions that are bound to a key that
1385 // is shadowed by a later occurrence of the same key. For the
1386 // marked expressions, no store code is emitted.
1387 expr->CalculateEmitStore();
1388
1389 for (int i = 0; i < expr->properties()->length(); i++) {
1390 ObjectLiteral::Property* property = expr->properties()->at(i);
1391 if (property->IsCompileTimeValue()) continue;
1392
1393 Literal* key = property->key();
1394 Expression* value = property->value();
1395 if (!result_saved) {
1396 __ push(v0); // Save result on stack.
1397 result_saved = true;
1398 }
1399 switch (property->kind()) {
1400 case ObjectLiteral::Property::CONSTANT:
1401 UNREACHABLE();
1402 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1403 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1404 // Fall through.
1405 case ObjectLiteral::Property::COMPUTED:
1406 if (key->handle()->IsSymbol()) {
1407 if (property->emit_store()) {
1408 VisitForAccumulatorValue(value);
1409 __ mov(a0, result_register());
1410 __ li(a2, Operand(key->handle()));
1411 __ lw(a1, MemOperand(sp));
1412 Handle<Code> ic = is_strict_mode()
1413 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1414 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001415 __ Call(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001416 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1417 } else {
1418 VisitForEffect(value);
1419 }
1420 break;
1421 }
1422 // Fall through.
1423 case ObjectLiteral::Property::PROTOTYPE:
1424 // Duplicate receiver on stack.
1425 __ lw(a0, MemOperand(sp));
1426 __ push(a0);
1427 VisitForStackValue(key);
1428 VisitForStackValue(value);
1429 if (property->emit_store()) {
1430 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1431 __ push(a0);
1432 __ CallRuntime(Runtime::kSetProperty, 4);
1433 } else {
1434 __ Drop(3);
1435 }
1436 break;
1437 case ObjectLiteral::Property::GETTER:
1438 case ObjectLiteral::Property::SETTER:
1439 // Duplicate receiver on stack.
1440 __ lw(a0, MemOperand(sp));
1441 __ push(a0);
1442 VisitForStackValue(key);
1443 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1444 Smi::FromInt(1) :
1445 Smi::FromInt(0)));
1446 __ push(a1);
1447 VisitForStackValue(value);
1448 __ CallRuntime(Runtime::kDefineAccessor, 4);
1449 break;
1450 }
1451 }
1452
1453 if (expr->has_function()) {
1454 ASSERT(result_saved);
1455 __ lw(a0, MemOperand(sp));
1456 __ push(a0);
1457 __ CallRuntime(Runtime::kToFastProperties, 1);
1458 }
1459
1460 if (result_saved) {
1461 context()->PlugTOS();
1462 } else {
1463 context()->Plug(v0);
1464 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001465}
1466
1467
1468void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001469 Comment cmnt(masm_, "[ ArrayLiteral");
1470
1471 ZoneList<Expression*>* subexprs = expr->values();
1472 int length = subexprs->length();
1473 __ mov(a0, result_register());
1474 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1475 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1476 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1477 __ li(a1, Operand(expr->constant_elements()));
1478 __ Push(a3, a2, a1);
1479 if (expr->constant_elements()->map() ==
1480 isolate()->heap()->fixed_cow_array_map()) {
1481 FastCloneShallowArrayStub stub(
1482 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1483 __ CallStub(&stub);
1484 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1485 1, a1, a2);
1486 } else if (expr->depth() > 1) {
1487 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1488 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1489 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1490 } else {
1491 FastCloneShallowArrayStub stub(
1492 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
1493 __ CallStub(&stub);
1494 }
1495
1496 bool result_saved = false; // Is the result saved to the stack?
1497
1498 // Emit code to evaluate all the non-constant subexpressions and to store
1499 // them into the newly cloned array.
1500 for (int i = 0; i < length; i++) {
1501 Expression* subexpr = subexprs->at(i);
1502 // If the subexpression is a literal or a simple materialized literal it
1503 // is already set in the cloned array.
1504 if (subexpr->AsLiteral() != NULL ||
1505 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1506 continue;
1507 }
1508
1509 if (!result_saved) {
1510 __ push(v0);
1511 result_saved = true;
1512 }
1513 VisitForAccumulatorValue(subexpr);
1514
1515 // Store the subexpression value in the array's elements.
1516 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1517 __ lw(a1, FieldMemOperand(a1, JSObject::kElementsOffset));
1518 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1519 __ sw(result_register(), FieldMemOperand(a1, offset));
1520
1521 // Update the write barrier for the array store with v0 as the scratch
1522 // register.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001523 __ RecordWrite(a1, Operand(offset), a2, result_register());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001524
1525 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1526 }
1527
1528 if (result_saved) {
1529 context()->PlugTOS();
1530 } else {
1531 context()->Plug(v0);
1532 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001533}
1534
1535
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001536void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001537 Comment cmnt(masm_, "[ Assignment");
1538 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1539 // on the left-hand side.
1540 if (!expr->target()->IsValidLeftHandSide()) {
1541 VisitForEffect(expr->target());
1542 return;
1543 }
1544
1545 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001546 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001547 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1548 LhsKind assign_type = VARIABLE;
1549 Property* property = expr->target()->AsProperty();
1550 if (property != NULL) {
1551 assign_type = (property->key()->IsPropertyName())
1552 ? NAMED_PROPERTY
1553 : KEYED_PROPERTY;
1554 }
1555
1556 // Evaluate LHS expression.
1557 switch (assign_type) {
1558 case VARIABLE:
1559 // Nothing to do here.
1560 break;
1561 case NAMED_PROPERTY:
1562 if (expr->is_compound()) {
1563 // We need the receiver both on the stack and in the accumulator.
1564 VisitForAccumulatorValue(property->obj());
1565 __ push(result_register());
1566 } else {
1567 VisitForStackValue(property->obj());
1568 }
1569 break;
1570 case KEYED_PROPERTY:
1571 // We need the key and receiver on both the stack and in v0 and a1.
1572 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001573 VisitForStackValue(property->obj());
1574 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001575 __ lw(a1, MemOperand(sp, 0));
1576 __ push(v0);
1577 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001578 VisitForStackValue(property->obj());
1579 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001580 }
1581 break;
1582 }
1583
1584 // For compound assignments we need another deoptimization point after the
1585 // variable/property load.
1586 if (expr->is_compound()) {
1587 { AccumulatorValueContext context(this);
1588 switch (assign_type) {
1589 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001590 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001591 PrepareForBailout(expr->target(), TOS_REG);
1592 break;
1593 case NAMED_PROPERTY:
1594 EmitNamedPropertyLoad(property);
1595 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1596 break;
1597 case KEYED_PROPERTY:
1598 EmitKeyedPropertyLoad(property);
1599 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1600 break;
1601 }
1602 }
1603
1604 Token::Value op = expr->binary_op();
1605 __ push(v0); // Left operand goes on the stack.
1606 VisitForAccumulatorValue(expr->value());
1607
1608 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1609 ? OVERWRITE_RIGHT
1610 : NO_OVERWRITE;
1611 SetSourcePosition(expr->position() + 1);
1612 AccumulatorValueContext context(this);
1613 if (ShouldInlineSmiCase(op)) {
1614 EmitInlineSmiBinaryOp(expr->binary_operation(),
1615 op,
1616 mode,
1617 expr->target(),
1618 expr->value());
1619 } else {
1620 EmitBinaryOp(expr->binary_operation(), op, mode);
1621 }
1622
1623 // Deoptimization point in case the binary operation may have side effects.
1624 PrepareForBailout(expr->binary_operation(), TOS_REG);
1625 } else {
1626 VisitForAccumulatorValue(expr->value());
1627 }
1628
1629 // Record source position before possible IC call.
1630 SetSourcePosition(expr->position());
1631
1632 // Store the value.
1633 switch (assign_type) {
1634 case VARIABLE:
1635 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1636 expr->op());
1637 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1638 context()->Plug(v0);
1639 break;
1640 case NAMED_PROPERTY:
1641 EmitNamedPropertyAssignment(expr);
1642 break;
1643 case KEYED_PROPERTY:
1644 EmitKeyedPropertyAssignment(expr);
1645 break;
1646 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001647}
1648
1649
ager@chromium.org5c838252010-02-19 08:53:10 +00001650void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001651 SetSourcePosition(prop->position());
1652 Literal* key = prop->key()->AsLiteral();
1653 __ mov(a0, result_register());
1654 __ li(a2, Operand(key->handle()));
1655 // Call load IC. It has arguments receiver and property name a0 and a2.
1656 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001657 __ Call(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001658}
1659
1660
1661void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001662 SetSourcePosition(prop->position());
1663 __ mov(a0, result_register());
1664 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1665 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001666 __ Call(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001667}
1668
1669
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001670void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001671 Token::Value op,
1672 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001673 Expression* left_expr,
1674 Expression* right_expr) {
1675 Label done, smi_case, stub_call;
1676
1677 Register scratch1 = a2;
1678 Register scratch2 = a3;
1679
1680 // Get the arguments.
1681 Register left = a1;
1682 Register right = a0;
1683 __ pop(left);
1684 __ mov(a0, result_register());
1685
1686 // Perform combined smi check on both operands.
1687 __ Or(scratch1, left, Operand(right));
1688 STATIC_ASSERT(kSmiTag == 0);
1689 JumpPatchSite patch_site(masm_);
1690 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1691
1692 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001693 BinaryOpStub stub(op, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001694 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001695 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001696 __ jmp(&done);
1697
1698 __ bind(&smi_case);
1699 // Smi case. This code works the same way as the smi-smi case in the type
1700 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001701 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001702 switch (op) {
1703 case Token::SAR:
1704 __ Branch(&stub_call);
1705 __ GetLeastBitsFromSmi(scratch1, right, 5);
1706 __ srav(right, left, scratch1);
1707 __ And(v0, right, Operand(~kSmiTagMask));
1708 break;
1709 case Token::SHL: {
1710 __ Branch(&stub_call);
1711 __ SmiUntag(scratch1, left);
1712 __ GetLeastBitsFromSmi(scratch2, right, 5);
1713 __ sllv(scratch1, scratch1, scratch2);
1714 __ Addu(scratch2, scratch1, Operand(0x40000000));
1715 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1716 __ SmiTag(v0, scratch1);
1717 break;
1718 }
1719 case Token::SHR: {
1720 __ Branch(&stub_call);
1721 __ SmiUntag(scratch1, left);
1722 __ GetLeastBitsFromSmi(scratch2, right, 5);
1723 __ srlv(scratch1, scratch1, scratch2);
1724 __ And(scratch2, scratch1, 0xc0000000);
1725 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1726 __ SmiTag(v0, scratch1);
1727 break;
1728 }
1729 case Token::ADD:
1730 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1731 __ BranchOnOverflow(&stub_call, scratch1);
1732 break;
1733 case Token::SUB:
1734 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1735 __ BranchOnOverflow(&stub_call, scratch1);
1736 break;
1737 case Token::MUL: {
1738 __ SmiUntag(scratch1, right);
1739 __ Mult(left, scratch1);
1740 __ mflo(scratch1);
1741 __ mfhi(scratch2);
1742 __ sra(scratch1, scratch1, 31);
1743 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1744 __ mflo(v0);
1745 __ Branch(&done, ne, v0, Operand(zero_reg));
1746 __ Addu(scratch2, right, left);
1747 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1748 ASSERT(Smi::FromInt(0) == 0);
1749 __ mov(v0, zero_reg);
1750 break;
1751 }
1752 case Token::BIT_OR:
1753 __ Or(v0, left, Operand(right));
1754 break;
1755 case Token::BIT_AND:
1756 __ And(v0, left, Operand(right));
1757 break;
1758 case Token::BIT_XOR:
1759 __ Xor(v0, left, Operand(right));
1760 break;
1761 default:
1762 UNREACHABLE();
1763 }
1764
1765 __ bind(&done);
1766 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001767}
1768
1769
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001770void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1771 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001772 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001773 __ mov(a0, result_register());
1774 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001775 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001776 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001777 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001778 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001779 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001780}
1781
1782
1783void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001784 // Invalid left-hand sides are rewritten to have a 'throw
1785 // ReferenceError' on the left-hand side.
1786 if (!expr->IsValidLeftHandSide()) {
1787 VisitForEffect(expr);
1788 return;
1789 }
1790
1791 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001792 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001793 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1794 LhsKind assign_type = VARIABLE;
1795 Property* prop = expr->AsProperty();
1796 if (prop != NULL) {
1797 assign_type = (prop->key()->IsPropertyName())
1798 ? NAMED_PROPERTY
1799 : KEYED_PROPERTY;
1800 }
1801
1802 switch (assign_type) {
1803 case VARIABLE: {
1804 Variable* var = expr->AsVariableProxy()->var();
1805 EffectContext context(this);
1806 EmitVariableAssignment(var, Token::ASSIGN);
1807 break;
1808 }
1809 case NAMED_PROPERTY: {
1810 __ push(result_register()); // Preserve value.
1811 VisitForAccumulatorValue(prop->obj());
1812 __ mov(a1, result_register());
1813 __ pop(a0); // Restore value.
1814 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1815 Handle<Code> ic = is_strict_mode()
1816 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1817 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001818 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001819 break;
1820 }
1821 case KEYED_PROPERTY: {
1822 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001823 VisitForStackValue(prop->obj());
1824 VisitForAccumulatorValue(prop->key());
1825 __ mov(a1, result_register());
1826 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001827 __ pop(a0); // Restore value.
1828 Handle<Code> ic = is_strict_mode()
1829 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
1830 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001831 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001832 break;
1833 }
1834 }
1835 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1836 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001837}
1838
1839
1840void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001841 Token::Value op) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001842 ASSERT(var != NULL);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001843 ASSERT(var->is_global() || var->rewrite() != NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001844
1845 if (var->is_global()) {
1846 ASSERT(!var->is_this());
1847 // Assignment to a global variable. Use inline caching for the
1848 // assignment. Right-hand-side value is passed in a0, variable name in
1849 // a2, and the global object in a1.
1850 __ mov(a0, result_register());
1851 __ li(a2, Operand(var->name()));
1852 __ lw(a1, GlobalObjectOperand());
1853 Handle<Code> ic = is_strict_mode()
1854 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1855 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001856 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001857
1858 } else if (op == Token::INIT_CONST) {
1859 // Like var declarations, const declarations are hoisted to function
1860 // scope. However, unlike var initializers, const initializers are able
1861 // to drill a hole to that function context, even from inside a 'with'
1862 // context. We thus bypass the normal static scope lookup.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001863 Slot* slot = var->rewrite();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001864 Label skip;
1865 switch (slot->type()) {
1866 case Slot::PARAMETER:
1867 // No const parameters.
1868 UNREACHABLE();
1869 break;
1870 case Slot::LOCAL:
1871 // Detect const reinitialization by checking for the hole value.
1872 __ lw(a1, MemOperand(fp, SlotOffset(slot)));
1873 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1874 __ Branch(&skip, ne, a1, Operand(t0));
1875 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1876 break;
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001877 case Slot::CONTEXT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001878 case Slot::LOOKUP:
1879 __ push(result_register());
1880 __ li(a0, Operand(slot->var()->name()));
1881 __ Push(cp, a0); // Context and name.
1882 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1883 break;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001884 case Slot::GLOBAL:
1885 UNREACHABLE();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001886 }
1887 __ bind(&skip);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001888 } else if (var->mode() == Variable::LET && op != Token::INIT_LET) {
1889 // Perform the assignment for non-const variables. Const assignments
1890 // are simply skipped.
1891 Slot* slot = var->AsSlot();
1892 switch (slot->type()) {
1893 case Slot::PARAMETER:
1894 case Slot::LOCAL: {
1895 Label assign;
1896 // Check for an initialized let binding.
1897 __ lw(a1, MemOperand(fp, SlotOffset(slot)));
1898 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1899 __ Branch(&assign, ne, a1, Operand(t0));
1900 __ li(a1, Operand(var->name()));
1901 __ push(a1);
1902 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1903 // Perform the assignment.
1904 __ bind(&assign);
1905 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1906 break;
1907 }
1908 case Slot::CONTEXT: {
1909 // Let variables may be the hole value if they have not been
1910 // initialized. Throw a type error in this case.
1911 Label assign;
1912 MemOperand target = EmitSlotSearch(slot, a1);
1913 // Check for an initialized let binding.
1914 __ lw(a3, target);
1915 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1916 __ Branch(&assign, ne, a3, Operand(t0));
1917 __ li(a3, Operand(var->name()));
1918 __ push(a3);
1919 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1920 // Perform the assignment.
1921 __ bind(&assign);
1922 __ sw(result_register(), target);
1923 // RecordWrite may destroy all its register arguments.
1924 __ mov(a3, result_register());
1925 int offset = Context::SlotOffset(slot->index());
1926 __ RecordWrite(a1, Operand(offset), a2, a3);
1927 break;
1928 }
1929 case Slot::LOOKUP:
1930 // Call the runtime for the assignment.
1931 __ push(v0); // Value.
1932 __ li(a1, Operand(slot->var()->name()));
1933 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1934 __ Push(cp, a1, a0); // Context, name, strict mode.
1935 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1936 break;
1937 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001938
1939 } else if (var->mode() != Variable::CONST) {
1940 // Perform the assignment for non-const variables. Const assignments
1941 // are simply skipped.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001942 Slot* slot = var->rewrite();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001943 switch (slot->type()) {
1944 case Slot::PARAMETER:
1945 case Slot::LOCAL:
1946 // Perform the assignment.
1947 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1948 break;
1949
1950 case Slot::CONTEXT: {
1951 MemOperand target = EmitSlotSearch(slot, a1);
1952 // Perform the assignment and issue the write barrier.
1953 __ sw(result_register(), target);
1954 // RecordWrite may destroy all its register arguments.
1955 __ mov(a3, result_register());
1956 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
1957 __ RecordWrite(a1, Operand(offset), a2, a3);
1958 break;
1959 }
1960
1961 case Slot::LOOKUP:
1962 // Call the runtime for the assignment.
1963 __ push(v0); // Value.
1964 __ li(a1, Operand(slot->var()->name()));
1965 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1966 __ Push(cp, a1, a0); // Context, name, strict mode.
1967 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1968 break;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001969
1970 case Slot::GLOBAL:
1971 UNREACHABLE();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001972 }
1973 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001974}
1975
1976
1977void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001978 // Assignment to a property, using a named store IC.
1979 Property* prop = expr->target()->AsProperty();
1980 ASSERT(prop != NULL);
1981 ASSERT(prop->key()->AsLiteral() != NULL);
1982
1983 // If the assignment starts a block of assignments to the same object,
1984 // change to slow case to avoid the quadratic behavior of repeatedly
1985 // adding fast properties.
1986 if (expr->starts_initialization_block()) {
1987 __ push(result_register());
1988 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1989 __ push(t0);
1990 __ CallRuntime(Runtime::kToSlowProperties, 1);
1991 __ pop(result_register());
1992 }
1993
1994 // Record source code position before IC call.
1995 SetSourcePosition(expr->position());
1996 __ mov(a0, result_register()); // Load the value.
1997 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1998 // Load receiver to a1. Leave a copy in the stack if needed for turning the
1999 // receiver into fast case.
2000 if (expr->ends_initialization_block()) {
2001 __ lw(a1, MemOperand(sp));
2002 } else {
2003 __ pop(a1);
2004 }
2005
2006 Handle<Code> ic = is_strict_mode()
2007 ? isolate()->builtins()->StoreIC_Initialize_Strict()
2008 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002009 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002010
2011 // If the assignment ends an initialization block, revert to fast case.
2012 if (expr->ends_initialization_block()) {
2013 __ push(v0); // Result of assignment, saved even if not needed.
2014 // Receiver is under the result value.
2015 __ lw(t0, MemOperand(sp, kPointerSize));
2016 __ push(t0);
2017 __ CallRuntime(Runtime::kToFastProperties, 1);
2018 __ pop(v0);
2019 __ Drop(1);
2020 }
2021 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2022 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002023}
2024
2025
2026void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002027 // Assignment to a property, using a keyed store IC.
2028
2029 // If the assignment starts a block of assignments to the same object,
2030 // change to slow case to avoid the quadratic behavior of repeatedly
2031 // adding fast properties.
2032 if (expr->starts_initialization_block()) {
2033 __ push(result_register());
2034 // Receiver is now under the key and value.
2035 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2036 __ push(t0);
2037 __ CallRuntime(Runtime::kToSlowProperties, 1);
2038 __ pop(result_register());
2039 }
2040
2041 // Record source code position before IC call.
2042 SetSourcePosition(expr->position());
2043 // Call keyed store IC.
2044 // The arguments are:
2045 // - a0 is the value,
2046 // - a1 is the key,
2047 // - a2 is the receiver.
2048 __ mov(a0, result_register());
2049 __ pop(a1); // Key.
2050 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2051 // receiver into fast case.
2052 if (expr->ends_initialization_block()) {
2053 __ lw(a2, MemOperand(sp));
2054 } else {
2055 __ pop(a2);
2056 }
2057
2058 Handle<Code> ic = is_strict_mode()
2059 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
2060 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002061 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002062
2063 // If the assignment ends an initialization block, revert to fast case.
2064 if (expr->ends_initialization_block()) {
2065 __ push(v0); // Result of assignment, saved even if not needed.
2066 // Receiver is under the result value.
2067 __ lw(t0, MemOperand(sp, kPointerSize));
2068 __ push(t0);
2069 __ CallRuntime(Runtime::kToFastProperties, 1);
2070 __ pop(v0);
2071 __ Drop(1);
2072 }
2073 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2074 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002075}
2076
2077
2078void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002079 Comment cmnt(masm_, "[ Property");
2080 Expression* key = expr->key();
2081
2082 if (key->IsPropertyName()) {
2083 VisitForAccumulatorValue(expr->obj());
2084 EmitNamedPropertyLoad(expr);
2085 context()->Plug(v0);
2086 } else {
2087 VisitForStackValue(expr->obj());
2088 VisitForAccumulatorValue(expr->key());
2089 __ pop(a1);
2090 EmitKeyedPropertyLoad(expr);
2091 context()->Plug(v0);
2092 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002093}
2094
lrn@chromium.org7516f052011-03-30 08:52:27 +00002095
ager@chromium.org5c838252010-02-19 08:53:10 +00002096void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002097 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002098 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002099 // Code common for calls using the IC.
2100 ZoneList<Expression*>* args = expr->arguments();
2101 int arg_count = args->length();
2102 { PreservePositionScope scope(masm()->positions_recorder());
2103 for (int i = 0; i < arg_count; i++) {
2104 VisitForStackValue(args->at(i));
2105 }
2106 __ li(a2, Operand(name));
2107 }
2108 // Record source position for debugger.
2109 SetSourcePosition(expr->position());
2110 // Call the IC initialization code.
2111 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2112 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00002113 isolate()->stub_cache()->ComputeCallInitialize(arg_count, in_loop, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002114 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002115 RecordJSReturnSite(expr);
2116 // Restore context register.
2117 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2118 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002119}
2120
2121
lrn@chromium.org7516f052011-03-30 08:52:27 +00002122void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002123 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002124 // Load the key.
2125 VisitForAccumulatorValue(key);
2126
2127 // Swap the name of the function and the receiver on the stack to follow
2128 // the calling convention for call ICs.
2129 __ pop(a1);
2130 __ push(v0);
2131 __ push(a1);
2132
2133 // Code common for calls using the IC.
2134 ZoneList<Expression*>* args = expr->arguments();
2135 int arg_count = args->length();
2136 { PreservePositionScope scope(masm()->positions_recorder());
2137 for (int i = 0; i < arg_count; i++) {
2138 VisitForStackValue(args->at(i));
2139 }
2140 }
2141 // Record source position for debugger.
2142 SetSourcePosition(expr->position());
2143 // Call the IC initialization code.
2144 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2145 Handle<Code> ic =
2146 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count, in_loop);
2147 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002148 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002149 RecordJSReturnSite(expr);
2150 // Restore context register.
2151 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2152 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002153}
2154
2155
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002156void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002157 // Code common for calls using the call stub.
2158 ZoneList<Expression*>* args = expr->arguments();
2159 int arg_count = args->length();
2160 { PreservePositionScope scope(masm()->positions_recorder());
2161 for (int i = 0; i < arg_count; i++) {
2162 VisitForStackValue(args->at(i));
2163 }
2164 }
2165 // Record source position for debugger.
2166 SetSourcePosition(expr->position());
2167 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2168 CallFunctionStub stub(arg_count, in_loop, flags);
2169 __ CallStub(&stub);
2170 RecordJSReturnSite(expr);
2171 // Restore context register.
2172 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2173 context()->DropAndPlug(1, v0);
2174}
2175
2176
2177void FullCodeGenerator::EmitResolvePossiblyDirectEval(ResolveEvalFlag flag,
2178 int arg_count) {
2179 // Push copy of the first argument or undefined if it doesn't exist.
2180 if (arg_count > 0) {
2181 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2182 } else {
2183 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2184 }
2185 __ push(a1);
2186
2187 // Push the receiver of the enclosing function and do runtime call.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002188 int receiver_offset = 2 + info_->scope()->num_parameters();
2189 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002190 __ push(a1);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002191 // Push the strict mode flag. In harmony mode every eval call
2192 // is a strict mode eval call.
2193 StrictModeFlag strict_mode = strict_mode_flag();
2194 if (FLAG_harmony_block_scoping) {
2195 strict_mode = kStrictMode;
2196 }
2197 __ li(a1, Operand(Smi::FromInt(strict_mode)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002198 __ push(a1);
2199
2200 __ CallRuntime(flag == SKIP_CONTEXT_LOOKUP
2201 ? Runtime::kResolvePossiblyDirectEvalNoLookup
2202 : Runtime::kResolvePossiblyDirectEval, 4);
ager@chromium.org5c838252010-02-19 08:53:10 +00002203}
2204
2205
2206void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002207#ifdef DEBUG
2208 // We want to verify that RecordJSReturnSite gets called on all paths
2209 // through this function. Avoid early returns.
2210 expr->return_is_recorded_ = false;
2211#endif
2212
2213 Comment cmnt(masm_, "[ Call");
2214 Expression* fun = expr->expression();
2215 Variable* var = fun->AsVariableProxy()->AsVariable();
2216
2217 if (var != NULL && var->is_possibly_eval()) {
2218 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2219 // resolve the function we need to call and the receiver of the
2220 // call. Then we call the resolved function using the given
2221 // arguments.
2222 ZoneList<Expression*>* args = expr->arguments();
2223 int arg_count = args->length();
2224
2225 { PreservePositionScope pos_scope(masm()->positions_recorder());
2226 VisitForStackValue(fun);
2227 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2228 __ push(a2); // Reserved receiver slot.
2229
2230 // Push the arguments.
2231 for (int i = 0; i < arg_count; i++) {
2232 VisitForStackValue(args->at(i));
2233 }
2234 // If we know that eval can only be shadowed by eval-introduced
2235 // variables we attempt to load the global eval function directly
2236 // in generated code. If we succeed, there is no need to perform a
2237 // context lookup in the runtime system.
2238 Label done;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002239 if (var->rewrite() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002240 Label slow;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002241 EmitLoadGlobalSlotCheckExtensions(var->rewrite(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002242 NOT_INSIDE_TYPEOF,
2243 &slow);
2244 // Push the function and resolve eval.
2245 __ push(v0);
2246 EmitResolvePossiblyDirectEval(SKIP_CONTEXT_LOOKUP, arg_count);
2247 __ jmp(&done);
2248 __ bind(&slow);
2249 }
2250
2251 // Push copy of the function (found below the arguments) and
2252 // resolve eval.
2253 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2254 __ push(a1);
2255 EmitResolvePossiblyDirectEval(PERFORM_CONTEXT_LOOKUP, arg_count);
2256 if (done.is_linked()) {
2257 __ bind(&done);
2258 }
2259
2260 // The runtime call returns a pair of values in v0 (function) and
2261 // v1 (receiver). Touch up the stack with the right values.
2262 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2263 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2264 }
2265 // Record source position for debugger.
2266 SetSourcePosition(expr->position());
2267 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002268 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002269 __ CallStub(&stub);
2270 RecordJSReturnSite(expr);
2271 // Restore context register.
2272 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2273 context()->DropAndPlug(1, v0);
2274 } else if (var != NULL && !var->is_this() && var->is_global()) {
2275 // Push global object as receiver for the call IC.
2276 __ lw(a0, GlobalObjectOperand());
2277 __ push(a0);
2278 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002279 } else if (var != NULL && var->rewrite() != NULL &&
2280 var->rewrite()->type() == Slot::LOOKUP) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002281 // Call to a lookup slot (dynamically introduced variable).
2282 Label slow, done;
2283
2284 { PreservePositionScope scope(masm()->positions_recorder());
2285 // Generate code for loading from variables potentially shadowed
2286 // by eval-introduced variables.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002287 EmitDynamicLoadFromSlotFastCase(var->rewrite(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002288 NOT_INSIDE_TYPEOF,
2289 &slow,
2290 &done);
2291 }
2292
2293 __ bind(&slow);
2294 // Call the runtime to find the function to call (returned in v0)
2295 // and the object holding it (returned in v1).
2296 __ push(context_register());
2297 __ li(a2, Operand(var->name()));
2298 __ push(a2);
2299 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2300 __ Push(v0, v1); // Function, receiver.
2301
2302 // If fast case code has been generated, emit code to push the
2303 // function and receiver and have the slow path jump around this
2304 // code.
2305 if (done.is_linked()) {
2306 Label call;
2307 __ Branch(&call);
2308 __ bind(&done);
2309 // Push function.
2310 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002311 // The receiver is implicitly the global receiver. Indicate this
2312 // by passing the hole to the call function stub.
2313 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002314 __ push(a1);
2315 __ bind(&call);
2316 }
2317
danno@chromium.org40cb8782011-05-25 07:58:50 +00002318 // The receiver is either the global receiver or an object found
2319 // by LoadContextSlot. That object could be the hole if the
2320 // receiver is implicitly the global object.
2321 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002322 } else if (fun->AsProperty() != NULL) {
2323 // Call to an object property.
2324 Property* prop = fun->AsProperty();
2325 Literal* key = prop->key()->AsLiteral();
2326 if (key != NULL && key->handle()->IsSymbol()) {
2327 // Call to a named property, use call IC.
2328 { PreservePositionScope scope(masm()->positions_recorder());
2329 VisitForStackValue(prop->obj());
2330 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00002331 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002332 } else {
2333 // Call to a keyed property.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002334 { PreservePositionScope scope(masm()->positions_recorder());
2335 VisitForStackValue(prop->obj());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002336 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002337 EmitKeyedCallWithIC(expr, prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002338 }
2339 } else {
2340 { PreservePositionScope scope(masm()->positions_recorder());
2341 VisitForStackValue(fun);
2342 }
2343 // Load global receiver object.
2344 __ lw(a1, GlobalObjectOperand());
2345 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2346 __ push(a1);
2347 // Emit function call.
2348 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2349 }
2350
2351#ifdef DEBUG
2352 // RecordJSReturnSite should have been called.
2353 ASSERT(expr->return_is_recorded_);
2354#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002355}
2356
2357
2358void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002359 Comment cmnt(masm_, "[ CallNew");
2360 // According to ECMA-262, section 11.2.2, page 44, the function
2361 // expression in new calls must be evaluated before the
2362 // arguments.
2363
2364 // Push constructor on the stack. If it's not a function it's used as
2365 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2366 // ignored.
2367 VisitForStackValue(expr->expression());
2368
2369 // Push the arguments ("left-to-right") on the stack.
2370 ZoneList<Expression*>* args = expr->arguments();
2371 int arg_count = args->length();
2372 for (int i = 0; i < arg_count; i++) {
2373 VisitForStackValue(args->at(i));
2374 }
2375
2376 // Call the construct call builtin that handles allocation and
2377 // constructor invocation.
2378 SetSourcePosition(expr->position());
2379
2380 // Load function and argument count into a1 and a0.
2381 __ li(a0, Operand(arg_count));
2382 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2383
2384 Handle<Code> construct_builtin =
2385 isolate()->builtins()->JSConstructCall();
2386 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2387 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002388}
2389
2390
lrn@chromium.org7516f052011-03-30 08:52:27 +00002391void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002392 ASSERT(args->length() == 1);
2393
2394 VisitForAccumulatorValue(args->at(0));
2395
2396 Label materialize_true, materialize_false;
2397 Label* if_true = NULL;
2398 Label* if_false = NULL;
2399 Label* fall_through = NULL;
2400 context()->PrepareTest(&materialize_true, &materialize_false,
2401 &if_true, &if_false, &fall_through);
2402
2403 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2404 __ And(t0, v0, Operand(kSmiTagMask));
2405 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2406
2407 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002408}
2409
2410
2411void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002412 ASSERT(args->length() == 1);
2413
2414 VisitForAccumulatorValue(args->at(0));
2415
2416 Label materialize_true, materialize_false;
2417 Label* if_true = NULL;
2418 Label* if_false = NULL;
2419 Label* fall_through = NULL;
2420 context()->PrepareTest(&materialize_true, &materialize_false,
2421 &if_true, &if_false, &fall_through);
2422
2423 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2424 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2425 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2426
2427 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002428}
2429
2430
2431void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002432 ASSERT(args->length() == 1);
2433
2434 VisitForAccumulatorValue(args->at(0));
2435
2436 Label materialize_true, materialize_false;
2437 Label* if_true = NULL;
2438 Label* if_false = NULL;
2439 Label* fall_through = NULL;
2440 context()->PrepareTest(&materialize_true, &materialize_false,
2441 &if_true, &if_false, &fall_through);
2442
2443 __ JumpIfSmi(v0, if_false);
2444 __ LoadRoot(at, Heap::kNullValueRootIndex);
2445 __ Branch(if_true, eq, v0, Operand(at));
2446 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2447 // Undetectable objects behave like undefined when tested with typeof.
2448 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2449 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2450 __ Branch(if_false, ne, at, Operand(zero_reg));
2451 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002452 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002453 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002454 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2455 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002456
2457 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002458}
2459
2460
2461void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002462 ASSERT(args->length() == 1);
2463
2464 VisitForAccumulatorValue(args->at(0));
2465
2466 Label materialize_true, materialize_false;
2467 Label* if_true = NULL;
2468 Label* if_false = NULL;
2469 Label* fall_through = NULL;
2470 context()->PrepareTest(&materialize_true, &materialize_false,
2471 &if_true, &if_false, &fall_through);
2472
2473 __ JumpIfSmi(v0, if_false);
2474 __ GetObjectType(v0, a1, a1);
2475 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002476 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002477 if_true, if_false, fall_through);
2478
2479 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002480}
2481
2482
2483void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002484 ASSERT(args->length() == 1);
2485
2486 VisitForAccumulatorValue(args->at(0));
2487
2488 Label materialize_true, materialize_false;
2489 Label* if_true = NULL;
2490 Label* if_false = NULL;
2491 Label* fall_through = NULL;
2492 context()->PrepareTest(&materialize_true, &materialize_false,
2493 &if_true, &if_false, &fall_through);
2494
2495 __ JumpIfSmi(v0, if_false);
2496 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2497 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2498 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2499 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2500 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2501
2502 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002503}
2504
2505
2506void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2507 ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002508
2509 ASSERT(args->length() == 1);
2510
2511 VisitForAccumulatorValue(args->at(0));
2512
2513 Label materialize_true, materialize_false;
2514 Label* if_true = NULL;
2515 Label* if_false = NULL;
2516 Label* fall_through = NULL;
2517 context()->PrepareTest(&materialize_true, &materialize_false,
2518 &if_true, &if_false, &fall_through);
2519
2520 if (FLAG_debug_code) __ AbortIfSmi(v0);
2521
2522 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2523 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2524 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2525 __ Branch(if_true, ne, t0, Operand(zero_reg));
2526
2527 // Check for fast case object. Generate false result for slow case object.
2528 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2529 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2530 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2531 __ Branch(if_false, eq, a2, Operand(t0));
2532
2533 // Look for valueOf symbol in the descriptor array, and indicate false if
2534 // found. The type is not checked, so if it is a transition it is a false
2535 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002536 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002537 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2538 // t0: descriptor array
2539 // a3: length of descriptor array
2540 // Calculate the end of the descriptor array.
2541 STATIC_ASSERT(kSmiTag == 0);
2542 STATIC_ASSERT(kSmiTagSize == 1);
2543 STATIC_ASSERT(kPointerSize == 4);
2544 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2545 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2546 __ Addu(a2, a2, t1);
2547
2548 // Calculate location of the first key name.
2549 __ Addu(t0,
2550 t0,
2551 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2552 DescriptorArray::kFirstIndex * kPointerSize));
2553 // Loop through all the keys in the descriptor array. If one of these is the
2554 // symbol valueOf the result is false.
2555 Label entry, loop;
2556 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2557 // used in the loop below.
2558 __ li(t2, Operand(FACTORY->value_of_symbol()));
2559 __ jmp(&entry);
2560 __ bind(&loop);
2561 __ lw(a3, MemOperand(t0, 0));
2562 __ Branch(if_false, eq, a3, Operand(t2));
2563 __ Addu(t0, t0, Operand(kPointerSize));
2564 __ bind(&entry);
2565 __ Branch(&loop, ne, t0, Operand(a2));
2566
2567 // If a valueOf property is not found on the object check that it's
2568 // prototype is the un-modified String prototype. If not result is false.
2569 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2570 __ JumpIfSmi(a2, if_false);
2571 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2572 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2573 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2574 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2575 __ Branch(if_false, ne, a2, Operand(a3));
2576
2577 // Set the bit in the map to indicate that it has been checked safe for
2578 // default valueOf and set true result.
2579 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2580 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2581 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2582 __ jmp(if_true);
2583
2584 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2585 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002586}
2587
2588
2589void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002590 ASSERT(args->length() == 1);
2591
2592 VisitForAccumulatorValue(args->at(0));
2593
2594 Label materialize_true, materialize_false;
2595 Label* if_true = NULL;
2596 Label* if_false = NULL;
2597 Label* fall_through = NULL;
2598 context()->PrepareTest(&materialize_true, &materialize_false,
2599 &if_true, &if_false, &fall_through);
2600
2601 __ JumpIfSmi(v0, if_false);
2602 __ GetObjectType(v0, a1, a2);
2603 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2604 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2605 __ Branch(if_false);
2606
2607 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002608}
2609
2610
2611void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002612 ASSERT(args->length() == 1);
2613
2614 VisitForAccumulatorValue(args->at(0));
2615
2616 Label materialize_true, materialize_false;
2617 Label* if_true = NULL;
2618 Label* if_false = NULL;
2619 Label* fall_through = NULL;
2620 context()->PrepareTest(&materialize_true, &materialize_false,
2621 &if_true, &if_false, &fall_through);
2622
2623 __ JumpIfSmi(v0, if_false);
2624 __ GetObjectType(v0, a1, a1);
2625 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2626 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2627 if_true, if_false, fall_through);
2628
2629 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002630}
2631
2632
2633void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002634 ASSERT(args->length() == 1);
2635
2636 VisitForAccumulatorValue(args->at(0));
2637
2638 Label materialize_true, materialize_false;
2639 Label* if_true = NULL;
2640 Label* if_false = NULL;
2641 Label* fall_through = NULL;
2642 context()->PrepareTest(&materialize_true, &materialize_false,
2643 &if_true, &if_false, &fall_through);
2644
2645 __ JumpIfSmi(v0, if_false);
2646 __ GetObjectType(v0, a1, a1);
2647 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2648 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2649
2650 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002651}
2652
2653
2654void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002655 ASSERT(args->length() == 0);
2656
2657 Label materialize_true, materialize_false;
2658 Label* if_true = NULL;
2659 Label* if_false = NULL;
2660 Label* fall_through = NULL;
2661 context()->PrepareTest(&materialize_true, &materialize_false,
2662 &if_true, &if_false, &fall_through);
2663
2664 // Get the frame pointer for the calling frame.
2665 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2666
2667 // Skip the arguments adaptor frame if it exists.
2668 Label check_frame_marker;
2669 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2670 __ Branch(&check_frame_marker, ne,
2671 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2672 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2673
2674 // Check the marker in the calling frame.
2675 __ bind(&check_frame_marker);
2676 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
2677 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2678 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2679 if_true, if_false, fall_through);
2680
2681 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002682}
2683
2684
2685void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002686 ASSERT(args->length() == 2);
2687
2688 // Load the two objects into registers and perform the comparison.
2689 VisitForStackValue(args->at(0));
2690 VisitForAccumulatorValue(args->at(1));
2691
2692 Label materialize_true, materialize_false;
2693 Label* if_true = NULL;
2694 Label* if_false = NULL;
2695 Label* fall_through = NULL;
2696 context()->PrepareTest(&materialize_true, &materialize_false,
2697 &if_true, &if_false, &fall_through);
2698
2699 __ pop(a1);
2700 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2701 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2702
2703 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002704}
2705
2706
2707void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002708 ASSERT(args->length() == 1);
2709
2710 // ArgumentsAccessStub expects the key in a1 and the formal
2711 // parameter count in a0.
2712 VisitForAccumulatorValue(args->at(0));
2713 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002714 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002715 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2716 __ CallStub(&stub);
2717 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002718}
2719
2720
2721void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002722 ASSERT(args->length() == 0);
2723
2724 Label exit;
2725 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002726 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002727
2728 // Check if the calling frame is an arguments adaptor frame.
2729 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2730 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2731 __ Branch(&exit, ne, a3,
2732 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2733
2734 // Arguments adaptor case: Read the arguments length from the
2735 // adaptor frame.
2736 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2737
2738 __ bind(&exit);
2739 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002740}
2741
2742
2743void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002744 ASSERT(args->length() == 1);
2745 Label done, null, function, non_function_constructor;
2746
2747 VisitForAccumulatorValue(args->at(0));
2748
2749 // If the object is a smi, we return null.
2750 __ JumpIfSmi(v0, &null);
2751
2752 // Check that the object is a JS object but take special care of JS
2753 // functions to make sure they have 'Function' as their class.
2754 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002755 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002756
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002757 // As long as LAST_CALLABLE_SPEC_OBJECT_TYPE is the last instance type, and
2758 // FIRST_CALLABLE_SPEC_OBJECT_TYPE comes right after
2759 // LAST_NONCALLABLE_SPEC_OBJECT_TYPE, we can avoid checking for the latter.
2760 STATIC_ASSERT(LAST_TYPE == LAST_CALLABLE_SPEC_OBJECT_TYPE);
2761 STATIC_ASSERT(FIRST_CALLABLE_SPEC_OBJECT_TYPE ==
2762 LAST_NONCALLABLE_SPEC_OBJECT_TYPE + 1);
2763 __ Branch(&function, ge, a1, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002764
2765 // Check if the constructor in the map is a function.
2766 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2767 __ GetObjectType(v0, a1, a1);
2768 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2769
2770 // v0 now contains the constructor function. Grab the
2771 // instance class name from there.
2772 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2773 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2774 __ Branch(&done);
2775
2776 // Functions have class 'Function'.
2777 __ bind(&function);
2778 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2779 __ jmp(&done);
2780
2781 // Objects with a non-function constructor have class 'Object'.
2782 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002783 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002784 __ jmp(&done);
2785
2786 // Non-JS objects have class null.
2787 __ bind(&null);
2788 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2789
2790 // All done.
2791 __ bind(&done);
2792
2793 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002794}
2795
2796
2797void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002798 // Conditionally generate a log call.
2799 // Args:
2800 // 0 (literal string): The type of logging (corresponds to the flags).
2801 // This is used to determine whether or not to generate the log call.
2802 // 1 (string): Format string. Access the string at argument index 2
2803 // with '%2s' (see Logger::LogRuntime for all the formats).
2804 // 2 (array): Arguments to the format string.
2805 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002806 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2807 VisitForStackValue(args->at(1));
2808 VisitForStackValue(args->at(2));
2809 __ CallRuntime(Runtime::kLog, 2);
2810 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002811
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002812 // Finally, we're expected to leave a value on the top of the stack.
2813 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2814 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002815}
2816
2817
2818void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002819 ASSERT(args->length() == 0);
2820
2821 Label slow_allocate_heapnumber;
2822 Label heapnumber_allocated;
2823
2824 // Save the new heap number in callee-saved register s0, since
2825 // we call out to external C code below.
2826 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2827 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2828 __ jmp(&heapnumber_allocated);
2829
2830 __ bind(&slow_allocate_heapnumber);
2831
2832 // Allocate a heap number.
2833 __ CallRuntime(Runtime::kNumberAlloc, 0);
2834 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2835
2836 __ bind(&heapnumber_allocated);
2837
2838 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2839 // by computing:
2840 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2841 if (CpuFeatures::IsSupported(FPU)) {
2842 __ PrepareCallCFunction(1, a0);
2843 __ li(a0, Operand(ExternalReference::isolate_address()));
2844 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2845
2846
2847 CpuFeatures::Scope scope(FPU);
2848 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2849 __ li(a1, Operand(0x41300000));
2850 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002851 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002852 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002853 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002854 // Subtract and store the result in the heap number.
2855 __ sub_d(f0, f12, f14);
2856 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2857 __ mov(v0, s0);
2858 } else {
2859 __ PrepareCallCFunction(2, a0);
2860 __ mov(a0, s0);
2861 __ li(a1, Operand(ExternalReference::isolate_address()));
2862 __ CallCFunction(
2863 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2864 }
2865
2866 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002867}
2868
2869
2870void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002871 // Load the arguments on the stack and call the stub.
2872 SubStringStub stub;
2873 ASSERT(args->length() == 3);
2874 VisitForStackValue(args->at(0));
2875 VisitForStackValue(args->at(1));
2876 VisitForStackValue(args->at(2));
2877 __ CallStub(&stub);
2878 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002879}
2880
2881
2882void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002883 // Load the arguments on the stack and call the stub.
2884 RegExpExecStub stub;
2885 ASSERT(args->length() == 4);
2886 VisitForStackValue(args->at(0));
2887 VisitForStackValue(args->at(1));
2888 VisitForStackValue(args->at(2));
2889 VisitForStackValue(args->at(3));
2890 __ CallStub(&stub);
2891 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002892}
2893
2894
2895void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002896 ASSERT(args->length() == 1);
2897
2898 VisitForAccumulatorValue(args->at(0)); // Load the object.
2899
2900 Label done;
2901 // If the object is a smi return the object.
2902 __ JumpIfSmi(v0, &done);
2903 // If the object is not a value type, return the object.
2904 __ GetObjectType(v0, a1, a1);
2905 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2906
2907 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2908
2909 __ bind(&done);
2910 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002911}
2912
2913
2914void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002915 // Load the arguments on the stack and call the runtime function.
2916 ASSERT(args->length() == 2);
2917 VisitForStackValue(args->at(0));
2918 VisitForStackValue(args->at(1));
2919 MathPowStub stub;
2920 __ CallStub(&stub);
2921 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002922}
2923
2924
2925void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002926 ASSERT(args->length() == 2);
2927
2928 VisitForStackValue(args->at(0)); // Load the object.
2929 VisitForAccumulatorValue(args->at(1)); // Load the value.
2930 __ pop(a1); // v0 = value. a1 = object.
2931
2932 Label done;
2933 // If the object is a smi, return the value.
2934 __ JumpIfSmi(a1, &done);
2935
2936 // If the object is not a value type, return the value.
2937 __ GetObjectType(a1, a2, a2);
2938 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2939
2940 // Store the value.
2941 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2942 // Update the write barrier. Save the value as it will be
2943 // overwritten by the write barrier code and is needed afterward.
2944 __ RecordWrite(a1, Operand(JSValue::kValueOffset - kHeapObjectTag), a2, a3);
2945
2946 __ bind(&done);
2947 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002948}
2949
2950
2951void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002952 ASSERT_EQ(args->length(), 1);
2953
2954 // Load the argument on the stack and call the stub.
2955 VisitForStackValue(args->at(0));
2956
2957 NumberToStringStub stub;
2958 __ CallStub(&stub);
2959 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002960}
2961
2962
2963void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002964 ASSERT(args->length() == 1);
2965
2966 VisitForAccumulatorValue(args->at(0));
2967
2968 Label done;
2969 StringCharFromCodeGenerator generator(v0, a1);
2970 generator.GenerateFast(masm_);
2971 __ jmp(&done);
2972
2973 NopRuntimeCallHelper call_helper;
2974 generator.GenerateSlow(masm_, call_helper);
2975
2976 __ bind(&done);
2977 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002978}
2979
2980
2981void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002982 ASSERT(args->length() == 2);
2983
2984 VisitForStackValue(args->at(0));
2985 VisitForAccumulatorValue(args->at(1));
2986 __ mov(a0, result_register());
2987
2988 Register object = a1;
2989 Register index = a0;
2990 Register scratch = a2;
2991 Register result = v0;
2992
2993 __ pop(object);
2994
2995 Label need_conversion;
2996 Label index_out_of_range;
2997 Label done;
2998 StringCharCodeAtGenerator generator(object,
2999 index,
3000 scratch,
3001 result,
3002 &need_conversion,
3003 &need_conversion,
3004 &index_out_of_range,
3005 STRING_INDEX_IS_NUMBER);
3006 generator.GenerateFast(masm_);
3007 __ jmp(&done);
3008
3009 __ bind(&index_out_of_range);
3010 // When the index is out of range, the spec requires us to return
3011 // NaN.
3012 __ LoadRoot(result, Heap::kNanValueRootIndex);
3013 __ jmp(&done);
3014
3015 __ bind(&need_conversion);
3016 // Load the undefined value into the result register, which will
3017 // trigger conversion.
3018 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3019 __ jmp(&done);
3020
3021 NopRuntimeCallHelper call_helper;
3022 generator.GenerateSlow(masm_, call_helper);
3023
3024 __ bind(&done);
3025 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003026}
3027
3028
3029void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003030 ASSERT(args->length() == 2);
3031
3032 VisitForStackValue(args->at(0));
3033 VisitForAccumulatorValue(args->at(1));
3034 __ mov(a0, result_register());
3035
3036 Register object = a1;
3037 Register index = a0;
3038 Register scratch1 = a2;
3039 Register scratch2 = a3;
3040 Register result = v0;
3041
3042 __ pop(object);
3043
3044 Label need_conversion;
3045 Label index_out_of_range;
3046 Label done;
3047 StringCharAtGenerator generator(object,
3048 index,
3049 scratch1,
3050 scratch2,
3051 result,
3052 &need_conversion,
3053 &need_conversion,
3054 &index_out_of_range,
3055 STRING_INDEX_IS_NUMBER);
3056 generator.GenerateFast(masm_);
3057 __ jmp(&done);
3058
3059 __ bind(&index_out_of_range);
3060 // When the index is out of range, the spec requires us to return
3061 // the empty string.
3062 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3063 __ jmp(&done);
3064
3065 __ bind(&need_conversion);
3066 // Move smi zero into the result register, which will trigger
3067 // conversion.
3068 __ li(result, Operand(Smi::FromInt(0)));
3069 __ jmp(&done);
3070
3071 NopRuntimeCallHelper call_helper;
3072 generator.GenerateSlow(masm_, call_helper);
3073
3074 __ bind(&done);
3075 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003076}
3077
3078
3079void FullCodeGenerator::EmitStringAdd(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 StringAddStub stub(NO_STRING_ADD_FLAGS);
3086 __ CallStub(&stub);
3087 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003088}
3089
3090
3091void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003092 ASSERT_EQ(2, args->length());
3093
3094 VisitForStackValue(args->at(0));
3095 VisitForStackValue(args->at(1));
3096
3097 StringCompareStub stub;
3098 __ CallStub(&stub);
3099 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003100}
3101
3102
3103void FullCodeGenerator::EmitMathSin(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::SIN,
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::EmitMathCos(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::COS,
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);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003124}
3125
3126
3127void FullCodeGenerator::EmitMathLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003128 // Load the argument on the stack and call the stub.
3129 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3130 TranscendentalCacheStub::TAGGED);
3131 ASSERT(args->length() == 1);
3132 VisitForStackValue(args->at(0));
3133 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3134 __ CallStub(&stub);
3135 context()->Plug(v0);
3136}
3137
3138
3139void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
3140 // Load the argument on the stack and call the runtime function.
3141 ASSERT(args->length() == 1);
3142 VisitForStackValue(args->at(0));
3143 __ CallRuntime(Runtime::kMath_sqrt, 1);
3144 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003145}
3146
3147
3148void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003149 ASSERT(args->length() >= 2);
3150
3151 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3152 for (int i = 0; i < arg_count + 1; i++) {
3153 VisitForStackValue(args->at(i));
3154 }
3155 VisitForAccumulatorValue(args->last()); // Function.
3156
3157 // InvokeFunction requires the function in a1. Move it in there.
3158 __ mov(a1, result_register());
3159 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003160 __ InvokeFunction(a1, count, CALL_FUNCTION,
3161 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003162 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3163 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003164}
3165
3166
3167void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003168 RegExpConstructResultStub stub;
3169 ASSERT(args->length() == 3);
3170 VisitForStackValue(args->at(0));
3171 VisitForStackValue(args->at(1));
3172 VisitForStackValue(args->at(2));
3173 __ CallStub(&stub);
3174 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003175}
3176
3177
3178void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003179 ASSERT(args->length() == 3);
3180 VisitForStackValue(args->at(0));
3181 VisitForStackValue(args->at(1));
3182 VisitForStackValue(args->at(2));
3183 Label done;
3184 Label slow_case;
3185 Register object = a0;
3186 Register index1 = a1;
3187 Register index2 = a2;
3188 Register elements = a3;
3189 Register scratch1 = t0;
3190 Register scratch2 = t1;
3191
3192 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3193 // Fetch the map and check if array is in fast case.
3194 // Check that object doesn't require security checks and
3195 // has no indexed interceptor.
3196 __ GetObjectType(object, scratch1, scratch2);
3197 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3198 // Map is now in scratch1.
3199
3200 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3201 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3202 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3203
3204 // Check the object's elements are in fast case and writable.
3205 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3206 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3207 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3208 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3209
3210 // Check that both indices are smis.
3211 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3212 __ lw(index2, MemOperand(sp, 0));
3213 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3214
3215 // Check that both indices are valid.
3216 Label not_hi;
3217 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3218 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3219 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3220 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3221 __ bind(&not_hi);
3222
3223 // Bring the address of the elements into index1 and index2.
3224 __ Addu(scratch1, elements,
3225 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3226 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3227 __ Addu(index1, scratch1, index1);
3228 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3229 __ Addu(index2, scratch1, index2);
3230
3231 // Swap elements.
3232 __ lw(scratch1, MemOperand(index1, 0));
3233 __ lw(scratch2, MemOperand(index2, 0));
3234 __ sw(scratch1, MemOperand(index2, 0));
3235 __ sw(scratch2, MemOperand(index1, 0));
3236
3237 Label new_space;
3238 __ InNewSpace(elements, scratch1, eq, &new_space);
3239 // Possible optimization: do a check that both values are Smis
3240 // (or them and test against Smi mask).
3241
3242 __ mov(scratch1, elements);
3243 __ RecordWriteHelper(elements, index1, scratch2);
3244 __ RecordWriteHelper(scratch1, index2, scratch2); // scratch1 holds elements.
3245
3246 __ bind(&new_space);
3247 // We are done. Drop elements from the stack, and return undefined.
3248 __ Drop(3);
3249 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3250 __ jmp(&done);
3251
3252 __ bind(&slow_case);
3253 __ CallRuntime(Runtime::kSwapElements, 3);
3254
3255 __ bind(&done);
3256 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003257}
3258
3259
3260void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003261 ASSERT_EQ(2, args->length());
3262
3263 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3264 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3265
3266 Handle<FixedArray> jsfunction_result_caches(
3267 isolate()->global_context()->jsfunction_result_caches());
3268 if (jsfunction_result_caches->length() <= cache_id) {
3269 __ Abort("Attempt to use undefined cache.");
3270 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3271 context()->Plug(v0);
3272 return;
3273 }
3274
3275 VisitForAccumulatorValue(args->at(1));
3276
3277 Register key = v0;
3278 Register cache = a1;
3279 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3280 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3281 __ lw(cache,
3282 ContextOperand(
3283 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3284 __ lw(cache,
3285 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3286
3287
3288 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003289 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003290 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3291 // a2 now holds finger offset as a smi.
3292 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3293 // a3 now points to the start of fixed array elements.
3294 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3295 __ addu(a3, a3, at);
3296 // a3 now points to key of indexed element of cache.
3297 __ lw(a2, MemOperand(a3));
3298 __ Branch(&not_found, ne, key, Operand(a2));
3299
3300 __ lw(v0, MemOperand(a3, kPointerSize));
3301 __ Branch(&done);
3302
3303 __ bind(&not_found);
3304 // Call runtime to perform the lookup.
3305 __ Push(cache, key);
3306 __ CallRuntime(Runtime::kGetFromCache, 2);
3307
3308 __ bind(&done);
3309 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003310}
3311
3312
3313void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003314 ASSERT_EQ(2, args->length());
3315
3316 Register right = v0;
3317 Register left = a1;
3318 Register tmp = a2;
3319 Register tmp2 = a3;
3320
3321 VisitForStackValue(args->at(0));
3322 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3323 __ pop(left);
3324
3325 Label done, fail, ok;
3326 __ Branch(&ok, eq, left, Operand(right));
3327 // Fail if either is a non-HeapObject.
3328 __ And(tmp, left, Operand(right));
3329 __ And(at, tmp, Operand(kSmiTagMask));
3330 __ Branch(&fail, eq, at, Operand(zero_reg));
3331 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3332 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3333 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3334 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3335 __ Branch(&fail, ne, tmp, Operand(tmp2));
3336 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3337 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3338 __ Branch(&ok, eq, tmp, Operand(tmp2));
3339 __ bind(&fail);
3340 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3341 __ jmp(&done);
3342 __ bind(&ok);
3343 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3344 __ bind(&done);
3345
3346 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003347}
3348
3349
3350void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003351 VisitForAccumulatorValue(args->at(0));
3352
3353 Label materialize_true, materialize_false;
3354 Label* if_true = NULL;
3355 Label* if_false = NULL;
3356 Label* fall_through = NULL;
3357 context()->PrepareTest(&materialize_true, &materialize_false,
3358 &if_true, &if_false, &fall_through);
3359
3360 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3361 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3362
3363 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3364 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3365
3366 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003367}
3368
3369
3370void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003371 ASSERT(args->length() == 1);
3372 VisitForAccumulatorValue(args->at(0));
3373
3374 if (FLAG_debug_code) {
3375 __ AbortIfNotString(v0);
3376 }
3377
3378 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3379 __ IndexFromHash(v0, v0);
3380
3381 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003382}
3383
3384
3385void FullCodeGenerator::EmitFastAsciiArrayJoin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003386 Label bailout, done, one_char_separator, long_separator,
3387 non_trivial_array, not_size_one_array, loop,
3388 empty_separator_loop, one_char_separator_loop,
3389 one_char_separator_loop_entry, long_separator_loop;
3390
3391 ASSERT(args->length() == 2);
3392 VisitForStackValue(args->at(1));
3393 VisitForAccumulatorValue(args->at(0));
3394
3395 // All aliases of the same register have disjoint lifetimes.
3396 Register array = v0;
3397 Register elements = no_reg; // Will be v0.
3398 Register result = no_reg; // Will be v0.
3399 Register separator = a1;
3400 Register array_length = a2;
3401 Register result_pos = no_reg; // Will be a2.
3402 Register string_length = a3;
3403 Register string = t0;
3404 Register element = t1;
3405 Register elements_end = t2;
3406 Register scratch1 = t3;
3407 Register scratch2 = t5;
3408 Register scratch3 = t4;
3409 Register scratch4 = v1;
3410
3411 // Separator operand is on the stack.
3412 __ pop(separator);
3413
3414 // Check that the array is a JSArray.
3415 __ JumpIfSmi(array, &bailout);
3416 __ GetObjectType(array, scratch1, scratch2);
3417 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3418
3419 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003420 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003421
3422 // If the array has length zero, return the empty string.
3423 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3424 __ SmiUntag(array_length);
3425 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3426 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3427 __ Branch(&done);
3428
3429 __ bind(&non_trivial_array);
3430
3431 // Get the FixedArray containing array's elements.
3432 elements = array;
3433 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3434 array = no_reg; // End of array's live range.
3435
3436 // Check that all array elements are sequential ASCII strings, and
3437 // accumulate the sum of their lengths, as a smi-encoded value.
3438 __ mov(string_length, zero_reg);
3439 __ Addu(element,
3440 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3441 __ sll(elements_end, array_length, kPointerSizeLog2);
3442 __ Addu(elements_end, element, elements_end);
3443 // Loop condition: while (element < elements_end).
3444 // Live values in registers:
3445 // elements: Fixed array of strings.
3446 // array_length: Length of the fixed array of strings (not smi)
3447 // separator: Separator string
3448 // string_length: Accumulated sum of string lengths (smi).
3449 // element: Current array element.
3450 // elements_end: Array end.
3451 if (FLAG_debug_code) {
3452 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3453 array_length, Operand(zero_reg));
3454 }
3455 __ bind(&loop);
3456 __ lw(string, MemOperand(element));
3457 __ Addu(element, element, kPointerSize);
3458 __ JumpIfSmi(string, &bailout);
3459 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3460 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3461 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3462 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3463 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3464 __ BranchOnOverflow(&bailout, scratch3);
3465 __ Branch(&loop, lt, element, Operand(elements_end));
3466
3467 // If array_length is 1, return elements[0], a string.
3468 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3469 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3470 __ Branch(&done);
3471
3472 __ bind(&not_size_one_array);
3473
3474 // Live values in registers:
3475 // separator: Separator string
3476 // array_length: Length of the array.
3477 // string_length: Sum of string lengths (smi).
3478 // elements: FixedArray of strings.
3479
3480 // Check that the separator is a flat ASCII string.
3481 __ JumpIfSmi(separator, &bailout);
3482 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3483 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3484 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3485
3486 // Add (separator length times array_length) - separator length to the
3487 // string_length to get the length of the result string. array_length is not
3488 // smi but the other values are, so the result is a smi.
3489 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3490 __ Subu(string_length, string_length, Operand(scratch1));
3491 __ Mult(array_length, scratch1);
3492 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3493 // zero.
3494 __ mfhi(scratch2);
3495 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3496 __ mflo(scratch2);
3497 __ And(scratch3, scratch2, Operand(0x80000000));
3498 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3499 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3500 __ BranchOnOverflow(&bailout, scratch3);
3501 __ SmiUntag(string_length);
3502
3503 // Get first element in the array to free up the elements register to be used
3504 // for the result.
3505 __ Addu(element,
3506 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3507 result = elements; // End of live range for elements.
3508 elements = no_reg;
3509 // Live values in registers:
3510 // element: First array element
3511 // separator: Separator string
3512 // string_length: Length of result string (not smi)
3513 // array_length: Length of the array.
3514 __ AllocateAsciiString(result,
3515 string_length,
3516 scratch1,
3517 scratch2,
3518 elements_end,
3519 &bailout);
3520 // Prepare for looping. Set up elements_end to end of the array. Set
3521 // result_pos to the position of the result where to write the first
3522 // character.
3523 __ sll(elements_end, array_length, kPointerSizeLog2);
3524 __ Addu(elements_end, element, elements_end);
3525 result_pos = array_length; // End of live range for array_length.
3526 array_length = no_reg;
3527 __ Addu(result_pos,
3528 result,
3529 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3530
3531 // Check the length of the separator.
3532 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3533 __ li(at, Operand(Smi::FromInt(1)));
3534 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3535 __ Branch(&long_separator, gt, scratch1, Operand(at));
3536
3537 // Empty separator case.
3538 __ bind(&empty_separator_loop);
3539 // Live values in registers:
3540 // result_pos: the position to which we are currently copying characters.
3541 // element: Current array element.
3542 // elements_end: Array end.
3543
3544 // Copy next array element to the result.
3545 __ lw(string, MemOperand(element));
3546 __ Addu(element, element, kPointerSize);
3547 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3548 __ SmiUntag(string_length);
3549 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3550 __ CopyBytes(string, result_pos, string_length, scratch1);
3551 // End while (element < elements_end).
3552 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3553 ASSERT(result.is(v0));
3554 __ Branch(&done);
3555
3556 // One-character separator case.
3557 __ bind(&one_char_separator);
3558 // Replace separator with its ascii character value.
3559 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3560 // Jump into the loop after the code that copies the separator, so the first
3561 // element is not preceded by a separator.
3562 __ jmp(&one_char_separator_loop_entry);
3563
3564 __ bind(&one_char_separator_loop);
3565 // Live values in registers:
3566 // result_pos: the position to which we are currently copying characters.
3567 // element: Current array element.
3568 // elements_end: Array end.
3569 // separator: Single separator ascii char (in lower byte).
3570
3571 // Copy the separator character to the result.
3572 __ sb(separator, MemOperand(result_pos));
3573 __ Addu(result_pos, result_pos, 1);
3574
3575 // Copy next array element to the result.
3576 __ bind(&one_char_separator_loop_entry);
3577 __ lw(string, MemOperand(element));
3578 __ Addu(element, element, kPointerSize);
3579 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3580 __ SmiUntag(string_length);
3581 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3582 __ CopyBytes(string, result_pos, string_length, scratch1);
3583 // End while (element < elements_end).
3584 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3585 ASSERT(result.is(v0));
3586 __ Branch(&done);
3587
3588 // Long separator case (separator is more than one character). Entry is at the
3589 // label long_separator below.
3590 __ bind(&long_separator_loop);
3591 // Live values in registers:
3592 // result_pos: the position to which we are currently copying characters.
3593 // element: Current array element.
3594 // elements_end: Array end.
3595 // separator: Separator string.
3596
3597 // Copy the separator to the result.
3598 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3599 __ SmiUntag(string_length);
3600 __ Addu(string,
3601 separator,
3602 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3603 __ CopyBytes(string, result_pos, string_length, scratch1);
3604
3605 __ bind(&long_separator);
3606 __ lw(string, MemOperand(element));
3607 __ Addu(element, element, kPointerSize);
3608 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3609 __ SmiUntag(string_length);
3610 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3611 __ CopyBytes(string, result_pos, string_length, scratch1);
3612 // End while (element < elements_end).
3613 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3614 ASSERT(result.is(v0));
3615 __ Branch(&done);
3616
3617 __ bind(&bailout);
3618 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3619 __ bind(&done);
3620 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003621}
3622
3623
ager@chromium.org5c838252010-02-19 08:53:10 +00003624void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003625 Handle<String> name = expr->name();
3626 if (name->length() > 0 && name->Get(0) == '_') {
3627 Comment cmnt(masm_, "[ InlineRuntimeCall");
3628 EmitInlineRuntimeCall(expr);
3629 return;
3630 }
3631
3632 Comment cmnt(masm_, "[ CallRuntime");
3633 ZoneList<Expression*>* args = expr->arguments();
3634
3635 if (expr->is_jsruntime()) {
3636 // Prepare for calling JS runtime function.
3637 __ lw(a0, GlobalObjectOperand());
3638 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3639 __ push(a0);
3640 }
3641
3642 // Push the arguments ("left-to-right").
3643 int arg_count = args->length();
3644 for (int i = 0; i < arg_count; i++) {
3645 VisitForStackValue(args->at(i));
3646 }
3647
3648 if (expr->is_jsruntime()) {
3649 // Call the JS runtime function.
3650 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003651 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003652 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00003653 isolate()->stub_cache()->ComputeCallInitialize(arg_count,
3654 NOT_IN_LOOP,
3655 mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003656 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003657 // Restore context register.
3658 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3659 } else {
3660 // Call the C runtime function.
3661 __ CallRuntime(expr->function(), arg_count);
3662 }
3663 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003664}
3665
3666
3667void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003668 switch (expr->op()) {
3669 case Token::DELETE: {
3670 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
3671 Property* prop = expr->expression()->AsProperty();
3672 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
3673
3674 if (prop != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003675 VisitForStackValue(prop->obj());
3676 VisitForStackValue(prop->key());
3677 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3678 __ push(a1);
3679 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3680 context()->Plug(v0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003681 } else if (var != NULL) {
3682 // Delete of an unqualified identifier is disallowed in strict mode
3683 // but "delete this" is.
3684 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
3685 if (var->is_global()) {
3686 __ lw(a2, GlobalObjectOperand());
3687 __ li(a1, Operand(var->name()));
3688 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3689 __ Push(a2, a1, a0);
3690 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3691 context()->Plug(v0);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003692 } else if (var->rewrite() != NULL &&
3693 var->rewrite()->type() != Slot::LOOKUP) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003694 // Result of deleting non-global, non-dynamic variables is false.
3695 // The subexpression does not have side effects.
3696 context()->Plug(false);
3697 } else {
3698 // Non-global variable. Call the runtime to try to delete from the
3699 // context where the variable was introduced.
3700 __ push(context_register());
3701 __ li(a2, Operand(var->name()));
3702 __ push(a2);
3703 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3704 context()->Plug(v0);
3705 }
3706 } else {
3707 // Result of deleting non-property, non-variable reference is true.
3708 // The subexpression may have side effects.
3709 VisitForEffect(expr->expression());
3710 context()->Plug(true);
3711 }
3712 break;
3713 }
3714
3715 case Token::VOID: {
3716 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3717 VisitForEffect(expr->expression());
3718 context()->Plug(Heap::kUndefinedValueRootIndex);
3719 break;
3720 }
3721
3722 case Token::NOT: {
3723 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3724 if (context()->IsEffect()) {
3725 // Unary NOT has no side effects so it's only necessary to visit the
3726 // subexpression. Match the optimizing compiler by not branching.
3727 VisitForEffect(expr->expression());
3728 } else {
3729 Label materialize_true, materialize_false;
3730 Label* if_true = NULL;
3731 Label* if_false = NULL;
3732 Label* fall_through = NULL;
3733
3734 // Notice that the labels are swapped.
3735 context()->PrepareTest(&materialize_true, &materialize_false,
3736 &if_false, &if_true, &fall_through);
3737 if (context()->IsTest()) ForwardBailoutToChild(expr);
3738 VisitForControl(expr->expression(), if_true, if_false, fall_through);
3739 context()->Plug(if_false, if_true); // Labels swapped.
3740 }
3741 break;
3742 }
3743
3744 case Token::TYPEOF: {
3745 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3746 { StackValueContext context(this);
3747 VisitForTypeofValue(expr->expression());
3748 }
3749 __ CallRuntime(Runtime::kTypeof, 1);
3750 context()->Plug(v0);
3751 break;
3752 }
3753
3754 case Token::ADD: {
3755 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3756 VisitForAccumulatorValue(expr->expression());
3757 Label no_conversion;
3758 __ JumpIfSmi(result_register(), &no_conversion);
3759 __ mov(a0, result_register());
3760 ToNumberStub convert_stub;
3761 __ CallStub(&convert_stub);
3762 __ bind(&no_conversion);
3763 context()->Plug(result_register());
3764 break;
3765 }
3766
3767 case Token::SUB:
3768 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3769 break;
3770
3771 case Token::BIT_NOT:
3772 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3773 break;
3774
3775 default:
3776 UNREACHABLE();
3777 }
3778}
3779
3780
3781void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3782 const char* comment) {
3783 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3784 Comment cmt(masm_, comment);
3785 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3786 UnaryOverwriteMode overwrite =
3787 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003788 UnaryOpStub stub(expr->op(), overwrite);
3789 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003790 VisitForAccumulatorValue(expr->expression());
3791 SetSourcePosition(expr->position());
3792 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003793 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003794 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003795}
3796
3797
3798void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003799 Comment cmnt(masm_, "[ CountOperation");
3800 SetSourcePosition(expr->position());
3801
3802 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3803 // as the left-hand side.
3804 if (!expr->expression()->IsValidLeftHandSide()) {
3805 VisitForEffect(expr->expression());
3806 return;
3807 }
3808
3809 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003810 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003811 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3812 LhsKind assign_type = VARIABLE;
3813 Property* prop = expr->expression()->AsProperty();
3814 // In case of a property we use the uninitialized expression context
3815 // of the key to detect a named property.
3816 if (prop != NULL) {
3817 assign_type =
3818 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3819 }
3820
3821 // Evaluate expression and get value.
3822 if (assign_type == VARIABLE) {
3823 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3824 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003825 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003826 } else {
3827 // Reserve space for result of postfix operation.
3828 if (expr->is_postfix() && !context()->IsEffect()) {
3829 __ li(at, Operand(Smi::FromInt(0)));
3830 __ push(at);
3831 }
3832 if (assign_type == NAMED_PROPERTY) {
3833 // Put the object both on the stack and in the accumulator.
3834 VisitForAccumulatorValue(prop->obj());
3835 __ push(v0);
3836 EmitNamedPropertyLoad(prop);
3837 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003838 VisitForStackValue(prop->obj());
3839 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003840 __ lw(a1, MemOperand(sp, 0));
3841 __ push(v0);
3842 EmitKeyedPropertyLoad(prop);
3843 }
3844 }
3845
3846 // We need a second deoptimization point after loading the value
3847 // in case evaluating the property load my have a side effect.
3848 if (assign_type == VARIABLE) {
3849 PrepareForBailout(expr->expression(), TOS_REG);
3850 } else {
3851 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3852 }
3853
3854 // Call ToNumber only if operand is not a smi.
3855 Label no_conversion;
3856 __ JumpIfSmi(v0, &no_conversion);
3857 __ mov(a0, v0);
3858 ToNumberStub convert_stub;
3859 __ CallStub(&convert_stub);
3860 __ bind(&no_conversion);
3861
3862 // Save result for postfix expressions.
3863 if (expr->is_postfix()) {
3864 if (!context()->IsEffect()) {
3865 // Save the result on the stack. If we have a named or keyed property
3866 // we store the result under the receiver that is currently on top
3867 // of the stack.
3868 switch (assign_type) {
3869 case VARIABLE:
3870 __ push(v0);
3871 break;
3872 case NAMED_PROPERTY:
3873 __ sw(v0, MemOperand(sp, kPointerSize));
3874 break;
3875 case KEYED_PROPERTY:
3876 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3877 break;
3878 }
3879 }
3880 }
3881 __ mov(a0, result_register());
3882
3883 // Inline smi case if we are in a loop.
3884 Label stub_call, done;
3885 JumpPatchSite patch_site(masm_);
3886
3887 int count_value = expr->op() == Token::INC ? 1 : -1;
3888 __ li(a1, Operand(Smi::FromInt(count_value)));
3889
3890 if (ShouldInlineSmiCase(expr->op())) {
3891 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
3892 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
3893
3894 // We could eliminate this smi check if we split the code at
3895 // the first smi check before calling ToNumber.
3896 patch_site.EmitJumpIfSmi(v0, &done);
3897 __ bind(&stub_call);
3898 }
3899
3900 // Record position before stub call.
3901 SetSourcePosition(expr->position());
3902
danno@chromium.org40cb8782011-05-25 07:58:50 +00003903 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003904 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003905 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003906 __ bind(&done);
3907
3908 // Store the value returned in v0.
3909 switch (assign_type) {
3910 case VARIABLE:
3911 if (expr->is_postfix()) {
3912 { EffectContext context(this);
3913 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3914 Token::ASSIGN);
3915 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3916 context.Plug(v0);
3917 }
3918 // For all contexts except EffectConstant we have the result on
3919 // top of the stack.
3920 if (!context()->IsEffect()) {
3921 context()->PlugTOS();
3922 }
3923 } else {
3924 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3925 Token::ASSIGN);
3926 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3927 context()->Plug(v0);
3928 }
3929 break;
3930 case NAMED_PROPERTY: {
3931 __ mov(a0, result_register()); // Value.
3932 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
3933 __ pop(a1); // Receiver.
3934 Handle<Code> ic = is_strict_mode()
3935 ? isolate()->builtins()->StoreIC_Initialize_Strict()
3936 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003937 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003938 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3939 if (expr->is_postfix()) {
3940 if (!context()->IsEffect()) {
3941 context()->PlugTOS();
3942 }
3943 } else {
3944 context()->Plug(v0);
3945 }
3946 break;
3947 }
3948 case KEYED_PROPERTY: {
3949 __ mov(a0, result_register()); // Value.
3950 __ pop(a1); // Key.
3951 __ pop(a2); // Receiver.
3952 Handle<Code> ic = is_strict_mode()
3953 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
3954 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003955 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003956 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3957 if (expr->is_postfix()) {
3958 if (!context()->IsEffect()) {
3959 context()->PlugTOS();
3960 }
3961 } else {
3962 context()->Plug(v0);
3963 }
3964 break;
3965 }
3966 }
ager@chromium.org5c838252010-02-19 08:53:10 +00003967}
3968
3969
lrn@chromium.org7516f052011-03-30 08:52:27 +00003970void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003971 VariableProxy* proxy = expr->AsVariableProxy();
3972 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
3973 Comment cmnt(masm_, "Global variable");
3974 __ lw(a0, GlobalObjectOperand());
3975 __ li(a2, Operand(proxy->name()));
3976 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
3977 // Use a regular load, not a contextual load, to avoid a reference
3978 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003979 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003980 PrepareForBailout(expr, TOS_REG);
3981 context()->Plug(v0);
3982 } else if (proxy != NULL &&
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003983 proxy->var()->rewrite() != NULL &&
3984 proxy->var()->rewrite()->type() == Slot::LOOKUP) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003985 Label done, slow;
3986
3987 // Generate code for loading from variables potentially shadowed
3988 // by eval-introduced variables.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003989 Slot* slot = proxy->var()->rewrite();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003990 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
3991
3992 __ bind(&slow);
3993 __ li(a0, Operand(proxy->name()));
3994 __ Push(cp, a0);
3995 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
3996 PrepareForBailout(expr, TOS_REG);
3997 __ bind(&done);
3998
3999 context()->Plug(v0);
4000 } else {
4001 // This expression cannot throw a reference error at the top level.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004002 VisitInCurrentContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004003 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004004}
4005
ager@chromium.org04921a82011-06-27 13:21:41 +00004006void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
4007 Handle<String> check,
4008 Label* if_true,
4009 Label* if_false,
4010 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004011 { AccumulatorValueContext context(this);
ager@chromium.org04921a82011-06-27 13:21:41 +00004012 VisitForTypeofValue(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004013 }
4014 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4015
4016 if (check->Equals(isolate()->heap()->number_symbol())) {
4017 __ JumpIfSmi(v0, if_true);
4018 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4019 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4020 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4021 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4022 __ JumpIfSmi(v0, if_false);
4023 // Check for undetectable objects => false.
4024 __ GetObjectType(v0, v0, a1);
4025 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4026 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4027 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4028 Split(eq, a1, Operand(zero_reg),
4029 if_true, if_false, fall_through);
4030 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4031 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4032 __ Branch(if_true, eq, v0, Operand(at));
4033 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4034 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004035 } else if (FLAG_harmony_typeof &&
4036 check->Equals(isolate()->heap()->null_symbol())) {
4037 __ LoadRoot(at, Heap::kNullValueRootIndex);
4038 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004039 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4040 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4041 __ Branch(if_true, eq, v0, Operand(at));
4042 __ JumpIfSmi(v0, if_false);
4043 // Check for undetectable objects => true.
4044 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4045 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4046 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4047 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4048 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4049 __ JumpIfSmi(v0, if_false);
4050 __ GetObjectType(v0, a1, v0); // Leave map in a1.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004051 Split(ge, v0, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004052 if_true, if_false, fall_through);
4053
4054 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4055 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004056 if (!FLAG_harmony_typeof) {
4057 __ LoadRoot(at, Heap::kNullValueRootIndex);
4058 __ Branch(if_true, eq, v0, Operand(at));
4059 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004060 // Check for JS objects => true.
4061 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004062 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004063 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004064 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004065 // Check for undetectable objects => false.
4066 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4067 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4068 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4069 } else {
4070 if (if_false != fall_through) __ jmp(if_false);
4071 }
ager@chromium.org04921a82011-06-27 13:21:41 +00004072}
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004073
ager@chromium.org04921a82011-06-27 13:21:41 +00004074
4075void FullCodeGenerator::EmitLiteralCompareUndefined(Expression* expr,
4076 Label* if_true,
4077 Label* if_false,
4078 Label* fall_through) {
4079 VisitForAccumulatorValue(expr);
4080 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4081
4082 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4083 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004084}
4085
4086
ager@chromium.org5c838252010-02-19 08:53:10 +00004087void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004088 Comment cmnt(masm_, "[ CompareOperation");
4089 SetSourcePosition(expr->position());
4090
4091 // Always perform the comparison for its control flow. Pack the result
4092 // into the expression's context after the comparison is performed.
4093
4094 Label materialize_true, materialize_false;
4095 Label* if_true = NULL;
4096 Label* if_false = NULL;
4097 Label* fall_through = NULL;
4098 context()->PrepareTest(&materialize_true, &materialize_false,
4099 &if_true, &if_false, &fall_through);
4100
4101 // First we try a fast inlined version of the compare when one of
4102 // the operands is a literal.
ager@chromium.org04921a82011-06-27 13:21:41 +00004103 if (TryLiteralCompare(expr, if_true, if_false, fall_through)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004104 context()->Plug(if_true, if_false);
4105 return;
4106 }
4107
ager@chromium.org04921a82011-06-27 13:21:41 +00004108 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004109 VisitForStackValue(expr->left());
4110 switch (op) {
4111 case Token::IN:
4112 VisitForStackValue(expr->right());
4113 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
4114 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
4115 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4116 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4117 break;
4118
4119 case Token::INSTANCEOF: {
4120 VisitForStackValue(expr->right());
4121 InstanceofStub stub(InstanceofStub::kNoFlags);
4122 __ CallStub(&stub);
4123 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4124 // The stub returns 0 for true.
4125 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4126 break;
4127 }
4128
4129 default: {
4130 VisitForAccumulatorValue(expr->right());
4131 Condition cc = eq;
4132 bool strict = false;
4133 switch (op) {
4134 case Token::EQ_STRICT:
4135 strict = true;
4136 // Fall through.
4137 case Token::EQ:
4138 cc = eq;
4139 __ mov(a0, result_register());
4140 __ pop(a1);
4141 break;
4142 case Token::LT:
4143 cc = lt;
4144 __ mov(a0, result_register());
4145 __ pop(a1);
4146 break;
4147 case Token::GT:
4148 // Reverse left and right sides to obtain ECMA-262 conversion order.
4149 cc = lt;
4150 __ mov(a1, result_register());
4151 __ pop(a0);
4152 break;
4153 case Token::LTE:
4154 // Reverse left and right sides to obtain ECMA-262 conversion order.
4155 cc = ge;
4156 __ mov(a1, result_register());
4157 __ pop(a0);
4158 break;
4159 case Token::GTE:
4160 cc = ge;
4161 __ mov(a0, result_register());
4162 __ pop(a1);
4163 break;
4164 case Token::IN:
4165 case Token::INSTANCEOF:
4166 default:
4167 UNREACHABLE();
4168 }
4169
4170 bool inline_smi_code = ShouldInlineSmiCase(op);
4171 JumpPatchSite patch_site(masm_);
4172 if (inline_smi_code) {
4173 Label slow_case;
4174 __ Or(a2, a0, Operand(a1));
4175 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4176 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4177 __ bind(&slow_case);
4178 }
4179 // Record position and call the compare IC.
4180 SetSourcePosition(expr->position());
4181 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004182 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004183 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004184 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4185 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4186 }
4187 }
4188
4189 // Convert the result of the comparison into one expected for this
4190 // expression's context.
4191 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004192}
4193
4194
lrn@chromium.org7516f052011-03-30 08:52:27 +00004195void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004196 Comment cmnt(masm_, "[ CompareToNull");
4197 Label materialize_true, materialize_false;
4198 Label* if_true = NULL;
4199 Label* if_false = NULL;
4200 Label* fall_through = NULL;
4201 context()->PrepareTest(&materialize_true, &materialize_false,
4202 &if_true, &if_false, &fall_through);
4203
4204 VisitForAccumulatorValue(expr->expression());
4205 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4206 __ mov(a0, result_register());
4207 __ LoadRoot(a1, Heap::kNullValueRootIndex);
4208 if (expr->is_strict()) {
4209 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4210 } else {
4211 __ Branch(if_true, eq, a0, Operand(a1));
4212 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
4213 __ Branch(if_true, eq, a0, Operand(a1));
4214 __ And(at, a0, Operand(kSmiTagMask));
4215 __ Branch(if_false, eq, at, Operand(zero_reg));
4216 // It can be an undetectable object.
4217 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4218 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4219 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4220 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4221 }
4222 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004223}
4224
4225
ager@chromium.org5c838252010-02-19 08:53:10 +00004226void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004227 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4228 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004229}
4230
4231
lrn@chromium.org7516f052011-03-30 08:52:27 +00004232Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004233 return v0;
4234}
ager@chromium.org5c838252010-02-19 08:53:10 +00004235
4236
lrn@chromium.org7516f052011-03-30 08:52:27 +00004237Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004238 return cp;
4239}
4240
4241
ager@chromium.org5c838252010-02-19 08:53:10 +00004242void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004243 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4244 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004245}
4246
4247
4248void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004249 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004250}
4251
4252
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004253void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4254 Scope* declaration_scope = scope()->DeclarationScope();
4255 if (declaration_scope->is_global_scope()) {
4256 // Contexts nested in the global context have a canonical empty function
4257 // as their closure, not the anonymous closure containing the global
4258 // code. Pass a smi sentinel and let the runtime look up the empty
4259 // function.
4260 __ li(at, Operand(Smi::FromInt(0)));
4261 } else if (declaration_scope->is_eval_scope()) {
4262 // Contexts created by a call to eval have the same closure as the
4263 // context calling eval, not the anonymous closure containing the eval
4264 // code. Fetch it from the context.
4265 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4266 } else {
4267 ASSERT(declaration_scope->is_function_scope());
4268 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4269 }
4270 __ push(at);
4271}
4272
4273
ager@chromium.org5c838252010-02-19 08:53:10 +00004274// ----------------------------------------------------------------------------
4275// Non-local control flow support.
4276
4277void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004278 ASSERT(!result_register().is(a1));
4279 // Store result register while executing finally block.
4280 __ push(result_register());
4281 // Cook return address in link register to stack (smi encoded Code* delta).
4282 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4283 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004284 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004285 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4286 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004287}
4288
4289
4290void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004291 ASSERT(!result_register().is(a1));
4292 // Restore result register from stack.
4293 __ pop(a1);
4294 // Uncook return address and return.
4295 __ pop(result_register());
4296 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4297 __ sra(a1, a1, 1); // Un-smi-tag value.
4298 __ Addu(at, a1, Operand(masm_->CodeObject()));
4299 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004300}
4301
4302
4303#undef __
4304
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004305#define __ ACCESS_MASM(masm())
4306
4307FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4308 int* stack_depth,
4309 int* context_length) {
4310 // The macros used here must preserve the result register.
4311
4312 // Because the handler block contains the context of the finally
4313 // code, we can restore it directly from there for the finally code
4314 // rather than iteratively unwinding contexts via their previous
4315 // links.
4316 __ Drop(*stack_depth); // Down to the handler block.
4317 if (*context_length > 0) {
4318 // Restore the context to its dedicated register and the stack.
4319 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4320 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4321 }
4322 __ PopTryHandler();
4323 __ Call(finally_entry_);
4324
4325 *stack_depth = 0;
4326 *context_length = 0;
4327 return previous_;
4328}
4329
4330
4331#undef __
4332
ager@chromium.org5c838252010-02-19 08:53:10 +00004333} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004334
4335#endif // V8_TARGET_ARCH_MIPS