blob: 5b9bbb57895aeea51bca13202bf28a37ed83668d [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) {
58 if (property->is_synthetic()) return AstNode::kNoNumber;
59 return property->id();
60}
61
62
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000063// A patch site is a location in the code which it is possible to patch. This
64// class has a number of methods to emit the code which is patchable and the
65// method EmitPatchInfo to record a marker back to the patchable code. This
66// marker is a andi at, rx, #yyy instruction, and x * 0x0000ffff + yyy (raw 16
67// bit immediate value is used) is the delta from the pc to the first
68// instruction of the patchable code.
69class JumpPatchSite BASE_EMBEDDED {
70 public:
71 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
72#ifdef DEBUG
73 info_emitted_ = false;
74#endif
75 }
76
77 ~JumpPatchSite() {
78 ASSERT(patch_site_.is_bound() == info_emitted_);
79 }
80
81 // When initially emitting this ensure that a jump is always generated to skip
82 // the inlined smi code.
83 void EmitJumpIfNotSmi(Register reg, Label* target) {
84 ASSERT(!patch_site_.is_bound() && !info_emitted_);
85 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
86 __ bind(&patch_site_);
87 __ andi(at, reg, 0);
88 // Always taken before patched.
89 __ Branch(target, eq, at, Operand(zero_reg));
90 }
91
92 // When initially emitting this ensure that a jump is never generated to skip
93 // the inlined smi code.
94 void EmitJumpIfSmi(Register reg, Label* target) {
95 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
96 ASSERT(!patch_site_.is_bound() && !info_emitted_);
97 __ bind(&patch_site_);
98 __ andi(at, reg, 0);
99 // Never taken before patched.
100 __ Branch(target, ne, at, Operand(zero_reg));
101 }
102
103 void EmitPatchInfo() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000104 if (patch_site_.is_bound()) {
105 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
106 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
107 __ andi(at, reg, delta_to_patch_site % kImm16Mask);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000108#ifdef DEBUG
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000109 info_emitted_ = true;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000110#endif
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000111 } else {
112 __ nop(); // Signals no inlined code.
113 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000114 }
115
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000116 private:
117 MacroAssembler* masm_;
118 Label patch_site_;
119#ifdef DEBUG
120 bool info_emitted_;
121#endif
122};
123
124
lrn@chromium.org7516f052011-03-30 08:52:27 +0000125// Generate code for a JS function. On entry to the function the receiver
126// and arguments have been pushed on the stack left to right. The actual
127// argument count matches the formal parameter count expected by the
128// function.
129//
130// The live registers are:
131// o a1: the JS function object being called (ie, ourselves)
132// o cp: our context
133// o fp: our caller's frame pointer
134// o sp: stack pointer
135// o ra: return address
136//
137// The function builds a JS frame. Please see JavaScriptFrameConstants in
138// frames-mips.h for its layout.
139void FullCodeGenerator::Generate(CompilationInfo* info) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000140 ASSERT(info_ == NULL);
141 info_ = info;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000142 scope_ = info->scope();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000143 SetFunctionPosition(function());
144 Comment cmnt(masm_, "[ function compiled by full code generator");
145
146#ifdef DEBUG
147 if (strlen(FLAG_stop_at) > 0 &&
148 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
149 __ stop("stop-at");
150 }
151#endif
152
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000153 // Strict mode functions and builtins need to replace the receiver
154 // with undefined when called as functions (without an explicit
155 // receiver object). t1 is zero for method calls and non-zero for
156 // function calls.
157 if (info->is_strict_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000158 Label ok;
159 __ Branch(&ok, eq, t1, Operand(zero_reg));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000160 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000161 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
162 __ sw(a2, MemOperand(sp, receiver_offset));
163 __ bind(&ok);
164 }
165
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000166 int locals_count = info->scope()->num_stack_slots();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000167
168 __ Push(ra, fp, cp, a1);
169 if (locals_count > 0) {
170 // Load undefined value here, so the value is ready for the loop
171 // below.
172 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
173 }
174 // Adjust fp to point to caller's fp.
175 __ Addu(fp, sp, Operand(2 * kPointerSize));
176
177 { Comment cmnt(masm_, "[ Allocate locals");
178 for (int i = 0; i < locals_count; i++) {
179 __ push(at);
180 }
181 }
182
183 bool function_in_register = true;
184
185 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000186 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000187 if (heap_slots > 0) {
188 Comment cmnt(masm_, "[ Allocate local context");
189 // Argument to NewContext is the function, which is in a1.
190 __ push(a1);
191 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
192 FastNewContextStub stub(heap_slots);
193 __ CallStub(&stub);
194 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000195 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000196 }
197 function_in_register = false;
198 // Context is returned in both v0 and cp. It replaces the context
199 // passed to us. It's saved in the stack and kept live in cp.
200 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
201 // Copy any necessary parameters into the context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000202 int num_parameters = info->scope()->num_parameters();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000203 for (int i = 0; i < num_parameters; i++) {
204 Slot* slot = scope()->parameter(i)->AsSlot();
205 if (slot != NULL && slot->type() == Slot::CONTEXT) {
206 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
207 (num_parameters - 1 - i) * kPointerSize;
208 // Load parameter from stack.
209 __ lw(a0, MemOperand(fp, parameter_offset));
210 // Store it in the context.
211 __ li(a1, Operand(Context::SlotOffset(slot->index())));
212 __ addu(a2, cp, a1);
213 __ sw(a0, MemOperand(a2, 0));
214 // Update the write barrier. This clobbers all involved
215 // registers, so we have to use two more registers to avoid
216 // clobbering cp.
217 __ mov(a2, cp);
218 __ RecordWrite(a2, a1, a3);
219 }
220 }
221 }
222
223 Variable* arguments = scope()->arguments();
224 if (arguments != NULL) {
225 // Function uses arguments object.
226 Comment cmnt(masm_, "[ Allocate arguments object");
227 if (!function_in_register) {
228 // Load this again, if it's used by the local context below.
229 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
230 } else {
231 __ mov(a3, a1);
232 }
233 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000234 int num_parameters = info->scope()->num_parameters();
235 int offset = num_parameters * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000236 __ Addu(a2, fp,
237 Operand(StandardFrameConstants::kCallerSPOffset + offset));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000238 __ li(a1, Operand(Smi::FromInt(num_parameters)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000239 __ Push(a3, a2, a1);
240
241 // Arguments to ArgumentsAccessStub:
242 // function, receiver address, parameter count.
243 // The stub will rewrite receiever and parameter count if the previous
244 // stack frame was an arguments adapter frame.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000245 ArgumentsAccessStub::Type type;
246 if (is_strict_mode()) {
247 type = ArgumentsAccessStub::NEW_STRICT;
248 } else if (function()->has_duplicate_parameters()) {
249 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
250 } else {
251 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
252 }
253 ArgumentsAccessStub stub(type);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000254 __ CallStub(&stub);
255
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000256 Move(arguments->AsSlot(), v0, a1, a2);
257 }
258
259 if (FLAG_trace) {
260 __ CallRuntime(Runtime::kTraceEnter, 0);
261 }
262
263 // Visit the declarations and body unless there is an illegal
264 // redeclaration.
265 if (scope()->HasIllegalRedeclaration()) {
266 Comment cmnt(masm_, "[ Declarations");
267 scope()->VisitIllegalRedeclaration(this);
268
269 } else {
270 { Comment cmnt(masm_, "[ Declarations");
271 // For named function expressions, declare the function name as a
272 // constant.
273 if (scope()->is_function_scope() && scope()->function() != NULL) {
274 EmitDeclaration(scope()->function(), Variable::CONST, NULL);
275 }
276 VisitDeclarations(scope()->declarations());
277 }
278
279 { Comment cmnt(masm_, "[ Stack check");
280 PrepareForBailoutForId(AstNode::kFunctionEntryId, NO_REGISTERS);
281 Label ok;
282 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
283 __ Branch(&ok, hs, sp, Operand(t0));
284 StackCheckStub stub;
285 __ CallStub(&stub);
286 __ bind(&ok);
287 }
288
289 { Comment cmnt(masm_, "[ Body");
290 ASSERT(loop_depth() == 0);
291 VisitStatements(function()->body());
292 ASSERT(loop_depth() == 0);
293 }
294 }
295
296 // Always emit a 'return undefined' in case control fell off the end of
297 // the body.
298 { Comment cmnt(masm_, "[ return <undefined>;");
299 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
300 }
301 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000302}
303
304
305void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000306 ASSERT(Smi::FromInt(0) == 0);
307 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000308}
309
310
311void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000312 Comment cmnt(masm_, "[ Stack check");
313 Label ok;
314 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
315 __ Branch(&ok, hs, sp, Operand(t0));
316 StackCheckStub stub;
317 // Record a mapping of this PC offset to the OSR id. This is used to find
318 // the AST id from the unoptimized code in order to use it as a key into
319 // the deoptimization input data found in the optimized code.
320 RecordStackCheck(stmt->OsrEntryId());
321
322 __ CallStub(&stub);
323 __ bind(&ok);
324 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
325 // Record a mapping of the OSR id to this PC. This is used if the OSR
326 // entry becomes the target of a bailout. We don't expect it to be, but
327 // we want it to work if it is.
328 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000329}
330
331
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000332void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000333 Comment cmnt(masm_, "[ Return sequence");
334 if (return_label_.is_bound()) {
335 __ Branch(&return_label_);
336 } else {
337 __ bind(&return_label_);
338 if (FLAG_trace) {
339 // Push the return value on the stack as the parameter.
340 // Runtime::TraceExit returns its parameter in v0.
341 __ push(v0);
342 __ CallRuntime(Runtime::kTraceExit, 1);
343 }
344
345#ifdef DEBUG
346 // Add a label for checking the size of the code used for returning.
347 Label check_exit_codesize;
348 masm_->bind(&check_exit_codesize);
349#endif
350 // Make sure that the constant pool is not emitted inside of the return
351 // sequence.
352 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
353 // Here we use masm_-> instead of the __ macro to avoid the code coverage
354 // tool from instrumenting as we rely on the code size here.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000355 int32_t sp_delta = (info_->scope()->num_parameters() + 1) * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000356 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
357 __ RecordJSReturn();
358 masm_->mov(sp, fp);
359 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
360 masm_->Addu(sp, sp, Operand(sp_delta));
361 masm_->Jump(ra);
362 }
363
364#ifdef DEBUG
365 // Check that the size of the code used for returning is large enough
366 // for the debugger's requirements.
367 ASSERT(Assembler::kJSReturnSequenceInstructions <=
368 masm_->InstructionsGeneratedSince(&check_exit_codesize));
369#endif
370 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000371}
372
373
lrn@chromium.org7516f052011-03-30 08:52:27 +0000374void FullCodeGenerator::EffectContext::Plug(Slot* slot) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000375}
376
377
lrn@chromium.org7516f052011-03-30 08:52:27 +0000378void FullCodeGenerator::AccumulatorValueContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000379 codegen()->Move(result_register(), slot);
ager@chromium.org5c838252010-02-19 08:53:10 +0000380}
381
382
lrn@chromium.org7516f052011-03-30 08:52:27 +0000383void FullCodeGenerator::StackValueContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000384 codegen()->Move(result_register(), slot);
385 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000386}
387
388
lrn@chromium.org7516f052011-03-30 08:52:27 +0000389void FullCodeGenerator::TestContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000390 // For simplicity we always test the accumulator register.
391 codegen()->Move(result_register(), slot);
392 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000393 codegen()->DoTest(this);
ager@chromium.org5c838252010-02-19 08:53:10 +0000394}
395
396
lrn@chromium.org7516f052011-03-30 08:52:27 +0000397void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000398}
399
400
lrn@chromium.org7516f052011-03-30 08:52:27 +0000401void FullCodeGenerator::AccumulatorValueContext::Plug(
402 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000403 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000404}
405
406
407void FullCodeGenerator::StackValueContext::Plug(
408 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000409 __ LoadRoot(result_register(), index);
410 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000411}
412
413
414void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000415 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
416 true,
417 true_label_,
418 false_label_);
419 if (index == Heap::kUndefinedValueRootIndex ||
420 index == Heap::kNullValueRootIndex ||
421 index == Heap::kFalseValueRootIndex) {
422 if (false_label_ != fall_through_) __ Branch(false_label_);
423 } else if (index == Heap::kTrueValueRootIndex) {
424 if (true_label_ != fall_through_) __ Branch(true_label_);
425 } else {
426 __ LoadRoot(result_register(), index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000427 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000428 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000429}
430
431
432void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000433}
434
435
436void FullCodeGenerator::AccumulatorValueContext::Plug(
437 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000438 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000439}
440
441
442void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000443 // Immediates cannot be pushed directly.
444 __ li(result_register(), Operand(lit));
445 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000446}
447
448
449void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000450 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
451 true,
452 true_label_,
453 false_label_);
454 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
455 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
456 if (false_label_ != fall_through_) __ Branch(false_label_);
457 } else if (lit->IsTrue() || lit->IsJSObject()) {
458 if (true_label_ != fall_through_) __ Branch(true_label_);
459 } else if (lit->IsString()) {
460 if (String::cast(*lit)->length() == 0) {
461 if (false_label_ != fall_through_) __ Branch(false_label_);
462 } else {
463 if (true_label_ != fall_through_) __ Branch(true_label_);
464 }
465 } else if (lit->IsSmi()) {
466 if (Smi::cast(*lit)->value() == 0) {
467 if (false_label_ != fall_through_) __ Branch(false_label_);
468 } else {
469 if (true_label_ != fall_through_) __ Branch(true_label_);
470 }
471 } else {
472 // For simplicity we always test the accumulator register.
473 __ li(result_register(), Operand(lit));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000474 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000475 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000476}
477
478
479void FullCodeGenerator::EffectContext::DropAndPlug(int count,
480 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000481 ASSERT(count > 0);
482 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000483}
484
485
486void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
487 int count,
488 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000489 ASSERT(count > 0);
490 __ Drop(count);
491 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000492}
493
494
495void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
496 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000497 ASSERT(count > 0);
498 if (count > 1) __ Drop(count - 1);
499 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000500}
501
502
503void FullCodeGenerator::TestContext::DropAndPlug(int count,
504 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000505 ASSERT(count > 0);
506 // For simplicity we always test the accumulator register.
507 __ Drop(count);
508 __ Move(result_register(), reg);
509 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000510 codegen()->DoTest(this);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000511}
512
513
514void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
515 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000516 ASSERT(materialize_true == materialize_false);
517 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000518}
519
520
521void FullCodeGenerator::AccumulatorValueContext::Plug(
522 Label* materialize_true,
523 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000524 Label done;
525 __ bind(materialize_true);
526 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
527 __ Branch(&done);
528 __ bind(materialize_false);
529 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
530 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000531}
532
533
534void FullCodeGenerator::StackValueContext::Plug(
535 Label* materialize_true,
536 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000537 Label done;
538 __ bind(materialize_true);
539 __ LoadRoot(at, Heap::kTrueValueRootIndex);
540 __ push(at);
541 __ Branch(&done);
542 __ bind(materialize_false);
543 __ LoadRoot(at, Heap::kFalseValueRootIndex);
544 __ push(at);
545 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000546}
547
548
549void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
550 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000551 ASSERT(materialize_true == true_label_);
552 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000553}
554
555
556void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000557}
558
559
560void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000561 Heap::RootListIndex value_root_index =
562 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
563 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000564}
565
566
567void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000568 Heap::RootListIndex value_root_index =
569 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
570 __ LoadRoot(at, value_root_index);
571 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000572}
573
574
575void FullCodeGenerator::TestContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000576 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
577 true,
578 true_label_,
579 false_label_);
580 if (flag) {
581 if (true_label_ != fall_through_) __ Branch(true_label_);
582 } else {
583 if (false_label_ != fall_through_) __ Branch(false_label_);
584 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000585}
586
587
whesse@chromium.org7b260152011-06-20 15:33:18 +0000588void FullCodeGenerator::DoTest(Expression* condition,
589 Label* if_true,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000590 Label* if_false,
591 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000592 if (CpuFeatures::IsSupported(FPU)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000593 ToBooleanStub stub(result_register());
594 __ CallStub(&stub);
595 __ mov(at, zero_reg);
596 } else {
597 // Call the runtime to find the boolean value of the source and then
598 // translate it into control flow to the pair of labels.
599 __ push(result_register());
600 __ CallRuntime(Runtime::kToBool, 1);
601 __ LoadRoot(at, Heap::kFalseValueRootIndex);
602 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000603 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000604}
605
606
lrn@chromium.org7516f052011-03-30 08:52:27 +0000607void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000608 Register lhs,
609 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000610 Label* if_true,
611 Label* if_false,
612 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000613 if (if_false == fall_through) {
614 __ Branch(if_true, cc, lhs, rhs);
615 } else if (if_true == fall_through) {
616 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
617 } else {
618 __ Branch(if_true, cc, lhs, rhs);
619 __ Branch(if_false);
620 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000621}
622
623
624MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000625 switch (slot->type()) {
626 case Slot::PARAMETER:
627 case Slot::LOCAL:
628 return MemOperand(fp, SlotOffset(slot));
629 case Slot::CONTEXT: {
630 int context_chain_length =
631 scope()->ContextChainLength(slot->var()->scope());
632 __ LoadContext(scratch, context_chain_length);
633 return ContextOperand(scratch, slot->index());
634 }
635 case Slot::LOOKUP:
636 UNREACHABLE();
637 }
638 UNREACHABLE();
639 return MemOperand(v0, 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000640}
641
642
643void FullCodeGenerator::Move(Register destination, Slot* source) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000644 // Use destination as scratch.
645 MemOperand slot_operand = EmitSlotSearch(source, destination);
646 __ lw(destination, slot_operand);
ager@chromium.org5c838252010-02-19 08:53:10 +0000647}
648
649
lrn@chromium.org7516f052011-03-30 08:52:27 +0000650void FullCodeGenerator::PrepareForBailoutBeforeSplit(State state,
651 bool should_normalize,
652 Label* if_true,
653 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000654 // Only prepare for bailouts before splits if we're in a test
655 // context. Otherwise, we let the Visit function deal with the
656 // preparation to avoid preparing with the same AST id twice.
657 if (!context()->IsTest() || !info_->IsOptimizable()) return;
658
659 Label skip;
660 if (should_normalize) __ Branch(&skip);
661
662 ForwardBailoutStack* current = forward_bailout_stack_;
663 while (current != NULL) {
664 PrepareForBailout(current->expr(), state);
665 current = current->parent();
666 }
667
668 if (should_normalize) {
669 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
670 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
671 __ bind(&skip);
672 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000673}
674
675
ager@chromium.org5c838252010-02-19 08:53:10 +0000676void FullCodeGenerator::Move(Slot* dst,
677 Register src,
678 Register scratch1,
679 Register scratch2) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000680 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
681 ASSERT(!scratch1.is(src) && !scratch2.is(src));
682 MemOperand location = EmitSlotSearch(dst, scratch1);
683 __ sw(src, location);
684 // Emit the write barrier code if the location is in the heap.
685 if (dst->type() == Slot::CONTEXT) {
686 __ RecordWrite(scratch1,
687 Operand(Context::SlotOffset(dst->index())),
688 scratch2,
689 src);
690 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000691}
692
693
lrn@chromium.org7516f052011-03-30 08:52:27 +0000694void FullCodeGenerator::EmitDeclaration(Variable* variable,
695 Variable::Mode mode,
696 FunctionLiteral* function) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000697 Comment cmnt(masm_, "[ Declaration");
698 ASSERT(variable != NULL); // Must have been resolved.
699 Slot* slot = variable->AsSlot();
700 Property* prop = variable->AsProperty();
701
702 if (slot != NULL) {
703 switch (slot->type()) {
704 case Slot::PARAMETER:
705 case Slot::LOCAL:
706 if (mode == Variable::CONST) {
707 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
708 __ sw(t0, MemOperand(fp, SlotOffset(slot)));
709 } else if (function != NULL) {
710 VisitForAccumulatorValue(function);
711 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
712 }
713 break;
714
715 case Slot::CONTEXT:
716 // We bypass the general EmitSlotSearch because we know more about
717 // this specific context.
718
719 // The variable in the decl always resides in the current function
720 // context.
721 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
722 if (FLAG_debug_code) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000723 // Check that we're not inside a with or catch context.
724 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
725 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
726 __ Check(ne, "Declaration in with context.",
727 a1, Operand(t0));
728 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
729 __ Check(ne, "Declaration in catch context.",
730 a1, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000731 }
732 if (mode == Variable::CONST) {
733 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
734 __ sw(at, ContextOperand(cp, slot->index()));
735 // No write barrier since the_hole_value is in old space.
736 } else if (function != NULL) {
737 VisitForAccumulatorValue(function);
738 __ sw(result_register(), ContextOperand(cp, slot->index()));
739 int offset = Context::SlotOffset(slot->index());
740 // We know that we have written a function, which is not a smi.
741 __ mov(a1, cp);
742 __ RecordWrite(a1, Operand(offset), a2, result_register());
743 }
744 break;
745
746 case Slot::LOOKUP: {
747 __ li(a2, Operand(variable->name()));
748 // Declaration nodes are always introduced in one of two modes.
749 ASSERT(mode == Variable::VAR ||
750 mode == Variable::CONST);
751 PropertyAttributes attr =
752 (mode == Variable::VAR) ? NONE : READ_ONLY;
753 __ li(a1, Operand(Smi::FromInt(attr)));
754 // Push initial value, if any.
755 // Note: For variables we must not push an initial value (such as
756 // 'undefined') because we may have a (legal) redeclaration and we
757 // must not destroy the current value.
758 if (mode == Variable::CONST) {
759 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
760 __ Push(cp, a2, a1, a0);
761 } else if (function != NULL) {
762 __ Push(cp, a2, a1);
763 // Push initial value for function declaration.
764 VisitForStackValue(function);
765 } else {
766 ASSERT(Smi::FromInt(0) == 0);
767 // No initial value!
768 __ mov(a0, zero_reg); // Operand(Smi::FromInt(0)));
769 __ Push(cp, a2, a1, a0);
770 }
771 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
772 break;
773 }
774 }
775
776 } else if (prop != NULL) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000777 // A const declaration aliasing a parameter is an illegal redeclaration.
778 ASSERT(mode != Variable::CONST);
779 if (function != NULL) {
780 // We are declaring a function that rewrites to a property.
781 // Use (keyed) IC to set the initial value. We cannot visit the
782 // rewrite because it's shared and we risk recording duplicate AST
783 // IDs for bailouts from optimized code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000784 ASSERT(prop->obj()->AsVariableProxy() != NULL);
785 { AccumulatorValueContext for_object(this);
786 EmitVariableLoad(prop->obj()->AsVariableProxy()->var());
787 }
danno@chromium.org40cb8782011-05-25 07:58:50 +0000788
789 __ push(result_register());
790 VisitForAccumulatorValue(function);
791 __ mov(a0, result_register());
792 __ pop(a2);
793
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000794 ASSERT(prop->key()->AsLiteral() != NULL &&
795 prop->key()->AsLiteral()->handle()->IsSmi());
796 __ li(a1, Operand(prop->key()->AsLiteral()->handle()));
797
798 Handle<Code> ic = is_strict_mode()
799 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
800 : isolate()->builtins()->KeyedStoreIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000801 __ CallWithAstId(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000802 // Value in v0 is ignored (declarations are statements).
803 }
804 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000805}
806
807
ager@chromium.org5c838252010-02-19 08:53:10 +0000808void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000809 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
ager@chromium.org5c838252010-02-19 08:53:10 +0000810}
811
812
813void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000814 // Call the runtime to declare the globals.
815 // The context is the first argument.
816 __ li(a2, Operand(pairs));
817 __ li(a1, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
818 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
819 __ Push(cp, a2, a1, a0);
820 __ CallRuntime(Runtime::kDeclareGlobals, 4);
821 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000822}
823
824
lrn@chromium.org7516f052011-03-30 08:52:27 +0000825void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000826 Comment cmnt(masm_, "[ SwitchStatement");
827 Breakable nested_statement(this, stmt);
828 SetStatementPosition(stmt);
829
830 // Keep the switch value on the stack until a case matches.
831 VisitForStackValue(stmt->tag());
832 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
833
834 ZoneList<CaseClause*>* clauses = stmt->cases();
835 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
836
837 Label next_test; // Recycled for each test.
838 // Compile all the tests with branches to their bodies.
839 for (int i = 0; i < clauses->length(); i++) {
840 CaseClause* clause = clauses->at(i);
841 clause->body_target()->Unuse();
842
843 // The default is not a test, but remember it as final fall through.
844 if (clause->is_default()) {
845 default_clause = clause;
846 continue;
847 }
848
849 Comment cmnt(masm_, "[ Case comparison");
850 __ bind(&next_test);
851 next_test.Unuse();
852
853 // Compile the label expression.
854 VisitForAccumulatorValue(clause->label());
855 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
856
857 // Perform the comparison as if via '==='.
858 __ lw(a1, MemOperand(sp, 0)); // Switch value.
859 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
860 JumpPatchSite patch_site(masm_);
861 if (inline_smi_code) {
862 Label slow_case;
863 __ or_(a2, a1, a0);
864 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
865
866 __ Branch(&next_test, ne, a1, Operand(a0));
867 __ Drop(1); // Switch value is no longer needed.
868 __ Branch(clause->body_target());
869
870 __ bind(&slow_case);
871 }
872
873 // Record position before stub call for type feedback.
874 SetSourcePosition(clause->position());
875 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000876 __ CallWithAstId(ic, RelocInfo::CODE_TARGET, clause->CompareId());
877 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +0000878
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000879 __ Branch(&next_test, ne, v0, Operand(zero_reg));
880 __ Drop(1); // Switch value is no longer needed.
881 __ Branch(clause->body_target());
882 }
883
884 // Discard the test value and jump to the default if present, otherwise to
885 // the end of the statement.
886 __ bind(&next_test);
887 __ Drop(1); // Switch value is no longer needed.
888 if (default_clause == NULL) {
889 __ Branch(nested_statement.break_target());
890 } else {
891 __ Branch(default_clause->body_target());
892 }
893
894 // Compile all the case bodies.
895 for (int i = 0; i < clauses->length(); i++) {
896 Comment cmnt(masm_, "[ Case body");
897 CaseClause* clause = clauses->at(i);
898 __ bind(clause->body_target());
899 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
900 VisitStatements(clause->statements());
901 }
902
903 __ bind(nested_statement.break_target());
904 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000905}
906
907
908void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000909 Comment cmnt(masm_, "[ ForInStatement");
910 SetStatementPosition(stmt);
911
912 Label loop, exit;
913 ForIn loop_statement(this, stmt);
914 increment_loop_depth();
915
916 // Get the object to enumerate over. Both SpiderMonkey and JSC
917 // ignore null and undefined in contrast to the specification; see
918 // ECMA-262 section 12.6.4.
919 VisitForAccumulatorValue(stmt->enumerable());
920 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
921 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
922 __ Branch(&exit, eq, a0, Operand(at));
923 Register null_value = t1;
924 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
925 __ Branch(&exit, eq, a0, Operand(null_value));
926
927 // Convert the object to a JS object.
928 Label convert, done_convert;
929 __ JumpIfSmi(a0, &convert);
930 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000931 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000932 __ bind(&convert);
933 __ push(a0);
934 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
935 __ mov(a0, v0);
936 __ bind(&done_convert);
937 __ push(a0);
938
939 // Check cache validity in generated code. This is a fast case for
940 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
941 // guarantee cache validity, call the runtime system to check cache
942 // validity or get the property names in a fixed array.
943 Label next, call_runtime;
944 // Preload a couple of values used in the loop.
945 Register empty_fixed_array_value = t2;
946 __ LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
947 Register empty_descriptor_array_value = t3;
948 __ LoadRoot(empty_descriptor_array_value,
949 Heap::kEmptyDescriptorArrayRootIndex);
950 __ mov(a1, a0);
951 __ bind(&next);
952
953 // Check that there are no elements. Register a1 contains the
954 // current JS object we've reached through the prototype chain.
955 __ lw(a2, FieldMemOperand(a1, JSObject::kElementsOffset));
956 __ Branch(&call_runtime, ne, a2, Operand(empty_fixed_array_value));
957
958 // Check that instance descriptors are not empty so that we can
959 // check for an enum cache. Leave the map in a2 for the subsequent
960 // prototype load.
961 __ lw(a2, FieldMemOperand(a1, HeapObject::kMapOffset));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000962 __ lw(a3, FieldMemOperand(a2, Map::kInstanceDescriptorsOrBitField3Offset));
963 __ JumpIfSmi(a3, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000964
965 // Check that there is an enum cache in the non-empty instance
966 // descriptors (a3). This is the case if the next enumeration
967 // index field does not contain a smi.
968 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumerationIndexOffset));
969 __ JumpIfSmi(a3, &call_runtime);
970
971 // For all objects but the receiver, check that the cache is empty.
972 Label check_prototype;
973 __ Branch(&check_prototype, eq, a1, Operand(a0));
974 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumCacheBridgeCacheOffset));
975 __ Branch(&call_runtime, ne, a3, Operand(empty_fixed_array_value));
976
977 // Load the prototype from the map and loop if non-null.
978 __ bind(&check_prototype);
979 __ lw(a1, FieldMemOperand(a2, Map::kPrototypeOffset));
980 __ Branch(&next, ne, a1, Operand(null_value));
981
982 // The enum cache is valid. Load the map of the object being
983 // iterated over and use the cache for the iteration.
984 Label use_cache;
985 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
986 __ Branch(&use_cache);
987
988 // Get the set of properties to enumerate.
989 __ bind(&call_runtime);
990 __ push(a0); // Duplicate the enumerable object on the stack.
991 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
992
993 // If we got a map from the runtime call, we can do a fast
994 // modification check. Otherwise, we got a fixed array, and we have
995 // to do a slow check.
996 Label fixed_array;
997 __ mov(a2, v0);
998 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
999 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1000 __ Branch(&fixed_array, ne, a1, Operand(at));
1001
1002 // We got a map in register v0. Get the enumeration cache from it.
1003 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001004 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001005 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
1006 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1007
1008 // Setup the four remaining stack slots.
1009 __ push(v0); // Map.
1010 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
1011 __ li(a0, Operand(Smi::FromInt(0)));
1012 // Push enumeration cache, enumeration cache length (as smi) and zero.
1013 __ Push(a2, a1, a0);
1014 __ jmp(&loop);
1015
1016 // We got a fixed array in register v0. Iterate through that.
1017 __ bind(&fixed_array);
1018 __ li(a1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
1019 __ Push(a1, v0);
1020 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1021 __ li(a0, Operand(Smi::FromInt(0)));
1022 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1023
1024 // Generate code for doing the condition check.
1025 __ bind(&loop);
1026 // Load the current count to a0, load the length to a1.
1027 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1028 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
1029 __ Branch(loop_statement.break_target(), hs, a0, Operand(a1));
1030
1031 // Get the current entry of the array into register a3.
1032 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1033 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1034 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1035 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1036 __ lw(a3, MemOperand(t0)); // Current entry.
1037
1038 // Get the expected map from the stack or a zero map in the
1039 // permanent slow case into register a2.
1040 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1041
1042 // Check if the expected map still matches that of the enumerable.
1043 // If not, we have to filter the key.
1044 Label update_each;
1045 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1046 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1047 __ Branch(&update_each, eq, t0, Operand(a2));
1048
1049 // Convert the entry to a string or (smi) 0 if it isn't a property
1050 // any more. If the property has been removed while iterating, we
1051 // just skip it.
1052 __ push(a1); // Enumerable.
1053 __ push(a3); // Current entry.
1054 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1055 __ mov(a3, result_register());
1056 __ Branch(loop_statement.continue_target(), eq, a3, Operand(zero_reg));
1057
1058 // Update the 'each' property or variable from the possibly filtered
1059 // entry in register a3.
1060 __ bind(&update_each);
1061 __ mov(result_register(), a3);
1062 // Perform the assignment as if via '='.
1063 { EffectContext context(this);
1064 EmitAssignment(stmt->each(), stmt->AssignmentId());
1065 }
1066
1067 // Generate code for the body of the loop.
1068 Visit(stmt->body());
1069
1070 // Generate code for the going to the next element by incrementing
1071 // the index (smi) stored on top of the stack.
1072 __ bind(loop_statement.continue_target());
1073 __ pop(a0);
1074 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1075 __ push(a0);
1076
1077 EmitStackCheck(stmt);
1078 __ Branch(&loop);
1079
1080 // Remove the pointers stored on the stack.
1081 __ bind(loop_statement.break_target());
1082 __ Drop(5);
1083
1084 // Exit and decrement the loop depth.
1085 __ bind(&exit);
1086 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001087}
1088
1089
1090void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1091 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001092 // Use the fast case closure allocation code that allocates in new
1093 // space for nested functions that don't need literals cloning. If
1094 // we're running with the --always-opt or the --prepare-always-opt
1095 // flag, we need to use the runtime function so that the new function
1096 // we are creating here gets a chance to have its code optimized and
1097 // doesn't just get a copy of the existing unoptimized code.
1098 if (!FLAG_always_opt &&
1099 !FLAG_prepare_always_opt &&
1100 !pretenure &&
1101 scope()->is_function_scope() &&
1102 info->num_literals() == 0) {
1103 FastNewClosureStub stub(info->strict_mode() ? kStrictMode : kNonStrictMode);
1104 __ li(a0, Operand(info));
1105 __ push(a0);
1106 __ CallStub(&stub);
1107 } else {
1108 __ li(a0, Operand(info));
1109 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1110 : Heap::kFalseValueRootIndex);
1111 __ Push(cp, a0, a1);
1112 __ CallRuntime(Runtime::kNewClosure, 3);
1113 }
1114 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001115}
1116
1117
1118void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001119 Comment cmnt(masm_, "[ VariableProxy");
1120 EmitVariableLoad(expr->var());
1121}
1122
1123
1124void FullCodeGenerator::EmitLoadGlobalSlotCheckExtensions(
1125 Slot* slot,
1126 TypeofState typeof_state,
1127 Label* slow) {
1128 Register current = cp;
1129 Register next = a1;
1130 Register temp = a2;
1131
1132 Scope* s = scope();
1133 while (s != NULL) {
1134 if (s->num_heap_slots() > 0) {
1135 if (s->calls_eval()) {
1136 // Check that extension is NULL.
1137 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1138 __ Branch(slow, ne, temp, Operand(zero_reg));
1139 }
1140 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001141 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001142 // Walk the rest of the chain without clobbering cp.
1143 current = next;
1144 }
1145 // If no outer scope calls eval, we do not need to check more
1146 // context extensions.
1147 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
1148 s = s->outer_scope();
1149 }
1150
1151 if (s->is_eval_scope()) {
1152 Label loop, fast;
1153 if (!current.is(next)) {
1154 __ Move(next, current);
1155 }
1156 __ bind(&loop);
1157 // Terminate at global context.
1158 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1159 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1160 __ Branch(&fast, eq, temp, Operand(t0));
1161 // Check that extension is NULL.
1162 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1163 __ Branch(slow, ne, temp, Operand(zero_reg));
1164 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001165 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001166 __ Branch(&loop);
1167 __ bind(&fast);
1168 }
1169
1170 __ lw(a0, GlobalObjectOperand());
1171 __ li(a2, Operand(slot->var()->name()));
1172 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1173 ? RelocInfo::CODE_TARGET
1174 : RelocInfo::CODE_TARGET_CONTEXT;
1175 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001176 __ CallWithAstId(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001177}
1178
1179
lrn@chromium.org7516f052011-03-30 08:52:27 +00001180MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(
1181 Slot* slot,
1182 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001183 ASSERT(slot->type() == Slot::CONTEXT);
1184 Register context = cp;
1185 Register next = a3;
1186 Register temp = t0;
1187
1188 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
1189 if (s->num_heap_slots() > 0) {
1190 if (s->calls_eval()) {
1191 // Check that extension is NULL.
1192 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1193 __ Branch(slow, ne, temp, Operand(zero_reg));
1194 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001195 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001196 // Walk the rest of the chain without clobbering cp.
1197 context = next;
1198 }
1199 }
1200 // Check that last extension is NULL.
1201 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1202 __ Branch(slow, ne, temp, Operand(zero_reg));
1203
1204 // This function is used only for loads, not stores, so it's safe to
1205 // return an cp-based operand (the write barrier cannot be allowed to
1206 // destroy the cp register).
1207 return ContextOperand(context, slot->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001208}
1209
1210
1211void FullCodeGenerator::EmitDynamicLoadFromSlotFastCase(
1212 Slot* slot,
1213 TypeofState typeof_state,
1214 Label* slow,
1215 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001216 // Generate fast-case code for variables that might be shadowed by
1217 // eval-introduced variables. Eval is used a lot without
1218 // introducing variables. In those cases, we do not want to
1219 // perform a runtime call for all variables in the scope
1220 // containing the eval.
1221 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
1222 EmitLoadGlobalSlotCheckExtensions(slot, typeof_state, slow);
1223 __ Branch(done);
1224 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
1225 Slot* potential_slot = slot->var()->local_if_not_shadowed()->AsSlot();
1226 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
1227 if (potential_slot != NULL) {
1228 // Generate fast case for locals that rewrite to slots.
1229 __ lw(v0, ContextSlotOperandCheckExtensions(potential_slot, slow));
1230 if (potential_slot->var()->mode() == Variable::CONST) {
1231 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1232 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1233 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1234 __ movz(v0, a0, at); // Conditional move.
1235 }
1236 __ Branch(done);
1237 } else if (rewrite != NULL) {
1238 // Generate fast case for calls of an argument function.
1239 Property* property = rewrite->AsProperty();
1240 if (property != NULL) {
1241 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
1242 Literal* key_literal = property->key()->AsLiteral();
1243 if (obj_proxy != NULL &&
1244 key_literal != NULL &&
1245 obj_proxy->IsArguments() &&
1246 key_literal->handle()->IsSmi()) {
1247 // Load arguments object if there are no eval-introduced
1248 // variables. Then load the argument from the arguments
1249 // object using keyed load.
1250 __ lw(a1,
1251 ContextSlotOperandCheckExtensions(obj_proxy->var()->AsSlot(),
1252 slow));
1253 __ li(a0, Operand(key_literal->handle()));
1254 Handle<Code> ic =
1255 isolate()->builtins()->KeyedLoadIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001256 __ CallWithAstId(ic, RelocInfo::CODE_TARGET, GetPropertyId(property));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001257 __ Branch(done);
1258 }
1259 }
1260 }
1261 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001262}
1263
1264
1265void FullCodeGenerator::EmitVariableLoad(Variable* var) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001266 // Three cases: non-this global variables, lookup slots, and all other
1267 // types of slots.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001268 Slot* slot = var->AsSlot();
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001269 ASSERT((var->is_global() && !var->is_this()) == (slot == NULL));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001270
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001271 if (slot == NULL) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001272 Comment cmnt(masm_, "Global variable");
1273 // Use inline caching. Variable name is passed in a2 and the global
1274 // object (receiver) in a0.
1275 __ lw(a0, GlobalObjectOperand());
1276 __ li(a2, Operand(var->name()));
1277 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001278 __ CallWithAstId(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001279 context()->Plug(v0);
1280
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001281 } else if (slot->type() == Slot::LOOKUP) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001282 Label done, slow;
1283
1284 // Generate code for loading from variables potentially shadowed
1285 // by eval-introduced variables.
1286 EmitDynamicLoadFromSlotFastCase(slot, NOT_INSIDE_TYPEOF, &slow, &done);
1287
1288 __ bind(&slow);
1289 Comment cmnt(masm_, "Lookup slot");
1290 __ li(a1, Operand(var->name()));
1291 __ Push(cp, a1); // Context and name.
1292 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1293 __ bind(&done);
1294
1295 context()->Plug(v0);
1296
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001297 } else {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001298 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1299 ? "Context slot"
1300 : "Stack slot");
1301 if (var->mode() == Variable::CONST) {
1302 // Constants may be the hole value if they have not been initialized.
1303 // Unhole them.
1304 MemOperand slot_operand = EmitSlotSearch(slot, a0);
1305 __ lw(v0, slot_operand);
1306 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1307 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1308 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1309 __ movz(v0, a0, at); // Conditional move.
1310 context()->Plug(v0);
1311 } else {
1312 context()->Plug(slot);
1313 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001314 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001315}
1316
1317
1318void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001319 Comment cmnt(masm_, "[ RegExpLiteral");
1320 Label materialized;
1321 // Registers will be used as follows:
1322 // t1 = materialized value (RegExp literal)
1323 // t0 = JS function, literals array
1324 // a3 = literal index
1325 // a2 = RegExp pattern
1326 // a1 = RegExp flags
1327 // a0 = RegExp literal clone
1328 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1329 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1330 int literal_offset =
1331 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1332 __ lw(t1, FieldMemOperand(t0, literal_offset));
1333 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1334 __ Branch(&materialized, ne, t1, Operand(at));
1335
1336 // Create regexp literal using runtime function.
1337 // Result will be in v0.
1338 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1339 __ li(a2, Operand(expr->pattern()));
1340 __ li(a1, Operand(expr->flags()));
1341 __ Push(t0, a3, a2, a1);
1342 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1343 __ mov(t1, v0);
1344
1345 __ bind(&materialized);
1346 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1347 Label allocated, runtime_allocate;
1348 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1349 __ jmp(&allocated);
1350
1351 __ bind(&runtime_allocate);
1352 __ push(t1);
1353 __ li(a0, Operand(Smi::FromInt(size)));
1354 __ push(a0);
1355 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1356 __ pop(t1);
1357
1358 __ bind(&allocated);
1359
1360 // After this, registers are used as follows:
1361 // v0: Newly allocated regexp.
1362 // t1: Materialized regexp.
1363 // a2: temp.
1364 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1365 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001366}
1367
1368
1369void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001370 Comment cmnt(masm_, "[ ObjectLiteral");
1371 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1372 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1373 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1374 __ li(a1, Operand(expr->constant_properties()));
1375 int flags = expr->fast_elements()
1376 ? ObjectLiteral::kFastElements
1377 : ObjectLiteral::kNoFlags;
1378 flags |= expr->has_function()
1379 ? ObjectLiteral::kHasFunction
1380 : ObjectLiteral::kNoFlags;
1381 __ li(a0, Operand(Smi::FromInt(flags)));
1382 __ Push(a3, a2, a1, a0);
1383 if (expr->depth() > 1) {
1384 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1385 } else {
1386 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
1387 }
1388
1389 // If result_saved is true the result is on top of the stack. If
1390 // result_saved is false the result is in v0.
1391 bool result_saved = false;
1392
1393 // Mark all computed expressions that are bound to a key that
1394 // is shadowed by a later occurrence of the same key. For the
1395 // marked expressions, no store code is emitted.
1396 expr->CalculateEmitStore();
1397
1398 for (int i = 0; i < expr->properties()->length(); i++) {
1399 ObjectLiteral::Property* property = expr->properties()->at(i);
1400 if (property->IsCompileTimeValue()) continue;
1401
1402 Literal* key = property->key();
1403 Expression* value = property->value();
1404 if (!result_saved) {
1405 __ push(v0); // Save result on stack.
1406 result_saved = true;
1407 }
1408 switch (property->kind()) {
1409 case ObjectLiteral::Property::CONSTANT:
1410 UNREACHABLE();
1411 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1412 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1413 // Fall through.
1414 case ObjectLiteral::Property::COMPUTED:
1415 if (key->handle()->IsSymbol()) {
1416 if (property->emit_store()) {
1417 VisitForAccumulatorValue(value);
1418 __ mov(a0, result_register());
1419 __ li(a2, Operand(key->handle()));
1420 __ lw(a1, MemOperand(sp));
1421 Handle<Code> ic = is_strict_mode()
1422 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1423 : isolate()->builtins()->StoreIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001424 __ CallWithAstId(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001425 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1426 } else {
1427 VisitForEffect(value);
1428 }
1429 break;
1430 }
1431 // Fall through.
1432 case ObjectLiteral::Property::PROTOTYPE:
1433 // Duplicate receiver on stack.
1434 __ lw(a0, MemOperand(sp));
1435 __ push(a0);
1436 VisitForStackValue(key);
1437 VisitForStackValue(value);
1438 if (property->emit_store()) {
1439 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1440 __ push(a0);
1441 __ CallRuntime(Runtime::kSetProperty, 4);
1442 } else {
1443 __ Drop(3);
1444 }
1445 break;
1446 case ObjectLiteral::Property::GETTER:
1447 case ObjectLiteral::Property::SETTER:
1448 // Duplicate receiver on stack.
1449 __ lw(a0, MemOperand(sp));
1450 __ push(a0);
1451 VisitForStackValue(key);
1452 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1453 Smi::FromInt(1) :
1454 Smi::FromInt(0)));
1455 __ push(a1);
1456 VisitForStackValue(value);
1457 __ CallRuntime(Runtime::kDefineAccessor, 4);
1458 break;
1459 }
1460 }
1461
1462 if (expr->has_function()) {
1463 ASSERT(result_saved);
1464 __ lw(a0, MemOperand(sp));
1465 __ push(a0);
1466 __ CallRuntime(Runtime::kToFastProperties, 1);
1467 }
1468
1469 if (result_saved) {
1470 context()->PlugTOS();
1471 } else {
1472 context()->Plug(v0);
1473 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001474}
1475
1476
1477void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001478 Comment cmnt(masm_, "[ ArrayLiteral");
1479
1480 ZoneList<Expression*>* subexprs = expr->values();
1481 int length = subexprs->length();
1482 __ mov(a0, result_register());
1483 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1484 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1485 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1486 __ li(a1, Operand(expr->constant_elements()));
1487 __ Push(a3, a2, a1);
1488 if (expr->constant_elements()->map() ==
1489 isolate()->heap()->fixed_cow_array_map()) {
1490 FastCloneShallowArrayStub stub(
1491 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1492 __ CallStub(&stub);
1493 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1494 1, a1, a2);
1495 } else if (expr->depth() > 1) {
1496 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1497 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1498 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1499 } else {
1500 FastCloneShallowArrayStub stub(
1501 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
1502 __ CallStub(&stub);
1503 }
1504
1505 bool result_saved = false; // Is the result saved to the stack?
1506
1507 // Emit code to evaluate all the non-constant subexpressions and to store
1508 // them into the newly cloned array.
1509 for (int i = 0; i < length; i++) {
1510 Expression* subexpr = subexprs->at(i);
1511 // If the subexpression is a literal or a simple materialized literal it
1512 // is already set in the cloned array.
1513 if (subexpr->AsLiteral() != NULL ||
1514 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1515 continue;
1516 }
1517
1518 if (!result_saved) {
1519 __ push(v0);
1520 result_saved = true;
1521 }
1522 VisitForAccumulatorValue(subexpr);
1523
1524 // Store the subexpression value in the array's elements.
1525 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1526 __ lw(a1, FieldMemOperand(a1, JSObject::kElementsOffset));
1527 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1528 __ sw(result_register(), FieldMemOperand(a1, offset));
1529
1530 // Update the write barrier for the array store with v0 as the scratch
1531 // register.
1532 __ li(a2, Operand(offset));
1533 // TODO(PJ): double check this RecordWrite call.
1534 __ RecordWrite(a1, a2, result_register());
1535
1536 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1537 }
1538
1539 if (result_saved) {
1540 context()->PlugTOS();
1541 } else {
1542 context()->Plug(v0);
1543 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001544}
1545
1546
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001547void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001548 Comment cmnt(masm_, "[ Assignment");
1549 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1550 // on the left-hand side.
1551 if (!expr->target()->IsValidLeftHandSide()) {
1552 VisitForEffect(expr->target());
1553 return;
1554 }
1555
1556 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001557 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001558 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1559 LhsKind assign_type = VARIABLE;
1560 Property* property = expr->target()->AsProperty();
1561 if (property != NULL) {
1562 assign_type = (property->key()->IsPropertyName())
1563 ? NAMED_PROPERTY
1564 : KEYED_PROPERTY;
1565 }
1566
1567 // Evaluate LHS expression.
1568 switch (assign_type) {
1569 case VARIABLE:
1570 // Nothing to do here.
1571 break;
1572 case NAMED_PROPERTY:
1573 if (expr->is_compound()) {
1574 // We need the receiver both on the stack and in the accumulator.
1575 VisitForAccumulatorValue(property->obj());
1576 __ push(result_register());
1577 } else {
1578 VisitForStackValue(property->obj());
1579 }
1580 break;
1581 case KEYED_PROPERTY:
1582 // We need the key and receiver on both the stack and in v0 and a1.
1583 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001584 VisitForStackValue(property->obj());
1585 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001586 __ lw(a1, MemOperand(sp, 0));
1587 __ push(v0);
1588 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001589 VisitForStackValue(property->obj());
1590 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001591 }
1592 break;
1593 }
1594
1595 // For compound assignments we need another deoptimization point after the
1596 // variable/property load.
1597 if (expr->is_compound()) {
1598 { AccumulatorValueContext context(this);
1599 switch (assign_type) {
1600 case VARIABLE:
1601 EmitVariableLoad(expr->target()->AsVariableProxy()->var());
1602 PrepareForBailout(expr->target(), TOS_REG);
1603 break;
1604 case NAMED_PROPERTY:
1605 EmitNamedPropertyLoad(property);
1606 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1607 break;
1608 case KEYED_PROPERTY:
1609 EmitKeyedPropertyLoad(property);
1610 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1611 break;
1612 }
1613 }
1614
1615 Token::Value op = expr->binary_op();
1616 __ push(v0); // Left operand goes on the stack.
1617 VisitForAccumulatorValue(expr->value());
1618
1619 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1620 ? OVERWRITE_RIGHT
1621 : NO_OVERWRITE;
1622 SetSourcePosition(expr->position() + 1);
1623 AccumulatorValueContext context(this);
1624 if (ShouldInlineSmiCase(op)) {
1625 EmitInlineSmiBinaryOp(expr->binary_operation(),
1626 op,
1627 mode,
1628 expr->target(),
1629 expr->value());
1630 } else {
1631 EmitBinaryOp(expr->binary_operation(), op, mode);
1632 }
1633
1634 // Deoptimization point in case the binary operation may have side effects.
1635 PrepareForBailout(expr->binary_operation(), TOS_REG);
1636 } else {
1637 VisitForAccumulatorValue(expr->value());
1638 }
1639
1640 // Record source position before possible IC call.
1641 SetSourcePosition(expr->position());
1642
1643 // Store the value.
1644 switch (assign_type) {
1645 case VARIABLE:
1646 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1647 expr->op());
1648 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1649 context()->Plug(v0);
1650 break;
1651 case NAMED_PROPERTY:
1652 EmitNamedPropertyAssignment(expr);
1653 break;
1654 case KEYED_PROPERTY:
1655 EmitKeyedPropertyAssignment(expr);
1656 break;
1657 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001658}
1659
1660
ager@chromium.org5c838252010-02-19 08:53:10 +00001661void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001662 SetSourcePosition(prop->position());
1663 Literal* key = prop->key()->AsLiteral();
1664 __ mov(a0, result_register());
1665 __ li(a2, Operand(key->handle()));
1666 // Call load IC. It has arguments receiver and property name a0 and a2.
1667 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001668 __ CallWithAstId(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001669}
1670
1671
1672void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001673 SetSourcePosition(prop->position());
1674 __ mov(a0, result_register());
1675 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1676 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001677 __ CallWithAstId(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001678}
1679
1680
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001681void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001682 Token::Value op,
1683 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001684 Expression* left_expr,
1685 Expression* right_expr) {
1686 Label done, smi_case, stub_call;
1687
1688 Register scratch1 = a2;
1689 Register scratch2 = a3;
1690
1691 // Get the arguments.
1692 Register left = a1;
1693 Register right = a0;
1694 __ pop(left);
1695 __ mov(a0, result_register());
1696
1697 // Perform combined smi check on both operands.
1698 __ Or(scratch1, left, Operand(right));
1699 STATIC_ASSERT(kSmiTag == 0);
1700 JumpPatchSite patch_site(masm_);
1701 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1702
1703 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001704 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001705 __ CallWithAstId(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
1706 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001707 __ jmp(&done);
1708
1709 __ bind(&smi_case);
1710 // Smi case. This code works the same way as the smi-smi case in the type
1711 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001712 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001713 switch (op) {
1714 case Token::SAR:
1715 __ Branch(&stub_call);
1716 __ GetLeastBitsFromSmi(scratch1, right, 5);
1717 __ srav(right, left, scratch1);
1718 __ And(v0, right, Operand(~kSmiTagMask));
1719 break;
1720 case Token::SHL: {
1721 __ Branch(&stub_call);
1722 __ SmiUntag(scratch1, left);
1723 __ GetLeastBitsFromSmi(scratch2, right, 5);
1724 __ sllv(scratch1, scratch1, scratch2);
1725 __ Addu(scratch2, scratch1, Operand(0x40000000));
1726 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1727 __ SmiTag(v0, scratch1);
1728 break;
1729 }
1730 case Token::SHR: {
1731 __ Branch(&stub_call);
1732 __ SmiUntag(scratch1, left);
1733 __ GetLeastBitsFromSmi(scratch2, right, 5);
1734 __ srlv(scratch1, scratch1, scratch2);
1735 __ And(scratch2, scratch1, 0xc0000000);
1736 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1737 __ SmiTag(v0, scratch1);
1738 break;
1739 }
1740 case Token::ADD:
1741 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1742 __ BranchOnOverflow(&stub_call, scratch1);
1743 break;
1744 case Token::SUB:
1745 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1746 __ BranchOnOverflow(&stub_call, scratch1);
1747 break;
1748 case Token::MUL: {
1749 __ SmiUntag(scratch1, right);
1750 __ Mult(left, scratch1);
1751 __ mflo(scratch1);
1752 __ mfhi(scratch2);
1753 __ sra(scratch1, scratch1, 31);
1754 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1755 __ mflo(v0);
1756 __ Branch(&done, ne, v0, Operand(zero_reg));
1757 __ Addu(scratch2, right, left);
1758 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1759 ASSERT(Smi::FromInt(0) == 0);
1760 __ mov(v0, zero_reg);
1761 break;
1762 }
1763 case Token::BIT_OR:
1764 __ Or(v0, left, Operand(right));
1765 break;
1766 case Token::BIT_AND:
1767 __ And(v0, left, Operand(right));
1768 break;
1769 case Token::BIT_XOR:
1770 __ Xor(v0, left, Operand(right));
1771 break;
1772 default:
1773 UNREACHABLE();
1774 }
1775
1776 __ bind(&done);
1777 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001778}
1779
1780
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001781void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1782 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001783 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001784 __ mov(a0, result_register());
1785 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001786 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001787 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
1788 __ CallWithAstId(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
1789 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001790 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001791}
1792
1793
1794void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001795 // Invalid left-hand sides are rewritten to have a 'throw
1796 // ReferenceError' on the left-hand side.
1797 if (!expr->IsValidLeftHandSide()) {
1798 VisitForEffect(expr);
1799 return;
1800 }
1801
1802 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001803 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001804 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1805 LhsKind assign_type = VARIABLE;
1806 Property* prop = expr->AsProperty();
1807 if (prop != NULL) {
1808 assign_type = (prop->key()->IsPropertyName())
1809 ? NAMED_PROPERTY
1810 : KEYED_PROPERTY;
1811 }
1812
1813 switch (assign_type) {
1814 case VARIABLE: {
1815 Variable* var = expr->AsVariableProxy()->var();
1816 EffectContext context(this);
1817 EmitVariableAssignment(var, Token::ASSIGN);
1818 break;
1819 }
1820 case NAMED_PROPERTY: {
1821 __ push(result_register()); // Preserve value.
1822 VisitForAccumulatorValue(prop->obj());
1823 __ mov(a1, result_register());
1824 __ pop(a0); // Restore value.
1825 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1826 Handle<Code> ic = is_strict_mode()
1827 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1828 : isolate()->builtins()->StoreIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001829 __ CallWithAstId(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001830 break;
1831 }
1832 case KEYED_PROPERTY: {
1833 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001834 VisitForStackValue(prop->obj());
1835 VisitForAccumulatorValue(prop->key());
1836 __ mov(a1, result_register());
1837 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001838 __ pop(a0); // Restore value.
1839 Handle<Code> ic = is_strict_mode()
1840 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
1841 : isolate()->builtins()->KeyedStoreIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001842 __ CallWithAstId(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001843 break;
1844 }
1845 }
1846 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1847 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001848}
1849
1850
1851void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001852 Token::Value op) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001853 ASSERT(var != NULL);
1854 ASSERT(var->is_global() || var->AsSlot() != NULL);
1855
1856 if (var->is_global()) {
1857 ASSERT(!var->is_this());
1858 // Assignment to a global variable. Use inline caching for the
1859 // assignment. Right-hand-side value is passed in a0, variable name in
1860 // a2, and the global object in a1.
1861 __ mov(a0, result_register());
1862 __ li(a2, Operand(var->name()));
1863 __ lw(a1, GlobalObjectOperand());
1864 Handle<Code> ic = is_strict_mode()
1865 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1866 : isolate()->builtins()->StoreIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001867 __ CallWithAstId(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001868
1869 } else if (op == Token::INIT_CONST) {
1870 // Like var declarations, const declarations are hoisted to function
1871 // scope. However, unlike var initializers, const initializers are able
1872 // to drill a hole to that function context, even from inside a 'with'
1873 // context. We thus bypass the normal static scope lookup.
1874 Slot* slot = var->AsSlot();
1875 Label skip;
1876 switch (slot->type()) {
1877 case Slot::PARAMETER:
1878 // No const parameters.
1879 UNREACHABLE();
1880 break;
1881 case Slot::LOCAL:
1882 // Detect const reinitialization by checking for the hole value.
1883 __ lw(a1, MemOperand(fp, SlotOffset(slot)));
1884 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1885 __ Branch(&skip, ne, a1, Operand(t0));
1886 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1887 break;
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001888 case Slot::CONTEXT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001889 case Slot::LOOKUP:
1890 __ push(result_register());
1891 __ li(a0, Operand(slot->var()->name()));
1892 __ Push(cp, a0); // Context and name.
1893 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1894 break;
1895 }
1896 __ bind(&skip);
1897
1898 } else if (var->mode() != Variable::CONST) {
1899 // Perform the assignment for non-const variables. Const assignments
1900 // are simply skipped.
1901 Slot* slot = var->AsSlot();
1902 switch (slot->type()) {
1903 case Slot::PARAMETER:
1904 case Slot::LOCAL:
1905 // Perform the assignment.
1906 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1907 break;
1908
1909 case Slot::CONTEXT: {
1910 MemOperand target = EmitSlotSearch(slot, a1);
1911 // Perform the assignment and issue the write barrier.
1912 __ sw(result_register(), target);
1913 // RecordWrite may destroy all its register arguments.
1914 __ mov(a3, result_register());
1915 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
1916 __ RecordWrite(a1, Operand(offset), a2, a3);
1917 break;
1918 }
1919
1920 case Slot::LOOKUP:
1921 // Call the runtime for the assignment.
1922 __ push(v0); // Value.
1923 __ li(a1, Operand(slot->var()->name()));
1924 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1925 __ Push(cp, a1, a0); // Context, name, strict mode.
1926 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1927 break;
1928 }
1929 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001930}
1931
1932
1933void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001934 // Assignment to a property, using a named store IC.
1935 Property* prop = expr->target()->AsProperty();
1936 ASSERT(prop != NULL);
1937 ASSERT(prop->key()->AsLiteral() != NULL);
1938
1939 // If the assignment starts a block of assignments to the same object,
1940 // change to slow case to avoid the quadratic behavior of repeatedly
1941 // adding fast properties.
1942 if (expr->starts_initialization_block()) {
1943 __ push(result_register());
1944 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1945 __ push(t0);
1946 __ CallRuntime(Runtime::kToSlowProperties, 1);
1947 __ pop(result_register());
1948 }
1949
1950 // Record source code position before IC call.
1951 SetSourcePosition(expr->position());
1952 __ mov(a0, result_register()); // Load the value.
1953 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1954 // Load receiver to a1. Leave a copy in the stack if needed for turning the
1955 // receiver into fast case.
1956 if (expr->ends_initialization_block()) {
1957 __ lw(a1, MemOperand(sp));
1958 } else {
1959 __ pop(a1);
1960 }
1961
1962 Handle<Code> ic = is_strict_mode()
1963 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1964 : isolate()->builtins()->StoreIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001965 __ CallWithAstId(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001966
1967 // If the assignment ends an initialization block, revert to fast case.
1968 if (expr->ends_initialization_block()) {
1969 __ push(v0); // Result of assignment, saved even if not needed.
1970 // Receiver is under the result value.
1971 __ lw(t0, MemOperand(sp, kPointerSize));
1972 __ push(t0);
1973 __ CallRuntime(Runtime::kToFastProperties, 1);
1974 __ pop(v0);
1975 __ Drop(1);
1976 }
1977 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1978 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001979}
1980
1981
1982void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001983 // Assignment to a property, using a keyed store IC.
1984
1985 // If the assignment starts a block of assignments to the same object,
1986 // change to slow case to avoid the quadratic behavior of repeatedly
1987 // adding fast properties.
1988 if (expr->starts_initialization_block()) {
1989 __ push(result_register());
1990 // Receiver is now under the key and value.
1991 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
1992 __ push(t0);
1993 __ CallRuntime(Runtime::kToSlowProperties, 1);
1994 __ pop(result_register());
1995 }
1996
1997 // Record source code position before IC call.
1998 SetSourcePosition(expr->position());
1999 // Call keyed store IC.
2000 // The arguments are:
2001 // - a0 is the value,
2002 // - a1 is the key,
2003 // - a2 is the receiver.
2004 __ mov(a0, result_register());
2005 __ pop(a1); // Key.
2006 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2007 // receiver into fast case.
2008 if (expr->ends_initialization_block()) {
2009 __ lw(a2, MemOperand(sp));
2010 } else {
2011 __ pop(a2);
2012 }
2013
2014 Handle<Code> ic = is_strict_mode()
2015 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
2016 : isolate()->builtins()->KeyedStoreIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002017 __ CallWithAstId(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002018
2019 // If the assignment ends an initialization block, revert to fast case.
2020 if (expr->ends_initialization_block()) {
2021 __ push(v0); // Result of assignment, saved even if not needed.
2022 // Receiver is under the result value.
2023 __ lw(t0, MemOperand(sp, kPointerSize));
2024 __ push(t0);
2025 __ CallRuntime(Runtime::kToFastProperties, 1);
2026 __ pop(v0);
2027 __ Drop(1);
2028 }
2029 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2030 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002031}
2032
2033
2034void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002035 Comment cmnt(masm_, "[ Property");
2036 Expression* key = expr->key();
2037
2038 if (key->IsPropertyName()) {
2039 VisitForAccumulatorValue(expr->obj());
2040 EmitNamedPropertyLoad(expr);
2041 context()->Plug(v0);
2042 } else {
2043 VisitForStackValue(expr->obj());
2044 VisitForAccumulatorValue(expr->key());
2045 __ pop(a1);
2046 EmitKeyedPropertyLoad(expr);
2047 context()->Plug(v0);
2048 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002049}
2050
lrn@chromium.org7516f052011-03-30 08:52:27 +00002051
ager@chromium.org5c838252010-02-19 08:53:10 +00002052void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002053 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002054 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002055 // Code common for calls using the IC.
2056 ZoneList<Expression*>* args = expr->arguments();
2057 int arg_count = args->length();
2058 { PreservePositionScope scope(masm()->positions_recorder());
2059 for (int i = 0; i < arg_count; i++) {
2060 VisitForStackValue(args->at(i));
2061 }
2062 __ li(a2, Operand(name));
2063 }
2064 // Record source position for debugger.
2065 SetSourcePosition(expr->position());
2066 // Call the IC initialization code.
2067 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2068 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00002069 isolate()->stub_cache()->ComputeCallInitialize(arg_count, in_loop, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002070 __ CallWithAstId(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002071 RecordJSReturnSite(expr);
2072 // Restore context register.
2073 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2074 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002075}
2076
2077
lrn@chromium.org7516f052011-03-30 08:52:27 +00002078void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002079 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002080 // Load the key.
2081 VisitForAccumulatorValue(key);
2082
2083 // Swap the name of the function and the receiver on the stack to follow
2084 // the calling convention for call ICs.
2085 __ pop(a1);
2086 __ push(v0);
2087 __ push(a1);
2088
2089 // Code common for calls using the IC.
2090 ZoneList<Expression*>* args = expr->arguments();
2091 int arg_count = args->length();
2092 { PreservePositionScope scope(masm()->positions_recorder());
2093 for (int i = 0; i < arg_count; i++) {
2094 VisitForStackValue(args->at(i));
2095 }
2096 }
2097 // Record source position for debugger.
2098 SetSourcePosition(expr->position());
2099 // Call the IC initialization code.
2100 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2101 Handle<Code> ic =
2102 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count, in_loop);
2103 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002104 __ CallWithAstId(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002105 RecordJSReturnSite(expr);
2106 // Restore context register.
2107 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2108 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002109}
2110
2111
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002112void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002113 // Code common for calls using the call stub.
2114 ZoneList<Expression*>* args = expr->arguments();
2115 int arg_count = args->length();
2116 { PreservePositionScope scope(masm()->positions_recorder());
2117 for (int i = 0; i < arg_count; i++) {
2118 VisitForStackValue(args->at(i));
2119 }
2120 }
2121 // Record source position for debugger.
2122 SetSourcePosition(expr->position());
2123 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2124 CallFunctionStub stub(arg_count, in_loop, flags);
2125 __ CallStub(&stub);
2126 RecordJSReturnSite(expr);
2127 // Restore context register.
2128 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2129 context()->DropAndPlug(1, v0);
2130}
2131
2132
2133void FullCodeGenerator::EmitResolvePossiblyDirectEval(ResolveEvalFlag flag,
2134 int arg_count) {
2135 // Push copy of the first argument or undefined if it doesn't exist.
2136 if (arg_count > 0) {
2137 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2138 } else {
2139 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2140 }
2141 __ push(a1);
2142
2143 // Push the receiver of the enclosing function and do runtime call.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002144 int receiver_offset = 2 + info_->scope()->num_parameters();
2145 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002146 __ push(a1);
2147 // Push the strict mode flag.
2148 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
2149 __ push(a1);
2150
2151 __ CallRuntime(flag == SKIP_CONTEXT_LOOKUP
2152 ? Runtime::kResolvePossiblyDirectEvalNoLookup
2153 : Runtime::kResolvePossiblyDirectEval, 4);
ager@chromium.org5c838252010-02-19 08:53:10 +00002154}
2155
2156
2157void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002158#ifdef DEBUG
2159 // We want to verify that RecordJSReturnSite gets called on all paths
2160 // through this function. Avoid early returns.
2161 expr->return_is_recorded_ = false;
2162#endif
2163
2164 Comment cmnt(masm_, "[ Call");
2165 Expression* fun = expr->expression();
2166 Variable* var = fun->AsVariableProxy()->AsVariable();
2167
2168 if (var != NULL && var->is_possibly_eval()) {
2169 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2170 // resolve the function we need to call and the receiver of the
2171 // call. Then we call the resolved function using the given
2172 // arguments.
2173 ZoneList<Expression*>* args = expr->arguments();
2174 int arg_count = args->length();
2175
2176 { PreservePositionScope pos_scope(masm()->positions_recorder());
2177 VisitForStackValue(fun);
2178 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2179 __ push(a2); // Reserved receiver slot.
2180
2181 // Push the arguments.
2182 for (int i = 0; i < arg_count; i++) {
2183 VisitForStackValue(args->at(i));
2184 }
2185 // If we know that eval can only be shadowed by eval-introduced
2186 // variables we attempt to load the global eval function directly
2187 // in generated code. If we succeed, there is no need to perform a
2188 // context lookup in the runtime system.
2189 Label done;
2190 if (var->AsSlot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
2191 Label slow;
2192 EmitLoadGlobalSlotCheckExtensions(var->AsSlot(),
2193 NOT_INSIDE_TYPEOF,
2194 &slow);
2195 // Push the function and resolve eval.
2196 __ push(v0);
2197 EmitResolvePossiblyDirectEval(SKIP_CONTEXT_LOOKUP, arg_count);
2198 __ jmp(&done);
2199 __ bind(&slow);
2200 }
2201
2202 // Push copy of the function (found below the arguments) and
2203 // resolve eval.
2204 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2205 __ push(a1);
2206 EmitResolvePossiblyDirectEval(PERFORM_CONTEXT_LOOKUP, arg_count);
2207 if (done.is_linked()) {
2208 __ bind(&done);
2209 }
2210
2211 // The runtime call returns a pair of values in v0 (function) and
2212 // v1 (receiver). Touch up the stack with the right values.
2213 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2214 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2215 }
2216 // Record source position for debugger.
2217 SetSourcePosition(expr->position());
2218 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002219 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002220 __ CallStub(&stub);
2221 RecordJSReturnSite(expr);
2222 // Restore context register.
2223 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2224 context()->DropAndPlug(1, v0);
2225 } else if (var != NULL && !var->is_this() && var->is_global()) {
2226 // Push global object as receiver for the call IC.
2227 __ lw(a0, GlobalObjectOperand());
2228 __ push(a0);
2229 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
2230 } else if (var != NULL && var->AsSlot() != NULL &&
2231 var->AsSlot()->type() == Slot::LOOKUP) {
2232 // Call to a lookup slot (dynamically introduced variable).
2233 Label slow, done;
2234
2235 { PreservePositionScope scope(masm()->positions_recorder());
2236 // Generate code for loading from variables potentially shadowed
2237 // by eval-introduced variables.
2238 EmitDynamicLoadFromSlotFastCase(var->AsSlot(),
2239 NOT_INSIDE_TYPEOF,
2240 &slow,
2241 &done);
2242 }
2243
2244 __ bind(&slow);
2245 // Call the runtime to find the function to call (returned in v0)
2246 // and the object holding it (returned in v1).
2247 __ push(context_register());
2248 __ li(a2, Operand(var->name()));
2249 __ push(a2);
2250 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2251 __ Push(v0, v1); // Function, receiver.
2252
2253 // If fast case code has been generated, emit code to push the
2254 // function and receiver and have the slow path jump around this
2255 // code.
2256 if (done.is_linked()) {
2257 Label call;
2258 __ Branch(&call);
2259 __ bind(&done);
2260 // Push function.
2261 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002262 // The receiver is implicitly the global receiver. Indicate this
2263 // by passing the hole to the call function stub.
2264 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002265 __ push(a1);
2266 __ bind(&call);
2267 }
2268
danno@chromium.org40cb8782011-05-25 07:58:50 +00002269 // The receiver is either the global receiver or an object found
2270 // by LoadContextSlot. That object could be the hole if the
2271 // receiver is implicitly the global object.
2272 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002273 } else if (fun->AsProperty() != NULL) {
2274 // Call to an object property.
2275 Property* prop = fun->AsProperty();
2276 Literal* key = prop->key()->AsLiteral();
2277 if (key != NULL && key->handle()->IsSymbol()) {
2278 // Call to a named property, use call IC.
2279 { PreservePositionScope scope(masm()->positions_recorder());
2280 VisitForStackValue(prop->obj());
2281 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00002282 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002283 } else {
2284 // Call to a keyed property.
2285 // For a synthetic property use keyed load IC followed by function call,
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002286 // for a regular property use EmitKeyedCallWithIC.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002287 if (prop->is_synthetic()) {
2288 // Do not visit the object and key subexpressions (they are shared
2289 // by all occurrences of the same rewritten parameter).
2290 ASSERT(prop->obj()->AsVariableProxy() != NULL);
2291 ASSERT(prop->obj()->AsVariableProxy()->var()->AsSlot() != NULL);
2292 Slot* slot = prop->obj()->AsVariableProxy()->var()->AsSlot();
2293 MemOperand operand = EmitSlotSearch(slot, a1);
2294 __ lw(a1, operand);
2295
2296 ASSERT(prop->key()->AsLiteral() != NULL);
2297 ASSERT(prop->key()->AsLiteral()->handle()->IsSmi());
2298 __ li(a0, Operand(prop->key()->AsLiteral()->handle()));
2299
2300 // Record source code position for IC call.
2301 SetSourcePosition(prop->position());
2302
2303 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002304 __ CallWithAstId(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002305 __ lw(a1, GlobalObjectOperand());
2306 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2307 __ Push(v0, a1); // Function, receiver.
2308 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2309 } else {
2310 { PreservePositionScope scope(masm()->positions_recorder());
2311 VisitForStackValue(prop->obj());
2312 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00002313 EmitKeyedCallWithIC(expr, prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002314 }
2315 }
2316 } else {
2317 { PreservePositionScope scope(masm()->positions_recorder());
2318 VisitForStackValue(fun);
2319 }
2320 // Load global receiver object.
2321 __ lw(a1, GlobalObjectOperand());
2322 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2323 __ push(a1);
2324 // Emit function call.
2325 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2326 }
2327
2328#ifdef DEBUG
2329 // RecordJSReturnSite should have been called.
2330 ASSERT(expr->return_is_recorded_);
2331#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002332}
2333
2334
2335void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002336 Comment cmnt(masm_, "[ CallNew");
2337 // According to ECMA-262, section 11.2.2, page 44, the function
2338 // expression in new calls must be evaluated before the
2339 // arguments.
2340
2341 // Push constructor on the stack. If it's not a function it's used as
2342 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2343 // ignored.
2344 VisitForStackValue(expr->expression());
2345
2346 // Push the arguments ("left-to-right") on the stack.
2347 ZoneList<Expression*>* args = expr->arguments();
2348 int arg_count = args->length();
2349 for (int i = 0; i < arg_count; i++) {
2350 VisitForStackValue(args->at(i));
2351 }
2352
2353 // Call the construct call builtin that handles allocation and
2354 // constructor invocation.
2355 SetSourcePosition(expr->position());
2356
2357 // Load function and argument count into a1 and a0.
2358 __ li(a0, Operand(arg_count));
2359 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2360
2361 Handle<Code> construct_builtin =
2362 isolate()->builtins()->JSConstructCall();
2363 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2364 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002365}
2366
2367
lrn@chromium.org7516f052011-03-30 08:52:27 +00002368void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002369 ASSERT(args->length() == 1);
2370
2371 VisitForAccumulatorValue(args->at(0));
2372
2373 Label materialize_true, materialize_false;
2374 Label* if_true = NULL;
2375 Label* if_false = NULL;
2376 Label* fall_through = NULL;
2377 context()->PrepareTest(&materialize_true, &materialize_false,
2378 &if_true, &if_false, &fall_through);
2379
2380 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2381 __ And(t0, v0, Operand(kSmiTagMask));
2382 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2383
2384 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002385}
2386
2387
2388void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002389 ASSERT(args->length() == 1);
2390
2391 VisitForAccumulatorValue(args->at(0));
2392
2393 Label materialize_true, materialize_false;
2394 Label* if_true = NULL;
2395 Label* if_false = NULL;
2396 Label* fall_through = NULL;
2397 context()->PrepareTest(&materialize_true, &materialize_false,
2398 &if_true, &if_false, &fall_through);
2399
2400 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2401 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2402 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2403
2404 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002405}
2406
2407
2408void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002409 ASSERT(args->length() == 1);
2410
2411 VisitForAccumulatorValue(args->at(0));
2412
2413 Label materialize_true, materialize_false;
2414 Label* if_true = NULL;
2415 Label* if_false = NULL;
2416 Label* fall_through = NULL;
2417 context()->PrepareTest(&materialize_true, &materialize_false,
2418 &if_true, &if_false, &fall_through);
2419
2420 __ JumpIfSmi(v0, if_false);
2421 __ LoadRoot(at, Heap::kNullValueRootIndex);
2422 __ Branch(if_true, eq, v0, Operand(at));
2423 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2424 // Undetectable objects behave like undefined when tested with typeof.
2425 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2426 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2427 __ Branch(if_false, ne, at, Operand(zero_reg));
2428 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002429 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002430 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002431 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2432 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002433
2434 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002435}
2436
2437
2438void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002439 ASSERT(args->length() == 1);
2440
2441 VisitForAccumulatorValue(args->at(0));
2442
2443 Label materialize_true, materialize_false;
2444 Label* if_true = NULL;
2445 Label* if_false = NULL;
2446 Label* fall_through = NULL;
2447 context()->PrepareTest(&materialize_true, &materialize_false,
2448 &if_true, &if_false, &fall_through);
2449
2450 __ JumpIfSmi(v0, if_false);
2451 __ GetObjectType(v0, a1, a1);
2452 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002453 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002454 if_true, if_false, fall_through);
2455
2456 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002457}
2458
2459
2460void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002461 ASSERT(args->length() == 1);
2462
2463 VisitForAccumulatorValue(args->at(0));
2464
2465 Label materialize_true, materialize_false;
2466 Label* if_true = NULL;
2467 Label* if_false = NULL;
2468 Label* fall_through = NULL;
2469 context()->PrepareTest(&materialize_true, &materialize_false,
2470 &if_true, &if_false, &fall_through);
2471
2472 __ JumpIfSmi(v0, if_false);
2473 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2474 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2475 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2476 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2477 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2478
2479 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002480}
2481
2482
2483void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2484 ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002485
2486 ASSERT(args->length() == 1);
2487
2488 VisitForAccumulatorValue(args->at(0));
2489
2490 Label materialize_true, materialize_false;
2491 Label* if_true = NULL;
2492 Label* if_false = NULL;
2493 Label* fall_through = NULL;
2494 context()->PrepareTest(&materialize_true, &materialize_false,
2495 &if_true, &if_false, &fall_through);
2496
2497 if (FLAG_debug_code) __ AbortIfSmi(v0);
2498
2499 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2500 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2501 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2502 __ Branch(if_true, ne, t0, Operand(zero_reg));
2503
2504 // Check for fast case object. Generate false result for slow case object.
2505 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2506 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2507 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2508 __ Branch(if_false, eq, a2, Operand(t0));
2509
2510 // Look for valueOf symbol in the descriptor array, and indicate false if
2511 // found. The type is not checked, so if it is a transition it is a false
2512 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002513 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002514 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2515 // t0: descriptor array
2516 // a3: length of descriptor array
2517 // Calculate the end of the descriptor array.
2518 STATIC_ASSERT(kSmiTag == 0);
2519 STATIC_ASSERT(kSmiTagSize == 1);
2520 STATIC_ASSERT(kPointerSize == 4);
2521 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2522 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2523 __ Addu(a2, a2, t1);
2524
2525 // Calculate location of the first key name.
2526 __ Addu(t0,
2527 t0,
2528 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2529 DescriptorArray::kFirstIndex * kPointerSize));
2530 // Loop through all the keys in the descriptor array. If one of these is the
2531 // symbol valueOf the result is false.
2532 Label entry, loop;
2533 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2534 // used in the loop below.
2535 __ li(t2, Operand(FACTORY->value_of_symbol()));
2536 __ jmp(&entry);
2537 __ bind(&loop);
2538 __ lw(a3, MemOperand(t0, 0));
2539 __ Branch(if_false, eq, a3, Operand(t2));
2540 __ Addu(t0, t0, Operand(kPointerSize));
2541 __ bind(&entry);
2542 __ Branch(&loop, ne, t0, Operand(a2));
2543
2544 // If a valueOf property is not found on the object check that it's
2545 // prototype is the un-modified String prototype. If not result is false.
2546 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2547 __ JumpIfSmi(a2, if_false);
2548 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2549 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2550 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2551 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2552 __ Branch(if_false, ne, a2, Operand(a3));
2553
2554 // Set the bit in the map to indicate that it has been checked safe for
2555 // default valueOf and set true result.
2556 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2557 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2558 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2559 __ jmp(if_true);
2560
2561 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2562 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002563}
2564
2565
2566void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002567 ASSERT(args->length() == 1);
2568
2569 VisitForAccumulatorValue(args->at(0));
2570
2571 Label materialize_true, materialize_false;
2572 Label* if_true = NULL;
2573 Label* if_false = NULL;
2574 Label* fall_through = NULL;
2575 context()->PrepareTest(&materialize_true, &materialize_false,
2576 &if_true, &if_false, &fall_through);
2577
2578 __ JumpIfSmi(v0, if_false);
2579 __ GetObjectType(v0, a1, a2);
2580 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2581 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2582 __ Branch(if_false);
2583
2584 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002585}
2586
2587
2588void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002589 ASSERT(args->length() == 1);
2590
2591 VisitForAccumulatorValue(args->at(0));
2592
2593 Label materialize_true, materialize_false;
2594 Label* if_true = NULL;
2595 Label* if_false = NULL;
2596 Label* fall_through = NULL;
2597 context()->PrepareTest(&materialize_true, &materialize_false,
2598 &if_true, &if_false, &fall_through);
2599
2600 __ JumpIfSmi(v0, if_false);
2601 __ GetObjectType(v0, a1, a1);
2602 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2603 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2604 if_true, if_false, fall_through);
2605
2606 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002607}
2608
2609
2610void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002611 ASSERT(args->length() == 1);
2612
2613 VisitForAccumulatorValue(args->at(0));
2614
2615 Label materialize_true, materialize_false;
2616 Label* if_true = NULL;
2617 Label* if_false = NULL;
2618 Label* fall_through = NULL;
2619 context()->PrepareTest(&materialize_true, &materialize_false,
2620 &if_true, &if_false, &fall_through);
2621
2622 __ JumpIfSmi(v0, if_false);
2623 __ GetObjectType(v0, a1, a1);
2624 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2625 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2626
2627 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002628}
2629
2630
2631void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002632 ASSERT(args->length() == 0);
2633
2634 Label materialize_true, materialize_false;
2635 Label* if_true = NULL;
2636 Label* if_false = NULL;
2637 Label* fall_through = NULL;
2638 context()->PrepareTest(&materialize_true, &materialize_false,
2639 &if_true, &if_false, &fall_through);
2640
2641 // Get the frame pointer for the calling frame.
2642 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2643
2644 // Skip the arguments adaptor frame if it exists.
2645 Label check_frame_marker;
2646 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2647 __ Branch(&check_frame_marker, ne,
2648 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2649 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2650
2651 // Check the marker in the calling frame.
2652 __ bind(&check_frame_marker);
2653 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
2654 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2655 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2656 if_true, if_false, fall_through);
2657
2658 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002659}
2660
2661
2662void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002663 ASSERT(args->length() == 2);
2664
2665 // Load the two objects into registers and perform the comparison.
2666 VisitForStackValue(args->at(0));
2667 VisitForAccumulatorValue(args->at(1));
2668
2669 Label materialize_true, materialize_false;
2670 Label* if_true = NULL;
2671 Label* if_false = NULL;
2672 Label* fall_through = NULL;
2673 context()->PrepareTest(&materialize_true, &materialize_false,
2674 &if_true, &if_false, &fall_through);
2675
2676 __ pop(a1);
2677 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2678 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2679
2680 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002681}
2682
2683
2684void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002685 ASSERT(args->length() == 1);
2686
2687 // ArgumentsAccessStub expects the key in a1 and the formal
2688 // parameter count in a0.
2689 VisitForAccumulatorValue(args->at(0));
2690 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002691 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002692 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2693 __ CallStub(&stub);
2694 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002695}
2696
2697
2698void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002699 ASSERT(args->length() == 0);
2700
2701 Label exit;
2702 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002703 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002704
2705 // Check if the calling frame is an arguments adaptor frame.
2706 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2707 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2708 __ Branch(&exit, ne, a3,
2709 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2710
2711 // Arguments adaptor case: Read the arguments length from the
2712 // adaptor frame.
2713 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2714
2715 __ bind(&exit);
2716 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002717}
2718
2719
2720void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002721 ASSERT(args->length() == 1);
2722 Label done, null, function, non_function_constructor;
2723
2724 VisitForAccumulatorValue(args->at(0));
2725
2726 // If the object is a smi, we return null.
2727 __ JumpIfSmi(v0, &null);
2728
2729 // Check that the object is a JS object but take special care of JS
2730 // functions to make sure they have 'Function' as their class.
2731 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002732 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002733
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002734 // As long as LAST_CALLABLE_SPEC_OBJECT_TYPE is the last instance type, and
2735 // FIRST_CALLABLE_SPEC_OBJECT_TYPE comes right after
2736 // LAST_NONCALLABLE_SPEC_OBJECT_TYPE, we can avoid checking for the latter.
2737 STATIC_ASSERT(LAST_TYPE == LAST_CALLABLE_SPEC_OBJECT_TYPE);
2738 STATIC_ASSERT(FIRST_CALLABLE_SPEC_OBJECT_TYPE ==
2739 LAST_NONCALLABLE_SPEC_OBJECT_TYPE + 1);
2740 __ Branch(&function, ge, a1, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002741
2742 // Check if the constructor in the map is a function.
2743 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2744 __ GetObjectType(v0, a1, a1);
2745 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2746
2747 // v0 now contains the constructor function. Grab the
2748 // instance class name from there.
2749 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2750 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2751 __ Branch(&done);
2752
2753 // Functions have class 'Function'.
2754 __ bind(&function);
2755 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2756 __ jmp(&done);
2757
2758 // Objects with a non-function constructor have class 'Object'.
2759 __ bind(&non_function_constructor);
2760 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2761 __ jmp(&done);
2762
2763 // Non-JS objects have class null.
2764 __ bind(&null);
2765 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2766
2767 // All done.
2768 __ bind(&done);
2769
2770 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002771}
2772
2773
2774void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002775 // Conditionally generate a log call.
2776 // Args:
2777 // 0 (literal string): The type of logging (corresponds to the flags).
2778 // This is used to determine whether or not to generate the log call.
2779 // 1 (string): Format string. Access the string at argument index 2
2780 // with '%2s' (see Logger::LogRuntime for all the formats).
2781 // 2 (array): Arguments to the format string.
2782 ASSERT_EQ(args->length(), 3);
2783#ifdef ENABLE_LOGGING_AND_PROFILING
2784 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2785 VisitForStackValue(args->at(1));
2786 VisitForStackValue(args->at(2));
2787 __ CallRuntime(Runtime::kLog, 2);
2788 }
2789#endif
2790 // Finally, we're expected to leave a value on the top of the stack.
2791 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2792 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002793}
2794
2795
2796void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002797 ASSERT(args->length() == 0);
2798
2799 Label slow_allocate_heapnumber;
2800 Label heapnumber_allocated;
2801
2802 // Save the new heap number in callee-saved register s0, since
2803 // we call out to external C code below.
2804 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2805 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2806 __ jmp(&heapnumber_allocated);
2807
2808 __ bind(&slow_allocate_heapnumber);
2809
2810 // Allocate a heap number.
2811 __ CallRuntime(Runtime::kNumberAlloc, 0);
2812 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2813
2814 __ bind(&heapnumber_allocated);
2815
2816 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2817 // by computing:
2818 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2819 if (CpuFeatures::IsSupported(FPU)) {
2820 __ PrepareCallCFunction(1, a0);
2821 __ li(a0, Operand(ExternalReference::isolate_address()));
2822 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2823
2824
2825 CpuFeatures::Scope scope(FPU);
2826 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2827 __ li(a1, Operand(0x41300000));
2828 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002829 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002830 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002831 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002832 // Subtract and store the result in the heap number.
2833 __ sub_d(f0, f12, f14);
2834 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2835 __ mov(v0, s0);
2836 } else {
2837 __ PrepareCallCFunction(2, a0);
2838 __ mov(a0, s0);
2839 __ li(a1, Operand(ExternalReference::isolate_address()));
2840 __ CallCFunction(
2841 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2842 }
2843
2844 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002845}
2846
2847
2848void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002849 // Load the arguments on the stack and call the stub.
2850 SubStringStub stub;
2851 ASSERT(args->length() == 3);
2852 VisitForStackValue(args->at(0));
2853 VisitForStackValue(args->at(1));
2854 VisitForStackValue(args->at(2));
2855 __ CallStub(&stub);
2856 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002857}
2858
2859
2860void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002861 // Load the arguments on the stack and call the stub.
2862 RegExpExecStub stub;
2863 ASSERT(args->length() == 4);
2864 VisitForStackValue(args->at(0));
2865 VisitForStackValue(args->at(1));
2866 VisitForStackValue(args->at(2));
2867 VisitForStackValue(args->at(3));
2868 __ CallStub(&stub);
2869 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002870}
2871
2872
2873void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002874 ASSERT(args->length() == 1);
2875
2876 VisitForAccumulatorValue(args->at(0)); // Load the object.
2877
2878 Label done;
2879 // If the object is a smi return the object.
2880 __ JumpIfSmi(v0, &done);
2881 // If the object is not a value type, return the object.
2882 __ GetObjectType(v0, a1, a1);
2883 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2884
2885 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2886
2887 __ bind(&done);
2888 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002889}
2890
2891
2892void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002893 // Load the arguments on the stack and call the runtime function.
2894 ASSERT(args->length() == 2);
2895 VisitForStackValue(args->at(0));
2896 VisitForStackValue(args->at(1));
2897 MathPowStub stub;
2898 __ CallStub(&stub);
2899 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002900}
2901
2902
2903void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002904 ASSERT(args->length() == 2);
2905
2906 VisitForStackValue(args->at(0)); // Load the object.
2907 VisitForAccumulatorValue(args->at(1)); // Load the value.
2908 __ pop(a1); // v0 = value. a1 = object.
2909
2910 Label done;
2911 // If the object is a smi, return the value.
2912 __ JumpIfSmi(a1, &done);
2913
2914 // If the object is not a value type, return the value.
2915 __ GetObjectType(a1, a2, a2);
2916 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2917
2918 // Store the value.
2919 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2920 // Update the write barrier. Save the value as it will be
2921 // overwritten by the write barrier code and is needed afterward.
2922 __ RecordWrite(a1, Operand(JSValue::kValueOffset - kHeapObjectTag), a2, a3);
2923
2924 __ bind(&done);
2925 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002926}
2927
2928
2929void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002930 ASSERT_EQ(args->length(), 1);
2931
2932 // Load the argument on the stack and call the stub.
2933 VisitForStackValue(args->at(0));
2934
2935 NumberToStringStub stub;
2936 __ CallStub(&stub);
2937 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002938}
2939
2940
2941void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002942 ASSERT(args->length() == 1);
2943
2944 VisitForAccumulatorValue(args->at(0));
2945
2946 Label done;
2947 StringCharFromCodeGenerator generator(v0, a1);
2948 generator.GenerateFast(masm_);
2949 __ jmp(&done);
2950
2951 NopRuntimeCallHelper call_helper;
2952 generator.GenerateSlow(masm_, call_helper);
2953
2954 __ bind(&done);
2955 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002956}
2957
2958
2959void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002960 ASSERT(args->length() == 2);
2961
2962 VisitForStackValue(args->at(0));
2963 VisitForAccumulatorValue(args->at(1));
2964 __ mov(a0, result_register());
2965
2966 Register object = a1;
2967 Register index = a0;
2968 Register scratch = a2;
2969 Register result = v0;
2970
2971 __ pop(object);
2972
2973 Label need_conversion;
2974 Label index_out_of_range;
2975 Label done;
2976 StringCharCodeAtGenerator generator(object,
2977 index,
2978 scratch,
2979 result,
2980 &need_conversion,
2981 &need_conversion,
2982 &index_out_of_range,
2983 STRING_INDEX_IS_NUMBER);
2984 generator.GenerateFast(masm_);
2985 __ jmp(&done);
2986
2987 __ bind(&index_out_of_range);
2988 // When the index is out of range, the spec requires us to return
2989 // NaN.
2990 __ LoadRoot(result, Heap::kNanValueRootIndex);
2991 __ jmp(&done);
2992
2993 __ bind(&need_conversion);
2994 // Load the undefined value into the result register, which will
2995 // trigger conversion.
2996 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2997 __ jmp(&done);
2998
2999 NopRuntimeCallHelper call_helper;
3000 generator.GenerateSlow(masm_, call_helper);
3001
3002 __ bind(&done);
3003 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003004}
3005
3006
3007void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003008 ASSERT(args->length() == 2);
3009
3010 VisitForStackValue(args->at(0));
3011 VisitForAccumulatorValue(args->at(1));
3012 __ mov(a0, result_register());
3013
3014 Register object = a1;
3015 Register index = a0;
3016 Register scratch1 = a2;
3017 Register scratch2 = a3;
3018 Register result = v0;
3019
3020 __ pop(object);
3021
3022 Label need_conversion;
3023 Label index_out_of_range;
3024 Label done;
3025 StringCharAtGenerator generator(object,
3026 index,
3027 scratch1,
3028 scratch2,
3029 result,
3030 &need_conversion,
3031 &need_conversion,
3032 &index_out_of_range,
3033 STRING_INDEX_IS_NUMBER);
3034 generator.GenerateFast(masm_);
3035 __ jmp(&done);
3036
3037 __ bind(&index_out_of_range);
3038 // When the index is out of range, the spec requires us to return
3039 // the empty string.
3040 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3041 __ jmp(&done);
3042
3043 __ bind(&need_conversion);
3044 // Move smi zero into the result register, which will trigger
3045 // conversion.
3046 __ li(result, Operand(Smi::FromInt(0)));
3047 __ jmp(&done);
3048
3049 NopRuntimeCallHelper call_helper;
3050 generator.GenerateSlow(masm_, call_helper);
3051
3052 __ bind(&done);
3053 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003054}
3055
3056
3057void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003058 ASSERT_EQ(2, args->length());
3059
3060 VisitForStackValue(args->at(0));
3061 VisitForStackValue(args->at(1));
3062
3063 StringAddStub stub(NO_STRING_ADD_FLAGS);
3064 __ CallStub(&stub);
3065 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003066}
3067
3068
3069void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003070 ASSERT_EQ(2, args->length());
3071
3072 VisitForStackValue(args->at(0));
3073 VisitForStackValue(args->at(1));
3074
3075 StringCompareStub stub;
3076 __ CallStub(&stub);
3077 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003078}
3079
3080
3081void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003082 // Load the argument on the stack and call the stub.
3083 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3084 TranscendentalCacheStub::TAGGED);
3085 ASSERT(args->length() == 1);
3086 VisitForStackValue(args->at(0));
3087 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3088 __ CallStub(&stub);
3089 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003090}
3091
3092
3093void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003094 // Load the argument on the stack and call the stub.
3095 TranscendentalCacheStub stub(TranscendentalCache::COS,
3096 TranscendentalCacheStub::TAGGED);
3097 ASSERT(args->length() == 1);
3098 VisitForStackValue(args->at(0));
3099 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3100 __ CallStub(&stub);
3101 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003102}
3103
3104
3105void FullCodeGenerator::EmitMathLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003106 // Load the argument on the stack and call the stub.
3107 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3108 TranscendentalCacheStub::TAGGED);
3109 ASSERT(args->length() == 1);
3110 VisitForStackValue(args->at(0));
3111 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3112 __ CallStub(&stub);
3113 context()->Plug(v0);
3114}
3115
3116
3117void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
3118 // Load the argument on the stack and call the runtime function.
3119 ASSERT(args->length() == 1);
3120 VisitForStackValue(args->at(0));
3121 __ CallRuntime(Runtime::kMath_sqrt, 1);
3122 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003123}
3124
3125
3126void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003127 ASSERT(args->length() >= 2);
3128
3129 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3130 for (int i = 0; i < arg_count + 1; i++) {
3131 VisitForStackValue(args->at(i));
3132 }
3133 VisitForAccumulatorValue(args->last()); // Function.
3134
3135 // InvokeFunction requires the function in a1. Move it in there.
3136 __ mov(a1, result_register());
3137 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003138 __ InvokeFunction(a1, count, CALL_FUNCTION,
3139 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003140 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3141 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003142}
3143
3144
3145void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003146 RegExpConstructResultStub stub;
3147 ASSERT(args->length() == 3);
3148 VisitForStackValue(args->at(0));
3149 VisitForStackValue(args->at(1));
3150 VisitForStackValue(args->at(2));
3151 __ CallStub(&stub);
3152 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003153}
3154
3155
3156void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003157 ASSERT(args->length() == 3);
3158 VisitForStackValue(args->at(0));
3159 VisitForStackValue(args->at(1));
3160 VisitForStackValue(args->at(2));
3161 Label done;
3162 Label slow_case;
3163 Register object = a0;
3164 Register index1 = a1;
3165 Register index2 = a2;
3166 Register elements = a3;
3167 Register scratch1 = t0;
3168 Register scratch2 = t1;
3169
3170 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3171 // Fetch the map and check if array is in fast case.
3172 // Check that object doesn't require security checks and
3173 // has no indexed interceptor.
3174 __ GetObjectType(object, scratch1, scratch2);
3175 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3176 // Map is now in scratch1.
3177
3178 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3179 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3180 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3181
3182 // Check the object's elements are in fast case and writable.
3183 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3184 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3185 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3186 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3187
3188 // Check that both indices are smis.
3189 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3190 __ lw(index2, MemOperand(sp, 0));
3191 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3192
3193 // Check that both indices are valid.
3194 Label not_hi;
3195 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3196 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3197 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3198 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3199 __ bind(&not_hi);
3200
3201 // Bring the address of the elements into index1 and index2.
3202 __ Addu(scratch1, elements,
3203 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3204 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3205 __ Addu(index1, scratch1, index1);
3206 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3207 __ Addu(index2, scratch1, index2);
3208
3209 // Swap elements.
3210 __ lw(scratch1, MemOperand(index1, 0));
3211 __ lw(scratch2, MemOperand(index2, 0));
3212 __ sw(scratch1, MemOperand(index2, 0));
3213 __ sw(scratch2, MemOperand(index1, 0));
3214
3215 Label new_space;
3216 __ InNewSpace(elements, scratch1, eq, &new_space);
3217 // Possible optimization: do a check that both values are Smis
3218 // (or them and test against Smi mask).
3219
3220 __ mov(scratch1, elements);
3221 __ RecordWriteHelper(elements, index1, scratch2);
3222 __ RecordWriteHelper(scratch1, index2, scratch2); // scratch1 holds elements.
3223
3224 __ bind(&new_space);
3225 // We are done. Drop elements from the stack, and return undefined.
3226 __ Drop(3);
3227 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3228 __ jmp(&done);
3229
3230 __ bind(&slow_case);
3231 __ CallRuntime(Runtime::kSwapElements, 3);
3232
3233 __ bind(&done);
3234 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003235}
3236
3237
3238void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003239 ASSERT_EQ(2, args->length());
3240
3241 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3242 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3243
3244 Handle<FixedArray> jsfunction_result_caches(
3245 isolate()->global_context()->jsfunction_result_caches());
3246 if (jsfunction_result_caches->length() <= cache_id) {
3247 __ Abort("Attempt to use undefined cache.");
3248 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3249 context()->Plug(v0);
3250 return;
3251 }
3252
3253 VisitForAccumulatorValue(args->at(1));
3254
3255 Register key = v0;
3256 Register cache = a1;
3257 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3258 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3259 __ lw(cache,
3260 ContextOperand(
3261 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3262 __ lw(cache,
3263 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3264
3265
3266 Label done, not_found;
3267 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
3268 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3269 // a2 now holds finger offset as a smi.
3270 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3271 // a3 now points to the start of fixed array elements.
3272 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3273 __ addu(a3, a3, at);
3274 // a3 now points to key of indexed element of cache.
3275 __ lw(a2, MemOperand(a3));
3276 __ Branch(&not_found, ne, key, Operand(a2));
3277
3278 __ lw(v0, MemOperand(a3, kPointerSize));
3279 __ Branch(&done);
3280
3281 __ bind(&not_found);
3282 // Call runtime to perform the lookup.
3283 __ Push(cache, key);
3284 __ CallRuntime(Runtime::kGetFromCache, 2);
3285
3286 __ bind(&done);
3287 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003288}
3289
3290
3291void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003292 ASSERT_EQ(2, args->length());
3293
3294 Register right = v0;
3295 Register left = a1;
3296 Register tmp = a2;
3297 Register tmp2 = a3;
3298
3299 VisitForStackValue(args->at(0));
3300 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3301 __ pop(left);
3302
3303 Label done, fail, ok;
3304 __ Branch(&ok, eq, left, Operand(right));
3305 // Fail if either is a non-HeapObject.
3306 __ And(tmp, left, Operand(right));
3307 __ And(at, tmp, Operand(kSmiTagMask));
3308 __ Branch(&fail, eq, at, Operand(zero_reg));
3309 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3310 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3311 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3312 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3313 __ Branch(&fail, ne, tmp, Operand(tmp2));
3314 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3315 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3316 __ Branch(&ok, eq, tmp, Operand(tmp2));
3317 __ bind(&fail);
3318 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3319 __ jmp(&done);
3320 __ bind(&ok);
3321 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3322 __ bind(&done);
3323
3324 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003325}
3326
3327
3328void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003329 VisitForAccumulatorValue(args->at(0));
3330
3331 Label materialize_true, materialize_false;
3332 Label* if_true = NULL;
3333 Label* if_false = NULL;
3334 Label* fall_through = NULL;
3335 context()->PrepareTest(&materialize_true, &materialize_false,
3336 &if_true, &if_false, &fall_through);
3337
3338 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3339 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3340
3341 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3342 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3343
3344 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003345}
3346
3347
3348void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003349 ASSERT(args->length() == 1);
3350 VisitForAccumulatorValue(args->at(0));
3351
3352 if (FLAG_debug_code) {
3353 __ AbortIfNotString(v0);
3354 }
3355
3356 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3357 __ IndexFromHash(v0, v0);
3358
3359 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003360}
3361
3362
3363void FullCodeGenerator::EmitFastAsciiArrayJoin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003364 Label bailout, done, one_char_separator, long_separator,
3365 non_trivial_array, not_size_one_array, loop,
3366 empty_separator_loop, one_char_separator_loop,
3367 one_char_separator_loop_entry, long_separator_loop;
3368
3369 ASSERT(args->length() == 2);
3370 VisitForStackValue(args->at(1));
3371 VisitForAccumulatorValue(args->at(0));
3372
3373 // All aliases of the same register have disjoint lifetimes.
3374 Register array = v0;
3375 Register elements = no_reg; // Will be v0.
3376 Register result = no_reg; // Will be v0.
3377 Register separator = a1;
3378 Register array_length = a2;
3379 Register result_pos = no_reg; // Will be a2.
3380 Register string_length = a3;
3381 Register string = t0;
3382 Register element = t1;
3383 Register elements_end = t2;
3384 Register scratch1 = t3;
3385 Register scratch2 = t5;
3386 Register scratch3 = t4;
3387 Register scratch4 = v1;
3388
3389 // Separator operand is on the stack.
3390 __ pop(separator);
3391
3392 // Check that the array is a JSArray.
3393 __ JumpIfSmi(array, &bailout);
3394 __ GetObjectType(array, scratch1, scratch2);
3395 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3396
3397 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003398 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003399
3400 // If the array has length zero, return the empty string.
3401 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3402 __ SmiUntag(array_length);
3403 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3404 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3405 __ Branch(&done);
3406
3407 __ bind(&non_trivial_array);
3408
3409 // Get the FixedArray containing array's elements.
3410 elements = array;
3411 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3412 array = no_reg; // End of array's live range.
3413
3414 // Check that all array elements are sequential ASCII strings, and
3415 // accumulate the sum of their lengths, as a smi-encoded value.
3416 __ mov(string_length, zero_reg);
3417 __ Addu(element,
3418 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3419 __ sll(elements_end, array_length, kPointerSizeLog2);
3420 __ Addu(elements_end, element, elements_end);
3421 // Loop condition: while (element < elements_end).
3422 // Live values in registers:
3423 // elements: Fixed array of strings.
3424 // array_length: Length of the fixed array of strings (not smi)
3425 // separator: Separator string
3426 // string_length: Accumulated sum of string lengths (smi).
3427 // element: Current array element.
3428 // elements_end: Array end.
3429 if (FLAG_debug_code) {
3430 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3431 array_length, Operand(zero_reg));
3432 }
3433 __ bind(&loop);
3434 __ lw(string, MemOperand(element));
3435 __ Addu(element, element, kPointerSize);
3436 __ JumpIfSmi(string, &bailout);
3437 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3438 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3439 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3440 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3441 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3442 __ BranchOnOverflow(&bailout, scratch3);
3443 __ Branch(&loop, lt, element, Operand(elements_end));
3444
3445 // If array_length is 1, return elements[0], a string.
3446 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3447 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3448 __ Branch(&done);
3449
3450 __ bind(&not_size_one_array);
3451
3452 // Live values in registers:
3453 // separator: Separator string
3454 // array_length: Length of the array.
3455 // string_length: Sum of string lengths (smi).
3456 // elements: FixedArray of strings.
3457
3458 // Check that the separator is a flat ASCII string.
3459 __ JumpIfSmi(separator, &bailout);
3460 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3461 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3462 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3463
3464 // Add (separator length times array_length) - separator length to the
3465 // string_length to get the length of the result string. array_length is not
3466 // smi but the other values are, so the result is a smi.
3467 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3468 __ Subu(string_length, string_length, Operand(scratch1));
3469 __ Mult(array_length, scratch1);
3470 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3471 // zero.
3472 __ mfhi(scratch2);
3473 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3474 __ mflo(scratch2);
3475 __ And(scratch3, scratch2, Operand(0x80000000));
3476 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3477 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3478 __ BranchOnOverflow(&bailout, scratch3);
3479 __ SmiUntag(string_length);
3480
3481 // Get first element in the array to free up the elements register to be used
3482 // for the result.
3483 __ Addu(element,
3484 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3485 result = elements; // End of live range for elements.
3486 elements = no_reg;
3487 // Live values in registers:
3488 // element: First array element
3489 // separator: Separator string
3490 // string_length: Length of result string (not smi)
3491 // array_length: Length of the array.
3492 __ AllocateAsciiString(result,
3493 string_length,
3494 scratch1,
3495 scratch2,
3496 elements_end,
3497 &bailout);
3498 // Prepare for looping. Set up elements_end to end of the array. Set
3499 // result_pos to the position of the result where to write the first
3500 // character.
3501 __ sll(elements_end, array_length, kPointerSizeLog2);
3502 __ Addu(elements_end, element, elements_end);
3503 result_pos = array_length; // End of live range for array_length.
3504 array_length = no_reg;
3505 __ Addu(result_pos,
3506 result,
3507 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3508
3509 // Check the length of the separator.
3510 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3511 __ li(at, Operand(Smi::FromInt(1)));
3512 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3513 __ Branch(&long_separator, gt, scratch1, Operand(at));
3514
3515 // Empty separator case.
3516 __ bind(&empty_separator_loop);
3517 // Live values in registers:
3518 // result_pos: the position to which we are currently copying characters.
3519 // element: Current array element.
3520 // elements_end: Array end.
3521
3522 // Copy next array element to the result.
3523 __ lw(string, MemOperand(element));
3524 __ Addu(element, element, kPointerSize);
3525 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3526 __ SmiUntag(string_length);
3527 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3528 __ CopyBytes(string, result_pos, string_length, scratch1);
3529 // End while (element < elements_end).
3530 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3531 ASSERT(result.is(v0));
3532 __ Branch(&done);
3533
3534 // One-character separator case.
3535 __ bind(&one_char_separator);
3536 // Replace separator with its ascii character value.
3537 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3538 // Jump into the loop after the code that copies the separator, so the first
3539 // element is not preceded by a separator.
3540 __ jmp(&one_char_separator_loop_entry);
3541
3542 __ bind(&one_char_separator_loop);
3543 // Live values in registers:
3544 // result_pos: the position to which we are currently copying characters.
3545 // element: Current array element.
3546 // elements_end: Array end.
3547 // separator: Single separator ascii char (in lower byte).
3548
3549 // Copy the separator character to the result.
3550 __ sb(separator, MemOperand(result_pos));
3551 __ Addu(result_pos, result_pos, 1);
3552
3553 // Copy next array element to the result.
3554 __ bind(&one_char_separator_loop_entry);
3555 __ lw(string, MemOperand(element));
3556 __ Addu(element, element, kPointerSize);
3557 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3558 __ SmiUntag(string_length);
3559 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3560 __ CopyBytes(string, result_pos, string_length, scratch1);
3561 // End while (element < elements_end).
3562 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3563 ASSERT(result.is(v0));
3564 __ Branch(&done);
3565
3566 // Long separator case (separator is more than one character). Entry is at the
3567 // label long_separator below.
3568 __ bind(&long_separator_loop);
3569 // Live values in registers:
3570 // result_pos: the position to which we are currently copying characters.
3571 // element: Current array element.
3572 // elements_end: Array end.
3573 // separator: Separator string.
3574
3575 // Copy the separator to the result.
3576 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3577 __ SmiUntag(string_length);
3578 __ Addu(string,
3579 separator,
3580 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3581 __ CopyBytes(string, result_pos, string_length, scratch1);
3582
3583 __ bind(&long_separator);
3584 __ lw(string, MemOperand(element));
3585 __ Addu(element, element, kPointerSize);
3586 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3587 __ SmiUntag(string_length);
3588 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3589 __ CopyBytes(string, result_pos, string_length, scratch1);
3590 // End while (element < elements_end).
3591 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3592 ASSERT(result.is(v0));
3593 __ Branch(&done);
3594
3595 __ bind(&bailout);
3596 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3597 __ bind(&done);
3598 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003599}
3600
3601
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003602void FullCodeGenerator::EmitIsNativeOrStrictMode(ZoneList<Expression*>* args) {
3603 ASSERT(args->length() == 1);
3604
3605 // Load the function into v0.
3606 VisitForAccumulatorValue(args->at(0));
3607
3608 // Prepare for the test.
3609 Label materialize_true, materialize_false;
3610 Label* if_true = NULL;
3611 Label* if_false = NULL;
3612 Label* fall_through = NULL;
3613 context()->PrepareTest(&materialize_true, &materialize_false,
3614 &if_true, &if_false, &fall_through);
3615
3616 // Test for strict mode function.
3617 __ lw(a1, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
3618 __ lw(a1, FieldMemOperand(a1, SharedFunctionInfo::kCompilerHintsOffset));
3619 __ And(at, a1, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
3620 kSmiTagSize)));
3621 __ Branch(if_true, ne, at, Operand(zero_reg));
3622
3623 // Test for native function.
3624 __ And(at, a1, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
3625 __ Branch(if_true, ne, at, Operand(zero_reg));
3626
3627 // Not native or strict-mode function.
3628 __ Branch(if_false);
3629
3630 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3631 context()->Plug(if_true, if_false);
3632}
3633
3634
ager@chromium.org5c838252010-02-19 08:53:10 +00003635void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003636 Handle<String> name = expr->name();
3637 if (name->length() > 0 && name->Get(0) == '_') {
3638 Comment cmnt(masm_, "[ InlineRuntimeCall");
3639 EmitInlineRuntimeCall(expr);
3640 return;
3641 }
3642
3643 Comment cmnt(masm_, "[ CallRuntime");
3644 ZoneList<Expression*>* args = expr->arguments();
3645
3646 if (expr->is_jsruntime()) {
3647 // Prepare for calling JS runtime function.
3648 __ lw(a0, GlobalObjectOperand());
3649 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3650 __ push(a0);
3651 }
3652
3653 // Push the arguments ("left-to-right").
3654 int arg_count = args->length();
3655 for (int i = 0; i < arg_count; i++) {
3656 VisitForStackValue(args->at(i));
3657 }
3658
3659 if (expr->is_jsruntime()) {
3660 // Call the JS runtime function.
3661 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003662 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003663 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00003664 isolate()->stub_cache()->ComputeCallInitialize(arg_count,
3665 NOT_IN_LOOP,
3666 mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003667 __ CallWithAstId(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003668 // Restore context register.
3669 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3670 } else {
3671 // Call the C runtime function.
3672 __ CallRuntime(expr->function(), arg_count);
3673 }
3674 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003675}
3676
3677
3678void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003679 switch (expr->op()) {
3680 case Token::DELETE: {
3681 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
3682 Property* prop = expr->expression()->AsProperty();
3683 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
3684
3685 if (prop != NULL) {
3686 if (prop->is_synthetic()) {
3687 // Result of deleting parameters is false, even when they rewrite
3688 // to accesses on the arguments object.
3689 context()->Plug(false);
3690 } else {
3691 VisitForStackValue(prop->obj());
3692 VisitForStackValue(prop->key());
3693 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3694 __ push(a1);
3695 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3696 context()->Plug(v0);
3697 }
3698 } else if (var != NULL) {
3699 // Delete of an unqualified identifier is disallowed in strict mode
3700 // but "delete this" is.
3701 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
3702 if (var->is_global()) {
3703 __ lw(a2, GlobalObjectOperand());
3704 __ li(a1, Operand(var->name()));
3705 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3706 __ Push(a2, a1, a0);
3707 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3708 context()->Plug(v0);
3709 } else if (var->AsSlot() != NULL &&
3710 var->AsSlot()->type() != Slot::LOOKUP) {
3711 // Result of deleting non-global, non-dynamic variables is false.
3712 // The subexpression does not have side effects.
3713 context()->Plug(false);
3714 } else {
3715 // Non-global variable. Call the runtime to try to delete from the
3716 // context where the variable was introduced.
3717 __ push(context_register());
3718 __ li(a2, Operand(var->name()));
3719 __ push(a2);
3720 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3721 context()->Plug(v0);
3722 }
3723 } else {
3724 // Result of deleting non-property, non-variable reference is true.
3725 // The subexpression may have side effects.
3726 VisitForEffect(expr->expression());
3727 context()->Plug(true);
3728 }
3729 break;
3730 }
3731
3732 case Token::VOID: {
3733 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3734 VisitForEffect(expr->expression());
3735 context()->Plug(Heap::kUndefinedValueRootIndex);
3736 break;
3737 }
3738
3739 case Token::NOT: {
3740 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3741 if (context()->IsEffect()) {
3742 // Unary NOT has no side effects so it's only necessary to visit the
3743 // subexpression. Match the optimizing compiler by not branching.
3744 VisitForEffect(expr->expression());
3745 } else {
3746 Label materialize_true, materialize_false;
3747 Label* if_true = NULL;
3748 Label* if_false = NULL;
3749 Label* fall_through = NULL;
3750
3751 // Notice that the labels are swapped.
3752 context()->PrepareTest(&materialize_true, &materialize_false,
3753 &if_false, &if_true, &fall_through);
3754 if (context()->IsTest()) ForwardBailoutToChild(expr);
3755 VisitForControl(expr->expression(), if_true, if_false, fall_through);
3756 context()->Plug(if_false, if_true); // Labels swapped.
3757 }
3758 break;
3759 }
3760
3761 case Token::TYPEOF: {
3762 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3763 { StackValueContext context(this);
3764 VisitForTypeofValue(expr->expression());
3765 }
3766 __ CallRuntime(Runtime::kTypeof, 1);
3767 context()->Plug(v0);
3768 break;
3769 }
3770
3771 case Token::ADD: {
3772 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3773 VisitForAccumulatorValue(expr->expression());
3774 Label no_conversion;
3775 __ JumpIfSmi(result_register(), &no_conversion);
3776 __ mov(a0, result_register());
3777 ToNumberStub convert_stub;
3778 __ CallStub(&convert_stub);
3779 __ bind(&no_conversion);
3780 context()->Plug(result_register());
3781 break;
3782 }
3783
3784 case Token::SUB:
3785 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3786 break;
3787
3788 case Token::BIT_NOT:
3789 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3790 break;
3791
3792 default:
3793 UNREACHABLE();
3794 }
3795}
3796
3797
3798void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3799 const char* comment) {
3800 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3801 Comment cmt(masm_, comment);
3802 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3803 UnaryOverwriteMode overwrite =
3804 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003805 UnaryOpStub stub(expr->op(), overwrite);
3806 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003807 VisitForAccumulatorValue(expr->expression());
3808 SetSourcePosition(expr->position());
3809 __ mov(a0, result_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003810 __ CallWithAstId(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003811 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003812}
3813
3814
3815void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003816 Comment cmnt(masm_, "[ CountOperation");
3817 SetSourcePosition(expr->position());
3818
3819 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3820 // as the left-hand side.
3821 if (!expr->expression()->IsValidLeftHandSide()) {
3822 VisitForEffect(expr->expression());
3823 return;
3824 }
3825
3826 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003827 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003828 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3829 LhsKind assign_type = VARIABLE;
3830 Property* prop = expr->expression()->AsProperty();
3831 // In case of a property we use the uninitialized expression context
3832 // of the key to detect a named property.
3833 if (prop != NULL) {
3834 assign_type =
3835 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3836 }
3837
3838 // Evaluate expression and get value.
3839 if (assign_type == VARIABLE) {
3840 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3841 AccumulatorValueContext context(this);
3842 EmitVariableLoad(expr->expression()->AsVariableProxy()->var());
3843 } else {
3844 // Reserve space for result of postfix operation.
3845 if (expr->is_postfix() && !context()->IsEffect()) {
3846 __ li(at, Operand(Smi::FromInt(0)));
3847 __ push(at);
3848 }
3849 if (assign_type == NAMED_PROPERTY) {
3850 // Put the object both on the stack and in the accumulator.
3851 VisitForAccumulatorValue(prop->obj());
3852 __ push(v0);
3853 EmitNamedPropertyLoad(prop);
3854 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003855 VisitForStackValue(prop->obj());
3856 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003857 __ lw(a1, MemOperand(sp, 0));
3858 __ push(v0);
3859 EmitKeyedPropertyLoad(prop);
3860 }
3861 }
3862
3863 // We need a second deoptimization point after loading the value
3864 // in case evaluating the property load my have a side effect.
3865 if (assign_type == VARIABLE) {
3866 PrepareForBailout(expr->expression(), TOS_REG);
3867 } else {
3868 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3869 }
3870
3871 // Call ToNumber only if operand is not a smi.
3872 Label no_conversion;
3873 __ JumpIfSmi(v0, &no_conversion);
3874 __ mov(a0, v0);
3875 ToNumberStub convert_stub;
3876 __ CallStub(&convert_stub);
3877 __ bind(&no_conversion);
3878
3879 // Save result for postfix expressions.
3880 if (expr->is_postfix()) {
3881 if (!context()->IsEffect()) {
3882 // Save the result on the stack. If we have a named or keyed property
3883 // we store the result under the receiver that is currently on top
3884 // of the stack.
3885 switch (assign_type) {
3886 case VARIABLE:
3887 __ push(v0);
3888 break;
3889 case NAMED_PROPERTY:
3890 __ sw(v0, MemOperand(sp, kPointerSize));
3891 break;
3892 case KEYED_PROPERTY:
3893 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3894 break;
3895 }
3896 }
3897 }
3898 __ mov(a0, result_register());
3899
3900 // Inline smi case if we are in a loop.
3901 Label stub_call, done;
3902 JumpPatchSite patch_site(masm_);
3903
3904 int count_value = expr->op() == Token::INC ? 1 : -1;
3905 __ li(a1, Operand(Smi::FromInt(count_value)));
3906
3907 if (ShouldInlineSmiCase(expr->op())) {
3908 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
3909 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
3910
3911 // We could eliminate this smi check if we split the code at
3912 // the first smi check before calling ToNumber.
3913 patch_site.EmitJumpIfSmi(v0, &done);
3914 __ bind(&stub_call);
3915 }
3916
3917 // Record position before stub call.
3918 SetSourcePosition(expr->position());
3919
danno@chromium.org40cb8782011-05-25 07:58:50 +00003920 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003921 __ CallWithAstId(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
3922 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003923 __ bind(&done);
3924
3925 // Store the value returned in v0.
3926 switch (assign_type) {
3927 case VARIABLE:
3928 if (expr->is_postfix()) {
3929 { EffectContext context(this);
3930 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3931 Token::ASSIGN);
3932 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3933 context.Plug(v0);
3934 }
3935 // For all contexts except EffectConstant we have the result on
3936 // top of the stack.
3937 if (!context()->IsEffect()) {
3938 context()->PlugTOS();
3939 }
3940 } else {
3941 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3942 Token::ASSIGN);
3943 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3944 context()->Plug(v0);
3945 }
3946 break;
3947 case NAMED_PROPERTY: {
3948 __ mov(a0, result_register()); // Value.
3949 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
3950 __ pop(a1); // Receiver.
3951 Handle<Code> ic = is_strict_mode()
3952 ? isolate()->builtins()->StoreIC_Initialize_Strict()
3953 : isolate()->builtins()->StoreIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003954 __ CallWithAstId(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003955 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3956 if (expr->is_postfix()) {
3957 if (!context()->IsEffect()) {
3958 context()->PlugTOS();
3959 }
3960 } else {
3961 context()->Plug(v0);
3962 }
3963 break;
3964 }
3965 case KEYED_PROPERTY: {
3966 __ mov(a0, result_register()); // Value.
3967 __ pop(a1); // Key.
3968 __ pop(a2); // Receiver.
3969 Handle<Code> ic = is_strict_mode()
3970 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
3971 : isolate()->builtins()->KeyedStoreIC_Initialize();
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003972 __ CallWithAstId(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003973 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3974 if (expr->is_postfix()) {
3975 if (!context()->IsEffect()) {
3976 context()->PlugTOS();
3977 }
3978 } else {
3979 context()->Plug(v0);
3980 }
3981 break;
3982 }
3983 }
ager@chromium.org5c838252010-02-19 08:53:10 +00003984}
3985
3986
lrn@chromium.org7516f052011-03-30 08:52:27 +00003987void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003988 VariableProxy* proxy = expr->AsVariableProxy();
3989 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
3990 Comment cmnt(masm_, "Global variable");
3991 __ lw(a0, GlobalObjectOperand());
3992 __ li(a2, Operand(proxy->name()));
3993 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
3994 // Use a regular load, not a contextual load, to avoid a reference
3995 // error.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003996 __ CallWithAstId(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003997 PrepareForBailout(expr, TOS_REG);
3998 context()->Plug(v0);
3999 } else if (proxy != NULL &&
4000 proxy->var()->AsSlot() != NULL &&
4001 proxy->var()->AsSlot()->type() == Slot::LOOKUP) {
4002 Label done, slow;
4003
4004 // Generate code for loading from variables potentially shadowed
4005 // by eval-introduced variables.
4006 Slot* slot = proxy->var()->AsSlot();
4007 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
4008
4009 __ bind(&slow);
4010 __ li(a0, Operand(proxy->name()));
4011 __ Push(cp, a0);
4012 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4013 PrepareForBailout(expr, TOS_REG);
4014 __ bind(&done);
4015
4016 context()->Plug(v0);
4017 } else {
4018 // This expression cannot throw a reference error at the top level.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004019 VisitInCurrentContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004020 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004021}
4022
ager@chromium.org04921a82011-06-27 13:21:41 +00004023void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
4024 Handle<String> check,
4025 Label* if_true,
4026 Label* if_false,
4027 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004028 { AccumulatorValueContext context(this);
ager@chromium.org04921a82011-06-27 13:21:41 +00004029 VisitForTypeofValue(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004030 }
4031 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4032
4033 if (check->Equals(isolate()->heap()->number_symbol())) {
4034 __ JumpIfSmi(v0, if_true);
4035 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4036 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4037 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4038 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4039 __ JumpIfSmi(v0, if_false);
4040 // Check for undetectable objects => false.
4041 __ GetObjectType(v0, v0, a1);
4042 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4043 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4044 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4045 Split(eq, a1, Operand(zero_reg),
4046 if_true, if_false, fall_through);
4047 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4048 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4049 __ Branch(if_true, eq, v0, Operand(at));
4050 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4051 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4052 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4053 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4054 __ Branch(if_true, eq, v0, Operand(at));
4055 __ JumpIfSmi(v0, if_false);
4056 // Check for undetectable objects => true.
4057 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4058 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4059 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4060 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4061 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4062 __ JumpIfSmi(v0, if_false);
4063 __ GetObjectType(v0, a1, v0); // Leave map in a1.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004064 Split(ge, v0, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004065 if_true, if_false, fall_through);
4066
4067 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4068 __ JumpIfSmi(v0, if_false);
4069 __ LoadRoot(at, Heap::kNullValueRootIndex);
4070 __ Branch(if_true, eq, v0, Operand(at));
4071 // Check for JS objects => true.
4072 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004073 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004074 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004075 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004076 // Check for undetectable objects => false.
4077 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4078 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4079 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4080 } else {
4081 if (if_false != fall_through) __ jmp(if_false);
4082 }
ager@chromium.org04921a82011-06-27 13:21:41 +00004083}
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004084
ager@chromium.org04921a82011-06-27 13:21:41 +00004085
4086void FullCodeGenerator::EmitLiteralCompareUndefined(Expression* expr,
4087 Label* if_true,
4088 Label* if_false,
4089 Label* fall_through) {
4090 VisitForAccumulatorValue(expr);
4091 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4092
4093 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4094 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004095}
4096
4097
ager@chromium.org5c838252010-02-19 08:53:10 +00004098void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004099 Comment cmnt(masm_, "[ CompareOperation");
4100 SetSourcePosition(expr->position());
4101
4102 // Always perform the comparison for its control flow. Pack the result
4103 // into the expression's context after the comparison is performed.
4104
4105 Label materialize_true, materialize_false;
4106 Label* if_true = NULL;
4107 Label* if_false = NULL;
4108 Label* fall_through = NULL;
4109 context()->PrepareTest(&materialize_true, &materialize_false,
4110 &if_true, &if_false, &fall_through);
4111
4112 // First we try a fast inlined version of the compare when one of
4113 // the operands is a literal.
ager@chromium.org04921a82011-06-27 13:21:41 +00004114 if (TryLiteralCompare(expr, if_true, if_false, fall_through)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004115 context()->Plug(if_true, if_false);
4116 return;
4117 }
4118
ager@chromium.org04921a82011-06-27 13:21:41 +00004119 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004120 VisitForStackValue(expr->left());
4121 switch (op) {
4122 case Token::IN:
4123 VisitForStackValue(expr->right());
4124 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
4125 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
4126 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4127 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4128 break;
4129
4130 case Token::INSTANCEOF: {
4131 VisitForStackValue(expr->right());
4132 InstanceofStub stub(InstanceofStub::kNoFlags);
4133 __ CallStub(&stub);
4134 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4135 // The stub returns 0 for true.
4136 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4137 break;
4138 }
4139
4140 default: {
4141 VisitForAccumulatorValue(expr->right());
4142 Condition cc = eq;
4143 bool strict = false;
4144 switch (op) {
4145 case Token::EQ_STRICT:
4146 strict = true;
4147 // Fall through.
4148 case Token::EQ:
4149 cc = eq;
4150 __ mov(a0, result_register());
4151 __ pop(a1);
4152 break;
4153 case Token::LT:
4154 cc = lt;
4155 __ mov(a0, result_register());
4156 __ pop(a1);
4157 break;
4158 case Token::GT:
4159 // Reverse left and right sides to obtain ECMA-262 conversion order.
4160 cc = lt;
4161 __ mov(a1, result_register());
4162 __ pop(a0);
4163 break;
4164 case Token::LTE:
4165 // Reverse left and right sides to obtain ECMA-262 conversion order.
4166 cc = ge;
4167 __ mov(a1, result_register());
4168 __ pop(a0);
4169 break;
4170 case Token::GTE:
4171 cc = ge;
4172 __ mov(a0, result_register());
4173 __ pop(a1);
4174 break;
4175 case Token::IN:
4176 case Token::INSTANCEOF:
4177 default:
4178 UNREACHABLE();
4179 }
4180
4181 bool inline_smi_code = ShouldInlineSmiCase(op);
4182 JumpPatchSite patch_site(masm_);
4183 if (inline_smi_code) {
4184 Label slow_case;
4185 __ Or(a2, a0, Operand(a1));
4186 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4187 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4188 __ bind(&slow_case);
4189 }
4190 // Record position and call the compare IC.
4191 SetSourcePosition(expr->position());
4192 Handle<Code> ic = CompareIC::GetUninitialized(op);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004193 __ CallWithAstId(ic, RelocInfo::CODE_TARGET, expr->id());
4194 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004195 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4196 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4197 }
4198 }
4199
4200 // Convert the result of the comparison into one expected for this
4201 // expression's context.
4202 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004203}
4204
4205
lrn@chromium.org7516f052011-03-30 08:52:27 +00004206void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004207 Comment cmnt(masm_, "[ CompareToNull");
4208 Label materialize_true, materialize_false;
4209 Label* if_true = NULL;
4210 Label* if_false = NULL;
4211 Label* fall_through = NULL;
4212 context()->PrepareTest(&materialize_true, &materialize_false,
4213 &if_true, &if_false, &fall_through);
4214
4215 VisitForAccumulatorValue(expr->expression());
4216 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4217 __ mov(a0, result_register());
4218 __ LoadRoot(a1, Heap::kNullValueRootIndex);
4219 if (expr->is_strict()) {
4220 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4221 } else {
4222 __ Branch(if_true, eq, a0, Operand(a1));
4223 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
4224 __ Branch(if_true, eq, a0, Operand(a1));
4225 __ And(at, a0, Operand(kSmiTagMask));
4226 __ Branch(if_false, eq, at, Operand(zero_reg));
4227 // It can be an undetectable object.
4228 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4229 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4230 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4231 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4232 }
4233 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004234}
4235
4236
ager@chromium.org5c838252010-02-19 08:53:10 +00004237void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004238 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4239 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004240}
4241
4242
lrn@chromium.org7516f052011-03-30 08:52:27 +00004243Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004244 return v0;
4245}
ager@chromium.org5c838252010-02-19 08:53:10 +00004246
4247
lrn@chromium.org7516f052011-03-30 08:52:27 +00004248Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004249 return cp;
4250}
4251
4252
ager@chromium.org5c838252010-02-19 08:53:10 +00004253void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004254 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4255 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004256}
4257
4258
4259void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004260 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004261}
4262
4263
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004264void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4265 Scope* declaration_scope = scope()->DeclarationScope();
4266 if (declaration_scope->is_global_scope()) {
4267 // Contexts nested in the global context have a canonical empty function
4268 // as their closure, not the anonymous closure containing the global
4269 // code. Pass a smi sentinel and let the runtime look up the empty
4270 // function.
4271 __ li(at, Operand(Smi::FromInt(0)));
4272 } else if (declaration_scope->is_eval_scope()) {
4273 // Contexts created by a call to eval have the same closure as the
4274 // context calling eval, not the anonymous closure containing the eval
4275 // code. Fetch it from the context.
4276 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4277 } else {
4278 ASSERT(declaration_scope->is_function_scope());
4279 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4280 }
4281 __ push(at);
4282}
4283
4284
ager@chromium.org5c838252010-02-19 08:53:10 +00004285// ----------------------------------------------------------------------------
4286// Non-local control flow support.
4287
4288void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004289 ASSERT(!result_register().is(a1));
4290 // Store result register while executing finally block.
4291 __ push(result_register());
4292 // Cook return address in link register to stack (smi encoded Code* delta).
4293 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4294 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4295 ASSERT_EQ(0, kSmiTag);
4296 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4297 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004298}
4299
4300
4301void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004302 ASSERT(!result_register().is(a1));
4303 // Restore result register from stack.
4304 __ pop(a1);
4305 // Uncook return address and return.
4306 __ pop(result_register());
4307 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4308 __ sra(a1, a1, 1); // Un-smi-tag value.
4309 __ Addu(at, a1, Operand(masm_->CodeObject()));
4310 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004311}
4312
4313
4314#undef __
4315
4316} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004317
4318#endif // V8_TARGET_ARCH_MIPS