blob: 7456510584eb47a72d29a2183b9021c3792ca0e6 [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());
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00002391
2392 // Record call targets in unoptimized code, but not in the snapshot.
2393 if (!Serializer::enabled()) {
2394 flags = static_cast<CallFunctionFlags>(flags | RECORD_CALL_TARGET);
2395 Handle<Object> uninitialized =
2396 TypeFeedbackCells::UninitializedSentinel(isolate());
2397 Handle<JSGlobalPropertyCell> cell =
2398 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
2399 RecordTypeFeedbackCell(expr->id(), cell);
2400 __ li(a2, Operand(cell));
2401 }
2402
lrn@chromium.org34e60782011-09-15 07:25:40 +00002403 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002404 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002405 __ CallStub(&stub);
2406 RecordJSReturnSite(expr);
2407 // Restore context register.
2408 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2409 context()->DropAndPlug(1, v0);
2410}
2411
2412
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002413void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002414 // Push copy of the first argument or undefined if it doesn't exist.
2415 if (arg_count > 0) {
2416 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2417 } else {
2418 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2419 }
2420 __ push(a1);
2421
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002422 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002423 int receiver_offset = 2 + info_->scope()->num_parameters();
2424 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002425 __ push(a1);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002426 // Push the language mode.
2427 __ li(a1, Operand(Smi::FromInt(language_mode())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002428 __ push(a1);
2429
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002430 // Push the start position of the scope the calls resides in.
2431 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
2432 __ push(a1);
2433
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002434 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002435 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org5c838252010-02-19 08:53:10 +00002436}
2437
2438
2439void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002440#ifdef DEBUG
2441 // We want to verify that RecordJSReturnSite gets called on all paths
2442 // through this function. Avoid early returns.
2443 expr->return_is_recorded_ = false;
2444#endif
2445
2446 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002447 Expression* callee = expr->expression();
2448 VariableProxy* proxy = callee->AsVariableProxy();
2449 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002450
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002451 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002452 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2453 // resolve the function we need to call and the receiver of the
2454 // call. Then we call the resolved function using the given
2455 // arguments.
2456 ZoneList<Expression*>* args = expr->arguments();
2457 int arg_count = args->length();
2458
2459 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002460 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002461 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2462 __ push(a2); // Reserved receiver slot.
2463
2464 // Push the arguments.
2465 for (int i = 0; i < arg_count; i++) {
2466 VisitForStackValue(args->at(i));
2467 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002468
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002469 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002470 // resolve eval.
2471 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2472 __ push(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002473 EmitResolvePossiblyDirectEval(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002474
2475 // The runtime call returns a pair of values in v0 (function) and
2476 // v1 (receiver). Touch up the stack with the right values.
2477 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2478 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2479 }
2480 // Record source position for debugger.
2481 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002482 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002483 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002484 __ CallStub(&stub);
2485 RecordJSReturnSite(expr);
2486 // Restore context register.
2487 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2488 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002489 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002490 // Push global object as receiver for the call IC.
2491 __ lw(a0, GlobalObjectOperand());
2492 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002493 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2494 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002495 // Call to a lookup slot (dynamically introduced variable).
2496 Label slow, done;
2497
2498 { PreservePositionScope scope(masm()->positions_recorder());
2499 // Generate code for loading from variables potentially shadowed
2500 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002501 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002502 }
2503
2504 __ bind(&slow);
2505 // Call the runtime to find the function to call (returned in v0)
2506 // and the object holding it (returned in v1).
2507 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002508 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002509 __ push(a2);
2510 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2511 __ Push(v0, v1); // Function, receiver.
2512
2513 // If fast case code has been generated, emit code to push the
2514 // function and receiver and have the slow path jump around this
2515 // code.
2516 if (done.is_linked()) {
2517 Label call;
2518 __ Branch(&call);
2519 __ bind(&done);
2520 // Push function.
2521 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002522 // The receiver is implicitly the global receiver. Indicate this
2523 // by passing the hole to the call function stub.
2524 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002525 __ push(a1);
2526 __ bind(&call);
2527 }
2528
danno@chromium.org40cb8782011-05-25 07:58:50 +00002529 // The receiver is either the global receiver or an object found
2530 // by LoadContextSlot. That object could be the hole if the
2531 // receiver is implicitly the global object.
2532 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002533 } else if (property != NULL) {
2534 { PreservePositionScope scope(masm()->positions_recorder());
2535 VisitForStackValue(property->obj());
2536 }
2537 if (property->key()->IsPropertyName()) {
2538 EmitCallWithIC(expr,
2539 property->key()->AsLiteral()->handle(),
2540 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002541 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002542 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002543 }
2544 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002545 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002546 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002547 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002548 }
2549 // Load global receiver object.
2550 __ lw(a1, GlobalObjectOperand());
2551 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2552 __ push(a1);
2553 // Emit function call.
2554 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2555 }
2556
2557#ifdef DEBUG
2558 // RecordJSReturnSite should have been called.
2559 ASSERT(expr->return_is_recorded_);
2560#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002561}
2562
2563
2564void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002565 Comment cmnt(masm_, "[ CallNew");
2566 // According to ECMA-262, section 11.2.2, page 44, the function
2567 // expression in new calls must be evaluated before the
2568 // arguments.
2569
2570 // Push constructor on the stack. If it's not a function it's used as
2571 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2572 // ignored.
2573 VisitForStackValue(expr->expression());
2574
2575 // Push the arguments ("left-to-right") on the stack.
2576 ZoneList<Expression*>* args = expr->arguments();
2577 int arg_count = args->length();
2578 for (int i = 0; i < arg_count; i++) {
2579 VisitForStackValue(args->at(i));
2580 }
2581
2582 // Call the construct call builtin that handles allocation and
2583 // constructor invocation.
2584 SetSourcePosition(expr->position());
2585
2586 // Load function and argument count into a1 and a0.
2587 __ li(a0, Operand(arg_count));
2588 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2589
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002590 // Record call targets in unoptimized code, but not in the snapshot.
2591 CallFunctionFlags flags;
2592 if (!Serializer::enabled()) {
2593 flags = RECORD_CALL_TARGET;
2594 Handle<Object> uninitialized =
2595 TypeFeedbackCells::UninitializedSentinel(isolate());
2596 Handle<JSGlobalPropertyCell> cell =
2597 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
2598 RecordTypeFeedbackCell(expr->id(), cell);
2599 __ li(a2, Operand(cell));
2600 } else {
2601 flags = NO_CALL_FUNCTION_FLAGS;
2602 }
2603
2604 CallConstructStub stub(flags);
2605 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
ulan@chromium.org967e2702012-02-28 09:49:15 +00002606 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002607 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002608}
2609
2610
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002611void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2612 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002613 ASSERT(args->length() == 1);
2614
2615 VisitForAccumulatorValue(args->at(0));
2616
2617 Label materialize_true, materialize_false;
2618 Label* if_true = NULL;
2619 Label* if_false = NULL;
2620 Label* fall_through = NULL;
2621 context()->PrepareTest(&materialize_true, &materialize_false,
2622 &if_true, &if_false, &fall_through);
2623
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002624 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002625 __ And(t0, v0, Operand(kSmiTagMask));
2626 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2627
2628 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002629}
2630
2631
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002632void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2633 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002634 ASSERT(args->length() == 1);
2635
2636 VisitForAccumulatorValue(args->at(0));
2637
2638 Label materialize_true, materialize_false;
2639 Label* if_true = NULL;
2640 Label* if_false = NULL;
2641 Label* fall_through = NULL;
2642 context()->PrepareTest(&materialize_true, &materialize_false,
2643 &if_true, &if_false, &fall_through);
2644
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002645 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002646 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2647 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2648
2649 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002650}
2651
2652
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002653void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2654 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002655 ASSERT(args->length() == 1);
2656
2657 VisitForAccumulatorValue(args->at(0));
2658
2659 Label materialize_true, materialize_false;
2660 Label* if_true = NULL;
2661 Label* if_false = NULL;
2662 Label* fall_through = NULL;
2663 context()->PrepareTest(&materialize_true, &materialize_false,
2664 &if_true, &if_false, &fall_through);
2665
2666 __ JumpIfSmi(v0, if_false);
2667 __ LoadRoot(at, Heap::kNullValueRootIndex);
2668 __ Branch(if_true, eq, v0, Operand(at));
2669 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2670 // Undetectable objects behave like undefined when tested with typeof.
2671 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2672 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2673 __ Branch(if_false, ne, at, Operand(zero_reg));
2674 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002675 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002676 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002677 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2678 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002679
2680 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002681}
2682
2683
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002684void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2685 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002686 ASSERT(args->length() == 1);
2687
2688 VisitForAccumulatorValue(args->at(0));
2689
2690 Label materialize_true, materialize_false;
2691 Label* if_true = NULL;
2692 Label* if_false = NULL;
2693 Label* fall_through = NULL;
2694 context()->PrepareTest(&materialize_true, &materialize_false,
2695 &if_true, &if_false, &fall_through);
2696
2697 __ JumpIfSmi(v0, if_false);
2698 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002699 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002700 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002701 if_true, if_false, fall_through);
2702
2703 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002704}
2705
2706
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002707void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2708 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002709 ASSERT(args->length() == 1);
2710
2711 VisitForAccumulatorValue(args->at(0));
2712
2713 Label materialize_true, materialize_false;
2714 Label* if_true = NULL;
2715 Label* if_false = NULL;
2716 Label* fall_through = NULL;
2717 context()->PrepareTest(&materialize_true, &materialize_false,
2718 &if_true, &if_false, &fall_through);
2719
2720 __ JumpIfSmi(v0, if_false);
2721 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2722 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2723 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002724 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002725 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2726
2727 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002728}
2729
2730
2731void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002732 CallRuntime* expr) {
2733 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002734 ASSERT(args->length() == 1);
2735
2736 VisitForAccumulatorValue(args->at(0));
2737
2738 Label materialize_true, materialize_false;
2739 Label* if_true = NULL;
2740 Label* if_false = NULL;
2741 Label* fall_through = NULL;
2742 context()->PrepareTest(&materialize_true, &materialize_false,
2743 &if_true, &if_false, &fall_through);
2744
2745 if (FLAG_debug_code) __ AbortIfSmi(v0);
2746
2747 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2748 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2749 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2750 __ Branch(if_true, ne, t0, Operand(zero_reg));
2751
2752 // Check for fast case object. Generate false result for slow case object.
2753 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2754 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2755 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2756 __ Branch(if_false, eq, a2, Operand(t0));
2757
2758 // Look for valueOf symbol in the descriptor array, and indicate false if
2759 // found. The type is not checked, so if it is a transition it is a false
2760 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002761 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002762 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2763 // t0: descriptor array
2764 // a3: length of descriptor array
2765 // Calculate the end of the descriptor array.
2766 STATIC_ASSERT(kSmiTag == 0);
2767 STATIC_ASSERT(kSmiTagSize == 1);
2768 STATIC_ASSERT(kPointerSize == 4);
2769 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2770 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2771 __ Addu(a2, a2, t1);
2772
2773 // Calculate location of the first key name.
2774 __ Addu(t0,
2775 t0,
2776 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2777 DescriptorArray::kFirstIndex * kPointerSize));
2778 // Loop through all the keys in the descriptor array. If one of these is the
2779 // symbol valueOf the result is false.
2780 Label entry, loop;
2781 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2782 // used in the loop below.
danno@chromium.org88aa0582012-03-23 15:11:57 +00002783 __ LoadRoot(t2, Heap::kvalue_of_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002784 __ jmp(&entry);
2785 __ bind(&loop);
2786 __ lw(a3, MemOperand(t0, 0));
2787 __ Branch(if_false, eq, a3, Operand(t2));
2788 __ Addu(t0, t0, Operand(kPointerSize));
2789 __ bind(&entry);
2790 __ Branch(&loop, ne, t0, Operand(a2));
2791
2792 // If a valueOf property is not found on the object check that it's
2793 // prototype is the un-modified String prototype. If not result is false.
2794 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2795 __ JumpIfSmi(a2, if_false);
2796 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2797 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2798 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2799 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2800 __ Branch(if_false, ne, a2, Operand(a3));
2801
2802 // Set the bit in the map to indicate that it has been checked safe for
2803 // default valueOf and set true result.
2804 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2805 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2806 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2807 __ jmp(if_true);
2808
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002809 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002810 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002811}
2812
2813
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002814void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2815 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002816 ASSERT(args->length() == 1);
2817
2818 VisitForAccumulatorValue(args->at(0));
2819
2820 Label materialize_true, materialize_false;
2821 Label* if_true = NULL;
2822 Label* if_false = NULL;
2823 Label* fall_through = NULL;
2824 context()->PrepareTest(&materialize_true, &materialize_false,
2825 &if_true, &if_false, &fall_through);
2826
2827 __ JumpIfSmi(v0, if_false);
2828 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002829 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002830 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2831 __ Branch(if_false);
2832
2833 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002834}
2835
2836
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002837void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2838 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002839 ASSERT(args->length() == 1);
2840
2841 VisitForAccumulatorValue(args->at(0));
2842
2843 Label materialize_true, materialize_false;
2844 Label* if_true = NULL;
2845 Label* if_false = NULL;
2846 Label* fall_through = NULL;
2847 context()->PrepareTest(&materialize_true, &materialize_false,
2848 &if_true, &if_false, &fall_through);
2849
2850 __ JumpIfSmi(v0, if_false);
2851 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002852 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002853 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2854 if_true, if_false, fall_through);
2855
2856 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002857}
2858
2859
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002860void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2861 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002862 ASSERT(args->length() == 1);
2863
2864 VisitForAccumulatorValue(args->at(0));
2865
2866 Label materialize_true, materialize_false;
2867 Label* if_true = NULL;
2868 Label* if_false = NULL;
2869 Label* fall_through = NULL;
2870 context()->PrepareTest(&materialize_true, &materialize_false,
2871 &if_true, &if_false, &fall_through);
2872
2873 __ JumpIfSmi(v0, if_false);
2874 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002875 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002876 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2877
2878 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002879}
2880
2881
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002882void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2883 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002884
2885 Label materialize_true, materialize_false;
2886 Label* if_true = NULL;
2887 Label* if_false = NULL;
2888 Label* fall_through = NULL;
2889 context()->PrepareTest(&materialize_true, &materialize_false,
2890 &if_true, &if_false, &fall_through);
2891
2892 // Get the frame pointer for the calling frame.
2893 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2894
2895 // Skip the arguments adaptor frame if it exists.
2896 Label check_frame_marker;
2897 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2898 __ Branch(&check_frame_marker, ne,
2899 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2900 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2901
2902 // Check the marker in the calling frame.
2903 __ bind(&check_frame_marker);
2904 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002905 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002906 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2907 if_true, if_false, fall_through);
2908
2909 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002910}
2911
2912
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002913void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2914 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002915 ASSERT(args->length() == 2);
2916
2917 // Load the two objects into registers and perform the comparison.
2918 VisitForStackValue(args->at(0));
2919 VisitForAccumulatorValue(args->at(1));
2920
2921 Label materialize_true, materialize_false;
2922 Label* if_true = NULL;
2923 Label* if_false = NULL;
2924 Label* fall_through = NULL;
2925 context()->PrepareTest(&materialize_true, &materialize_false,
2926 &if_true, &if_false, &fall_through);
2927
2928 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002929 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002930 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2931
2932 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002933}
2934
2935
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002936void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2937 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002938 ASSERT(args->length() == 1);
2939
2940 // ArgumentsAccessStub expects the key in a1 and the formal
2941 // parameter count in a0.
2942 VisitForAccumulatorValue(args->at(0));
2943 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002944 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002945 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2946 __ CallStub(&stub);
2947 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002948}
2949
2950
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002951void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2952 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002953 Label exit;
2954 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002955 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002956
2957 // Check if the calling frame is an arguments adaptor frame.
2958 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2959 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2960 __ Branch(&exit, ne, a3,
2961 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2962
2963 // Arguments adaptor case: Read the arguments length from the
2964 // adaptor frame.
2965 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2966
2967 __ bind(&exit);
2968 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002969}
2970
2971
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002972void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2973 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002974 ASSERT(args->length() == 1);
2975 Label done, null, function, non_function_constructor;
2976
2977 VisitForAccumulatorValue(args->at(0));
2978
2979 // If the object is a smi, we return null.
2980 __ JumpIfSmi(v0, &null);
2981
2982 // Check that the object is a JS object but take special care of JS
2983 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002984 // Assume that there are only two callable types, and one of them is at
2985 // either end of the type range for JS object types. Saves extra comparisons.
2986 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002987 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002988 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002989
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002990 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2991 FIRST_SPEC_OBJECT_TYPE + 1);
2992 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002993
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002994 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2995 LAST_SPEC_OBJECT_TYPE - 1);
2996 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2997 // Assume that there is no larger type.
2998 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2999
3000 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003001 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
3002 __ GetObjectType(v0, a1, a1);
3003 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
3004
3005 // v0 now contains the constructor function. Grab the
3006 // instance class name from there.
3007 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
3008 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
3009 __ Branch(&done);
3010
3011 // Functions have class 'Function'.
3012 __ bind(&function);
3013 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
3014 __ jmp(&done);
3015
3016 // Objects with a non-function constructor have class 'Object'.
3017 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00003018 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003019 __ jmp(&done);
3020
3021 // Non-JS objects have class null.
3022 __ bind(&null);
3023 __ LoadRoot(v0, Heap::kNullValueRootIndex);
3024
3025 // All done.
3026 __ bind(&done);
3027
3028 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003029}
3030
3031
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003032void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003033 // Conditionally generate a log call.
3034 // Args:
3035 // 0 (literal string): The type of logging (corresponds to the flags).
3036 // This is used to determine whether or not to generate the log call.
3037 // 1 (string): Format string. Access the string at argument index 2
3038 // with '%2s' (see Logger::LogRuntime for all the formats).
3039 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003040 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003041 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003042 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
3043 VisitForStackValue(args->at(1));
3044 VisitForStackValue(args->at(2));
3045 __ CallRuntime(Runtime::kLog, 2);
3046 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003047
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003048 // Finally, we're expected to leave a value on the top of the stack.
3049 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3050 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003051}
3052
3053
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003054void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
3055 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003056 Label slow_allocate_heapnumber;
3057 Label heapnumber_allocated;
3058
3059 // Save the new heap number in callee-saved register s0, since
3060 // we call out to external C code below.
3061 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
3062 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
3063 __ jmp(&heapnumber_allocated);
3064
3065 __ bind(&slow_allocate_heapnumber);
3066
3067 // Allocate a heap number.
3068 __ CallRuntime(Runtime::kNumberAlloc, 0);
3069 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
3070
3071 __ bind(&heapnumber_allocated);
3072
3073 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
3074 // by computing:
3075 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
3076 if (CpuFeatures::IsSupported(FPU)) {
3077 __ PrepareCallCFunction(1, a0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003078 __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
3079 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003080 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
3081
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003082 CpuFeatures::Scope scope(FPU);
3083 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
3084 __ li(a1, Operand(0x41300000));
3085 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00003086 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003087 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00003088 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003089 // Subtract and store the result in the heap number.
3090 __ sub_d(f0, f12, f14);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003091 __ sdc1(f0, FieldMemOperand(s0, HeapNumber::kValueOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003092 __ mov(v0, s0);
3093 } else {
3094 __ PrepareCallCFunction(2, a0);
3095 __ mov(a0, s0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003096 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
3097 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003098 __ CallCFunction(
3099 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
3100 }
3101
3102 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003103}
3104
3105
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003106void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003107 // Load the arguments on the stack and call the stub.
3108 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003109 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003110 ASSERT(args->length() == 3);
3111 VisitForStackValue(args->at(0));
3112 VisitForStackValue(args->at(1));
3113 VisitForStackValue(args->at(2));
3114 __ CallStub(&stub);
3115 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003116}
3117
3118
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003119void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003120 // Load the arguments on the stack and call the stub.
3121 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003122 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003123 ASSERT(args->length() == 4);
3124 VisitForStackValue(args->at(0));
3125 VisitForStackValue(args->at(1));
3126 VisitForStackValue(args->at(2));
3127 VisitForStackValue(args->at(3));
3128 __ CallStub(&stub);
3129 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003130}
3131
3132
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003133void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3134 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003135 ASSERT(args->length() == 1);
3136
3137 VisitForAccumulatorValue(args->at(0)); // Load the object.
3138
3139 Label done;
3140 // If the object is a smi return the object.
3141 __ JumpIfSmi(v0, &done);
3142 // If the object is not a value type, return the object.
3143 __ GetObjectType(v0, a1, a1);
3144 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
3145
3146 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
3147
3148 __ bind(&done);
3149 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003150}
3151
3152
yangguo@chromium.org154ff992012-03-13 08:09:54 +00003153void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3154 ZoneList<Expression*>* args = expr->arguments();
3155 ASSERT(args->length() == 2);
3156 ASSERT_NE(NULL, args->at(1)->AsLiteral());
3157 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->handle()));
3158
3159 VisitForAccumulatorValue(args->at(0)); // Load the object.
3160
3161 Label runtime, done;
3162 Register object = v0;
3163 Register result = v0;
3164 Register scratch0 = t5;
3165 Register scratch1 = a1;
3166
3167#ifdef DEBUG
3168 __ AbortIfSmi(object);
3169 __ GetObjectType(object, scratch1, scratch1);
3170 __ Assert(eq, "Trying to get date field from non-date.",
3171 scratch1, Operand(JS_DATE_TYPE));
3172#endif
3173
3174 if (index->value() == 0) {
3175 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset));
3176 } else {
3177 if (index->value() < JSDate::kFirstUncachedField) {
3178 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3179 __ li(scratch1, Operand(stamp));
3180 __ lw(scratch1, MemOperand(scratch1));
3181 __ lw(scratch0, FieldMemOperand(object, JSDate::kCacheStampOffset));
3182 __ Branch(&runtime, ne, scratch1, Operand(scratch0));
3183 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset +
3184 kPointerSize * index->value()));
3185 __ jmp(&done);
3186 }
3187 __ bind(&runtime);
3188 __ PrepareCallCFunction(2, scratch1);
3189 __ li(a1, Operand(index));
3190 __ Move(a0, object);
3191 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
3192 __ bind(&done);
3193 }
3194
3195 context()->Plug(v0);
3196}
3197
3198
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003199void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003200 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003201 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003202 ASSERT(args->length() == 2);
3203 VisitForStackValue(args->at(0));
3204 VisitForStackValue(args->at(1));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003205 if (CpuFeatures::IsSupported(FPU)) {
3206 MathPowStub stub(MathPowStub::ON_STACK);
3207 __ CallStub(&stub);
3208 } else {
3209 __ CallRuntime(Runtime::kMath_pow, 2);
3210 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003211 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003212}
3213
3214
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003215void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3216 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003217 ASSERT(args->length() == 2);
3218
3219 VisitForStackValue(args->at(0)); // Load the object.
3220 VisitForAccumulatorValue(args->at(1)); // Load the value.
3221 __ pop(a1); // v0 = value. a1 = object.
3222
3223 Label done;
3224 // If the object is a smi, return the value.
3225 __ JumpIfSmi(a1, &done);
3226
3227 // If the object is not a value type, return the value.
3228 __ GetObjectType(a1, a2, a2);
3229 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
3230
3231 // Store the value.
3232 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
3233 // Update the write barrier. Save the value as it will be
3234 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003235 __ mov(a2, v0);
3236 __ RecordWriteField(
3237 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003238
3239 __ bind(&done);
3240 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003241}
3242
3243
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003244void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3245 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003246 ASSERT_EQ(args->length(), 1);
3247
3248 // Load the argument on the stack and call the stub.
3249 VisitForStackValue(args->at(0));
3250
3251 NumberToStringStub stub;
3252 __ CallStub(&stub);
3253 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003254}
3255
3256
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003257void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3258 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003259 ASSERT(args->length() == 1);
3260
3261 VisitForAccumulatorValue(args->at(0));
3262
3263 Label done;
3264 StringCharFromCodeGenerator generator(v0, a1);
3265 generator.GenerateFast(masm_);
3266 __ jmp(&done);
3267
3268 NopRuntimeCallHelper call_helper;
3269 generator.GenerateSlow(masm_, call_helper);
3270
3271 __ bind(&done);
3272 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003273}
3274
3275
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003276void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3277 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003278 ASSERT(args->length() == 2);
3279
3280 VisitForStackValue(args->at(0));
3281 VisitForAccumulatorValue(args->at(1));
3282 __ mov(a0, result_register());
3283
3284 Register object = a1;
3285 Register index = a0;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003286 Register result = v0;
3287
3288 __ pop(object);
3289
3290 Label need_conversion;
3291 Label index_out_of_range;
3292 Label done;
3293 StringCharCodeAtGenerator generator(object,
3294 index,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003295 result,
3296 &need_conversion,
3297 &need_conversion,
3298 &index_out_of_range,
3299 STRING_INDEX_IS_NUMBER);
3300 generator.GenerateFast(masm_);
3301 __ jmp(&done);
3302
3303 __ bind(&index_out_of_range);
3304 // When the index is out of range, the spec requires us to return
3305 // NaN.
3306 __ LoadRoot(result, Heap::kNanValueRootIndex);
3307 __ jmp(&done);
3308
3309 __ bind(&need_conversion);
3310 // Load the undefined value into the result register, which will
3311 // trigger conversion.
3312 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3313 __ jmp(&done);
3314
3315 NopRuntimeCallHelper call_helper;
3316 generator.GenerateSlow(masm_, call_helper);
3317
3318 __ bind(&done);
3319 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003320}
3321
3322
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003323void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3324 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003325 ASSERT(args->length() == 2);
3326
3327 VisitForStackValue(args->at(0));
3328 VisitForAccumulatorValue(args->at(1));
3329 __ mov(a0, result_register());
3330
3331 Register object = a1;
3332 Register index = a0;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003333 Register scratch = a3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003334 Register result = v0;
3335
3336 __ pop(object);
3337
3338 Label need_conversion;
3339 Label index_out_of_range;
3340 Label done;
3341 StringCharAtGenerator generator(object,
3342 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003343 scratch,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003344 result,
3345 &need_conversion,
3346 &need_conversion,
3347 &index_out_of_range,
3348 STRING_INDEX_IS_NUMBER);
3349 generator.GenerateFast(masm_);
3350 __ jmp(&done);
3351
3352 __ bind(&index_out_of_range);
3353 // When the index is out of range, the spec requires us to return
3354 // the empty string.
3355 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3356 __ jmp(&done);
3357
3358 __ bind(&need_conversion);
3359 // Move smi zero into the result register, which will trigger
3360 // conversion.
3361 __ li(result, Operand(Smi::FromInt(0)));
3362 __ jmp(&done);
3363
3364 NopRuntimeCallHelper call_helper;
3365 generator.GenerateSlow(masm_, call_helper);
3366
3367 __ bind(&done);
3368 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003369}
3370
3371
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003372void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3373 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003374 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003375 VisitForStackValue(args->at(0));
3376 VisitForStackValue(args->at(1));
3377
3378 StringAddStub stub(NO_STRING_ADD_FLAGS);
3379 __ CallStub(&stub);
3380 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003381}
3382
3383
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003384void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3385 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003386 ASSERT_EQ(2, args->length());
3387
3388 VisitForStackValue(args->at(0));
3389 VisitForStackValue(args->at(1));
3390
3391 StringCompareStub stub;
3392 __ CallStub(&stub);
3393 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003394}
3395
3396
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003397void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003398 // Load the argument on the stack and call the stub.
3399 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3400 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003401 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003402 ASSERT(args->length() == 1);
3403 VisitForStackValue(args->at(0));
3404 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3405 __ CallStub(&stub);
3406 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003407}
3408
3409
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003410void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003411 // Load the argument on the stack and call the stub.
3412 TranscendentalCacheStub stub(TranscendentalCache::COS,
3413 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003414 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003415 ASSERT(args->length() == 1);
3416 VisitForStackValue(args->at(0));
3417 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3418 __ CallStub(&stub);
3419 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003420}
3421
3422
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003423void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3424 // Load the argument on the stack and call the stub.
3425 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3426 TranscendentalCacheStub::TAGGED);
3427 ZoneList<Expression*>* args = expr->arguments();
3428 ASSERT(args->length() == 1);
3429 VisitForStackValue(args->at(0));
3430 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3431 __ CallStub(&stub);
3432 context()->Plug(v0);
3433}
3434
3435
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003436void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003437 // Load the argument on the stack and call the stub.
3438 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3439 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003440 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003441 ASSERT(args->length() == 1);
3442 VisitForStackValue(args->at(0));
3443 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3444 __ CallStub(&stub);
3445 context()->Plug(v0);
3446}
3447
3448
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003449void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003450 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003451 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003452 ASSERT(args->length() == 1);
3453 VisitForStackValue(args->at(0));
3454 __ CallRuntime(Runtime::kMath_sqrt, 1);
3455 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003456}
3457
3458
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003459void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3460 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003461 ASSERT(args->length() >= 2);
3462
3463 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3464 for (int i = 0; i < arg_count + 1; i++) {
3465 VisitForStackValue(args->at(i));
3466 }
3467 VisitForAccumulatorValue(args->last()); // Function.
3468
danno@chromium.orgc612e022011-11-10 11:38:15 +00003469 // Check for proxy.
3470 Label proxy, done;
3471 __ GetObjectType(v0, a1, a1);
3472 __ Branch(&proxy, eq, a1, Operand(JS_FUNCTION_PROXY_TYPE));
3473
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003474 // InvokeFunction requires the function in a1. Move it in there.
3475 __ mov(a1, result_register());
3476 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003477 __ InvokeFunction(a1, count, CALL_FUNCTION,
3478 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003479 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003480 __ jmp(&done);
3481
3482 __ bind(&proxy);
3483 __ push(v0);
3484 __ CallRuntime(Runtime::kCall, args->length());
3485 __ bind(&done);
3486
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003487 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003488}
3489
3490
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003491void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003492 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003493 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003494 ASSERT(args->length() == 3);
3495 VisitForStackValue(args->at(0));
3496 VisitForStackValue(args->at(1));
3497 VisitForStackValue(args->at(2));
3498 __ CallStub(&stub);
3499 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003500}
3501
3502
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003503void FullCodeGenerator::EmitSwapElements(CallRuntime* expr) {
3504 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003505 ASSERT(args->length() == 3);
3506 VisitForStackValue(args->at(0));
3507 VisitForStackValue(args->at(1));
3508 VisitForStackValue(args->at(2));
3509 Label done;
3510 Label slow_case;
3511 Register object = a0;
3512 Register index1 = a1;
3513 Register index2 = a2;
3514 Register elements = a3;
3515 Register scratch1 = t0;
3516 Register scratch2 = t1;
3517
3518 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3519 // Fetch the map and check if array is in fast case.
3520 // Check that object doesn't require security checks and
3521 // has no indexed interceptor.
3522 __ GetObjectType(object, scratch1, scratch2);
3523 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3524 // Map is now in scratch1.
3525
3526 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3527 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3528 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3529
3530 // Check the object's elements are in fast case and writable.
3531 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3532 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3533 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3534 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3535
3536 // Check that both indices are smis.
3537 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3538 __ lw(index2, MemOperand(sp, 0));
3539 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3540
3541 // Check that both indices are valid.
3542 Label not_hi;
3543 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3544 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3545 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3546 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3547 __ bind(&not_hi);
3548
3549 // Bring the address of the elements into index1 and index2.
3550 __ Addu(scratch1, elements,
3551 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3552 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3553 __ Addu(index1, scratch1, index1);
3554 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3555 __ Addu(index2, scratch1, index2);
3556
3557 // Swap elements.
3558 __ lw(scratch1, MemOperand(index1, 0));
3559 __ lw(scratch2, MemOperand(index2, 0));
3560 __ sw(scratch1, MemOperand(index2, 0));
3561 __ sw(scratch2, MemOperand(index1, 0));
3562
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003563 Label no_remembered_set;
3564 __ CheckPageFlag(elements,
3565 scratch1,
3566 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3567 ne,
3568 &no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003569 // Possible optimization: do a check that both values are Smis
3570 // (or them and test against Smi mask).
3571
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003572 // We are swapping two objects in an array and the incremental marker never
3573 // pauses in the middle of scanning a single object. Therefore the
3574 // incremental marker is not disturbed, so we don't need to call the
3575 // RecordWrite stub that notifies the incremental marker.
3576 __ RememberedSetHelper(elements,
3577 index1,
3578 scratch2,
3579 kDontSaveFPRegs,
3580 MacroAssembler::kFallThroughAtEnd);
3581 __ RememberedSetHelper(elements,
3582 index2,
3583 scratch2,
3584 kDontSaveFPRegs,
3585 MacroAssembler::kFallThroughAtEnd);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003586
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003587 __ bind(&no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003588 // We are done. Drop elements from the stack, and return undefined.
3589 __ Drop(3);
3590 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3591 __ jmp(&done);
3592
3593 __ bind(&slow_case);
3594 __ CallRuntime(Runtime::kSwapElements, 3);
3595
3596 __ bind(&done);
3597 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003598}
3599
3600
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003601void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3602 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003603 ASSERT_EQ(2, args->length());
3604
3605 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3606 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3607
3608 Handle<FixedArray> jsfunction_result_caches(
3609 isolate()->global_context()->jsfunction_result_caches());
3610 if (jsfunction_result_caches->length() <= cache_id) {
3611 __ Abort("Attempt to use undefined cache.");
3612 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3613 context()->Plug(v0);
3614 return;
3615 }
3616
3617 VisitForAccumulatorValue(args->at(1));
3618
3619 Register key = v0;
3620 Register cache = a1;
3621 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3622 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3623 __ lw(cache,
3624 ContextOperand(
3625 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3626 __ lw(cache,
3627 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3628
3629
3630 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003631 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003632 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3633 // a2 now holds finger offset as a smi.
3634 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3635 // a3 now points to the start of fixed array elements.
3636 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3637 __ addu(a3, a3, at);
3638 // a3 now points to key of indexed element of cache.
3639 __ lw(a2, MemOperand(a3));
3640 __ Branch(&not_found, ne, key, Operand(a2));
3641
3642 __ lw(v0, MemOperand(a3, kPointerSize));
3643 __ Branch(&done);
3644
3645 __ bind(&not_found);
3646 // Call runtime to perform the lookup.
3647 __ Push(cache, key);
3648 __ CallRuntime(Runtime::kGetFromCache, 2);
3649
3650 __ bind(&done);
3651 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003652}
3653
3654
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003655void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3656 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003657 ASSERT_EQ(2, args->length());
3658
3659 Register right = v0;
3660 Register left = a1;
3661 Register tmp = a2;
3662 Register tmp2 = a3;
3663
3664 VisitForStackValue(args->at(0));
3665 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3666 __ pop(left);
3667
3668 Label done, fail, ok;
3669 __ Branch(&ok, eq, left, Operand(right));
3670 // Fail if either is a non-HeapObject.
3671 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003672 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003673 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3674 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3675 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3676 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3677 __ Branch(&fail, ne, tmp, Operand(tmp2));
3678 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3679 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3680 __ Branch(&ok, eq, tmp, Operand(tmp2));
3681 __ bind(&fail);
3682 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3683 __ jmp(&done);
3684 __ bind(&ok);
3685 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3686 __ bind(&done);
3687
3688 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003689}
3690
3691
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003692void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3693 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003694 VisitForAccumulatorValue(args->at(0));
3695
3696 Label materialize_true, materialize_false;
3697 Label* if_true = NULL;
3698 Label* if_false = NULL;
3699 Label* fall_through = NULL;
3700 context()->PrepareTest(&materialize_true, &materialize_false,
3701 &if_true, &if_false, &fall_through);
3702
3703 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3704 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3705
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003706 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003707 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3708
3709 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003710}
3711
3712
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003713void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3714 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003715 ASSERT(args->length() == 1);
3716 VisitForAccumulatorValue(args->at(0));
3717
3718 if (FLAG_debug_code) {
3719 __ AbortIfNotString(v0);
3720 }
3721
3722 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3723 __ IndexFromHash(v0, v0);
3724
3725 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003726}
3727
3728
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003729void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003730 Label bailout, done, one_char_separator, long_separator,
3731 non_trivial_array, not_size_one_array, loop,
3732 empty_separator_loop, one_char_separator_loop,
3733 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003734 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003735 ASSERT(args->length() == 2);
3736 VisitForStackValue(args->at(1));
3737 VisitForAccumulatorValue(args->at(0));
3738
3739 // All aliases of the same register have disjoint lifetimes.
3740 Register array = v0;
3741 Register elements = no_reg; // Will be v0.
3742 Register result = no_reg; // Will be v0.
3743 Register separator = a1;
3744 Register array_length = a2;
3745 Register result_pos = no_reg; // Will be a2.
3746 Register string_length = a3;
3747 Register string = t0;
3748 Register element = t1;
3749 Register elements_end = t2;
3750 Register scratch1 = t3;
3751 Register scratch2 = t5;
3752 Register scratch3 = t4;
3753 Register scratch4 = v1;
3754
3755 // Separator operand is on the stack.
3756 __ pop(separator);
3757
3758 // Check that the array is a JSArray.
3759 __ JumpIfSmi(array, &bailout);
3760 __ GetObjectType(array, scratch1, scratch2);
3761 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3762
3763 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003764 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003765
3766 // If the array has length zero, return the empty string.
3767 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3768 __ SmiUntag(array_length);
3769 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3770 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3771 __ Branch(&done);
3772
3773 __ bind(&non_trivial_array);
3774
3775 // Get the FixedArray containing array's elements.
3776 elements = array;
3777 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3778 array = no_reg; // End of array's live range.
3779
3780 // Check that all array elements are sequential ASCII strings, and
3781 // accumulate the sum of their lengths, as a smi-encoded value.
3782 __ mov(string_length, zero_reg);
3783 __ Addu(element,
3784 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3785 __ sll(elements_end, array_length, kPointerSizeLog2);
3786 __ Addu(elements_end, element, elements_end);
3787 // Loop condition: while (element < elements_end).
3788 // Live values in registers:
3789 // elements: Fixed array of strings.
3790 // array_length: Length of the fixed array of strings (not smi)
3791 // separator: Separator string
3792 // string_length: Accumulated sum of string lengths (smi).
3793 // element: Current array element.
3794 // elements_end: Array end.
3795 if (FLAG_debug_code) {
3796 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3797 array_length, Operand(zero_reg));
3798 }
3799 __ bind(&loop);
3800 __ lw(string, MemOperand(element));
3801 __ Addu(element, element, kPointerSize);
3802 __ JumpIfSmi(string, &bailout);
3803 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3804 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3805 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3806 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3807 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3808 __ BranchOnOverflow(&bailout, scratch3);
3809 __ Branch(&loop, lt, element, Operand(elements_end));
3810
3811 // If array_length is 1, return elements[0], a string.
3812 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3813 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3814 __ Branch(&done);
3815
3816 __ bind(&not_size_one_array);
3817
3818 // Live values in registers:
3819 // separator: Separator string
3820 // array_length: Length of the array.
3821 // string_length: Sum of string lengths (smi).
3822 // elements: FixedArray of strings.
3823
3824 // Check that the separator is a flat ASCII string.
3825 __ JumpIfSmi(separator, &bailout);
3826 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3827 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3828 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3829
3830 // Add (separator length times array_length) - separator length to the
3831 // string_length to get the length of the result string. array_length is not
3832 // smi but the other values are, so the result is a smi.
3833 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3834 __ Subu(string_length, string_length, Operand(scratch1));
3835 __ Mult(array_length, scratch1);
3836 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3837 // zero.
3838 __ mfhi(scratch2);
3839 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3840 __ mflo(scratch2);
3841 __ And(scratch3, scratch2, Operand(0x80000000));
3842 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3843 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3844 __ BranchOnOverflow(&bailout, scratch3);
3845 __ SmiUntag(string_length);
3846
3847 // Get first element in the array to free up the elements register to be used
3848 // for the result.
3849 __ Addu(element,
3850 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3851 result = elements; // End of live range for elements.
3852 elements = no_reg;
3853 // Live values in registers:
3854 // element: First array element
3855 // separator: Separator string
3856 // string_length: Length of result string (not smi)
3857 // array_length: Length of the array.
3858 __ AllocateAsciiString(result,
3859 string_length,
3860 scratch1,
3861 scratch2,
3862 elements_end,
3863 &bailout);
3864 // Prepare for looping. Set up elements_end to end of the array. Set
3865 // result_pos to the position of the result where to write the first
3866 // character.
3867 __ sll(elements_end, array_length, kPointerSizeLog2);
3868 __ Addu(elements_end, element, elements_end);
3869 result_pos = array_length; // End of live range for array_length.
3870 array_length = no_reg;
3871 __ Addu(result_pos,
3872 result,
3873 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3874
3875 // Check the length of the separator.
3876 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3877 __ li(at, Operand(Smi::FromInt(1)));
3878 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3879 __ Branch(&long_separator, gt, scratch1, Operand(at));
3880
3881 // Empty separator case.
3882 __ bind(&empty_separator_loop);
3883 // Live values in registers:
3884 // result_pos: the position to which we are currently copying characters.
3885 // element: Current array element.
3886 // elements_end: Array end.
3887
3888 // Copy next array element to the result.
3889 __ lw(string, MemOperand(element));
3890 __ Addu(element, element, kPointerSize);
3891 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3892 __ SmiUntag(string_length);
3893 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3894 __ CopyBytes(string, result_pos, string_length, scratch1);
3895 // End while (element < elements_end).
3896 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3897 ASSERT(result.is(v0));
3898 __ Branch(&done);
3899
3900 // One-character separator case.
3901 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003902 // Replace separator with its ASCII character value.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003903 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3904 // Jump into the loop after the code that copies the separator, so the first
3905 // element is not preceded by a separator.
3906 __ jmp(&one_char_separator_loop_entry);
3907
3908 __ bind(&one_char_separator_loop);
3909 // Live values in registers:
3910 // result_pos: the position to which we are currently copying characters.
3911 // element: Current array element.
3912 // elements_end: Array end.
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003913 // separator: Single separator ASCII char (in lower byte).
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003914
3915 // Copy the separator character to the result.
3916 __ sb(separator, MemOperand(result_pos));
3917 __ Addu(result_pos, result_pos, 1);
3918
3919 // Copy next array element to the result.
3920 __ bind(&one_char_separator_loop_entry);
3921 __ lw(string, MemOperand(element));
3922 __ Addu(element, element, kPointerSize);
3923 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3924 __ SmiUntag(string_length);
3925 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3926 __ CopyBytes(string, result_pos, string_length, scratch1);
3927 // End while (element < elements_end).
3928 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3929 ASSERT(result.is(v0));
3930 __ Branch(&done);
3931
3932 // Long separator case (separator is more than one character). Entry is at the
3933 // label long_separator below.
3934 __ bind(&long_separator_loop);
3935 // Live values in registers:
3936 // result_pos: the position to which we are currently copying characters.
3937 // element: Current array element.
3938 // elements_end: Array end.
3939 // separator: Separator string.
3940
3941 // Copy the separator to the result.
3942 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3943 __ SmiUntag(string_length);
3944 __ Addu(string,
3945 separator,
3946 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3947 __ CopyBytes(string, result_pos, string_length, scratch1);
3948
3949 __ bind(&long_separator);
3950 __ lw(string, MemOperand(element));
3951 __ Addu(element, element, kPointerSize);
3952 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3953 __ SmiUntag(string_length);
3954 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3955 __ CopyBytes(string, result_pos, string_length, scratch1);
3956 // End while (element < elements_end).
3957 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3958 ASSERT(result.is(v0));
3959 __ Branch(&done);
3960
3961 __ bind(&bailout);
3962 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3963 __ bind(&done);
3964 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003965}
3966
3967
ager@chromium.org5c838252010-02-19 08:53:10 +00003968void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003969 Handle<String> name = expr->name();
3970 if (name->length() > 0 && name->Get(0) == '_') {
3971 Comment cmnt(masm_, "[ InlineRuntimeCall");
3972 EmitInlineRuntimeCall(expr);
3973 return;
3974 }
3975
3976 Comment cmnt(masm_, "[ CallRuntime");
3977 ZoneList<Expression*>* args = expr->arguments();
3978
3979 if (expr->is_jsruntime()) {
3980 // Prepare for calling JS runtime function.
3981 __ lw(a0, GlobalObjectOperand());
3982 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3983 __ push(a0);
3984 }
3985
3986 // Push the arguments ("left-to-right").
3987 int arg_count = args->length();
3988 for (int i = 0; i < arg_count; i++) {
3989 VisitForStackValue(args->at(i));
3990 }
3991
3992 if (expr->is_jsruntime()) {
3993 // Call the JS runtime function.
3994 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003995 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003996 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003997 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00003998 CallIC(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003999 // Restore context register.
4000 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4001 } else {
4002 // Call the C runtime function.
4003 __ CallRuntime(expr->function(), arg_count);
4004 }
4005 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004006}
4007
4008
4009void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004010 switch (expr->op()) {
4011 case Token::DELETE: {
4012 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004013 Property* property = expr->expression()->AsProperty();
4014 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004015
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004016 if (property != NULL) {
4017 VisitForStackValue(property->obj());
4018 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004019 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
4020 ? kNonStrictMode : kStrictMode;
4021 __ li(a1, Operand(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004022 __ push(a1);
4023 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4024 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004025 } else if (proxy != NULL) {
4026 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004027 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004028 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004029 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004030 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004031 __ lw(a2, GlobalObjectOperand());
4032 __ li(a1, Operand(var->name()));
4033 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
4034 __ Push(a2, a1, a0);
4035 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4036 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004037 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004038 // Result of deleting non-global, non-dynamic variables is false.
4039 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004040 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004041 } else {
4042 // Non-global variable. Call the runtime to try to delete from the
4043 // context where the variable was introduced.
4044 __ push(context_register());
4045 __ li(a2, Operand(var->name()));
4046 __ push(a2);
4047 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
4048 context()->Plug(v0);
4049 }
4050 } else {
4051 // Result of deleting non-property, non-variable reference is true.
4052 // The subexpression may have side effects.
4053 VisitForEffect(expr->expression());
4054 context()->Plug(true);
4055 }
4056 break;
4057 }
4058
4059 case Token::VOID: {
4060 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4061 VisitForEffect(expr->expression());
4062 context()->Plug(Heap::kUndefinedValueRootIndex);
4063 break;
4064 }
4065
4066 case Token::NOT: {
4067 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
4068 if (context()->IsEffect()) {
4069 // Unary NOT has no side effects so it's only necessary to visit the
4070 // subexpression. Match the optimizing compiler by not branching.
4071 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004072 } else if (context()->IsTest()) {
4073 const TestContext* test = TestContext::cast(context());
4074 // The labels are swapped for the recursive call.
4075 VisitForControl(expr->expression(),
4076 test->false_label(),
4077 test->true_label(),
4078 test->fall_through());
4079 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004080 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004081 // We handle value contexts explicitly rather than simply visiting
4082 // for control and plugging the control flow into the context,
4083 // because we need to prepare a pair of extra administrative AST ids
4084 // for the optimizing compiler.
4085 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
4086 Label materialize_true, materialize_false, done;
4087 VisitForControl(expr->expression(),
4088 &materialize_false,
4089 &materialize_true,
4090 &materialize_true);
4091 __ bind(&materialize_true);
4092 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4093 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
4094 if (context()->IsStackValue()) __ push(v0);
4095 __ jmp(&done);
4096 __ bind(&materialize_false);
4097 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4098 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
4099 if (context()->IsStackValue()) __ push(v0);
4100 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004101 }
4102 break;
4103 }
4104
4105 case Token::TYPEOF: {
4106 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
4107 { StackValueContext context(this);
4108 VisitForTypeofValue(expr->expression());
4109 }
4110 __ CallRuntime(Runtime::kTypeof, 1);
4111 context()->Plug(v0);
4112 break;
4113 }
4114
4115 case Token::ADD: {
4116 Comment cmt(masm_, "[ UnaryOperation (ADD)");
4117 VisitForAccumulatorValue(expr->expression());
4118 Label no_conversion;
4119 __ JumpIfSmi(result_register(), &no_conversion);
4120 __ mov(a0, result_register());
4121 ToNumberStub convert_stub;
4122 __ CallStub(&convert_stub);
4123 __ bind(&no_conversion);
4124 context()->Plug(result_register());
4125 break;
4126 }
4127
4128 case Token::SUB:
4129 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
4130 break;
4131
4132 case Token::BIT_NOT:
4133 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
4134 break;
4135
4136 default:
4137 UNREACHABLE();
4138 }
4139}
4140
4141
4142void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
4143 const char* comment) {
4144 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
4145 Comment cmt(masm_, comment);
4146 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
4147 UnaryOverwriteMode overwrite =
4148 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004149 UnaryOpStub stub(expr->op(), overwrite);
4150 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004151 VisitForAccumulatorValue(expr->expression());
4152 SetSourcePosition(expr->position());
4153 __ mov(a0, result_register());
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004154 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004155 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004156}
4157
4158
4159void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004160 Comment cmnt(masm_, "[ CountOperation");
4161 SetSourcePosition(expr->position());
4162
4163 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
4164 // as the left-hand side.
4165 if (!expr->expression()->IsValidLeftHandSide()) {
4166 VisitForEffect(expr->expression());
4167 return;
4168 }
4169
4170 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004171 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004172 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
4173 LhsKind assign_type = VARIABLE;
4174 Property* prop = expr->expression()->AsProperty();
4175 // In case of a property we use the uninitialized expression context
4176 // of the key to detect a named property.
4177 if (prop != NULL) {
4178 assign_type =
4179 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
4180 }
4181
4182 // Evaluate expression and get value.
4183 if (assign_type == VARIABLE) {
4184 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
4185 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00004186 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004187 } else {
4188 // Reserve space for result of postfix operation.
4189 if (expr->is_postfix() && !context()->IsEffect()) {
4190 __ li(at, Operand(Smi::FromInt(0)));
4191 __ push(at);
4192 }
4193 if (assign_type == NAMED_PROPERTY) {
4194 // Put the object both on the stack and in the accumulator.
4195 VisitForAccumulatorValue(prop->obj());
4196 __ push(v0);
4197 EmitNamedPropertyLoad(prop);
4198 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004199 VisitForStackValue(prop->obj());
4200 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004201 __ lw(a1, MemOperand(sp, 0));
4202 __ push(v0);
4203 EmitKeyedPropertyLoad(prop);
4204 }
4205 }
4206
4207 // We need a second deoptimization point after loading the value
4208 // in case evaluating the property load my have a side effect.
4209 if (assign_type == VARIABLE) {
4210 PrepareForBailout(expr->expression(), TOS_REG);
4211 } else {
4212 PrepareForBailoutForId(expr->CountId(), TOS_REG);
4213 }
4214
4215 // Call ToNumber only if operand is not a smi.
4216 Label no_conversion;
4217 __ JumpIfSmi(v0, &no_conversion);
4218 __ mov(a0, v0);
4219 ToNumberStub convert_stub;
4220 __ CallStub(&convert_stub);
4221 __ bind(&no_conversion);
4222
4223 // Save result for postfix expressions.
4224 if (expr->is_postfix()) {
4225 if (!context()->IsEffect()) {
4226 // Save the result on the stack. If we have a named or keyed property
4227 // we store the result under the receiver that is currently on top
4228 // of the stack.
4229 switch (assign_type) {
4230 case VARIABLE:
4231 __ push(v0);
4232 break;
4233 case NAMED_PROPERTY:
4234 __ sw(v0, MemOperand(sp, kPointerSize));
4235 break;
4236 case KEYED_PROPERTY:
4237 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4238 break;
4239 }
4240 }
4241 }
4242 __ mov(a0, result_register());
4243
4244 // Inline smi case if we are in a loop.
4245 Label stub_call, done;
4246 JumpPatchSite patch_site(masm_);
4247
4248 int count_value = expr->op() == Token::INC ? 1 : -1;
4249 __ li(a1, Operand(Smi::FromInt(count_value)));
4250
4251 if (ShouldInlineSmiCase(expr->op())) {
4252 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
4253 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
4254
4255 // We could eliminate this smi check if we split the code at
4256 // the first smi check before calling ToNumber.
4257 patch_site.EmitJumpIfSmi(v0, &done);
4258 __ bind(&stub_call);
4259 }
4260
4261 // Record position before stub call.
4262 SetSourcePosition(expr->position());
4263
danno@chromium.org40cb8782011-05-25 07:58:50 +00004264 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004265 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004266 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004267 __ bind(&done);
4268
4269 // Store the value returned in v0.
4270 switch (assign_type) {
4271 case VARIABLE:
4272 if (expr->is_postfix()) {
4273 { EffectContext context(this);
4274 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4275 Token::ASSIGN);
4276 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4277 context.Plug(v0);
4278 }
4279 // For all contexts except EffectConstant we have the result on
4280 // top of the stack.
4281 if (!context()->IsEffect()) {
4282 context()->PlugTOS();
4283 }
4284 } else {
4285 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4286 Token::ASSIGN);
4287 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4288 context()->Plug(v0);
4289 }
4290 break;
4291 case NAMED_PROPERTY: {
4292 __ mov(a0, result_register()); // Value.
4293 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4294 __ pop(a1); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004295 Handle<Code> ic = is_classic_mode()
4296 ? isolate()->builtins()->StoreIC_Initialize()
4297 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004298 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004299 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4300 if (expr->is_postfix()) {
4301 if (!context()->IsEffect()) {
4302 context()->PlugTOS();
4303 }
4304 } else {
4305 context()->Plug(v0);
4306 }
4307 break;
4308 }
4309 case KEYED_PROPERTY: {
4310 __ mov(a0, result_register()); // Value.
4311 __ pop(a1); // Key.
4312 __ pop(a2); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004313 Handle<Code> ic = is_classic_mode()
4314 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4315 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004316 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004317 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4318 if (expr->is_postfix()) {
4319 if (!context()->IsEffect()) {
4320 context()->PlugTOS();
4321 }
4322 } else {
4323 context()->Plug(v0);
4324 }
4325 break;
4326 }
4327 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004328}
4329
4330
lrn@chromium.org7516f052011-03-30 08:52:27 +00004331void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004332 ASSERT(!context()->IsEffect());
4333 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004334 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004335 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004336 Comment cmnt(masm_, "Global variable");
4337 __ lw(a0, GlobalObjectOperand());
4338 __ li(a2, Operand(proxy->name()));
4339 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4340 // Use a regular load, not a contextual load, to avoid a reference
4341 // error.
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004342 CallIC(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004343 PrepareForBailout(expr, TOS_REG);
4344 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004345 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004346 Label done, slow;
4347
4348 // Generate code for loading from variables potentially shadowed
4349 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004350 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004351
4352 __ bind(&slow);
4353 __ li(a0, Operand(proxy->name()));
4354 __ Push(cp, a0);
4355 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4356 PrepareForBailout(expr, TOS_REG);
4357 __ bind(&done);
4358
4359 context()->Plug(v0);
4360 } else {
4361 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004362 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004363 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004364}
4365
ager@chromium.org04921a82011-06-27 13:21:41 +00004366void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004367 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004368 Handle<String> check) {
4369 Label materialize_true, materialize_false;
4370 Label* if_true = NULL;
4371 Label* if_false = NULL;
4372 Label* fall_through = NULL;
4373 context()->PrepareTest(&materialize_true, &materialize_false,
4374 &if_true, &if_false, &fall_through);
4375
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004376 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004377 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004378 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004379 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004380
4381 if (check->Equals(isolate()->heap()->number_symbol())) {
4382 __ JumpIfSmi(v0, if_true);
4383 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4384 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4385 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4386 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4387 __ JumpIfSmi(v0, if_false);
4388 // Check for undetectable objects => false.
4389 __ GetObjectType(v0, v0, a1);
4390 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4391 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4392 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4393 Split(eq, a1, Operand(zero_reg),
4394 if_true, if_false, fall_through);
4395 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4396 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4397 __ Branch(if_true, eq, v0, Operand(at));
4398 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4399 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004400 } else if (FLAG_harmony_typeof &&
4401 check->Equals(isolate()->heap()->null_symbol())) {
4402 __ LoadRoot(at, Heap::kNullValueRootIndex);
4403 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004404 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4405 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4406 __ Branch(if_true, eq, v0, Operand(at));
4407 __ JumpIfSmi(v0, if_false);
4408 // Check for undetectable objects => true.
4409 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4410 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4411 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4412 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4413 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4414 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004415 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4416 __ GetObjectType(v0, v0, a1);
4417 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4418 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4419 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004420 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4421 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004422 if (!FLAG_harmony_typeof) {
4423 __ LoadRoot(at, Heap::kNullValueRootIndex);
4424 __ Branch(if_true, eq, v0, Operand(at));
4425 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004426 // Check for JS objects => true.
4427 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004428 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004429 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004430 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004431 // Check for undetectable objects => false.
4432 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4433 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4434 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4435 } else {
4436 if (if_false != fall_through) __ jmp(if_false);
4437 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004438 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004439}
4440
4441
ager@chromium.org5c838252010-02-19 08:53:10 +00004442void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004443 Comment cmnt(masm_, "[ CompareOperation");
4444 SetSourcePosition(expr->position());
4445
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004446 // First we try a fast inlined version of the compare when one of
4447 // the operands is a literal.
4448 if (TryLiteralCompare(expr)) return;
4449
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004450 // Always perform the comparison for its control flow. Pack the result
4451 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004452 Label materialize_true, materialize_false;
4453 Label* if_true = NULL;
4454 Label* if_false = NULL;
4455 Label* fall_through = NULL;
4456 context()->PrepareTest(&materialize_true, &materialize_false,
4457 &if_true, &if_false, &fall_through);
4458
ager@chromium.org04921a82011-06-27 13:21:41 +00004459 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004460 VisitForStackValue(expr->left());
4461 switch (op) {
4462 case Token::IN:
4463 VisitForStackValue(expr->right());
4464 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004465 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004466 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4467 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4468 break;
4469
4470 case Token::INSTANCEOF: {
4471 VisitForStackValue(expr->right());
4472 InstanceofStub stub(InstanceofStub::kNoFlags);
4473 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004474 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004475 // The stub returns 0 for true.
4476 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4477 break;
4478 }
4479
4480 default: {
4481 VisitForAccumulatorValue(expr->right());
4482 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004483 switch (op) {
4484 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004485 case Token::EQ:
4486 cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004487 break;
4488 case Token::LT:
4489 cc = lt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004490 break;
4491 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004492 cc = gt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004493 break;
4494 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004495 cc = le;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004496 break;
4497 case Token::GTE:
4498 cc = ge;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004499 break;
4500 case Token::IN:
4501 case Token::INSTANCEOF:
4502 default:
4503 UNREACHABLE();
4504 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004505 __ mov(a0, result_register());
4506 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004507
4508 bool inline_smi_code = ShouldInlineSmiCase(op);
4509 JumpPatchSite patch_site(masm_);
4510 if (inline_smi_code) {
4511 Label slow_case;
4512 __ Or(a2, a0, Operand(a1));
4513 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4514 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4515 __ bind(&slow_case);
4516 }
4517 // Record position and call the compare IC.
4518 SetSourcePosition(expr->position());
4519 Handle<Code> ic = CompareIC::GetUninitialized(op);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +00004520 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004521 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004522 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004523 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4524 }
4525 }
4526
4527 // Convert the result of the comparison into one expected for this
4528 // expression's context.
4529 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004530}
4531
4532
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004533void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4534 Expression* sub_expr,
4535 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004536 Label materialize_true, materialize_false;
4537 Label* if_true = NULL;
4538 Label* if_false = NULL;
4539 Label* fall_through = NULL;
4540 context()->PrepareTest(&materialize_true, &materialize_false,
4541 &if_true, &if_false, &fall_through);
4542
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004543 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004544 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004545 Heap::RootListIndex nil_value = nil == kNullValue ?
4546 Heap::kNullValueRootIndex :
4547 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004548 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004549 __ LoadRoot(a1, nil_value);
4550 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004551 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4552 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004553 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4554 Heap::kUndefinedValueRootIndex :
4555 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004556 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004557 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004558 __ Branch(if_true, eq, a0, Operand(a1));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004559 __ JumpIfSmi(a0, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004560 // It can be an undetectable object.
4561 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4562 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4563 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4564 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4565 }
4566 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004567}
4568
4569
ager@chromium.org5c838252010-02-19 08:53:10 +00004570void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004571 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4572 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004573}
4574
4575
lrn@chromium.org7516f052011-03-30 08:52:27 +00004576Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004577 return v0;
4578}
ager@chromium.org5c838252010-02-19 08:53:10 +00004579
4580
lrn@chromium.org7516f052011-03-30 08:52:27 +00004581Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004582 return cp;
4583}
4584
4585
ager@chromium.org5c838252010-02-19 08:53:10 +00004586void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004587 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4588 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004589}
4590
4591
4592void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004593 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004594}
4595
4596
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004597void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4598 Scope* declaration_scope = scope()->DeclarationScope();
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004599 if (declaration_scope->is_global_scope() ||
4600 declaration_scope->is_module_scope()) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004601 // Contexts nested in the global context have a canonical empty function
4602 // as their closure, not the anonymous closure containing the global
4603 // code. Pass a smi sentinel and let the runtime look up the empty
4604 // function.
4605 __ li(at, Operand(Smi::FromInt(0)));
4606 } else if (declaration_scope->is_eval_scope()) {
4607 // Contexts created by a call to eval have the same closure as the
4608 // context calling eval, not the anonymous closure containing the eval
4609 // code. Fetch it from the context.
4610 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4611 } else {
4612 ASSERT(declaration_scope->is_function_scope());
4613 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4614 }
4615 __ push(at);
4616}
4617
4618
ager@chromium.org5c838252010-02-19 08:53:10 +00004619// ----------------------------------------------------------------------------
4620// Non-local control flow support.
4621
4622void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004623 ASSERT(!result_register().is(a1));
4624 // Store result register while executing finally block.
4625 __ push(result_register());
4626 // Cook return address in link register to stack (smi encoded Code* delta).
4627 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4628 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004629 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004630 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4631 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004632}
4633
4634
4635void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004636 ASSERT(!result_register().is(a1));
4637 // Restore result register from stack.
4638 __ pop(a1);
4639 // Uncook return address and return.
4640 __ pop(result_register());
4641 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4642 __ sra(a1, a1, 1); // Un-smi-tag value.
4643 __ Addu(at, a1, Operand(masm_->CodeObject()));
4644 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004645}
4646
4647
4648#undef __
4649
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004650#define __ ACCESS_MASM(masm())
4651
4652FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4653 int* stack_depth,
4654 int* context_length) {
4655 // The macros used here must preserve the result register.
4656
4657 // Because the handler block contains the context of the finally
4658 // code, we can restore it directly from there for the finally code
4659 // rather than iteratively unwinding contexts via their previous
4660 // links.
4661 __ Drop(*stack_depth); // Down to the handler block.
4662 if (*context_length > 0) {
4663 // Restore the context to its dedicated register and the stack.
4664 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4665 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4666 }
4667 __ PopTryHandler();
4668 __ Call(finally_entry_);
4669
4670 *stack_depth = 0;
4671 *context_length = 0;
4672 return previous_;
4673}
4674
4675
4676#undef __
4677
ager@chromium.org5c838252010-02-19 08:53:10 +00004678} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004679
4680#endif // V8_TARGET_ARCH_MIPS