blob: 64cdc8e9d58d47321716d6c65ae02925feadb68d [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(
142 Handle<Smi>(Smi::FromInt(FLAG_interrupt_budget)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000143 SetFunctionPosition(function());
144 Comment cmnt(masm_, "[ function compiled by full code generator");
145
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000146#ifdef DEBUG
147 if (strlen(FLAG_stop_at) > 0 &&
148 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
149 __ stop("stop-at");
150 }
151#endif
152
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000153 // Strict mode functions and builtins need to replace the receiver
154 // with undefined when called as functions (without an explicit
155 // receiver object). t1 is zero for method calls and non-zero for
156 // function calls.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000157 if (!info->is_classic_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000158 Label ok;
159 __ Branch(&ok, eq, t1, Operand(zero_reg));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000160 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000161 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
162 __ sw(a2, MemOperand(sp, receiver_offset));
163 __ bind(&ok);
164 }
165
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000166 // Open a frame scope to indicate that there is a frame on the stack. The
167 // MANUAL indicates that the scope shouldn't actually generate code to set up
168 // the frame (that is done below).
169 FrameScope frame_scope(masm_, StackFrame::MANUAL);
170
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000171 int locals_count = info->scope()->num_stack_slots();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000172
173 __ Push(ra, fp, cp, a1);
174 if (locals_count > 0) {
175 // Load undefined value here, so the value is ready for the loop
176 // below.
177 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
178 }
179 // Adjust fp to point to caller's fp.
180 __ Addu(fp, sp, Operand(2 * kPointerSize));
181
182 { Comment cmnt(masm_, "[ Allocate locals");
183 for (int i = 0; i < locals_count; i++) {
184 __ push(at);
185 }
186 }
187
188 bool function_in_register = true;
189
190 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000191 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000192 if (heap_slots > 0) {
193 Comment cmnt(masm_, "[ Allocate local context");
194 // Argument to NewContext is the function, which is in a1.
195 __ push(a1);
196 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
197 FastNewContextStub stub(heap_slots);
198 __ CallStub(&stub);
199 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000200 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000201 }
202 function_in_register = false;
203 // Context is returned in both v0 and cp. It replaces the context
204 // passed to us. It's saved in the stack and kept live in cp.
205 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
206 // Copy any necessary parameters into the context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000207 int num_parameters = info->scope()->num_parameters();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000208 for (int i = 0; i < num_parameters; i++) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000209 Variable* var = scope()->parameter(i);
210 if (var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000211 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
212 (num_parameters - 1 - i) * kPointerSize;
213 // Load parameter from stack.
214 __ lw(a0, MemOperand(fp, parameter_offset));
215 // Store it in the context.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000216 MemOperand target = ContextOperand(cp, var->index());
217 __ sw(a0, target);
218
219 // Update the write barrier.
220 __ RecordWriteContextSlot(
221 cp, target.offset(), a0, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000222 }
223 }
224 }
225
226 Variable* arguments = scope()->arguments();
227 if (arguments != NULL) {
228 // Function uses arguments object.
229 Comment cmnt(masm_, "[ Allocate arguments object");
230 if (!function_in_register) {
231 // Load this again, if it's used by the local context below.
232 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
233 } else {
234 __ mov(a3, a1);
235 }
236 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000237 int num_parameters = info->scope()->num_parameters();
238 int offset = num_parameters * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000239 __ Addu(a2, fp,
240 Operand(StandardFrameConstants::kCallerSPOffset + offset));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000241 __ li(a1, Operand(Smi::FromInt(num_parameters)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000242 __ Push(a3, a2, a1);
243
244 // Arguments to ArgumentsAccessStub:
245 // function, receiver address, parameter count.
246 // The stub will rewrite receiever and parameter count if the previous
247 // stack frame was an arguments adapter frame.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000248 ArgumentsAccessStub::Type type;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000249 if (!is_classic_mode()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000250 type = ArgumentsAccessStub::NEW_STRICT;
251 } else if (function()->has_duplicate_parameters()) {
252 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
253 } else {
254 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
255 }
256 ArgumentsAccessStub stub(type);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000257 __ CallStub(&stub);
258
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000259 SetVar(arguments, v0, a1, a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000260 }
261
262 if (FLAG_trace) {
263 __ CallRuntime(Runtime::kTraceEnter, 0);
264 }
265
266 // Visit the declarations and body unless there is an illegal
267 // redeclaration.
268 if (scope()->HasIllegalRedeclaration()) {
269 Comment cmnt(masm_, "[ Declarations");
270 scope()->VisitIllegalRedeclaration(this);
271
272 } else {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000273 PrepareForBailoutForId(AstNode::kFunctionEntryId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000274 { Comment cmnt(masm_, "[ Declarations");
275 // For named function expressions, declare the function name as a
276 // constant.
277 if (scope()->is_function_scope() && scope()->function() != NULL) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000278 VariableProxy* proxy = scope()->function();
279 ASSERT(proxy->var()->mode() == CONST ||
280 proxy->var()->mode() == CONST_HARMONY);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000281 ASSERT(proxy->var()->location() != Variable::UNALLOCATED);
282 EmitDeclaration(proxy, proxy->var()->mode(), NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000283 }
284 VisitDeclarations(scope()->declarations());
285 }
286
287 { Comment cmnt(masm_, "[ Stack check");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000288 PrepareForBailoutForId(AstNode::kDeclarationsId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000289 Label ok;
290 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
291 __ Branch(&ok, hs, sp, Operand(t0));
292 StackCheckStub stub;
293 __ CallStub(&stub);
294 __ bind(&ok);
295 }
296
297 { Comment cmnt(masm_, "[ Body");
298 ASSERT(loop_depth() == 0);
299 VisitStatements(function()->body());
300 ASSERT(loop_depth() == 0);
301 }
302 }
303
304 // Always emit a 'return undefined' in case control fell off the end of
305 // the body.
306 { Comment cmnt(masm_, "[ return <undefined>;");
307 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
308 }
309 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000310}
311
312
313void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000314 ASSERT(Smi::FromInt(0) == 0);
315 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000316}
317
318
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000319void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
320 __ li(a2, Operand(profiling_counter_));
321 __ lw(a3, FieldMemOperand(a2, JSGlobalPropertyCell::kValueOffset));
322 __ Subu(a3, a3, Operand(Smi::FromInt(delta)));
323 __ sw(a3, FieldMemOperand(a2, JSGlobalPropertyCell::kValueOffset));
324}
325
326
327void FullCodeGenerator::EmitProfilingCounterReset() {
328 int reset_value = FLAG_interrupt_budget;
329 if (info_->ShouldSelfOptimize() && !FLAG_retry_self_opt) {
330 // Self-optimization is a one-off thing: if it fails, don't try again.
331 reset_value = Smi::kMaxValue;
332 }
333 if (isolate()->IsDebuggerActive()) {
334 // Detect debug break requests as soon as possible.
335 reset_value = 10;
336 }
337 __ li(a2, Operand(profiling_counter_));
338 __ li(a3, Operand(Smi::FromInt(reset_value)));
339 __ sw(a3, FieldMemOperand(a2, JSGlobalPropertyCell::kValueOffset));
340}
341
342
343static const int kMaxBackEdgeWeight = 127;
344static const int kBackEdgeDistanceDivisor = 142;
345
346
yangguo@chromium.org56454712012-02-16 15:33:53 +0000347void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt,
348 Label* back_edge_target) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000349 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
350 // to make sure it is constant. Branch may emit a skip-or-jump sequence
351 // instead of the normal Branch. It seems that the "skip" part of that
352 // sequence is about as long as this Branch would be so it is safe to ignore
353 // that.
354 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000355 Comment cmnt(masm_, "[ Stack check");
356 Label ok;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000357 if (FLAG_count_based_interrupts) {
358 int weight = 1;
359 if (FLAG_weighted_back_edges) {
360 ASSERT(back_edge_target->is_bound());
361 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
362 weight = Min(kMaxBackEdgeWeight,
363 Max(1, distance / kBackEdgeDistanceDivisor));
364 }
365 EmitProfilingCounterDecrement(weight);
366 __ slt(at, a3, zero_reg);
367 __ beq(at, zero_reg, &ok);
368 // CallStub will emit a li t9 first, so it is safe to use the delay slot.
369 InterruptStub stub;
370 __ CallStub(&stub);
371 } else {
372 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
373 __ sltu(at, sp, t0);
374 __ beq(at, zero_reg, &ok);
375 // CallStub will emit a li t9 first, so it is safe to use the delay slot.
376 StackCheckStub stub;
377 __ CallStub(&stub);
378 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000379 // Record a mapping of this PC offset to the OSR id. This is used to find
380 // the AST id from the unoptimized code in order to use it as a key into
381 // the deoptimization input data found in the optimized code.
382 RecordStackCheck(stmt->OsrEntryId());
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000383 if (FLAG_count_based_interrupts) {
384 EmitProfilingCounterReset();
385 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000386
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000387 __ bind(&ok);
388 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
389 // Record a mapping of the OSR id to this PC. This is used if the OSR
390 // entry becomes the target of a bailout. We don't expect it to be, but
391 // we want it to work if it is.
392 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000393}
394
395
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000396void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000397 Comment cmnt(masm_, "[ Return sequence");
398 if (return_label_.is_bound()) {
399 __ Branch(&return_label_);
400 } else {
401 __ bind(&return_label_);
402 if (FLAG_trace) {
403 // Push the return value on the stack as the parameter.
404 // Runtime::TraceExit returns its parameter in v0.
405 __ push(v0);
406 __ CallRuntime(Runtime::kTraceExit, 1);
407 }
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000408 if (FLAG_interrupt_at_exit || FLAG_self_optimization) {
409 // Pretend that the exit is a backwards jump to the entry.
410 int weight = 1;
411 if (info_->ShouldSelfOptimize()) {
412 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
413 } else if (FLAG_weighted_back_edges) {
414 int distance = masm_->pc_offset();
415 weight = Min(kMaxBackEdgeWeight,
416 Max(1, distance / kBackEdgeDistanceDivisor));
417 }
418 EmitProfilingCounterDecrement(weight);
419 Label ok;
420 __ Branch(&ok, ge, a3, Operand(zero_reg));
421 __ push(v0);
422 if (info_->ShouldSelfOptimize() && FLAG_direct_self_opt) {
423 __ lw(a2, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
424 __ push(a2);
425 __ CallRuntime(Runtime::kOptimizeFunctionOnNextCall, 1);
426 } else {
427 InterruptStub stub;
428 __ CallStub(&stub);
429 }
430 __ pop(v0);
431 EmitProfilingCounterReset();
432 __ bind(&ok);
433 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000434
435#ifdef DEBUG
436 // Add a label for checking the size of the code used for returning.
437 Label check_exit_codesize;
438 masm_->bind(&check_exit_codesize);
439#endif
440 // Make sure that the constant pool is not emitted inside of the return
441 // sequence.
442 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
443 // Here we use masm_-> instead of the __ macro to avoid the code coverage
444 // tool from instrumenting as we rely on the code size here.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000445 int32_t sp_delta = (info_->scope()->num_parameters() + 1) * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000446 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
447 __ RecordJSReturn();
448 masm_->mov(sp, fp);
449 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
450 masm_->Addu(sp, sp, Operand(sp_delta));
451 masm_->Jump(ra);
452 }
453
454#ifdef DEBUG
455 // Check that the size of the code used for returning is large enough
456 // for the debugger's requirements.
457 ASSERT(Assembler::kJSReturnSequenceInstructions <=
458 masm_->InstructionsGeneratedSince(&check_exit_codesize));
459#endif
460 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000461}
462
463
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000464void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
465 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
ager@chromium.org5c838252010-02-19 08:53:10 +0000466}
467
468
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000469void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
470 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
471 codegen()->GetVar(result_register(), var);
ager@chromium.org5c838252010-02-19 08:53:10 +0000472}
473
474
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000475void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
476 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
477 codegen()->GetVar(result_register(), var);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000478 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000479}
480
481
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000482void FullCodeGenerator::TestContext::Plug(Variable* var) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000483 // For simplicity we always test the accumulator register.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000484 codegen()->GetVar(result_register(), var);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000485 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000486 codegen()->DoTest(this);
ager@chromium.org5c838252010-02-19 08:53:10 +0000487}
488
489
lrn@chromium.org7516f052011-03-30 08:52:27 +0000490void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000491}
492
493
lrn@chromium.org7516f052011-03-30 08:52:27 +0000494void FullCodeGenerator::AccumulatorValueContext::Plug(
495 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000496 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000497}
498
499
500void FullCodeGenerator::StackValueContext::Plug(
501 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000502 __ LoadRoot(result_register(), index);
503 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000504}
505
506
507void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000508 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000509 true,
510 true_label_,
511 false_label_);
512 if (index == Heap::kUndefinedValueRootIndex ||
513 index == Heap::kNullValueRootIndex ||
514 index == Heap::kFalseValueRootIndex) {
515 if (false_label_ != fall_through_) __ Branch(false_label_);
516 } else if (index == Heap::kTrueValueRootIndex) {
517 if (true_label_ != fall_through_) __ Branch(true_label_);
518 } else {
519 __ LoadRoot(result_register(), index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000520 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000521 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000522}
523
524
525void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000526}
527
528
529void FullCodeGenerator::AccumulatorValueContext::Plug(
530 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000531 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000532}
533
534
535void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000536 // Immediates cannot be pushed directly.
537 __ li(result_register(), Operand(lit));
538 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000539}
540
541
542void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000543 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000544 true,
545 true_label_,
546 false_label_);
547 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
548 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
549 if (false_label_ != fall_through_) __ Branch(false_label_);
550 } else if (lit->IsTrue() || lit->IsJSObject()) {
551 if (true_label_ != fall_through_) __ Branch(true_label_);
552 } else if (lit->IsString()) {
553 if (String::cast(*lit)->length() == 0) {
554 if (false_label_ != fall_through_) __ Branch(false_label_);
555 } else {
556 if (true_label_ != fall_through_) __ Branch(true_label_);
557 }
558 } else if (lit->IsSmi()) {
559 if (Smi::cast(*lit)->value() == 0) {
560 if (false_label_ != fall_through_) __ Branch(false_label_);
561 } else {
562 if (true_label_ != fall_through_) __ Branch(true_label_);
563 }
564 } else {
565 // For simplicity we always test the accumulator register.
566 __ li(result_register(), Operand(lit));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000567 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000568 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000569}
570
571
572void FullCodeGenerator::EffectContext::DropAndPlug(int count,
573 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000574 ASSERT(count > 0);
575 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000576}
577
578
579void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
580 int count,
581 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000582 ASSERT(count > 0);
583 __ Drop(count);
584 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000585}
586
587
588void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
589 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000590 ASSERT(count > 0);
591 if (count > 1) __ Drop(count - 1);
592 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000593}
594
595
596void FullCodeGenerator::TestContext::DropAndPlug(int count,
597 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000598 ASSERT(count > 0);
599 // For simplicity we always test the accumulator register.
600 __ Drop(count);
601 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000602 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000603 codegen()->DoTest(this);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000604}
605
606
607void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
608 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000609 ASSERT(materialize_true == materialize_false);
610 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000611}
612
613
614void FullCodeGenerator::AccumulatorValueContext::Plug(
615 Label* materialize_true,
616 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000617 Label done;
618 __ bind(materialize_true);
619 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
620 __ Branch(&done);
621 __ bind(materialize_false);
622 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
623 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000624}
625
626
627void FullCodeGenerator::StackValueContext::Plug(
628 Label* materialize_true,
629 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000630 Label done;
631 __ bind(materialize_true);
632 __ LoadRoot(at, Heap::kTrueValueRootIndex);
633 __ push(at);
634 __ Branch(&done);
635 __ bind(materialize_false);
636 __ LoadRoot(at, Heap::kFalseValueRootIndex);
637 __ push(at);
638 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000639}
640
641
642void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
643 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000644 ASSERT(materialize_true == true_label_);
645 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000646}
647
648
649void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000650}
651
652
653void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000654 Heap::RootListIndex value_root_index =
655 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
656 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000657}
658
659
660void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000661 Heap::RootListIndex value_root_index =
662 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
663 __ LoadRoot(at, value_root_index);
664 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000665}
666
667
668void FullCodeGenerator::TestContext::Plug(bool flag) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000669 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000670 true,
671 true_label_,
672 false_label_);
673 if (flag) {
674 if (true_label_ != fall_through_) __ Branch(true_label_);
675 } else {
676 if (false_label_ != fall_through_) __ Branch(false_label_);
677 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000678}
679
680
whesse@chromium.org7b260152011-06-20 15:33:18 +0000681void FullCodeGenerator::DoTest(Expression* condition,
682 Label* if_true,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000683 Label* if_false,
684 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000685 if (CpuFeatures::IsSupported(FPU)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000686 ToBooleanStub stub(result_register());
687 __ CallStub(&stub);
688 __ mov(at, zero_reg);
689 } else {
690 // Call the runtime to find the boolean value of the source and then
691 // translate it into control flow to the pair of labels.
692 __ push(result_register());
693 __ CallRuntime(Runtime::kToBool, 1);
694 __ LoadRoot(at, Heap::kFalseValueRootIndex);
695 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000696 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000697}
698
699
lrn@chromium.org7516f052011-03-30 08:52:27 +0000700void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000701 Register lhs,
702 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000703 Label* if_true,
704 Label* if_false,
705 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000706 if (if_false == fall_through) {
707 __ Branch(if_true, cc, lhs, rhs);
708 } else if (if_true == fall_through) {
709 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
710 } else {
711 __ Branch(if_true, cc, lhs, rhs);
712 __ Branch(if_false);
713 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000714}
715
716
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000717MemOperand FullCodeGenerator::StackOperand(Variable* var) {
718 ASSERT(var->IsStackAllocated());
719 // Offset is negative because higher indexes are at lower addresses.
720 int offset = -var->index() * kPointerSize;
721 // Adjust by a (parameter or local) base offset.
722 if (var->IsParameter()) {
723 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
724 } else {
725 offset += JavaScriptFrameConstants::kLocal0Offset;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000726 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000727 return MemOperand(fp, offset);
ager@chromium.org5c838252010-02-19 08:53:10 +0000728}
729
730
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000731MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
732 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
733 if (var->IsContextSlot()) {
734 int context_chain_length = scope()->ContextChainLength(var->scope());
735 __ LoadContext(scratch, context_chain_length);
736 return ContextOperand(scratch, var->index());
737 } else {
738 return StackOperand(var);
739 }
740}
741
742
743void FullCodeGenerator::GetVar(Register dest, Variable* var) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000744 // Use destination as scratch.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000745 MemOperand location = VarOperand(var, dest);
746 __ lw(dest, location);
747}
748
749
750void FullCodeGenerator::SetVar(Variable* var,
751 Register src,
752 Register scratch0,
753 Register scratch1) {
754 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
755 ASSERT(!scratch0.is(src));
756 ASSERT(!scratch0.is(scratch1));
757 ASSERT(!scratch1.is(src));
758 MemOperand location = VarOperand(var, scratch0);
759 __ sw(src, location);
760 // Emit the write barrier code if the location is in the heap.
761 if (var->IsContextSlot()) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000762 __ RecordWriteContextSlot(scratch0,
763 location.offset(),
764 src,
765 scratch1,
766 kRAHasBeenSaved,
767 kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000768 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000769}
770
771
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000772void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000773 bool should_normalize,
774 Label* if_true,
775 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000776 // Only prepare for bailouts before splits if we're in a test
777 // context. Otherwise, we let the Visit function deal with the
778 // preparation to avoid preparing with the same AST id twice.
779 if (!context()->IsTest() || !info_->IsOptimizable()) return;
780
781 Label skip;
782 if (should_normalize) __ Branch(&skip);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000783 PrepareForBailout(expr, TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000784 if (should_normalize) {
785 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
786 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
787 __ bind(&skip);
788 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000789}
790
791
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000792void FullCodeGenerator::EmitDeclaration(VariableProxy* proxy,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000793 VariableMode mode,
yangguo@chromium.org56454712012-02-16 15:33:53 +0000794 FunctionLiteral* function) {
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.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000798 Variable* variable = proxy->var();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000799 bool binding_needs_init = (function == NULL) &&
800 (mode == CONST || mode == CONST_HARMONY || mode == LET);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000801 switch (variable->location()) {
802 case Variable::UNALLOCATED:
yangguo@chromium.org56454712012-02-16 15:33:53 +0000803 ++global_count_;
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000804 break;
805
806 case Variable::PARAMETER:
807 case Variable::LOCAL:
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000808 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000809 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000810 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000811 __ sw(result_register(), StackOperand(variable));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000812 } else if (binding_needs_init) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000813 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000814 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000815 __ sw(t0, StackOperand(variable));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000816 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000817 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000818
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000819 case Variable::CONTEXT:
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000820 // The variable in the decl always resides in the current function
821 // context.
822 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
823 if (FLAG_debug_code) {
824 // Check that we're not inside a with or catch context.
825 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
826 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
827 __ Check(ne, "Declaration in with context.",
828 a1, Operand(t0));
829 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
830 __ Check(ne, "Declaration in catch context.",
831 a1, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000832 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000833 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000834 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000835 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000836 __ sw(result_register(), ContextOperand(cp, variable->index()));
837 int offset = Context::SlotOffset(variable->index());
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000838 // We know that we have written a function, which is not a smi.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000839 __ RecordWriteContextSlot(cp,
840 offset,
841 result_register(),
842 a2,
843 kRAHasBeenSaved,
844 kDontSaveFPRegs,
845 EMIT_REMEMBERED_SET,
846 OMIT_SMI_CHECK);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000847 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000848 } else if (binding_needs_init) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000849 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000850 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000851 __ sw(at, ContextOperand(cp, variable->index()));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000852 // No write barrier since the_hole_value is in old space.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000853 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000854 }
855 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000856
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000857 case Variable::LOOKUP: {
858 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000859 __ li(a2, Operand(variable->name()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000860 // Declaration nodes are always introduced in one of four modes.
861 ASSERT(mode == VAR ||
862 mode == CONST ||
863 mode == CONST_HARMONY ||
864 mode == LET);
865 PropertyAttributes attr = (mode == CONST || mode == CONST_HARMONY)
866 ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000867 __ li(a1, Operand(Smi::FromInt(attr)));
868 // Push initial value, if any.
869 // Note: For variables we must not push an initial value (such as
870 // 'undefined') because we may have a (legal) redeclaration and we
871 // must not destroy the current value.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000872 if (function != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000873 __ Push(cp, a2, a1);
874 // Push initial value for function declaration.
875 VisitForStackValue(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000876 } else if (binding_needs_init) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000877 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
878 __ Push(cp, a2, a1, a0);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000879 } else {
880 ASSERT(Smi::FromInt(0) == 0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000881 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000882 __ Push(cp, a2, a1, a0);
883 }
884 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
885 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000886 }
887 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000888}
889
890
ager@chromium.org5c838252010-02-19 08:53:10 +0000891void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000892 // Call the runtime to declare the globals.
893 // The context is the first argument.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000894 __ li(a1, Operand(pairs));
895 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
896 __ Push(cp, a1, a0);
897 __ CallRuntime(Runtime::kDeclareGlobals, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000898 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000899}
900
901
lrn@chromium.org7516f052011-03-30 08:52:27 +0000902void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000903 Comment cmnt(masm_, "[ SwitchStatement");
904 Breakable nested_statement(this, stmt);
905 SetStatementPosition(stmt);
906
907 // Keep the switch value on the stack until a case matches.
908 VisitForStackValue(stmt->tag());
909 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
910
911 ZoneList<CaseClause*>* clauses = stmt->cases();
912 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
913
914 Label next_test; // Recycled for each test.
915 // Compile all the tests with branches to their bodies.
916 for (int i = 0; i < clauses->length(); i++) {
917 CaseClause* clause = clauses->at(i);
918 clause->body_target()->Unuse();
919
920 // The default is not a test, but remember it as final fall through.
921 if (clause->is_default()) {
922 default_clause = clause;
923 continue;
924 }
925
926 Comment cmnt(masm_, "[ Case comparison");
927 __ bind(&next_test);
928 next_test.Unuse();
929
930 // Compile the label expression.
931 VisitForAccumulatorValue(clause->label());
932 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
933
934 // Perform the comparison as if via '==='.
935 __ lw(a1, MemOperand(sp, 0)); // Switch value.
936 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
937 JumpPatchSite patch_site(masm_);
938 if (inline_smi_code) {
939 Label slow_case;
940 __ or_(a2, a1, a0);
941 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
942
943 __ Branch(&next_test, ne, a1, Operand(a0));
944 __ Drop(1); // Switch value is no longer needed.
945 __ Branch(clause->body_target());
946
947 __ bind(&slow_case);
948 }
949
950 // Record position before stub call for type feedback.
951 SetSourcePosition(clause->position());
952 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000953 CallIC(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000954 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +0000955
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000956 __ Branch(&next_test, ne, v0, Operand(zero_reg));
957 __ Drop(1); // Switch value is no longer needed.
958 __ Branch(clause->body_target());
959 }
960
961 // Discard the test value and jump to the default if present, otherwise to
962 // the end of the statement.
963 __ bind(&next_test);
964 __ Drop(1); // Switch value is no longer needed.
965 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000966 __ Branch(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000967 } else {
968 __ Branch(default_clause->body_target());
969 }
970
971 // Compile all the case bodies.
972 for (int i = 0; i < clauses->length(); i++) {
973 Comment cmnt(masm_, "[ Case body");
974 CaseClause* clause = clauses->at(i);
975 __ bind(clause->body_target());
976 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
977 VisitStatements(clause->statements());
978 }
979
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000980 __ bind(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000981 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000982}
983
984
985void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000986 Comment cmnt(masm_, "[ ForInStatement");
987 SetStatementPosition(stmt);
988
989 Label loop, exit;
990 ForIn loop_statement(this, stmt);
991 increment_loop_depth();
992
993 // Get the object to enumerate over. Both SpiderMonkey and JSC
994 // ignore null and undefined in contrast to the specification; see
995 // ECMA-262 section 12.6.4.
996 VisitForAccumulatorValue(stmt->enumerable());
997 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
998 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
999 __ Branch(&exit, eq, a0, Operand(at));
1000 Register null_value = t1;
1001 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1002 __ Branch(&exit, eq, a0, Operand(null_value));
ulan@chromium.org812308e2012-02-29 15:58:45 +00001003 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1004 __ mov(a0, v0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001005 // Convert the object to a JS object.
1006 Label convert, done_convert;
1007 __ JumpIfSmi(a0, &convert);
1008 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001009 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001010 __ bind(&convert);
1011 __ push(a0);
1012 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1013 __ mov(a0, v0);
1014 __ bind(&done_convert);
1015 __ push(a0);
1016
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001017 // Check for proxies.
1018 Label call_runtime;
1019 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1020 __ GetObjectType(a0, a1, a1);
1021 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
1022
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001023 // Check cache validity in generated code. This is a fast case for
1024 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1025 // guarantee cache validity, call the runtime system to check cache
1026 // validity or get the property names in a fixed array.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001027 __ CheckEnumCache(null_value, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001028
1029 // The enum cache is valid. Load the map of the object being
1030 // iterated over and use the cache for the iteration.
1031 Label use_cache;
1032 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
1033 __ Branch(&use_cache);
1034
1035 // Get the set of properties to enumerate.
1036 __ bind(&call_runtime);
1037 __ push(a0); // Duplicate the enumerable object on the stack.
1038 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1039
1040 // If we got a map from the runtime call, we can do a fast
1041 // modification check. Otherwise, we got a fixed array, and we have
1042 // to do a slow check.
1043 Label fixed_array;
1044 __ mov(a2, v0);
1045 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
1046 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1047 __ Branch(&fixed_array, ne, a1, Operand(at));
1048
1049 // We got a map in register v0. Get the enumeration cache from it.
1050 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001051 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001052 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
1053 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1054
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001055 // Set up the four remaining stack slots.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001056 __ push(v0); // Map.
1057 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
1058 __ li(a0, Operand(Smi::FromInt(0)));
1059 // Push enumeration cache, enumeration cache length (as smi) and zero.
1060 __ Push(a2, a1, a0);
1061 __ jmp(&loop);
1062
1063 // We got a fixed array in register v0. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001064 Label non_proxy;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001065 __ bind(&fixed_array);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001066
1067 Handle<JSGlobalPropertyCell> cell =
1068 isolate()->factory()->NewJSGlobalPropertyCell(
1069 Handle<Object>(
1070 Smi::FromInt(TypeFeedbackCells::kForInFastCaseMarker)));
1071 RecordTypeFeedbackCell(stmt->PrepareId(), cell);
1072 __ LoadHeapObject(a1, cell);
1073 __ li(a2, Operand(Smi::FromInt(TypeFeedbackCells::kForInSlowCaseMarker)));
1074 __ sw(a2, FieldMemOperand(a1, JSGlobalPropertyCell::kValueOffset));
1075
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001076 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1077 __ lw(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1078 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1079 __ GetObjectType(a2, a3, a3);
1080 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1081 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1082 __ bind(&non_proxy);
1083 __ Push(a1, v0); // Smi and array
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001084 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1085 __ li(a0, Operand(Smi::FromInt(0)));
1086 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1087
1088 // Generate code for doing the condition check.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001089 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001090 __ bind(&loop);
1091 // Load the current count to a0, load the length to a1.
1092 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1093 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001094 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001095
1096 // Get the current entry of the array into register a3.
1097 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1098 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1099 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1100 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1101 __ lw(a3, MemOperand(t0)); // Current entry.
1102
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001103 // Get the expected map from the stack or a smi in the
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001104 // permanent slow case into register a2.
1105 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1106
1107 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001108 // If not, we may have to filter the key.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001109 Label update_each;
1110 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1111 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1112 __ Branch(&update_each, eq, t0, Operand(a2));
1113
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001114 // For proxies, no filtering is done.
1115 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1116 ASSERT_EQ(Smi::FromInt(0), 0);
1117 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1118
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001119 // Convert the entry to a string or (smi) 0 if it isn't a property
1120 // any more. If the property has been removed while iterating, we
1121 // just skip it.
1122 __ push(a1); // Enumerable.
1123 __ push(a3); // Current entry.
1124 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1125 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001126 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001127
1128 // Update the 'each' property or variable from the possibly filtered
1129 // entry in register a3.
1130 __ bind(&update_each);
1131 __ mov(result_register(), a3);
1132 // Perform the assignment as if via '='.
1133 { EffectContext context(this);
ulan@chromium.org812308e2012-02-29 15:58:45 +00001134 EmitAssignment(stmt->each());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001135 }
1136
1137 // Generate code for the body of the loop.
1138 Visit(stmt->body());
1139
1140 // Generate code for the going to the next element by incrementing
1141 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001142 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001143 __ pop(a0);
1144 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1145 __ push(a0);
1146
yangguo@chromium.org56454712012-02-16 15:33:53 +00001147 EmitStackCheck(stmt, &loop);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001148 __ Branch(&loop);
1149
1150 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001151 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001152 __ Drop(5);
1153
1154 // Exit and decrement the loop depth.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001155 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001156 __ bind(&exit);
1157 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001158}
1159
1160
1161void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1162 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001163 // Use the fast case closure allocation code that allocates in new
1164 // space for nested functions that don't need literals cloning. If
1165 // we're running with the --always-opt or the --prepare-always-opt
1166 // flag, we need to use the runtime function so that the new function
1167 // we are creating here gets a chance to have its code optimized and
1168 // doesn't just get a copy of the existing unoptimized code.
1169 if (!FLAG_always_opt &&
1170 !FLAG_prepare_always_opt &&
1171 !pretenure &&
1172 scope()->is_function_scope() &&
1173 info->num_literals() == 0) {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001174 FastNewClosureStub stub(info->language_mode());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001175 __ li(a0, Operand(info));
1176 __ push(a0);
1177 __ CallStub(&stub);
1178 } else {
1179 __ li(a0, Operand(info));
1180 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1181 : Heap::kFalseValueRootIndex);
1182 __ Push(cp, a0, a1);
1183 __ CallRuntime(Runtime::kNewClosure, 3);
1184 }
1185 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001186}
1187
1188
1189void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001190 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001191 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001192}
1193
1194
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001195void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1196 TypeofState typeof_state,
1197 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001198 Register current = cp;
1199 Register next = a1;
1200 Register temp = a2;
1201
1202 Scope* s = scope();
1203 while (s != NULL) {
1204 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001205 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001206 // Check that extension is NULL.
1207 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1208 __ Branch(slow, ne, temp, Operand(zero_reg));
1209 }
1210 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001211 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001212 // Walk the rest of the chain without clobbering cp.
1213 current = next;
1214 }
1215 // If no outer scope calls eval, we do not need to check more
1216 // context extensions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001217 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001218 s = s->outer_scope();
1219 }
1220
1221 if (s->is_eval_scope()) {
1222 Label loop, fast;
1223 if (!current.is(next)) {
1224 __ Move(next, current);
1225 }
1226 __ bind(&loop);
1227 // Terminate at global context.
1228 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1229 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1230 __ Branch(&fast, eq, temp, Operand(t0));
1231 // Check that extension is NULL.
1232 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1233 __ Branch(slow, ne, temp, Operand(zero_reg));
1234 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001235 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001236 __ Branch(&loop);
1237 __ bind(&fast);
1238 }
1239
1240 __ lw(a0, GlobalObjectOperand());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001241 __ li(a2, Operand(var->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001242 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1243 ? RelocInfo::CODE_TARGET
1244 : RelocInfo::CODE_TARGET_CONTEXT;
1245 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001246 CallIC(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001247}
1248
1249
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001250MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1251 Label* slow) {
1252 ASSERT(var->IsContextSlot());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001253 Register context = cp;
1254 Register next = a3;
1255 Register temp = t0;
1256
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001257 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001258 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001259 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001260 // Check that extension is NULL.
1261 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1262 __ Branch(slow, ne, temp, Operand(zero_reg));
1263 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001264 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001265 // Walk the rest of the chain without clobbering cp.
1266 context = next;
1267 }
1268 }
1269 // Check that last extension is NULL.
1270 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1271 __ Branch(slow, ne, temp, Operand(zero_reg));
1272
1273 // This function is used only for loads, not stores, so it's safe to
1274 // return an cp-based operand (the write barrier cannot be allowed to
1275 // destroy the cp register).
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001276 return ContextOperand(context, var->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001277}
1278
1279
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001280void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1281 TypeofState typeof_state,
1282 Label* slow,
1283 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001284 // Generate fast-case code for variables that might be shadowed by
1285 // eval-introduced variables. Eval is used a lot without
1286 // introducing variables. In those cases, we do not want to
1287 // perform a runtime call for all variables in the scope
1288 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001289 if (var->mode() == DYNAMIC_GLOBAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001290 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001291 __ Branch(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001292 } else if (var->mode() == DYNAMIC_LOCAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001293 Variable* local = var->local_if_not_shadowed();
1294 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001295 if (local->mode() == CONST ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001296 local->mode() == CONST_HARMONY ||
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001297 local->mode() == LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001298 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1299 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001300 if (local->mode() == CONST) {
1301 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +00001302 __ Movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001303 } else { // LET || CONST_HARMONY
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001304 __ Branch(done, ne, at, Operand(zero_reg));
1305 __ li(a0, Operand(var->name()));
1306 __ push(a0);
1307 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1308 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001309 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001310 __ Branch(done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001311 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001312}
1313
1314
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001315void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1316 // Record position before possible IC call.
1317 SetSourcePosition(proxy->position());
1318 Variable* var = proxy->var();
1319
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001320 // Three cases: global variables, lookup variables, and all other types of
1321 // variables.
1322 switch (var->location()) {
1323 case Variable::UNALLOCATED: {
1324 Comment cmnt(masm_, "Global variable");
1325 // Use inline caching. Variable name is passed in a2 and the global
1326 // object (receiver) in a0.
1327 __ lw(a0, GlobalObjectOperand());
1328 __ li(a2, Operand(var->name()));
1329 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001330 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001331 context()->Plug(v0);
1332 break;
1333 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001334
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001335 case Variable::PARAMETER:
1336 case Variable::LOCAL:
1337 case Variable::CONTEXT: {
1338 Comment cmnt(masm_, var->IsContextSlot()
1339 ? "Context variable"
1340 : "Stack variable");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001341 if (var->binding_needs_init()) {
1342 // var->scope() may be NULL when the proxy is located in eval code and
1343 // refers to a potential outside binding. Currently those bindings are
1344 // always looked up dynamically, i.e. in that case
1345 // var->location() == LOOKUP.
1346 // always holds.
1347 ASSERT(var->scope() != NULL);
1348
1349 // Check if the binding really needs an initialization check. The check
1350 // can be skipped in the following situation: we have a LET or CONST
1351 // binding in harmony mode, both the Variable and the VariableProxy have
1352 // the same declaration scope (i.e. they are both in global code, in the
1353 // same function or in the same eval code) and the VariableProxy is in
1354 // the source physically located after the initializer of the variable.
1355 //
1356 // We cannot skip any initialization checks for CONST in non-harmony
1357 // mode because const variables may be declared but never initialized:
1358 // if (false) { const x; }; var y = x;
1359 //
1360 // The condition on the declaration scopes is a conservative check for
1361 // nested functions that access a binding and are called before the
1362 // binding is initialized:
1363 // function() { f(); let x = 1; function f() { x = 2; } }
1364 //
1365 bool skip_init_check;
1366 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1367 skip_init_check = false;
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001368 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001369 // Check that we always have valid source position.
1370 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1371 ASSERT(proxy->position() != RelocInfo::kNoPosition);
1372 skip_init_check = var->mode() != CONST &&
1373 var->initializer_position() < proxy->position();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001374 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001375
1376 if (!skip_init_check) {
1377 // Let and const need a read barrier.
1378 GetVar(v0, var);
1379 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1380 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1381 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1382 // Throw a reference error when using an uninitialized let/const
1383 // binding in harmony mode.
1384 Label done;
1385 __ Branch(&done, ne, at, Operand(zero_reg));
1386 __ li(a0, Operand(var->name()));
1387 __ push(a0);
1388 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1389 __ bind(&done);
1390 } else {
1391 // Uninitalized const bindings outside of harmony mode are unholed.
1392 ASSERT(var->mode() == CONST);
1393 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +00001394 __ Movz(v0, a0, at); // Conditional move: Undefined if TheHole.
danno@chromium.orgc612e022011-11-10 11:38:15 +00001395 }
1396 context()->Plug(v0);
1397 break;
1398 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001399 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001400 context()->Plug(var);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001401 break;
1402 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001403
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001404 case Variable::LOOKUP: {
1405 Label done, slow;
1406 // Generate code for loading from variables potentially shadowed
1407 // by eval-introduced variables.
1408 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1409 __ bind(&slow);
1410 Comment cmnt(masm_, "Lookup variable");
1411 __ li(a1, Operand(var->name()));
1412 __ Push(cp, a1); // Context and name.
1413 __ CallRuntime(Runtime::kLoadContextSlot, 2);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001414 __ bind(&done);
1415 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001416 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001417 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001418}
1419
1420
1421void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001422 Comment cmnt(masm_, "[ RegExpLiteral");
1423 Label materialized;
1424 // Registers will be used as follows:
1425 // t1 = materialized value (RegExp literal)
1426 // t0 = JS function, literals array
1427 // a3 = literal index
1428 // a2 = RegExp pattern
1429 // a1 = RegExp flags
1430 // a0 = RegExp literal clone
1431 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1432 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1433 int literal_offset =
1434 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1435 __ lw(t1, FieldMemOperand(t0, literal_offset));
1436 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1437 __ Branch(&materialized, ne, t1, Operand(at));
1438
1439 // Create regexp literal using runtime function.
1440 // Result will be in v0.
1441 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1442 __ li(a2, Operand(expr->pattern()));
1443 __ li(a1, Operand(expr->flags()));
1444 __ Push(t0, a3, a2, a1);
1445 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1446 __ mov(t1, v0);
1447
1448 __ bind(&materialized);
1449 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1450 Label allocated, runtime_allocate;
1451 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1452 __ jmp(&allocated);
1453
1454 __ bind(&runtime_allocate);
1455 __ push(t1);
1456 __ li(a0, Operand(Smi::FromInt(size)));
1457 __ push(a0);
1458 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1459 __ pop(t1);
1460
1461 __ bind(&allocated);
1462
1463 // After this, registers are used as follows:
1464 // v0: Newly allocated regexp.
1465 // t1: Materialized regexp.
1466 // a2: temp.
1467 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1468 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001469}
1470
1471
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001472void FullCodeGenerator::EmitAccessor(Expression* expression) {
1473 if (expression == NULL) {
1474 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1475 __ push(a1);
1476 } else {
1477 VisitForStackValue(expression);
1478 }
1479}
1480
1481
ager@chromium.org5c838252010-02-19 08:53:10 +00001482void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001483 Comment cmnt(masm_, "[ ObjectLiteral");
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001484 Handle<FixedArray> constant_properties = expr->constant_properties();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001485 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1486 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1487 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001488 __ li(a1, Operand(constant_properties));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001489 int flags = expr->fast_elements()
1490 ? ObjectLiteral::kFastElements
1491 : ObjectLiteral::kNoFlags;
1492 flags |= expr->has_function()
1493 ? ObjectLiteral::kHasFunction
1494 : ObjectLiteral::kNoFlags;
1495 __ li(a0, Operand(Smi::FromInt(flags)));
1496 __ Push(a3, a2, a1, a0);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001497 int properties_count = constant_properties->length() / 2;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001498 if (expr->depth() > 1) {
1499 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001500 } else if (flags != ObjectLiteral::kFastElements ||
1501 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001502 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001503 } else {
1504 FastCloneShallowObjectStub stub(properties_count);
1505 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001506 }
1507
1508 // If result_saved is true the result is on top of the stack. If
1509 // result_saved is false the result is in v0.
1510 bool result_saved = false;
1511
1512 // Mark all computed expressions that are bound to a key that
1513 // is shadowed by a later occurrence of the same key. For the
1514 // marked expressions, no store code is emitted.
1515 expr->CalculateEmitStore();
1516
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001517 AccessorTable accessor_table(isolate()->zone());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001518 for (int i = 0; i < expr->properties()->length(); i++) {
1519 ObjectLiteral::Property* property = expr->properties()->at(i);
1520 if (property->IsCompileTimeValue()) continue;
1521
1522 Literal* key = property->key();
1523 Expression* value = property->value();
1524 if (!result_saved) {
1525 __ push(v0); // Save result on stack.
1526 result_saved = true;
1527 }
1528 switch (property->kind()) {
1529 case ObjectLiteral::Property::CONSTANT:
1530 UNREACHABLE();
1531 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1532 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1533 // Fall through.
1534 case ObjectLiteral::Property::COMPUTED:
1535 if (key->handle()->IsSymbol()) {
1536 if (property->emit_store()) {
1537 VisitForAccumulatorValue(value);
1538 __ mov(a0, result_register());
1539 __ li(a2, Operand(key->handle()));
1540 __ lw(a1, MemOperand(sp));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001541 Handle<Code> ic = is_classic_mode()
1542 ? isolate()->builtins()->StoreIC_Initialize()
1543 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001544 CallIC(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001545 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1546 } else {
1547 VisitForEffect(value);
1548 }
1549 break;
1550 }
1551 // Fall through.
1552 case ObjectLiteral::Property::PROTOTYPE:
1553 // Duplicate receiver on stack.
1554 __ lw(a0, MemOperand(sp));
1555 __ push(a0);
1556 VisitForStackValue(key);
1557 VisitForStackValue(value);
1558 if (property->emit_store()) {
1559 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1560 __ push(a0);
1561 __ CallRuntime(Runtime::kSetProperty, 4);
1562 } else {
1563 __ Drop(3);
1564 }
1565 break;
1566 case ObjectLiteral::Property::GETTER:
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001567 accessor_table.lookup(key)->second->getter = value;
1568 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001569 case ObjectLiteral::Property::SETTER:
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001570 accessor_table.lookup(key)->second->setter = value;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001571 break;
1572 }
1573 }
1574
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001575 // Emit code to define accessors, using only a single call to the runtime for
1576 // each pair of corresponding getters and setters.
1577 for (AccessorTable::Iterator it = accessor_table.begin();
1578 it != accessor_table.end();
1579 ++it) {
1580 __ lw(a0, MemOperand(sp)); // Duplicate receiver.
1581 __ push(a0);
1582 VisitForStackValue(it->first);
1583 EmitAccessor(it->second->getter);
1584 EmitAccessor(it->second->setter);
1585 __ li(a0, Operand(Smi::FromInt(NONE)));
1586 __ push(a0);
1587 __ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
1588 }
1589
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001590 if (expr->has_function()) {
1591 ASSERT(result_saved);
1592 __ lw(a0, MemOperand(sp));
1593 __ push(a0);
1594 __ CallRuntime(Runtime::kToFastProperties, 1);
1595 }
1596
1597 if (result_saved) {
1598 context()->PlugTOS();
1599 } else {
1600 context()->Plug(v0);
1601 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001602}
1603
1604
1605void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001606 Comment cmnt(masm_, "[ ArrayLiteral");
1607
1608 ZoneList<Expression*>* subexprs = expr->values();
1609 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001610
1611 Handle<FixedArray> constant_elements = expr->constant_elements();
1612 ASSERT_EQ(2, constant_elements->length());
1613 ElementsKind constant_elements_kind =
1614 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001615 bool has_fast_elements = constant_elements_kind == FAST_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001616 Handle<FixedArrayBase> constant_elements_values(
1617 FixedArrayBase::cast(constant_elements->get(1)));
1618
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001619 __ mov(a0, result_register());
1620 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1621 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1622 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001623 __ li(a1, Operand(constant_elements));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001624 __ Push(a3, a2, a1);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001625 if (has_fast_elements && constant_elements_values->map() ==
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001626 isolate()->heap()->fixed_cow_array_map()) {
1627 FastCloneShallowArrayStub stub(
1628 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1629 __ CallStub(&stub);
1630 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1631 1, a1, a2);
1632 } else if (expr->depth() > 1) {
1633 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1634 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1635 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1636 } else {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001637 ASSERT(constant_elements_kind == FAST_ELEMENTS ||
1638 constant_elements_kind == FAST_SMI_ONLY_ELEMENTS ||
1639 FLAG_smi_only_arrays);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001640 FastCloneShallowArrayStub::Mode mode = has_fast_elements
1641 ? FastCloneShallowArrayStub::CLONE_ELEMENTS
1642 : FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001643 FastCloneShallowArrayStub stub(mode, length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001644 __ CallStub(&stub);
1645 }
1646
1647 bool result_saved = false; // Is the result saved to the stack?
1648
1649 // Emit code to evaluate all the non-constant subexpressions and to store
1650 // them into the newly cloned array.
1651 for (int i = 0; i < length; i++) {
1652 Expression* subexpr = subexprs->at(i);
1653 // If the subexpression is a literal or a simple materialized literal it
1654 // is already set in the cloned array.
1655 if (subexpr->AsLiteral() != NULL ||
1656 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1657 continue;
1658 }
1659
1660 if (!result_saved) {
1661 __ push(v0);
1662 result_saved = true;
1663 }
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001664
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001665 VisitForAccumulatorValue(subexpr);
1666
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001667 if (constant_elements_kind == FAST_ELEMENTS) {
1668 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1669 __ lw(t2, MemOperand(sp)); // Copy of array literal.
1670 __ lw(a1, FieldMemOperand(t2, JSObject::kElementsOffset));
1671 __ sw(result_register(), FieldMemOperand(a1, offset));
1672 // Update the write barrier for the array store.
1673 __ RecordWriteField(a1, offset, result_register(), a2,
1674 kRAHasBeenSaved, kDontSaveFPRegs,
1675 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1676 } else {
1677 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1678 __ lw(a2, FieldMemOperand(a1, JSObject::kMapOffset));
1679 __ li(a3, Operand(Smi::FromInt(i)));
1680 __ li(t0, Operand(Smi::FromInt(expr->literal_index())));
1681 __ mov(a0, result_register());
1682 StoreArrayLiteralElementStub stub;
1683 __ CallStub(&stub);
1684 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001685
1686 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1687 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001688 if (result_saved) {
1689 context()->PlugTOS();
1690 } else {
1691 context()->Plug(v0);
1692 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001693}
1694
1695
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001696void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001697 Comment cmnt(masm_, "[ Assignment");
1698 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1699 // on the left-hand side.
1700 if (!expr->target()->IsValidLeftHandSide()) {
1701 VisitForEffect(expr->target());
1702 return;
1703 }
1704
1705 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001706 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001707 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1708 LhsKind assign_type = VARIABLE;
1709 Property* property = expr->target()->AsProperty();
1710 if (property != NULL) {
1711 assign_type = (property->key()->IsPropertyName())
1712 ? NAMED_PROPERTY
1713 : KEYED_PROPERTY;
1714 }
1715
1716 // Evaluate LHS expression.
1717 switch (assign_type) {
1718 case VARIABLE:
1719 // Nothing to do here.
1720 break;
1721 case NAMED_PROPERTY:
1722 if (expr->is_compound()) {
1723 // We need the receiver both on the stack and in the accumulator.
1724 VisitForAccumulatorValue(property->obj());
1725 __ push(result_register());
1726 } else {
1727 VisitForStackValue(property->obj());
1728 }
1729 break;
1730 case KEYED_PROPERTY:
1731 // We need the key and receiver on both the stack and in v0 and a1.
1732 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001733 VisitForStackValue(property->obj());
1734 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001735 __ lw(a1, MemOperand(sp, 0));
1736 __ push(v0);
1737 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001738 VisitForStackValue(property->obj());
1739 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001740 }
1741 break;
1742 }
1743
1744 // For compound assignments we need another deoptimization point after the
1745 // variable/property load.
1746 if (expr->is_compound()) {
1747 { AccumulatorValueContext context(this);
1748 switch (assign_type) {
1749 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001750 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001751 PrepareForBailout(expr->target(), TOS_REG);
1752 break;
1753 case NAMED_PROPERTY:
1754 EmitNamedPropertyLoad(property);
1755 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1756 break;
1757 case KEYED_PROPERTY:
1758 EmitKeyedPropertyLoad(property);
1759 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1760 break;
1761 }
1762 }
1763
1764 Token::Value op = expr->binary_op();
1765 __ push(v0); // Left operand goes on the stack.
1766 VisitForAccumulatorValue(expr->value());
1767
1768 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1769 ? OVERWRITE_RIGHT
1770 : NO_OVERWRITE;
1771 SetSourcePosition(expr->position() + 1);
1772 AccumulatorValueContext context(this);
1773 if (ShouldInlineSmiCase(op)) {
1774 EmitInlineSmiBinaryOp(expr->binary_operation(),
1775 op,
1776 mode,
1777 expr->target(),
1778 expr->value());
1779 } else {
1780 EmitBinaryOp(expr->binary_operation(), op, mode);
1781 }
1782
1783 // Deoptimization point in case the binary operation may have side effects.
1784 PrepareForBailout(expr->binary_operation(), TOS_REG);
1785 } else {
1786 VisitForAccumulatorValue(expr->value());
1787 }
1788
1789 // Record source position before possible IC call.
1790 SetSourcePosition(expr->position());
1791
1792 // Store the value.
1793 switch (assign_type) {
1794 case VARIABLE:
1795 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1796 expr->op());
1797 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1798 context()->Plug(v0);
1799 break;
1800 case NAMED_PROPERTY:
1801 EmitNamedPropertyAssignment(expr);
1802 break;
1803 case KEYED_PROPERTY:
1804 EmitKeyedPropertyAssignment(expr);
1805 break;
1806 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001807}
1808
1809
ager@chromium.org5c838252010-02-19 08:53:10 +00001810void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001811 SetSourcePosition(prop->position());
1812 Literal* key = prop->key()->AsLiteral();
1813 __ mov(a0, result_register());
1814 __ li(a2, Operand(key->handle()));
1815 // Call load IC. It has arguments receiver and property name a0 and a2.
1816 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001817 CallIC(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001818}
1819
1820
1821void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001822 SetSourcePosition(prop->position());
1823 __ mov(a0, result_register());
1824 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1825 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001826 CallIC(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001827}
1828
1829
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001830void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001831 Token::Value op,
1832 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001833 Expression* left_expr,
1834 Expression* right_expr) {
1835 Label done, smi_case, stub_call;
1836
1837 Register scratch1 = a2;
1838 Register scratch2 = a3;
1839
1840 // Get the arguments.
1841 Register left = a1;
1842 Register right = a0;
1843 __ pop(left);
1844 __ mov(a0, result_register());
1845
1846 // Perform combined smi check on both operands.
1847 __ Or(scratch1, left, Operand(right));
1848 STATIC_ASSERT(kSmiTag == 0);
1849 JumpPatchSite patch_site(masm_);
1850 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1851
1852 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001853 BinaryOpStub stub(op, mode);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001854 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001855 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001856 __ jmp(&done);
1857
1858 __ bind(&smi_case);
1859 // Smi case. This code works the same way as the smi-smi case in the type
1860 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001861 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001862 switch (op) {
1863 case Token::SAR:
1864 __ Branch(&stub_call);
1865 __ GetLeastBitsFromSmi(scratch1, right, 5);
1866 __ srav(right, left, scratch1);
1867 __ And(v0, right, Operand(~kSmiTagMask));
1868 break;
1869 case Token::SHL: {
1870 __ Branch(&stub_call);
1871 __ SmiUntag(scratch1, left);
1872 __ GetLeastBitsFromSmi(scratch2, right, 5);
1873 __ sllv(scratch1, scratch1, scratch2);
1874 __ Addu(scratch2, scratch1, Operand(0x40000000));
1875 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1876 __ SmiTag(v0, scratch1);
1877 break;
1878 }
1879 case Token::SHR: {
1880 __ Branch(&stub_call);
1881 __ SmiUntag(scratch1, left);
1882 __ GetLeastBitsFromSmi(scratch2, right, 5);
1883 __ srlv(scratch1, scratch1, scratch2);
1884 __ And(scratch2, scratch1, 0xc0000000);
1885 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1886 __ SmiTag(v0, scratch1);
1887 break;
1888 }
1889 case Token::ADD:
1890 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1891 __ BranchOnOverflow(&stub_call, scratch1);
1892 break;
1893 case Token::SUB:
1894 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1895 __ BranchOnOverflow(&stub_call, scratch1);
1896 break;
1897 case Token::MUL: {
1898 __ SmiUntag(scratch1, right);
1899 __ Mult(left, scratch1);
1900 __ mflo(scratch1);
1901 __ mfhi(scratch2);
1902 __ sra(scratch1, scratch1, 31);
1903 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1904 __ mflo(v0);
1905 __ Branch(&done, ne, v0, Operand(zero_reg));
1906 __ Addu(scratch2, right, left);
1907 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1908 ASSERT(Smi::FromInt(0) == 0);
1909 __ mov(v0, zero_reg);
1910 break;
1911 }
1912 case Token::BIT_OR:
1913 __ Or(v0, left, Operand(right));
1914 break;
1915 case Token::BIT_AND:
1916 __ And(v0, left, Operand(right));
1917 break;
1918 case Token::BIT_XOR:
1919 __ Xor(v0, left, Operand(right));
1920 break;
1921 default:
1922 UNREACHABLE();
1923 }
1924
1925 __ bind(&done);
1926 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001927}
1928
1929
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001930void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1931 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001932 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001933 __ mov(a0, result_register());
1934 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001935 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001936 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001937 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001938 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001939 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001940}
1941
1942
ulan@chromium.org812308e2012-02-29 15:58:45 +00001943void FullCodeGenerator::EmitAssignment(Expression* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001944 // Invalid left-hand sides are rewritten to have a 'throw
1945 // ReferenceError' on the left-hand side.
1946 if (!expr->IsValidLeftHandSide()) {
1947 VisitForEffect(expr);
1948 return;
1949 }
1950
1951 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001952 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001953 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1954 LhsKind assign_type = VARIABLE;
1955 Property* prop = expr->AsProperty();
1956 if (prop != NULL) {
1957 assign_type = (prop->key()->IsPropertyName())
1958 ? NAMED_PROPERTY
1959 : KEYED_PROPERTY;
1960 }
1961
1962 switch (assign_type) {
1963 case VARIABLE: {
1964 Variable* var = expr->AsVariableProxy()->var();
1965 EffectContext context(this);
1966 EmitVariableAssignment(var, Token::ASSIGN);
1967 break;
1968 }
1969 case NAMED_PROPERTY: {
1970 __ push(result_register()); // Preserve value.
1971 VisitForAccumulatorValue(prop->obj());
1972 __ mov(a1, result_register());
1973 __ pop(a0); // Restore value.
1974 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001975 Handle<Code> ic = is_classic_mode()
1976 ? isolate()->builtins()->StoreIC_Initialize()
1977 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001978 CallIC(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001979 break;
1980 }
1981 case KEYED_PROPERTY: {
1982 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001983 VisitForStackValue(prop->obj());
1984 VisitForAccumulatorValue(prop->key());
1985 __ mov(a1, result_register());
1986 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001987 __ pop(a0); // Restore value.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001988 Handle<Code> ic = is_classic_mode()
1989 ? isolate()->builtins()->KeyedStoreIC_Initialize()
1990 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001991 CallIC(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001992 break;
1993 }
1994 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001995 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001996}
1997
1998
1999void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002000 Token::Value op) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002001 if (var->IsUnallocated()) {
2002 // Global var, const, or let.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002003 __ mov(a0, result_register());
2004 __ li(a2, Operand(var->name()));
2005 __ lw(a1, GlobalObjectOperand());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002006 Handle<Code> ic = is_classic_mode()
2007 ? isolate()->builtins()->StoreIC_Initialize()
2008 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002009 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002010
2011 } else if (op == Token::INIT_CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002012 // Const initializers need a write barrier.
2013 ASSERT(!var->IsParameter()); // No const parameters.
2014 if (var->IsStackLocal()) {
2015 Label skip;
2016 __ lw(a1, StackOperand(var));
2017 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2018 __ Branch(&skip, ne, a1, Operand(t0));
2019 __ sw(result_register(), StackOperand(var));
2020 __ bind(&skip);
2021 } else {
2022 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
2023 // Like var declarations, const declarations are hoisted to function
2024 // scope. However, unlike var initializers, const initializers are
2025 // able to drill a hole to that function context, even from inside a
2026 // 'with' context. We thus bypass the normal static scope lookup for
2027 // var->IsContextSlot().
2028 __ push(v0);
2029 __ li(a0, Operand(var->name()));
2030 __ Push(cp, a0); // Context and name.
2031 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002032 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002033
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002034 } else if (var->mode() == LET && op != Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002035 // Non-initializing assignment to let variable needs a write barrier.
2036 if (var->IsLookupSlot()) {
2037 __ push(v0); // Value.
2038 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002039 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002040 __ Push(cp, a1, a0); // Context, name, strict mode.
2041 __ CallRuntime(Runtime::kStoreContextSlot, 4);
2042 } else {
2043 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
2044 Label assign;
2045 MemOperand location = VarOperand(var, a1);
2046 __ lw(a3, location);
2047 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2048 __ Branch(&assign, ne, a3, Operand(t0));
2049 __ li(a3, Operand(var->name()));
2050 __ push(a3);
2051 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2052 // Perform the assignment.
2053 __ bind(&assign);
2054 __ sw(result_register(), location);
2055 if (var->IsContextSlot()) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002056 // RecordWrite may destroy all its register arguments.
2057 __ mov(a3, result_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002058 int offset = Context::SlotOffset(var->index());
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002059 __ RecordWriteContextSlot(
2060 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002061 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002062 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002063
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002064 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2065 // Assignment to var or initializing assignment to let/const
2066 // in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002067 if (var->IsStackAllocated() || var->IsContextSlot()) {
2068 MemOperand location = VarOperand(var, a1);
2069 if (FLAG_debug_code && op == Token::INIT_LET) {
2070 // Check for an uninitialized let binding.
2071 __ lw(a2, location);
2072 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2073 __ Check(eq, "Let binding re-initialization.", a2, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002074 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002075 // Perform the assignment.
2076 __ sw(v0, location);
2077 if (var->IsContextSlot()) {
2078 __ mov(a3, v0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002079 int offset = Context::SlotOffset(var->index());
2080 __ RecordWriteContextSlot(
2081 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002082 }
2083 } else {
2084 ASSERT(var->IsLookupSlot());
2085 __ push(v0); // Value.
2086 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002087 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002088 __ Push(cp, a1, a0); // Context, name, strict mode.
2089 __ CallRuntime(Runtime::kStoreContextSlot, 4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002090 }
2091 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002092 // Non-initializing assignments to consts are ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +00002093}
2094
2095
2096void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002097 // Assignment to a property, using a named store IC.
2098 Property* prop = expr->target()->AsProperty();
2099 ASSERT(prop != NULL);
2100 ASSERT(prop->key()->AsLiteral() != NULL);
2101
2102 // If the assignment starts a block of assignments to the same object,
2103 // change to slow case to avoid the quadratic behavior of repeatedly
2104 // adding fast properties.
2105 if (expr->starts_initialization_block()) {
2106 __ push(result_register());
2107 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
2108 __ push(t0);
2109 __ CallRuntime(Runtime::kToSlowProperties, 1);
2110 __ pop(result_register());
2111 }
2112
2113 // Record source code position before IC call.
2114 SetSourcePosition(expr->position());
2115 __ mov(a0, result_register()); // Load the value.
2116 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
2117 // Load receiver to a1. Leave a copy in the stack if needed for turning the
2118 // receiver into fast case.
2119 if (expr->ends_initialization_block()) {
2120 __ lw(a1, MemOperand(sp));
2121 } else {
2122 __ pop(a1);
2123 }
2124
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002125 Handle<Code> ic = is_classic_mode()
2126 ? isolate()->builtins()->StoreIC_Initialize()
2127 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002128 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002129
2130 // If the assignment ends an initialization block, revert to fast case.
2131 if (expr->ends_initialization_block()) {
2132 __ push(v0); // Result of assignment, saved even if not needed.
2133 // Receiver is under the result value.
2134 __ lw(t0, MemOperand(sp, kPointerSize));
2135 __ push(t0);
2136 __ CallRuntime(Runtime::kToFastProperties, 1);
2137 __ pop(v0);
2138 __ Drop(1);
2139 }
2140 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2141 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002142}
2143
2144
2145void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002146 // Assignment to a property, using a keyed store IC.
2147
2148 // If the assignment starts a block of assignments to the same object,
2149 // change to slow case to avoid the quadratic behavior of repeatedly
2150 // adding fast properties.
2151 if (expr->starts_initialization_block()) {
2152 __ push(result_register());
2153 // Receiver is now under the key and value.
2154 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2155 __ push(t0);
2156 __ CallRuntime(Runtime::kToSlowProperties, 1);
2157 __ pop(result_register());
2158 }
2159
2160 // Record source code position before IC call.
2161 SetSourcePosition(expr->position());
2162 // Call keyed store IC.
2163 // The arguments are:
2164 // - a0 is the value,
2165 // - a1 is the key,
2166 // - a2 is the receiver.
2167 __ mov(a0, result_register());
2168 __ pop(a1); // Key.
2169 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2170 // receiver into fast case.
2171 if (expr->ends_initialization_block()) {
2172 __ lw(a2, MemOperand(sp));
2173 } else {
2174 __ pop(a2);
2175 }
2176
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002177 Handle<Code> ic = is_classic_mode()
2178 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2179 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002180 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002181
2182 // If the assignment ends an initialization block, revert to fast case.
2183 if (expr->ends_initialization_block()) {
2184 __ push(v0); // Result of assignment, saved even if not needed.
2185 // Receiver is under the result value.
2186 __ lw(t0, MemOperand(sp, kPointerSize));
2187 __ push(t0);
2188 __ CallRuntime(Runtime::kToFastProperties, 1);
2189 __ pop(v0);
2190 __ Drop(1);
2191 }
2192 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2193 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002194}
2195
2196
2197void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002198 Comment cmnt(masm_, "[ Property");
2199 Expression* key = expr->key();
2200
2201 if (key->IsPropertyName()) {
2202 VisitForAccumulatorValue(expr->obj());
2203 EmitNamedPropertyLoad(expr);
2204 context()->Plug(v0);
2205 } else {
2206 VisitForStackValue(expr->obj());
2207 VisitForAccumulatorValue(expr->key());
2208 __ pop(a1);
2209 EmitKeyedPropertyLoad(expr);
2210 context()->Plug(v0);
2211 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002212}
2213
lrn@chromium.org7516f052011-03-30 08:52:27 +00002214
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002215void FullCodeGenerator::CallIC(Handle<Code> code,
2216 RelocInfo::Mode rmode,
2217 unsigned ast_id) {
2218 ic_total_count_++;
2219 __ Call(code, rmode, ast_id);
2220}
2221
2222
ager@chromium.org5c838252010-02-19 08:53:10 +00002223void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002224 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002225 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002226 // Code common for calls using the IC.
2227 ZoneList<Expression*>* args = expr->arguments();
2228 int arg_count = args->length();
2229 { PreservePositionScope scope(masm()->positions_recorder());
2230 for (int i = 0; i < arg_count; i++) {
2231 VisitForStackValue(args->at(i));
2232 }
2233 __ li(a2, Operand(name));
2234 }
2235 // Record source position for debugger.
2236 SetSourcePosition(expr->position());
2237 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002238 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002239 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002240 CallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002241 RecordJSReturnSite(expr);
2242 // Restore context register.
2243 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2244 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002245}
2246
2247
lrn@chromium.org7516f052011-03-30 08:52:27 +00002248void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002249 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002250 // Load the key.
2251 VisitForAccumulatorValue(key);
2252
2253 // Swap the name of the function and the receiver on the stack to follow
2254 // the calling convention for call ICs.
2255 __ pop(a1);
2256 __ push(v0);
2257 __ push(a1);
2258
2259 // Code common for calls using the IC.
2260 ZoneList<Expression*>* args = expr->arguments();
2261 int arg_count = args->length();
2262 { PreservePositionScope scope(masm()->positions_recorder());
2263 for (int i = 0; i < arg_count; i++) {
2264 VisitForStackValue(args->at(i));
2265 }
2266 }
2267 // Record source position for debugger.
2268 SetSourcePosition(expr->position());
2269 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002270 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002271 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002272 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002273 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002274 RecordJSReturnSite(expr);
2275 // Restore context register.
2276 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2277 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002278}
2279
2280
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002281void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002282 // Code common for calls using the call stub.
2283 ZoneList<Expression*>* args = expr->arguments();
2284 int arg_count = args->length();
2285 { PreservePositionScope scope(masm()->positions_recorder());
2286 for (int i = 0; i < arg_count; i++) {
2287 VisitForStackValue(args->at(i));
2288 }
2289 }
2290 // Record source position for debugger.
2291 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002292 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002293 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002294 __ CallStub(&stub);
2295 RecordJSReturnSite(expr);
2296 // Restore context register.
2297 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2298 context()->DropAndPlug(1, v0);
2299}
2300
2301
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002302void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002303 // Push copy of the first argument or undefined if it doesn't exist.
2304 if (arg_count > 0) {
2305 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2306 } else {
2307 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2308 }
2309 __ push(a1);
2310
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002311 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002312 int receiver_offset = 2 + info_->scope()->num_parameters();
2313 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002314 __ push(a1);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002315 // Push the language mode.
2316 __ li(a1, Operand(Smi::FromInt(language_mode())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002317 __ push(a1);
2318
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002319 // Push the start position of the scope the calls resides in.
2320 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
2321 __ push(a1);
2322
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002323 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002324 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org5c838252010-02-19 08:53:10 +00002325}
2326
2327
2328void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002329#ifdef DEBUG
2330 // We want to verify that RecordJSReturnSite gets called on all paths
2331 // through this function. Avoid early returns.
2332 expr->return_is_recorded_ = false;
2333#endif
2334
2335 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002336 Expression* callee = expr->expression();
2337 VariableProxy* proxy = callee->AsVariableProxy();
2338 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002339
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002340 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002341 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2342 // resolve the function we need to call and the receiver of the
2343 // call. Then we call the resolved function using the given
2344 // arguments.
2345 ZoneList<Expression*>* args = expr->arguments();
2346 int arg_count = args->length();
2347
2348 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002349 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002350 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2351 __ push(a2); // Reserved receiver slot.
2352
2353 // Push the arguments.
2354 for (int i = 0; i < arg_count; i++) {
2355 VisitForStackValue(args->at(i));
2356 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002357
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002358 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002359 // resolve eval.
2360 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2361 __ push(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002362 EmitResolvePossiblyDirectEval(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002363
2364 // The runtime call returns a pair of values in v0 (function) and
2365 // v1 (receiver). Touch up the stack with the right values.
2366 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2367 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2368 }
2369 // Record source position for debugger.
2370 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002371 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002372 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002373 __ CallStub(&stub);
2374 RecordJSReturnSite(expr);
2375 // Restore context register.
2376 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2377 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002378 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002379 // Push global object as receiver for the call IC.
2380 __ lw(a0, GlobalObjectOperand());
2381 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002382 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2383 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002384 // Call to a lookup slot (dynamically introduced variable).
2385 Label slow, done;
2386
2387 { PreservePositionScope scope(masm()->positions_recorder());
2388 // Generate code for loading from variables potentially shadowed
2389 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002390 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002391 }
2392
2393 __ bind(&slow);
2394 // Call the runtime to find the function to call (returned in v0)
2395 // and the object holding it (returned in v1).
2396 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002397 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002398 __ push(a2);
2399 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2400 __ Push(v0, v1); // Function, receiver.
2401
2402 // If fast case code has been generated, emit code to push the
2403 // function and receiver and have the slow path jump around this
2404 // code.
2405 if (done.is_linked()) {
2406 Label call;
2407 __ Branch(&call);
2408 __ bind(&done);
2409 // Push function.
2410 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002411 // The receiver is implicitly the global receiver. Indicate this
2412 // by passing the hole to the call function stub.
2413 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002414 __ push(a1);
2415 __ bind(&call);
2416 }
2417
danno@chromium.org40cb8782011-05-25 07:58:50 +00002418 // The receiver is either the global receiver or an object found
2419 // by LoadContextSlot. That object could be the hole if the
2420 // receiver is implicitly the global object.
2421 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002422 } else if (property != NULL) {
2423 { PreservePositionScope scope(masm()->positions_recorder());
2424 VisitForStackValue(property->obj());
2425 }
2426 if (property->key()->IsPropertyName()) {
2427 EmitCallWithIC(expr,
2428 property->key()->AsLiteral()->handle(),
2429 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002430 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002431 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002432 }
2433 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002434 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002435 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002436 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002437 }
2438 // Load global receiver object.
2439 __ lw(a1, GlobalObjectOperand());
2440 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2441 __ push(a1);
2442 // Emit function call.
2443 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2444 }
2445
2446#ifdef DEBUG
2447 // RecordJSReturnSite should have been called.
2448 ASSERT(expr->return_is_recorded_);
2449#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002450}
2451
2452
2453void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002454 Comment cmnt(masm_, "[ CallNew");
2455 // According to ECMA-262, section 11.2.2, page 44, the function
2456 // expression in new calls must be evaluated before the
2457 // arguments.
2458
2459 // Push constructor on the stack. If it's not a function it's used as
2460 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2461 // ignored.
2462 VisitForStackValue(expr->expression());
2463
2464 // Push the arguments ("left-to-right") on the stack.
2465 ZoneList<Expression*>* args = expr->arguments();
2466 int arg_count = args->length();
2467 for (int i = 0; i < arg_count; i++) {
2468 VisitForStackValue(args->at(i));
2469 }
2470
2471 // Call the construct call builtin that handles allocation and
2472 // constructor invocation.
2473 SetSourcePosition(expr->position());
2474
2475 // Load function and argument count into a1 and a0.
2476 __ li(a0, Operand(arg_count));
2477 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2478
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002479 // Record call targets in unoptimized code, but not in the snapshot.
2480 CallFunctionFlags flags;
2481 if (!Serializer::enabled()) {
2482 flags = RECORD_CALL_TARGET;
2483 Handle<Object> uninitialized =
2484 TypeFeedbackCells::UninitializedSentinel(isolate());
2485 Handle<JSGlobalPropertyCell> cell =
2486 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
2487 RecordTypeFeedbackCell(expr->id(), cell);
2488 __ li(a2, Operand(cell));
2489 } else {
2490 flags = NO_CALL_FUNCTION_FLAGS;
2491 }
2492
2493 CallConstructStub stub(flags);
2494 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
ulan@chromium.org967e2702012-02-28 09:49:15 +00002495 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002496 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002497}
2498
2499
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002500void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2501 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002502 ASSERT(args->length() == 1);
2503
2504 VisitForAccumulatorValue(args->at(0));
2505
2506 Label materialize_true, materialize_false;
2507 Label* if_true = NULL;
2508 Label* if_false = NULL;
2509 Label* fall_through = NULL;
2510 context()->PrepareTest(&materialize_true, &materialize_false,
2511 &if_true, &if_false, &fall_through);
2512
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002513 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002514 __ And(t0, v0, Operand(kSmiTagMask));
2515 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2516
2517 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002518}
2519
2520
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002521void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2522 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002523 ASSERT(args->length() == 1);
2524
2525 VisitForAccumulatorValue(args->at(0));
2526
2527 Label materialize_true, materialize_false;
2528 Label* if_true = NULL;
2529 Label* if_false = NULL;
2530 Label* fall_through = NULL;
2531 context()->PrepareTest(&materialize_true, &materialize_false,
2532 &if_true, &if_false, &fall_through);
2533
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002534 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002535 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2536 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2537
2538 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002539}
2540
2541
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002542void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2543 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002544 ASSERT(args->length() == 1);
2545
2546 VisitForAccumulatorValue(args->at(0));
2547
2548 Label materialize_true, materialize_false;
2549 Label* if_true = NULL;
2550 Label* if_false = NULL;
2551 Label* fall_through = NULL;
2552 context()->PrepareTest(&materialize_true, &materialize_false,
2553 &if_true, &if_false, &fall_through);
2554
2555 __ JumpIfSmi(v0, if_false);
2556 __ LoadRoot(at, Heap::kNullValueRootIndex);
2557 __ Branch(if_true, eq, v0, Operand(at));
2558 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2559 // Undetectable objects behave like undefined when tested with typeof.
2560 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2561 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2562 __ Branch(if_false, ne, at, Operand(zero_reg));
2563 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002564 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002565 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002566 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2567 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002568
2569 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002570}
2571
2572
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002573void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2574 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002575 ASSERT(args->length() == 1);
2576
2577 VisitForAccumulatorValue(args->at(0));
2578
2579 Label materialize_true, materialize_false;
2580 Label* if_true = NULL;
2581 Label* if_false = NULL;
2582 Label* fall_through = NULL;
2583 context()->PrepareTest(&materialize_true, &materialize_false,
2584 &if_true, &if_false, &fall_through);
2585
2586 __ JumpIfSmi(v0, if_false);
2587 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002588 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002589 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002590 if_true, if_false, fall_through);
2591
2592 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002593}
2594
2595
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002596void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2597 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002598 ASSERT(args->length() == 1);
2599
2600 VisitForAccumulatorValue(args->at(0));
2601
2602 Label materialize_true, materialize_false;
2603 Label* if_true = NULL;
2604 Label* if_false = NULL;
2605 Label* fall_through = NULL;
2606 context()->PrepareTest(&materialize_true, &materialize_false,
2607 &if_true, &if_false, &fall_through);
2608
2609 __ JumpIfSmi(v0, if_false);
2610 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2611 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2612 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002613 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002614 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2615
2616 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002617}
2618
2619
2620void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002621 CallRuntime* expr) {
2622 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002623 ASSERT(args->length() == 1);
2624
2625 VisitForAccumulatorValue(args->at(0));
2626
2627 Label materialize_true, materialize_false;
2628 Label* if_true = NULL;
2629 Label* if_false = NULL;
2630 Label* fall_through = NULL;
2631 context()->PrepareTest(&materialize_true, &materialize_false,
2632 &if_true, &if_false, &fall_through);
2633
2634 if (FLAG_debug_code) __ AbortIfSmi(v0);
2635
2636 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2637 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2638 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2639 __ Branch(if_true, ne, t0, Operand(zero_reg));
2640
2641 // Check for fast case object. Generate false result for slow case object.
2642 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2643 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2644 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2645 __ Branch(if_false, eq, a2, Operand(t0));
2646
2647 // Look for valueOf symbol in the descriptor array, and indicate false if
2648 // found. The type is not checked, so if it is a transition it is a false
2649 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002650 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002651 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2652 // t0: descriptor array
2653 // a3: length of descriptor array
2654 // Calculate the end of the descriptor array.
2655 STATIC_ASSERT(kSmiTag == 0);
2656 STATIC_ASSERT(kSmiTagSize == 1);
2657 STATIC_ASSERT(kPointerSize == 4);
2658 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2659 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2660 __ Addu(a2, a2, t1);
2661
2662 // Calculate location of the first key name.
2663 __ Addu(t0,
2664 t0,
2665 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2666 DescriptorArray::kFirstIndex * kPointerSize));
2667 // Loop through all the keys in the descriptor array. If one of these is the
2668 // symbol valueOf the result is false.
2669 Label entry, loop;
2670 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2671 // used in the loop below.
danno@chromium.org88aa0582012-03-23 15:11:57 +00002672 __ LoadRoot(t2, Heap::kvalue_of_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002673 __ jmp(&entry);
2674 __ bind(&loop);
2675 __ lw(a3, MemOperand(t0, 0));
2676 __ Branch(if_false, eq, a3, Operand(t2));
2677 __ Addu(t0, t0, Operand(kPointerSize));
2678 __ bind(&entry);
2679 __ Branch(&loop, ne, t0, Operand(a2));
2680
2681 // If a valueOf property is not found on the object check that it's
2682 // prototype is the un-modified String prototype. If not result is false.
2683 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2684 __ JumpIfSmi(a2, if_false);
2685 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2686 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2687 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2688 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2689 __ Branch(if_false, ne, a2, Operand(a3));
2690
2691 // Set the bit in the map to indicate that it has been checked safe for
2692 // default valueOf and set true result.
2693 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2694 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2695 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2696 __ jmp(if_true);
2697
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002698 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002699 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002700}
2701
2702
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002703void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2704 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002705 ASSERT(args->length() == 1);
2706
2707 VisitForAccumulatorValue(args->at(0));
2708
2709 Label materialize_true, materialize_false;
2710 Label* if_true = NULL;
2711 Label* if_false = NULL;
2712 Label* fall_through = NULL;
2713 context()->PrepareTest(&materialize_true, &materialize_false,
2714 &if_true, &if_false, &fall_through);
2715
2716 __ JumpIfSmi(v0, if_false);
2717 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002718 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002719 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2720 __ Branch(if_false);
2721
2722 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002723}
2724
2725
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002726void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2727 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002728 ASSERT(args->length() == 1);
2729
2730 VisitForAccumulatorValue(args->at(0));
2731
2732 Label materialize_true, materialize_false;
2733 Label* if_true = NULL;
2734 Label* if_false = NULL;
2735 Label* fall_through = NULL;
2736 context()->PrepareTest(&materialize_true, &materialize_false,
2737 &if_true, &if_false, &fall_through);
2738
2739 __ JumpIfSmi(v0, if_false);
2740 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002741 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002742 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2743 if_true, if_false, fall_through);
2744
2745 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002746}
2747
2748
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002749void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2750 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002751 ASSERT(args->length() == 1);
2752
2753 VisitForAccumulatorValue(args->at(0));
2754
2755 Label materialize_true, materialize_false;
2756 Label* if_true = NULL;
2757 Label* if_false = NULL;
2758 Label* fall_through = NULL;
2759 context()->PrepareTest(&materialize_true, &materialize_false,
2760 &if_true, &if_false, &fall_through);
2761
2762 __ JumpIfSmi(v0, if_false);
2763 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002764 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002765 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2766
2767 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002768}
2769
2770
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002771void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2772 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002773
2774 Label materialize_true, materialize_false;
2775 Label* if_true = NULL;
2776 Label* if_false = NULL;
2777 Label* fall_through = NULL;
2778 context()->PrepareTest(&materialize_true, &materialize_false,
2779 &if_true, &if_false, &fall_through);
2780
2781 // Get the frame pointer for the calling frame.
2782 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2783
2784 // Skip the arguments adaptor frame if it exists.
2785 Label check_frame_marker;
2786 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2787 __ Branch(&check_frame_marker, ne,
2788 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2789 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2790
2791 // Check the marker in the calling frame.
2792 __ bind(&check_frame_marker);
2793 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002794 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002795 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2796 if_true, if_false, fall_through);
2797
2798 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002799}
2800
2801
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002802void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2803 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002804 ASSERT(args->length() == 2);
2805
2806 // Load the two objects into registers and perform the comparison.
2807 VisitForStackValue(args->at(0));
2808 VisitForAccumulatorValue(args->at(1));
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 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002818 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002819 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2820
2821 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002822}
2823
2824
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002825void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2826 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002827 ASSERT(args->length() == 1);
2828
2829 // ArgumentsAccessStub expects the key in a1 and the formal
2830 // parameter count in a0.
2831 VisitForAccumulatorValue(args->at(0));
2832 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002833 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002834 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2835 __ CallStub(&stub);
2836 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002837}
2838
2839
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002840void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2841 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002842 Label exit;
2843 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002844 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002845
2846 // Check if the calling frame is an arguments adaptor frame.
2847 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2848 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2849 __ Branch(&exit, ne, a3,
2850 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2851
2852 // Arguments adaptor case: Read the arguments length from the
2853 // adaptor frame.
2854 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2855
2856 __ bind(&exit);
2857 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002858}
2859
2860
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002861void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2862 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002863 ASSERT(args->length() == 1);
2864 Label done, null, function, non_function_constructor;
2865
2866 VisitForAccumulatorValue(args->at(0));
2867
2868 // If the object is a smi, we return null.
2869 __ JumpIfSmi(v0, &null);
2870
2871 // Check that the object is a JS object but take special care of JS
2872 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002873 // Assume that there are only two callable types, and one of them is at
2874 // either end of the type range for JS object types. Saves extra comparisons.
2875 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002876 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002877 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002878
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002879 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2880 FIRST_SPEC_OBJECT_TYPE + 1);
2881 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002882
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002883 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2884 LAST_SPEC_OBJECT_TYPE - 1);
2885 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2886 // Assume that there is no larger type.
2887 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2888
2889 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002890 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2891 __ GetObjectType(v0, a1, a1);
2892 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2893
2894 // v0 now contains the constructor function. Grab the
2895 // instance class name from there.
2896 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2897 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2898 __ Branch(&done);
2899
2900 // Functions have class 'Function'.
2901 __ bind(&function);
2902 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2903 __ jmp(&done);
2904
2905 // Objects with a non-function constructor have class 'Object'.
2906 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002907 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002908 __ jmp(&done);
2909
2910 // Non-JS objects have class null.
2911 __ bind(&null);
2912 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2913
2914 // All done.
2915 __ bind(&done);
2916
2917 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002918}
2919
2920
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002921void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002922 // Conditionally generate a log call.
2923 // Args:
2924 // 0 (literal string): The type of logging (corresponds to the flags).
2925 // This is used to determine whether or not to generate the log call.
2926 // 1 (string): Format string. Access the string at argument index 2
2927 // with '%2s' (see Logger::LogRuntime for all the formats).
2928 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002929 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002930 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002931 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2932 VisitForStackValue(args->at(1));
2933 VisitForStackValue(args->at(2));
2934 __ CallRuntime(Runtime::kLog, 2);
2935 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002936
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002937 // Finally, we're expected to leave a value on the top of the stack.
2938 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2939 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002940}
2941
2942
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002943void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
2944 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002945 Label slow_allocate_heapnumber;
2946 Label heapnumber_allocated;
2947
2948 // Save the new heap number in callee-saved register s0, since
2949 // we call out to external C code below.
2950 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2951 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2952 __ jmp(&heapnumber_allocated);
2953
2954 __ bind(&slow_allocate_heapnumber);
2955
2956 // Allocate a heap number.
2957 __ CallRuntime(Runtime::kNumberAlloc, 0);
2958 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2959
2960 __ bind(&heapnumber_allocated);
2961
2962 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2963 // by computing:
2964 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2965 if (CpuFeatures::IsSupported(FPU)) {
2966 __ PrepareCallCFunction(1, a0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002967 __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
2968 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002969 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2970
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002971 CpuFeatures::Scope scope(FPU);
2972 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2973 __ li(a1, Operand(0x41300000));
2974 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002975 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002976 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002977 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002978 // Subtract and store the result in the heap number.
2979 __ sub_d(f0, f12, f14);
2980 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2981 __ mov(v0, s0);
2982 } else {
2983 __ PrepareCallCFunction(2, a0);
2984 __ mov(a0, s0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002985 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
2986 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002987 __ CallCFunction(
2988 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2989 }
2990
2991 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002992}
2993
2994
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002995void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002996 // Load the arguments on the stack and call the stub.
2997 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002998 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002999 ASSERT(args->length() == 3);
3000 VisitForStackValue(args->at(0));
3001 VisitForStackValue(args->at(1));
3002 VisitForStackValue(args->at(2));
3003 __ CallStub(&stub);
3004 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003005}
3006
3007
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003008void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003009 // Load the arguments on the stack and call the stub.
3010 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003011 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003012 ASSERT(args->length() == 4);
3013 VisitForStackValue(args->at(0));
3014 VisitForStackValue(args->at(1));
3015 VisitForStackValue(args->at(2));
3016 VisitForStackValue(args->at(3));
3017 __ CallStub(&stub);
3018 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003019}
3020
3021
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003022void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3023 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003024 ASSERT(args->length() == 1);
3025
3026 VisitForAccumulatorValue(args->at(0)); // Load the object.
3027
3028 Label done;
3029 // If the object is a smi return the object.
3030 __ JumpIfSmi(v0, &done);
3031 // If the object is not a value type, return the object.
3032 __ GetObjectType(v0, a1, a1);
3033 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
3034
3035 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
3036
3037 __ bind(&done);
3038 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003039}
3040
3041
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003042void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3043 ZoneList<Expression*>* args = expr->arguments();
3044 ASSERT(args->length() == 2);
3045 ASSERT_NE(NULL, args->at(1)->AsLiteral());
3046 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->handle()));
3047
3048 VisitForAccumulatorValue(args->at(0)); // Load the object.
3049
3050 Label runtime, done;
3051 Register object = v0;
3052 Register result = v0;
3053 Register scratch0 = t5;
3054 Register scratch1 = a1;
3055
3056#ifdef DEBUG
3057 __ AbortIfSmi(object);
3058 __ GetObjectType(object, scratch1, scratch1);
3059 __ Assert(eq, "Trying to get date field from non-date.",
3060 scratch1, Operand(JS_DATE_TYPE));
3061#endif
3062
3063 if (index->value() == 0) {
3064 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset));
3065 } else {
3066 if (index->value() < JSDate::kFirstUncachedField) {
3067 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3068 __ li(scratch1, Operand(stamp));
3069 __ lw(scratch1, MemOperand(scratch1));
3070 __ lw(scratch0, FieldMemOperand(object, JSDate::kCacheStampOffset));
3071 __ Branch(&runtime, ne, scratch1, Operand(scratch0));
3072 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset +
3073 kPointerSize * index->value()));
3074 __ jmp(&done);
3075 }
3076 __ bind(&runtime);
3077 __ PrepareCallCFunction(2, scratch1);
3078 __ li(a1, Operand(index));
3079 __ Move(a0, object);
3080 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
3081 __ bind(&done);
3082 }
3083
3084 context()->Plug(v0);
3085}
3086
3087
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003088void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003089 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003090 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003091 ASSERT(args->length() == 2);
3092 VisitForStackValue(args->at(0));
3093 VisitForStackValue(args->at(1));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003094 if (CpuFeatures::IsSupported(FPU)) {
3095 MathPowStub stub(MathPowStub::ON_STACK);
3096 __ CallStub(&stub);
3097 } else {
3098 __ CallRuntime(Runtime::kMath_pow, 2);
3099 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003100 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003101}
3102
3103
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003104void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3105 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003106 ASSERT(args->length() == 2);
3107
3108 VisitForStackValue(args->at(0)); // Load the object.
3109 VisitForAccumulatorValue(args->at(1)); // Load the value.
3110 __ pop(a1); // v0 = value. a1 = object.
3111
3112 Label done;
3113 // If the object is a smi, return the value.
3114 __ JumpIfSmi(a1, &done);
3115
3116 // If the object is not a value type, return the value.
3117 __ GetObjectType(a1, a2, a2);
3118 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
3119
3120 // Store the value.
3121 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
3122 // Update the write barrier. Save the value as it will be
3123 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003124 __ mov(a2, v0);
3125 __ RecordWriteField(
3126 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003127
3128 __ bind(&done);
3129 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003130}
3131
3132
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003133void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3134 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003135 ASSERT_EQ(args->length(), 1);
3136
3137 // Load the argument on the stack and call the stub.
3138 VisitForStackValue(args->at(0));
3139
3140 NumberToStringStub stub;
3141 __ CallStub(&stub);
3142 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003143}
3144
3145
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003146void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3147 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003148 ASSERT(args->length() == 1);
3149
3150 VisitForAccumulatorValue(args->at(0));
3151
3152 Label done;
3153 StringCharFromCodeGenerator generator(v0, a1);
3154 generator.GenerateFast(masm_);
3155 __ jmp(&done);
3156
3157 NopRuntimeCallHelper call_helper;
3158 generator.GenerateSlow(masm_, call_helper);
3159
3160 __ bind(&done);
3161 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003162}
3163
3164
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003165void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3166 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003167 ASSERT(args->length() == 2);
3168
3169 VisitForStackValue(args->at(0));
3170 VisitForAccumulatorValue(args->at(1));
3171 __ mov(a0, result_register());
3172
3173 Register object = a1;
3174 Register index = a0;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003175 Register result = v0;
3176
3177 __ pop(object);
3178
3179 Label need_conversion;
3180 Label index_out_of_range;
3181 Label done;
3182 StringCharCodeAtGenerator generator(object,
3183 index,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003184 result,
3185 &need_conversion,
3186 &need_conversion,
3187 &index_out_of_range,
3188 STRING_INDEX_IS_NUMBER);
3189 generator.GenerateFast(masm_);
3190 __ jmp(&done);
3191
3192 __ bind(&index_out_of_range);
3193 // When the index is out of range, the spec requires us to return
3194 // NaN.
3195 __ LoadRoot(result, Heap::kNanValueRootIndex);
3196 __ jmp(&done);
3197
3198 __ bind(&need_conversion);
3199 // Load the undefined value into the result register, which will
3200 // trigger conversion.
3201 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3202 __ jmp(&done);
3203
3204 NopRuntimeCallHelper call_helper;
3205 generator.GenerateSlow(masm_, call_helper);
3206
3207 __ bind(&done);
3208 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003209}
3210
3211
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003212void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3213 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003214 ASSERT(args->length() == 2);
3215
3216 VisitForStackValue(args->at(0));
3217 VisitForAccumulatorValue(args->at(1));
3218 __ mov(a0, result_register());
3219
3220 Register object = a1;
3221 Register index = a0;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003222 Register scratch = a3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003223 Register result = v0;
3224
3225 __ pop(object);
3226
3227 Label need_conversion;
3228 Label index_out_of_range;
3229 Label done;
3230 StringCharAtGenerator generator(object,
3231 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003232 scratch,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003233 result,
3234 &need_conversion,
3235 &need_conversion,
3236 &index_out_of_range,
3237 STRING_INDEX_IS_NUMBER);
3238 generator.GenerateFast(masm_);
3239 __ jmp(&done);
3240
3241 __ bind(&index_out_of_range);
3242 // When the index is out of range, the spec requires us to return
3243 // the empty string.
3244 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3245 __ jmp(&done);
3246
3247 __ bind(&need_conversion);
3248 // Move smi zero into the result register, which will trigger
3249 // conversion.
3250 __ li(result, Operand(Smi::FromInt(0)));
3251 __ jmp(&done);
3252
3253 NopRuntimeCallHelper call_helper;
3254 generator.GenerateSlow(masm_, call_helper);
3255
3256 __ bind(&done);
3257 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003258}
3259
3260
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003261void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3262 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003263 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003264 VisitForStackValue(args->at(0));
3265 VisitForStackValue(args->at(1));
3266
3267 StringAddStub stub(NO_STRING_ADD_FLAGS);
3268 __ CallStub(&stub);
3269 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003270}
3271
3272
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003273void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3274 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003275 ASSERT_EQ(2, args->length());
3276
3277 VisitForStackValue(args->at(0));
3278 VisitForStackValue(args->at(1));
3279
3280 StringCompareStub stub;
3281 __ CallStub(&stub);
3282 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003283}
3284
3285
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003286void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003287 // Load the argument on the stack and call the stub.
3288 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3289 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003290 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003291 ASSERT(args->length() == 1);
3292 VisitForStackValue(args->at(0));
3293 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3294 __ CallStub(&stub);
3295 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003296}
3297
3298
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003299void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003300 // Load the argument on the stack and call the stub.
3301 TranscendentalCacheStub stub(TranscendentalCache::COS,
3302 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003303 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003304 ASSERT(args->length() == 1);
3305 VisitForStackValue(args->at(0));
3306 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3307 __ CallStub(&stub);
3308 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003309}
3310
3311
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003312void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3313 // Load the argument on the stack and call the stub.
3314 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3315 TranscendentalCacheStub::TAGGED);
3316 ZoneList<Expression*>* args = expr->arguments();
3317 ASSERT(args->length() == 1);
3318 VisitForStackValue(args->at(0));
3319 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3320 __ CallStub(&stub);
3321 context()->Plug(v0);
3322}
3323
3324
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003325void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003326 // Load the argument on the stack and call the stub.
3327 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3328 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003329 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003330 ASSERT(args->length() == 1);
3331 VisitForStackValue(args->at(0));
3332 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3333 __ CallStub(&stub);
3334 context()->Plug(v0);
3335}
3336
3337
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003338void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003339 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003340 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003341 ASSERT(args->length() == 1);
3342 VisitForStackValue(args->at(0));
3343 __ CallRuntime(Runtime::kMath_sqrt, 1);
3344 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003345}
3346
3347
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003348void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3349 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003350 ASSERT(args->length() >= 2);
3351
3352 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3353 for (int i = 0; i < arg_count + 1; i++) {
3354 VisitForStackValue(args->at(i));
3355 }
3356 VisitForAccumulatorValue(args->last()); // Function.
3357
danno@chromium.orgc612e022011-11-10 11:38:15 +00003358 // Check for proxy.
3359 Label proxy, done;
3360 __ GetObjectType(v0, a1, a1);
3361 __ Branch(&proxy, eq, a1, Operand(JS_FUNCTION_PROXY_TYPE));
3362
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003363 // InvokeFunction requires the function in a1. Move it in there.
3364 __ mov(a1, result_register());
3365 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003366 __ InvokeFunction(a1, count, CALL_FUNCTION,
3367 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003368 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003369 __ jmp(&done);
3370
3371 __ bind(&proxy);
3372 __ push(v0);
3373 __ CallRuntime(Runtime::kCall, args->length());
3374 __ bind(&done);
3375
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003376 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003377}
3378
3379
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003380void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003381 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003382 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003383 ASSERT(args->length() == 3);
3384 VisitForStackValue(args->at(0));
3385 VisitForStackValue(args->at(1));
3386 VisitForStackValue(args->at(2));
3387 __ CallStub(&stub);
3388 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003389}
3390
3391
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003392void FullCodeGenerator::EmitSwapElements(CallRuntime* expr) {
3393 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003394 ASSERT(args->length() == 3);
3395 VisitForStackValue(args->at(0));
3396 VisitForStackValue(args->at(1));
3397 VisitForStackValue(args->at(2));
3398 Label done;
3399 Label slow_case;
3400 Register object = a0;
3401 Register index1 = a1;
3402 Register index2 = a2;
3403 Register elements = a3;
3404 Register scratch1 = t0;
3405 Register scratch2 = t1;
3406
3407 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3408 // Fetch the map and check if array is in fast case.
3409 // Check that object doesn't require security checks and
3410 // has no indexed interceptor.
3411 __ GetObjectType(object, scratch1, scratch2);
3412 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3413 // Map is now in scratch1.
3414
3415 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3416 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3417 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3418
3419 // Check the object's elements are in fast case and writable.
3420 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3421 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3422 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3423 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3424
3425 // Check that both indices are smis.
3426 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3427 __ lw(index2, MemOperand(sp, 0));
3428 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3429
3430 // Check that both indices are valid.
3431 Label not_hi;
3432 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3433 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3434 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3435 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3436 __ bind(&not_hi);
3437
3438 // Bring the address of the elements into index1 and index2.
3439 __ Addu(scratch1, elements,
3440 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3441 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3442 __ Addu(index1, scratch1, index1);
3443 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3444 __ Addu(index2, scratch1, index2);
3445
3446 // Swap elements.
3447 __ lw(scratch1, MemOperand(index1, 0));
3448 __ lw(scratch2, MemOperand(index2, 0));
3449 __ sw(scratch1, MemOperand(index2, 0));
3450 __ sw(scratch2, MemOperand(index1, 0));
3451
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003452 Label no_remembered_set;
3453 __ CheckPageFlag(elements,
3454 scratch1,
3455 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3456 ne,
3457 &no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003458 // Possible optimization: do a check that both values are Smis
3459 // (or them and test against Smi mask).
3460
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003461 // We are swapping two objects in an array and the incremental marker never
3462 // pauses in the middle of scanning a single object. Therefore the
3463 // incremental marker is not disturbed, so we don't need to call the
3464 // RecordWrite stub that notifies the incremental marker.
3465 __ RememberedSetHelper(elements,
3466 index1,
3467 scratch2,
3468 kDontSaveFPRegs,
3469 MacroAssembler::kFallThroughAtEnd);
3470 __ RememberedSetHelper(elements,
3471 index2,
3472 scratch2,
3473 kDontSaveFPRegs,
3474 MacroAssembler::kFallThroughAtEnd);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003475
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003476 __ bind(&no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003477 // We are done. Drop elements from the stack, and return undefined.
3478 __ Drop(3);
3479 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3480 __ jmp(&done);
3481
3482 __ bind(&slow_case);
3483 __ CallRuntime(Runtime::kSwapElements, 3);
3484
3485 __ bind(&done);
3486 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003487}
3488
3489
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003490void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3491 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003492 ASSERT_EQ(2, args->length());
3493
3494 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3495 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3496
3497 Handle<FixedArray> jsfunction_result_caches(
3498 isolate()->global_context()->jsfunction_result_caches());
3499 if (jsfunction_result_caches->length() <= cache_id) {
3500 __ Abort("Attempt to use undefined cache.");
3501 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3502 context()->Plug(v0);
3503 return;
3504 }
3505
3506 VisitForAccumulatorValue(args->at(1));
3507
3508 Register key = v0;
3509 Register cache = a1;
3510 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3511 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3512 __ lw(cache,
3513 ContextOperand(
3514 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3515 __ lw(cache,
3516 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3517
3518
3519 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003520 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003521 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3522 // a2 now holds finger offset as a smi.
3523 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3524 // a3 now points to the start of fixed array elements.
3525 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3526 __ addu(a3, a3, at);
3527 // a3 now points to key of indexed element of cache.
3528 __ lw(a2, MemOperand(a3));
3529 __ Branch(&not_found, ne, key, Operand(a2));
3530
3531 __ lw(v0, MemOperand(a3, kPointerSize));
3532 __ Branch(&done);
3533
3534 __ bind(&not_found);
3535 // Call runtime to perform the lookup.
3536 __ Push(cache, key);
3537 __ CallRuntime(Runtime::kGetFromCache, 2);
3538
3539 __ bind(&done);
3540 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003541}
3542
3543
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003544void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3545 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003546 ASSERT_EQ(2, args->length());
3547
3548 Register right = v0;
3549 Register left = a1;
3550 Register tmp = a2;
3551 Register tmp2 = a3;
3552
3553 VisitForStackValue(args->at(0));
3554 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3555 __ pop(left);
3556
3557 Label done, fail, ok;
3558 __ Branch(&ok, eq, left, Operand(right));
3559 // Fail if either is a non-HeapObject.
3560 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003561 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003562 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3563 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3564 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3565 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3566 __ Branch(&fail, ne, tmp, Operand(tmp2));
3567 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3568 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3569 __ Branch(&ok, eq, tmp, Operand(tmp2));
3570 __ bind(&fail);
3571 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3572 __ jmp(&done);
3573 __ bind(&ok);
3574 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3575 __ bind(&done);
3576
3577 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003578}
3579
3580
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003581void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3582 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003583 VisitForAccumulatorValue(args->at(0));
3584
3585 Label materialize_true, materialize_false;
3586 Label* if_true = NULL;
3587 Label* if_false = NULL;
3588 Label* fall_through = NULL;
3589 context()->PrepareTest(&materialize_true, &materialize_false,
3590 &if_true, &if_false, &fall_through);
3591
3592 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3593 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3594
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003595 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003596 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3597
3598 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003599}
3600
3601
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003602void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3603 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003604 ASSERT(args->length() == 1);
3605 VisitForAccumulatorValue(args->at(0));
3606
3607 if (FLAG_debug_code) {
3608 __ AbortIfNotString(v0);
3609 }
3610
3611 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3612 __ IndexFromHash(v0, v0);
3613
3614 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003615}
3616
3617
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003618void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003619 Label bailout, done, one_char_separator, long_separator,
3620 non_trivial_array, not_size_one_array, loop,
3621 empty_separator_loop, one_char_separator_loop,
3622 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003623 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003624 ASSERT(args->length() == 2);
3625 VisitForStackValue(args->at(1));
3626 VisitForAccumulatorValue(args->at(0));
3627
3628 // All aliases of the same register have disjoint lifetimes.
3629 Register array = v0;
3630 Register elements = no_reg; // Will be v0.
3631 Register result = no_reg; // Will be v0.
3632 Register separator = a1;
3633 Register array_length = a2;
3634 Register result_pos = no_reg; // Will be a2.
3635 Register string_length = a3;
3636 Register string = t0;
3637 Register element = t1;
3638 Register elements_end = t2;
3639 Register scratch1 = t3;
3640 Register scratch2 = t5;
3641 Register scratch3 = t4;
3642 Register scratch4 = v1;
3643
3644 // Separator operand is on the stack.
3645 __ pop(separator);
3646
3647 // Check that the array is a JSArray.
3648 __ JumpIfSmi(array, &bailout);
3649 __ GetObjectType(array, scratch1, scratch2);
3650 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3651
3652 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003653 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003654
3655 // If the array has length zero, return the empty string.
3656 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3657 __ SmiUntag(array_length);
3658 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3659 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3660 __ Branch(&done);
3661
3662 __ bind(&non_trivial_array);
3663
3664 // Get the FixedArray containing array's elements.
3665 elements = array;
3666 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3667 array = no_reg; // End of array's live range.
3668
3669 // Check that all array elements are sequential ASCII strings, and
3670 // accumulate the sum of their lengths, as a smi-encoded value.
3671 __ mov(string_length, zero_reg);
3672 __ Addu(element,
3673 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3674 __ sll(elements_end, array_length, kPointerSizeLog2);
3675 __ Addu(elements_end, element, elements_end);
3676 // Loop condition: while (element < elements_end).
3677 // Live values in registers:
3678 // elements: Fixed array of strings.
3679 // array_length: Length of the fixed array of strings (not smi)
3680 // separator: Separator string
3681 // string_length: Accumulated sum of string lengths (smi).
3682 // element: Current array element.
3683 // elements_end: Array end.
3684 if (FLAG_debug_code) {
3685 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3686 array_length, Operand(zero_reg));
3687 }
3688 __ bind(&loop);
3689 __ lw(string, MemOperand(element));
3690 __ Addu(element, element, kPointerSize);
3691 __ JumpIfSmi(string, &bailout);
3692 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3693 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3694 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3695 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3696 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3697 __ BranchOnOverflow(&bailout, scratch3);
3698 __ Branch(&loop, lt, element, Operand(elements_end));
3699
3700 // If array_length is 1, return elements[0], a string.
3701 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3702 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3703 __ Branch(&done);
3704
3705 __ bind(&not_size_one_array);
3706
3707 // Live values in registers:
3708 // separator: Separator string
3709 // array_length: Length of the array.
3710 // string_length: Sum of string lengths (smi).
3711 // elements: FixedArray of strings.
3712
3713 // Check that the separator is a flat ASCII string.
3714 __ JumpIfSmi(separator, &bailout);
3715 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3716 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3717 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3718
3719 // Add (separator length times array_length) - separator length to the
3720 // string_length to get the length of the result string. array_length is not
3721 // smi but the other values are, so the result is a smi.
3722 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3723 __ Subu(string_length, string_length, Operand(scratch1));
3724 __ Mult(array_length, scratch1);
3725 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3726 // zero.
3727 __ mfhi(scratch2);
3728 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3729 __ mflo(scratch2);
3730 __ And(scratch3, scratch2, Operand(0x80000000));
3731 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3732 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3733 __ BranchOnOverflow(&bailout, scratch3);
3734 __ SmiUntag(string_length);
3735
3736 // Get first element in the array to free up the elements register to be used
3737 // for the result.
3738 __ Addu(element,
3739 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3740 result = elements; // End of live range for elements.
3741 elements = no_reg;
3742 // Live values in registers:
3743 // element: First array element
3744 // separator: Separator string
3745 // string_length: Length of result string (not smi)
3746 // array_length: Length of the array.
3747 __ AllocateAsciiString(result,
3748 string_length,
3749 scratch1,
3750 scratch2,
3751 elements_end,
3752 &bailout);
3753 // Prepare for looping. Set up elements_end to end of the array. Set
3754 // result_pos to the position of the result where to write the first
3755 // character.
3756 __ sll(elements_end, array_length, kPointerSizeLog2);
3757 __ Addu(elements_end, element, elements_end);
3758 result_pos = array_length; // End of live range for array_length.
3759 array_length = no_reg;
3760 __ Addu(result_pos,
3761 result,
3762 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3763
3764 // Check the length of the separator.
3765 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3766 __ li(at, Operand(Smi::FromInt(1)));
3767 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3768 __ Branch(&long_separator, gt, scratch1, Operand(at));
3769
3770 // Empty separator case.
3771 __ bind(&empty_separator_loop);
3772 // Live values in registers:
3773 // result_pos: the position to which we are currently copying characters.
3774 // element: Current array element.
3775 // elements_end: Array end.
3776
3777 // Copy next array element to the result.
3778 __ lw(string, MemOperand(element));
3779 __ Addu(element, element, kPointerSize);
3780 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3781 __ SmiUntag(string_length);
3782 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3783 __ CopyBytes(string, result_pos, string_length, scratch1);
3784 // End while (element < elements_end).
3785 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3786 ASSERT(result.is(v0));
3787 __ Branch(&done);
3788
3789 // One-character separator case.
3790 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003791 // Replace separator with its ASCII character value.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003792 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3793 // Jump into the loop after the code that copies the separator, so the first
3794 // element is not preceded by a separator.
3795 __ jmp(&one_char_separator_loop_entry);
3796
3797 __ bind(&one_char_separator_loop);
3798 // Live values in registers:
3799 // result_pos: the position to which we are currently copying characters.
3800 // element: Current array element.
3801 // elements_end: Array end.
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003802 // separator: Single separator ASCII char (in lower byte).
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003803
3804 // Copy the separator character to the result.
3805 __ sb(separator, MemOperand(result_pos));
3806 __ Addu(result_pos, result_pos, 1);
3807
3808 // Copy next array element to the result.
3809 __ bind(&one_char_separator_loop_entry);
3810 __ lw(string, MemOperand(element));
3811 __ Addu(element, element, kPointerSize);
3812 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3813 __ SmiUntag(string_length);
3814 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3815 __ CopyBytes(string, result_pos, string_length, scratch1);
3816 // End while (element < elements_end).
3817 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3818 ASSERT(result.is(v0));
3819 __ Branch(&done);
3820
3821 // Long separator case (separator is more than one character). Entry is at the
3822 // label long_separator below.
3823 __ bind(&long_separator_loop);
3824 // Live values in registers:
3825 // result_pos: the position to which we are currently copying characters.
3826 // element: Current array element.
3827 // elements_end: Array end.
3828 // separator: Separator string.
3829
3830 // Copy the separator to the result.
3831 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3832 __ SmiUntag(string_length);
3833 __ Addu(string,
3834 separator,
3835 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3836 __ CopyBytes(string, result_pos, string_length, scratch1);
3837
3838 __ bind(&long_separator);
3839 __ lw(string, MemOperand(element));
3840 __ Addu(element, element, kPointerSize);
3841 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3842 __ SmiUntag(string_length);
3843 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3844 __ CopyBytes(string, result_pos, string_length, scratch1);
3845 // End while (element < elements_end).
3846 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3847 ASSERT(result.is(v0));
3848 __ Branch(&done);
3849
3850 __ bind(&bailout);
3851 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3852 __ bind(&done);
3853 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003854}
3855
3856
ager@chromium.org5c838252010-02-19 08:53:10 +00003857void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003858 Handle<String> name = expr->name();
3859 if (name->length() > 0 && name->Get(0) == '_') {
3860 Comment cmnt(masm_, "[ InlineRuntimeCall");
3861 EmitInlineRuntimeCall(expr);
3862 return;
3863 }
3864
3865 Comment cmnt(masm_, "[ CallRuntime");
3866 ZoneList<Expression*>* args = expr->arguments();
3867
3868 if (expr->is_jsruntime()) {
3869 // Prepare for calling JS runtime function.
3870 __ lw(a0, GlobalObjectOperand());
3871 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3872 __ push(a0);
3873 }
3874
3875 // Push the arguments ("left-to-right").
3876 int arg_count = args->length();
3877 for (int i = 0; i < arg_count; i++) {
3878 VisitForStackValue(args->at(i));
3879 }
3880
3881 if (expr->is_jsruntime()) {
3882 // Call the JS runtime function.
3883 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003884 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003885 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003886 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00003887 CallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003888 // Restore context register.
3889 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3890 } else {
3891 // Call the C runtime function.
3892 __ CallRuntime(expr->function(), arg_count);
3893 }
3894 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003895}
3896
3897
3898void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003899 switch (expr->op()) {
3900 case Token::DELETE: {
3901 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003902 Property* property = expr->expression()->AsProperty();
3903 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003904
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003905 if (property != NULL) {
3906 VisitForStackValue(property->obj());
3907 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003908 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
3909 ? kNonStrictMode : kStrictMode;
3910 __ li(a1, Operand(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003911 __ push(a1);
3912 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3913 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003914 } else if (proxy != NULL) {
3915 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003916 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003917 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003918 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003919 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003920 __ lw(a2, GlobalObjectOperand());
3921 __ li(a1, Operand(var->name()));
3922 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3923 __ Push(a2, a1, a0);
3924 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3925 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003926 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003927 // Result of deleting non-global, non-dynamic variables is false.
3928 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003929 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003930 } else {
3931 // Non-global variable. Call the runtime to try to delete from the
3932 // context where the variable was introduced.
3933 __ push(context_register());
3934 __ li(a2, Operand(var->name()));
3935 __ push(a2);
3936 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3937 context()->Plug(v0);
3938 }
3939 } else {
3940 // Result of deleting non-property, non-variable reference is true.
3941 // The subexpression may have side effects.
3942 VisitForEffect(expr->expression());
3943 context()->Plug(true);
3944 }
3945 break;
3946 }
3947
3948 case Token::VOID: {
3949 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3950 VisitForEffect(expr->expression());
3951 context()->Plug(Heap::kUndefinedValueRootIndex);
3952 break;
3953 }
3954
3955 case Token::NOT: {
3956 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3957 if (context()->IsEffect()) {
3958 // Unary NOT has no side effects so it's only necessary to visit the
3959 // subexpression. Match the optimizing compiler by not branching.
3960 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003961 } else if (context()->IsTest()) {
3962 const TestContext* test = TestContext::cast(context());
3963 // The labels are swapped for the recursive call.
3964 VisitForControl(expr->expression(),
3965 test->false_label(),
3966 test->true_label(),
3967 test->fall_through());
3968 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003969 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003970 // We handle value contexts explicitly rather than simply visiting
3971 // for control and plugging the control flow into the context,
3972 // because we need to prepare a pair of extra administrative AST ids
3973 // for the optimizing compiler.
3974 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3975 Label materialize_true, materialize_false, done;
3976 VisitForControl(expr->expression(),
3977 &materialize_false,
3978 &materialize_true,
3979 &materialize_true);
3980 __ bind(&materialize_true);
3981 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3982 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3983 if (context()->IsStackValue()) __ push(v0);
3984 __ jmp(&done);
3985 __ bind(&materialize_false);
3986 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3987 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3988 if (context()->IsStackValue()) __ push(v0);
3989 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003990 }
3991 break;
3992 }
3993
3994 case Token::TYPEOF: {
3995 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3996 { StackValueContext context(this);
3997 VisitForTypeofValue(expr->expression());
3998 }
3999 __ CallRuntime(Runtime::kTypeof, 1);
4000 context()->Plug(v0);
4001 break;
4002 }
4003
4004 case Token::ADD: {
4005 Comment cmt(masm_, "[ UnaryOperation (ADD)");
4006 VisitForAccumulatorValue(expr->expression());
4007 Label no_conversion;
4008 __ JumpIfSmi(result_register(), &no_conversion);
4009 __ mov(a0, result_register());
4010 ToNumberStub convert_stub;
4011 __ CallStub(&convert_stub);
4012 __ bind(&no_conversion);
4013 context()->Plug(result_register());
4014 break;
4015 }
4016
4017 case Token::SUB:
4018 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
4019 break;
4020
4021 case Token::BIT_NOT:
4022 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
4023 break;
4024
4025 default:
4026 UNREACHABLE();
4027 }
4028}
4029
4030
4031void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
4032 const char* comment) {
4033 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
4034 Comment cmt(masm_, comment);
4035 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
4036 UnaryOverwriteMode overwrite =
4037 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004038 UnaryOpStub stub(expr->op(), overwrite);
4039 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004040 VisitForAccumulatorValue(expr->expression());
4041 SetSourcePosition(expr->position());
4042 __ mov(a0, result_register());
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004043 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004044 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004045}
4046
4047
4048void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004049 Comment cmnt(masm_, "[ CountOperation");
4050 SetSourcePosition(expr->position());
4051
4052 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
4053 // as the left-hand side.
4054 if (!expr->expression()->IsValidLeftHandSide()) {
4055 VisitForEffect(expr->expression());
4056 return;
4057 }
4058
4059 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004060 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004061 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
4062 LhsKind assign_type = VARIABLE;
4063 Property* prop = expr->expression()->AsProperty();
4064 // In case of a property we use the uninitialized expression context
4065 // of the key to detect a named property.
4066 if (prop != NULL) {
4067 assign_type =
4068 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
4069 }
4070
4071 // Evaluate expression and get value.
4072 if (assign_type == VARIABLE) {
4073 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
4074 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00004075 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004076 } else {
4077 // Reserve space for result of postfix operation.
4078 if (expr->is_postfix() && !context()->IsEffect()) {
4079 __ li(at, Operand(Smi::FromInt(0)));
4080 __ push(at);
4081 }
4082 if (assign_type == NAMED_PROPERTY) {
4083 // Put the object both on the stack and in the accumulator.
4084 VisitForAccumulatorValue(prop->obj());
4085 __ push(v0);
4086 EmitNamedPropertyLoad(prop);
4087 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004088 VisitForStackValue(prop->obj());
4089 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004090 __ lw(a1, MemOperand(sp, 0));
4091 __ push(v0);
4092 EmitKeyedPropertyLoad(prop);
4093 }
4094 }
4095
4096 // We need a second deoptimization point after loading the value
4097 // in case evaluating the property load my have a side effect.
4098 if (assign_type == VARIABLE) {
4099 PrepareForBailout(expr->expression(), TOS_REG);
4100 } else {
4101 PrepareForBailoutForId(expr->CountId(), TOS_REG);
4102 }
4103
4104 // Call ToNumber only if operand is not a smi.
4105 Label no_conversion;
4106 __ JumpIfSmi(v0, &no_conversion);
4107 __ mov(a0, v0);
4108 ToNumberStub convert_stub;
4109 __ CallStub(&convert_stub);
4110 __ bind(&no_conversion);
4111
4112 // Save result for postfix expressions.
4113 if (expr->is_postfix()) {
4114 if (!context()->IsEffect()) {
4115 // Save the result on the stack. If we have a named or keyed property
4116 // we store the result under the receiver that is currently on top
4117 // of the stack.
4118 switch (assign_type) {
4119 case VARIABLE:
4120 __ push(v0);
4121 break;
4122 case NAMED_PROPERTY:
4123 __ sw(v0, MemOperand(sp, kPointerSize));
4124 break;
4125 case KEYED_PROPERTY:
4126 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4127 break;
4128 }
4129 }
4130 }
4131 __ mov(a0, result_register());
4132
4133 // Inline smi case if we are in a loop.
4134 Label stub_call, done;
4135 JumpPatchSite patch_site(masm_);
4136
4137 int count_value = expr->op() == Token::INC ? 1 : -1;
4138 __ li(a1, Operand(Smi::FromInt(count_value)));
4139
4140 if (ShouldInlineSmiCase(expr->op())) {
4141 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
4142 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
4143
4144 // We could eliminate this smi check if we split the code at
4145 // the first smi check before calling ToNumber.
4146 patch_site.EmitJumpIfSmi(v0, &done);
4147 __ bind(&stub_call);
4148 }
4149
4150 // Record position before stub call.
4151 SetSourcePosition(expr->position());
4152
danno@chromium.org40cb8782011-05-25 07:58:50 +00004153 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004154 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004155 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004156 __ bind(&done);
4157
4158 // Store the value returned in v0.
4159 switch (assign_type) {
4160 case VARIABLE:
4161 if (expr->is_postfix()) {
4162 { EffectContext context(this);
4163 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4164 Token::ASSIGN);
4165 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4166 context.Plug(v0);
4167 }
4168 // For all contexts except EffectConstant we have the result on
4169 // top of the stack.
4170 if (!context()->IsEffect()) {
4171 context()->PlugTOS();
4172 }
4173 } else {
4174 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4175 Token::ASSIGN);
4176 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4177 context()->Plug(v0);
4178 }
4179 break;
4180 case NAMED_PROPERTY: {
4181 __ mov(a0, result_register()); // Value.
4182 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4183 __ pop(a1); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004184 Handle<Code> ic = is_classic_mode()
4185 ? isolate()->builtins()->StoreIC_Initialize()
4186 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004187 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004188 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4189 if (expr->is_postfix()) {
4190 if (!context()->IsEffect()) {
4191 context()->PlugTOS();
4192 }
4193 } else {
4194 context()->Plug(v0);
4195 }
4196 break;
4197 }
4198 case KEYED_PROPERTY: {
4199 __ mov(a0, result_register()); // Value.
4200 __ pop(a1); // Key.
4201 __ pop(a2); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004202 Handle<Code> ic = is_classic_mode()
4203 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4204 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004205 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004206 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4207 if (expr->is_postfix()) {
4208 if (!context()->IsEffect()) {
4209 context()->PlugTOS();
4210 }
4211 } else {
4212 context()->Plug(v0);
4213 }
4214 break;
4215 }
4216 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004217}
4218
4219
lrn@chromium.org7516f052011-03-30 08:52:27 +00004220void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004221 ASSERT(!context()->IsEffect());
4222 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004223 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004224 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004225 Comment cmnt(masm_, "Global variable");
4226 __ lw(a0, GlobalObjectOperand());
4227 __ li(a2, Operand(proxy->name()));
4228 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4229 // Use a regular load, not a contextual load, to avoid a reference
4230 // error.
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004231 CallIC(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004232 PrepareForBailout(expr, TOS_REG);
4233 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004234 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004235 Label done, slow;
4236
4237 // Generate code for loading from variables potentially shadowed
4238 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004239 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004240
4241 __ bind(&slow);
4242 __ li(a0, Operand(proxy->name()));
4243 __ Push(cp, a0);
4244 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4245 PrepareForBailout(expr, TOS_REG);
4246 __ bind(&done);
4247
4248 context()->Plug(v0);
4249 } else {
4250 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004251 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004252 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004253}
4254
ager@chromium.org04921a82011-06-27 13:21:41 +00004255void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004256 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004257 Handle<String> check) {
4258 Label materialize_true, materialize_false;
4259 Label* if_true = NULL;
4260 Label* if_false = NULL;
4261 Label* fall_through = NULL;
4262 context()->PrepareTest(&materialize_true, &materialize_false,
4263 &if_true, &if_false, &fall_through);
4264
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004265 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004266 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004267 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004268 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004269
4270 if (check->Equals(isolate()->heap()->number_symbol())) {
4271 __ JumpIfSmi(v0, if_true);
4272 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4273 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4274 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4275 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4276 __ JumpIfSmi(v0, if_false);
4277 // Check for undetectable objects => false.
4278 __ GetObjectType(v0, v0, a1);
4279 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4280 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4281 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4282 Split(eq, a1, Operand(zero_reg),
4283 if_true, if_false, fall_through);
4284 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4285 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4286 __ Branch(if_true, eq, v0, Operand(at));
4287 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4288 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004289 } else if (FLAG_harmony_typeof &&
4290 check->Equals(isolate()->heap()->null_symbol())) {
4291 __ LoadRoot(at, Heap::kNullValueRootIndex);
4292 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004293 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4294 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4295 __ Branch(if_true, eq, v0, Operand(at));
4296 __ JumpIfSmi(v0, if_false);
4297 // Check for undetectable objects => true.
4298 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4299 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4300 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4301 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4302 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4303 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004304 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4305 __ GetObjectType(v0, v0, a1);
4306 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4307 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4308 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004309 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4310 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004311 if (!FLAG_harmony_typeof) {
4312 __ LoadRoot(at, Heap::kNullValueRootIndex);
4313 __ Branch(if_true, eq, v0, Operand(at));
4314 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004315 // Check for JS objects => true.
4316 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004317 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004318 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004319 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004320 // Check for undetectable objects => false.
4321 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4322 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4323 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4324 } else {
4325 if (if_false != fall_through) __ jmp(if_false);
4326 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004327 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004328}
4329
4330
ager@chromium.org5c838252010-02-19 08:53:10 +00004331void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004332 Comment cmnt(masm_, "[ CompareOperation");
4333 SetSourcePosition(expr->position());
4334
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004335 // First we try a fast inlined version of the compare when one of
4336 // the operands is a literal.
4337 if (TryLiteralCompare(expr)) return;
4338
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004339 // Always perform the comparison for its control flow. Pack the result
4340 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004341 Label materialize_true, materialize_false;
4342 Label* if_true = NULL;
4343 Label* if_false = NULL;
4344 Label* fall_through = NULL;
4345 context()->PrepareTest(&materialize_true, &materialize_false,
4346 &if_true, &if_false, &fall_through);
4347
ager@chromium.org04921a82011-06-27 13:21:41 +00004348 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004349 VisitForStackValue(expr->left());
4350 switch (op) {
4351 case Token::IN:
4352 VisitForStackValue(expr->right());
4353 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004354 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004355 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4356 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4357 break;
4358
4359 case Token::INSTANCEOF: {
4360 VisitForStackValue(expr->right());
4361 InstanceofStub stub(InstanceofStub::kNoFlags);
4362 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004363 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004364 // The stub returns 0 for true.
4365 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4366 break;
4367 }
4368
4369 default: {
4370 VisitForAccumulatorValue(expr->right());
4371 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004372 switch (op) {
4373 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004374 case Token::EQ:
4375 cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004376 break;
4377 case Token::LT:
4378 cc = lt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004379 break;
4380 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004381 cc = gt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004382 break;
4383 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004384 cc = le;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004385 break;
4386 case Token::GTE:
4387 cc = ge;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004388 break;
4389 case Token::IN:
4390 case Token::INSTANCEOF:
4391 default:
4392 UNREACHABLE();
4393 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004394 __ mov(a0, result_register());
4395 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004396
4397 bool inline_smi_code = ShouldInlineSmiCase(op);
4398 JumpPatchSite patch_site(masm_);
4399 if (inline_smi_code) {
4400 Label slow_case;
4401 __ Or(a2, a0, Operand(a1));
4402 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4403 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4404 __ bind(&slow_case);
4405 }
4406 // Record position and call the compare IC.
4407 SetSourcePosition(expr->position());
4408 Handle<Code> ic = CompareIC::GetUninitialized(op);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004409 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004410 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004411 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004412 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4413 }
4414 }
4415
4416 // Convert the result of the comparison into one expected for this
4417 // expression's context.
4418 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004419}
4420
4421
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004422void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4423 Expression* sub_expr,
4424 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004425 Label materialize_true, materialize_false;
4426 Label* if_true = NULL;
4427 Label* if_false = NULL;
4428 Label* fall_through = NULL;
4429 context()->PrepareTest(&materialize_true, &materialize_false,
4430 &if_true, &if_false, &fall_through);
4431
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004432 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004433 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004434 Heap::RootListIndex nil_value = nil == kNullValue ?
4435 Heap::kNullValueRootIndex :
4436 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004437 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004438 __ LoadRoot(a1, nil_value);
4439 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004440 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4441 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004442 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4443 Heap::kUndefinedValueRootIndex :
4444 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004445 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004446 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004447 __ Branch(if_true, eq, a0, Operand(a1));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004448 __ JumpIfSmi(a0, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004449 // It can be an undetectable object.
4450 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4451 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4452 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4453 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4454 }
4455 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004456}
4457
4458
ager@chromium.org5c838252010-02-19 08:53:10 +00004459void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004460 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4461 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004462}
4463
4464
lrn@chromium.org7516f052011-03-30 08:52:27 +00004465Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004466 return v0;
4467}
ager@chromium.org5c838252010-02-19 08:53:10 +00004468
4469
lrn@chromium.org7516f052011-03-30 08:52:27 +00004470Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004471 return cp;
4472}
4473
4474
ager@chromium.org5c838252010-02-19 08:53:10 +00004475void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004476 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4477 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004478}
4479
4480
4481void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004482 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004483}
4484
4485
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004486void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4487 Scope* declaration_scope = scope()->DeclarationScope();
4488 if (declaration_scope->is_global_scope()) {
4489 // Contexts nested in the global context have a canonical empty function
4490 // as their closure, not the anonymous closure containing the global
4491 // code. Pass a smi sentinel and let the runtime look up the empty
4492 // function.
4493 __ li(at, Operand(Smi::FromInt(0)));
4494 } else if (declaration_scope->is_eval_scope()) {
4495 // Contexts created by a call to eval have the same closure as the
4496 // context calling eval, not the anonymous closure containing the eval
4497 // code. Fetch it from the context.
4498 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4499 } else {
4500 ASSERT(declaration_scope->is_function_scope());
4501 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4502 }
4503 __ push(at);
4504}
4505
4506
ager@chromium.org5c838252010-02-19 08:53:10 +00004507// ----------------------------------------------------------------------------
4508// Non-local control flow support.
4509
4510void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004511 ASSERT(!result_register().is(a1));
4512 // Store result register while executing finally block.
4513 __ push(result_register());
4514 // Cook return address in link register to stack (smi encoded Code* delta).
4515 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4516 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004517 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004518 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4519 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004520}
4521
4522
4523void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004524 ASSERT(!result_register().is(a1));
4525 // Restore result register from stack.
4526 __ pop(a1);
4527 // Uncook return address and return.
4528 __ pop(result_register());
4529 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4530 __ sra(a1, a1, 1); // Un-smi-tag value.
4531 __ Addu(at, a1, Operand(masm_->CodeObject()));
4532 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004533}
4534
4535
4536#undef __
4537
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004538#define __ ACCESS_MASM(masm())
4539
4540FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4541 int* stack_depth,
4542 int* context_length) {
4543 // The macros used here must preserve the result register.
4544
4545 // Because the handler block contains the context of the finally
4546 // code, we can restore it directly from there for the finally code
4547 // rather than iteratively unwinding contexts via their previous
4548 // links.
4549 __ Drop(*stack_depth); // Down to the handler block.
4550 if (*context_length > 0) {
4551 // Restore the context to its dedicated register and the stack.
4552 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4553 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4554 }
4555 __ PopTryHandler();
4556 __ Call(finally_entry_);
4557
4558 *stack_depth = 0;
4559 *context_length = 0;
4560 return previous_;
4561}
4562
4563
4564#undef __
4565
ager@chromium.org5c838252010-02-19 08:53:10 +00004566} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004567
4568#endif // V8_TARGET_ARCH_MIPS