blob: cf48ccfd4fa0af4798baacd37b68aa3a2150deef [file] [log] [blame]
lrn@chromium.org7516f052011-03-30 08:52:27 +00001// Copyright 2011 the V8 project authors. All rights reserved.
ager@chromium.org5c838252010-02-19 08:53:10 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000030#if defined(V8_TARGET_ARCH_MIPS)
31
lrn@chromium.org7516f052011-03-30 08:52:27 +000032// Note on Mips implementation:
33//
34// The result_register() for mips is the 'v0' register, which is defined
35// by the ABI to contain function return values. However, the first
36// parameter to a function is defined to be 'a0'. So there are many
37// places where we have to move a previous result in v0 to a0 for the
38// next call: mov(a0, v0). This is not needed on the other architectures.
39
40#include "code-stubs.h"
karlklose@chromium.org83a47282011-05-11 11:54:09 +000041#include "codegen.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000042#include "compiler.h"
43#include "debug.h"
44#include "full-codegen.h"
45#include "parser.h"
lrn@chromium.org7516f052011-03-30 08:52:27 +000046#include "scopes.h"
47#include "stub-cache.h"
48
49#include "mips/code-stubs-mips.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000050
51namespace v8 {
52namespace internal {
53
54#define __ ACCESS_MASM(masm_)
55
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000056
danno@chromium.org40cb8782011-05-25 07:58:50 +000057static unsigned GetPropertyId(Property* property) {
danno@chromium.org40cb8782011-05-25 07:58:50 +000058 return property->id();
59}
60
61
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000062// A patch site is a location in the code which it is possible to patch. This
63// class has a number of methods to emit the code which is patchable and the
64// method EmitPatchInfo to record a marker back to the patchable code. This
65// marker is a andi at, rx, #yyy instruction, and x * 0x0000ffff + yyy (raw 16
66// bit immediate value is used) is the delta from the pc to the first
67// instruction of the patchable code.
68class JumpPatchSite BASE_EMBEDDED {
69 public:
70 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
71#ifdef DEBUG
72 info_emitted_ = false;
73#endif
74 }
75
76 ~JumpPatchSite() {
77 ASSERT(patch_site_.is_bound() == info_emitted_);
78 }
79
80 // When initially emitting this ensure that a jump is always generated to skip
81 // the inlined smi code.
82 void EmitJumpIfNotSmi(Register reg, Label* target) {
83 ASSERT(!patch_site_.is_bound() && !info_emitted_);
84 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
85 __ bind(&patch_site_);
86 __ andi(at, reg, 0);
87 // Always taken before patched.
88 __ Branch(target, eq, at, Operand(zero_reg));
89 }
90
91 // When initially emitting this ensure that a jump is never generated to skip
92 // the inlined smi code.
93 void EmitJumpIfSmi(Register reg, Label* target) {
94 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
95 ASSERT(!patch_site_.is_bound() && !info_emitted_);
96 __ bind(&patch_site_);
97 __ andi(at, reg, 0);
98 // Never taken before patched.
99 __ Branch(target, ne, at, Operand(zero_reg));
100 }
101
102 void EmitPatchInfo() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000103 if (patch_site_.is_bound()) {
104 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
105 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
106 __ andi(at, reg, delta_to_patch_site % kImm16Mask);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000107#ifdef DEBUG
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000108 info_emitted_ = true;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000109#endif
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000110 } else {
111 __ nop(); // Signals no inlined code.
112 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000113 }
114
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000115 private:
116 MacroAssembler* masm_;
117 Label patch_site_;
118#ifdef DEBUG
119 bool info_emitted_;
120#endif
121};
122
123
lrn@chromium.org7516f052011-03-30 08:52:27 +0000124// Generate code for a JS function. On entry to the function the receiver
125// and arguments have been pushed on the stack left to right. The actual
126// argument count matches the formal parameter count expected by the
127// function.
128//
129// The live registers are:
130// o a1: the JS function object being called (ie, ourselves)
131// o cp: our context
132// o fp: our caller's frame pointer
133// o sp: stack pointer
134// o ra: return address
135//
136// The function builds a JS frame. Please see JavaScriptFrameConstants in
137// frames-mips.h for its layout.
138void FullCodeGenerator::Generate(CompilationInfo* info) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000139 ASSERT(info_ == NULL);
140 info_ = info;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000141 scope_ = info->scope();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000142 SetFunctionPosition(function());
143 Comment cmnt(masm_, "[ function compiled by full code generator");
144
145#ifdef DEBUG
146 if (strlen(FLAG_stop_at) > 0 &&
147 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
148 __ stop("stop-at");
149 }
150#endif
151
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000152 // Strict mode functions and builtins need to replace the receiver
153 // with undefined when called as functions (without an explicit
154 // receiver object). t1 is zero for method calls and non-zero for
155 // function calls.
156 if (info->is_strict_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000157 Label ok;
158 __ Branch(&ok, eq, t1, Operand(zero_reg));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000159 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000160 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
161 __ sw(a2, MemOperand(sp, receiver_offset));
162 __ bind(&ok);
163 }
164
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000165 int locals_count = info->scope()->num_stack_slots();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000166
167 __ Push(ra, fp, cp, a1);
168 if (locals_count > 0) {
169 // Load undefined value here, so the value is ready for the loop
170 // below.
171 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
172 }
173 // Adjust fp to point to caller's fp.
174 __ Addu(fp, sp, Operand(2 * kPointerSize));
175
176 { Comment cmnt(masm_, "[ Allocate locals");
177 for (int i = 0; i < locals_count; i++) {
178 __ push(at);
179 }
180 }
181
182 bool function_in_register = true;
183
184 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000185 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000186 if (heap_slots > 0) {
187 Comment cmnt(masm_, "[ Allocate local context");
188 // Argument to NewContext is the function, which is in a1.
189 __ push(a1);
190 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
191 FastNewContextStub stub(heap_slots);
192 __ CallStub(&stub);
193 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000194 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000195 }
196 function_in_register = false;
197 // Context is returned in both v0 and cp. It replaces the context
198 // passed to us. It's saved in the stack and kept live in cp.
199 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
200 // Copy any necessary parameters into the context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000201 int num_parameters = info->scope()->num_parameters();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000202 for (int i = 0; i < num_parameters; i++) {
203 Slot* slot = scope()->parameter(i)->AsSlot();
204 if (slot != NULL && slot->type() == Slot::CONTEXT) {
205 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
206 (num_parameters - 1 - i) * kPointerSize;
207 // Load parameter from stack.
208 __ lw(a0, MemOperand(fp, parameter_offset));
209 // Store it in the context.
210 __ li(a1, Operand(Context::SlotOffset(slot->index())));
211 __ addu(a2, cp, a1);
212 __ sw(a0, MemOperand(a2, 0));
213 // Update the write barrier. This clobbers all involved
214 // registers, so we have to use two more registers to avoid
215 // clobbering cp.
216 __ mov(a2, cp);
217 __ RecordWrite(a2, a1, a3);
218 }
219 }
220 }
221
222 Variable* arguments = scope()->arguments();
223 if (arguments != NULL) {
224 // Function uses arguments object.
225 Comment cmnt(masm_, "[ Allocate arguments object");
226 if (!function_in_register) {
227 // Load this again, if it's used by the local context below.
228 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
229 } else {
230 __ mov(a3, a1);
231 }
232 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000233 int num_parameters = info->scope()->num_parameters();
234 int offset = num_parameters * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000235 __ Addu(a2, fp,
236 Operand(StandardFrameConstants::kCallerSPOffset + offset));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000237 __ li(a1, Operand(Smi::FromInt(num_parameters)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000238 __ Push(a3, a2, a1);
239
240 // Arguments to ArgumentsAccessStub:
241 // function, receiver address, parameter count.
242 // The stub will rewrite receiever and parameter count if the previous
243 // stack frame was an arguments adapter frame.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000244 ArgumentsAccessStub::Type type;
245 if (is_strict_mode()) {
246 type = ArgumentsAccessStub::NEW_STRICT;
247 } else if (function()->has_duplicate_parameters()) {
248 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
249 } else {
250 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
251 }
252 ArgumentsAccessStub stub(type);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000253 __ CallStub(&stub);
254
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000255 Move(arguments->AsSlot(), v0, a1, a2);
256 }
257
258 if (FLAG_trace) {
259 __ CallRuntime(Runtime::kTraceEnter, 0);
260 }
261
262 // Visit the declarations and body unless there is an illegal
263 // redeclaration.
264 if (scope()->HasIllegalRedeclaration()) {
265 Comment cmnt(masm_, "[ Declarations");
266 scope()->VisitIllegalRedeclaration(this);
267
268 } else {
269 { Comment cmnt(masm_, "[ Declarations");
270 // For named function expressions, declare the function name as a
271 // constant.
272 if (scope()->is_function_scope() && scope()->function() != NULL) {
273 EmitDeclaration(scope()->function(), Variable::CONST, NULL);
274 }
275 VisitDeclarations(scope()->declarations());
276 }
277
278 { Comment cmnt(masm_, "[ Stack check");
279 PrepareForBailoutForId(AstNode::kFunctionEntryId, NO_REGISTERS);
280 Label ok;
281 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
282 __ Branch(&ok, hs, sp, Operand(t0));
283 StackCheckStub stub;
284 __ CallStub(&stub);
285 __ bind(&ok);
286 }
287
288 { Comment cmnt(masm_, "[ Body");
289 ASSERT(loop_depth() == 0);
290 VisitStatements(function()->body());
291 ASSERT(loop_depth() == 0);
292 }
293 }
294
295 // Always emit a 'return undefined' in case control fell off the end of
296 // the body.
297 { Comment cmnt(masm_, "[ return <undefined>;");
298 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
299 }
300 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000301}
302
303
304void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000305 ASSERT(Smi::FromInt(0) == 0);
306 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000307}
308
309
310void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000311 Comment cmnt(masm_, "[ Stack check");
312 Label ok;
313 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
314 __ Branch(&ok, hs, sp, Operand(t0));
315 StackCheckStub stub;
316 // Record a mapping of this PC offset to the OSR id. This is used to find
317 // the AST id from the unoptimized code in order to use it as a key into
318 // the deoptimization input data found in the optimized code.
319 RecordStackCheck(stmt->OsrEntryId());
320
321 __ CallStub(&stub);
322 __ bind(&ok);
323 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
324 // Record a mapping of the OSR id to this PC. This is used if the OSR
325 // entry becomes the target of a bailout. We don't expect it to be, but
326 // we want it to work if it is.
327 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000328}
329
330
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000331void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000332 Comment cmnt(masm_, "[ Return sequence");
333 if (return_label_.is_bound()) {
334 __ Branch(&return_label_);
335 } else {
336 __ bind(&return_label_);
337 if (FLAG_trace) {
338 // Push the return value on the stack as the parameter.
339 // Runtime::TraceExit returns its parameter in v0.
340 __ push(v0);
341 __ CallRuntime(Runtime::kTraceExit, 1);
342 }
343
344#ifdef DEBUG
345 // Add a label for checking the size of the code used for returning.
346 Label check_exit_codesize;
347 masm_->bind(&check_exit_codesize);
348#endif
349 // Make sure that the constant pool is not emitted inside of the return
350 // sequence.
351 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
352 // Here we use masm_-> instead of the __ macro to avoid the code coverage
353 // tool from instrumenting as we rely on the code size here.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000354 int32_t sp_delta = (info_->scope()->num_parameters() + 1) * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000355 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
356 __ RecordJSReturn();
357 masm_->mov(sp, fp);
358 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
359 masm_->Addu(sp, sp, Operand(sp_delta));
360 masm_->Jump(ra);
361 }
362
363#ifdef DEBUG
364 // Check that the size of the code used for returning is large enough
365 // for the debugger's requirements.
366 ASSERT(Assembler::kJSReturnSequenceInstructions <=
367 masm_->InstructionsGeneratedSince(&check_exit_codesize));
368#endif
369 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000370}
371
372
lrn@chromium.org7516f052011-03-30 08:52:27 +0000373void FullCodeGenerator::EffectContext::Plug(Slot* slot) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000374}
375
376
lrn@chromium.org7516f052011-03-30 08:52:27 +0000377void FullCodeGenerator::AccumulatorValueContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000378 codegen()->Move(result_register(), slot);
ager@chromium.org5c838252010-02-19 08:53:10 +0000379}
380
381
lrn@chromium.org7516f052011-03-30 08:52:27 +0000382void FullCodeGenerator::StackValueContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000383 codegen()->Move(result_register(), slot);
384 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000385}
386
387
lrn@chromium.org7516f052011-03-30 08:52:27 +0000388void FullCodeGenerator::TestContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000389 // For simplicity we always test the accumulator register.
390 codegen()->Move(result_register(), slot);
391 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000392 codegen()->DoTest(this);
ager@chromium.org5c838252010-02-19 08:53:10 +0000393}
394
395
lrn@chromium.org7516f052011-03-30 08:52:27 +0000396void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000397}
398
399
lrn@chromium.org7516f052011-03-30 08:52:27 +0000400void FullCodeGenerator::AccumulatorValueContext::Plug(
401 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000402 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000403}
404
405
406void FullCodeGenerator::StackValueContext::Plug(
407 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000408 __ LoadRoot(result_register(), index);
409 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000410}
411
412
413void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000414 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
415 true,
416 true_label_,
417 false_label_);
418 if (index == Heap::kUndefinedValueRootIndex ||
419 index == Heap::kNullValueRootIndex ||
420 index == Heap::kFalseValueRootIndex) {
421 if (false_label_ != fall_through_) __ Branch(false_label_);
422 } else if (index == Heap::kTrueValueRootIndex) {
423 if (true_label_ != fall_through_) __ Branch(true_label_);
424 } else {
425 __ LoadRoot(result_register(), index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000426 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000427 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000428}
429
430
431void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000432}
433
434
435void FullCodeGenerator::AccumulatorValueContext::Plug(
436 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000437 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000438}
439
440
441void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000442 // Immediates cannot be pushed directly.
443 __ li(result_register(), Operand(lit));
444 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000445}
446
447
448void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000449 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
450 true,
451 true_label_,
452 false_label_);
453 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
454 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
455 if (false_label_ != fall_through_) __ Branch(false_label_);
456 } else if (lit->IsTrue() || lit->IsJSObject()) {
457 if (true_label_ != fall_through_) __ Branch(true_label_);
458 } else if (lit->IsString()) {
459 if (String::cast(*lit)->length() == 0) {
460 if (false_label_ != fall_through_) __ Branch(false_label_);
461 } else {
462 if (true_label_ != fall_through_) __ Branch(true_label_);
463 }
464 } else if (lit->IsSmi()) {
465 if (Smi::cast(*lit)->value() == 0) {
466 if (false_label_ != fall_through_) __ Branch(false_label_);
467 } else {
468 if (true_label_ != fall_through_) __ Branch(true_label_);
469 }
470 } else {
471 // For simplicity we always test the accumulator register.
472 __ li(result_register(), Operand(lit));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000473 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000474 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000475}
476
477
478void FullCodeGenerator::EffectContext::DropAndPlug(int count,
479 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000480 ASSERT(count > 0);
481 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000482}
483
484
485void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
486 int count,
487 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000488 ASSERT(count > 0);
489 __ Drop(count);
490 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000491}
492
493
494void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
495 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000496 ASSERT(count > 0);
497 if (count > 1) __ Drop(count - 1);
498 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000499}
500
501
502void FullCodeGenerator::TestContext::DropAndPlug(int count,
503 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000504 ASSERT(count > 0);
505 // For simplicity we always test the accumulator register.
506 __ Drop(count);
507 __ Move(result_register(), reg);
508 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000509 codegen()->DoTest(this);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000510}
511
512
513void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
514 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000515 ASSERT(materialize_true == materialize_false);
516 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000517}
518
519
520void FullCodeGenerator::AccumulatorValueContext::Plug(
521 Label* materialize_true,
522 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000523 Label done;
524 __ bind(materialize_true);
525 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
526 __ Branch(&done);
527 __ bind(materialize_false);
528 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
529 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000530}
531
532
533void FullCodeGenerator::StackValueContext::Plug(
534 Label* materialize_true,
535 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000536 Label done;
537 __ bind(materialize_true);
538 __ LoadRoot(at, Heap::kTrueValueRootIndex);
539 __ push(at);
540 __ Branch(&done);
541 __ bind(materialize_false);
542 __ LoadRoot(at, Heap::kFalseValueRootIndex);
543 __ push(at);
544 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000545}
546
547
548void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
549 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000550 ASSERT(materialize_true == true_label_);
551 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000552}
553
554
555void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000556}
557
558
559void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000560 Heap::RootListIndex value_root_index =
561 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
562 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000563}
564
565
566void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000567 Heap::RootListIndex value_root_index =
568 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
569 __ LoadRoot(at, value_root_index);
570 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000571}
572
573
574void FullCodeGenerator::TestContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000575 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
576 true,
577 true_label_,
578 false_label_);
579 if (flag) {
580 if (true_label_ != fall_through_) __ Branch(true_label_);
581 } else {
582 if (false_label_ != fall_through_) __ Branch(false_label_);
583 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000584}
585
586
whesse@chromium.org7b260152011-06-20 15:33:18 +0000587void FullCodeGenerator::DoTest(Expression* condition,
588 Label* if_true,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000589 Label* if_false,
590 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000591 if (CpuFeatures::IsSupported(FPU)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000592 ToBooleanStub stub(result_register());
593 __ CallStub(&stub);
594 __ mov(at, zero_reg);
595 } else {
596 // Call the runtime to find the boolean value of the source and then
597 // translate it into control flow to the pair of labels.
598 __ push(result_register());
599 __ CallRuntime(Runtime::kToBool, 1);
600 __ LoadRoot(at, Heap::kFalseValueRootIndex);
601 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000602 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000603}
604
605
lrn@chromium.org7516f052011-03-30 08:52:27 +0000606void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000607 Register lhs,
608 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000609 Label* if_true,
610 Label* if_false,
611 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000612 if (if_false == fall_through) {
613 __ Branch(if_true, cc, lhs, rhs);
614 } else if (if_true == fall_through) {
615 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
616 } else {
617 __ Branch(if_true, cc, lhs, rhs);
618 __ Branch(if_false);
619 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000620}
621
622
623MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000624 switch (slot->type()) {
625 case Slot::PARAMETER:
626 case Slot::LOCAL:
627 return MemOperand(fp, SlotOffset(slot));
628 case Slot::CONTEXT: {
629 int context_chain_length =
630 scope()->ContextChainLength(slot->var()->scope());
631 __ LoadContext(scratch, context_chain_length);
632 return ContextOperand(scratch, slot->index());
633 }
634 case Slot::LOOKUP:
635 UNREACHABLE();
636 }
637 UNREACHABLE();
638 return MemOperand(v0, 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000639}
640
641
642void FullCodeGenerator::Move(Register destination, Slot* source) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000643 // Use destination as scratch.
644 MemOperand slot_operand = EmitSlotSearch(source, destination);
645 __ lw(destination, slot_operand);
ager@chromium.org5c838252010-02-19 08:53:10 +0000646}
647
648
lrn@chromium.org7516f052011-03-30 08:52:27 +0000649void FullCodeGenerator::PrepareForBailoutBeforeSplit(State state,
650 bool should_normalize,
651 Label* if_true,
652 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000653 // Only prepare for bailouts before splits if we're in a test
654 // context. Otherwise, we let the Visit function deal with the
655 // preparation to avoid preparing with the same AST id twice.
656 if (!context()->IsTest() || !info_->IsOptimizable()) return;
657
658 Label skip;
659 if (should_normalize) __ Branch(&skip);
660
661 ForwardBailoutStack* current = forward_bailout_stack_;
662 while (current != NULL) {
663 PrepareForBailout(current->expr(), state);
664 current = current->parent();
665 }
666
667 if (should_normalize) {
668 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
669 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
670 __ bind(&skip);
671 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000672}
673
674
ager@chromium.org5c838252010-02-19 08:53:10 +0000675void FullCodeGenerator::Move(Slot* dst,
676 Register src,
677 Register scratch1,
678 Register scratch2) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000679 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
680 ASSERT(!scratch1.is(src) && !scratch2.is(src));
681 MemOperand location = EmitSlotSearch(dst, scratch1);
682 __ sw(src, location);
683 // Emit the write barrier code if the location is in the heap.
684 if (dst->type() == Slot::CONTEXT) {
685 __ RecordWrite(scratch1,
686 Operand(Context::SlotOffset(dst->index())),
687 scratch2,
688 src);
689 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000690}
691
692
lrn@chromium.org7516f052011-03-30 08:52:27 +0000693void FullCodeGenerator::EmitDeclaration(Variable* variable,
694 Variable::Mode mode,
695 FunctionLiteral* function) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000696 Comment cmnt(masm_, "[ Declaration");
697 ASSERT(variable != NULL); // Must have been resolved.
698 Slot* slot = variable->AsSlot();
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000699 ASSERT(slot != NULL);
700 switch (slot->type()) {
701 case Slot::PARAMETER:
702 case Slot::LOCAL:
703 if (mode == Variable::CONST) {
704 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
705 __ sw(t0, MemOperand(fp, SlotOffset(slot)));
706 } else if (function != NULL) {
707 VisitForAccumulatorValue(function);
708 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000709 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000710 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000711
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000712 case Slot::CONTEXT:
713 // We bypass the general EmitSlotSearch because we know more about
714 // this specific context.
715
716 // The variable in the decl always resides in the current function
717 // context.
718 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
719 if (FLAG_debug_code) {
720 // Check that we're not inside a with or catch context.
721 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
722 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
723 __ Check(ne, "Declaration in with context.",
724 a1, Operand(t0));
725 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
726 __ Check(ne, "Declaration in catch context.",
727 a1, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000728 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000729 if (mode == Variable::CONST) {
730 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
731 __ sw(at, ContextOperand(cp, slot->index()));
732 // No write barrier since the_hole_value is in old space.
733 } else if (function != NULL) {
734 VisitForAccumulatorValue(function);
735 __ sw(result_register(), ContextOperand(cp, slot->index()));
736 int offset = Context::SlotOffset(slot->index());
737 // We know that we have written a function, which is not a smi.
738 __ mov(a1, cp);
739 __ RecordWrite(a1, Operand(offset), a2, result_register());
740 }
741 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000742
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000743 case Slot::LOOKUP: {
744 __ li(a2, Operand(variable->name()));
745 // Declaration nodes are always introduced in one of two modes.
746 ASSERT(mode == Variable::VAR ||
747 mode == Variable::CONST ||
748 mode == Variable::LET);
749 PropertyAttributes attr = (mode == Variable::CONST) ? READ_ONLY : NONE;
750 __ li(a1, Operand(Smi::FromInt(attr)));
751 // Push initial value, if any.
752 // Note: For variables we must not push an initial value (such as
753 // 'undefined') because we may have a (legal) redeclaration and we
754 // must not destroy the current value.
755 if (mode == Variable::CONST) {
756 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
757 __ Push(cp, a2, a1, a0);
758 } else if (function != NULL) {
759 __ Push(cp, a2, a1);
760 // Push initial value for function declaration.
761 VisitForStackValue(function);
762 } else {
763 ASSERT(Smi::FromInt(0) == 0);
764 // No initial value!
765 __ mov(a0, zero_reg); // Operand(Smi::FromInt(0)));
766 __ Push(cp, a2, a1, a0);
767 }
768 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
769 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000770 }
771 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000772}
773
774
ager@chromium.org5c838252010-02-19 08:53:10 +0000775void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000776 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
ager@chromium.org5c838252010-02-19 08:53:10 +0000777}
778
779
780void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000781 // Call the runtime to declare the globals.
782 // The context is the first argument.
783 __ li(a2, Operand(pairs));
784 __ li(a1, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
785 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
786 __ Push(cp, a2, a1, a0);
787 __ CallRuntime(Runtime::kDeclareGlobals, 4);
788 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000789}
790
791
lrn@chromium.org7516f052011-03-30 08:52:27 +0000792void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000793 Comment cmnt(masm_, "[ SwitchStatement");
794 Breakable nested_statement(this, stmt);
795 SetStatementPosition(stmt);
796
797 // Keep the switch value on the stack until a case matches.
798 VisitForStackValue(stmt->tag());
799 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
800
801 ZoneList<CaseClause*>* clauses = stmt->cases();
802 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
803
804 Label next_test; // Recycled for each test.
805 // Compile all the tests with branches to their bodies.
806 for (int i = 0; i < clauses->length(); i++) {
807 CaseClause* clause = clauses->at(i);
808 clause->body_target()->Unuse();
809
810 // The default is not a test, but remember it as final fall through.
811 if (clause->is_default()) {
812 default_clause = clause;
813 continue;
814 }
815
816 Comment cmnt(masm_, "[ Case comparison");
817 __ bind(&next_test);
818 next_test.Unuse();
819
820 // Compile the label expression.
821 VisitForAccumulatorValue(clause->label());
822 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
823
824 // Perform the comparison as if via '==='.
825 __ lw(a1, MemOperand(sp, 0)); // Switch value.
826 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
827 JumpPatchSite patch_site(masm_);
828 if (inline_smi_code) {
829 Label slow_case;
830 __ or_(a2, a1, a0);
831 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
832
833 __ Branch(&next_test, ne, a1, Operand(a0));
834 __ Drop(1); // Switch value is no longer needed.
835 __ Branch(clause->body_target());
836
837 __ bind(&slow_case);
838 }
839
840 // Record position before stub call for type feedback.
841 SetSourcePosition(clause->position());
842 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000843 __ Call(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000844 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +0000845
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000846 __ Branch(&next_test, ne, v0, Operand(zero_reg));
847 __ Drop(1); // Switch value is no longer needed.
848 __ Branch(clause->body_target());
849 }
850
851 // Discard the test value and jump to the default if present, otherwise to
852 // the end of the statement.
853 __ bind(&next_test);
854 __ Drop(1); // Switch value is no longer needed.
855 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000856 __ Branch(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000857 } else {
858 __ Branch(default_clause->body_target());
859 }
860
861 // Compile all the case bodies.
862 for (int i = 0; i < clauses->length(); i++) {
863 Comment cmnt(masm_, "[ Case body");
864 CaseClause* clause = clauses->at(i);
865 __ bind(clause->body_target());
866 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
867 VisitStatements(clause->statements());
868 }
869
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000870 __ bind(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000871 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000872}
873
874
875void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000876 Comment cmnt(masm_, "[ ForInStatement");
877 SetStatementPosition(stmt);
878
879 Label loop, exit;
880 ForIn loop_statement(this, stmt);
881 increment_loop_depth();
882
883 // Get the object to enumerate over. Both SpiderMonkey and JSC
884 // ignore null and undefined in contrast to the specification; see
885 // ECMA-262 section 12.6.4.
886 VisitForAccumulatorValue(stmt->enumerable());
887 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
888 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
889 __ Branch(&exit, eq, a0, Operand(at));
890 Register null_value = t1;
891 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
892 __ Branch(&exit, eq, a0, Operand(null_value));
893
894 // Convert the object to a JS object.
895 Label convert, done_convert;
896 __ JumpIfSmi(a0, &convert);
897 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000898 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000899 __ bind(&convert);
900 __ push(a0);
901 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
902 __ mov(a0, v0);
903 __ bind(&done_convert);
904 __ push(a0);
905
906 // Check cache validity in generated code. This is a fast case for
907 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
908 // guarantee cache validity, call the runtime system to check cache
909 // validity or get the property names in a fixed array.
910 Label next, call_runtime;
911 // Preload a couple of values used in the loop.
912 Register empty_fixed_array_value = t2;
913 __ LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
914 Register empty_descriptor_array_value = t3;
915 __ LoadRoot(empty_descriptor_array_value,
916 Heap::kEmptyDescriptorArrayRootIndex);
917 __ mov(a1, a0);
918 __ bind(&next);
919
920 // Check that there are no elements. Register a1 contains the
921 // current JS object we've reached through the prototype chain.
922 __ lw(a2, FieldMemOperand(a1, JSObject::kElementsOffset));
923 __ Branch(&call_runtime, ne, a2, Operand(empty_fixed_array_value));
924
925 // Check that instance descriptors are not empty so that we can
926 // check for an enum cache. Leave the map in a2 for the subsequent
927 // prototype load.
928 __ lw(a2, FieldMemOperand(a1, HeapObject::kMapOffset));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000929 __ lw(a3, FieldMemOperand(a2, Map::kInstanceDescriptorsOrBitField3Offset));
930 __ JumpIfSmi(a3, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000931
932 // Check that there is an enum cache in the non-empty instance
933 // descriptors (a3). This is the case if the next enumeration
934 // index field does not contain a smi.
935 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumerationIndexOffset));
936 __ JumpIfSmi(a3, &call_runtime);
937
938 // For all objects but the receiver, check that the cache is empty.
939 Label check_prototype;
940 __ Branch(&check_prototype, eq, a1, Operand(a0));
941 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumCacheBridgeCacheOffset));
942 __ Branch(&call_runtime, ne, a3, Operand(empty_fixed_array_value));
943
944 // Load the prototype from the map and loop if non-null.
945 __ bind(&check_prototype);
946 __ lw(a1, FieldMemOperand(a2, Map::kPrototypeOffset));
947 __ Branch(&next, ne, a1, Operand(null_value));
948
949 // The enum cache is valid. Load the map of the object being
950 // iterated over and use the cache for the iteration.
951 Label use_cache;
952 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
953 __ Branch(&use_cache);
954
955 // Get the set of properties to enumerate.
956 __ bind(&call_runtime);
957 __ push(a0); // Duplicate the enumerable object on the stack.
958 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
959
960 // If we got a map from the runtime call, we can do a fast
961 // modification check. Otherwise, we got a fixed array, and we have
962 // to do a slow check.
963 Label fixed_array;
964 __ mov(a2, v0);
965 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
966 __ LoadRoot(at, Heap::kMetaMapRootIndex);
967 __ Branch(&fixed_array, ne, a1, Operand(at));
968
969 // We got a map in register v0. Get the enumeration cache from it.
970 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000971 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000972 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
973 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
974
975 // Setup the four remaining stack slots.
976 __ push(v0); // Map.
977 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
978 __ li(a0, Operand(Smi::FromInt(0)));
979 // Push enumeration cache, enumeration cache length (as smi) and zero.
980 __ Push(a2, a1, a0);
981 __ jmp(&loop);
982
983 // We got a fixed array in register v0. Iterate through that.
984 __ bind(&fixed_array);
985 __ li(a1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
986 __ Push(a1, v0);
987 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
988 __ li(a0, Operand(Smi::FromInt(0)));
989 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
990
991 // Generate code for doing the condition check.
992 __ bind(&loop);
993 // Load the current count to a0, load the length to a1.
994 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
995 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000996 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000997
998 // Get the current entry of the array into register a3.
999 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1000 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1001 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1002 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1003 __ lw(a3, MemOperand(t0)); // Current entry.
1004
1005 // Get the expected map from the stack or a zero map in the
1006 // permanent slow case into register a2.
1007 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1008
1009 // Check if the expected map still matches that of the enumerable.
1010 // If not, we have to filter the key.
1011 Label update_each;
1012 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1013 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1014 __ Branch(&update_each, eq, t0, Operand(a2));
1015
1016 // Convert the entry to a string or (smi) 0 if it isn't a property
1017 // any more. If the property has been removed while iterating, we
1018 // just skip it.
1019 __ push(a1); // Enumerable.
1020 __ push(a3); // Current entry.
1021 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1022 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001023 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001024
1025 // Update the 'each' property or variable from the possibly filtered
1026 // entry in register a3.
1027 __ bind(&update_each);
1028 __ mov(result_register(), a3);
1029 // Perform the assignment as if via '='.
1030 { EffectContext context(this);
1031 EmitAssignment(stmt->each(), stmt->AssignmentId());
1032 }
1033
1034 // Generate code for the body of the loop.
1035 Visit(stmt->body());
1036
1037 // Generate code for the going to the next element by incrementing
1038 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001039 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001040 __ pop(a0);
1041 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1042 __ push(a0);
1043
1044 EmitStackCheck(stmt);
1045 __ Branch(&loop);
1046
1047 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001048 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001049 __ Drop(5);
1050
1051 // Exit and decrement the loop depth.
1052 __ bind(&exit);
1053 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001054}
1055
1056
1057void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1058 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001059 // Use the fast case closure allocation code that allocates in new
1060 // space for nested functions that don't need literals cloning. If
1061 // we're running with the --always-opt or the --prepare-always-opt
1062 // flag, we need to use the runtime function so that the new function
1063 // we are creating here gets a chance to have its code optimized and
1064 // doesn't just get a copy of the existing unoptimized code.
1065 if (!FLAG_always_opt &&
1066 !FLAG_prepare_always_opt &&
1067 !pretenure &&
1068 scope()->is_function_scope() &&
1069 info->num_literals() == 0) {
1070 FastNewClosureStub stub(info->strict_mode() ? kStrictMode : kNonStrictMode);
1071 __ li(a0, Operand(info));
1072 __ push(a0);
1073 __ CallStub(&stub);
1074 } else {
1075 __ li(a0, Operand(info));
1076 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1077 : Heap::kFalseValueRootIndex);
1078 __ Push(cp, a0, a1);
1079 __ CallRuntime(Runtime::kNewClosure, 3);
1080 }
1081 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001082}
1083
1084
1085void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001086 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001087 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001088}
1089
1090
1091void FullCodeGenerator::EmitLoadGlobalSlotCheckExtensions(
1092 Slot* slot,
1093 TypeofState typeof_state,
1094 Label* slow) {
1095 Register current = cp;
1096 Register next = a1;
1097 Register temp = a2;
1098
1099 Scope* s = scope();
1100 while (s != NULL) {
1101 if (s->num_heap_slots() > 0) {
1102 if (s->calls_eval()) {
1103 // Check that extension is NULL.
1104 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1105 __ Branch(slow, ne, temp, Operand(zero_reg));
1106 }
1107 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001108 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001109 // Walk the rest of the chain without clobbering cp.
1110 current = next;
1111 }
1112 // If no outer scope calls eval, we do not need to check more
1113 // context extensions.
1114 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
1115 s = s->outer_scope();
1116 }
1117
1118 if (s->is_eval_scope()) {
1119 Label loop, fast;
1120 if (!current.is(next)) {
1121 __ Move(next, current);
1122 }
1123 __ bind(&loop);
1124 // Terminate at global context.
1125 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1126 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1127 __ Branch(&fast, eq, temp, Operand(t0));
1128 // Check that extension is NULL.
1129 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1130 __ Branch(slow, ne, temp, Operand(zero_reg));
1131 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001132 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001133 __ Branch(&loop);
1134 __ bind(&fast);
1135 }
1136
1137 __ lw(a0, GlobalObjectOperand());
1138 __ li(a2, Operand(slot->var()->name()));
1139 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1140 ? RelocInfo::CODE_TARGET
1141 : RelocInfo::CODE_TARGET_CONTEXT;
1142 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001143 __ Call(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001144}
1145
1146
lrn@chromium.org7516f052011-03-30 08:52:27 +00001147MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(
1148 Slot* slot,
1149 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001150 ASSERT(slot->type() == Slot::CONTEXT);
1151 Register context = cp;
1152 Register next = a3;
1153 Register temp = t0;
1154
1155 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
1156 if (s->num_heap_slots() > 0) {
1157 if (s->calls_eval()) {
1158 // Check that extension is NULL.
1159 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1160 __ Branch(slow, ne, temp, Operand(zero_reg));
1161 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001162 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001163 // Walk the rest of the chain without clobbering cp.
1164 context = next;
1165 }
1166 }
1167 // Check that last extension is NULL.
1168 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1169 __ Branch(slow, ne, temp, Operand(zero_reg));
1170
1171 // This function is used only for loads, not stores, so it's safe to
1172 // return an cp-based operand (the write barrier cannot be allowed to
1173 // destroy the cp register).
1174 return ContextOperand(context, slot->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001175}
1176
1177
1178void FullCodeGenerator::EmitDynamicLoadFromSlotFastCase(
1179 Slot* slot,
1180 TypeofState typeof_state,
1181 Label* slow,
1182 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001183 // Generate fast-case code for variables that might be shadowed by
1184 // eval-introduced variables. Eval is used a lot without
1185 // introducing variables. In those cases, we do not want to
1186 // perform a runtime call for all variables in the scope
1187 // containing the eval.
1188 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
1189 EmitLoadGlobalSlotCheckExtensions(slot, typeof_state, slow);
1190 __ Branch(done);
1191 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
1192 Slot* potential_slot = slot->var()->local_if_not_shadowed()->AsSlot();
1193 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
1194 if (potential_slot != NULL) {
1195 // Generate fast case for locals that rewrite to slots.
1196 __ lw(v0, ContextSlotOperandCheckExtensions(potential_slot, slow));
1197 if (potential_slot->var()->mode() == Variable::CONST) {
1198 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1199 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1200 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1201 __ movz(v0, a0, at); // Conditional move.
1202 }
1203 __ Branch(done);
1204 } else if (rewrite != NULL) {
1205 // Generate fast case for calls of an argument function.
1206 Property* property = rewrite->AsProperty();
1207 if (property != NULL) {
1208 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
1209 Literal* key_literal = property->key()->AsLiteral();
1210 if (obj_proxy != NULL &&
1211 key_literal != NULL &&
1212 obj_proxy->IsArguments() &&
1213 key_literal->handle()->IsSmi()) {
1214 // Load arguments object if there are no eval-introduced
1215 // variables. Then load the argument from the arguments
1216 // object using keyed load.
1217 __ lw(a1,
1218 ContextSlotOperandCheckExtensions(obj_proxy->var()->AsSlot(),
1219 slow));
1220 __ li(a0, Operand(key_literal->handle()));
1221 Handle<Code> ic =
1222 isolate()->builtins()->KeyedLoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001223 __ Call(ic, RelocInfo::CODE_TARGET, GetPropertyId(property));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001224 __ Branch(done);
1225 }
1226 }
1227 }
1228 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001229}
1230
1231
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001232void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1233 // Record position before possible IC call.
1234 SetSourcePosition(proxy->position());
1235 Variable* var = proxy->var();
1236
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001237 // Three cases: non-this global variables, lookup slots, and all other
1238 // types of slots.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001239 Slot* slot = var->AsSlot();
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001240 ASSERT((var->is_global() && !var->is_this()) == (slot == NULL));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001241
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001242 if (slot == NULL) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001243 Comment cmnt(masm_, "Global variable");
1244 // Use inline caching. Variable name is passed in a2 and the global
1245 // object (receiver) in a0.
1246 __ lw(a0, GlobalObjectOperand());
1247 __ li(a2, Operand(var->name()));
1248 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001249 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001250 context()->Plug(v0);
1251
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001252 } else if (slot->type() == Slot::LOOKUP) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001253 Label done, slow;
1254
1255 // Generate code for loading from variables potentially shadowed
1256 // by eval-introduced variables.
1257 EmitDynamicLoadFromSlotFastCase(slot, NOT_INSIDE_TYPEOF, &slow, &done);
1258
1259 __ bind(&slow);
1260 Comment cmnt(masm_, "Lookup slot");
1261 __ li(a1, Operand(var->name()));
1262 __ Push(cp, a1); // Context and name.
1263 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1264 __ bind(&done);
1265
1266 context()->Plug(v0);
1267
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001268 } else {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001269 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1270 ? "Context slot"
1271 : "Stack slot");
1272 if (var->mode() == Variable::CONST) {
1273 // Constants may be the hole value if they have not been initialized.
1274 // Unhole them.
1275 MemOperand slot_operand = EmitSlotSearch(slot, a0);
1276 __ lw(v0, slot_operand);
1277 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1278 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1279 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1280 __ movz(v0, a0, at); // Conditional move.
1281 context()->Plug(v0);
1282 } else {
1283 context()->Plug(slot);
1284 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001285 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001286}
1287
1288
1289void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001290 Comment cmnt(masm_, "[ RegExpLiteral");
1291 Label materialized;
1292 // Registers will be used as follows:
1293 // t1 = materialized value (RegExp literal)
1294 // t0 = JS function, literals array
1295 // a3 = literal index
1296 // a2 = RegExp pattern
1297 // a1 = RegExp flags
1298 // a0 = RegExp literal clone
1299 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1300 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1301 int literal_offset =
1302 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1303 __ lw(t1, FieldMemOperand(t0, literal_offset));
1304 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1305 __ Branch(&materialized, ne, t1, Operand(at));
1306
1307 // Create regexp literal using runtime function.
1308 // Result will be in v0.
1309 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1310 __ li(a2, Operand(expr->pattern()));
1311 __ li(a1, Operand(expr->flags()));
1312 __ Push(t0, a3, a2, a1);
1313 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1314 __ mov(t1, v0);
1315
1316 __ bind(&materialized);
1317 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1318 Label allocated, runtime_allocate;
1319 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1320 __ jmp(&allocated);
1321
1322 __ bind(&runtime_allocate);
1323 __ push(t1);
1324 __ li(a0, Operand(Smi::FromInt(size)));
1325 __ push(a0);
1326 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1327 __ pop(t1);
1328
1329 __ bind(&allocated);
1330
1331 // After this, registers are used as follows:
1332 // v0: Newly allocated regexp.
1333 // t1: Materialized regexp.
1334 // a2: temp.
1335 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1336 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001337}
1338
1339
1340void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001341 Comment cmnt(masm_, "[ ObjectLiteral");
1342 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1343 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1344 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1345 __ li(a1, Operand(expr->constant_properties()));
1346 int flags = expr->fast_elements()
1347 ? ObjectLiteral::kFastElements
1348 : ObjectLiteral::kNoFlags;
1349 flags |= expr->has_function()
1350 ? ObjectLiteral::kHasFunction
1351 : ObjectLiteral::kNoFlags;
1352 __ li(a0, Operand(Smi::FromInt(flags)));
1353 __ Push(a3, a2, a1, a0);
1354 if (expr->depth() > 1) {
1355 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1356 } else {
1357 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
1358 }
1359
1360 // If result_saved is true the result is on top of the stack. If
1361 // result_saved is false the result is in v0.
1362 bool result_saved = false;
1363
1364 // Mark all computed expressions that are bound to a key that
1365 // is shadowed by a later occurrence of the same key. For the
1366 // marked expressions, no store code is emitted.
1367 expr->CalculateEmitStore();
1368
1369 for (int i = 0; i < expr->properties()->length(); i++) {
1370 ObjectLiteral::Property* property = expr->properties()->at(i);
1371 if (property->IsCompileTimeValue()) continue;
1372
1373 Literal* key = property->key();
1374 Expression* value = property->value();
1375 if (!result_saved) {
1376 __ push(v0); // Save result on stack.
1377 result_saved = true;
1378 }
1379 switch (property->kind()) {
1380 case ObjectLiteral::Property::CONSTANT:
1381 UNREACHABLE();
1382 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1383 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1384 // Fall through.
1385 case ObjectLiteral::Property::COMPUTED:
1386 if (key->handle()->IsSymbol()) {
1387 if (property->emit_store()) {
1388 VisitForAccumulatorValue(value);
1389 __ mov(a0, result_register());
1390 __ li(a2, Operand(key->handle()));
1391 __ lw(a1, MemOperand(sp));
1392 Handle<Code> ic = is_strict_mode()
1393 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1394 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001395 __ Call(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001396 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1397 } else {
1398 VisitForEffect(value);
1399 }
1400 break;
1401 }
1402 // Fall through.
1403 case ObjectLiteral::Property::PROTOTYPE:
1404 // Duplicate receiver on stack.
1405 __ lw(a0, MemOperand(sp));
1406 __ push(a0);
1407 VisitForStackValue(key);
1408 VisitForStackValue(value);
1409 if (property->emit_store()) {
1410 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1411 __ push(a0);
1412 __ CallRuntime(Runtime::kSetProperty, 4);
1413 } else {
1414 __ Drop(3);
1415 }
1416 break;
1417 case ObjectLiteral::Property::GETTER:
1418 case ObjectLiteral::Property::SETTER:
1419 // Duplicate receiver on stack.
1420 __ lw(a0, MemOperand(sp));
1421 __ push(a0);
1422 VisitForStackValue(key);
1423 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1424 Smi::FromInt(1) :
1425 Smi::FromInt(0)));
1426 __ push(a1);
1427 VisitForStackValue(value);
1428 __ CallRuntime(Runtime::kDefineAccessor, 4);
1429 break;
1430 }
1431 }
1432
1433 if (expr->has_function()) {
1434 ASSERT(result_saved);
1435 __ lw(a0, MemOperand(sp));
1436 __ push(a0);
1437 __ CallRuntime(Runtime::kToFastProperties, 1);
1438 }
1439
1440 if (result_saved) {
1441 context()->PlugTOS();
1442 } else {
1443 context()->Plug(v0);
1444 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001445}
1446
1447
1448void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001449 Comment cmnt(masm_, "[ ArrayLiteral");
1450
1451 ZoneList<Expression*>* subexprs = expr->values();
1452 int length = subexprs->length();
1453 __ mov(a0, result_register());
1454 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1455 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1456 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1457 __ li(a1, Operand(expr->constant_elements()));
1458 __ Push(a3, a2, a1);
1459 if (expr->constant_elements()->map() ==
1460 isolate()->heap()->fixed_cow_array_map()) {
1461 FastCloneShallowArrayStub stub(
1462 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1463 __ CallStub(&stub);
1464 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1465 1, a1, a2);
1466 } else if (expr->depth() > 1) {
1467 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1468 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1469 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1470 } else {
1471 FastCloneShallowArrayStub stub(
1472 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
1473 __ CallStub(&stub);
1474 }
1475
1476 bool result_saved = false; // Is the result saved to the stack?
1477
1478 // Emit code to evaluate all the non-constant subexpressions and to store
1479 // them into the newly cloned array.
1480 for (int i = 0; i < length; i++) {
1481 Expression* subexpr = subexprs->at(i);
1482 // If the subexpression is a literal or a simple materialized literal it
1483 // is already set in the cloned array.
1484 if (subexpr->AsLiteral() != NULL ||
1485 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1486 continue;
1487 }
1488
1489 if (!result_saved) {
1490 __ push(v0);
1491 result_saved = true;
1492 }
1493 VisitForAccumulatorValue(subexpr);
1494
1495 // Store the subexpression value in the array's elements.
1496 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1497 __ lw(a1, FieldMemOperand(a1, JSObject::kElementsOffset));
1498 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1499 __ sw(result_register(), FieldMemOperand(a1, offset));
1500
1501 // Update the write barrier for the array store with v0 as the scratch
1502 // register.
1503 __ li(a2, Operand(offset));
1504 // TODO(PJ): double check this RecordWrite call.
1505 __ RecordWrite(a1, a2, result_register());
1506
1507 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1508 }
1509
1510 if (result_saved) {
1511 context()->PlugTOS();
1512 } else {
1513 context()->Plug(v0);
1514 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001515}
1516
1517
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001518void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001519 Comment cmnt(masm_, "[ Assignment");
1520 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1521 // on the left-hand side.
1522 if (!expr->target()->IsValidLeftHandSide()) {
1523 VisitForEffect(expr->target());
1524 return;
1525 }
1526
1527 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001528 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001529 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1530 LhsKind assign_type = VARIABLE;
1531 Property* property = expr->target()->AsProperty();
1532 if (property != NULL) {
1533 assign_type = (property->key()->IsPropertyName())
1534 ? NAMED_PROPERTY
1535 : KEYED_PROPERTY;
1536 }
1537
1538 // Evaluate LHS expression.
1539 switch (assign_type) {
1540 case VARIABLE:
1541 // Nothing to do here.
1542 break;
1543 case NAMED_PROPERTY:
1544 if (expr->is_compound()) {
1545 // We need the receiver both on the stack and in the accumulator.
1546 VisitForAccumulatorValue(property->obj());
1547 __ push(result_register());
1548 } else {
1549 VisitForStackValue(property->obj());
1550 }
1551 break;
1552 case KEYED_PROPERTY:
1553 // We need the key and receiver on both the stack and in v0 and a1.
1554 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001555 VisitForStackValue(property->obj());
1556 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001557 __ lw(a1, MemOperand(sp, 0));
1558 __ push(v0);
1559 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001560 VisitForStackValue(property->obj());
1561 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001562 }
1563 break;
1564 }
1565
1566 // For compound assignments we need another deoptimization point after the
1567 // variable/property load.
1568 if (expr->is_compound()) {
1569 { AccumulatorValueContext context(this);
1570 switch (assign_type) {
1571 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001572 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001573 PrepareForBailout(expr->target(), TOS_REG);
1574 break;
1575 case NAMED_PROPERTY:
1576 EmitNamedPropertyLoad(property);
1577 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1578 break;
1579 case KEYED_PROPERTY:
1580 EmitKeyedPropertyLoad(property);
1581 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1582 break;
1583 }
1584 }
1585
1586 Token::Value op = expr->binary_op();
1587 __ push(v0); // Left operand goes on the stack.
1588 VisitForAccumulatorValue(expr->value());
1589
1590 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1591 ? OVERWRITE_RIGHT
1592 : NO_OVERWRITE;
1593 SetSourcePosition(expr->position() + 1);
1594 AccumulatorValueContext context(this);
1595 if (ShouldInlineSmiCase(op)) {
1596 EmitInlineSmiBinaryOp(expr->binary_operation(),
1597 op,
1598 mode,
1599 expr->target(),
1600 expr->value());
1601 } else {
1602 EmitBinaryOp(expr->binary_operation(), op, mode);
1603 }
1604
1605 // Deoptimization point in case the binary operation may have side effects.
1606 PrepareForBailout(expr->binary_operation(), TOS_REG);
1607 } else {
1608 VisitForAccumulatorValue(expr->value());
1609 }
1610
1611 // Record source position before possible IC call.
1612 SetSourcePosition(expr->position());
1613
1614 // Store the value.
1615 switch (assign_type) {
1616 case VARIABLE:
1617 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1618 expr->op());
1619 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1620 context()->Plug(v0);
1621 break;
1622 case NAMED_PROPERTY:
1623 EmitNamedPropertyAssignment(expr);
1624 break;
1625 case KEYED_PROPERTY:
1626 EmitKeyedPropertyAssignment(expr);
1627 break;
1628 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001629}
1630
1631
ager@chromium.org5c838252010-02-19 08:53:10 +00001632void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001633 SetSourcePosition(prop->position());
1634 Literal* key = prop->key()->AsLiteral();
1635 __ mov(a0, result_register());
1636 __ li(a2, Operand(key->handle()));
1637 // Call load IC. It has arguments receiver and property name a0 and a2.
1638 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001639 __ Call(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001640}
1641
1642
1643void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001644 SetSourcePosition(prop->position());
1645 __ mov(a0, result_register());
1646 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1647 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001648 __ Call(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001649}
1650
1651
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001652void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001653 Token::Value op,
1654 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001655 Expression* left_expr,
1656 Expression* right_expr) {
1657 Label done, smi_case, stub_call;
1658
1659 Register scratch1 = a2;
1660 Register scratch2 = a3;
1661
1662 // Get the arguments.
1663 Register left = a1;
1664 Register right = a0;
1665 __ pop(left);
1666 __ mov(a0, result_register());
1667
1668 // Perform combined smi check on both operands.
1669 __ Or(scratch1, left, Operand(right));
1670 STATIC_ASSERT(kSmiTag == 0);
1671 JumpPatchSite patch_site(masm_);
1672 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1673
1674 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001675 BinaryOpStub stub(op, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001676 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001677 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001678 __ jmp(&done);
1679
1680 __ bind(&smi_case);
1681 // Smi case. This code works the same way as the smi-smi case in the type
1682 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001683 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001684 switch (op) {
1685 case Token::SAR:
1686 __ Branch(&stub_call);
1687 __ GetLeastBitsFromSmi(scratch1, right, 5);
1688 __ srav(right, left, scratch1);
1689 __ And(v0, right, Operand(~kSmiTagMask));
1690 break;
1691 case Token::SHL: {
1692 __ Branch(&stub_call);
1693 __ SmiUntag(scratch1, left);
1694 __ GetLeastBitsFromSmi(scratch2, right, 5);
1695 __ sllv(scratch1, scratch1, scratch2);
1696 __ Addu(scratch2, scratch1, Operand(0x40000000));
1697 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1698 __ SmiTag(v0, scratch1);
1699 break;
1700 }
1701 case Token::SHR: {
1702 __ Branch(&stub_call);
1703 __ SmiUntag(scratch1, left);
1704 __ GetLeastBitsFromSmi(scratch2, right, 5);
1705 __ srlv(scratch1, scratch1, scratch2);
1706 __ And(scratch2, scratch1, 0xc0000000);
1707 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1708 __ SmiTag(v0, scratch1);
1709 break;
1710 }
1711 case Token::ADD:
1712 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1713 __ BranchOnOverflow(&stub_call, scratch1);
1714 break;
1715 case Token::SUB:
1716 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1717 __ BranchOnOverflow(&stub_call, scratch1);
1718 break;
1719 case Token::MUL: {
1720 __ SmiUntag(scratch1, right);
1721 __ Mult(left, scratch1);
1722 __ mflo(scratch1);
1723 __ mfhi(scratch2);
1724 __ sra(scratch1, scratch1, 31);
1725 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1726 __ mflo(v0);
1727 __ Branch(&done, ne, v0, Operand(zero_reg));
1728 __ Addu(scratch2, right, left);
1729 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1730 ASSERT(Smi::FromInt(0) == 0);
1731 __ mov(v0, zero_reg);
1732 break;
1733 }
1734 case Token::BIT_OR:
1735 __ Or(v0, left, Operand(right));
1736 break;
1737 case Token::BIT_AND:
1738 __ And(v0, left, Operand(right));
1739 break;
1740 case Token::BIT_XOR:
1741 __ Xor(v0, left, Operand(right));
1742 break;
1743 default:
1744 UNREACHABLE();
1745 }
1746
1747 __ bind(&done);
1748 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001749}
1750
1751
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001752void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1753 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001754 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001755 __ mov(a0, result_register());
1756 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001757 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001758 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001759 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001760 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001761 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001762}
1763
1764
1765void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001766 // Invalid left-hand sides are rewritten to have a 'throw
1767 // ReferenceError' on the left-hand side.
1768 if (!expr->IsValidLeftHandSide()) {
1769 VisitForEffect(expr);
1770 return;
1771 }
1772
1773 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001774 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001775 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1776 LhsKind assign_type = VARIABLE;
1777 Property* prop = expr->AsProperty();
1778 if (prop != NULL) {
1779 assign_type = (prop->key()->IsPropertyName())
1780 ? NAMED_PROPERTY
1781 : KEYED_PROPERTY;
1782 }
1783
1784 switch (assign_type) {
1785 case VARIABLE: {
1786 Variable* var = expr->AsVariableProxy()->var();
1787 EffectContext context(this);
1788 EmitVariableAssignment(var, Token::ASSIGN);
1789 break;
1790 }
1791 case NAMED_PROPERTY: {
1792 __ push(result_register()); // Preserve value.
1793 VisitForAccumulatorValue(prop->obj());
1794 __ mov(a1, result_register());
1795 __ pop(a0); // Restore value.
1796 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1797 Handle<Code> ic = is_strict_mode()
1798 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1799 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001800 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001801 break;
1802 }
1803 case KEYED_PROPERTY: {
1804 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001805 VisitForStackValue(prop->obj());
1806 VisitForAccumulatorValue(prop->key());
1807 __ mov(a1, result_register());
1808 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001809 __ pop(a0); // Restore value.
1810 Handle<Code> ic = is_strict_mode()
1811 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
1812 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001813 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001814 break;
1815 }
1816 }
1817 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1818 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001819}
1820
1821
1822void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001823 Token::Value op) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001824 ASSERT(var != NULL);
1825 ASSERT(var->is_global() || var->AsSlot() != NULL);
1826
1827 if (var->is_global()) {
1828 ASSERT(!var->is_this());
1829 // Assignment to a global variable. Use inline caching for the
1830 // assignment. Right-hand-side value is passed in a0, variable name in
1831 // a2, and the global object in a1.
1832 __ mov(a0, result_register());
1833 __ li(a2, Operand(var->name()));
1834 __ lw(a1, GlobalObjectOperand());
1835 Handle<Code> ic = is_strict_mode()
1836 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1837 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001838 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001839
1840 } else if (op == Token::INIT_CONST) {
1841 // Like var declarations, const declarations are hoisted to function
1842 // scope. However, unlike var initializers, const initializers are able
1843 // to drill a hole to that function context, even from inside a 'with'
1844 // context. We thus bypass the normal static scope lookup.
1845 Slot* slot = var->AsSlot();
1846 Label skip;
1847 switch (slot->type()) {
1848 case Slot::PARAMETER:
1849 // No const parameters.
1850 UNREACHABLE();
1851 break;
1852 case Slot::LOCAL:
1853 // Detect const reinitialization by checking for the hole value.
1854 __ lw(a1, MemOperand(fp, SlotOffset(slot)));
1855 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1856 __ Branch(&skip, ne, a1, Operand(t0));
1857 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1858 break;
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001859 case Slot::CONTEXT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001860 case Slot::LOOKUP:
1861 __ push(result_register());
1862 __ li(a0, Operand(slot->var()->name()));
1863 __ Push(cp, a0); // Context and name.
1864 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1865 break;
1866 }
1867 __ bind(&skip);
1868
1869 } else if (var->mode() != Variable::CONST) {
1870 // Perform the assignment for non-const variables. Const assignments
1871 // are simply skipped.
1872 Slot* slot = var->AsSlot();
1873 switch (slot->type()) {
1874 case Slot::PARAMETER:
1875 case Slot::LOCAL:
1876 // Perform the assignment.
1877 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1878 break;
1879
1880 case Slot::CONTEXT: {
1881 MemOperand target = EmitSlotSearch(slot, a1);
1882 // Perform the assignment and issue the write barrier.
1883 __ sw(result_register(), target);
1884 // RecordWrite may destroy all its register arguments.
1885 __ mov(a3, result_register());
1886 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
1887 __ RecordWrite(a1, Operand(offset), a2, a3);
1888 break;
1889 }
1890
1891 case Slot::LOOKUP:
1892 // Call the runtime for the assignment.
1893 __ push(v0); // Value.
1894 __ li(a1, Operand(slot->var()->name()));
1895 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1896 __ Push(cp, a1, a0); // Context, name, strict mode.
1897 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1898 break;
1899 }
1900 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001901}
1902
1903
1904void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001905 // Assignment to a property, using a named store IC.
1906 Property* prop = expr->target()->AsProperty();
1907 ASSERT(prop != NULL);
1908 ASSERT(prop->key()->AsLiteral() != NULL);
1909
1910 // If the assignment starts a block of assignments to the same object,
1911 // change to slow case to avoid the quadratic behavior of repeatedly
1912 // adding fast properties.
1913 if (expr->starts_initialization_block()) {
1914 __ push(result_register());
1915 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1916 __ push(t0);
1917 __ CallRuntime(Runtime::kToSlowProperties, 1);
1918 __ pop(result_register());
1919 }
1920
1921 // Record source code position before IC call.
1922 SetSourcePosition(expr->position());
1923 __ mov(a0, result_register()); // Load the value.
1924 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1925 // Load receiver to a1. Leave a copy in the stack if needed for turning the
1926 // receiver into fast case.
1927 if (expr->ends_initialization_block()) {
1928 __ lw(a1, MemOperand(sp));
1929 } else {
1930 __ pop(a1);
1931 }
1932
1933 Handle<Code> ic = is_strict_mode()
1934 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1935 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001936 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001937
1938 // If the assignment ends an initialization block, revert to fast case.
1939 if (expr->ends_initialization_block()) {
1940 __ push(v0); // Result of assignment, saved even if not needed.
1941 // Receiver is under the result value.
1942 __ lw(t0, MemOperand(sp, kPointerSize));
1943 __ push(t0);
1944 __ CallRuntime(Runtime::kToFastProperties, 1);
1945 __ pop(v0);
1946 __ Drop(1);
1947 }
1948 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1949 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001950}
1951
1952
1953void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001954 // Assignment to a property, using a keyed store IC.
1955
1956 // If the assignment starts a block of assignments to the same object,
1957 // change to slow case to avoid the quadratic behavior of repeatedly
1958 // adding fast properties.
1959 if (expr->starts_initialization_block()) {
1960 __ push(result_register());
1961 // Receiver is now under the key and value.
1962 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
1963 __ push(t0);
1964 __ CallRuntime(Runtime::kToSlowProperties, 1);
1965 __ pop(result_register());
1966 }
1967
1968 // Record source code position before IC call.
1969 SetSourcePosition(expr->position());
1970 // Call keyed store IC.
1971 // The arguments are:
1972 // - a0 is the value,
1973 // - a1 is the key,
1974 // - a2 is the receiver.
1975 __ mov(a0, result_register());
1976 __ pop(a1); // Key.
1977 // Load receiver to a2. Leave a copy in the stack if needed for turning the
1978 // receiver into fast case.
1979 if (expr->ends_initialization_block()) {
1980 __ lw(a2, MemOperand(sp));
1981 } else {
1982 __ pop(a2);
1983 }
1984
1985 Handle<Code> ic = is_strict_mode()
1986 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
1987 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001988 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001989
1990 // If the assignment ends an initialization block, revert to fast case.
1991 if (expr->ends_initialization_block()) {
1992 __ push(v0); // Result of assignment, saved even if not needed.
1993 // Receiver is under the result value.
1994 __ lw(t0, MemOperand(sp, kPointerSize));
1995 __ push(t0);
1996 __ CallRuntime(Runtime::kToFastProperties, 1);
1997 __ pop(v0);
1998 __ Drop(1);
1999 }
2000 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2001 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002002}
2003
2004
2005void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002006 Comment cmnt(masm_, "[ Property");
2007 Expression* key = expr->key();
2008
2009 if (key->IsPropertyName()) {
2010 VisitForAccumulatorValue(expr->obj());
2011 EmitNamedPropertyLoad(expr);
2012 context()->Plug(v0);
2013 } else {
2014 VisitForStackValue(expr->obj());
2015 VisitForAccumulatorValue(expr->key());
2016 __ pop(a1);
2017 EmitKeyedPropertyLoad(expr);
2018 context()->Plug(v0);
2019 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002020}
2021
lrn@chromium.org7516f052011-03-30 08:52:27 +00002022
ager@chromium.org5c838252010-02-19 08:53:10 +00002023void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002024 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002025 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002026 // Code common for calls using the IC.
2027 ZoneList<Expression*>* args = expr->arguments();
2028 int arg_count = args->length();
2029 { PreservePositionScope scope(masm()->positions_recorder());
2030 for (int i = 0; i < arg_count; i++) {
2031 VisitForStackValue(args->at(i));
2032 }
2033 __ li(a2, Operand(name));
2034 }
2035 // Record source position for debugger.
2036 SetSourcePosition(expr->position());
2037 // Call the IC initialization code.
2038 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2039 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00002040 isolate()->stub_cache()->ComputeCallInitialize(arg_count, in_loop, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002041 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002042 RecordJSReturnSite(expr);
2043 // Restore context register.
2044 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2045 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002046}
2047
2048
lrn@chromium.org7516f052011-03-30 08:52:27 +00002049void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002050 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002051 // Load the key.
2052 VisitForAccumulatorValue(key);
2053
2054 // Swap the name of the function and the receiver on the stack to follow
2055 // the calling convention for call ICs.
2056 __ pop(a1);
2057 __ push(v0);
2058 __ push(a1);
2059
2060 // Code common for calls using the IC.
2061 ZoneList<Expression*>* args = expr->arguments();
2062 int arg_count = args->length();
2063 { PreservePositionScope scope(masm()->positions_recorder());
2064 for (int i = 0; i < arg_count; i++) {
2065 VisitForStackValue(args->at(i));
2066 }
2067 }
2068 // Record source position for debugger.
2069 SetSourcePosition(expr->position());
2070 // Call the IC initialization code.
2071 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2072 Handle<Code> ic =
2073 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count, in_loop);
2074 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002075 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002076 RecordJSReturnSite(expr);
2077 // Restore context register.
2078 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2079 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002080}
2081
2082
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002083void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002084 // Code common for calls using the call stub.
2085 ZoneList<Expression*>* args = expr->arguments();
2086 int arg_count = args->length();
2087 { PreservePositionScope scope(masm()->positions_recorder());
2088 for (int i = 0; i < arg_count; i++) {
2089 VisitForStackValue(args->at(i));
2090 }
2091 }
2092 // Record source position for debugger.
2093 SetSourcePosition(expr->position());
2094 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2095 CallFunctionStub stub(arg_count, in_loop, flags);
2096 __ CallStub(&stub);
2097 RecordJSReturnSite(expr);
2098 // Restore context register.
2099 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2100 context()->DropAndPlug(1, v0);
2101}
2102
2103
2104void FullCodeGenerator::EmitResolvePossiblyDirectEval(ResolveEvalFlag flag,
2105 int arg_count) {
2106 // Push copy of the first argument or undefined if it doesn't exist.
2107 if (arg_count > 0) {
2108 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2109 } else {
2110 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2111 }
2112 __ push(a1);
2113
2114 // Push the receiver of the enclosing function and do runtime call.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002115 int receiver_offset = 2 + info_->scope()->num_parameters();
2116 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002117 __ push(a1);
2118 // Push the strict mode flag.
2119 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
2120 __ push(a1);
2121
2122 __ CallRuntime(flag == SKIP_CONTEXT_LOOKUP
2123 ? Runtime::kResolvePossiblyDirectEvalNoLookup
2124 : Runtime::kResolvePossiblyDirectEval, 4);
ager@chromium.org5c838252010-02-19 08:53:10 +00002125}
2126
2127
2128void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002129#ifdef DEBUG
2130 // We want to verify that RecordJSReturnSite gets called on all paths
2131 // through this function. Avoid early returns.
2132 expr->return_is_recorded_ = false;
2133#endif
2134
2135 Comment cmnt(masm_, "[ Call");
2136 Expression* fun = expr->expression();
2137 Variable* var = fun->AsVariableProxy()->AsVariable();
2138
2139 if (var != NULL && var->is_possibly_eval()) {
2140 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2141 // resolve the function we need to call and the receiver of the
2142 // call. Then we call the resolved function using the given
2143 // arguments.
2144 ZoneList<Expression*>* args = expr->arguments();
2145 int arg_count = args->length();
2146
2147 { PreservePositionScope pos_scope(masm()->positions_recorder());
2148 VisitForStackValue(fun);
2149 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2150 __ push(a2); // Reserved receiver slot.
2151
2152 // Push the arguments.
2153 for (int i = 0; i < arg_count; i++) {
2154 VisitForStackValue(args->at(i));
2155 }
2156 // If we know that eval can only be shadowed by eval-introduced
2157 // variables we attempt to load the global eval function directly
2158 // in generated code. If we succeed, there is no need to perform a
2159 // context lookup in the runtime system.
2160 Label done;
2161 if (var->AsSlot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
2162 Label slow;
2163 EmitLoadGlobalSlotCheckExtensions(var->AsSlot(),
2164 NOT_INSIDE_TYPEOF,
2165 &slow);
2166 // Push the function and resolve eval.
2167 __ push(v0);
2168 EmitResolvePossiblyDirectEval(SKIP_CONTEXT_LOOKUP, arg_count);
2169 __ jmp(&done);
2170 __ bind(&slow);
2171 }
2172
2173 // Push copy of the function (found below the arguments) and
2174 // resolve eval.
2175 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2176 __ push(a1);
2177 EmitResolvePossiblyDirectEval(PERFORM_CONTEXT_LOOKUP, arg_count);
2178 if (done.is_linked()) {
2179 __ bind(&done);
2180 }
2181
2182 // The runtime call returns a pair of values in v0 (function) and
2183 // v1 (receiver). Touch up the stack with the right values.
2184 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2185 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2186 }
2187 // Record source position for debugger.
2188 SetSourcePosition(expr->position());
2189 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002190 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002191 __ CallStub(&stub);
2192 RecordJSReturnSite(expr);
2193 // Restore context register.
2194 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2195 context()->DropAndPlug(1, v0);
2196 } else if (var != NULL && !var->is_this() && var->is_global()) {
2197 // Push global object as receiver for the call IC.
2198 __ lw(a0, GlobalObjectOperand());
2199 __ push(a0);
2200 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
2201 } else if (var != NULL && var->AsSlot() != NULL &&
2202 var->AsSlot()->type() == Slot::LOOKUP) {
2203 // Call to a lookup slot (dynamically introduced variable).
2204 Label slow, done;
2205
2206 { PreservePositionScope scope(masm()->positions_recorder());
2207 // Generate code for loading from variables potentially shadowed
2208 // by eval-introduced variables.
2209 EmitDynamicLoadFromSlotFastCase(var->AsSlot(),
2210 NOT_INSIDE_TYPEOF,
2211 &slow,
2212 &done);
2213 }
2214
2215 __ bind(&slow);
2216 // Call the runtime to find the function to call (returned in v0)
2217 // and the object holding it (returned in v1).
2218 __ push(context_register());
2219 __ li(a2, Operand(var->name()));
2220 __ push(a2);
2221 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2222 __ Push(v0, v1); // Function, receiver.
2223
2224 // If fast case code has been generated, emit code to push the
2225 // function and receiver and have the slow path jump around this
2226 // code.
2227 if (done.is_linked()) {
2228 Label call;
2229 __ Branch(&call);
2230 __ bind(&done);
2231 // Push function.
2232 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002233 // The receiver is implicitly the global receiver. Indicate this
2234 // by passing the hole to the call function stub.
2235 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002236 __ push(a1);
2237 __ bind(&call);
2238 }
2239
danno@chromium.org40cb8782011-05-25 07:58:50 +00002240 // The receiver is either the global receiver or an object found
2241 // by LoadContextSlot. That object could be the hole if the
2242 // receiver is implicitly the global object.
2243 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002244 } else if (fun->AsProperty() != NULL) {
2245 // Call to an object property.
2246 Property* prop = fun->AsProperty();
2247 Literal* key = prop->key()->AsLiteral();
2248 if (key != NULL && key->handle()->IsSymbol()) {
2249 // Call to a named property, use call IC.
2250 { PreservePositionScope scope(masm()->positions_recorder());
2251 VisitForStackValue(prop->obj());
2252 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00002253 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002254 } else {
2255 // Call to a keyed property.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002256 { PreservePositionScope scope(masm()->positions_recorder());
2257 VisitForStackValue(prop->obj());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002258 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002259 EmitKeyedCallWithIC(expr, prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002260 }
2261 } else {
2262 { PreservePositionScope scope(masm()->positions_recorder());
2263 VisitForStackValue(fun);
2264 }
2265 // Load global receiver object.
2266 __ lw(a1, GlobalObjectOperand());
2267 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2268 __ push(a1);
2269 // Emit function call.
2270 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2271 }
2272
2273#ifdef DEBUG
2274 // RecordJSReturnSite should have been called.
2275 ASSERT(expr->return_is_recorded_);
2276#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002277}
2278
2279
2280void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002281 Comment cmnt(masm_, "[ CallNew");
2282 // According to ECMA-262, section 11.2.2, page 44, the function
2283 // expression in new calls must be evaluated before the
2284 // arguments.
2285
2286 // Push constructor on the stack. If it's not a function it's used as
2287 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2288 // ignored.
2289 VisitForStackValue(expr->expression());
2290
2291 // Push the arguments ("left-to-right") on the stack.
2292 ZoneList<Expression*>* args = expr->arguments();
2293 int arg_count = args->length();
2294 for (int i = 0; i < arg_count; i++) {
2295 VisitForStackValue(args->at(i));
2296 }
2297
2298 // Call the construct call builtin that handles allocation and
2299 // constructor invocation.
2300 SetSourcePosition(expr->position());
2301
2302 // Load function and argument count into a1 and a0.
2303 __ li(a0, Operand(arg_count));
2304 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2305
2306 Handle<Code> construct_builtin =
2307 isolate()->builtins()->JSConstructCall();
2308 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2309 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002310}
2311
2312
lrn@chromium.org7516f052011-03-30 08:52:27 +00002313void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002314 ASSERT(args->length() == 1);
2315
2316 VisitForAccumulatorValue(args->at(0));
2317
2318 Label materialize_true, materialize_false;
2319 Label* if_true = NULL;
2320 Label* if_false = NULL;
2321 Label* fall_through = NULL;
2322 context()->PrepareTest(&materialize_true, &materialize_false,
2323 &if_true, &if_false, &fall_through);
2324
2325 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2326 __ And(t0, v0, Operand(kSmiTagMask));
2327 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2328
2329 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002330}
2331
2332
2333void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002334 ASSERT(args->length() == 1);
2335
2336 VisitForAccumulatorValue(args->at(0));
2337
2338 Label materialize_true, materialize_false;
2339 Label* if_true = NULL;
2340 Label* if_false = NULL;
2341 Label* fall_through = NULL;
2342 context()->PrepareTest(&materialize_true, &materialize_false,
2343 &if_true, &if_false, &fall_through);
2344
2345 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2346 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2347 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2348
2349 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002350}
2351
2352
2353void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002354 ASSERT(args->length() == 1);
2355
2356 VisitForAccumulatorValue(args->at(0));
2357
2358 Label materialize_true, materialize_false;
2359 Label* if_true = NULL;
2360 Label* if_false = NULL;
2361 Label* fall_through = NULL;
2362 context()->PrepareTest(&materialize_true, &materialize_false,
2363 &if_true, &if_false, &fall_through);
2364
2365 __ JumpIfSmi(v0, if_false);
2366 __ LoadRoot(at, Heap::kNullValueRootIndex);
2367 __ Branch(if_true, eq, v0, Operand(at));
2368 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2369 // Undetectable objects behave like undefined when tested with typeof.
2370 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2371 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2372 __ Branch(if_false, ne, at, Operand(zero_reg));
2373 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002374 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002375 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002376 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2377 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002378
2379 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002380}
2381
2382
2383void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002384 ASSERT(args->length() == 1);
2385
2386 VisitForAccumulatorValue(args->at(0));
2387
2388 Label materialize_true, materialize_false;
2389 Label* if_true = NULL;
2390 Label* if_false = NULL;
2391 Label* fall_through = NULL;
2392 context()->PrepareTest(&materialize_true, &materialize_false,
2393 &if_true, &if_false, &fall_through);
2394
2395 __ JumpIfSmi(v0, if_false);
2396 __ GetObjectType(v0, a1, a1);
2397 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002398 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002399 if_true, if_false, fall_through);
2400
2401 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002402}
2403
2404
2405void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002406 ASSERT(args->length() == 1);
2407
2408 VisitForAccumulatorValue(args->at(0));
2409
2410 Label materialize_true, materialize_false;
2411 Label* if_true = NULL;
2412 Label* if_false = NULL;
2413 Label* fall_through = NULL;
2414 context()->PrepareTest(&materialize_true, &materialize_false,
2415 &if_true, &if_false, &fall_through);
2416
2417 __ JumpIfSmi(v0, if_false);
2418 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2419 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2420 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2421 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2422 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2423
2424 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002425}
2426
2427
2428void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2429 ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002430
2431 ASSERT(args->length() == 1);
2432
2433 VisitForAccumulatorValue(args->at(0));
2434
2435 Label materialize_true, materialize_false;
2436 Label* if_true = NULL;
2437 Label* if_false = NULL;
2438 Label* fall_through = NULL;
2439 context()->PrepareTest(&materialize_true, &materialize_false,
2440 &if_true, &if_false, &fall_through);
2441
2442 if (FLAG_debug_code) __ AbortIfSmi(v0);
2443
2444 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2445 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2446 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2447 __ Branch(if_true, ne, t0, Operand(zero_reg));
2448
2449 // Check for fast case object. Generate false result for slow case object.
2450 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2451 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2452 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2453 __ Branch(if_false, eq, a2, Operand(t0));
2454
2455 // Look for valueOf symbol in the descriptor array, and indicate false if
2456 // found. The type is not checked, so if it is a transition it is a false
2457 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002458 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002459 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2460 // t0: descriptor array
2461 // a3: length of descriptor array
2462 // Calculate the end of the descriptor array.
2463 STATIC_ASSERT(kSmiTag == 0);
2464 STATIC_ASSERT(kSmiTagSize == 1);
2465 STATIC_ASSERT(kPointerSize == 4);
2466 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2467 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2468 __ Addu(a2, a2, t1);
2469
2470 // Calculate location of the first key name.
2471 __ Addu(t0,
2472 t0,
2473 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2474 DescriptorArray::kFirstIndex * kPointerSize));
2475 // Loop through all the keys in the descriptor array. If one of these is the
2476 // symbol valueOf the result is false.
2477 Label entry, loop;
2478 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2479 // used in the loop below.
2480 __ li(t2, Operand(FACTORY->value_of_symbol()));
2481 __ jmp(&entry);
2482 __ bind(&loop);
2483 __ lw(a3, MemOperand(t0, 0));
2484 __ Branch(if_false, eq, a3, Operand(t2));
2485 __ Addu(t0, t0, Operand(kPointerSize));
2486 __ bind(&entry);
2487 __ Branch(&loop, ne, t0, Operand(a2));
2488
2489 // If a valueOf property is not found on the object check that it's
2490 // prototype is the un-modified String prototype. If not result is false.
2491 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2492 __ JumpIfSmi(a2, if_false);
2493 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2494 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2495 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2496 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2497 __ Branch(if_false, ne, a2, Operand(a3));
2498
2499 // Set the bit in the map to indicate that it has been checked safe for
2500 // default valueOf and set true result.
2501 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2502 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2503 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2504 __ jmp(if_true);
2505
2506 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2507 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002508}
2509
2510
2511void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002512 ASSERT(args->length() == 1);
2513
2514 VisitForAccumulatorValue(args->at(0));
2515
2516 Label materialize_true, materialize_false;
2517 Label* if_true = NULL;
2518 Label* if_false = NULL;
2519 Label* fall_through = NULL;
2520 context()->PrepareTest(&materialize_true, &materialize_false,
2521 &if_true, &if_false, &fall_through);
2522
2523 __ JumpIfSmi(v0, if_false);
2524 __ GetObjectType(v0, a1, a2);
2525 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2526 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2527 __ Branch(if_false);
2528
2529 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002530}
2531
2532
2533void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002534 ASSERT(args->length() == 1);
2535
2536 VisitForAccumulatorValue(args->at(0));
2537
2538 Label materialize_true, materialize_false;
2539 Label* if_true = NULL;
2540 Label* if_false = NULL;
2541 Label* fall_through = NULL;
2542 context()->PrepareTest(&materialize_true, &materialize_false,
2543 &if_true, &if_false, &fall_through);
2544
2545 __ JumpIfSmi(v0, if_false);
2546 __ GetObjectType(v0, a1, a1);
2547 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2548 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2549 if_true, if_false, fall_through);
2550
2551 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002552}
2553
2554
2555void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002556 ASSERT(args->length() == 1);
2557
2558 VisitForAccumulatorValue(args->at(0));
2559
2560 Label materialize_true, materialize_false;
2561 Label* if_true = NULL;
2562 Label* if_false = NULL;
2563 Label* fall_through = NULL;
2564 context()->PrepareTest(&materialize_true, &materialize_false,
2565 &if_true, &if_false, &fall_through);
2566
2567 __ JumpIfSmi(v0, if_false);
2568 __ GetObjectType(v0, a1, a1);
2569 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2570 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2571
2572 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002573}
2574
2575
2576void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002577 ASSERT(args->length() == 0);
2578
2579 Label materialize_true, materialize_false;
2580 Label* if_true = NULL;
2581 Label* if_false = NULL;
2582 Label* fall_through = NULL;
2583 context()->PrepareTest(&materialize_true, &materialize_false,
2584 &if_true, &if_false, &fall_through);
2585
2586 // Get the frame pointer for the calling frame.
2587 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2588
2589 // Skip the arguments adaptor frame if it exists.
2590 Label check_frame_marker;
2591 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2592 __ Branch(&check_frame_marker, ne,
2593 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2594 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2595
2596 // Check the marker in the calling frame.
2597 __ bind(&check_frame_marker);
2598 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
2599 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2600 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2601 if_true, if_false, fall_through);
2602
2603 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002604}
2605
2606
2607void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002608 ASSERT(args->length() == 2);
2609
2610 // Load the two objects into registers and perform the comparison.
2611 VisitForStackValue(args->at(0));
2612 VisitForAccumulatorValue(args->at(1));
2613
2614 Label materialize_true, materialize_false;
2615 Label* if_true = NULL;
2616 Label* if_false = NULL;
2617 Label* fall_through = NULL;
2618 context()->PrepareTest(&materialize_true, &materialize_false,
2619 &if_true, &if_false, &fall_through);
2620
2621 __ pop(a1);
2622 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2623 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2624
2625 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002626}
2627
2628
2629void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002630 ASSERT(args->length() == 1);
2631
2632 // ArgumentsAccessStub expects the key in a1 and the formal
2633 // parameter count in a0.
2634 VisitForAccumulatorValue(args->at(0));
2635 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002636 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002637 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2638 __ CallStub(&stub);
2639 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002640}
2641
2642
2643void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002644 ASSERT(args->length() == 0);
2645
2646 Label exit;
2647 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002648 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002649
2650 // Check if the calling frame is an arguments adaptor frame.
2651 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2652 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2653 __ Branch(&exit, ne, a3,
2654 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2655
2656 // Arguments adaptor case: Read the arguments length from the
2657 // adaptor frame.
2658 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2659
2660 __ bind(&exit);
2661 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002662}
2663
2664
2665void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002666 ASSERT(args->length() == 1);
2667 Label done, null, function, non_function_constructor;
2668
2669 VisitForAccumulatorValue(args->at(0));
2670
2671 // If the object is a smi, we return null.
2672 __ JumpIfSmi(v0, &null);
2673
2674 // Check that the object is a JS object but take special care of JS
2675 // functions to make sure they have 'Function' as their class.
2676 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002677 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002678
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002679 // As long as LAST_CALLABLE_SPEC_OBJECT_TYPE is the last instance type, and
2680 // FIRST_CALLABLE_SPEC_OBJECT_TYPE comes right after
2681 // LAST_NONCALLABLE_SPEC_OBJECT_TYPE, we can avoid checking for the latter.
2682 STATIC_ASSERT(LAST_TYPE == LAST_CALLABLE_SPEC_OBJECT_TYPE);
2683 STATIC_ASSERT(FIRST_CALLABLE_SPEC_OBJECT_TYPE ==
2684 LAST_NONCALLABLE_SPEC_OBJECT_TYPE + 1);
2685 __ Branch(&function, ge, a1, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002686
2687 // Check if the constructor in the map is a function.
2688 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2689 __ GetObjectType(v0, a1, a1);
2690 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2691
2692 // v0 now contains the constructor function. Grab the
2693 // instance class name from there.
2694 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2695 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2696 __ Branch(&done);
2697
2698 // Functions have class 'Function'.
2699 __ bind(&function);
2700 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2701 __ jmp(&done);
2702
2703 // Objects with a non-function constructor have class 'Object'.
2704 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002705 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002706 __ jmp(&done);
2707
2708 // Non-JS objects have class null.
2709 __ bind(&null);
2710 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2711
2712 // All done.
2713 __ bind(&done);
2714
2715 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002716}
2717
2718
2719void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002720 // Conditionally generate a log call.
2721 // Args:
2722 // 0 (literal string): The type of logging (corresponds to the flags).
2723 // This is used to determine whether or not to generate the log call.
2724 // 1 (string): Format string. Access the string at argument index 2
2725 // with '%2s' (see Logger::LogRuntime for all the formats).
2726 // 2 (array): Arguments to the format string.
2727 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002728 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2729 VisitForStackValue(args->at(1));
2730 VisitForStackValue(args->at(2));
2731 __ CallRuntime(Runtime::kLog, 2);
2732 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002733
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002734 // Finally, we're expected to leave a value on the top of the stack.
2735 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2736 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002737}
2738
2739
2740void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002741 ASSERT(args->length() == 0);
2742
2743 Label slow_allocate_heapnumber;
2744 Label heapnumber_allocated;
2745
2746 // Save the new heap number in callee-saved register s0, since
2747 // we call out to external C code below.
2748 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2749 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2750 __ jmp(&heapnumber_allocated);
2751
2752 __ bind(&slow_allocate_heapnumber);
2753
2754 // Allocate a heap number.
2755 __ CallRuntime(Runtime::kNumberAlloc, 0);
2756 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2757
2758 __ bind(&heapnumber_allocated);
2759
2760 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2761 // by computing:
2762 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2763 if (CpuFeatures::IsSupported(FPU)) {
2764 __ PrepareCallCFunction(1, a0);
2765 __ li(a0, Operand(ExternalReference::isolate_address()));
2766 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2767
2768
2769 CpuFeatures::Scope scope(FPU);
2770 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2771 __ li(a1, Operand(0x41300000));
2772 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002773 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002774 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002775 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002776 // Subtract and store the result in the heap number.
2777 __ sub_d(f0, f12, f14);
2778 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2779 __ mov(v0, s0);
2780 } else {
2781 __ PrepareCallCFunction(2, a0);
2782 __ mov(a0, s0);
2783 __ li(a1, Operand(ExternalReference::isolate_address()));
2784 __ CallCFunction(
2785 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2786 }
2787
2788 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002789}
2790
2791
2792void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002793 // Load the arguments on the stack and call the stub.
2794 SubStringStub stub;
2795 ASSERT(args->length() == 3);
2796 VisitForStackValue(args->at(0));
2797 VisitForStackValue(args->at(1));
2798 VisitForStackValue(args->at(2));
2799 __ CallStub(&stub);
2800 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002801}
2802
2803
2804void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002805 // Load the arguments on the stack and call the stub.
2806 RegExpExecStub stub;
2807 ASSERT(args->length() == 4);
2808 VisitForStackValue(args->at(0));
2809 VisitForStackValue(args->at(1));
2810 VisitForStackValue(args->at(2));
2811 VisitForStackValue(args->at(3));
2812 __ CallStub(&stub);
2813 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002814}
2815
2816
2817void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002818 ASSERT(args->length() == 1);
2819
2820 VisitForAccumulatorValue(args->at(0)); // Load the object.
2821
2822 Label done;
2823 // If the object is a smi return the object.
2824 __ JumpIfSmi(v0, &done);
2825 // If the object is not a value type, return the object.
2826 __ GetObjectType(v0, a1, a1);
2827 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2828
2829 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2830
2831 __ bind(&done);
2832 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002833}
2834
2835
2836void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002837 // Load the arguments on the stack and call the runtime function.
2838 ASSERT(args->length() == 2);
2839 VisitForStackValue(args->at(0));
2840 VisitForStackValue(args->at(1));
2841 MathPowStub stub;
2842 __ CallStub(&stub);
2843 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002844}
2845
2846
2847void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002848 ASSERT(args->length() == 2);
2849
2850 VisitForStackValue(args->at(0)); // Load the object.
2851 VisitForAccumulatorValue(args->at(1)); // Load the value.
2852 __ pop(a1); // v0 = value. a1 = object.
2853
2854 Label done;
2855 // If the object is a smi, return the value.
2856 __ JumpIfSmi(a1, &done);
2857
2858 // If the object is not a value type, return the value.
2859 __ GetObjectType(a1, a2, a2);
2860 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2861
2862 // Store the value.
2863 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2864 // Update the write barrier. Save the value as it will be
2865 // overwritten by the write barrier code and is needed afterward.
2866 __ RecordWrite(a1, Operand(JSValue::kValueOffset - kHeapObjectTag), a2, a3);
2867
2868 __ bind(&done);
2869 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002870}
2871
2872
2873void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002874 ASSERT_EQ(args->length(), 1);
2875
2876 // Load the argument on the stack and call the stub.
2877 VisitForStackValue(args->at(0));
2878
2879 NumberToStringStub stub;
2880 __ CallStub(&stub);
2881 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002882}
2883
2884
2885void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002886 ASSERT(args->length() == 1);
2887
2888 VisitForAccumulatorValue(args->at(0));
2889
2890 Label done;
2891 StringCharFromCodeGenerator generator(v0, a1);
2892 generator.GenerateFast(masm_);
2893 __ jmp(&done);
2894
2895 NopRuntimeCallHelper call_helper;
2896 generator.GenerateSlow(masm_, call_helper);
2897
2898 __ bind(&done);
2899 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002900}
2901
2902
2903void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002904 ASSERT(args->length() == 2);
2905
2906 VisitForStackValue(args->at(0));
2907 VisitForAccumulatorValue(args->at(1));
2908 __ mov(a0, result_register());
2909
2910 Register object = a1;
2911 Register index = a0;
2912 Register scratch = a2;
2913 Register result = v0;
2914
2915 __ pop(object);
2916
2917 Label need_conversion;
2918 Label index_out_of_range;
2919 Label done;
2920 StringCharCodeAtGenerator generator(object,
2921 index,
2922 scratch,
2923 result,
2924 &need_conversion,
2925 &need_conversion,
2926 &index_out_of_range,
2927 STRING_INDEX_IS_NUMBER);
2928 generator.GenerateFast(masm_);
2929 __ jmp(&done);
2930
2931 __ bind(&index_out_of_range);
2932 // When the index is out of range, the spec requires us to return
2933 // NaN.
2934 __ LoadRoot(result, Heap::kNanValueRootIndex);
2935 __ jmp(&done);
2936
2937 __ bind(&need_conversion);
2938 // Load the undefined value into the result register, which will
2939 // trigger conversion.
2940 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2941 __ jmp(&done);
2942
2943 NopRuntimeCallHelper call_helper;
2944 generator.GenerateSlow(masm_, call_helper);
2945
2946 __ bind(&done);
2947 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002948}
2949
2950
2951void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002952 ASSERT(args->length() == 2);
2953
2954 VisitForStackValue(args->at(0));
2955 VisitForAccumulatorValue(args->at(1));
2956 __ mov(a0, result_register());
2957
2958 Register object = a1;
2959 Register index = a0;
2960 Register scratch1 = a2;
2961 Register scratch2 = a3;
2962 Register result = v0;
2963
2964 __ pop(object);
2965
2966 Label need_conversion;
2967 Label index_out_of_range;
2968 Label done;
2969 StringCharAtGenerator generator(object,
2970 index,
2971 scratch1,
2972 scratch2,
2973 result,
2974 &need_conversion,
2975 &need_conversion,
2976 &index_out_of_range,
2977 STRING_INDEX_IS_NUMBER);
2978 generator.GenerateFast(masm_);
2979 __ jmp(&done);
2980
2981 __ bind(&index_out_of_range);
2982 // When the index is out of range, the spec requires us to return
2983 // the empty string.
2984 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2985 __ jmp(&done);
2986
2987 __ bind(&need_conversion);
2988 // Move smi zero into the result register, which will trigger
2989 // conversion.
2990 __ li(result, Operand(Smi::FromInt(0)));
2991 __ jmp(&done);
2992
2993 NopRuntimeCallHelper call_helper;
2994 generator.GenerateSlow(masm_, call_helper);
2995
2996 __ bind(&done);
2997 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002998}
2999
3000
3001void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003002 ASSERT_EQ(2, args->length());
3003
3004 VisitForStackValue(args->at(0));
3005 VisitForStackValue(args->at(1));
3006
3007 StringAddStub stub(NO_STRING_ADD_FLAGS);
3008 __ CallStub(&stub);
3009 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003010}
3011
3012
3013void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003014 ASSERT_EQ(2, args->length());
3015
3016 VisitForStackValue(args->at(0));
3017 VisitForStackValue(args->at(1));
3018
3019 StringCompareStub stub;
3020 __ CallStub(&stub);
3021 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003022}
3023
3024
3025void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003026 // Load the argument on the stack and call the stub.
3027 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3028 TranscendentalCacheStub::TAGGED);
3029 ASSERT(args->length() == 1);
3030 VisitForStackValue(args->at(0));
3031 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3032 __ CallStub(&stub);
3033 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003034}
3035
3036
3037void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003038 // Load the argument on the stack and call the stub.
3039 TranscendentalCacheStub stub(TranscendentalCache::COS,
3040 TranscendentalCacheStub::TAGGED);
3041 ASSERT(args->length() == 1);
3042 VisitForStackValue(args->at(0));
3043 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3044 __ CallStub(&stub);
3045 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003046}
3047
3048
3049void FullCodeGenerator::EmitMathLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003050 // Load the argument on the stack and call the stub.
3051 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3052 TranscendentalCacheStub::TAGGED);
3053 ASSERT(args->length() == 1);
3054 VisitForStackValue(args->at(0));
3055 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3056 __ CallStub(&stub);
3057 context()->Plug(v0);
3058}
3059
3060
3061void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
3062 // Load the argument on the stack and call the runtime function.
3063 ASSERT(args->length() == 1);
3064 VisitForStackValue(args->at(0));
3065 __ CallRuntime(Runtime::kMath_sqrt, 1);
3066 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003067}
3068
3069
3070void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003071 ASSERT(args->length() >= 2);
3072
3073 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3074 for (int i = 0; i < arg_count + 1; i++) {
3075 VisitForStackValue(args->at(i));
3076 }
3077 VisitForAccumulatorValue(args->last()); // Function.
3078
3079 // InvokeFunction requires the function in a1. Move it in there.
3080 __ mov(a1, result_register());
3081 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003082 __ InvokeFunction(a1, count, CALL_FUNCTION,
3083 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003084 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3085 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003086}
3087
3088
3089void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003090 RegExpConstructResultStub stub;
3091 ASSERT(args->length() == 3);
3092 VisitForStackValue(args->at(0));
3093 VisitForStackValue(args->at(1));
3094 VisitForStackValue(args->at(2));
3095 __ CallStub(&stub);
3096 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003097}
3098
3099
3100void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003101 ASSERT(args->length() == 3);
3102 VisitForStackValue(args->at(0));
3103 VisitForStackValue(args->at(1));
3104 VisitForStackValue(args->at(2));
3105 Label done;
3106 Label slow_case;
3107 Register object = a0;
3108 Register index1 = a1;
3109 Register index2 = a2;
3110 Register elements = a3;
3111 Register scratch1 = t0;
3112 Register scratch2 = t1;
3113
3114 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3115 // Fetch the map and check if array is in fast case.
3116 // Check that object doesn't require security checks and
3117 // has no indexed interceptor.
3118 __ GetObjectType(object, scratch1, scratch2);
3119 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3120 // Map is now in scratch1.
3121
3122 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3123 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3124 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3125
3126 // Check the object's elements are in fast case and writable.
3127 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3128 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3129 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3130 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3131
3132 // Check that both indices are smis.
3133 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3134 __ lw(index2, MemOperand(sp, 0));
3135 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3136
3137 // Check that both indices are valid.
3138 Label not_hi;
3139 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3140 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3141 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3142 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3143 __ bind(&not_hi);
3144
3145 // Bring the address of the elements into index1 and index2.
3146 __ Addu(scratch1, elements,
3147 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3148 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3149 __ Addu(index1, scratch1, index1);
3150 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3151 __ Addu(index2, scratch1, index2);
3152
3153 // Swap elements.
3154 __ lw(scratch1, MemOperand(index1, 0));
3155 __ lw(scratch2, MemOperand(index2, 0));
3156 __ sw(scratch1, MemOperand(index2, 0));
3157 __ sw(scratch2, MemOperand(index1, 0));
3158
3159 Label new_space;
3160 __ InNewSpace(elements, scratch1, eq, &new_space);
3161 // Possible optimization: do a check that both values are Smis
3162 // (or them and test against Smi mask).
3163
3164 __ mov(scratch1, elements);
3165 __ RecordWriteHelper(elements, index1, scratch2);
3166 __ RecordWriteHelper(scratch1, index2, scratch2); // scratch1 holds elements.
3167
3168 __ bind(&new_space);
3169 // We are done. Drop elements from the stack, and return undefined.
3170 __ Drop(3);
3171 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3172 __ jmp(&done);
3173
3174 __ bind(&slow_case);
3175 __ CallRuntime(Runtime::kSwapElements, 3);
3176
3177 __ bind(&done);
3178 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003179}
3180
3181
3182void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003183 ASSERT_EQ(2, args->length());
3184
3185 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3186 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3187
3188 Handle<FixedArray> jsfunction_result_caches(
3189 isolate()->global_context()->jsfunction_result_caches());
3190 if (jsfunction_result_caches->length() <= cache_id) {
3191 __ Abort("Attempt to use undefined cache.");
3192 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3193 context()->Plug(v0);
3194 return;
3195 }
3196
3197 VisitForAccumulatorValue(args->at(1));
3198
3199 Register key = v0;
3200 Register cache = a1;
3201 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3202 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3203 __ lw(cache,
3204 ContextOperand(
3205 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3206 __ lw(cache,
3207 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3208
3209
3210 Label done, not_found;
3211 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
3212 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3213 // a2 now holds finger offset as a smi.
3214 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3215 // a3 now points to the start of fixed array elements.
3216 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3217 __ addu(a3, a3, at);
3218 // a3 now points to key of indexed element of cache.
3219 __ lw(a2, MemOperand(a3));
3220 __ Branch(&not_found, ne, key, Operand(a2));
3221
3222 __ lw(v0, MemOperand(a3, kPointerSize));
3223 __ Branch(&done);
3224
3225 __ bind(&not_found);
3226 // Call runtime to perform the lookup.
3227 __ Push(cache, key);
3228 __ CallRuntime(Runtime::kGetFromCache, 2);
3229
3230 __ bind(&done);
3231 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003232}
3233
3234
3235void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003236 ASSERT_EQ(2, args->length());
3237
3238 Register right = v0;
3239 Register left = a1;
3240 Register tmp = a2;
3241 Register tmp2 = a3;
3242
3243 VisitForStackValue(args->at(0));
3244 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3245 __ pop(left);
3246
3247 Label done, fail, ok;
3248 __ Branch(&ok, eq, left, Operand(right));
3249 // Fail if either is a non-HeapObject.
3250 __ And(tmp, left, Operand(right));
3251 __ And(at, tmp, Operand(kSmiTagMask));
3252 __ Branch(&fail, eq, at, Operand(zero_reg));
3253 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3254 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3255 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3256 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3257 __ Branch(&fail, ne, tmp, Operand(tmp2));
3258 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3259 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3260 __ Branch(&ok, eq, tmp, Operand(tmp2));
3261 __ bind(&fail);
3262 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3263 __ jmp(&done);
3264 __ bind(&ok);
3265 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3266 __ bind(&done);
3267
3268 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003269}
3270
3271
3272void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003273 VisitForAccumulatorValue(args->at(0));
3274
3275 Label materialize_true, materialize_false;
3276 Label* if_true = NULL;
3277 Label* if_false = NULL;
3278 Label* fall_through = NULL;
3279 context()->PrepareTest(&materialize_true, &materialize_false,
3280 &if_true, &if_false, &fall_through);
3281
3282 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3283 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3284
3285 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3286 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3287
3288 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003289}
3290
3291
3292void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003293 ASSERT(args->length() == 1);
3294 VisitForAccumulatorValue(args->at(0));
3295
3296 if (FLAG_debug_code) {
3297 __ AbortIfNotString(v0);
3298 }
3299
3300 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3301 __ IndexFromHash(v0, v0);
3302
3303 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003304}
3305
3306
3307void FullCodeGenerator::EmitFastAsciiArrayJoin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003308 Label bailout, done, one_char_separator, long_separator,
3309 non_trivial_array, not_size_one_array, loop,
3310 empty_separator_loop, one_char_separator_loop,
3311 one_char_separator_loop_entry, long_separator_loop;
3312
3313 ASSERT(args->length() == 2);
3314 VisitForStackValue(args->at(1));
3315 VisitForAccumulatorValue(args->at(0));
3316
3317 // All aliases of the same register have disjoint lifetimes.
3318 Register array = v0;
3319 Register elements = no_reg; // Will be v0.
3320 Register result = no_reg; // Will be v0.
3321 Register separator = a1;
3322 Register array_length = a2;
3323 Register result_pos = no_reg; // Will be a2.
3324 Register string_length = a3;
3325 Register string = t0;
3326 Register element = t1;
3327 Register elements_end = t2;
3328 Register scratch1 = t3;
3329 Register scratch2 = t5;
3330 Register scratch3 = t4;
3331 Register scratch4 = v1;
3332
3333 // Separator operand is on the stack.
3334 __ pop(separator);
3335
3336 // Check that the array is a JSArray.
3337 __ JumpIfSmi(array, &bailout);
3338 __ GetObjectType(array, scratch1, scratch2);
3339 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3340
3341 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003342 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003343
3344 // If the array has length zero, return the empty string.
3345 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3346 __ SmiUntag(array_length);
3347 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3348 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3349 __ Branch(&done);
3350
3351 __ bind(&non_trivial_array);
3352
3353 // Get the FixedArray containing array's elements.
3354 elements = array;
3355 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3356 array = no_reg; // End of array's live range.
3357
3358 // Check that all array elements are sequential ASCII strings, and
3359 // accumulate the sum of their lengths, as a smi-encoded value.
3360 __ mov(string_length, zero_reg);
3361 __ Addu(element,
3362 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3363 __ sll(elements_end, array_length, kPointerSizeLog2);
3364 __ Addu(elements_end, element, elements_end);
3365 // Loop condition: while (element < elements_end).
3366 // Live values in registers:
3367 // elements: Fixed array of strings.
3368 // array_length: Length of the fixed array of strings (not smi)
3369 // separator: Separator string
3370 // string_length: Accumulated sum of string lengths (smi).
3371 // element: Current array element.
3372 // elements_end: Array end.
3373 if (FLAG_debug_code) {
3374 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3375 array_length, Operand(zero_reg));
3376 }
3377 __ bind(&loop);
3378 __ lw(string, MemOperand(element));
3379 __ Addu(element, element, kPointerSize);
3380 __ JumpIfSmi(string, &bailout);
3381 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3382 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3383 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3384 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3385 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3386 __ BranchOnOverflow(&bailout, scratch3);
3387 __ Branch(&loop, lt, element, Operand(elements_end));
3388
3389 // If array_length is 1, return elements[0], a string.
3390 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3391 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3392 __ Branch(&done);
3393
3394 __ bind(&not_size_one_array);
3395
3396 // Live values in registers:
3397 // separator: Separator string
3398 // array_length: Length of the array.
3399 // string_length: Sum of string lengths (smi).
3400 // elements: FixedArray of strings.
3401
3402 // Check that the separator is a flat ASCII string.
3403 __ JumpIfSmi(separator, &bailout);
3404 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3405 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3406 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3407
3408 // Add (separator length times array_length) - separator length to the
3409 // string_length to get the length of the result string. array_length is not
3410 // smi but the other values are, so the result is a smi.
3411 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3412 __ Subu(string_length, string_length, Operand(scratch1));
3413 __ Mult(array_length, scratch1);
3414 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3415 // zero.
3416 __ mfhi(scratch2);
3417 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3418 __ mflo(scratch2);
3419 __ And(scratch3, scratch2, Operand(0x80000000));
3420 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3421 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3422 __ BranchOnOverflow(&bailout, scratch3);
3423 __ SmiUntag(string_length);
3424
3425 // Get first element in the array to free up the elements register to be used
3426 // for the result.
3427 __ Addu(element,
3428 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3429 result = elements; // End of live range for elements.
3430 elements = no_reg;
3431 // Live values in registers:
3432 // element: First array element
3433 // separator: Separator string
3434 // string_length: Length of result string (not smi)
3435 // array_length: Length of the array.
3436 __ AllocateAsciiString(result,
3437 string_length,
3438 scratch1,
3439 scratch2,
3440 elements_end,
3441 &bailout);
3442 // Prepare for looping. Set up elements_end to end of the array. Set
3443 // result_pos to the position of the result where to write the first
3444 // character.
3445 __ sll(elements_end, array_length, kPointerSizeLog2);
3446 __ Addu(elements_end, element, elements_end);
3447 result_pos = array_length; // End of live range for array_length.
3448 array_length = no_reg;
3449 __ Addu(result_pos,
3450 result,
3451 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3452
3453 // Check the length of the separator.
3454 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3455 __ li(at, Operand(Smi::FromInt(1)));
3456 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3457 __ Branch(&long_separator, gt, scratch1, Operand(at));
3458
3459 // Empty separator case.
3460 __ bind(&empty_separator_loop);
3461 // Live values in registers:
3462 // result_pos: the position to which we are currently copying characters.
3463 // element: Current array element.
3464 // elements_end: Array end.
3465
3466 // Copy next array element to the result.
3467 __ lw(string, MemOperand(element));
3468 __ Addu(element, element, kPointerSize);
3469 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3470 __ SmiUntag(string_length);
3471 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3472 __ CopyBytes(string, result_pos, string_length, scratch1);
3473 // End while (element < elements_end).
3474 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3475 ASSERT(result.is(v0));
3476 __ Branch(&done);
3477
3478 // One-character separator case.
3479 __ bind(&one_char_separator);
3480 // Replace separator with its ascii character value.
3481 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3482 // Jump into the loop after the code that copies the separator, so the first
3483 // element is not preceded by a separator.
3484 __ jmp(&one_char_separator_loop_entry);
3485
3486 __ bind(&one_char_separator_loop);
3487 // Live values in registers:
3488 // result_pos: the position to which we are currently copying characters.
3489 // element: Current array element.
3490 // elements_end: Array end.
3491 // separator: Single separator ascii char (in lower byte).
3492
3493 // Copy the separator character to the result.
3494 __ sb(separator, MemOperand(result_pos));
3495 __ Addu(result_pos, result_pos, 1);
3496
3497 // Copy next array element to the result.
3498 __ bind(&one_char_separator_loop_entry);
3499 __ lw(string, MemOperand(element));
3500 __ Addu(element, element, kPointerSize);
3501 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3502 __ SmiUntag(string_length);
3503 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3504 __ CopyBytes(string, result_pos, string_length, scratch1);
3505 // End while (element < elements_end).
3506 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3507 ASSERT(result.is(v0));
3508 __ Branch(&done);
3509
3510 // Long separator case (separator is more than one character). Entry is at the
3511 // label long_separator below.
3512 __ bind(&long_separator_loop);
3513 // Live values in registers:
3514 // result_pos: the position to which we are currently copying characters.
3515 // element: Current array element.
3516 // elements_end: Array end.
3517 // separator: Separator string.
3518
3519 // Copy the separator to the result.
3520 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3521 __ SmiUntag(string_length);
3522 __ Addu(string,
3523 separator,
3524 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3525 __ CopyBytes(string, result_pos, string_length, scratch1);
3526
3527 __ bind(&long_separator);
3528 __ lw(string, MemOperand(element));
3529 __ Addu(element, element, kPointerSize);
3530 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3531 __ SmiUntag(string_length);
3532 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3533 __ CopyBytes(string, result_pos, string_length, scratch1);
3534 // End while (element < elements_end).
3535 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3536 ASSERT(result.is(v0));
3537 __ Branch(&done);
3538
3539 __ bind(&bailout);
3540 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3541 __ bind(&done);
3542 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003543}
3544
3545
ager@chromium.org5c838252010-02-19 08:53:10 +00003546void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003547 Handle<String> name = expr->name();
3548 if (name->length() > 0 && name->Get(0) == '_') {
3549 Comment cmnt(masm_, "[ InlineRuntimeCall");
3550 EmitInlineRuntimeCall(expr);
3551 return;
3552 }
3553
3554 Comment cmnt(masm_, "[ CallRuntime");
3555 ZoneList<Expression*>* args = expr->arguments();
3556
3557 if (expr->is_jsruntime()) {
3558 // Prepare for calling JS runtime function.
3559 __ lw(a0, GlobalObjectOperand());
3560 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3561 __ push(a0);
3562 }
3563
3564 // Push the arguments ("left-to-right").
3565 int arg_count = args->length();
3566 for (int i = 0; i < arg_count; i++) {
3567 VisitForStackValue(args->at(i));
3568 }
3569
3570 if (expr->is_jsruntime()) {
3571 // Call the JS runtime function.
3572 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003573 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003574 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00003575 isolate()->stub_cache()->ComputeCallInitialize(arg_count,
3576 NOT_IN_LOOP,
3577 mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003578 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003579 // Restore context register.
3580 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3581 } else {
3582 // Call the C runtime function.
3583 __ CallRuntime(expr->function(), arg_count);
3584 }
3585 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003586}
3587
3588
3589void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003590 switch (expr->op()) {
3591 case Token::DELETE: {
3592 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
3593 Property* prop = expr->expression()->AsProperty();
3594 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
3595
3596 if (prop != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003597 VisitForStackValue(prop->obj());
3598 VisitForStackValue(prop->key());
3599 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3600 __ push(a1);
3601 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3602 context()->Plug(v0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003603 } else if (var != NULL) {
3604 // Delete of an unqualified identifier is disallowed in strict mode
3605 // but "delete this" is.
3606 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
3607 if (var->is_global()) {
3608 __ lw(a2, GlobalObjectOperand());
3609 __ li(a1, Operand(var->name()));
3610 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3611 __ Push(a2, a1, a0);
3612 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3613 context()->Plug(v0);
3614 } else if (var->AsSlot() != NULL &&
3615 var->AsSlot()->type() != Slot::LOOKUP) {
3616 // Result of deleting non-global, non-dynamic variables is false.
3617 // The subexpression does not have side effects.
3618 context()->Plug(false);
3619 } else {
3620 // Non-global variable. Call the runtime to try to delete from the
3621 // context where the variable was introduced.
3622 __ push(context_register());
3623 __ li(a2, Operand(var->name()));
3624 __ push(a2);
3625 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3626 context()->Plug(v0);
3627 }
3628 } else {
3629 // Result of deleting non-property, non-variable reference is true.
3630 // The subexpression may have side effects.
3631 VisitForEffect(expr->expression());
3632 context()->Plug(true);
3633 }
3634 break;
3635 }
3636
3637 case Token::VOID: {
3638 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3639 VisitForEffect(expr->expression());
3640 context()->Plug(Heap::kUndefinedValueRootIndex);
3641 break;
3642 }
3643
3644 case Token::NOT: {
3645 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3646 if (context()->IsEffect()) {
3647 // Unary NOT has no side effects so it's only necessary to visit the
3648 // subexpression. Match the optimizing compiler by not branching.
3649 VisitForEffect(expr->expression());
3650 } else {
3651 Label materialize_true, materialize_false;
3652 Label* if_true = NULL;
3653 Label* if_false = NULL;
3654 Label* fall_through = NULL;
3655
3656 // Notice that the labels are swapped.
3657 context()->PrepareTest(&materialize_true, &materialize_false,
3658 &if_false, &if_true, &fall_through);
3659 if (context()->IsTest()) ForwardBailoutToChild(expr);
3660 VisitForControl(expr->expression(), if_true, if_false, fall_through);
3661 context()->Plug(if_false, if_true); // Labels swapped.
3662 }
3663 break;
3664 }
3665
3666 case Token::TYPEOF: {
3667 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3668 { StackValueContext context(this);
3669 VisitForTypeofValue(expr->expression());
3670 }
3671 __ CallRuntime(Runtime::kTypeof, 1);
3672 context()->Plug(v0);
3673 break;
3674 }
3675
3676 case Token::ADD: {
3677 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3678 VisitForAccumulatorValue(expr->expression());
3679 Label no_conversion;
3680 __ JumpIfSmi(result_register(), &no_conversion);
3681 __ mov(a0, result_register());
3682 ToNumberStub convert_stub;
3683 __ CallStub(&convert_stub);
3684 __ bind(&no_conversion);
3685 context()->Plug(result_register());
3686 break;
3687 }
3688
3689 case Token::SUB:
3690 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3691 break;
3692
3693 case Token::BIT_NOT:
3694 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3695 break;
3696
3697 default:
3698 UNREACHABLE();
3699 }
3700}
3701
3702
3703void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3704 const char* comment) {
3705 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3706 Comment cmt(masm_, comment);
3707 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3708 UnaryOverwriteMode overwrite =
3709 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003710 UnaryOpStub stub(expr->op(), overwrite);
3711 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003712 VisitForAccumulatorValue(expr->expression());
3713 SetSourcePosition(expr->position());
3714 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003715 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003716 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003717}
3718
3719
3720void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003721 Comment cmnt(masm_, "[ CountOperation");
3722 SetSourcePosition(expr->position());
3723
3724 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3725 // as the left-hand side.
3726 if (!expr->expression()->IsValidLeftHandSide()) {
3727 VisitForEffect(expr->expression());
3728 return;
3729 }
3730
3731 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003732 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003733 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3734 LhsKind assign_type = VARIABLE;
3735 Property* prop = expr->expression()->AsProperty();
3736 // In case of a property we use the uninitialized expression context
3737 // of the key to detect a named property.
3738 if (prop != NULL) {
3739 assign_type =
3740 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3741 }
3742
3743 // Evaluate expression and get value.
3744 if (assign_type == VARIABLE) {
3745 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3746 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003747 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003748 } else {
3749 // Reserve space for result of postfix operation.
3750 if (expr->is_postfix() && !context()->IsEffect()) {
3751 __ li(at, Operand(Smi::FromInt(0)));
3752 __ push(at);
3753 }
3754 if (assign_type == NAMED_PROPERTY) {
3755 // Put the object both on the stack and in the accumulator.
3756 VisitForAccumulatorValue(prop->obj());
3757 __ push(v0);
3758 EmitNamedPropertyLoad(prop);
3759 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003760 VisitForStackValue(prop->obj());
3761 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003762 __ lw(a1, MemOperand(sp, 0));
3763 __ push(v0);
3764 EmitKeyedPropertyLoad(prop);
3765 }
3766 }
3767
3768 // We need a second deoptimization point after loading the value
3769 // in case evaluating the property load my have a side effect.
3770 if (assign_type == VARIABLE) {
3771 PrepareForBailout(expr->expression(), TOS_REG);
3772 } else {
3773 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3774 }
3775
3776 // Call ToNumber only if operand is not a smi.
3777 Label no_conversion;
3778 __ JumpIfSmi(v0, &no_conversion);
3779 __ mov(a0, v0);
3780 ToNumberStub convert_stub;
3781 __ CallStub(&convert_stub);
3782 __ bind(&no_conversion);
3783
3784 // Save result for postfix expressions.
3785 if (expr->is_postfix()) {
3786 if (!context()->IsEffect()) {
3787 // Save the result on the stack. If we have a named or keyed property
3788 // we store the result under the receiver that is currently on top
3789 // of the stack.
3790 switch (assign_type) {
3791 case VARIABLE:
3792 __ push(v0);
3793 break;
3794 case NAMED_PROPERTY:
3795 __ sw(v0, MemOperand(sp, kPointerSize));
3796 break;
3797 case KEYED_PROPERTY:
3798 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3799 break;
3800 }
3801 }
3802 }
3803 __ mov(a0, result_register());
3804
3805 // Inline smi case if we are in a loop.
3806 Label stub_call, done;
3807 JumpPatchSite patch_site(masm_);
3808
3809 int count_value = expr->op() == Token::INC ? 1 : -1;
3810 __ li(a1, Operand(Smi::FromInt(count_value)));
3811
3812 if (ShouldInlineSmiCase(expr->op())) {
3813 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
3814 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
3815
3816 // We could eliminate this smi check if we split the code at
3817 // the first smi check before calling ToNumber.
3818 patch_site.EmitJumpIfSmi(v0, &done);
3819 __ bind(&stub_call);
3820 }
3821
3822 // Record position before stub call.
3823 SetSourcePosition(expr->position());
3824
danno@chromium.org40cb8782011-05-25 07:58:50 +00003825 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003826 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003827 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003828 __ bind(&done);
3829
3830 // Store the value returned in v0.
3831 switch (assign_type) {
3832 case VARIABLE:
3833 if (expr->is_postfix()) {
3834 { EffectContext context(this);
3835 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3836 Token::ASSIGN);
3837 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3838 context.Plug(v0);
3839 }
3840 // For all contexts except EffectConstant we have the result on
3841 // top of the stack.
3842 if (!context()->IsEffect()) {
3843 context()->PlugTOS();
3844 }
3845 } else {
3846 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3847 Token::ASSIGN);
3848 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3849 context()->Plug(v0);
3850 }
3851 break;
3852 case NAMED_PROPERTY: {
3853 __ mov(a0, result_register()); // Value.
3854 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
3855 __ pop(a1); // Receiver.
3856 Handle<Code> ic = is_strict_mode()
3857 ? isolate()->builtins()->StoreIC_Initialize_Strict()
3858 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003859 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003860 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3861 if (expr->is_postfix()) {
3862 if (!context()->IsEffect()) {
3863 context()->PlugTOS();
3864 }
3865 } else {
3866 context()->Plug(v0);
3867 }
3868 break;
3869 }
3870 case KEYED_PROPERTY: {
3871 __ mov(a0, result_register()); // Value.
3872 __ pop(a1); // Key.
3873 __ pop(a2); // Receiver.
3874 Handle<Code> ic = is_strict_mode()
3875 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
3876 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003877 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003878 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3879 if (expr->is_postfix()) {
3880 if (!context()->IsEffect()) {
3881 context()->PlugTOS();
3882 }
3883 } else {
3884 context()->Plug(v0);
3885 }
3886 break;
3887 }
3888 }
ager@chromium.org5c838252010-02-19 08:53:10 +00003889}
3890
3891
lrn@chromium.org7516f052011-03-30 08:52:27 +00003892void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003893 VariableProxy* proxy = expr->AsVariableProxy();
3894 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
3895 Comment cmnt(masm_, "Global variable");
3896 __ lw(a0, GlobalObjectOperand());
3897 __ li(a2, Operand(proxy->name()));
3898 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
3899 // Use a regular load, not a contextual load, to avoid a reference
3900 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003901 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003902 PrepareForBailout(expr, TOS_REG);
3903 context()->Plug(v0);
3904 } else if (proxy != NULL &&
3905 proxy->var()->AsSlot() != NULL &&
3906 proxy->var()->AsSlot()->type() == Slot::LOOKUP) {
3907 Label done, slow;
3908
3909 // Generate code for loading from variables potentially shadowed
3910 // by eval-introduced variables.
3911 Slot* slot = proxy->var()->AsSlot();
3912 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
3913
3914 __ bind(&slow);
3915 __ li(a0, Operand(proxy->name()));
3916 __ Push(cp, a0);
3917 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
3918 PrepareForBailout(expr, TOS_REG);
3919 __ bind(&done);
3920
3921 context()->Plug(v0);
3922 } else {
3923 // This expression cannot throw a reference error at the top level.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003924 VisitInCurrentContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003925 }
ager@chromium.org5c838252010-02-19 08:53:10 +00003926}
3927
ager@chromium.org04921a82011-06-27 13:21:41 +00003928void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
3929 Handle<String> check,
3930 Label* if_true,
3931 Label* if_false,
3932 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003933 { AccumulatorValueContext context(this);
ager@chromium.org04921a82011-06-27 13:21:41 +00003934 VisitForTypeofValue(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003935 }
3936 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3937
3938 if (check->Equals(isolate()->heap()->number_symbol())) {
3939 __ JumpIfSmi(v0, if_true);
3940 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
3941 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3942 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
3943 } else if (check->Equals(isolate()->heap()->string_symbol())) {
3944 __ JumpIfSmi(v0, if_false);
3945 // Check for undetectable objects => false.
3946 __ GetObjectType(v0, v0, a1);
3947 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
3948 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
3949 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
3950 Split(eq, a1, Operand(zero_reg),
3951 if_true, if_false, fall_through);
3952 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
3953 __ LoadRoot(at, Heap::kTrueValueRootIndex);
3954 __ Branch(if_true, eq, v0, Operand(at));
3955 __ LoadRoot(at, Heap::kFalseValueRootIndex);
3956 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00003957 } else if (FLAG_harmony_typeof &&
3958 check->Equals(isolate()->heap()->null_symbol())) {
3959 __ LoadRoot(at, Heap::kNullValueRootIndex);
3960 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003961 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
3962 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
3963 __ Branch(if_true, eq, v0, Operand(at));
3964 __ JumpIfSmi(v0, if_false);
3965 // Check for undetectable objects => true.
3966 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
3967 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
3968 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
3969 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
3970 } else if (check->Equals(isolate()->heap()->function_symbol())) {
3971 __ JumpIfSmi(v0, if_false);
3972 __ GetObjectType(v0, a1, v0); // Leave map in a1.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003973 Split(ge, v0, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003974 if_true, if_false, fall_through);
3975
3976 } else if (check->Equals(isolate()->heap()->object_symbol())) {
3977 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00003978 if (!FLAG_harmony_typeof) {
3979 __ LoadRoot(at, Heap::kNullValueRootIndex);
3980 __ Branch(if_true, eq, v0, Operand(at));
3981 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003982 // Check for JS objects => true.
3983 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003984 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003985 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003986 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003987 // Check for undetectable objects => false.
3988 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
3989 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
3990 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
3991 } else {
3992 if (if_false != fall_through) __ jmp(if_false);
3993 }
ager@chromium.org04921a82011-06-27 13:21:41 +00003994}
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003995
ager@chromium.org04921a82011-06-27 13:21:41 +00003996
3997void FullCodeGenerator::EmitLiteralCompareUndefined(Expression* expr,
3998 Label* if_true,
3999 Label* if_false,
4000 Label* fall_through) {
4001 VisitForAccumulatorValue(expr);
4002 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4003
4004 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4005 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004006}
4007
4008
ager@chromium.org5c838252010-02-19 08:53:10 +00004009void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004010 Comment cmnt(masm_, "[ CompareOperation");
4011 SetSourcePosition(expr->position());
4012
4013 // Always perform the comparison for its control flow. Pack the result
4014 // into the expression's context after the comparison is performed.
4015
4016 Label materialize_true, materialize_false;
4017 Label* if_true = NULL;
4018 Label* if_false = NULL;
4019 Label* fall_through = NULL;
4020 context()->PrepareTest(&materialize_true, &materialize_false,
4021 &if_true, &if_false, &fall_through);
4022
4023 // First we try a fast inlined version of the compare when one of
4024 // the operands is a literal.
ager@chromium.org04921a82011-06-27 13:21:41 +00004025 if (TryLiteralCompare(expr, if_true, if_false, fall_through)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004026 context()->Plug(if_true, if_false);
4027 return;
4028 }
4029
ager@chromium.org04921a82011-06-27 13:21:41 +00004030 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004031 VisitForStackValue(expr->left());
4032 switch (op) {
4033 case Token::IN:
4034 VisitForStackValue(expr->right());
4035 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
4036 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
4037 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4038 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4039 break;
4040
4041 case Token::INSTANCEOF: {
4042 VisitForStackValue(expr->right());
4043 InstanceofStub stub(InstanceofStub::kNoFlags);
4044 __ CallStub(&stub);
4045 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4046 // The stub returns 0 for true.
4047 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4048 break;
4049 }
4050
4051 default: {
4052 VisitForAccumulatorValue(expr->right());
4053 Condition cc = eq;
4054 bool strict = false;
4055 switch (op) {
4056 case Token::EQ_STRICT:
4057 strict = true;
4058 // Fall through.
4059 case Token::EQ:
4060 cc = eq;
4061 __ mov(a0, result_register());
4062 __ pop(a1);
4063 break;
4064 case Token::LT:
4065 cc = lt;
4066 __ mov(a0, result_register());
4067 __ pop(a1);
4068 break;
4069 case Token::GT:
4070 // Reverse left and right sides to obtain ECMA-262 conversion order.
4071 cc = lt;
4072 __ mov(a1, result_register());
4073 __ pop(a0);
4074 break;
4075 case Token::LTE:
4076 // Reverse left and right sides to obtain ECMA-262 conversion order.
4077 cc = ge;
4078 __ mov(a1, result_register());
4079 __ pop(a0);
4080 break;
4081 case Token::GTE:
4082 cc = ge;
4083 __ mov(a0, result_register());
4084 __ pop(a1);
4085 break;
4086 case Token::IN:
4087 case Token::INSTANCEOF:
4088 default:
4089 UNREACHABLE();
4090 }
4091
4092 bool inline_smi_code = ShouldInlineSmiCase(op);
4093 JumpPatchSite patch_site(masm_);
4094 if (inline_smi_code) {
4095 Label slow_case;
4096 __ Or(a2, a0, Operand(a1));
4097 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4098 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4099 __ bind(&slow_case);
4100 }
4101 // Record position and call the compare IC.
4102 SetSourcePosition(expr->position());
4103 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004104 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004105 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004106 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4107 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4108 }
4109 }
4110
4111 // Convert the result of the comparison into one expected for this
4112 // expression's context.
4113 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004114}
4115
4116
lrn@chromium.org7516f052011-03-30 08:52:27 +00004117void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004118 Comment cmnt(masm_, "[ CompareToNull");
4119 Label materialize_true, materialize_false;
4120 Label* if_true = NULL;
4121 Label* if_false = NULL;
4122 Label* fall_through = NULL;
4123 context()->PrepareTest(&materialize_true, &materialize_false,
4124 &if_true, &if_false, &fall_through);
4125
4126 VisitForAccumulatorValue(expr->expression());
4127 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4128 __ mov(a0, result_register());
4129 __ LoadRoot(a1, Heap::kNullValueRootIndex);
4130 if (expr->is_strict()) {
4131 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4132 } else {
4133 __ Branch(if_true, eq, a0, Operand(a1));
4134 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
4135 __ Branch(if_true, eq, a0, Operand(a1));
4136 __ And(at, a0, Operand(kSmiTagMask));
4137 __ Branch(if_false, eq, at, Operand(zero_reg));
4138 // It can be an undetectable object.
4139 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4140 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4141 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4142 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4143 }
4144 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004145}
4146
4147
ager@chromium.org5c838252010-02-19 08:53:10 +00004148void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004149 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4150 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004151}
4152
4153
lrn@chromium.org7516f052011-03-30 08:52:27 +00004154Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004155 return v0;
4156}
ager@chromium.org5c838252010-02-19 08:53:10 +00004157
4158
lrn@chromium.org7516f052011-03-30 08:52:27 +00004159Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004160 return cp;
4161}
4162
4163
ager@chromium.org5c838252010-02-19 08:53:10 +00004164void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004165 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4166 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004167}
4168
4169
4170void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004171 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004172}
4173
4174
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004175void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4176 Scope* declaration_scope = scope()->DeclarationScope();
4177 if (declaration_scope->is_global_scope()) {
4178 // Contexts nested in the global context have a canonical empty function
4179 // as their closure, not the anonymous closure containing the global
4180 // code. Pass a smi sentinel and let the runtime look up the empty
4181 // function.
4182 __ li(at, Operand(Smi::FromInt(0)));
4183 } else if (declaration_scope->is_eval_scope()) {
4184 // Contexts created by a call to eval have the same closure as the
4185 // context calling eval, not the anonymous closure containing the eval
4186 // code. Fetch it from the context.
4187 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4188 } else {
4189 ASSERT(declaration_scope->is_function_scope());
4190 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4191 }
4192 __ push(at);
4193}
4194
4195
ager@chromium.org5c838252010-02-19 08:53:10 +00004196// ----------------------------------------------------------------------------
4197// Non-local control flow support.
4198
4199void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004200 ASSERT(!result_register().is(a1));
4201 // Store result register while executing finally block.
4202 __ push(result_register());
4203 // Cook return address in link register to stack (smi encoded Code* delta).
4204 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4205 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4206 ASSERT_EQ(0, kSmiTag);
4207 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4208 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004209}
4210
4211
4212void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004213 ASSERT(!result_register().is(a1));
4214 // Restore result register from stack.
4215 __ pop(a1);
4216 // Uncook return address and return.
4217 __ pop(result_register());
4218 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4219 __ sra(a1, a1, 1); // Un-smi-tag value.
4220 __ Addu(at, a1, Operand(masm_->CodeObject()));
4221 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004222}
4223
4224
4225#undef __
4226
4227} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004228
4229#endif // V8_TARGET_ARCH_MIPS