blob: bd6f9902aaa016abe5342651d617f455e67979fd [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) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000278 VariableDeclaration* function = scope()->function();
279 ASSERT(function->proxy()->var()->mode() == CONST ||
280 function->proxy()->var()->mode() == CONST_HARMONY);
281 ASSERT(function->proxy()->var()->location() != Variable::UNALLOCATED);
282 VisitVariableDeclaration(function);
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
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000792void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
793 // The variable in the declaration always resides in the current function
794 // context.
795 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
796 if (FLAG_debug_code) {
797 // Check that we're not inside a with or catch context.
798 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
799 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
800 __ Check(ne, "Declaration in with context.",
801 a1, Operand(t0));
802 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
803 __ Check(ne, "Declaration in catch context.",
804 a1, Operand(t0));
805 }
806}
807
808
809void FullCodeGenerator::VisitVariableDeclaration(
810 VariableDeclaration* declaration) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000811 // If it was not possible to allocate the variable at compile time, we
812 // need to "declare" it at runtime to make sure it actually exists in the
813 // local context.
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000814 VariableProxy* proxy = declaration->proxy();
815 VariableMode mode = declaration->mode();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000816 Variable* variable = proxy->var();
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000817 bool hole_init = mode == CONST || mode == CONST_HARMONY || mode == LET;
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000818 switch (variable->location()) {
819 case Variable::UNALLOCATED:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000820 globals_->Add(variable->name());
821 globals_->Add(variable->binding_needs_init()
822 ? isolate()->factory()->the_hole_value()
823 : isolate()->factory()->undefined_value());
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000824 break;
825
826 case Variable::PARAMETER:
827 case Variable::LOCAL:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000828 if (hole_init) {
829 Comment cmnt(masm_, "[ VariableDeclaration");
830 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
831 __ sw(t0, StackOperand(variable));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000832 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000833 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000834
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000835 case Variable::CONTEXT:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000836 if (hole_init) {
837 Comment cmnt(masm_, "[ VariableDeclaration");
838 EmitDebugCheckDeclarationContext(variable);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000839 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000840 __ sw(at, ContextOperand(cp, variable->index()));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000841 // No write barrier since the_hole_value is in old space.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000842 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000843 }
844 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000845
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000846 case Variable::LOOKUP: {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000847 Comment cmnt(masm_, "[ VariableDeclaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000848 __ li(a2, Operand(variable->name()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000849 // Declaration nodes are always introduced in one of four modes.
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000850 ASSERT(mode == VAR || mode == LET ||
851 mode == CONST || mode == CONST_HARMONY);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000852 PropertyAttributes attr = (mode == CONST || mode == CONST_HARMONY)
853 ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000854 __ li(a1, Operand(Smi::FromInt(attr)));
855 // Push initial value, if any.
856 // Note: For variables we must not push an initial value (such as
857 // 'undefined') because we may have a (legal) redeclaration and we
858 // must not destroy the current value.
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000859 if (hole_init) {
860 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
861 __ Push(cp, a2, a1, a0);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000862 } else {
863 ASSERT(Smi::FromInt(0) == 0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000864 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000865 __ Push(cp, a2, a1, a0);
866 }
867 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
868 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000869 }
870 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000871}
872
873
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000874void FullCodeGenerator::VisitFunctionDeclaration(
875 FunctionDeclaration* declaration) {
876 VariableProxy* proxy = declaration->proxy();
877 Variable* variable = proxy->var();
878 switch (variable->location()) {
879 case Variable::UNALLOCATED: {
880 globals_->Add(variable->name());
881 Handle<SharedFunctionInfo> function =
882 Compiler::BuildFunctionInfo(declaration->fun(), script());
883 // Check for stack-overflow exception.
884 if (function.is_null()) return SetStackOverflow();
885 globals_->Add(function);
886 break;
887 }
888
889 case Variable::PARAMETER:
890 case Variable::LOCAL: {
891 Comment cmnt(masm_, "[ FunctionDeclaration");
892 VisitForAccumulatorValue(declaration->fun());
893 __ sw(result_register(), StackOperand(variable));
894 break;
895 }
896
897 case Variable::CONTEXT: {
898 Comment cmnt(masm_, "[ FunctionDeclaration");
899 EmitDebugCheckDeclarationContext(variable);
900 VisitForAccumulatorValue(declaration->fun());
901 __ sw(result_register(), ContextOperand(cp, variable->index()));
902 int offset = Context::SlotOffset(variable->index());
903 // We know that we have written a function, which is not a smi.
904 __ RecordWriteContextSlot(cp,
905 offset,
906 result_register(),
907 a2,
908 kRAHasBeenSaved,
909 kDontSaveFPRegs,
910 EMIT_REMEMBERED_SET,
911 OMIT_SMI_CHECK);
912 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
913 break;
914 }
915
916 case Variable::LOOKUP: {
917 Comment cmnt(masm_, "[ FunctionDeclaration");
918 __ li(a2, Operand(variable->name()));
919 __ li(a1, Operand(Smi::FromInt(NONE)));
920 __ Push(cp, a2, a1);
921 // Push initial value for function declaration.
922 VisitForStackValue(declaration->fun());
923 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
924 break;
925 }
926 }
927}
928
929
930void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* declaration) {
931 VariableProxy* proxy = declaration->proxy();
932 Variable* variable = proxy->var();
933 Handle<JSModule> instance = declaration->module()->interface()->Instance();
934 ASSERT(!instance.is_null());
935
936 switch (variable->location()) {
937 case Variable::UNALLOCATED: {
938 Comment cmnt(masm_, "[ ModuleDeclaration");
939 globals_->Add(variable->name());
940 globals_->Add(instance);
941 Visit(declaration->module());
942 break;
943 }
944
945 case Variable::CONTEXT: {
946 Comment cmnt(masm_, "[ ModuleDeclaration");
947 EmitDebugCheckDeclarationContext(variable);
948 __ li(a1, Operand(instance));
949 __ sw(a1, ContextOperand(cp, variable->index()));
950 Visit(declaration->module());
951 break;
952 }
953
954 case Variable::PARAMETER:
955 case Variable::LOCAL:
956 case Variable::LOOKUP:
957 UNREACHABLE();
958 }
959}
960
961
962void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* declaration) {
963 VariableProxy* proxy = declaration->proxy();
964 Variable* variable = proxy->var();
965 switch (variable->location()) {
966 case Variable::UNALLOCATED:
967 // TODO(rossberg)
968 break;
969
970 case Variable::CONTEXT: {
971 Comment cmnt(masm_, "[ ImportDeclaration");
972 EmitDebugCheckDeclarationContext(variable);
973 // TODO(rossberg)
974 break;
975 }
976
977 case Variable::PARAMETER:
978 case Variable::LOCAL:
979 case Variable::LOOKUP:
980 UNREACHABLE();
981 }
982}
983
984
985void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* declaration) {
986 // TODO(rossberg)
987}
988
989
ager@chromium.org5c838252010-02-19 08:53:10 +0000990void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000991 // Call the runtime to declare the globals.
992 // The context is the first argument.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000993 __ li(a1, Operand(pairs));
994 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
995 __ Push(cp, a1, a0);
996 __ CallRuntime(Runtime::kDeclareGlobals, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000997 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000998}
999
1000
lrn@chromium.org7516f052011-03-30 08:52:27 +00001001void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001002 Comment cmnt(masm_, "[ SwitchStatement");
1003 Breakable nested_statement(this, stmt);
1004 SetStatementPosition(stmt);
1005
1006 // Keep the switch value on the stack until a case matches.
1007 VisitForStackValue(stmt->tag());
1008 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
1009
1010 ZoneList<CaseClause*>* clauses = stmt->cases();
1011 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
1012
1013 Label next_test; // Recycled for each test.
1014 // Compile all the tests with branches to their bodies.
1015 for (int i = 0; i < clauses->length(); i++) {
1016 CaseClause* clause = clauses->at(i);
1017 clause->body_target()->Unuse();
1018
1019 // The default is not a test, but remember it as final fall through.
1020 if (clause->is_default()) {
1021 default_clause = clause;
1022 continue;
1023 }
1024
1025 Comment cmnt(masm_, "[ Case comparison");
1026 __ bind(&next_test);
1027 next_test.Unuse();
1028
1029 // Compile the label expression.
1030 VisitForAccumulatorValue(clause->label());
1031 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
1032
1033 // Perform the comparison as if via '==='.
1034 __ lw(a1, MemOperand(sp, 0)); // Switch value.
1035 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
1036 JumpPatchSite patch_site(masm_);
1037 if (inline_smi_code) {
1038 Label slow_case;
1039 __ or_(a2, a1, a0);
1040 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
1041
1042 __ Branch(&next_test, ne, a1, Operand(a0));
1043 __ Drop(1); // Switch value is no longer needed.
1044 __ Branch(clause->body_target());
1045
1046 __ bind(&slow_case);
1047 }
1048
1049 // Record position before stub call for type feedback.
1050 SetSourcePosition(clause->position());
1051 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001052 CallIC(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001053 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001054
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001055 __ Branch(&next_test, ne, v0, Operand(zero_reg));
1056 __ Drop(1); // Switch value is no longer needed.
1057 __ Branch(clause->body_target());
1058 }
1059
1060 // Discard the test value and jump to the default if present, otherwise to
1061 // the end of the statement.
1062 __ bind(&next_test);
1063 __ Drop(1); // Switch value is no longer needed.
1064 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001065 __ Branch(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001066 } else {
1067 __ Branch(default_clause->body_target());
1068 }
1069
1070 // Compile all the case bodies.
1071 for (int i = 0; i < clauses->length(); i++) {
1072 Comment cmnt(masm_, "[ Case body");
1073 CaseClause* clause = clauses->at(i);
1074 __ bind(clause->body_target());
1075 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
1076 VisitStatements(clause->statements());
1077 }
1078
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001079 __ bind(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001080 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001081}
1082
1083
1084void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001085 Comment cmnt(masm_, "[ ForInStatement");
1086 SetStatementPosition(stmt);
1087
1088 Label loop, exit;
1089 ForIn loop_statement(this, stmt);
1090 increment_loop_depth();
1091
1092 // Get the object to enumerate over. Both SpiderMonkey and JSC
1093 // ignore null and undefined in contrast to the specification; see
1094 // ECMA-262 section 12.6.4.
1095 VisitForAccumulatorValue(stmt->enumerable());
1096 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
1097 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1098 __ Branch(&exit, eq, a0, Operand(at));
1099 Register null_value = t1;
1100 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1101 __ Branch(&exit, eq, a0, Operand(null_value));
ulan@chromium.org812308e2012-02-29 15:58:45 +00001102 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1103 __ mov(a0, v0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001104 // Convert the object to a JS object.
1105 Label convert, done_convert;
1106 __ JumpIfSmi(a0, &convert);
1107 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001108 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001109 __ bind(&convert);
1110 __ push(a0);
1111 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1112 __ mov(a0, v0);
1113 __ bind(&done_convert);
1114 __ push(a0);
1115
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001116 // Check for proxies.
1117 Label call_runtime;
1118 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1119 __ GetObjectType(a0, a1, a1);
1120 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
1121
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001122 // Check cache validity in generated code. This is a fast case for
1123 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1124 // guarantee cache validity, call the runtime system to check cache
1125 // validity or get the property names in a fixed array.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001126 __ CheckEnumCache(null_value, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001127
1128 // The enum cache is valid. Load the map of the object being
1129 // iterated over and use the cache for the iteration.
1130 Label use_cache;
1131 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
1132 __ Branch(&use_cache);
1133
1134 // Get the set of properties to enumerate.
1135 __ bind(&call_runtime);
1136 __ push(a0); // Duplicate the enumerable object on the stack.
1137 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1138
1139 // If we got a map from the runtime call, we can do a fast
1140 // modification check. Otherwise, we got a fixed array, and we have
1141 // to do a slow check.
1142 Label fixed_array;
1143 __ mov(a2, v0);
1144 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
1145 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1146 __ Branch(&fixed_array, ne, a1, Operand(at));
1147
1148 // We got a map in register v0. Get the enumeration cache from it.
1149 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001150 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001151 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
1152 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1153
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001154 // Set up the four remaining stack slots.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001155 __ push(v0); // Map.
1156 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
1157 __ li(a0, Operand(Smi::FromInt(0)));
1158 // Push enumeration cache, enumeration cache length (as smi) and zero.
1159 __ Push(a2, a1, a0);
1160 __ jmp(&loop);
1161
1162 // We got a fixed array in register v0. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001163 Label non_proxy;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001164 __ bind(&fixed_array);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001165
1166 Handle<JSGlobalPropertyCell> cell =
1167 isolate()->factory()->NewJSGlobalPropertyCell(
1168 Handle<Object>(
1169 Smi::FromInt(TypeFeedbackCells::kForInFastCaseMarker)));
1170 RecordTypeFeedbackCell(stmt->PrepareId(), cell);
1171 __ LoadHeapObject(a1, cell);
1172 __ li(a2, Operand(Smi::FromInt(TypeFeedbackCells::kForInSlowCaseMarker)));
1173 __ sw(a2, FieldMemOperand(a1, JSGlobalPropertyCell::kValueOffset));
1174
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001175 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1176 __ lw(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1177 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1178 __ GetObjectType(a2, a3, a3);
1179 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1180 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1181 __ bind(&non_proxy);
1182 __ Push(a1, v0); // Smi and array
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001183 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1184 __ li(a0, Operand(Smi::FromInt(0)));
1185 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1186
1187 // Generate code for doing the condition check.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001188 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001189 __ bind(&loop);
1190 // Load the current count to a0, load the length to a1.
1191 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1192 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001193 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001194
1195 // Get the current entry of the array into register a3.
1196 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1197 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1198 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1199 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1200 __ lw(a3, MemOperand(t0)); // Current entry.
1201
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001202 // Get the expected map from the stack or a smi in the
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001203 // permanent slow case into register a2.
1204 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1205
1206 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001207 // If not, we may have to filter the key.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001208 Label update_each;
1209 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1210 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1211 __ Branch(&update_each, eq, t0, Operand(a2));
1212
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001213 // For proxies, no filtering is done.
1214 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1215 ASSERT_EQ(Smi::FromInt(0), 0);
1216 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1217
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001218 // Convert the entry to a string or (smi) 0 if it isn't a property
1219 // any more. If the property has been removed while iterating, we
1220 // just skip it.
1221 __ push(a1); // Enumerable.
1222 __ push(a3); // Current entry.
1223 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1224 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001225 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001226
1227 // Update the 'each' property or variable from the possibly filtered
1228 // entry in register a3.
1229 __ bind(&update_each);
1230 __ mov(result_register(), a3);
1231 // Perform the assignment as if via '='.
1232 { EffectContext context(this);
ulan@chromium.org812308e2012-02-29 15:58:45 +00001233 EmitAssignment(stmt->each());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001234 }
1235
1236 // Generate code for the body of the loop.
1237 Visit(stmt->body());
1238
1239 // Generate code for the going to the next element by incrementing
1240 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001241 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001242 __ pop(a0);
1243 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1244 __ push(a0);
1245
yangguo@chromium.org56454712012-02-16 15:33:53 +00001246 EmitStackCheck(stmt, &loop);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001247 __ Branch(&loop);
1248
1249 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001250 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001251 __ Drop(5);
1252
1253 // Exit and decrement the loop depth.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001254 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001255 __ bind(&exit);
1256 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001257}
1258
1259
1260void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1261 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001262 // Use the fast case closure allocation code that allocates in new
1263 // space for nested functions that don't need literals cloning. If
1264 // we're running with the --always-opt or the --prepare-always-opt
1265 // flag, we need to use the runtime function so that the new function
1266 // we are creating here gets a chance to have its code optimized and
1267 // doesn't just get a copy of the existing unoptimized code.
1268 if (!FLAG_always_opt &&
1269 !FLAG_prepare_always_opt &&
1270 !pretenure &&
1271 scope()->is_function_scope() &&
1272 info->num_literals() == 0) {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001273 FastNewClosureStub stub(info->language_mode());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001274 __ li(a0, Operand(info));
1275 __ push(a0);
1276 __ CallStub(&stub);
1277 } else {
1278 __ li(a0, Operand(info));
1279 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1280 : Heap::kFalseValueRootIndex);
1281 __ Push(cp, a0, a1);
1282 __ CallRuntime(Runtime::kNewClosure, 3);
1283 }
1284 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001285}
1286
1287
1288void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001289 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001290 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001291}
1292
1293
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001294void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1295 TypeofState typeof_state,
1296 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001297 Register current = cp;
1298 Register next = a1;
1299 Register temp = a2;
1300
1301 Scope* s = scope();
1302 while (s != NULL) {
1303 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001304 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001305 // Check that extension is NULL.
1306 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1307 __ Branch(slow, ne, temp, Operand(zero_reg));
1308 }
1309 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001310 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001311 // Walk the rest of the chain without clobbering cp.
1312 current = next;
1313 }
1314 // If no outer scope calls eval, we do not need to check more
1315 // context extensions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001316 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001317 s = s->outer_scope();
1318 }
1319
1320 if (s->is_eval_scope()) {
1321 Label loop, fast;
1322 if (!current.is(next)) {
1323 __ Move(next, current);
1324 }
1325 __ bind(&loop);
1326 // Terminate at global context.
1327 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1328 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1329 __ Branch(&fast, eq, temp, Operand(t0));
1330 // Check that extension is NULL.
1331 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1332 __ Branch(slow, ne, temp, Operand(zero_reg));
1333 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001334 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001335 __ Branch(&loop);
1336 __ bind(&fast);
1337 }
1338
1339 __ lw(a0, GlobalObjectOperand());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001340 __ li(a2, Operand(var->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001341 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1342 ? RelocInfo::CODE_TARGET
1343 : RelocInfo::CODE_TARGET_CONTEXT;
1344 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001345 CallIC(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001346}
1347
1348
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001349MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1350 Label* slow) {
1351 ASSERT(var->IsContextSlot());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001352 Register context = cp;
1353 Register next = a3;
1354 Register temp = t0;
1355
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001356 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001357 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001358 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001359 // Check that extension is NULL.
1360 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1361 __ Branch(slow, ne, temp, Operand(zero_reg));
1362 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001363 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001364 // Walk the rest of the chain without clobbering cp.
1365 context = next;
1366 }
1367 }
1368 // Check that last extension is NULL.
1369 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1370 __ Branch(slow, ne, temp, Operand(zero_reg));
1371
1372 // This function is used only for loads, not stores, so it's safe to
1373 // return an cp-based operand (the write barrier cannot be allowed to
1374 // destroy the cp register).
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001375 return ContextOperand(context, var->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001376}
1377
1378
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001379void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1380 TypeofState typeof_state,
1381 Label* slow,
1382 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001383 // Generate fast-case code for variables that might be shadowed by
1384 // eval-introduced variables. Eval is used a lot without
1385 // introducing variables. In those cases, we do not want to
1386 // perform a runtime call for all variables in the scope
1387 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001388 if (var->mode() == DYNAMIC_GLOBAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001389 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001390 __ Branch(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001391 } else if (var->mode() == DYNAMIC_LOCAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001392 Variable* local = var->local_if_not_shadowed();
1393 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001394 if (local->mode() == CONST ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001395 local->mode() == CONST_HARMONY ||
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001396 local->mode() == LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001397 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1398 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001399 if (local->mode() == CONST) {
1400 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +00001401 __ Movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001402 } else { // LET || CONST_HARMONY
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001403 __ Branch(done, ne, at, Operand(zero_reg));
1404 __ li(a0, Operand(var->name()));
1405 __ push(a0);
1406 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1407 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001408 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001409 __ Branch(done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001410 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001411}
1412
1413
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001414void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1415 // Record position before possible IC call.
1416 SetSourcePosition(proxy->position());
1417 Variable* var = proxy->var();
1418
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001419 // Three cases: global variables, lookup variables, and all other types of
1420 // variables.
1421 switch (var->location()) {
1422 case Variable::UNALLOCATED: {
1423 Comment cmnt(masm_, "Global variable");
1424 // Use inline caching. Variable name is passed in a2 and the global
1425 // object (receiver) in a0.
1426 __ lw(a0, GlobalObjectOperand());
1427 __ li(a2, Operand(var->name()));
1428 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001429 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001430 context()->Plug(v0);
1431 break;
1432 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001433
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001434 case Variable::PARAMETER:
1435 case Variable::LOCAL:
1436 case Variable::CONTEXT: {
1437 Comment cmnt(masm_, var->IsContextSlot()
1438 ? "Context variable"
1439 : "Stack variable");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001440 if (var->binding_needs_init()) {
1441 // var->scope() may be NULL when the proxy is located in eval code and
1442 // refers to a potential outside binding. Currently those bindings are
1443 // always looked up dynamically, i.e. in that case
1444 // var->location() == LOOKUP.
1445 // always holds.
1446 ASSERT(var->scope() != NULL);
1447
1448 // Check if the binding really needs an initialization check. The check
1449 // can be skipped in the following situation: we have a LET or CONST
1450 // binding in harmony mode, both the Variable and the VariableProxy have
1451 // the same declaration scope (i.e. they are both in global code, in the
1452 // same function or in the same eval code) and the VariableProxy is in
1453 // the source physically located after the initializer of the variable.
1454 //
1455 // We cannot skip any initialization checks for CONST in non-harmony
1456 // mode because const variables may be declared but never initialized:
1457 // if (false) { const x; }; var y = x;
1458 //
1459 // The condition on the declaration scopes is a conservative check for
1460 // nested functions that access a binding and are called before the
1461 // binding is initialized:
1462 // function() { f(); let x = 1; function f() { x = 2; } }
1463 //
1464 bool skip_init_check;
1465 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1466 skip_init_check = false;
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001467 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001468 // Check that we always have valid source position.
1469 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1470 ASSERT(proxy->position() != RelocInfo::kNoPosition);
1471 skip_init_check = var->mode() != CONST &&
1472 var->initializer_position() < proxy->position();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001473 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001474
1475 if (!skip_init_check) {
1476 // Let and const need a read barrier.
1477 GetVar(v0, var);
1478 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1479 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1480 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1481 // Throw a reference error when using an uninitialized let/const
1482 // binding in harmony mode.
1483 Label done;
1484 __ Branch(&done, ne, at, Operand(zero_reg));
1485 __ li(a0, Operand(var->name()));
1486 __ push(a0);
1487 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1488 __ bind(&done);
1489 } else {
1490 // Uninitalized const bindings outside of harmony mode are unholed.
1491 ASSERT(var->mode() == CONST);
1492 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +00001493 __ Movz(v0, a0, at); // Conditional move: Undefined if TheHole.
danno@chromium.orgc612e022011-11-10 11:38:15 +00001494 }
1495 context()->Plug(v0);
1496 break;
1497 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001498 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001499 context()->Plug(var);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001500 break;
1501 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001502
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001503 case Variable::LOOKUP: {
1504 Label done, slow;
1505 // Generate code for loading from variables potentially shadowed
1506 // by eval-introduced variables.
1507 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1508 __ bind(&slow);
1509 Comment cmnt(masm_, "Lookup variable");
1510 __ li(a1, Operand(var->name()));
1511 __ Push(cp, a1); // Context and name.
1512 __ CallRuntime(Runtime::kLoadContextSlot, 2);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001513 __ bind(&done);
1514 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001515 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001516 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001517}
1518
1519
1520void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001521 Comment cmnt(masm_, "[ RegExpLiteral");
1522 Label materialized;
1523 // Registers will be used as follows:
1524 // t1 = materialized value (RegExp literal)
1525 // t0 = JS function, literals array
1526 // a3 = literal index
1527 // a2 = RegExp pattern
1528 // a1 = RegExp flags
1529 // a0 = RegExp literal clone
1530 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1531 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1532 int literal_offset =
1533 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1534 __ lw(t1, FieldMemOperand(t0, literal_offset));
1535 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1536 __ Branch(&materialized, ne, t1, Operand(at));
1537
1538 // Create regexp literal using runtime function.
1539 // Result will be in v0.
1540 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1541 __ li(a2, Operand(expr->pattern()));
1542 __ li(a1, Operand(expr->flags()));
1543 __ Push(t0, a3, a2, a1);
1544 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1545 __ mov(t1, v0);
1546
1547 __ bind(&materialized);
1548 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1549 Label allocated, runtime_allocate;
1550 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1551 __ jmp(&allocated);
1552
1553 __ bind(&runtime_allocate);
1554 __ push(t1);
1555 __ li(a0, Operand(Smi::FromInt(size)));
1556 __ push(a0);
1557 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1558 __ pop(t1);
1559
1560 __ bind(&allocated);
1561
1562 // After this, registers are used as follows:
1563 // v0: Newly allocated regexp.
1564 // t1: Materialized regexp.
1565 // a2: temp.
1566 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1567 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001568}
1569
1570
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001571void FullCodeGenerator::EmitAccessor(Expression* expression) {
1572 if (expression == NULL) {
1573 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1574 __ push(a1);
1575 } else {
1576 VisitForStackValue(expression);
1577 }
1578}
1579
1580
ager@chromium.org5c838252010-02-19 08:53:10 +00001581void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001582 Comment cmnt(masm_, "[ ObjectLiteral");
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001583 Handle<FixedArray> constant_properties = expr->constant_properties();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001584 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1585 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1586 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001587 __ li(a1, Operand(constant_properties));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001588 int flags = expr->fast_elements()
1589 ? ObjectLiteral::kFastElements
1590 : ObjectLiteral::kNoFlags;
1591 flags |= expr->has_function()
1592 ? ObjectLiteral::kHasFunction
1593 : ObjectLiteral::kNoFlags;
1594 __ li(a0, Operand(Smi::FromInt(flags)));
1595 __ Push(a3, a2, a1, a0);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001596 int properties_count = constant_properties->length() / 2;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001597 if (expr->depth() > 1) {
1598 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001599 } else if (flags != ObjectLiteral::kFastElements ||
1600 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001601 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001602 } else {
1603 FastCloneShallowObjectStub stub(properties_count);
1604 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001605 }
1606
1607 // If result_saved is true the result is on top of the stack. If
1608 // result_saved is false the result is in v0.
1609 bool result_saved = false;
1610
1611 // Mark all computed expressions that are bound to a key that
1612 // is shadowed by a later occurrence of the same key. For the
1613 // marked expressions, no store code is emitted.
1614 expr->CalculateEmitStore();
1615
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001616 AccessorTable accessor_table(isolate()->zone());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001617 for (int i = 0; i < expr->properties()->length(); i++) {
1618 ObjectLiteral::Property* property = expr->properties()->at(i);
1619 if (property->IsCompileTimeValue()) continue;
1620
1621 Literal* key = property->key();
1622 Expression* value = property->value();
1623 if (!result_saved) {
1624 __ push(v0); // Save result on stack.
1625 result_saved = true;
1626 }
1627 switch (property->kind()) {
1628 case ObjectLiteral::Property::CONSTANT:
1629 UNREACHABLE();
1630 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1631 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1632 // Fall through.
1633 case ObjectLiteral::Property::COMPUTED:
1634 if (key->handle()->IsSymbol()) {
1635 if (property->emit_store()) {
1636 VisitForAccumulatorValue(value);
1637 __ mov(a0, result_register());
1638 __ li(a2, Operand(key->handle()));
1639 __ lw(a1, MemOperand(sp));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001640 Handle<Code> ic = is_classic_mode()
1641 ? isolate()->builtins()->StoreIC_Initialize()
1642 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001643 CallIC(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001644 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1645 } else {
1646 VisitForEffect(value);
1647 }
1648 break;
1649 }
1650 // Fall through.
1651 case ObjectLiteral::Property::PROTOTYPE:
1652 // Duplicate receiver on stack.
1653 __ lw(a0, MemOperand(sp));
1654 __ push(a0);
1655 VisitForStackValue(key);
1656 VisitForStackValue(value);
1657 if (property->emit_store()) {
1658 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1659 __ push(a0);
1660 __ CallRuntime(Runtime::kSetProperty, 4);
1661 } else {
1662 __ Drop(3);
1663 }
1664 break;
1665 case ObjectLiteral::Property::GETTER:
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001666 accessor_table.lookup(key)->second->getter = value;
1667 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001668 case ObjectLiteral::Property::SETTER:
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001669 accessor_table.lookup(key)->second->setter = value;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001670 break;
1671 }
1672 }
1673
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001674 // Emit code to define accessors, using only a single call to the runtime for
1675 // each pair of corresponding getters and setters.
1676 for (AccessorTable::Iterator it = accessor_table.begin();
1677 it != accessor_table.end();
1678 ++it) {
1679 __ lw(a0, MemOperand(sp)); // Duplicate receiver.
1680 __ push(a0);
1681 VisitForStackValue(it->first);
1682 EmitAccessor(it->second->getter);
1683 EmitAccessor(it->second->setter);
1684 __ li(a0, Operand(Smi::FromInt(NONE)));
1685 __ push(a0);
1686 __ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
1687 }
1688
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001689 if (expr->has_function()) {
1690 ASSERT(result_saved);
1691 __ lw(a0, MemOperand(sp));
1692 __ push(a0);
1693 __ CallRuntime(Runtime::kToFastProperties, 1);
1694 }
1695
1696 if (result_saved) {
1697 context()->PlugTOS();
1698 } else {
1699 context()->Plug(v0);
1700 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001701}
1702
1703
1704void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001705 Comment cmnt(masm_, "[ ArrayLiteral");
1706
1707 ZoneList<Expression*>* subexprs = expr->values();
1708 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001709
1710 Handle<FixedArray> constant_elements = expr->constant_elements();
1711 ASSERT_EQ(2, constant_elements->length());
1712 ElementsKind constant_elements_kind =
1713 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001714 bool has_fast_elements = constant_elements_kind == FAST_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001715 Handle<FixedArrayBase> constant_elements_values(
1716 FixedArrayBase::cast(constant_elements->get(1)));
1717
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001718 __ mov(a0, result_register());
1719 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1720 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1721 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001722 __ li(a1, Operand(constant_elements));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001723 __ Push(a3, a2, a1);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001724 if (has_fast_elements && constant_elements_values->map() ==
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001725 isolate()->heap()->fixed_cow_array_map()) {
1726 FastCloneShallowArrayStub stub(
1727 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1728 __ CallStub(&stub);
1729 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1730 1, a1, a2);
1731 } else if (expr->depth() > 1) {
1732 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1733 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1734 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1735 } else {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001736 ASSERT(constant_elements_kind == FAST_ELEMENTS ||
1737 constant_elements_kind == FAST_SMI_ONLY_ELEMENTS ||
1738 FLAG_smi_only_arrays);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001739 FastCloneShallowArrayStub::Mode mode = has_fast_elements
1740 ? FastCloneShallowArrayStub::CLONE_ELEMENTS
1741 : FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001742 FastCloneShallowArrayStub stub(mode, length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001743 __ CallStub(&stub);
1744 }
1745
1746 bool result_saved = false; // Is the result saved to the stack?
1747
1748 // Emit code to evaluate all the non-constant subexpressions and to store
1749 // them into the newly cloned array.
1750 for (int i = 0; i < length; i++) {
1751 Expression* subexpr = subexprs->at(i);
1752 // If the subexpression is a literal or a simple materialized literal it
1753 // is already set in the cloned array.
1754 if (subexpr->AsLiteral() != NULL ||
1755 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1756 continue;
1757 }
1758
1759 if (!result_saved) {
1760 __ push(v0);
1761 result_saved = true;
1762 }
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001763
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001764 VisitForAccumulatorValue(subexpr);
1765
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001766 if (constant_elements_kind == FAST_ELEMENTS) {
1767 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1768 __ lw(t2, MemOperand(sp)); // Copy of array literal.
1769 __ lw(a1, FieldMemOperand(t2, JSObject::kElementsOffset));
1770 __ sw(result_register(), FieldMemOperand(a1, offset));
1771 // Update the write barrier for the array store.
1772 __ RecordWriteField(a1, offset, result_register(), a2,
1773 kRAHasBeenSaved, kDontSaveFPRegs,
1774 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1775 } else {
1776 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1777 __ lw(a2, FieldMemOperand(a1, JSObject::kMapOffset));
1778 __ li(a3, Operand(Smi::FromInt(i)));
1779 __ li(t0, Operand(Smi::FromInt(expr->literal_index())));
1780 __ mov(a0, result_register());
1781 StoreArrayLiteralElementStub stub;
1782 __ CallStub(&stub);
1783 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001784
1785 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1786 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001787 if (result_saved) {
1788 context()->PlugTOS();
1789 } else {
1790 context()->Plug(v0);
1791 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001792}
1793
1794
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001795void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001796 Comment cmnt(masm_, "[ Assignment");
1797 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1798 // on the left-hand side.
1799 if (!expr->target()->IsValidLeftHandSide()) {
1800 VisitForEffect(expr->target());
1801 return;
1802 }
1803
1804 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001805 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001806 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1807 LhsKind assign_type = VARIABLE;
1808 Property* property = expr->target()->AsProperty();
1809 if (property != NULL) {
1810 assign_type = (property->key()->IsPropertyName())
1811 ? NAMED_PROPERTY
1812 : KEYED_PROPERTY;
1813 }
1814
1815 // Evaluate LHS expression.
1816 switch (assign_type) {
1817 case VARIABLE:
1818 // Nothing to do here.
1819 break;
1820 case NAMED_PROPERTY:
1821 if (expr->is_compound()) {
1822 // We need the receiver both on the stack and in the accumulator.
1823 VisitForAccumulatorValue(property->obj());
1824 __ push(result_register());
1825 } else {
1826 VisitForStackValue(property->obj());
1827 }
1828 break;
1829 case KEYED_PROPERTY:
1830 // We need the key and receiver on both the stack and in v0 and a1.
1831 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001832 VisitForStackValue(property->obj());
1833 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001834 __ lw(a1, MemOperand(sp, 0));
1835 __ push(v0);
1836 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001837 VisitForStackValue(property->obj());
1838 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001839 }
1840 break;
1841 }
1842
1843 // For compound assignments we need another deoptimization point after the
1844 // variable/property load.
1845 if (expr->is_compound()) {
1846 { AccumulatorValueContext context(this);
1847 switch (assign_type) {
1848 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001849 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001850 PrepareForBailout(expr->target(), TOS_REG);
1851 break;
1852 case NAMED_PROPERTY:
1853 EmitNamedPropertyLoad(property);
1854 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1855 break;
1856 case KEYED_PROPERTY:
1857 EmitKeyedPropertyLoad(property);
1858 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1859 break;
1860 }
1861 }
1862
1863 Token::Value op = expr->binary_op();
1864 __ push(v0); // Left operand goes on the stack.
1865 VisitForAccumulatorValue(expr->value());
1866
1867 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1868 ? OVERWRITE_RIGHT
1869 : NO_OVERWRITE;
1870 SetSourcePosition(expr->position() + 1);
1871 AccumulatorValueContext context(this);
1872 if (ShouldInlineSmiCase(op)) {
1873 EmitInlineSmiBinaryOp(expr->binary_operation(),
1874 op,
1875 mode,
1876 expr->target(),
1877 expr->value());
1878 } else {
1879 EmitBinaryOp(expr->binary_operation(), op, mode);
1880 }
1881
1882 // Deoptimization point in case the binary operation may have side effects.
1883 PrepareForBailout(expr->binary_operation(), TOS_REG);
1884 } else {
1885 VisitForAccumulatorValue(expr->value());
1886 }
1887
1888 // Record source position before possible IC call.
1889 SetSourcePosition(expr->position());
1890
1891 // Store the value.
1892 switch (assign_type) {
1893 case VARIABLE:
1894 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1895 expr->op());
1896 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1897 context()->Plug(v0);
1898 break;
1899 case NAMED_PROPERTY:
1900 EmitNamedPropertyAssignment(expr);
1901 break;
1902 case KEYED_PROPERTY:
1903 EmitKeyedPropertyAssignment(expr);
1904 break;
1905 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001906}
1907
1908
ager@chromium.org5c838252010-02-19 08:53:10 +00001909void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001910 SetSourcePosition(prop->position());
1911 Literal* key = prop->key()->AsLiteral();
1912 __ mov(a0, result_register());
1913 __ li(a2, Operand(key->handle()));
1914 // Call load IC. It has arguments receiver and property name a0 and a2.
1915 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001916 CallIC(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001917}
1918
1919
1920void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001921 SetSourcePosition(prop->position());
1922 __ mov(a0, result_register());
1923 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1924 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001925 CallIC(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001926}
1927
1928
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001929void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001930 Token::Value op,
1931 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001932 Expression* left_expr,
1933 Expression* right_expr) {
1934 Label done, smi_case, stub_call;
1935
1936 Register scratch1 = a2;
1937 Register scratch2 = a3;
1938
1939 // Get the arguments.
1940 Register left = a1;
1941 Register right = a0;
1942 __ pop(left);
1943 __ mov(a0, result_register());
1944
1945 // Perform combined smi check on both operands.
1946 __ Or(scratch1, left, Operand(right));
1947 STATIC_ASSERT(kSmiTag == 0);
1948 JumpPatchSite patch_site(masm_);
1949 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1950
1951 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001952 BinaryOpStub stub(op, mode);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00001953 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001954 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001955 __ jmp(&done);
1956
1957 __ bind(&smi_case);
1958 // Smi case. This code works the same way as the smi-smi case in the type
1959 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001960 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001961 switch (op) {
1962 case Token::SAR:
1963 __ Branch(&stub_call);
1964 __ GetLeastBitsFromSmi(scratch1, right, 5);
1965 __ srav(right, left, scratch1);
1966 __ And(v0, right, Operand(~kSmiTagMask));
1967 break;
1968 case Token::SHL: {
1969 __ Branch(&stub_call);
1970 __ SmiUntag(scratch1, left);
1971 __ GetLeastBitsFromSmi(scratch2, right, 5);
1972 __ sllv(scratch1, scratch1, scratch2);
1973 __ Addu(scratch2, scratch1, Operand(0x40000000));
1974 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1975 __ SmiTag(v0, scratch1);
1976 break;
1977 }
1978 case Token::SHR: {
1979 __ Branch(&stub_call);
1980 __ SmiUntag(scratch1, left);
1981 __ GetLeastBitsFromSmi(scratch2, right, 5);
1982 __ srlv(scratch1, scratch1, scratch2);
1983 __ And(scratch2, scratch1, 0xc0000000);
1984 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1985 __ SmiTag(v0, scratch1);
1986 break;
1987 }
1988 case Token::ADD:
1989 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1990 __ BranchOnOverflow(&stub_call, scratch1);
1991 break;
1992 case Token::SUB:
1993 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1994 __ BranchOnOverflow(&stub_call, scratch1);
1995 break;
1996 case Token::MUL: {
1997 __ SmiUntag(scratch1, right);
1998 __ Mult(left, scratch1);
1999 __ mflo(scratch1);
2000 __ mfhi(scratch2);
2001 __ sra(scratch1, scratch1, 31);
2002 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
2003 __ mflo(v0);
2004 __ Branch(&done, ne, v0, Operand(zero_reg));
2005 __ Addu(scratch2, right, left);
2006 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
2007 ASSERT(Smi::FromInt(0) == 0);
2008 __ mov(v0, zero_reg);
2009 break;
2010 }
2011 case Token::BIT_OR:
2012 __ Or(v0, left, Operand(right));
2013 break;
2014 case Token::BIT_AND:
2015 __ And(v0, left, Operand(right));
2016 break;
2017 case Token::BIT_XOR:
2018 __ Xor(v0, left, Operand(right));
2019 break;
2020 default:
2021 UNREACHABLE();
2022 }
2023
2024 __ bind(&done);
2025 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002026}
2027
2028
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002029void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
2030 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002031 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002032 __ mov(a0, result_register());
2033 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00002034 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002035 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002036 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002037 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002038 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002039}
2040
2041
ulan@chromium.org812308e2012-02-29 15:58:45 +00002042void FullCodeGenerator::EmitAssignment(Expression* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002043 // Invalid left-hand sides are rewritten to have a 'throw
2044 // ReferenceError' on the left-hand side.
2045 if (!expr->IsValidLeftHandSide()) {
2046 VisitForEffect(expr);
2047 return;
2048 }
2049
2050 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00002051 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002052 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2053 LhsKind assign_type = VARIABLE;
2054 Property* prop = expr->AsProperty();
2055 if (prop != NULL) {
2056 assign_type = (prop->key()->IsPropertyName())
2057 ? NAMED_PROPERTY
2058 : KEYED_PROPERTY;
2059 }
2060
2061 switch (assign_type) {
2062 case VARIABLE: {
2063 Variable* var = expr->AsVariableProxy()->var();
2064 EffectContext context(this);
2065 EmitVariableAssignment(var, Token::ASSIGN);
2066 break;
2067 }
2068 case NAMED_PROPERTY: {
2069 __ push(result_register()); // Preserve value.
2070 VisitForAccumulatorValue(prop->obj());
2071 __ mov(a1, result_register());
2072 __ pop(a0); // Restore value.
2073 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002074 Handle<Code> ic = is_classic_mode()
2075 ? isolate()->builtins()->StoreIC_Initialize()
2076 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002077 CallIC(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002078 break;
2079 }
2080 case KEYED_PROPERTY: {
2081 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00002082 VisitForStackValue(prop->obj());
2083 VisitForAccumulatorValue(prop->key());
2084 __ mov(a1, result_register());
2085 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002086 __ pop(a0); // Restore value.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002087 Handle<Code> ic = is_classic_mode()
2088 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2089 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002090 CallIC(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002091 break;
2092 }
2093 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002094 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002095}
2096
2097
2098void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002099 Token::Value op) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002100 if (var->IsUnallocated()) {
2101 // Global var, const, or let.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002102 __ mov(a0, result_register());
2103 __ li(a2, Operand(var->name()));
2104 __ lw(a1, GlobalObjectOperand());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002105 Handle<Code> ic = is_classic_mode()
2106 ? isolate()->builtins()->StoreIC_Initialize()
2107 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002108 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002109
2110 } else if (op == Token::INIT_CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002111 // Const initializers need a write barrier.
2112 ASSERT(!var->IsParameter()); // No const parameters.
2113 if (var->IsStackLocal()) {
2114 Label skip;
2115 __ lw(a1, StackOperand(var));
2116 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2117 __ Branch(&skip, ne, a1, Operand(t0));
2118 __ sw(result_register(), StackOperand(var));
2119 __ bind(&skip);
2120 } else {
2121 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
2122 // Like var declarations, const declarations are hoisted to function
2123 // scope. However, unlike var initializers, const initializers are
2124 // able to drill a hole to that function context, even from inside a
2125 // 'with' context. We thus bypass the normal static scope lookup for
2126 // var->IsContextSlot().
2127 __ push(v0);
2128 __ li(a0, Operand(var->name()));
2129 __ Push(cp, a0); // Context and name.
2130 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002131 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002132
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002133 } else if (var->mode() == LET && op != Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002134 // Non-initializing assignment to let variable needs a write barrier.
2135 if (var->IsLookupSlot()) {
2136 __ push(v0); // Value.
2137 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002138 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002139 __ Push(cp, a1, a0); // Context, name, strict mode.
2140 __ CallRuntime(Runtime::kStoreContextSlot, 4);
2141 } else {
2142 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
2143 Label assign;
2144 MemOperand location = VarOperand(var, a1);
2145 __ lw(a3, location);
2146 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2147 __ Branch(&assign, ne, a3, Operand(t0));
2148 __ li(a3, Operand(var->name()));
2149 __ push(a3);
2150 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2151 // Perform the assignment.
2152 __ bind(&assign);
2153 __ sw(result_register(), location);
2154 if (var->IsContextSlot()) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002155 // RecordWrite may destroy all its register arguments.
2156 __ mov(a3, result_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002157 int offset = Context::SlotOffset(var->index());
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002158 __ RecordWriteContextSlot(
2159 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002160 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002161 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002162
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002163 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2164 // Assignment to var or initializing assignment to let/const
2165 // in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002166 if (var->IsStackAllocated() || var->IsContextSlot()) {
2167 MemOperand location = VarOperand(var, a1);
2168 if (FLAG_debug_code && op == Token::INIT_LET) {
2169 // Check for an uninitialized let binding.
2170 __ lw(a2, location);
2171 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2172 __ Check(eq, "Let binding re-initialization.", a2, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002173 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002174 // Perform the assignment.
2175 __ sw(v0, location);
2176 if (var->IsContextSlot()) {
2177 __ mov(a3, v0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002178 int offset = Context::SlotOffset(var->index());
2179 __ RecordWriteContextSlot(
2180 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002181 }
2182 } else {
2183 ASSERT(var->IsLookupSlot());
2184 __ push(v0); // Value.
2185 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002186 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002187 __ Push(cp, a1, a0); // Context, name, strict mode.
2188 __ CallRuntime(Runtime::kStoreContextSlot, 4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002189 }
2190 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002191 // Non-initializing assignments to consts are ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +00002192}
2193
2194
2195void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002196 // Assignment to a property, using a named store IC.
2197 Property* prop = expr->target()->AsProperty();
2198 ASSERT(prop != NULL);
2199 ASSERT(prop->key()->AsLiteral() != NULL);
2200
2201 // If the assignment starts a block of assignments to the same object,
2202 // change to slow case to avoid the quadratic behavior of repeatedly
2203 // adding fast properties.
2204 if (expr->starts_initialization_block()) {
2205 __ push(result_register());
2206 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
2207 __ push(t0);
2208 __ CallRuntime(Runtime::kToSlowProperties, 1);
2209 __ pop(result_register());
2210 }
2211
2212 // Record source code position before IC call.
2213 SetSourcePosition(expr->position());
2214 __ mov(a0, result_register()); // Load the value.
2215 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
2216 // Load receiver to a1. Leave a copy in the stack if needed for turning the
2217 // receiver into fast case.
2218 if (expr->ends_initialization_block()) {
2219 __ lw(a1, MemOperand(sp));
2220 } else {
2221 __ pop(a1);
2222 }
2223
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002224 Handle<Code> ic = is_classic_mode()
2225 ? isolate()->builtins()->StoreIC_Initialize()
2226 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002227 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002228
2229 // If the assignment ends an initialization block, revert to fast case.
2230 if (expr->ends_initialization_block()) {
2231 __ push(v0); // Result of assignment, saved even if not needed.
2232 // Receiver is under the result value.
2233 __ lw(t0, MemOperand(sp, kPointerSize));
2234 __ push(t0);
2235 __ CallRuntime(Runtime::kToFastProperties, 1);
2236 __ pop(v0);
2237 __ Drop(1);
2238 }
2239 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2240 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002241}
2242
2243
2244void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002245 // Assignment to a property, using a keyed store IC.
2246
2247 // If the assignment starts a block of assignments to the same object,
2248 // change to slow case to avoid the quadratic behavior of repeatedly
2249 // adding fast properties.
2250 if (expr->starts_initialization_block()) {
2251 __ push(result_register());
2252 // Receiver is now under the key and value.
2253 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2254 __ push(t0);
2255 __ CallRuntime(Runtime::kToSlowProperties, 1);
2256 __ pop(result_register());
2257 }
2258
2259 // Record source code position before IC call.
2260 SetSourcePosition(expr->position());
2261 // Call keyed store IC.
2262 // The arguments are:
2263 // - a0 is the value,
2264 // - a1 is the key,
2265 // - a2 is the receiver.
2266 __ mov(a0, result_register());
2267 __ pop(a1); // Key.
2268 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2269 // receiver into fast case.
2270 if (expr->ends_initialization_block()) {
2271 __ lw(a2, MemOperand(sp));
2272 } else {
2273 __ pop(a2);
2274 }
2275
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002276 Handle<Code> ic = is_classic_mode()
2277 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2278 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002279 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002280
2281 // If the assignment ends an initialization block, revert to fast case.
2282 if (expr->ends_initialization_block()) {
2283 __ push(v0); // Result of assignment, saved even if not needed.
2284 // Receiver is under the result value.
2285 __ lw(t0, MemOperand(sp, kPointerSize));
2286 __ push(t0);
2287 __ CallRuntime(Runtime::kToFastProperties, 1);
2288 __ pop(v0);
2289 __ Drop(1);
2290 }
2291 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2292 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002293}
2294
2295
2296void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002297 Comment cmnt(masm_, "[ Property");
2298 Expression* key = expr->key();
2299
2300 if (key->IsPropertyName()) {
2301 VisitForAccumulatorValue(expr->obj());
2302 EmitNamedPropertyLoad(expr);
2303 context()->Plug(v0);
2304 } else {
2305 VisitForStackValue(expr->obj());
2306 VisitForAccumulatorValue(expr->key());
2307 __ pop(a1);
2308 EmitKeyedPropertyLoad(expr);
2309 context()->Plug(v0);
2310 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002311}
2312
lrn@chromium.org7516f052011-03-30 08:52:27 +00002313
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002314void FullCodeGenerator::CallIC(Handle<Code> code,
2315 RelocInfo::Mode rmode,
2316 unsigned ast_id) {
2317 ic_total_count_++;
2318 __ Call(code, rmode, ast_id);
2319}
2320
2321
ager@chromium.org5c838252010-02-19 08:53:10 +00002322void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002323 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002324 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002325 // Code common for calls using the IC.
2326 ZoneList<Expression*>* args = expr->arguments();
2327 int arg_count = args->length();
2328 { PreservePositionScope scope(masm()->positions_recorder());
2329 for (int i = 0; i < arg_count; i++) {
2330 VisitForStackValue(args->at(i));
2331 }
2332 __ li(a2, Operand(name));
2333 }
2334 // Record source position for debugger.
2335 SetSourcePosition(expr->position());
2336 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002337 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002338 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002339 CallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002340 RecordJSReturnSite(expr);
2341 // Restore context register.
2342 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2343 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002344}
2345
2346
lrn@chromium.org7516f052011-03-30 08:52:27 +00002347void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002348 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002349 // Load the key.
2350 VisitForAccumulatorValue(key);
2351
2352 // Swap the name of the function and the receiver on the stack to follow
2353 // the calling convention for call ICs.
2354 __ pop(a1);
2355 __ push(v0);
2356 __ push(a1);
2357
2358 // Code common for calls using the IC.
2359 ZoneList<Expression*>* args = expr->arguments();
2360 int arg_count = args->length();
2361 { PreservePositionScope scope(masm()->positions_recorder());
2362 for (int i = 0; i < arg_count; i++) {
2363 VisitForStackValue(args->at(i));
2364 }
2365 }
2366 // Record source position for debugger.
2367 SetSourcePosition(expr->position());
2368 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002369 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002370 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002371 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00002372 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002373 RecordJSReturnSite(expr);
2374 // Restore context register.
2375 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2376 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002377}
2378
2379
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002380void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002381 // Code common for calls using the call stub.
2382 ZoneList<Expression*>* args = expr->arguments();
2383 int arg_count = args->length();
2384 { PreservePositionScope scope(masm()->positions_recorder());
2385 for (int i = 0; i < arg_count; i++) {
2386 VisitForStackValue(args->at(i));
2387 }
2388 }
2389 // Record source position for debugger.
2390 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002391 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002392 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002393 __ CallStub(&stub);
2394 RecordJSReturnSite(expr);
2395 // Restore context register.
2396 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2397 context()->DropAndPlug(1, v0);
2398}
2399
2400
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002401void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002402 // Push copy of the first argument or undefined if it doesn't exist.
2403 if (arg_count > 0) {
2404 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2405 } else {
2406 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2407 }
2408 __ push(a1);
2409
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002410 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002411 int receiver_offset = 2 + info_->scope()->num_parameters();
2412 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002413 __ push(a1);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002414 // Push the language mode.
2415 __ li(a1, Operand(Smi::FromInt(language_mode())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002416 __ push(a1);
2417
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002418 // Push the start position of the scope the calls resides in.
2419 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
2420 __ push(a1);
2421
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002422 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002423 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org5c838252010-02-19 08:53:10 +00002424}
2425
2426
2427void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002428#ifdef DEBUG
2429 // We want to verify that RecordJSReturnSite gets called on all paths
2430 // through this function. Avoid early returns.
2431 expr->return_is_recorded_ = false;
2432#endif
2433
2434 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002435 Expression* callee = expr->expression();
2436 VariableProxy* proxy = callee->AsVariableProxy();
2437 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002438
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002439 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002440 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2441 // resolve the function we need to call and the receiver of the
2442 // call. Then we call the resolved function using the given
2443 // arguments.
2444 ZoneList<Expression*>* args = expr->arguments();
2445 int arg_count = args->length();
2446
2447 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002448 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002449 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2450 __ push(a2); // Reserved receiver slot.
2451
2452 // Push the arguments.
2453 for (int i = 0; i < arg_count; i++) {
2454 VisitForStackValue(args->at(i));
2455 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002456
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002457 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002458 // resolve eval.
2459 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2460 __ push(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002461 EmitResolvePossiblyDirectEval(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002462
2463 // The runtime call returns a pair of values in v0 (function) and
2464 // v1 (receiver). Touch up the stack with the right values.
2465 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2466 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2467 }
2468 // Record source position for debugger.
2469 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002470 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002471 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002472 __ CallStub(&stub);
2473 RecordJSReturnSite(expr);
2474 // Restore context register.
2475 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2476 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002477 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002478 // Push global object as receiver for the call IC.
2479 __ lw(a0, GlobalObjectOperand());
2480 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002481 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2482 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002483 // Call to a lookup slot (dynamically introduced variable).
2484 Label slow, done;
2485
2486 { PreservePositionScope scope(masm()->positions_recorder());
2487 // Generate code for loading from variables potentially shadowed
2488 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002489 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002490 }
2491
2492 __ bind(&slow);
2493 // Call the runtime to find the function to call (returned in v0)
2494 // and the object holding it (returned in v1).
2495 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002496 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002497 __ push(a2);
2498 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2499 __ Push(v0, v1); // Function, receiver.
2500
2501 // If fast case code has been generated, emit code to push the
2502 // function and receiver and have the slow path jump around this
2503 // code.
2504 if (done.is_linked()) {
2505 Label call;
2506 __ Branch(&call);
2507 __ bind(&done);
2508 // Push function.
2509 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002510 // The receiver is implicitly the global receiver. Indicate this
2511 // by passing the hole to the call function stub.
2512 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002513 __ push(a1);
2514 __ bind(&call);
2515 }
2516
danno@chromium.org40cb8782011-05-25 07:58:50 +00002517 // The receiver is either the global receiver or an object found
2518 // by LoadContextSlot. That object could be the hole if the
2519 // receiver is implicitly the global object.
2520 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002521 } else if (property != NULL) {
2522 { PreservePositionScope scope(masm()->positions_recorder());
2523 VisitForStackValue(property->obj());
2524 }
2525 if (property->key()->IsPropertyName()) {
2526 EmitCallWithIC(expr,
2527 property->key()->AsLiteral()->handle(),
2528 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002529 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002530 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002531 }
2532 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002533 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002534 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002535 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002536 }
2537 // Load global receiver object.
2538 __ lw(a1, GlobalObjectOperand());
2539 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2540 __ push(a1);
2541 // Emit function call.
2542 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2543 }
2544
2545#ifdef DEBUG
2546 // RecordJSReturnSite should have been called.
2547 ASSERT(expr->return_is_recorded_);
2548#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002549}
2550
2551
2552void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002553 Comment cmnt(masm_, "[ CallNew");
2554 // According to ECMA-262, section 11.2.2, page 44, the function
2555 // expression in new calls must be evaluated before the
2556 // arguments.
2557
2558 // Push constructor on the stack. If it's not a function it's used as
2559 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2560 // ignored.
2561 VisitForStackValue(expr->expression());
2562
2563 // Push the arguments ("left-to-right") on the stack.
2564 ZoneList<Expression*>* args = expr->arguments();
2565 int arg_count = args->length();
2566 for (int i = 0; i < arg_count; i++) {
2567 VisitForStackValue(args->at(i));
2568 }
2569
2570 // Call the construct call builtin that handles allocation and
2571 // constructor invocation.
2572 SetSourcePosition(expr->position());
2573
2574 // Load function and argument count into a1 and a0.
2575 __ li(a0, Operand(arg_count));
2576 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2577
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002578 // Record call targets in unoptimized code, but not in the snapshot.
2579 CallFunctionFlags flags;
2580 if (!Serializer::enabled()) {
2581 flags = RECORD_CALL_TARGET;
2582 Handle<Object> uninitialized =
2583 TypeFeedbackCells::UninitializedSentinel(isolate());
2584 Handle<JSGlobalPropertyCell> cell =
2585 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
2586 RecordTypeFeedbackCell(expr->id(), cell);
2587 __ li(a2, Operand(cell));
2588 } else {
2589 flags = NO_CALL_FUNCTION_FLAGS;
2590 }
2591
2592 CallConstructStub stub(flags);
2593 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
ulan@chromium.org967e2702012-02-28 09:49:15 +00002594 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002595 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002596}
2597
2598
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002599void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2600 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002601 ASSERT(args->length() == 1);
2602
2603 VisitForAccumulatorValue(args->at(0));
2604
2605 Label materialize_true, materialize_false;
2606 Label* if_true = NULL;
2607 Label* if_false = NULL;
2608 Label* fall_through = NULL;
2609 context()->PrepareTest(&materialize_true, &materialize_false,
2610 &if_true, &if_false, &fall_through);
2611
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002612 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002613 __ And(t0, v0, Operand(kSmiTagMask));
2614 Split(eq, t0, 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
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002620void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2621 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002622 ASSERT(args->length() == 1);
2623
2624 VisitForAccumulatorValue(args->at(0));
2625
2626 Label materialize_true, materialize_false;
2627 Label* if_true = NULL;
2628 Label* if_false = NULL;
2629 Label* fall_through = NULL;
2630 context()->PrepareTest(&materialize_true, &materialize_false,
2631 &if_true, &if_false, &fall_through);
2632
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002633 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002634 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2635 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2636
2637 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002638}
2639
2640
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002641void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2642 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002643 ASSERT(args->length() == 1);
2644
2645 VisitForAccumulatorValue(args->at(0));
2646
2647 Label materialize_true, materialize_false;
2648 Label* if_true = NULL;
2649 Label* if_false = NULL;
2650 Label* fall_through = NULL;
2651 context()->PrepareTest(&materialize_true, &materialize_false,
2652 &if_true, &if_false, &fall_through);
2653
2654 __ JumpIfSmi(v0, if_false);
2655 __ LoadRoot(at, Heap::kNullValueRootIndex);
2656 __ Branch(if_true, eq, v0, Operand(at));
2657 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2658 // Undetectable objects behave like undefined when tested with typeof.
2659 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2660 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2661 __ Branch(if_false, ne, at, Operand(zero_reg));
2662 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002663 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002664 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002665 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2666 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002667
2668 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002669}
2670
2671
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002672void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2673 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002674 ASSERT(args->length() == 1);
2675
2676 VisitForAccumulatorValue(args->at(0));
2677
2678 Label materialize_true, materialize_false;
2679 Label* if_true = NULL;
2680 Label* if_false = NULL;
2681 Label* fall_through = NULL;
2682 context()->PrepareTest(&materialize_true, &materialize_false,
2683 &if_true, &if_false, &fall_through);
2684
2685 __ JumpIfSmi(v0, if_false);
2686 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002687 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002688 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002689 if_true, if_false, fall_through);
2690
2691 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002692}
2693
2694
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002695void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2696 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002697 ASSERT(args->length() == 1);
2698
2699 VisitForAccumulatorValue(args->at(0));
2700
2701 Label materialize_true, materialize_false;
2702 Label* if_true = NULL;
2703 Label* if_false = NULL;
2704 Label* fall_through = NULL;
2705 context()->PrepareTest(&materialize_true, &materialize_false,
2706 &if_true, &if_false, &fall_through);
2707
2708 __ JumpIfSmi(v0, if_false);
2709 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2710 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2711 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002712 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002713 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2714
2715 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002716}
2717
2718
2719void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002720 CallRuntime* expr) {
2721 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002722 ASSERT(args->length() == 1);
2723
2724 VisitForAccumulatorValue(args->at(0));
2725
2726 Label materialize_true, materialize_false;
2727 Label* if_true = NULL;
2728 Label* if_false = NULL;
2729 Label* fall_through = NULL;
2730 context()->PrepareTest(&materialize_true, &materialize_false,
2731 &if_true, &if_false, &fall_through);
2732
2733 if (FLAG_debug_code) __ AbortIfSmi(v0);
2734
2735 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2736 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2737 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2738 __ Branch(if_true, ne, t0, Operand(zero_reg));
2739
2740 // Check for fast case object. Generate false result for slow case object.
2741 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2742 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2743 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2744 __ Branch(if_false, eq, a2, Operand(t0));
2745
2746 // Look for valueOf symbol in the descriptor array, and indicate false if
2747 // found. The type is not checked, so if it is a transition it is a false
2748 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002749 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002750 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2751 // t0: descriptor array
2752 // a3: length of descriptor array
2753 // Calculate the end of the descriptor array.
2754 STATIC_ASSERT(kSmiTag == 0);
2755 STATIC_ASSERT(kSmiTagSize == 1);
2756 STATIC_ASSERT(kPointerSize == 4);
2757 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2758 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2759 __ Addu(a2, a2, t1);
2760
2761 // Calculate location of the first key name.
2762 __ Addu(t0,
2763 t0,
2764 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2765 DescriptorArray::kFirstIndex * kPointerSize));
2766 // Loop through all the keys in the descriptor array. If one of these is the
2767 // symbol valueOf the result is false.
2768 Label entry, loop;
2769 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2770 // used in the loop below.
danno@chromium.org88aa0582012-03-23 15:11:57 +00002771 __ LoadRoot(t2, Heap::kvalue_of_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002772 __ jmp(&entry);
2773 __ bind(&loop);
2774 __ lw(a3, MemOperand(t0, 0));
2775 __ Branch(if_false, eq, a3, Operand(t2));
2776 __ Addu(t0, t0, Operand(kPointerSize));
2777 __ bind(&entry);
2778 __ Branch(&loop, ne, t0, Operand(a2));
2779
2780 // If a valueOf property is not found on the object check that it's
2781 // prototype is the un-modified String prototype. If not result is false.
2782 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2783 __ JumpIfSmi(a2, if_false);
2784 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2785 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2786 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2787 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2788 __ Branch(if_false, ne, a2, Operand(a3));
2789
2790 // Set the bit in the map to indicate that it has been checked safe for
2791 // default valueOf and set true result.
2792 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2793 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2794 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2795 __ jmp(if_true);
2796
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002797 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002798 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::EmitIsFunction(CallRuntime* expr) {
2803 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002804 ASSERT(args->length() == 1);
2805
2806 VisitForAccumulatorValue(args->at(0));
2807
2808 Label materialize_true, materialize_false;
2809 Label* if_true = NULL;
2810 Label* if_false = NULL;
2811 Label* fall_through = NULL;
2812 context()->PrepareTest(&materialize_true, &materialize_false,
2813 &if_true, &if_false, &fall_through);
2814
2815 __ JumpIfSmi(v0, if_false);
2816 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002817 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002818 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2819 __ Branch(if_false);
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::EmitIsArray(CallRuntime* expr) {
2826 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002827 ASSERT(args->length() == 1);
2828
2829 VisitForAccumulatorValue(args->at(0));
2830
2831 Label materialize_true, materialize_false;
2832 Label* if_true = NULL;
2833 Label* if_false = NULL;
2834 Label* fall_through = NULL;
2835 context()->PrepareTest(&materialize_true, &materialize_false,
2836 &if_true, &if_false, &fall_through);
2837
2838 __ JumpIfSmi(v0, if_false);
2839 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002840 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002841 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2842 if_true, if_false, fall_through);
2843
2844 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002845}
2846
2847
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002848void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2849 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002850 ASSERT(args->length() == 1);
2851
2852 VisitForAccumulatorValue(args->at(0));
2853
2854 Label materialize_true, materialize_false;
2855 Label* if_true = NULL;
2856 Label* if_false = NULL;
2857 Label* fall_through = NULL;
2858 context()->PrepareTest(&materialize_true, &materialize_false,
2859 &if_true, &if_false, &fall_through);
2860
2861 __ JumpIfSmi(v0, if_false);
2862 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002863 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002864 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2865
2866 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002867}
2868
2869
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002870void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2871 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002872
2873 Label materialize_true, materialize_false;
2874 Label* if_true = NULL;
2875 Label* if_false = NULL;
2876 Label* fall_through = NULL;
2877 context()->PrepareTest(&materialize_true, &materialize_false,
2878 &if_true, &if_false, &fall_through);
2879
2880 // Get the frame pointer for the calling frame.
2881 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2882
2883 // Skip the arguments adaptor frame if it exists.
2884 Label check_frame_marker;
2885 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2886 __ Branch(&check_frame_marker, ne,
2887 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2888 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2889
2890 // Check the marker in the calling frame.
2891 __ bind(&check_frame_marker);
2892 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002893 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002894 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2895 if_true, if_false, fall_through);
2896
2897 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002898}
2899
2900
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002901void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2902 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002903 ASSERT(args->length() == 2);
2904
2905 // Load the two objects into registers and perform the comparison.
2906 VisitForStackValue(args->at(0));
2907 VisitForAccumulatorValue(args->at(1));
2908
2909 Label materialize_true, materialize_false;
2910 Label* if_true = NULL;
2911 Label* if_false = NULL;
2912 Label* fall_through = NULL;
2913 context()->PrepareTest(&materialize_true, &materialize_false,
2914 &if_true, &if_false, &fall_through);
2915
2916 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002917 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002918 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2919
2920 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002921}
2922
2923
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002924void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2925 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002926 ASSERT(args->length() == 1);
2927
2928 // ArgumentsAccessStub expects the key in a1 and the formal
2929 // parameter count in a0.
2930 VisitForAccumulatorValue(args->at(0));
2931 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002932 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002933 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2934 __ CallStub(&stub);
2935 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002936}
2937
2938
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002939void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2940 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002941 Label exit;
2942 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002943 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002944
2945 // Check if the calling frame is an arguments adaptor frame.
2946 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2947 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2948 __ Branch(&exit, ne, a3,
2949 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2950
2951 // Arguments adaptor case: Read the arguments length from the
2952 // adaptor frame.
2953 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2954
2955 __ bind(&exit);
2956 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002957}
2958
2959
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002960void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2961 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002962 ASSERT(args->length() == 1);
2963 Label done, null, function, non_function_constructor;
2964
2965 VisitForAccumulatorValue(args->at(0));
2966
2967 // If the object is a smi, we return null.
2968 __ JumpIfSmi(v0, &null);
2969
2970 // Check that the object is a JS object but take special care of JS
2971 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002972 // Assume that there are only two callable types, and one of them is at
2973 // either end of the type range for JS object types. Saves extra comparisons.
2974 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002975 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002976 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002977
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002978 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2979 FIRST_SPEC_OBJECT_TYPE + 1);
2980 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002981
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002982 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2983 LAST_SPEC_OBJECT_TYPE - 1);
2984 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2985 // Assume that there is no larger type.
2986 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2987
2988 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002989 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2990 __ GetObjectType(v0, a1, a1);
2991 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2992
2993 // v0 now contains the constructor function. Grab the
2994 // instance class name from there.
2995 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2996 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2997 __ Branch(&done);
2998
2999 // Functions have class 'Function'.
3000 __ bind(&function);
3001 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
3002 __ jmp(&done);
3003
3004 // Objects with a non-function constructor have class 'Object'.
3005 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00003006 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003007 __ jmp(&done);
3008
3009 // Non-JS objects have class null.
3010 __ bind(&null);
3011 __ LoadRoot(v0, Heap::kNullValueRootIndex);
3012
3013 // All done.
3014 __ bind(&done);
3015
3016 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003017}
3018
3019
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003020void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003021 // Conditionally generate a log call.
3022 // Args:
3023 // 0 (literal string): The type of logging (corresponds to the flags).
3024 // This is used to determine whether or not to generate the log call.
3025 // 1 (string): Format string. Access the string at argument index 2
3026 // with '%2s' (see Logger::LogRuntime for all the formats).
3027 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003028 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003029 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003030 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
3031 VisitForStackValue(args->at(1));
3032 VisitForStackValue(args->at(2));
3033 __ CallRuntime(Runtime::kLog, 2);
3034 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003035
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003036 // Finally, we're expected to leave a value on the top of the stack.
3037 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3038 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003039}
3040
3041
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003042void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
3043 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003044 Label slow_allocate_heapnumber;
3045 Label heapnumber_allocated;
3046
3047 // Save the new heap number in callee-saved register s0, since
3048 // we call out to external C code below.
3049 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
3050 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
3051 __ jmp(&heapnumber_allocated);
3052
3053 __ bind(&slow_allocate_heapnumber);
3054
3055 // Allocate a heap number.
3056 __ CallRuntime(Runtime::kNumberAlloc, 0);
3057 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
3058
3059 __ bind(&heapnumber_allocated);
3060
3061 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
3062 // by computing:
3063 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
3064 if (CpuFeatures::IsSupported(FPU)) {
3065 __ PrepareCallCFunction(1, a0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003066 __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
3067 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003068 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
3069
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003070 CpuFeatures::Scope scope(FPU);
3071 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
3072 __ li(a1, Operand(0x41300000));
3073 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00003074 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003075 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00003076 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003077 // Subtract and store the result in the heap number.
3078 __ sub_d(f0, f12, f14);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003079 __ sdc1(f0, FieldMemOperand(s0, HeapNumber::kValueOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003080 __ mov(v0, s0);
3081 } else {
3082 __ PrepareCallCFunction(2, a0);
3083 __ mov(a0, s0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003084 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
3085 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003086 __ CallCFunction(
3087 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
3088 }
3089
3090 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003091}
3092
3093
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003094void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003095 // Load the arguments on the stack and call the stub.
3096 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003097 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003098 ASSERT(args->length() == 3);
3099 VisitForStackValue(args->at(0));
3100 VisitForStackValue(args->at(1));
3101 VisitForStackValue(args->at(2));
3102 __ CallStub(&stub);
3103 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003104}
3105
3106
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003107void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003108 // Load the arguments on the stack and call the stub.
3109 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003110 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003111 ASSERT(args->length() == 4);
3112 VisitForStackValue(args->at(0));
3113 VisitForStackValue(args->at(1));
3114 VisitForStackValue(args->at(2));
3115 VisitForStackValue(args->at(3));
3116 __ CallStub(&stub);
3117 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003118}
3119
3120
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003121void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3122 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003123 ASSERT(args->length() == 1);
3124
3125 VisitForAccumulatorValue(args->at(0)); // Load the object.
3126
3127 Label done;
3128 // If the object is a smi return the object.
3129 __ JumpIfSmi(v0, &done);
3130 // If the object is not a value type, return the object.
3131 __ GetObjectType(v0, a1, a1);
3132 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
3133
3134 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
3135
3136 __ bind(&done);
3137 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003138}
3139
3140
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003141void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3142 ZoneList<Expression*>* args = expr->arguments();
3143 ASSERT(args->length() == 2);
3144 ASSERT_NE(NULL, args->at(1)->AsLiteral());
3145 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->handle()));
3146
3147 VisitForAccumulatorValue(args->at(0)); // Load the object.
3148
3149 Label runtime, done;
3150 Register object = v0;
3151 Register result = v0;
3152 Register scratch0 = t5;
3153 Register scratch1 = a1;
3154
3155#ifdef DEBUG
3156 __ AbortIfSmi(object);
3157 __ GetObjectType(object, scratch1, scratch1);
3158 __ Assert(eq, "Trying to get date field from non-date.",
3159 scratch1, Operand(JS_DATE_TYPE));
3160#endif
3161
3162 if (index->value() == 0) {
3163 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset));
3164 } else {
3165 if (index->value() < JSDate::kFirstUncachedField) {
3166 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3167 __ li(scratch1, Operand(stamp));
3168 __ lw(scratch1, MemOperand(scratch1));
3169 __ lw(scratch0, FieldMemOperand(object, JSDate::kCacheStampOffset));
3170 __ Branch(&runtime, ne, scratch1, Operand(scratch0));
3171 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset +
3172 kPointerSize * index->value()));
3173 __ jmp(&done);
3174 }
3175 __ bind(&runtime);
3176 __ PrepareCallCFunction(2, scratch1);
3177 __ li(a1, Operand(index));
3178 __ Move(a0, object);
3179 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
3180 __ bind(&done);
3181 }
3182
3183 context()->Plug(v0);
3184}
3185
3186
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003187void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003188 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003189 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003190 ASSERT(args->length() == 2);
3191 VisitForStackValue(args->at(0));
3192 VisitForStackValue(args->at(1));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003193 if (CpuFeatures::IsSupported(FPU)) {
3194 MathPowStub stub(MathPowStub::ON_STACK);
3195 __ CallStub(&stub);
3196 } else {
3197 __ CallRuntime(Runtime::kMath_pow, 2);
3198 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003199 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003200}
3201
3202
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003203void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3204 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003205 ASSERT(args->length() == 2);
3206
3207 VisitForStackValue(args->at(0)); // Load the object.
3208 VisitForAccumulatorValue(args->at(1)); // Load the value.
3209 __ pop(a1); // v0 = value. a1 = object.
3210
3211 Label done;
3212 // If the object is a smi, return the value.
3213 __ JumpIfSmi(a1, &done);
3214
3215 // If the object is not a value type, return the value.
3216 __ GetObjectType(a1, a2, a2);
3217 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
3218
3219 // Store the value.
3220 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
3221 // Update the write barrier. Save the value as it will be
3222 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003223 __ mov(a2, v0);
3224 __ RecordWriteField(
3225 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003226
3227 __ bind(&done);
3228 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003229}
3230
3231
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003232void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3233 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003234 ASSERT_EQ(args->length(), 1);
3235
3236 // Load the argument on the stack and call the stub.
3237 VisitForStackValue(args->at(0));
3238
3239 NumberToStringStub stub;
3240 __ CallStub(&stub);
3241 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003242}
3243
3244
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003245void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3246 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003247 ASSERT(args->length() == 1);
3248
3249 VisitForAccumulatorValue(args->at(0));
3250
3251 Label done;
3252 StringCharFromCodeGenerator generator(v0, a1);
3253 generator.GenerateFast(masm_);
3254 __ jmp(&done);
3255
3256 NopRuntimeCallHelper call_helper;
3257 generator.GenerateSlow(masm_, call_helper);
3258
3259 __ bind(&done);
3260 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003261}
3262
3263
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003264void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3265 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003266 ASSERT(args->length() == 2);
3267
3268 VisitForStackValue(args->at(0));
3269 VisitForAccumulatorValue(args->at(1));
3270 __ mov(a0, result_register());
3271
3272 Register object = a1;
3273 Register index = a0;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003274 Register result = v0;
3275
3276 __ pop(object);
3277
3278 Label need_conversion;
3279 Label index_out_of_range;
3280 Label done;
3281 StringCharCodeAtGenerator generator(object,
3282 index,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003283 result,
3284 &need_conversion,
3285 &need_conversion,
3286 &index_out_of_range,
3287 STRING_INDEX_IS_NUMBER);
3288 generator.GenerateFast(masm_);
3289 __ jmp(&done);
3290
3291 __ bind(&index_out_of_range);
3292 // When the index is out of range, the spec requires us to return
3293 // NaN.
3294 __ LoadRoot(result, Heap::kNanValueRootIndex);
3295 __ jmp(&done);
3296
3297 __ bind(&need_conversion);
3298 // Load the undefined value into the result register, which will
3299 // trigger conversion.
3300 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3301 __ jmp(&done);
3302
3303 NopRuntimeCallHelper call_helper;
3304 generator.GenerateSlow(masm_, call_helper);
3305
3306 __ bind(&done);
3307 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003308}
3309
3310
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003311void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3312 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003313 ASSERT(args->length() == 2);
3314
3315 VisitForStackValue(args->at(0));
3316 VisitForAccumulatorValue(args->at(1));
3317 __ mov(a0, result_register());
3318
3319 Register object = a1;
3320 Register index = a0;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003321 Register scratch = a3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003322 Register result = v0;
3323
3324 __ pop(object);
3325
3326 Label need_conversion;
3327 Label index_out_of_range;
3328 Label done;
3329 StringCharAtGenerator generator(object,
3330 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003331 scratch,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003332 result,
3333 &need_conversion,
3334 &need_conversion,
3335 &index_out_of_range,
3336 STRING_INDEX_IS_NUMBER);
3337 generator.GenerateFast(masm_);
3338 __ jmp(&done);
3339
3340 __ bind(&index_out_of_range);
3341 // When the index is out of range, the spec requires us to return
3342 // the empty string.
3343 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3344 __ jmp(&done);
3345
3346 __ bind(&need_conversion);
3347 // Move smi zero into the result register, which will trigger
3348 // conversion.
3349 __ li(result, Operand(Smi::FromInt(0)));
3350 __ jmp(&done);
3351
3352 NopRuntimeCallHelper call_helper;
3353 generator.GenerateSlow(masm_, call_helper);
3354
3355 __ bind(&done);
3356 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003357}
3358
3359
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003360void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3361 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003362 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003363 VisitForStackValue(args->at(0));
3364 VisitForStackValue(args->at(1));
3365
3366 StringAddStub stub(NO_STRING_ADD_FLAGS);
3367 __ CallStub(&stub);
3368 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003369}
3370
3371
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003372void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3373 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003374 ASSERT_EQ(2, args->length());
3375
3376 VisitForStackValue(args->at(0));
3377 VisitForStackValue(args->at(1));
3378
3379 StringCompareStub stub;
3380 __ CallStub(&stub);
3381 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003382}
3383
3384
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003385void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003386 // Load the argument on the stack and call the stub.
3387 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3388 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003389 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003390 ASSERT(args->length() == 1);
3391 VisitForStackValue(args->at(0));
3392 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3393 __ CallStub(&stub);
3394 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003395}
3396
3397
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003398void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003399 // Load the argument on the stack and call the stub.
3400 TranscendentalCacheStub stub(TranscendentalCache::COS,
3401 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003402 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003403 ASSERT(args->length() == 1);
3404 VisitForStackValue(args->at(0));
3405 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3406 __ CallStub(&stub);
3407 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003408}
3409
3410
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003411void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3412 // Load the argument on the stack and call the stub.
3413 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3414 TranscendentalCacheStub::TAGGED);
3415 ZoneList<Expression*>* args = expr->arguments();
3416 ASSERT(args->length() == 1);
3417 VisitForStackValue(args->at(0));
3418 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3419 __ CallStub(&stub);
3420 context()->Plug(v0);
3421}
3422
3423
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003424void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003425 // Load the argument on the stack and call the stub.
3426 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3427 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003428 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003429 ASSERT(args->length() == 1);
3430 VisitForStackValue(args->at(0));
3431 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3432 __ CallStub(&stub);
3433 context()->Plug(v0);
3434}
3435
3436
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003437void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003438 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003439 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003440 ASSERT(args->length() == 1);
3441 VisitForStackValue(args->at(0));
3442 __ CallRuntime(Runtime::kMath_sqrt, 1);
3443 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003444}
3445
3446
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003447void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3448 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003449 ASSERT(args->length() >= 2);
3450
3451 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3452 for (int i = 0; i < arg_count + 1; i++) {
3453 VisitForStackValue(args->at(i));
3454 }
3455 VisitForAccumulatorValue(args->last()); // Function.
3456
danno@chromium.orgc612e022011-11-10 11:38:15 +00003457 // Check for proxy.
3458 Label proxy, done;
3459 __ GetObjectType(v0, a1, a1);
3460 __ Branch(&proxy, eq, a1, Operand(JS_FUNCTION_PROXY_TYPE));
3461
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003462 // InvokeFunction requires the function in a1. Move it in there.
3463 __ mov(a1, result_register());
3464 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003465 __ InvokeFunction(a1, count, CALL_FUNCTION,
3466 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003467 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003468 __ jmp(&done);
3469
3470 __ bind(&proxy);
3471 __ push(v0);
3472 __ CallRuntime(Runtime::kCall, args->length());
3473 __ bind(&done);
3474
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003475 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003476}
3477
3478
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003479void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003480 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003481 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003482 ASSERT(args->length() == 3);
3483 VisitForStackValue(args->at(0));
3484 VisitForStackValue(args->at(1));
3485 VisitForStackValue(args->at(2));
3486 __ CallStub(&stub);
3487 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003488}
3489
3490
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003491void FullCodeGenerator::EmitSwapElements(CallRuntime* expr) {
3492 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003493 ASSERT(args->length() == 3);
3494 VisitForStackValue(args->at(0));
3495 VisitForStackValue(args->at(1));
3496 VisitForStackValue(args->at(2));
3497 Label done;
3498 Label slow_case;
3499 Register object = a0;
3500 Register index1 = a1;
3501 Register index2 = a2;
3502 Register elements = a3;
3503 Register scratch1 = t0;
3504 Register scratch2 = t1;
3505
3506 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3507 // Fetch the map and check if array is in fast case.
3508 // Check that object doesn't require security checks and
3509 // has no indexed interceptor.
3510 __ GetObjectType(object, scratch1, scratch2);
3511 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3512 // Map is now in scratch1.
3513
3514 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3515 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3516 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3517
3518 // Check the object's elements are in fast case and writable.
3519 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3520 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3521 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3522 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3523
3524 // Check that both indices are smis.
3525 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3526 __ lw(index2, MemOperand(sp, 0));
3527 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3528
3529 // Check that both indices are valid.
3530 Label not_hi;
3531 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3532 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3533 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3534 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3535 __ bind(&not_hi);
3536
3537 // Bring the address of the elements into index1 and index2.
3538 __ Addu(scratch1, elements,
3539 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3540 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3541 __ Addu(index1, scratch1, index1);
3542 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3543 __ Addu(index2, scratch1, index2);
3544
3545 // Swap elements.
3546 __ lw(scratch1, MemOperand(index1, 0));
3547 __ lw(scratch2, MemOperand(index2, 0));
3548 __ sw(scratch1, MemOperand(index2, 0));
3549 __ sw(scratch2, MemOperand(index1, 0));
3550
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003551 Label no_remembered_set;
3552 __ CheckPageFlag(elements,
3553 scratch1,
3554 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3555 ne,
3556 &no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003557 // Possible optimization: do a check that both values are Smis
3558 // (or them and test against Smi mask).
3559
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003560 // We are swapping two objects in an array and the incremental marker never
3561 // pauses in the middle of scanning a single object. Therefore the
3562 // incremental marker is not disturbed, so we don't need to call the
3563 // RecordWrite stub that notifies the incremental marker.
3564 __ RememberedSetHelper(elements,
3565 index1,
3566 scratch2,
3567 kDontSaveFPRegs,
3568 MacroAssembler::kFallThroughAtEnd);
3569 __ RememberedSetHelper(elements,
3570 index2,
3571 scratch2,
3572 kDontSaveFPRegs,
3573 MacroAssembler::kFallThroughAtEnd);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003574
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003575 __ bind(&no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003576 // We are done. Drop elements from the stack, and return undefined.
3577 __ Drop(3);
3578 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3579 __ jmp(&done);
3580
3581 __ bind(&slow_case);
3582 __ CallRuntime(Runtime::kSwapElements, 3);
3583
3584 __ bind(&done);
3585 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003586}
3587
3588
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003589void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3590 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003591 ASSERT_EQ(2, args->length());
3592
3593 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3594 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3595
3596 Handle<FixedArray> jsfunction_result_caches(
3597 isolate()->global_context()->jsfunction_result_caches());
3598 if (jsfunction_result_caches->length() <= cache_id) {
3599 __ Abort("Attempt to use undefined cache.");
3600 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3601 context()->Plug(v0);
3602 return;
3603 }
3604
3605 VisitForAccumulatorValue(args->at(1));
3606
3607 Register key = v0;
3608 Register cache = a1;
3609 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3610 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3611 __ lw(cache,
3612 ContextOperand(
3613 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3614 __ lw(cache,
3615 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3616
3617
3618 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003619 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003620 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3621 // a2 now holds finger offset as a smi.
3622 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3623 // a3 now points to the start of fixed array elements.
3624 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3625 __ addu(a3, a3, at);
3626 // a3 now points to key of indexed element of cache.
3627 __ lw(a2, MemOperand(a3));
3628 __ Branch(&not_found, ne, key, Operand(a2));
3629
3630 __ lw(v0, MemOperand(a3, kPointerSize));
3631 __ Branch(&done);
3632
3633 __ bind(&not_found);
3634 // Call runtime to perform the lookup.
3635 __ Push(cache, key);
3636 __ CallRuntime(Runtime::kGetFromCache, 2);
3637
3638 __ bind(&done);
3639 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003640}
3641
3642
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003643void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3644 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003645 ASSERT_EQ(2, args->length());
3646
3647 Register right = v0;
3648 Register left = a1;
3649 Register tmp = a2;
3650 Register tmp2 = a3;
3651
3652 VisitForStackValue(args->at(0));
3653 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3654 __ pop(left);
3655
3656 Label done, fail, ok;
3657 __ Branch(&ok, eq, left, Operand(right));
3658 // Fail if either is a non-HeapObject.
3659 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003660 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003661 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3662 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3663 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3664 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3665 __ Branch(&fail, ne, tmp, Operand(tmp2));
3666 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3667 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3668 __ Branch(&ok, eq, tmp, Operand(tmp2));
3669 __ bind(&fail);
3670 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3671 __ jmp(&done);
3672 __ bind(&ok);
3673 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3674 __ bind(&done);
3675
3676 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003677}
3678
3679
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003680void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3681 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003682 VisitForAccumulatorValue(args->at(0));
3683
3684 Label materialize_true, materialize_false;
3685 Label* if_true = NULL;
3686 Label* if_false = NULL;
3687 Label* fall_through = NULL;
3688 context()->PrepareTest(&materialize_true, &materialize_false,
3689 &if_true, &if_false, &fall_through);
3690
3691 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3692 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3693
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003694 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003695 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3696
3697 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003698}
3699
3700
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003701void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3702 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003703 ASSERT(args->length() == 1);
3704 VisitForAccumulatorValue(args->at(0));
3705
3706 if (FLAG_debug_code) {
3707 __ AbortIfNotString(v0);
3708 }
3709
3710 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3711 __ IndexFromHash(v0, v0);
3712
3713 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003714}
3715
3716
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003717void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003718 Label bailout, done, one_char_separator, long_separator,
3719 non_trivial_array, not_size_one_array, loop,
3720 empty_separator_loop, one_char_separator_loop,
3721 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003722 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003723 ASSERT(args->length() == 2);
3724 VisitForStackValue(args->at(1));
3725 VisitForAccumulatorValue(args->at(0));
3726
3727 // All aliases of the same register have disjoint lifetimes.
3728 Register array = v0;
3729 Register elements = no_reg; // Will be v0.
3730 Register result = no_reg; // Will be v0.
3731 Register separator = a1;
3732 Register array_length = a2;
3733 Register result_pos = no_reg; // Will be a2.
3734 Register string_length = a3;
3735 Register string = t0;
3736 Register element = t1;
3737 Register elements_end = t2;
3738 Register scratch1 = t3;
3739 Register scratch2 = t5;
3740 Register scratch3 = t4;
3741 Register scratch4 = v1;
3742
3743 // Separator operand is on the stack.
3744 __ pop(separator);
3745
3746 // Check that the array is a JSArray.
3747 __ JumpIfSmi(array, &bailout);
3748 __ GetObjectType(array, scratch1, scratch2);
3749 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3750
3751 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003752 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003753
3754 // If the array has length zero, return the empty string.
3755 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3756 __ SmiUntag(array_length);
3757 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3758 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3759 __ Branch(&done);
3760
3761 __ bind(&non_trivial_array);
3762
3763 // Get the FixedArray containing array's elements.
3764 elements = array;
3765 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3766 array = no_reg; // End of array's live range.
3767
3768 // Check that all array elements are sequential ASCII strings, and
3769 // accumulate the sum of their lengths, as a smi-encoded value.
3770 __ mov(string_length, zero_reg);
3771 __ Addu(element,
3772 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3773 __ sll(elements_end, array_length, kPointerSizeLog2);
3774 __ Addu(elements_end, element, elements_end);
3775 // Loop condition: while (element < elements_end).
3776 // Live values in registers:
3777 // elements: Fixed array of strings.
3778 // array_length: Length of the fixed array of strings (not smi)
3779 // separator: Separator string
3780 // string_length: Accumulated sum of string lengths (smi).
3781 // element: Current array element.
3782 // elements_end: Array end.
3783 if (FLAG_debug_code) {
3784 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3785 array_length, Operand(zero_reg));
3786 }
3787 __ bind(&loop);
3788 __ lw(string, MemOperand(element));
3789 __ Addu(element, element, kPointerSize);
3790 __ JumpIfSmi(string, &bailout);
3791 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3792 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3793 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3794 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3795 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3796 __ BranchOnOverflow(&bailout, scratch3);
3797 __ Branch(&loop, lt, element, Operand(elements_end));
3798
3799 // If array_length is 1, return elements[0], a string.
3800 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3801 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3802 __ Branch(&done);
3803
3804 __ bind(&not_size_one_array);
3805
3806 // Live values in registers:
3807 // separator: Separator string
3808 // array_length: Length of the array.
3809 // string_length: Sum of string lengths (smi).
3810 // elements: FixedArray of strings.
3811
3812 // Check that the separator is a flat ASCII string.
3813 __ JumpIfSmi(separator, &bailout);
3814 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3815 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3816 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3817
3818 // Add (separator length times array_length) - separator length to the
3819 // string_length to get the length of the result string. array_length is not
3820 // smi but the other values are, so the result is a smi.
3821 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3822 __ Subu(string_length, string_length, Operand(scratch1));
3823 __ Mult(array_length, scratch1);
3824 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3825 // zero.
3826 __ mfhi(scratch2);
3827 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3828 __ mflo(scratch2);
3829 __ And(scratch3, scratch2, Operand(0x80000000));
3830 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3831 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3832 __ BranchOnOverflow(&bailout, scratch3);
3833 __ SmiUntag(string_length);
3834
3835 // Get first element in the array to free up the elements register to be used
3836 // for the result.
3837 __ Addu(element,
3838 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3839 result = elements; // End of live range for elements.
3840 elements = no_reg;
3841 // Live values in registers:
3842 // element: First array element
3843 // separator: Separator string
3844 // string_length: Length of result string (not smi)
3845 // array_length: Length of the array.
3846 __ AllocateAsciiString(result,
3847 string_length,
3848 scratch1,
3849 scratch2,
3850 elements_end,
3851 &bailout);
3852 // Prepare for looping. Set up elements_end to end of the array. Set
3853 // result_pos to the position of the result where to write the first
3854 // character.
3855 __ sll(elements_end, array_length, kPointerSizeLog2);
3856 __ Addu(elements_end, element, elements_end);
3857 result_pos = array_length; // End of live range for array_length.
3858 array_length = no_reg;
3859 __ Addu(result_pos,
3860 result,
3861 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3862
3863 // Check the length of the separator.
3864 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3865 __ li(at, Operand(Smi::FromInt(1)));
3866 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3867 __ Branch(&long_separator, gt, scratch1, Operand(at));
3868
3869 // Empty separator case.
3870 __ bind(&empty_separator_loop);
3871 // Live values in registers:
3872 // result_pos: the position to which we are currently copying characters.
3873 // element: Current array element.
3874 // elements_end: Array end.
3875
3876 // Copy next array element to the result.
3877 __ lw(string, MemOperand(element));
3878 __ Addu(element, element, kPointerSize);
3879 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3880 __ SmiUntag(string_length);
3881 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3882 __ CopyBytes(string, result_pos, string_length, scratch1);
3883 // End while (element < elements_end).
3884 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3885 ASSERT(result.is(v0));
3886 __ Branch(&done);
3887
3888 // One-character separator case.
3889 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003890 // Replace separator with its ASCII character value.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003891 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3892 // Jump into the loop after the code that copies the separator, so the first
3893 // element is not preceded by a separator.
3894 __ jmp(&one_char_separator_loop_entry);
3895
3896 __ bind(&one_char_separator_loop);
3897 // Live values in registers:
3898 // result_pos: the position to which we are currently copying characters.
3899 // element: Current array element.
3900 // elements_end: Array end.
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003901 // separator: Single separator ASCII char (in lower byte).
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003902
3903 // Copy the separator character to the result.
3904 __ sb(separator, MemOperand(result_pos));
3905 __ Addu(result_pos, result_pos, 1);
3906
3907 // Copy next array element to the result.
3908 __ bind(&one_char_separator_loop_entry);
3909 __ lw(string, MemOperand(element));
3910 __ Addu(element, element, kPointerSize);
3911 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3912 __ SmiUntag(string_length);
3913 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3914 __ CopyBytes(string, result_pos, string_length, scratch1);
3915 // End while (element < elements_end).
3916 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3917 ASSERT(result.is(v0));
3918 __ Branch(&done);
3919
3920 // Long separator case (separator is more than one character). Entry is at the
3921 // label long_separator below.
3922 __ bind(&long_separator_loop);
3923 // Live values in registers:
3924 // result_pos: the position to which we are currently copying characters.
3925 // element: Current array element.
3926 // elements_end: Array end.
3927 // separator: Separator string.
3928
3929 // Copy the separator to the result.
3930 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3931 __ SmiUntag(string_length);
3932 __ Addu(string,
3933 separator,
3934 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3935 __ CopyBytes(string, result_pos, string_length, scratch1);
3936
3937 __ bind(&long_separator);
3938 __ lw(string, MemOperand(element));
3939 __ Addu(element, element, kPointerSize);
3940 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3941 __ SmiUntag(string_length);
3942 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3943 __ CopyBytes(string, result_pos, string_length, scratch1);
3944 // End while (element < elements_end).
3945 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3946 ASSERT(result.is(v0));
3947 __ Branch(&done);
3948
3949 __ bind(&bailout);
3950 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3951 __ bind(&done);
3952 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003953}
3954
3955
ager@chromium.org5c838252010-02-19 08:53:10 +00003956void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003957 Handle<String> name = expr->name();
3958 if (name->length() > 0 && name->Get(0) == '_') {
3959 Comment cmnt(masm_, "[ InlineRuntimeCall");
3960 EmitInlineRuntimeCall(expr);
3961 return;
3962 }
3963
3964 Comment cmnt(masm_, "[ CallRuntime");
3965 ZoneList<Expression*>* args = expr->arguments();
3966
3967 if (expr->is_jsruntime()) {
3968 // Prepare for calling JS runtime function.
3969 __ lw(a0, GlobalObjectOperand());
3970 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3971 __ push(a0);
3972 }
3973
3974 // Push the arguments ("left-to-right").
3975 int arg_count = args->length();
3976 for (int i = 0; i < arg_count; i++) {
3977 VisitForStackValue(args->at(i));
3978 }
3979
3980 if (expr->is_jsruntime()) {
3981 // Call the JS runtime function.
3982 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003983 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003984 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003985 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00003986 CallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003987 // Restore context register.
3988 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3989 } else {
3990 // Call the C runtime function.
3991 __ CallRuntime(expr->function(), arg_count);
3992 }
3993 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003994}
3995
3996
3997void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003998 switch (expr->op()) {
3999 case Token::DELETE: {
4000 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004001 Property* property = expr->expression()->AsProperty();
4002 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004003
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004004 if (property != NULL) {
4005 VisitForStackValue(property->obj());
4006 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004007 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
4008 ? kNonStrictMode : kStrictMode;
4009 __ li(a1, Operand(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004010 __ push(a1);
4011 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4012 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004013 } else if (proxy != NULL) {
4014 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004015 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004016 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004017 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004018 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004019 __ lw(a2, GlobalObjectOperand());
4020 __ li(a1, Operand(var->name()));
4021 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
4022 __ Push(a2, a1, a0);
4023 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4024 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004025 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004026 // Result of deleting non-global, non-dynamic variables is false.
4027 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004028 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004029 } else {
4030 // Non-global variable. Call the runtime to try to delete from the
4031 // context where the variable was introduced.
4032 __ push(context_register());
4033 __ li(a2, Operand(var->name()));
4034 __ push(a2);
4035 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
4036 context()->Plug(v0);
4037 }
4038 } else {
4039 // Result of deleting non-property, non-variable reference is true.
4040 // The subexpression may have side effects.
4041 VisitForEffect(expr->expression());
4042 context()->Plug(true);
4043 }
4044 break;
4045 }
4046
4047 case Token::VOID: {
4048 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4049 VisitForEffect(expr->expression());
4050 context()->Plug(Heap::kUndefinedValueRootIndex);
4051 break;
4052 }
4053
4054 case Token::NOT: {
4055 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
4056 if (context()->IsEffect()) {
4057 // Unary NOT has no side effects so it's only necessary to visit the
4058 // subexpression. Match the optimizing compiler by not branching.
4059 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004060 } else if (context()->IsTest()) {
4061 const TestContext* test = TestContext::cast(context());
4062 // The labels are swapped for the recursive call.
4063 VisitForControl(expr->expression(),
4064 test->false_label(),
4065 test->true_label(),
4066 test->fall_through());
4067 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004068 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004069 // We handle value contexts explicitly rather than simply visiting
4070 // for control and plugging the control flow into the context,
4071 // because we need to prepare a pair of extra administrative AST ids
4072 // for the optimizing compiler.
4073 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
4074 Label materialize_true, materialize_false, done;
4075 VisitForControl(expr->expression(),
4076 &materialize_false,
4077 &materialize_true,
4078 &materialize_true);
4079 __ bind(&materialize_true);
4080 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4081 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
4082 if (context()->IsStackValue()) __ push(v0);
4083 __ jmp(&done);
4084 __ bind(&materialize_false);
4085 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4086 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
4087 if (context()->IsStackValue()) __ push(v0);
4088 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004089 }
4090 break;
4091 }
4092
4093 case Token::TYPEOF: {
4094 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
4095 { StackValueContext context(this);
4096 VisitForTypeofValue(expr->expression());
4097 }
4098 __ CallRuntime(Runtime::kTypeof, 1);
4099 context()->Plug(v0);
4100 break;
4101 }
4102
4103 case Token::ADD: {
4104 Comment cmt(masm_, "[ UnaryOperation (ADD)");
4105 VisitForAccumulatorValue(expr->expression());
4106 Label no_conversion;
4107 __ JumpIfSmi(result_register(), &no_conversion);
4108 __ mov(a0, result_register());
4109 ToNumberStub convert_stub;
4110 __ CallStub(&convert_stub);
4111 __ bind(&no_conversion);
4112 context()->Plug(result_register());
4113 break;
4114 }
4115
4116 case Token::SUB:
4117 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
4118 break;
4119
4120 case Token::BIT_NOT:
4121 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
4122 break;
4123
4124 default:
4125 UNREACHABLE();
4126 }
4127}
4128
4129
4130void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
4131 const char* comment) {
4132 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
4133 Comment cmt(masm_, comment);
4134 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
4135 UnaryOverwriteMode overwrite =
4136 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004137 UnaryOpStub stub(expr->op(), overwrite);
4138 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004139 VisitForAccumulatorValue(expr->expression());
4140 SetSourcePosition(expr->position());
4141 __ mov(a0, result_register());
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004142 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004143 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004144}
4145
4146
4147void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004148 Comment cmnt(masm_, "[ CountOperation");
4149 SetSourcePosition(expr->position());
4150
4151 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
4152 // as the left-hand side.
4153 if (!expr->expression()->IsValidLeftHandSide()) {
4154 VisitForEffect(expr->expression());
4155 return;
4156 }
4157
4158 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004159 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004160 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
4161 LhsKind assign_type = VARIABLE;
4162 Property* prop = expr->expression()->AsProperty();
4163 // In case of a property we use the uninitialized expression context
4164 // of the key to detect a named property.
4165 if (prop != NULL) {
4166 assign_type =
4167 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
4168 }
4169
4170 // Evaluate expression and get value.
4171 if (assign_type == VARIABLE) {
4172 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
4173 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00004174 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004175 } else {
4176 // Reserve space for result of postfix operation.
4177 if (expr->is_postfix() && !context()->IsEffect()) {
4178 __ li(at, Operand(Smi::FromInt(0)));
4179 __ push(at);
4180 }
4181 if (assign_type == NAMED_PROPERTY) {
4182 // Put the object both on the stack and in the accumulator.
4183 VisitForAccumulatorValue(prop->obj());
4184 __ push(v0);
4185 EmitNamedPropertyLoad(prop);
4186 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004187 VisitForStackValue(prop->obj());
4188 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004189 __ lw(a1, MemOperand(sp, 0));
4190 __ push(v0);
4191 EmitKeyedPropertyLoad(prop);
4192 }
4193 }
4194
4195 // We need a second deoptimization point after loading the value
4196 // in case evaluating the property load my have a side effect.
4197 if (assign_type == VARIABLE) {
4198 PrepareForBailout(expr->expression(), TOS_REG);
4199 } else {
4200 PrepareForBailoutForId(expr->CountId(), TOS_REG);
4201 }
4202
4203 // Call ToNumber only if operand is not a smi.
4204 Label no_conversion;
4205 __ JumpIfSmi(v0, &no_conversion);
4206 __ mov(a0, v0);
4207 ToNumberStub convert_stub;
4208 __ CallStub(&convert_stub);
4209 __ bind(&no_conversion);
4210
4211 // Save result for postfix expressions.
4212 if (expr->is_postfix()) {
4213 if (!context()->IsEffect()) {
4214 // Save the result on the stack. If we have a named or keyed property
4215 // we store the result under the receiver that is currently on top
4216 // of the stack.
4217 switch (assign_type) {
4218 case VARIABLE:
4219 __ push(v0);
4220 break;
4221 case NAMED_PROPERTY:
4222 __ sw(v0, MemOperand(sp, kPointerSize));
4223 break;
4224 case KEYED_PROPERTY:
4225 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4226 break;
4227 }
4228 }
4229 }
4230 __ mov(a0, result_register());
4231
4232 // Inline smi case if we are in a loop.
4233 Label stub_call, done;
4234 JumpPatchSite patch_site(masm_);
4235
4236 int count_value = expr->op() == Token::INC ? 1 : -1;
4237 __ li(a1, Operand(Smi::FromInt(count_value)));
4238
4239 if (ShouldInlineSmiCase(expr->op())) {
4240 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
4241 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
4242
4243 // We could eliminate this smi check if we split the code at
4244 // the first smi check before calling ToNumber.
4245 patch_site.EmitJumpIfSmi(v0, &done);
4246 __ bind(&stub_call);
4247 }
4248
4249 // Record position before stub call.
4250 SetSourcePosition(expr->position());
4251
danno@chromium.org40cb8782011-05-25 07:58:50 +00004252 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004253 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004254 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004255 __ bind(&done);
4256
4257 // Store the value returned in v0.
4258 switch (assign_type) {
4259 case VARIABLE:
4260 if (expr->is_postfix()) {
4261 { EffectContext context(this);
4262 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4263 Token::ASSIGN);
4264 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4265 context.Plug(v0);
4266 }
4267 // For all contexts except EffectConstant we have the result on
4268 // top of the stack.
4269 if (!context()->IsEffect()) {
4270 context()->PlugTOS();
4271 }
4272 } else {
4273 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4274 Token::ASSIGN);
4275 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4276 context()->Plug(v0);
4277 }
4278 break;
4279 case NAMED_PROPERTY: {
4280 __ mov(a0, result_register()); // Value.
4281 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4282 __ pop(a1); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004283 Handle<Code> ic = is_classic_mode()
4284 ? isolate()->builtins()->StoreIC_Initialize()
4285 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004286 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004287 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4288 if (expr->is_postfix()) {
4289 if (!context()->IsEffect()) {
4290 context()->PlugTOS();
4291 }
4292 } else {
4293 context()->Plug(v0);
4294 }
4295 break;
4296 }
4297 case KEYED_PROPERTY: {
4298 __ mov(a0, result_register()); // Value.
4299 __ pop(a1); // Key.
4300 __ pop(a2); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004301 Handle<Code> ic = is_classic_mode()
4302 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4303 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004304 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004305 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4306 if (expr->is_postfix()) {
4307 if (!context()->IsEffect()) {
4308 context()->PlugTOS();
4309 }
4310 } else {
4311 context()->Plug(v0);
4312 }
4313 break;
4314 }
4315 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004316}
4317
4318
lrn@chromium.org7516f052011-03-30 08:52:27 +00004319void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004320 ASSERT(!context()->IsEffect());
4321 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004322 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004323 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004324 Comment cmnt(masm_, "Global variable");
4325 __ lw(a0, GlobalObjectOperand());
4326 __ li(a2, Operand(proxy->name()));
4327 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4328 // Use a regular load, not a contextual load, to avoid a reference
4329 // error.
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004330 CallIC(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004331 PrepareForBailout(expr, TOS_REG);
4332 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004333 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004334 Label done, slow;
4335
4336 // Generate code for loading from variables potentially shadowed
4337 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004338 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004339
4340 __ bind(&slow);
4341 __ li(a0, Operand(proxy->name()));
4342 __ Push(cp, a0);
4343 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4344 PrepareForBailout(expr, TOS_REG);
4345 __ bind(&done);
4346
4347 context()->Plug(v0);
4348 } else {
4349 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004350 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004351 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004352}
4353
ager@chromium.org04921a82011-06-27 13:21:41 +00004354void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004355 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004356 Handle<String> check) {
4357 Label materialize_true, materialize_false;
4358 Label* if_true = NULL;
4359 Label* if_false = NULL;
4360 Label* fall_through = NULL;
4361 context()->PrepareTest(&materialize_true, &materialize_false,
4362 &if_true, &if_false, &fall_through);
4363
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004364 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004365 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004366 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004367 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004368
4369 if (check->Equals(isolate()->heap()->number_symbol())) {
4370 __ JumpIfSmi(v0, if_true);
4371 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4372 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4373 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4374 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4375 __ JumpIfSmi(v0, if_false);
4376 // Check for undetectable objects => false.
4377 __ GetObjectType(v0, v0, a1);
4378 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4379 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4380 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4381 Split(eq, a1, Operand(zero_reg),
4382 if_true, if_false, fall_through);
4383 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4384 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4385 __ Branch(if_true, eq, v0, Operand(at));
4386 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4387 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004388 } else if (FLAG_harmony_typeof &&
4389 check->Equals(isolate()->heap()->null_symbol())) {
4390 __ LoadRoot(at, Heap::kNullValueRootIndex);
4391 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004392 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4393 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4394 __ Branch(if_true, eq, v0, Operand(at));
4395 __ JumpIfSmi(v0, if_false);
4396 // Check for undetectable objects => true.
4397 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4398 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4399 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4400 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4401 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4402 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004403 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4404 __ GetObjectType(v0, v0, a1);
4405 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4406 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4407 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004408 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4409 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004410 if (!FLAG_harmony_typeof) {
4411 __ LoadRoot(at, Heap::kNullValueRootIndex);
4412 __ Branch(if_true, eq, v0, Operand(at));
4413 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004414 // Check for JS objects => true.
4415 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004416 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004417 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004418 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004419 // Check for undetectable objects => false.
4420 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4421 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4422 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4423 } else {
4424 if (if_false != fall_through) __ jmp(if_false);
4425 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004426 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004427}
4428
4429
ager@chromium.org5c838252010-02-19 08:53:10 +00004430void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004431 Comment cmnt(masm_, "[ CompareOperation");
4432 SetSourcePosition(expr->position());
4433
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004434 // First we try a fast inlined version of the compare when one of
4435 // the operands is a literal.
4436 if (TryLiteralCompare(expr)) return;
4437
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004438 // Always perform the comparison for its control flow. Pack the result
4439 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004440 Label materialize_true, materialize_false;
4441 Label* if_true = NULL;
4442 Label* if_false = NULL;
4443 Label* fall_through = NULL;
4444 context()->PrepareTest(&materialize_true, &materialize_false,
4445 &if_true, &if_false, &fall_through);
4446
ager@chromium.org04921a82011-06-27 13:21:41 +00004447 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004448 VisitForStackValue(expr->left());
4449 switch (op) {
4450 case Token::IN:
4451 VisitForStackValue(expr->right());
4452 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004453 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004454 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4455 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4456 break;
4457
4458 case Token::INSTANCEOF: {
4459 VisitForStackValue(expr->right());
4460 InstanceofStub stub(InstanceofStub::kNoFlags);
4461 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004462 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004463 // The stub returns 0 for true.
4464 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4465 break;
4466 }
4467
4468 default: {
4469 VisitForAccumulatorValue(expr->right());
4470 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004471 switch (op) {
4472 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004473 case Token::EQ:
4474 cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004475 break;
4476 case Token::LT:
4477 cc = lt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004478 break;
4479 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004480 cc = gt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004481 break;
4482 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004483 cc = le;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004484 break;
4485 case Token::GTE:
4486 cc = ge;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004487 break;
4488 case Token::IN:
4489 case Token::INSTANCEOF:
4490 default:
4491 UNREACHABLE();
4492 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004493 __ mov(a0, result_register());
4494 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004495
4496 bool inline_smi_code = ShouldInlineSmiCase(op);
4497 JumpPatchSite patch_site(masm_);
4498 if (inline_smi_code) {
4499 Label slow_case;
4500 __ Or(a2, a0, Operand(a1));
4501 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4502 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4503 __ bind(&slow_case);
4504 }
4505 // Record position and call the compare IC.
4506 SetSourcePosition(expr->position());
4507 Handle<Code> ic = CompareIC::GetUninitialized(op);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004508 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004509 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004510 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004511 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4512 }
4513 }
4514
4515 // Convert the result of the comparison into one expected for this
4516 // expression's context.
4517 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004518}
4519
4520
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004521void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4522 Expression* sub_expr,
4523 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004524 Label materialize_true, materialize_false;
4525 Label* if_true = NULL;
4526 Label* if_false = NULL;
4527 Label* fall_through = NULL;
4528 context()->PrepareTest(&materialize_true, &materialize_false,
4529 &if_true, &if_false, &fall_through);
4530
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004531 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004532 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004533 Heap::RootListIndex nil_value = nil == kNullValue ?
4534 Heap::kNullValueRootIndex :
4535 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004536 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004537 __ LoadRoot(a1, nil_value);
4538 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004539 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4540 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004541 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4542 Heap::kUndefinedValueRootIndex :
4543 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004544 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004545 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004546 __ Branch(if_true, eq, a0, Operand(a1));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004547 __ JumpIfSmi(a0, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004548 // It can be an undetectable object.
4549 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4550 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4551 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4552 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4553 }
4554 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004555}
4556
4557
ager@chromium.org5c838252010-02-19 08:53:10 +00004558void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004559 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4560 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004561}
4562
4563
lrn@chromium.org7516f052011-03-30 08:52:27 +00004564Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004565 return v0;
4566}
ager@chromium.org5c838252010-02-19 08:53:10 +00004567
4568
lrn@chromium.org7516f052011-03-30 08:52:27 +00004569Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004570 return cp;
4571}
4572
4573
ager@chromium.org5c838252010-02-19 08:53:10 +00004574void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004575 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4576 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004577}
4578
4579
4580void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004581 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004582}
4583
4584
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004585void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4586 Scope* declaration_scope = scope()->DeclarationScope();
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004587 if (declaration_scope->is_global_scope() ||
4588 declaration_scope->is_module_scope()) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004589 // Contexts nested in the global context have a canonical empty function
4590 // as their closure, not the anonymous closure containing the global
4591 // code. Pass a smi sentinel and let the runtime look up the empty
4592 // function.
4593 __ li(at, Operand(Smi::FromInt(0)));
4594 } else if (declaration_scope->is_eval_scope()) {
4595 // Contexts created by a call to eval have the same closure as the
4596 // context calling eval, not the anonymous closure containing the eval
4597 // code. Fetch it from the context.
4598 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4599 } else {
4600 ASSERT(declaration_scope->is_function_scope());
4601 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4602 }
4603 __ push(at);
4604}
4605
4606
ager@chromium.org5c838252010-02-19 08:53:10 +00004607// ----------------------------------------------------------------------------
4608// Non-local control flow support.
4609
4610void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004611 ASSERT(!result_register().is(a1));
4612 // Store result register while executing finally block.
4613 __ push(result_register());
4614 // Cook return address in link register to stack (smi encoded Code* delta).
4615 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4616 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004617 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004618 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4619 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004620}
4621
4622
4623void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004624 ASSERT(!result_register().is(a1));
4625 // Restore result register from stack.
4626 __ pop(a1);
4627 // Uncook return address and return.
4628 __ pop(result_register());
4629 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4630 __ sra(a1, a1, 1); // Un-smi-tag value.
4631 __ Addu(at, a1, Operand(masm_->CodeObject()));
4632 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004633}
4634
4635
4636#undef __
4637
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004638#define __ ACCESS_MASM(masm())
4639
4640FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4641 int* stack_depth,
4642 int* context_length) {
4643 // The macros used here must preserve the result register.
4644
4645 // Because the handler block contains the context of the finally
4646 // code, we can restore it directly from there for the finally code
4647 // rather than iteratively unwinding contexts via their previous
4648 // links.
4649 __ Drop(*stack_depth); // Down to the handler block.
4650 if (*context_length > 0) {
4651 // Restore the context to its dedicated register and the stack.
4652 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4653 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4654 }
4655 __ PopTryHandler();
4656 __ Call(finally_entry_);
4657
4658 *stack_depth = 0;
4659 *context_length = 0;
4660 return previous_;
4661}
4662
4663
4664#undef __
4665
ager@chromium.org5c838252010-02-19 08:53:10 +00004666} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004667
4668#endif // V8_TARGET_ARCH_MIPS