blob: 3a7671df915a54a6d583248c3f2b82518bfedb72 [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() {
104 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
105 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
106 __ andi(at, reg, delta_to_patch_site % kImm16Mask);
107#ifdef DEBUG
108 info_emitted_ = true;
109#endif
110 }
111
112 bool is_bound() const { return patch_site_.is_bound(); }
113
114 private:
115 MacroAssembler* masm_;
116 Label patch_site_;
117#ifdef DEBUG
118 bool info_emitted_;
119#endif
120};
121
122
lrn@chromium.org7516f052011-03-30 08:52:27 +0000123// Generate code for a JS function. On entry to the function the receiver
124// and arguments have been pushed on the stack left to right. The actual
125// argument count matches the formal parameter count expected by the
126// function.
127//
128// The live registers are:
129// o a1: the JS function object being called (ie, ourselves)
130// o cp: our context
131// o fp: our caller's frame pointer
132// o sp: stack pointer
133// o ra: return address
134//
135// The function builds a JS frame. Please see JavaScriptFrameConstants in
136// frames-mips.h for its layout.
137void FullCodeGenerator::Generate(CompilationInfo* info) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000138 ASSERT(info_ == NULL);
139 info_ = info;
140 SetFunctionPosition(function());
141 Comment cmnt(masm_, "[ function compiled by full code generator");
142
143#ifdef DEBUG
144 if (strlen(FLAG_stop_at) > 0 &&
145 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
146 __ stop("stop-at");
147 }
148#endif
149
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000150 // Strict mode functions and builtins need to replace the receiver
151 // with undefined when called as functions (without an explicit
152 // receiver object). t1 is zero for method calls and non-zero for
153 // function calls.
154 if (info->is_strict_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000155 Label ok;
156 __ Branch(&ok, eq, t1, Operand(zero_reg));
157 int receiver_offset = scope()->num_parameters() * kPointerSize;
158 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
159 __ sw(a2, MemOperand(sp, receiver_offset));
160 __ bind(&ok);
161 }
162
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000163 int locals_count = scope()->num_stack_slots();
164
165 __ Push(ra, fp, cp, a1);
166 if (locals_count > 0) {
167 // Load undefined value here, so the value is ready for the loop
168 // below.
169 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
170 }
171 // Adjust fp to point to caller's fp.
172 __ Addu(fp, sp, Operand(2 * kPointerSize));
173
174 { Comment cmnt(masm_, "[ Allocate locals");
175 for (int i = 0; i < locals_count; i++) {
176 __ push(at);
177 }
178 }
179
180 bool function_in_register = true;
181
182 // Possibly allocate a local context.
183 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
184 if (heap_slots > 0) {
185 Comment cmnt(masm_, "[ Allocate local context");
186 // Argument to NewContext is the function, which is in a1.
187 __ push(a1);
188 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
189 FastNewContextStub stub(heap_slots);
190 __ CallStub(&stub);
191 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000192 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000193 }
194 function_in_register = false;
195 // Context is returned in both v0 and cp. It replaces the context
196 // passed to us. It's saved in the stack and kept live in cp.
197 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
198 // Copy any necessary parameters into the context.
199 int num_parameters = scope()->num_parameters();
200 for (int i = 0; i < num_parameters; i++) {
201 Slot* slot = scope()->parameter(i)->AsSlot();
202 if (slot != NULL && slot->type() == Slot::CONTEXT) {
203 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
204 (num_parameters - 1 - i) * kPointerSize;
205 // Load parameter from stack.
206 __ lw(a0, MemOperand(fp, parameter_offset));
207 // Store it in the context.
208 __ li(a1, Operand(Context::SlotOffset(slot->index())));
209 __ addu(a2, cp, a1);
210 __ sw(a0, MemOperand(a2, 0));
211 // Update the write barrier. This clobbers all involved
212 // registers, so we have to use two more registers to avoid
213 // clobbering cp.
214 __ mov(a2, cp);
215 __ RecordWrite(a2, a1, a3);
216 }
217 }
218 }
219
220 Variable* arguments = scope()->arguments();
221 if (arguments != NULL) {
222 // Function uses arguments object.
223 Comment cmnt(masm_, "[ Allocate arguments object");
224 if (!function_in_register) {
225 // Load this again, if it's used by the local context below.
226 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
227 } else {
228 __ mov(a3, a1);
229 }
230 // Receiver is just before the parameters on the caller's stack.
231 int offset = scope()->num_parameters() * kPointerSize;
232 __ Addu(a2, fp,
233 Operand(StandardFrameConstants::kCallerSPOffset + offset));
234 __ li(a1, Operand(Smi::FromInt(scope()->num_parameters())));
235 __ Push(a3, a2, a1);
236
237 // Arguments to ArgumentsAccessStub:
238 // function, receiver address, parameter count.
239 // The stub will rewrite receiever and parameter count if the previous
240 // stack frame was an arguments adapter frame.
241 ArgumentsAccessStub stub(
242 is_strict_mode() ? ArgumentsAccessStub::NEW_STRICT
243 : ArgumentsAccessStub::NEW_NON_STRICT);
244 __ CallStub(&stub);
245
246 Variable* arguments_shadow = scope()->arguments_shadow();
247 if (arguments_shadow != NULL) {
248 // Duplicate the value; move-to-slot operation might clobber registers.
249 __ mov(a3, v0);
250 Move(arguments_shadow->AsSlot(), a3, a1, a2);
251 }
252 Move(arguments->AsSlot(), v0, a1, a2);
253 }
254
255 if (FLAG_trace) {
256 __ CallRuntime(Runtime::kTraceEnter, 0);
257 }
258
259 // Visit the declarations and body unless there is an illegal
260 // redeclaration.
261 if (scope()->HasIllegalRedeclaration()) {
262 Comment cmnt(masm_, "[ Declarations");
263 scope()->VisitIllegalRedeclaration(this);
264
265 } else {
266 { Comment cmnt(masm_, "[ Declarations");
267 // For named function expressions, declare the function name as a
268 // constant.
269 if (scope()->is_function_scope() && scope()->function() != NULL) {
270 EmitDeclaration(scope()->function(), Variable::CONST, NULL);
271 }
272 VisitDeclarations(scope()->declarations());
273 }
274
275 { Comment cmnt(masm_, "[ Stack check");
276 PrepareForBailoutForId(AstNode::kFunctionEntryId, NO_REGISTERS);
277 Label ok;
278 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
279 __ Branch(&ok, hs, sp, Operand(t0));
280 StackCheckStub stub;
281 __ CallStub(&stub);
282 __ bind(&ok);
283 }
284
285 { Comment cmnt(masm_, "[ Body");
286 ASSERT(loop_depth() == 0);
287 VisitStatements(function()->body());
288 ASSERT(loop_depth() == 0);
289 }
290 }
291
292 // Always emit a 'return undefined' in case control fell off the end of
293 // the body.
294 { Comment cmnt(masm_, "[ return <undefined>;");
295 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
296 }
297 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000298}
299
300
301void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000302 ASSERT(Smi::FromInt(0) == 0);
303 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000304}
305
306
307void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000308 Comment cmnt(masm_, "[ Stack check");
309 Label ok;
310 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
311 __ Branch(&ok, hs, sp, Operand(t0));
312 StackCheckStub stub;
313 // Record a mapping of this PC offset to the OSR id. This is used to find
314 // the AST id from the unoptimized code in order to use it as a key into
315 // the deoptimization input data found in the optimized code.
316 RecordStackCheck(stmt->OsrEntryId());
317
318 __ CallStub(&stub);
319 __ bind(&ok);
320 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
321 // Record a mapping of the OSR id to this PC. This is used if the OSR
322 // entry becomes the target of a bailout. We don't expect it to be, but
323 // we want it to work if it is.
324 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000325}
326
327
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000328void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000329 Comment cmnt(masm_, "[ Return sequence");
330 if (return_label_.is_bound()) {
331 __ Branch(&return_label_);
332 } else {
333 __ bind(&return_label_);
334 if (FLAG_trace) {
335 // Push the return value on the stack as the parameter.
336 // Runtime::TraceExit returns its parameter in v0.
337 __ push(v0);
338 __ CallRuntime(Runtime::kTraceExit, 1);
339 }
340
341#ifdef DEBUG
342 // Add a label for checking the size of the code used for returning.
343 Label check_exit_codesize;
344 masm_->bind(&check_exit_codesize);
345#endif
346 // Make sure that the constant pool is not emitted inside of the return
347 // sequence.
348 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
349 // Here we use masm_-> instead of the __ macro to avoid the code coverage
350 // tool from instrumenting as we rely on the code size here.
351 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
352 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
353 __ RecordJSReturn();
354 masm_->mov(sp, fp);
355 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
356 masm_->Addu(sp, sp, Operand(sp_delta));
357 masm_->Jump(ra);
358 }
359
360#ifdef DEBUG
361 // Check that the size of the code used for returning is large enough
362 // for the debugger's requirements.
363 ASSERT(Assembler::kJSReturnSequenceInstructions <=
364 masm_->InstructionsGeneratedSince(&check_exit_codesize));
365#endif
366 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000367}
368
369
lrn@chromium.org7516f052011-03-30 08:52:27 +0000370void FullCodeGenerator::EffectContext::Plug(Slot* slot) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000371}
372
373
lrn@chromium.org7516f052011-03-30 08:52:27 +0000374void FullCodeGenerator::AccumulatorValueContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000375 codegen()->Move(result_register(), slot);
ager@chromium.org5c838252010-02-19 08:53:10 +0000376}
377
378
lrn@chromium.org7516f052011-03-30 08:52:27 +0000379void FullCodeGenerator::StackValueContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000380 codegen()->Move(result_register(), slot);
381 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000382}
383
384
lrn@chromium.org7516f052011-03-30 08:52:27 +0000385void FullCodeGenerator::TestContext::Plug(Slot* slot) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000386 // For simplicity we always test the accumulator register.
387 codegen()->Move(result_register(), slot);
388 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
389 codegen()->DoTest(true_label_, false_label_, fall_through_);
ager@chromium.org5c838252010-02-19 08:53:10 +0000390}
391
392
lrn@chromium.org7516f052011-03-30 08:52:27 +0000393void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000394}
395
396
lrn@chromium.org7516f052011-03-30 08:52:27 +0000397void FullCodeGenerator::AccumulatorValueContext::Plug(
398 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000399 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000400}
401
402
403void FullCodeGenerator::StackValueContext::Plug(
404 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000405 __ LoadRoot(result_register(), index);
406 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000407}
408
409
410void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000411 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
412 true,
413 true_label_,
414 false_label_);
415 if (index == Heap::kUndefinedValueRootIndex ||
416 index == Heap::kNullValueRootIndex ||
417 index == Heap::kFalseValueRootIndex) {
418 if (false_label_ != fall_through_) __ Branch(false_label_);
419 } else if (index == Heap::kTrueValueRootIndex) {
420 if (true_label_ != fall_through_) __ Branch(true_label_);
421 } else {
422 __ LoadRoot(result_register(), index);
423 codegen()->DoTest(true_label_, false_label_, fall_through_);
424 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000425}
426
427
428void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000429}
430
431
432void FullCodeGenerator::AccumulatorValueContext::Plug(
433 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000434 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000435}
436
437
438void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000439 // Immediates cannot be pushed directly.
440 __ li(result_register(), Operand(lit));
441 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000442}
443
444
445void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000446 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
447 true,
448 true_label_,
449 false_label_);
450 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
451 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
452 if (false_label_ != fall_through_) __ Branch(false_label_);
453 } else if (lit->IsTrue() || lit->IsJSObject()) {
454 if (true_label_ != fall_through_) __ Branch(true_label_);
455 } else if (lit->IsString()) {
456 if (String::cast(*lit)->length() == 0) {
457 if (false_label_ != fall_through_) __ Branch(false_label_);
458 } else {
459 if (true_label_ != fall_through_) __ Branch(true_label_);
460 }
461 } else if (lit->IsSmi()) {
462 if (Smi::cast(*lit)->value() == 0) {
463 if (false_label_ != fall_through_) __ Branch(false_label_);
464 } else {
465 if (true_label_ != fall_through_) __ Branch(true_label_);
466 }
467 } else {
468 // For simplicity we always test the accumulator register.
469 __ li(result_register(), Operand(lit));
470 codegen()->DoTest(true_label_, false_label_, fall_through_);
471 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000472}
473
474
475void FullCodeGenerator::EffectContext::DropAndPlug(int count,
476 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000477 ASSERT(count > 0);
478 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000479}
480
481
482void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
483 int count,
484 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000485 ASSERT(count > 0);
486 __ Drop(count);
487 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000488}
489
490
491void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
492 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000493 ASSERT(count > 0);
494 if (count > 1) __ Drop(count - 1);
495 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000496}
497
498
499void FullCodeGenerator::TestContext::DropAndPlug(int count,
500 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000501 ASSERT(count > 0);
502 // For simplicity we always test the accumulator register.
503 __ Drop(count);
504 __ Move(result_register(), reg);
505 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
506 codegen()->DoTest(true_label_, false_label_, fall_through_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000507}
508
509
510void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
511 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000512 ASSERT(materialize_true == materialize_false);
513 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000514}
515
516
517void FullCodeGenerator::AccumulatorValueContext::Plug(
518 Label* materialize_true,
519 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000520 Label done;
521 __ bind(materialize_true);
522 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
523 __ Branch(&done);
524 __ bind(materialize_false);
525 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
526 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000527}
528
529
530void FullCodeGenerator::StackValueContext::Plug(
531 Label* materialize_true,
532 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000533 Label done;
534 __ bind(materialize_true);
535 __ LoadRoot(at, Heap::kTrueValueRootIndex);
536 __ push(at);
537 __ Branch(&done);
538 __ bind(materialize_false);
539 __ LoadRoot(at, Heap::kFalseValueRootIndex);
540 __ push(at);
541 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000542}
543
544
545void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
546 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000547 ASSERT(materialize_true == true_label_);
548 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000549}
550
551
552void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000553}
554
555
556void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000557 Heap::RootListIndex value_root_index =
558 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
559 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000560}
561
562
563void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000564 Heap::RootListIndex value_root_index =
565 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
566 __ LoadRoot(at, value_root_index);
567 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000568}
569
570
571void FullCodeGenerator::TestContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000572 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
573 true,
574 true_label_,
575 false_label_);
576 if (flag) {
577 if (true_label_ != fall_through_) __ Branch(true_label_);
578 } else {
579 if (false_label_ != fall_through_) __ Branch(false_label_);
580 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000581}
582
583
584void FullCodeGenerator::DoTest(Label* if_true,
585 Label* if_false,
586 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000587 if (CpuFeatures::IsSupported(FPU)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000588 ToBooleanStub stub(result_register());
589 __ CallStub(&stub);
590 __ mov(at, zero_reg);
591 } else {
592 // Call the runtime to find the boolean value of the source and then
593 // translate it into control flow to the pair of labels.
594 __ push(result_register());
595 __ CallRuntime(Runtime::kToBool, 1);
596 __ LoadRoot(at, Heap::kFalseValueRootIndex);
597 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000598 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000599}
600
601
lrn@chromium.org7516f052011-03-30 08:52:27 +0000602void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000603 Register lhs,
604 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000605 Label* if_true,
606 Label* if_false,
607 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000608 if (if_false == fall_through) {
609 __ Branch(if_true, cc, lhs, rhs);
610 } else if (if_true == fall_through) {
611 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
612 } else {
613 __ Branch(if_true, cc, lhs, rhs);
614 __ Branch(if_false);
615 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000616}
617
618
619MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000620 switch (slot->type()) {
621 case Slot::PARAMETER:
622 case Slot::LOCAL:
623 return MemOperand(fp, SlotOffset(slot));
624 case Slot::CONTEXT: {
625 int context_chain_length =
626 scope()->ContextChainLength(slot->var()->scope());
627 __ LoadContext(scratch, context_chain_length);
628 return ContextOperand(scratch, slot->index());
629 }
630 case Slot::LOOKUP:
631 UNREACHABLE();
632 }
633 UNREACHABLE();
634 return MemOperand(v0, 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000635}
636
637
638void FullCodeGenerator::Move(Register destination, Slot* source) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000639 // Use destination as scratch.
640 MemOperand slot_operand = EmitSlotSearch(source, destination);
641 __ lw(destination, slot_operand);
ager@chromium.org5c838252010-02-19 08:53:10 +0000642}
643
644
lrn@chromium.org7516f052011-03-30 08:52:27 +0000645void FullCodeGenerator::PrepareForBailoutBeforeSplit(State state,
646 bool should_normalize,
647 Label* if_true,
648 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000649 // Only prepare for bailouts before splits if we're in a test
650 // context. Otherwise, we let the Visit function deal with the
651 // preparation to avoid preparing with the same AST id twice.
652 if (!context()->IsTest() || !info_->IsOptimizable()) return;
653
654 Label skip;
655 if (should_normalize) __ Branch(&skip);
656
657 ForwardBailoutStack* current = forward_bailout_stack_;
658 while (current != NULL) {
659 PrepareForBailout(current->expr(), state);
660 current = current->parent();
661 }
662
663 if (should_normalize) {
664 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
665 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
666 __ bind(&skip);
667 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000668}
669
670
ager@chromium.org5c838252010-02-19 08:53:10 +0000671void FullCodeGenerator::Move(Slot* dst,
672 Register src,
673 Register scratch1,
674 Register scratch2) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000675 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
676 ASSERT(!scratch1.is(src) && !scratch2.is(src));
677 MemOperand location = EmitSlotSearch(dst, scratch1);
678 __ sw(src, location);
679 // Emit the write barrier code if the location is in the heap.
680 if (dst->type() == Slot::CONTEXT) {
681 __ RecordWrite(scratch1,
682 Operand(Context::SlotOffset(dst->index())),
683 scratch2,
684 src);
685 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000686}
687
688
lrn@chromium.org7516f052011-03-30 08:52:27 +0000689void FullCodeGenerator::EmitDeclaration(Variable* variable,
690 Variable::Mode mode,
691 FunctionLiteral* function) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000692 Comment cmnt(masm_, "[ Declaration");
693 ASSERT(variable != NULL); // Must have been resolved.
694 Slot* slot = variable->AsSlot();
695 Property* prop = variable->AsProperty();
696
697 if (slot != NULL) {
698 switch (slot->type()) {
699 case Slot::PARAMETER:
700 case Slot::LOCAL:
701 if (mode == Variable::CONST) {
702 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
703 __ sw(t0, MemOperand(fp, SlotOffset(slot)));
704 } else if (function != NULL) {
705 VisitForAccumulatorValue(function);
706 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
707 }
708 break;
709
710 case Slot::CONTEXT:
711 // We bypass the general EmitSlotSearch because we know more about
712 // this specific context.
713
714 // The variable in the decl always resides in the current function
715 // context.
716 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
717 if (FLAG_debug_code) {
718 // Check that we're not inside a 'with'.
719 __ lw(a1, ContextOperand(cp, Context::FCONTEXT_INDEX));
720 __ Check(eq, "Unexpected declaration in current context.",
721 a1, Operand(cp));
722 }
723 if (mode == Variable::CONST) {
724 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
725 __ sw(at, ContextOperand(cp, slot->index()));
726 // No write barrier since the_hole_value is in old space.
727 } else if (function != NULL) {
728 VisitForAccumulatorValue(function);
729 __ sw(result_register(), ContextOperand(cp, slot->index()));
730 int offset = Context::SlotOffset(slot->index());
731 // We know that we have written a function, which is not a smi.
732 __ mov(a1, cp);
733 __ RecordWrite(a1, Operand(offset), a2, result_register());
734 }
735 break;
736
737 case Slot::LOOKUP: {
738 __ li(a2, Operand(variable->name()));
739 // Declaration nodes are always introduced in one of two modes.
740 ASSERT(mode == Variable::VAR ||
741 mode == Variable::CONST);
742 PropertyAttributes attr =
743 (mode == Variable::VAR) ? NONE : READ_ONLY;
744 __ li(a1, Operand(Smi::FromInt(attr)));
745 // Push initial value, if any.
746 // Note: For variables we must not push an initial value (such as
747 // 'undefined') because we may have a (legal) redeclaration and we
748 // must not destroy the current value.
749 if (mode == Variable::CONST) {
750 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
751 __ Push(cp, a2, a1, a0);
752 } else if (function != NULL) {
753 __ Push(cp, a2, a1);
754 // Push initial value for function declaration.
755 VisitForStackValue(function);
756 } else {
757 ASSERT(Smi::FromInt(0) == 0);
758 // No initial value!
759 __ mov(a0, zero_reg); // Operand(Smi::FromInt(0)));
760 __ Push(cp, a2, a1, a0);
761 }
762 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
763 break;
764 }
765 }
766
767 } else if (prop != NULL) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000768 // A const declaration aliasing a parameter is an illegal redeclaration.
769 ASSERT(mode != Variable::CONST);
770 if (function != NULL) {
771 // We are declaring a function that rewrites to a property.
772 // Use (keyed) IC to set the initial value. We cannot visit the
773 // rewrite because it's shared and we risk recording duplicate AST
774 // IDs for bailouts from optimized code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000775 ASSERT(prop->obj()->AsVariableProxy() != NULL);
776 { AccumulatorValueContext for_object(this);
777 EmitVariableLoad(prop->obj()->AsVariableProxy()->var());
778 }
danno@chromium.org40cb8782011-05-25 07:58:50 +0000779
780 __ push(result_register());
781 VisitForAccumulatorValue(function);
782 __ mov(a0, result_register());
783 __ pop(a2);
784
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000785 ASSERT(prop->key()->AsLiteral() != NULL &&
786 prop->key()->AsLiteral()->handle()->IsSmi());
787 __ li(a1, Operand(prop->key()->AsLiteral()->handle()));
788
789 Handle<Code> ic = is_strict_mode()
790 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
791 : isolate()->builtins()->KeyedStoreIC_Initialize();
792 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
793 // Value in v0 is ignored (declarations are statements).
794 }
795 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000796}
797
798
ager@chromium.org5c838252010-02-19 08:53:10 +0000799void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000800 EmitDeclaration(decl->proxy()->var(), decl->mode(), decl->fun());
ager@chromium.org5c838252010-02-19 08:53:10 +0000801}
802
803
804void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000805 // Call the runtime to declare the globals.
806 // The context is the first argument.
807 __ li(a2, Operand(pairs));
808 __ li(a1, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
809 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
810 __ Push(cp, a2, a1, a0);
811 __ CallRuntime(Runtime::kDeclareGlobals, 4);
812 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000813}
814
815
lrn@chromium.org7516f052011-03-30 08:52:27 +0000816void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000817 Comment cmnt(masm_, "[ SwitchStatement");
818 Breakable nested_statement(this, stmt);
819 SetStatementPosition(stmt);
820
821 // Keep the switch value on the stack until a case matches.
822 VisitForStackValue(stmt->tag());
823 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
824
825 ZoneList<CaseClause*>* clauses = stmt->cases();
826 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
827
828 Label next_test; // Recycled for each test.
829 // Compile all the tests with branches to their bodies.
830 for (int i = 0; i < clauses->length(); i++) {
831 CaseClause* clause = clauses->at(i);
832 clause->body_target()->Unuse();
833
834 // The default is not a test, but remember it as final fall through.
835 if (clause->is_default()) {
836 default_clause = clause;
837 continue;
838 }
839
840 Comment cmnt(masm_, "[ Case comparison");
841 __ bind(&next_test);
842 next_test.Unuse();
843
844 // Compile the label expression.
845 VisitForAccumulatorValue(clause->label());
846 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
847
848 // Perform the comparison as if via '==='.
849 __ lw(a1, MemOperand(sp, 0)); // Switch value.
850 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
851 JumpPatchSite patch_site(masm_);
852 if (inline_smi_code) {
853 Label slow_case;
854 __ or_(a2, a1, a0);
855 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
856
857 __ Branch(&next_test, ne, a1, Operand(a0));
858 __ Drop(1); // Switch value is no longer needed.
859 __ Branch(clause->body_target());
860
861 __ bind(&slow_case);
862 }
863
864 // Record position before stub call for type feedback.
865 SetSourcePosition(clause->position());
866 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
867 EmitCallIC(ic, &patch_site, clause->CompareId());
danno@chromium.org40cb8782011-05-25 07:58:50 +0000868
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000869 __ Branch(&next_test, ne, v0, Operand(zero_reg));
870 __ Drop(1); // Switch value is no longer needed.
871 __ Branch(clause->body_target());
872 }
873
874 // Discard the test value and jump to the default if present, otherwise to
875 // the end of the statement.
876 __ bind(&next_test);
877 __ Drop(1); // Switch value is no longer needed.
878 if (default_clause == NULL) {
879 __ Branch(nested_statement.break_target());
880 } else {
881 __ Branch(default_clause->body_target());
882 }
883
884 // Compile all the case bodies.
885 for (int i = 0; i < clauses->length(); i++) {
886 Comment cmnt(masm_, "[ Case body");
887 CaseClause* clause = clauses->at(i);
888 __ bind(clause->body_target());
889 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
890 VisitStatements(clause->statements());
891 }
892
893 __ bind(nested_statement.break_target());
894 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000895}
896
897
898void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000899 Comment cmnt(masm_, "[ ForInStatement");
900 SetStatementPosition(stmt);
901
902 Label loop, exit;
903 ForIn loop_statement(this, stmt);
904 increment_loop_depth();
905
906 // Get the object to enumerate over. Both SpiderMonkey and JSC
907 // ignore null and undefined in contrast to the specification; see
908 // ECMA-262 section 12.6.4.
909 VisitForAccumulatorValue(stmt->enumerable());
910 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
911 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
912 __ Branch(&exit, eq, a0, Operand(at));
913 Register null_value = t1;
914 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
915 __ Branch(&exit, eq, a0, Operand(null_value));
916
917 // Convert the object to a JS object.
918 Label convert, done_convert;
919 __ JumpIfSmi(a0, &convert);
920 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000921 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000922 __ bind(&convert);
923 __ push(a0);
924 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
925 __ mov(a0, v0);
926 __ bind(&done_convert);
927 __ push(a0);
928
929 // Check cache validity in generated code. This is a fast case for
930 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
931 // guarantee cache validity, call the runtime system to check cache
932 // validity or get the property names in a fixed array.
933 Label next, call_runtime;
934 // Preload a couple of values used in the loop.
935 Register empty_fixed_array_value = t2;
936 __ LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
937 Register empty_descriptor_array_value = t3;
938 __ LoadRoot(empty_descriptor_array_value,
939 Heap::kEmptyDescriptorArrayRootIndex);
940 __ mov(a1, a0);
941 __ bind(&next);
942
943 // Check that there are no elements. Register a1 contains the
944 // current JS object we've reached through the prototype chain.
945 __ lw(a2, FieldMemOperand(a1, JSObject::kElementsOffset));
946 __ Branch(&call_runtime, ne, a2, Operand(empty_fixed_array_value));
947
948 // Check that instance descriptors are not empty so that we can
949 // check for an enum cache. Leave the map in a2 for the subsequent
950 // prototype load.
951 __ lw(a2, FieldMemOperand(a1, HeapObject::kMapOffset));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000952 __ lw(a3, FieldMemOperand(a2, Map::kInstanceDescriptorsOrBitField3Offset));
953 __ JumpIfSmi(a3, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000954
955 // Check that there is an enum cache in the non-empty instance
956 // descriptors (a3). This is the case if the next enumeration
957 // index field does not contain a smi.
958 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumerationIndexOffset));
959 __ JumpIfSmi(a3, &call_runtime);
960
961 // For all objects but the receiver, check that the cache is empty.
962 Label check_prototype;
963 __ Branch(&check_prototype, eq, a1, Operand(a0));
964 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumCacheBridgeCacheOffset));
965 __ Branch(&call_runtime, ne, a3, Operand(empty_fixed_array_value));
966
967 // Load the prototype from the map and loop if non-null.
968 __ bind(&check_prototype);
969 __ lw(a1, FieldMemOperand(a2, Map::kPrototypeOffset));
970 __ Branch(&next, ne, a1, Operand(null_value));
971
972 // The enum cache is valid. Load the map of the object being
973 // iterated over and use the cache for the iteration.
974 Label use_cache;
975 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
976 __ Branch(&use_cache);
977
978 // Get the set of properties to enumerate.
979 __ bind(&call_runtime);
980 __ push(a0); // Duplicate the enumerable object on the stack.
981 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
982
983 // If we got a map from the runtime call, we can do a fast
984 // modification check. Otherwise, we got a fixed array, and we have
985 // to do a slow check.
986 Label fixed_array;
987 __ mov(a2, v0);
988 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
989 __ LoadRoot(at, Heap::kMetaMapRootIndex);
990 __ Branch(&fixed_array, ne, a1, Operand(at));
991
992 // We got a map in register v0. Get the enumeration cache from it.
993 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000994 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000995 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
996 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
997
998 // Setup the four remaining stack slots.
999 __ push(v0); // Map.
1000 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
1001 __ li(a0, Operand(Smi::FromInt(0)));
1002 // Push enumeration cache, enumeration cache length (as smi) and zero.
1003 __ Push(a2, a1, a0);
1004 __ jmp(&loop);
1005
1006 // We got a fixed array in register v0. Iterate through that.
1007 __ bind(&fixed_array);
1008 __ li(a1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
1009 __ Push(a1, v0);
1010 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1011 __ li(a0, Operand(Smi::FromInt(0)));
1012 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1013
1014 // Generate code for doing the condition check.
1015 __ bind(&loop);
1016 // Load the current count to a0, load the length to a1.
1017 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1018 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
1019 __ Branch(loop_statement.break_target(), hs, a0, Operand(a1));
1020
1021 // Get the current entry of the array into register a3.
1022 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1023 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1024 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1025 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1026 __ lw(a3, MemOperand(t0)); // Current entry.
1027
1028 // Get the expected map from the stack or a zero map in the
1029 // permanent slow case into register a2.
1030 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1031
1032 // Check if the expected map still matches that of the enumerable.
1033 // If not, we have to filter the key.
1034 Label update_each;
1035 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1036 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1037 __ Branch(&update_each, eq, t0, Operand(a2));
1038
1039 // Convert the entry to a string or (smi) 0 if it isn't a property
1040 // any more. If the property has been removed while iterating, we
1041 // just skip it.
1042 __ push(a1); // Enumerable.
1043 __ push(a3); // Current entry.
1044 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1045 __ mov(a3, result_register());
1046 __ Branch(loop_statement.continue_target(), eq, a3, Operand(zero_reg));
1047
1048 // Update the 'each' property or variable from the possibly filtered
1049 // entry in register a3.
1050 __ bind(&update_each);
1051 __ mov(result_register(), a3);
1052 // Perform the assignment as if via '='.
1053 { EffectContext context(this);
1054 EmitAssignment(stmt->each(), stmt->AssignmentId());
1055 }
1056
1057 // Generate code for the body of the loop.
1058 Visit(stmt->body());
1059
1060 // Generate code for the going to the next element by incrementing
1061 // the index (smi) stored on top of the stack.
1062 __ bind(loop_statement.continue_target());
1063 __ pop(a0);
1064 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1065 __ push(a0);
1066
1067 EmitStackCheck(stmt);
1068 __ Branch(&loop);
1069
1070 // Remove the pointers stored on the stack.
1071 __ bind(loop_statement.break_target());
1072 __ Drop(5);
1073
1074 // Exit and decrement the loop depth.
1075 __ bind(&exit);
1076 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001077}
1078
1079
1080void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1081 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001082 // Use the fast case closure allocation code that allocates in new
1083 // space for nested functions that don't need literals cloning. If
1084 // we're running with the --always-opt or the --prepare-always-opt
1085 // flag, we need to use the runtime function so that the new function
1086 // we are creating here gets a chance to have its code optimized and
1087 // doesn't just get a copy of the existing unoptimized code.
1088 if (!FLAG_always_opt &&
1089 !FLAG_prepare_always_opt &&
1090 !pretenure &&
1091 scope()->is_function_scope() &&
1092 info->num_literals() == 0) {
1093 FastNewClosureStub stub(info->strict_mode() ? kStrictMode : kNonStrictMode);
1094 __ li(a0, Operand(info));
1095 __ push(a0);
1096 __ CallStub(&stub);
1097 } else {
1098 __ li(a0, Operand(info));
1099 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1100 : Heap::kFalseValueRootIndex);
1101 __ Push(cp, a0, a1);
1102 __ CallRuntime(Runtime::kNewClosure, 3);
1103 }
1104 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001105}
1106
1107
1108void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001109 Comment cmnt(masm_, "[ VariableProxy");
1110 EmitVariableLoad(expr->var());
1111}
1112
1113
1114void FullCodeGenerator::EmitLoadGlobalSlotCheckExtensions(
1115 Slot* slot,
1116 TypeofState typeof_state,
1117 Label* slow) {
1118 Register current = cp;
1119 Register next = a1;
1120 Register temp = a2;
1121
1122 Scope* s = scope();
1123 while (s != NULL) {
1124 if (s->num_heap_slots() > 0) {
1125 if (s->calls_eval()) {
1126 // Check that extension is NULL.
1127 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1128 __ Branch(slow, ne, temp, Operand(zero_reg));
1129 }
1130 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001131 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001132 // Walk the rest of the chain without clobbering cp.
1133 current = next;
1134 }
1135 // If no outer scope calls eval, we do not need to check more
1136 // context extensions.
1137 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
1138 s = s->outer_scope();
1139 }
1140
1141 if (s->is_eval_scope()) {
1142 Label loop, fast;
1143 if (!current.is(next)) {
1144 __ Move(next, current);
1145 }
1146 __ bind(&loop);
1147 // Terminate at global context.
1148 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1149 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1150 __ Branch(&fast, eq, temp, Operand(t0));
1151 // Check that extension is NULL.
1152 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1153 __ Branch(slow, ne, temp, Operand(zero_reg));
1154 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001155 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001156 __ Branch(&loop);
1157 __ bind(&fast);
1158 }
1159
1160 __ lw(a0, GlobalObjectOperand());
1161 __ li(a2, Operand(slot->var()->name()));
1162 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1163 ? RelocInfo::CODE_TARGET
1164 : RelocInfo::CODE_TARGET_CONTEXT;
1165 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1166 EmitCallIC(ic, mode, AstNode::kNoNumber);
ager@chromium.org5c838252010-02-19 08:53:10 +00001167}
1168
1169
lrn@chromium.org7516f052011-03-30 08:52:27 +00001170MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(
1171 Slot* slot,
1172 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001173 ASSERT(slot->type() == Slot::CONTEXT);
1174 Register context = cp;
1175 Register next = a3;
1176 Register temp = t0;
1177
1178 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
1179 if (s->num_heap_slots() > 0) {
1180 if (s->calls_eval()) {
1181 // Check that extension is NULL.
1182 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1183 __ Branch(slow, ne, temp, Operand(zero_reg));
1184 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001185 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001186 // Walk the rest of the chain without clobbering cp.
1187 context = next;
1188 }
1189 }
1190 // Check that last extension is NULL.
1191 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1192 __ Branch(slow, ne, temp, Operand(zero_reg));
1193
1194 // This function is used only for loads, not stores, so it's safe to
1195 // return an cp-based operand (the write barrier cannot be allowed to
1196 // destroy the cp register).
1197 return ContextOperand(context, slot->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001198}
1199
1200
1201void FullCodeGenerator::EmitDynamicLoadFromSlotFastCase(
1202 Slot* slot,
1203 TypeofState typeof_state,
1204 Label* slow,
1205 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001206 // Generate fast-case code for variables that might be shadowed by
1207 // eval-introduced variables. Eval is used a lot without
1208 // introducing variables. In those cases, we do not want to
1209 // perform a runtime call for all variables in the scope
1210 // containing the eval.
1211 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
1212 EmitLoadGlobalSlotCheckExtensions(slot, typeof_state, slow);
1213 __ Branch(done);
1214 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
1215 Slot* potential_slot = slot->var()->local_if_not_shadowed()->AsSlot();
1216 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
1217 if (potential_slot != NULL) {
1218 // Generate fast case for locals that rewrite to slots.
1219 __ lw(v0, ContextSlotOperandCheckExtensions(potential_slot, slow));
1220 if (potential_slot->var()->mode() == Variable::CONST) {
1221 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1222 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1223 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1224 __ movz(v0, a0, at); // Conditional move.
1225 }
1226 __ Branch(done);
1227 } else if (rewrite != NULL) {
1228 // Generate fast case for calls of an argument function.
1229 Property* property = rewrite->AsProperty();
1230 if (property != NULL) {
1231 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
1232 Literal* key_literal = property->key()->AsLiteral();
1233 if (obj_proxy != NULL &&
1234 key_literal != NULL &&
1235 obj_proxy->IsArguments() &&
1236 key_literal->handle()->IsSmi()) {
1237 // Load arguments object if there are no eval-introduced
1238 // variables. Then load the argument from the arguments
1239 // object using keyed load.
1240 __ lw(a1,
1241 ContextSlotOperandCheckExtensions(obj_proxy->var()->AsSlot(),
1242 slow));
1243 __ li(a0, Operand(key_literal->handle()));
1244 Handle<Code> ic =
1245 isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001246 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(property));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001247 __ Branch(done);
1248 }
1249 }
1250 }
1251 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001252}
1253
1254
1255void FullCodeGenerator::EmitVariableLoad(Variable* var) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001256 // Four cases: non-this global variables, lookup slots, all other
1257 // types of slots, and parameters that rewrite to explicit property
1258 // accesses on the arguments object.
1259 Slot* slot = var->AsSlot();
1260 Property* property = var->AsProperty();
1261
1262 if (var->is_global() && !var->is_this()) {
1263 Comment cmnt(masm_, "Global variable");
1264 // Use inline caching. Variable name is passed in a2 and the global
1265 // object (receiver) in a0.
1266 __ lw(a0, GlobalObjectOperand());
1267 __ li(a2, Operand(var->name()));
1268 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1269 EmitCallIC(ic, RelocInfo::CODE_TARGET_CONTEXT, AstNode::kNoNumber);
1270 context()->Plug(v0);
1271
1272 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
1273 Label done, slow;
1274
1275 // Generate code for loading from variables potentially shadowed
1276 // by eval-introduced variables.
1277 EmitDynamicLoadFromSlotFastCase(slot, NOT_INSIDE_TYPEOF, &slow, &done);
1278
1279 __ bind(&slow);
1280 Comment cmnt(masm_, "Lookup slot");
1281 __ li(a1, Operand(var->name()));
1282 __ Push(cp, a1); // Context and name.
1283 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1284 __ bind(&done);
1285
1286 context()->Plug(v0);
1287
1288 } else if (slot != NULL) {
1289 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1290 ? "Context slot"
1291 : "Stack slot");
1292 if (var->mode() == Variable::CONST) {
1293 // Constants may be the hole value if they have not been initialized.
1294 // Unhole them.
1295 MemOperand slot_operand = EmitSlotSearch(slot, a0);
1296 __ lw(v0, slot_operand);
1297 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1298 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1299 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1300 __ movz(v0, a0, at); // Conditional move.
1301 context()->Plug(v0);
1302 } else {
1303 context()->Plug(slot);
1304 }
1305 } else {
1306 Comment cmnt(masm_, "Rewritten parameter");
1307 ASSERT_NOT_NULL(property);
1308 // Rewritten parameter accesses are of the form "slot[literal]".
1309 // Assert that the object is in a slot.
1310 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
1311 ASSERT_NOT_NULL(object_var);
1312 Slot* object_slot = object_var->AsSlot();
1313 ASSERT_NOT_NULL(object_slot);
1314
1315 // Load the object.
1316 Move(a1, object_slot);
1317
1318 // Assert that the key is a smi.
1319 Literal* key_literal = property->key()->AsLiteral();
1320 ASSERT_NOT_NULL(key_literal);
1321 ASSERT(key_literal->handle()->IsSmi());
1322
1323 // Load the key.
1324 __ li(a0, Operand(key_literal->handle()));
1325
1326 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1327 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001328 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(property));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001329 context()->Plug(v0);
1330 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001331}
1332
1333
1334void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001335 Comment cmnt(masm_, "[ RegExpLiteral");
1336 Label materialized;
1337 // Registers will be used as follows:
1338 // t1 = materialized value (RegExp literal)
1339 // t0 = JS function, literals array
1340 // a3 = literal index
1341 // a2 = RegExp pattern
1342 // a1 = RegExp flags
1343 // a0 = RegExp literal clone
1344 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1345 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1346 int literal_offset =
1347 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1348 __ lw(t1, FieldMemOperand(t0, literal_offset));
1349 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1350 __ Branch(&materialized, ne, t1, Operand(at));
1351
1352 // Create regexp literal using runtime function.
1353 // Result will be in v0.
1354 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1355 __ li(a2, Operand(expr->pattern()));
1356 __ li(a1, Operand(expr->flags()));
1357 __ Push(t0, a3, a2, a1);
1358 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1359 __ mov(t1, v0);
1360
1361 __ bind(&materialized);
1362 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1363 Label allocated, runtime_allocate;
1364 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1365 __ jmp(&allocated);
1366
1367 __ bind(&runtime_allocate);
1368 __ push(t1);
1369 __ li(a0, Operand(Smi::FromInt(size)));
1370 __ push(a0);
1371 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1372 __ pop(t1);
1373
1374 __ bind(&allocated);
1375
1376 // After this, registers are used as follows:
1377 // v0: Newly allocated regexp.
1378 // t1: Materialized regexp.
1379 // a2: temp.
1380 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1381 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001382}
1383
1384
1385void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001386 Comment cmnt(masm_, "[ ObjectLiteral");
1387 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1388 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1389 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1390 __ li(a1, Operand(expr->constant_properties()));
1391 int flags = expr->fast_elements()
1392 ? ObjectLiteral::kFastElements
1393 : ObjectLiteral::kNoFlags;
1394 flags |= expr->has_function()
1395 ? ObjectLiteral::kHasFunction
1396 : ObjectLiteral::kNoFlags;
1397 __ li(a0, Operand(Smi::FromInt(flags)));
1398 __ Push(a3, a2, a1, a0);
1399 if (expr->depth() > 1) {
1400 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1401 } else {
1402 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
1403 }
1404
1405 // If result_saved is true the result is on top of the stack. If
1406 // result_saved is false the result is in v0.
1407 bool result_saved = false;
1408
1409 // Mark all computed expressions that are bound to a key that
1410 // is shadowed by a later occurrence of the same key. For the
1411 // marked expressions, no store code is emitted.
1412 expr->CalculateEmitStore();
1413
1414 for (int i = 0; i < expr->properties()->length(); i++) {
1415 ObjectLiteral::Property* property = expr->properties()->at(i);
1416 if (property->IsCompileTimeValue()) continue;
1417
1418 Literal* key = property->key();
1419 Expression* value = property->value();
1420 if (!result_saved) {
1421 __ push(v0); // Save result on stack.
1422 result_saved = true;
1423 }
1424 switch (property->kind()) {
1425 case ObjectLiteral::Property::CONSTANT:
1426 UNREACHABLE();
1427 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1428 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1429 // Fall through.
1430 case ObjectLiteral::Property::COMPUTED:
1431 if (key->handle()->IsSymbol()) {
1432 if (property->emit_store()) {
1433 VisitForAccumulatorValue(value);
1434 __ mov(a0, result_register());
1435 __ li(a2, Operand(key->handle()));
1436 __ lw(a1, MemOperand(sp));
1437 Handle<Code> ic = is_strict_mode()
1438 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1439 : isolate()->builtins()->StoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001440 EmitCallIC(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001441 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1442 } else {
1443 VisitForEffect(value);
1444 }
1445 break;
1446 }
1447 // Fall through.
1448 case ObjectLiteral::Property::PROTOTYPE:
1449 // Duplicate receiver on stack.
1450 __ lw(a0, MemOperand(sp));
1451 __ push(a0);
1452 VisitForStackValue(key);
1453 VisitForStackValue(value);
1454 if (property->emit_store()) {
1455 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1456 __ push(a0);
1457 __ CallRuntime(Runtime::kSetProperty, 4);
1458 } else {
1459 __ Drop(3);
1460 }
1461 break;
1462 case ObjectLiteral::Property::GETTER:
1463 case ObjectLiteral::Property::SETTER:
1464 // Duplicate receiver on stack.
1465 __ lw(a0, MemOperand(sp));
1466 __ push(a0);
1467 VisitForStackValue(key);
1468 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1469 Smi::FromInt(1) :
1470 Smi::FromInt(0)));
1471 __ push(a1);
1472 VisitForStackValue(value);
1473 __ CallRuntime(Runtime::kDefineAccessor, 4);
1474 break;
1475 }
1476 }
1477
1478 if (expr->has_function()) {
1479 ASSERT(result_saved);
1480 __ lw(a0, MemOperand(sp));
1481 __ push(a0);
1482 __ CallRuntime(Runtime::kToFastProperties, 1);
1483 }
1484
1485 if (result_saved) {
1486 context()->PlugTOS();
1487 } else {
1488 context()->Plug(v0);
1489 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001490}
1491
1492
1493void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001494 Comment cmnt(masm_, "[ ArrayLiteral");
1495
1496 ZoneList<Expression*>* subexprs = expr->values();
1497 int length = subexprs->length();
1498 __ mov(a0, result_register());
1499 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1500 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1501 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1502 __ li(a1, Operand(expr->constant_elements()));
1503 __ Push(a3, a2, a1);
1504 if (expr->constant_elements()->map() ==
1505 isolate()->heap()->fixed_cow_array_map()) {
1506 FastCloneShallowArrayStub stub(
1507 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1508 __ CallStub(&stub);
1509 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1510 1, a1, a2);
1511 } else if (expr->depth() > 1) {
1512 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1513 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1514 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1515 } else {
1516 FastCloneShallowArrayStub stub(
1517 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
1518 __ CallStub(&stub);
1519 }
1520
1521 bool result_saved = false; // Is the result saved to the stack?
1522
1523 // Emit code to evaluate all the non-constant subexpressions and to store
1524 // them into the newly cloned array.
1525 for (int i = 0; i < length; i++) {
1526 Expression* subexpr = subexprs->at(i);
1527 // If the subexpression is a literal or a simple materialized literal it
1528 // is already set in the cloned array.
1529 if (subexpr->AsLiteral() != NULL ||
1530 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1531 continue;
1532 }
1533
1534 if (!result_saved) {
1535 __ push(v0);
1536 result_saved = true;
1537 }
1538 VisitForAccumulatorValue(subexpr);
1539
1540 // Store the subexpression value in the array's elements.
1541 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1542 __ lw(a1, FieldMemOperand(a1, JSObject::kElementsOffset));
1543 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1544 __ sw(result_register(), FieldMemOperand(a1, offset));
1545
1546 // Update the write barrier for the array store with v0 as the scratch
1547 // register.
1548 __ li(a2, Operand(offset));
1549 // TODO(PJ): double check this RecordWrite call.
1550 __ RecordWrite(a1, a2, result_register());
1551
1552 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1553 }
1554
1555 if (result_saved) {
1556 context()->PlugTOS();
1557 } else {
1558 context()->Plug(v0);
1559 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001560}
1561
1562
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001563void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001564 Comment cmnt(masm_, "[ Assignment");
1565 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1566 // on the left-hand side.
1567 if (!expr->target()->IsValidLeftHandSide()) {
1568 VisitForEffect(expr->target());
1569 return;
1570 }
1571
1572 // Left-hand side can only be a property, a global or a (parameter or local)
1573 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1574 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1575 LhsKind assign_type = VARIABLE;
1576 Property* property = expr->target()->AsProperty();
1577 if (property != NULL) {
1578 assign_type = (property->key()->IsPropertyName())
1579 ? NAMED_PROPERTY
1580 : KEYED_PROPERTY;
1581 }
1582
1583 // Evaluate LHS expression.
1584 switch (assign_type) {
1585 case VARIABLE:
1586 // Nothing to do here.
1587 break;
1588 case NAMED_PROPERTY:
1589 if (expr->is_compound()) {
1590 // We need the receiver both on the stack and in the accumulator.
1591 VisitForAccumulatorValue(property->obj());
1592 __ push(result_register());
1593 } else {
1594 VisitForStackValue(property->obj());
1595 }
1596 break;
1597 case KEYED_PROPERTY:
1598 // We need the key and receiver on both the stack and in v0 and a1.
1599 if (expr->is_compound()) {
1600 if (property->is_arguments_access()) {
1601 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
1602 __ lw(v0, EmitSlotSearch(obj_proxy->var()->AsSlot(), v0));
1603 __ push(v0);
1604 __ li(v0, Operand(property->key()->AsLiteral()->handle()));
1605 } else {
1606 VisitForStackValue(property->obj());
1607 VisitForAccumulatorValue(property->key());
1608 }
1609 __ lw(a1, MemOperand(sp, 0));
1610 __ push(v0);
1611 } else {
1612 if (property->is_arguments_access()) {
1613 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
1614 __ lw(a1, EmitSlotSearch(obj_proxy->var()->AsSlot(), v0));
1615 __ li(v0, Operand(property->key()->AsLiteral()->handle()));
1616 __ Push(a1, v0);
1617 } else {
1618 VisitForStackValue(property->obj());
1619 VisitForStackValue(property->key());
1620 }
1621 }
1622 break;
1623 }
1624
1625 // For compound assignments we need another deoptimization point after the
1626 // variable/property load.
1627 if (expr->is_compound()) {
1628 { AccumulatorValueContext context(this);
1629 switch (assign_type) {
1630 case VARIABLE:
1631 EmitVariableLoad(expr->target()->AsVariableProxy()->var());
1632 PrepareForBailout(expr->target(), TOS_REG);
1633 break;
1634 case NAMED_PROPERTY:
1635 EmitNamedPropertyLoad(property);
1636 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1637 break;
1638 case KEYED_PROPERTY:
1639 EmitKeyedPropertyLoad(property);
1640 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1641 break;
1642 }
1643 }
1644
1645 Token::Value op = expr->binary_op();
1646 __ push(v0); // Left operand goes on the stack.
1647 VisitForAccumulatorValue(expr->value());
1648
1649 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1650 ? OVERWRITE_RIGHT
1651 : NO_OVERWRITE;
1652 SetSourcePosition(expr->position() + 1);
1653 AccumulatorValueContext context(this);
1654 if (ShouldInlineSmiCase(op)) {
1655 EmitInlineSmiBinaryOp(expr->binary_operation(),
1656 op,
1657 mode,
1658 expr->target(),
1659 expr->value());
1660 } else {
1661 EmitBinaryOp(expr->binary_operation(), op, mode);
1662 }
1663
1664 // Deoptimization point in case the binary operation may have side effects.
1665 PrepareForBailout(expr->binary_operation(), TOS_REG);
1666 } else {
1667 VisitForAccumulatorValue(expr->value());
1668 }
1669
1670 // Record source position before possible IC call.
1671 SetSourcePosition(expr->position());
1672
1673 // Store the value.
1674 switch (assign_type) {
1675 case VARIABLE:
1676 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1677 expr->op());
1678 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1679 context()->Plug(v0);
1680 break;
1681 case NAMED_PROPERTY:
1682 EmitNamedPropertyAssignment(expr);
1683 break;
1684 case KEYED_PROPERTY:
1685 EmitKeyedPropertyAssignment(expr);
1686 break;
1687 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001688}
1689
1690
ager@chromium.org5c838252010-02-19 08:53:10 +00001691void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001692 SetSourcePosition(prop->position());
1693 Literal* key = prop->key()->AsLiteral();
1694 __ mov(a0, result_register());
1695 __ li(a2, Operand(key->handle()));
1696 // Call load IC. It has arguments receiver and property name a0 and a2.
1697 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001698 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001699}
1700
1701
1702void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001703 SetSourcePosition(prop->position());
1704 __ mov(a0, result_register());
1705 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1706 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001707 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001708}
1709
1710
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001711void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001712 Token::Value op,
1713 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001714 Expression* left_expr,
1715 Expression* right_expr) {
1716 Label done, smi_case, stub_call;
1717
1718 Register scratch1 = a2;
1719 Register scratch2 = a3;
1720
1721 // Get the arguments.
1722 Register left = a1;
1723 Register right = a0;
1724 __ pop(left);
1725 __ mov(a0, result_register());
1726
1727 // Perform combined smi check on both operands.
1728 __ Or(scratch1, left, Operand(right));
1729 STATIC_ASSERT(kSmiTag == 0);
1730 JumpPatchSite patch_site(masm_);
1731 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1732
1733 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001734 BinaryOpStub stub(op, mode);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001735 EmitCallIC(stub.GetCode(), &patch_site, expr->id());
1736 __ jmp(&done);
1737
1738 __ bind(&smi_case);
1739 // Smi case. This code works the same way as the smi-smi case in the type
1740 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001741 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001742 switch (op) {
1743 case Token::SAR:
1744 __ Branch(&stub_call);
1745 __ GetLeastBitsFromSmi(scratch1, right, 5);
1746 __ srav(right, left, scratch1);
1747 __ And(v0, right, Operand(~kSmiTagMask));
1748 break;
1749 case Token::SHL: {
1750 __ Branch(&stub_call);
1751 __ SmiUntag(scratch1, left);
1752 __ GetLeastBitsFromSmi(scratch2, right, 5);
1753 __ sllv(scratch1, scratch1, scratch2);
1754 __ Addu(scratch2, scratch1, Operand(0x40000000));
1755 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1756 __ SmiTag(v0, scratch1);
1757 break;
1758 }
1759 case Token::SHR: {
1760 __ Branch(&stub_call);
1761 __ SmiUntag(scratch1, left);
1762 __ GetLeastBitsFromSmi(scratch2, right, 5);
1763 __ srlv(scratch1, scratch1, scratch2);
1764 __ And(scratch2, scratch1, 0xc0000000);
1765 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1766 __ SmiTag(v0, scratch1);
1767 break;
1768 }
1769 case Token::ADD:
1770 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1771 __ BranchOnOverflow(&stub_call, scratch1);
1772 break;
1773 case Token::SUB:
1774 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1775 __ BranchOnOverflow(&stub_call, scratch1);
1776 break;
1777 case Token::MUL: {
1778 __ SmiUntag(scratch1, right);
1779 __ Mult(left, scratch1);
1780 __ mflo(scratch1);
1781 __ mfhi(scratch2);
1782 __ sra(scratch1, scratch1, 31);
1783 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1784 __ mflo(v0);
1785 __ Branch(&done, ne, v0, Operand(zero_reg));
1786 __ Addu(scratch2, right, left);
1787 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1788 ASSERT(Smi::FromInt(0) == 0);
1789 __ mov(v0, zero_reg);
1790 break;
1791 }
1792 case Token::BIT_OR:
1793 __ Or(v0, left, Operand(right));
1794 break;
1795 case Token::BIT_AND:
1796 __ And(v0, left, Operand(right));
1797 break;
1798 case Token::BIT_XOR:
1799 __ Xor(v0, left, Operand(right));
1800 break;
1801 default:
1802 UNREACHABLE();
1803 }
1804
1805 __ bind(&done);
1806 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001807}
1808
1809
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001810void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1811 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001812 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001813 __ mov(a0, result_register());
1814 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001815 BinaryOpStub stub(op, mode);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001816 EmitCallIC(stub.GetCode(), NULL, expr->id());
1817 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001818}
1819
1820
1821void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001822 // Invalid left-hand sides are rewritten to have a 'throw
1823 // ReferenceError' on the left-hand side.
1824 if (!expr->IsValidLeftHandSide()) {
1825 VisitForEffect(expr);
1826 return;
1827 }
1828
1829 // Left-hand side can only be a property, a global or a (parameter or local)
1830 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1831 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1832 LhsKind assign_type = VARIABLE;
1833 Property* prop = expr->AsProperty();
1834 if (prop != NULL) {
1835 assign_type = (prop->key()->IsPropertyName())
1836 ? NAMED_PROPERTY
1837 : KEYED_PROPERTY;
1838 }
1839
1840 switch (assign_type) {
1841 case VARIABLE: {
1842 Variable* var = expr->AsVariableProxy()->var();
1843 EffectContext context(this);
1844 EmitVariableAssignment(var, Token::ASSIGN);
1845 break;
1846 }
1847 case NAMED_PROPERTY: {
1848 __ push(result_register()); // Preserve value.
1849 VisitForAccumulatorValue(prop->obj());
1850 __ mov(a1, result_register());
1851 __ pop(a0); // Restore value.
1852 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1853 Handle<Code> ic = is_strict_mode()
1854 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1855 : isolate()->builtins()->StoreIC_Initialize();
1856 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
1857 break;
1858 }
1859 case KEYED_PROPERTY: {
1860 __ push(result_register()); // Preserve value.
1861 if (prop->is_synthetic()) {
1862 ASSERT(prop->obj()->AsVariableProxy() != NULL);
1863 ASSERT(prop->key()->AsLiteral() != NULL);
1864 { AccumulatorValueContext for_object(this);
1865 EmitVariableLoad(prop->obj()->AsVariableProxy()->var());
1866 }
1867 __ mov(a2, result_register());
1868 __ li(a1, Operand(prop->key()->AsLiteral()->handle()));
1869 } else {
1870 VisitForStackValue(prop->obj());
1871 VisitForAccumulatorValue(prop->key());
1872 __ mov(a1, result_register());
1873 __ pop(a2);
1874 }
1875 __ pop(a0); // Restore value.
1876 Handle<Code> ic = is_strict_mode()
1877 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
1878 : isolate()->builtins()->KeyedStoreIC_Initialize();
1879 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
1880 break;
1881 }
1882 }
1883 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1884 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001885}
1886
1887
1888void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001889 Token::Value op) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001890 // Left-hand sides that rewrite to explicit property accesses do not reach
1891 // here.
1892 ASSERT(var != NULL);
1893 ASSERT(var->is_global() || var->AsSlot() != NULL);
1894
1895 if (var->is_global()) {
1896 ASSERT(!var->is_this());
1897 // Assignment to a global variable. Use inline caching for the
1898 // assignment. Right-hand-side value is passed in a0, variable name in
1899 // a2, and the global object in a1.
1900 __ mov(a0, result_register());
1901 __ li(a2, Operand(var->name()));
1902 __ lw(a1, GlobalObjectOperand());
1903 Handle<Code> ic = is_strict_mode()
1904 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1905 : isolate()->builtins()->StoreIC_Initialize();
1906 EmitCallIC(ic, RelocInfo::CODE_TARGET_CONTEXT, AstNode::kNoNumber);
1907
1908 } else if (op == Token::INIT_CONST) {
1909 // Like var declarations, const declarations are hoisted to function
1910 // scope. However, unlike var initializers, const initializers are able
1911 // to drill a hole to that function context, even from inside a 'with'
1912 // context. We thus bypass the normal static scope lookup.
1913 Slot* slot = var->AsSlot();
1914 Label skip;
1915 switch (slot->type()) {
1916 case Slot::PARAMETER:
1917 // No const parameters.
1918 UNREACHABLE();
1919 break;
1920 case Slot::LOCAL:
1921 // Detect const reinitialization by checking for the hole value.
1922 __ lw(a1, MemOperand(fp, SlotOffset(slot)));
1923 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1924 __ Branch(&skip, ne, a1, Operand(t0));
1925 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1926 break;
1927 case Slot::CONTEXT: {
1928 __ lw(a1, ContextOperand(cp, Context::FCONTEXT_INDEX));
1929 __ lw(a2, ContextOperand(a1, slot->index()));
1930 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1931 __ Branch(&skip, ne, a2, Operand(t0));
1932 __ sw(result_register(), ContextOperand(a1, slot->index()));
1933 int offset = Context::SlotOffset(slot->index());
1934 __ mov(a3, result_register()); // Preserve the stored value in v0.
1935 __ RecordWrite(a1, Operand(offset), a3, a2);
1936 break;
1937 }
1938 case Slot::LOOKUP:
1939 __ push(result_register());
1940 __ li(a0, Operand(slot->var()->name()));
1941 __ Push(cp, a0); // Context and name.
1942 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1943 break;
1944 }
1945 __ bind(&skip);
1946
1947 } else if (var->mode() != Variable::CONST) {
1948 // Perform the assignment for non-const variables. Const assignments
1949 // are simply skipped.
1950 Slot* slot = var->AsSlot();
1951 switch (slot->type()) {
1952 case Slot::PARAMETER:
1953 case Slot::LOCAL:
1954 // Perform the assignment.
1955 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1956 break;
1957
1958 case Slot::CONTEXT: {
1959 MemOperand target = EmitSlotSearch(slot, a1);
1960 // Perform the assignment and issue the write barrier.
1961 __ sw(result_register(), target);
1962 // RecordWrite may destroy all its register arguments.
1963 __ mov(a3, result_register());
1964 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
1965 __ RecordWrite(a1, Operand(offset), a2, a3);
1966 break;
1967 }
1968
1969 case Slot::LOOKUP:
1970 // Call the runtime for the assignment.
1971 __ push(v0); // Value.
1972 __ li(a1, Operand(slot->var()->name()));
1973 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1974 __ Push(cp, a1, a0); // Context, name, strict mode.
1975 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1976 break;
1977 }
1978 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001979}
1980
1981
1982void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001983 // Assignment to a property, using a named store IC.
1984 Property* prop = expr->target()->AsProperty();
1985 ASSERT(prop != NULL);
1986 ASSERT(prop->key()->AsLiteral() != NULL);
1987
1988 // If the assignment starts a block of assignments to the same object,
1989 // change to slow case to avoid the quadratic behavior of repeatedly
1990 // adding fast properties.
1991 if (expr->starts_initialization_block()) {
1992 __ push(result_register());
1993 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1994 __ push(t0);
1995 __ CallRuntime(Runtime::kToSlowProperties, 1);
1996 __ pop(result_register());
1997 }
1998
1999 // Record source code position before IC call.
2000 SetSourcePosition(expr->position());
2001 __ mov(a0, result_register()); // Load the value.
2002 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
2003 // Load receiver to a1. Leave a copy in the stack if needed for turning the
2004 // receiver into fast case.
2005 if (expr->ends_initialization_block()) {
2006 __ lw(a1, MemOperand(sp));
2007 } else {
2008 __ pop(a1);
2009 }
2010
2011 Handle<Code> ic = is_strict_mode()
2012 ? isolate()->builtins()->StoreIC_Initialize_Strict()
2013 : isolate()->builtins()->StoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00002014 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002015
2016 // If the assignment ends an initialization block, revert to fast case.
2017 if (expr->ends_initialization_block()) {
2018 __ push(v0); // Result of assignment, saved even if not needed.
2019 // Receiver is under the result value.
2020 __ lw(t0, MemOperand(sp, kPointerSize));
2021 __ push(t0);
2022 __ CallRuntime(Runtime::kToFastProperties, 1);
2023 __ pop(v0);
2024 __ Drop(1);
2025 }
2026 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2027 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002028}
2029
2030
2031void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002032 // Assignment to a property, using a keyed store IC.
2033
2034 // If the assignment starts a block of assignments to the same object,
2035 // change to slow case to avoid the quadratic behavior of repeatedly
2036 // adding fast properties.
2037 if (expr->starts_initialization_block()) {
2038 __ push(result_register());
2039 // Receiver is now under the key and value.
2040 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2041 __ push(t0);
2042 __ CallRuntime(Runtime::kToSlowProperties, 1);
2043 __ pop(result_register());
2044 }
2045
2046 // Record source code position before IC call.
2047 SetSourcePosition(expr->position());
2048 // Call keyed store IC.
2049 // The arguments are:
2050 // - a0 is the value,
2051 // - a1 is the key,
2052 // - a2 is the receiver.
2053 __ mov(a0, result_register());
2054 __ pop(a1); // Key.
2055 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2056 // receiver into fast case.
2057 if (expr->ends_initialization_block()) {
2058 __ lw(a2, MemOperand(sp));
2059 } else {
2060 __ pop(a2);
2061 }
2062
2063 Handle<Code> ic = is_strict_mode()
2064 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
2065 : isolate()->builtins()->KeyedStoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00002066 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002067
2068 // If the assignment ends an initialization block, revert to fast case.
2069 if (expr->ends_initialization_block()) {
2070 __ push(v0); // Result of assignment, saved even if not needed.
2071 // Receiver is under the result value.
2072 __ lw(t0, MemOperand(sp, kPointerSize));
2073 __ push(t0);
2074 __ CallRuntime(Runtime::kToFastProperties, 1);
2075 __ pop(v0);
2076 __ Drop(1);
2077 }
2078 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2079 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002080}
2081
2082
2083void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002084 Comment cmnt(masm_, "[ Property");
2085 Expression* key = expr->key();
2086
2087 if (key->IsPropertyName()) {
2088 VisitForAccumulatorValue(expr->obj());
2089 EmitNamedPropertyLoad(expr);
2090 context()->Plug(v0);
2091 } else {
2092 VisitForStackValue(expr->obj());
2093 VisitForAccumulatorValue(expr->key());
2094 __ pop(a1);
2095 EmitKeyedPropertyLoad(expr);
2096 context()->Plug(v0);
2097 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002098}
2099
lrn@chromium.org7516f052011-03-30 08:52:27 +00002100
ager@chromium.org5c838252010-02-19 08:53:10 +00002101void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002102 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002103 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002104 // Code common for calls using the IC.
2105 ZoneList<Expression*>* args = expr->arguments();
2106 int arg_count = args->length();
2107 { PreservePositionScope scope(masm()->positions_recorder());
2108 for (int i = 0; i < arg_count; i++) {
2109 VisitForStackValue(args->at(i));
2110 }
2111 __ li(a2, Operand(name));
2112 }
2113 // Record source position for debugger.
2114 SetSourcePosition(expr->position());
2115 // Call the IC initialization code.
2116 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2117 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00002118 isolate()->stub_cache()->ComputeCallInitialize(arg_count, in_loop, mode);
2119 EmitCallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002120 RecordJSReturnSite(expr);
2121 // Restore context register.
2122 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2123 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002124}
2125
2126
lrn@chromium.org7516f052011-03-30 08:52:27 +00002127void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002128 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002129 // Load the key.
2130 VisitForAccumulatorValue(key);
2131
2132 // Swap the name of the function and the receiver on the stack to follow
2133 // the calling convention for call ICs.
2134 __ pop(a1);
2135 __ push(v0);
2136 __ push(a1);
2137
2138 // Code common for calls using the IC.
2139 ZoneList<Expression*>* args = expr->arguments();
2140 int arg_count = args->length();
2141 { PreservePositionScope scope(masm()->positions_recorder());
2142 for (int i = 0; i < arg_count; i++) {
2143 VisitForStackValue(args->at(i));
2144 }
2145 }
2146 // Record source position for debugger.
2147 SetSourcePosition(expr->position());
2148 // Call the IC initialization code.
2149 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2150 Handle<Code> ic =
2151 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count, in_loop);
2152 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002153 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002154 RecordJSReturnSite(expr);
2155 // Restore context register.
2156 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2157 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002158}
2159
2160
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002161void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002162 // Code common for calls using the call stub.
2163 ZoneList<Expression*>* args = expr->arguments();
2164 int arg_count = args->length();
2165 { PreservePositionScope scope(masm()->positions_recorder());
2166 for (int i = 0; i < arg_count; i++) {
2167 VisitForStackValue(args->at(i));
2168 }
2169 }
2170 // Record source position for debugger.
2171 SetSourcePosition(expr->position());
2172 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2173 CallFunctionStub stub(arg_count, in_loop, flags);
2174 __ CallStub(&stub);
2175 RecordJSReturnSite(expr);
2176 // Restore context register.
2177 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2178 context()->DropAndPlug(1, v0);
2179}
2180
2181
2182void FullCodeGenerator::EmitResolvePossiblyDirectEval(ResolveEvalFlag flag,
2183 int arg_count) {
2184 // Push copy of the first argument or undefined if it doesn't exist.
2185 if (arg_count > 0) {
2186 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2187 } else {
2188 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2189 }
2190 __ push(a1);
2191
2192 // Push the receiver of the enclosing function and do runtime call.
2193 __ lw(a1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
2194 __ push(a1);
2195 // Push the strict mode flag.
2196 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
2197 __ push(a1);
2198
2199 __ CallRuntime(flag == SKIP_CONTEXT_LOOKUP
2200 ? Runtime::kResolvePossiblyDirectEvalNoLookup
2201 : Runtime::kResolvePossiblyDirectEval, 4);
ager@chromium.org5c838252010-02-19 08:53:10 +00002202}
2203
2204
2205void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002206#ifdef DEBUG
2207 // We want to verify that RecordJSReturnSite gets called on all paths
2208 // through this function. Avoid early returns.
2209 expr->return_is_recorded_ = false;
2210#endif
2211
2212 Comment cmnt(masm_, "[ Call");
2213 Expression* fun = expr->expression();
2214 Variable* var = fun->AsVariableProxy()->AsVariable();
2215
2216 if (var != NULL && var->is_possibly_eval()) {
2217 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2218 // resolve the function we need to call and the receiver of the
2219 // call. Then we call the resolved function using the given
2220 // arguments.
2221 ZoneList<Expression*>* args = expr->arguments();
2222 int arg_count = args->length();
2223
2224 { PreservePositionScope pos_scope(masm()->positions_recorder());
2225 VisitForStackValue(fun);
2226 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2227 __ push(a2); // Reserved receiver slot.
2228
2229 // Push the arguments.
2230 for (int i = 0; i < arg_count; i++) {
2231 VisitForStackValue(args->at(i));
2232 }
2233 // If we know that eval can only be shadowed by eval-introduced
2234 // variables we attempt to load the global eval function directly
2235 // in generated code. If we succeed, there is no need to perform a
2236 // context lookup in the runtime system.
2237 Label done;
2238 if (var->AsSlot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
2239 Label slow;
2240 EmitLoadGlobalSlotCheckExtensions(var->AsSlot(),
2241 NOT_INSIDE_TYPEOF,
2242 &slow);
2243 // Push the function and resolve eval.
2244 __ push(v0);
2245 EmitResolvePossiblyDirectEval(SKIP_CONTEXT_LOOKUP, arg_count);
2246 __ jmp(&done);
2247 __ bind(&slow);
2248 }
2249
2250 // Push copy of the function (found below the arguments) and
2251 // resolve eval.
2252 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2253 __ push(a1);
2254 EmitResolvePossiblyDirectEval(PERFORM_CONTEXT_LOOKUP, arg_count);
2255 if (done.is_linked()) {
2256 __ bind(&done);
2257 }
2258
2259 // The runtime call returns a pair of values in v0 (function) and
2260 // v1 (receiver). Touch up the stack with the right values.
2261 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2262 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2263 }
2264 // Record source position for debugger.
2265 SetSourcePosition(expr->position());
2266 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002267 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002268 __ CallStub(&stub);
2269 RecordJSReturnSite(expr);
2270 // Restore context register.
2271 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2272 context()->DropAndPlug(1, v0);
2273 } else if (var != NULL && !var->is_this() && var->is_global()) {
2274 // Push global object as receiver for the call IC.
2275 __ lw(a0, GlobalObjectOperand());
2276 __ push(a0);
2277 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
2278 } else if (var != NULL && var->AsSlot() != NULL &&
2279 var->AsSlot()->type() == Slot::LOOKUP) {
2280 // Call to a lookup slot (dynamically introduced variable).
2281 Label slow, done;
2282
2283 { PreservePositionScope scope(masm()->positions_recorder());
2284 // Generate code for loading from variables potentially shadowed
2285 // by eval-introduced variables.
2286 EmitDynamicLoadFromSlotFastCase(var->AsSlot(),
2287 NOT_INSIDE_TYPEOF,
2288 &slow,
2289 &done);
2290 }
2291
2292 __ bind(&slow);
2293 // Call the runtime to find the function to call (returned in v0)
2294 // and the object holding it (returned in v1).
2295 __ push(context_register());
2296 __ li(a2, Operand(var->name()));
2297 __ push(a2);
2298 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2299 __ Push(v0, v1); // Function, receiver.
2300
2301 // If fast case code has been generated, emit code to push the
2302 // function and receiver and have the slow path jump around this
2303 // code.
2304 if (done.is_linked()) {
2305 Label call;
2306 __ Branch(&call);
2307 __ bind(&done);
2308 // Push function.
2309 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002310 // The receiver is implicitly the global receiver. Indicate this
2311 // by passing the hole to the call function stub.
2312 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002313 __ push(a1);
2314 __ bind(&call);
2315 }
2316
danno@chromium.org40cb8782011-05-25 07:58:50 +00002317 // The receiver is either the global receiver or an object found
2318 // by LoadContextSlot. That object could be the hole if the
2319 // receiver is implicitly the global object.
2320 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002321 } else if (fun->AsProperty() != NULL) {
2322 // Call to an object property.
2323 Property* prop = fun->AsProperty();
2324 Literal* key = prop->key()->AsLiteral();
2325 if (key != NULL && key->handle()->IsSymbol()) {
2326 // Call to a named property, use call IC.
2327 { PreservePositionScope scope(masm()->positions_recorder());
2328 VisitForStackValue(prop->obj());
2329 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00002330 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002331 } else {
2332 // Call to a keyed property.
2333 // For a synthetic property use keyed load IC followed by function call,
2334 // for a regular property use keyed EmitCallIC.
2335 if (prop->is_synthetic()) {
2336 // Do not visit the object and key subexpressions (they are shared
2337 // by all occurrences of the same rewritten parameter).
2338 ASSERT(prop->obj()->AsVariableProxy() != NULL);
2339 ASSERT(prop->obj()->AsVariableProxy()->var()->AsSlot() != NULL);
2340 Slot* slot = prop->obj()->AsVariableProxy()->var()->AsSlot();
2341 MemOperand operand = EmitSlotSearch(slot, a1);
2342 __ lw(a1, operand);
2343
2344 ASSERT(prop->key()->AsLiteral() != NULL);
2345 ASSERT(prop->key()->AsLiteral()->handle()->IsSmi());
2346 __ li(a0, Operand(prop->key()->AsLiteral()->handle()));
2347
2348 // Record source code position for IC call.
2349 SetSourcePosition(prop->position());
2350
2351 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00002352 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002353 __ lw(a1, GlobalObjectOperand());
2354 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2355 __ Push(v0, a1); // Function, receiver.
2356 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2357 } else {
2358 { PreservePositionScope scope(masm()->positions_recorder());
2359 VisitForStackValue(prop->obj());
2360 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00002361 EmitKeyedCallWithIC(expr, prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002362 }
2363 }
2364 } else {
2365 { PreservePositionScope scope(masm()->positions_recorder());
2366 VisitForStackValue(fun);
2367 }
2368 // Load global receiver object.
2369 __ lw(a1, GlobalObjectOperand());
2370 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2371 __ push(a1);
2372 // Emit function call.
2373 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2374 }
2375
2376#ifdef DEBUG
2377 // RecordJSReturnSite should have been called.
2378 ASSERT(expr->return_is_recorded_);
2379#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002380}
2381
2382
2383void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002384 Comment cmnt(masm_, "[ CallNew");
2385 // According to ECMA-262, section 11.2.2, page 44, the function
2386 // expression in new calls must be evaluated before the
2387 // arguments.
2388
2389 // Push constructor on the stack. If it's not a function it's used as
2390 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2391 // ignored.
2392 VisitForStackValue(expr->expression());
2393
2394 // Push the arguments ("left-to-right") on the stack.
2395 ZoneList<Expression*>* args = expr->arguments();
2396 int arg_count = args->length();
2397 for (int i = 0; i < arg_count; i++) {
2398 VisitForStackValue(args->at(i));
2399 }
2400
2401 // Call the construct call builtin that handles allocation and
2402 // constructor invocation.
2403 SetSourcePosition(expr->position());
2404
2405 // Load function and argument count into a1 and a0.
2406 __ li(a0, Operand(arg_count));
2407 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2408
2409 Handle<Code> construct_builtin =
2410 isolate()->builtins()->JSConstructCall();
2411 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2412 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002413}
2414
2415
lrn@chromium.org7516f052011-03-30 08:52:27 +00002416void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002417 ASSERT(args->length() == 1);
2418
2419 VisitForAccumulatorValue(args->at(0));
2420
2421 Label materialize_true, materialize_false;
2422 Label* if_true = NULL;
2423 Label* if_false = NULL;
2424 Label* fall_through = NULL;
2425 context()->PrepareTest(&materialize_true, &materialize_false,
2426 &if_true, &if_false, &fall_through);
2427
2428 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2429 __ And(t0, v0, Operand(kSmiTagMask));
2430 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2431
2432 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002433}
2434
2435
2436void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002437 ASSERT(args->length() == 1);
2438
2439 VisitForAccumulatorValue(args->at(0));
2440
2441 Label materialize_true, materialize_false;
2442 Label* if_true = NULL;
2443 Label* if_false = NULL;
2444 Label* fall_through = NULL;
2445 context()->PrepareTest(&materialize_true, &materialize_false,
2446 &if_true, &if_false, &fall_through);
2447
2448 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2449 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2450 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2451
2452 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002453}
2454
2455
2456void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002457 ASSERT(args->length() == 1);
2458
2459 VisitForAccumulatorValue(args->at(0));
2460
2461 Label materialize_true, materialize_false;
2462 Label* if_true = NULL;
2463 Label* if_false = NULL;
2464 Label* fall_through = NULL;
2465 context()->PrepareTest(&materialize_true, &materialize_false,
2466 &if_true, &if_false, &fall_through);
2467
2468 __ JumpIfSmi(v0, if_false);
2469 __ LoadRoot(at, Heap::kNullValueRootIndex);
2470 __ Branch(if_true, eq, v0, Operand(at));
2471 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2472 // Undetectable objects behave like undefined when tested with typeof.
2473 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2474 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2475 __ Branch(if_false, ne, at, Operand(zero_reg));
2476 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002477 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002478 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002479 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2480 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002481
2482 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002483}
2484
2485
2486void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002487 ASSERT(args->length() == 1);
2488
2489 VisitForAccumulatorValue(args->at(0));
2490
2491 Label materialize_true, materialize_false;
2492 Label* if_true = NULL;
2493 Label* if_false = NULL;
2494 Label* fall_through = NULL;
2495 context()->PrepareTest(&materialize_true, &materialize_false,
2496 &if_true, &if_false, &fall_through);
2497
2498 __ JumpIfSmi(v0, if_false);
2499 __ GetObjectType(v0, a1, a1);
2500 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002501 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002502 if_true, if_false, fall_through);
2503
2504 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002505}
2506
2507
2508void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002509 ASSERT(args->length() == 1);
2510
2511 VisitForAccumulatorValue(args->at(0));
2512
2513 Label materialize_true, materialize_false;
2514 Label* if_true = NULL;
2515 Label* if_false = NULL;
2516 Label* fall_through = NULL;
2517 context()->PrepareTest(&materialize_true, &materialize_false,
2518 &if_true, &if_false, &fall_through);
2519
2520 __ JumpIfSmi(v0, if_false);
2521 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2522 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2523 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2524 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2525 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2526
2527 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002528}
2529
2530
2531void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2532 ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002533
2534 ASSERT(args->length() == 1);
2535
2536 VisitForAccumulatorValue(args->at(0));
2537
2538 Label materialize_true, materialize_false;
2539 Label* if_true = NULL;
2540 Label* if_false = NULL;
2541 Label* fall_through = NULL;
2542 context()->PrepareTest(&materialize_true, &materialize_false,
2543 &if_true, &if_false, &fall_through);
2544
2545 if (FLAG_debug_code) __ AbortIfSmi(v0);
2546
2547 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2548 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2549 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2550 __ Branch(if_true, ne, t0, Operand(zero_reg));
2551
2552 // Check for fast case object. Generate false result for slow case object.
2553 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2554 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2555 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2556 __ Branch(if_false, eq, a2, Operand(t0));
2557
2558 // Look for valueOf symbol in the descriptor array, and indicate false if
2559 // found. The type is not checked, so if it is a transition it is a false
2560 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002561 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002562 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2563 // t0: descriptor array
2564 // a3: length of descriptor array
2565 // Calculate the end of the descriptor array.
2566 STATIC_ASSERT(kSmiTag == 0);
2567 STATIC_ASSERT(kSmiTagSize == 1);
2568 STATIC_ASSERT(kPointerSize == 4);
2569 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2570 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2571 __ Addu(a2, a2, t1);
2572
2573 // Calculate location of the first key name.
2574 __ Addu(t0,
2575 t0,
2576 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2577 DescriptorArray::kFirstIndex * kPointerSize));
2578 // Loop through all the keys in the descriptor array. If one of these is the
2579 // symbol valueOf the result is false.
2580 Label entry, loop;
2581 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2582 // used in the loop below.
2583 __ li(t2, Operand(FACTORY->value_of_symbol()));
2584 __ jmp(&entry);
2585 __ bind(&loop);
2586 __ lw(a3, MemOperand(t0, 0));
2587 __ Branch(if_false, eq, a3, Operand(t2));
2588 __ Addu(t0, t0, Operand(kPointerSize));
2589 __ bind(&entry);
2590 __ Branch(&loop, ne, t0, Operand(a2));
2591
2592 // If a valueOf property is not found on the object check that it's
2593 // prototype is the un-modified String prototype. If not result is false.
2594 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2595 __ JumpIfSmi(a2, if_false);
2596 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2597 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2598 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2599 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2600 __ Branch(if_false, ne, a2, Operand(a3));
2601
2602 // Set the bit in the map to indicate that it has been checked safe for
2603 // default valueOf and set true result.
2604 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2605 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2606 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2607 __ jmp(if_true);
2608
2609 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2610 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002611}
2612
2613
2614void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002615 ASSERT(args->length() == 1);
2616
2617 VisitForAccumulatorValue(args->at(0));
2618
2619 Label materialize_true, materialize_false;
2620 Label* if_true = NULL;
2621 Label* if_false = NULL;
2622 Label* fall_through = NULL;
2623 context()->PrepareTest(&materialize_true, &materialize_false,
2624 &if_true, &if_false, &fall_through);
2625
2626 __ JumpIfSmi(v0, if_false);
2627 __ GetObjectType(v0, a1, a2);
2628 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2629 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2630 __ Branch(if_false);
2631
2632 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002633}
2634
2635
2636void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002637 ASSERT(args->length() == 1);
2638
2639 VisitForAccumulatorValue(args->at(0));
2640
2641 Label materialize_true, materialize_false;
2642 Label* if_true = NULL;
2643 Label* if_false = NULL;
2644 Label* fall_through = NULL;
2645 context()->PrepareTest(&materialize_true, &materialize_false,
2646 &if_true, &if_false, &fall_through);
2647
2648 __ JumpIfSmi(v0, if_false);
2649 __ GetObjectType(v0, a1, a1);
2650 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2651 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2652 if_true, if_false, fall_through);
2653
2654 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002655}
2656
2657
2658void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002659 ASSERT(args->length() == 1);
2660
2661 VisitForAccumulatorValue(args->at(0));
2662
2663 Label materialize_true, materialize_false;
2664 Label* if_true = NULL;
2665 Label* if_false = NULL;
2666 Label* fall_through = NULL;
2667 context()->PrepareTest(&materialize_true, &materialize_false,
2668 &if_true, &if_false, &fall_through);
2669
2670 __ JumpIfSmi(v0, if_false);
2671 __ GetObjectType(v0, a1, a1);
2672 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2673 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2674
2675 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002676}
2677
2678
2679void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002680 ASSERT(args->length() == 0);
2681
2682 Label materialize_true, materialize_false;
2683 Label* if_true = NULL;
2684 Label* if_false = NULL;
2685 Label* fall_through = NULL;
2686 context()->PrepareTest(&materialize_true, &materialize_false,
2687 &if_true, &if_false, &fall_through);
2688
2689 // Get the frame pointer for the calling frame.
2690 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2691
2692 // Skip the arguments adaptor frame if it exists.
2693 Label check_frame_marker;
2694 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2695 __ Branch(&check_frame_marker, ne,
2696 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2697 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2698
2699 // Check the marker in the calling frame.
2700 __ bind(&check_frame_marker);
2701 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
2702 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2703 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2704 if_true, if_false, fall_through);
2705
2706 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002707}
2708
2709
2710void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002711 ASSERT(args->length() == 2);
2712
2713 // Load the two objects into registers and perform the comparison.
2714 VisitForStackValue(args->at(0));
2715 VisitForAccumulatorValue(args->at(1));
2716
2717 Label materialize_true, materialize_false;
2718 Label* if_true = NULL;
2719 Label* if_false = NULL;
2720 Label* fall_through = NULL;
2721 context()->PrepareTest(&materialize_true, &materialize_false,
2722 &if_true, &if_false, &fall_through);
2723
2724 __ pop(a1);
2725 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2726 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2727
2728 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002729}
2730
2731
2732void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002733 ASSERT(args->length() == 1);
2734
2735 // ArgumentsAccessStub expects the key in a1 and the formal
2736 // parameter count in a0.
2737 VisitForAccumulatorValue(args->at(0));
2738 __ mov(a1, v0);
2739 __ li(a0, Operand(Smi::FromInt(scope()->num_parameters())));
2740 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2741 __ CallStub(&stub);
2742 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002743}
2744
2745
2746void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002747 ASSERT(args->length() == 0);
2748
2749 Label exit;
2750 // Get the number of formal parameters.
2751 __ li(v0, Operand(Smi::FromInt(scope()->num_parameters())));
2752
2753 // Check if the calling frame is an arguments adaptor frame.
2754 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2755 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2756 __ Branch(&exit, ne, a3,
2757 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2758
2759 // Arguments adaptor case: Read the arguments length from the
2760 // adaptor frame.
2761 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2762
2763 __ bind(&exit);
2764 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002765}
2766
2767
2768void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002769 ASSERT(args->length() == 1);
2770 Label done, null, function, non_function_constructor;
2771
2772 VisitForAccumulatorValue(args->at(0));
2773
2774 // If the object is a smi, we return null.
2775 __ JumpIfSmi(v0, &null);
2776
2777 // Check that the object is a JS object but take special care of JS
2778 // functions to make sure they have 'Function' as their class.
2779 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002780 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002781
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002782 // As long as LAST_CALLABLE_SPEC_OBJECT_TYPE is the last instance type, and
2783 // FIRST_CALLABLE_SPEC_OBJECT_TYPE comes right after
2784 // LAST_NONCALLABLE_SPEC_OBJECT_TYPE, we can avoid checking for the latter.
2785 STATIC_ASSERT(LAST_TYPE == LAST_CALLABLE_SPEC_OBJECT_TYPE);
2786 STATIC_ASSERT(FIRST_CALLABLE_SPEC_OBJECT_TYPE ==
2787 LAST_NONCALLABLE_SPEC_OBJECT_TYPE + 1);
2788 __ Branch(&function, ge, a1, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002789
2790 // Check if the constructor in the map is a function.
2791 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2792 __ GetObjectType(v0, a1, a1);
2793 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2794
2795 // v0 now contains the constructor function. Grab the
2796 // instance class name from there.
2797 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2798 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2799 __ Branch(&done);
2800
2801 // Functions have class 'Function'.
2802 __ bind(&function);
2803 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2804 __ jmp(&done);
2805
2806 // Objects with a non-function constructor have class 'Object'.
2807 __ bind(&non_function_constructor);
2808 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2809 __ jmp(&done);
2810
2811 // Non-JS objects have class null.
2812 __ bind(&null);
2813 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2814
2815 // All done.
2816 __ bind(&done);
2817
2818 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002819}
2820
2821
2822void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002823 // Conditionally generate a log call.
2824 // Args:
2825 // 0 (literal string): The type of logging (corresponds to the flags).
2826 // This is used to determine whether or not to generate the log call.
2827 // 1 (string): Format string. Access the string at argument index 2
2828 // with '%2s' (see Logger::LogRuntime for all the formats).
2829 // 2 (array): Arguments to the format string.
2830 ASSERT_EQ(args->length(), 3);
2831#ifdef ENABLE_LOGGING_AND_PROFILING
2832 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2833 VisitForStackValue(args->at(1));
2834 VisitForStackValue(args->at(2));
2835 __ CallRuntime(Runtime::kLog, 2);
2836 }
2837#endif
2838 // Finally, we're expected to leave a value on the top of the stack.
2839 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2840 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002841}
2842
2843
2844void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002845 ASSERT(args->length() == 0);
2846
2847 Label slow_allocate_heapnumber;
2848 Label heapnumber_allocated;
2849
2850 // Save the new heap number in callee-saved register s0, since
2851 // we call out to external C code below.
2852 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2853 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2854 __ jmp(&heapnumber_allocated);
2855
2856 __ bind(&slow_allocate_heapnumber);
2857
2858 // Allocate a heap number.
2859 __ CallRuntime(Runtime::kNumberAlloc, 0);
2860 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2861
2862 __ bind(&heapnumber_allocated);
2863
2864 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2865 // by computing:
2866 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2867 if (CpuFeatures::IsSupported(FPU)) {
2868 __ PrepareCallCFunction(1, a0);
2869 __ li(a0, Operand(ExternalReference::isolate_address()));
2870 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2871
2872
2873 CpuFeatures::Scope scope(FPU);
2874 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2875 __ li(a1, Operand(0x41300000));
2876 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002877 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002878 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002879 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002880 // Subtract and store the result in the heap number.
2881 __ sub_d(f0, f12, f14);
2882 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2883 __ mov(v0, s0);
2884 } else {
2885 __ PrepareCallCFunction(2, a0);
2886 __ mov(a0, s0);
2887 __ li(a1, Operand(ExternalReference::isolate_address()));
2888 __ CallCFunction(
2889 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2890 }
2891
2892 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002893}
2894
2895
2896void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002897 // Load the arguments on the stack and call the stub.
2898 SubStringStub stub;
2899 ASSERT(args->length() == 3);
2900 VisitForStackValue(args->at(0));
2901 VisitForStackValue(args->at(1));
2902 VisitForStackValue(args->at(2));
2903 __ CallStub(&stub);
2904 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002905}
2906
2907
2908void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002909 // Load the arguments on the stack and call the stub.
2910 RegExpExecStub stub;
2911 ASSERT(args->length() == 4);
2912 VisitForStackValue(args->at(0));
2913 VisitForStackValue(args->at(1));
2914 VisitForStackValue(args->at(2));
2915 VisitForStackValue(args->at(3));
2916 __ CallStub(&stub);
2917 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002918}
2919
2920
2921void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002922 ASSERT(args->length() == 1);
2923
2924 VisitForAccumulatorValue(args->at(0)); // Load the object.
2925
2926 Label done;
2927 // If the object is a smi return the object.
2928 __ JumpIfSmi(v0, &done);
2929 // If the object is not a value type, return the object.
2930 __ GetObjectType(v0, a1, a1);
2931 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2932
2933 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2934
2935 __ bind(&done);
2936 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002937}
2938
2939
2940void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002941 // Load the arguments on the stack and call the runtime function.
2942 ASSERT(args->length() == 2);
2943 VisitForStackValue(args->at(0));
2944 VisitForStackValue(args->at(1));
2945 MathPowStub stub;
2946 __ CallStub(&stub);
2947 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002948}
2949
2950
2951void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002952 ASSERT(args->length() == 2);
2953
2954 VisitForStackValue(args->at(0)); // Load the object.
2955 VisitForAccumulatorValue(args->at(1)); // Load the value.
2956 __ pop(a1); // v0 = value. a1 = object.
2957
2958 Label done;
2959 // If the object is a smi, return the value.
2960 __ JumpIfSmi(a1, &done);
2961
2962 // If the object is not a value type, return the value.
2963 __ GetObjectType(a1, a2, a2);
2964 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2965
2966 // Store the value.
2967 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2968 // Update the write barrier. Save the value as it will be
2969 // overwritten by the write barrier code and is needed afterward.
2970 __ RecordWrite(a1, Operand(JSValue::kValueOffset - kHeapObjectTag), a2, a3);
2971
2972 __ bind(&done);
2973 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002974}
2975
2976
2977void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002978 ASSERT_EQ(args->length(), 1);
2979
2980 // Load the argument on the stack and call the stub.
2981 VisitForStackValue(args->at(0));
2982
2983 NumberToStringStub stub;
2984 __ CallStub(&stub);
2985 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002986}
2987
2988
2989void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002990 ASSERT(args->length() == 1);
2991
2992 VisitForAccumulatorValue(args->at(0));
2993
2994 Label done;
2995 StringCharFromCodeGenerator generator(v0, a1);
2996 generator.GenerateFast(masm_);
2997 __ jmp(&done);
2998
2999 NopRuntimeCallHelper call_helper;
3000 generator.GenerateSlow(masm_, call_helper);
3001
3002 __ bind(&done);
3003 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003004}
3005
3006
3007void FullCodeGenerator::EmitStringCharCodeAt(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 scratch = a2;
3017 Register result = v0;
3018
3019 __ pop(object);
3020
3021 Label need_conversion;
3022 Label index_out_of_range;
3023 Label done;
3024 StringCharCodeAtGenerator generator(object,
3025 index,
3026 scratch,
3027 result,
3028 &need_conversion,
3029 &need_conversion,
3030 &index_out_of_range,
3031 STRING_INDEX_IS_NUMBER);
3032 generator.GenerateFast(masm_);
3033 __ jmp(&done);
3034
3035 __ bind(&index_out_of_range);
3036 // When the index is out of range, the spec requires us to return
3037 // NaN.
3038 __ LoadRoot(result, Heap::kNanValueRootIndex);
3039 __ jmp(&done);
3040
3041 __ bind(&need_conversion);
3042 // Load the undefined value into the result register, which will
3043 // trigger conversion.
3044 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3045 __ jmp(&done);
3046
3047 NopRuntimeCallHelper call_helper;
3048 generator.GenerateSlow(masm_, call_helper);
3049
3050 __ bind(&done);
3051 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003052}
3053
3054
3055void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003056 ASSERT(args->length() == 2);
3057
3058 VisitForStackValue(args->at(0));
3059 VisitForAccumulatorValue(args->at(1));
3060 __ mov(a0, result_register());
3061
3062 Register object = a1;
3063 Register index = a0;
3064 Register scratch1 = a2;
3065 Register scratch2 = a3;
3066 Register result = v0;
3067
3068 __ pop(object);
3069
3070 Label need_conversion;
3071 Label index_out_of_range;
3072 Label done;
3073 StringCharAtGenerator generator(object,
3074 index,
3075 scratch1,
3076 scratch2,
3077 result,
3078 &need_conversion,
3079 &need_conversion,
3080 &index_out_of_range,
3081 STRING_INDEX_IS_NUMBER);
3082 generator.GenerateFast(masm_);
3083 __ jmp(&done);
3084
3085 __ bind(&index_out_of_range);
3086 // When the index is out of range, the spec requires us to return
3087 // the empty string.
3088 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3089 __ jmp(&done);
3090
3091 __ bind(&need_conversion);
3092 // Move smi zero into the result register, which will trigger
3093 // conversion.
3094 __ li(result, Operand(Smi::FromInt(0)));
3095 __ jmp(&done);
3096
3097 NopRuntimeCallHelper call_helper;
3098 generator.GenerateSlow(masm_, call_helper);
3099
3100 __ bind(&done);
3101 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003102}
3103
3104
3105void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003106 ASSERT_EQ(2, args->length());
3107
3108 VisitForStackValue(args->at(0));
3109 VisitForStackValue(args->at(1));
3110
3111 StringAddStub stub(NO_STRING_ADD_FLAGS);
3112 __ CallStub(&stub);
3113 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003114}
3115
3116
3117void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003118 ASSERT_EQ(2, args->length());
3119
3120 VisitForStackValue(args->at(0));
3121 VisitForStackValue(args->at(1));
3122
3123 StringCompareStub stub;
3124 __ CallStub(&stub);
3125 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003126}
3127
3128
3129void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003130 // Load the argument on the stack and call the stub.
3131 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3132 TranscendentalCacheStub::TAGGED);
3133 ASSERT(args->length() == 1);
3134 VisitForStackValue(args->at(0));
3135 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3136 __ CallStub(&stub);
3137 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003138}
3139
3140
3141void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003142 // Load the argument on the stack and call the stub.
3143 TranscendentalCacheStub stub(TranscendentalCache::COS,
3144 TranscendentalCacheStub::TAGGED);
3145 ASSERT(args->length() == 1);
3146 VisitForStackValue(args->at(0));
3147 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3148 __ CallStub(&stub);
3149 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003150}
3151
3152
3153void FullCodeGenerator::EmitMathLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003154 // Load the argument on the stack and call the stub.
3155 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3156 TranscendentalCacheStub::TAGGED);
3157 ASSERT(args->length() == 1);
3158 VisitForStackValue(args->at(0));
3159 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3160 __ CallStub(&stub);
3161 context()->Plug(v0);
3162}
3163
3164
3165void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
3166 // Load the argument on the stack and call the runtime function.
3167 ASSERT(args->length() == 1);
3168 VisitForStackValue(args->at(0));
3169 __ CallRuntime(Runtime::kMath_sqrt, 1);
3170 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003171}
3172
3173
3174void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003175 ASSERT(args->length() >= 2);
3176
3177 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3178 for (int i = 0; i < arg_count + 1; i++) {
3179 VisitForStackValue(args->at(i));
3180 }
3181 VisitForAccumulatorValue(args->last()); // Function.
3182
3183 // InvokeFunction requires the function in a1. Move it in there.
3184 __ mov(a1, result_register());
3185 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003186 __ InvokeFunction(a1, count, CALL_FUNCTION,
3187 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003188 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3189 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003190}
3191
3192
3193void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003194 RegExpConstructResultStub stub;
3195 ASSERT(args->length() == 3);
3196 VisitForStackValue(args->at(0));
3197 VisitForStackValue(args->at(1));
3198 VisitForStackValue(args->at(2));
3199 __ CallStub(&stub);
3200 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003201}
3202
3203
3204void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003205 ASSERT(args->length() == 3);
3206 VisitForStackValue(args->at(0));
3207 VisitForStackValue(args->at(1));
3208 VisitForStackValue(args->at(2));
3209 Label done;
3210 Label slow_case;
3211 Register object = a0;
3212 Register index1 = a1;
3213 Register index2 = a2;
3214 Register elements = a3;
3215 Register scratch1 = t0;
3216 Register scratch2 = t1;
3217
3218 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3219 // Fetch the map and check if array is in fast case.
3220 // Check that object doesn't require security checks and
3221 // has no indexed interceptor.
3222 __ GetObjectType(object, scratch1, scratch2);
3223 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3224 // Map is now in scratch1.
3225
3226 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3227 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3228 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3229
3230 // Check the object's elements are in fast case and writable.
3231 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3232 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3233 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3234 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3235
3236 // Check that both indices are smis.
3237 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3238 __ lw(index2, MemOperand(sp, 0));
3239 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3240
3241 // Check that both indices are valid.
3242 Label not_hi;
3243 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3244 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3245 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3246 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3247 __ bind(&not_hi);
3248
3249 // Bring the address of the elements into index1 and index2.
3250 __ Addu(scratch1, elements,
3251 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3252 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3253 __ Addu(index1, scratch1, index1);
3254 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3255 __ Addu(index2, scratch1, index2);
3256
3257 // Swap elements.
3258 __ lw(scratch1, MemOperand(index1, 0));
3259 __ lw(scratch2, MemOperand(index2, 0));
3260 __ sw(scratch1, MemOperand(index2, 0));
3261 __ sw(scratch2, MemOperand(index1, 0));
3262
3263 Label new_space;
3264 __ InNewSpace(elements, scratch1, eq, &new_space);
3265 // Possible optimization: do a check that both values are Smis
3266 // (or them and test against Smi mask).
3267
3268 __ mov(scratch1, elements);
3269 __ RecordWriteHelper(elements, index1, scratch2);
3270 __ RecordWriteHelper(scratch1, index2, scratch2); // scratch1 holds elements.
3271
3272 __ bind(&new_space);
3273 // We are done. Drop elements from the stack, and return undefined.
3274 __ Drop(3);
3275 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3276 __ jmp(&done);
3277
3278 __ bind(&slow_case);
3279 __ CallRuntime(Runtime::kSwapElements, 3);
3280
3281 __ bind(&done);
3282 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003283}
3284
3285
3286void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003287 ASSERT_EQ(2, args->length());
3288
3289 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3290 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3291
3292 Handle<FixedArray> jsfunction_result_caches(
3293 isolate()->global_context()->jsfunction_result_caches());
3294 if (jsfunction_result_caches->length() <= cache_id) {
3295 __ Abort("Attempt to use undefined cache.");
3296 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3297 context()->Plug(v0);
3298 return;
3299 }
3300
3301 VisitForAccumulatorValue(args->at(1));
3302
3303 Register key = v0;
3304 Register cache = a1;
3305 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3306 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3307 __ lw(cache,
3308 ContextOperand(
3309 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3310 __ lw(cache,
3311 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3312
3313
3314 Label done, not_found;
3315 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
3316 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3317 // a2 now holds finger offset as a smi.
3318 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3319 // a3 now points to the start of fixed array elements.
3320 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3321 __ addu(a3, a3, at);
3322 // a3 now points to key of indexed element of cache.
3323 __ lw(a2, MemOperand(a3));
3324 __ Branch(&not_found, ne, key, Operand(a2));
3325
3326 __ lw(v0, MemOperand(a3, kPointerSize));
3327 __ Branch(&done);
3328
3329 __ bind(&not_found);
3330 // Call runtime to perform the lookup.
3331 __ Push(cache, key);
3332 __ CallRuntime(Runtime::kGetFromCache, 2);
3333
3334 __ bind(&done);
3335 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003336}
3337
3338
3339void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003340 ASSERT_EQ(2, args->length());
3341
3342 Register right = v0;
3343 Register left = a1;
3344 Register tmp = a2;
3345 Register tmp2 = a3;
3346
3347 VisitForStackValue(args->at(0));
3348 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3349 __ pop(left);
3350
3351 Label done, fail, ok;
3352 __ Branch(&ok, eq, left, Operand(right));
3353 // Fail if either is a non-HeapObject.
3354 __ And(tmp, left, Operand(right));
3355 __ And(at, tmp, Operand(kSmiTagMask));
3356 __ Branch(&fail, eq, at, Operand(zero_reg));
3357 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3358 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3359 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3360 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3361 __ Branch(&fail, ne, tmp, Operand(tmp2));
3362 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3363 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3364 __ Branch(&ok, eq, tmp, Operand(tmp2));
3365 __ bind(&fail);
3366 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3367 __ jmp(&done);
3368 __ bind(&ok);
3369 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3370 __ bind(&done);
3371
3372 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003373}
3374
3375
3376void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003377 VisitForAccumulatorValue(args->at(0));
3378
3379 Label materialize_true, materialize_false;
3380 Label* if_true = NULL;
3381 Label* if_false = NULL;
3382 Label* fall_through = NULL;
3383 context()->PrepareTest(&materialize_true, &materialize_false,
3384 &if_true, &if_false, &fall_through);
3385
3386 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3387 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3388
3389 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3390 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3391
3392 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003393}
3394
3395
3396void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003397 ASSERT(args->length() == 1);
3398 VisitForAccumulatorValue(args->at(0));
3399
3400 if (FLAG_debug_code) {
3401 __ AbortIfNotString(v0);
3402 }
3403
3404 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3405 __ IndexFromHash(v0, v0);
3406
3407 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003408}
3409
3410
3411void FullCodeGenerator::EmitFastAsciiArrayJoin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003412 Label bailout, done, one_char_separator, long_separator,
3413 non_trivial_array, not_size_one_array, loop,
3414 empty_separator_loop, one_char_separator_loop,
3415 one_char_separator_loop_entry, long_separator_loop;
3416
3417 ASSERT(args->length() == 2);
3418 VisitForStackValue(args->at(1));
3419 VisitForAccumulatorValue(args->at(0));
3420
3421 // All aliases of the same register have disjoint lifetimes.
3422 Register array = v0;
3423 Register elements = no_reg; // Will be v0.
3424 Register result = no_reg; // Will be v0.
3425 Register separator = a1;
3426 Register array_length = a2;
3427 Register result_pos = no_reg; // Will be a2.
3428 Register string_length = a3;
3429 Register string = t0;
3430 Register element = t1;
3431 Register elements_end = t2;
3432 Register scratch1 = t3;
3433 Register scratch2 = t5;
3434 Register scratch3 = t4;
3435 Register scratch4 = v1;
3436
3437 // Separator operand is on the stack.
3438 __ pop(separator);
3439
3440 // Check that the array is a JSArray.
3441 __ JumpIfSmi(array, &bailout);
3442 __ GetObjectType(array, scratch1, scratch2);
3443 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3444
3445 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003446 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003447
3448 // If the array has length zero, return the empty string.
3449 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3450 __ SmiUntag(array_length);
3451 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3452 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3453 __ Branch(&done);
3454
3455 __ bind(&non_trivial_array);
3456
3457 // Get the FixedArray containing array's elements.
3458 elements = array;
3459 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3460 array = no_reg; // End of array's live range.
3461
3462 // Check that all array elements are sequential ASCII strings, and
3463 // accumulate the sum of their lengths, as a smi-encoded value.
3464 __ mov(string_length, zero_reg);
3465 __ Addu(element,
3466 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3467 __ sll(elements_end, array_length, kPointerSizeLog2);
3468 __ Addu(elements_end, element, elements_end);
3469 // Loop condition: while (element < elements_end).
3470 // Live values in registers:
3471 // elements: Fixed array of strings.
3472 // array_length: Length of the fixed array of strings (not smi)
3473 // separator: Separator string
3474 // string_length: Accumulated sum of string lengths (smi).
3475 // element: Current array element.
3476 // elements_end: Array end.
3477 if (FLAG_debug_code) {
3478 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3479 array_length, Operand(zero_reg));
3480 }
3481 __ bind(&loop);
3482 __ lw(string, MemOperand(element));
3483 __ Addu(element, element, kPointerSize);
3484 __ JumpIfSmi(string, &bailout);
3485 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3486 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3487 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3488 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3489 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3490 __ BranchOnOverflow(&bailout, scratch3);
3491 __ Branch(&loop, lt, element, Operand(elements_end));
3492
3493 // If array_length is 1, return elements[0], a string.
3494 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3495 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3496 __ Branch(&done);
3497
3498 __ bind(&not_size_one_array);
3499
3500 // Live values in registers:
3501 // separator: Separator string
3502 // array_length: Length of the array.
3503 // string_length: Sum of string lengths (smi).
3504 // elements: FixedArray of strings.
3505
3506 // Check that the separator is a flat ASCII string.
3507 __ JumpIfSmi(separator, &bailout);
3508 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3509 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3510 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3511
3512 // Add (separator length times array_length) - separator length to the
3513 // string_length to get the length of the result string. array_length is not
3514 // smi but the other values are, so the result is a smi.
3515 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3516 __ Subu(string_length, string_length, Operand(scratch1));
3517 __ Mult(array_length, scratch1);
3518 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3519 // zero.
3520 __ mfhi(scratch2);
3521 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3522 __ mflo(scratch2);
3523 __ And(scratch3, scratch2, Operand(0x80000000));
3524 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3525 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3526 __ BranchOnOverflow(&bailout, scratch3);
3527 __ SmiUntag(string_length);
3528
3529 // Get first element in the array to free up the elements register to be used
3530 // for the result.
3531 __ Addu(element,
3532 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3533 result = elements; // End of live range for elements.
3534 elements = no_reg;
3535 // Live values in registers:
3536 // element: First array element
3537 // separator: Separator string
3538 // string_length: Length of result string (not smi)
3539 // array_length: Length of the array.
3540 __ AllocateAsciiString(result,
3541 string_length,
3542 scratch1,
3543 scratch2,
3544 elements_end,
3545 &bailout);
3546 // Prepare for looping. Set up elements_end to end of the array. Set
3547 // result_pos to the position of the result where to write the first
3548 // character.
3549 __ sll(elements_end, array_length, kPointerSizeLog2);
3550 __ Addu(elements_end, element, elements_end);
3551 result_pos = array_length; // End of live range for array_length.
3552 array_length = no_reg;
3553 __ Addu(result_pos,
3554 result,
3555 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3556
3557 // Check the length of the separator.
3558 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3559 __ li(at, Operand(Smi::FromInt(1)));
3560 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3561 __ Branch(&long_separator, gt, scratch1, Operand(at));
3562
3563 // Empty separator case.
3564 __ bind(&empty_separator_loop);
3565 // Live values in registers:
3566 // result_pos: the position to which we are currently copying characters.
3567 // element: Current array element.
3568 // elements_end: Array end.
3569
3570 // Copy next array element to the result.
3571 __ lw(string, MemOperand(element));
3572 __ Addu(element, element, kPointerSize);
3573 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3574 __ SmiUntag(string_length);
3575 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3576 __ CopyBytes(string, result_pos, string_length, scratch1);
3577 // End while (element < elements_end).
3578 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3579 ASSERT(result.is(v0));
3580 __ Branch(&done);
3581
3582 // One-character separator case.
3583 __ bind(&one_char_separator);
3584 // Replace separator with its ascii character value.
3585 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3586 // Jump into the loop after the code that copies the separator, so the first
3587 // element is not preceded by a separator.
3588 __ jmp(&one_char_separator_loop_entry);
3589
3590 __ bind(&one_char_separator_loop);
3591 // Live values in registers:
3592 // result_pos: the position to which we are currently copying characters.
3593 // element: Current array element.
3594 // elements_end: Array end.
3595 // separator: Single separator ascii char (in lower byte).
3596
3597 // Copy the separator character to the result.
3598 __ sb(separator, MemOperand(result_pos));
3599 __ Addu(result_pos, result_pos, 1);
3600
3601 // Copy next array element to the result.
3602 __ bind(&one_char_separator_loop_entry);
3603 __ lw(string, MemOperand(element));
3604 __ Addu(element, element, kPointerSize);
3605 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3606 __ SmiUntag(string_length);
3607 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3608 __ CopyBytes(string, result_pos, string_length, scratch1);
3609 // End while (element < elements_end).
3610 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3611 ASSERT(result.is(v0));
3612 __ Branch(&done);
3613
3614 // Long separator case (separator is more than one character). Entry is at the
3615 // label long_separator below.
3616 __ bind(&long_separator_loop);
3617 // Live values in registers:
3618 // result_pos: the position to which we are currently copying characters.
3619 // element: Current array element.
3620 // elements_end: Array end.
3621 // separator: Separator string.
3622
3623 // Copy the separator to the result.
3624 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3625 __ SmiUntag(string_length);
3626 __ Addu(string,
3627 separator,
3628 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3629 __ CopyBytes(string, result_pos, string_length, scratch1);
3630
3631 __ bind(&long_separator);
3632 __ lw(string, MemOperand(element));
3633 __ Addu(element, element, kPointerSize);
3634 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3635 __ SmiUntag(string_length);
3636 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3637 __ CopyBytes(string, result_pos, string_length, scratch1);
3638 // End while (element < elements_end).
3639 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3640 ASSERT(result.is(v0));
3641 __ Branch(&done);
3642
3643 __ bind(&bailout);
3644 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3645 __ bind(&done);
3646 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003647}
3648
3649
ager@chromium.org5c838252010-02-19 08:53:10 +00003650void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003651 Handle<String> name = expr->name();
3652 if (name->length() > 0 && name->Get(0) == '_') {
3653 Comment cmnt(masm_, "[ InlineRuntimeCall");
3654 EmitInlineRuntimeCall(expr);
3655 return;
3656 }
3657
3658 Comment cmnt(masm_, "[ CallRuntime");
3659 ZoneList<Expression*>* args = expr->arguments();
3660
3661 if (expr->is_jsruntime()) {
3662 // Prepare for calling JS runtime function.
3663 __ lw(a0, GlobalObjectOperand());
3664 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3665 __ push(a0);
3666 }
3667
3668 // Push the arguments ("left-to-right").
3669 int arg_count = args->length();
3670 for (int i = 0; i < arg_count; i++) {
3671 VisitForStackValue(args->at(i));
3672 }
3673
3674 if (expr->is_jsruntime()) {
3675 // Call the JS runtime function.
3676 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003677 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003678 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00003679 isolate()->stub_cache()->ComputeCallInitialize(arg_count,
3680 NOT_IN_LOOP,
3681 mode);
3682 EmitCallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003683 // Restore context register.
3684 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3685 } else {
3686 // Call the C runtime function.
3687 __ CallRuntime(expr->function(), arg_count);
3688 }
3689 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003690}
3691
3692
3693void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003694 switch (expr->op()) {
3695 case Token::DELETE: {
3696 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
3697 Property* prop = expr->expression()->AsProperty();
3698 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
3699
3700 if (prop != NULL) {
3701 if (prop->is_synthetic()) {
3702 // Result of deleting parameters is false, even when they rewrite
3703 // to accesses on the arguments object.
3704 context()->Plug(false);
3705 } else {
3706 VisitForStackValue(prop->obj());
3707 VisitForStackValue(prop->key());
3708 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3709 __ push(a1);
3710 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3711 context()->Plug(v0);
3712 }
3713 } else if (var != NULL) {
3714 // Delete of an unqualified identifier is disallowed in strict mode
3715 // but "delete this" is.
3716 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
3717 if (var->is_global()) {
3718 __ lw(a2, GlobalObjectOperand());
3719 __ li(a1, Operand(var->name()));
3720 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3721 __ Push(a2, a1, a0);
3722 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3723 context()->Plug(v0);
3724 } else if (var->AsSlot() != NULL &&
3725 var->AsSlot()->type() != Slot::LOOKUP) {
3726 // Result of deleting non-global, non-dynamic variables is false.
3727 // The subexpression does not have side effects.
3728 context()->Plug(false);
3729 } else {
3730 // Non-global variable. Call the runtime to try to delete from the
3731 // context where the variable was introduced.
3732 __ push(context_register());
3733 __ li(a2, Operand(var->name()));
3734 __ push(a2);
3735 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3736 context()->Plug(v0);
3737 }
3738 } else {
3739 // Result of deleting non-property, non-variable reference is true.
3740 // The subexpression may have side effects.
3741 VisitForEffect(expr->expression());
3742 context()->Plug(true);
3743 }
3744 break;
3745 }
3746
3747 case Token::VOID: {
3748 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3749 VisitForEffect(expr->expression());
3750 context()->Plug(Heap::kUndefinedValueRootIndex);
3751 break;
3752 }
3753
3754 case Token::NOT: {
3755 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3756 if (context()->IsEffect()) {
3757 // Unary NOT has no side effects so it's only necessary to visit the
3758 // subexpression. Match the optimizing compiler by not branching.
3759 VisitForEffect(expr->expression());
3760 } else {
3761 Label materialize_true, materialize_false;
3762 Label* if_true = NULL;
3763 Label* if_false = NULL;
3764 Label* fall_through = NULL;
3765
3766 // Notice that the labels are swapped.
3767 context()->PrepareTest(&materialize_true, &materialize_false,
3768 &if_false, &if_true, &fall_through);
3769 if (context()->IsTest()) ForwardBailoutToChild(expr);
3770 VisitForControl(expr->expression(), if_true, if_false, fall_through);
3771 context()->Plug(if_false, if_true); // Labels swapped.
3772 }
3773 break;
3774 }
3775
3776 case Token::TYPEOF: {
3777 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3778 { StackValueContext context(this);
3779 VisitForTypeofValue(expr->expression());
3780 }
3781 __ CallRuntime(Runtime::kTypeof, 1);
3782 context()->Plug(v0);
3783 break;
3784 }
3785
3786 case Token::ADD: {
3787 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3788 VisitForAccumulatorValue(expr->expression());
3789 Label no_conversion;
3790 __ JumpIfSmi(result_register(), &no_conversion);
3791 __ mov(a0, result_register());
3792 ToNumberStub convert_stub;
3793 __ CallStub(&convert_stub);
3794 __ bind(&no_conversion);
3795 context()->Plug(result_register());
3796 break;
3797 }
3798
3799 case Token::SUB:
3800 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3801 break;
3802
3803 case Token::BIT_NOT:
3804 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3805 break;
3806
3807 default:
3808 UNREACHABLE();
3809 }
3810}
3811
3812
3813void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3814 const char* comment) {
3815 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3816 Comment cmt(masm_, comment);
3817 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3818 UnaryOverwriteMode overwrite =
3819 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003820 UnaryOpStub stub(expr->op(), overwrite);
3821 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003822 VisitForAccumulatorValue(expr->expression());
3823 SetSourcePosition(expr->position());
3824 __ mov(a0, result_register());
3825 EmitCallIC(stub.GetCode(), NULL, expr->id());
3826 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003827}
3828
3829
3830void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003831 Comment cmnt(masm_, "[ CountOperation");
3832 SetSourcePosition(expr->position());
3833
3834 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3835 // as the left-hand side.
3836 if (!expr->expression()->IsValidLeftHandSide()) {
3837 VisitForEffect(expr->expression());
3838 return;
3839 }
3840
3841 // Expression can only be a property, a global or a (parameter or local)
3842 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
3843 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3844 LhsKind assign_type = VARIABLE;
3845 Property* prop = expr->expression()->AsProperty();
3846 // In case of a property we use the uninitialized expression context
3847 // of the key to detect a named property.
3848 if (prop != NULL) {
3849 assign_type =
3850 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3851 }
3852
3853 // Evaluate expression and get value.
3854 if (assign_type == VARIABLE) {
3855 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3856 AccumulatorValueContext context(this);
3857 EmitVariableLoad(expr->expression()->AsVariableProxy()->var());
3858 } else {
3859 // Reserve space for result of postfix operation.
3860 if (expr->is_postfix() && !context()->IsEffect()) {
3861 __ li(at, Operand(Smi::FromInt(0)));
3862 __ push(at);
3863 }
3864 if (assign_type == NAMED_PROPERTY) {
3865 // Put the object both on the stack and in the accumulator.
3866 VisitForAccumulatorValue(prop->obj());
3867 __ push(v0);
3868 EmitNamedPropertyLoad(prop);
3869 } else {
3870 if (prop->is_arguments_access()) {
3871 VariableProxy* obj_proxy = prop->obj()->AsVariableProxy();
3872 __ lw(v0, EmitSlotSearch(obj_proxy->var()->AsSlot(), v0));
3873 __ push(v0);
3874 __ li(v0, Operand(prop->key()->AsLiteral()->handle()));
3875 } else {
3876 VisitForStackValue(prop->obj());
3877 VisitForAccumulatorValue(prop->key());
3878 }
3879 __ lw(a1, MemOperand(sp, 0));
3880 __ push(v0);
3881 EmitKeyedPropertyLoad(prop);
3882 }
3883 }
3884
3885 // We need a second deoptimization point after loading the value
3886 // in case evaluating the property load my have a side effect.
3887 if (assign_type == VARIABLE) {
3888 PrepareForBailout(expr->expression(), TOS_REG);
3889 } else {
3890 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3891 }
3892
3893 // Call ToNumber only if operand is not a smi.
3894 Label no_conversion;
3895 __ JumpIfSmi(v0, &no_conversion);
3896 __ mov(a0, v0);
3897 ToNumberStub convert_stub;
3898 __ CallStub(&convert_stub);
3899 __ bind(&no_conversion);
3900
3901 // Save result for postfix expressions.
3902 if (expr->is_postfix()) {
3903 if (!context()->IsEffect()) {
3904 // Save the result on the stack. If we have a named or keyed property
3905 // we store the result under the receiver that is currently on top
3906 // of the stack.
3907 switch (assign_type) {
3908 case VARIABLE:
3909 __ push(v0);
3910 break;
3911 case NAMED_PROPERTY:
3912 __ sw(v0, MemOperand(sp, kPointerSize));
3913 break;
3914 case KEYED_PROPERTY:
3915 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3916 break;
3917 }
3918 }
3919 }
3920 __ mov(a0, result_register());
3921
3922 // Inline smi case if we are in a loop.
3923 Label stub_call, done;
3924 JumpPatchSite patch_site(masm_);
3925
3926 int count_value = expr->op() == Token::INC ? 1 : -1;
3927 __ li(a1, Operand(Smi::FromInt(count_value)));
3928
3929 if (ShouldInlineSmiCase(expr->op())) {
3930 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
3931 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
3932
3933 // We could eliminate this smi check if we split the code at
3934 // the first smi check before calling ToNumber.
3935 patch_site.EmitJumpIfSmi(v0, &done);
3936 __ bind(&stub_call);
3937 }
3938
3939 // Record position before stub call.
3940 SetSourcePosition(expr->position());
3941
danno@chromium.org40cb8782011-05-25 07:58:50 +00003942 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003943 EmitCallIC(stub.GetCode(), &patch_site, expr->CountId());
3944 __ bind(&done);
3945
3946 // Store the value returned in v0.
3947 switch (assign_type) {
3948 case VARIABLE:
3949 if (expr->is_postfix()) {
3950 { EffectContext context(this);
3951 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3952 Token::ASSIGN);
3953 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3954 context.Plug(v0);
3955 }
3956 // For all contexts except EffectConstant we have the result on
3957 // top of the stack.
3958 if (!context()->IsEffect()) {
3959 context()->PlugTOS();
3960 }
3961 } else {
3962 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3963 Token::ASSIGN);
3964 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3965 context()->Plug(v0);
3966 }
3967 break;
3968 case NAMED_PROPERTY: {
3969 __ mov(a0, result_register()); // Value.
3970 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
3971 __ pop(a1); // Receiver.
3972 Handle<Code> ic = is_strict_mode()
3973 ? isolate()->builtins()->StoreIC_Initialize_Strict()
3974 : isolate()->builtins()->StoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00003975 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003976 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3977 if (expr->is_postfix()) {
3978 if (!context()->IsEffect()) {
3979 context()->PlugTOS();
3980 }
3981 } else {
3982 context()->Plug(v0);
3983 }
3984 break;
3985 }
3986 case KEYED_PROPERTY: {
3987 __ mov(a0, result_register()); // Value.
3988 __ pop(a1); // Key.
3989 __ pop(a2); // Receiver.
3990 Handle<Code> ic = is_strict_mode()
3991 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
3992 : isolate()->builtins()->KeyedStoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00003993 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003994 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3995 if (expr->is_postfix()) {
3996 if (!context()->IsEffect()) {
3997 context()->PlugTOS();
3998 }
3999 } else {
4000 context()->Plug(v0);
4001 }
4002 break;
4003 }
4004 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004005}
4006
4007
lrn@chromium.org7516f052011-03-30 08:52:27 +00004008void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004009 VariableProxy* proxy = expr->AsVariableProxy();
4010 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
4011 Comment cmnt(masm_, "Global variable");
4012 __ lw(a0, GlobalObjectOperand());
4013 __ li(a2, Operand(proxy->name()));
4014 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4015 // Use a regular load, not a contextual load, to avoid a reference
4016 // error.
4017 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
4018 PrepareForBailout(expr, TOS_REG);
4019 context()->Plug(v0);
4020 } else if (proxy != NULL &&
4021 proxy->var()->AsSlot() != NULL &&
4022 proxy->var()->AsSlot()->type() == Slot::LOOKUP) {
4023 Label done, slow;
4024
4025 // Generate code for loading from variables potentially shadowed
4026 // by eval-introduced variables.
4027 Slot* slot = proxy->var()->AsSlot();
4028 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
4029
4030 __ bind(&slow);
4031 __ li(a0, Operand(proxy->name()));
4032 __ Push(cp, a0);
4033 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4034 PrepareForBailout(expr, TOS_REG);
4035 __ bind(&done);
4036
4037 context()->Plug(v0);
4038 } else {
4039 // This expression cannot throw a reference error at the top level.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004040 VisitInCurrentContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004041 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004042}
4043
4044
lrn@chromium.org7516f052011-03-30 08:52:27 +00004045bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
4046 Expression* left,
4047 Expression* right,
4048 Label* if_true,
4049 Label* if_false,
4050 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004051 if (op != Token::EQ && op != Token::EQ_STRICT) return false;
4052
4053 // Check for the pattern: typeof <expression> == <string literal>.
4054 Literal* right_literal = right->AsLiteral();
4055 if (right_literal == NULL) return false;
4056 Handle<Object> right_literal_value = right_literal->handle();
4057 if (!right_literal_value->IsString()) return false;
4058 UnaryOperation* left_unary = left->AsUnaryOperation();
4059 if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
4060 Handle<String> check = Handle<String>::cast(right_literal_value);
4061
4062 { AccumulatorValueContext context(this);
4063 VisitForTypeofValue(left_unary->expression());
4064 }
4065 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4066
4067 if (check->Equals(isolate()->heap()->number_symbol())) {
4068 __ JumpIfSmi(v0, if_true);
4069 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4070 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4071 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4072 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4073 __ JumpIfSmi(v0, if_false);
4074 // Check for undetectable objects => false.
4075 __ GetObjectType(v0, v0, a1);
4076 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4077 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4078 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4079 Split(eq, a1, Operand(zero_reg),
4080 if_true, if_false, fall_through);
4081 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4082 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4083 __ Branch(if_true, eq, v0, Operand(at));
4084 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4085 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4086 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4087 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4088 __ Branch(if_true, eq, v0, Operand(at));
4089 __ JumpIfSmi(v0, if_false);
4090 // Check for undetectable objects => true.
4091 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4092 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4093 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4094 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4095 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4096 __ JumpIfSmi(v0, if_false);
4097 __ GetObjectType(v0, a1, v0); // Leave map in a1.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004098 Split(ge, v0, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004099 if_true, if_false, fall_through);
4100
4101 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4102 __ JumpIfSmi(v0, if_false);
4103 __ LoadRoot(at, Heap::kNullValueRootIndex);
4104 __ Branch(if_true, eq, v0, Operand(at));
4105 // Check for JS objects => true.
4106 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004107 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004108 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004109 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004110 // Check for undetectable objects => false.
4111 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4112 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4113 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4114 } else {
4115 if (if_false != fall_through) __ jmp(if_false);
4116 }
4117
4118 return true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00004119}
4120
4121
ager@chromium.org5c838252010-02-19 08:53:10 +00004122void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004123 Comment cmnt(masm_, "[ CompareOperation");
4124 SetSourcePosition(expr->position());
4125
4126 // Always perform the comparison for its control flow. Pack the result
4127 // into the expression's context after the comparison is performed.
4128
4129 Label materialize_true, materialize_false;
4130 Label* if_true = NULL;
4131 Label* if_false = NULL;
4132 Label* fall_through = NULL;
4133 context()->PrepareTest(&materialize_true, &materialize_false,
4134 &if_true, &if_false, &fall_through);
4135
4136 // First we try a fast inlined version of the compare when one of
4137 // the operands is a literal.
4138 Token::Value op = expr->op();
4139 Expression* left = expr->left();
4140 Expression* right = expr->right();
4141 if (TryLiteralCompare(op, left, right, if_true, if_false, fall_through)) {
4142 context()->Plug(if_true, if_false);
4143 return;
4144 }
4145
4146 VisitForStackValue(expr->left());
4147 switch (op) {
4148 case Token::IN:
4149 VisitForStackValue(expr->right());
4150 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
4151 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
4152 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4153 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4154 break;
4155
4156 case Token::INSTANCEOF: {
4157 VisitForStackValue(expr->right());
4158 InstanceofStub stub(InstanceofStub::kNoFlags);
4159 __ CallStub(&stub);
4160 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4161 // The stub returns 0 for true.
4162 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4163 break;
4164 }
4165
4166 default: {
4167 VisitForAccumulatorValue(expr->right());
4168 Condition cc = eq;
4169 bool strict = false;
4170 switch (op) {
4171 case Token::EQ_STRICT:
4172 strict = true;
4173 // Fall through.
4174 case Token::EQ:
4175 cc = eq;
4176 __ mov(a0, result_register());
4177 __ pop(a1);
4178 break;
4179 case Token::LT:
4180 cc = lt;
4181 __ mov(a0, result_register());
4182 __ pop(a1);
4183 break;
4184 case Token::GT:
4185 // Reverse left and right sides to obtain ECMA-262 conversion order.
4186 cc = lt;
4187 __ mov(a1, result_register());
4188 __ pop(a0);
4189 break;
4190 case Token::LTE:
4191 // Reverse left and right sides to obtain ECMA-262 conversion order.
4192 cc = ge;
4193 __ mov(a1, result_register());
4194 __ pop(a0);
4195 break;
4196 case Token::GTE:
4197 cc = ge;
4198 __ mov(a0, result_register());
4199 __ pop(a1);
4200 break;
4201 case Token::IN:
4202 case Token::INSTANCEOF:
4203 default:
4204 UNREACHABLE();
4205 }
4206
4207 bool inline_smi_code = ShouldInlineSmiCase(op);
4208 JumpPatchSite patch_site(masm_);
4209 if (inline_smi_code) {
4210 Label slow_case;
4211 __ Or(a2, a0, Operand(a1));
4212 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4213 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4214 __ bind(&slow_case);
4215 }
4216 // Record position and call the compare IC.
4217 SetSourcePosition(expr->position());
4218 Handle<Code> ic = CompareIC::GetUninitialized(op);
4219 EmitCallIC(ic, &patch_site, expr->id());
4220 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4221 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4222 }
4223 }
4224
4225 // Convert the result of the comparison into one expected for this
4226 // expression's context.
4227 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004228}
4229
4230
lrn@chromium.org7516f052011-03-30 08:52:27 +00004231void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004232 Comment cmnt(masm_, "[ CompareToNull");
4233 Label materialize_true, materialize_false;
4234 Label* if_true = NULL;
4235 Label* if_false = NULL;
4236 Label* fall_through = NULL;
4237 context()->PrepareTest(&materialize_true, &materialize_false,
4238 &if_true, &if_false, &fall_through);
4239
4240 VisitForAccumulatorValue(expr->expression());
4241 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4242 __ mov(a0, result_register());
4243 __ LoadRoot(a1, Heap::kNullValueRootIndex);
4244 if (expr->is_strict()) {
4245 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4246 } else {
4247 __ Branch(if_true, eq, a0, Operand(a1));
4248 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
4249 __ Branch(if_true, eq, a0, Operand(a1));
4250 __ And(at, a0, Operand(kSmiTagMask));
4251 __ Branch(if_false, eq, at, Operand(zero_reg));
4252 // It can be an undetectable object.
4253 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4254 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4255 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4256 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4257 }
4258 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004259}
4260
4261
ager@chromium.org5c838252010-02-19 08:53:10 +00004262void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004263 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4264 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004265}
4266
4267
lrn@chromium.org7516f052011-03-30 08:52:27 +00004268Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004269 return v0;
4270}
ager@chromium.org5c838252010-02-19 08:53:10 +00004271
4272
lrn@chromium.org7516f052011-03-30 08:52:27 +00004273Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004274 return cp;
4275}
4276
4277
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004278void FullCodeGenerator::EmitCallIC(Handle<Code> ic,
4279 RelocInfo::Mode mode,
4280 unsigned ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004281 ASSERT(mode == RelocInfo::CODE_TARGET ||
danno@chromium.org40cb8782011-05-25 07:58:50 +00004282 mode == RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004283 Counters* counters = isolate()->counters();
4284 switch (ic->kind()) {
4285 case Code::LOAD_IC:
4286 __ IncrementCounter(counters->named_load_full(), 1, a1, a2);
4287 break;
4288 case Code::KEYED_LOAD_IC:
4289 __ IncrementCounter(counters->keyed_load_full(), 1, a1, a2);
4290 break;
4291 case Code::STORE_IC:
4292 __ IncrementCounter(counters->named_store_full(), 1, a1, a2);
4293 break;
4294 case Code::KEYED_STORE_IC:
4295 __ IncrementCounter(counters->keyed_store_full(), 1, a1, a2);
4296 default:
4297 break;
4298 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00004299 if (ast_id == kNoASTId || mode == RelocInfo::CODE_TARGET_CONTEXT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004300 __ Call(ic, mode);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004301 } else {
4302 ASSERT(mode == RelocInfo::CODE_TARGET);
4303 mode = RelocInfo::CODE_TARGET_WITH_ID;
4304 __ CallWithAstId(ic, mode, ast_id);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004305 }
4306}
4307
4308
4309void FullCodeGenerator::EmitCallIC(Handle<Code> ic,
4310 JumpPatchSite* patch_site,
4311 unsigned ast_id) {
4312 Counters* counters = isolate()->counters();
4313 switch (ic->kind()) {
4314 case Code::LOAD_IC:
4315 __ IncrementCounter(counters->named_load_full(), 1, a1, a2);
4316 break;
4317 case Code::KEYED_LOAD_IC:
4318 __ IncrementCounter(counters->keyed_load_full(), 1, a1, a2);
4319 break;
4320 case Code::STORE_IC:
4321 __ IncrementCounter(counters->named_store_full(), 1, a1, a2);
4322 break;
4323 case Code::KEYED_STORE_IC:
4324 __ IncrementCounter(counters->keyed_store_full(), 1, a1, a2);
4325 default:
4326 break;
4327 }
4328
danno@chromium.org40cb8782011-05-25 07:58:50 +00004329 if (ast_id == kNoASTId) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004330 __ Call(ic, RelocInfo::CODE_TARGET);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004331 } else {
4332 __ CallWithAstId(ic, RelocInfo::CODE_TARGET_WITH_ID, ast_id);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004333 }
4334 if (patch_site != NULL && patch_site->is_bound()) {
4335 patch_site->EmitPatchInfo();
4336 } else {
4337 __ nop(); // Signals no inlined code.
4338 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00004339}
ager@chromium.org5c838252010-02-19 08:53:10 +00004340
4341
4342void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004343 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4344 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004345}
4346
4347
4348void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004349 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004350}
4351
4352
4353// ----------------------------------------------------------------------------
4354// Non-local control flow support.
4355
4356void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004357 ASSERT(!result_register().is(a1));
4358 // Store result register while executing finally block.
4359 __ push(result_register());
4360 // Cook return address in link register to stack (smi encoded Code* delta).
4361 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4362 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4363 ASSERT_EQ(0, kSmiTag);
4364 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4365 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004366}
4367
4368
4369void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004370 ASSERT(!result_register().is(a1));
4371 // Restore result register from stack.
4372 __ pop(a1);
4373 // Uncook return address and return.
4374 __ pop(result_register());
4375 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4376 __ sra(a1, a1, 1); // Un-smi-tag value.
4377 __ Addu(at, a1, Operand(masm_->CodeObject()));
4378 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004379}
4380
4381
4382#undef __
4383
4384} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004385
4386#endif // V8_TARGET_ARCH_MIPS