blob: 57da2e7d111691a2a150626c020ca1c469bb8379 [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) {
danno@chromium.org40cb8782011-05-25 07:58:50 +000058 return property->id();
59}
60
61
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000062// A patch site is a location in the code which it is possible to patch. This
63// class has a number of methods to emit the code which is patchable and the
64// method EmitPatchInfo to record a marker back to the patchable code. This
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000065// marker is a andi zero_reg, rx, #yyyy instruction, and rx * 0x0000ffff + yyyy
66// (raw 16 bit immediate value is used) is the delta from the pc to the first
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000067// instruction of the patchable code.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000068// The marker instruction is effectively a NOP (dest is zero_reg) and will
69// never be emitted by normal code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000070class JumpPatchSite BASE_EMBEDDED {
71 public:
72 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
73#ifdef DEBUG
74 info_emitted_ = false;
75#endif
76 }
77
78 ~JumpPatchSite() {
79 ASSERT(patch_site_.is_bound() == info_emitted_);
80 }
81
82 // When initially emitting this ensure that a jump is always generated to skip
83 // the inlined smi code.
84 void EmitJumpIfNotSmi(Register reg, Label* target) {
85 ASSERT(!patch_site_.is_bound() && !info_emitted_);
86 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
87 __ bind(&patch_site_);
88 __ andi(at, reg, 0);
89 // Always taken before patched.
90 __ Branch(target, eq, at, Operand(zero_reg));
91 }
92
93 // When initially emitting this ensure that a jump is never generated to skip
94 // the inlined smi code.
95 void EmitJumpIfSmi(Register reg, Label* target) {
96 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
97 ASSERT(!patch_site_.is_bound() && !info_emitted_);
98 __ bind(&patch_site_);
99 __ andi(at, reg, 0);
100 // Never taken before patched.
101 __ Branch(target, ne, at, Operand(zero_reg));
102 }
103
104 void EmitPatchInfo() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000105 if (patch_site_.is_bound()) {
106 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
107 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000108 __ andi(zero_reg, reg, delta_to_patch_site % kImm16Mask);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000109#ifdef DEBUG
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000110 info_emitted_ = true;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000111#endif
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000112 } else {
113 __ nop(); // Signals no inlined code.
114 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000115 }
116
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000117 private:
118 MacroAssembler* masm_;
119 Label patch_site_;
120#ifdef DEBUG
121 bool info_emitted_;
122#endif
123};
124
125
lrn@chromium.org7516f052011-03-30 08:52:27 +0000126// Generate code for a JS function. On entry to the function the receiver
127// and arguments have been pushed on the stack left to right. The actual
128// argument count matches the formal parameter count expected by the
129// function.
130//
131// The live registers are:
132// o a1: the JS function object being called (ie, ourselves)
133// o cp: our context
134// o fp: our caller's frame pointer
135// o sp: stack pointer
136// o ra: return address
137//
138// The function builds a JS frame. Please see JavaScriptFrameConstants in
139// frames-mips.h for its layout.
140void FullCodeGenerator::Generate(CompilationInfo* info) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000141 ASSERT(info_ == NULL);
142 info_ = info;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000143 scope_ = info->scope();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000144 SetFunctionPosition(function());
145 Comment cmnt(masm_, "[ function compiled by full code generator");
146
147#ifdef DEBUG
148 if (strlen(FLAG_stop_at) > 0 &&
149 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
150 __ stop("stop-at");
151 }
152#endif
153
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000154 // Strict mode functions and builtins need to replace the receiver
155 // with undefined when called as functions (without an explicit
156 // receiver object). t1 is zero for method calls and non-zero for
157 // function calls.
158 if (info->is_strict_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000159 Label ok;
160 __ Branch(&ok, eq, t1, Operand(zero_reg));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000161 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000162 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
163 __ sw(a2, MemOperand(sp, receiver_offset));
164 __ bind(&ok);
165 }
166
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000167 // Open a frame scope to indicate that there is a frame on the stack. The
168 // MANUAL indicates that the scope shouldn't actually generate code to set up
169 // the frame (that is done below).
170 FrameScope frame_scope(masm_, StackFrame::MANUAL);
171
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000172 int locals_count = info->scope()->num_stack_slots();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000173
174 __ Push(ra, fp, cp, a1);
175 if (locals_count > 0) {
176 // Load undefined value here, so the value is ready for the loop
177 // below.
178 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
179 }
180 // Adjust fp to point to caller's fp.
181 __ Addu(fp, sp, Operand(2 * kPointerSize));
182
183 { Comment cmnt(masm_, "[ Allocate locals");
184 for (int i = 0; i < locals_count; i++) {
185 __ push(at);
186 }
187 }
188
189 bool function_in_register = true;
190
191 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000192 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000193 if (heap_slots > 0) {
194 Comment cmnt(masm_, "[ Allocate local context");
195 // Argument to NewContext is the function, which is in a1.
196 __ push(a1);
197 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
198 FastNewContextStub stub(heap_slots);
199 __ CallStub(&stub);
200 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000201 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000202 }
203 function_in_register = false;
204 // Context is returned in both v0 and cp. It replaces the context
205 // passed to us. It's saved in the stack and kept live in cp.
206 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
207 // Copy any necessary parameters into the context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000208 int num_parameters = info->scope()->num_parameters();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000209 for (int i = 0; i < num_parameters; i++) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000210 Variable* var = scope()->parameter(i);
211 if (var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000212 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
213 (num_parameters - 1 - i) * kPointerSize;
214 // Load parameter from stack.
215 __ lw(a0, MemOperand(fp, parameter_offset));
216 // Store it in the context.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000217 __ li(a1, Operand(Context::SlotOffset(var->index())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000218 __ addu(a2, cp, a1);
219 __ sw(a0, MemOperand(a2, 0));
220 // Update the write barrier. This clobbers all involved
221 // registers, so we have to use two more registers to avoid
222 // clobbering cp.
223 __ mov(a2, cp);
224 __ RecordWrite(a2, a1, a3);
225 }
226 }
227 }
228
229 Variable* arguments = scope()->arguments();
230 if (arguments != NULL) {
231 // Function uses arguments object.
232 Comment cmnt(masm_, "[ Allocate arguments object");
233 if (!function_in_register) {
234 // Load this again, if it's used by the local context below.
235 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
236 } else {
237 __ mov(a3, a1);
238 }
239 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000240 int num_parameters = info->scope()->num_parameters();
241 int offset = num_parameters * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000242 __ Addu(a2, fp,
243 Operand(StandardFrameConstants::kCallerSPOffset + offset));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000244 __ li(a1, Operand(Smi::FromInt(num_parameters)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000245 __ Push(a3, a2, a1);
246
247 // Arguments to ArgumentsAccessStub:
248 // function, receiver address, parameter count.
249 // The stub will rewrite receiever and parameter count if the previous
250 // stack frame was an arguments adapter frame.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000251 ArgumentsAccessStub::Type type;
252 if (is_strict_mode()) {
253 type = ArgumentsAccessStub::NEW_STRICT;
254 } else if (function()->has_duplicate_parameters()) {
255 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
256 } else {
257 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
258 }
259 ArgumentsAccessStub stub(type);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000260 __ CallStub(&stub);
261
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000262 SetVar(arguments, v0, a1, a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000263 }
264
265 if (FLAG_trace) {
266 __ CallRuntime(Runtime::kTraceEnter, 0);
267 }
268
269 // Visit the declarations and body unless there is an illegal
270 // redeclaration.
271 if (scope()->HasIllegalRedeclaration()) {
272 Comment cmnt(masm_, "[ Declarations");
273 scope()->VisitIllegalRedeclaration(this);
274
275 } else {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000276 PrepareForBailoutForId(AstNode::kFunctionEntryId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000277 { Comment cmnt(masm_, "[ Declarations");
278 // For named function expressions, declare the function name as a
279 // constant.
280 if (scope()->is_function_scope() && scope()->function() != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000281 int ignored = 0;
282 EmitDeclaration(scope()->function(), Variable::CONST, NULL, &ignored);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000283 }
284 VisitDeclarations(scope()->declarations());
285 }
286
287 { Comment cmnt(masm_, "[ Stack check");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000288 PrepareForBailoutForId(AstNode::kDeclarationsId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000289 Label ok;
290 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
291 __ Branch(&ok, hs, sp, Operand(t0));
292 StackCheckStub stub;
293 __ CallStub(&stub);
294 __ bind(&ok);
295 }
296
297 { Comment cmnt(masm_, "[ Body");
298 ASSERT(loop_depth() == 0);
299 VisitStatements(function()->body());
300 ASSERT(loop_depth() == 0);
301 }
302 }
303
304 // Always emit a 'return undefined' in case control fell off the end of
305 // the body.
306 { Comment cmnt(masm_, "[ return <undefined>;");
307 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
308 }
309 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000310}
311
312
313void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000314 ASSERT(Smi::FromInt(0) == 0);
315 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000316}
317
318
319void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000320 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
321 // to make sure it is constant. Branch may emit a skip-or-jump sequence
322 // instead of the normal Branch. It seems that the "skip" part of that
323 // sequence is about as long as this Branch would be so it is safe to ignore
324 // that.
325 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000326 Comment cmnt(masm_, "[ Stack check");
327 Label ok;
328 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000329 __ sltu(at, sp, t0);
330 __ beq(at, zero_reg, &ok);
331 // CallStub will emit a li t9, ... first, so it is safe to use the delay slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000332 StackCheckStub stub;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000333 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000334 // Record a mapping of this PC offset to the OSR id. This is used to find
335 // the AST id from the unoptimized code in order to use it as a key into
336 // the deoptimization input data found in the optimized code.
337 RecordStackCheck(stmt->OsrEntryId());
338
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000339 __ bind(&ok);
340 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
341 // Record a mapping of the OSR id to this PC. This is used if the OSR
342 // entry becomes the target of a bailout. We don't expect it to be, but
343 // we want it to work if it is.
344 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000345}
346
347
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000348void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000349 Comment cmnt(masm_, "[ Return sequence");
350 if (return_label_.is_bound()) {
351 __ Branch(&return_label_);
352 } else {
353 __ bind(&return_label_);
354 if (FLAG_trace) {
355 // Push the return value on the stack as the parameter.
356 // Runtime::TraceExit returns its parameter in v0.
357 __ push(v0);
358 __ CallRuntime(Runtime::kTraceExit, 1);
359 }
360
361#ifdef DEBUG
362 // Add a label for checking the size of the code used for returning.
363 Label check_exit_codesize;
364 masm_->bind(&check_exit_codesize);
365#endif
366 // Make sure that the constant pool is not emitted inside of the return
367 // sequence.
368 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
369 // Here we use masm_-> instead of the __ macro to avoid the code coverage
370 // tool from instrumenting as we rely on the code size here.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000371 int32_t sp_delta = (info_->scope()->num_parameters() + 1) * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000372 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
373 __ RecordJSReturn();
374 masm_->mov(sp, fp);
375 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
376 masm_->Addu(sp, sp, Operand(sp_delta));
377 masm_->Jump(ra);
378 }
379
380#ifdef DEBUG
381 // Check that the size of the code used for returning is large enough
382 // for the debugger's requirements.
383 ASSERT(Assembler::kJSReturnSequenceInstructions <=
384 masm_->InstructionsGeneratedSince(&check_exit_codesize));
385#endif
386 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000387}
388
389
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000390void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
391 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
ager@chromium.org5c838252010-02-19 08:53:10 +0000392}
393
394
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000395void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
396 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
397 codegen()->GetVar(result_register(), var);
ager@chromium.org5c838252010-02-19 08:53:10 +0000398}
399
400
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000401void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
402 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
403 codegen()->GetVar(result_register(), var);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000404 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000405}
406
407
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000408void FullCodeGenerator::TestContext::Plug(Variable* var) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000409 // For simplicity we always test the accumulator register.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000410 codegen()->GetVar(result_register(), var);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000411 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000412 codegen()->DoTest(this);
ager@chromium.org5c838252010-02-19 08:53:10 +0000413}
414
415
lrn@chromium.org7516f052011-03-30 08:52:27 +0000416void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000417}
418
419
lrn@chromium.org7516f052011-03-30 08:52:27 +0000420void FullCodeGenerator::AccumulatorValueContext::Plug(
421 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000422 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000423}
424
425
426void FullCodeGenerator::StackValueContext::Plug(
427 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000428 __ LoadRoot(result_register(), index);
429 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000430}
431
432
433void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000434 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
435 true,
436 true_label_,
437 false_label_);
438 if (index == Heap::kUndefinedValueRootIndex ||
439 index == Heap::kNullValueRootIndex ||
440 index == Heap::kFalseValueRootIndex) {
441 if (false_label_ != fall_through_) __ Branch(false_label_);
442 } else if (index == Heap::kTrueValueRootIndex) {
443 if (true_label_ != fall_through_) __ Branch(true_label_);
444 } else {
445 __ LoadRoot(result_register(), index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000446 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000447 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000448}
449
450
451void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000452}
453
454
455void FullCodeGenerator::AccumulatorValueContext::Plug(
456 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000457 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000458}
459
460
461void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000462 // Immediates cannot be pushed directly.
463 __ li(result_register(), Operand(lit));
464 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000465}
466
467
468void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000469 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
470 true,
471 true_label_,
472 false_label_);
473 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
474 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
475 if (false_label_ != fall_through_) __ Branch(false_label_);
476 } else if (lit->IsTrue() || lit->IsJSObject()) {
477 if (true_label_ != fall_through_) __ Branch(true_label_);
478 } else if (lit->IsString()) {
479 if (String::cast(*lit)->length() == 0) {
480 if (false_label_ != fall_through_) __ Branch(false_label_);
481 } else {
482 if (true_label_ != fall_through_) __ Branch(true_label_);
483 }
484 } else if (lit->IsSmi()) {
485 if (Smi::cast(*lit)->value() == 0) {
486 if (false_label_ != fall_through_) __ Branch(false_label_);
487 } else {
488 if (true_label_ != fall_through_) __ Branch(true_label_);
489 }
490 } else {
491 // For simplicity we always test the accumulator register.
492 __ li(result_register(), Operand(lit));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000493 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000494 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000495}
496
497
498void FullCodeGenerator::EffectContext::DropAndPlug(int count,
499 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000500 ASSERT(count > 0);
501 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000502}
503
504
505void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
506 int count,
507 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000508 ASSERT(count > 0);
509 __ Drop(count);
510 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000511}
512
513
514void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
515 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000516 ASSERT(count > 0);
517 if (count > 1) __ Drop(count - 1);
518 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000519}
520
521
522void FullCodeGenerator::TestContext::DropAndPlug(int count,
523 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000524 ASSERT(count > 0);
525 // For simplicity we always test the accumulator register.
526 __ Drop(count);
527 __ Move(result_register(), reg);
528 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000529 codegen()->DoTest(this);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000530}
531
532
533void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
534 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000535 ASSERT(materialize_true == materialize_false);
536 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000537}
538
539
540void FullCodeGenerator::AccumulatorValueContext::Plug(
541 Label* materialize_true,
542 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000543 Label done;
544 __ bind(materialize_true);
545 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
546 __ Branch(&done);
547 __ bind(materialize_false);
548 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
549 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000550}
551
552
553void FullCodeGenerator::StackValueContext::Plug(
554 Label* materialize_true,
555 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000556 Label done;
557 __ bind(materialize_true);
558 __ LoadRoot(at, Heap::kTrueValueRootIndex);
559 __ push(at);
560 __ Branch(&done);
561 __ bind(materialize_false);
562 __ LoadRoot(at, Heap::kFalseValueRootIndex);
563 __ push(at);
564 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000565}
566
567
568void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
569 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000570 ASSERT(materialize_true == true_label_);
571 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000572}
573
574
575void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000576}
577
578
579void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000580 Heap::RootListIndex value_root_index =
581 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
582 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000583}
584
585
586void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000587 Heap::RootListIndex value_root_index =
588 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
589 __ LoadRoot(at, value_root_index);
590 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000591}
592
593
594void FullCodeGenerator::TestContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000595 codegen()->PrepareForBailoutBeforeSplit(TOS_REG,
596 true,
597 true_label_,
598 false_label_);
599 if (flag) {
600 if (true_label_ != fall_through_) __ Branch(true_label_);
601 } else {
602 if (false_label_ != fall_through_) __ Branch(false_label_);
603 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000604}
605
606
whesse@chromium.org7b260152011-06-20 15:33:18 +0000607void FullCodeGenerator::DoTest(Expression* condition,
608 Label* if_true,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000609 Label* if_false,
610 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000611 if (CpuFeatures::IsSupported(FPU)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000612 ToBooleanStub stub(result_register());
613 __ CallStub(&stub);
614 __ mov(at, zero_reg);
615 } else {
616 // Call the runtime to find the boolean value of the source and then
617 // translate it into control flow to the pair of labels.
618 __ push(result_register());
619 __ CallRuntime(Runtime::kToBool, 1);
620 __ LoadRoot(at, Heap::kFalseValueRootIndex);
621 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000622 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000623}
624
625
lrn@chromium.org7516f052011-03-30 08:52:27 +0000626void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000627 Register lhs,
628 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000629 Label* if_true,
630 Label* if_false,
631 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000632 if (if_false == fall_through) {
633 __ Branch(if_true, cc, lhs, rhs);
634 } else if (if_true == fall_through) {
635 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
636 } else {
637 __ Branch(if_true, cc, lhs, rhs);
638 __ Branch(if_false);
639 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000640}
641
642
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000643MemOperand FullCodeGenerator::StackOperand(Variable* var) {
644 ASSERT(var->IsStackAllocated());
645 // Offset is negative because higher indexes are at lower addresses.
646 int offset = -var->index() * kPointerSize;
647 // Adjust by a (parameter or local) base offset.
648 if (var->IsParameter()) {
649 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
650 } else {
651 offset += JavaScriptFrameConstants::kLocal0Offset;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000652 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000653 return MemOperand(fp, offset);
ager@chromium.org5c838252010-02-19 08:53:10 +0000654}
655
656
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000657MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
658 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
659 if (var->IsContextSlot()) {
660 int context_chain_length = scope()->ContextChainLength(var->scope());
661 __ LoadContext(scratch, context_chain_length);
662 return ContextOperand(scratch, var->index());
663 } else {
664 return StackOperand(var);
665 }
666}
667
668
669void FullCodeGenerator::GetVar(Register dest, Variable* var) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000670 // Use destination as scratch.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000671 MemOperand location = VarOperand(var, dest);
672 __ lw(dest, location);
673}
674
675
676void FullCodeGenerator::SetVar(Variable* var,
677 Register src,
678 Register scratch0,
679 Register scratch1) {
680 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
681 ASSERT(!scratch0.is(src));
682 ASSERT(!scratch0.is(scratch1));
683 ASSERT(!scratch1.is(src));
684 MemOperand location = VarOperand(var, scratch0);
685 __ sw(src, location);
686 // Emit the write barrier code if the location is in the heap.
687 if (var->IsContextSlot()) {
688 __ RecordWrite(scratch0,
689 Operand(Context::SlotOffset(var->index())),
690 scratch1,
691 src);
692 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000693}
694
695
lrn@chromium.org7516f052011-03-30 08:52:27 +0000696void FullCodeGenerator::PrepareForBailoutBeforeSplit(State state,
697 bool should_normalize,
698 Label* if_true,
699 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000700 // Only prepare for bailouts before splits if we're in a test
701 // context. Otherwise, we let the Visit function deal with the
702 // preparation to avoid preparing with the same AST id twice.
703 if (!context()->IsTest() || !info_->IsOptimizable()) return;
704
705 Label skip;
706 if (should_normalize) __ Branch(&skip);
707
708 ForwardBailoutStack* current = forward_bailout_stack_;
709 while (current != NULL) {
710 PrepareForBailout(current->expr(), state);
711 current = current->parent();
712 }
713
714 if (should_normalize) {
715 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
716 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
717 __ bind(&skip);
718 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000719}
720
721
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000722void FullCodeGenerator::EmitDeclaration(VariableProxy* proxy,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000723 Variable::Mode mode,
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000724 FunctionLiteral* function,
725 int* global_count) {
726 // If it was not possible to allocate the variable at compile time, we
727 // need to "declare" it at runtime to make sure it actually exists in the
728 // local context.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000729 Variable* variable = proxy->var();
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000730 switch (variable->location()) {
731 case Variable::UNALLOCATED:
732 ++(*global_count);
733 break;
734
735 case Variable::PARAMETER:
736 case Variable::LOCAL:
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000737 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000738 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000739 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000740 __ sw(result_register(), StackOperand(variable));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000741 } else if (mode == Variable::CONST || mode == Variable::LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000742 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000743 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000744 __ sw(t0, StackOperand(variable));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000745 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000746 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000747
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000748 case Variable::CONTEXT:
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000749 // The variable in the decl always resides in the current function
750 // context.
751 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
752 if (FLAG_debug_code) {
753 // Check that we're not inside a with or catch context.
754 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
755 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
756 __ Check(ne, "Declaration in with context.",
757 a1, Operand(t0));
758 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
759 __ Check(ne, "Declaration in catch context.",
760 a1, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000761 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000762 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000763 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000764 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000765 __ sw(result_register(), ContextOperand(cp, variable->index()));
766 int offset = Context::SlotOffset(variable->index());
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000767 // We know that we have written a function, which is not a smi.
768 __ mov(a1, cp);
769 __ RecordWrite(a1, Operand(offset), a2, result_register());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000770 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000771 } else if (mode == Variable::CONST || mode == Variable::LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000772 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000773 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000774 __ sw(at, ContextOperand(cp, variable->index()));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000775 // No write barrier since the_hole_value is in old space.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000776 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000777 }
778 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000779
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000780 case Variable::LOOKUP: {
781 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000782 __ li(a2, Operand(variable->name()));
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000783 // Declaration nodes are always introduced in one of three modes.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000784 ASSERT(mode == Variable::VAR ||
785 mode == Variable::CONST ||
786 mode == Variable::LET);
787 PropertyAttributes attr = (mode == Variable::CONST) ? READ_ONLY : NONE;
788 __ li(a1, Operand(Smi::FromInt(attr)));
789 // Push initial value, if any.
790 // Note: For variables we must not push an initial value (such as
791 // 'undefined') because we may have a (legal) redeclaration and we
792 // must not destroy the current value.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000793 if (function != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000794 __ Push(cp, a2, a1);
795 // Push initial value for function declaration.
796 VisitForStackValue(function);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000797 } else if (mode == Variable::CONST || mode == Variable::LET) {
798 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
799 __ Push(cp, a2, a1, a0);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000800 } else {
801 ASSERT(Smi::FromInt(0) == 0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000802 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000803 __ Push(cp, a2, a1, a0);
804 }
805 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
806 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000807 }
808 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000809}
810
811
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000812void FullCodeGenerator::VisitDeclaration(Declaration* decl) { }
ager@chromium.org5c838252010-02-19 08:53:10 +0000813
814
815void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000816 // Call the runtime to declare the globals.
817 // The context is the first argument.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000818 __ li(a1, Operand(pairs));
819 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
820 __ Push(cp, a1, a0);
821 __ CallRuntime(Runtime::kDeclareGlobals, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000822 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000823}
824
825
lrn@chromium.org7516f052011-03-30 08:52:27 +0000826void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000827 Comment cmnt(masm_, "[ SwitchStatement");
828 Breakable nested_statement(this, stmt);
829 SetStatementPosition(stmt);
830
831 // Keep the switch value on the stack until a case matches.
832 VisitForStackValue(stmt->tag());
833 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
834
835 ZoneList<CaseClause*>* clauses = stmt->cases();
836 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
837
838 Label next_test; // Recycled for each test.
839 // Compile all the tests with branches to their bodies.
840 for (int i = 0; i < clauses->length(); i++) {
841 CaseClause* clause = clauses->at(i);
842 clause->body_target()->Unuse();
843
844 // The default is not a test, but remember it as final fall through.
845 if (clause->is_default()) {
846 default_clause = clause;
847 continue;
848 }
849
850 Comment cmnt(masm_, "[ Case comparison");
851 __ bind(&next_test);
852 next_test.Unuse();
853
854 // Compile the label expression.
855 VisitForAccumulatorValue(clause->label());
856 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
857
858 // Perform the comparison as if via '==='.
859 __ lw(a1, MemOperand(sp, 0)); // Switch value.
860 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
861 JumpPatchSite patch_site(masm_);
862 if (inline_smi_code) {
863 Label slow_case;
864 __ or_(a2, a1, a0);
865 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
866
867 __ Branch(&next_test, ne, a1, Operand(a0));
868 __ Drop(1); // Switch value is no longer needed.
869 __ Branch(clause->body_target());
870
871 __ bind(&slow_case);
872 }
873
874 // Record position before stub call for type feedback.
875 SetSourcePosition(clause->position());
876 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000877 __ Call(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000878 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +0000879
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000880 __ Branch(&next_test, ne, v0, Operand(zero_reg));
881 __ Drop(1); // Switch value is no longer needed.
882 __ Branch(clause->body_target());
883 }
884
885 // Discard the test value and jump to the default if present, otherwise to
886 // the end of the statement.
887 __ bind(&next_test);
888 __ Drop(1); // Switch value is no longer needed.
889 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000890 __ Branch(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000891 } else {
892 __ Branch(default_clause->body_target());
893 }
894
895 // Compile all the case bodies.
896 for (int i = 0; i < clauses->length(); i++) {
897 Comment cmnt(masm_, "[ Case body");
898 CaseClause* clause = clauses->at(i);
899 __ bind(clause->body_target());
900 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
901 VisitStatements(clause->statements());
902 }
903
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000904 __ bind(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000905 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000906}
907
908
909void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000910 Comment cmnt(masm_, "[ ForInStatement");
911 SetStatementPosition(stmt);
912
913 Label loop, exit;
914 ForIn loop_statement(this, stmt);
915 increment_loop_depth();
916
917 // Get the object to enumerate over. Both SpiderMonkey and JSC
918 // ignore null and undefined in contrast to the specification; see
919 // ECMA-262 section 12.6.4.
920 VisitForAccumulatorValue(stmt->enumerable());
921 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
922 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
923 __ Branch(&exit, eq, a0, Operand(at));
924 Register null_value = t1;
925 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
926 __ Branch(&exit, eq, a0, Operand(null_value));
927
928 // Convert the object to a JS object.
929 Label convert, done_convert;
930 __ JumpIfSmi(a0, &convert);
931 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000932 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000933 __ bind(&convert);
934 __ push(a0);
935 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
936 __ mov(a0, v0);
937 __ bind(&done_convert);
938 __ push(a0);
939
940 // Check cache validity in generated code. This is a fast case for
941 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
942 // guarantee cache validity, call the runtime system to check cache
943 // validity or get the property names in a fixed array.
944 Label next, call_runtime;
945 // Preload a couple of values used in the loop.
946 Register empty_fixed_array_value = t2;
947 __ LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
948 Register empty_descriptor_array_value = t3;
949 __ LoadRoot(empty_descriptor_array_value,
950 Heap::kEmptyDescriptorArrayRootIndex);
951 __ mov(a1, a0);
952 __ bind(&next);
953
954 // Check that there are no elements. Register a1 contains the
955 // current JS object we've reached through the prototype chain.
956 __ lw(a2, FieldMemOperand(a1, JSObject::kElementsOffset));
957 __ Branch(&call_runtime, ne, a2, Operand(empty_fixed_array_value));
958
959 // Check that instance descriptors are not empty so that we can
960 // check for an enum cache. Leave the map in a2 for the subsequent
961 // prototype load.
962 __ lw(a2, FieldMemOperand(a1, HeapObject::kMapOffset));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000963 __ lw(a3, FieldMemOperand(a2, Map::kInstanceDescriptorsOrBitField3Offset));
964 __ JumpIfSmi(a3, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000965
966 // Check that there is an enum cache in the non-empty instance
967 // descriptors (a3). This is the case if the next enumeration
968 // index field does not contain a smi.
969 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumerationIndexOffset));
970 __ JumpIfSmi(a3, &call_runtime);
971
972 // For all objects but the receiver, check that the cache is empty.
973 Label check_prototype;
974 __ Branch(&check_prototype, eq, a1, Operand(a0));
975 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumCacheBridgeCacheOffset));
976 __ Branch(&call_runtime, ne, a3, Operand(empty_fixed_array_value));
977
978 // Load the prototype from the map and loop if non-null.
979 __ bind(&check_prototype);
980 __ lw(a1, FieldMemOperand(a2, Map::kPrototypeOffset));
981 __ Branch(&next, ne, a1, Operand(null_value));
982
983 // The enum cache is valid. Load the map of the object being
984 // iterated over and use the cache for the iteration.
985 Label use_cache;
986 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
987 __ Branch(&use_cache);
988
989 // Get the set of properties to enumerate.
990 __ bind(&call_runtime);
991 __ push(a0); // Duplicate the enumerable object on the stack.
992 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
993
994 // If we got a map from the runtime call, we can do a fast
995 // modification check. Otherwise, we got a fixed array, and we have
996 // to do a slow check.
997 Label fixed_array;
998 __ mov(a2, v0);
999 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
1000 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1001 __ Branch(&fixed_array, ne, a1, Operand(at));
1002
1003 // We got a map in register v0. Get the enumeration cache from it.
1004 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001005 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001006 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
1007 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1008
1009 // Setup the four remaining stack slots.
1010 __ push(v0); // Map.
1011 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
1012 __ li(a0, Operand(Smi::FromInt(0)));
1013 // Push enumeration cache, enumeration cache length (as smi) and zero.
1014 __ Push(a2, a1, a0);
1015 __ jmp(&loop);
1016
1017 // We got a fixed array in register v0. Iterate through that.
1018 __ bind(&fixed_array);
1019 __ li(a1, Operand(Smi::FromInt(0))); // Map (0) - force slow check.
1020 __ Push(a1, v0);
1021 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1022 __ li(a0, Operand(Smi::FromInt(0)));
1023 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1024
1025 // Generate code for doing the condition check.
1026 __ bind(&loop);
1027 // Load the current count to a0, load the length to a1.
1028 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1029 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001030 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001031
1032 // Get the current entry of the array into register a3.
1033 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1034 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1035 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1036 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1037 __ lw(a3, MemOperand(t0)); // Current entry.
1038
1039 // Get the expected map from the stack or a zero map in the
1040 // permanent slow case into register a2.
1041 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1042
1043 // Check if the expected map still matches that of the enumerable.
1044 // If not, we have to filter the key.
1045 Label update_each;
1046 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1047 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1048 __ Branch(&update_each, eq, t0, Operand(a2));
1049
1050 // Convert the entry to a string or (smi) 0 if it isn't a property
1051 // any more. If the property has been removed while iterating, we
1052 // just skip it.
1053 __ push(a1); // Enumerable.
1054 __ push(a3); // Current entry.
1055 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1056 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001057 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001058
1059 // Update the 'each' property or variable from the possibly filtered
1060 // entry in register a3.
1061 __ bind(&update_each);
1062 __ mov(result_register(), a3);
1063 // Perform the assignment as if via '='.
1064 { EffectContext context(this);
1065 EmitAssignment(stmt->each(), stmt->AssignmentId());
1066 }
1067
1068 // Generate code for the body of the loop.
1069 Visit(stmt->body());
1070
1071 // Generate code for the going to the next element by incrementing
1072 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001073 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001074 __ pop(a0);
1075 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1076 __ push(a0);
1077
1078 EmitStackCheck(stmt);
1079 __ Branch(&loop);
1080
1081 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001082 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001083 __ Drop(5);
1084
1085 // Exit and decrement the loop depth.
1086 __ bind(&exit);
1087 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001088}
1089
1090
1091void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1092 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001093 // Use the fast case closure allocation code that allocates in new
1094 // space for nested functions that don't need literals cloning. If
1095 // we're running with the --always-opt or the --prepare-always-opt
1096 // flag, we need to use the runtime function so that the new function
1097 // we are creating here gets a chance to have its code optimized and
1098 // doesn't just get a copy of the existing unoptimized code.
1099 if (!FLAG_always_opt &&
1100 !FLAG_prepare_always_opt &&
1101 !pretenure &&
1102 scope()->is_function_scope() &&
1103 info->num_literals() == 0) {
1104 FastNewClosureStub stub(info->strict_mode() ? kStrictMode : kNonStrictMode);
1105 __ li(a0, Operand(info));
1106 __ push(a0);
1107 __ CallStub(&stub);
1108 } else {
1109 __ li(a0, Operand(info));
1110 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1111 : Heap::kFalseValueRootIndex);
1112 __ Push(cp, a0, a1);
1113 __ CallRuntime(Runtime::kNewClosure, 3);
1114 }
1115 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001116}
1117
1118
1119void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001120 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001121 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001122}
1123
1124
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001125void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1126 TypeofState typeof_state,
1127 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001128 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());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001171 __ li(a2, Operand(var->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001172 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
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001180MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1181 Label* slow) {
1182 ASSERT(var->IsContextSlot());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001183 Register context = cp;
1184 Register next = a3;
1185 Register temp = t0;
1186
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001187 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001188 if (s->num_heap_slots() > 0) {
1189 if (s->calls_eval()) {
1190 // Check that extension is NULL.
1191 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1192 __ Branch(slow, ne, temp, Operand(zero_reg));
1193 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001194 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001195 // Walk the rest of the chain without clobbering cp.
1196 context = next;
1197 }
1198 }
1199 // Check that last extension is NULL.
1200 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1201 __ Branch(slow, ne, temp, Operand(zero_reg));
1202
1203 // This function is used only for loads, not stores, so it's safe to
1204 // return an cp-based operand (the write barrier cannot be allowed to
1205 // destroy the cp register).
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001206 return ContextOperand(context, var->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001207}
1208
1209
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001210void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1211 TypeofState typeof_state,
1212 Label* slow,
1213 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001214 // Generate fast-case code for variables that might be shadowed by
1215 // eval-introduced variables. Eval is used a lot without
1216 // introducing variables. In those cases, we do not want to
1217 // perform a runtime call for all variables in the scope
1218 // containing the eval.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001219 if (var->mode() == Variable::DYNAMIC_GLOBAL) {
1220 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001221 __ Branch(done);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001222 } else if (var->mode() == Variable::DYNAMIC_LOCAL) {
1223 Variable* local = var->local_if_not_shadowed();
1224 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
1225 if (local->mode() == Variable::CONST) {
1226 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1227 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1228 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1229 __ movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001230 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001231 __ Branch(done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001232 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001233}
1234
1235
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001236void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1237 // Record position before possible IC call.
1238 SetSourcePosition(proxy->position());
1239 Variable* var = proxy->var();
1240
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001241 // Three cases: global variables, lookup variables, and all other types of
1242 // variables.
1243 switch (var->location()) {
1244 case Variable::UNALLOCATED: {
1245 Comment cmnt(masm_, "Global variable");
1246 // Use inline caching. Variable name is passed in a2 and the global
1247 // object (receiver) in a0.
1248 __ lw(a0, GlobalObjectOperand());
1249 __ li(a2, Operand(var->name()));
1250 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1251 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
1252 context()->Plug(v0);
1253 break;
1254 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001255
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001256 case Variable::PARAMETER:
1257 case Variable::LOCAL:
1258 case Variable::CONTEXT: {
1259 Comment cmnt(masm_, var->IsContextSlot()
1260 ? "Context variable"
1261 : "Stack variable");
1262 if (var->mode() != Variable::LET && var->mode() != Variable::CONST) {
1263 context()->Plug(var);
1264 } else {
1265 // Let and const need a read barrier.
1266 GetVar(v0, var);
1267 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1268 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1269 if (var->mode() == Variable::LET) {
1270 Label done;
1271 __ Branch(&done, ne, at, Operand(zero_reg));
1272 __ li(a0, Operand(var->name()));
1273 __ push(a0);
1274 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1275 __ bind(&done);
1276 } else {
1277 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1278 __ movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1279 }
1280 context()->Plug(v0);
1281 }
1282 break;
1283 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001284
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001285 case Variable::LOOKUP: {
1286 Label done, slow;
1287 // Generate code for loading from variables potentially shadowed
1288 // by eval-introduced variables.
1289 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1290 __ bind(&slow);
1291 Comment cmnt(masm_, "Lookup variable");
1292 __ li(a1, Operand(var->name()));
1293 __ Push(cp, a1); // Context and name.
1294 __ CallRuntime(Runtime::kLoadContextSlot, 2);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001295 __ bind(&done);
1296 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001297 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001298 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001299}
1300
1301
1302void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001303 Comment cmnt(masm_, "[ RegExpLiteral");
1304 Label materialized;
1305 // Registers will be used as follows:
1306 // t1 = materialized value (RegExp literal)
1307 // t0 = JS function, literals array
1308 // a3 = literal index
1309 // a2 = RegExp pattern
1310 // a1 = RegExp flags
1311 // a0 = RegExp literal clone
1312 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1313 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1314 int literal_offset =
1315 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1316 __ lw(t1, FieldMemOperand(t0, literal_offset));
1317 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1318 __ Branch(&materialized, ne, t1, Operand(at));
1319
1320 // Create regexp literal using runtime function.
1321 // Result will be in v0.
1322 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1323 __ li(a2, Operand(expr->pattern()));
1324 __ li(a1, Operand(expr->flags()));
1325 __ Push(t0, a3, a2, a1);
1326 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1327 __ mov(t1, v0);
1328
1329 __ bind(&materialized);
1330 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1331 Label allocated, runtime_allocate;
1332 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1333 __ jmp(&allocated);
1334
1335 __ bind(&runtime_allocate);
1336 __ push(t1);
1337 __ li(a0, Operand(Smi::FromInt(size)));
1338 __ push(a0);
1339 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1340 __ pop(t1);
1341
1342 __ bind(&allocated);
1343
1344 // After this, registers are used as follows:
1345 // v0: Newly allocated regexp.
1346 // t1: Materialized regexp.
1347 // a2: temp.
1348 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1349 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001350}
1351
1352
1353void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001354 Comment cmnt(masm_, "[ ObjectLiteral");
1355 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1356 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1357 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1358 __ li(a1, Operand(expr->constant_properties()));
1359 int flags = expr->fast_elements()
1360 ? ObjectLiteral::kFastElements
1361 : ObjectLiteral::kNoFlags;
1362 flags |= expr->has_function()
1363 ? ObjectLiteral::kHasFunction
1364 : ObjectLiteral::kNoFlags;
1365 __ li(a0, Operand(Smi::FromInt(flags)));
1366 __ Push(a3, a2, a1, a0);
1367 if (expr->depth() > 1) {
1368 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1369 } else {
1370 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
1371 }
1372
1373 // If result_saved is true the result is on top of the stack. If
1374 // result_saved is false the result is in v0.
1375 bool result_saved = false;
1376
1377 // Mark all computed expressions that are bound to a key that
1378 // is shadowed by a later occurrence of the same key. For the
1379 // marked expressions, no store code is emitted.
1380 expr->CalculateEmitStore();
1381
1382 for (int i = 0; i < expr->properties()->length(); i++) {
1383 ObjectLiteral::Property* property = expr->properties()->at(i);
1384 if (property->IsCompileTimeValue()) continue;
1385
1386 Literal* key = property->key();
1387 Expression* value = property->value();
1388 if (!result_saved) {
1389 __ push(v0); // Save result on stack.
1390 result_saved = true;
1391 }
1392 switch (property->kind()) {
1393 case ObjectLiteral::Property::CONSTANT:
1394 UNREACHABLE();
1395 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1396 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1397 // Fall through.
1398 case ObjectLiteral::Property::COMPUTED:
1399 if (key->handle()->IsSymbol()) {
1400 if (property->emit_store()) {
1401 VisitForAccumulatorValue(value);
1402 __ mov(a0, result_register());
1403 __ li(a2, Operand(key->handle()));
1404 __ lw(a1, MemOperand(sp));
1405 Handle<Code> ic = is_strict_mode()
1406 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1407 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001408 __ Call(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001409 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1410 } else {
1411 VisitForEffect(value);
1412 }
1413 break;
1414 }
1415 // Fall through.
1416 case ObjectLiteral::Property::PROTOTYPE:
1417 // Duplicate receiver on stack.
1418 __ lw(a0, MemOperand(sp));
1419 __ push(a0);
1420 VisitForStackValue(key);
1421 VisitForStackValue(value);
1422 if (property->emit_store()) {
1423 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1424 __ push(a0);
1425 __ CallRuntime(Runtime::kSetProperty, 4);
1426 } else {
1427 __ Drop(3);
1428 }
1429 break;
1430 case ObjectLiteral::Property::GETTER:
1431 case ObjectLiteral::Property::SETTER:
1432 // Duplicate receiver on stack.
1433 __ lw(a0, MemOperand(sp));
1434 __ push(a0);
1435 VisitForStackValue(key);
1436 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1437 Smi::FromInt(1) :
1438 Smi::FromInt(0)));
1439 __ push(a1);
1440 VisitForStackValue(value);
1441 __ CallRuntime(Runtime::kDefineAccessor, 4);
1442 break;
1443 }
1444 }
1445
1446 if (expr->has_function()) {
1447 ASSERT(result_saved);
1448 __ lw(a0, MemOperand(sp));
1449 __ push(a0);
1450 __ CallRuntime(Runtime::kToFastProperties, 1);
1451 }
1452
1453 if (result_saved) {
1454 context()->PlugTOS();
1455 } else {
1456 context()->Plug(v0);
1457 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001458}
1459
1460
1461void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001462 Comment cmnt(masm_, "[ ArrayLiteral");
1463
1464 ZoneList<Expression*>* subexprs = expr->values();
1465 int length = subexprs->length();
1466 __ mov(a0, result_register());
1467 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1468 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1469 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1470 __ li(a1, Operand(expr->constant_elements()));
1471 __ Push(a3, a2, a1);
1472 if (expr->constant_elements()->map() ==
1473 isolate()->heap()->fixed_cow_array_map()) {
1474 FastCloneShallowArrayStub stub(
1475 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1476 __ CallStub(&stub);
1477 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1478 1, a1, a2);
1479 } else if (expr->depth() > 1) {
1480 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1481 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1482 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1483 } else {
1484 FastCloneShallowArrayStub stub(
1485 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
1486 __ CallStub(&stub);
1487 }
1488
1489 bool result_saved = false; // Is the result saved to the stack?
1490
1491 // Emit code to evaluate all the non-constant subexpressions and to store
1492 // them into the newly cloned array.
1493 for (int i = 0; i < length; i++) {
1494 Expression* subexpr = subexprs->at(i);
1495 // If the subexpression is a literal or a simple materialized literal it
1496 // is already set in the cloned array.
1497 if (subexpr->AsLiteral() != NULL ||
1498 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1499 continue;
1500 }
1501
1502 if (!result_saved) {
1503 __ push(v0);
1504 result_saved = true;
1505 }
1506 VisitForAccumulatorValue(subexpr);
1507
1508 // Store the subexpression value in the array's elements.
1509 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1510 __ lw(a1, FieldMemOperand(a1, JSObject::kElementsOffset));
1511 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1512 __ sw(result_register(), FieldMemOperand(a1, offset));
1513
1514 // Update the write barrier for the array store with v0 as the scratch
1515 // register.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001516 __ RecordWrite(a1, Operand(offset), a2, result_register());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001517
1518 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1519 }
1520
1521 if (result_saved) {
1522 context()->PlugTOS();
1523 } else {
1524 context()->Plug(v0);
1525 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001526}
1527
1528
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001529void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001530 Comment cmnt(masm_, "[ Assignment");
1531 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1532 // on the left-hand side.
1533 if (!expr->target()->IsValidLeftHandSide()) {
1534 VisitForEffect(expr->target());
1535 return;
1536 }
1537
1538 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001539 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001540 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1541 LhsKind assign_type = VARIABLE;
1542 Property* property = expr->target()->AsProperty();
1543 if (property != NULL) {
1544 assign_type = (property->key()->IsPropertyName())
1545 ? NAMED_PROPERTY
1546 : KEYED_PROPERTY;
1547 }
1548
1549 // Evaluate LHS expression.
1550 switch (assign_type) {
1551 case VARIABLE:
1552 // Nothing to do here.
1553 break;
1554 case NAMED_PROPERTY:
1555 if (expr->is_compound()) {
1556 // We need the receiver both on the stack and in the accumulator.
1557 VisitForAccumulatorValue(property->obj());
1558 __ push(result_register());
1559 } else {
1560 VisitForStackValue(property->obj());
1561 }
1562 break;
1563 case KEYED_PROPERTY:
1564 // We need the key and receiver on both the stack and in v0 and a1.
1565 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001566 VisitForStackValue(property->obj());
1567 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001568 __ lw(a1, MemOperand(sp, 0));
1569 __ push(v0);
1570 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001571 VisitForStackValue(property->obj());
1572 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001573 }
1574 break;
1575 }
1576
1577 // For compound assignments we need another deoptimization point after the
1578 // variable/property load.
1579 if (expr->is_compound()) {
1580 { AccumulatorValueContext context(this);
1581 switch (assign_type) {
1582 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001583 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001584 PrepareForBailout(expr->target(), TOS_REG);
1585 break;
1586 case NAMED_PROPERTY:
1587 EmitNamedPropertyLoad(property);
1588 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1589 break;
1590 case KEYED_PROPERTY:
1591 EmitKeyedPropertyLoad(property);
1592 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1593 break;
1594 }
1595 }
1596
1597 Token::Value op = expr->binary_op();
1598 __ push(v0); // Left operand goes on the stack.
1599 VisitForAccumulatorValue(expr->value());
1600
1601 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1602 ? OVERWRITE_RIGHT
1603 : NO_OVERWRITE;
1604 SetSourcePosition(expr->position() + 1);
1605 AccumulatorValueContext context(this);
1606 if (ShouldInlineSmiCase(op)) {
1607 EmitInlineSmiBinaryOp(expr->binary_operation(),
1608 op,
1609 mode,
1610 expr->target(),
1611 expr->value());
1612 } else {
1613 EmitBinaryOp(expr->binary_operation(), op, mode);
1614 }
1615
1616 // Deoptimization point in case the binary operation may have side effects.
1617 PrepareForBailout(expr->binary_operation(), TOS_REG);
1618 } else {
1619 VisitForAccumulatorValue(expr->value());
1620 }
1621
1622 // Record source position before possible IC call.
1623 SetSourcePosition(expr->position());
1624
1625 // Store the value.
1626 switch (assign_type) {
1627 case VARIABLE:
1628 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1629 expr->op());
1630 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1631 context()->Plug(v0);
1632 break;
1633 case NAMED_PROPERTY:
1634 EmitNamedPropertyAssignment(expr);
1635 break;
1636 case KEYED_PROPERTY:
1637 EmitKeyedPropertyAssignment(expr);
1638 break;
1639 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001640}
1641
1642
ager@chromium.org5c838252010-02-19 08:53:10 +00001643void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001644 SetSourcePosition(prop->position());
1645 Literal* key = prop->key()->AsLiteral();
1646 __ mov(a0, result_register());
1647 __ li(a2, Operand(key->handle()));
1648 // Call load IC. It has arguments receiver and property name a0 and a2.
1649 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001650 __ Call(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001651}
1652
1653
1654void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001655 SetSourcePosition(prop->position());
1656 __ mov(a0, result_register());
1657 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1658 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001659 __ Call(ic, RelocInfo::CODE_TARGET, GetPropertyId(prop));
ager@chromium.org5c838252010-02-19 08:53:10 +00001660}
1661
1662
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001663void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001664 Token::Value op,
1665 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001666 Expression* left_expr,
1667 Expression* right_expr) {
1668 Label done, smi_case, stub_call;
1669
1670 Register scratch1 = a2;
1671 Register scratch2 = a3;
1672
1673 // Get the arguments.
1674 Register left = a1;
1675 Register right = a0;
1676 __ pop(left);
1677 __ mov(a0, result_register());
1678
1679 // Perform combined smi check on both operands.
1680 __ Or(scratch1, left, Operand(right));
1681 STATIC_ASSERT(kSmiTag == 0);
1682 JumpPatchSite patch_site(masm_);
1683 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1684
1685 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001686 BinaryOpStub stub(op, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001687 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001688 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001689 __ jmp(&done);
1690
1691 __ bind(&smi_case);
1692 // Smi case. This code works the same way as the smi-smi case in the type
1693 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001694 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001695 switch (op) {
1696 case Token::SAR:
1697 __ Branch(&stub_call);
1698 __ GetLeastBitsFromSmi(scratch1, right, 5);
1699 __ srav(right, left, scratch1);
1700 __ And(v0, right, Operand(~kSmiTagMask));
1701 break;
1702 case Token::SHL: {
1703 __ Branch(&stub_call);
1704 __ SmiUntag(scratch1, left);
1705 __ GetLeastBitsFromSmi(scratch2, right, 5);
1706 __ sllv(scratch1, scratch1, scratch2);
1707 __ Addu(scratch2, scratch1, Operand(0x40000000));
1708 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1709 __ SmiTag(v0, scratch1);
1710 break;
1711 }
1712 case Token::SHR: {
1713 __ Branch(&stub_call);
1714 __ SmiUntag(scratch1, left);
1715 __ GetLeastBitsFromSmi(scratch2, right, 5);
1716 __ srlv(scratch1, scratch1, scratch2);
1717 __ And(scratch2, scratch1, 0xc0000000);
1718 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1719 __ SmiTag(v0, scratch1);
1720 break;
1721 }
1722 case Token::ADD:
1723 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1724 __ BranchOnOverflow(&stub_call, scratch1);
1725 break;
1726 case Token::SUB:
1727 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1728 __ BranchOnOverflow(&stub_call, scratch1);
1729 break;
1730 case Token::MUL: {
1731 __ SmiUntag(scratch1, right);
1732 __ Mult(left, scratch1);
1733 __ mflo(scratch1);
1734 __ mfhi(scratch2);
1735 __ sra(scratch1, scratch1, 31);
1736 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1737 __ mflo(v0);
1738 __ Branch(&done, ne, v0, Operand(zero_reg));
1739 __ Addu(scratch2, right, left);
1740 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1741 ASSERT(Smi::FromInt(0) == 0);
1742 __ mov(v0, zero_reg);
1743 break;
1744 }
1745 case Token::BIT_OR:
1746 __ Or(v0, left, Operand(right));
1747 break;
1748 case Token::BIT_AND:
1749 __ And(v0, left, Operand(right));
1750 break;
1751 case Token::BIT_XOR:
1752 __ Xor(v0, left, Operand(right));
1753 break;
1754 default:
1755 UNREACHABLE();
1756 }
1757
1758 __ bind(&done);
1759 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001760}
1761
1762
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001763void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1764 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001765 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001766 __ mov(a0, result_register());
1767 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001768 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001769 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001770 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001771 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001772 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001773}
1774
1775
1776void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001777 // Invalid left-hand sides are rewritten to have a 'throw
1778 // ReferenceError' on the left-hand side.
1779 if (!expr->IsValidLeftHandSide()) {
1780 VisitForEffect(expr);
1781 return;
1782 }
1783
1784 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001785 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001786 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1787 LhsKind assign_type = VARIABLE;
1788 Property* prop = expr->AsProperty();
1789 if (prop != NULL) {
1790 assign_type = (prop->key()->IsPropertyName())
1791 ? NAMED_PROPERTY
1792 : KEYED_PROPERTY;
1793 }
1794
1795 switch (assign_type) {
1796 case VARIABLE: {
1797 Variable* var = expr->AsVariableProxy()->var();
1798 EffectContext context(this);
1799 EmitVariableAssignment(var, Token::ASSIGN);
1800 break;
1801 }
1802 case NAMED_PROPERTY: {
1803 __ push(result_register()); // Preserve value.
1804 VisitForAccumulatorValue(prop->obj());
1805 __ mov(a1, result_register());
1806 __ pop(a0); // Restore value.
1807 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1808 Handle<Code> ic = is_strict_mode()
1809 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1810 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001811 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001812 break;
1813 }
1814 case KEYED_PROPERTY: {
1815 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001816 VisitForStackValue(prop->obj());
1817 VisitForAccumulatorValue(prop->key());
1818 __ mov(a1, result_register());
1819 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001820 __ pop(a0); // Restore value.
1821 Handle<Code> ic = is_strict_mode()
1822 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
1823 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001824 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001825 break;
1826 }
1827 }
1828 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1829 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001830}
1831
1832
1833void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001834 Token::Value op) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001835 if (var->IsUnallocated()) {
1836 // Global var, const, or let.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001837 __ mov(a0, result_register());
1838 __ li(a2, Operand(var->name()));
1839 __ lw(a1, GlobalObjectOperand());
1840 Handle<Code> ic = is_strict_mode()
1841 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1842 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001843 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001844
1845 } else if (op == Token::INIT_CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001846 // Const initializers need a write barrier.
1847 ASSERT(!var->IsParameter()); // No const parameters.
1848 if (var->IsStackLocal()) {
1849 Label skip;
1850 __ lw(a1, StackOperand(var));
1851 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1852 __ Branch(&skip, ne, a1, Operand(t0));
1853 __ sw(result_register(), StackOperand(var));
1854 __ bind(&skip);
1855 } else {
1856 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
1857 // Like var declarations, const declarations are hoisted to function
1858 // scope. However, unlike var initializers, const initializers are
1859 // able to drill a hole to that function context, even from inside a
1860 // 'with' context. We thus bypass the normal static scope lookup for
1861 // var->IsContextSlot().
1862 __ push(v0);
1863 __ li(a0, Operand(var->name()));
1864 __ Push(cp, a0); // Context and name.
1865 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001866 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001867
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001868 } else if (var->mode() == Variable::LET && op != Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001869 // Non-initializing assignment to let variable needs a write barrier.
1870 if (var->IsLookupSlot()) {
1871 __ push(v0); // Value.
1872 __ li(a1, Operand(var->name()));
1873 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1874 __ Push(cp, a1, a0); // Context, name, strict mode.
1875 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1876 } else {
1877 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
1878 Label assign;
1879 MemOperand location = VarOperand(var, a1);
1880 __ lw(a3, location);
1881 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1882 __ Branch(&assign, ne, a3, Operand(t0));
1883 __ li(a3, Operand(var->name()));
1884 __ push(a3);
1885 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1886 // Perform the assignment.
1887 __ bind(&assign);
1888 __ sw(result_register(), location);
1889 if (var->IsContextSlot()) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001890 // RecordWrite may destroy all its register arguments.
1891 __ mov(a3, result_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001892 int offset = Context::SlotOffset(var->index());
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001893 __ RecordWrite(a1, Operand(offset), a2, a3);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001894 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001895 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001896
1897 } else if (var->mode() != Variable::CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001898 // Assignment to var or initializing assignment to let.
1899 if (var->IsStackAllocated() || var->IsContextSlot()) {
1900 MemOperand location = VarOperand(var, a1);
1901 if (FLAG_debug_code && op == Token::INIT_LET) {
1902 // Check for an uninitialized let binding.
1903 __ lw(a2, location);
1904 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1905 __ Check(eq, "Let binding re-initialization.", a2, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001906 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001907 // Perform the assignment.
1908 __ sw(v0, location);
1909 if (var->IsContextSlot()) {
1910 __ mov(a3, v0);
1911 __ RecordWrite(a1, Operand(Context::SlotOffset(var->index())), a2, a3);
1912 }
1913 } else {
1914 ASSERT(var->IsLookupSlot());
1915 __ push(v0); // Value.
1916 __ li(a1, Operand(var->name()));
1917 __ li(a0, Operand(Smi::FromInt(strict_mode_flag())));
1918 __ Push(cp, a1, a0); // Context, name, strict mode.
1919 __ CallRuntime(Runtime::kStoreContextSlot, 4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001920 }
1921 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001922 // Non-initializing assignments to consts are ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +00001923}
1924
1925
1926void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001927 // Assignment to a property, using a named store IC.
1928 Property* prop = expr->target()->AsProperty();
1929 ASSERT(prop != NULL);
1930 ASSERT(prop->key()->AsLiteral() != NULL);
1931
1932 // If the assignment starts a block of assignments to the same object,
1933 // change to slow case to avoid the quadratic behavior of repeatedly
1934 // adding fast properties.
1935 if (expr->starts_initialization_block()) {
1936 __ push(result_register());
1937 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
1938 __ push(t0);
1939 __ CallRuntime(Runtime::kToSlowProperties, 1);
1940 __ pop(result_register());
1941 }
1942
1943 // Record source code position before IC call.
1944 SetSourcePosition(expr->position());
1945 __ mov(a0, result_register()); // Load the value.
1946 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
1947 // Load receiver to a1. Leave a copy in the stack if needed for turning the
1948 // receiver into fast case.
1949 if (expr->ends_initialization_block()) {
1950 __ lw(a1, MemOperand(sp));
1951 } else {
1952 __ pop(a1);
1953 }
1954
1955 Handle<Code> ic = is_strict_mode()
1956 ? isolate()->builtins()->StoreIC_Initialize_Strict()
1957 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001958 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001959
1960 // If the assignment ends an initialization block, revert to fast case.
1961 if (expr->ends_initialization_block()) {
1962 __ push(v0); // Result of assignment, saved even if not needed.
1963 // Receiver is under the result value.
1964 __ lw(t0, MemOperand(sp, kPointerSize));
1965 __ push(t0);
1966 __ CallRuntime(Runtime::kToFastProperties, 1);
1967 __ pop(v0);
1968 __ Drop(1);
1969 }
1970 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1971 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001972}
1973
1974
1975void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001976 // Assignment to a property, using a keyed store IC.
1977
1978 // If the assignment starts a block of assignments to the same object,
1979 // change to slow case to avoid the quadratic behavior of repeatedly
1980 // adding fast properties.
1981 if (expr->starts_initialization_block()) {
1982 __ push(result_register());
1983 // Receiver is now under the key and value.
1984 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
1985 __ push(t0);
1986 __ CallRuntime(Runtime::kToSlowProperties, 1);
1987 __ pop(result_register());
1988 }
1989
1990 // Record source code position before IC call.
1991 SetSourcePosition(expr->position());
1992 // Call keyed store IC.
1993 // The arguments are:
1994 // - a0 is the value,
1995 // - a1 is the key,
1996 // - a2 is the receiver.
1997 __ mov(a0, result_register());
1998 __ pop(a1); // Key.
1999 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2000 // receiver into fast case.
2001 if (expr->ends_initialization_block()) {
2002 __ lw(a2, MemOperand(sp));
2003 } else {
2004 __ pop(a2);
2005 }
2006
2007 Handle<Code> ic = is_strict_mode()
2008 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
2009 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002010 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002011
2012 // If the assignment ends an initialization block, revert to fast case.
2013 if (expr->ends_initialization_block()) {
2014 __ push(v0); // Result of assignment, saved even if not needed.
2015 // Receiver is under the result value.
2016 __ lw(t0, MemOperand(sp, kPointerSize));
2017 __ push(t0);
2018 __ CallRuntime(Runtime::kToFastProperties, 1);
2019 __ pop(v0);
2020 __ Drop(1);
2021 }
2022 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2023 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002024}
2025
2026
2027void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002028 Comment cmnt(masm_, "[ Property");
2029 Expression* key = expr->key();
2030
2031 if (key->IsPropertyName()) {
2032 VisitForAccumulatorValue(expr->obj());
2033 EmitNamedPropertyLoad(expr);
2034 context()->Plug(v0);
2035 } else {
2036 VisitForStackValue(expr->obj());
2037 VisitForAccumulatorValue(expr->key());
2038 __ pop(a1);
2039 EmitKeyedPropertyLoad(expr);
2040 context()->Plug(v0);
2041 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002042}
2043
lrn@chromium.org7516f052011-03-30 08:52:27 +00002044
ager@chromium.org5c838252010-02-19 08:53:10 +00002045void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002046 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002047 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002048 // Code common for calls using the IC.
2049 ZoneList<Expression*>* args = expr->arguments();
2050 int arg_count = args->length();
2051 { PreservePositionScope scope(masm()->positions_recorder());
2052 for (int i = 0; i < arg_count; i++) {
2053 VisitForStackValue(args->at(i));
2054 }
2055 __ li(a2, Operand(name));
2056 }
2057 // Record source position for debugger.
2058 SetSourcePosition(expr->position());
2059 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002060 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002061 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002062 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002063 RecordJSReturnSite(expr);
2064 // Restore context register.
2065 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2066 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002067}
2068
2069
lrn@chromium.org7516f052011-03-30 08:52:27 +00002070void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002071 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002072 // Load the key.
2073 VisitForAccumulatorValue(key);
2074
2075 // Swap the name of the function and the receiver on the stack to follow
2076 // the calling convention for call ICs.
2077 __ pop(a1);
2078 __ push(v0);
2079 __ push(a1);
2080
2081 // Code common for calls using the IC.
2082 ZoneList<Expression*>* args = expr->arguments();
2083 int arg_count = args->length();
2084 { PreservePositionScope scope(masm()->positions_recorder());
2085 for (int i = 0; i < arg_count; i++) {
2086 VisitForStackValue(args->at(i));
2087 }
2088 }
2089 // Record source position for debugger.
2090 SetSourcePosition(expr->position());
2091 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002092 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002093 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002094 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002095 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002096 RecordJSReturnSite(expr);
2097 // Restore context register.
2098 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2099 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002100}
2101
2102
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002103void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002104 // Code common for calls using the call stub.
2105 ZoneList<Expression*>* args = expr->arguments();
2106 int arg_count = args->length();
2107 { PreservePositionScope scope(masm()->positions_recorder());
2108 for (int i = 0; i < arg_count; i++) {
2109 VisitForStackValue(args->at(i));
2110 }
2111 }
2112 // Record source position for debugger.
2113 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002114 CallFunctionStub stub(arg_count, flags);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002115 __ CallStub(&stub);
2116 RecordJSReturnSite(expr);
2117 // Restore context register.
2118 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2119 context()->DropAndPlug(1, v0);
2120}
2121
2122
2123void FullCodeGenerator::EmitResolvePossiblyDirectEval(ResolveEvalFlag flag,
2124 int arg_count) {
2125 // Push copy of the first argument or undefined if it doesn't exist.
2126 if (arg_count > 0) {
2127 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2128 } else {
2129 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2130 }
2131 __ push(a1);
2132
2133 // Push the receiver of the enclosing function and do runtime call.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002134 int receiver_offset = 2 + info_->scope()->num_parameters();
2135 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002136 __ push(a1);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002137 // Push the strict mode flag. In harmony mode every eval call
2138 // is a strict mode eval call.
2139 StrictModeFlag strict_mode = strict_mode_flag();
2140 if (FLAG_harmony_block_scoping) {
2141 strict_mode = kStrictMode;
2142 }
2143 __ li(a1, Operand(Smi::FromInt(strict_mode)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002144 __ push(a1);
2145
2146 __ CallRuntime(flag == SKIP_CONTEXT_LOOKUP
2147 ? Runtime::kResolvePossiblyDirectEvalNoLookup
2148 : Runtime::kResolvePossiblyDirectEval, 4);
ager@chromium.org5c838252010-02-19 08:53:10 +00002149}
2150
2151
2152void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002153#ifdef DEBUG
2154 // We want to verify that RecordJSReturnSite gets called on all paths
2155 // through this function. Avoid early returns.
2156 expr->return_is_recorded_ = false;
2157#endif
2158
2159 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002160 Expression* callee = expr->expression();
2161 VariableProxy* proxy = callee->AsVariableProxy();
2162 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002163
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002164 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002165 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2166 // resolve the function we need to call and the receiver of the
2167 // call. Then we call the resolved function using the given
2168 // arguments.
2169 ZoneList<Expression*>* args = expr->arguments();
2170 int arg_count = args->length();
2171
2172 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002173 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002174 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2175 __ push(a2); // Reserved receiver slot.
2176
2177 // Push the arguments.
2178 for (int i = 0; i < arg_count; i++) {
2179 VisitForStackValue(args->at(i));
2180 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002181
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002182 // If we know that eval can only be shadowed by eval-introduced
2183 // variables we attempt to load the global eval function directly
2184 // in generated code. If we succeed, there is no need to perform a
2185 // context lookup in the runtime system.
2186 Label done;
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002187 Variable* var = proxy->var();
2188 if (!var->IsUnallocated() && var->mode() == Variable::DYNAMIC_GLOBAL) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002189 Label slow;
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002190 EmitLoadGlobalCheckExtensions(var, NOT_INSIDE_TYPEOF, &slow);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002191 // Push the function and resolve eval.
2192 __ push(v0);
2193 EmitResolvePossiblyDirectEval(SKIP_CONTEXT_LOOKUP, arg_count);
2194 __ jmp(&done);
2195 __ bind(&slow);
2196 }
2197
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002198 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002199 // resolve eval.
2200 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2201 __ push(a1);
2202 EmitResolvePossiblyDirectEval(PERFORM_CONTEXT_LOOKUP, arg_count);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002203 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002204
2205 // The runtime call returns a pair of values in v0 (function) and
2206 // v1 (receiver). Touch up the stack with the right values.
2207 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2208 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2209 }
2210 // Record source position for debugger.
2211 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002212 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002213 __ CallStub(&stub);
2214 RecordJSReturnSite(expr);
2215 // Restore context register.
2216 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2217 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002218 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002219 // Push global object as receiver for the call IC.
2220 __ lw(a0, GlobalObjectOperand());
2221 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002222 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2223 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002224 // Call to a lookup slot (dynamically introduced variable).
2225 Label slow, done;
2226
2227 { PreservePositionScope scope(masm()->positions_recorder());
2228 // Generate code for loading from variables potentially shadowed
2229 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002230 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002231 }
2232
2233 __ bind(&slow);
2234 // Call the runtime to find the function to call (returned in v0)
2235 // and the object holding it (returned in v1).
2236 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002237 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002238 __ push(a2);
2239 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2240 __ Push(v0, v1); // Function, receiver.
2241
2242 // If fast case code has been generated, emit code to push the
2243 // function and receiver and have the slow path jump around this
2244 // code.
2245 if (done.is_linked()) {
2246 Label call;
2247 __ Branch(&call);
2248 __ bind(&done);
2249 // Push function.
2250 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002251 // The receiver is implicitly the global receiver. Indicate this
2252 // by passing the hole to the call function stub.
2253 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002254 __ push(a1);
2255 __ bind(&call);
2256 }
2257
danno@chromium.org40cb8782011-05-25 07:58:50 +00002258 // The receiver is either the global receiver or an object found
2259 // by LoadContextSlot. That object could be the hole if the
2260 // receiver is implicitly the global object.
2261 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002262 } else if (property != NULL) {
2263 { PreservePositionScope scope(masm()->positions_recorder());
2264 VisitForStackValue(property->obj());
2265 }
2266 if (property->key()->IsPropertyName()) {
2267 EmitCallWithIC(expr,
2268 property->key()->AsLiteral()->handle(),
2269 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002270 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002271 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002272 }
2273 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002274 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002275 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002276 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002277 }
2278 // Load global receiver object.
2279 __ lw(a1, GlobalObjectOperand());
2280 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2281 __ push(a1);
2282 // Emit function call.
2283 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2284 }
2285
2286#ifdef DEBUG
2287 // RecordJSReturnSite should have been called.
2288 ASSERT(expr->return_is_recorded_);
2289#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002290}
2291
2292
2293void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002294 Comment cmnt(masm_, "[ CallNew");
2295 // According to ECMA-262, section 11.2.2, page 44, the function
2296 // expression in new calls must be evaluated before the
2297 // arguments.
2298
2299 // Push constructor on the stack. If it's not a function it's used as
2300 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2301 // ignored.
2302 VisitForStackValue(expr->expression());
2303
2304 // Push the arguments ("left-to-right") on the stack.
2305 ZoneList<Expression*>* args = expr->arguments();
2306 int arg_count = args->length();
2307 for (int i = 0; i < arg_count; i++) {
2308 VisitForStackValue(args->at(i));
2309 }
2310
2311 // Call the construct call builtin that handles allocation and
2312 // constructor invocation.
2313 SetSourcePosition(expr->position());
2314
2315 // Load function and argument count into a1 and a0.
2316 __ li(a0, Operand(arg_count));
2317 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2318
2319 Handle<Code> construct_builtin =
2320 isolate()->builtins()->JSConstructCall();
2321 __ Call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
2322 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002323}
2324
2325
lrn@chromium.org7516f052011-03-30 08:52:27 +00002326void FullCodeGenerator::EmitIsSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002327 ASSERT(args->length() == 1);
2328
2329 VisitForAccumulatorValue(args->at(0));
2330
2331 Label materialize_true, materialize_false;
2332 Label* if_true = NULL;
2333 Label* if_false = NULL;
2334 Label* fall_through = NULL;
2335 context()->PrepareTest(&materialize_true, &materialize_false,
2336 &if_true, &if_false, &fall_through);
2337
2338 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2339 __ And(t0, v0, Operand(kSmiTagMask));
2340 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2341
2342 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002343}
2344
2345
2346void FullCodeGenerator::EmitIsNonNegativeSmi(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002347 ASSERT(args->length() == 1);
2348
2349 VisitForAccumulatorValue(args->at(0));
2350
2351 Label materialize_true, materialize_false;
2352 Label* if_true = NULL;
2353 Label* if_false = NULL;
2354 Label* fall_through = NULL;
2355 context()->PrepareTest(&materialize_true, &materialize_false,
2356 &if_true, &if_false, &fall_through);
2357
2358 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2359 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2360 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2361
2362 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002363}
2364
2365
2366void FullCodeGenerator::EmitIsObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002367 ASSERT(args->length() == 1);
2368
2369 VisitForAccumulatorValue(args->at(0));
2370
2371 Label materialize_true, materialize_false;
2372 Label* if_true = NULL;
2373 Label* if_false = NULL;
2374 Label* fall_through = NULL;
2375 context()->PrepareTest(&materialize_true, &materialize_false,
2376 &if_true, &if_false, &fall_through);
2377
2378 __ JumpIfSmi(v0, if_false);
2379 __ LoadRoot(at, Heap::kNullValueRootIndex);
2380 __ Branch(if_true, eq, v0, Operand(at));
2381 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2382 // Undetectable objects behave like undefined when tested with typeof.
2383 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2384 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2385 __ Branch(if_false, ne, at, Operand(zero_reg));
2386 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002387 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002388 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002389 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2390 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002391
2392 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002393}
2394
2395
2396void FullCodeGenerator::EmitIsSpecObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002397 ASSERT(args->length() == 1);
2398
2399 VisitForAccumulatorValue(args->at(0));
2400
2401 Label materialize_true, materialize_false;
2402 Label* if_true = NULL;
2403 Label* if_false = NULL;
2404 Label* fall_through = NULL;
2405 context()->PrepareTest(&materialize_true, &materialize_false,
2406 &if_true, &if_false, &fall_through);
2407
2408 __ JumpIfSmi(v0, if_false);
2409 __ GetObjectType(v0, a1, a1);
2410 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002411 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002412 if_true, if_false, fall_through);
2413
2414 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002415}
2416
2417
2418void FullCodeGenerator::EmitIsUndetectableObject(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002419 ASSERT(args->length() == 1);
2420
2421 VisitForAccumulatorValue(args->at(0));
2422
2423 Label materialize_true, materialize_false;
2424 Label* if_true = NULL;
2425 Label* if_false = NULL;
2426 Label* fall_through = NULL;
2427 context()->PrepareTest(&materialize_true, &materialize_false,
2428 &if_true, &if_false, &fall_through);
2429
2430 __ JumpIfSmi(v0, if_false);
2431 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2432 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2433 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2434 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2435 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2436
2437 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002438}
2439
2440
2441void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
2442 ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002443
2444 ASSERT(args->length() == 1);
2445
2446 VisitForAccumulatorValue(args->at(0));
2447
2448 Label materialize_true, materialize_false;
2449 Label* if_true = NULL;
2450 Label* if_false = NULL;
2451 Label* fall_through = NULL;
2452 context()->PrepareTest(&materialize_true, &materialize_false,
2453 &if_true, &if_false, &fall_through);
2454
2455 if (FLAG_debug_code) __ AbortIfSmi(v0);
2456
2457 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2458 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2459 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2460 __ Branch(if_true, ne, t0, Operand(zero_reg));
2461
2462 // Check for fast case object. Generate false result for slow case object.
2463 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2464 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2465 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2466 __ Branch(if_false, eq, a2, Operand(t0));
2467
2468 // Look for valueOf symbol in the descriptor array, and indicate false if
2469 // found. The type is not checked, so if it is a transition it is a false
2470 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002471 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002472 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2473 // t0: descriptor array
2474 // a3: length of descriptor array
2475 // Calculate the end of the descriptor array.
2476 STATIC_ASSERT(kSmiTag == 0);
2477 STATIC_ASSERT(kSmiTagSize == 1);
2478 STATIC_ASSERT(kPointerSize == 4);
2479 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2480 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2481 __ Addu(a2, a2, t1);
2482
2483 // Calculate location of the first key name.
2484 __ Addu(t0,
2485 t0,
2486 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2487 DescriptorArray::kFirstIndex * kPointerSize));
2488 // Loop through all the keys in the descriptor array. If one of these is the
2489 // symbol valueOf the result is false.
2490 Label entry, loop;
2491 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2492 // used in the loop below.
2493 __ li(t2, Operand(FACTORY->value_of_symbol()));
2494 __ jmp(&entry);
2495 __ bind(&loop);
2496 __ lw(a3, MemOperand(t0, 0));
2497 __ Branch(if_false, eq, a3, Operand(t2));
2498 __ Addu(t0, t0, Operand(kPointerSize));
2499 __ bind(&entry);
2500 __ Branch(&loop, ne, t0, Operand(a2));
2501
2502 // If a valueOf property is not found on the object check that it's
2503 // prototype is the un-modified String prototype. If not result is false.
2504 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2505 __ JumpIfSmi(a2, if_false);
2506 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2507 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2508 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2509 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2510 __ Branch(if_false, ne, a2, Operand(a3));
2511
2512 // Set the bit in the map to indicate that it has been checked safe for
2513 // default valueOf and set true result.
2514 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2515 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2516 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2517 __ jmp(if_true);
2518
2519 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2520 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002521}
2522
2523
2524void FullCodeGenerator::EmitIsFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002525 ASSERT(args->length() == 1);
2526
2527 VisitForAccumulatorValue(args->at(0));
2528
2529 Label materialize_true, materialize_false;
2530 Label* if_true = NULL;
2531 Label* if_false = NULL;
2532 Label* fall_through = NULL;
2533 context()->PrepareTest(&materialize_true, &materialize_false,
2534 &if_true, &if_false, &fall_through);
2535
2536 __ JumpIfSmi(v0, if_false);
2537 __ GetObjectType(v0, a1, a2);
2538 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2539 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2540 __ Branch(if_false);
2541
2542 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002543}
2544
2545
2546void FullCodeGenerator::EmitIsArray(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002547 ASSERT(args->length() == 1);
2548
2549 VisitForAccumulatorValue(args->at(0));
2550
2551 Label materialize_true, materialize_false;
2552 Label* if_true = NULL;
2553 Label* if_false = NULL;
2554 Label* fall_through = NULL;
2555 context()->PrepareTest(&materialize_true, &materialize_false,
2556 &if_true, &if_false, &fall_through);
2557
2558 __ JumpIfSmi(v0, if_false);
2559 __ GetObjectType(v0, a1, a1);
2560 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2561 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2562 if_true, if_false, fall_through);
2563
2564 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002565}
2566
2567
2568void FullCodeGenerator::EmitIsRegExp(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002569 ASSERT(args->length() == 1);
2570
2571 VisitForAccumulatorValue(args->at(0));
2572
2573 Label materialize_true, materialize_false;
2574 Label* if_true = NULL;
2575 Label* if_false = NULL;
2576 Label* fall_through = NULL;
2577 context()->PrepareTest(&materialize_true, &materialize_false,
2578 &if_true, &if_false, &fall_through);
2579
2580 __ JumpIfSmi(v0, if_false);
2581 __ GetObjectType(v0, a1, a1);
2582 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2583 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2584
2585 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002586}
2587
2588
2589void FullCodeGenerator::EmitIsConstructCall(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002590 ASSERT(args->length() == 0);
2591
2592 Label materialize_true, materialize_false;
2593 Label* if_true = NULL;
2594 Label* if_false = NULL;
2595 Label* fall_through = NULL;
2596 context()->PrepareTest(&materialize_true, &materialize_false,
2597 &if_true, &if_false, &fall_through);
2598
2599 // Get the frame pointer for the calling frame.
2600 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2601
2602 // Skip the arguments adaptor frame if it exists.
2603 Label check_frame_marker;
2604 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2605 __ Branch(&check_frame_marker, ne,
2606 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2607 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2608
2609 // Check the marker in the calling frame.
2610 __ bind(&check_frame_marker);
2611 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
2612 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2613 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2614 if_true, if_false, fall_through);
2615
2616 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002617}
2618
2619
2620void FullCodeGenerator::EmitObjectEquals(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002621 ASSERT(args->length() == 2);
2622
2623 // Load the two objects into registers and perform the comparison.
2624 VisitForStackValue(args->at(0));
2625 VisitForAccumulatorValue(args->at(1));
2626
2627 Label materialize_true, materialize_false;
2628 Label* if_true = NULL;
2629 Label* if_false = NULL;
2630 Label* fall_through = NULL;
2631 context()->PrepareTest(&materialize_true, &materialize_false,
2632 &if_true, &if_false, &fall_through);
2633
2634 __ pop(a1);
2635 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
2636 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2637
2638 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002639}
2640
2641
2642void FullCodeGenerator::EmitArguments(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002643 ASSERT(args->length() == 1);
2644
2645 // ArgumentsAccessStub expects the key in a1 and the formal
2646 // parameter count in a0.
2647 VisitForAccumulatorValue(args->at(0));
2648 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002649 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002650 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2651 __ CallStub(&stub);
2652 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002653}
2654
2655
2656void FullCodeGenerator::EmitArgumentsLength(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002657 ASSERT(args->length() == 0);
2658
2659 Label exit;
2660 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002661 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002662
2663 // Check if the calling frame is an arguments adaptor frame.
2664 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2665 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2666 __ Branch(&exit, ne, a3,
2667 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2668
2669 // Arguments adaptor case: Read the arguments length from the
2670 // adaptor frame.
2671 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2672
2673 __ bind(&exit);
2674 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002675}
2676
2677
2678void FullCodeGenerator::EmitClassOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002679 ASSERT(args->length() == 1);
2680 Label done, null, function, non_function_constructor;
2681
2682 VisitForAccumulatorValue(args->at(0));
2683
2684 // If the object is a smi, we return null.
2685 __ JumpIfSmi(v0, &null);
2686
2687 // Check that the object is a JS object but take special care of JS
2688 // functions to make sure they have 'Function' as their class.
2689 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002690 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002691
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002692 // As long as LAST_CALLABLE_SPEC_OBJECT_TYPE is the last instance type, and
2693 // FIRST_CALLABLE_SPEC_OBJECT_TYPE comes right after
2694 // LAST_NONCALLABLE_SPEC_OBJECT_TYPE, we can avoid checking for the latter.
2695 STATIC_ASSERT(LAST_TYPE == LAST_CALLABLE_SPEC_OBJECT_TYPE);
2696 STATIC_ASSERT(FIRST_CALLABLE_SPEC_OBJECT_TYPE ==
2697 LAST_NONCALLABLE_SPEC_OBJECT_TYPE + 1);
2698 __ Branch(&function, ge, a1, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002699
2700 // Check if the constructor in the map is a function.
2701 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2702 __ GetObjectType(v0, a1, a1);
2703 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2704
2705 // v0 now contains the constructor function. Grab the
2706 // instance class name from there.
2707 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2708 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2709 __ Branch(&done);
2710
2711 // Functions have class 'Function'.
2712 __ bind(&function);
2713 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2714 __ jmp(&done);
2715
2716 // Objects with a non-function constructor have class 'Object'.
2717 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002718 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002719 __ jmp(&done);
2720
2721 // Non-JS objects have class null.
2722 __ bind(&null);
2723 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2724
2725 // All done.
2726 __ bind(&done);
2727
2728 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002729}
2730
2731
2732void FullCodeGenerator::EmitLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002733 // Conditionally generate a log call.
2734 // Args:
2735 // 0 (literal string): The type of logging (corresponds to the flags).
2736 // This is used to determine whether or not to generate the log call.
2737 // 1 (string): Format string. Access the string at argument index 2
2738 // with '%2s' (see Logger::LogRuntime for all the formats).
2739 // 2 (array): Arguments to the format string.
2740 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002741 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2742 VisitForStackValue(args->at(1));
2743 VisitForStackValue(args->at(2));
2744 __ CallRuntime(Runtime::kLog, 2);
2745 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002746
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002747 // Finally, we're expected to leave a value on the top of the stack.
2748 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2749 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002750}
2751
2752
2753void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002754 ASSERT(args->length() == 0);
2755
2756 Label slow_allocate_heapnumber;
2757 Label heapnumber_allocated;
2758
2759 // Save the new heap number in callee-saved register s0, since
2760 // we call out to external C code below.
2761 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2762 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2763 __ jmp(&heapnumber_allocated);
2764
2765 __ bind(&slow_allocate_heapnumber);
2766
2767 // Allocate a heap number.
2768 __ CallRuntime(Runtime::kNumberAlloc, 0);
2769 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2770
2771 __ bind(&heapnumber_allocated);
2772
2773 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2774 // by computing:
2775 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2776 if (CpuFeatures::IsSupported(FPU)) {
2777 __ PrepareCallCFunction(1, a0);
2778 __ li(a0, Operand(ExternalReference::isolate_address()));
2779 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2780
2781
2782 CpuFeatures::Scope scope(FPU);
2783 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2784 __ li(a1, Operand(0x41300000));
2785 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002786 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002787 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002788 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002789 // Subtract and store the result in the heap number.
2790 __ sub_d(f0, f12, f14);
2791 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2792 __ mov(v0, s0);
2793 } else {
2794 __ PrepareCallCFunction(2, a0);
2795 __ mov(a0, s0);
2796 __ li(a1, Operand(ExternalReference::isolate_address()));
2797 __ CallCFunction(
2798 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2799 }
2800
2801 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002802}
2803
2804
2805void FullCodeGenerator::EmitSubString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002806 // Load the arguments on the stack and call the stub.
2807 SubStringStub stub;
2808 ASSERT(args->length() == 3);
2809 VisitForStackValue(args->at(0));
2810 VisitForStackValue(args->at(1));
2811 VisitForStackValue(args->at(2));
2812 __ CallStub(&stub);
2813 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002814}
2815
2816
2817void FullCodeGenerator::EmitRegExpExec(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002818 // Load the arguments on the stack and call the stub.
2819 RegExpExecStub stub;
2820 ASSERT(args->length() == 4);
2821 VisitForStackValue(args->at(0));
2822 VisitForStackValue(args->at(1));
2823 VisitForStackValue(args->at(2));
2824 VisitForStackValue(args->at(3));
2825 __ CallStub(&stub);
2826 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002827}
2828
2829
2830void FullCodeGenerator::EmitValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002831 ASSERT(args->length() == 1);
2832
2833 VisitForAccumulatorValue(args->at(0)); // Load the object.
2834
2835 Label done;
2836 // If the object is a smi return the object.
2837 __ JumpIfSmi(v0, &done);
2838 // If the object is not a value type, return the object.
2839 __ GetObjectType(v0, a1, a1);
2840 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2841
2842 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2843
2844 __ bind(&done);
2845 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002846}
2847
2848
2849void FullCodeGenerator::EmitMathPow(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002850 // Load the arguments on the stack and call the runtime function.
2851 ASSERT(args->length() == 2);
2852 VisitForStackValue(args->at(0));
2853 VisitForStackValue(args->at(1));
2854 MathPowStub stub;
2855 __ CallStub(&stub);
2856 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002857}
2858
2859
2860void FullCodeGenerator::EmitSetValueOf(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002861 ASSERT(args->length() == 2);
2862
2863 VisitForStackValue(args->at(0)); // Load the object.
2864 VisitForAccumulatorValue(args->at(1)); // Load the value.
2865 __ pop(a1); // v0 = value. a1 = object.
2866
2867 Label done;
2868 // If the object is a smi, return the value.
2869 __ JumpIfSmi(a1, &done);
2870
2871 // If the object is not a value type, return the value.
2872 __ GetObjectType(a1, a2, a2);
2873 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2874
2875 // Store the value.
2876 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2877 // Update the write barrier. Save the value as it will be
2878 // overwritten by the write barrier code and is needed afterward.
2879 __ RecordWrite(a1, Operand(JSValue::kValueOffset - kHeapObjectTag), a2, a3);
2880
2881 __ bind(&done);
2882 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002883}
2884
2885
2886void FullCodeGenerator::EmitNumberToString(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002887 ASSERT_EQ(args->length(), 1);
2888
2889 // Load the argument on the stack and call the stub.
2890 VisitForStackValue(args->at(0));
2891
2892 NumberToStringStub stub;
2893 __ CallStub(&stub);
2894 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002895}
2896
2897
2898void FullCodeGenerator::EmitStringCharFromCode(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002899 ASSERT(args->length() == 1);
2900
2901 VisitForAccumulatorValue(args->at(0));
2902
2903 Label done;
2904 StringCharFromCodeGenerator generator(v0, a1);
2905 generator.GenerateFast(masm_);
2906 __ jmp(&done);
2907
2908 NopRuntimeCallHelper call_helper;
2909 generator.GenerateSlow(masm_, call_helper);
2910
2911 __ bind(&done);
2912 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002913}
2914
2915
2916void FullCodeGenerator::EmitStringCharCodeAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002917 ASSERT(args->length() == 2);
2918
2919 VisitForStackValue(args->at(0));
2920 VisitForAccumulatorValue(args->at(1));
2921 __ mov(a0, result_register());
2922
2923 Register object = a1;
2924 Register index = a0;
2925 Register scratch = a2;
2926 Register result = v0;
2927
2928 __ pop(object);
2929
2930 Label need_conversion;
2931 Label index_out_of_range;
2932 Label done;
2933 StringCharCodeAtGenerator generator(object,
2934 index,
2935 scratch,
2936 result,
2937 &need_conversion,
2938 &need_conversion,
2939 &index_out_of_range,
2940 STRING_INDEX_IS_NUMBER);
2941 generator.GenerateFast(masm_);
2942 __ jmp(&done);
2943
2944 __ bind(&index_out_of_range);
2945 // When the index is out of range, the spec requires us to return
2946 // NaN.
2947 __ LoadRoot(result, Heap::kNanValueRootIndex);
2948 __ jmp(&done);
2949
2950 __ bind(&need_conversion);
2951 // Load the undefined value into the result register, which will
2952 // trigger conversion.
2953 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2954 __ jmp(&done);
2955
2956 NopRuntimeCallHelper call_helper;
2957 generator.GenerateSlow(masm_, call_helper);
2958
2959 __ bind(&done);
2960 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002961}
2962
2963
2964void FullCodeGenerator::EmitStringCharAt(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002965 ASSERT(args->length() == 2);
2966
2967 VisitForStackValue(args->at(0));
2968 VisitForAccumulatorValue(args->at(1));
2969 __ mov(a0, result_register());
2970
2971 Register object = a1;
2972 Register index = a0;
2973 Register scratch1 = a2;
2974 Register scratch2 = a3;
2975 Register result = v0;
2976
2977 __ pop(object);
2978
2979 Label need_conversion;
2980 Label index_out_of_range;
2981 Label done;
2982 StringCharAtGenerator generator(object,
2983 index,
2984 scratch1,
2985 scratch2,
2986 result,
2987 &need_conversion,
2988 &need_conversion,
2989 &index_out_of_range,
2990 STRING_INDEX_IS_NUMBER);
2991 generator.GenerateFast(masm_);
2992 __ jmp(&done);
2993
2994 __ bind(&index_out_of_range);
2995 // When the index is out of range, the spec requires us to return
2996 // the empty string.
2997 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
2998 __ jmp(&done);
2999
3000 __ bind(&need_conversion);
3001 // Move smi zero into the result register, which will trigger
3002 // conversion.
3003 __ li(result, Operand(Smi::FromInt(0)));
3004 __ jmp(&done);
3005
3006 NopRuntimeCallHelper call_helper;
3007 generator.GenerateSlow(masm_, call_helper);
3008
3009 __ bind(&done);
3010 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003011}
3012
3013
3014void FullCodeGenerator::EmitStringAdd(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003015 ASSERT_EQ(2, args->length());
3016
3017 VisitForStackValue(args->at(0));
3018 VisitForStackValue(args->at(1));
3019
3020 StringAddStub stub(NO_STRING_ADD_FLAGS);
3021 __ CallStub(&stub);
3022 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003023}
3024
3025
3026void FullCodeGenerator::EmitStringCompare(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003027 ASSERT_EQ(2, args->length());
3028
3029 VisitForStackValue(args->at(0));
3030 VisitForStackValue(args->at(1));
3031
3032 StringCompareStub stub;
3033 __ CallStub(&stub);
3034 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003035}
3036
3037
3038void FullCodeGenerator::EmitMathSin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003039 // Load the argument on the stack and call the stub.
3040 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3041 TranscendentalCacheStub::TAGGED);
3042 ASSERT(args->length() == 1);
3043 VisitForStackValue(args->at(0));
3044 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3045 __ CallStub(&stub);
3046 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003047}
3048
3049
3050void FullCodeGenerator::EmitMathCos(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003051 // Load the argument on the stack and call the stub.
3052 TranscendentalCacheStub stub(TranscendentalCache::COS,
3053 TranscendentalCacheStub::TAGGED);
3054 ASSERT(args->length() == 1);
3055 VisitForStackValue(args->at(0));
3056 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3057 __ CallStub(&stub);
3058 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003059}
3060
3061
3062void FullCodeGenerator::EmitMathLog(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003063 // Load the argument on the stack and call the stub.
3064 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3065 TranscendentalCacheStub::TAGGED);
3066 ASSERT(args->length() == 1);
3067 VisitForStackValue(args->at(0));
3068 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3069 __ CallStub(&stub);
3070 context()->Plug(v0);
3071}
3072
3073
3074void FullCodeGenerator::EmitMathSqrt(ZoneList<Expression*>* args) {
3075 // Load the argument on the stack and call the runtime function.
3076 ASSERT(args->length() == 1);
3077 VisitForStackValue(args->at(0));
3078 __ CallRuntime(Runtime::kMath_sqrt, 1);
3079 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003080}
3081
3082
3083void FullCodeGenerator::EmitCallFunction(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003084 ASSERT(args->length() >= 2);
3085
3086 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3087 for (int i = 0; i < arg_count + 1; i++) {
3088 VisitForStackValue(args->at(i));
3089 }
3090 VisitForAccumulatorValue(args->last()); // Function.
3091
3092 // InvokeFunction requires the function in a1. Move it in there.
3093 __ mov(a1, result_register());
3094 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003095 __ InvokeFunction(a1, count, CALL_FUNCTION,
3096 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003097 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3098 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003099}
3100
3101
3102void FullCodeGenerator::EmitRegExpConstructResult(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003103 RegExpConstructResultStub stub;
3104 ASSERT(args->length() == 3);
3105 VisitForStackValue(args->at(0));
3106 VisitForStackValue(args->at(1));
3107 VisitForStackValue(args->at(2));
3108 __ CallStub(&stub);
3109 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003110}
3111
3112
3113void FullCodeGenerator::EmitSwapElements(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003114 ASSERT(args->length() == 3);
3115 VisitForStackValue(args->at(0));
3116 VisitForStackValue(args->at(1));
3117 VisitForStackValue(args->at(2));
3118 Label done;
3119 Label slow_case;
3120 Register object = a0;
3121 Register index1 = a1;
3122 Register index2 = a2;
3123 Register elements = a3;
3124 Register scratch1 = t0;
3125 Register scratch2 = t1;
3126
3127 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3128 // Fetch the map and check if array is in fast case.
3129 // Check that object doesn't require security checks and
3130 // has no indexed interceptor.
3131 __ GetObjectType(object, scratch1, scratch2);
3132 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3133 // Map is now in scratch1.
3134
3135 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3136 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3137 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3138
3139 // Check the object's elements are in fast case and writable.
3140 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3141 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3142 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3143 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3144
3145 // Check that both indices are smis.
3146 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3147 __ lw(index2, MemOperand(sp, 0));
3148 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3149
3150 // Check that both indices are valid.
3151 Label not_hi;
3152 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3153 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3154 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3155 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3156 __ bind(&not_hi);
3157
3158 // Bring the address of the elements into index1 and index2.
3159 __ Addu(scratch1, elements,
3160 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3161 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3162 __ Addu(index1, scratch1, index1);
3163 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3164 __ Addu(index2, scratch1, index2);
3165
3166 // Swap elements.
3167 __ lw(scratch1, MemOperand(index1, 0));
3168 __ lw(scratch2, MemOperand(index2, 0));
3169 __ sw(scratch1, MemOperand(index2, 0));
3170 __ sw(scratch2, MemOperand(index1, 0));
3171
3172 Label new_space;
3173 __ InNewSpace(elements, scratch1, eq, &new_space);
3174 // Possible optimization: do a check that both values are Smis
3175 // (or them and test against Smi mask).
3176
3177 __ mov(scratch1, elements);
3178 __ RecordWriteHelper(elements, index1, scratch2);
3179 __ RecordWriteHelper(scratch1, index2, scratch2); // scratch1 holds elements.
3180
3181 __ bind(&new_space);
3182 // We are done. Drop elements from the stack, and return undefined.
3183 __ Drop(3);
3184 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3185 __ jmp(&done);
3186
3187 __ bind(&slow_case);
3188 __ CallRuntime(Runtime::kSwapElements, 3);
3189
3190 __ bind(&done);
3191 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003192}
3193
3194
3195void FullCodeGenerator::EmitGetFromCache(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003196 ASSERT_EQ(2, args->length());
3197
3198 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3199 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3200
3201 Handle<FixedArray> jsfunction_result_caches(
3202 isolate()->global_context()->jsfunction_result_caches());
3203 if (jsfunction_result_caches->length() <= cache_id) {
3204 __ Abort("Attempt to use undefined cache.");
3205 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3206 context()->Plug(v0);
3207 return;
3208 }
3209
3210 VisitForAccumulatorValue(args->at(1));
3211
3212 Register key = v0;
3213 Register cache = a1;
3214 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3215 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3216 __ lw(cache,
3217 ContextOperand(
3218 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3219 __ lw(cache,
3220 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3221
3222
3223 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003224 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003225 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3226 // a2 now holds finger offset as a smi.
3227 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3228 // a3 now points to the start of fixed array elements.
3229 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3230 __ addu(a3, a3, at);
3231 // a3 now points to key of indexed element of cache.
3232 __ lw(a2, MemOperand(a3));
3233 __ Branch(&not_found, ne, key, Operand(a2));
3234
3235 __ lw(v0, MemOperand(a3, kPointerSize));
3236 __ Branch(&done);
3237
3238 __ bind(&not_found);
3239 // Call runtime to perform the lookup.
3240 __ Push(cache, key);
3241 __ CallRuntime(Runtime::kGetFromCache, 2);
3242
3243 __ bind(&done);
3244 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003245}
3246
3247
3248void FullCodeGenerator::EmitIsRegExpEquivalent(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003249 ASSERT_EQ(2, args->length());
3250
3251 Register right = v0;
3252 Register left = a1;
3253 Register tmp = a2;
3254 Register tmp2 = a3;
3255
3256 VisitForStackValue(args->at(0));
3257 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3258 __ pop(left);
3259
3260 Label done, fail, ok;
3261 __ Branch(&ok, eq, left, Operand(right));
3262 // Fail if either is a non-HeapObject.
3263 __ And(tmp, left, Operand(right));
3264 __ And(at, tmp, Operand(kSmiTagMask));
3265 __ Branch(&fail, eq, at, Operand(zero_reg));
3266 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3267 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3268 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3269 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3270 __ Branch(&fail, ne, tmp, Operand(tmp2));
3271 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3272 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3273 __ Branch(&ok, eq, tmp, Operand(tmp2));
3274 __ bind(&fail);
3275 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3276 __ jmp(&done);
3277 __ bind(&ok);
3278 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3279 __ bind(&done);
3280
3281 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003282}
3283
3284
3285void FullCodeGenerator::EmitHasCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003286 VisitForAccumulatorValue(args->at(0));
3287
3288 Label materialize_true, materialize_false;
3289 Label* if_true = NULL;
3290 Label* if_false = NULL;
3291 Label* fall_through = NULL;
3292 context()->PrepareTest(&materialize_true, &materialize_false,
3293 &if_true, &if_false, &fall_through);
3294
3295 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3296 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3297
3298 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3299 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3300
3301 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003302}
3303
3304
3305void FullCodeGenerator::EmitGetCachedArrayIndex(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003306 ASSERT(args->length() == 1);
3307 VisitForAccumulatorValue(args->at(0));
3308
3309 if (FLAG_debug_code) {
3310 __ AbortIfNotString(v0);
3311 }
3312
3313 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3314 __ IndexFromHash(v0, v0);
3315
3316 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003317}
3318
3319
3320void FullCodeGenerator::EmitFastAsciiArrayJoin(ZoneList<Expression*>* args) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003321 Label bailout, done, one_char_separator, long_separator,
3322 non_trivial_array, not_size_one_array, loop,
3323 empty_separator_loop, one_char_separator_loop,
3324 one_char_separator_loop_entry, long_separator_loop;
3325
3326 ASSERT(args->length() == 2);
3327 VisitForStackValue(args->at(1));
3328 VisitForAccumulatorValue(args->at(0));
3329
3330 // All aliases of the same register have disjoint lifetimes.
3331 Register array = v0;
3332 Register elements = no_reg; // Will be v0.
3333 Register result = no_reg; // Will be v0.
3334 Register separator = a1;
3335 Register array_length = a2;
3336 Register result_pos = no_reg; // Will be a2.
3337 Register string_length = a3;
3338 Register string = t0;
3339 Register element = t1;
3340 Register elements_end = t2;
3341 Register scratch1 = t3;
3342 Register scratch2 = t5;
3343 Register scratch3 = t4;
3344 Register scratch4 = v1;
3345
3346 // Separator operand is on the stack.
3347 __ pop(separator);
3348
3349 // Check that the array is a JSArray.
3350 __ JumpIfSmi(array, &bailout);
3351 __ GetObjectType(array, scratch1, scratch2);
3352 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3353
3354 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003355 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003356
3357 // If the array has length zero, return the empty string.
3358 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3359 __ SmiUntag(array_length);
3360 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3361 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3362 __ Branch(&done);
3363
3364 __ bind(&non_trivial_array);
3365
3366 // Get the FixedArray containing array's elements.
3367 elements = array;
3368 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3369 array = no_reg; // End of array's live range.
3370
3371 // Check that all array elements are sequential ASCII strings, and
3372 // accumulate the sum of their lengths, as a smi-encoded value.
3373 __ mov(string_length, zero_reg);
3374 __ Addu(element,
3375 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3376 __ sll(elements_end, array_length, kPointerSizeLog2);
3377 __ Addu(elements_end, element, elements_end);
3378 // Loop condition: while (element < elements_end).
3379 // Live values in registers:
3380 // elements: Fixed array of strings.
3381 // array_length: Length of the fixed array of strings (not smi)
3382 // separator: Separator string
3383 // string_length: Accumulated sum of string lengths (smi).
3384 // element: Current array element.
3385 // elements_end: Array end.
3386 if (FLAG_debug_code) {
3387 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3388 array_length, Operand(zero_reg));
3389 }
3390 __ bind(&loop);
3391 __ lw(string, MemOperand(element));
3392 __ Addu(element, element, kPointerSize);
3393 __ JumpIfSmi(string, &bailout);
3394 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3395 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3396 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3397 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3398 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3399 __ BranchOnOverflow(&bailout, scratch3);
3400 __ Branch(&loop, lt, element, Operand(elements_end));
3401
3402 // If array_length is 1, return elements[0], a string.
3403 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3404 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3405 __ Branch(&done);
3406
3407 __ bind(&not_size_one_array);
3408
3409 // Live values in registers:
3410 // separator: Separator string
3411 // array_length: Length of the array.
3412 // string_length: Sum of string lengths (smi).
3413 // elements: FixedArray of strings.
3414
3415 // Check that the separator is a flat ASCII string.
3416 __ JumpIfSmi(separator, &bailout);
3417 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3418 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3419 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3420
3421 // Add (separator length times array_length) - separator length to the
3422 // string_length to get the length of the result string. array_length is not
3423 // smi but the other values are, so the result is a smi.
3424 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3425 __ Subu(string_length, string_length, Operand(scratch1));
3426 __ Mult(array_length, scratch1);
3427 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3428 // zero.
3429 __ mfhi(scratch2);
3430 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3431 __ mflo(scratch2);
3432 __ And(scratch3, scratch2, Operand(0x80000000));
3433 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3434 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3435 __ BranchOnOverflow(&bailout, scratch3);
3436 __ SmiUntag(string_length);
3437
3438 // Get first element in the array to free up the elements register to be used
3439 // for the result.
3440 __ Addu(element,
3441 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3442 result = elements; // End of live range for elements.
3443 elements = no_reg;
3444 // Live values in registers:
3445 // element: First array element
3446 // separator: Separator string
3447 // string_length: Length of result string (not smi)
3448 // array_length: Length of the array.
3449 __ AllocateAsciiString(result,
3450 string_length,
3451 scratch1,
3452 scratch2,
3453 elements_end,
3454 &bailout);
3455 // Prepare for looping. Set up elements_end to end of the array. Set
3456 // result_pos to the position of the result where to write the first
3457 // character.
3458 __ sll(elements_end, array_length, kPointerSizeLog2);
3459 __ Addu(elements_end, element, elements_end);
3460 result_pos = array_length; // End of live range for array_length.
3461 array_length = no_reg;
3462 __ Addu(result_pos,
3463 result,
3464 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3465
3466 // Check the length of the separator.
3467 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3468 __ li(at, Operand(Smi::FromInt(1)));
3469 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3470 __ Branch(&long_separator, gt, scratch1, Operand(at));
3471
3472 // Empty separator case.
3473 __ bind(&empty_separator_loop);
3474 // Live values in registers:
3475 // result_pos: the position to which we are currently copying characters.
3476 // element: Current array element.
3477 // elements_end: Array end.
3478
3479 // Copy next array element to the result.
3480 __ lw(string, MemOperand(element));
3481 __ Addu(element, element, kPointerSize);
3482 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3483 __ SmiUntag(string_length);
3484 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3485 __ CopyBytes(string, result_pos, string_length, scratch1);
3486 // End while (element < elements_end).
3487 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3488 ASSERT(result.is(v0));
3489 __ Branch(&done);
3490
3491 // One-character separator case.
3492 __ bind(&one_char_separator);
3493 // Replace separator with its ascii character value.
3494 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3495 // Jump into the loop after the code that copies the separator, so the first
3496 // element is not preceded by a separator.
3497 __ jmp(&one_char_separator_loop_entry);
3498
3499 __ bind(&one_char_separator_loop);
3500 // Live values in registers:
3501 // result_pos: the position to which we are currently copying characters.
3502 // element: Current array element.
3503 // elements_end: Array end.
3504 // separator: Single separator ascii char (in lower byte).
3505
3506 // Copy the separator character to the result.
3507 __ sb(separator, MemOperand(result_pos));
3508 __ Addu(result_pos, result_pos, 1);
3509
3510 // Copy next array element to the result.
3511 __ bind(&one_char_separator_loop_entry);
3512 __ lw(string, MemOperand(element));
3513 __ Addu(element, element, kPointerSize);
3514 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3515 __ SmiUntag(string_length);
3516 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3517 __ CopyBytes(string, result_pos, string_length, scratch1);
3518 // End while (element < elements_end).
3519 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3520 ASSERT(result.is(v0));
3521 __ Branch(&done);
3522
3523 // Long separator case (separator is more than one character). Entry is at the
3524 // label long_separator below.
3525 __ bind(&long_separator_loop);
3526 // Live values in registers:
3527 // result_pos: the position to which we are currently copying characters.
3528 // element: Current array element.
3529 // elements_end: Array end.
3530 // separator: Separator string.
3531
3532 // Copy the separator to the result.
3533 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3534 __ SmiUntag(string_length);
3535 __ Addu(string,
3536 separator,
3537 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3538 __ CopyBytes(string, result_pos, string_length, scratch1);
3539
3540 __ bind(&long_separator);
3541 __ lw(string, MemOperand(element));
3542 __ Addu(element, element, kPointerSize);
3543 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3544 __ SmiUntag(string_length);
3545 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3546 __ CopyBytes(string, result_pos, string_length, scratch1);
3547 // End while (element < elements_end).
3548 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3549 ASSERT(result.is(v0));
3550 __ Branch(&done);
3551
3552 __ bind(&bailout);
3553 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3554 __ bind(&done);
3555 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003556}
3557
3558
ager@chromium.org5c838252010-02-19 08:53:10 +00003559void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003560 Handle<String> name = expr->name();
3561 if (name->length() > 0 && name->Get(0) == '_') {
3562 Comment cmnt(masm_, "[ InlineRuntimeCall");
3563 EmitInlineRuntimeCall(expr);
3564 return;
3565 }
3566
3567 Comment cmnt(masm_, "[ CallRuntime");
3568 ZoneList<Expression*>* args = expr->arguments();
3569
3570 if (expr->is_jsruntime()) {
3571 // Prepare for calling JS runtime function.
3572 __ lw(a0, GlobalObjectOperand());
3573 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3574 __ push(a0);
3575 }
3576
3577 // Push the arguments ("left-to-right").
3578 int arg_count = args->length();
3579 for (int i = 0; i < arg_count; i++) {
3580 VisitForStackValue(args->at(i));
3581 }
3582
3583 if (expr->is_jsruntime()) {
3584 // Call the JS runtime function.
3585 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003586 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003587 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003588 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003589 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003590 // Restore context register.
3591 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3592 } else {
3593 // Call the C runtime function.
3594 __ CallRuntime(expr->function(), arg_count);
3595 }
3596 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003597}
3598
3599
3600void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003601 switch (expr->op()) {
3602 case Token::DELETE: {
3603 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003604 Property* property = expr->expression()->AsProperty();
3605 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003606
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003607 if (property != NULL) {
3608 VisitForStackValue(property->obj());
3609 VisitForStackValue(property->key());
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003610 __ li(a1, Operand(Smi::FromInt(strict_mode_flag())));
3611 __ push(a1);
3612 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3613 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003614 } else if (proxy != NULL) {
3615 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003616 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003617 // but "delete this" is allowed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003618 ASSERT(strict_mode_flag() == kNonStrictMode || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003619 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003620 __ lw(a2, GlobalObjectOperand());
3621 __ li(a1, Operand(var->name()));
3622 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3623 __ Push(a2, a1, a0);
3624 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3625 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003626 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003627 // Result of deleting non-global, non-dynamic variables is false.
3628 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003629 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003630 } else {
3631 // Non-global variable. Call the runtime to try to delete from the
3632 // context where the variable was introduced.
3633 __ push(context_register());
3634 __ li(a2, Operand(var->name()));
3635 __ push(a2);
3636 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3637 context()->Plug(v0);
3638 }
3639 } else {
3640 // Result of deleting non-property, non-variable reference is true.
3641 // The subexpression may have side effects.
3642 VisitForEffect(expr->expression());
3643 context()->Plug(true);
3644 }
3645 break;
3646 }
3647
3648 case Token::VOID: {
3649 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3650 VisitForEffect(expr->expression());
3651 context()->Plug(Heap::kUndefinedValueRootIndex);
3652 break;
3653 }
3654
3655 case Token::NOT: {
3656 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3657 if (context()->IsEffect()) {
3658 // Unary NOT has no side effects so it's only necessary to visit the
3659 // subexpression. Match the optimizing compiler by not branching.
3660 VisitForEffect(expr->expression());
3661 } else {
3662 Label materialize_true, materialize_false;
3663 Label* if_true = NULL;
3664 Label* if_false = NULL;
3665 Label* fall_through = NULL;
3666
3667 // Notice that the labels are swapped.
3668 context()->PrepareTest(&materialize_true, &materialize_false,
3669 &if_false, &if_true, &fall_through);
3670 if (context()->IsTest()) ForwardBailoutToChild(expr);
3671 VisitForControl(expr->expression(), if_true, if_false, fall_through);
3672 context()->Plug(if_false, if_true); // Labels swapped.
3673 }
3674 break;
3675 }
3676
3677 case Token::TYPEOF: {
3678 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3679 { StackValueContext context(this);
3680 VisitForTypeofValue(expr->expression());
3681 }
3682 __ CallRuntime(Runtime::kTypeof, 1);
3683 context()->Plug(v0);
3684 break;
3685 }
3686
3687 case Token::ADD: {
3688 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3689 VisitForAccumulatorValue(expr->expression());
3690 Label no_conversion;
3691 __ JumpIfSmi(result_register(), &no_conversion);
3692 __ mov(a0, result_register());
3693 ToNumberStub convert_stub;
3694 __ CallStub(&convert_stub);
3695 __ bind(&no_conversion);
3696 context()->Plug(result_register());
3697 break;
3698 }
3699
3700 case Token::SUB:
3701 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3702 break;
3703
3704 case Token::BIT_NOT:
3705 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3706 break;
3707
3708 default:
3709 UNREACHABLE();
3710 }
3711}
3712
3713
3714void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3715 const char* comment) {
3716 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3717 Comment cmt(masm_, comment);
3718 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3719 UnaryOverwriteMode overwrite =
3720 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003721 UnaryOpStub stub(expr->op(), overwrite);
3722 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003723 VisitForAccumulatorValue(expr->expression());
3724 SetSourcePosition(expr->position());
3725 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003726 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003727 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003728}
3729
3730
3731void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003732 Comment cmnt(masm_, "[ CountOperation");
3733 SetSourcePosition(expr->position());
3734
3735 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3736 // as the left-hand side.
3737 if (!expr->expression()->IsValidLeftHandSide()) {
3738 VisitForEffect(expr->expression());
3739 return;
3740 }
3741
3742 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003743 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003744 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3745 LhsKind assign_type = VARIABLE;
3746 Property* prop = expr->expression()->AsProperty();
3747 // In case of a property we use the uninitialized expression context
3748 // of the key to detect a named property.
3749 if (prop != NULL) {
3750 assign_type =
3751 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3752 }
3753
3754 // Evaluate expression and get value.
3755 if (assign_type == VARIABLE) {
3756 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3757 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003758 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003759 } else {
3760 // Reserve space for result of postfix operation.
3761 if (expr->is_postfix() && !context()->IsEffect()) {
3762 __ li(at, Operand(Smi::FromInt(0)));
3763 __ push(at);
3764 }
3765 if (assign_type == NAMED_PROPERTY) {
3766 // Put the object both on the stack and in the accumulator.
3767 VisitForAccumulatorValue(prop->obj());
3768 __ push(v0);
3769 EmitNamedPropertyLoad(prop);
3770 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003771 VisitForStackValue(prop->obj());
3772 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003773 __ lw(a1, MemOperand(sp, 0));
3774 __ push(v0);
3775 EmitKeyedPropertyLoad(prop);
3776 }
3777 }
3778
3779 // We need a second deoptimization point after loading the value
3780 // in case evaluating the property load my have a side effect.
3781 if (assign_type == VARIABLE) {
3782 PrepareForBailout(expr->expression(), TOS_REG);
3783 } else {
3784 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3785 }
3786
3787 // Call ToNumber only if operand is not a smi.
3788 Label no_conversion;
3789 __ JumpIfSmi(v0, &no_conversion);
3790 __ mov(a0, v0);
3791 ToNumberStub convert_stub;
3792 __ CallStub(&convert_stub);
3793 __ bind(&no_conversion);
3794
3795 // Save result for postfix expressions.
3796 if (expr->is_postfix()) {
3797 if (!context()->IsEffect()) {
3798 // Save the result on the stack. If we have a named or keyed property
3799 // we store the result under the receiver that is currently on top
3800 // of the stack.
3801 switch (assign_type) {
3802 case VARIABLE:
3803 __ push(v0);
3804 break;
3805 case NAMED_PROPERTY:
3806 __ sw(v0, MemOperand(sp, kPointerSize));
3807 break;
3808 case KEYED_PROPERTY:
3809 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
3810 break;
3811 }
3812 }
3813 }
3814 __ mov(a0, result_register());
3815
3816 // Inline smi case if we are in a loop.
3817 Label stub_call, done;
3818 JumpPatchSite patch_site(masm_);
3819
3820 int count_value = expr->op() == Token::INC ? 1 : -1;
3821 __ li(a1, Operand(Smi::FromInt(count_value)));
3822
3823 if (ShouldInlineSmiCase(expr->op())) {
3824 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
3825 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
3826
3827 // We could eliminate this smi check if we split the code at
3828 // the first smi check before calling ToNumber.
3829 patch_site.EmitJumpIfSmi(v0, &done);
3830 __ bind(&stub_call);
3831 }
3832
3833 // Record position before stub call.
3834 SetSourcePosition(expr->position());
3835
danno@chromium.org40cb8782011-05-25 07:58:50 +00003836 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003837 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003838 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003839 __ bind(&done);
3840
3841 // Store the value returned in v0.
3842 switch (assign_type) {
3843 case VARIABLE:
3844 if (expr->is_postfix()) {
3845 { EffectContext context(this);
3846 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3847 Token::ASSIGN);
3848 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3849 context.Plug(v0);
3850 }
3851 // For all contexts except EffectConstant we have the result on
3852 // top of the stack.
3853 if (!context()->IsEffect()) {
3854 context()->PlugTOS();
3855 }
3856 } else {
3857 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3858 Token::ASSIGN);
3859 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3860 context()->Plug(v0);
3861 }
3862 break;
3863 case NAMED_PROPERTY: {
3864 __ mov(a0, result_register()); // Value.
3865 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
3866 __ pop(a1); // Receiver.
3867 Handle<Code> ic = is_strict_mode()
3868 ? isolate()->builtins()->StoreIC_Initialize_Strict()
3869 : isolate()->builtins()->StoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003870 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003871 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3872 if (expr->is_postfix()) {
3873 if (!context()->IsEffect()) {
3874 context()->PlugTOS();
3875 }
3876 } else {
3877 context()->Plug(v0);
3878 }
3879 break;
3880 }
3881 case KEYED_PROPERTY: {
3882 __ mov(a0, result_register()); // Value.
3883 __ pop(a1); // Key.
3884 __ pop(a2); // Receiver.
3885 Handle<Code> ic = is_strict_mode()
3886 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
3887 : isolate()->builtins()->KeyedStoreIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003888 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003889 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3890 if (expr->is_postfix()) {
3891 if (!context()->IsEffect()) {
3892 context()->PlugTOS();
3893 }
3894 } else {
3895 context()->Plug(v0);
3896 }
3897 break;
3898 }
3899 }
ager@chromium.org5c838252010-02-19 08:53:10 +00003900}
3901
3902
lrn@chromium.org7516f052011-03-30 08:52:27 +00003903void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003904 ASSERT(!context()->IsEffect());
3905 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003906 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003907 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003908 Comment cmnt(masm_, "Global variable");
3909 __ lw(a0, GlobalObjectOperand());
3910 __ li(a2, Operand(proxy->name()));
3911 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
3912 // Use a regular load, not a contextual load, to avoid a reference
3913 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003914 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003915 PrepareForBailout(expr, TOS_REG);
3916 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003917 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003918 Label done, slow;
3919
3920 // Generate code for loading from variables potentially shadowed
3921 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003922 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003923
3924 __ bind(&slow);
3925 __ li(a0, Operand(proxy->name()));
3926 __ Push(cp, a0);
3927 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
3928 PrepareForBailout(expr, TOS_REG);
3929 __ bind(&done);
3930
3931 context()->Plug(v0);
3932 } else {
3933 // This expression cannot throw a reference error at the top level.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003934 VisitInCurrentContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003935 }
ager@chromium.org5c838252010-02-19 08:53:10 +00003936}
3937
ager@chromium.org04921a82011-06-27 13:21:41 +00003938void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003939 Handle<String> check) {
3940 Label materialize_true, materialize_false;
3941 Label* if_true = NULL;
3942 Label* if_false = NULL;
3943 Label* fall_through = NULL;
3944 context()->PrepareTest(&materialize_true, &materialize_false,
3945 &if_true, &if_false, &fall_through);
3946
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003947 { AccumulatorValueContext context(this);
ager@chromium.org04921a82011-06-27 13:21:41 +00003948 VisitForTypeofValue(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003949 }
3950 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
3951
3952 if (check->Equals(isolate()->heap()->number_symbol())) {
3953 __ JumpIfSmi(v0, if_true);
3954 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
3955 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3956 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
3957 } else if (check->Equals(isolate()->heap()->string_symbol())) {
3958 __ JumpIfSmi(v0, if_false);
3959 // Check for undetectable objects => false.
3960 __ GetObjectType(v0, v0, a1);
3961 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
3962 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
3963 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
3964 Split(eq, a1, Operand(zero_reg),
3965 if_true, if_false, fall_through);
3966 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
3967 __ LoadRoot(at, Heap::kTrueValueRootIndex);
3968 __ Branch(if_true, eq, v0, Operand(at));
3969 __ LoadRoot(at, Heap::kFalseValueRootIndex);
3970 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00003971 } else if (FLAG_harmony_typeof &&
3972 check->Equals(isolate()->heap()->null_symbol())) {
3973 __ LoadRoot(at, Heap::kNullValueRootIndex);
3974 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003975 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
3976 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
3977 __ Branch(if_true, eq, v0, Operand(at));
3978 __ JumpIfSmi(v0, if_false);
3979 // Check for undetectable objects => true.
3980 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
3981 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
3982 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
3983 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
3984 } else if (check->Equals(isolate()->heap()->function_symbol())) {
3985 __ JumpIfSmi(v0, if_false);
3986 __ GetObjectType(v0, a1, v0); // Leave map in a1.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003987 Split(ge, v0, Operand(FIRST_CALLABLE_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003988 if_true, if_false, fall_through);
3989
3990 } else if (check->Equals(isolate()->heap()->object_symbol())) {
3991 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00003992 if (!FLAG_harmony_typeof) {
3993 __ LoadRoot(at, Heap::kNullValueRootIndex);
3994 __ Branch(if_true, eq, v0, Operand(at));
3995 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003996 // Check for JS objects => true.
3997 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003998 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003999 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004000 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004001 // Check for undetectable objects => false.
4002 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4003 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4004 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4005 } else {
4006 if (if_false != fall_through) __ jmp(if_false);
4007 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004008 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004009}
4010
4011
ager@chromium.org5c838252010-02-19 08:53:10 +00004012void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004013 Comment cmnt(masm_, "[ CompareOperation");
4014 SetSourcePosition(expr->position());
4015
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004016 // First we try a fast inlined version of the compare when one of
4017 // the operands is a literal.
4018 if (TryLiteralCompare(expr)) return;
4019
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004020 // Always perform the comparison for its control flow. Pack the result
4021 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004022 Label materialize_true, materialize_false;
4023 Label* if_true = NULL;
4024 Label* if_false = NULL;
4025 Label* fall_through = NULL;
4026 context()->PrepareTest(&materialize_true, &materialize_false,
4027 &if_true, &if_false, &fall_through);
4028
ager@chromium.org04921a82011-06-27 13:21:41 +00004029 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004030 VisitForStackValue(expr->left());
4031 switch (op) {
4032 case Token::IN:
4033 VisitForStackValue(expr->right());
4034 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
4035 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
4036 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4037 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4038 break;
4039
4040 case Token::INSTANCEOF: {
4041 VisitForStackValue(expr->right());
4042 InstanceofStub stub(InstanceofStub::kNoFlags);
4043 __ CallStub(&stub);
4044 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4045 // The stub returns 0 for true.
4046 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4047 break;
4048 }
4049
4050 default: {
4051 VisitForAccumulatorValue(expr->right());
4052 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004053 switch (op) {
4054 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004055 case Token::EQ:
4056 cc = eq;
4057 __ mov(a0, result_register());
4058 __ pop(a1);
4059 break;
4060 case Token::LT:
4061 cc = lt;
4062 __ mov(a0, result_register());
4063 __ pop(a1);
4064 break;
4065 case Token::GT:
4066 // Reverse left and right sides to obtain ECMA-262 conversion order.
4067 cc = lt;
4068 __ mov(a1, result_register());
4069 __ pop(a0);
4070 break;
4071 case Token::LTE:
4072 // Reverse left and right sides to obtain ECMA-262 conversion order.
4073 cc = ge;
4074 __ mov(a1, result_register());
4075 __ pop(a0);
4076 break;
4077 case Token::GTE:
4078 cc = ge;
4079 __ mov(a0, result_register());
4080 __ pop(a1);
4081 break;
4082 case Token::IN:
4083 case Token::INSTANCEOF:
4084 default:
4085 UNREACHABLE();
4086 }
4087
4088 bool inline_smi_code = ShouldInlineSmiCase(op);
4089 JumpPatchSite patch_site(masm_);
4090 if (inline_smi_code) {
4091 Label slow_case;
4092 __ Or(a2, a0, Operand(a1));
4093 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4094 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4095 __ bind(&slow_case);
4096 }
4097 // Record position and call the compare IC.
4098 SetSourcePosition(expr->position());
4099 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004100 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004101 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004102 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
4103 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4104 }
4105 }
4106
4107 // Convert the result of the comparison into one expected for this
4108 // expression's context.
4109 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004110}
4111
4112
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004113void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4114 Expression* sub_expr,
4115 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004116 Label materialize_true, materialize_false;
4117 Label* if_true = NULL;
4118 Label* if_false = NULL;
4119 Label* fall_through = NULL;
4120 context()->PrepareTest(&materialize_true, &materialize_false,
4121 &if_true, &if_false, &fall_through);
4122
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004123 VisitForAccumulatorValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004124 PrepareForBailoutBeforeSplit(TOS_REG, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004125 Heap::RootListIndex nil_value = nil == kNullValue ?
4126 Heap::kNullValueRootIndex :
4127 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004128 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004129 __ LoadRoot(a1, nil_value);
4130 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004131 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4132 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004133 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4134 Heap::kUndefinedValueRootIndex :
4135 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004136 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004137 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004138 __ Branch(if_true, eq, a0, Operand(a1));
4139 __ And(at, a0, Operand(kSmiTagMask));
4140 __ Branch(if_false, eq, at, Operand(zero_reg));
4141 // It can be an undetectable object.
4142 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4143 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4144 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4145 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4146 }
4147 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004148}
4149
4150
ager@chromium.org5c838252010-02-19 08:53:10 +00004151void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004152 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4153 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004154}
4155
4156
lrn@chromium.org7516f052011-03-30 08:52:27 +00004157Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004158 return v0;
4159}
ager@chromium.org5c838252010-02-19 08:53:10 +00004160
4161
lrn@chromium.org7516f052011-03-30 08:52:27 +00004162Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004163 return cp;
4164}
4165
4166
ager@chromium.org5c838252010-02-19 08:53:10 +00004167void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004168 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4169 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004170}
4171
4172
4173void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004174 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004175}
4176
4177
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004178void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4179 Scope* declaration_scope = scope()->DeclarationScope();
4180 if (declaration_scope->is_global_scope()) {
4181 // Contexts nested in the global context have a canonical empty function
4182 // as their closure, not the anonymous closure containing the global
4183 // code. Pass a smi sentinel and let the runtime look up the empty
4184 // function.
4185 __ li(at, Operand(Smi::FromInt(0)));
4186 } else if (declaration_scope->is_eval_scope()) {
4187 // Contexts created by a call to eval have the same closure as the
4188 // context calling eval, not the anonymous closure containing the eval
4189 // code. Fetch it from the context.
4190 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4191 } else {
4192 ASSERT(declaration_scope->is_function_scope());
4193 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4194 }
4195 __ push(at);
4196}
4197
4198
ager@chromium.org5c838252010-02-19 08:53:10 +00004199// ----------------------------------------------------------------------------
4200// Non-local control flow support.
4201
4202void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004203 ASSERT(!result_register().is(a1));
4204 // Store result register while executing finally block.
4205 __ push(result_register());
4206 // Cook return address in link register to stack (smi encoded Code* delta).
4207 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4208 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004209 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004210 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4211 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004212}
4213
4214
4215void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004216 ASSERT(!result_register().is(a1));
4217 // Restore result register from stack.
4218 __ pop(a1);
4219 // Uncook return address and return.
4220 __ pop(result_register());
4221 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4222 __ sra(a1, a1, 1); // Un-smi-tag value.
4223 __ Addu(at, a1, Operand(masm_->CodeObject()));
4224 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004225}
4226
4227
4228#undef __
4229
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004230#define __ ACCESS_MASM(masm())
4231
4232FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4233 int* stack_depth,
4234 int* context_length) {
4235 // The macros used here must preserve the result register.
4236
4237 // Because the handler block contains the context of the finally
4238 // code, we can restore it directly from there for the finally code
4239 // rather than iteratively unwinding contexts via their previous
4240 // links.
4241 __ Drop(*stack_depth); // Down to the handler block.
4242 if (*context_length > 0) {
4243 // Restore the context to its dedicated register and the stack.
4244 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4245 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4246 }
4247 __ PopTryHandler();
4248 __ Call(finally_entry_);
4249
4250 *stack_depth = 0;
4251 *context_length = 0;
4252 return previous_;
4253}
4254
4255
4256#undef __
4257
ager@chromium.org5c838252010-02-19 08:53:10 +00004258} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004259
4260#endif // V8_TARGET_ARCH_MIPS