blob: c4341dc375a3c33a1bfd314e11153225620cab17 [file] [log] [blame]
ulan@chromium.org65a89c22012-02-14 11:46:07 +00001// Copyright 2012 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"
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000045#include "isolate-inl.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000046#include "parser.h"
lrn@chromium.org7516f052011-03-30 08:52:27 +000047#include "scopes.h"
48#include "stub-cache.h"
49
50#include "mips/code-stubs-mips.h"
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +000051#include "mips/macro-assembler-mips.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000052
53namespace v8 {
54namespace internal {
55
56#define __ ACCESS_MASM(masm_)
57
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000058
59// A patch site is a location in the code which it is possible to patch. This
60// class has a number of methods to emit the code which is patchable and the
61// method EmitPatchInfo to record a marker back to the patchable code. This
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000062// marker is a andi zero_reg, rx, #yyyy instruction, and rx * 0x0000ffff + yyyy
63// (raw 16 bit immediate value is used) is the delta from the pc to the first
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000064// instruction of the patchable code.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000065// The marker instruction is effectively a NOP (dest is zero_reg) and will
66// never be emitted by normal code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000067class JumpPatchSite BASE_EMBEDDED {
68 public:
69 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
70#ifdef DEBUG
71 info_emitted_ = false;
72#endif
73 }
74
75 ~JumpPatchSite() {
76 ASSERT(patch_site_.is_bound() == info_emitted_);
77 }
78
79 // When initially emitting this ensure that a jump is always generated to skip
80 // the inlined smi code.
81 void EmitJumpIfNotSmi(Register reg, Label* target) {
82 ASSERT(!patch_site_.is_bound() && !info_emitted_);
83 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
84 __ bind(&patch_site_);
85 __ andi(at, reg, 0);
86 // Always taken before patched.
87 __ Branch(target, eq, at, Operand(zero_reg));
88 }
89
90 // When initially emitting this ensure that a jump is never generated to skip
91 // the inlined smi code.
92 void EmitJumpIfSmi(Register reg, Label* target) {
93 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
94 ASSERT(!patch_site_.is_bound() && !info_emitted_);
95 __ bind(&patch_site_);
96 __ andi(at, reg, 0);
97 // Never taken before patched.
98 __ Branch(target, ne, at, Operand(zero_reg));
99 }
100
101 void EmitPatchInfo() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000102 if (patch_site_.is_bound()) {
103 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
104 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000105 __ andi(zero_reg, reg, delta_to_patch_site % kImm16Mask);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000106#ifdef DEBUG
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000107 info_emitted_ = true;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000108#endif
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000109 } else {
110 __ nop(); // Signals no inlined code.
111 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000112 }
113
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000114 private:
115 MacroAssembler* masm_;
116 Label patch_site_;
117#ifdef DEBUG
118 bool info_emitted_;
119#endif
120};
121
122
lrn@chromium.org7516f052011-03-30 08:52:27 +0000123// Generate code for a JS function. On entry to the function the receiver
124// and arguments have been pushed on the stack left to right. The actual
125// argument count matches the formal parameter count expected by the
126// function.
127//
128// The live registers are:
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000129// o a1: the JS function object being called (i.e. ourselves)
lrn@chromium.org7516f052011-03-30 08:52:27 +0000130// o cp: our context
131// o fp: our caller's frame pointer
132// o sp: stack pointer
133// o ra: return address
134//
135// The function builds a JS frame. Please see JavaScriptFrameConstants in
136// frames-mips.h for its layout.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000137void FullCodeGenerator::Generate() {
138 CompilationInfo* info = info_;
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +0000139 handler_table_ =
140 isolate()->factory()->NewFixedArray(function()->handler_count(), TENURED);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000141 profiling_counter_ = isolate()->factory()->NewJSGlobalPropertyCell(
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000142 Handle<Smi>(Smi::FromInt(FLAG_interrupt_budget), isolate()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000143 SetFunctionPosition(function());
144 Comment cmnt(masm_, "[ function compiled by full code generator");
145
danno@chromium.org129d3982012-07-25 15:01:47 +0000146 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
147
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000148#ifdef DEBUG
149 if (strlen(FLAG_stop_at) > 0 &&
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000150 info->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000151 __ stop("stop-at");
152 }
153#endif
154
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000155 // Strict mode functions and builtins need to replace the receiver
156 // with undefined when called as functions (without an explicit
157 // receiver object). t1 is zero for method calls and non-zero for
158 // function calls.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000159 if (!info->is_classic_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000160 Label ok;
161 __ Branch(&ok, eq, t1, Operand(zero_reg));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000162 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000163 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
164 __ sw(a2, MemOperand(sp, receiver_offset));
165 __ bind(&ok);
166 }
167
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000168 // Open a frame scope to indicate that there is a frame on the stack. The
169 // MANUAL indicates that the scope shouldn't actually generate code to set up
170 // the frame (that is done below).
171 FrameScope frame_scope(masm_, StackFrame::MANUAL);
172
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000173 int locals_count = info->scope()->num_stack_slots();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000174
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +0000175 info->set_prologue_offset(masm_->pc_offset());
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000176 // The following three instructions must remain together and unmodified for
177 // code aging to work properly.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000178 __ Push(ra, fp, cp, a1);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000179 // Load undefined value here, so the value is ready for the loop
180 // below.
181 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000182 // Adjust fp to point to caller's fp.
183 __ Addu(fp, sp, Operand(2 * kPointerSize));
184
185 { Comment cmnt(masm_, "[ Allocate locals");
186 for (int i = 0; i < locals_count; i++) {
187 __ push(at);
188 }
189 }
190
191 bool function_in_register = true;
192
193 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000194 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000195 if (heap_slots > 0) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000196 Comment cmnt(masm_, "[ Allocate context");
197 // Argument to NewContext is the function, which is still in a1.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000198 __ push(a1);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000199 if (FLAG_harmony_scoping && info->scope()->is_global_scope()) {
200 __ Push(info->scope()->GetScopeInfo());
201 __ CallRuntime(Runtime::kNewGlobalContext, 2);
202 } else if (heap_slots <= FastNewContextStub::kMaximumSlots) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000203 FastNewContextStub stub(heap_slots);
204 __ CallStub(&stub);
205 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000206 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000207 }
208 function_in_register = false;
209 // Context is returned in both v0 and cp. It replaces the context
210 // passed to us. It's saved in the stack and kept live in cp.
211 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
212 // Copy any necessary parameters into the context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000213 int num_parameters = info->scope()->num_parameters();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000214 for (int i = 0; i < num_parameters; i++) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000215 Variable* var = scope()->parameter(i);
216 if (var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000217 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
218 (num_parameters - 1 - i) * kPointerSize;
219 // Load parameter from stack.
220 __ lw(a0, MemOperand(fp, parameter_offset));
221 // Store it in the context.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000222 MemOperand target = ContextOperand(cp, var->index());
223 __ sw(a0, target);
224
225 // Update the write barrier.
226 __ RecordWriteContextSlot(
227 cp, target.offset(), a0, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000228 }
229 }
230 }
231
232 Variable* arguments = scope()->arguments();
233 if (arguments != NULL) {
234 // Function uses arguments object.
235 Comment cmnt(masm_, "[ Allocate arguments object");
236 if (!function_in_register) {
237 // Load this again, if it's used by the local context below.
238 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
239 } else {
240 __ mov(a3, a1);
241 }
242 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000243 int num_parameters = info->scope()->num_parameters();
244 int offset = num_parameters * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000245 __ Addu(a2, fp,
246 Operand(StandardFrameConstants::kCallerSPOffset + offset));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000247 __ li(a1, Operand(Smi::FromInt(num_parameters)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000248 __ Push(a3, a2, a1);
249
250 // Arguments to ArgumentsAccessStub:
251 // function, receiver address, parameter count.
252 // The stub will rewrite receiever and parameter count if the previous
253 // stack frame was an arguments adapter frame.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000254 ArgumentsAccessStub::Type type;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000255 if (!is_classic_mode()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000256 type = ArgumentsAccessStub::NEW_STRICT;
257 } else if (function()->has_duplicate_parameters()) {
258 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
259 } else {
260 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
261 }
262 ArgumentsAccessStub stub(type);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000263 __ CallStub(&stub);
264
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000265 SetVar(arguments, v0, a1, a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000266 }
267
268 if (FLAG_trace) {
269 __ CallRuntime(Runtime::kTraceEnter, 0);
270 }
271
272 // Visit the declarations and body unless there is an illegal
273 // redeclaration.
274 if (scope()->HasIllegalRedeclaration()) {
275 Comment cmnt(masm_, "[ Declarations");
276 scope()->VisitIllegalRedeclaration(this);
277
278 } else {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000279 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000280 { Comment cmnt(masm_, "[ Declarations");
281 // For named function expressions, declare the function name as a
282 // constant.
283 if (scope()->is_function_scope() && scope()->function() != NULL) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000284 VariableDeclaration* function = scope()->function();
285 ASSERT(function->proxy()->var()->mode() == CONST ||
286 function->proxy()->var()->mode() == CONST_HARMONY);
287 ASSERT(function->proxy()->var()->location() != Variable::UNALLOCATED);
288 VisitVariableDeclaration(function);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000289 }
290 VisitDeclarations(scope()->declarations());
291 }
292
293 { Comment cmnt(masm_, "[ Stack check");
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000294 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000295 Label ok;
296 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
297 __ Branch(&ok, hs, sp, Operand(t0));
298 StackCheckStub stub;
299 __ CallStub(&stub);
300 __ bind(&ok);
301 }
302
303 { Comment cmnt(masm_, "[ Body");
304 ASSERT(loop_depth() == 0);
305 VisitStatements(function()->body());
306 ASSERT(loop_depth() == 0);
307 }
308 }
309
310 // Always emit a 'return undefined' in case control fell off the end of
311 // the body.
312 { Comment cmnt(masm_, "[ return <undefined>;");
313 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
314 }
315 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000316}
317
318
319void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000320 ASSERT(Smi::FromInt(0) == 0);
321 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000322}
323
324
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000325void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
326 __ li(a2, Operand(profiling_counter_));
327 __ lw(a3, FieldMemOperand(a2, JSGlobalPropertyCell::kValueOffset));
328 __ Subu(a3, a3, Operand(Smi::FromInt(delta)));
329 __ sw(a3, FieldMemOperand(a2, JSGlobalPropertyCell::kValueOffset));
330}
331
332
333void FullCodeGenerator::EmitProfilingCounterReset() {
334 int reset_value = FLAG_interrupt_budget;
335 if (info_->ShouldSelfOptimize() && !FLAG_retry_self_opt) {
336 // Self-optimization is a one-off thing: if it fails, don't try again.
337 reset_value = Smi::kMaxValue;
338 }
339 if (isolate()->IsDebuggerActive()) {
340 // Detect debug break requests as soon as possible.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000341 reset_value = FLAG_interrupt_budget >> 4;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000342 }
343 __ li(a2, Operand(profiling_counter_));
344 __ li(a3, Operand(Smi::FromInt(reset_value)));
345 __ sw(a3, FieldMemOperand(a2, JSGlobalPropertyCell::kValueOffset));
346}
347
348
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000349void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
350 Label* back_edge_target) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000351 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
352 // to make sure it is constant. Branch may emit a skip-or-jump sequence
353 // instead of the normal Branch. It seems that the "skip" part of that
354 // sequence is about as long as this Branch would be so it is safe to ignore
355 // that.
356 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000357 Comment cmnt(masm_, "[ Back edge bookkeeping");
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000358 Label ok;
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000359 int weight = 1;
360 if (FLAG_weighted_back_edges) {
361 ASSERT(back_edge_target->is_bound());
362 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
363 weight = Min(kMaxBackEdgeWeight,
364 Max(1, distance / kBackEdgeDistanceUnit));
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000365 }
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000366 EmitProfilingCounterDecrement(weight);
367 __ slt(at, a3, zero_reg);
368 __ beq(at, zero_reg, &ok);
369 // CallStub will emit a li t9 first, so it is safe to use the delay slot.
370 InterruptStub stub;
371 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000372 // Record a mapping of this PC offset to the OSR id. This is used to find
373 // the AST id from the unoptimized code in order to use it as a key into
374 // the deoptimization input data found in the optimized code.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000375 RecordBackEdge(stmt->OsrEntryId());
376 EmitProfilingCounterReset();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000377
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000378 __ bind(&ok);
379 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
380 // Record a mapping of the OSR id to this PC. This is used if the OSR
381 // entry becomes the target of a bailout. We don't expect it to be, but
382 // we want it to work if it is.
383 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000384}
385
386
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000387void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000388 Comment cmnt(masm_, "[ Return sequence");
389 if (return_label_.is_bound()) {
390 __ Branch(&return_label_);
391 } else {
392 __ bind(&return_label_);
393 if (FLAG_trace) {
394 // Push the return value on the stack as the parameter.
395 // Runtime::TraceExit returns its parameter in v0.
396 __ push(v0);
397 __ CallRuntime(Runtime::kTraceExit, 1);
398 }
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000399 if (FLAG_interrupt_at_exit || FLAG_self_optimization) {
400 // Pretend that the exit is a backwards jump to the entry.
401 int weight = 1;
402 if (info_->ShouldSelfOptimize()) {
403 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
404 } else if (FLAG_weighted_back_edges) {
405 int distance = masm_->pc_offset();
406 weight = Min(kMaxBackEdgeWeight,
danno@chromium.org129d3982012-07-25 15:01:47 +0000407 Max(1, distance / kBackEdgeDistanceUnit));
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000408 }
409 EmitProfilingCounterDecrement(weight);
410 Label ok;
411 __ Branch(&ok, ge, a3, Operand(zero_reg));
412 __ push(v0);
413 if (info_->ShouldSelfOptimize() && FLAG_direct_self_opt) {
414 __ lw(a2, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
415 __ push(a2);
416 __ CallRuntime(Runtime::kOptimizeFunctionOnNextCall, 1);
417 } else {
418 InterruptStub stub;
419 __ CallStub(&stub);
420 }
421 __ pop(v0);
422 EmitProfilingCounterReset();
423 __ bind(&ok);
424 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000425
426#ifdef DEBUG
427 // Add a label for checking the size of the code used for returning.
428 Label check_exit_codesize;
429 masm_->bind(&check_exit_codesize);
430#endif
431 // Make sure that the constant pool is not emitted inside of the return
432 // sequence.
433 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
434 // Here we use masm_-> instead of the __ macro to avoid the code coverage
435 // tool from instrumenting as we rely on the code size here.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000436 int32_t sp_delta = (info_->scope()->num_parameters() + 1) * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000437 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
438 __ RecordJSReturn();
439 masm_->mov(sp, fp);
440 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
441 masm_->Addu(sp, sp, Operand(sp_delta));
442 masm_->Jump(ra);
443 }
444
445#ifdef DEBUG
446 // Check that the size of the code used for returning is large enough
447 // for the debugger's requirements.
448 ASSERT(Assembler::kJSReturnSequenceInstructions <=
449 masm_->InstructionsGeneratedSince(&check_exit_codesize));
450#endif
451 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000452}
453
454
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000455void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
456 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
ager@chromium.org5c838252010-02-19 08:53:10 +0000457}
458
459
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000460void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
461 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
462 codegen()->GetVar(result_register(), var);
ager@chromium.org5c838252010-02-19 08:53:10 +0000463}
464
465
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000466void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
467 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
468 codegen()->GetVar(result_register(), var);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000469 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000470}
471
472
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000473void FullCodeGenerator::TestContext::Plug(Variable* var) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000474 // For simplicity we always test the accumulator register.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000475 codegen()->GetVar(result_register(), var);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000476 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000477 codegen()->DoTest(this);
ager@chromium.org5c838252010-02-19 08:53:10 +0000478}
479
480
lrn@chromium.org7516f052011-03-30 08:52:27 +0000481void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000482}
483
484
lrn@chromium.org7516f052011-03-30 08:52:27 +0000485void FullCodeGenerator::AccumulatorValueContext::Plug(
486 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000487 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000488}
489
490
491void FullCodeGenerator::StackValueContext::Plug(
492 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000493 __ LoadRoot(result_register(), index);
494 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000495}
496
497
498void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000499 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000500 true,
501 true_label_,
502 false_label_);
503 if (index == Heap::kUndefinedValueRootIndex ||
504 index == Heap::kNullValueRootIndex ||
505 index == Heap::kFalseValueRootIndex) {
506 if (false_label_ != fall_through_) __ Branch(false_label_);
507 } else if (index == Heap::kTrueValueRootIndex) {
508 if (true_label_ != fall_through_) __ Branch(true_label_);
509 } else {
510 __ LoadRoot(result_register(), index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000511 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000512 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000513}
514
515
516void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000517}
518
519
520void FullCodeGenerator::AccumulatorValueContext::Plug(
521 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000522 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000523}
524
525
526void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000527 // Immediates cannot be pushed directly.
528 __ li(result_register(), Operand(lit));
529 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000530}
531
532
533void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000534 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000535 true,
536 true_label_,
537 false_label_);
538 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
539 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
540 if (false_label_ != fall_through_) __ Branch(false_label_);
541 } else if (lit->IsTrue() || lit->IsJSObject()) {
542 if (true_label_ != fall_through_) __ Branch(true_label_);
543 } else if (lit->IsString()) {
544 if (String::cast(*lit)->length() == 0) {
545 if (false_label_ != fall_through_) __ Branch(false_label_);
546 } else {
547 if (true_label_ != fall_through_) __ Branch(true_label_);
548 }
549 } else if (lit->IsSmi()) {
550 if (Smi::cast(*lit)->value() == 0) {
551 if (false_label_ != fall_through_) __ Branch(false_label_);
552 } else {
553 if (true_label_ != fall_through_) __ Branch(true_label_);
554 }
555 } else {
556 // For simplicity we always test the accumulator register.
557 __ li(result_register(), Operand(lit));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000558 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000559 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000560}
561
562
563void FullCodeGenerator::EffectContext::DropAndPlug(int count,
564 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000565 ASSERT(count > 0);
566 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000567}
568
569
570void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
571 int count,
572 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000573 ASSERT(count > 0);
574 __ Drop(count);
575 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000576}
577
578
579void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
580 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000581 ASSERT(count > 0);
582 if (count > 1) __ Drop(count - 1);
583 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000584}
585
586
587void FullCodeGenerator::TestContext::DropAndPlug(int count,
588 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000589 ASSERT(count > 0);
590 // For simplicity we always test the accumulator register.
591 __ Drop(count);
592 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000593 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000594 codegen()->DoTest(this);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000595}
596
597
598void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
599 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000600 ASSERT(materialize_true == materialize_false);
601 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000602}
603
604
605void FullCodeGenerator::AccumulatorValueContext::Plug(
606 Label* materialize_true,
607 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000608 Label done;
609 __ bind(materialize_true);
610 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
611 __ Branch(&done);
612 __ bind(materialize_false);
613 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
614 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000615}
616
617
618void FullCodeGenerator::StackValueContext::Plug(
619 Label* materialize_true,
620 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000621 Label done;
622 __ bind(materialize_true);
623 __ LoadRoot(at, Heap::kTrueValueRootIndex);
624 __ push(at);
625 __ Branch(&done);
626 __ bind(materialize_false);
627 __ LoadRoot(at, Heap::kFalseValueRootIndex);
628 __ push(at);
629 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000630}
631
632
633void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
634 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000635 ASSERT(materialize_true == true_label_);
636 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000637}
638
639
640void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000641}
642
643
644void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000645 Heap::RootListIndex value_root_index =
646 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
647 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000648}
649
650
651void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000652 Heap::RootListIndex value_root_index =
653 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
654 __ LoadRoot(at, value_root_index);
655 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000656}
657
658
659void FullCodeGenerator::TestContext::Plug(bool flag) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000660 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000661 true,
662 true_label_,
663 false_label_);
664 if (flag) {
665 if (true_label_ != fall_through_) __ Branch(true_label_);
666 } else {
667 if (false_label_ != fall_through_) __ Branch(false_label_);
668 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000669}
670
671
whesse@chromium.org7b260152011-06-20 15:33:18 +0000672void FullCodeGenerator::DoTest(Expression* condition,
673 Label* if_true,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000674 Label* if_false,
675 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000676 if (CpuFeatures::IsSupported(FPU)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000677 ToBooleanStub stub(result_register());
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000678 __ CallStub(&stub, condition->test_id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000679 __ mov(at, zero_reg);
680 } else {
681 // Call the runtime to find the boolean value of the source and then
682 // translate it into control flow to the pair of labels.
683 __ push(result_register());
684 __ CallRuntime(Runtime::kToBool, 1);
685 __ LoadRoot(at, Heap::kFalseValueRootIndex);
686 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000687 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000688}
689
690
lrn@chromium.org7516f052011-03-30 08:52:27 +0000691void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000692 Register lhs,
693 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000694 Label* if_true,
695 Label* if_false,
696 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000697 if (if_false == fall_through) {
698 __ Branch(if_true, cc, lhs, rhs);
699 } else if (if_true == fall_through) {
700 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
701 } else {
702 __ Branch(if_true, cc, lhs, rhs);
703 __ Branch(if_false);
704 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000705}
706
707
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000708MemOperand FullCodeGenerator::StackOperand(Variable* var) {
709 ASSERT(var->IsStackAllocated());
710 // Offset is negative because higher indexes are at lower addresses.
711 int offset = -var->index() * kPointerSize;
712 // Adjust by a (parameter or local) base offset.
713 if (var->IsParameter()) {
714 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
715 } else {
716 offset += JavaScriptFrameConstants::kLocal0Offset;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000717 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000718 return MemOperand(fp, offset);
ager@chromium.org5c838252010-02-19 08:53:10 +0000719}
720
721
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000722MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
723 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
724 if (var->IsContextSlot()) {
725 int context_chain_length = scope()->ContextChainLength(var->scope());
726 __ LoadContext(scratch, context_chain_length);
727 return ContextOperand(scratch, var->index());
728 } else {
729 return StackOperand(var);
730 }
731}
732
733
734void FullCodeGenerator::GetVar(Register dest, Variable* var) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000735 // Use destination as scratch.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000736 MemOperand location = VarOperand(var, dest);
737 __ lw(dest, location);
738}
739
740
741void FullCodeGenerator::SetVar(Variable* var,
742 Register src,
743 Register scratch0,
744 Register scratch1) {
745 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
746 ASSERT(!scratch0.is(src));
747 ASSERT(!scratch0.is(scratch1));
748 ASSERT(!scratch1.is(src));
749 MemOperand location = VarOperand(var, scratch0);
750 __ sw(src, location);
751 // Emit the write barrier code if the location is in the heap.
752 if (var->IsContextSlot()) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000753 __ RecordWriteContextSlot(scratch0,
754 location.offset(),
755 src,
756 scratch1,
757 kRAHasBeenSaved,
758 kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000759 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000760}
761
762
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000763void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000764 bool should_normalize,
765 Label* if_true,
766 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000767 // Only prepare for bailouts before splits if we're in a test
768 // context. Otherwise, we let the Visit function deal with the
769 // preparation to avoid preparing with the same AST id twice.
770 if (!context()->IsTest() || !info_->IsOptimizable()) return;
771
772 Label skip;
773 if (should_normalize) __ Branch(&skip);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000774 PrepareForBailout(expr, TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000775 if (should_normalize) {
776 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
777 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
778 __ bind(&skip);
779 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000780}
781
782
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000783void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
784 // The variable in the declaration always resides in the current function
785 // context.
786 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000787 if (generate_debug_code_) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000788 // Check that we're not inside a with or catch context.
789 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
790 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
791 __ Check(ne, "Declaration in with context.",
792 a1, Operand(t0));
793 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
794 __ Check(ne, "Declaration in catch context.",
795 a1, Operand(t0));
796 }
797}
798
799
800void FullCodeGenerator::VisitVariableDeclaration(
801 VariableDeclaration* declaration) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000802 // If it was not possible to allocate the variable at compile time, we
803 // need to "declare" it at runtime to make sure it actually exists in the
804 // local context.
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000805 VariableProxy* proxy = declaration->proxy();
806 VariableMode mode = declaration->mode();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000807 Variable* variable = proxy->var();
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000808 bool hole_init = mode == CONST || mode == CONST_HARMONY || mode == LET;
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000809 switch (variable->location()) {
810 case Variable::UNALLOCATED:
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000811 globals_->Add(variable->name(), zone());
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000812 globals_->Add(variable->binding_needs_init()
813 ? isolate()->factory()->the_hole_value()
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000814 : isolate()->factory()->undefined_value(),
815 zone());
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000816 break;
817
818 case Variable::PARAMETER:
819 case Variable::LOCAL:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000820 if (hole_init) {
821 Comment cmnt(masm_, "[ VariableDeclaration");
822 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
823 __ sw(t0, StackOperand(variable));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000824 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000825 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000826
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000827 case Variable::CONTEXT:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000828 if (hole_init) {
829 Comment cmnt(masm_, "[ VariableDeclaration");
830 EmitDebugCheckDeclarationContext(variable);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000831 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000832 __ sw(at, ContextOperand(cp, variable->index()));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000833 // No write barrier since the_hole_value is in old space.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000834 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000835 }
836 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000837
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000838 case Variable::LOOKUP: {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000839 Comment cmnt(masm_, "[ VariableDeclaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000840 __ li(a2, Operand(variable->name()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000841 // Declaration nodes are always introduced in one of four modes.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000842 ASSERT(IsDeclaredVariableMode(mode));
843 PropertyAttributes attr =
844 IsImmutableVariableMode(mode) ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000845 __ li(a1, Operand(Smi::FromInt(attr)));
846 // Push initial value, if any.
847 // Note: For variables we must not push an initial value (such as
848 // 'undefined') because we may have a (legal) redeclaration and we
849 // must not destroy the current value.
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000850 if (hole_init) {
851 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
852 __ Push(cp, a2, a1, a0);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000853 } else {
854 ASSERT(Smi::FromInt(0) == 0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000855 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000856 __ Push(cp, a2, a1, a0);
857 }
858 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
859 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000860 }
861 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000862}
863
864
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000865void FullCodeGenerator::VisitFunctionDeclaration(
866 FunctionDeclaration* declaration) {
867 VariableProxy* proxy = declaration->proxy();
868 Variable* variable = proxy->var();
869 switch (variable->location()) {
870 case Variable::UNALLOCATED: {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000871 globals_->Add(variable->name(), zone());
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000872 Handle<SharedFunctionInfo> function =
873 Compiler::BuildFunctionInfo(declaration->fun(), script());
874 // Check for stack-overflow exception.
875 if (function.is_null()) return SetStackOverflow();
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000876 globals_->Add(function, zone());
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000877 break;
878 }
879
880 case Variable::PARAMETER:
881 case Variable::LOCAL: {
882 Comment cmnt(masm_, "[ FunctionDeclaration");
883 VisitForAccumulatorValue(declaration->fun());
884 __ sw(result_register(), StackOperand(variable));
885 break;
886 }
887
888 case Variable::CONTEXT: {
889 Comment cmnt(masm_, "[ FunctionDeclaration");
890 EmitDebugCheckDeclarationContext(variable);
891 VisitForAccumulatorValue(declaration->fun());
892 __ sw(result_register(), ContextOperand(cp, variable->index()));
893 int offset = Context::SlotOffset(variable->index());
894 // We know that we have written a function, which is not a smi.
895 __ RecordWriteContextSlot(cp,
896 offset,
897 result_register(),
898 a2,
899 kRAHasBeenSaved,
900 kDontSaveFPRegs,
901 EMIT_REMEMBERED_SET,
902 OMIT_SMI_CHECK);
903 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
904 break;
905 }
906
907 case Variable::LOOKUP: {
908 Comment cmnt(masm_, "[ FunctionDeclaration");
909 __ li(a2, Operand(variable->name()));
910 __ li(a1, Operand(Smi::FromInt(NONE)));
911 __ Push(cp, a2, a1);
912 // Push initial value for function declaration.
913 VisitForStackValue(declaration->fun());
914 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
915 break;
916 }
917 }
918}
919
920
921void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* declaration) {
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000922 Variable* variable = declaration->proxy()->var();
923 ASSERT(variable->location() == Variable::CONTEXT);
924 ASSERT(variable->interface()->IsFrozen());
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000925
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000926 Comment cmnt(masm_, "[ ModuleDeclaration");
927 EmitDebugCheckDeclarationContext(variable);
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000928
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000929 // Load instance object.
930 __ LoadContext(a1, scope_->ContextChainLength(scope_->GlobalScope()));
931 __ lw(a1, ContextOperand(a1, variable->interface()->Index()));
932 __ lw(a1, ContextOperand(a1, Context::EXTENSION_INDEX));
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000933
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000934 // Assign it.
935 __ sw(a1, ContextOperand(cp, variable->index()));
936 // We know that we have written a module, which is not a smi.
937 __ RecordWriteContextSlot(cp,
938 Context::SlotOffset(variable->index()),
939 a1,
940 a3,
941 kRAHasBeenSaved,
942 kDontSaveFPRegs,
943 EMIT_REMEMBERED_SET,
944 OMIT_SMI_CHECK);
945 PrepareForBailoutForId(declaration->proxy()->id(), NO_REGISTERS);
946
947 // Traverse into body.
948 Visit(declaration->module());
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000949}
950
951
952void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* declaration) {
953 VariableProxy* proxy = declaration->proxy();
954 Variable* variable = proxy->var();
955 switch (variable->location()) {
956 case Variable::UNALLOCATED:
957 // TODO(rossberg)
958 break;
959
960 case Variable::CONTEXT: {
961 Comment cmnt(masm_, "[ ImportDeclaration");
962 EmitDebugCheckDeclarationContext(variable);
963 // TODO(rossberg)
964 break;
965 }
966
967 case Variable::PARAMETER:
968 case Variable::LOCAL:
969 case Variable::LOOKUP:
970 UNREACHABLE();
971 }
972}
973
974
975void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* declaration) {
976 // TODO(rossberg)
977}
978
979
ager@chromium.org5c838252010-02-19 08:53:10 +0000980void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000981 // Call the runtime to declare the globals.
982 // The context is the first argument.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000983 __ li(a1, Operand(pairs));
984 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
985 __ Push(cp, a1, a0);
986 __ CallRuntime(Runtime::kDeclareGlobals, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000987 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000988}
989
990
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000991void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
992 // Call the runtime to declare the modules.
993 __ Push(descriptions);
994 __ CallRuntime(Runtime::kDeclareModules, 1);
995 // Return value is ignored.
996}
997
998
lrn@chromium.org7516f052011-03-30 08:52:27 +0000999void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001000 Comment cmnt(masm_, "[ SwitchStatement");
1001 Breakable nested_statement(this, stmt);
1002 SetStatementPosition(stmt);
1003
1004 // Keep the switch value on the stack until a case matches.
1005 VisitForStackValue(stmt->tag());
1006 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
1007
1008 ZoneList<CaseClause*>* clauses = stmt->cases();
1009 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
1010
1011 Label next_test; // Recycled for each test.
1012 // Compile all the tests with branches to their bodies.
1013 for (int i = 0; i < clauses->length(); i++) {
1014 CaseClause* clause = clauses->at(i);
1015 clause->body_target()->Unuse();
1016
1017 // The default is not a test, but remember it as final fall through.
1018 if (clause->is_default()) {
1019 default_clause = clause;
1020 continue;
1021 }
1022
1023 Comment cmnt(masm_, "[ Case comparison");
1024 __ bind(&next_test);
1025 next_test.Unuse();
1026
1027 // Compile the label expression.
1028 VisitForAccumulatorValue(clause->label());
1029 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
1030
1031 // Perform the comparison as if via '==='.
1032 __ lw(a1, MemOperand(sp, 0)); // Switch value.
1033 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
1034 JumpPatchSite patch_site(masm_);
1035 if (inline_smi_code) {
1036 Label slow_case;
1037 __ or_(a2, a1, a0);
1038 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
1039
1040 __ Branch(&next_test, ne, a1, Operand(a0));
1041 __ Drop(1); // Switch value is no longer needed.
1042 __ Branch(clause->body_target());
1043
1044 __ bind(&slow_case);
1045 }
1046
1047 // Record position before stub call for type feedback.
1048 SetSourcePosition(clause->position());
1049 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001050 CallIC(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001051 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001052
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001053 __ Branch(&next_test, ne, v0, Operand(zero_reg));
1054 __ Drop(1); // Switch value is no longer needed.
1055 __ Branch(clause->body_target());
1056 }
1057
1058 // Discard the test value and jump to the default if present, otherwise to
1059 // the end of the statement.
1060 __ bind(&next_test);
1061 __ Drop(1); // Switch value is no longer needed.
1062 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001063 __ Branch(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001064 } else {
1065 __ Branch(default_clause->body_target());
1066 }
1067
1068 // Compile all the case bodies.
1069 for (int i = 0; i < clauses->length(); i++) {
1070 Comment cmnt(masm_, "[ Case body");
1071 CaseClause* clause = clauses->at(i);
1072 __ bind(clause->body_target());
1073 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
1074 VisitStatements(clause->statements());
1075 }
1076
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001077 __ bind(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001078 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001079}
1080
1081
1082void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001083 Comment cmnt(masm_, "[ ForInStatement");
1084 SetStatementPosition(stmt);
1085
1086 Label loop, exit;
1087 ForIn loop_statement(this, stmt);
1088 increment_loop_depth();
1089
1090 // Get the object to enumerate over. Both SpiderMonkey and JSC
1091 // ignore null and undefined in contrast to the specification; see
1092 // ECMA-262 section 12.6.4.
1093 VisitForAccumulatorValue(stmt->enumerable());
1094 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
1095 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1096 __ Branch(&exit, eq, a0, Operand(at));
1097 Register null_value = t1;
1098 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1099 __ Branch(&exit, eq, a0, Operand(null_value));
ulan@chromium.org812308e2012-02-29 15:58:45 +00001100 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1101 __ mov(a0, v0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001102 // Convert the object to a JS object.
1103 Label convert, done_convert;
1104 __ JumpIfSmi(a0, &convert);
1105 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001106 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001107 __ bind(&convert);
1108 __ push(a0);
1109 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1110 __ mov(a0, v0);
1111 __ bind(&done_convert);
1112 __ push(a0);
1113
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001114 // Check for proxies.
1115 Label call_runtime;
1116 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1117 __ GetObjectType(a0, a1, a1);
1118 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
1119
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001120 // Check cache validity in generated code. This is a fast case for
1121 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1122 // guarantee cache validity, call the runtime system to check cache
1123 // validity or get the property names in a fixed array.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001124 __ CheckEnumCache(null_value, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001125
1126 // The enum cache is valid. Load the map of the object being
1127 // iterated over and use the cache for the iteration.
1128 Label use_cache;
1129 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
1130 __ Branch(&use_cache);
1131
1132 // Get the set of properties to enumerate.
1133 __ bind(&call_runtime);
1134 __ push(a0); // Duplicate the enumerable object on the stack.
1135 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1136
1137 // If we got a map from the runtime call, we can do a fast
1138 // modification check. Otherwise, we got a fixed array, and we have
1139 // to do a slow check.
1140 Label fixed_array;
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001141 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001142 __ LoadRoot(at, Heap::kMetaMapRootIndex);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001143 __ Branch(&fixed_array, ne, a2, Operand(at));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001144
1145 // We got a map in register v0. Get the enumeration cache from it.
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001146 Label no_descriptors;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001147 __ bind(&use_cache);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001148
1149 __ EnumLength(a1, v0);
1150 __ Branch(&no_descriptors, eq, a1, Operand(Smi::FromInt(0)));
1151
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001152 __ LoadInstanceDescriptors(v0, a2);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001153 __ lw(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheOffset));
1154 __ lw(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheBridgeCacheOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001155
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001156 // Set up the four remaining stack slots.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001157 __ push(v0); // Map.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001158 __ li(a0, Operand(Smi::FromInt(0)));
1159 // Push enumeration cache, enumeration cache length (as smi) and zero.
1160 __ Push(a2, a1, a0);
1161 __ jmp(&loop);
1162
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001163 __ bind(&no_descriptors);
1164 __ Drop(1);
1165 __ jmp(&exit);
1166
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001167 // We got a fixed array in register v0. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001168 Label non_proxy;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001169 __ bind(&fixed_array);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001170
1171 Handle<JSGlobalPropertyCell> cell =
1172 isolate()->factory()->NewJSGlobalPropertyCell(
1173 Handle<Object>(
1174 Smi::FromInt(TypeFeedbackCells::kForInFastCaseMarker)));
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001175 RecordTypeFeedbackCell(stmt->ForInFeedbackId(), cell);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001176 __ LoadHeapObject(a1, cell);
1177 __ li(a2, Operand(Smi::FromInt(TypeFeedbackCells::kForInSlowCaseMarker)));
1178 __ sw(a2, FieldMemOperand(a1, JSGlobalPropertyCell::kValueOffset));
1179
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001180 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1181 __ lw(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1182 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1183 __ GetObjectType(a2, a3, a3);
1184 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1185 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1186 __ bind(&non_proxy);
1187 __ Push(a1, v0); // Smi and array
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001188 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1189 __ li(a0, Operand(Smi::FromInt(0)));
1190 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1191
1192 // Generate code for doing the condition check.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001193 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001194 __ bind(&loop);
1195 // Load the current count to a0, load the length to a1.
1196 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1197 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001198 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001199
1200 // Get the current entry of the array into register a3.
1201 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1202 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1203 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1204 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1205 __ lw(a3, MemOperand(t0)); // Current entry.
1206
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001207 // Get the expected map from the stack or a smi in the
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001208 // permanent slow case into register a2.
1209 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1210
1211 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001212 // If not, we may have to filter the key.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001213 Label update_each;
1214 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1215 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1216 __ Branch(&update_each, eq, t0, Operand(a2));
1217
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001218 // For proxies, no filtering is done.
1219 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1220 ASSERT_EQ(Smi::FromInt(0), 0);
1221 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1222
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001223 // Convert the entry to a string or (smi) 0 if it isn't a property
1224 // any more. If the property has been removed while iterating, we
1225 // just skip it.
1226 __ push(a1); // Enumerable.
1227 __ push(a3); // Current entry.
1228 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1229 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001230 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001231
1232 // Update the 'each' property or variable from the possibly filtered
1233 // entry in register a3.
1234 __ bind(&update_each);
1235 __ mov(result_register(), a3);
1236 // Perform the assignment as if via '='.
1237 { EffectContext context(this);
ulan@chromium.org812308e2012-02-29 15:58:45 +00001238 EmitAssignment(stmt->each());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001239 }
1240
1241 // Generate code for the body of the loop.
1242 Visit(stmt->body());
1243
1244 // Generate code for the going to the next element by incrementing
1245 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001246 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001247 __ pop(a0);
1248 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1249 __ push(a0);
1250
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001251 EmitBackEdgeBookkeeping(stmt, &loop);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001252 __ Branch(&loop);
1253
1254 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001255 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001256 __ Drop(5);
1257
1258 // Exit and decrement the loop depth.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001259 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001260 __ bind(&exit);
1261 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001262}
1263
1264
1265void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1266 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001267 // Use the fast case closure allocation code that allocates in new
1268 // space for nested functions that don't need literals cloning. If
1269 // we're running with the --always-opt or the --prepare-always-opt
1270 // flag, we need to use the runtime function so that the new function
1271 // we are creating here gets a chance to have its code optimized and
1272 // doesn't just get a copy of the existing unoptimized code.
1273 if (!FLAG_always_opt &&
1274 !FLAG_prepare_always_opt &&
1275 !pretenure &&
1276 scope()->is_function_scope() &&
1277 info->num_literals() == 0) {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001278 FastNewClosureStub stub(info->language_mode());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001279 __ li(a0, Operand(info));
1280 __ push(a0);
1281 __ CallStub(&stub);
1282 } else {
1283 __ li(a0, Operand(info));
1284 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1285 : Heap::kFalseValueRootIndex);
1286 __ Push(cp, a0, a1);
1287 __ CallRuntime(Runtime::kNewClosure, 3);
1288 }
1289 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001290}
1291
1292
1293void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001294 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001295 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001296}
1297
1298
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001299void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1300 TypeofState typeof_state,
1301 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001302 Register current = cp;
1303 Register next = a1;
1304 Register temp = a2;
1305
1306 Scope* s = scope();
1307 while (s != NULL) {
1308 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001309 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001310 // Check that extension is NULL.
1311 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1312 __ Branch(slow, ne, temp, Operand(zero_reg));
1313 }
1314 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001315 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001316 // Walk the rest of the chain without clobbering cp.
1317 current = next;
1318 }
1319 // If no outer scope calls eval, we do not need to check more
1320 // context extensions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001321 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001322 s = s->outer_scope();
1323 }
1324
1325 if (s->is_eval_scope()) {
1326 Label loop, fast;
1327 if (!current.is(next)) {
1328 __ Move(next, current);
1329 }
1330 __ bind(&loop);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001331 // Terminate at native context.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001332 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001333 __ LoadRoot(t0, Heap::kNativeContextMapRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001334 __ Branch(&fast, eq, temp, Operand(t0));
1335 // Check that extension is NULL.
1336 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1337 __ Branch(slow, ne, temp, Operand(zero_reg));
1338 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001339 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001340 __ Branch(&loop);
1341 __ bind(&fast);
1342 }
1343
1344 __ lw(a0, GlobalObjectOperand());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001345 __ li(a2, Operand(var->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001346 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1347 ? RelocInfo::CODE_TARGET
1348 : RelocInfo::CODE_TARGET_CONTEXT;
1349 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001350 CallIC(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001351}
1352
1353
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001354MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1355 Label* slow) {
1356 ASSERT(var->IsContextSlot());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001357 Register context = cp;
1358 Register next = a3;
1359 Register temp = t0;
1360
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001361 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001362 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001363 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001364 // Check that extension is NULL.
1365 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1366 __ Branch(slow, ne, temp, Operand(zero_reg));
1367 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001368 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001369 // Walk the rest of the chain without clobbering cp.
1370 context = next;
1371 }
1372 }
1373 // Check that last extension is NULL.
1374 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1375 __ Branch(slow, ne, temp, Operand(zero_reg));
1376
1377 // This function is used only for loads, not stores, so it's safe to
1378 // return an cp-based operand (the write barrier cannot be allowed to
1379 // destroy the cp register).
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001380 return ContextOperand(context, var->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001381}
1382
1383
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001384void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1385 TypeofState typeof_state,
1386 Label* slow,
1387 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001388 // Generate fast-case code for variables that might be shadowed by
1389 // eval-introduced variables. Eval is used a lot without
1390 // introducing variables. In those cases, we do not want to
1391 // perform a runtime call for all variables in the scope
1392 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001393 if (var->mode() == DYNAMIC_GLOBAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001394 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001395 __ Branch(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001396 } else if (var->mode() == DYNAMIC_LOCAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001397 Variable* local = var->local_if_not_shadowed();
1398 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
danno@chromium.org1f34ad32012-11-26 14:53:56 +00001399 if (local->mode() == LET ||
1400 local->mode() == CONST ||
1401 local->mode() == CONST_HARMONY) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001402 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1403 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001404 if (local->mode() == CONST) {
1405 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +00001406 __ Movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001407 } else { // LET || CONST_HARMONY
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001408 __ Branch(done, ne, at, Operand(zero_reg));
1409 __ li(a0, Operand(var->name()));
1410 __ push(a0);
1411 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1412 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001413 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001414 __ Branch(done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001415 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001416}
1417
1418
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001419void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1420 // Record position before possible IC call.
1421 SetSourcePosition(proxy->position());
1422 Variable* var = proxy->var();
1423
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001424 // Three cases: global variables, lookup variables, and all other types of
1425 // variables.
1426 switch (var->location()) {
1427 case Variable::UNALLOCATED: {
1428 Comment cmnt(masm_, "Global variable");
1429 // Use inline caching. Variable name is passed in a2 and the global
1430 // object (receiver) in a0.
1431 __ lw(a0, GlobalObjectOperand());
1432 __ li(a2, Operand(var->name()));
1433 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001434 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001435 context()->Plug(v0);
1436 break;
1437 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001438
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001439 case Variable::PARAMETER:
1440 case Variable::LOCAL:
1441 case Variable::CONTEXT: {
1442 Comment cmnt(masm_, var->IsContextSlot()
1443 ? "Context variable"
1444 : "Stack variable");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001445 if (var->binding_needs_init()) {
1446 // var->scope() may be NULL when the proxy is located in eval code and
1447 // refers to a potential outside binding. Currently those bindings are
1448 // always looked up dynamically, i.e. in that case
1449 // var->location() == LOOKUP.
1450 // always holds.
1451 ASSERT(var->scope() != NULL);
1452
1453 // Check if the binding really needs an initialization check. The check
1454 // can be skipped in the following situation: we have a LET or CONST
1455 // binding in harmony mode, both the Variable and the VariableProxy have
1456 // the same declaration scope (i.e. they are both in global code, in the
1457 // same function or in the same eval code) and the VariableProxy is in
1458 // the source physically located after the initializer of the variable.
1459 //
1460 // We cannot skip any initialization checks for CONST in non-harmony
1461 // mode because const variables may be declared but never initialized:
1462 // if (false) { const x; }; var y = x;
1463 //
1464 // The condition on the declaration scopes is a conservative check for
1465 // nested functions that access a binding and are called before the
1466 // binding is initialized:
1467 // function() { f(); let x = 1; function f() { x = 2; } }
1468 //
1469 bool skip_init_check;
1470 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1471 skip_init_check = false;
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001472 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001473 // Check that we always have valid source position.
1474 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1475 ASSERT(proxy->position() != RelocInfo::kNoPosition);
1476 skip_init_check = var->mode() != CONST &&
1477 var->initializer_position() < proxy->position();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001478 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001479
1480 if (!skip_init_check) {
1481 // Let and const need a read barrier.
1482 GetVar(v0, var);
1483 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1484 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1485 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1486 // Throw a reference error when using an uninitialized let/const
1487 // binding in harmony mode.
1488 Label done;
1489 __ Branch(&done, ne, at, Operand(zero_reg));
1490 __ li(a0, Operand(var->name()));
1491 __ push(a0);
1492 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1493 __ bind(&done);
1494 } else {
1495 // Uninitalized const bindings outside of harmony mode are unholed.
1496 ASSERT(var->mode() == CONST);
1497 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +00001498 __ Movz(v0, a0, at); // Conditional move: Undefined if TheHole.
danno@chromium.orgc612e022011-11-10 11:38:15 +00001499 }
1500 context()->Plug(v0);
1501 break;
1502 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001503 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001504 context()->Plug(var);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001505 break;
1506 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001507
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001508 case Variable::LOOKUP: {
1509 Label done, slow;
1510 // Generate code for loading from variables potentially shadowed
1511 // by eval-introduced variables.
1512 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1513 __ bind(&slow);
1514 Comment cmnt(masm_, "Lookup variable");
1515 __ li(a1, Operand(var->name()));
1516 __ Push(cp, a1); // Context and name.
1517 __ CallRuntime(Runtime::kLoadContextSlot, 2);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001518 __ bind(&done);
1519 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001520 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001521 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001522}
1523
1524
1525void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001526 Comment cmnt(masm_, "[ RegExpLiteral");
1527 Label materialized;
1528 // Registers will be used as follows:
1529 // t1 = materialized value (RegExp literal)
1530 // t0 = JS function, literals array
1531 // a3 = literal index
1532 // a2 = RegExp pattern
1533 // a1 = RegExp flags
1534 // a0 = RegExp literal clone
1535 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1536 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1537 int literal_offset =
1538 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1539 __ lw(t1, FieldMemOperand(t0, literal_offset));
1540 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1541 __ Branch(&materialized, ne, t1, Operand(at));
1542
1543 // Create regexp literal using runtime function.
1544 // Result will be in v0.
1545 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1546 __ li(a2, Operand(expr->pattern()));
1547 __ li(a1, Operand(expr->flags()));
1548 __ Push(t0, a3, a2, a1);
1549 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1550 __ mov(t1, v0);
1551
1552 __ bind(&materialized);
1553 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1554 Label allocated, runtime_allocate;
1555 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1556 __ jmp(&allocated);
1557
1558 __ bind(&runtime_allocate);
1559 __ push(t1);
1560 __ li(a0, Operand(Smi::FromInt(size)));
1561 __ push(a0);
1562 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1563 __ pop(t1);
1564
1565 __ bind(&allocated);
1566
1567 // After this, registers are used as follows:
1568 // v0: Newly allocated regexp.
1569 // t1: Materialized regexp.
1570 // a2: temp.
1571 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1572 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001573}
1574
1575
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001576void FullCodeGenerator::EmitAccessor(Expression* expression) {
1577 if (expression == NULL) {
1578 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1579 __ push(a1);
1580 } else {
1581 VisitForStackValue(expression);
1582 }
1583}
1584
1585
ager@chromium.org5c838252010-02-19 08:53:10 +00001586void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001587 Comment cmnt(masm_, "[ ObjectLiteral");
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001588 Handle<FixedArray> constant_properties = expr->constant_properties();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001589 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1590 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1591 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001592 __ li(a1, Operand(constant_properties));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001593 int flags = expr->fast_elements()
1594 ? ObjectLiteral::kFastElements
1595 : ObjectLiteral::kNoFlags;
1596 flags |= expr->has_function()
1597 ? ObjectLiteral::kHasFunction
1598 : ObjectLiteral::kNoFlags;
1599 __ li(a0, Operand(Smi::FromInt(flags)));
1600 __ Push(a3, a2, a1, a0);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001601 int properties_count = constant_properties->length() / 2;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001602 if (expr->depth() > 1) {
1603 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001604 } else if (flags != ObjectLiteral::kFastElements ||
1605 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001606 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001607 } else {
1608 FastCloneShallowObjectStub stub(properties_count);
1609 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001610 }
1611
1612 // If result_saved is true the result is on top of the stack. If
1613 // result_saved is false the result is in v0.
1614 bool result_saved = false;
1615
1616 // Mark all computed expressions that are bound to a key that
1617 // is shadowed by a later occurrence of the same key. For the
1618 // marked expressions, no store code is emitted.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001619 expr->CalculateEmitStore(zone());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001620
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001621 AccessorTable accessor_table(zone());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001622 for (int i = 0; i < expr->properties()->length(); i++) {
1623 ObjectLiteral::Property* property = expr->properties()->at(i);
1624 if (property->IsCompileTimeValue()) continue;
1625
1626 Literal* key = property->key();
1627 Expression* value = property->value();
1628 if (!result_saved) {
1629 __ push(v0); // Save result on stack.
1630 result_saved = true;
1631 }
1632 switch (property->kind()) {
1633 case ObjectLiteral::Property::CONSTANT:
1634 UNREACHABLE();
1635 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1636 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1637 // Fall through.
1638 case ObjectLiteral::Property::COMPUTED:
1639 if (key->handle()->IsSymbol()) {
1640 if (property->emit_store()) {
1641 VisitForAccumulatorValue(value);
1642 __ mov(a0, result_register());
1643 __ li(a2, Operand(key->handle()));
1644 __ lw(a1, MemOperand(sp));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001645 Handle<Code> ic = is_classic_mode()
1646 ? isolate()->builtins()->StoreIC_Initialize()
1647 : isolate()->builtins()->StoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001648 CallIC(ic, RelocInfo::CODE_TARGET, key->LiteralFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001649 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1650 } else {
1651 VisitForEffect(value);
1652 }
1653 break;
1654 }
1655 // Fall through.
1656 case ObjectLiteral::Property::PROTOTYPE:
1657 // Duplicate receiver on stack.
1658 __ lw(a0, MemOperand(sp));
1659 __ push(a0);
1660 VisitForStackValue(key);
1661 VisitForStackValue(value);
1662 if (property->emit_store()) {
1663 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1664 __ push(a0);
1665 __ CallRuntime(Runtime::kSetProperty, 4);
1666 } else {
1667 __ Drop(3);
1668 }
1669 break;
1670 case ObjectLiteral::Property::GETTER:
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001671 accessor_table.lookup(key)->second->getter = value;
1672 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001673 case ObjectLiteral::Property::SETTER:
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001674 accessor_table.lookup(key)->second->setter = value;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001675 break;
1676 }
1677 }
1678
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001679 // Emit code to define accessors, using only a single call to the runtime for
1680 // each pair of corresponding getters and setters.
1681 for (AccessorTable::Iterator it = accessor_table.begin();
1682 it != accessor_table.end();
1683 ++it) {
1684 __ lw(a0, MemOperand(sp)); // Duplicate receiver.
1685 __ push(a0);
1686 VisitForStackValue(it->first);
1687 EmitAccessor(it->second->getter);
1688 EmitAccessor(it->second->setter);
1689 __ li(a0, Operand(Smi::FromInt(NONE)));
1690 __ push(a0);
1691 __ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
1692 }
1693
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001694 if (expr->has_function()) {
1695 ASSERT(result_saved);
1696 __ lw(a0, MemOperand(sp));
1697 __ push(a0);
1698 __ CallRuntime(Runtime::kToFastProperties, 1);
1699 }
1700
1701 if (result_saved) {
1702 context()->PlugTOS();
1703 } else {
1704 context()->Plug(v0);
1705 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001706}
1707
1708
1709void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001710 Comment cmnt(masm_, "[ ArrayLiteral");
1711
1712 ZoneList<Expression*>* subexprs = expr->values();
1713 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001714
1715 Handle<FixedArray> constant_elements = expr->constant_elements();
1716 ASSERT_EQ(2, constant_elements->length());
1717 ElementsKind constant_elements_kind =
1718 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001719 bool has_fast_elements =
1720 IsFastObjectElementsKind(constant_elements_kind);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001721 Handle<FixedArrayBase> constant_elements_values(
1722 FixedArrayBase::cast(constant_elements->get(1)));
1723
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001724 __ mov(a0, result_register());
1725 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1726 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1727 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001728 __ li(a1, Operand(constant_elements));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001729 __ Push(a3, a2, a1);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001730 if (has_fast_elements && constant_elements_values->map() ==
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001731 isolate()->heap()->fixed_cow_array_map()) {
1732 FastCloneShallowArrayStub stub(
yangguo@chromium.org28381b42013-01-21 14:39:38 +00001733 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS,
1734 DONT_TRACK_ALLOCATION_SITE,
1735 length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001736 __ CallStub(&stub);
1737 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1738 1, a1, a2);
1739 } else if (expr->depth() > 1) {
1740 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1741 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1742 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1743 } else {
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001744 ASSERT(IsFastSmiOrObjectElementsKind(constant_elements_kind) ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001745 FLAG_smi_only_arrays);
yangguo@chromium.org28381b42013-01-21 14:39:38 +00001746 FastCloneShallowArrayStub::Mode mode =
1747 FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS;
1748 AllocationSiteMode allocation_site_mode = FLAG_track_allocation_sites
1749 ? TRACK_ALLOCATION_SITE : DONT_TRACK_ALLOCATION_SITE;
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001750
yangguo@chromium.org28381b42013-01-21 14:39:38 +00001751 if (has_fast_elements) {
1752 mode = FastCloneShallowArrayStub::CLONE_ELEMENTS;
1753 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001754 }
1755
yangguo@chromium.org28381b42013-01-21 14:39:38 +00001756 FastCloneShallowArrayStub stub(mode, allocation_site_mode, length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001757 __ CallStub(&stub);
1758 }
1759
1760 bool result_saved = false; // Is the result saved to the stack?
1761
1762 // Emit code to evaluate all the non-constant subexpressions and to store
1763 // them into the newly cloned array.
1764 for (int i = 0; i < length; i++) {
1765 Expression* subexpr = subexprs->at(i);
1766 // If the subexpression is a literal or a simple materialized literal it
1767 // is already set in the cloned array.
1768 if (subexpr->AsLiteral() != NULL ||
1769 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1770 continue;
1771 }
1772
1773 if (!result_saved) {
1774 __ push(v0);
1775 result_saved = true;
1776 }
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001777
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001778 VisitForAccumulatorValue(subexpr);
1779
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001780 if (IsFastObjectElementsKind(constant_elements_kind)) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001781 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1782 __ lw(t2, MemOperand(sp)); // Copy of array literal.
1783 __ lw(a1, FieldMemOperand(t2, JSObject::kElementsOffset));
1784 __ sw(result_register(), FieldMemOperand(a1, offset));
1785 // Update the write barrier for the array store.
1786 __ RecordWriteField(a1, offset, result_register(), a2,
1787 kRAHasBeenSaved, kDontSaveFPRegs,
1788 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1789 } else {
1790 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1791 __ lw(a2, FieldMemOperand(a1, JSObject::kMapOffset));
1792 __ li(a3, Operand(Smi::FromInt(i)));
1793 __ li(t0, Operand(Smi::FromInt(expr->literal_index())));
1794 __ mov(a0, result_register());
1795 StoreArrayLiteralElementStub stub;
1796 __ CallStub(&stub);
1797 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001798
1799 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1800 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001801 if (result_saved) {
1802 context()->PlugTOS();
1803 } else {
1804 context()->Plug(v0);
1805 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001806}
1807
1808
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001809void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001810 Comment cmnt(masm_, "[ Assignment");
1811 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1812 // on the left-hand side.
1813 if (!expr->target()->IsValidLeftHandSide()) {
1814 VisitForEffect(expr->target());
1815 return;
1816 }
1817
1818 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001819 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001820 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1821 LhsKind assign_type = VARIABLE;
1822 Property* property = expr->target()->AsProperty();
1823 if (property != NULL) {
1824 assign_type = (property->key()->IsPropertyName())
1825 ? NAMED_PROPERTY
1826 : KEYED_PROPERTY;
1827 }
1828
1829 // Evaluate LHS expression.
1830 switch (assign_type) {
1831 case VARIABLE:
1832 // Nothing to do here.
1833 break;
1834 case NAMED_PROPERTY:
1835 if (expr->is_compound()) {
1836 // We need the receiver both on the stack and in the accumulator.
1837 VisitForAccumulatorValue(property->obj());
1838 __ push(result_register());
1839 } else {
1840 VisitForStackValue(property->obj());
1841 }
1842 break;
1843 case KEYED_PROPERTY:
1844 // We need the key and receiver on both the stack and in v0 and a1.
1845 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001846 VisitForStackValue(property->obj());
1847 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001848 __ lw(a1, MemOperand(sp, 0));
1849 __ push(v0);
1850 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001851 VisitForStackValue(property->obj());
1852 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001853 }
1854 break;
1855 }
1856
1857 // For compound assignments we need another deoptimization point after the
1858 // variable/property load.
1859 if (expr->is_compound()) {
1860 { AccumulatorValueContext context(this);
1861 switch (assign_type) {
1862 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001863 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001864 PrepareForBailout(expr->target(), TOS_REG);
1865 break;
1866 case NAMED_PROPERTY:
1867 EmitNamedPropertyLoad(property);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001868 PrepareForBailoutForId(property->LoadId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001869 break;
1870 case KEYED_PROPERTY:
1871 EmitKeyedPropertyLoad(property);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001872 PrepareForBailoutForId(property->LoadId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001873 break;
1874 }
1875 }
1876
1877 Token::Value op = expr->binary_op();
1878 __ push(v0); // Left operand goes on the stack.
1879 VisitForAccumulatorValue(expr->value());
1880
1881 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1882 ? OVERWRITE_RIGHT
1883 : NO_OVERWRITE;
1884 SetSourcePosition(expr->position() + 1);
1885 AccumulatorValueContext context(this);
1886 if (ShouldInlineSmiCase(op)) {
1887 EmitInlineSmiBinaryOp(expr->binary_operation(),
1888 op,
1889 mode,
1890 expr->target(),
1891 expr->value());
1892 } else {
1893 EmitBinaryOp(expr->binary_operation(), op, mode);
1894 }
1895
1896 // Deoptimization point in case the binary operation may have side effects.
1897 PrepareForBailout(expr->binary_operation(), TOS_REG);
1898 } else {
1899 VisitForAccumulatorValue(expr->value());
1900 }
1901
1902 // Record source position before possible IC call.
1903 SetSourcePosition(expr->position());
1904
1905 // Store the value.
1906 switch (assign_type) {
1907 case VARIABLE:
1908 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1909 expr->op());
1910 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1911 context()->Plug(v0);
1912 break;
1913 case NAMED_PROPERTY:
1914 EmitNamedPropertyAssignment(expr);
1915 break;
1916 case KEYED_PROPERTY:
1917 EmitKeyedPropertyAssignment(expr);
1918 break;
1919 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001920}
1921
1922
ager@chromium.org5c838252010-02-19 08:53:10 +00001923void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001924 SetSourcePosition(prop->position());
1925 Literal* key = prop->key()->AsLiteral();
1926 __ mov(a0, result_register());
1927 __ li(a2, Operand(key->handle()));
1928 // Call load IC. It has arguments receiver and property name a0 and a2.
1929 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001930 CallIC(ic, RelocInfo::CODE_TARGET, prop->PropertyFeedbackId());
ager@chromium.org5c838252010-02-19 08:53:10 +00001931}
1932
1933
1934void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001935 SetSourcePosition(prop->position());
1936 __ mov(a0, result_register());
1937 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1938 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001939 CallIC(ic, RelocInfo::CODE_TARGET, prop->PropertyFeedbackId());
ager@chromium.org5c838252010-02-19 08:53:10 +00001940}
1941
1942
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001943void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001944 Token::Value op,
1945 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001946 Expression* left_expr,
1947 Expression* right_expr) {
1948 Label done, smi_case, stub_call;
1949
1950 Register scratch1 = a2;
1951 Register scratch2 = a3;
1952
1953 // Get the arguments.
1954 Register left = a1;
1955 Register right = a0;
1956 __ pop(left);
1957 __ mov(a0, result_register());
1958
1959 // Perform combined smi check on both operands.
1960 __ Or(scratch1, left, Operand(right));
1961 STATIC_ASSERT(kSmiTag == 0);
1962 JumpPatchSite patch_site(masm_);
1963 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1964
1965 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001966 BinaryOpStub stub(op, mode);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001967 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET,
1968 expr->BinaryOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001969 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001970 __ jmp(&done);
1971
1972 __ bind(&smi_case);
1973 // Smi case. This code works the same way as the smi-smi case in the type
1974 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001975 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001976 switch (op) {
1977 case Token::SAR:
1978 __ Branch(&stub_call);
1979 __ GetLeastBitsFromSmi(scratch1, right, 5);
1980 __ srav(right, left, scratch1);
1981 __ And(v0, right, Operand(~kSmiTagMask));
1982 break;
1983 case Token::SHL: {
1984 __ Branch(&stub_call);
1985 __ SmiUntag(scratch1, left);
1986 __ GetLeastBitsFromSmi(scratch2, right, 5);
1987 __ sllv(scratch1, scratch1, scratch2);
1988 __ Addu(scratch2, scratch1, Operand(0x40000000));
1989 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1990 __ SmiTag(v0, scratch1);
1991 break;
1992 }
1993 case Token::SHR: {
1994 __ Branch(&stub_call);
1995 __ SmiUntag(scratch1, left);
1996 __ GetLeastBitsFromSmi(scratch2, right, 5);
1997 __ srlv(scratch1, scratch1, scratch2);
1998 __ And(scratch2, scratch1, 0xc0000000);
1999 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
2000 __ SmiTag(v0, scratch1);
2001 break;
2002 }
2003 case Token::ADD:
2004 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
2005 __ BranchOnOverflow(&stub_call, scratch1);
2006 break;
2007 case Token::SUB:
2008 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
2009 __ BranchOnOverflow(&stub_call, scratch1);
2010 break;
2011 case Token::MUL: {
2012 __ SmiUntag(scratch1, right);
2013 __ Mult(left, scratch1);
2014 __ mflo(scratch1);
2015 __ mfhi(scratch2);
2016 __ sra(scratch1, scratch1, 31);
2017 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
2018 __ mflo(v0);
2019 __ Branch(&done, ne, v0, Operand(zero_reg));
2020 __ Addu(scratch2, right, left);
2021 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
2022 ASSERT(Smi::FromInt(0) == 0);
2023 __ mov(v0, zero_reg);
2024 break;
2025 }
2026 case Token::BIT_OR:
2027 __ Or(v0, left, Operand(right));
2028 break;
2029 case Token::BIT_AND:
2030 __ And(v0, left, Operand(right));
2031 break;
2032 case Token::BIT_XOR:
2033 __ Xor(v0, left, Operand(right));
2034 break;
2035 default:
2036 UNREACHABLE();
2037 }
2038
2039 __ bind(&done);
2040 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002041}
2042
2043
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002044void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
2045 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002046 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002047 __ mov(a0, result_register());
2048 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00002049 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002050 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002051 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET,
2052 expr->BinaryOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002053 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002054 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002055}
2056
2057
ulan@chromium.org812308e2012-02-29 15:58:45 +00002058void FullCodeGenerator::EmitAssignment(Expression* expr) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002059 // Invalid left-hand sides are rewritten by the parser to have a 'throw
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002060 // ReferenceError' on the left-hand side.
2061 if (!expr->IsValidLeftHandSide()) {
2062 VisitForEffect(expr);
2063 return;
2064 }
2065
2066 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00002067 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002068 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2069 LhsKind assign_type = VARIABLE;
2070 Property* prop = expr->AsProperty();
2071 if (prop != NULL) {
2072 assign_type = (prop->key()->IsPropertyName())
2073 ? NAMED_PROPERTY
2074 : KEYED_PROPERTY;
2075 }
2076
2077 switch (assign_type) {
2078 case VARIABLE: {
2079 Variable* var = expr->AsVariableProxy()->var();
2080 EffectContext context(this);
2081 EmitVariableAssignment(var, Token::ASSIGN);
2082 break;
2083 }
2084 case NAMED_PROPERTY: {
2085 __ push(result_register()); // Preserve value.
2086 VisitForAccumulatorValue(prop->obj());
2087 __ mov(a1, result_register());
2088 __ pop(a0); // Restore value.
2089 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002090 Handle<Code> ic = is_classic_mode()
2091 ? isolate()->builtins()->StoreIC_Initialize()
2092 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002093 CallIC(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002094 break;
2095 }
2096 case KEYED_PROPERTY: {
2097 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00002098 VisitForStackValue(prop->obj());
2099 VisitForAccumulatorValue(prop->key());
2100 __ mov(a1, result_register());
2101 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002102 __ pop(a0); // Restore value.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002103 Handle<Code> ic = is_classic_mode()
2104 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2105 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002106 CallIC(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002107 break;
2108 }
2109 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002110 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002111}
2112
2113
2114void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002115 Token::Value op) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002116 if (var->IsUnallocated()) {
2117 // Global var, const, or let.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002118 __ mov(a0, result_register());
2119 __ li(a2, Operand(var->name()));
2120 __ lw(a1, GlobalObjectOperand());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002121 Handle<Code> ic = is_classic_mode()
2122 ? isolate()->builtins()->StoreIC_Initialize()
2123 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002124 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002125
2126 } else if (op == Token::INIT_CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002127 // Const initializers need a write barrier.
2128 ASSERT(!var->IsParameter()); // No const parameters.
2129 if (var->IsStackLocal()) {
2130 Label skip;
2131 __ lw(a1, StackOperand(var));
2132 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2133 __ Branch(&skip, ne, a1, Operand(t0));
2134 __ sw(result_register(), StackOperand(var));
2135 __ bind(&skip);
2136 } else {
2137 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
2138 // Like var declarations, const declarations are hoisted to function
2139 // scope. However, unlike var initializers, const initializers are
2140 // able to drill a hole to that function context, even from inside a
2141 // 'with' context. We thus bypass the normal static scope lookup for
2142 // var->IsContextSlot().
2143 __ push(v0);
2144 __ li(a0, Operand(var->name()));
2145 __ Push(cp, a0); // Context and name.
2146 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002147 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002148
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002149 } else if (var->mode() == LET && op != Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002150 // Non-initializing assignment to let variable needs a write barrier.
2151 if (var->IsLookupSlot()) {
2152 __ push(v0); // Value.
2153 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002154 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002155 __ Push(cp, a1, a0); // Context, name, strict mode.
2156 __ CallRuntime(Runtime::kStoreContextSlot, 4);
2157 } else {
2158 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
2159 Label assign;
2160 MemOperand location = VarOperand(var, a1);
2161 __ lw(a3, location);
2162 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2163 __ Branch(&assign, ne, a3, Operand(t0));
2164 __ li(a3, Operand(var->name()));
2165 __ push(a3);
2166 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2167 // Perform the assignment.
2168 __ bind(&assign);
2169 __ sw(result_register(), location);
2170 if (var->IsContextSlot()) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002171 // RecordWrite may destroy all its register arguments.
2172 __ mov(a3, result_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002173 int offset = Context::SlotOffset(var->index());
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002174 __ RecordWriteContextSlot(
2175 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002176 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002177 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002178
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002179 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2180 // Assignment to var or initializing assignment to let/const
2181 // in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002182 if (var->IsStackAllocated() || var->IsContextSlot()) {
2183 MemOperand location = VarOperand(var, a1);
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002184 if (generate_debug_code_ && op == Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002185 // Check for an uninitialized let binding.
2186 __ lw(a2, location);
2187 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2188 __ Check(eq, "Let binding re-initialization.", a2, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002189 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002190 // Perform the assignment.
2191 __ sw(v0, location);
2192 if (var->IsContextSlot()) {
2193 __ mov(a3, v0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002194 int offset = Context::SlotOffset(var->index());
2195 __ RecordWriteContextSlot(
2196 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002197 }
2198 } else {
2199 ASSERT(var->IsLookupSlot());
2200 __ push(v0); // Value.
2201 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002202 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002203 __ Push(cp, a1, a0); // Context, name, strict mode.
2204 __ CallRuntime(Runtime::kStoreContextSlot, 4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002205 }
2206 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002207 // Non-initializing assignments to consts are ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +00002208}
2209
2210
2211void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002212 // Assignment to a property, using a named store IC.
2213 Property* prop = expr->target()->AsProperty();
2214 ASSERT(prop != NULL);
2215 ASSERT(prop->key()->AsLiteral() != NULL);
2216
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002217 // Record source code position before IC call.
2218 SetSourcePosition(expr->position());
2219 __ mov(a0, result_register()); // Load the value.
2220 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002221 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002222
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002223 Handle<Code> ic = is_classic_mode()
2224 ? isolate()->builtins()->StoreIC_Initialize()
2225 : isolate()->builtins()->StoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002226 CallIC(ic, RelocInfo::CODE_TARGET, expr->AssignmentFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002227
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002228 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2229 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002230}
2231
2232
2233void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002234 // Assignment to a property, using a keyed store IC.
2235
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002236 // Record source code position before IC call.
2237 SetSourcePosition(expr->position());
2238 // Call keyed store IC.
2239 // The arguments are:
2240 // - a0 is the value,
2241 // - a1 is the key,
2242 // - a2 is the receiver.
2243 __ mov(a0, result_register());
2244 __ pop(a1); // Key.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002245 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002246
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002247 Handle<Code> ic = is_classic_mode()
2248 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2249 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002250 CallIC(ic, RelocInfo::CODE_TARGET, expr->AssignmentFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002251
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002252 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2253 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002254}
2255
2256
2257void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002258 Comment cmnt(masm_, "[ Property");
2259 Expression* key = expr->key();
2260
2261 if (key->IsPropertyName()) {
2262 VisitForAccumulatorValue(expr->obj());
2263 EmitNamedPropertyLoad(expr);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002264 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002265 context()->Plug(v0);
2266 } else {
2267 VisitForStackValue(expr->obj());
2268 VisitForAccumulatorValue(expr->key());
2269 __ pop(a1);
2270 EmitKeyedPropertyLoad(expr);
2271 context()->Plug(v0);
2272 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002273}
2274
lrn@chromium.org7516f052011-03-30 08:52:27 +00002275
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002276void FullCodeGenerator::CallIC(Handle<Code> code,
2277 RelocInfo::Mode rmode,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002278 TypeFeedbackId id) {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002279 ic_total_count_++;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002280 __ Call(code, rmode, id);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002281}
2282
2283
ager@chromium.org5c838252010-02-19 08:53:10 +00002284void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002285 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002286 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002287 // Code common for calls using the IC.
2288 ZoneList<Expression*>* args = expr->arguments();
2289 int arg_count = args->length();
2290 { PreservePositionScope scope(masm()->positions_recorder());
2291 for (int i = 0; i < arg_count; i++) {
2292 VisitForStackValue(args->at(i));
2293 }
2294 __ li(a2, Operand(name));
2295 }
2296 // Record source position for debugger.
2297 SetSourcePosition(expr->position());
2298 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002299 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002300 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002301 CallIC(ic, mode, expr->CallFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002302 RecordJSReturnSite(expr);
2303 // Restore context register.
2304 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2305 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002306}
2307
2308
lrn@chromium.org7516f052011-03-30 08:52:27 +00002309void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002310 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002311 // Load the key.
2312 VisitForAccumulatorValue(key);
2313
2314 // Swap the name of the function and the receiver on the stack to follow
2315 // the calling convention for call ICs.
2316 __ pop(a1);
2317 __ push(v0);
2318 __ push(a1);
2319
2320 // Code common for calls using the IC.
2321 ZoneList<Expression*>* args = expr->arguments();
2322 int arg_count = args->length();
2323 { PreservePositionScope scope(masm()->positions_recorder());
2324 for (int i = 0; i < arg_count; i++) {
2325 VisitForStackValue(args->at(i));
2326 }
2327 }
2328 // Record source position for debugger.
2329 SetSourcePosition(expr->position());
2330 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002331 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002332 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002333 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002334 CallIC(ic, RelocInfo::CODE_TARGET, expr->CallFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002335 RecordJSReturnSite(expr);
2336 // Restore context register.
2337 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2338 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002339}
2340
2341
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002342void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002343 // Code common for calls using the call stub.
2344 ZoneList<Expression*>* args = expr->arguments();
2345 int arg_count = args->length();
2346 { PreservePositionScope scope(masm()->positions_recorder());
2347 for (int i = 0; i < arg_count; i++) {
2348 VisitForStackValue(args->at(i));
2349 }
2350 }
2351 // Record source position for debugger.
2352 SetSourcePosition(expr->position());
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00002353
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002354 // Record call targets.
2355 flags = static_cast<CallFunctionFlags>(flags | RECORD_CALL_TARGET);
2356 Handle<Object> uninitialized =
2357 TypeFeedbackCells::UninitializedSentinel(isolate());
2358 Handle<JSGlobalPropertyCell> cell =
2359 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002360 RecordTypeFeedbackCell(expr->CallFeedbackId(), cell);
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002361 __ li(a2, Operand(cell));
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00002362
lrn@chromium.org34e60782011-09-15 07:25:40 +00002363 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002364 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00002365 __ CallStub(&stub, expr->CallFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002366 RecordJSReturnSite(expr);
2367 // Restore context register.
2368 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2369 context()->DropAndPlug(1, v0);
2370}
2371
2372
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002373void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002374 // Push copy of the first argument or undefined if it doesn't exist.
2375 if (arg_count > 0) {
2376 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2377 } else {
2378 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2379 }
2380 __ push(a1);
2381
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002382 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002383 int receiver_offset = 2 + info_->scope()->num_parameters();
2384 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002385 __ push(a1);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002386 // Push the language mode.
2387 __ li(a1, Operand(Smi::FromInt(language_mode())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002388 __ push(a1);
2389
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002390 // Push the start position of the scope the calls resides in.
2391 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
2392 __ push(a1);
2393
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002394 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002395 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org5c838252010-02-19 08:53:10 +00002396}
2397
2398
2399void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002400#ifdef DEBUG
2401 // We want to verify that RecordJSReturnSite gets called on all paths
2402 // through this function. Avoid early returns.
2403 expr->return_is_recorded_ = false;
2404#endif
2405
2406 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002407 Expression* callee = expr->expression();
2408 VariableProxy* proxy = callee->AsVariableProxy();
2409 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002410
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002411 if (proxy != NULL && proxy->var()->is_possibly_eval(isolate())) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002412 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2413 // resolve the function we need to call and the receiver of the
2414 // call. Then we call the resolved function using the given
2415 // arguments.
2416 ZoneList<Expression*>* args = expr->arguments();
2417 int arg_count = args->length();
2418
2419 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002420 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002421 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2422 __ push(a2); // Reserved receiver slot.
2423
2424 // Push the arguments.
2425 for (int i = 0; i < arg_count; i++) {
2426 VisitForStackValue(args->at(i));
2427 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002428
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002429 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002430 // resolve eval.
2431 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2432 __ push(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002433 EmitResolvePossiblyDirectEval(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002434
2435 // The runtime call returns a pair of values in v0 (function) and
2436 // v1 (receiver). Touch up the stack with the right values.
2437 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2438 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2439 }
2440 // Record source position for debugger.
2441 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002442 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002443 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002444 __ CallStub(&stub);
2445 RecordJSReturnSite(expr);
2446 // Restore context register.
2447 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2448 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002449 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002450 // Push global object as receiver for the call IC.
2451 __ lw(a0, GlobalObjectOperand());
2452 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002453 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2454 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002455 // Call to a lookup slot (dynamically introduced variable).
2456 Label slow, done;
2457
2458 { PreservePositionScope scope(masm()->positions_recorder());
2459 // Generate code for loading from variables potentially shadowed
2460 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002461 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002462 }
2463
2464 __ bind(&slow);
2465 // Call the runtime to find the function to call (returned in v0)
2466 // and the object holding it (returned in v1).
2467 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002468 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002469 __ push(a2);
2470 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2471 __ Push(v0, v1); // Function, receiver.
2472
2473 // If fast case code has been generated, emit code to push the
2474 // function and receiver and have the slow path jump around this
2475 // code.
2476 if (done.is_linked()) {
2477 Label call;
2478 __ Branch(&call);
2479 __ bind(&done);
2480 // Push function.
2481 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002482 // The receiver is implicitly the global receiver. Indicate this
2483 // by passing the hole to the call function stub.
2484 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002485 __ push(a1);
2486 __ bind(&call);
2487 }
2488
danno@chromium.org40cb8782011-05-25 07:58:50 +00002489 // The receiver is either the global receiver or an object found
2490 // by LoadContextSlot. That object could be the hole if the
2491 // receiver is implicitly the global object.
2492 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002493 } else if (property != NULL) {
2494 { PreservePositionScope scope(masm()->positions_recorder());
2495 VisitForStackValue(property->obj());
2496 }
2497 if (property->key()->IsPropertyName()) {
2498 EmitCallWithIC(expr,
2499 property->key()->AsLiteral()->handle(),
2500 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002501 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002502 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002503 }
2504 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002505 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002506 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002507 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002508 }
2509 // Load global receiver object.
2510 __ lw(a1, GlobalObjectOperand());
2511 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2512 __ push(a1);
2513 // Emit function call.
2514 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2515 }
2516
2517#ifdef DEBUG
2518 // RecordJSReturnSite should have been called.
2519 ASSERT(expr->return_is_recorded_);
2520#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002521}
2522
2523
2524void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002525 Comment cmnt(masm_, "[ CallNew");
2526 // According to ECMA-262, section 11.2.2, page 44, the function
2527 // expression in new calls must be evaluated before the
2528 // arguments.
2529
2530 // Push constructor on the stack. If it's not a function it's used as
2531 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2532 // ignored.
2533 VisitForStackValue(expr->expression());
2534
2535 // Push the arguments ("left-to-right") on the stack.
2536 ZoneList<Expression*>* args = expr->arguments();
2537 int arg_count = args->length();
2538 for (int i = 0; i < arg_count; i++) {
2539 VisitForStackValue(args->at(i));
2540 }
2541
2542 // Call the construct call builtin that handles allocation and
2543 // constructor invocation.
2544 SetSourcePosition(expr->position());
2545
2546 // Load function and argument count into a1 and a0.
2547 __ li(a0, Operand(arg_count));
2548 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2549
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002550 // Record call targets in unoptimized code.
2551 Handle<Object> uninitialized =
2552 TypeFeedbackCells::UninitializedSentinel(isolate());
2553 Handle<JSGlobalPropertyCell> cell =
2554 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002555 RecordTypeFeedbackCell(expr->CallNewFeedbackId(), cell);
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002556 __ li(a2, Operand(cell));
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002557
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002558 CallConstructStub stub(RECORD_CALL_TARGET);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002559 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
ulan@chromium.org967e2702012-02-28 09:49:15 +00002560 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002561 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002562}
2563
2564
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002565void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2566 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002567 ASSERT(args->length() == 1);
2568
2569 VisitForAccumulatorValue(args->at(0));
2570
2571 Label materialize_true, materialize_false;
2572 Label* if_true = NULL;
2573 Label* if_false = NULL;
2574 Label* fall_through = NULL;
2575 context()->PrepareTest(&materialize_true, &materialize_false,
2576 &if_true, &if_false, &fall_through);
2577
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002578 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002579 __ And(t0, v0, Operand(kSmiTagMask));
2580 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2581
2582 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002583}
2584
2585
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002586void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2587 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002588 ASSERT(args->length() == 1);
2589
2590 VisitForAccumulatorValue(args->at(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
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002599 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002600 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2601 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2602
2603 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002604}
2605
2606
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002607void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2608 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002609 ASSERT(args->length() == 1);
2610
2611 VisitForAccumulatorValue(args->at(0));
2612
2613 Label materialize_true, materialize_false;
2614 Label* if_true = NULL;
2615 Label* if_false = NULL;
2616 Label* fall_through = NULL;
2617 context()->PrepareTest(&materialize_true, &materialize_false,
2618 &if_true, &if_false, &fall_through);
2619
2620 __ JumpIfSmi(v0, if_false);
2621 __ LoadRoot(at, Heap::kNullValueRootIndex);
2622 __ Branch(if_true, eq, v0, Operand(at));
2623 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2624 // Undetectable objects behave like undefined when tested with typeof.
2625 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2626 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2627 __ Branch(if_false, ne, at, Operand(zero_reg));
2628 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002629 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002630 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002631 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2632 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002633
2634 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002635}
2636
2637
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002638void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2639 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002640 ASSERT(args->length() == 1);
2641
2642 VisitForAccumulatorValue(args->at(0));
2643
2644 Label materialize_true, materialize_false;
2645 Label* if_true = NULL;
2646 Label* if_false = NULL;
2647 Label* fall_through = NULL;
2648 context()->PrepareTest(&materialize_true, &materialize_false,
2649 &if_true, &if_false, &fall_through);
2650
2651 __ JumpIfSmi(v0, if_false);
2652 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002653 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002654 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002655 if_true, if_false, fall_through);
2656
2657 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002658}
2659
2660
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002661void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2662 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002663 ASSERT(args->length() == 1);
2664
2665 VisitForAccumulatorValue(args->at(0));
2666
2667 Label materialize_true, materialize_false;
2668 Label* if_true = NULL;
2669 Label* if_false = NULL;
2670 Label* fall_through = NULL;
2671 context()->PrepareTest(&materialize_true, &materialize_false,
2672 &if_true, &if_false, &fall_through);
2673
2674 __ JumpIfSmi(v0, if_false);
2675 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2676 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2677 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002678 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002679 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2680
2681 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002682}
2683
2684
2685void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002686 CallRuntime* expr) {
2687 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002688 ASSERT(args->length() == 1);
2689
2690 VisitForAccumulatorValue(args->at(0));
2691
2692 Label materialize_true, materialize_false;
2693 Label* if_true = NULL;
2694 Label* if_false = NULL;
2695 Label* fall_through = NULL;
2696 context()->PrepareTest(&materialize_true, &materialize_false,
2697 &if_true, &if_false, &fall_through);
2698
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002699 __ AssertNotSmi(v0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002700
2701 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2702 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2703 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2704 __ Branch(if_true, ne, t0, Operand(zero_reg));
2705
2706 // Check for fast case object. Generate false result for slow case object.
2707 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2708 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2709 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2710 __ Branch(if_false, eq, a2, Operand(t0));
2711
2712 // Look for valueOf symbol in the descriptor array, and indicate false if
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002713 // found. Since we omit an enumeration index check, if it is added via a
2714 // transition that shares its descriptor array, this is a false positive.
2715 Label entry, loop, done;
2716
2717 // Skip loop if no descriptors are valid.
2718 __ NumberOfOwnDescriptors(a3, a1);
2719 __ Branch(&done, eq, a3, Operand(zero_reg));
2720
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002721 __ LoadInstanceDescriptors(a1, t0);
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002722 // t0: descriptor array.
2723 // a3: valid entries in the descriptor array.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002724 STATIC_ASSERT(kSmiTag == 0);
2725 STATIC_ASSERT(kSmiTagSize == 1);
2726 STATIC_ASSERT(kPointerSize == 4);
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002727 __ li(at, Operand(DescriptorArray::kDescriptorSize));
2728 __ Mul(a3, a3, at);
2729 // Calculate location of the first key name.
2730 __ Addu(t0, t0, Operand(DescriptorArray::kFirstOffset - kHeapObjectTag));
2731 // Calculate the end of the descriptor array.
2732 __ mov(a2, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002733 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2734 __ Addu(a2, a2, t1);
2735
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002736 // Loop through all the keys in the descriptor array. If one of these is the
2737 // symbol valueOf the result is false.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002738 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2739 // used in the loop below.
danno@chromium.org88aa0582012-03-23 15:11:57 +00002740 __ LoadRoot(t2, Heap::kvalue_of_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002741 __ jmp(&entry);
2742 __ bind(&loop);
2743 __ lw(a3, MemOperand(t0, 0));
2744 __ Branch(if_false, eq, a3, Operand(t2));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002745 __ Addu(t0, t0, Operand(DescriptorArray::kDescriptorSize * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002746 __ bind(&entry);
2747 __ Branch(&loop, ne, t0, Operand(a2));
2748
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002749 __ bind(&done);
2750 // If a valueOf property is not found on the object check that its
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002751 // prototype is the un-modified String prototype. If not result is false.
2752 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2753 __ JumpIfSmi(a2, if_false);
2754 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002755 __ lw(a3, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
2756 __ lw(a3, FieldMemOperand(a3, GlobalObject::kNativeContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002757 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2758 __ Branch(if_false, ne, a2, Operand(a3));
2759
2760 // Set the bit in the map to indicate that it has been checked safe for
2761 // default valueOf and set true result.
2762 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2763 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2764 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2765 __ jmp(if_true);
2766
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002767 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002768 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002769}
2770
2771
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002772void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2773 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002774 ASSERT(args->length() == 1);
2775
2776 VisitForAccumulatorValue(args->at(0));
2777
2778 Label materialize_true, materialize_false;
2779 Label* if_true = NULL;
2780 Label* if_false = NULL;
2781 Label* fall_through = NULL;
2782 context()->PrepareTest(&materialize_true, &materialize_false,
2783 &if_true, &if_false, &fall_through);
2784
2785 __ JumpIfSmi(v0, if_false);
2786 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002787 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002788 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2789 __ Branch(if_false);
2790
2791 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002792}
2793
2794
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002795void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2796 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002797 ASSERT(args->length() == 1);
2798
2799 VisitForAccumulatorValue(args->at(0));
2800
2801 Label materialize_true, materialize_false;
2802 Label* if_true = NULL;
2803 Label* if_false = NULL;
2804 Label* fall_through = NULL;
2805 context()->PrepareTest(&materialize_true, &materialize_false,
2806 &if_true, &if_false, &fall_through);
2807
2808 __ JumpIfSmi(v0, if_false);
2809 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002810 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002811 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2812 if_true, if_false, fall_through);
2813
2814 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002815}
2816
2817
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002818void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2819 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002820 ASSERT(args->length() == 1);
2821
2822 VisitForAccumulatorValue(args->at(0));
2823
2824 Label materialize_true, materialize_false;
2825 Label* if_true = NULL;
2826 Label* if_false = NULL;
2827 Label* fall_through = NULL;
2828 context()->PrepareTest(&materialize_true, &materialize_false,
2829 &if_true, &if_false, &fall_through);
2830
2831 __ JumpIfSmi(v0, if_false);
2832 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002833 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002834 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2835
2836 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002837}
2838
2839
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002840void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2841 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002842
2843 Label materialize_true, materialize_false;
2844 Label* if_true = NULL;
2845 Label* if_false = NULL;
2846 Label* fall_through = NULL;
2847 context()->PrepareTest(&materialize_true, &materialize_false,
2848 &if_true, &if_false, &fall_through);
2849
2850 // Get the frame pointer for the calling frame.
2851 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2852
2853 // Skip the arguments adaptor frame if it exists.
2854 Label check_frame_marker;
2855 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2856 __ Branch(&check_frame_marker, ne,
2857 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2858 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2859
2860 // Check the marker in the calling frame.
2861 __ bind(&check_frame_marker);
2862 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002863 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002864 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2865 if_true, if_false, fall_through);
2866
2867 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002868}
2869
2870
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002871void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2872 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002873 ASSERT(args->length() == 2);
2874
2875 // Load the two objects into registers and perform the comparison.
2876 VisitForStackValue(args->at(0));
2877 VisitForAccumulatorValue(args->at(1));
2878
2879 Label materialize_true, materialize_false;
2880 Label* if_true = NULL;
2881 Label* if_false = NULL;
2882 Label* fall_through = NULL;
2883 context()->PrepareTest(&materialize_true, &materialize_false,
2884 &if_true, &if_false, &fall_through);
2885
2886 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002887 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002888 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2889
2890 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002891}
2892
2893
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002894void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2895 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002896 ASSERT(args->length() == 1);
2897
2898 // ArgumentsAccessStub expects the key in a1 and the formal
2899 // parameter count in a0.
2900 VisitForAccumulatorValue(args->at(0));
2901 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002902 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002903 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2904 __ CallStub(&stub);
2905 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002906}
2907
2908
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002909void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2910 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002911 Label exit;
2912 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002913 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002914
2915 // Check if the calling frame is an arguments adaptor frame.
2916 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2917 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2918 __ Branch(&exit, ne, a3,
2919 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2920
2921 // Arguments adaptor case: Read the arguments length from the
2922 // adaptor frame.
2923 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2924
2925 __ bind(&exit);
2926 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002927}
2928
2929
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002930void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2931 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002932 ASSERT(args->length() == 1);
2933 Label done, null, function, non_function_constructor;
2934
2935 VisitForAccumulatorValue(args->at(0));
2936
2937 // If the object is a smi, we return null.
2938 __ JumpIfSmi(v0, &null);
2939
2940 // Check that the object is a JS object but take special care of JS
2941 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002942 // Assume that there are only two callable types, and one of them is at
2943 // either end of the type range for JS object types. Saves extra comparisons.
2944 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002945 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002946 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002947
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002948 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2949 FIRST_SPEC_OBJECT_TYPE + 1);
2950 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002951
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002952 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2953 LAST_SPEC_OBJECT_TYPE - 1);
2954 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2955 // Assume that there is no larger type.
2956 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2957
2958 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002959 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2960 __ GetObjectType(v0, a1, a1);
2961 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2962
2963 // v0 now contains the constructor function. Grab the
2964 // instance class name from there.
2965 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2966 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2967 __ Branch(&done);
2968
2969 // Functions have class 'Function'.
2970 __ bind(&function);
2971 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2972 __ jmp(&done);
2973
2974 // Objects with a non-function constructor have class 'Object'.
2975 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002976 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002977 __ jmp(&done);
2978
2979 // Non-JS objects have class null.
2980 __ bind(&null);
2981 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2982
2983 // All done.
2984 __ bind(&done);
2985
2986 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002987}
2988
2989
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002990void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002991 // Conditionally generate a log call.
2992 // Args:
2993 // 0 (literal string): The type of logging (corresponds to the flags).
2994 // This is used to determine whether or not to generate the log call.
2995 // 1 (string): Format string. Access the string at argument index 2
2996 // with '%2s' (see Logger::LogRuntime for all the formats).
2997 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002998 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002999 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003000 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
3001 VisitForStackValue(args->at(1));
3002 VisitForStackValue(args->at(2));
3003 __ CallRuntime(Runtime::kLog, 2);
3004 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003005
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003006 // Finally, we're expected to leave a value on the top of the stack.
3007 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3008 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003009}
3010
3011
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003012void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
3013 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003014 Label slow_allocate_heapnumber;
3015 Label heapnumber_allocated;
3016
3017 // Save the new heap number in callee-saved register s0, since
3018 // we call out to external C code below.
3019 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
3020 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
3021 __ jmp(&heapnumber_allocated);
3022
3023 __ bind(&slow_allocate_heapnumber);
3024
3025 // Allocate a heap number.
3026 __ CallRuntime(Runtime::kNumberAlloc, 0);
3027 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
3028
3029 __ bind(&heapnumber_allocated);
3030
3031 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
3032 // by computing:
3033 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
3034 if (CpuFeatures::IsSupported(FPU)) {
3035 __ PrepareCallCFunction(1, a0);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003036 __ lw(a0, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
3037 __ lw(a0, FieldMemOperand(a0, GlobalObject::kNativeContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003038 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
3039
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003040 CpuFeatures::Scope scope(FPU);
3041 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
3042 __ li(a1, Operand(0x41300000));
3043 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00003044 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003045 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00003046 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003047 // Subtract and store the result in the heap number.
3048 __ sub_d(f0, f12, f14);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003049 __ sdc1(f0, FieldMemOperand(s0, HeapNumber::kValueOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003050 __ mov(v0, s0);
3051 } else {
3052 __ PrepareCallCFunction(2, a0);
3053 __ mov(a0, s0);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003054 __ lw(a1, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
3055 __ lw(a1, FieldMemOperand(a1, GlobalObject::kNativeContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003056 __ CallCFunction(
3057 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
3058 }
3059
3060 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003061}
3062
3063
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003064void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003065 // Load the arguments on the stack and call the stub.
3066 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003067 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003068 ASSERT(args->length() == 3);
3069 VisitForStackValue(args->at(0));
3070 VisitForStackValue(args->at(1));
3071 VisitForStackValue(args->at(2));
3072 __ CallStub(&stub);
3073 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003074}
3075
3076
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003077void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003078 // Load the arguments on the stack and call the stub.
3079 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003080 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003081 ASSERT(args->length() == 4);
3082 VisitForStackValue(args->at(0));
3083 VisitForStackValue(args->at(1));
3084 VisitForStackValue(args->at(2));
3085 VisitForStackValue(args->at(3));
3086 __ CallStub(&stub);
3087 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003088}
3089
3090
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003091void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3092 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003093 ASSERT(args->length() == 1);
3094
3095 VisitForAccumulatorValue(args->at(0)); // Load the object.
3096
3097 Label done;
3098 // If the object is a smi return the object.
3099 __ JumpIfSmi(v0, &done);
3100 // If the object is not a value type, return the object.
3101 __ GetObjectType(v0, a1, a1);
3102 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
3103
3104 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
3105
3106 __ bind(&done);
3107 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003108}
3109
3110
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003111void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3112 ZoneList<Expression*>* args = expr->arguments();
3113 ASSERT(args->length() == 2);
3114 ASSERT_NE(NULL, args->at(1)->AsLiteral());
3115 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->handle()));
3116
3117 VisitForAccumulatorValue(args->at(0)); // Load the object.
3118
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003119 Label runtime, done, not_date_object;
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003120 Register object = v0;
3121 Register result = v0;
3122 Register scratch0 = t5;
3123 Register scratch1 = a1;
3124
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003125 __ JumpIfSmi(object, &not_date_object);
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003126 __ GetObjectType(object, scratch1, scratch1);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003127 __ Branch(&not_date_object, ne, scratch1, Operand(JS_DATE_TYPE));
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003128
3129 if (index->value() == 0) {
3130 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset));
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003131 __ jmp(&done);
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003132 } else {
3133 if (index->value() < JSDate::kFirstUncachedField) {
3134 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3135 __ li(scratch1, Operand(stamp));
3136 __ lw(scratch1, MemOperand(scratch1));
3137 __ lw(scratch0, FieldMemOperand(object, JSDate::kCacheStampOffset));
3138 __ Branch(&runtime, ne, scratch1, Operand(scratch0));
3139 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset +
3140 kPointerSize * index->value()));
3141 __ jmp(&done);
3142 }
3143 __ bind(&runtime);
3144 __ PrepareCallCFunction(2, scratch1);
3145 __ li(a1, Operand(index));
3146 __ Move(a0, object);
3147 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003148 __ jmp(&done);
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003149 }
3150
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003151 __ bind(&not_date_object);
3152 __ CallRuntime(Runtime::kThrowNotDateError, 0);
3153 __ bind(&done);
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003154 context()->Plug(v0);
3155}
3156
3157
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003158void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3159 ZoneList<Expression*>* args = expr->arguments();
3160 ASSERT_EQ(3, args->length());
3161
3162 VisitForStackValue(args->at(1)); // index
3163 VisitForStackValue(args->at(2)); // value
3164 __ pop(a2);
3165 __ pop(a1);
3166 VisitForAccumulatorValue(args->at(0)); // string
3167
3168 static const String::Encoding encoding = String::ONE_BYTE_ENCODING;
3169 SeqStringSetCharGenerator::Generate(masm_, encoding, v0, a1, a2);
3170 context()->Plug(v0);
3171}
3172
3173
3174void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3175 ZoneList<Expression*>* args = expr->arguments();
3176 ASSERT_EQ(3, args->length());
3177
3178 VisitForStackValue(args->at(1)); // index
3179 VisitForStackValue(args->at(2)); // value
3180 __ pop(a2);
3181 __ pop(a1);
3182 VisitForAccumulatorValue(args->at(0)); // string
3183
3184 static const String::Encoding encoding = String::TWO_BYTE_ENCODING;
3185 SeqStringSetCharGenerator::Generate(masm_, encoding, v0, a1, a2);
3186 context()->Plug(v0);
3187}
3188
3189
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003190void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003191 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003192 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003193 ASSERT(args->length() == 2);
3194 VisitForStackValue(args->at(0));
3195 VisitForStackValue(args->at(1));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003196 if (CpuFeatures::IsSupported(FPU)) {
3197 MathPowStub stub(MathPowStub::ON_STACK);
3198 __ CallStub(&stub);
3199 } else {
3200 __ CallRuntime(Runtime::kMath_pow, 2);
3201 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003202 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003203}
3204
3205
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003206void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3207 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003208 ASSERT(args->length() == 2);
3209
3210 VisitForStackValue(args->at(0)); // Load the object.
3211 VisitForAccumulatorValue(args->at(1)); // Load the value.
3212 __ pop(a1); // v0 = value. a1 = object.
3213
3214 Label done;
3215 // If the object is a smi, return the value.
3216 __ JumpIfSmi(a1, &done);
3217
3218 // If the object is not a value type, return the value.
3219 __ GetObjectType(a1, a2, a2);
3220 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
3221
3222 // Store the value.
3223 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
3224 // Update the write barrier. Save the value as it will be
3225 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003226 __ mov(a2, v0);
3227 __ RecordWriteField(
3228 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003229
3230 __ bind(&done);
3231 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003232}
3233
3234
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003235void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3236 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003237 ASSERT_EQ(args->length(), 1);
3238
3239 // Load the argument on the stack and call the stub.
3240 VisitForStackValue(args->at(0));
3241
3242 NumberToStringStub stub;
3243 __ CallStub(&stub);
3244 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003245}
3246
3247
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003248void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3249 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003250 ASSERT(args->length() == 1);
3251
3252 VisitForAccumulatorValue(args->at(0));
3253
3254 Label done;
3255 StringCharFromCodeGenerator generator(v0, a1);
3256 generator.GenerateFast(masm_);
3257 __ jmp(&done);
3258
3259 NopRuntimeCallHelper call_helper;
3260 generator.GenerateSlow(masm_, call_helper);
3261
3262 __ bind(&done);
3263 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003264}
3265
3266
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003267void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3268 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003269 ASSERT(args->length() == 2);
3270
3271 VisitForStackValue(args->at(0));
3272 VisitForAccumulatorValue(args->at(1));
3273 __ mov(a0, result_register());
3274
3275 Register object = a1;
3276 Register index = a0;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003277 Register result = v0;
3278
3279 __ pop(object);
3280
3281 Label need_conversion;
3282 Label index_out_of_range;
3283 Label done;
3284 StringCharCodeAtGenerator generator(object,
3285 index,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003286 result,
3287 &need_conversion,
3288 &need_conversion,
3289 &index_out_of_range,
3290 STRING_INDEX_IS_NUMBER);
3291 generator.GenerateFast(masm_);
3292 __ jmp(&done);
3293
3294 __ bind(&index_out_of_range);
3295 // When the index is out of range, the spec requires us to return
3296 // NaN.
3297 __ LoadRoot(result, Heap::kNanValueRootIndex);
3298 __ jmp(&done);
3299
3300 __ bind(&need_conversion);
3301 // Load the undefined value into the result register, which will
3302 // trigger conversion.
3303 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3304 __ jmp(&done);
3305
3306 NopRuntimeCallHelper call_helper;
3307 generator.GenerateSlow(masm_, call_helper);
3308
3309 __ bind(&done);
3310 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003311}
3312
3313
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003314void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3315 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003316 ASSERT(args->length() == 2);
3317
3318 VisitForStackValue(args->at(0));
3319 VisitForAccumulatorValue(args->at(1));
3320 __ mov(a0, result_register());
3321
3322 Register object = a1;
3323 Register index = a0;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003324 Register scratch = a3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003325 Register result = v0;
3326
3327 __ pop(object);
3328
3329 Label need_conversion;
3330 Label index_out_of_range;
3331 Label done;
3332 StringCharAtGenerator generator(object,
3333 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003334 scratch,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003335 result,
3336 &need_conversion,
3337 &need_conversion,
3338 &index_out_of_range,
3339 STRING_INDEX_IS_NUMBER);
3340 generator.GenerateFast(masm_);
3341 __ jmp(&done);
3342
3343 __ bind(&index_out_of_range);
3344 // When the index is out of range, the spec requires us to return
3345 // the empty string.
3346 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3347 __ jmp(&done);
3348
3349 __ bind(&need_conversion);
3350 // Move smi zero into the result register, which will trigger
3351 // conversion.
3352 __ li(result, Operand(Smi::FromInt(0)));
3353 __ jmp(&done);
3354
3355 NopRuntimeCallHelper call_helper;
3356 generator.GenerateSlow(masm_, call_helper);
3357
3358 __ bind(&done);
3359 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003360}
3361
3362
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003363void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3364 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003365 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003366 VisitForStackValue(args->at(0));
3367 VisitForStackValue(args->at(1));
3368
3369 StringAddStub stub(NO_STRING_ADD_FLAGS);
3370 __ CallStub(&stub);
3371 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003372}
3373
3374
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003375void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3376 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003377 ASSERT_EQ(2, args->length());
3378
3379 VisitForStackValue(args->at(0));
3380 VisitForStackValue(args->at(1));
3381
3382 StringCompareStub stub;
3383 __ CallStub(&stub);
3384 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003385}
3386
3387
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003388void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003389 // Load the argument on the stack and call the stub.
3390 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3391 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003392 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003393 ASSERT(args->length() == 1);
3394 VisitForStackValue(args->at(0));
3395 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3396 __ CallStub(&stub);
3397 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003398}
3399
3400
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003401void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003402 // Load the argument on the stack and call the stub.
3403 TranscendentalCacheStub stub(TranscendentalCache::COS,
3404 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003405 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003406 ASSERT(args->length() == 1);
3407 VisitForStackValue(args->at(0));
3408 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3409 __ CallStub(&stub);
3410 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003411}
3412
3413
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003414void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3415 // Load the argument on the stack and call the stub.
3416 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3417 TranscendentalCacheStub::TAGGED);
3418 ZoneList<Expression*>* args = expr->arguments();
3419 ASSERT(args->length() == 1);
3420 VisitForStackValue(args->at(0));
3421 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3422 __ CallStub(&stub);
3423 context()->Plug(v0);
3424}
3425
3426
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003427void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003428 // Load the argument on the stack and call the stub.
3429 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3430 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003431 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003432 ASSERT(args->length() == 1);
3433 VisitForStackValue(args->at(0));
3434 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3435 __ CallStub(&stub);
3436 context()->Plug(v0);
3437}
3438
3439
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003440void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003441 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003442 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003443 ASSERT(args->length() == 1);
3444 VisitForStackValue(args->at(0));
3445 __ CallRuntime(Runtime::kMath_sqrt, 1);
3446 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003447}
3448
3449
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003450void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3451 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003452 ASSERT(args->length() >= 2);
3453
3454 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3455 for (int i = 0; i < arg_count + 1; i++) {
3456 VisitForStackValue(args->at(i));
3457 }
3458 VisitForAccumulatorValue(args->last()); // Function.
3459
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00003460 Label runtime, done;
3461 // Check for non-function argument (including proxy).
3462 __ JumpIfSmi(v0, &runtime);
danno@chromium.orgc612e022011-11-10 11:38:15 +00003463 __ GetObjectType(v0, a1, a1);
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00003464 __ Branch(&runtime, ne, a1, Operand(JS_FUNCTION_TYPE));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003465
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003466 // InvokeFunction requires the function in a1. Move it in there.
3467 __ mov(a1, result_register());
3468 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003469 __ InvokeFunction(a1, count, CALL_FUNCTION,
3470 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003471 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003472 __ jmp(&done);
3473
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00003474 __ bind(&runtime);
danno@chromium.orgc612e022011-11-10 11:38:15 +00003475 __ push(v0);
3476 __ CallRuntime(Runtime::kCall, args->length());
3477 __ bind(&done);
3478
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003479 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003480}
3481
3482
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003483void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003484 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003485 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003486 ASSERT(args->length() == 3);
3487 VisitForStackValue(args->at(0));
3488 VisitForStackValue(args->at(1));
3489 VisitForStackValue(args->at(2));
3490 __ CallStub(&stub);
3491 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003492}
3493
3494
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003495void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3496 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003497 ASSERT_EQ(2, args->length());
3498
3499 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3500 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3501
3502 Handle<FixedArray> jsfunction_result_caches(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003503 isolate()->native_context()->jsfunction_result_caches());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003504 if (jsfunction_result_caches->length() <= cache_id) {
3505 __ Abort("Attempt to use undefined cache.");
3506 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3507 context()->Plug(v0);
3508 return;
3509 }
3510
3511 VisitForAccumulatorValue(args->at(1));
3512
3513 Register key = v0;
3514 Register cache = a1;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003515 __ lw(cache, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
3516 __ lw(cache, FieldMemOperand(cache, GlobalObject::kNativeContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003517 __ lw(cache,
3518 ContextOperand(
3519 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3520 __ lw(cache,
3521 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3522
3523
3524 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003525 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003526 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3527 // a2 now holds finger offset as a smi.
3528 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3529 // a3 now points to the start of fixed array elements.
3530 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3531 __ addu(a3, a3, at);
3532 // a3 now points to key of indexed element of cache.
3533 __ lw(a2, MemOperand(a3));
3534 __ Branch(&not_found, ne, key, Operand(a2));
3535
3536 __ lw(v0, MemOperand(a3, kPointerSize));
3537 __ Branch(&done);
3538
3539 __ bind(&not_found);
3540 // Call runtime to perform the lookup.
3541 __ Push(cache, key);
3542 __ CallRuntime(Runtime::kGetFromCache, 2);
3543
3544 __ bind(&done);
3545 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003546}
3547
3548
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003549void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3550 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003551 ASSERT_EQ(2, args->length());
3552
3553 Register right = v0;
3554 Register left = a1;
3555 Register tmp = a2;
3556 Register tmp2 = a3;
3557
3558 VisitForStackValue(args->at(0));
3559 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3560 __ pop(left);
3561
3562 Label done, fail, ok;
3563 __ Branch(&ok, eq, left, Operand(right));
3564 // Fail if either is a non-HeapObject.
3565 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003566 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003567 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3568 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3569 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3570 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3571 __ Branch(&fail, ne, tmp, Operand(tmp2));
3572 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3573 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3574 __ Branch(&ok, eq, tmp, Operand(tmp2));
3575 __ bind(&fail);
3576 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3577 __ jmp(&done);
3578 __ bind(&ok);
3579 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3580 __ bind(&done);
3581
3582 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003583}
3584
3585
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003586void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3587 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003588 VisitForAccumulatorValue(args->at(0));
3589
3590 Label materialize_true, materialize_false;
3591 Label* if_true = NULL;
3592 Label* if_false = NULL;
3593 Label* fall_through = NULL;
3594 context()->PrepareTest(&materialize_true, &materialize_false,
3595 &if_true, &if_false, &fall_through);
3596
3597 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3598 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3599
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003600 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003601 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3602
3603 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003604}
3605
3606
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003607void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3608 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003609 ASSERT(args->length() == 1);
3610 VisitForAccumulatorValue(args->at(0));
3611
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003612 __ AssertString(v0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003613
3614 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3615 __ IndexFromHash(v0, v0);
3616
3617 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003618}
3619
3620
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003621void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003622 Label bailout, done, one_char_separator, long_separator,
3623 non_trivial_array, not_size_one_array, loop,
3624 empty_separator_loop, one_char_separator_loop,
3625 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003626 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003627 ASSERT(args->length() == 2);
3628 VisitForStackValue(args->at(1));
3629 VisitForAccumulatorValue(args->at(0));
3630
3631 // All aliases of the same register have disjoint lifetimes.
3632 Register array = v0;
3633 Register elements = no_reg; // Will be v0.
3634 Register result = no_reg; // Will be v0.
3635 Register separator = a1;
3636 Register array_length = a2;
3637 Register result_pos = no_reg; // Will be a2.
3638 Register string_length = a3;
3639 Register string = t0;
3640 Register element = t1;
3641 Register elements_end = t2;
3642 Register scratch1 = t3;
3643 Register scratch2 = t5;
3644 Register scratch3 = t4;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003645
3646 // Separator operand is on the stack.
3647 __ pop(separator);
3648
3649 // Check that the array is a JSArray.
3650 __ JumpIfSmi(array, &bailout);
3651 __ GetObjectType(array, scratch1, scratch2);
3652 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3653
3654 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003655 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003656
3657 // If the array has length zero, return the empty string.
3658 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3659 __ SmiUntag(array_length);
3660 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3661 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3662 __ Branch(&done);
3663
3664 __ bind(&non_trivial_array);
3665
3666 // Get the FixedArray containing array's elements.
3667 elements = array;
3668 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3669 array = no_reg; // End of array's live range.
3670
3671 // Check that all array elements are sequential ASCII strings, and
3672 // accumulate the sum of their lengths, as a smi-encoded value.
3673 __ mov(string_length, zero_reg);
3674 __ Addu(element,
3675 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3676 __ sll(elements_end, array_length, kPointerSizeLog2);
3677 __ Addu(elements_end, element, elements_end);
3678 // Loop condition: while (element < elements_end).
3679 // Live values in registers:
3680 // elements: Fixed array of strings.
3681 // array_length: Length of the fixed array of strings (not smi)
3682 // separator: Separator string
3683 // string_length: Accumulated sum of string lengths (smi).
3684 // element: Current array element.
3685 // elements_end: Array end.
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00003686 if (generate_debug_code_) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003687 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3688 array_length, Operand(zero_reg));
3689 }
3690 __ bind(&loop);
3691 __ lw(string, MemOperand(element));
3692 __ Addu(element, element, kPointerSize);
3693 __ JumpIfSmi(string, &bailout);
3694 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3695 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3696 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003697 __ lw(scratch1, FieldMemOperand(string, SeqOneByteString::kLengthOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003698 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3699 __ BranchOnOverflow(&bailout, scratch3);
3700 __ Branch(&loop, lt, element, Operand(elements_end));
3701
3702 // If array_length is 1, return elements[0], a string.
3703 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3704 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3705 __ Branch(&done);
3706
3707 __ bind(&not_size_one_array);
3708
3709 // Live values in registers:
3710 // separator: Separator string
3711 // array_length: Length of the array.
3712 // string_length: Sum of string lengths (smi).
3713 // elements: FixedArray of strings.
3714
3715 // Check that the separator is a flat ASCII string.
3716 __ JumpIfSmi(separator, &bailout);
3717 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3718 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3719 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3720
3721 // Add (separator length times array_length) - separator length to the
3722 // string_length to get the length of the result string. array_length is not
3723 // smi but the other values are, so the result is a smi.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003724 __ lw(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003725 __ Subu(string_length, string_length, Operand(scratch1));
3726 __ Mult(array_length, scratch1);
3727 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3728 // zero.
3729 __ mfhi(scratch2);
3730 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3731 __ mflo(scratch2);
3732 __ And(scratch3, scratch2, Operand(0x80000000));
3733 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3734 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3735 __ BranchOnOverflow(&bailout, scratch3);
3736 __ SmiUntag(string_length);
3737
3738 // Get first element in the array to free up the elements register to be used
3739 // for the result.
3740 __ Addu(element,
3741 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3742 result = elements; // End of live range for elements.
3743 elements = no_reg;
3744 // Live values in registers:
3745 // element: First array element
3746 // separator: Separator string
3747 // string_length: Length of result string (not smi)
3748 // array_length: Length of the array.
3749 __ AllocateAsciiString(result,
3750 string_length,
3751 scratch1,
3752 scratch2,
3753 elements_end,
3754 &bailout);
3755 // Prepare for looping. Set up elements_end to end of the array. Set
3756 // result_pos to the position of the result where to write the first
3757 // character.
3758 __ sll(elements_end, array_length, kPointerSizeLog2);
3759 __ Addu(elements_end, element, elements_end);
3760 result_pos = array_length; // End of live range for array_length.
3761 array_length = no_reg;
3762 __ Addu(result_pos,
3763 result,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003764 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003765
3766 // Check the length of the separator.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003767 __ lw(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003768 __ li(at, Operand(Smi::FromInt(1)));
3769 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3770 __ Branch(&long_separator, gt, scratch1, Operand(at));
3771
3772 // Empty separator case.
3773 __ bind(&empty_separator_loop);
3774 // Live values in registers:
3775 // result_pos: the position to which we are currently copying characters.
3776 // element: Current array element.
3777 // elements_end: Array end.
3778
3779 // Copy next array element to the result.
3780 __ lw(string, MemOperand(element));
3781 __ Addu(element, element, kPointerSize);
3782 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3783 __ SmiUntag(string_length);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003784 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003785 __ CopyBytes(string, result_pos, string_length, scratch1);
3786 // End while (element < elements_end).
3787 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3788 ASSERT(result.is(v0));
3789 __ Branch(&done);
3790
3791 // One-character separator case.
3792 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003793 // Replace separator with its ASCII character value.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003794 __ lbu(separator, FieldMemOperand(separator, SeqOneByteString::kHeaderSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003795 // Jump into the loop after the code that copies the separator, so the first
3796 // element is not preceded by a separator.
3797 __ jmp(&one_char_separator_loop_entry);
3798
3799 __ bind(&one_char_separator_loop);
3800 // Live values in registers:
3801 // result_pos: the position to which we are currently copying characters.
3802 // element: Current array element.
3803 // elements_end: Array end.
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003804 // separator: Single separator ASCII char (in lower byte).
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003805
3806 // Copy the separator character to the result.
3807 __ sb(separator, MemOperand(result_pos));
3808 __ Addu(result_pos, result_pos, 1);
3809
3810 // Copy next array element to the result.
3811 __ bind(&one_char_separator_loop_entry);
3812 __ lw(string, MemOperand(element));
3813 __ Addu(element, element, kPointerSize);
3814 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3815 __ SmiUntag(string_length);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003816 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003817 __ CopyBytes(string, result_pos, string_length, scratch1);
3818 // End while (element < elements_end).
3819 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3820 ASSERT(result.is(v0));
3821 __ Branch(&done);
3822
3823 // Long separator case (separator is more than one character). Entry is at the
3824 // label long_separator below.
3825 __ bind(&long_separator_loop);
3826 // Live values in registers:
3827 // result_pos: the position to which we are currently copying characters.
3828 // element: Current array element.
3829 // elements_end: Array end.
3830 // separator: Separator string.
3831
3832 // Copy the separator to the result.
3833 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3834 __ SmiUntag(string_length);
3835 __ Addu(string,
3836 separator,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003837 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003838 __ CopyBytes(string, result_pos, string_length, scratch1);
3839
3840 __ bind(&long_separator);
3841 __ lw(string, MemOperand(element));
3842 __ Addu(element, element, kPointerSize);
3843 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3844 __ SmiUntag(string_length);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003845 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003846 __ CopyBytes(string, result_pos, string_length, scratch1);
3847 // End while (element < elements_end).
3848 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3849 ASSERT(result.is(v0));
3850 __ Branch(&done);
3851
3852 __ bind(&bailout);
3853 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3854 __ bind(&done);
3855 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003856}
3857
3858
ager@chromium.org5c838252010-02-19 08:53:10 +00003859void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003860 Handle<String> name = expr->name();
3861 if (name->length() > 0 && name->Get(0) == '_') {
3862 Comment cmnt(masm_, "[ InlineRuntimeCall");
3863 EmitInlineRuntimeCall(expr);
3864 return;
3865 }
3866
3867 Comment cmnt(masm_, "[ CallRuntime");
3868 ZoneList<Expression*>* args = expr->arguments();
3869
3870 if (expr->is_jsruntime()) {
3871 // Prepare for calling JS runtime function.
3872 __ lw(a0, GlobalObjectOperand());
3873 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3874 __ push(a0);
3875 }
3876
3877 // Push the arguments ("left-to-right").
3878 int arg_count = args->length();
3879 for (int i = 0; i < arg_count; i++) {
3880 VisitForStackValue(args->at(i));
3881 }
3882
3883 if (expr->is_jsruntime()) {
3884 // Call the JS runtime function.
3885 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003886 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003887 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003888 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00003889 CallIC(ic, mode, expr->CallRuntimeFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003890 // Restore context register.
3891 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3892 } else {
3893 // Call the C runtime function.
3894 __ CallRuntime(expr->function(), arg_count);
3895 }
3896 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003897}
3898
3899
3900void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003901 switch (expr->op()) {
3902 case Token::DELETE: {
3903 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003904 Property* property = expr->expression()->AsProperty();
3905 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003906
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003907 if (property != NULL) {
3908 VisitForStackValue(property->obj());
3909 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003910 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
3911 ? kNonStrictMode : kStrictMode;
3912 __ li(a1, Operand(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003913 __ push(a1);
3914 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3915 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003916 } else if (proxy != NULL) {
3917 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003918 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003919 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003920 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003921 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003922 __ lw(a2, GlobalObjectOperand());
3923 __ li(a1, Operand(var->name()));
3924 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3925 __ Push(a2, a1, a0);
3926 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3927 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003928 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003929 // Result of deleting non-global, non-dynamic variables is false.
3930 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003931 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003932 } else {
3933 // Non-global variable. Call the runtime to try to delete from the
3934 // context where the variable was introduced.
3935 __ push(context_register());
3936 __ li(a2, Operand(var->name()));
3937 __ push(a2);
3938 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3939 context()->Plug(v0);
3940 }
3941 } else {
3942 // Result of deleting non-property, non-variable reference is true.
3943 // The subexpression may have side effects.
3944 VisitForEffect(expr->expression());
3945 context()->Plug(true);
3946 }
3947 break;
3948 }
3949
3950 case Token::VOID: {
3951 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3952 VisitForEffect(expr->expression());
3953 context()->Plug(Heap::kUndefinedValueRootIndex);
3954 break;
3955 }
3956
3957 case Token::NOT: {
3958 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3959 if (context()->IsEffect()) {
3960 // Unary NOT has no side effects so it's only necessary to visit the
3961 // subexpression. Match the optimizing compiler by not branching.
3962 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003963 } else if (context()->IsTest()) {
3964 const TestContext* test = TestContext::cast(context());
3965 // The labels are swapped for the recursive call.
3966 VisitForControl(expr->expression(),
3967 test->false_label(),
3968 test->true_label(),
3969 test->fall_through());
3970 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003971 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003972 // We handle value contexts explicitly rather than simply visiting
3973 // for control and plugging the control flow into the context,
3974 // because we need to prepare a pair of extra administrative AST ids
3975 // for the optimizing compiler.
3976 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3977 Label materialize_true, materialize_false, done;
3978 VisitForControl(expr->expression(),
3979 &materialize_false,
3980 &materialize_true,
3981 &materialize_true);
3982 __ bind(&materialize_true);
3983 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3984 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3985 if (context()->IsStackValue()) __ push(v0);
3986 __ jmp(&done);
3987 __ bind(&materialize_false);
3988 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3989 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3990 if (context()->IsStackValue()) __ push(v0);
3991 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003992 }
3993 break;
3994 }
3995
3996 case Token::TYPEOF: {
3997 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3998 { StackValueContext context(this);
3999 VisitForTypeofValue(expr->expression());
4000 }
4001 __ CallRuntime(Runtime::kTypeof, 1);
4002 context()->Plug(v0);
4003 break;
4004 }
4005
4006 case Token::ADD: {
4007 Comment cmt(masm_, "[ UnaryOperation (ADD)");
4008 VisitForAccumulatorValue(expr->expression());
4009 Label no_conversion;
4010 __ JumpIfSmi(result_register(), &no_conversion);
4011 __ mov(a0, result_register());
4012 ToNumberStub convert_stub;
4013 __ CallStub(&convert_stub);
4014 __ bind(&no_conversion);
4015 context()->Plug(result_register());
4016 break;
4017 }
4018
4019 case Token::SUB:
4020 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
4021 break;
4022
4023 case Token::BIT_NOT:
4024 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
4025 break;
4026
4027 default:
4028 UNREACHABLE();
4029 }
4030}
4031
4032
4033void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
4034 const char* comment) {
4035 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
4036 Comment cmt(masm_, comment);
4037 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
4038 UnaryOverwriteMode overwrite =
4039 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004040 UnaryOpStub stub(expr->op(), overwrite);
4041 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004042 VisitForAccumulatorValue(expr->expression());
4043 SetSourcePosition(expr->position());
4044 __ mov(a0, result_register());
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004045 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET,
4046 expr->UnaryOperationFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004047 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004048}
4049
4050
4051void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004052 Comment cmnt(masm_, "[ CountOperation");
4053 SetSourcePosition(expr->position());
4054
4055 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
4056 // as the left-hand side.
4057 if (!expr->expression()->IsValidLeftHandSide()) {
4058 VisitForEffect(expr->expression());
4059 return;
4060 }
4061
4062 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004063 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004064 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
4065 LhsKind assign_type = VARIABLE;
4066 Property* prop = expr->expression()->AsProperty();
4067 // In case of a property we use the uninitialized expression context
4068 // of the key to detect a named property.
4069 if (prop != NULL) {
4070 assign_type =
4071 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
4072 }
4073
4074 // Evaluate expression and get value.
4075 if (assign_type == VARIABLE) {
4076 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
4077 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00004078 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004079 } else {
4080 // Reserve space for result of postfix operation.
4081 if (expr->is_postfix() && !context()->IsEffect()) {
4082 __ li(at, Operand(Smi::FromInt(0)));
4083 __ push(at);
4084 }
4085 if (assign_type == NAMED_PROPERTY) {
4086 // Put the object both on the stack and in the accumulator.
4087 VisitForAccumulatorValue(prop->obj());
4088 __ push(v0);
4089 EmitNamedPropertyLoad(prop);
4090 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004091 VisitForStackValue(prop->obj());
4092 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004093 __ lw(a1, MemOperand(sp, 0));
4094 __ push(v0);
4095 EmitKeyedPropertyLoad(prop);
4096 }
4097 }
4098
4099 // We need a second deoptimization point after loading the value
4100 // in case evaluating the property load my have a side effect.
4101 if (assign_type == VARIABLE) {
4102 PrepareForBailout(expr->expression(), TOS_REG);
4103 } else {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004104 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004105 }
4106
4107 // Call ToNumber only if operand is not a smi.
4108 Label no_conversion;
4109 __ JumpIfSmi(v0, &no_conversion);
4110 __ mov(a0, v0);
4111 ToNumberStub convert_stub;
4112 __ CallStub(&convert_stub);
4113 __ bind(&no_conversion);
4114
4115 // Save result for postfix expressions.
4116 if (expr->is_postfix()) {
4117 if (!context()->IsEffect()) {
4118 // Save the result on the stack. If we have a named or keyed property
4119 // we store the result under the receiver that is currently on top
4120 // of the stack.
4121 switch (assign_type) {
4122 case VARIABLE:
4123 __ push(v0);
4124 break;
4125 case NAMED_PROPERTY:
4126 __ sw(v0, MemOperand(sp, kPointerSize));
4127 break;
4128 case KEYED_PROPERTY:
4129 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4130 break;
4131 }
4132 }
4133 }
4134 __ mov(a0, result_register());
4135
4136 // Inline smi case if we are in a loop.
4137 Label stub_call, done;
4138 JumpPatchSite patch_site(masm_);
4139
4140 int count_value = expr->op() == Token::INC ? 1 : -1;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004141 if (ShouldInlineSmiCase(expr->op())) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00004142 __ li(a1, Operand(Smi::FromInt(count_value)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004143 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
4144 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
4145
4146 // We could eliminate this smi check if we split the code at
4147 // the first smi check before calling ToNumber.
4148 patch_site.EmitJumpIfSmi(v0, &done);
4149 __ bind(&stub_call);
4150 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00004151 __ mov(a1, a0);
4152 __ li(a0, Operand(Smi::FromInt(count_value)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004153
4154 // Record position before stub call.
4155 SetSourcePosition(expr->position());
4156
danno@chromium.org40cb8782011-05-25 07:58:50 +00004157 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004158 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountBinOpFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004159 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004160 __ bind(&done);
4161
4162 // Store the value returned in v0.
4163 switch (assign_type) {
4164 case VARIABLE:
4165 if (expr->is_postfix()) {
4166 { EffectContext context(this);
4167 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4168 Token::ASSIGN);
4169 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4170 context.Plug(v0);
4171 }
4172 // For all contexts except EffectConstant we have the result on
4173 // top of the stack.
4174 if (!context()->IsEffect()) {
4175 context()->PlugTOS();
4176 }
4177 } else {
4178 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4179 Token::ASSIGN);
4180 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4181 context()->Plug(v0);
4182 }
4183 break;
4184 case NAMED_PROPERTY: {
4185 __ mov(a0, result_register()); // Value.
4186 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4187 __ pop(a1); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004188 Handle<Code> ic = is_classic_mode()
4189 ? isolate()->builtins()->StoreIC_Initialize()
4190 : isolate()->builtins()->StoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004191 CallIC(ic, RelocInfo::CODE_TARGET, expr->CountStoreFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004192 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4193 if (expr->is_postfix()) {
4194 if (!context()->IsEffect()) {
4195 context()->PlugTOS();
4196 }
4197 } else {
4198 context()->Plug(v0);
4199 }
4200 break;
4201 }
4202 case KEYED_PROPERTY: {
4203 __ mov(a0, result_register()); // Value.
4204 __ pop(a1); // Key.
4205 __ pop(a2); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004206 Handle<Code> ic = is_classic_mode()
4207 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4208 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004209 CallIC(ic, RelocInfo::CODE_TARGET, expr->CountStoreFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004210 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4211 if (expr->is_postfix()) {
4212 if (!context()->IsEffect()) {
4213 context()->PlugTOS();
4214 }
4215 } else {
4216 context()->Plug(v0);
4217 }
4218 break;
4219 }
4220 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004221}
4222
4223
lrn@chromium.org7516f052011-03-30 08:52:27 +00004224void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004225 ASSERT(!context()->IsEffect());
4226 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004227 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004228 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004229 Comment cmnt(masm_, "Global variable");
4230 __ lw(a0, GlobalObjectOperand());
4231 __ li(a2, Operand(proxy->name()));
4232 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4233 // Use a regular load, not a contextual load, to avoid a reference
4234 // error.
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004235 CallIC(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004236 PrepareForBailout(expr, TOS_REG);
4237 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004238 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004239 Label done, slow;
4240
4241 // Generate code for loading from variables potentially shadowed
4242 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004243 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004244
4245 __ bind(&slow);
4246 __ li(a0, Operand(proxy->name()));
4247 __ Push(cp, a0);
4248 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4249 PrepareForBailout(expr, TOS_REG);
4250 __ bind(&done);
4251
4252 context()->Plug(v0);
4253 } else {
4254 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004255 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004256 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004257}
4258
ager@chromium.org04921a82011-06-27 13:21:41 +00004259void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004260 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004261 Handle<String> check) {
4262 Label materialize_true, materialize_false;
4263 Label* if_true = NULL;
4264 Label* if_false = NULL;
4265 Label* fall_through = NULL;
4266 context()->PrepareTest(&materialize_true, &materialize_false,
4267 &if_true, &if_false, &fall_through);
4268
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004269 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004270 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004271 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004272 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004273
4274 if (check->Equals(isolate()->heap()->number_symbol())) {
4275 __ JumpIfSmi(v0, if_true);
4276 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4277 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4278 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4279 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4280 __ JumpIfSmi(v0, if_false);
4281 // Check for undetectable objects => false.
4282 __ GetObjectType(v0, v0, a1);
4283 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4284 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4285 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4286 Split(eq, a1, Operand(zero_reg),
4287 if_true, if_false, fall_through);
4288 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4289 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4290 __ Branch(if_true, eq, v0, Operand(at));
4291 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4292 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004293 } else if (FLAG_harmony_typeof &&
4294 check->Equals(isolate()->heap()->null_symbol())) {
4295 __ LoadRoot(at, Heap::kNullValueRootIndex);
4296 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004297 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4298 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4299 __ Branch(if_true, eq, v0, Operand(at));
4300 __ JumpIfSmi(v0, if_false);
4301 // Check for undetectable objects => true.
4302 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4303 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4304 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4305 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4306 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4307 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004308 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4309 __ GetObjectType(v0, v0, a1);
4310 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4311 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4312 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004313 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4314 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004315 if (!FLAG_harmony_typeof) {
4316 __ LoadRoot(at, Heap::kNullValueRootIndex);
4317 __ Branch(if_true, eq, v0, Operand(at));
4318 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004319 // Check for JS objects => true.
4320 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004321 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004322 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004323 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004324 // Check for undetectable objects => false.
4325 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4326 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4327 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4328 } else {
4329 if (if_false != fall_through) __ jmp(if_false);
4330 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004331 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004332}
4333
4334
ager@chromium.org5c838252010-02-19 08:53:10 +00004335void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004336 Comment cmnt(masm_, "[ CompareOperation");
4337 SetSourcePosition(expr->position());
4338
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004339 // First we try a fast inlined version of the compare when one of
4340 // the operands is a literal.
4341 if (TryLiteralCompare(expr)) return;
4342
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004343 // Always perform the comparison for its control flow. Pack the result
4344 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004345 Label materialize_true, materialize_false;
4346 Label* if_true = NULL;
4347 Label* if_false = NULL;
4348 Label* fall_through = NULL;
4349 context()->PrepareTest(&materialize_true, &materialize_false,
4350 &if_true, &if_false, &fall_through);
4351
ager@chromium.org04921a82011-06-27 13:21:41 +00004352 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004353 VisitForStackValue(expr->left());
4354 switch (op) {
4355 case Token::IN:
4356 VisitForStackValue(expr->right());
4357 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004358 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004359 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4360 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4361 break;
4362
4363 case Token::INSTANCEOF: {
4364 VisitForStackValue(expr->right());
4365 InstanceofStub stub(InstanceofStub::kNoFlags);
4366 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004367 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004368 // The stub returns 0 for true.
4369 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4370 break;
4371 }
4372
4373 default: {
4374 VisitForAccumulatorValue(expr->right());
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00004375 Condition cc = CompareIC::ComputeCondition(op);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004376 __ mov(a0, result_register());
4377 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004378
4379 bool inline_smi_code = ShouldInlineSmiCase(op);
4380 JumpPatchSite patch_site(masm_);
4381 if (inline_smi_code) {
4382 Label slow_case;
4383 __ Or(a2, a0, Operand(a1));
4384 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4385 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4386 __ bind(&slow_case);
4387 }
4388 // Record position and call the compare IC.
4389 SetSourcePosition(expr->position());
4390 Handle<Code> ic = CompareIC::GetUninitialized(op);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004391 CallIC(ic, RelocInfo::CODE_TARGET, expr->CompareOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004392 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004393 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004394 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4395 }
4396 }
4397
4398 // Convert the result of the comparison into one expected for this
4399 // expression's context.
4400 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004401}
4402
4403
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004404void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4405 Expression* sub_expr,
4406 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004407 Label materialize_true, materialize_false;
4408 Label* if_true = NULL;
4409 Label* if_false = NULL;
4410 Label* fall_through = NULL;
4411 context()->PrepareTest(&materialize_true, &materialize_false,
4412 &if_true, &if_false, &fall_through);
4413
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004414 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004415 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004416 Heap::RootListIndex nil_value = nil == kNullValue ?
4417 Heap::kNullValueRootIndex :
4418 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004419 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004420 __ LoadRoot(a1, nil_value);
4421 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004422 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4423 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004424 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4425 Heap::kUndefinedValueRootIndex :
4426 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004427 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004428 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004429 __ Branch(if_true, eq, a0, Operand(a1));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004430 __ JumpIfSmi(a0, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004431 // It can be an undetectable object.
4432 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4433 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4434 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4435 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4436 }
4437 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004438}
4439
4440
ager@chromium.org5c838252010-02-19 08:53:10 +00004441void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004442 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4443 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004444}
4445
4446
lrn@chromium.org7516f052011-03-30 08:52:27 +00004447Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004448 return v0;
4449}
ager@chromium.org5c838252010-02-19 08:53:10 +00004450
4451
lrn@chromium.org7516f052011-03-30 08:52:27 +00004452Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004453 return cp;
4454}
4455
4456
ager@chromium.org5c838252010-02-19 08:53:10 +00004457void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004458 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4459 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004460}
4461
4462
4463void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004464 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004465}
4466
4467
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004468void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4469 Scope* declaration_scope = scope()->DeclarationScope();
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004470 if (declaration_scope->is_global_scope() ||
4471 declaration_scope->is_module_scope()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00004472 // Contexts nested in the native context have a canonical empty function
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004473 // as their closure, not the anonymous closure containing the global
4474 // code. Pass a smi sentinel and let the runtime look up the empty
4475 // function.
4476 __ li(at, Operand(Smi::FromInt(0)));
4477 } else if (declaration_scope->is_eval_scope()) {
4478 // Contexts created by a call to eval have the same closure as the
4479 // context calling eval, not the anonymous closure containing the eval
4480 // code. Fetch it from the context.
4481 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4482 } else {
4483 ASSERT(declaration_scope->is_function_scope());
4484 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4485 }
4486 __ push(at);
4487}
4488
4489
ager@chromium.org5c838252010-02-19 08:53:10 +00004490// ----------------------------------------------------------------------------
4491// Non-local control flow support.
4492
4493void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004494 ASSERT(!result_register().is(a1));
4495 // Store result register while executing finally block.
4496 __ push(result_register());
4497 // Cook return address in link register to stack (smi encoded Code* delta).
4498 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4499 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004500 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004501 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004502
4503 // Store result register while executing finally block.
4504 __ push(a1);
4505
4506 // Store pending message while executing finally block.
4507 ExternalReference pending_message_obj =
4508 ExternalReference::address_of_pending_message_obj(isolate());
4509 __ li(at, Operand(pending_message_obj));
4510 __ lw(a1, MemOperand(at));
4511 __ push(a1);
4512
4513 ExternalReference has_pending_message =
4514 ExternalReference::address_of_has_pending_message(isolate());
4515 __ li(at, Operand(has_pending_message));
4516 __ lw(a1, MemOperand(at));
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004517 __ SmiTag(a1);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004518 __ push(a1);
4519
4520 ExternalReference pending_message_script =
4521 ExternalReference::address_of_pending_message_script(isolate());
4522 __ li(at, Operand(pending_message_script));
4523 __ lw(a1, MemOperand(at));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004524 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004525}
4526
4527
4528void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004529 ASSERT(!result_register().is(a1));
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004530 // Restore pending message from stack.
4531 __ pop(a1);
4532 ExternalReference pending_message_script =
4533 ExternalReference::address_of_pending_message_script(isolate());
4534 __ li(at, Operand(pending_message_script));
4535 __ sw(a1, MemOperand(at));
4536
4537 __ pop(a1);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004538 __ SmiUntag(a1);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004539 ExternalReference has_pending_message =
4540 ExternalReference::address_of_has_pending_message(isolate());
4541 __ li(at, Operand(has_pending_message));
4542 __ sw(a1, MemOperand(at));
4543
4544 __ pop(a1);
4545 ExternalReference pending_message_obj =
4546 ExternalReference::address_of_pending_message_obj(isolate());
4547 __ li(at, Operand(pending_message_obj));
4548 __ sw(a1, MemOperand(at));
4549
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004550 // Restore result register from stack.
4551 __ pop(a1);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004552
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004553 // Uncook return address and return.
4554 __ pop(result_register());
4555 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4556 __ sra(a1, a1, 1); // Un-smi-tag value.
4557 __ Addu(at, a1, Operand(masm_->CodeObject()));
4558 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004559}
4560
4561
4562#undef __
4563
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004564#define __ ACCESS_MASM(masm())
4565
4566FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4567 int* stack_depth,
4568 int* context_length) {
4569 // The macros used here must preserve the result register.
4570
4571 // Because the handler block contains the context of the finally
4572 // code, we can restore it directly from there for the finally code
4573 // rather than iteratively unwinding contexts via their previous
4574 // links.
4575 __ Drop(*stack_depth); // Down to the handler block.
4576 if (*context_length > 0) {
4577 // Restore the context to its dedicated register and the stack.
4578 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4579 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4580 }
4581 __ PopTryHandler();
4582 __ Call(finally_entry_);
4583
4584 *stack_depth = 0;
4585 *context_length = 0;
4586 return previous_;
4587}
4588
4589
4590#undef __
4591
ager@chromium.org5c838252010-02-19 08:53:10 +00004592} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004593
4594#endif // V8_TARGET_ARCH_MIPS