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