blob: 872af86a95f6595ab34fa6f0413a2868ae2d7615 [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
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +0000173 info->set_prologue_offset(masm_->pc_offset());
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000174 // The following three instructions must remain together and unmodified for
175 // code aging to work properly.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000176 __ Push(ra, fp, cp, a1);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000177 // Load undefined value here, so the value is ready for the loop
178 // below.
179 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000180 // Adjust fp to point to caller's fp.
181 __ Addu(fp, sp, Operand(2 * kPointerSize));
182
183 { Comment cmnt(masm_, "[ Allocate locals");
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000184 int locals_count = info->scope()->num_stack_slots();
185 // Generators allocate locals, if any, in context slots.
186 ASSERT(!info->function()->is_generator() || locals_count == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000187 for (int i = 0; i < locals_count; i++) {
188 __ push(at);
189 }
190 }
191
192 bool function_in_register = true;
193
194 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000195 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000196 if (heap_slots > 0) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000197 Comment cmnt(masm_, "[ Allocate context");
198 // Argument to NewContext is the function, which is still in a1.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000199 __ push(a1);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000200 if (FLAG_harmony_scoping && info->scope()->is_global_scope()) {
201 __ Push(info->scope()->GetScopeInfo());
202 __ CallRuntime(Runtime::kNewGlobalContext, 2);
203 } else if (heap_slots <= FastNewContextStub::kMaximumSlots) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000204 FastNewContextStub stub(heap_slots);
205 __ CallStub(&stub);
206 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000207 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000208 }
209 function_in_register = false;
210 // Context is returned in both v0 and cp. It replaces the context
211 // passed to us. It's saved in the stack and kept live in cp.
212 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
213 // Copy any necessary parameters into the context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000214 int num_parameters = info->scope()->num_parameters();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000215 for (int i = 0; i < num_parameters; i++) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000216 Variable* var = scope()->parameter(i);
217 if (var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000218 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
219 (num_parameters - 1 - i) * kPointerSize;
220 // Load parameter from stack.
221 __ lw(a0, MemOperand(fp, parameter_offset));
222 // Store it in the context.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000223 MemOperand target = ContextOperand(cp, var->index());
224 __ sw(a0, target);
225
226 // Update the write barrier.
227 __ RecordWriteContextSlot(
228 cp, target.offset(), a0, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000229 }
230 }
231 }
232
233 Variable* arguments = scope()->arguments();
234 if (arguments != NULL) {
235 // Function uses arguments object.
236 Comment cmnt(masm_, "[ Allocate arguments object");
237 if (!function_in_register) {
238 // Load this again, if it's used by the local context below.
239 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
240 } else {
241 __ mov(a3, a1);
242 }
243 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000244 int num_parameters = info->scope()->num_parameters();
245 int offset = num_parameters * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000246 __ Addu(a2, fp,
247 Operand(StandardFrameConstants::kCallerSPOffset + offset));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000248 __ li(a1, Operand(Smi::FromInt(num_parameters)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000249 __ Push(a3, a2, a1);
250
251 // Arguments to ArgumentsAccessStub:
252 // function, receiver address, parameter count.
253 // The stub will rewrite receiever and parameter count if the previous
254 // stack frame was an arguments adapter frame.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000255 ArgumentsAccessStub::Type type;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000256 if (!is_classic_mode()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000257 type = ArgumentsAccessStub::NEW_STRICT;
258 } else if (function()->has_duplicate_parameters()) {
259 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
260 } else {
261 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
262 }
263 ArgumentsAccessStub stub(type);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000264 __ CallStub(&stub);
265
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000266 SetVar(arguments, v0, a1, a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000267 }
268
269 if (FLAG_trace) {
270 __ CallRuntime(Runtime::kTraceEnter, 0);
271 }
272
273 // Visit the declarations and body unless there is an illegal
274 // redeclaration.
275 if (scope()->HasIllegalRedeclaration()) {
276 Comment cmnt(masm_, "[ Declarations");
277 scope()->VisitIllegalRedeclaration(this);
278
279 } else {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000280 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000281 { Comment cmnt(masm_, "[ Declarations");
282 // For named function expressions, declare the function name as a
283 // constant.
284 if (scope()->is_function_scope() && scope()->function() != NULL) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000285 VariableDeclaration* function = scope()->function();
286 ASSERT(function->proxy()->var()->mode() == CONST ||
287 function->proxy()->var()->mode() == CONST_HARMONY);
288 ASSERT(function->proxy()->var()->location() != Variable::UNALLOCATED);
289 VisitVariableDeclaration(function);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000290 }
291 VisitDeclarations(scope()->declarations());
292 }
293
294 { Comment cmnt(masm_, "[ Stack check");
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000295 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000296 Label ok;
297 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
298 __ Branch(&ok, hs, sp, Operand(t0));
299 StackCheckStub stub;
300 __ CallStub(&stub);
301 __ bind(&ok);
302 }
303
304 { Comment cmnt(masm_, "[ Body");
305 ASSERT(loop_depth() == 0);
306 VisitStatements(function()->body());
307 ASSERT(loop_depth() == 0);
308 }
309 }
310
311 // Always emit a 'return undefined' in case control fell off the end of
312 // the body.
313 { Comment cmnt(masm_, "[ return <undefined>;");
314 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
315 }
316 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000317}
318
319
320void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000321 ASSERT(Smi::FromInt(0) == 0);
322 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000323}
324
325
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000326void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
327 __ li(a2, Operand(profiling_counter_));
328 __ lw(a3, FieldMemOperand(a2, JSGlobalPropertyCell::kValueOffset));
329 __ Subu(a3, a3, Operand(Smi::FromInt(delta)));
330 __ sw(a3, FieldMemOperand(a2, JSGlobalPropertyCell::kValueOffset));
331}
332
333
334void FullCodeGenerator::EmitProfilingCounterReset() {
335 int reset_value = FLAG_interrupt_budget;
336 if (info_->ShouldSelfOptimize() && !FLAG_retry_self_opt) {
337 // Self-optimization is a one-off thing: if it fails, don't try again.
338 reset_value = Smi::kMaxValue;
339 }
340 if (isolate()->IsDebuggerActive()) {
341 // Detect debug break requests as soon as possible.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000342 reset_value = FLAG_interrupt_budget >> 4;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000343 }
344 __ li(a2, Operand(profiling_counter_));
345 __ li(a3, Operand(Smi::FromInt(reset_value)));
346 __ sw(a3, FieldMemOperand(a2, JSGlobalPropertyCell::kValueOffset));
347}
348
349
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000350void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
351 Label* back_edge_target) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000352 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
353 // to make sure it is constant. Branch may emit a skip-or-jump sequence
354 // instead of the normal Branch. It seems that the "skip" part of that
355 // sequence is about as long as this Branch would be so it is safe to ignore
356 // that.
357 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000358 Comment cmnt(masm_, "[ Back edge bookkeeping");
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000359 Label ok;
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000360 int weight = 1;
361 if (FLAG_weighted_back_edges) {
362 ASSERT(back_edge_target->is_bound());
363 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
364 weight = Min(kMaxBackEdgeWeight,
365 Max(1, distance / kBackEdgeDistanceUnit));
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000366 }
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000367 EmitProfilingCounterDecrement(weight);
368 __ slt(at, a3, zero_reg);
369 __ beq(at, zero_reg, &ok);
370 // CallStub will emit a li t9 first, so it is safe to use the delay slot.
371 InterruptStub stub;
372 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000373 // Record a mapping of this PC offset to the OSR id. This is used to find
374 // the AST id from the unoptimized code in order to use it as a key into
375 // the deoptimization input data found in the optimized code.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000376 RecordBackEdge(stmt->OsrEntryId());
377 EmitProfilingCounterReset();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000378
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000379 __ bind(&ok);
380 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
381 // Record a mapping of the OSR id to this PC. This is used if the OSR
382 // entry becomes the target of a bailout. We don't expect it to be, but
383 // we want it to work if it is.
384 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000385}
386
387
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000388void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000389 Comment cmnt(masm_, "[ Return sequence");
390 if (return_label_.is_bound()) {
391 __ Branch(&return_label_);
392 } else {
393 __ bind(&return_label_);
394 if (FLAG_trace) {
395 // Push the return value on the stack as the parameter.
396 // Runtime::TraceExit returns its parameter in v0.
397 __ push(v0);
398 __ CallRuntime(Runtime::kTraceExit, 1);
399 }
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000400 if (FLAG_interrupt_at_exit || FLAG_self_optimization) {
401 // Pretend that the exit is a backwards jump to the entry.
402 int weight = 1;
403 if (info_->ShouldSelfOptimize()) {
404 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
405 } else if (FLAG_weighted_back_edges) {
406 int distance = masm_->pc_offset();
407 weight = Min(kMaxBackEdgeWeight,
danno@chromium.org129d3982012-07-25 15:01:47 +0000408 Max(1, distance / kBackEdgeDistanceUnit));
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000409 }
410 EmitProfilingCounterDecrement(weight);
411 Label ok;
412 __ Branch(&ok, ge, a3, Operand(zero_reg));
413 __ push(v0);
414 if (info_->ShouldSelfOptimize() && FLAG_direct_self_opt) {
415 __ lw(a2, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
416 __ push(a2);
417 __ CallRuntime(Runtime::kOptimizeFunctionOnNextCall, 1);
418 } else {
419 InterruptStub stub;
420 __ CallStub(&stub);
421 }
422 __ pop(v0);
423 EmitProfilingCounterReset();
424 __ bind(&ok);
425 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000426
427#ifdef DEBUG
428 // Add a label for checking the size of the code used for returning.
429 Label check_exit_codesize;
430 masm_->bind(&check_exit_codesize);
431#endif
432 // Make sure that the constant pool is not emitted inside of the return
433 // sequence.
434 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
435 // Here we use masm_-> instead of the __ macro to avoid the code coverage
436 // tool from instrumenting as we rely on the code size here.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000437 int32_t sp_delta = (info_->scope()->num_parameters() + 1) * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000438 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
439 __ RecordJSReturn();
440 masm_->mov(sp, fp);
441 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
442 masm_->Addu(sp, sp, Operand(sp_delta));
443 masm_->Jump(ra);
444 }
445
446#ifdef DEBUG
447 // Check that the size of the code used for returning is large enough
448 // for the debugger's requirements.
449 ASSERT(Assembler::kJSReturnSequenceInstructions <=
450 masm_->InstructionsGeneratedSince(&check_exit_codesize));
451#endif
452 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000453}
454
455
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000456void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
457 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
ager@chromium.org5c838252010-02-19 08:53:10 +0000458}
459
460
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000461void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
462 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
463 codegen()->GetVar(result_register(), var);
ager@chromium.org5c838252010-02-19 08:53:10 +0000464}
465
466
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000467void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
468 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
469 codegen()->GetVar(result_register(), var);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000470 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000471}
472
473
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000474void FullCodeGenerator::TestContext::Plug(Variable* var) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000475 // For simplicity we always test the accumulator register.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000476 codegen()->GetVar(result_register(), var);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000477 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000478 codegen()->DoTest(this);
ager@chromium.org5c838252010-02-19 08:53:10 +0000479}
480
481
lrn@chromium.org7516f052011-03-30 08:52:27 +0000482void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000483}
484
485
lrn@chromium.org7516f052011-03-30 08:52:27 +0000486void FullCodeGenerator::AccumulatorValueContext::Plug(
487 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000488 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000489}
490
491
492void FullCodeGenerator::StackValueContext::Plug(
493 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000494 __ LoadRoot(result_register(), index);
495 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000496}
497
498
499void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000500 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000501 true,
502 true_label_,
503 false_label_);
504 if (index == Heap::kUndefinedValueRootIndex ||
505 index == Heap::kNullValueRootIndex ||
506 index == Heap::kFalseValueRootIndex) {
507 if (false_label_ != fall_through_) __ Branch(false_label_);
508 } else if (index == Heap::kTrueValueRootIndex) {
509 if (true_label_ != fall_through_) __ Branch(true_label_);
510 } else {
511 __ LoadRoot(result_register(), index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000512 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000513 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000514}
515
516
517void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000518}
519
520
521void FullCodeGenerator::AccumulatorValueContext::Plug(
522 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000523 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000524}
525
526
527void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000528 // Immediates cannot be pushed directly.
529 __ li(result_register(), Operand(lit));
530 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000531}
532
533
534void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000535 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000536 true,
537 true_label_,
538 false_label_);
539 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
540 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
541 if (false_label_ != fall_through_) __ Branch(false_label_);
542 } else if (lit->IsTrue() || lit->IsJSObject()) {
543 if (true_label_ != fall_through_) __ Branch(true_label_);
544 } else if (lit->IsString()) {
545 if (String::cast(*lit)->length() == 0) {
546 if (false_label_ != fall_through_) __ Branch(false_label_);
547 } else {
548 if (true_label_ != fall_through_) __ Branch(true_label_);
549 }
550 } else if (lit->IsSmi()) {
551 if (Smi::cast(*lit)->value() == 0) {
552 if (false_label_ != fall_through_) __ Branch(false_label_);
553 } else {
554 if (true_label_ != fall_through_) __ Branch(true_label_);
555 }
556 } else {
557 // For simplicity we always test the accumulator register.
558 __ li(result_register(), Operand(lit));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000559 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000560 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000561}
562
563
564void FullCodeGenerator::EffectContext::DropAndPlug(int count,
565 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000566 ASSERT(count > 0);
567 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000568}
569
570
571void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
572 int count,
573 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000574 ASSERT(count > 0);
575 __ Drop(count);
576 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000577}
578
579
580void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
581 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000582 ASSERT(count > 0);
583 if (count > 1) __ Drop(count - 1);
584 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000585}
586
587
588void FullCodeGenerator::TestContext::DropAndPlug(int count,
589 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000590 ASSERT(count > 0);
591 // For simplicity we always test the accumulator register.
592 __ Drop(count);
593 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000594 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000595 codegen()->DoTest(this);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000596}
597
598
599void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
600 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000601 ASSERT(materialize_true == materialize_false);
602 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000603}
604
605
606void FullCodeGenerator::AccumulatorValueContext::Plug(
607 Label* materialize_true,
608 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000609 Label done;
610 __ bind(materialize_true);
611 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
612 __ Branch(&done);
613 __ bind(materialize_false);
614 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
615 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000616}
617
618
619void FullCodeGenerator::StackValueContext::Plug(
620 Label* materialize_true,
621 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000622 Label done;
623 __ bind(materialize_true);
624 __ LoadRoot(at, Heap::kTrueValueRootIndex);
625 __ push(at);
626 __ Branch(&done);
627 __ bind(materialize_false);
628 __ LoadRoot(at, Heap::kFalseValueRootIndex);
629 __ push(at);
630 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000631}
632
633
634void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
635 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000636 ASSERT(materialize_true == true_label_);
637 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000638}
639
640
641void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000642}
643
644
645void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000646 Heap::RootListIndex value_root_index =
647 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
648 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000649}
650
651
652void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000653 Heap::RootListIndex value_root_index =
654 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
655 __ LoadRoot(at, value_root_index);
656 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000657}
658
659
660void FullCodeGenerator::TestContext::Plug(bool flag) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000661 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000662 true,
663 true_label_,
664 false_label_);
665 if (flag) {
666 if (true_label_ != fall_through_) __ Branch(true_label_);
667 } else {
668 if (false_label_ != fall_through_) __ Branch(false_label_);
669 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000670}
671
672
whesse@chromium.org7b260152011-06-20 15:33:18 +0000673void FullCodeGenerator::DoTest(Expression* condition,
674 Label* if_true,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000675 Label* if_false,
676 Label* fall_through) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000677 ToBooleanStub stub(result_register());
678 __ CallStub(&stub, condition->test_id());
679 __ mov(at, zero_reg);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000680 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000681}
682
683
lrn@chromium.org7516f052011-03-30 08:52:27 +0000684void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000685 Register lhs,
686 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000687 Label* if_true,
688 Label* if_false,
689 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000690 if (if_false == fall_through) {
691 __ Branch(if_true, cc, lhs, rhs);
692 } else if (if_true == fall_through) {
693 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
694 } else {
695 __ Branch(if_true, cc, lhs, rhs);
696 __ Branch(if_false);
697 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000698}
699
700
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000701MemOperand FullCodeGenerator::StackOperand(Variable* var) {
702 ASSERT(var->IsStackAllocated());
703 // Offset is negative because higher indexes are at lower addresses.
704 int offset = -var->index() * kPointerSize;
705 // Adjust by a (parameter or local) base offset.
706 if (var->IsParameter()) {
707 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
708 } else {
709 offset += JavaScriptFrameConstants::kLocal0Offset;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000710 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000711 return MemOperand(fp, offset);
ager@chromium.org5c838252010-02-19 08:53:10 +0000712}
713
714
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000715MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
716 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
717 if (var->IsContextSlot()) {
718 int context_chain_length = scope()->ContextChainLength(var->scope());
719 __ LoadContext(scratch, context_chain_length);
720 return ContextOperand(scratch, var->index());
721 } else {
722 return StackOperand(var);
723 }
724}
725
726
727void FullCodeGenerator::GetVar(Register dest, Variable* var) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000728 // Use destination as scratch.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000729 MemOperand location = VarOperand(var, dest);
730 __ lw(dest, location);
731}
732
733
734void FullCodeGenerator::SetVar(Variable* var,
735 Register src,
736 Register scratch0,
737 Register scratch1) {
738 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
739 ASSERT(!scratch0.is(src));
740 ASSERT(!scratch0.is(scratch1));
741 ASSERT(!scratch1.is(src));
742 MemOperand location = VarOperand(var, scratch0);
743 __ sw(src, location);
744 // Emit the write barrier code if the location is in the heap.
745 if (var->IsContextSlot()) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000746 __ RecordWriteContextSlot(scratch0,
747 location.offset(),
748 src,
749 scratch1,
750 kRAHasBeenSaved,
751 kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000752 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000753}
754
755
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000756void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000757 bool should_normalize,
758 Label* if_true,
759 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000760 // Only prepare for bailouts before splits if we're in a test
761 // context. Otherwise, we let the Visit function deal with the
762 // preparation to avoid preparing with the same AST id twice.
763 if (!context()->IsTest() || !info_->IsOptimizable()) return;
764
765 Label skip;
766 if (should_normalize) __ Branch(&skip);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000767 PrepareForBailout(expr, TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000768 if (should_normalize) {
769 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
770 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
771 __ bind(&skip);
772 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000773}
774
775
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000776void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
777 // The variable in the declaration always resides in the current function
778 // context.
779 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000780 if (generate_debug_code_) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000781 // Check that we're not inside a with or catch context.
782 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
783 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
784 __ Check(ne, "Declaration in with context.",
785 a1, Operand(t0));
786 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
787 __ Check(ne, "Declaration in catch context.",
788 a1, Operand(t0));
789 }
790}
791
792
793void FullCodeGenerator::VisitVariableDeclaration(
794 VariableDeclaration* declaration) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000795 // If it was not possible to allocate the variable at compile time, we
796 // need to "declare" it at runtime to make sure it actually exists in the
797 // local context.
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000798 VariableProxy* proxy = declaration->proxy();
799 VariableMode mode = declaration->mode();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000800 Variable* variable = proxy->var();
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000801 bool hole_init = mode == CONST || mode == CONST_HARMONY || mode == LET;
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000802 switch (variable->location()) {
803 case Variable::UNALLOCATED:
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000804 globals_->Add(variable->name(), zone());
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000805 globals_->Add(variable->binding_needs_init()
806 ? isolate()->factory()->the_hole_value()
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000807 : isolate()->factory()->undefined_value(),
808 zone());
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000809 break;
810
811 case Variable::PARAMETER:
812 case Variable::LOCAL:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000813 if (hole_init) {
814 Comment cmnt(masm_, "[ VariableDeclaration");
815 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
816 __ sw(t0, StackOperand(variable));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000817 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000818 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000819
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000820 case Variable::CONTEXT:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000821 if (hole_init) {
822 Comment cmnt(masm_, "[ VariableDeclaration");
823 EmitDebugCheckDeclarationContext(variable);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000824 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000825 __ sw(at, ContextOperand(cp, variable->index()));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000826 // No write barrier since the_hole_value is in old space.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000827 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000828 }
829 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000830
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000831 case Variable::LOOKUP: {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000832 Comment cmnt(masm_, "[ VariableDeclaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000833 __ li(a2, Operand(variable->name()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000834 // Declaration nodes are always introduced in one of four modes.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000835 ASSERT(IsDeclaredVariableMode(mode));
836 PropertyAttributes attr =
837 IsImmutableVariableMode(mode) ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000838 __ li(a1, Operand(Smi::FromInt(attr)));
839 // Push initial value, if any.
840 // Note: For variables we must not push an initial value (such as
841 // 'undefined') because we may have a (legal) redeclaration and we
842 // must not destroy the current value.
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000843 if (hole_init) {
844 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
845 __ Push(cp, a2, a1, a0);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000846 } else {
847 ASSERT(Smi::FromInt(0) == 0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000848 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000849 __ Push(cp, a2, a1, a0);
850 }
851 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
852 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000853 }
854 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000855}
856
857
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000858void FullCodeGenerator::VisitFunctionDeclaration(
859 FunctionDeclaration* declaration) {
860 VariableProxy* proxy = declaration->proxy();
861 Variable* variable = proxy->var();
862 switch (variable->location()) {
863 case Variable::UNALLOCATED: {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000864 globals_->Add(variable->name(), zone());
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000865 Handle<SharedFunctionInfo> function =
866 Compiler::BuildFunctionInfo(declaration->fun(), script());
867 // Check for stack-overflow exception.
868 if (function.is_null()) return SetStackOverflow();
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000869 globals_->Add(function, zone());
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000870 break;
871 }
872
873 case Variable::PARAMETER:
874 case Variable::LOCAL: {
875 Comment cmnt(masm_, "[ FunctionDeclaration");
876 VisitForAccumulatorValue(declaration->fun());
877 __ sw(result_register(), StackOperand(variable));
878 break;
879 }
880
881 case Variable::CONTEXT: {
882 Comment cmnt(masm_, "[ FunctionDeclaration");
883 EmitDebugCheckDeclarationContext(variable);
884 VisitForAccumulatorValue(declaration->fun());
885 __ sw(result_register(), ContextOperand(cp, variable->index()));
886 int offset = Context::SlotOffset(variable->index());
887 // We know that we have written a function, which is not a smi.
888 __ RecordWriteContextSlot(cp,
889 offset,
890 result_register(),
891 a2,
892 kRAHasBeenSaved,
893 kDontSaveFPRegs,
894 EMIT_REMEMBERED_SET,
895 OMIT_SMI_CHECK);
896 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
897 break;
898 }
899
900 case Variable::LOOKUP: {
901 Comment cmnt(masm_, "[ FunctionDeclaration");
902 __ li(a2, Operand(variable->name()));
903 __ li(a1, Operand(Smi::FromInt(NONE)));
904 __ Push(cp, a2, a1);
905 // Push initial value for function declaration.
906 VisitForStackValue(declaration->fun());
907 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
908 break;
909 }
910 }
911}
912
913
914void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* declaration) {
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000915 Variable* variable = declaration->proxy()->var();
916 ASSERT(variable->location() == Variable::CONTEXT);
917 ASSERT(variable->interface()->IsFrozen());
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000918
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000919 Comment cmnt(masm_, "[ ModuleDeclaration");
920 EmitDebugCheckDeclarationContext(variable);
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000921
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000922 // Load instance object.
923 __ LoadContext(a1, scope_->ContextChainLength(scope_->GlobalScope()));
924 __ lw(a1, ContextOperand(a1, variable->interface()->Index()));
925 __ lw(a1, ContextOperand(a1, Context::EXTENSION_INDEX));
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000926
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000927 // Assign it.
928 __ sw(a1, ContextOperand(cp, variable->index()));
929 // We know that we have written a module, which is not a smi.
930 __ RecordWriteContextSlot(cp,
931 Context::SlotOffset(variable->index()),
932 a1,
933 a3,
934 kRAHasBeenSaved,
935 kDontSaveFPRegs,
936 EMIT_REMEMBERED_SET,
937 OMIT_SMI_CHECK);
938 PrepareForBailoutForId(declaration->proxy()->id(), NO_REGISTERS);
939
940 // Traverse into body.
941 Visit(declaration->module());
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000942}
943
944
945void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* declaration) {
946 VariableProxy* proxy = declaration->proxy();
947 Variable* variable = proxy->var();
948 switch (variable->location()) {
949 case Variable::UNALLOCATED:
950 // TODO(rossberg)
951 break;
952
953 case Variable::CONTEXT: {
954 Comment cmnt(masm_, "[ ImportDeclaration");
955 EmitDebugCheckDeclarationContext(variable);
956 // TODO(rossberg)
957 break;
958 }
959
960 case Variable::PARAMETER:
961 case Variable::LOCAL:
962 case Variable::LOOKUP:
963 UNREACHABLE();
964 }
965}
966
967
968void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* declaration) {
969 // TODO(rossberg)
970}
971
972
ager@chromium.org5c838252010-02-19 08:53:10 +0000973void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000974 // Call the runtime to declare the globals.
975 // The context is the first argument.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000976 __ li(a1, Operand(pairs));
977 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
978 __ Push(cp, a1, a0);
979 __ CallRuntime(Runtime::kDeclareGlobals, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000980 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000981}
982
983
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000984void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
985 // Call the runtime to declare the modules.
986 __ Push(descriptions);
987 __ CallRuntime(Runtime::kDeclareModules, 1);
988 // Return value is ignored.
989}
990
991
lrn@chromium.org7516f052011-03-30 08:52:27 +0000992void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000993 Comment cmnt(masm_, "[ SwitchStatement");
994 Breakable nested_statement(this, stmt);
995 SetStatementPosition(stmt);
996
997 // Keep the switch value on the stack until a case matches.
998 VisitForStackValue(stmt->tag());
999 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
1000
1001 ZoneList<CaseClause*>* clauses = stmt->cases();
1002 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
1003
1004 Label next_test; // Recycled for each test.
1005 // Compile all the tests with branches to their bodies.
1006 for (int i = 0; i < clauses->length(); i++) {
1007 CaseClause* clause = clauses->at(i);
1008 clause->body_target()->Unuse();
1009
1010 // The default is not a test, but remember it as final fall through.
1011 if (clause->is_default()) {
1012 default_clause = clause;
1013 continue;
1014 }
1015
1016 Comment cmnt(masm_, "[ Case comparison");
1017 __ bind(&next_test);
1018 next_test.Unuse();
1019
1020 // Compile the label expression.
1021 VisitForAccumulatorValue(clause->label());
1022 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
1023
1024 // Perform the comparison as if via '==='.
1025 __ lw(a1, MemOperand(sp, 0)); // Switch value.
1026 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
1027 JumpPatchSite patch_site(masm_);
1028 if (inline_smi_code) {
1029 Label slow_case;
1030 __ or_(a2, a1, a0);
1031 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
1032
1033 __ Branch(&next_test, ne, a1, Operand(a0));
1034 __ Drop(1); // Switch value is no longer needed.
1035 __ Branch(clause->body_target());
1036
1037 __ bind(&slow_case);
1038 }
1039
1040 // Record position before stub call for type feedback.
1041 SetSourcePosition(clause->position());
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001042 Handle<Code> ic = CompareIC::GetUninitialized(isolate(), Token::EQ_STRICT);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001043 CallIC(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001044 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001045
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001046 __ Branch(&next_test, ne, v0, Operand(zero_reg));
1047 __ Drop(1); // Switch value is no longer needed.
1048 __ Branch(clause->body_target());
1049 }
1050
1051 // Discard the test value and jump to the default if present, otherwise to
1052 // the end of the statement.
1053 __ bind(&next_test);
1054 __ Drop(1); // Switch value is no longer needed.
1055 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001056 __ Branch(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001057 } else {
1058 __ Branch(default_clause->body_target());
1059 }
1060
1061 // Compile all the case bodies.
1062 for (int i = 0; i < clauses->length(); i++) {
1063 Comment cmnt(masm_, "[ Case body");
1064 CaseClause* clause = clauses->at(i);
1065 __ bind(clause->body_target());
1066 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
1067 VisitStatements(clause->statements());
1068 }
1069
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001070 __ bind(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001071 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001072}
1073
1074
1075void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001076 Comment cmnt(masm_, "[ ForInStatement");
1077 SetStatementPosition(stmt);
1078
1079 Label loop, exit;
1080 ForIn loop_statement(this, stmt);
1081 increment_loop_depth();
1082
1083 // Get the object to enumerate over. Both SpiderMonkey and JSC
1084 // ignore null and undefined in contrast to the specification; see
1085 // ECMA-262 section 12.6.4.
1086 VisitForAccumulatorValue(stmt->enumerable());
1087 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
1088 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1089 __ Branch(&exit, eq, a0, Operand(at));
1090 Register null_value = t1;
1091 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1092 __ Branch(&exit, eq, a0, Operand(null_value));
ulan@chromium.org812308e2012-02-29 15:58:45 +00001093 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1094 __ mov(a0, v0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001095 // Convert the object to a JS object.
1096 Label convert, done_convert;
1097 __ JumpIfSmi(a0, &convert);
1098 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001099 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001100 __ bind(&convert);
1101 __ push(a0);
1102 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1103 __ mov(a0, v0);
1104 __ bind(&done_convert);
1105 __ push(a0);
1106
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001107 // Check for proxies.
1108 Label call_runtime;
1109 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1110 __ GetObjectType(a0, a1, a1);
1111 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
1112
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001113 // Check cache validity in generated code. This is a fast case for
1114 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1115 // guarantee cache validity, call the runtime system to check cache
1116 // validity or get the property names in a fixed array.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001117 __ CheckEnumCache(null_value, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001118
1119 // The enum cache is valid. Load the map of the object being
1120 // iterated over and use the cache for the iteration.
1121 Label use_cache;
1122 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
1123 __ Branch(&use_cache);
1124
1125 // Get the set of properties to enumerate.
1126 __ bind(&call_runtime);
1127 __ push(a0); // Duplicate the enumerable object on the stack.
1128 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1129
1130 // If we got a map from the runtime call, we can do a fast
1131 // modification check. Otherwise, we got a fixed array, and we have
1132 // to do a slow check.
1133 Label fixed_array;
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001134 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001135 __ LoadRoot(at, Heap::kMetaMapRootIndex);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001136 __ Branch(&fixed_array, ne, a2, Operand(at));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001137
1138 // We got a map in register v0. Get the enumeration cache from it.
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001139 Label no_descriptors;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001140 __ bind(&use_cache);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001141
1142 __ EnumLength(a1, v0);
1143 __ Branch(&no_descriptors, eq, a1, Operand(Smi::FromInt(0)));
1144
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001145 __ LoadInstanceDescriptors(v0, a2);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001146 __ lw(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheOffset));
1147 __ lw(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheBridgeCacheOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001148
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001149 // Set up the four remaining stack slots.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001150 __ push(v0); // Map.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001151 __ li(a0, Operand(Smi::FromInt(0)));
1152 // Push enumeration cache, enumeration cache length (as smi) and zero.
1153 __ Push(a2, a1, a0);
1154 __ jmp(&loop);
1155
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001156 __ bind(&no_descriptors);
1157 __ Drop(1);
1158 __ jmp(&exit);
1159
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001160 // We got a fixed array in register v0. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001161 Label non_proxy;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001162 __ bind(&fixed_array);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001163
1164 Handle<JSGlobalPropertyCell> cell =
1165 isolate()->factory()->NewJSGlobalPropertyCell(
1166 Handle<Object>(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001167 Smi::FromInt(TypeFeedbackCells::kForInFastCaseMarker),
1168 isolate()));
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001169 RecordTypeFeedbackCell(stmt->ForInFeedbackId(), cell);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001170 __ LoadHeapObject(a1, cell);
1171 __ li(a2, Operand(Smi::FromInt(TypeFeedbackCells::kForInSlowCaseMarker)));
1172 __ sw(a2, FieldMemOperand(a1, JSGlobalPropertyCell::kValueOffset));
1173
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001174 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1175 __ lw(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1176 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1177 __ GetObjectType(a2, a3, a3);
1178 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1179 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1180 __ bind(&non_proxy);
1181 __ Push(a1, v0); // Smi and array
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001182 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1183 __ li(a0, Operand(Smi::FromInt(0)));
1184 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1185
1186 // Generate code for doing the condition check.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001187 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001188 __ bind(&loop);
1189 // Load the current count to a0, load the length to a1.
1190 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1191 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001192 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001193
1194 // Get the current entry of the array into register a3.
1195 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1196 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1197 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1198 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1199 __ lw(a3, MemOperand(t0)); // Current entry.
1200
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001201 // Get the expected map from the stack or a smi in the
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001202 // permanent slow case into register a2.
1203 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1204
1205 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001206 // If not, we may have to filter the key.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001207 Label update_each;
1208 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1209 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1210 __ Branch(&update_each, eq, t0, Operand(a2));
1211
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001212 // For proxies, no filtering is done.
1213 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1214 ASSERT_EQ(Smi::FromInt(0), 0);
1215 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1216
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001217 // Convert the entry to a string or (smi) 0 if it isn't a property
1218 // any more. If the property has been removed while iterating, we
1219 // just skip it.
1220 __ push(a1); // Enumerable.
1221 __ push(a3); // Current entry.
1222 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1223 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001224 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001225
1226 // Update the 'each' property or variable from the possibly filtered
1227 // entry in register a3.
1228 __ bind(&update_each);
1229 __ mov(result_register(), a3);
1230 // Perform the assignment as if via '='.
1231 { EffectContext context(this);
ulan@chromium.org812308e2012-02-29 15:58:45 +00001232 EmitAssignment(stmt->each());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001233 }
1234
1235 // Generate code for the body of the loop.
1236 Visit(stmt->body());
1237
1238 // Generate code for the going to the next element by incrementing
1239 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001240 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001241 __ pop(a0);
1242 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1243 __ push(a0);
1244
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001245 EmitBackEdgeBookkeeping(stmt, &loop);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001246 __ Branch(&loop);
1247
1248 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001249 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001250 __ Drop(5);
1251
1252 // Exit and decrement the loop depth.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001253 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001254 __ bind(&exit);
1255 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001256}
1257
1258
1259void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1260 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001261 // Use the fast case closure allocation code that allocates in new
1262 // space for nested functions that don't need literals cloning. If
1263 // we're running with the --always-opt or the --prepare-always-opt
1264 // flag, we need to use the runtime function so that the new function
1265 // we are creating here gets a chance to have its code optimized and
1266 // doesn't just get a copy of the existing unoptimized code.
1267 if (!FLAG_always_opt &&
1268 !FLAG_prepare_always_opt &&
1269 !pretenure &&
1270 scope()->is_function_scope() &&
1271 info->num_literals() == 0) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001272 FastNewClosureStub stub(info->language_mode(), info->is_generator());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001273 __ li(a0, Operand(info));
1274 __ push(a0);
1275 __ CallStub(&stub);
1276 } else {
1277 __ li(a0, Operand(info));
1278 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1279 : Heap::kFalseValueRootIndex);
1280 __ Push(cp, a0, a1);
1281 __ CallRuntime(Runtime::kNewClosure, 3);
1282 }
1283 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001284}
1285
1286
1287void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001288 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001289 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001290}
1291
1292
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001293void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1294 TypeofState typeof_state,
1295 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001296 Register current = cp;
1297 Register next = a1;
1298 Register temp = a2;
1299
1300 Scope* s = scope();
1301 while (s != NULL) {
1302 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001303 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001304 // Check that extension is NULL.
1305 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1306 __ Branch(slow, ne, temp, Operand(zero_reg));
1307 }
1308 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001309 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001310 // Walk the rest of the chain without clobbering cp.
1311 current = next;
1312 }
1313 // If no outer scope calls eval, we do not need to check more
1314 // context extensions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001315 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001316 s = s->outer_scope();
1317 }
1318
1319 if (s->is_eval_scope()) {
1320 Label loop, fast;
1321 if (!current.is(next)) {
1322 __ Move(next, current);
1323 }
1324 __ bind(&loop);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001325 // Terminate at native context.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001326 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001327 __ LoadRoot(t0, Heap::kNativeContextMapRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001328 __ Branch(&fast, eq, temp, Operand(t0));
1329 // Check that extension is NULL.
1330 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1331 __ Branch(slow, ne, temp, Operand(zero_reg));
1332 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001333 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001334 __ Branch(&loop);
1335 __ bind(&fast);
1336 }
1337
1338 __ lw(a0, GlobalObjectOperand());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001339 __ li(a2, Operand(var->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001340 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1341 ? RelocInfo::CODE_TARGET
1342 : RelocInfo::CODE_TARGET_CONTEXT;
1343 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001344 CallIC(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001345}
1346
1347
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001348MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1349 Label* slow) {
1350 ASSERT(var->IsContextSlot());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001351 Register context = cp;
1352 Register next = a3;
1353 Register temp = t0;
1354
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001355 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001356 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001357 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001358 // Check that extension is NULL.
1359 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1360 __ Branch(slow, ne, temp, Operand(zero_reg));
1361 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001362 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001363 // Walk the rest of the chain without clobbering cp.
1364 context = next;
1365 }
1366 }
1367 // Check that last extension is NULL.
1368 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1369 __ Branch(slow, ne, temp, Operand(zero_reg));
1370
1371 // This function is used only for loads, not stores, so it's safe to
1372 // return an cp-based operand (the write barrier cannot be allowed to
1373 // destroy the cp register).
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001374 return ContextOperand(context, var->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001375}
1376
1377
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001378void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1379 TypeofState typeof_state,
1380 Label* slow,
1381 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001382 // Generate fast-case code for variables that might be shadowed by
1383 // eval-introduced variables. Eval is used a lot without
1384 // introducing variables. In those cases, we do not want to
1385 // perform a runtime call for all variables in the scope
1386 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001387 if (var->mode() == DYNAMIC_GLOBAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001388 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001389 __ Branch(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001390 } else if (var->mode() == DYNAMIC_LOCAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001391 Variable* local = var->local_if_not_shadowed();
1392 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
danno@chromium.org1f34ad32012-11-26 14:53:56 +00001393 if (local->mode() == LET ||
1394 local->mode() == CONST ||
1395 local->mode() == CONST_HARMONY) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001396 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1397 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001398 if (local->mode() == CONST) {
1399 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +00001400 __ Movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001401 } else { // LET || CONST_HARMONY
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001402 __ Branch(done, ne, at, Operand(zero_reg));
1403 __ li(a0, Operand(var->name()));
1404 __ push(a0);
1405 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1406 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001407 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001408 __ Branch(done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001409 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001410}
1411
1412
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001413void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1414 // Record position before possible IC call.
1415 SetSourcePosition(proxy->position());
1416 Variable* var = proxy->var();
1417
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001418 // Three cases: global variables, lookup variables, and all other types of
1419 // variables.
1420 switch (var->location()) {
1421 case Variable::UNALLOCATED: {
1422 Comment cmnt(masm_, "Global variable");
1423 // Use inline caching. Variable name is passed in a2 and the global
1424 // object (receiver) in a0.
1425 __ lw(a0, GlobalObjectOperand());
1426 __ li(a2, Operand(var->name()));
1427 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001428 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001429 context()->Plug(v0);
1430 break;
1431 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001432
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001433 case Variable::PARAMETER:
1434 case Variable::LOCAL:
1435 case Variable::CONTEXT: {
1436 Comment cmnt(masm_, var->IsContextSlot()
1437 ? "Context variable"
1438 : "Stack variable");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001439 if (var->binding_needs_init()) {
1440 // var->scope() may be NULL when the proxy is located in eval code and
1441 // refers to a potential outside binding. Currently those bindings are
1442 // always looked up dynamically, i.e. in that case
1443 // var->location() == LOOKUP.
1444 // always holds.
1445 ASSERT(var->scope() != NULL);
1446
1447 // Check if the binding really needs an initialization check. The check
1448 // can be skipped in the following situation: we have a LET or CONST
1449 // binding in harmony mode, both the Variable and the VariableProxy have
1450 // the same declaration scope (i.e. they are both in global code, in the
1451 // same function or in the same eval code) and the VariableProxy is in
1452 // the source physically located after the initializer of the variable.
1453 //
1454 // We cannot skip any initialization checks for CONST in non-harmony
1455 // mode because const variables may be declared but never initialized:
1456 // if (false) { const x; }; var y = x;
1457 //
1458 // The condition on the declaration scopes is a conservative check for
1459 // nested functions that access a binding and are called before the
1460 // binding is initialized:
1461 // function() { f(); let x = 1; function f() { x = 2; } }
1462 //
1463 bool skip_init_check;
1464 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1465 skip_init_check = false;
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001466 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001467 // Check that we always have valid source position.
1468 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1469 ASSERT(proxy->position() != RelocInfo::kNoPosition);
1470 skip_init_check = var->mode() != CONST &&
1471 var->initializer_position() < proxy->position();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001472 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001473
1474 if (!skip_init_check) {
1475 // Let and const need a read barrier.
1476 GetVar(v0, var);
1477 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1478 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1479 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1480 // Throw a reference error when using an uninitialized let/const
1481 // binding in harmony mode.
1482 Label done;
1483 __ Branch(&done, ne, at, Operand(zero_reg));
1484 __ li(a0, Operand(var->name()));
1485 __ push(a0);
1486 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1487 __ bind(&done);
1488 } else {
1489 // Uninitalized const bindings outside of harmony mode are unholed.
1490 ASSERT(var->mode() == CONST);
1491 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +00001492 __ Movz(v0, a0, at); // Conditional move: Undefined if TheHole.
danno@chromium.orgc612e022011-11-10 11:38:15 +00001493 }
1494 context()->Plug(v0);
1495 break;
1496 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001497 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001498 context()->Plug(var);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001499 break;
1500 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001501
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001502 case Variable::LOOKUP: {
1503 Label done, slow;
1504 // Generate code for loading from variables potentially shadowed
1505 // by eval-introduced variables.
1506 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1507 __ bind(&slow);
1508 Comment cmnt(masm_, "Lookup variable");
1509 __ li(a1, Operand(var->name()));
1510 __ Push(cp, a1); // Context and name.
1511 __ CallRuntime(Runtime::kLoadContextSlot, 2);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001512 __ bind(&done);
1513 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001514 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001515 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001516}
1517
1518
1519void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001520 Comment cmnt(masm_, "[ RegExpLiteral");
1521 Label materialized;
1522 // Registers will be used as follows:
1523 // t1 = materialized value (RegExp literal)
1524 // t0 = JS function, literals array
1525 // a3 = literal index
1526 // a2 = RegExp pattern
1527 // a1 = RegExp flags
1528 // a0 = RegExp literal clone
1529 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1530 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1531 int literal_offset =
1532 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1533 __ lw(t1, FieldMemOperand(t0, literal_offset));
1534 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1535 __ Branch(&materialized, ne, t1, Operand(at));
1536
1537 // Create regexp literal using runtime function.
1538 // Result will be in v0.
1539 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1540 __ li(a2, Operand(expr->pattern()));
1541 __ li(a1, Operand(expr->flags()));
1542 __ Push(t0, a3, a2, a1);
1543 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1544 __ mov(t1, v0);
1545
1546 __ bind(&materialized);
1547 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1548 Label allocated, runtime_allocate;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +00001549 __ Allocate(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001550 __ jmp(&allocated);
1551
1552 __ bind(&runtime_allocate);
1553 __ push(t1);
1554 __ li(a0, Operand(Smi::FromInt(size)));
1555 __ push(a0);
1556 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1557 __ pop(t1);
1558
1559 __ bind(&allocated);
1560
1561 // After this, registers are used as follows:
1562 // v0: Newly allocated regexp.
1563 // t1: Materialized regexp.
1564 // a2: temp.
1565 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1566 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001567}
1568
1569
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001570void FullCodeGenerator::EmitAccessor(Expression* expression) {
1571 if (expression == NULL) {
1572 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1573 __ push(a1);
1574 } else {
1575 VisitForStackValue(expression);
1576 }
1577}
1578
1579
ager@chromium.org5c838252010-02-19 08:53:10 +00001580void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001581 Comment cmnt(masm_, "[ ObjectLiteral");
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001582 Handle<FixedArray> constant_properties = expr->constant_properties();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001583 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001584 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1585 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001586 __ li(a1, Operand(constant_properties));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001587 int flags = expr->fast_elements()
1588 ? ObjectLiteral::kFastElements
1589 : ObjectLiteral::kNoFlags;
1590 flags |= expr->has_function()
1591 ? ObjectLiteral::kHasFunction
1592 : ObjectLiteral::kNoFlags;
1593 __ li(a0, Operand(Smi::FromInt(flags)));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001594 int properties_count = constant_properties->length() / 2;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001595 if ((FLAG_track_double_fields && expr->may_store_doubles()) ||
1596 expr->depth() > 1) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001597 __ Push(a3, a2, a1, a0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001598 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001599 } else if (Serializer::enabled() || flags != ObjectLiteral::kFastElements ||
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001600 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001601 __ Push(a3, a2, a1, a0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001602 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001603 } else {
1604 FastCloneShallowObjectStub stub(properties_count);
1605 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001606 }
1607
1608 // If result_saved is true the result is on top of the stack. If
1609 // result_saved is false the result is in v0.
1610 bool result_saved = false;
1611
1612 // Mark all computed expressions that are bound to a key that
1613 // is shadowed by a later occurrence of the same key. For the
1614 // marked expressions, no store code is emitted.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001615 expr->CalculateEmitStore(zone());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001616
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001617 AccessorTable accessor_table(zone());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001618 for (int i = 0; i < expr->properties()->length(); i++) {
1619 ObjectLiteral::Property* property = expr->properties()->at(i);
1620 if (property->IsCompileTimeValue()) continue;
1621
1622 Literal* key = property->key();
1623 Expression* value = property->value();
1624 if (!result_saved) {
1625 __ push(v0); // Save result on stack.
1626 result_saved = true;
1627 }
1628 switch (property->kind()) {
1629 case ObjectLiteral::Property::CONSTANT:
1630 UNREACHABLE();
1631 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1632 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1633 // Fall through.
1634 case ObjectLiteral::Property::COMPUTED:
ulan@chromium.org750145a2013-03-07 15:14:13 +00001635 if (key->handle()->IsInternalizedString()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001636 if (property->emit_store()) {
1637 VisitForAccumulatorValue(value);
1638 __ mov(a0, result_register());
1639 __ li(a2, Operand(key->handle()));
1640 __ lw(a1, MemOperand(sp));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001641 Handle<Code> ic = is_classic_mode()
1642 ? isolate()->builtins()->StoreIC_Initialize()
1643 : isolate()->builtins()->StoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001644 CallIC(ic, RelocInfo::CODE_TARGET, key->LiteralFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001645 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1646 } else {
1647 VisitForEffect(value);
1648 }
1649 break;
1650 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001651 // Duplicate receiver on stack.
1652 __ lw(a0, MemOperand(sp));
1653 __ push(a0);
1654 VisitForStackValue(key);
1655 VisitForStackValue(value);
1656 if (property->emit_store()) {
1657 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1658 __ push(a0);
1659 __ CallRuntime(Runtime::kSetProperty, 4);
1660 } else {
1661 __ Drop(3);
1662 }
1663 break;
ulan@chromium.org750145a2013-03-07 15:14:13 +00001664 case ObjectLiteral::Property::PROTOTYPE:
1665 // Duplicate receiver on stack.
1666 __ lw(a0, MemOperand(sp));
1667 __ push(a0);
1668 VisitForStackValue(value);
1669 if (property->emit_store()) {
1670 __ CallRuntime(Runtime::kSetPrototype, 2);
1671 } else {
1672 __ Drop(2);
1673 }
1674 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001675 case ObjectLiteral::Property::GETTER:
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001676 accessor_table.lookup(key)->second->getter = value;
1677 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001678 case ObjectLiteral::Property::SETTER:
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001679 accessor_table.lookup(key)->second->setter = value;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001680 break;
1681 }
1682 }
1683
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001684 // Emit code to define accessors, using only a single call to the runtime for
1685 // each pair of corresponding getters and setters.
1686 for (AccessorTable::Iterator it = accessor_table.begin();
1687 it != accessor_table.end();
1688 ++it) {
1689 __ lw(a0, MemOperand(sp)); // Duplicate receiver.
1690 __ push(a0);
1691 VisitForStackValue(it->first);
1692 EmitAccessor(it->second->getter);
1693 EmitAccessor(it->second->setter);
1694 __ li(a0, Operand(Smi::FromInt(NONE)));
1695 __ push(a0);
1696 __ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
1697 }
1698
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001699 if (expr->has_function()) {
1700 ASSERT(result_saved);
1701 __ lw(a0, MemOperand(sp));
1702 __ push(a0);
1703 __ CallRuntime(Runtime::kToFastProperties, 1);
1704 }
1705
1706 if (result_saved) {
1707 context()->PlugTOS();
1708 } else {
1709 context()->Plug(v0);
1710 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001711}
1712
1713
1714void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001715 Comment cmnt(masm_, "[ ArrayLiteral");
1716
1717 ZoneList<Expression*>* subexprs = expr->values();
1718 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001719
1720 Handle<FixedArray> constant_elements = expr->constant_elements();
1721 ASSERT_EQ(2, constant_elements->length());
1722 ElementsKind constant_elements_kind =
1723 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001724 bool has_fast_elements =
1725 IsFastObjectElementsKind(constant_elements_kind);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001726 Handle<FixedArrayBase> constant_elements_values(
1727 FixedArrayBase::cast(constant_elements->get(1)));
1728
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001729 __ mov(a0, result_register());
1730 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1731 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1732 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001733 __ li(a1, Operand(constant_elements));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001734 if (has_fast_elements && constant_elements_values->map() ==
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001735 isolate()->heap()->fixed_cow_array_map()) {
1736 FastCloneShallowArrayStub stub(
yangguo@chromium.org28381b42013-01-21 14:39:38 +00001737 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS,
1738 DONT_TRACK_ALLOCATION_SITE,
1739 length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001740 __ CallStub(&stub);
1741 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1742 1, a1, a2);
1743 } else if (expr->depth() > 1) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001744 __ Push(a3, a2, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001745 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001746 } else if (Serializer::enabled() ||
1747 length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1748 __ Push(a3, a2, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001749 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1750 } else {
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001751 ASSERT(IsFastSmiOrObjectElementsKind(constant_elements_kind) ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001752 FLAG_smi_only_arrays);
yangguo@chromium.org28381b42013-01-21 14:39:38 +00001753 FastCloneShallowArrayStub::Mode mode =
1754 FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS;
1755 AllocationSiteMode allocation_site_mode = FLAG_track_allocation_sites
1756 ? TRACK_ALLOCATION_SITE : DONT_TRACK_ALLOCATION_SITE;
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001757
yangguo@chromium.org28381b42013-01-21 14:39:38 +00001758 if (has_fast_elements) {
1759 mode = FastCloneShallowArrayStub::CLONE_ELEMENTS;
1760 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001761 }
1762
yangguo@chromium.org28381b42013-01-21 14:39:38 +00001763 FastCloneShallowArrayStub stub(mode, allocation_site_mode, length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001764 __ CallStub(&stub);
1765 }
1766
1767 bool result_saved = false; // Is the result saved to the stack?
1768
1769 // Emit code to evaluate all the non-constant subexpressions and to store
1770 // them into the newly cloned array.
1771 for (int i = 0; i < length; i++) {
1772 Expression* subexpr = subexprs->at(i);
1773 // If the subexpression is a literal or a simple materialized literal it
1774 // is already set in the cloned array.
1775 if (subexpr->AsLiteral() != NULL ||
1776 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1777 continue;
1778 }
1779
1780 if (!result_saved) {
1781 __ push(v0);
1782 result_saved = true;
1783 }
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001784
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001785 VisitForAccumulatorValue(subexpr);
1786
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001787 if (IsFastObjectElementsKind(constant_elements_kind)) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001788 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1789 __ lw(t2, MemOperand(sp)); // Copy of array literal.
1790 __ lw(a1, FieldMemOperand(t2, JSObject::kElementsOffset));
1791 __ sw(result_register(), FieldMemOperand(a1, offset));
1792 // Update the write barrier for the array store.
1793 __ RecordWriteField(a1, offset, result_register(), a2,
1794 kRAHasBeenSaved, kDontSaveFPRegs,
1795 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1796 } else {
1797 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1798 __ lw(a2, FieldMemOperand(a1, JSObject::kMapOffset));
1799 __ li(a3, Operand(Smi::FromInt(i)));
1800 __ li(t0, Operand(Smi::FromInt(expr->literal_index())));
1801 __ mov(a0, result_register());
1802 StoreArrayLiteralElementStub stub;
1803 __ CallStub(&stub);
1804 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001805
1806 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1807 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001808 if (result_saved) {
1809 context()->PlugTOS();
1810 } else {
1811 context()->Plug(v0);
1812 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001813}
1814
1815
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001816void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001817 Comment cmnt(masm_, "[ Assignment");
1818 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1819 // on the left-hand side.
1820 if (!expr->target()->IsValidLeftHandSide()) {
1821 VisitForEffect(expr->target());
1822 return;
1823 }
1824
1825 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001826 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001827 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1828 LhsKind assign_type = VARIABLE;
1829 Property* property = expr->target()->AsProperty();
1830 if (property != NULL) {
1831 assign_type = (property->key()->IsPropertyName())
1832 ? NAMED_PROPERTY
1833 : KEYED_PROPERTY;
1834 }
1835
1836 // Evaluate LHS expression.
1837 switch (assign_type) {
1838 case VARIABLE:
1839 // Nothing to do here.
1840 break;
1841 case NAMED_PROPERTY:
1842 if (expr->is_compound()) {
1843 // We need the receiver both on the stack and in the accumulator.
1844 VisitForAccumulatorValue(property->obj());
1845 __ push(result_register());
1846 } else {
1847 VisitForStackValue(property->obj());
1848 }
1849 break;
1850 case KEYED_PROPERTY:
1851 // We need the key and receiver on both the stack and in v0 and a1.
1852 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001853 VisitForStackValue(property->obj());
1854 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001855 __ lw(a1, MemOperand(sp, 0));
1856 __ push(v0);
1857 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001858 VisitForStackValue(property->obj());
1859 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001860 }
1861 break;
1862 }
1863
1864 // For compound assignments we need another deoptimization point after the
1865 // variable/property load.
1866 if (expr->is_compound()) {
1867 { AccumulatorValueContext context(this);
1868 switch (assign_type) {
1869 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001870 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001871 PrepareForBailout(expr->target(), TOS_REG);
1872 break;
1873 case NAMED_PROPERTY:
1874 EmitNamedPropertyLoad(property);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001875 PrepareForBailoutForId(property->LoadId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001876 break;
1877 case KEYED_PROPERTY:
1878 EmitKeyedPropertyLoad(property);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001879 PrepareForBailoutForId(property->LoadId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001880 break;
1881 }
1882 }
1883
1884 Token::Value op = expr->binary_op();
1885 __ push(v0); // Left operand goes on the stack.
1886 VisitForAccumulatorValue(expr->value());
1887
1888 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1889 ? OVERWRITE_RIGHT
1890 : NO_OVERWRITE;
1891 SetSourcePosition(expr->position() + 1);
1892 AccumulatorValueContext context(this);
1893 if (ShouldInlineSmiCase(op)) {
1894 EmitInlineSmiBinaryOp(expr->binary_operation(),
1895 op,
1896 mode,
1897 expr->target(),
1898 expr->value());
1899 } else {
1900 EmitBinaryOp(expr->binary_operation(), op, mode);
1901 }
1902
1903 // Deoptimization point in case the binary operation may have side effects.
1904 PrepareForBailout(expr->binary_operation(), TOS_REG);
1905 } else {
1906 VisitForAccumulatorValue(expr->value());
1907 }
1908
1909 // Record source position before possible IC call.
1910 SetSourcePosition(expr->position());
1911
1912 // Store the value.
1913 switch (assign_type) {
1914 case VARIABLE:
1915 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1916 expr->op());
1917 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1918 context()->Plug(v0);
1919 break;
1920 case NAMED_PROPERTY:
1921 EmitNamedPropertyAssignment(expr);
1922 break;
1923 case KEYED_PROPERTY:
1924 EmitKeyedPropertyAssignment(expr);
1925 break;
1926 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001927}
1928
1929
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001930void FullCodeGenerator::VisitYield(Yield* expr) {
1931 Comment cmnt(masm_, "[ Yield");
1932 // Evaluate yielded value first; the initial iterator definition depends on
1933 // this. It stays on the stack while we update the iterator.
1934 VisitForStackValue(expr->expression());
1935
1936 switch (expr->yield_kind()) {
1937 case Yield::INITIAL:
1938 case Yield::SUSPEND: {
1939 VisitForStackValue(expr->generator_object());
1940 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
1941 __ lw(context_register(),
1942 MemOperand(fp, StandardFrameConstants::kContextOffset));
1943
1944 Label resume;
1945 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1946 __ Branch(&resume, ne, result_register(), Operand(at));
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001947 if (expr->yield_kind() == Yield::SUSPEND) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001948 EmitReturnIteratorResult(false);
1949 } else {
1950 __ pop(result_register());
1951 EmitReturnSequence();
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001952 }
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001953
1954 __ bind(&resume);
1955 context()->Plug(result_register());
1956 break;
1957 }
1958
1959 case Yield::FINAL: {
1960 VisitForAccumulatorValue(expr->generator_object());
1961 __ li(a1, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorClosed)));
1962 __ sw(a1, FieldMemOperand(result_register(),
1963 JSGeneratorObject::kContinuationOffset));
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001964 EmitReturnIteratorResult(true);
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001965 break;
1966 }
1967
1968 case Yield::DELEGATING:
1969 UNIMPLEMENTED();
1970 }
1971}
1972
1973
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001974void FullCodeGenerator::EmitGeneratorResume(Expression *generator,
1975 Expression *value,
1976 JSGeneratorObject::ResumeMode resume_mode) {
1977 // The value stays in a0, and is ultimately read by the resumed generator, as
1978 // if the CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. a1
1979 // will hold the generator object until the activation has been resumed.
1980 VisitForStackValue(generator);
1981 VisitForAccumulatorValue(value);
1982 __ pop(a1);
1983
1984 // Check generator state.
1985 Label wrong_state, done;
1986 __ lw(a3, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
1987 STATIC_ASSERT(JSGeneratorObject::kGeneratorExecuting <= 0);
1988 STATIC_ASSERT(JSGeneratorObject::kGeneratorClosed <= 0);
1989 __ Branch(&wrong_state, le, a3, Operand(zero_reg));
1990
1991 // Load suspended function and context.
1992 __ lw(cp, FieldMemOperand(a1, JSGeneratorObject::kContextOffset));
1993 __ lw(t0, FieldMemOperand(a1, JSGeneratorObject::kFunctionOffset));
1994
1995 // Load receiver and store as the first argument.
1996 __ lw(a2, FieldMemOperand(a1, JSGeneratorObject::kReceiverOffset));
1997 __ push(a2);
1998
1999 // Push holes for the rest of the arguments to the generator function.
2000 __ lw(a3, FieldMemOperand(t0, JSFunction::kSharedFunctionInfoOffset));
2001 __ lw(a3,
2002 FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset));
2003 __ LoadRoot(a2, Heap::kTheHoleValueRootIndex);
2004 Label push_argument_holes, push_frame;
2005 __ bind(&push_argument_holes);
2006 __ Subu(a3, a3, Operand(1));
2007 __ Branch(&push_frame, lt, a3, Operand(zero_reg));
2008 __ push(a2);
2009 __ jmp(&push_argument_holes);
2010
2011 // Enter a new JavaScript frame, and initialize its slots as they were when
2012 // the generator was suspended.
2013 Label resume_frame;
2014 __ bind(&push_frame);
2015 __ Call(&resume_frame);
2016 __ jmp(&done);
2017 __ bind(&resume_frame);
2018 __ push(ra); // Return address.
2019 __ push(fp); // Caller's frame pointer.
2020 __ mov(fp, sp);
2021 __ push(cp); // Callee's context.
2022 __ push(t0); // Callee's JS Function.
2023
2024 // Load the operand stack size.
2025 __ lw(a3, FieldMemOperand(a1, JSGeneratorObject::kOperandStackOffset));
2026 __ lw(a3, FieldMemOperand(a3, FixedArray::kLengthOffset));
2027 __ SmiUntag(a3);
2028
2029 // If we are sending a value and there is no operand stack, we can jump back
2030 // in directly.
2031 if (resume_mode == JSGeneratorObject::SEND) {
2032 Label slow_resume;
2033 __ Branch(&slow_resume, ne, a3, Operand(zero_reg));
2034 __ lw(a3, FieldMemOperand(t0, JSFunction::kCodeEntryOffset));
2035 __ lw(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2036 __ SmiUntag(a2);
2037 __ Addu(a3, a3, Operand(a2));
2038 __ li(a2, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorExecuting)));
2039 __ sw(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2040 __ Jump(a3);
2041 __ bind(&slow_resume);
2042 }
2043
2044 // Otherwise, we push holes for the operand stack and call the runtime to fix
2045 // up the stack and the handlers.
2046 Label push_operand_holes, call_resume;
2047 __ bind(&push_operand_holes);
2048 __ Subu(a3, a3, Operand(1));
2049 __ Branch(&call_resume, lt, a3, Operand(zero_reg));
2050 __ push(a2);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002051 __ Branch(&push_operand_holes);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002052 __ bind(&call_resume);
2053 __ push(a1);
2054 __ push(result_register());
2055 __ Push(Smi::FromInt(resume_mode));
2056 __ CallRuntime(Runtime::kResumeJSGeneratorObject, 3);
2057 // Not reached: the runtime call returns elsewhere.
2058 __ stop("not-reached");
2059
2060 // Throw error if we attempt to operate on a running generator.
2061 __ bind(&wrong_state);
2062 __ push(a1);
2063 __ CallRuntime(Runtime::kThrowGeneratorStateError, 1);
2064
2065 __ bind(&done);
2066 context()->Plug(result_register());
2067}
2068
2069
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002070void FullCodeGenerator::EmitReturnIteratorResult(bool done) {
2071 Label gc_required;
2072 Label allocated;
2073
2074 Handle<Map> map(isolate()->native_context()->generator_result_map());
2075
2076 __ Allocate(map->instance_size(), a0, a2, a3, &gc_required, TAG_OBJECT);
2077
2078 __ bind(&allocated);
2079 __ li(a1, Operand(map));
2080 __ pop(a2);
2081 __ li(a3, Operand(isolate()->factory()->ToBoolean(done)));
2082 __ li(t0, Operand(isolate()->factory()->empty_fixed_array()));
2083 ASSERT_EQ(map->instance_size(), 5 * kPointerSize);
2084 __ sw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
2085 __ sw(t0, FieldMemOperand(a0, JSObject::kPropertiesOffset));
2086 __ sw(t0, FieldMemOperand(a0, JSObject::kElementsOffset));
2087 __ sw(a2,
2088 FieldMemOperand(a0, JSGeneratorObject::kResultValuePropertyOffset));
2089 __ sw(a3,
2090 FieldMemOperand(a0, JSGeneratorObject::kResultDonePropertyOffset));
2091
2092 // Only the value field needs a write barrier, as the other values are in the
2093 // root set.
2094 __ RecordWriteField(a0, JSGeneratorObject::kResultValuePropertyOffset,
2095 a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
2096
2097 if (done) {
2098 // Exit all nested statements.
2099 NestedStatement* current = nesting_stack_;
2100 int stack_depth = 0;
2101 int context_length = 0;
2102 while (current != NULL) {
2103 current = current->Exit(&stack_depth, &context_length);
2104 }
2105 __ Drop(stack_depth);
2106 }
2107
2108 __ mov(result_register(), a0);
2109 EmitReturnSequence();
2110
2111 __ bind(&gc_required);
2112 __ Push(Smi::FromInt(map->instance_size()));
2113 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
2114 __ lw(context_register(),
2115 MemOperand(fp, StandardFrameConstants::kContextOffset));
2116 __ jmp(&allocated);
2117}
2118
2119
ager@chromium.org5c838252010-02-19 08:53:10 +00002120void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002121 SetSourcePosition(prop->position());
2122 Literal* key = prop->key()->AsLiteral();
2123 __ mov(a0, result_register());
2124 __ li(a2, Operand(key->handle()));
2125 // Call load IC. It has arguments receiver and property name a0 and a2.
2126 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002127 CallIC(ic, RelocInfo::CODE_TARGET, prop->PropertyFeedbackId());
ager@chromium.org5c838252010-02-19 08:53:10 +00002128}
2129
2130
2131void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002132 SetSourcePosition(prop->position());
2133 __ mov(a0, result_register());
2134 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
2135 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002136 CallIC(ic, RelocInfo::CODE_TARGET, prop->PropertyFeedbackId());
ager@chromium.org5c838252010-02-19 08:53:10 +00002137}
2138
2139
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002140void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002141 Token::Value op,
2142 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002143 Expression* left_expr,
2144 Expression* right_expr) {
2145 Label done, smi_case, stub_call;
2146
2147 Register scratch1 = a2;
2148 Register scratch2 = a3;
2149
2150 // Get the arguments.
2151 Register left = a1;
2152 Register right = a0;
2153 __ pop(left);
2154 __ mov(a0, result_register());
2155
2156 // Perform combined smi check on both operands.
2157 __ Or(scratch1, left, Operand(right));
2158 STATIC_ASSERT(kSmiTag == 0);
2159 JumpPatchSite patch_site(masm_);
2160 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
2161
2162 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00002163 BinaryOpStub stub(op, mode);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002164 CallIC(stub.GetCode(isolate()), RelocInfo::CODE_TARGET,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002165 expr->BinaryOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002166 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002167 __ jmp(&done);
2168
2169 __ bind(&smi_case);
2170 // Smi case. This code works the same way as the smi-smi case in the type
2171 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00002172 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002173 switch (op) {
2174 case Token::SAR:
2175 __ Branch(&stub_call);
2176 __ GetLeastBitsFromSmi(scratch1, right, 5);
2177 __ srav(right, left, scratch1);
2178 __ And(v0, right, Operand(~kSmiTagMask));
2179 break;
2180 case Token::SHL: {
2181 __ Branch(&stub_call);
2182 __ SmiUntag(scratch1, left);
2183 __ GetLeastBitsFromSmi(scratch2, right, 5);
2184 __ sllv(scratch1, scratch1, scratch2);
2185 __ Addu(scratch2, scratch1, Operand(0x40000000));
2186 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
2187 __ SmiTag(v0, scratch1);
2188 break;
2189 }
2190 case Token::SHR: {
2191 __ Branch(&stub_call);
2192 __ SmiUntag(scratch1, left);
2193 __ GetLeastBitsFromSmi(scratch2, right, 5);
2194 __ srlv(scratch1, scratch1, scratch2);
2195 __ And(scratch2, scratch1, 0xc0000000);
2196 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
2197 __ SmiTag(v0, scratch1);
2198 break;
2199 }
2200 case Token::ADD:
2201 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
2202 __ BranchOnOverflow(&stub_call, scratch1);
2203 break;
2204 case Token::SUB:
2205 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
2206 __ BranchOnOverflow(&stub_call, scratch1);
2207 break;
2208 case Token::MUL: {
2209 __ SmiUntag(scratch1, right);
2210 __ Mult(left, scratch1);
2211 __ mflo(scratch1);
2212 __ mfhi(scratch2);
2213 __ sra(scratch1, scratch1, 31);
2214 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
2215 __ mflo(v0);
2216 __ Branch(&done, ne, v0, Operand(zero_reg));
2217 __ Addu(scratch2, right, left);
2218 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
2219 ASSERT(Smi::FromInt(0) == 0);
2220 __ mov(v0, zero_reg);
2221 break;
2222 }
2223 case Token::BIT_OR:
2224 __ Or(v0, left, Operand(right));
2225 break;
2226 case Token::BIT_AND:
2227 __ And(v0, left, Operand(right));
2228 break;
2229 case Token::BIT_XOR:
2230 __ Xor(v0, left, Operand(right));
2231 break;
2232 default:
2233 UNREACHABLE();
2234 }
2235
2236 __ bind(&done);
2237 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002238}
2239
2240
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002241void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
2242 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002243 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002244 __ mov(a0, result_register());
2245 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00002246 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002247 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002248 CallIC(stub.GetCode(isolate()), RelocInfo::CODE_TARGET,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002249 expr->BinaryOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002250 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002251 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002252}
2253
2254
ulan@chromium.org812308e2012-02-29 15:58:45 +00002255void FullCodeGenerator::EmitAssignment(Expression* expr) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002256 // Invalid left-hand sides are rewritten by the parser to have a 'throw
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002257 // ReferenceError' on the left-hand side.
2258 if (!expr->IsValidLeftHandSide()) {
2259 VisitForEffect(expr);
2260 return;
2261 }
2262
2263 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00002264 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002265 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2266 LhsKind assign_type = VARIABLE;
2267 Property* prop = expr->AsProperty();
2268 if (prop != NULL) {
2269 assign_type = (prop->key()->IsPropertyName())
2270 ? NAMED_PROPERTY
2271 : KEYED_PROPERTY;
2272 }
2273
2274 switch (assign_type) {
2275 case VARIABLE: {
2276 Variable* var = expr->AsVariableProxy()->var();
2277 EffectContext context(this);
2278 EmitVariableAssignment(var, Token::ASSIGN);
2279 break;
2280 }
2281 case NAMED_PROPERTY: {
2282 __ push(result_register()); // Preserve value.
2283 VisitForAccumulatorValue(prop->obj());
2284 __ mov(a1, result_register());
2285 __ pop(a0); // Restore value.
2286 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002287 Handle<Code> ic = is_classic_mode()
2288 ? isolate()->builtins()->StoreIC_Initialize()
2289 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002290 CallIC(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002291 break;
2292 }
2293 case KEYED_PROPERTY: {
2294 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00002295 VisitForStackValue(prop->obj());
2296 VisitForAccumulatorValue(prop->key());
2297 __ mov(a1, result_register());
2298 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002299 __ pop(a0); // Restore value.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002300 Handle<Code> ic = is_classic_mode()
2301 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2302 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002303 CallIC(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002304 break;
2305 }
2306 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002307 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002308}
2309
2310
2311void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002312 Token::Value op) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002313 if (var->IsUnallocated()) {
2314 // Global var, const, or let.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002315 __ mov(a0, result_register());
2316 __ li(a2, Operand(var->name()));
2317 __ lw(a1, GlobalObjectOperand());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002318 Handle<Code> ic = is_classic_mode()
2319 ? isolate()->builtins()->StoreIC_Initialize()
2320 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002321 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002322
2323 } else if (op == Token::INIT_CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002324 // Const initializers need a write barrier.
2325 ASSERT(!var->IsParameter()); // No const parameters.
2326 if (var->IsStackLocal()) {
2327 Label skip;
2328 __ lw(a1, StackOperand(var));
2329 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2330 __ Branch(&skip, ne, a1, Operand(t0));
2331 __ sw(result_register(), StackOperand(var));
2332 __ bind(&skip);
2333 } else {
2334 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
2335 // Like var declarations, const declarations are hoisted to function
2336 // scope. However, unlike var initializers, const initializers are
2337 // able to drill a hole to that function context, even from inside a
2338 // 'with' context. We thus bypass the normal static scope lookup for
2339 // var->IsContextSlot().
2340 __ push(v0);
2341 __ li(a0, Operand(var->name()));
2342 __ Push(cp, a0); // Context and name.
2343 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002344 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002345
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002346 } else if (var->mode() == LET && op != Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002347 // Non-initializing assignment to let variable needs a write barrier.
2348 if (var->IsLookupSlot()) {
2349 __ push(v0); // Value.
2350 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002351 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002352 __ Push(cp, a1, a0); // Context, name, strict mode.
2353 __ CallRuntime(Runtime::kStoreContextSlot, 4);
2354 } else {
2355 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
2356 Label assign;
2357 MemOperand location = VarOperand(var, a1);
2358 __ lw(a3, location);
2359 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2360 __ Branch(&assign, ne, a3, Operand(t0));
2361 __ li(a3, Operand(var->name()));
2362 __ push(a3);
2363 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2364 // Perform the assignment.
2365 __ bind(&assign);
2366 __ sw(result_register(), location);
2367 if (var->IsContextSlot()) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002368 // RecordWrite may destroy all its register arguments.
2369 __ mov(a3, result_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002370 int offset = Context::SlotOffset(var->index());
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002371 __ RecordWriteContextSlot(
2372 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002373 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002374 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002375
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002376 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2377 // Assignment to var or initializing assignment to let/const
2378 // in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002379 if (var->IsStackAllocated() || var->IsContextSlot()) {
2380 MemOperand location = VarOperand(var, a1);
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002381 if (generate_debug_code_ && op == Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002382 // Check for an uninitialized let binding.
2383 __ lw(a2, location);
2384 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2385 __ Check(eq, "Let binding re-initialization.", a2, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002386 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002387 // Perform the assignment.
2388 __ sw(v0, location);
2389 if (var->IsContextSlot()) {
2390 __ mov(a3, v0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002391 int offset = Context::SlotOffset(var->index());
2392 __ RecordWriteContextSlot(
2393 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002394 }
2395 } else {
2396 ASSERT(var->IsLookupSlot());
2397 __ push(v0); // Value.
2398 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002399 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002400 __ Push(cp, a1, a0); // Context, name, strict mode.
2401 __ CallRuntime(Runtime::kStoreContextSlot, 4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002402 }
2403 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002404 // Non-initializing assignments to consts are ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +00002405}
2406
2407
2408void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002409 // Assignment to a property, using a named store IC.
2410 Property* prop = expr->target()->AsProperty();
2411 ASSERT(prop != NULL);
2412 ASSERT(prop->key()->AsLiteral() != NULL);
2413
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002414 // Record source code position before IC call.
2415 SetSourcePosition(expr->position());
2416 __ mov(a0, result_register()); // Load the value.
2417 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002418 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002419
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002420 Handle<Code> ic = is_classic_mode()
2421 ? isolate()->builtins()->StoreIC_Initialize()
2422 : isolate()->builtins()->StoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002423 CallIC(ic, RelocInfo::CODE_TARGET, expr->AssignmentFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002424
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002425 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2426 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002427}
2428
2429
2430void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002431 // Assignment to a property, using a keyed store IC.
2432
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002433 // Record source code position before IC call.
2434 SetSourcePosition(expr->position());
2435 // Call keyed store IC.
2436 // The arguments are:
2437 // - a0 is the value,
2438 // - a1 is the key,
2439 // - a2 is the receiver.
2440 __ mov(a0, result_register());
2441 __ pop(a1); // Key.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002442 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002443
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002444 Handle<Code> ic = is_classic_mode()
2445 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2446 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002447 CallIC(ic, RelocInfo::CODE_TARGET, expr->AssignmentFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002448
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002449 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2450 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002451}
2452
2453
2454void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002455 Comment cmnt(masm_, "[ Property");
2456 Expression* key = expr->key();
2457
2458 if (key->IsPropertyName()) {
2459 VisitForAccumulatorValue(expr->obj());
2460 EmitNamedPropertyLoad(expr);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002461 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002462 context()->Plug(v0);
2463 } else {
2464 VisitForStackValue(expr->obj());
2465 VisitForAccumulatorValue(expr->key());
2466 __ pop(a1);
2467 EmitKeyedPropertyLoad(expr);
2468 context()->Plug(v0);
2469 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002470}
2471
lrn@chromium.org7516f052011-03-30 08:52:27 +00002472
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002473void FullCodeGenerator::CallIC(Handle<Code> code,
2474 RelocInfo::Mode rmode,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002475 TypeFeedbackId id) {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002476 ic_total_count_++;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002477 __ Call(code, rmode, id);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002478}
2479
2480
ager@chromium.org5c838252010-02-19 08:53:10 +00002481void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002482 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002483 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002484 // Code common for calls using the IC.
2485 ZoneList<Expression*>* args = expr->arguments();
2486 int arg_count = args->length();
2487 { PreservePositionScope scope(masm()->positions_recorder());
2488 for (int i = 0; i < arg_count; i++) {
2489 VisitForStackValue(args->at(i));
2490 }
2491 __ li(a2, Operand(name));
2492 }
2493 // Record source position for debugger.
2494 SetSourcePosition(expr->position());
2495 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002496 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002497 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002498 CallIC(ic, mode, expr->CallFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002499 RecordJSReturnSite(expr);
2500 // Restore context register.
2501 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2502 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002503}
2504
2505
lrn@chromium.org7516f052011-03-30 08:52:27 +00002506void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002507 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002508 // Load the key.
2509 VisitForAccumulatorValue(key);
2510
2511 // Swap the name of the function and the receiver on the stack to follow
2512 // the calling convention for call ICs.
2513 __ pop(a1);
2514 __ push(v0);
2515 __ push(a1);
2516
2517 // Code common for calls using the IC.
2518 ZoneList<Expression*>* args = expr->arguments();
2519 int arg_count = args->length();
2520 { PreservePositionScope scope(masm()->positions_recorder());
2521 for (int i = 0; i < arg_count; i++) {
2522 VisitForStackValue(args->at(i));
2523 }
2524 }
2525 // Record source position for debugger.
2526 SetSourcePosition(expr->position());
2527 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002528 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002529 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002530 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002531 CallIC(ic, RelocInfo::CODE_TARGET, expr->CallFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002532 RecordJSReturnSite(expr);
2533 // Restore context register.
2534 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2535 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002536}
2537
2538
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002539void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002540 // Code common for calls using the call stub.
2541 ZoneList<Expression*>* args = expr->arguments();
2542 int arg_count = args->length();
2543 { PreservePositionScope scope(masm()->positions_recorder());
2544 for (int i = 0; i < arg_count; i++) {
2545 VisitForStackValue(args->at(i));
2546 }
2547 }
2548 // Record source position for debugger.
2549 SetSourcePosition(expr->position());
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00002550
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002551 // Record call targets.
2552 flags = static_cast<CallFunctionFlags>(flags | RECORD_CALL_TARGET);
2553 Handle<Object> uninitialized =
2554 TypeFeedbackCells::UninitializedSentinel(isolate());
2555 Handle<JSGlobalPropertyCell> cell =
2556 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002557 RecordTypeFeedbackCell(expr->CallFeedbackId(), cell);
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002558 __ li(a2, Operand(cell));
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00002559
lrn@chromium.org34e60782011-09-15 07:25:40 +00002560 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002561 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00002562 __ CallStub(&stub, expr->CallFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002563 RecordJSReturnSite(expr);
2564 // Restore context register.
2565 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2566 context()->DropAndPlug(1, v0);
2567}
2568
2569
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002570void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002571 // Push copy of the first argument or undefined if it doesn't exist.
2572 if (arg_count > 0) {
2573 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2574 } else {
2575 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2576 }
2577 __ push(a1);
2578
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002579 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002580 int receiver_offset = 2 + info_->scope()->num_parameters();
2581 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002582 __ push(a1);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002583 // Push the language mode.
2584 __ li(a1, Operand(Smi::FromInt(language_mode())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002585 __ push(a1);
2586
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002587 // Push the start position of the scope the calls resides in.
2588 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
2589 __ push(a1);
2590
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002591 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002592 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org5c838252010-02-19 08:53:10 +00002593}
2594
2595
2596void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002597#ifdef DEBUG
2598 // We want to verify that RecordJSReturnSite gets called on all paths
2599 // through this function. Avoid early returns.
2600 expr->return_is_recorded_ = false;
2601#endif
2602
2603 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002604 Expression* callee = expr->expression();
2605 VariableProxy* proxy = callee->AsVariableProxy();
2606 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002607
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002608 if (proxy != NULL && proxy->var()->is_possibly_eval(isolate())) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002609 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2610 // resolve the function we need to call and the receiver of the
2611 // call. Then we call the resolved function using the given
2612 // arguments.
2613 ZoneList<Expression*>* args = expr->arguments();
2614 int arg_count = args->length();
2615
2616 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002617 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002618 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2619 __ push(a2); // Reserved receiver slot.
2620
2621 // Push the arguments.
2622 for (int i = 0; i < arg_count; i++) {
2623 VisitForStackValue(args->at(i));
2624 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002625
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002626 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002627 // resolve eval.
2628 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2629 __ push(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002630 EmitResolvePossiblyDirectEval(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002631
2632 // The runtime call returns a pair of values in v0 (function) and
2633 // v1 (receiver). Touch up the stack with the right values.
2634 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2635 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2636 }
2637 // Record source position for debugger.
2638 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002639 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002640 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002641 __ CallStub(&stub);
2642 RecordJSReturnSite(expr);
2643 // Restore context register.
2644 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2645 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002646 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002647 // Push global object as receiver for the call IC.
2648 __ lw(a0, GlobalObjectOperand());
2649 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002650 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2651 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002652 // Call to a lookup slot (dynamically introduced variable).
2653 Label slow, done;
2654
2655 { PreservePositionScope scope(masm()->positions_recorder());
2656 // Generate code for loading from variables potentially shadowed
2657 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002658 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002659 }
2660
2661 __ bind(&slow);
2662 // Call the runtime to find the function to call (returned in v0)
2663 // and the object holding it (returned in v1).
2664 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002665 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002666 __ push(a2);
2667 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2668 __ Push(v0, v1); // Function, receiver.
2669
2670 // If fast case code has been generated, emit code to push the
2671 // function and receiver and have the slow path jump around this
2672 // code.
2673 if (done.is_linked()) {
2674 Label call;
2675 __ Branch(&call);
2676 __ bind(&done);
2677 // Push function.
2678 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002679 // The receiver is implicitly the global receiver. Indicate this
2680 // by passing the hole to the call function stub.
2681 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002682 __ push(a1);
2683 __ bind(&call);
2684 }
2685
danno@chromium.org40cb8782011-05-25 07:58:50 +00002686 // The receiver is either the global receiver or an object found
2687 // by LoadContextSlot. That object could be the hole if the
2688 // receiver is implicitly the global object.
2689 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002690 } else if (property != NULL) {
2691 { PreservePositionScope scope(masm()->positions_recorder());
2692 VisitForStackValue(property->obj());
2693 }
2694 if (property->key()->IsPropertyName()) {
2695 EmitCallWithIC(expr,
2696 property->key()->AsLiteral()->handle(),
2697 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002698 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002699 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002700 }
2701 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002702 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002703 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002704 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002705 }
2706 // Load global receiver object.
2707 __ lw(a1, GlobalObjectOperand());
2708 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2709 __ push(a1);
2710 // Emit function call.
2711 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2712 }
2713
2714#ifdef DEBUG
2715 // RecordJSReturnSite should have been called.
2716 ASSERT(expr->return_is_recorded_);
2717#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002718}
2719
2720
2721void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002722 Comment cmnt(masm_, "[ CallNew");
2723 // According to ECMA-262, section 11.2.2, page 44, the function
2724 // expression in new calls must be evaluated before the
2725 // arguments.
2726
2727 // Push constructor on the stack. If it's not a function it's used as
2728 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2729 // ignored.
2730 VisitForStackValue(expr->expression());
2731
2732 // Push the arguments ("left-to-right") on the stack.
2733 ZoneList<Expression*>* args = expr->arguments();
2734 int arg_count = args->length();
2735 for (int i = 0; i < arg_count; i++) {
2736 VisitForStackValue(args->at(i));
2737 }
2738
2739 // Call the construct call builtin that handles allocation and
2740 // constructor invocation.
2741 SetSourcePosition(expr->position());
2742
2743 // Load function and argument count into a1 and a0.
2744 __ li(a0, Operand(arg_count));
2745 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2746
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002747 // Record call targets in unoptimized code.
2748 Handle<Object> uninitialized =
2749 TypeFeedbackCells::UninitializedSentinel(isolate());
2750 Handle<JSGlobalPropertyCell> cell =
2751 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002752 RecordTypeFeedbackCell(expr->CallNewFeedbackId(), cell);
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002753 __ li(a2, Operand(cell));
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002754
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002755 CallConstructStub stub(RECORD_CALL_TARGET);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002756 __ Call(stub.GetCode(isolate()), RelocInfo::CONSTRUCT_CALL);
ulan@chromium.org967e2702012-02-28 09:49:15 +00002757 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002758 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002759}
2760
2761
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002762void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2763 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002764 ASSERT(args->length() == 1);
2765
2766 VisitForAccumulatorValue(args->at(0));
2767
2768 Label materialize_true, materialize_false;
2769 Label* if_true = NULL;
2770 Label* if_false = NULL;
2771 Label* fall_through = NULL;
2772 context()->PrepareTest(&materialize_true, &materialize_false,
2773 &if_true, &if_false, &fall_through);
2774
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002775 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002776 __ And(t0, v0, Operand(kSmiTagMask));
2777 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2778
2779 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002780}
2781
2782
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002783void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2784 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002785 ASSERT(args->length() == 1);
2786
2787 VisitForAccumulatorValue(args->at(0));
2788
2789 Label materialize_true, materialize_false;
2790 Label* if_true = NULL;
2791 Label* if_false = NULL;
2792 Label* fall_through = NULL;
2793 context()->PrepareTest(&materialize_true, &materialize_false,
2794 &if_true, &if_false, &fall_through);
2795
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002796 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002797 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2798 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2799
2800 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002801}
2802
2803
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002804void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2805 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002806 ASSERT(args->length() == 1);
2807
2808 VisitForAccumulatorValue(args->at(0));
2809
2810 Label materialize_true, materialize_false;
2811 Label* if_true = NULL;
2812 Label* if_false = NULL;
2813 Label* fall_through = NULL;
2814 context()->PrepareTest(&materialize_true, &materialize_false,
2815 &if_true, &if_false, &fall_through);
2816
2817 __ JumpIfSmi(v0, if_false);
2818 __ LoadRoot(at, Heap::kNullValueRootIndex);
2819 __ Branch(if_true, eq, v0, Operand(at));
2820 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2821 // Undetectable objects behave like undefined when tested with typeof.
2822 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2823 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2824 __ Branch(if_false, ne, at, Operand(zero_reg));
2825 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002826 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002827 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002828 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2829 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002830
2831 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002832}
2833
2834
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002835void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2836 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002837 ASSERT(args->length() == 1);
2838
2839 VisitForAccumulatorValue(args->at(0));
2840
2841 Label materialize_true, materialize_false;
2842 Label* if_true = NULL;
2843 Label* if_false = NULL;
2844 Label* fall_through = NULL;
2845 context()->PrepareTest(&materialize_true, &materialize_false,
2846 &if_true, &if_false, &fall_through);
2847
2848 __ JumpIfSmi(v0, if_false);
2849 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002850 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002851 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002852 if_true, if_false, fall_through);
2853
2854 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002855}
2856
2857
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002858void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2859 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002860 ASSERT(args->length() == 1);
2861
2862 VisitForAccumulatorValue(args->at(0));
2863
2864 Label materialize_true, materialize_false;
2865 Label* if_true = NULL;
2866 Label* if_false = NULL;
2867 Label* fall_through = NULL;
2868 context()->PrepareTest(&materialize_true, &materialize_false,
2869 &if_true, &if_false, &fall_through);
2870
2871 __ JumpIfSmi(v0, if_false);
2872 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2873 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2874 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002875 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002876 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2877
2878 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002879}
2880
2881
2882void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002883 CallRuntime* expr) {
2884 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002885 ASSERT(args->length() == 1);
2886
2887 VisitForAccumulatorValue(args->at(0));
2888
2889 Label materialize_true, materialize_false;
2890 Label* if_true = NULL;
2891 Label* if_false = NULL;
2892 Label* fall_through = NULL;
2893 context()->PrepareTest(&materialize_true, &materialize_false,
2894 &if_true, &if_false, &fall_through);
2895
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002896 __ AssertNotSmi(v0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002897
2898 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2899 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2900 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2901 __ Branch(if_true, ne, t0, Operand(zero_reg));
2902
2903 // Check for fast case object. Generate false result for slow case object.
2904 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2905 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2906 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2907 __ Branch(if_false, eq, a2, Operand(t0));
2908
ulan@chromium.org750145a2013-03-07 15:14:13 +00002909 // Look for valueOf name in the descriptor array, and indicate false if
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002910 // found. Since we omit an enumeration index check, if it is added via a
2911 // transition that shares its descriptor array, this is a false positive.
2912 Label entry, loop, done;
2913
2914 // Skip loop if no descriptors are valid.
2915 __ NumberOfOwnDescriptors(a3, a1);
2916 __ Branch(&done, eq, a3, Operand(zero_reg));
2917
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002918 __ LoadInstanceDescriptors(a1, t0);
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002919 // t0: descriptor array.
2920 // a3: valid entries in the descriptor array.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002921 STATIC_ASSERT(kSmiTag == 0);
2922 STATIC_ASSERT(kSmiTagSize == 1);
2923 STATIC_ASSERT(kPointerSize == 4);
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002924 __ li(at, Operand(DescriptorArray::kDescriptorSize));
2925 __ Mul(a3, a3, at);
2926 // Calculate location of the first key name.
2927 __ Addu(t0, t0, Operand(DescriptorArray::kFirstOffset - kHeapObjectTag));
2928 // Calculate the end of the descriptor array.
2929 __ mov(a2, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002930 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2931 __ Addu(a2, a2, t1);
2932
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002933 // Loop through all the keys in the descriptor array. If one of these is the
ulan@chromium.org750145a2013-03-07 15:14:13 +00002934 // string "valueOf" the result is false.
2935 // The use of t2 to store the valueOf string assumes that it is not otherwise
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002936 // used in the loop below.
ulan@chromium.org750145a2013-03-07 15:14:13 +00002937 __ li(t2, Operand(FACTORY->value_of_string()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002938 __ jmp(&entry);
2939 __ bind(&loop);
2940 __ lw(a3, MemOperand(t0, 0));
2941 __ Branch(if_false, eq, a3, Operand(t2));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002942 __ Addu(t0, t0, Operand(DescriptorArray::kDescriptorSize * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002943 __ bind(&entry);
2944 __ Branch(&loop, ne, t0, Operand(a2));
2945
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002946 __ bind(&done);
2947 // If a valueOf property is not found on the object check that its
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002948 // prototype is the un-modified String prototype. If not result is false.
2949 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2950 __ JumpIfSmi(a2, if_false);
2951 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002952 __ lw(a3, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
2953 __ lw(a3, FieldMemOperand(a3, GlobalObject::kNativeContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002954 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2955 __ Branch(if_false, ne, a2, Operand(a3));
2956
2957 // Set the bit in the map to indicate that it has been checked safe for
2958 // default valueOf and set true result.
2959 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2960 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2961 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2962 __ jmp(if_true);
2963
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002964 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002965 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002966}
2967
2968
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002969void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2970 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002971 ASSERT(args->length() == 1);
2972
2973 VisitForAccumulatorValue(args->at(0));
2974
2975 Label materialize_true, materialize_false;
2976 Label* if_true = NULL;
2977 Label* if_false = NULL;
2978 Label* fall_through = NULL;
2979 context()->PrepareTest(&materialize_true, &materialize_false,
2980 &if_true, &if_false, &fall_through);
2981
2982 __ JumpIfSmi(v0, if_false);
2983 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002984 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002985 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2986 __ Branch(if_false);
2987
2988 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002989}
2990
2991
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002992void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2993 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002994 ASSERT(args->length() == 1);
2995
2996 VisitForAccumulatorValue(args->at(0));
2997
2998 Label materialize_true, materialize_false;
2999 Label* if_true = NULL;
3000 Label* if_false = NULL;
3001 Label* fall_through = NULL;
3002 context()->PrepareTest(&materialize_true, &materialize_false,
3003 &if_true, &if_false, &fall_through);
3004
3005 __ JumpIfSmi(v0, if_false);
3006 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003007 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003008 Split(eq, a1, Operand(JS_ARRAY_TYPE),
3009 if_true, if_false, fall_through);
3010
3011 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003012}
3013
3014
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003015void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
3016 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003017 ASSERT(args->length() == 1);
3018
3019 VisitForAccumulatorValue(args->at(0));
3020
3021 Label materialize_true, materialize_false;
3022 Label* if_true = NULL;
3023 Label* if_false = NULL;
3024 Label* fall_through = NULL;
3025 context()->PrepareTest(&materialize_true, &materialize_false,
3026 &if_true, &if_false, &fall_through);
3027
3028 __ JumpIfSmi(v0, if_false);
3029 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003030 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003031 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
3032
3033 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003034}
3035
3036
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003037void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
3038 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003039
3040 Label materialize_true, materialize_false;
3041 Label* if_true = NULL;
3042 Label* if_false = NULL;
3043 Label* fall_through = NULL;
3044 context()->PrepareTest(&materialize_true, &materialize_false,
3045 &if_true, &if_false, &fall_through);
3046
3047 // Get the frame pointer for the calling frame.
3048 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3049
3050 // Skip the arguments adaptor frame if it exists.
3051 Label check_frame_marker;
3052 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
3053 __ Branch(&check_frame_marker, ne,
3054 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3055 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
3056
3057 // Check the marker in the calling frame.
3058 __ bind(&check_frame_marker);
3059 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003060 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003061 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
3062 if_true, if_false, fall_through);
3063
3064 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003065}
3066
3067
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003068void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
3069 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003070 ASSERT(args->length() == 2);
3071
3072 // Load the two objects into registers and perform the comparison.
3073 VisitForStackValue(args->at(0));
3074 VisitForAccumulatorValue(args->at(1));
3075
3076 Label materialize_true, materialize_false;
3077 Label* if_true = NULL;
3078 Label* if_false = NULL;
3079 Label* fall_through = NULL;
3080 context()->PrepareTest(&materialize_true, &materialize_false,
3081 &if_true, &if_false, &fall_through);
3082
3083 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003084 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003085 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
3086
3087 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003088}
3089
3090
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003091void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
3092 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003093 ASSERT(args->length() == 1);
3094
3095 // ArgumentsAccessStub expects the key in a1 and the formal
3096 // parameter count in a0.
3097 VisitForAccumulatorValue(args->at(0));
3098 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003099 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003100 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
3101 __ CallStub(&stub);
3102 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003103}
3104
3105
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003106void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
3107 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003108 Label exit;
3109 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003110 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003111
3112 // Check if the calling frame is an arguments adaptor frame.
3113 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3114 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
3115 __ Branch(&exit, ne, a3,
3116 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3117
3118 // Arguments adaptor case: Read the arguments length from the
3119 // adaptor frame.
3120 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3121
3122 __ bind(&exit);
3123 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003124}
3125
3126
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003127void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
3128 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003129 ASSERT(args->length() == 1);
3130 Label done, null, function, non_function_constructor;
3131
3132 VisitForAccumulatorValue(args->at(0));
3133
3134 // If the object is a smi, we return null.
3135 __ JumpIfSmi(v0, &null);
3136
3137 // Check that the object is a JS object but take special care of JS
3138 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003139 // Assume that there are only two callable types, and one of them is at
3140 // either end of the type range for JS object types. Saves extra comparisons.
3141 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003142 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003143 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003144
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003145 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3146 FIRST_SPEC_OBJECT_TYPE + 1);
3147 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003148
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003149 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3150 LAST_SPEC_OBJECT_TYPE - 1);
3151 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
3152 // Assume that there is no larger type.
3153 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
3154
3155 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003156 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
3157 __ GetObjectType(v0, a1, a1);
3158 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
3159
3160 // v0 now contains the constructor function. Grab the
3161 // instance class name from there.
3162 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
3163 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
3164 __ Branch(&done);
3165
3166 // Functions have class 'Function'.
3167 __ bind(&function);
ulan@chromium.org750145a2013-03-07 15:14:13 +00003168 __ LoadRoot(v0, Heap::kfunction_class_stringRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003169 __ jmp(&done);
3170
3171 // Objects with a non-function constructor have class 'Object'.
3172 __ bind(&non_function_constructor);
ulan@chromium.org750145a2013-03-07 15:14:13 +00003173 __ LoadRoot(v0, Heap::kObject_stringRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003174 __ jmp(&done);
3175
3176 // Non-JS objects have class null.
3177 __ bind(&null);
3178 __ LoadRoot(v0, Heap::kNullValueRootIndex);
3179
3180 // All done.
3181 __ bind(&done);
3182
3183 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003184}
3185
3186
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003187void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003188 // Conditionally generate a log call.
3189 // Args:
3190 // 0 (literal string): The type of logging (corresponds to the flags).
3191 // This is used to determine whether or not to generate the log call.
3192 // 1 (string): Format string. Access the string at argument index 2
3193 // with '%2s' (see Logger::LogRuntime for all the formats).
3194 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003195 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003196 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003197 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
3198 VisitForStackValue(args->at(1));
3199 VisitForStackValue(args->at(2));
3200 __ CallRuntime(Runtime::kLog, 2);
3201 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003202
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003203 // Finally, we're expected to leave a value on the top of the stack.
3204 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3205 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003206}
3207
3208
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003209void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
3210 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003211 Label slow_allocate_heapnumber;
3212 Label heapnumber_allocated;
3213
3214 // Save the new heap number in callee-saved register s0, since
3215 // we call out to external C code below.
3216 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
3217 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
3218 __ jmp(&heapnumber_allocated);
3219
3220 __ bind(&slow_allocate_heapnumber);
3221
3222 // Allocate a heap number.
3223 __ CallRuntime(Runtime::kNumberAlloc, 0);
3224 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
3225
3226 __ bind(&heapnumber_allocated);
3227
3228 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
3229 // by computing:
3230 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00003231 __ PrepareCallCFunction(1, a0);
3232 __ lw(a0, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
3233 __ lw(a0, FieldMemOperand(a0, GlobalObject::kNativeContextOffset));
3234 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003235
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00003236 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
3237 __ li(a1, Operand(0x41300000));
3238 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
3239 __ Move(f12, v0, a1);
3240 // Move 0x4130000000000000 to FPU.
3241 __ Move(f14, zero_reg, a1);
3242 // Subtract and store the result in the heap number.
3243 __ sub_d(f0, f12, f14);
3244 __ sdc1(f0, FieldMemOperand(s0, HeapNumber::kValueOffset));
3245 __ mov(v0, s0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003246
3247 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003248}
3249
3250
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003251void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003252 // Load the arguments on the stack and call the stub.
3253 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003254 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003255 ASSERT(args->length() == 3);
3256 VisitForStackValue(args->at(0));
3257 VisitForStackValue(args->at(1));
3258 VisitForStackValue(args->at(2));
3259 __ CallStub(&stub);
3260 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003261}
3262
3263
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003264void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003265 // Load the arguments on the stack and call the stub.
3266 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003267 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003268 ASSERT(args->length() == 4);
3269 VisitForStackValue(args->at(0));
3270 VisitForStackValue(args->at(1));
3271 VisitForStackValue(args->at(2));
3272 VisitForStackValue(args->at(3));
3273 __ CallStub(&stub);
3274 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003275}
3276
3277
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003278void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3279 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003280 ASSERT(args->length() == 1);
3281
3282 VisitForAccumulatorValue(args->at(0)); // Load the object.
3283
3284 Label done;
3285 // If the object is a smi return the object.
3286 __ JumpIfSmi(v0, &done);
3287 // If the object is not a value type, return the object.
3288 __ GetObjectType(v0, a1, a1);
3289 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
3290
3291 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
3292
3293 __ bind(&done);
3294 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003295}
3296
3297
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003298void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3299 ZoneList<Expression*>* args = expr->arguments();
3300 ASSERT(args->length() == 2);
3301 ASSERT_NE(NULL, args->at(1)->AsLiteral());
3302 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->handle()));
3303
3304 VisitForAccumulatorValue(args->at(0)); // Load the object.
3305
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003306 Label runtime, done, not_date_object;
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003307 Register object = v0;
3308 Register result = v0;
3309 Register scratch0 = t5;
3310 Register scratch1 = a1;
3311
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003312 __ JumpIfSmi(object, &not_date_object);
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003313 __ GetObjectType(object, scratch1, scratch1);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003314 __ Branch(&not_date_object, ne, scratch1, Operand(JS_DATE_TYPE));
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003315
3316 if (index->value() == 0) {
3317 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset));
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003318 __ jmp(&done);
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003319 } else {
3320 if (index->value() < JSDate::kFirstUncachedField) {
3321 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3322 __ li(scratch1, Operand(stamp));
3323 __ lw(scratch1, MemOperand(scratch1));
3324 __ lw(scratch0, FieldMemOperand(object, JSDate::kCacheStampOffset));
3325 __ Branch(&runtime, ne, scratch1, Operand(scratch0));
3326 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset +
3327 kPointerSize * index->value()));
3328 __ jmp(&done);
3329 }
3330 __ bind(&runtime);
3331 __ PrepareCallCFunction(2, scratch1);
3332 __ li(a1, Operand(index));
3333 __ Move(a0, object);
3334 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003335 __ jmp(&done);
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003336 }
3337
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003338 __ bind(&not_date_object);
3339 __ CallRuntime(Runtime::kThrowNotDateError, 0);
3340 __ bind(&done);
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003341 context()->Plug(v0);
3342}
3343
3344
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003345void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3346 ZoneList<Expression*>* args = expr->arguments();
3347 ASSERT_EQ(3, args->length());
3348
3349 VisitForStackValue(args->at(1)); // index
3350 VisitForStackValue(args->at(2)); // value
3351 __ pop(a2);
3352 __ pop(a1);
3353 VisitForAccumulatorValue(args->at(0)); // string
3354
3355 static const String::Encoding encoding = String::ONE_BYTE_ENCODING;
3356 SeqStringSetCharGenerator::Generate(masm_, encoding, v0, a1, a2);
3357 context()->Plug(v0);
3358}
3359
3360
3361void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3362 ZoneList<Expression*>* args = expr->arguments();
3363 ASSERT_EQ(3, args->length());
3364
3365 VisitForStackValue(args->at(1)); // index
3366 VisitForStackValue(args->at(2)); // value
3367 __ pop(a2);
3368 __ pop(a1);
3369 VisitForAccumulatorValue(args->at(0)); // string
3370
3371 static const String::Encoding encoding = String::TWO_BYTE_ENCODING;
3372 SeqStringSetCharGenerator::Generate(masm_, encoding, v0, a1, a2);
3373 context()->Plug(v0);
3374}
3375
3376
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003377void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003378 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003379 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003380 ASSERT(args->length() == 2);
3381 VisitForStackValue(args->at(0));
3382 VisitForStackValue(args->at(1));
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00003383 MathPowStub stub(MathPowStub::ON_STACK);
3384 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003385 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003386}
3387
3388
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003389void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3390 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003391 ASSERT(args->length() == 2);
3392
3393 VisitForStackValue(args->at(0)); // Load the object.
3394 VisitForAccumulatorValue(args->at(1)); // Load the value.
3395 __ pop(a1); // v0 = value. a1 = object.
3396
3397 Label done;
3398 // If the object is a smi, return the value.
3399 __ JumpIfSmi(a1, &done);
3400
3401 // If the object is not a value type, return the value.
3402 __ GetObjectType(a1, a2, a2);
3403 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
3404
3405 // Store the value.
3406 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
3407 // Update the write barrier. Save the value as it will be
3408 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003409 __ mov(a2, v0);
3410 __ RecordWriteField(
3411 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003412
3413 __ bind(&done);
3414 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003415}
3416
3417
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003418void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3419 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003420 ASSERT_EQ(args->length(), 1);
3421
3422 // Load the argument on the stack and call the stub.
3423 VisitForStackValue(args->at(0));
3424
3425 NumberToStringStub stub;
3426 __ CallStub(&stub);
3427 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003428}
3429
3430
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003431void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3432 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003433 ASSERT(args->length() == 1);
3434
3435 VisitForAccumulatorValue(args->at(0));
3436
3437 Label done;
3438 StringCharFromCodeGenerator generator(v0, a1);
3439 generator.GenerateFast(masm_);
3440 __ jmp(&done);
3441
3442 NopRuntimeCallHelper call_helper;
3443 generator.GenerateSlow(masm_, call_helper);
3444
3445 __ bind(&done);
3446 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003447}
3448
3449
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003450void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3451 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003452 ASSERT(args->length() == 2);
3453
3454 VisitForStackValue(args->at(0));
3455 VisitForAccumulatorValue(args->at(1));
3456 __ mov(a0, result_register());
3457
3458 Register object = a1;
3459 Register index = a0;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003460 Register result = v0;
3461
3462 __ pop(object);
3463
3464 Label need_conversion;
3465 Label index_out_of_range;
3466 Label done;
3467 StringCharCodeAtGenerator generator(object,
3468 index,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003469 result,
3470 &need_conversion,
3471 &need_conversion,
3472 &index_out_of_range,
3473 STRING_INDEX_IS_NUMBER);
3474 generator.GenerateFast(masm_);
3475 __ jmp(&done);
3476
3477 __ bind(&index_out_of_range);
3478 // When the index is out of range, the spec requires us to return
3479 // NaN.
3480 __ LoadRoot(result, Heap::kNanValueRootIndex);
3481 __ jmp(&done);
3482
3483 __ bind(&need_conversion);
3484 // Load the undefined value into the result register, which will
3485 // trigger conversion.
3486 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3487 __ jmp(&done);
3488
3489 NopRuntimeCallHelper call_helper;
3490 generator.GenerateSlow(masm_, call_helper);
3491
3492 __ bind(&done);
3493 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003494}
3495
3496
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003497void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3498 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003499 ASSERT(args->length() == 2);
3500
3501 VisitForStackValue(args->at(0));
3502 VisitForAccumulatorValue(args->at(1));
3503 __ mov(a0, result_register());
3504
3505 Register object = a1;
3506 Register index = a0;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003507 Register scratch = a3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003508 Register result = v0;
3509
3510 __ pop(object);
3511
3512 Label need_conversion;
3513 Label index_out_of_range;
3514 Label done;
3515 StringCharAtGenerator generator(object,
3516 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003517 scratch,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003518 result,
3519 &need_conversion,
3520 &need_conversion,
3521 &index_out_of_range,
3522 STRING_INDEX_IS_NUMBER);
3523 generator.GenerateFast(masm_);
3524 __ jmp(&done);
3525
3526 __ bind(&index_out_of_range);
3527 // When the index is out of range, the spec requires us to return
3528 // the empty string.
ulan@chromium.org750145a2013-03-07 15:14:13 +00003529 __ LoadRoot(result, Heap::kempty_stringRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003530 __ jmp(&done);
3531
3532 __ bind(&need_conversion);
3533 // Move smi zero into the result register, which will trigger
3534 // conversion.
3535 __ li(result, Operand(Smi::FromInt(0)));
3536 __ jmp(&done);
3537
3538 NopRuntimeCallHelper call_helper;
3539 generator.GenerateSlow(masm_, call_helper);
3540
3541 __ bind(&done);
3542 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003543}
3544
3545
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003546void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3547 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003548 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003549 VisitForStackValue(args->at(0));
3550 VisitForStackValue(args->at(1));
3551
3552 StringAddStub stub(NO_STRING_ADD_FLAGS);
3553 __ CallStub(&stub);
3554 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003555}
3556
3557
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003558void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3559 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003560 ASSERT_EQ(2, args->length());
3561
3562 VisitForStackValue(args->at(0));
3563 VisitForStackValue(args->at(1));
3564
3565 StringCompareStub stub;
3566 __ CallStub(&stub);
3567 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003568}
3569
3570
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003571void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003572 // Load the argument on the stack and call the stub.
3573 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3574 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003575 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003576 ASSERT(args->length() == 1);
3577 VisitForStackValue(args->at(0));
3578 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3579 __ CallStub(&stub);
3580 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003581}
3582
3583
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003584void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003585 // Load the argument on the stack and call the stub.
3586 TranscendentalCacheStub stub(TranscendentalCache::COS,
3587 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003588 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003589 ASSERT(args->length() == 1);
3590 VisitForStackValue(args->at(0));
3591 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3592 __ CallStub(&stub);
3593 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003594}
3595
3596
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003597void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3598 // Load the argument on the stack and call the stub.
3599 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3600 TranscendentalCacheStub::TAGGED);
3601 ZoneList<Expression*>* args = expr->arguments();
3602 ASSERT(args->length() == 1);
3603 VisitForStackValue(args->at(0));
3604 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3605 __ CallStub(&stub);
3606 context()->Plug(v0);
3607}
3608
3609
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003610void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003611 // Load the argument on the stack and call the stub.
3612 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3613 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003614 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003615 ASSERT(args->length() == 1);
3616 VisitForStackValue(args->at(0));
3617 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3618 __ CallStub(&stub);
3619 context()->Plug(v0);
3620}
3621
3622
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003623void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003624 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003625 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003626 ASSERT(args->length() == 1);
3627 VisitForStackValue(args->at(0));
3628 __ CallRuntime(Runtime::kMath_sqrt, 1);
3629 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003630}
3631
3632
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003633void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3634 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003635 ASSERT(args->length() >= 2);
3636
3637 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3638 for (int i = 0; i < arg_count + 1; i++) {
3639 VisitForStackValue(args->at(i));
3640 }
3641 VisitForAccumulatorValue(args->last()); // Function.
3642
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00003643 Label runtime, done;
3644 // Check for non-function argument (including proxy).
3645 __ JumpIfSmi(v0, &runtime);
danno@chromium.orgc612e022011-11-10 11:38:15 +00003646 __ GetObjectType(v0, a1, a1);
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00003647 __ Branch(&runtime, ne, a1, Operand(JS_FUNCTION_TYPE));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003648
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003649 // InvokeFunction requires the function in a1. Move it in there.
3650 __ mov(a1, result_register());
3651 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003652 __ InvokeFunction(a1, count, CALL_FUNCTION,
3653 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003654 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003655 __ jmp(&done);
3656
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00003657 __ bind(&runtime);
danno@chromium.orgc612e022011-11-10 11:38:15 +00003658 __ push(v0);
3659 __ CallRuntime(Runtime::kCall, args->length());
3660 __ bind(&done);
3661
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003662 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003663}
3664
3665
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003666void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003667 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003668 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003669 ASSERT(args->length() == 3);
3670 VisitForStackValue(args->at(0));
3671 VisitForStackValue(args->at(1));
3672 VisitForStackValue(args->at(2));
3673 __ CallStub(&stub);
3674 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003675}
3676
3677
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003678void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3679 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003680 ASSERT_EQ(2, args->length());
3681
3682 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3683 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3684
3685 Handle<FixedArray> jsfunction_result_caches(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003686 isolate()->native_context()->jsfunction_result_caches());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003687 if (jsfunction_result_caches->length() <= cache_id) {
3688 __ Abort("Attempt to use undefined cache.");
3689 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3690 context()->Plug(v0);
3691 return;
3692 }
3693
3694 VisitForAccumulatorValue(args->at(1));
3695
3696 Register key = v0;
3697 Register cache = a1;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003698 __ lw(cache, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
3699 __ lw(cache, FieldMemOperand(cache, GlobalObject::kNativeContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003700 __ lw(cache,
3701 ContextOperand(
3702 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3703 __ lw(cache,
3704 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3705
3706
3707 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003708 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003709 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3710 // a2 now holds finger offset as a smi.
3711 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3712 // a3 now points to the start of fixed array elements.
3713 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3714 __ addu(a3, a3, at);
3715 // a3 now points to key of indexed element of cache.
3716 __ lw(a2, MemOperand(a3));
3717 __ Branch(&not_found, ne, key, Operand(a2));
3718
3719 __ lw(v0, MemOperand(a3, kPointerSize));
3720 __ Branch(&done);
3721
3722 __ bind(&not_found);
3723 // Call runtime to perform the lookup.
3724 __ Push(cache, key);
3725 __ CallRuntime(Runtime::kGetFromCache, 2);
3726
3727 __ bind(&done);
3728 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003729}
3730
3731
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003732void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3733 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003734 ASSERT_EQ(2, args->length());
3735
3736 Register right = v0;
3737 Register left = a1;
3738 Register tmp = a2;
3739 Register tmp2 = a3;
3740
3741 VisitForStackValue(args->at(0));
3742 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3743 __ pop(left);
3744
3745 Label done, fail, ok;
3746 __ Branch(&ok, eq, left, Operand(right));
3747 // Fail if either is a non-HeapObject.
3748 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003749 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003750 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3751 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3752 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3753 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3754 __ Branch(&fail, ne, tmp, Operand(tmp2));
3755 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3756 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3757 __ Branch(&ok, eq, tmp, Operand(tmp2));
3758 __ bind(&fail);
3759 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3760 __ jmp(&done);
3761 __ bind(&ok);
3762 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3763 __ bind(&done);
3764
3765 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003766}
3767
3768
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003769void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3770 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003771 VisitForAccumulatorValue(args->at(0));
3772
3773 Label materialize_true, materialize_false;
3774 Label* if_true = NULL;
3775 Label* if_false = NULL;
3776 Label* fall_through = NULL;
3777 context()->PrepareTest(&materialize_true, &materialize_false,
3778 &if_true, &if_false, &fall_through);
3779
3780 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3781 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3782
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003783 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003784 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3785
3786 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003787}
3788
3789
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003790void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3791 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003792 ASSERT(args->length() == 1);
3793 VisitForAccumulatorValue(args->at(0));
3794
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003795 __ AssertString(v0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003796
3797 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3798 __ IndexFromHash(v0, v0);
3799
3800 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003801}
3802
3803
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003804void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003805 Label bailout, done, one_char_separator, long_separator,
3806 non_trivial_array, not_size_one_array, loop,
3807 empty_separator_loop, one_char_separator_loop,
3808 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003809 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003810 ASSERT(args->length() == 2);
3811 VisitForStackValue(args->at(1));
3812 VisitForAccumulatorValue(args->at(0));
3813
3814 // All aliases of the same register have disjoint lifetimes.
3815 Register array = v0;
3816 Register elements = no_reg; // Will be v0.
3817 Register result = no_reg; // Will be v0.
3818 Register separator = a1;
3819 Register array_length = a2;
3820 Register result_pos = no_reg; // Will be a2.
3821 Register string_length = a3;
3822 Register string = t0;
3823 Register element = t1;
3824 Register elements_end = t2;
3825 Register scratch1 = t3;
3826 Register scratch2 = t5;
3827 Register scratch3 = t4;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003828
3829 // Separator operand is on the stack.
3830 __ pop(separator);
3831
3832 // Check that the array is a JSArray.
3833 __ JumpIfSmi(array, &bailout);
3834 __ GetObjectType(array, scratch1, scratch2);
3835 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3836
3837 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003838 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003839
3840 // If the array has length zero, return the empty string.
3841 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3842 __ SmiUntag(array_length);
3843 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
ulan@chromium.org750145a2013-03-07 15:14:13 +00003844 __ LoadRoot(v0, Heap::kempty_stringRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003845 __ Branch(&done);
3846
3847 __ bind(&non_trivial_array);
3848
3849 // Get the FixedArray containing array's elements.
3850 elements = array;
3851 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3852 array = no_reg; // End of array's live range.
3853
3854 // Check that all array elements are sequential ASCII strings, and
3855 // accumulate the sum of their lengths, as a smi-encoded value.
3856 __ mov(string_length, zero_reg);
3857 __ Addu(element,
3858 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3859 __ sll(elements_end, array_length, kPointerSizeLog2);
3860 __ Addu(elements_end, element, elements_end);
3861 // Loop condition: while (element < elements_end).
3862 // Live values in registers:
3863 // elements: Fixed array of strings.
3864 // array_length: Length of the fixed array of strings (not smi)
3865 // separator: Separator string
3866 // string_length: Accumulated sum of string lengths (smi).
3867 // element: Current array element.
3868 // elements_end: Array end.
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00003869 if (generate_debug_code_) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003870 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3871 array_length, Operand(zero_reg));
3872 }
3873 __ bind(&loop);
3874 __ lw(string, MemOperand(element));
3875 __ Addu(element, element, kPointerSize);
3876 __ JumpIfSmi(string, &bailout);
3877 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3878 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3879 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003880 __ lw(scratch1, FieldMemOperand(string, SeqOneByteString::kLengthOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003881 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3882 __ BranchOnOverflow(&bailout, scratch3);
3883 __ Branch(&loop, lt, element, Operand(elements_end));
3884
3885 // If array_length is 1, return elements[0], a string.
3886 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3887 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3888 __ Branch(&done);
3889
3890 __ bind(&not_size_one_array);
3891
3892 // Live values in registers:
3893 // separator: Separator string
3894 // array_length: Length of the array.
3895 // string_length: Sum of string lengths (smi).
3896 // elements: FixedArray of strings.
3897
3898 // Check that the separator is a flat ASCII string.
3899 __ JumpIfSmi(separator, &bailout);
3900 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3901 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3902 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3903
3904 // Add (separator length times array_length) - separator length to the
3905 // string_length to get the length of the result string. array_length is not
3906 // smi but the other values are, so the result is a smi.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003907 __ lw(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003908 __ Subu(string_length, string_length, Operand(scratch1));
3909 __ Mult(array_length, scratch1);
3910 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3911 // zero.
3912 __ mfhi(scratch2);
3913 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3914 __ mflo(scratch2);
3915 __ And(scratch3, scratch2, Operand(0x80000000));
3916 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3917 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3918 __ BranchOnOverflow(&bailout, scratch3);
3919 __ SmiUntag(string_length);
3920
3921 // Get first element in the array to free up the elements register to be used
3922 // for the result.
3923 __ Addu(element,
3924 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3925 result = elements; // End of live range for elements.
3926 elements = no_reg;
3927 // Live values in registers:
3928 // element: First array element
3929 // separator: Separator string
3930 // string_length: Length of result string (not smi)
3931 // array_length: Length of the array.
3932 __ AllocateAsciiString(result,
3933 string_length,
3934 scratch1,
3935 scratch2,
3936 elements_end,
3937 &bailout);
3938 // Prepare for looping. Set up elements_end to end of the array. Set
3939 // result_pos to the position of the result where to write the first
3940 // character.
3941 __ sll(elements_end, array_length, kPointerSizeLog2);
3942 __ Addu(elements_end, element, elements_end);
3943 result_pos = array_length; // End of live range for array_length.
3944 array_length = no_reg;
3945 __ Addu(result_pos,
3946 result,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003947 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003948
3949 // Check the length of the separator.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003950 __ lw(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003951 __ li(at, Operand(Smi::FromInt(1)));
3952 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3953 __ Branch(&long_separator, gt, scratch1, Operand(at));
3954
3955 // Empty separator case.
3956 __ bind(&empty_separator_loop);
3957 // Live values in registers:
3958 // result_pos: the position to which we are currently copying characters.
3959 // element: Current array element.
3960 // elements_end: Array end.
3961
3962 // Copy next array element to the result.
3963 __ lw(string, MemOperand(element));
3964 __ Addu(element, element, kPointerSize);
3965 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3966 __ SmiUntag(string_length);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003967 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003968 __ CopyBytes(string, result_pos, string_length, scratch1);
3969 // End while (element < elements_end).
3970 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3971 ASSERT(result.is(v0));
3972 __ Branch(&done);
3973
3974 // One-character separator case.
3975 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003976 // Replace separator with its ASCII character value.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003977 __ lbu(separator, FieldMemOperand(separator, SeqOneByteString::kHeaderSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003978 // Jump into the loop after the code that copies the separator, so the first
3979 // element is not preceded by a separator.
3980 __ jmp(&one_char_separator_loop_entry);
3981
3982 __ bind(&one_char_separator_loop);
3983 // Live values in registers:
3984 // result_pos: the position to which we are currently copying characters.
3985 // element: Current array element.
3986 // elements_end: Array end.
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003987 // separator: Single separator ASCII char (in lower byte).
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003988
3989 // Copy the separator character to the result.
3990 __ sb(separator, MemOperand(result_pos));
3991 __ Addu(result_pos, result_pos, 1);
3992
3993 // Copy next array element to the result.
3994 __ bind(&one_char_separator_loop_entry);
3995 __ lw(string, MemOperand(element));
3996 __ Addu(element, element, kPointerSize);
3997 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3998 __ SmiUntag(string_length);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003999 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004000 __ CopyBytes(string, result_pos, string_length, scratch1);
4001 // End while (element < elements_end).
4002 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
4003 ASSERT(result.is(v0));
4004 __ Branch(&done);
4005
4006 // Long separator case (separator is more than one character). Entry is at the
4007 // label long_separator below.
4008 __ bind(&long_separator_loop);
4009 // Live values in registers:
4010 // result_pos: the position to which we are currently copying characters.
4011 // element: Current array element.
4012 // elements_end: Array end.
4013 // separator: Separator string.
4014
4015 // Copy the separator to the result.
4016 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
4017 __ SmiUntag(string_length);
4018 __ Addu(string,
4019 separator,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004020 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004021 __ CopyBytes(string, result_pos, string_length, scratch1);
4022
4023 __ bind(&long_separator);
4024 __ lw(string, MemOperand(element));
4025 __ Addu(element, element, kPointerSize);
4026 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
4027 __ SmiUntag(string_length);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004028 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004029 __ CopyBytes(string, result_pos, string_length, scratch1);
4030 // End while (element < elements_end).
4031 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
4032 ASSERT(result.is(v0));
4033 __ Branch(&done);
4034
4035 __ bind(&bailout);
4036 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
4037 __ bind(&done);
4038 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004039}
4040
4041
ager@chromium.org5c838252010-02-19 08:53:10 +00004042void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004043 Handle<String> name = expr->name();
4044 if (name->length() > 0 && name->Get(0) == '_') {
4045 Comment cmnt(masm_, "[ InlineRuntimeCall");
4046 EmitInlineRuntimeCall(expr);
4047 return;
4048 }
4049
4050 Comment cmnt(masm_, "[ CallRuntime");
4051 ZoneList<Expression*>* args = expr->arguments();
4052
4053 if (expr->is_jsruntime()) {
4054 // Prepare for calling JS runtime function.
4055 __ lw(a0, GlobalObjectOperand());
4056 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
4057 __ push(a0);
4058 }
4059
4060 // Push the arguments ("left-to-right").
4061 int arg_count = args->length();
4062 for (int i = 0; i < arg_count; i++) {
4063 VisitForStackValue(args->at(i));
4064 }
4065
4066 if (expr->is_jsruntime()) {
4067 // Call the JS runtime function.
4068 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00004069 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004070 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00004071 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004072 CallIC(ic, mode, expr->CallRuntimeFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004073 // Restore context register.
4074 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4075 } else {
4076 // Call the C runtime function.
4077 __ CallRuntime(expr->function(), arg_count);
4078 }
4079 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004080}
4081
4082
4083void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004084 switch (expr->op()) {
4085 case Token::DELETE: {
4086 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004087 Property* property = expr->expression()->AsProperty();
4088 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004089
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004090 if (property != NULL) {
4091 VisitForStackValue(property->obj());
4092 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004093 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
4094 ? kNonStrictMode : kStrictMode;
4095 __ li(a1, Operand(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004096 __ push(a1);
4097 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4098 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004099 } else if (proxy != NULL) {
4100 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004101 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004102 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004103 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004104 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004105 __ lw(a2, GlobalObjectOperand());
4106 __ li(a1, Operand(var->name()));
4107 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
4108 __ Push(a2, a1, a0);
4109 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4110 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004111 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004112 // Result of deleting non-global, non-dynamic variables is false.
4113 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004114 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004115 } else {
4116 // Non-global variable. Call the runtime to try to delete from the
4117 // context where the variable was introduced.
4118 __ push(context_register());
4119 __ li(a2, Operand(var->name()));
4120 __ push(a2);
4121 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
4122 context()->Plug(v0);
4123 }
4124 } else {
4125 // Result of deleting non-property, non-variable reference is true.
4126 // The subexpression may have side effects.
4127 VisitForEffect(expr->expression());
4128 context()->Plug(true);
4129 }
4130 break;
4131 }
4132
4133 case Token::VOID: {
4134 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4135 VisitForEffect(expr->expression());
4136 context()->Plug(Heap::kUndefinedValueRootIndex);
4137 break;
4138 }
4139
4140 case Token::NOT: {
4141 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
4142 if (context()->IsEffect()) {
4143 // Unary NOT has no side effects so it's only necessary to visit the
4144 // subexpression. Match the optimizing compiler by not branching.
4145 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004146 } else if (context()->IsTest()) {
4147 const TestContext* test = TestContext::cast(context());
4148 // The labels are swapped for the recursive call.
4149 VisitForControl(expr->expression(),
4150 test->false_label(),
4151 test->true_label(),
4152 test->fall_through());
4153 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004154 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004155 // We handle value contexts explicitly rather than simply visiting
4156 // for control and plugging the control flow into the context,
4157 // because we need to prepare a pair of extra administrative AST ids
4158 // for the optimizing compiler.
4159 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
4160 Label materialize_true, materialize_false, done;
4161 VisitForControl(expr->expression(),
4162 &materialize_false,
4163 &materialize_true,
4164 &materialize_true);
4165 __ bind(&materialize_true);
4166 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4167 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
4168 if (context()->IsStackValue()) __ push(v0);
4169 __ jmp(&done);
4170 __ bind(&materialize_false);
4171 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4172 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
4173 if (context()->IsStackValue()) __ push(v0);
4174 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004175 }
4176 break;
4177 }
4178
4179 case Token::TYPEOF: {
4180 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
4181 { StackValueContext context(this);
4182 VisitForTypeofValue(expr->expression());
4183 }
4184 __ CallRuntime(Runtime::kTypeof, 1);
4185 context()->Plug(v0);
4186 break;
4187 }
4188
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004189 case Token::SUB:
4190 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
4191 break;
4192
4193 case Token::BIT_NOT:
4194 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
4195 break;
4196
4197 default:
4198 UNREACHABLE();
4199 }
4200}
4201
4202
4203void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
4204 const char* comment) {
4205 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
4206 Comment cmt(masm_, comment);
4207 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
4208 UnaryOverwriteMode overwrite =
4209 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004210 UnaryOpStub stub(expr->op(), overwrite);
4211 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004212 VisitForAccumulatorValue(expr->expression());
4213 SetSourcePosition(expr->position());
4214 __ mov(a0, result_register());
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004215 CallIC(stub.GetCode(isolate()), RelocInfo::CODE_TARGET,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004216 expr->UnaryOperationFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004217 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004218}
4219
4220
4221void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004222 Comment cmnt(masm_, "[ CountOperation");
4223 SetSourcePosition(expr->position());
4224
4225 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
4226 // as the left-hand side.
4227 if (!expr->expression()->IsValidLeftHandSide()) {
4228 VisitForEffect(expr->expression());
4229 return;
4230 }
4231
4232 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004233 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004234 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
4235 LhsKind assign_type = VARIABLE;
4236 Property* prop = expr->expression()->AsProperty();
4237 // In case of a property we use the uninitialized expression context
4238 // of the key to detect a named property.
4239 if (prop != NULL) {
4240 assign_type =
4241 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
4242 }
4243
4244 // Evaluate expression and get value.
4245 if (assign_type == VARIABLE) {
4246 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
4247 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00004248 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004249 } else {
4250 // Reserve space for result of postfix operation.
4251 if (expr->is_postfix() && !context()->IsEffect()) {
4252 __ li(at, Operand(Smi::FromInt(0)));
4253 __ push(at);
4254 }
4255 if (assign_type == NAMED_PROPERTY) {
4256 // Put the object both on the stack and in the accumulator.
4257 VisitForAccumulatorValue(prop->obj());
4258 __ push(v0);
4259 EmitNamedPropertyLoad(prop);
4260 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004261 VisitForStackValue(prop->obj());
4262 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004263 __ lw(a1, MemOperand(sp, 0));
4264 __ push(v0);
4265 EmitKeyedPropertyLoad(prop);
4266 }
4267 }
4268
4269 // We need a second deoptimization point after loading the value
4270 // in case evaluating the property load my have a side effect.
4271 if (assign_type == VARIABLE) {
4272 PrepareForBailout(expr->expression(), TOS_REG);
4273 } else {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004274 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004275 }
4276
4277 // Call ToNumber only if operand is not a smi.
4278 Label no_conversion;
4279 __ JumpIfSmi(v0, &no_conversion);
4280 __ mov(a0, v0);
4281 ToNumberStub convert_stub;
4282 __ CallStub(&convert_stub);
4283 __ bind(&no_conversion);
4284
4285 // Save result for postfix expressions.
4286 if (expr->is_postfix()) {
4287 if (!context()->IsEffect()) {
4288 // Save the result on the stack. If we have a named or keyed property
4289 // we store the result under the receiver that is currently on top
4290 // of the stack.
4291 switch (assign_type) {
4292 case VARIABLE:
4293 __ push(v0);
4294 break;
4295 case NAMED_PROPERTY:
4296 __ sw(v0, MemOperand(sp, kPointerSize));
4297 break;
4298 case KEYED_PROPERTY:
4299 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4300 break;
4301 }
4302 }
4303 }
4304 __ mov(a0, result_register());
4305
4306 // Inline smi case if we are in a loop.
4307 Label stub_call, done;
4308 JumpPatchSite patch_site(masm_);
4309
4310 int count_value = expr->op() == Token::INC ? 1 : -1;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004311 if (ShouldInlineSmiCase(expr->op())) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00004312 __ li(a1, Operand(Smi::FromInt(count_value)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004313 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
4314 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
4315
4316 // We could eliminate this smi check if we split the code at
4317 // the first smi check before calling ToNumber.
4318 patch_site.EmitJumpIfSmi(v0, &done);
4319 __ bind(&stub_call);
4320 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00004321 __ mov(a1, a0);
4322 __ li(a0, Operand(Smi::FromInt(count_value)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004323
4324 // Record position before stub call.
4325 SetSourcePosition(expr->position());
4326
danno@chromium.org40cb8782011-05-25 07:58:50 +00004327 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004328 CallIC(stub.GetCode(isolate()),
4329 RelocInfo::CODE_TARGET,
4330 expr->CountBinOpFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004331 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004332 __ bind(&done);
4333
4334 // Store the value returned in v0.
4335 switch (assign_type) {
4336 case VARIABLE:
4337 if (expr->is_postfix()) {
4338 { EffectContext context(this);
4339 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4340 Token::ASSIGN);
4341 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4342 context.Plug(v0);
4343 }
4344 // For all contexts except EffectConstant we have the result on
4345 // top of the stack.
4346 if (!context()->IsEffect()) {
4347 context()->PlugTOS();
4348 }
4349 } else {
4350 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4351 Token::ASSIGN);
4352 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4353 context()->Plug(v0);
4354 }
4355 break;
4356 case NAMED_PROPERTY: {
4357 __ mov(a0, result_register()); // Value.
4358 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4359 __ pop(a1); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004360 Handle<Code> ic = is_classic_mode()
4361 ? isolate()->builtins()->StoreIC_Initialize()
4362 : isolate()->builtins()->StoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004363 CallIC(ic, RelocInfo::CODE_TARGET, expr->CountStoreFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004364 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4365 if (expr->is_postfix()) {
4366 if (!context()->IsEffect()) {
4367 context()->PlugTOS();
4368 }
4369 } else {
4370 context()->Plug(v0);
4371 }
4372 break;
4373 }
4374 case KEYED_PROPERTY: {
4375 __ mov(a0, result_register()); // Value.
4376 __ pop(a1); // Key.
4377 __ pop(a2); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004378 Handle<Code> ic = is_classic_mode()
4379 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4380 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004381 CallIC(ic, RelocInfo::CODE_TARGET, expr->CountStoreFeedbackId());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004382 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4383 if (expr->is_postfix()) {
4384 if (!context()->IsEffect()) {
4385 context()->PlugTOS();
4386 }
4387 } else {
4388 context()->Plug(v0);
4389 }
4390 break;
4391 }
4392 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004393}
4394
4395
lrn@chromium.org7516f052011-03-30 08:52:27 +00004396void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004397 ASSERT(!context()->IsEffect());
4398 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004399 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004400 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004401 Comment cmnt(masm_, "Global variable");
4402 __ lw(a0, GlobalObjectOperand());
4403 __ li(a2, Operand(proxy->name()));
4404 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4405 // Use a regular load, not a contextual load, to avoid a reference
4406 // error.
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004407 CallIC(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004408 PrepareForBailout(expr, TOS_REG);
4409 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004410 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004411 Label done, slow;
4412
4413 // Generate code for loading from variables potentially shadowed
4414 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004415 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004416
4417 __ bind(&slow);
4418 __ li(a0, Operand(proxy->name()));
4419 __ Push(cp, a0);
4420 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4421 PrepareForBailout(expr, TOS_REG);
4422 __ bind(&done);
4423
4424 context()->Plug(v0);
4425 } else {
4426 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004427 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004428 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004429}
4430
ager@chromium.org04921a82011-06-27 13:21:41 +00004431void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004432 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004433 Handle<String> check) {
4434 Label materialize_true, materialize_false;
4435 Label* if_true = NULL;
4436 Label* if_false = NULL;
4437 Label* fall_through = NULL;
4438 context()->PrepareTest(&materialize_true, &materialize_false,
4439 &if_true, &if_false, &fall_through);
4440
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004441 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004442 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004443 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004444 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004445
ulan@chromium.org750145a2013-03-07 15:14:13 +00004446 if (check->Equals(isolate()->heap()->number_string())) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004447 __ JumpIfSmi(v0, if_true);
4448 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4449 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4450 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
ulan@chromium.org750145a2013-03-07 15:14:13 +00004451 } else if (check->Equals(isolate()->heap()->string_string())) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004452 __ JumpIfSmi(v0, if_false);
4453 // Check for undetectable objects => false.
4454 __ GetObjectType(v0, v0, a1);
4455 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4456 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4457 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4458 Split(eq, a1, Operand(zero_reg),
4459 if_true, if_false, fall_through);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00004460 } else if (check->Equals(isolate()->heap()->symbol_string())) {
4461 __ JumpIfSmi(v0, if_false);
4462 __ GetObjectType(v0, v0, a1);
4463 Split(eq, a1, Operand(SYMBOL_TYPE), if_true, if_false, fall_through);
ulan@chromium.org750145a2013-03-07 15:14:13 +00004464 } else if (check->Equals(isolate()->heap()->boolean_string())) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004465 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4466 __ Branch(if_true, eq, v0, Operand(at));
4467 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4468 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004469 } else if (FLAG_harmony_typeof &&
ulan@chromium.org750145a2013-03-07 15:14:13 +00004470 check->Equals(isolate()->heap()->null_string())) {
danno@chromium.orgb6451162011-08-17 14:33:23 +00004471 __ LoadRoot(at, Heap::kNullValueRootIndex);
4472 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
ulan@chromium.org750145a2013-03-07 15:14:13 +00004473 } else if (check->Equals(isolate()->heap()->undefined_string())) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004474 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4475 __ Branch(if_true, eq, v0, Operand(at));
4476 __ JumpIfSmi(v0, if_false);
4477 // Check for undetectable objects => true.
4478 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4479 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4480 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4481 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
ulan@chromium.org750145a2013-03-07 15:14:13 +00004482 } else if (check->Equals(isolate()->heap()->function_string())) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004483 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004484 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4485 __ GetObjectType(v0, v0, a1);
4486 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4487 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4488 if_true, if_false, fall_through);
ulan@chromium.org750145a2013-03-07 15:14:13 +00004489 } else if (check->Equals(isolate()->heap()->object_string())) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004490 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004491 if (!FLAG_harmony_typeof) {
4492 __ LoadRoot(at, Heap::kNullValueRootIndex);
4493 __ Branch(if_true, eq, v0, Operand(at));
4494 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004495 // Check for JS objects => true.
4496 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004497 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004498 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004499 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004500 // Check for undetectable objects => false.
4501 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4502 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4503 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4504 } else {
4505 if (if_false != fall_through) __ jmp(if_false);
4506 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004507 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004508}
4509
4510
ager@chromium.org5c838252010-02-19 08:53:10 +00004511void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004512 Comment cmnt(masm_, "[ CompareOperation");
4513 SetSourcePosition(expr->position());
4514
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004515 // First we try a fast inlined version of the compare when one of
4516 // the operands is a literal.
4517 if (TryLiteralCompare(expr)) return;
4518
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004519 // Always perform the comparison for its control flow. Pack the result
4520 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004521 Label materialize_true, materialize_false;
4522 Label* if_true = NULL;
4523 Label* if_false = NULL;
4524 Label* fall_through = NULL;
4525 context()->PrepareTest(&materialize_true, &materialize_false,
4526 &if_true, &if_false, &fall_through);
4527
ager@chromium.org04921a82011-06-27 13:21:41 +00004528 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004529 VisitForStackValue(expr->left());
4530 switch (op) {
4531 case Token::IN:
4532 VisitForStackValue(expr->right());
4533 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004534 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004535 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4536 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4537 break;
4538
4539 case Token::INSTANCEOF: {
4540 VisitForStackValue(expr->right());
4541 InstanceofStub stub(InstanceofStub::kNoFlags);
4542 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004543 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004544 // The stub returns 0 for true.
4545 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4546 break;
4547 }
4548
4549 default: {
4550 VisitForAccumulatorValue(expr->right());
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00004551 Condition cc = CompareIC::ComputeCondition(op);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004552 __ mov(a0, result_register());
4553 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004554
4555 bool inline_smi_code = ShouldInlineSmiCase(op);
4556 JumpPatchSite patch_site(masm_);
4557 if (inline_smi_code) {
4558 Label slow_case;
4559 __ Or(a2, a0, Operand(a1));
4560 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4561 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4562 __ bind(&slow_case);
4563 }
4564 // Record position and call the compare IC.
4565 SetSourcePosition(expr->position());
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004566 Handle<Code> ic = CompareIC::GetUninitialized(isolate(), op);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004567 CallIC(ic, RelocInfo::CODE_TARGET, expr->CompareOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004568 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004569 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004570 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4571 }
4572 }
4573
4574 // Convert the result of the comparison into one expected for this
4575 // expression's context.
4576 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004577}
4578
4579
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004580void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4581 Expression* sub_expr,
4582 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004583 Label materialize_true, materialize_false;
4584 Label* if_true = NULL;
4585 Label* if_false = NULL;
4586 Label* fall_through = NULL;
4587 context()->PrepareTest(&materialize_true, &materialize_false,
4588 &if_true, &if_false, &fall_through);
4589
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004590 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004591 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004592 EqualityKind kind = expr->op() == Token::EQ_STRICT
4593 ? kStrictEquality : kNonStrictEquality;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004594 __ mov(a0, result_register());
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004595 if (kind == kStrictEquality) {
4596 Heap::RootListIndex nil_value = nil == kNullValue ?
4597 Heap::kNullValueRootIndex :
4598 Heap::kUndefinedValueRootIndex;
4599 __ LoadRoot(a1, nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004600 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4601 } else {
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004602 Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(),
4603 kNonStrictEquality,
4604 nil);
4605 CallIC(ic, RelocInfo::CODE_TARGET, expr->CompareOperationFeedbackId());
4606 Split(ne, v0, Operand(zero_reg), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004607 }
4608 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004609}
4610
4611
ager@chromium.org5c838252010-02-19 08:53:10 +00004612void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004613 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4614 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004615}
4616
4617
lrn@chromium.org7516f052011-03-30 08:52:27 +00004618Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004619 return v0;
4620}
ager@chromium.org5c838252010-02-19 08:53:10 +00004621
4622
lrn@chromium.org7516f052011-03-30 08:52:27 +00004623Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004624 return cp;
4625}
4626
4627
ager@chromium.org5c838252010-02-19 08:53:10 +00004628void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004629 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4630 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004631}
4632
4633
4634void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004635 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004636}
4637
4638
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004639void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4640 Scope* declaration_scope = scope()->DeclarationScope();
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004641 if (declaration_scope->is_global_scope() ||
4642 declaration_scope->is_module_scope()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00004643 // Contexts nested in the native context have a canonical empty function
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004644 // as their closure, not the anonymous closure containing the global
4645 // code. Pass a smi sentinel and let the runtime look up the empty
4646 // function.
4647 __ li(at, Operand(Smi::FromInt(0)));
4648 } else if (declaration_scope->is_eval_scope()) {
4649 // Contexts created by a call to eval have the same closure as the
4650 // context calling eval, not the anonymous closure containing the eval
4651 // code. Fetch it from the context.
4652 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4653 } else {
4654 ASSERT(declaration_scope->is_function_scope());
4655 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4656 }
4657 __ push(at);
4658}
4659
4660
ager@chromium.org5c838252010-02-19 08:53:10 +00004661// ----------------------------------------------------------------------------
4662// Non-local control flow support.
4663
4664void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004665 ASSERT(!result_register().is(a1));
4666 // Store result register while executing finally block.
4667 __ push(result_register());
4668 // Cook return address in link register to stack (smi encoded Code* delta).
4669 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4670 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004671 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004672 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004673
4674 // Store result register while executing finally block.
4675 __ push(a1);
4676
4677 // Store pending message while executing finally block.
4678 ExternalReference pending_message_obj =
4679 ExternalReference::address_of_pending_message_obj(isolate());
4680 __ li(at, Operand(pending_message_obj));
4681 __ lw(a1, MemOperand(at));
4682 __ push(a1);
4683
4684 ExternalReference has_pending_message =
4685 ExternalReference::address_of_has_pending_message(isolate());
4686 __ li(at, Operand(has_pending_message));
4687 __ lw(a1, MemOperand(at));
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004688 __ SmiTag(a1);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004689 __ push(a1);
4690
4691 ExternalReference pending_message_script =
4692 ExternalReference::address_of_pending_message_script(isolate());
4693 __ li(at, Operand(pending_message_script));
4694 __ lw(a1, MemOperand(at));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004695 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004696}
4697
4698
4699void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004700 ASSERT(!result_register().is(a1));
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004701 // Restore pending message from stack.
4702 __ pop(a1);
4703 ExternalReference pending_message_script =
4704 ExternalReference::address_of_pending_message_script(isolate());
4705 __ li(at, Operand(pending_message_script));
4706 __ sw(a1, MemOperand(at));
4707
4708 __ pop(a1);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004709 __ SmiUntag(a1);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004710 ExternalReference has_pending_message =
4711 ExternalReference::address_of_has_pending_message(isolate());
4712 __ li(at, Operand(has_pending_message));
4713 __ sw(a1, MemOperand(at));
4714
4715 __ pop(a1);
4716 ExternalReference pending_message_obj =
4717 ExternalReference::address_of_pending_message_obj(isolate());
4718 __ li(at, Operand(pending_message_obj));
4719 __ sw(a1, MemOperand(at));
4720
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004721 // Restore result register from stack.
4722 __ pop(a1);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004723
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004724 // Uncook return address and return.
4725 __ pop(result_register());
4726 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4727 __ sra(a1, a1, 1); // Un-smi-tag value.
4728 __ Addu(at, a1, Operand(masm_->CodeObject()));
4729 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004730}
4731
4732
4733#undef __
4734
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004735#define __ ACCESS_MASM(masm())
4736
4737FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4738 int* stack_depth,
4739 int* context_length) {
4740 // The macros used here must preserve the result register.
4741
4742 // Because the handler block contains the context of the finally
4743 // code, we can restore it directly from there for the finally code
4744 // rather than iteratively unwinding contexts via their previous
4745 // links.
4746 __ Drop(*stack_depth); // Down to the handler block.
4747 if (*context_length > 0) {
4748 // Restore the context to its dedicated register and the stack.
4749 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4750 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4751 }
4752 __ PopTryHandler();
4753 __ Call(finally_entry_);
4754
4755 *stack_depth = 0;
4756 *context_length = 0;
4757 return previous_;
4758}
4759
4760
4761#undef __
4762
ager@chromium.org5c838252010-02-19 08:53:10 +00004763} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004764
4765#endif // V8_TARGET_ARCH_MIPS