blob: 8381eefffd6b1e91795333e70be8c4df9c10752f [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 {
192 __ CallRuntime(Runtime::kNewContext, 1);
193 }
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.
1131 __ lw(next, ContextOperand(current, Context::CLOSURE_INDEX));
1132 __ lw(next, FieldMemOperand(next, JSFunction::kContextOffset));
1133 // Walk the rest of the chain without clobbering cp.
1134 current = next;
1135 }
1136 // If no outer scope calls eval, we do not need to check more
1137 // context extensions.
1138 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
1139 s = s->outer_scope();
1140 }
1141
1142 if (s->is_eval_scope()) {
1143 Label loop, fast;
1144 if (!current.is(next)) {
1145 __ Move(next, current);
1146 }
1147 __ bind(&loop);
1148 // Terminate at global context.
1149 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1150 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1151 __ Branch(&fast, eq, temp, Operand(t0));
1152 // Check that extension is NULL.
1153 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1154 __ Branch(slow, ne, temp, Operand(zero_reg));
1155 // Load next context in chain.
1156 __ lw(next, ContextOperand(next, Context::CLOSURE_INDEX));
1157 __ lw(next, FieldMemOperand(next, JSFunction::kContextOffset));
1158 __ Branch(&loop);
1159 __ bind(&fast);
1160 }
1161
1162 __ lw(a0, GlobalObjectOperand());
1163 __ li(a2, Operand(slot->var()->name()));
1164 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1165 ? RelocInfo::CODE_TARGET
1166 : RelocInfo::CODE_TARGET_CONTEXT;
1167 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1168 EmitCallIC(ic, mode, AstNode::kNoNumber);
ager@chromium.org5c838252010-02-19 08:53:10 +00001169}
1170
1171
lrn@chromium.org7516f052011-03-30 08:52:27 +00001172MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(
1173 Slot* slot,
1174 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001175 ASSERT(slot->type() == Slot::CONTEXT);
1176 Register context = cp;
1177 Register next = a3;
1178 Register temp = t0;
1179
1180 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
1181 if (s->num_heap_slots() > 0) {
1182 if (s->calls_eval()) {
1183 // Check that extension is NULL.
1184 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1185 __ Branch(slow, ne, temp, Operand(zero_reg));
1186 }
1187 __ lw(next, ContextOperand(context, Context::CLOSURE_INDEX));
1188 __ lw(next, FieldMemOperand(next, JSFunction::kContextOffset));
1189 // Walk the rest of the chain without clobbering cp.
1190 context = next;
1191 }
1192 }
1193 // Check that last extension is NULL.
1194 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1195 __ Branch(slow, ne, temp, Operand(zero_reg));
1196
1197 // This function is used only for loads, not stores, so it's safe to
1198 // return an cp-based operand (the write barrier cannot be allowed to
1199 // destroy the cp register).
1200 return ContextOperand(context, slot->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001201}
1202
1203
1204void FullCodeGenerator::EmitDynamicLoadFromSlotFastCase(
1205 Slot* slot,
1206 TypeofState typeof_state,
1207 Label* slow,
1208 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001209 // Generate fast-case code for variables that might be shadowed by
1210 // eval-introduced variables. Eval is used a lot without
1211 // introducing variables. In those cases, we do not want to
1212 // perform a runtime call for all variables in the scope
1213 // containing the eval.
1214 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
1215 EmitLoadGlobalSlotCheckExtensions(slot, typeof_state, slow);
1216 __ Branch(done);
1217 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
1218 Slot* potential_slot = slot->var()->local_if_not_shadowed()->AsSlot();
1219 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
1220 if (potential_slot != NULL) {
1221 // Generate fast case for locals that rewrite to slots.
1222 __ lw(v0, ContextSlotOperandCheckExtensions(potential_slot, slow));
1223 if (potential_slot->var()->mode() == Variable::CONST) {
1224 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1225 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1226 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1227 __ movz(v0, a0, at); // Conditional move.
1228 }
1229 __ Branch(done);
1230 } else if (rewrite != NULL) {
1231 // Generate fast case for calls of an argument function.
1232 Property* property = rewrite->AsProperty();
1233 if (property != NULL) {
1234 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
1235 Literal* key_literal = property->key()->AsLiteral();
1236 if (obj_proxy != NULL &&
1237 key_literal != NULL &&
1238 obj_proxy->IsArguments() &&
1239 key_literal->handle()->IsSmi()) {
1240 // Load arguments object if there are no eval-introduced
1241 // variables. Then load the argument from the arguments
1242 // object using keyed load.
1243 __ lw(a1,
1244 ContextSlotOperandCheckExtensions(obj_proxy->var()->AsSlot(),
1245 slow));
1246 __ li(a0, Operand(key_literal->handle()));
1247 Handle<Code> ic =
1248 isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001249 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(property));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001250 __ Branch(done);
1251 }
1252 }
1253 }
1254 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001255}
1256
1257
1258void FullCodeGenerator::EmitVariableLoad(Variable* var) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001259 // Four cases: non-this global variables, lookup slots, all other
1260 // types of slots, and parameters that rewrite to explicit property
1261 // accesses on the arguments object.
1262 Slot* slot = var->AsSlot();
1263 Property* property = var->AsProperty();
1264
1265 if (var->is_global() && !var->is_this()) {
1266 Comment cmnt(masm_, "Global variable");
1267 // Use inline caching. Variable name is passed in a2 and the global
1268 // object (receiver) in a0.
1269 __ lw(a0, GlobalObjectOperand());
1270 __ li(a2, Operand(var->name()));
1271 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1272 EmitCallIC(ic, RelocInfo::CODE_TARGET_CONTEXT, AstNode::kNoNumber);
1273 context()->Plug(v0);
1274
1275 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
1276 Label done, slow;
1277
1278 // Generate code for loading from variables potentially shadowed
1279 // by eval-introduced variables.
1280 EmitDynamicLoadFromSlotFastCase(slot, NOT_INSIDE_TYPEOF, &slow, &done);
1281
1282 __ bind(&slow);
1283 Comment cmnt(masm_, "Lookup slot");
1284 __ li(a1, Operand(var->name()));
1285 __ Push(cp, a1); // Context and name.
1286 __ CallRuntime(Runtime::kLoadContextSlot, 2);
1287 __ bind(&done);
1288
1289 context()->Plug(v0);
1290
1291 } else if (slot != NULL) {
1292 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
1293 ? "Context slot"
1294 : "Stack slot");
1295 if (var->mode() == Variable::CONST) {
1296 // Constants may be the hole value if they have not been initialized.
1297 // Unhole them.
1298 MemOperand slot_operand = EmitSlotSearch(slot, a0);
1299 __ lw(v0, slot_operand);
1300 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1301 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1302 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1303 __ movz(v0, a0, at); // Conditional move.
1304 context()->Plug(v0);
1305 } else {
1306 context()->Plug(slot);
1307 }
1308 } else {
1309 Comment cmnt(masm_, "Rewritten parameter");
1310 ASSERT_NOT_NULL(property);
1311 // Rewritten parameter accesses are of the form "slot[literal]".
1312 // Assert that the object is in a slot.
1313 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
1314 ASSERT_NOT_NULL(object_var);
1315 Slot* object_slot = object_var->AsSlot();
1316 ASSERT_NOT_NULL(object_slot);
1317
1318 // Load the object.
1319 Move(a1, object_slot);
1320
1321 // Assert that the key is a smi.
1322 Literal* key_literal = property->key()->AsLiteral();
1323 ASSERT_NOT_NULL(key_literal);
1324 ASSERT(key_literal->handle()->IsSmi());
1325
1326 // Load the key.
1327 __ li(a0, Operand(key_literal->handle()));
1328
1329 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1330 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001331 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(property));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001332 context()->Plug(v0);
1333 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001334}
1335
1336
1337void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001338 Comment cmnt(masm_, "[ RegExpLiteral");
1339 Label materialized;
1340 // Registers will be used as follows:
1341 // t1 = materialized value (RegExp literal)
1342 // t0 = JS function, literals array
1343 // a3 = literal index
1344 // a2 = RegExp pattern
1345 // a1 = RegExp flags
1346 // a0 = RegExp literal clone
1347 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1348 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1349 int literal_offset =
1350 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1351 __ lw(t1, FieldMemOperand(t0, literal_offset));
1352 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1353 __ Branch(&materialized, ne, t1, Operand(at));
1354
1355 // Create regexp literal using runtime function.
1356 // Result will be in v0.
1357 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1358 __ li(a2, Operand(expr->pattern()));
1359 __ li(a1, Operand(expr->flags()));
1360 __ Push(t0, a3, a2, a1);
1361 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1362 __ mov(t1, v0);
1363
1364 __ bind(&materialized);
1365 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1366 Label allocated, runtime_allocate;
1367 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1368 __ jmp(&allocated);
1369
1370 __ bind(&runtime_allocate);
1371 __ push(t1);
1372 __ li(a0, Operand(Smi::FromInt(size)));
1373 __ push(a0);
1374 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1375 __ pop(t1);
1376
1377 __ bind(&allocated);
1378
1379 // After this, registers are used as follows:
1380 // v0: Newly allocated regexp.
1381 // t1: Materialized regexp.
1382 // a2: temp.
1383 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1384 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001385}
1386
1387
1388void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001389 Comment cmnt(masm_, "[ ObjectLiteral");
1390 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1391 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1392 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1393 __ li(a1, Operand(expr->constant_properties()));
1394 int flags = expr->fast_elements()
1395 ? ObjectLiteral::kFastElements
1396 : ObjectLiteral::kNoFlags;
1397 flags |= expr->has_function()
1398 ? ObjectLiteral::kHasFunction
1399 : ObjectLiteral::kNoFlags;
1400 __ li(a0, Operand(Smi::FromInt(flags)));
1401 __ Push(a3, a2, a1, a0);
1402 if (expr->depth() > 1) {
1403 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1404 } else {
1405 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
1406 }
1407
1408 // If result_saved is true the result is on top of the stack. If
1409 // result_saved is false the result is in v0.
1410 bool result_saved = false;
1411
1412 // Mark all computed expressions that are bound to a key that
1413 // is shadowed by a later occurrence of the same key. For the
1414 // marked expressions, no store code is emitted.
1415 expr->CalculateEmitStore();
1416
1417 for (int i = 0; i < expr->properties()->length(); i++) {
1418 ObjectLiteral::Property* property = expr->properties()->at(i);
1419 if (property->IsCompileTimeValue()) continue;
1420
1421 Literal* key = property->key();
1422 Expression* value = property->value();
1423 if (!result_saved) {
1424 __ push(v0); // Save result on stack.
1425 result_saved = true;
1426 }
1427 switch (property->kind()) {
1428 case ObjectLiteral::Property::CONSTANT:
1429 UNREACHABLE();
1430 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1431 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1432 // Fall through.
1433 case ObjectLiteral::Property::COMPUTED:
1434 if (key->handle()->IsSymbol()) {
1435 if (property->emit_store()) {
1436 VisitForAccumulatorValue(value);
1437 __ mov(a0, result_register());
1438 __ li(a2, Operand(key->handle()));
1439 __ lw(a1, MemOperand(sp));
1440 Handle<Code> ic = is_strict_mode()
1441 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1442 : isolate()->builtins()->StoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001443 EmitCallIC(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001444 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1445 } else {
1446 VisitForEffect(value);
1447 }
1448 break;
1449 }
1450 // Fall through.
1451 case ObjectLiteral::Property::PROTOTYPE:
1452 // Duplicate receiver on stack.
1453 __ lw(a0, MemOperand(sp));
1454 __ push(a0);
1455 VisitForStackValue(key);
1456 VisitForStackValue(value);
1457 if (property->emit_store()) {
1458 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1459 __ push(a0);
1460 __ CallRuntime(Runtime::kSetProperty, 4);
1461 } else {
1462 __ Drop(3);
1463 }
1464 break;
1465 case ObjectLiteral::Property::GETTER:
1466 case ObjectLiteral::Property::SETTER:
1467 // Duplicate receiver on stack.
1468 __ lw(a0, MemOperand(sp));
1469 __ push(a0);
1470 VisitForStackValue(key);
1471 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1472 Smi::FromInt(1) :
1473 Smi::FromInt(0)));
1474 __ push(a1);
1475 VisitForStackValue(value);
1476 __ CallRuntime(Runtime::kDefineAccessor, 4);
1477 break;
1478 }
1479 }
1480
1481 if (expr->has_function()) {
1482 ASSERT(result_saved);
1483 __ lw(a0, MemOperand(sp));
1484 __ push(a0);
1485 __ CallRuntime(Runtime::kToFastProperties, 1);
1486 }
1487
1488 if (result_saved) {
1489 context()->PlugTOS();
1490 } else {
1491 context()->Plug(v0);
1492 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001493}
1494
1495
1496void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001497 Comment cmnt(masm_, "[ ArrayLiteral");
1498
1499 ZoneList<Expression*>* subexprs = expr->values();
1500 int length = subexprs->length();
1501 __ mov(a0, result_register());
1502 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1503 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1504 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1505 __ li(a1, Operand(expr->constant_elements()));
1506 __ Push(a3, a2, a1);
1507 if (expr->constant_elements()->map() ==
1508 isolate()->heap()->fixed_cow_array_map()) {
1509 FastCloneShallowArrayStub stub(
1510 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1511 __ CallStub(&stub);
1512 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1513 1, a1, a2);
1514 } else if (expr->depth() > 1) {
1515 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1516 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1517 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1518 } else {
1519 FastCloneShallowArrayStub stub(
1520 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
1521 __ CallStub(&stub);
1522 }
1523
1524 bool result_saved = false; // Is the result saved to the stack?
1525
1526 // Emit code to evaluate all the non-constant subexpressions and to store
1527 // them into the newly cloned array.
1528 for (int i = 0; i < length; i++) {
1529 Expression* subexpr = subexprs->at(i);
1530 // If the subexpression is a literal or a simple materialized literal it
1531 // is already set in the cloned array.
1532 if (subexpr->AsLiteral() != NULL ||
1533 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1534 continue;
1535 }
1536
1537 if (!result_saved) {
1538 __ push(v0);
1539 result_saved = true;
1540 }
1541 VisitForAccumulatorValue(subexpr);
1542
1543 // Store the subexpression value in the array's elements.
1544 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1545 __ lw(a1, FieldMemOperand(a1, JSObject::kElementsOffset));
1546 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1547 __ sw(result_register(), FieldMemOperand(a1, offset));
1548
1549 // Update the write barrier for the array store with v0 as the scratch
1550 // register.
1551 __ li(a2, Operand(offset));
1552 // TODO(PJ): double check this RecordWrite call.
1553 __ RecordWrite(a1, a2, result_register());
1554
1555 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1556 }
1557
1558 if (result_saved) {
1559 context()->PlugTOS();
1560 } else {
1561 context()->Plug(v0);
1562 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001563}
1564
1565
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001566void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001567 Comment cmnt(masm_, "[ Assignment");
1568 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1569 // on the left-hand side.
1570 if (!expr->target()->IsValidLeftHandSide()) {
1571 VisitForEffect(expr->target());
1572 return;
1573 }
1574
1575 // Left-hand side can only be a property, a global or a (parameter or local)
1576 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1577 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1578 LhsKind assign_type = VARIABLE;
1579 Property* property = expr->target()->AsProperty();
1580 if (property != NULL) {
1581 assign_type = (property->key()->IsPropertyName())
1582 ? NAMED_PROPERTY
1583 : KEYED_PROPERTY;
1584 }
1585
1586 // Evaluate LHS expression.
1587 switch (assign_type) {
1588 case VARIABLE:
1589 // Nothing to do here.
1590 break;
1591 case NAMED_PROPERTY:
1592 if (expr->is_compound()) {
1593 // We need the receiver both on the stack and in the accumulator.
1594 VisitForAccumulatorValue(property->obj());
1595 __ push(result_register());
1596 } else {
1597 VisitForStackValue(property->obj());
1598 }
1599 break;
1600 case KEYED_PROPERTY:
1601 // We need the key and receiver on both the stack and in v0 and a1.
1602 if (expr->is_compound()) {
1603 if (property->is_arguments_access()) {
1604 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
1605 __ lw(v0, EmitSlotSearch(obj_proxy->var()->AsSlot(), v0));
1606 __ push(v0);
1607 __ li(v0, Operand(property->key()->AsLiteral()->handle()));
1608 } else {
1609 VisitForStackValue(property->obj());
1610 VisitForAccumulatorValue(property->key());
1611 }
1612 __ lw(a1, MemOperand(sp, 0));
1613 __ push(v0);
1614 } else {
1615 if (property->is_arguments_access()) {
1616 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
1617 __ lw(a1, EmitSlotSearch(obj_proxy->var()->AsSlot(), v0));
1618 __ li(v0, Operand(property->key()->AsLiteral()->handle()));
1619 __ Push(a1, v0);
1620 } else {
1621 VisitForStackValue(property->obj());
1622 VisitForStackValue(property->key());
1623 }
1624 }
1625 break;
1626 }
1627
1628 // For compound assignments we need another deoptimization point after the
1629 // variable/property load.
1630 if (expr->is_compound()) {
1631 { AccumulatorValueContext context(this);
1632 switch (assign_type) {
1633 case VARIABLE:
1634 EmitVariableLoad(expr->target()->AsVariableProxy()->var());
1635 PrepareForBailout(expr->target(), TOS_REG);
1636 break;
1637 case NAMED_PROPERTY:
1638 EmitNamedPropertyLoad(property);
1639 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1640 break;
1641 case KEYED_PROPERTY:
1642 EmitKeyedPropertyLoad(property);
1643 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1644 break;
1645 }
1646 }
1647
1648 Token::Value op = expr->binary_op();
1649 __ push(v0); // Left operand goes on the stack.
1650 VisitForAccumulatorValue(expr->value());
1651
1652 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1653 ? OVERWRITE_RIGHT
1654 : NO_OVERWRITE;
1655 SetSourcePosition(expr->position() + 1);
1656 AccumulatorValueContext context(this);
1657 if (ShouldInlineSmiCase(op)) {
1658 EmitInlineSmiBinaryOp(expr->binary_operation(),
1659 op,
1660 mode,
1661 expr->target(),
1662 expr->value());
1663 } else {
1664 EmitBinaryOp(expr->binary_operation(), op, mode);
1665 }
1666
1667 // Deoptimization point in case the binary operation may have side effects.
1668 PrepareForBailout(expr->binary_operation(), TOS_REG);
1669 } else {
1670 VisitForAccumulatorValue(expr->value());
1671 }
1672
1673 // Record source position before possible IC call.
1674 SetSourcePosition(expr->position());
1675
1676 // Store the value.
1677 switch (assign_type) {
1678 case VARIABLE:
1679 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1680 expr->op());
1681 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1682 context()->Plug(v0);
1683 break;
1684 case NAMED_PROPERTY:
1685 EmitNamedPropertyAssignment(expr);
1686 break;
1687 case KEYED_PROPERTY:
1688 EmitKeyedPropertyAssignment(expr);
1689 break;
1690 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001691}
1692
1693
ager@chromium.org5c838252010-02-19 08:53:10 +00001694void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001695 SetSourcePosition(prop->position());
1696 Literal* key = prop->key()->AsLiteral();
1697 __ mov(a0, result_register());
1698 __ li(a2, Operand(key->handle()));
1699 // Call load IC. It has arguments receiver and property name a0 and a2.
1700 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001701 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001702}
1703
1704
1705void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001706 SetSourcePosition(prop->position());
1707 __ mov(a0, result_register());
1708 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1709 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001710 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001711}
1712
1713
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001714void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001715 Token::Value op,
1716 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001717 Expression* left_expr,
1718 Expression* right_expr) {
1719 Label done, smi_case, stub_call;
1720
1721 Register scratch1 = a2;
1722 Register scratch2 = a3;
1723
1724 // Get the arguments.
1725 Register left = a1;
1726 Register right = a0;
1727 __ pop(left);
1728 __ mov(a0, result_register());
1729
1730 // Perform combined smi check on both operands.
1731 __ Or(scratch1, left, Operand(right));
1732 STATIC_ASSERT(kSmiTag == 0);
1733 JumpPatchSite patch_site(masm_);
1734 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1735
1736 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001737 BinaryOpStub stub(op, mode);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001738 EmitCallIC(stub.GetCode(), &patch_site, expr->id());
1739 __ jmp(&done);
1740
1741 __ bind(&smi_case);
1742 // Smi case. This code works the same way as the smi-smi case in the type
1743 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001744 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001745 switch (op) {
1746 case Token::SAR:
1747 __ Branch(&stub_call);
1748 __ GetLeastBitsFromSmi(scratch1, right, 5);
1749 __ srav(right, left, scratch1);
1750 __ And(v0, right, Operand(~kSmiTagMask));
1751 break;
1752 case Token::SHL: {
1753 __ Branch(&stub_call);
1754 __ SmiUntag(scratch1, left);
1755 __ GetLeastBitsFromSmi(scratch2, right, 5);
1756 __ sllv(scratch1, scratch1, scratch2);
1757 __ Addu(scratch2, scratch1, Operand(0x40000000));
1758 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1759 __ SmiTag(v0, scratch1);
1760 break;
1761 }
1762 case Token::SHR: {
1763 __ Branch(&stub_call);
1764 __ SmiUntag(scratch1, left);
1765 __ GetLeastBitsFromSmi(scratch2, right, 5);
1766 __ srlv(scratch1, scratch1, scratch2);
1767 __ And(scratch2, scratch1, 0xc0000000);
1768 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1769 __ SmiTag(v0, scratch1);
1770 break;
1771 }
1772 case Token::ADD:
1773 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1774 __ BranchOnOverflow(&stub_call, scratch1);
1775 break;
1776 case Token::SUB:
1777 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1778 __ BranchOnOverflow(&stub_call, scratch1);
1779 break;
1780 case Token::MUL: {
1781 __ SmiUntag(scratch1, right);
1782 __ Mult(left, scratch1);
1783 __ mflo(scratch1);
1784 __ mfhi(scratch2);
1785 __ sra(scratch1, scratch1, 31);
1786 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1787 __ mflo(v0);
1788 __ Branch(&done, ne, v0, Operand(zero_reg));
1789 __ Addu(scratch2, right, left);
1790 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1791 ASSERT(Smi::FromInt(0) == 0);
1792 __ mov(v0, zero_reg);
1793 break;
1794 }
1795 case Token::BIT_OR:
1796 __ Or(v0, left, Operand(right));
1797 break;
1798 case Token::BIT_AND:
1799 __ And(v0, left, Operand(right));
1800 break;
1801 case Token::BIT_XOR:
1802 __ Xor(v0, left, Operand(right));
1803 break;
1804 default:
1805 UNREACHABLE();
1806 }
1807
1808 __ bind(&done);
1809 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001810}
1811
1812
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001813void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1814 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001815 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001816 __ mov(a0, result_register());
1817 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001818 BinaryOpStub stub(op, mode);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001819 EmitCallIC(stub.GetCode(), NULL, expr->id());
1820 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001821}
1822
1823
1824void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001825 // Invalid left-hand sides are rewritten to have a 'throw
1826 // ReferenceError' on the left-hand side.
1827 if (!expr->IsValidLeftHandSide()) {
1828 VisitForEffect(expr);
1829 return;
1830 }
1831
1832 // Left-hand side can only be a property, a global or a (parameter or local)
1833 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1834 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1835 LhsKind assign_type = VARIABLE;
1836 Property* prop = expr->AsProperty();
1837 if (prop != NULL) {
1838 assign_type = (prop->key()->IsPropertyName())
1839 ? NAMED_PROPERTY
1840 : KEYED_PROPERTY;
1841 }
1842
1843 switch (assign_type) {
1844 case VARIABLE: {
1845 Variable* var = expr->AsVariableProxy()->var();
1846 EffectContext context(this);
1847 EmitVariableAssignment(var, Token::ASSIGN);
1848 break;
1849 }
1850 case NAMED_PROPERTY: {
1851 __ push(result_register()); // Preserve value.
1852 VisitForAccumulatorValue(prop->obj());
1853 __ mov(a1, result_register());
1854 __ pop(a0); // Restore value.
1855 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1856 Handle<Code> ic = is_strict_mode()
1857 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1858 : isolate()->builtins()->StoreIC_Initialize();
1859 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
1860 break;
1861 }
1862 case KEYED_PROPERTY: {
1863 __ push(result_register()); // Preserve value.
1864 if (prop->is_synthetic()) {
1865 ASSERT(prop->obj()->AsVariableProxy() != NULL);
1866 ASSERT(prop->key()->AsLiteral() != NULL);
1867 { AccumulatorValueContext for_object(this);
1868 EmitVariableLoad(prop->obj()->AsVariableProxy()->var());
1869 }
1870 __ mov(a2, result_register());
1871 __ li(a1, Operand(prop->key()->AsLiteral()->handle()));
1872 } else {
1873 VisitForStackValue(prop->obj());
1874 VisitForAccumulatorValue(prop->key());
1875 __ mov(a1, result_register());
1876 __ pop(a2);
1877 }
1878 __ pop(a0); // Restore value.
1879 Handle<Code> ic = is_strict_mode()
1880 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
1881 : isolate()->builtins()->KeyedStoreIC_Initialize();
1882 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
1883 break;
1884 }
1885 }
1886 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1887 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001888}
1889
1890
1891void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001892 Token::Value op) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001893 // Left-hand sides that rewrite to explicit property accesses do not reach
1894 // here.
1895 ASSERT(var != NULL);
1896 ASSERT(var->is_global() || var->AsSlot() != NULL);
1897
1898 if (var->is_global()) {
1899 ASSERT(!var->is_this());
1900 // Assignment to a global variable. Use inline caching for the
1901 // assignment. Right-hand-side value is passed in a0, variable name in
1902 // a2, and the global object in a1.
1903 __ mov(a0, result_register());
1904 __ li(a2, Operand(var->name()));
1905 __ lw(a1, GlobalObjectOperand());
1906 Handle<Code> ic = is_strict_mode()
1907 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1908 : isolate()->builtins()->StoreIC_Initialize();
1909 EmitCallIC(ic, RelocInfo::CODE_TARGET_CONTEXT, AstNode::kNoNumber);
1910
1911 } else if (op == Token::INIT_CONST) {
1912 // Like var declarations, const declarations are hoisted to function
1913 // scope. However, unlike var initializers, const initializers are able
1914 // to drill a hole to that function context, even from inside a 'with'
1915 // context. We thus bypass the normal static scope lookup.
1916 Slot* slot = var->AsSlot();
1917 Label skip;
1918 switch (slot->type()) {
1919 case Slot::PARAMETER:
1920 // No const parameters.
1921 UNREACHABLE();
1922 break;
1923 case Slot::LOCAL:
1924 // Detect const reinitialization by checking for the hole value.
1925 __ lw(a1, MemOperand(fp, SlotOffset(slot)));
1926 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1927 __ Branch(&skip, ne, a1, Operand(t0));
1928 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1929 break;
1930 case Slot::CONTEXT: {
1931 __ lw(a1, ContextOperand(cp, Context::FCONTEXT_INDEX));
1932 __ lw(a2, ContextOperand(a1, slot->index()));
1933 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1934 __ Branch(&skip, ne, a2, Operand(t0));
1935 __ sw(result_register(), ContextOperand(a1, slot->index()));
1936 int offset = Context::SlotOffset(slot->index());
1937 __ mov(a3, result_register()); // Preserve the stored value in v0.
1938 __ RecordWrite(a1, Operand(offset), a3, a2);
1939 break;
1940 }
1941 case Slot::LOOKUP:
1942 __ push(result_register());
1943 __ li(a0, Operand(slot->var()->name()));
1944 __ Push(cp, a0); // Context and name.
1945 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
1946 break;
1947 }
1948 __ bind(&skip);
1949
1950 } else if (var->mode() != Variable::CONST) {
1951 // Perform the assignment for non-const variables. Const assignments
1952 // are simply skipped.
1953 Slot* slot = var->AsSlot();
1954 switch (slot->type()) {
1955 case Slot::PARAMETER:
1956 case Slot::LOCAL:
1957 // Perform the assignment.
1958 __ sw(result_register(), MemOperand(fp, SlotOffset(slot)));
1959 break;
1960
1961 case Slot::CONTEXT: {
1962 MemOperand target = EmitSlotSearch(slot, a1);
1963 // Perform the assignment and issue the write barrier.
1964 __ sw(result_register(), target);
1965 // RecordWrite may destroy all its register arguments.
1966 __ mov(a3, result_register());
1967 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
1968 __ RecordWrite(a1, Operand(offset), a2, a3);
1969 break;
1970 }
1971
1972 case Slot::LOOKUP:
1973 // Call the runtime for the assignment.
1974 __ push(v0); // Value.
1975 __ li(a1, Operand(slot->var()->name()));
1976 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1977 __ Push(cp, a1, a0); // Context, name, strict mode.
1978 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1979 break;
1980 }
1981 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001982}
1983
1984
1985void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001986 // Assignment to a property, using a named store IC.
1987 Property* prop = expr->target()->AsProperty();
1988 ASSERT(prop != NULL);
1989 ASSERT(prop->key()->AsLiteral() != NULL);
1990
1991 // If the assignment starts a block of assignments to the same object,
1992 // change to slow case to avoid the quadratic behavior of repeatedly
1993 // adding fast properties.
1994 if (expr->starts_initialization_block()) {
1995 __ push(result_register());
1996 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1997 __ push(t0);
1998 __ CallRuntime(Runtime::kToSlowProperties, 1);
1999 __ pop(result_register());
2000 }
2001
2002 // Record source code position before IC call.
2003 SetSourcePosition(expr->position());
2004 __ mov(a0, result_register()); // Load the value.
2005 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
2006 // Load receiver to a1. Leave a copy in the stack if needed for turning the
2007 // receiver into fast case.
2008 if (expr->ends_initialization_block()) {
2009 __ lw(a1, MemOperand(sp));
2010 } else {
2011 __ pop(a1);
2012 }
2013
2014 Handle<Code> ic = is_strict_mode()
2015 ? isolate()->builtins()->StoreIC_Initialize_Strict()
2016 : isolate()->builtins()->StoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00002017 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002018
2019 // If the assignment ends an initialization block, revert to fast case.
2020 if (expr->ends_initialization_block()) {
2021 __ push(v0); // Result of assignment, saved even if not needed.
2022 // Receiver is under the result value.
2023 __ lw(t0, MemOperand(sp, kPointerSize));
2024 __ push(t0);
2025 __ CallRuntime(Runtime::kToFastProperties, 1);
2026 __ pop(v0);
2027 __ Drop(1);
2028 }
2029 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2030 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002031}
2032
2033
2034void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002035 // Assignment to a property, using a keyed store IC.
2036
2037 // If the assignment starts a block of assignments to the same object,
2038 // change to slow case to avoid the quadratic behavior of repeatedly
2039 // adding fast properties.
2040 if (expr->starts_initialization_block()) {
2041 __ push(result_register());
2042 // Receiver is now under the key and value.
2043 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2044 __ push(t0);
2045 __ CallRuntime(Runtime::kToSlowProperties, 1);
2046 __ pop(result_register());
2047 }
2048
2049 // Record source code position before IC call.
2050 SetSourcePosition(expr->position());
2051 // Call keyed store IC.
2052 // The arguments are:
2053 // - a0 is the value,
2054 // - a1 is the key,
2055 // - a2 is the receiver.
2056 __ mov(a0, result_register());
2057 __ pop(a1); // Key.
2058 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2059 // receiver into fast case.
2060 if (expr->ends_initialization_block()) {
2061 __ lw(a2, MemOperand(sp));
2062 } else {
2063 __ pop(a2);
2064 }
2065
2066 Handle<Code> ic = is_strict_mode()
2067 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
2068 : isolate()->builtins()->KeyedStoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00002069 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002070
2071 // If the assignment ends an initialization block, revert to fast case.
2072 if (expr->ends_initialization_block()) {
2073 __ push(v0); // Result of assignment, saved even if not needed.
2074 // Receiver is under the result value.
2075 __ lw(t0, MemOperand(sp, kPointerSize));
2076 __ push(t0);
2077 __ CallRuntime(Runtime::kToFastProperties, 1);
2078 __ pop(v0);
2079 __ Drop(1);
2080 }
2081 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2082 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002083}
2084
2085
2086void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002087 Comment cmnt(masm_, "[ Property");
2088 Expression* key = expr->key();
2089
2090 if (key->IsPropertyName()) {
2091 VisitForAccumulatorValue(expr->obj());
2092 EmitNamedPropertyLoad(expr);
2093 context()->Plug(v0);
2094 } else {
2095 VisitForStackValue(expr->obj());
2096 VisitForAccumulatorValue(expr->key());
2097 __ pop(a1);
2098 EmitKeyedPropertyLoad(expr);
2099 context()->Plug(v0);
2100 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002101}
2102
lrn@chromium.org7516f052011-03-30 08:52:27 +00002103
ager@chromium.org5c838252010-02-19 08:53:10 +00002104void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002105 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002106 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002107 // Code common for calls using the IC.
2108 ZoneList<Expression*>* args = expr->arguments();
2109 int arg_count = args->length();
2110 { PreservePositionScope scope(masm()->positions_recorder());
2111 for (int i = 0; i < arg_count; i++) {
2112 VisitForStackValue(args->at(i));
2113 }
2114 __ li(a2, Operand(name));
2115 }
2116 // Record source position for debugger.
2117 SetSourcePosition(expr->position());
2118 // Call the IC initialization code.
2119 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2120 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00002121 isolate()->stub_cache()->ComputeCallInitialize(arg_count, in_loop, mode);
2122 EmitCallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002123 RecordJSReturnSite(expr);
2124 // Restore context register.
2125 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2126 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002127}
2128
2129
lrn@chromium.org7516f052011-03-30 08:52:27 +00002130void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002131 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002132 // Load the key.
2133 VisitForAccumulatorValue(key);
2134
2135 // Swap the name of the function and the receiver on the stack to follow
2136 // the calling convention for call ICs.
2137 __ pop(a1);
2138 __ push(v0);
2139 __ push(a1);
2140
2141 // Code common for calls using the IC.
2142 ZoneList<Expression*>* args = expr->arguments();
2143 int arg_count = args->length();
2144 { PreservePositionScope scope(masm()->positions_recorder());
2145 for (int i = 0; i < arg_count; i++) {
2146 VisitForStackValue(args->at(i));
2147 }
2148 }
2149 // Record source position for debugger.
2150 SetSourcePosition(expr->position());
2151 // Call the IC initialization code.
2152 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2153 Handle<Code> ic =
2154 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count, in_loop);
2155 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002156 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002157 RecordJSReturnSite(expr);
2158 // Restore context register.
2159 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2160 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002161}
2162
2163
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002164void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002165 // Code common for calls using the call stub.
2166 ZoneList<Expression*>* args = expr->arguments();
2167 int arg_count = args->length();
2168 { PreservePositionScope scope(masm()->positions_recorder());
2169 for (int i = 0; i < arg_count; i++) {
2170 VisitForStackValue(args->at(i));
2171 }
2172 }
2173 // Record source position for debugger.
2174 SetSourcePosition(expr->position());
2175 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
2176 CallFunctionStub stub(arg_count, in_loop, flags);
2177 __ CallStub(&stub);
2178 RecordJSReturnSite(expr);
2179 // Restore context register.
2180 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2181 context()->DropAndPlug(1, v0);
2182}
2183
2184
2185void FullCodeGenerator::EmitResolvePossiblyDirectEval(ResolveEvalFlag flag,
2186 int arg_count) {
2187 // Push copy of the first argument or undefined if it doesn't exist.
2188 if (arg_count > 0) {
2189 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2190 } else {
2191 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2192 }
2193 __ push(a1);
2194
2195 // Push the receiver of the enclosing function and do runtime call.
2196 __ lw(a1, MemOperand(fp, (2 + scope()->num_parameters()) * kPointerSize));
2197 __ push(a1);
2198 // Push the strict mode flag.
2199 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
2200 __ push(a1);
2201
2202 __ CallRuntime(flag == SKIP_CONTEXT_LOOKUP
2203 ? Runtime::kResolvePossiblyDirectEvalNoLookup
2204 : Runtime::kResolvePossiblyDirectEval, 4);
ager@chromium.org5c838252010-02-19 08:53:10 +00002205}
2206
2207
2208void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002209#ifdef DEBUG
2210 // We want to verify that RecordJSReturnSite gets called on all paths
2211 // through this function. Avoid early returns.
2212 expr->return_is_recorded_ = false;
2213#endif
2214
2215 Comment cmnt(masm_, "[ Call");
2216 Expression* fun = expr->expression();
2217 Variable* var = fun->AsVariableProxy()->AsVariable();
2218
2219 if (var != NULL && var->is_possibly_eval()) {
2220 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2221 // resolve the function we need to call and the receiver of the
2222 // call. Then we call the resolved function using the given
2223 // arguments.
2224 ZoneList<Expression*>* args = expr->arguments();
2225 int arg_count = args->length();
2226
2227 { PreservePositionScope pos_scope(masm()->positions_recorder());
2228 VisitForStackValue(fun);
2229 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2230 __ push(a2); // Reserved receiver slot.
2231
2232 // Push the arguments.
2233 for (int i = 0; i < arg_count; i++) {
2234 VisitForStackValue(args->at(i));
2235 }
2236 // If we know that eval can only be shadowed by eval-introduced
2237 // variables we attempt to load the global eval function directly
2238 // in generated code. If we succeed, there is no need to perform a
2239 // context lookup in the runtime system.
2240 Label done;
2241 if (var->AsSlot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
2242 Label slow;
2243 EmitLoadGlobalSlotCheckExtensions(var->AsSlot(),
2244 NOT_INSIDE_TYPEOF,
2245 &slow);
2246 // Push the function and resolve eval.
2247 __ push(v0);
2248 EmitResolvePossiblyDirectEval(SKIP_CONTEXT_LOOKUP, arg_count);
2249 __ jmp(&done);
2250 __ bind(&slow);
2251 }
2252
2253 // Push copy of the function (found below the arguments) and
2254 // resolve eval.
2255 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2256 __ push(a1);
2257 EmitResolvePossiblyDirectEval(PERFORM_CONTEXT_LOOKUP, arg_count);
2258 if (done.is_linked()) {
2259 __ bind(&done);
2260 }
2261
2262 // The runtime call returns a pair of values in v0 (function) and
2263 // v1 (receiver). Touch up the stack with the right values.
2264 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2265 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2266 }
2267 // Record source position for debugger.
2268 SetSourcePosition(expr->position());
2269 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002270 CallFunctionStub stub(arg_count, in_loop, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002271 __ CallStub(&stub);
2272 RecordJSReturnSite(expr);
2273 // Restore context register.
2274 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2275 context()->DropAndPlug(1, v0);
2276 } else if (var != NULL && !var->is_this() && var->is_global()) {
2277 // Push global object as receiver for the call IC.
2278 __ lw(a0, GlobalObjectOperand());
2279 __ push(a0);
2280 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
2281 } else if (var != NULL && var->AsSlot() != NULL &&
2282 var->AsSlot()->type() == Slot::LOOKUP) {
2283 // Call to a lookup slot (dynamically introduced variable).
2284 Label slow, done;
2285
2286 { PreservePositionScope scope(masm()->positions_recorder());
2287 // Generate code for loading from variables potentially shadowed
2288 // by eval-introduced variables.
2289 EmitDynamicLoadFromSlotFastCase(var->AsSlot(),
2290 NOT_INSIDE_TYPEOF,
2291 &slow,
2292 &done);
2293 }
2294
2295 __ bind(&slow);
2296 // Call the runtime to find the function to call (returned in v0)
2297 // and the object holding it (returned in v1).
2298 __ push(context_register());
2299 __ li(a2, Operand(var->name()));
2300 __ push(a2);
2301 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2302 __ Push(v0, v1); // Function, receiver.
2303
2304 // If fast case code has been generated, emit code to push the
2305 // function and receiver and have the slow path jump around this
2306 // code.
2307 if (done.is_linked()) {
2308 Label call;
2309 __ Branch(&call);
2310 __ bind(&done);
2311 // Push function.
2312 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002313 // The receiver is implicitly the global receiver. Indicate this
2314 // by passing the hole to the call function stub.
2315 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002316 __ push(a1);
2317 __ bind(&call);
2318 }
2319
danno@chromium.org40cb8782011-05-25 07:58:50 +00002320 // The receiver is either the global receiver or an object found
2321 // by LoadContextSlot. That object could be the hole if the
2322 // receiver is implicitly the global object.
2323 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002324 } else if (fun->AsProperty() != NULL) {
2325 // Call to an object property.
2326 Property* prop = fun->AsProperty();
2327 Literal* key = prop->key()->AsLiteral();
2328 if (key != NULL && key->handle()->IsSymbol()) {
2329 // Call to a named property, use call IC.
2330 { PreservePositionScope scope(masm()->positions_recorder());
2331 VisitForStackValue(prop->obj());
2332 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00002333 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002334 } else {
2335 // Call to a keyed property.
2336 // For a synthetic property use keyed load IC followed by function call,
2337 // for a regular property use keyed EmitCallIC.
2338 if (prop->is_synthetic()) {
2339 // Do not visit the object and key subexpressions (they are shared
2340 // by all occurrences of the same rewritten parameter).
2341 ASSERT(prop->obj()->AsVariableProxy() != NULL);
2342 ASSERT(prop->obj()->AsVariableProxy()->var()->AsSlot() != NULL);
2343 Slot* slot = prop->obj()->AsVariableProxy()->var()->AsSlot();
2344 MemOperand operand = EmitSlotSearch(slot, a1);
2345 __ lw(a1, operand);
2346
2347 ASSERT(prop->key()->AsLiteral() != NULL);
2348 ASSERT(prop->key()->AsLiteral()->handle()->IsSmi());
2349 __ li(a0, Operand(prop->key()->AsLiteral()->handle()));
2350
2351 // Record source code position for IC call.
2352 SetSourcePosition(prop->position());
2353
2354 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00002355 EmitCallIC(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002356 __ lw(a1, GlobalObjectOperand());
2357 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2358 __ Push(v0, a1); // Function, receiver.
2359 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2360 } else {
2361 { PreservePositionScope scope(masm()->positions_recorder());
2362 VisitForStackValue(prop->obj());
2363 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00002364 EmitKeyedCallWithIC(expr, prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002365 }
2366 }
2367 } else {
2368 { PreservePositionScope scope(masm()->positions_recorder());
2369 VisitForStackValue(fun);
2370 }
2371 // Load global receiver object.
2372 __ lw(a1, GlobalObjectOperand());
2373 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2374 __ push(a1);
2375 // Emit function call.
2376 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2377 }
2378
2379#ifdef DEBUG
2380 // RecordJSReturnSite should have been called.
2381 ASSERT(expr->return_is_recorded_);
2382#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002383}
2384
2385
2386void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002387 Comment cmnt(masm_, "[ CallNew");
2388 // According to ECMA-262, section 11.2.2, page 44, the function
2389 // expression in new calls must be evaluated before the
2390 // arguments.
2391
2392 // Push constructor on the stack. If it's not a function it's used as
2393 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2394 // ignored.
2395 VisitForStackValue(expr->expression());
2396
2397 // Push the arguments ("left-to-right") on the stack.
2398 ZoneList<Expression*>* args = expr->arguments();
2399 int arg_count = args->length();
2400 for (int i = 0; i < arg_count; i++) {
2401 VisitForStackValue(args->at(i));
2402 }
2403
2404 // Call the construct call builtin that handles allocation and
2405 // constructor invocation.
2406 SetSourcePosition(expr->position());
2407
2408 // Load function and argument count into a1 and a0.
2409 __ li(a0, Operand(arg_count));
2410 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2411
2412 Handle<Code> construct_builtin =
2413 isolate()->builtins()->JSConstructCall();
2414 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2415 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002416}
2417
2418
lrn@chromium.org7516f052011-03-30 08:52:27 +00002419void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002420 ASSERT(args->length() == 1);
2421
2422 VisitForAccumulatorValue(args->at(0));
2423
2424 Label materialize_true, materialize_false;
2425 Label* if_true = NULL;
2426 Label* if_false = NULL;
2427 Label* fall_through = NULL;
2428 context()->PrepareTest(&materialize_true, &materialize_false,
2429 &if_true, &if_false, &fall_through);
2430
2431 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2432 __ And(t0, v0, Operand(kSmiTagMask));
2433 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2434
2435 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002436}
2437
2438
2439void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002440 ASSERT(args->length() == 1);
2441
2442 VisitForAccumulatorValue(args->at(0));
2443
2444 Label materialize_true, materialize_false;
2445 Label* if_true = NULL;
2446 Label* if_false = NULL;
2447 Label* fall_through = NULL;
2448 context()->PrepareTest(&materialize_true, &materialize_false,
2449 &if_true, &if_false, &fall_through);
2450
2451 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2452 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2453 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2454
2455 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002456}
2457
2458
2459void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002460 ASSERT(args->length() == 1);
2461
2462 VisitForAccumulatorValue(args->at(0));
2463
2464 Label materialize_true, materialize_false;
2465 Label* if_true = NULL;
2466 Label* if_false = NULL;
2467 Label* fall_through = NULL;
2468 context()->PrepareTest(&materialize_true, &materialize_false,
2469 &if_true, &if_false, &fall_through);
2470
2471 __ JumpIfSmi(v0, if_false);
2472 __ LoadRoot(at, Heap::kNullValueRootIndex);
2473 __ Branch(if_true, eq, v0, Operand(at));
2474 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2475 // Undetectable objects behave like undefined when tested with typeof.
2476 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2477 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2478 __ Branch(if_false, ne, at, Operand(zero_reg));
2479 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002480 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002481 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002482 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2483 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002484
2485 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002486}
2487
2488
2489void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002490 ASSERT(args->length() == 1);
2491
2492 VisitForAccumulatorValue(args->at(0));
2493
2494 Label materialize_true, materialize_false;
2495 Label* if_true = NULL;
2496 Label* if_false = NULL;
2497 Label* fall_through = NULL;
2498 context()->PrepareTest(&materialize_true, &materialize_false,
2499 &if_true, &if_false, &fall_through);
2500
2501 __ JumpIfSmi(v0, if_false);
2502 __ GetObjectType(v0, a1, a1);
2503 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002504 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002505 if_true, if_false, fall_through);
2506
2507 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002508}
2509
2510
2511void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002512 ASSERT(args->length() == 1);
2513
2514 VisitForAccumulatorValue(args->at(0));
2515
2516 Label materialize_true, materialize_false;
2517 Label* if_true = NULL;
2518 Label* if_false = NULL;
2519 Label* fall_through = NULL;
2520 context()->PrepareTest(&materialize_true, &materialize_false,
2521 &if_true, &if_false, &fall_through);
2522
2523 __ JumpIfSmi(v0, if_false);
2524 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2525 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2526 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2527 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2528 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2529
2530 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002531}
2532
2533
2534void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2535 ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002536
2537 ASSERT(args->length() == 1);
2538
2539 VisitForAccumulatorValue(args->at(0));
2540
2541 Label materialize_true, materialize_false;
2542 Label* if_true = NULL;
2543 Label* if_false = NULL;
2544 Label* fall_through = NULL;
2545 context()->PrepareTest(&materialize_true, &materialize_false,
2546 &if_true, &if_false, &fall_through);
2547
2548 if (FLAG_debug_code) __ AbortIfSmi(v0);
2549
2550 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2551 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2552 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2553 __ Branch(if_true, ne, t0, Operand(zero_reg));
2554
2555 // Check for fast case object. Generate false result for slow case object.
2556 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2557 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2558 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2559 __ Branch(if_false, eq, a2, Operand(t0));
2560
2561 // Look for valueOf symbol in the descriptor array, and indicate false if
2562 // found. The type is not checked, so if it is a transition it is a false
2563 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002564 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002565 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2566 // t0: descriptor array
2567 // a3: length of descriptor array
2568 // Calculate the end of the descriptor array.
2569 STATIC_ASSERT(kSmiTag == 0);
2570 STATIC_ASSERT(kSmiTagSize == 1);
2571 STATIC_ASSERT(kPointerSize == 4);
2572 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2573 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2574 __ Addu(a2, a2, t1);
2575
2576 // Calculate location of the first key name.
2577 __ Addu(t0,
2578 t0,
2579 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2580 DescriptorArray::kFirstIndex * kPointerSize));
2581 // Loop through all the keys in the descriptor array. If one of these is the
2582 // symbol valueOf the result is false.
2583 Label entry, loop;
2584 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2585 // used in the loop below.
2586 __ li(t2, Operand(FACTORY->value_of_symbol()));
2587 __ jmp(&entry);
2588 __ bind(&loop);
2589 __ lw(a3, MemOperand(t0, 0));
2590 __ Branch(if_false, eq, a3, Operand(t2));
2591 __ Addu(t0, t0, Operand(kPointerSize));
2592 __ bind(&entry);
2593 __ Branch(&loop, ne, t0, Operand(a2));
2594
2595 // If a valueOf property is not found on the object check that it's
2596 // prototype is the un-modified String prototype. If not result is false.
2597 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2598 __ JumpIfSmi(a2, if_false);
2599 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2600 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2601 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2602 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2603 __ Branch(if_false, ne, a2, Operand(a3));
2604
2605 // Set the bit in the map to indicate that it has been checked safe for
2606 // default valueOf and set true result.
2607 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2608 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2609 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2610 __ jmp(if_true);
2611
2612 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2613 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002614}
2615
2616
2617void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002618 ASSERT(args->length() == 1);
2619
2620 VisitForAccumulatorValue(args->at(0));
2621
2622 Label materialize_true, materialize_false;
2623 Label* if_true = NULL;
2624 Label* if_false = NULL;
2625 Label* fall_through = NULL;
2626 context()->PrepareTest(&materialize_true, &materialize_false,
2627 &if_true, &if_false, &fall_through);
2628
2629 __ JumpIfSmi(v0, if_false);
2630 __ GetObjectType(v0, a1, a2);
2631 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2632 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2633 __ Branch(if_false);
2634
2635 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002636}
2637
2638
2639void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002640 ASSERT(args->length() == 1);
2641
2642 VisitForAccumulatorValue(args->at(0));
2643
2644 Label materialize_true, materialize_false;
2645 Label* if_true = NULL;
2646 Label* if_false = NULL;
2647 Label* fall_through = NULL;
2648 context()->PrepareTest(&materialize_true, &materialize_false,
2649 &if_true, &if_false, &fall_through);
2650
2651 __ JumpIfSmi(v0, if_false);
2652 __ GetObjectType(v0, a1, a1);
2653 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2654 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2655 if_true, if_false, fall_through);
2656
2657 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002658}
2659
2660
2661void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002662 ASSERT(args->length() == 1);
2663
2664 VisitForAccumulatorValue(args->at(0));
2665
2666 Label materialize_true, materialize_false;
2667 Label* if_true = NULL;
2668 Label* if_false = NULL;
2669 Label* fall_through = NULL;
2670 context()->PrepareTest(&materialize_true, &materialize_false,
2671 &if_true, &if_false, &fall_through);
2672
2673 __ JumpIfSmi(v0, if_false);
2674 __ GetObjectType(v0, a1, a1);
2675 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2676 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2677
2678 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002679}
2680
2681
2682void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002683 ASSERT(args->length() == 0);
2684
2685 Label materialize_true, materialize_false;
2686 Label* if_true = NULL;
2687 Label* if_false = NULL;
2688 Label* fall_through = NULL;
2689 context()->PrepareTest(&materialize_true, &materialize_false,
2690 &if_true, &if_false, &fall_through);
2691
2692 // Get the frame pointer for the calling frame.
2693 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2694
2695 // Skip the arguments adaptor frame if it exists.
2696 Label check_frame_marker;
2697 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2698 __ Branch(&check_frame_marker, ne,
2699 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2700 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2701
2702 // Check the marker in the calling frame.
2703 __ bind(&check_frame_marker);
2704 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
2705 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2706 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2707 if_true, if_false, fall_through);
2708
2709 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002710}
2711
2712
2713void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002714 ASSERT(args->length() == 2);
2715
2716 // Load the two objects into registers and perform the comparison.
2717 VisitForStackValue(args->at(0));
2718 VisitForAccumulatorValue(args->at(1));
2719
2720 Label materialize_true, materialize_false;
2721 Label* if_true = NULL;
2722 Label* if_false = NULL;
2723 Label* fall_through = NULL;
2724 context()->PrepareTest(&materialize_true, &materialize_false,
2725 &if_true, &if_false, &fall_through);
2726
2727 __ pop(a1);
2728 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2729 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2730
2731 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002732}
2733
2734
2735void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002736 ASSERT(args->length() == 1);
2737
2738 // ArgumentsAccessStub expects the key in a1 and the formal
2739 // parameter count in a0.
2740 VisitForAccumulatorValue(args->at(0));
2741 __ mov(a1, v0);
2742 __ li(a0, Operand(Smi::FromInt(scope()->num_parameters())));
2743 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2744 __ CallStub(&stub);
2745 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002746}
2747
2748
2749void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002750 ASSERT(args->length() == 0);
2751
2752 Label exit;
2753 // Get the number of formal parameters.
2754 __ li(v0, Operand(Smi::FromInt(scope()->num_parameters())));
2755
2756 // Check if the calling frame is an arguments adaptor frame.
2757 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2758 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2759 __ Branch(&exit, ne, a3,
2760 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2761
2762 // Arguments adaptor case: Read the arguments length from the
2763 // adaptor frame.
2764 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2765
2766 __ bind(&exit);
2767 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002768}
2769
2770
2771void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002772 ASSERT(args->length() == 1);
2773 Label done, null, function, non_function_constructor;
2774
2775 VisitForAccumulatorValue(args->at(0));
2776
2777 // If the object is a smi, we return null.
2778 __ JumpIfSmi(v0, &null);
2779
2780 // Check that the object is a JS object but take special care of JS
2781 // functions to make sure they have 'Function' as their class.
2782 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002783 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002784
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002785 // As long as LAST_CALLABLE_SPEC_OBJECT_TYPE is the last instance type, and
2786 // FIRST_CALLABLE_SPEC_OBJECT_TYPE comes right after
2787 // LAST_NONCALLABLE_SPEC_OBJECT_TYPE, we can avoid checking for the latter.
2788 STATIC_ASSERT(LAST_TYPE == LAST_CALLABLE_SPEC_OBJECT_TYPE);
2789 STATIC_ASSERT(FIRST_CALLABLE_SPEC_OBJECT_TYPE ==
2790 LAST_NONCALLABLE_SPEC_OBJECT_TYPE + 1);
2791 __ Branch(&function, ge, a1, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002792
2793 // Check if the constructor in the map is a function.
2794 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2795 __ GetObjectType(v0, a1, a1);
2796 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2797
2798 // v0 now contains the constructor function. Grab the
2799 // instance class name from there.
2800 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2801 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2802 __ Branch(&done);
2803
2804 // Functions have class 'Function'.
2805 __ bind(&function);
2806 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2807 __ jmp(&done);
2808
2809 // Objects with a non-function constructor have class 'Object'.
2810 __ bind(&non_function_constructor);
2811 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2812 __ jmp(&done);
2813
2814 // Non-JS objects have class null.
2815 __ bind(&null);
2816 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2817
2818 // All done.
2819 __ bind(&done);
2820
2821 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002822}
2823
2824
2825void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002826 // Conditionally generate a log call.
2827 // Args:
2828 // 0 (literal string): The type of logging (corresponds to the flags).
2829 // This is used to determine whether or not to generate the log call.
2830 // 1 (string): Format string. Access the string at argument index 2
2831 // with '%2s' (see Logger::LogRuntime for all the formats).
2832 // 2 (array): Arguments to the format string.
2833 ASSERT_EQ(args->length(), 3);
2834#ifdef ENABLE_LOGGING_AND_PROFILING
2835 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2836 VisitForStackValue(args->at(1));
2837 VisitForStackValue(args->at(2));
2838 __ CallRuntime(Runtime::kLog, 2);
2839 }
2840#endif
2841 // Finally, we're expected to leave a value on the top of the stack.
2842 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2843 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002844}
2845
2846
2847void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002848 ASSERT(args->length() == 0);
2849
2850 Label slow_allocate_heapnumber;
2851 Label heapnumber_allocated;
2852
2853 // Save the new heap number in callee-saved register s0, since
2854 // we call out to external C code below.
2855 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2856 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2857 __ jmp(&heapnumber_allocated);
2858
2859 __ bind(&slow_allocate_heapnumber);
2860
2861 // Allocate a heap number.
2862 __ CallRuntime(Runtime::kNumberAlloc, 0);
2863 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2864
2865 __ bind(&heapnumber_allocated);
2866
2867 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2868 // by computing:
2869 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2870 if (CpuFeatures::IsSupported(FPU)) {
2871 __ PrepareCallCFunction(1, a0);
2872 __ li(a0, Operand(ExternalReference::isolate_address()));
2873 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2874
2875
2876 CpuFeatures::Scope scope(FPU);
2877 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2878 __ li(a1, Operand(0x41300000));
2879 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002880 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002881 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002882 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002883 // Subtract and store the result in the heap number.
2884 __ sub_d(f0, f12, f14);
2885 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2886 __ mov(v0, s0);
2887 } else {
2888 __ PrepareCallCFunction(2, a0);
2889 __ mov(a0, s0);
2890 __ li(a1, Operand(ExternalReference::isolate_address()));
2891 __ CallCFunction(
2892 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2893 }
2894
2895 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002896}
2897
2898
2899void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002900 // Load the arguments on the stack and call the stub.
2901 SubStringStub stub;
2902 ASSERT(args->length() == 3);
2903 VisitForStackValue(args->at(0));
2904 VisitForStackValue(args->at(1));
2905 VisitForStackValue(args->at(2));
2906 __ CallStub(&stub);
2907 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002908}
2909
2910
2911void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002912 // Load the arguments on the stack and call the stub.
2913 RegExpExecStub stub;
2914 ASSERT(args->length() == 4);
2915 VisitForStackValue(args->at(0));
2916 VisitForStackValue(args->at(1));
2917 VisitForStackValue(args->at(2));
2918 VisitForStackValue(args->at(3));
2919 __ CallStub(&stub);
2920 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002921}
2922
2923
2924void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002925 ASSERT(args->length() == 1);
2926
2927 VisitForAccumulatorValue(args->at(0)); // Load the object.
2928
2929 Label done;
2930 // If the object is a smi return the object.
2931 __ JumpIfSmi(v0, &done);
2932 // If the object is not a value type, return the object.
2933 __ GetObjectType(v0, a1, a1);
2934 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2935
2936 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2937
2938 __ bind(&done);
2939 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002940}
2941
2942
2943void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002944 // Load the arguments on the stack and call the runtime function.
2945 ASSERT(args->length() == 2);
2946 VisitForStackValue(args->at(0));
2947 VisitForStackValue(args->at(1));
2948 MathPowStub stub;
2949 __ CallStub(&stub);
2950 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002951}
2952
2953
2954void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002955 ASSERT(args->length() == 2);
2956
2957 VisitForStackValue(args->at(0)); // Load the object.
2958 VisitForAccumulatorValue(args->at(1)); // Load the value.
2959 __ pop(a1); // v0 = value. a1 = object.
2960
2961 Label done;
2962 // If the object is a smi, return the value.
2963 __ JumpIfSmi(a1, &done);
2964
2965 // If the object is not a value type, return the value.
2966 __ GetObjectType(a1, a2, a2);
2967 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2968
2969 // Store the value.
2970 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2971 // Update the write barrier. Save the value as it will be
2972 // overwritten by the write barrier code and is needed afterward.
2973 __ RecordWrite(a1, Operand(JSValue::kValueOffset - kHeapObjectTag), a2, a3);
2974
2975 __ bind(&done);
2976 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002977}
2978
2979
2980void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002981 ASSERT_EQ(args->length(), 1);
2982
2983 // Load the argument on the stack and call the stub.
2984 VisitForStackValue(args->at(0));
2985
2986 NumberToStringStub stub;
2987 __ CallStub(&stub);
2988 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002989}
2990
2991
2992void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002993 ASSERT(args->length() == 1);
2994
2995 VisitForAccumulatorValue(args->at(0));
2996
2997 Label done;
2998 StringCharFromCodeGenerator generator(v0, a1);
2999 generator.GenerateFast(masm_);
3000 __ jmp(&done);
3001
3002 NopRuntimeCallHelper call_helper;
3003 generator.GenerateSlow(masm_, call_helper);
3004
3005 __ bind(&done);
3006 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003007}
3008
3009
3010void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003011 ASSERT(args->length() == 2);
3012
3013 VisitForStackValue(args->at(0));
3014 VisitForAccumulatorValue(args->at(1));
3015 __ mov(a0, result_register());
3016
3017 Register object = a1;
3018 Register index = a0;
3019 Register scratch = a2;
3020 Register result = v0;
3021
3022 __ pop(object);
3023
3024 Label need_conversion;
3025 Label index_out_of_range;
3026 Label done;
3027 StringCharCodeAtGenerator generator(object,
3028 index,
3029 scratch,
3030 result,
3031 &need_conversion,
3032 &need_conversion,
3033 &index_out_of_range,
3034 STRING_INDEX_IS_NUMBER);
3035 generator.GenerateFast(masm_);
3036 __ jmp(&done);
3037
3038 __ bind(&index_out_of_range);
3039 // When the index is out of range, the spec requires us to return
3040 // NaN.
3041 __ LoadRoot(result, Heap::kNanValueRootIndex);
3042 __ jmp(&done);
3043
3044 __ bind(&need_conversion);
3045 // Load the undefined value into the result register, which will
3046 // trigger conversion.
3047 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3048 __ jmp(&done);
3049
3050 NopRuntimeCallHelper call_helper;
3051 generator.GenerateSlow(masm_, call_helper);
3052
3053 __ bind(&done);
3054 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003055}
3056
3057
3058void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003059 ASSERT(args->length() == 2);
3060
3061 VisitForStackValue(args->at(0));
3062 VisitForAccumulatorValue(args->at(1));
3063 __ mov(a0, result_register());
3064
3065 Register object = a1;
3066 Register index = a0;
3067 Register scratch1 = a2;
3068 Register scratch2 = a3;
3069 Register result = v0;
3070
3071 __ pop(object);
3072
3073 Label need_conversion;
3074 Label index_out_of_range;
3075 Label done;
3076 StringCharAtGenerator generator(object,
3077 index,
3078 scratch1,
3079 scratch2,
3080 result,
3081 &need_conversion,
3082 &need_conversion,
3083 &index_out_of_range,
3084 STRING_INDEX_IS_NUMBER);
3085 generator.GenerateFast(masm_);
3086 __ jmp(&done);
3087
3088 __ bind(&index_out_of_range);
3089 // When the index is out of range, the spec requires us to return
3090 // the empty string.
3091 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3092 __ jmp(&done);
3093
3094 __ bind(&need_conversion);
3095 // Move smi zero into the result register, which will trigger
3096 // conversion.
3097 __ li(result, Operand(Smi::FromInt(0)));
3098 __ jmp(&done);
3099
3100 NopRuntimeCallHelper call_helper;
3101 generator.GenerateSlow(masm_, call_helper);
3102
3103 __ bind(&done);
3104 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003105}
3106
3107
3108void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003109 ASSERT_EQ(2, args->length());
3110
3111 VisitForStackValue(args->at(0));
3112 VisitForStackValue(args->at(1));
3113
3114 StringAddStub stub(NO_STRING_ADD_FLAGS);
3115 __ CallStub(&stub);
3116 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003117}
3118
3119
3120void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003121 ASSERT_EQ(2, args->length());
3122
3123 VisitForStackValue(args->at(0));
3124 VisitForStackValue(args->at(1));
3125
3126 StringCompareStub stub;
3127 __ CallStub(&stub);
3128 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003129}
3130
3131
3132void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003133 // Load the argument on the stack and call the stub.
3134 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3135 TranscendentalCacheStub::TAGGED);
3136 ASSERT(args->length() == 1);
3137 VisitForStackValue(args->at(0));
3138 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3139 __ CallStub(&stub);
3140 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003141}
3142
3143
3144void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003145 // Load the argument on the stack and call the stub.
3146 TranscendentalCacheStub stub(TranscendentalCache::COS,
3147 TranscendentalCacheStub::TAGGED);
3148 ASSERT(args->length() == 1);
3149 VisitForStackValue(args->at(0));
3150 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3151 __ CallStub(&stub);
3152 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003153}
3154
3155
3156void FullCodeGenerator::EmitMathLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003157 // Load the argument on the stack and call the stub.
3158 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3159 TranscendentalCacheStub::TAGGED);
3160 ASSERT(args->length() == 1);
3161 VisitForStackValue(args->at(0));
3162 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3163 __ CallStub(&stub);
3164 context()->Plug(v0);
3165}
3166
3167
3168void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
3169 // Load the argument on the stack and call the runtime function.
3170 ASSERT(args->length() == 1);
3171 VisitForStackValue(args->at(0));
3172 __ CallRuntime(Runtime::kMath_sqrt, 1);
3173 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003174}
3175
3176
3177void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003178 ASSERT(args->length() >= 2);
3179
3180 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3181 for (int i = 0; i < arg_count + 1; i++) {
3182 VisitForStackValue(args->at(i));
3183 }
3184 VisitForAccumulatorValue(args->last()); // Function.
3185
3186 // InvokeFunction requires the function in a1. Move it in there.
3187 __ mov(a1, result_register());
3188 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003189 __ InvokeFunction(a1, count, CALL_FUNCTION,
3190 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003191 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3192 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003193}
3194
3195
3196void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003197 RegExpConstructResultStub stub;
3198 ASSERT(args->length() == 3);
3199 VisitForStackValue(args->at(0));
3200 VisitForStackValue(args->at(1));
3201 VisitForStackValue(args->at(2));
3202 __ CallStub(&stub);
3203 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003204}
3205
3206
3207void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003208 ASSERT(args->length() == 3);
3209 VisitForStackValue(args->at(0));
3210 VisitForStackValue(args->at(1));
3211 VisitForStackValue(args->at(2));
3212 Label done;
3213 Label slow_case;
3214 Register object = a0;
3215 Register index1 = a1;
3216 Register index2 = a2;
3217 Register elements = a3;
3218 Register scratch1 = t0;
3219 Register scratch2 = t1;
3220
3221 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3222 // Fetch the map and check if array is in fast case.
3223 // Check that object doesn't require security checks and
3224 // has no indexed interceptor.
3225 __ GetObjectType(object, scratch1, scratch2);
3226 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3227 // Map is now in scratch1.
3228
3229 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3230 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3231 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3232
3233 // Check the object's elements are in fast case and writable.
3234 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3235 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3236 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3237 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3238
3239 // Check that both indices are smis.
3240 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3241 __ lw(index2, MemOperand(sp, 0));
3242 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3243
3244 // Check that both indices are valid.
3245 Label not_hi;
3246 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3247 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3248 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3249 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3250 __ bind(&not_hi);
3251
3252 // Bring the address of the elements into index1 and index2.
3253 __ Addu(scratch1, elements,
3254 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3255 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3256 __ Addu(index1, scratch1, index1);
3257 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3258 __ Addu(index2, scratch1, index2);
3259
3260 // Swap elements.
3261 __ lw(scratch1, MemOperand(index1, 0));
3262 __ lw(scratch2, MemOperand(index2, 0));
3263 __ sw(scratch1, MemOperand(index2, 0));
3264 __ sw(scratch2, MemOperand(index1, 0));
3265
3266 Label new_space;
3267 __ InNewSpace(elements, scratch1, eq, &new_space);
3268 // Possible optimization: do a check that both values are Smis
3269 // (or them and test against Smi mask).
3270
3271 __ mov(scratch1, elements);
3272 __ RecordWriteHelper(elements, index1, scratch2);
3273 __ RecordWriteHelper(scratch1, index2, scratch2); // scratch1 holds elements.
3274
3275 __ bind(&new_space);
3276 // We are done. Drop elements from the stack, and return undefined.
3277 __ Drop(3);
3278 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3279 __ jmp(&done);
3280
3281 __ bind(&slow_case);
3282 __ CallRuntime(Runtime::kSwapElements, 3);
3283
3284 __ bind(&done);
3285 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003286}
3287
3288
3289void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003290 ASSERT_EQ(2, args->length());
3291
3292 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3293 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3294
3295 Handle<FixedArray> jsfunction_result_caches(
3296 isolate()->global_context()->jsfunction_result_caches());
3297 if (jsfunction_result_caches->length() <= cache_id) {
3298 __ Abort("Attempt to use undefined cache.");
3299 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3300 context()->Plug(v0);
3301 return;
3302 }
3303
3304 VisitForAccumulatorValue(args->at(1));
3305
3306 Register key = v0;
3307 Register cache = a1;
3308 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3309 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3310 __ lw(cache,
3311 ContextOperand(
3312 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3313 __ lw(cache,
3314 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3315
3316
3317 Label done, not_found;
3318 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
3319 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3320 // a2 now holds finger offset as a smi.
3321 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3322 // a3 now points to the start of fixed array elements.
3323 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3324 __ addu(a3, a3, at);
3325 // a3 now points to key of indexed element of cache.
3326 __ lw(a2, MemOperand(a3));
3327 __ Branch(&not_found, ne, key, Operand(a2));
3328
3329 __ lw(v0, MemOperand(a3, kPointerSize));
3330 __ Branch(&done);
3331
3332 __ bind(&not_found);
3333 // Call runtime to perform the lookup.
3334 __ Push(cache, key);
3335 __ CallRuntime(Runtime::kGetFromCache, 2);
3336
3337 __ bind(&done);
3338 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003339}
3340
3341
3342void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003343 ASSERT_EQ(2, args->length());
3344
3345 Register right = v0;
3346 Register left = a1;
3347 Register tmp = a2;
3348 Register tmp2 = a3;
3349
3350 VisitForStackValue(args->at(0));
3351 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3352 __ pop(left);
3353
3354 Label done, fail, ok;
3355 __ Branch(&ok, eq, left, Operand(right));
3356 // Fail if either is a non-HeapObject.
3357 __ And(tmp, left, Operand(right));
3358 __ And(at, tmp, Operand(kSmiTagMask));
3359 __ Branch(&fail, eq, at, Operand(zero_reg));
3360 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3361 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3362 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3363 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3364 __ Branch(&fail, ne, tmp, Operand(tmp2));
3365 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3366 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3367 __ Branch(&ok, eq, tmp, Operand(tmp2));
3368 __ bind(&fail);
3369 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3370 __ jmp(&done);
3371 __ bind(&ok);
3372 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3373 __ bind(&done);
3374
3375 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003376}
3377
3378
3379void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003380 VisitForAccumulatorValue(args->at(0));
3381
3382 Label materialize_true, materialize_false;
3383 Label* if_true = NULL;
3384 Label* if_false = NULL;
3385 Label* fall_through = NULL;
3386 context()->PrepareTest(&materialize_true, &materialize_false,
3387 &if_true, &if_false, &fall_through);
3388
3389 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3390 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3391
3392 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3393 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3394
3395 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003396}
3397
3398
3399void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003400 ASSERT(args->length() == 1);
3401 VisitForAccumulatorValue(args->at(0));
3402
3403 if (FLAG_debug_code) {
3404 __ AbortIfNotString(v0);
3405 }
3406
3407 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3408 __ IndexFromHash(v0, v0);
3409
3410 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003411}
3412
3413
3414void FullCodeGenerator::EmitFastAsciiArrayJoin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003415 Label bailout, done, one_char_separator, long_separator,
3416 non_trivial_array, not_size_one_array, loop,
3417 empty_separator_loop, one_char_separator_loop,
3418 one_char_separator_loop_entry, long_separator_loop;
3419
3420 ASSERT(args->length() == 2);
3421 VisitForStackValue(args->at(1));
3422 VisitForAccumulatorValue(args->at(0));
3423
3424 // All aliases of the same register have disjoint lifetimes.
3425 Register array = v0;
3426 Register elements = no_reg; // Will be v0.
3427 Register result = no_reg; // Will be v0.
3428 Register separator = a1;
3429 Register array_length = a2;
3430 Register result_pos = no_reg; // Will be a2.
3431 Register string_length = a3;
3432 Register string = t0;
3433 Register element = t1;
3434 Register elements_end = t2;
3435 Register scratch1 = t3;
3436 Register scratch2 = t5;
3437 Register scratch3 = t4;
3438 Register scratch4 = v1;
3439
3440 // Separator operand is on the stack.
3441 __ pop(separator);
3442
3443 // Check that the array is a JSArray.
3444 __ JumpIfSmi(array, &bailout);
3445 __ GetObjectType(array, scratch1, scratch2);
3446 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3447
3448 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003449 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003450
3451 // If the array has length zero, return the empty string.
3452 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3453 __ SmiUntag(array_length);
3454 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3455 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3456 __ Branch(&done);
3457
3458 __ bind(&non_trivial_array);
3459
3460 // Get the FixedArray containing array's elements.
3461 elements = array;
3462 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3463 array = no_reg; // End of array's live range.
3464
3465 // Check that all array elements are sequential ASCII strings, and
3466 // accumulate the sum of their lengths, as a smi-encoded value.
3467 __ mov(string_length, zero_reg);
3468 __ Addu(element,
3469 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3470 __ sll(elements_end, array_length, kPointerSizeLog2);
3471 __ Addu(elements_end, element, elements_end);
3472 // Loop condition: while (element < elements_end).
3473 // Live values in registers:
3474 // elements: Fixed array of strings.
3475 // array_length: Length of the fixed array of strings (not smi)
3476 // separator: Separator string
3477 // string_length: Accumulated sum of string lengths (smi).
3478 // element: Current array element.
3479 // elements_end: Array end.
3480 if (FLAG_debug_code) {
3481 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3482 array_length, Operand(zero_reg));
3483 }
3484 __ bind(&loop);
3485 __ lw(string, MemOperand(element));
3486 __ Addu(element, element, kPointerSize);
3487 __ JumpIfSmi(string, &bailout);
3488 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3489 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3490 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3491 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3492 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3493 __ BranchOnOverflow(&bailout, scratch3);
3494 __ Branch(&loop, lt, element, Operand(elements_end));
3495
3496 // If array_length is 1, return elements[0], a string.
3497 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3498 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3499 __ Branch(&done);
3500
3501 __ bind(&not_size_one_array);
3502
3503 // Live values in registers:
3504 // separator: Separator string
3505 // array_length: Length of the array.
3506 // string_length: Sum of string lengths (smi).
3507 // elements: FixedArray of strings.
3508
3509 // Check that the separator is a flat ASCII string.
3510 __ JumpIfSmi(separator, &bailout);
3511 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3512 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3513 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3514
3515 // Add (separator length times array_length) - separator length to the
3516 // string_length to get the length of the result string. array_length is not
3517 // smi but the other values are, so the result is a smi.
3518 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3519 __ Subu(string_length, string_length, Operand(scratch1));
3520 __ Mult(array_length, scratch1);
3521 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3522 // zero.
3523 __ mfhi(scratch2);
3524 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3525 __ mflo(scratch2);
3526 __ And(scratch3, scratch2, Operand(0x80000000));
3527 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3528 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3529 __ BranchOnOverflow(&bailout, scratch3);
3530 __ SmiUntag(string_length);
3531
3532 // Get first element in the array to free up the elements register to be used
3533 // for the result.
3534 __ Addu(element,
3535 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3536 result = elements; // End of live range for elements.
3537 elements = no_reg;
3538 // Live values in registers:
3539 // element: First array element
3540 // separator: Separator string
3541 // string_length: Length of result string (not smi)
3542 // array_length: Length of the array.
3543 __ AllocateAsciiString(result,
3544 string_length,
3545 scratch1,
3546 scratch2,
3547 elements_end,
3548 &bailout);
3549 // Prepare for looping. Set up elements_end to end of the array. Set
3550 // result_pos to the position of the result where to write the first
3551 // character.
3552 __ sll(elements_end, array_length, kPointerSizeLog2);
3553 __ Addu(elements_end, element, elements_end);
3554 result_pos = array_length; // End of live range for array_length.
3555 array_length = no_reg;
3556 __ Addu(result_pos,
3557 result,
3558 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3559
3560 // Check the length of the separator.
3561 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3562 __ li(at, Operand(Smi::FromInt(1)));
3563 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3564 __ Branch(&long_separator, gt, scratch1, Operand(at));
3565
3566 // Empty separator case.
3567 __ bind(&empty_separator_loop);
3568 // Live values in registers:
3569 // result_pos: the position to which we are currently copying characters.
3570 // element: Current array element.
3571 // elements_end: Array end.
3572
3573 // Copy next array element to the result.
3574 __ lw(string, MemOperand(element));
3575 __ Addu(element, element, kPointerSize);
3576 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3577 __ SmiUntag(string_length);
3578 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3579 __ CopyBytes(string, result_pos, string_length, scratch1);
3580 // End while (element < elements_end).
3581 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3582 ASSERT(result.is(v0));
3583 __ Branch(&done);
3584
3585 // One-character separator case.
3586 __ bind(&one_char_separator);
3587 // Replace separator with its ascii character value.
3588 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3589 // Jump into the loop after the code that copies the separator, so the first
3590 // element is not preceded by a separator.
3591 __ jmp(&one_char_separator_loop_entry);
3592
3593 __ bind(&one_char_separator_loop);
3594 // Live values in registers:
3595 // result_pos: the position to which we are currently copying characters.
3596 // element: Current array element.
3597 // elements_end: Array end.
3598 // separator: Single separator ascii char (in lower byte).
3599
3600 // Copy the separator character to the result.
3601 __ sb(separator, MemOperand(result_pos));
3602 __ Addu(result_pos, result_pos, 1);
3603
3604 // Copy next array element to the result.
3605 __ bind(&one_char_separator_loop_entry);
3606 __ lw(string, MemOperand(element));
3607 __ Addu(element, element, kPointerSize);
3608 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3609 __ SmiUntag(string_length);
3610 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3611 __ CopyBytes(string, result_pos, string_length, scratch1);
3612 // End while (element < elements_end).
3613 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3614 ASSERT(result.is(v0));
3615 __ Branch(&done);
3616
3617 // Long separator case (separator is more than one character). Entry is at the
3618 // label long_separator below.
3619 __ bind(&long_separator_loop);
3620 // Live values in registers:
3621 // result_pos: the position to which we are currently copying characters.
3622 // element: Current array element.
3623 // elements_end: Array end.
3624 // separator: Separator string.
3625
3626 // Copy the separator to the result.
3627 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3628 __ SmiUntag(string_length);
3629 __ Addu(string,
3630 separator,
3631 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3632 __ CopyBytes(string, result_pos, string_length, scratch1);
3633
3634 __ bind(&long_separator);
3635 __ lw(string, MemOperand(element));
3636 __ Addu(element, element, kPointerSize);
3637 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3638 __ SmiUntag(string_length);
3639 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3640 __ CopyBytes(string, result_pos, string_length, scratch1);
3641 // End while (element < elements_end).
3642 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3643 ASSERT(result.is(v0));
3644 __ Branch(&done);
3645
3646 __ bind(&bailout);
3647 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3648 __ bind(&done);
3649 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003650}
3651
3652
ager@chromium.org5c838252010-02-19 08:53:10 +00003653void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003654 Handle<String> name = expr->name();
3655 if (name->length() > 0 && name->Get(0) == '_') {
3656 Comment cmnt(masm_, "[ InlineRuntimeCall");
3657 EmitInlineRuntimeCall(expr);
3658 return;
3659 }
3660
3661 Comment cmnt(masm_, "[ CallRuntime");
3662 ZoneList<Expression*>* args = expr->arguments();
3663
3664 if (expr->is_jsruntime()) {
3665 // Prepare for calling JS runtime function.
3666 __ lw(a0, GlobalObjectOperand());
3667 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3668 __ push(a0);
3669 }
3670
3671 // Push the arguments ("left-to-right").
3672 int arg_count = args->length();
3673 for (int i = 0; i < arg_count; i++) {
3674 VisitForStackValue(args->at(i));
3675 }
3676
3677 if (expr->is_jsruntime()) {
3678 // Call the JS runtime function.
3679 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003680 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003681 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00003682 isolate()->stub_cache()->ComputeCallInitialize(arg_count,
3683 NOT_IN_LOOP,
3684 mode);
3685 EmitCallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003686 // Restore context register.
3687 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3688 } else {
3689 // Call the C runtime function.
3690 __ CallRuntime(expr->function(), arg_count);
3691 }
3692 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003693}
3694
3695
3696void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003697 switch (expr->op()) {
3698 case Token::DELETE: {
3699 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
3700 Property* prop = expr->expression()->AsProperty();
3701 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
3702
3703 if (prop != NULL) {
3704 if (prop->is_synthetic()) {
3705 // Result of deleting parameters is false, even when they rewrite
3706 // to accesses on the arguments object.
3707 context()->Plug(false);
3708 } else {
3709 VisitForStackValue(prop->obj());
3710 VisitForStackValue(prop->key());
3711 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3712 __ push(a1);
3713 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3714 context()->Plug(v0);
3715 }
3716 } else if (var != NULL) {
3717 // Delete of an unqualified identifier is disallowed in strict mode
3718 // but "delete this" is.
3719 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
3720 if (var->is_global()) {
3721 __ lw(a2, GlobalObjectOperand());
3722 __ li(a1, Operand(var->name()));
3723 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3724 __ Push(a2, a1, a0);
3725 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3726 context()->Plug(v0);
3727 } else if (var->AsSlot() != NULL &&
3728 var->AsSlot()->type() != Slot::LOOKUP) {
3729 // Result of deleting non-global, non-dynamic variables is false.
3730 // The subexpression does not have side effects.
3731 context()->Plug(false);
3732 } else {
3733 // Non-global variable. Call the runtime to try to delete from the
3734 // context where the variable was introduced.
3735 __ push(context_register());
3736 __ li(a2, Operand(var->name()));
3737 __ push(a2);
3738 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3739 context()->Plug(v0);
3740 }
3741 } else {
3742 // Result of deleting non-property, non-variable reference is true.
3743 // The subexpression may have side effects.
3744 VisitForEffect(expr->expression());
3745 context()->Plug(true);
3746 }
3747 break;
3748 }
3749
3750 case Token::VOID: {
3751 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3752 VisitForEffect(expr->expression());
3753 context()->Plug(Heap::kUndefinedValueRootIndex);
3754 break;
3755 }
3756
3757 case Token::NOT: {
3758 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3759 if (context()->IsEffect()) {
3760 // Unary NOT has no side effects so it's only necessary to visit the
3761 // subexpression. Match the optimizing compiler by not branching.
3762 VisitForEffect(expr->expression());
3763 } else {
3764 Label materialize_true, materialize_false;
3765 Label* if_true = NULL;
3766 Label* if_false = NULL;
3767 Label* fall_through = NULL;
3768
3769 // Notice that the labels are swapped.
3770 context()->PrepareTest(&materialize_true, &materialize_false,
3771 &if_false, &if_true, &fall_through);
3772 if (context()->IsTest()) ForwardBailoutToChild(expr);
3773 VisitForControl(expr->expression(), if_true, if_false, fall_through);
3774 context()->Plug(if_false, if_true); // Labels swapped.
3775 }
3776 break;
3777 }
3778
3779 case Token::TYPEOF: {
3780 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3781 { StackValueContext context(this);
3782 VisitForTypeofValue(expr->expression());
3783 }
3784 __ CallRuntime(Runtime::kTypeof, 1);
3785 context()->Plug(v0);
3786 break;
3787 }
3788
3789 case Token::ADD: {
3790 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3791 VisitForAccumulatorValue(expr->expression());
3792 Label no_conversion;
3793 __ JumpIfSmi(result_register(), &no_conversion);
3794 __ mov(a0, result_register());
3795 ToNumberStub convert_stub;
3796 __ CallStub(&convert_stub);
3797 __ bind(&no_conversion);
3798 context()->Plug(result_register());
3799 break;
3800 }
3801
3802 case Token::SUB:
3803 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3804 break;
3805
3806 case Token::BIT_NOT:
3807 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3808 break;
3809
3810 default:
3811 UNREACHABLE();
3812 }
3813}
3814
3815
3816void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3817 const char* comment) {
3818 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3819 Comment cmt(masm_, comment);
3820 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3821 UnaryOverwriteMode overwrite =
3822 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003823 UnaryOpStub stub(expr->op(), overwrite);
3824 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003825 VisitForAccumulatorValue(expr->expression());
3826 SetSourcePosition(expr->position());
3827 __ mov(a0, result_register());
3828 EmitCallIC(stub.GetCode(), NULL, expr->id());
3829 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003830}
3831
3832
3833void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003834 Comment cmnt(masm_, "[ CountOperation");
3835 SetSourcePosition(expr->position());
3836
3837 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3838 // as the left-hand side.
3839 if (!expr->expression()->IsValidLeftHandSide()) {
3840 VisitForEffect(expr->expression());
3841 return;
3842 }
3843
3844 // Expression can only be a property, a global or a (parameter or local)
3845 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
3846 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3847 LhsKind assign_type = VARIABLE;
3848 Property* prop = expr->expression()->AsProperty();
3849 // In case of a property we use the uninitialized expression context
3850 // of the key to detect a named property.
3851 if (prop != NULL) {
3852 assign_type =
3853 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3854 }
3855
3856 // Evaluate expression and get value.
3857 if (assign_type == VARIABLE) {
3858 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3859 AccumulatorValueContext context(this);
3860 EmitVariableLoad(expr->expression()->AsVariableProxy()->var());
3861 } else {
3862 // Reserve space for result of postfix operation.
3863 if (expr->is_postfix() && !context()->IsEffect()) {
3864 __ li(at, Operand(Smi::FromInt(0)));
3865 __ push(at);
3866 }
3867 if (assign_type == NAMED_PROPERTY) {
3868 // Put the object both on the stack and in the accumulator.
3869 VisitForAccumulatorValue(prop->obj());
3870 __ push(v0);
3871 EmitNamedPropertyLoad(prop);
3872 } else {
3873 if (prop->is_arguments_access()) {
3874 VariableProxy* obj_proxy = prop->obj()->AsVariableProxy();
3875 __ lw(v0, EmitSlotSearch(obj_proxy->var()->AsSlot(), v0));
3876 __ push(v0);
3877 __ li(v0, Operand(prop->key()->AsLiteral()->handle()));
3878 } else {
3879 VisitForStackValue(prop->obj());
3880 VisitForAccumulatorValue(prop->key());
3881 }
3882 __ lw(a1, MemOperand(sp, 0));
3883 __ push(v0);
3884 EmitKeyedPropertyLoad(prop);
3885 }
3886 }
3887
3888 // We need a second deoptimization point after loading the value
3889 // in case evaluating the property load my have a side effect.
3890 if (assign_type == VARIABLE) {
3891 PrepareForBailout(expr->expression(), TOS_REG);
3892 } else {
3893 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3894 }
3895
3896 // Call ToNumber only if operand is not a smi.
3897 Label no_conversion;
3898 __ JumpIfSmi(v0, &no_conversion);
3899 __ mov(a0, v0);
3900 ToNumberStub convert_stub;
3901 __ CallStub(&convert_stub);
3902 __ bind(&no_conversion);
3903
3904 // Save result for postfix expressions.
3905 if (expr->is_postfix()) {
3906 if (!context()->IsEffect()) {
3907 // Save the result on the stack. If we have a named or keyed property
3908 // we store the result under the receiver that is currently on top
3909 // of the stack.
3910 switch (assign_type) {
3911 case VARIABLE:
3912 __ push(v0);
3913 break;
3914 case NAMED_PROPERTY:
3915 __ sw(v0, MemOperand(sp, kPointerSize));
3916 break;
3917 case KEYED_PROPERTY:
3918 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3919 break;
3920 }
3921 }
3922 }
3923 __ mov(a0, result_register());
3924
3925 // Inline smi case if we are in a loop.
3926 Label stub_call, done;
3927 JumpPatchSite patch_site(masm_);
3928
3929 int count_value = expr->op() == Token::INC ? 1 : -1;
3930 __ li(a1, Operand(Smi::FromInt(count_value)));
3931
3932 if (ShouldInlineSmiCase(expr->op())) {
3933 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
3934 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
3935
3936 // We could eliminate this smi check if we split the code at
3937 // the first smi check before calling ToNumber.
3938 patch_site.EmitJumpIfSmi(v0, &done);
3939 __ bind(&stub_call);
3940 }
3941
3942 // Record position before stub call.
3943 SetSourcePosition(expr->position());
3944
danno@chromium.org40cb8782011-05-25 07:58:50 +00003945 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003946 EmitCallIC(stub.GetCode(), &patch_site, expr->CountId());
3947 __ bind(&done);
3948
3949 // Store the value returned in v0.
3950 switch (assign_type) {
3951 case VARIABLE:
3952 if (expr->is_postfix()) {
3953 { EffectContext context(this);
3954 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3955 Token::ASSIGN);
3956 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3957 context.Plug(v0);
3958 }
3959 // For all contexts except EffectConstant we have the result on
3960 // top of the stack.
3961 if (!context()->IsEffect()) {
3962 context()->PlugTOS();
3963 }
3964 } else {
3965 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3966 Token::ASSIGN);
3967 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3968 context()->Plug(v0);
3969 }
3970 break;
3971 case NAMED_PROPERTY: {
3972 __ mov(a0, result_register()); // Value.
3973 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
3974 __ pop(a1); // Receiver.
3975 Handle<Code> ic = is_strict_mode()
3976 ? isolate()->builtins()->StoreIC_Initialize_Strict()
3977 : isolate()->builtins()->StoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00003978 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003979 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3980 if (expr->is_postfix()) {
3981 if (!context()->IsEffect()) {
3982 context()->PlugTOS();
3983 }
3984 } else {
3985 context()->Plug(v0);
3986 }
3987 break;
3988 }
3989 case KEYED_PROPERTY: {
3990 __ mov(a0, result_register()); // Value.
3991 __ pop(a1); // Key.
3992 __ pop(a2); // Receiver.
3993 Handle<Code> ic = is_strict_mode()
3994 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
3995 : isolate()->builtins()->KeyedStoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00003996 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003997 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3998 if (expr->is_postfix()) {
3999 if (!context()->IsEffect()) {
4000 context()->PlugTOS();
4001 }
4002 } else {
4003 context()->Plug(v0);
4004 }
4005 break;
4006 }
4007 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004008}
4009
4010
lrn@chromium.org7516f052011-03-30 08:52:27 +00004011void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004012 VariableProxy* proxy = expr->AsVariableProxy();
4013 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
4014 Comment cmnt(masm_, "Global variable");
4015 __ lw(a0, GlobalObjectOperand());
4016 __ li(a2, Operand(proxy->name()));
4017 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4018 // Use a regular load, not a contextual load, to avoid a reference
4019 // error.
4020 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
4021 PrepareForBailout(expr, TOS_REG);
4022 context()->Plug(v0);
4023 } else if (proxy != NULL &&
4024 proxy->var()->AsSlot() != NULL &&
4025 proxy->var()->AsSlot()->type() == Slot::LOOKUP) {
4026 Label done, slow;
4027
4028 // Generate code for loading from variables potentially shadowed
4029 // by eval-introduced variables.
4030 Slot* slot = proxy->var()->AsSlot();
4031 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
4032
4033 __ bind(&slow);
4034 __ li(a0, Operand(proxy->name()));
4035 __ Push(cp, a0);
4036 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4037 PrepareForBailout(expr, TOS_REG);
4038 __ bind(&done);
4039
4040 context()->Plug(v0);
4041 } else {
4042 // This expression cannot throw a reference error at the top level.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004043 VisitInCurrentContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004044 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004045}
4046
4047
lrn@chromium.org7516f052011-03-30 08:52:27 +00004048bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
4049 Expression* left,
4050 Expression* right,
4051 Label* if_true,
4052 Label* if_false,
4053 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004054 if (op != Token::EQ && op != Token::EQ_STRICT) return false;
4055
4056 // Check for the pattern: typeof <expression> == <string literal>.
4057 Literal* right_literal = right->AsLiteral();
4058 if (right_literal == NULL) return false;
4059 Handle<Object> right_literal_value = right_literal->handle();
4060 if (!right_literal_value->IsString()) return false;
4061 UnaryOperation* left_unary = left->AsUnaryOperation();
4062 if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
4063 Handle<String> check = Handle<String>::cast(right_literal_value);
4064
4065 { AccumulatorValueContext context(this);
4066 VisitForTypeofValue(left_unary->expression());
4067 }
4068 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4069
4070 if (check->Equals(isolate()->heap()->number_symbol())) {
4071 __ JumpIfSmi(v0, if_true);
4072 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4073 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4074 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4075 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4076 __ JumpIfSmi(v0, if_false);
4077 // Check for undetectable objects => false.
4078 __ GetObjectType(v0, v0, a1);
4079 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4080 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4081 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4082 Split(eq, a1, Operand(zero_reg),
4083 if_true, if_false, fall_through);
4084 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4085 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4086 __ Branch(if_true, eq, v0, Operand(at));
4087 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4088 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4089 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4090 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4091 __ Branch(if_true, eq, v0, Operand(at));
4092 __ JumpIfSmi(v0, if_false);
4093 // Check for undetectable objects => true.
4094 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4095 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4096 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4097 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4098 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4099 __ JumpIfSmi(v0, if_false);
4100 __ GetObjectType(v0, a1, v0); // Leave map in a1.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004101 Split(ge, v0, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004102 if_true, if_false, fall_through);
4103
4104 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4105 __ JumpIfSmi(v0, if_false);
4106 __ LoadRoot(at, Heap::kNullValueRootIndex);
4107 __ Branch(if_true, eq, v0, Operand(at));
4108 // Check for JS objects => true.
4109 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004110 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004111 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004112 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004113 // Check for undetectable objects => false.
4114 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4115 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4116 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4117 } else {
4118 if (if_false != fall_through) __ jmp(if_false);
4119 }
4120
4121 return true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00004122}
4123
4124
ager@chromium.org5c838252010-02-19 08:53:10 +00004125void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004126 Comment cmnt(masm_, "[ CompareOperation");
4127 SetSourcePosition(expr->position());
4128
4129 // Always perform the comparison for its control flow. Pack the result
4130 // into the expression's context after the comparison is performed.
4131
4132 Label materialize_true, materialize_false;
4133 Label* if_true = NULL;
4134 Label* if_false = NULL;
4135 Label* fall_through = NULL;
4136 context()->PrepareTest(&materialize_true, &materialize_false,
4137 &if_true, &if_false, &fall_through);
4138
4139 // First we try a fast inlined version of the compare when one of
4140 // the operands is a literal.
4141 Token::Value op = expr->op();
4142 Expression* left = expr->left();
4143 Expression* right = expr->right();
4144 if (TryLiteralCompare(op, left, right, if_true, if_false, fall_through)) {
4145 context()->Plug(if_true, if_false);
4146 return;
4147 }
4148
4149 VisitForStackValue(expr->left());
4150 switch (op) {
4151 case Token::IN:
4152 VisitForStackValue(expr->right());
4153 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
4154 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
4155 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4156 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4157 break;
4158
4159 case Token::INSTANCEOF: {
4160 VisitForStackValue(expr->right());
4161 InstanceofStub stub(InstanceofStub::kNoFlags);
4162 __ CallStub(&stub);
4163 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4164 // The stub returns 0 for true.
4165 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4166 break;
4167 }
4168
4169 default: {
4170 VisitForAccumulatorValue(expr->right());
4171 Condition cc = eq;
4172 bool strict = false;
4173 switch (op) {
4174 case Token::EQ_STRICT:
4175 strict = true;
4176 // Fall through.
4177 case Token::EQ:
4178 cc = eq;
4179 __ mov(a0, result_register());
4180 __ pop(a1);
4181 break;
4182 case Token::LT:
4183 cc = lt;
4184 __ mov(a0, result_register());
4185 __ pop(a1);
4186 break;
4187 case Token::GT:
4188 // Reverse left and right sides to obtain ECMA-262 conversion order.
4189 cc = lt;
4190 __ mov(a1, result_register());
4191 __ pop(a0);
4192 break;
4193 case Token::LTE:
4194 // Reverse left and right sides to obtain ECMA-262 conversion order.
4195 cc = ge;
4196 __ mov(a1, result_register());
4197 __ pop(a0);
4198 break;
4199 case Token::GTE:
4200 cc = ge;
4201 __ mov(a0, result_register());
4202 __ pop(a1);
4203 break;
4204 case Token::IN:
4205 case Token::INSTANCEOF:
4206 default:
4207 UNREACHABLE();
4208 }
4209
4210 bool inline_smi_code = ShouldInlineSmiCase(op);
4211 JumpPatchSite patch_site(masm_);
4212 if (inline_smi_code) {
4213 Label slow_case;
4214 __ Or(a2, a0, Operand(a1));
4215 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4216 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4217 __ bind(&slow_case);
4218 }
4219 // Record position and call the compare IC.
4220 SetSourcePosition(expr->position());
4221 Handle<Code> ic = CompareIC::GetUninitialized(op);
4222 EmitCallIC(ic, &patch_site, expr->id());
4223 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4224 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4225 }
4226 }
4227
4228 // Convert the result of the comparison into one expected for this
4229 // expression's context.
4230 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004231}
4232
4233
lrn@chromium.org7516f052011-03-30 08:52:27 +00004234void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004235 Comment cmnt(masm_, "[ CompareToNull");
4236 Label materialize_true, materialize_false;
4237 Label* if_true = NULL;
4238 Label* if_false = NULL;
4239 Label* fall_through = NULL;
4240 context()->PrepareTest(&materialize_true, &materialize_false,
4241 &if_true, &if_false, &fall_through);
4242
4243 VisitForAccumulatorValue(expr->expression());
4244 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4245 __ mov(a0, result_register());
4246 __ LoadRoot(a1, Heap::kNullValueRootIndex);
4247 if (expr->is_strict()) {
4248 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4249 } else {
4250 __ Branch(if_true, eq, a0, Operand(a1));
4251 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
4252 __ Branch(if_true, eq, a0, Operand(a1));
4253 __ And(at, a0, Operand(kSmiTagMask));
4254 __ Branch(if_false, eq, at, Operand(zero_reg));
4255 // It can be an undetectable object.
4256 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4257 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4258 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4259 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4260 }
4261 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004262}
4263
4264
ager@chromium.org5c838252010-02-19 08:53:10 +00004265void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004266 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4267 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004268}
4269
4270
lrn@chromium.org7516f052011-03-30 08:52:27 +00004271Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004272 return v0;
4273}
ager@chromium.org5c838252010-02-19 08:53:10 +00004274
4275
lrn@chromium.org7516f052011-03-30 08:52:27 +00004276Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004277 return cp;
4278}
4279
4280
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004281void FullCodeGenerator::EmitCallIC(Handle<Code> ic,
4282 RelocInfo::Mode mode,
4283 unsigned ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004284 ASSERT(mode == RelocInfo::CODE_TARGET ||
danno@chromium.org40cb8782011-05-25 07:58:50 +00004285 mode == RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004286 Counters* counters = isolate()->counters();
4287 switch (ic->kind()) {
4288 case Code::LOAD_IC:
4289 __ IncrementCounter(counters->named_load_full(), 1, a1, a2);
4290 break;
4291 case Code::KEYED_LOAD_IC:
4292 __ IncrementCounter(counters->keyed_load_full(), 1, a1, a2);
4293 break;
4294 case Code::STORE_IC:
4295 __ IncrementCounter(counters->named_store_full(), 1, a1, a2);
4296 break;
4297 case Code::KEYED_STORE_IC:
4298 __ IncrementCounter(counters->keyed_store_full(), 1, a1, a2);
4299 default:
4300 break;
4301 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00004302 if (ast_id == kNoASTId || mode == RelocInfo::CODE_TARGET_CONTEXT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004303 __ Call(ic, mode);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004304 } else {
4305 ASSERT(mode == RelocInfo::CODE_TARGET);
4306 mode = RelocInfo::CODE_TARGET_WITH_ID;
4307 __ CallWithAstId(ic, mode, ast_id);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004308 }
4309}
4310
4311
4312void FullCodeGenerator::EmitCallIC(Handle<Code> ic,
4313 JumpPatchSite* patch_site,
4314 unsigned ast_id) {
4315 Counters* counters = isolate()->counters();
4316 switch (ic->kind()) {
4317 case Code::LOAD_IC:
4318 __ IncrementCounter(counters->named_load_full(), 1, a1, a2);
4319 break;
4320 case Code::KEYED_LOAD_IC:
4321 __ IncrementCounter(counters->keyed_load_full(), 1, a1, a2);
4322 break;
4323 case Code::STORE_IC:
4324 __ IncrementCounter(counters->named_store_full(), 1, a1, a2);
4325 break;
4326 case Code::KEYED_STORE_IC:
4327 __ IncrementCounter(counters->keyed_store_full(), 1, a1, a2);
4328 default:
4329 break;
4330 }
4331
danno@chromium.org40cb8782011-05-25 07:58:50 +00004332 if (ast_id == kNoASTId) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004333 __ Call(ic, RelocInfo::CODE_TARGET);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004334 } else {
4335 __ CallWithAstId(ic, RelocInfo::CODE_TARGET_WITH_ID, ast_id);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004336 }
4337 if (patch_site != NULL && patch_site->is_bound()) {
4338 patch_site->EmitPatchInfo();
4339 } else {
4340 __ nop(); // Signals no inlined code.
4341 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00004342}
ager@chromium.org5c838252010-02-19 08:53:10 +00004343
4344
4345void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004346 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4347 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004348}
4349
4350
4351void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004352 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004353}
4354
4355
4356// ----------------------------------------------------------------------------
4357// Non-local control flow support.
4358
4359void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004360 ASSERT(!result_register().is(a1));
4361 // Store result register while executing finally block.
4362 __ push(result_register());
4363 // Cook return address in link register to stack (smi encoded Code* delta).
4364 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4365 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4366 ASSERT_EQ(0, kSmiTag);
4367 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4368 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004369}
4370
4371
4372void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004373 ASSERT(!result_register().is(a1));
4374 // Restore result register from stack.
4375 __ pop(a1);
4376 // Uncook return address and return.
4377 __ pop(result_register());
4378 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4379 __ sra(a1, a1, 1); // Un-smi-tag value.
4380 __ Addu(at, a1, Operand(masm_->CodeObject()));
4381 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004382}
4383
4384
4385#undef __
4386
4387} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004388
4389#endif // V8_TARGET_ARCH_MIPS