blob: 9c93c633bbe5b40a24d0c48b2043fdda5457764b [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
danno@chromium.org40cb8782011-05-25 07:58:50 +0000150 // Strict mode functions need to replace the receiver with undefined
151 // when called as functions (without an explicit receiver
152 // object). t1 is zero for method calls and non-zero for function
153 // calls.
154 if (info->is_strict_mode()) {
155 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);
921 __ Branch(&done_convert, hs, a1, Operand(FIRST_JS_OBJECT_TYPE));
922 __ 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);
2313 // Push global receiver.
2314 __ lw(a1, GlobalObjectOperand());
2315 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2316 __ 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));
2480 __ Branch(if_false, lt, a1, Operand(FIRST_JS_OBJECT_TYPE));
2481 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2482 Split(le, a1, Operand(LAST_JS_OBJECT_TYPE), if_true, if_false, fall_through);
2483
2484 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002485}
2486
2487
2488void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002489 ASSERT(args->length() == 1);
2490
2491 VisitForAccumulatorValue(args->at(0));
2492
2493 Label materialize_true, materialize_false;
2494 Label* if_true = NULL;
2495 Label* if_false = NULL;
2496 Label* fall_through = NULL;
2497 context()->PrepareTest(&materialize_true, &materialize_false,
2498 &if_true, &if_false, &fall_through);
2499
2500 __ JumpIfSmi(v0, if_false);
2501 __ GetObjectType(v0, a1, a1);
2502 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2503 Split(ge, a1, Operand(FIRST_JS_OBJECT_TYPE),
2504 if_true, if_false, fall_through);
2505
2506 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002507}
2508
2509
2510void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002511 ASSERT(args->length() == 1);
2512
2513 VisitForAccumulatorValue(args->at(0));
2514
2515 Label materialize_true, materialize_false;
2516 Label* if_true = NULL;
2517 Label* if_false = NULL;
2518 Label* fall_through = NULL;
2519 context()->PrepareTest(&materialize_true, &materialize_false,
2520 &if_true, &if_false, &fall_through);
2521
2522 __ JumpIfSmi(v0, if_false);
2523 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2524 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2525 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2526 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2527 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2528
2529 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002530}
2531
2532
2533void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2534 ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002535
2536 ASSERT(args->length() == 1);
2537
2538 VisitForAccumulatorValue(args->at(0));
2539
2540 Label materialize_true, materialize_false;
2541 Label* if_true = NULL;
2542 Label* if_false = NULL;
2543 Label* fall_through = NULL;
2544 context()->PrepareTest(&materialize_true, &materialize_false,
2545 &if_true, &if_false, &fall_through);
2546
2547 if (FLAG_debug_code) __ AbortIfSmi(v0);
2548
2549 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2550 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2551 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2552 __ Branch(if_true, ne, t0, Operand(zero_reg));
2553
2554 // Check for fast case object. Generate false result for slow case object.
2555 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2556 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2557 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2558 __ Branch(if_false, eq, a2, Operand(t0));
2559
2560 // Look for valueOf symbol in the descriptor array, and indicate false if
2561 // found. The type is not checked, so if it is a transition it is a false
2562 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002563 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002564 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2565 // t0: descriptor array
2566 // a3: length of descriptor array
2567 // Calculate the end of the descriptor array.
2568 STATIC_ASSERT(kSmiTag == 0);
2569 STATIC_ASSERT(kSmiTagSize == 1);
2570 STATIC_ASSERT(kPointerSize == 4);
2571 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2572 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2573 __ Addu(a2, a2, t1);
2574
2575 // Calculate location of the first key name.
2576 __ Addu(t0,
2577 t0,
2578 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2579 DescriptorArray::kFirstIndex * kPointerSize));
2580 // Loop through all the keys in the descriptor array. If one of these is the
2581 // symbol valueOf the result is false.
2582 Label entry, loop;
2583 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2584 // used in the loop below.
2585 __ li(t2, Operand(FACTORY->value_of_symbol()));
2586 __ jmp(&entry);
2587 __ bind(&loop);
2588 __ lw(a3, MemOperand(t0, 0));
2589 __ Branch(if_false, eq, a3, Operand(t2));
2590 __ Addu(t0, t0, Operand(kPointerSize));
2591 __ bind(&entry);
2592 __ Branch(&loop, ne, t0, Operand(a2));
2593
2594 // If a valueOf property is not found on the object check that it's
2595 // prototype is the un-modified String prototype. If not result is false.
2596 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2597 __ JumpIfSmi(a2, if_false);
2598 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2599 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2600 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2601 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2602 __ Branch(if_false, ne, a2, Operand(a3));
2603
2604 // Set the bit in the map to indicate that it has been checked safe for
2605 // default valueOf and set true result.
2606 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2607 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2608 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2609 __ jmp(if_true);
2610
2611 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2612 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002613}
2614
2615
2616void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002617 ASSERT(args->length() == 1);
2618
2619 VisitForAccumulatorValue(args->at(0));
2620
2621 Label materialize_true, materialize_false;
2622 Label* if_true = NULL;
2623 Label* if_false = NULL;
2624 Label* fall_through = NULL;
2625 context()->PrepareTest(&materialize_true, &materialize_false,
2626 &if_true, &if_false, &fall_through);
2627
2628 __ JumpIfSmi(v0, if_false);
2629 __ GetObjectType(v0, a1, a2);
2630 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2631 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2632 __ Branch(if_false);
2633
2634 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002635}
2636
2637
2638void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002639 ASSERT(args->length() == 1);
2640
2641 VisitForAccumulatorValue(args->at(0));
2642
2643 Label materialize_true, materialize_false;
2644 Label* if_true = NULL;
2645 Label* if_false = NULL;
2646 Label* fall_through = NULL;
2647 context()->PrepareTest(&materialize_true, &materialize_false,
2648 &if_true, &if_false, &fall_through);
2649
2650 __ JumpIfSmi(v0, if_false);
2651 __ GetObjectType(v0, a1, a1);
2652 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2653 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2654 if_true, if_false, fall_through);
2655
2656 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002657}
2658
2659
2660void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002661 ASSERT(args->length() == 1);
2662
2663 VisitForAccumulatorValue(args->at(0));
2664
2665 Label materialize_true, materialize_false;
2666 Label* if_true = NULL;
2667 Label* if_false = NULL;
2668 Label* fall_through = NULL;
2669 context()->PrepareTest(&materialize_true, &materialize_false,
2670 &if_true, &if_false, &fall_through);
2671
2672 __ JumpIfSmi(v0, if_false);
2673 __ GetObjectType(v0, a1, a1);
2674 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2675 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2676
2677 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002678}
2679
2680
2681void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002682 ASSERT(args->length() == 0);
2683
2684 Label materialize_true, materialize_false;
2685 Label* if_true = NULL;
2686 Label* if_false = NULL;
2687 Label* fall_through = NULL;
2688 context()->PrepareTest(&materialize_true, &materialize_false,
2689 &if_true, &if_false, &fall_through);
2690
2691 // Get the frame pointer for the calling frame.
2692 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2693
2694 // Skip the arguments adaptor frame if it exists.
2695 Label check_frame_marker;
2696 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2697 __ Branch(&check_frame_marker, ne,
2698 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2699 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2700
2701 // Check the marker in the calling frame.
2702 __ bind(&check_frame_marker);
2703 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
2704 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2705 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2706 if_true, if_false, fall_through);
2707
2708 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002709}
2710
2711
2712void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002713 ASSERT(args->length() == 2);
2714
2715 // Load the two objects into registers and perform the comparison.
2716 VisitForStackValue(args->at(0));
2717 VisitForAccumulatorValue(args->at(1));
2718
2719 Label materialize_true, materialize_false;
2720 Label* if_true = NULL;
2721 Label* if_false = NULL;
2722 Label* fall_through = NULL;
2723 context()->PrepareTest(&materialize_true, &materialize_false,
2724 &if_true, &if_false, &fall_through);
2725
2726 __ pop(a1);
2727 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2728 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2729
2730 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002731}
2732
2733
2734void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002735 ASSERT(args->length() == 1);
2736
2737 // ArgumentsAccessStub expects the key in a1 and the formal
2738 // parameter count in a0.
2739 VisitForAccumulatorValue(args->at(0));
2740 __ mov(a1, v0);
2741 __ li(a0, Operand(Smi::FromInt(scope()->num_parameters())));
2742 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2743 __ CallStub(&stub);
2744 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002745}
2746
2747
2748void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002749 ASSERT(args->length() == 0);
2750
2751 Label exit;
2752 // Get the number of formal parameters.
2753 __ li(v0, Operand(Smi::FromInt(scope()->num_parameters())));
2754
2755 // Check if the calling frame is an arguments adaptor frame.
2756 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2757 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2758 __ Branch(&exit, ne, a3,
2759 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2760
2761 // Arguments adaptor case: Read the arguments length from the
2762 // adaptor frame.
2763 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2764
2765 __ bind(&exit);
2766 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002767}
2768
2769
2770void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002771 ASSERT(args->length() == 1);
2772 Label done, null, function, non_function_constructor;
2773
2774 VisitForAccumulatorValue(args->at(0));
2775
2776 // If the object is a smi, we return null.
2777 __ JumpIfSmi(v0, &null);
2778
2779 // Check that the object is a JS object but take special care of JS
2780 // functions to make sure they have 'Function' as their class.
2781 __ GetObjectType(v0, v0, a1); // Map is now in v0.
2782 __ Branch(&null, lt, a1, Operand(FIRST_JS_OBJECT_TYPE));
2783
2784 // As long as JS_FUNCTION_TYPE is the last instance type and it is
2785 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
2786 // LAST_JS_OBJECT_TYPE.
2787 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2788 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
2789 __ Branch(&function, eq, a1, Operand(JS_FUNCTION_TYPE));
2790
2791 // Check if the constructor in the map is a function.
2792 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2793 __ GetObjectType(v0, a1, a1);
2794 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2795
2796 // v0 now contains the constructor function. Grab the
2797 // instance class name from there.
2798 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2799 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2800 __ Branch(&done);
2801
2802 // Functions have class 'Function'.
2803 __ bind(&function);
2804 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2805 __ jmp(&done);
2806
2807 // Objects with a non-function constructor have class 'Object'.
2808 __ bind(&non_function_constructor);
2809 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2810 __ jmp(&done);
2811
2812 // Non-JS objects have class null.
2813 __ bind(&null);
2814 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2815
2816 // All done.
2817 __ bind(&done);
2818
2819 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002820}
2821
2822
2823void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002824 // Conditionally generate a log call.
2825 // Args:
2826 // 0 (literal string): The type of logging (corresponds to the flags).
2827 // This is used to determine whether or not to generate the log call.
2828 // 1 (string): Format string. Access the string at argument index 2
2829 // with '%2s' (see Logger::LogRuntime for all the formats).
2830 // 2 (array): Arguments to the format string.
2831 ASSERT_EQ(args->length(), 3);
2832#ifdef ENABLE_LOGGING_AND_PROFILING
2833 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2834 VisitForStackValue(args->at(1));
2835 VisitForStackValue(args->at(2));
2836 __ CallRuntime(Runtime::kLog, 2);
2837 }
2838#endif
2839 // Finally, we're expected to leave a value on the top of the stack.
2840 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2841 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002842}
2843
2844
2845void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002846 ASSERT(args->length() == 0);
2847
2848 Label slow_allocate_heapnumber;
2849 Label heapnumber_allocated;
2850
2851 // Save the new heap number in callee-saved register s0, since
2852 // we call out to external C code below.
2853 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2854 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2855 __ jmp(&heapnumber_allocated);
2856
2857 __ bind(&slow_allocate_heapnumber);
2858
2859 // Allocate a heap number.
2860 __ CallRuntime(Runtime::kNumberAlloc, 0);
2861 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2862
2863 __ bind(&heapnumber_allocated);
2864
2865 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2866 // by computing:
2867 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2868 if (CpuFeatures::IsSupported(FPU)) {
2869 __ PrepareCallCFunction(1, a0);
2870 __ li(a0, Operand(ExternalReference::isolate_address()));
2871 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2872
2873
2874 CpuFeatures::Scope scope(FPU);
2875 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2876 __ li(a1, Operand(0x41300000));
2877 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002878 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002879 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002880 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002881 // Subtract and store the result in the heap number.
2882 __ sub_d(f0, f12, f14);
2883 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2884 __ mov(v0, s0);
2885 } else {
2886 __ PrepareCallCFunction(2, a0);
2887 __ mov(a0, s0);
2888 __ li(a1, Operand(ExternalReference::isolate_address()));
2889 __ CallCFunction(
2890 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2891 }
2892
2893 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002894}
2895
2896
2897void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002898 // Load the arguments on the stack and call the stub.
2899 SubStringStub stub;
2900 ASSERT(args->length() == 3);
2901 VisitForStackValue(args->at(0));
2902 VisitForStackValue(args->at(1));
2903 VisitForStackValue(args->at(2));
2904 __ CallStub(&stub);
2905 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002906}
2907
2908
2909void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002910 // Load the arguments on the stack and call the stub.
2911 RegExpExecStub stub;
2912 ASSERT(args->length() == 4);
2913 VisitForStackValue(args->at(0));
2914 VisitForStackValue(args->at(1));
2915 VisitForStackValue(args->at(2));
2916 VisitForStackValue(args->at(3));
2917 __ CallStub(&stub);
2918 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002919}
2920
2921
2922void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002923 ASSERT(args->length() == 1);
2924
2925 VisitForAccumulatorValue(args->at(0)); // Load the object.
2926
2927 Label done;
2928 // If the object is a smi return the object.
2929 __ JumpIfSmi(v0, &done);
2930 // If the object is not a value type, return the object.
2931 __ GetObjectType(v0, a1, a1);
2932 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2933
2934 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2935
2936 __ bind(&done);
2937 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002938}
2939
2940
2941void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002942 // Load the arguments on the stack and call the runtime function.
2943 ASSERT(args->length() == 2);
2944 VisitForStackValue(args->at(0));
2945 VisitForStackValue(args->at(1));
2946 MathPowStub stub;
2947 __ CallStub(&stub);
2948 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002949}
2950
2951
2952void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002953 ASSERT(args->length() == 2);
2954
2955 VisitForStackValue(args->at(0)); // Load the object.
2956 VisitForAccumulatorValue(args->at(1)); // Load the value.
2957 __ pop(a1); // v0 = value. a1 = object.
2958
2959 Label done;
2960 // If the object is a smi, return the value.
2961 __ JumpIfSmi(a1, &done);
2962
2963 // If the object is not a value type, return the value.
2964 __ GetObjectType(a1, a2, a2);
2965 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2966
2967 // Store the value.
2968 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2969 // Update the write barrier. Save the value as it will be
2970 // overwritten by the write barrier code and is needed afterward.
2971 __ RecordWrite(a1, Operand(JSValue::kValueOffset - kHeapObjectTag), a2, a3);
2972
2973 __ bind(&done);
2974 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002975}
2976
2977
2978void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002979 ASSERT_EQ(args->length(), 1);
2980
2981 // Load the argument on the stack and call the stub.
2982 VisitForStackValue(args->at(0));
2983
2984 NumberToStringStub stub;
2985 __ CallStub(&stub);
2986 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002987}
2988
2989
2990void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002991 ASSERT(args->length() == 1);
2992
2993 VisitForAccumulatorValue(args->at(0));
2994
2995 Label done;
2996 StringCharFromCodeGenerator generator(v0, a1);
2997 generator.GenerateFast(masm_);
2998 __ jmp(&done);
2999
3000 NopRuntimeCallHelper call_helper;
3001 generator.GenerateSlow(masm_, call_helper);
3002
3003 __ bind(&done);
3004 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003005}
3006
3007
3008void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003009 ASSERT(args->length() == 2);
3010
3011 VisitForStackValue(args->at(0));
3012 VisitForAccumulatorValue(args->at(1));
3013 __ mov(a0, result_register());
3014
3015 Register object = a1;
3016 Register index = a0;
3017 Register scratch = a2;
3018 Register result = v0;
3019
3020 __ pop(object);
3021
3022 Label need_conversion;
3023 Label index_out_of_range;
3024 Label done;
3025 StringCharCodeAtGenerator generator(object,
3026 index,
3027 scratch,
3028 result,
3029 &need_conversion,
3030 &need_conversion,
3031 &index_out_of_range,
3032 STRING_INDEX_IS_NUMBER);
3033 generator.GenerateFast(masm_);
3034 __ jmp(&done);
3035
3036 __ bind(&index_out_of_range);
3037 // When the index is out of range, the spec requires us to return
3038 // NaN.
3039 __ LoadRoot(result, Heap::kNanValueRootIndex);
3040 __ jmp(&done);
3041
3042 __ bind(&need_conversion);
3043 // Load the undefined value into the result register, which will
3044 // trigger conversion.
3045 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3046 __ jmp(&done);
3047
3048 NopRuntimeCallHelper call_helper;
3049 generator.GenerateSlow(masm_, call_helper);
3050
3051 __ bind(&done);
3052 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003053}
3054
3055
3056void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003057 ASSERT(args->length() == 2);
3058
3059 VisitForStackValue(args->at(0));
3060 VisitForAccumulatorValue(args->at(1));
3061 __ mov(a0, result_register());
3062
3063 Register object = a1;
3064 Register index = a0;
3065 Register scratch1 = a2;
3066 Register scratch2 = a3;
3067 Register result = v0;
3068
3069 __ pop(object);
3070
3071 Label need_conversion;
3072 Label index_out_of_range;
3073 Label done;
3074 StringCharAtGenerator generator(object,
3075 index,
3076 scratch1,
3077 scratch2,
3078 result,
3079 &need_conversion,
3080 &need_conversion,
3081 &index_out_of_range,
3082 STRING_INDEX_IS_NUMBER);
3083 generator.GenerateFast(masm_);
3084 __ jmp(&done);
3085
3086 __ bind(&index_out_of_range);
3087 // When the index is out of range, the spec requires us to return
3088 // the empty string.
3089 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3090 __ jmp(&done);
3091
3092 __ bind(&need_conversion);
3093 // Move smi zero into the result register, which will trigger
3094 // conversion.
3095 __ li(result, Operand(Smi::FromInt(0)));
3096 __ jmp(&done);
3097
3098 NopRuntimeCallHelper call_helper;
3099 generator.GenerateSlow(masm_, call_helper);
3100
3101 __ bind(&done);
3102 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003103}
3104
3105
3106void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003107 ASSERT_EQ(2, args->length());
3108
3109 VisitForStackValue(args->at(0));
3110 VisitForStackValue(args->at(1));
3111
3112 StringAddStub stub(NO_STRING_ADD_FLAGS);
3113 __ CallStub(&stub);
3114 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003115}
3116
3117
3118void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003119 ASSERT_EQ(2, args->length());
3120
3121 VisitForStackValue(args->at(0));
3122 VisitForStackValue(args->at(1));
3123
3124 StringCompareStub stub;
3125 __ CallStub(&stub);
3126 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003127}
3128
3129
3130void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003131 // Load the argument on the stack and call the stub.
3132 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3133 TranscendentalCacheStub::TAGGED);
3134 ASSERT(args->length() == 1);
3135 VisitForStackValue(args->at(0));
3136 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3137 __ CallStub(&stub);
3138 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003139}
3140
3141
3142void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003143 // Load the argument on the stack and call the stub.
3144 TranscendentalCacheStub stub(TranscendentalCache::COS,
3145 TranscendentalCacheStub::TAGGED);
3146 ASSERT(args->length() == 1);
3147 VisitForStackValue(args->at(0));
3148 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3149 __ CallStub(&stub);
3150 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003151}
3152
3153
3154void FullCodeGenerator::EmitMathLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003155 // Load the argument on the stack and call the stub.
3156 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3157 TranscendentalCacheStub::TAGGED);
3158 ASSERT(args->length() == 1);
3159 VisitForStackValue(args->at(0));
3160 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3161 __ CallStub(&stub);
3162 context()->Plug(v0);
3163}
3164
3165
3166void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
3167 // Load the argument on the stack and call the runtime function.
3168 ASSERT(args->length() == 1);
3169 VisitForStackValue(args->at(0));
3170 __ CallRuntime(Runtime::kMath_sqrt, 1);
3171 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003172}
3173
3174
3175void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003176 ASSERT(args->length() >= 2);
3177
3178 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3179 for (int i = 0; i < arg_count + 1; i++) {
3180 VisitForStackValue(args->at(i));
3181 }
3182 VisitForAccumulatorValue(args->last()); // Function.
3183
3184 // InvokeFunction requires the function in a1. Move it in there.
3185 __ mov(a1, result_register());
3186 ParameterCount count(arg_count);
3187 __ InvokeFunction(a1, count, CALL_FUNCTION);
3188 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3189 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003190}
3191
3192
3193void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003194 RegExpConstructResultStub stub;
3195 ASSERT(args->length() == 3);
3196 VisitForStackValue(args->at(0));
3197 VisitForStackValue(args->at(1));
3198 VisitForStackValue(args->at(2));
3199 __ CallStub(&stub);
3200 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003201}
3202
3203
3204void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003205 ASSERT(args->length() == 3);
3206 VisitForStackValue(args->at(0));
3207 VisitForStackValue(args->at(1));
3208 VisitForStackValue(args->at(2));
3209 Label done;
3210 Label slow_case;
3211 Register object = a0;
3212 Register index1 = a1;
3213 Register index2 = a2;
3214 Register elements = a3;
3215 Register scratch1 = t0;
3216 Register scratch2 = t1;
3217
3218 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3219 // Fetch the map and check if array is in fast case.
3220 // Check that object doesn't require security checks and
3221 // has no indexed interceptor.
3222 __ GetObjectType(object, scratch1, scratch2);
3223 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3224 // Map is now in scratch1.
3225
3226 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3227 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3228 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3229
3230 // Check the object's elements are in fast case and writable.
3231 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3232 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3233 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3234 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3235
3236 // Check that both indices are smis.
3237 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3238 __ lw(index2, MemOperand(sp, 0));
3239 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3240
3241 // Check that both indices are valid.
3242 Label not_hi;
3243 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3244 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3245 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3246 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3247 __ bind(&not_hi);
3248
3249 // Bring the address of the elements into index1 and index2.
3250 __ Addu(scratch1, elements,
3251 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3252 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3253 __ Addu(index1, scratch1, index1);
3254 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3255 __ Addu(index2, scratch1, index2);
3256
3257 // Swap elements.
3258 __ lw(scratch1, MemOperand(index1, 0));
3259 __ lw(scratch2, MemOperand(index2, 0));
3260 __ sw(scratch1, MemOperand(index2, 0));
3261 __ sw(scratch2, MemOperand(index1, 0));
3262
3263 Label new_space;
3264 __ InNewSpace(elements, scratch1, eq, &new_space);
3265 // Possible optimization: do a check that both values are Smis
3266 // (or them and test against Smi mask).
3267
3268 __ mov(scratch1, elements);
3269 __ RecordWriteHelper(elements, index1, scratch2);
3270 __ RecordWriteHelper(scratch1, index2, scratch2); // scratch1 holds elements.
3271
3272 __ bind(&new_space);
3273 // We are done. Drop elements from the stack, and return undefined.
3274 __ Drop(3);
3275 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3276 __ jmp(&done);
3277
3278 __ bind(&slow_case);
3279 __ CallRuntime(Runtime::kSwapElements, 3);
3280
3281 __ bind(&done);
3282 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003283}
3284
3285
3286void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003287 ASSERT_EQ(2, args->length());
3288
3289 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3290 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3291
3292 Handle<FixedArray> jsfunction_result_caches(
3293 isolate()->global_context()->jsfunction_result_caches());
3294 if (jsfunction_result_caches->length() <= cache_id) {
3295 __ Abort("Attempt to use undefined cache.");
3296 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3297 context()->Plug(v0);
3298 return;
3299 }
3300
3301 VisitForAccumulatorValue(args->at(1));
3302
3303 Register key = v0;
3304 Register cache = a1;
3305 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3306 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3307 __ lw(cache,
3308 ContextOperand(
3309 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3310 __ lw(cache,
3311 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3312
3313
3314 Label done, not_found;
3315 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
3316 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3317 // a2 now holds finger offset as a smi.
3318 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3319 // a3 now points to the start of fixed array elements.
3320 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3321 __ addu(a3, a3, at);
3322 // a3 now points to key of indexed element of cache.
3323 __ lw(a2, MemOperand(a3));
3324 __ Branch(&not_found, ne, key, Operand(a2));
3325
3326 __ lw(v0, MemOperand(a3, kPointerSize));
3327 __ Branch(&done);
3328
3329 __ bind(&not_found);
3330 // Call runtime to perform the lookup.
3331 __ Push(cache, key);
3332 __ CallRuntime(Runtime::kGetFromCache, 2);
3333
3334 __ bind(&done);
3335 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003336}
3337
3338
3339void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003340 ASSERT_EQ(2, args->length());
3341
3342 Register right = v0;
3343 Register left = a1;
3344 Register tmp = a2;
3345 Register tmp2 = a3;
3346
3347 VisitForStackValue(args->at(0));
3348 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3349 __ pop(left);
3350
3351 Label done, fail, ok;
3352 __ Branch(&ok, eq, left, Operand(right));
3353 // Fail if either is a non-HeapObject.
3354 __ And(tmp, left, Operand(right));
3355 __ And(at, tmp, Operand(kSmiTagMask));
3356 __ Branch(&fail, eq, at, Operand(zero_reg));
3357 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3358 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3359 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3360 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3361 __ Branch(&fail, ne, tmp, Operand(tmp2));
3362 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3363 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3364 __ Branch(&ok, eq, tmp, Operand(tmp2));
3365 __ bind(&fail);
3366 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3367 __ jmp(&done);
3368 __ bind(&ok);
3369 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3370 __ bind(&done);
3371
3372 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003373}
3374
3375
3376void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003377 VisitForAccumulatorValue(args->at(0));
3378
3379 Label materialize_true, materialize_false;
3380 Label* if_true = NULL;
3381 Label* if_false = NULL;
3382 Label* fall_through = NULL;
3383 context()->PrepareTest(&materialize_true, &materialize_false,
3384 &if_true, &if_false, &fall_through);
3385
3386 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3387 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3388
3389 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3390 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3391
3392 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003393}
3394
3395
3396void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003397 ASSERT(args->length() == 1);
3398 VisitForAccumulatorValue(args->at(0));
3399
3400 if (FLAG_debug_code) {
3401 __ AbortIfNotString(v0);
3402 }
3403
3404 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3405 __ IndexFromHash(v0, v0);
3406
3407 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003408}
3409
3410
3411void FullCodeGenerator::EmitFastAsciiArrayJoin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003412 Label bailout, done, one_char_separator, long_separator,
3413 non_trivial_array, not_size_one_array, loop,
3414 empty_separator_loop, one_char_separator_loop,
3415 one_char_separator_loop_entry, long_separator_loop;
3416
3417 ASSERT(args->length() == 2);
3418 VisitForStackValue(args->at(1));
3419 VisitForAccumulatorValue(args->at(0));
3420
3421 // All aliases of the same register have disjoint lifetimes.
3422 Register array = v0;
3423 Register elements = no_reg; // Will be v0.
3424 Register result = no_reg; // Will be v0.
3425 Register separator = a1;
3426 Register array_length = a2;
3427 Register result_pos = no_reg; // Will be a2.
3428 Register string_length = a3;
3429 Register string = t0;
3430 Register element = t1;
3431 Register elements_end = t2;
3432 Register scratch1 = t3;
3433 Register scratch2 = t5;
3434 Register scratch3 = t4;
3435 Register scratch4 = v1;
3436
3437 // Separator operand is on the stack.
3438 __ pop(separator);
3439
3440 // Check that the array is a JSArray.
3441 __ JumpIfSmi(array, &bailout);
3442 __ GetObjectType(array, scratch1, scratch2);
3443 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3444
3445 // Check that the array has fast elements.
3446 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitField2Offset));
3447 __ And(scratch3, scratch2, Operand(1 << Map::kHasFastElements));
3448 __ Branch(&bailout, eq, scratch3, Operand(zero_reg));
3449
3450 // If the array has length zero, return the empty string.
3451 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3452 __ SmiUntag(array_length);
3453 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3454 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3455 __ Branch(&done);
3456
3457 __ bind(&non_trivial_array);
3458
3459 // Get the FixedArray containing array's elements.
3460 elements = array;
3461 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3462 array = no_reg; // End of array's live range.
3463
3464 // Check that all array elements are sequential ASCII strings, and
3465 // accumulate the sum of their lengths, as a smi-encoded value.
3466 __ mov(string_length, zero_reg);
3467 __ Addu(element,
3468 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3469 __ sll(elements_end, array_length, kPointerSizeLog2);
3470 __ Addu(elements_end, element, elements_end);
3471 // Loop condition: while (element < elements_end).
3472 // Live values in registers:
3473 // elements: Fixed array of strings.
3474 // array_length: Length of the fixed array of strings (not smi)
3475 // separator: Separator string
3476 // string_length: Accumulated sum of string lengths (smi).
3477 // element: Current array element.
3478 // elements_end: Array end.
3479 if (FLAG_debug_code) {
3480 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3481 array_length, Operand(zero_reg));
3482 }
3483 __ bind(&loop);
3484 __ lw(string, MemOperand(element));
3485 __ Addu(element, element, kPointerSize);
3486 __ JumpIfSmi(string, &bailout);
3487 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3488 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3489 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3490 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3491 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3492 __ BranchOnOverflow(&bailout, scratch3);
3493 __ Branch(&loop, lt, element, Operand(elements_end));
3494
3495 // If array_length is 1, return elements[0], a string.
3496 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3497 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3498 __ Branch(&done);
3499
3500 __ bind(&not_size_one_array);
3501
3502 // Live values in registers:
3503 // separator: Separator string
3504 // array_length: Length of the array.
3505 // string_length: Sum of string lengths (smi).
3506 // elements: FixedArray of strings.
3507
3508 // Check that the separator is a flat ASCII string.
3509 __ JumpIfSmi(separator, &bailout);
3510 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3511 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3512 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3513
3514 // Add (separator length times array_length) - separator length to the
3515 // string_length to get the length of the result string. array_length is not
3516 // smi but the other values are, so the result is a smi.
3517 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3518 __ Subu(string_length, string_length, Operand(scratch1));
3519 __ Mult(array_length, scratch1);
3520 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3521 // zero.
3522 __ mfhi(scratch2);
3523 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3524 __ mflo(scratch2);
3525 __ And(scratch3, scratch2, Operand(0x80000000));
3526 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3527 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3528 __ BranchOnOverflow(&bailout, scratch3);
3529 __ SmiUntag(string_length);
3530
3531 // Get first element in the array to free up the elements register to be used
3532 // for the result.
3533 __ Addu(element,
3534 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3535 result = elements; // End of live range for elements.
3536 elements = no_reg;
3537 // Live values in registers:
3538 // element: First array element
3539 // separator: Separator string
3540 // string_length: Length of result string (not smi)
3541 // array_length: Length of the array.
3542 __ AllocateAsciiString(result,
3543 string_length,
3544 scratch1,
3545 scratch2,
3546 elements_end,
3547 &bailout);
3548 // Prepare for looping. Set up elements_end to end of the array. Set
3549 // result_pos to the position of the result where to write the first
3550 // character.
3551 __ sll(elements_end, array_length, kPointerSizeLog2);
3552 __ Addu(elements_end, element, elements_end);
3553 result_pos = array_length; // End of live range for array_length.
3554 array_length = no_reg;
3555 __ Addu(result_pos,
3556 result,
3557 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3558
3559 // Check the length of the separator.
3560 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3561 __ li(at, Operand(Smi::FromInt(1)));
3562 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3563 __ Branch(&long_separator, gt, scratch1, Operand(at));
3564
3565 // Empty separator case.
3566 __ bind(&empty_separator_loop);
3567 // Live values in registers:
3568 // result_pos: the position to which we are currently copying characters.
3569 // element: Current array element.
3570 // elements_end: Array end.
3571
3572 // Copy next array element to the result.
3573 __ lw(string, MemOperand(element));
3574 __ Addu(element, element, kPointerSize);
3575 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3576 __ SmiUntag(string_length);
3577 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3578 __ CopyBytes(string, result_pos, string_length, scratch1);
3579 // End while (element < elements_end).
3580 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3581 ASSERT(result.is(v0));
3582 __ Branch(&done);
3583
3584 // One-character separator case.
3585 __ bind(&one_char_separator);
3586 // Replace separator with its ascii character value.
3587 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3588 // Jump into the loop after the code that copies the separator, so the first
3589 // element is not preceded by a separator.
3590 __ jmp(&one_char_separator_loop_entry);
3591
3592 __ bind(&one_char_separator_loop);
3593 // Live values in registers:
3594 // result_pos: the position to which we are currently copying characters.
3595 // element: Current array element.
3596 // elements_end: Array end.
3597 // separator: Single separator ascii char (in lower byte).
3598
3599 // Copy the separator character to the result.
3600 __ sb(separator, MemOperand(result_pos));
3601 __ Addu(result_pos, result_pos, 1);
3602
3603 // Copy next array element to the result.
3604 __ bind(&one_char_separator_loop_entry);
3605 __ lw(string, MemOperand(element));
3606 __ Addu(element, element, kPointerSize);
3607 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3608 __ SmiUntag(string_length);
3609 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3610 __ CopyBytes(string, result_pos, string_length, scratch1);
3611 // End while (element < elements_end).
3612 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3613 ASSERT(result.is(v0));
3614 __ Branch(&done);
3615
3616 // Long separator case (separator is more than one character). Entry is at the
3617 // label long_separator below.
3618 __ bind(&long_separator_loop);
3619 // Live values in registers:
3620 // result_pos: the position to which we are currently copying characters.
3621 // element: Current array element.
3622 // elements_end: Array end.
3623 // separator: Separator string.
3624
3625 // Copy the separator to the result.
3626 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3627 __ SmiUntag(string_length);
3628 __ Addu(string,
3629 separator,
3630 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3631 __ CopyBytes(string, result_pos, string_length, scratch1);
3632
3633 __ bind(&long_separator);
3634 __ lw(string, MemOperand(element));
3635 __ Addu(element, element, kPointerSize);
3636 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3637 __ SmiUntag(string_length);
3638 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3639 __ CopyBytes(string, result_pos, string_length, scratch1);
3640 // End while (element < elements_end).
3641 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3642 ASSERT(result.is(v0));
3643 __ Branch(&done);
3644
3645 __ bind(&bailout);
3646 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3647 __ bind(&done);
3648 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003649}
3650
3651
ager@chromium.org5c838252010-02-19 08:53:10 +00003652void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003653 Handle<String> name = expr->name();
3654 if (name->length() > 0 && name->Get(0) == '_') {
3655 Comment cmnt(masm_, "[ InlineRuntimeCall");
3656 EmitInlineRuntimeCall(expr);
3657 return;
3658 }
3659
3660 Comment cmnt(masm_, "[ CallRuntime");
3661 ZoneList<Expression*>* args = expr->arguments();
3662
3663 if (expr->is_jsruntime()) {
3664 // Prepare for calling JS runtime function.
3665 __ lw(a0, GlobalObjectOperand());
3666 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3667 __ push(a0);
3668 }
3669
3670 // Push the arguments ("left-to-right").
3671 int arg_count = args->length();
3672 for (int i = 0; i < arg_count; i++) {
3673 VisitForStackValue(args->at(i));
3674 }
3675
3676 if (expr->is_jsruntime()) {
3677 // Call the JS runtime function.
3678 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003679 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003680 Handle<Code> ic =
danno@chromium.org40cb8782011-05-25 07:58:50 +00003681 isolate()->stub_cache()->ComputeCallInitialize(arg_count,
3682 NOT_IN_LOOP,
3683 mode);
3684 EmitCallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003685 // Restore context register.
3686 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3687 } else {
3688 // Call the C runtime function.
3689 __ CallRuntime(expr->function(), arg_count);
3690 }
3691 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003692}
3693
3694
3695void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003696 switch (expr->op()) {
3697 case Token::DELETE: {
3698 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
3699 Property* prop = expr->expression()->AsProperty();
3700 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
3701
3702 if (prop != NULL) {
3703 if (prop->is_synthetic()) {
3704 // Result of deleting parameters is false, even when they rewrite
3705 // to accesses on the arguments object.
3706 context()->Plug(false);
3707 } else {
3708 VisitForStackValue(prop->obj());
3709 VisitForStackValue(prop->key());
3710 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3711 __ push(a1);
3712 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3713 context()->Plug(v0);
3714 }
3715 } else if (var != NULL) {
3716 // Delete of an unqualified identifier is disallowed in strict mode
3717 // but "delete this" is.
3718 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
3719 if (var->is_global()) {
3720 __ lw(a2, GlobalObjectOperand());
3721 __ li(a1, Operand(var->name()));
3722 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3723 __ Push(a2, a1, a0);
3724 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3725 context()->Plug(v0);
3726 } else if (var->AsSlot() != NULL &&
3727 var->AsSlot()->type() != Slot::LOOKUP) {
3728 // Result of deleting non-global, non-dynamic variables is false.
3729 // The subexpression does not have side effects.
3730 context()->Plug(false);
3731 } else {
3732 // Non-global variable. Call the runtime to try to delete from the
3733 // context where the variable was introduced.
3734 __ push(context_register());
3735 __ li(a2, Operand(var->name()));
3736 __ push(a2);
3737 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3738 context()->Plug(v0);
3739 }
3740 } else {
3741 // Result of deleting non-property, non-variable reference is true.
3742 // The subexpression may have side effects.
3743 VisitForEffect(expr->expression());
3744 context()->Plug(true);
3745 }
3746 break;
3747 }
3748
3749 case Token::VOID: {
3750 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3751 VisitForEffect(expr->expression());
3752 context()->Plug(Heap::kUndefinedValueRootIndex);
3753 break;
3754 }
3755
3756 case Token::NOT: {
3757 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3758 if (context()->IsEffect()) {
3759 // Unary NOT has no side effects so it's only necessary to visit the
3760 // subexpression. Match the optimizing compiler by not branching.
3761 VisitForEffect(expr->expression());
3762 } else {
3763 Label materialize_true, materialize_false;
3764 Label* if_true = NULL;
3765 Label* if_false = NULL;
3766 Label* fall_through = NULL;
3767
3768 // Notice that the labels are swapped.
3769 context()->PrepareTest(&materialize_true, &materialize_false,
3770 &if_false, &if_true, &fall_through);
3771 if (context()->IsTest()) ForwardBailoutToChild(expr);
3772 VisitForControl(expr->expression(), if_true, if_false, fall_through);
3773 context()->Plug(if_false, if_true); // Labels swapped.
3774 }
3775 break;
3776 }
3777
3778 case Token::TYPEOF: {
3779 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3780 { StackValueContext context(this);
3781 VisitForTypeofValue(expr->expression());
3782 }
3783 __ CallRuntime(Runtime::kTypeof, 1);
3784 context()->Plug(v0);
3785 break;
3786 }
3787
3788 case Token::ADD: {
3789 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3790 VisitForAccumulatorValue(expr->expression());
3791 Label no_conversion;
3792 __ JumpIfSmi(result_register(), &no_conversion);
3793 __ mov(a0, result_register());
3794 ToNumberStub convert_stub;
3795 __ CallStub(&convert_stub);
3796 __ bind(&no_conversion);
3797 context()->Plug(result_register());
3798 break;
3799 }
3800
3801 case Token::SUB:
3802 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3803 break;
3804
3805 case Token::BIT_NOT:
3806 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3807 break;
3808
3809 default:
3810 UNREACHABLE();
3811 }
3812}
3813
3814
3815void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3816 const char* comment) {
3817 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3818 Comment cmt(masm_, comment);
3819 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3820 UnaryOverwriteMode overwrite =
3821 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003822 UnaryOpStub stub(expr->op(), overwrite);
3823 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003824 VisitForAccumulatorValue(expr->expression());
3825 SetSourcePosition(expr->position());
3826 __ mov(a0, result_register());
3827 EmitCallIC(stub.GetCode(), NULL, expr->id());
3828 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003829}
3830
3831
3832void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003833 Comment cmnt(masm_, "[ CountOperation");
3834 SetSourcePosition(expr->position());
3835
3836 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3837 // as the left-hand side.
3838 if (!expr->expression()->IsValidLeftHandSide()) {
3839 VisitForEffect(expr->expression());
3840 return;
3841 }
3842
3843 // Expression can only be a property, a global or a (parameter or local)
3844 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
3845 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3846 LhsKind assign_type = VARIABLE;
3847 Property* prop = expr->expression()->AsProperty();
3848 // In case of a property we use the uninitialized expression context
3849 // of the key to detect a named property.
3850 if (prop != NULL) {
3851 assign_type =
3852 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3853 }
3854
3855 // Evaluate expression and get value.
3856 if (assign_type == VARIABLE) {
3857 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3858 AccumulatorValueContext context(this);
3859 EmitVariableLoad(expr->expression()->AsVariableProxy()->var());
3860 } else {
3861 // Reserve space for result of postfix operation.
3862 if (expr->is_postfix() && !context()->IsEffect()) {
3863 __ li(at, Operand(Smi::FromInt(0)));
3864 __ push(at);
3865 }
3866 if (assign_type == NAMED_PROPERTY) {
3867 // Put the object both on the stack and in the accumulator.
3868 VisitForAccumulatorValue(prop->obj());
3869 __ push(v0);
3870 EmitNamedPropertyLoad(prop);
3871 } else {
3872 if (prop->is_arguments_access()) {
3873 VariableProxy* obj_proxy = prop->obj()->AsVariableProxy();
3874 __ lw(v0, EmitSlotSearch(obj_proxy->var()->AsSlot(), v0));
3875 __ push(v0);
3876 __ li(v0, Operand(prop->key()->AsLiteral()->handle()));
3877 } else {
3878 VisitForStackValue(prop->obj());
3879 VisitForAccumulatorValue(prop->key());
3880 }
3881 __ lw(a1, MemOperand(sp, 0));
3882 __ push(v0);
3883 EmitKeyedPropertyLoad(prop);
3884 }
3885 }
3886
3887 // We need a second deoptimization point after loading the value
3888 // in case evaluating the property load my have a side effect.
3889 if (assign_type == VARIABLE) {
3890 PrepareForBailout(expr->expression(), TOS_REG);
3891 } else {
3892 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3893 }
3894
3895 // Call ToNumber only if operand is not a smi.
3896 Label no_conversion;
3897 __ JumpIfSmi(v0, &no_conversion);
3898 __ mov(a0, v0);
3899 ToNumberStub convert_stub;
3900 __ CallStub(&convert_stub);
3901 __ bind(&no_conversion);
3902
3903 // Save result for postfix expressions.
3904 if (expr->is_postfix()) {
3905 if (!context()->IsEffect()) {
3906 // Save the result on the stack. If we have a named or keyed property
3907 // we store the result under the receiver that is currently on top
3908 // of the stack.
3909 switch (assign_type) {
3910 case VARIABLE:
3911 __ push(v0);
3912 break;
3913 case NAMED_PROPERTY:
3914 __ sw(v0, MemOperand(sp, kPointerSize));
3915 break;
3916 case KEYED_PROPERTY:
3917 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3918 break;
3919 }
3920 }
3921 }
3922 __ mov(a0, result_register());
3923
3924 // Inline smi case if we are in a loop.
3925 Label stub_call, done;
3926 JumpPatchSite patch_site(masm_);
3927
3928 int count_value = expr->op() == Token::INC ? 1 : -1;
3929 __ li(a1, Operand(Smi::FromInt(count_value)));
3930
3931 if (ShouldInlineSmiCase(expr->op())) {
3932 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
3933 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
3934
3935 // We could eliminate this smi check if we split the code at
3936 // the first smi check before calling ToNumber.
3937 patch_site.EmitJumpIfSmi(v0, &done);
3938 __ bind(&stub_call);
3939 }
3940
3941 // Record position before stub call.
3942 SetSourcePosition(expr->position());
3943
danno@chromium.org40cb8782011-05-25 07:58:50 +00003944 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003945 EmitCallIC(stub.GetCode(), &patch_site, expr->CountId());
3946 __ bind(&done);
3947
3948 // Store the value returned in v0.
3949 switch (assign_type) {
3950 case VARIABLE:
3951 if (expr->is_postfix()) {
3952 { EffectContext context(this);
3953 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3954 Token::ASSIGN);
3955 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3956 context.Plug(v0);
3957 }
3958 // For all contexts except EffectConstant we have the result on
3959 // top of the stack.
3960 if (!context()->IsEffect()) {
3961 context()->PlugTOS();
3962 }
3963 } else {
3964 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3965 Token::ASSIGN);
3966 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3967 context()->Plug(v0);
3968 }
3969 break;
3970 case NAMED_PROPERTY: {
3971 __ mov(a0, result_register()); // Value.
3972 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
3973 __ pop(a1); // Receiver.
3974 Handle<Code> ic = is_strict_mode()
3975 ? isolate()->builtins()->StoreIC_Initialize_Strict()
3976 : isolate()->builtins()->StoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00003977 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003978 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3979 if (expr->is_postfix()) {
3980 if (!context()->IsEffect()) {
3981 context()->PlugTOS();
3982 }
3983 } else {
3984 context()->Plug(v0);
3985 }
3986 break;
3987 }
3988 case KEYED_PROPERTY: {
3989 __ mov(a0, result_register()); // Value.
3990 __ pop(a1); // Key.
3991 __ pop(a2); // Receiver.
3992 Handle<Code> ic = is_strict_mode()
3993 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
3994 : isolate()->builtins()->KeyedStoreIC_Initialize();
danno@chromium.org40cb8782011-05-25 07:58:50 +00003995 EmitCallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003996 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3997 if (expr->is_postfix()) {
3998 if (!context()->IsEffect()) {
3999 context()->PlugTOS();
4000 }
4001 } else {
4002 context()->Plug(v0);
4003 }
4004 break;
4005 }
4006 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004007}
4008
4009
lrn@chromium.org7516f052011-03-30 08:52:27 +00004010void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004011 VariableProxy* proxy = expr->AsVariableProxy();
4012 if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
4013 Comment cmnt(masm_, "Global variable");
4014 __ lw(a0, GlobalObjectOperand());
4015 __ li(a2, Operand(proxy->name()));
4016 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4017 // Use a regular load, not a contextual load, to avoid a reference
4018 // error.
4019 EmitCallIC(ic, RelocInfo::CODE_TARGET, AstNode::kNoNumber);
4020 PrepareForBailout(expr, TOS_REG);
4021 context()->Plug(v0);
4022 } else if (proxy != NULL &&
4023 proxy->var()->AsSlot() != NULL &&
4024 proxy->var()->AsSlot()->type() == Slot::LOOKUP) {
4025 Label done, slow;
4026
4027 // Generate code for loading from variables potentially shadowed
4028 // by eval-introduced variables.
4029 Slot* slot = proxy->var()->AsSlot();
4030 EmitDynamicLoadFromSlotFastCase(slot, INSIDE_TYPEOF, &slow, &done);
4031
4032 __ bind(&slow);
4033 __ li(a0, Operand(proxy->name()));
4034 __ Push(cp, a0);
4035 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4036 PrepareForBailout(expr, TOS_REG);
4037 __ bind(&done);
4038
4039 context()->Plug(v0);
4040 } else {
4041 // This expression cannot throw a reference error at the top level.
4042 context()->HandleExpression(expr);
4043 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004044}
4045
4046
lrn@chromium.org7516f052011-03-30 08:52:27 +00004047bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
4048 Expression* left,
4049 Expression* right,
4050 Label* if_true,
4051 Label* if_false,
4052 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004053 if (op != Token::EQ && op != Token::EQ_STRICT) return false;
4054
4055 // Check for the pattern: typeof <expression> == <string literal>.
4056 Literal* right_literal = right->AsLiteral();
4057 if (right_literal == NULL) return false;
4058 Handle<Object> right_literal_value = right_literal->handle();
4059 if (!right_literal_value->IsString()) return false;
4060 UnaryOperation* left_unary = left->AsUnaryOperation();
4061 if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
4062 Handle<String> check = Handle<String>::cast(right_literal_value);
4063
4064 { AccumulatorValueContext context(this);
4065 VisitForTypeofValue(left_unary->expression());
4066 }
4067 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4068
4069 if (check->Equals(isolate()->heap()->number_symbol())) {
4070 __ JumpIfSmi(v0, if_true);
4071 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4072 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4073 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4074 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4075 __ JumpIfSmi(v0, if_false);
4076 // Check for undetectable objects => false.
4077 __ GetObjectType(v0, v0, a1);
4078 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4079 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4080 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4081 Split(eq, a1, Operand(zero_reg),
4082 if_true, if_false, fall_through);
4083 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4084 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4085 __ Branch(if_true, eq, v0, Operand(at));
4086 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4087 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4088 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4089 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4090 __ Branch(if_true, eq, v0, Operand(at));
4091 __ JumpIfSmi(v0, if_false);
4092 // Check for undetectable objects => true.
4093 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4094 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4095 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4096 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4097 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4098 __ JumpIfSmi(v0, if_false);
4099 __ GetObjectType(v0, a1, v0); // Leave map in a1.
4100 Split(ge, v0, Operand(FIRST_FUNCTION_CLASS_TYPE),
4101 if_true, if_false, fall_through);
4102
4103 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4104 __ JumpIfSmi(v0, if_false);
4105 __ LoadRoot(at, Heap::kNullValueRootIndex);
4106 __ Branch(if_true, eq, v0, Operand(at));
4107 // Check for JS objects => true.
4108 __ GetObjectType(v0, v0, a1);
4109 __ Branch(if_false, lo, a1, Operand(FIRST_JS_OBJECT_TYPE));
4110 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
4111 __ Branch(if_false, hs, a1, Operand(FIRST_FUNCTION_CLASS_TYPE));
4112 // Check for undetectable objects => false.
4113 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4114 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4115 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4116 } else {
4117 if (if_false != fall_through) __ jmp(if_false);
4118 }
4119
4120 return true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00004121}
4122
4123
ager@chromium.org5c838252010-02-19 08:53:10 +00004124void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004125 Comment cmnt(masm_, "[ CompareOperation");
4126 SetSourcePosition(expr->position());
4127
4128 // Always perform the comparison for its control flow. Pack the result
4129 // into the expression's context after the comparison is performed.
4130
4131 Label materialize_true, materialize_false;
4132 Label* if_true = NULL;
4133 Label* if_false = NULL;
4134 Label* fall_through = NULL;
4135 context()->PrepareTest(&materialize_true, &materialize_false,
4136 &if_true, &if_false, &fall_through);
4137
4138 // First we try a fast inlined version of the compare when one of
4139 // the operands is a literal.
4140 Token::Value op = expr->op();
4141 Expression* left = expr->left();
4142 Expression* right = expr->right();
4143 if (TryLiteralCompare(op, left, right, if_true, if_false, fall_through)) {
4144 context()->Plug(if_true, if_false);
4145 return;
4146 }
4147
4148 VisitForStackValue(expr->left());
4149 switch (op) {
4150 case Token::IN:
4151 VisitForStackValue(expr->right());
4152 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
4153 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
4154 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4155 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4156 break;
4157
4158 case Token::INSTANCEOF: {
4159 VisitForStackValue(expr->right());
4160 InstanceofStub stub(InstanceofStub::kNoFlags);
4161 __ CallStub(&stub);
4162 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4163 // The stub returns 0 for true.
4164 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4165 break;
4166 }
4167
4168 default: {
4169 VisitForAccumulatorValue(expr->right());
4170 Condition cc = eq;
4171 bool strict = false;
4172 switch (op) {
4173 case Token::EQ_STRICT:
4174 strict = true;
4175 // Fall through.
4176 case Token::EQ:
4177 cc = eq;
4178 __ mov(a0, result_register());
4179 __ pop(a1);
4180 break;
4181 case Token::LT:
4182 cc = lt;
4183 __ mov(a0, result_register());
4184 __ pop(a1);
4185 break;
4186 case Token::GT:
4187 // Reverse left and right sides to obtain ECMA-262 conversion order.
4188 cc = lt;
4189 __ mov(a1, result_register());
4190 __ pop(a0);
4191 break;
4192 case Token::LTE:
4193 // Reverse left and right sides to obtain ECMA-262 conversion order.
4194 cc = ge;
4195 __ mov(a1, result_register());
4196 __ pop(a0);
4197 break;
4198 case Token::GTE:
4199 cc = ge;
4200 __ mov(a0, result_register());
4201 __ pop(a1);
4202 break;
4203 case Token::IN:
4204 case Token::INSTANCEOF:
4205 default:
4206 UNREACHABLE();
4207 }
4208
4209 bool inline_smi_code = ShouldInlineSmiCase(op);
4210 JumpPatchSite patch_site(masm_);
4211 if (inline_smi_code) {
4212 Label slow_case;
4213 __ Or(a2, a0, Operand(a1));
4214 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4215 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4216 __ bind(&slow_case);
4217 }
4218 // Record position and call the compare IC.
4219 SetSourcePosition(expr->position());
4220 Handle<Code> ic = CompareIC::GetUninitialized(op);
4221 EmitCallIC(ic, &patch_site, expr->id());
4222 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4223 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4224 }
4225 }
4226
4227 // Convert the result of the comparison into one expected for this
4228 // expression's context.
4229 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004230}
4231
4232
lrn@chromium.org7516f052011-03-30 08:52:27 +00004233void FullCodeGenerator::VisitCompareToNull(CompareToNull* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004234 Comment cmnt(masm_, "[ CompareToNull");
4235 Label materialize_true, materialize_false;
4236 Label* if_true = NULL;
4237 Label* if_false = NULL;
4238 Label* fall_through = NULL;
4239 context()->PrepareTest(&materialize_true, &materialize_false,
4240 &if_true, &if_false, &fall_through);
4241
4242 VisitForAccumulatorValue(expr->expression());
4243 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4244 __ mov(a0, result_register());
4245 __ LoadRoot(a1, Heap::kNullValueRootIndex);
4246 if (expr->is_strict()) {
4247 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4248 } else {
4249 __ Branch(if_true, eq, a0, Operand(a1));
4250 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
4251 __ Branch(if_true, eq, a0, Operand(a1));
4252 __ And(at, a0, Operand(kSmiTagMask));
4253 __ Branch(if_false, eq, at, Operand(zero_reg));
4254 // It can be an undetectable object.
4255 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4256 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4257 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4258 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4259 }
4260 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004261}
4262
4263
ager@chromium.org5c838252010-02-19 08:53:10 +00004264void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004265 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4266 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004267}
4268
4269
lrn@chromium.org7516f052011-03-30 08:52:27 +00004270Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004271 return v0;
4272}
ager@chromium.org5c838252010-02-19 08:53:10 +00004273
4274
lrn@chromium.org7516f052011-03-30 08:52:27 +00004275Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004276 return cp;
4277}
4278
4279
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004280void FullCodeGenerator::EmitCallIC(Handle<Code> ic,
4281 RelocInfo::Mode mode,
4282 unsigned ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004283 ASSERT(mode == RelocInfo::CODE_TARGET ||
danno@chromium.org40cb8782011-05-25 07:58:50 +00004284 mode == RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004285 Counters* counters = isolate()->counters();
4286 switch (ic->kind()) {
4287 case Code::LOAD_IC:
4288 __ IncrementCounter(counters->named_load_full(), 1, a1, a2);
4289 break;
4290 case Code::KEYED_LOAD_IC:
4291 __ IncrementCounter(counters->keyed_load_full(), 1, a1, a2);
4292 break;
4293 case Code::STORE_IC:
4294 __ IncrementCounter(counters->named_store_full(), 1, a1, a2);
4295 break;
4296 case Code::KEYED_STORE_IC:
4297 __ IncrementCounter(counters->keyed_store_full(), 1, a1, a2);
4298 default:
4299 break;
4300 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00004301 if (ast_id == kNoASTId || mode == RelocInfo::CODE_TARGET_CONTEXT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004302 __ Call(ic, mode);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004303 } else {
4304 ASSERT(mode == RelocInfo::CODE_TARGET);
4305 mode = RelocInfo::CODE_TARGET_WITH_ID;
4306 __ CallWithAstId(ic, mode, ast_id);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004307 }
4308}
4309
4310
4311void FullCodeGenerator::EmitCallIC(Handle<Code> ic,
4312 JumpPatchSite* patch_site,
4313 unsigned ast_id) {
4314 Counters* counters = isolate()->counters();
4315 switch (ic->kind()) {
4316 case Code::LOAD_IC:
4317 __ IncrementCounter(counters->named_load_full(), 1, a1, a2);
4318 break;
4319 case Code::KEYED_LOAD_IC:
4320 __ IncrementCounter(counters->keyed_load_full(), 1, a1, a2);
4321 break;
4322 case Code::STORE_IC:
4323 __ IncrementCounter(counters->named_store_full(), 1, a1, a2);
4324 break;
4325 case Code::KEYED_STORE_IC:
4326 __ IncrementCounter(counters->keyed_store_full(), 1, a1, a2);
4327 default:
4328 break;
4329 }
4330
danno@chromium.org40cb8782011-05-25 07:58:50 +00004331 if (ast_id == kNoASTId) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004332 __ Call(ic, RelocInfo::CODE_TARGET);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004333 } else {
4334 __ CallWithAstId(ic, RelocInfo::CODE_TARGET_WITH_ID, ast_id);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004335 }
4336 if (patch_site != NULL && patch_site->is_bound()) {
4337 patch_site->EmitPatchInfo();
4338 } else {
4339 __ nop(); // Signals no inlined code.
4340 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00004341}
ager@chromium.org5c838252010-02-19 08:53:10 +00004342
4343
4344void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004345 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4346 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004347}
4348
4349
4350void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004351 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004352}
4353
4354
4355// ----------------------------------------------------------------------------
4356// Non-local control flow support.
4357
4358void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004359 ASSERT(!result_register().is(a1));
4360 // Store result register while executing finally block.
4361 __ push(result_register());
4362 // Cook return address in link register to stack (smi encoded Code* delta).
4363 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4364 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4365 ASSERT_EQ(0, kSmiTag);
4366 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4367 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004368}
4369
4370
4371void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004372 ASSERT(!result_register().is(a1));
4373 // Restore result register from stack.
4374 __ pop(a1);
4375 // Uncook return address and return.
4376 __ pop(result_register());
4377 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4378 __ sra(a1, a1, 1); // Un-smi-tag value.
4379 __ Addu(at, a1, Operand(masm_->CodeObject()));
4380 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004381}
4382
4383
4384#undef __
4385
4386} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004387
4388#endif // V8_TARGET_ARCH_MIPS