blob: 7adc404b28936d9835a04437905b6b310981cf39 [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"
45#include "parser.h"
lrn@chromium.org7516f052011-03-30 08:52:27 +000046#include "scopes.h"
47#include "stub-cache.h"
48
49#include "mips/code-stubs-mips.h"
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +000050#include "mips/macro-assembler-mips.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000051
52namespace v8 {
53namespace internal {
54
55#define __ ACCESS_MASM(masm_)
56
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000057
58// A patch site is a location in the code which it is possible to patch. This
59// class has a number of methods to emit the code which is patchable and the
60// method EmitPatchInfo to record a marker back to the patchable code. This
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000061// marker is a andi zero_reg, rx, #yyyy instruction, and rx * 0x0000ffff + yyyy
62// (raw 16 bit immediate value is used) is the delta from the pc to the first
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000063// instruction of the patchable code.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000064// The marker instruction is effectively a NOP (dest is zero_reg) and will
65// never be emitted by normal code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000066class JumpPatchSite BASE_EMBEDDED {
67 public:
68 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
69#ifdef DEBUG
70 info_emitted_ = false;
71#endif
72 }
73
74 ~JumpPatchSite() {
75 ASSERT(patch_site_.is_bound() == info_emitted_);
76 }
77
78 // When initially emitting this ensure that a jump is always generated to skip
79 // the inlined smi code.
80 void EmitJumpIfNotSmi(Register reg, Label* target) {
81 ASSERT(!patch_site_.is_bound() && !info_emitted_);
82 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
83 __ bind(&patch_site_);
84 __ andi(at, reg, 0);
85 // Always taken before patched.
86 __ Branch(target, eq, at, Operand(zero_reg));
87 }
88
89 // When initially emitting this ensure that a jump is never generated to skip
90 // the inlined smi code.
91 void EmitJumpIfSmi(Register reg, Label* target) {
92 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
93 ASSERT(!patch_site_.is_bound() && !info_emitted_);
94 __ bind(&patch_site_);
95 __ andi(at, reg, 0);
96 // Never taken before patched.
97 __ Branch(target, ne, at, Operand(zero_reg));
98 }
99
100 void EmitPatchInfo() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000101 if (patch_site_.is_bound()) {
102 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
103 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000104 __ andi(zero_reg, reg, delta_to_patch_site % kImm16Mask);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000105#ifdef DEBUG
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000106 info_emitted_ = true;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000107#endif
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000108 } else {
109 __ nop(); // Signals no inlined code.
110 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000111 }
112
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000113 private:
114 MacroAssembler* masm_;
115 Label patch_site_;
116#ifdef DEBUG
117 bool info_emitted_;
118#endif
119};
120
121
lrn@chromium.org7516f052011-03-30 08:52:27 +0000122// Generate code for a JS function. On entry to the function the receiver
123// and arguments have been pushed on the stack left to right. The actual
124// argument count matches the formal parameter count expected by the
125// function.
126//
127// The live registers are:
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000128// o a1: the JS function object being called (i.e. ourselves)
lrn@chromium.org7516f052011-03-30 08:52:27 +0000129// o cp: our context
130// o fp: our caller's frame pointer
131// o sp: stack pointer
132// o ra: return address
133//
134// The function builds a JS frame. Please see JavaScriptFrameConstants in
135// frames-mips.h for its layout.
136void FullCodeGenerator::Generate(CompilationInfo* info) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000137 ASSERT(info_ == NULL);
138 info_ = info;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000139 scope_ = info->scope();
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +0000140 handler_table_ =
141 isolate()->factory()->NewFixedArray(function()->handler_count(), TENURED);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000142 SetFunctionPosition(function());
143 Comment cmnt(masm_, "[ function compiled by full code generator");
144
145#ifdef DEBUG
146 if (strlen(FLAG_stop_at) > 0 &&
147 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
148 __ stop("stop-at");
149 }
150#endif
151
ulan@chromium.org65a89c22012-02-14 11:46:07 +0000152 // We can optionally optimize based on counters rather than statistical
153 // sampling.
154 if (info->ShouldSelfOptimize()) {
155 if (FLAG_trace_opt) {
156 PrintF("[adding self-optimization header to %s]\n",
157 *info->function()->debug_name()->ToCString());
158 }
159 MaybeObject* maybe_cell = isolate()->heap()->AllocateJSGlobalPropertyCell(
160 Smi::FromInt(Compiler::kCallsUntilPrimitiveOpt));
161 JSGlobalPropertyCell* cell;
162 if (maybe_cell->To(&cell)) {
163 __ li(a2, Handle<JSGlobalPropertyCell>(cell));
164 __ lw(a3, FieldMemOperand(a2, JSGlobalPropertyCell::kValueOffset));
165 __ Subu(a3, a3, Operand(Smi::FromInt(1)));
166 __ sw(a3, FieldMemOperand(a2, JSGlobalPropertyCell::kValueOffset));
167 Handle<Code> compile_stub(
168 isolate()->builtins()->builtin(Builtins::kLazyRecompile));
169 __ Jump(compile_stub, RelocInfo::CODE_TARGET, eq, a3, Operand(zero_reg));
170 }
171 }
172
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000173 // Strict mode functions and builtins need to replace the receiver
174 // with undefined when called as functions (without an explicit
175 // receiver object). t1 is zero for method calls and non-zero for
176 // function calls.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000177 if (!info->is_classic_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000178 Label ok;
179 __ Branch(&ok, eq, t1, Operand(zero_reg));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000180 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000181 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
182 __ sw(a2, MemOperand(sp, receiver_offset));
183 __ bind(&ok);
184 }
185
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000186 // Open a frame scope to indicate that there is a frame on the stack. The
187 // MANUAL indicates that the scope shouldn't actually generate code to set up
188 // the frame (that is done below).
189 FrameScope frame_scope(masm_, StackFrame::MANUAL);
190
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000191 int locals_count = info->scope()->num_stack_slots();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000192
193 __ Push(ra, fp, cp, a1);
194 if (locals_count > 0) {
195 // Load undefined value here, so the value is ready for the loop
196 // below.
197 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
198 }
199 // Adjust fp to point to caller's fp.
200 __ Addu(fp, sp, Operand(2 * kPointerSize));
201
202 { Comment cmnt(masm_, "[ Allocate locals");
203 for (int i = 0; i < locals_count; i++) {
204 __ push(at);
205 }
206 }
207
208 bool function_in_register = true;
209
210 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000211 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000212 if (heap_slots > 0) {
213 Comment cmnt(masm_, "[ Allocate local context");
214 // Argument to NewContext is the function, which is in a1.
215 __ push(a1);
216 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
217 FastNewContextStub stub(heap_slots);
218 __ CallStub(&stub);
219 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000220 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000221 }
222 function_in_register = false;
223 // Context is returned in both v0 and cp. It replaces the context
224 // passed to us. It's saved in the stack and kept live in cp.
225 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
226 // Copy any necessary parameters into the context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000227 int num_parameters = info->scope()->num_parameters();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000228 for (int i = 0; i < num_parameters; i++) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000229 Variable* var = scope()->parameter(i);
230 if (var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000231 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
232 (num_parameters - 1 - i) * kPointerSize;
233 // Load parameter from stack.
234 __ lw(a0, MemOperand(fp, parameter_offset));
235 // Store it in the context.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000236 MemOperand target = ContextOperand(cp, var->index());
237 __ sw(a0, target);
238
239 // Update the write barrier.
240 __ RecordWriteContextSlot(
241 cp, target.offset(), a0, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000242 }
243 }
244 }
245
246 Variable* arguments = scope()->arguments();
247 if (arguments != NULL) {
248 // Function uses arguments object.
249 Comment cmnt(masm_, "[ Allocate arguments object");
250 if (!function_in_register) {
251 // Load this again, if it's used by the local context below.
252 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
253 } else {
254 __ mov(a3, a1);
255 }
256 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000257 int num_parameters = info->scope()->num_parameters();
258 int offset = num_parameters * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000259 __ Addu(a2, fp,
260 Operand(StandardFrameConstants::kCallerSPOffset + offset));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000261 __ li(a1, Operand(Smi::FromInt(num_parameters)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000262 __ Push(a3, a2, a1);
263
264 // Arguments to ArgumentsAccessStub:
265 // function, receiver address, parameter count.
266 // The stub will rewrite receiever and parameter count if the previous
267 // stack frame was an arguments adapter frame.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000268 ArgumentsAccessStub::Type type;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000269 if (!is_classic_mode()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000270 type = ArgumentsAccessStub::NEW_STRICT;
271 } else if (function()->has_duplicate_parameters()) {
272 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
273 } else {
274 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
275 }
276 ArgumentsAccessStub stub(type);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000277 __ CallStub(&stub);
278
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000279 SetVar(arguments, v0, a1, a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000280 }
281
282 if (FLAG_trace) {
283 __ CallRuntime(Runtime::kTraceEnter, 0);
284 }
285
286 // Visit the declarations and body unless there is an illegal
287 // redeclaration.
288 if (scope()->HasIllegalRedeclaration()) {
289 Comment cmnt(masm_, "[ Declarations");
290 scope()->VisitIllegalRedeclaration(this);
291
292 } else {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000293 PrepareForBailoutForId(AstNode::kFunctionEntryId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000294 { Comment cmnt(masm_, "[ Declarations");
295 // For named function expressions, declare the function name as a
296 // constant.
297 if (scope()->is_function_scope() && scope()->function() != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000298 int ignored = 0;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000299 VariableProxy* proxy = scope()->function();
300 ASSERT(proxy->var()->mode() == CONST ||
301 proxy->var()->mode() == CONST_HARMONY);
302 EmitDeclaration(proxy, proxy->var()->mode(), NULL, &ignored);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000303 }
304 VisitDeclarations(scope()->declarations());
305 }
306
307 { Comment cmnt(masm_, "[ Stack check");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000308 PrepareForBailoutForId(AstNode::kDeclarationsId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000309 Label ok;
310 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
311 __ Branch(&ok, hs, sp, Operand(t0));
312 StackCheckStub stub;
313 __ CallStub(&stub);
314 __ bind(&ok);
315 }
316
317 { Comment cmnt(masm_, "[ Body");
318 ASSERT(loop_depth() == 0);
319 VisitStatements(function()->body());
320 ASSERT(loop_depth() == 0);
321 }
322 }
323
324 // Always emit a 'return undefined' in case control fell off the end of
325 // the body.
326 { Comment cmnt(masm_, "[ return <undefined>;");
327 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
328 }
329 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000330}
331
332
333void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000334 ASSERT(Smi::FromInt(0) == 0);
335 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000336}
337
338
339void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000340 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
341 // to make sure it is constant. Branch may emit a skip-or-jump sequence
342 // instead of the normal Branch. It seems that the "skip" part of that
343 // sequence is about as long as this Branch would be so it is safe to ignore
344 // that.
345 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000346 Comment cmnt(masm_, "[ Stack check");
347 Label ok;
348 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000349 __ sltu(at, sp, t0);
350 __ beq(at, zero_reg, &ok);
351 // CallStub will emit a li t9, ... first, so it is safe to use the delay slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000352 StackCheckStub stub;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000353 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000354 // Record a mapping of this PC offset to the OSR id. This is used to find
355 // the AST id from the unoptimized code in order to use it as a key into
356 // the deoptimization input data found in the optimized code.
357 RecordStackCheck(stmt->OsrEntryId());
358
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000359 __ bind(&ok);
360 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
361 // Record a mapping of the OSR id to this PC. This is used if the OSR
362 // entry becomes the target of a bailout. We don't expect it to be, but
363 // we want it to work if it is.
364 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000365}
366
367
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000368void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000369 Comment cmnt(masm_, "[ Return sequence");
370 if (return_label_.is_bound()) {
371 __ Branch(&return_label_);
372 } else {
373 __ bind(&return_label_);
374 if (FLAG_trace) {
375 // Push the return value on the stack as the parameter.
376 // Runtime::TraceExit returns its parameter in v0.
377 __ push(v0);
378 __ CallRuntime(Runtime::kTraceExit, 1);
379 }
380
381#ifdef DEBUG
382 // Add a label for checking the size of the code used for returning.
383 Label check_exit_codesize;
384 masm_->bind(&check_exit_codesize);
385#endif
386 // Make sure that the constant pool is not emitted inside of the return
387 // sequence.
388 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
389 // Here we use masm_-> instead of the __ macro to avoid the code coverage
390 // tool from instrumenting as we rely on the code size here.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000391 int32_t sp_delta = (info_->scope()->num_parameters() + 1) * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000392 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
393 __ RecordJSReturn();
394 masm_->mov(sp, fp);
395 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
396 masm_->Addu(sp, sp, Operand(sp_delta));
397 masm_->Jump(ra);
398 }
399
400#ifdef DEBUG
401 // Check that the size of the code used for returning is large enough
402 // for the debugger's requirements.
403 ASSERT(Assembler::kJSReturnSequenceInstructions <=
404 masm_->InstructionsGeneratedSince(&check_exit_codesize));
405#endif
406 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000407}
408
409
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000410void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
411 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
ager@chromium.org5c838252010-02-19 08:53:10 +0000412}
413
414
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000415void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
416 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
417 codegen()->GetVar(result_register(), var);
ager@chromium.org5c838252010-02-19 08:53:10 +0000418}
419
420
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000421void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
422 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
423 codegen()->GetVar(result_register(), var);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000424 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000425}
426
427
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000428void FullCodeGenerator::TestContext::Plug(Variable* var) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000429 // For simplicity we always test the accumulator register.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000430 codegen()->GetVar(result_register(), var);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000431 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000432 codegen()->DoTest(this);
ager@chromium.org5c838252010-02-19 08:53:10 +0000433}
434
435
lrn@chromium.org7516f052011-03-30 08:52:27 +0000436void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000437}
438
439
lrn@chromium.org7516f052011-03-30 08:52:27 +0000440void FullCodeGenerator::AccumulatorValueContext::Plug(
441 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000442 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000443}
444
445
446void FullCodeGenerator::StackValueContext::Plug(
447 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000448 __ LoadRoot(result_register(), index);
449 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000450}
451
452
453void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000454 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000455 true,
456 true_label_,
457 false_label_);
458 if (index == Heap::kUndefinedValueRootIndex ||
459 index == Heap::kNullValueRootIndex ||
460 index == Heap::kFalseValueRootIndex) {
461 if (false_label_ != fall_through_) __ Branch(false_label_);
462 } else if (index == Heap::kTrueValueRootIndex) {
463 if (true_label_ != fall_through_) __ Branch(true_label_);
464 } else {
465 __ LoadRoot(result_register(), index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000466 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000467 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000468}
469
470
471void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000472}
473
474
475void FullCodeGenerator::AccumulatorValueContext::Plug(
476 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000477 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000478}
479
480
481void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000482 // Immediates cannot be pushed directly.
483 __ li(result_register(), Operand(lit));
484 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000485}
486
487
488void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000489 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000490 true,
491 true_label_,
492 false_label_);
493 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
494 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
495 if (false_label_ != fall_through_) __ Branch(false_label_);
496 } else if (lit->IsTrue() || lit->IsJSObject()) {
497 if (true_label_ != fall_through_) __ Branch(true_label_);
498 } else if (lit->IsString()) {
499 if (String::cast(*lit)->length() == 0) {
500 if (false_label_ != fall_through_) __ Branch(false_label_);
501 } else {
502 if (true_label_ != fall_through_) __ Branch(true_label_);
503 }
504 } else if (lit->IsSmi()) {
505 if (Smi::cast(*lit)->value() == 0) {
506 if (false_label_ != fall_through_) __ Branch(false_label_);
507 } else {
508 if (true_label_ != fall_through_) __ Branch(true_label_);
509 }
510 } else {
511 // For simplicity we always test the accumulator register.
512 __ li(result_register(), Operand(lit));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000513 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000514 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000515}
516
517
518void FullCodeGenerator::EffectContext::DropAndPlug(int count,
519 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000520 ASSERT(count > 0);
521 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000522}
523
524
525void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
526 int count,
527 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000528 ASSERT(count > 0);
529 __ Drop(count);
530 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000531}
532
533
534void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
535 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000536 ASSERT(count > 0);
537 if (count > 1) __ Drop(count - 1);
538 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000539}
540
541
542void FullCodeGenerator::TestContext::DropAndPlug(int count,
543 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000544 ASSERT(count > 0);
545 // For simplicity we always test the accumulator register.
546 __ Drop(count);
547 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000548 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000549 codegen()->DoTest(this);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000550}
551
552
553void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
554 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000555 ASSERT(materialize_true == materialize_false);
556 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000557}
558
559
560void FullCodeGenerator::AccumulatorValueContext::Plug(
561 Label* materialize_true,
562 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000563 Label done;
564 __ bind(materialize_true);
565 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
566 __ Branch(&done);
567 __ bind(materialize_false);
568 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
569 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000570}
571
572
573void FullCodeGenerator::StackValueContext::Plug(
574 Label* materialize_true,
575 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000576 Label done;
577 __ bind(materialize_true);
578 __ LoadRoot(at, Heap::kTrueValueRootIndex);
579 __ push(at);
580 __ Branch(&done);
581 __ bind(materialize_false);
582 __ LoadRoot(at, Heap::kFalseValueRootIndex);
583 __ push(at);
584 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000585}
586
587
588void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
589 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000590 ASSERT(materialize_true == true_label_);
591 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000592}
593
594
595void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000596}
597
598
599void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000600 Heap::RootListIndex value_root_index =
601 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
602 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000603}
604
605
606void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000607 Heap::RootListIndex value_root_index =
608 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
609 __ LoadRoot(at, value_root_index);
610 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000611}
612
613
614void FullCodeGenerator::TestContext::Plug(bool flag) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000615 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000616 true,
617 true_label_,
618 false_label_);
619 if (flag) {
620 if (true_label_ != fall_through_) __ Branch(true_label_);
621 } else {
622 if (false_label_ != fall_through_) __ Branch(false_label_);
623 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000624}
625
626
whesse@chromium.org7b260152011-06-20 15:33:18 +0000627void FullCodeGenerator::DoTest(Expression* condition,
628 Label* if_true,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000629 Label* if_false,
630 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000631 if (CpuFeatures::IsSupported(FPU)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000632 ToBooleanStub stub(result_register());
633 __ CallStub(&stub);
634 __ mov(at, zero_reg);
635 } else {
636 // Call the runtime to find the boolean value of the source and then
637 // translate it into control flow to the pair of labels.
638 __ push(result_register());
639 __ CallRuntime(Runtime::kToBool, 1);
640 __ LoadRoot(at, Heap::kFalseValueRootIndex);
641 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000642 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000643}
644
645
lrn@chromium.org7516f052011-03-30 08:52:27 +0000646void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000647 Register lhs,
648 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000649 Label* if_true,
650 Label* if_false,
651 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000652 if (if_false == fall_through) {
653 __ Branch(if_true, cc, lhs, rhs);
654 } else if (if_true == fall_through) {
655 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
656 } else {
657 __ Branch(if_true, cc, lhs, rhs);
658 __ Branch(if_false);
659 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000660}
661
662
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000663MemOperand FullCodeGenerator::StackOperand(Variable* var) {
664 ASSERT(var->IsStackAllocated());
665 // Offset is negative because higher indexes are at lower addresses.
666 int offset = -var->index() * kPointerSize;
667 // Adjust by a (parameter or local) base offset.
668 if (var->IsParameter()) {
669 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
670 } else {
671 offset += JavaScriptFrameConstants::kLocal0Offset;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000672 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000673 return MemOperand(fp, offset);
ager@chromium.org5c838252010-02-19 08:53:10 +0000674}
675
676
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000677MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
678 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
679 if (var->IsContextSlot()) {
680 int context_chain_length = scope()->ContextChainLength(var->scope());
681 __ LoadContext(scratch, context_chain_length);
682 return ContextOperand(scratch, var->index());
683 } else {
684 return StackOperand(var);
685 }
686}
687
688
689void FullCodeGenerator::GetVar(Register dest, Variable* var) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000690 // Use destination as scratch.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000691 MemOperand location = VarOperand(var, dest);
692 __ lw(dest, location);
693}
694
695
696void FullCodeGenerator::SetVar(Variable* var,
697 Register src,
698 Register scratch0,
699 Register scratch1) {
700 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
701 ASSERT(!scratch0.is(src));
702 ASSERT(!scratch0.is(scratch1));
703 ASSERT(!scratch1.is(src));
704 MemOperand location = VarOperand(var, scratch0);
705 __ sw(src, location);
706 // Emit the write barrier code if the location is in the heap.
707 if (var->IsContextSlot()) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000708 __ RecordWriteContextSlot(scratch0,
709 location.offset(),
710 src,
711 scratch1,
712 kRAHasBeenSaved,
713 kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000714 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000715}
716
717
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000718void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000719 bool should_normalize,
720 Label* if_true,
721 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000722 // Only prepare for bailouts before splits if we're in a test
723 // context. Otherwise, we let the Visit function deal with the
724 // preparation to avoid preparing with the same AST id twice.
725 if (!context()->IsTest() || !info_->IsOptimizable()) return;
726
727 Label skip;
728 if (should_normalize) __ Branch(&skip);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000729 PrepareForBailout(expr, TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000730 if (should_normalize) {
731 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
732 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
733 __ bind(&skip);
734 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000735}
736
737
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000738void FullCodeGenerator::EmitDeclaration(VariableProxy* proxy,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000739 VariableMode mode,
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000740 FunctionLiteral* function,
741 int* global_count) {
742 // If it was not possible to allocate the variable at compile time, we
743 // need to "declare" it at runtime to make sure it actually exists in the
744 // local context.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000745 Variable* variable = proxy->var();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000746 bool binding_needs_init = (function == NULL) &&
747 (mode == CONST || mode == CONST_HARMONY || mode == LET);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000748 switch (variable->location()) {
749 case Variable::UNALLOCATED:
750 ++(*global_count);
751 break;
752
753 case Variable::PARAMETER:
754 case Variable::LOCAL:
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000755 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000756 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000757 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000758 __ sw(result_register(), StackOperand(variable));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000759 } else if (binding_needs_init) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000760 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000761 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000762 __ sw(t0, StackOperand(variable));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000763 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000764 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000765
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000766 case Variable::CONTEXT:
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000767 // The variable in the decl always resides in the current function
768 // context.
769 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
770 if (FLAG_debug_code) {
771 // Check that we're not inside a with or catch context.
772 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
773 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
774 __ Check(ne, "Declaration in with context.",
775 a1, Operand(t0));
776 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
777 __ Check(ne, "Declaration in catch context.",
778 a1, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000779 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000780 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000781 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000782 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000783 __ sw(result_register(), ContextOperand(cp, variable->index()));
784 int offset = Context::SlotOffset(variable->index());
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000785 // We know that we have written a function, which is not a smi.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000786 __ RecordWriteContextSlot(cp,
787 offset,
788 result_register(),
789 a2,
790 kRAHasBeenSaved,
791 kDontSaveFPRegs,
792 EMIT_REMEMBERED_SET,
793 OMIT_SMI_CHECK);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000794 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000795 } else if (binding_needs_init) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000796 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000797 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000798 __ sw(at, ContextOperand(cp, variable->index()));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000799 // No write barrier since the_hole_value is in old space.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000800 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000801 }
802 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000803
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000804 case Variable::LOOKUP: {
805 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000806 __ li(a2, Operand(variable->name()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000807 // Declaration nodes are always introduced in one of four modes.
808 ASSERT(mode == VAR ||
809 mode == CONST ||
810 mode == CONST_HARMONY ||
811 mode == LET);
812 PropertyAttributes attr = (mode == CONST || mode == CONST_HARMONY)
813 ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000814 __ li(a1, Operand(Smi::FromInt(attr)));
815 // Push initial value, if any.
816 // Note: For variables we must not push an initial value (such as
817 // 'undefined') because we may have a (legal) redeclaration and we
818 // must not destroy the current value.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000819 if (function != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000820 __ Push(cp, a2, a1);
821 // Push initial value for function declaration.
822 VisitForStackValue(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000823 } else if (binding_needs_init) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000824 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
825 __ Push(cp, a2, a1, a0);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000826 } else {
827 ASSERT(Smi::FromInt(0) == 0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000828 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000829 __ Push(cp, a2, a1, a0);
830 }
831 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
832 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000833 }
834 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000835}
836
837
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000838void FullCodeGenerator::VisitDeclaration(Declaration* decl) { }
ager@chromium.org5c838252010-02-19 08:53:10 +0000839
840
841void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000842 // Call the runtime to declare the globals.
843 // The context is the first argument.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000844 __ li(a1, Operand(pairs));
845 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
846 __ Push(cp, a1, a0);
847 __ CallRuntime(Runtime::kDeclareGlobals, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000848 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000849}
850
851
lrn@chromium.org7516f052011-03-30 08:52:27 +0000852void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000853 Comment cmnt(masm_, "[ SwitchStatement");
854 Breakable nested_statement(this, stmt);
855 SetStatementPosition(stmt);
856
857 // Keep the switch value on the stack until a case matches.
858 VisitForStackValue(stmt->tag());
859 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
860
861 ZoneList<CaseClause*>* clauses = stmt->cases();
862 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
863
864 Label next_test; // Recycled for each test.
865 // Compile all the tests with branches to their bodies.
866 for (int i = 0; i < clauses->length(); i++) {
867 CaseClause* clause = clauses->at(i);
868 clause->body_target()->Unuse();
869
870 // The default is not a test, but remember it as final fall through.
871 if (clause->is_default()) {
872 default_clause = clause;
873 continue;
874 }
875
876 Comment cmnt(masm_, "[ Case comparison");
877 __ bind(&next_test);
878 next_test.Unuse();
879
880 // Compile the label expression.
881 VisitForAccumulatorValue(clause->label());
882 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
883
884 // Perform the comparison as if via '==='.
885 __ lw(a1, MemOperand(sp, 0)); // Switch value.
886 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
887 JumpPatchSite patch_site(masm_);
888 if (inline_smi_code) {
889 Label slow_case;
890 __ or_(a2, a1, a0);
891 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
892
893 __ Branch(&next_test, ne, a1, Operand(a0));
894 __ Drop(1); // Switch value is no longer needed.
895 __ Branch(clause->body_target());
896
897 __ bind(&slow_case);
898 }
899
900 // Record position before stub call for type feedback.
901 SetSourcePosition(clause->position());
902 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000903 __ Call(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000904 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +0000905
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000906 __ Branch(&next_test, ne, v0, Operand(zero_reg));
907 __ Drop(1); // Switch value is no longer needed.
908 __ Branch(clause->body_target());
909 }
910
911 // Discard the test value and jump to the default if present, otherwise to
912 // the end of the statement.
913 __ bind(&next_test);
914 __ Drop(1); // Switch value is no longer needed.
915 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000916 __ Branch(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000917 } else {
918 __ Branch(default_clause->body_target());
919 }
920
921 // Compile all the case bodies.
922 for (int i = 0; i < clauses->length(); i++) {
923 Comment cmnt(masm_, "[ Case body");
924 CaseClause* clause = clauses->at(i);
925 __ bind(clause->body_target());
926 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
927 VisitStatements(clause->statements());
928 }
929
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000930 __ bind(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000931 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000932}
933
934
935void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000936 Comment cmnt(masm_, "[ ForInStatement");
937 SetStatementPosition(stmt);
938
939 Label loop, exit;
940 ForIn loop_statement(this, stmt);
941 increment_loop_depth();
942
943 // Get the object to enumerate over. Both SpiderMonkey and JSC
944 // ignore null and undefined in contrast to the specification; see
945 // ECMA-262 section 12.6.4.
946 VisitForAccumulatorValue(stmt->enumerable());
947 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
948 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
949 __ Branch(&exit, eq, a0, Operand(at));
950 Register null_value = t1;
951 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
952 __ Branch(&exit, eq, a0, Operand(null_value));
953
954 // Convert the object to a JS object.
955 Label convert, done_convert;
956 __ JumpIfSmi(a0, &convert);
957 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000958 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000959 __ bind(&convert);
960 __ push(a0);
961 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
962 __ mov(a0, v0);
963 __ bind(&done_convert);
964 __ push(a0);
965
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000966 // Check for proxies.
967 Label call_runtime;
968 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
969 __ GetObjectType(a0, a1, a1);
970 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
971
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000972 // Check cache validity in generated code. This is a fast case for
973 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
974 // guarantee cache validity, call the runtime system to check cache
975 // validity or get the property names in a fixed array.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000976 Label next;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000977 // Preload a couple of values used in the loop.
978 Register empty_fixed_array_value = t2;
979 __ LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
980 Register empty_descriptor_array_value = t3;
981 __ LoadRoot(empty_descriptor_array_value,
982 Heap::kEmptyDescriptorArrayRootIndex);
983 __ mov(a1, a0);
984 __ bind(&next);
985
986 // Check that there are no elements. Register a1 contains the
987 // current JS object we've reached through the prototype chain.
988 __ lw(a2, FieldMemOperand(a1, JSObject::kElementsOffset));
989 __ Branch(&call_runtime, ne, a2, Operand(empty_fixed_array_value));
990
991 // Check that instance descriptors are not empty so that we can
992 // check for an enum cache. Leave the map in a2 for the subsequent
993 // prototype load.
994 __ lw(a2, FieldMemOperand(a1, HeapObject::kMapOffset));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000995 __ lw(a3, FieldMemOperand(a2, Map::kInstanceDescriptorsOrBitField3Offset));
996 __ JumpIfSmi(a3, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000997
998 // Check that there is an enum cache in the non-empty instance
999 // descriptors (a3). This is the case if the next enumeration
1000 // index field does not contain a smi.
1001 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumerationIndexOffset));
1002 __ JumpIfSmi(a3, &call_runtime);
1003
1004 // For all objects but the receiver, check that the cache is empty.
1005 Label check_prototype;
1006 __ Branch(&check_prototype, eq, a1, Operand(a0));
1007 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumCacheBridgeCacheOffset));
1008 __ Branch(&call_runtime, ne, a3, Operand(empty_fixed_array_value));
1009
1010 // Load the prototype from the map and loop if non-null.
1011 __ bind(&check_prototype);
1012 __ lw(a1, FieldMemOperand(a2, Map::kPrototypeOffset));
1013 __ Branch(&next, ne, a1, Operand(null_value));
1014
1015 // The enum cache is valid. Load the map of the object being
1016 // iterated over and use the cache for the iteration.
1017 Label use_cache;
1018 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
1019 __ Branch(&use_cache);
1020
1021 // Get the set of properties to enumerate.
1022 __ bind(&call_runtime);
1023 __ push(a0); // Duplicate the enumerable object on the stack.
1024 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1025
1026 // If we got a map from the runtime call, we can do a fast
1027 // modification check. Otherwise, we got a fixed array, and we have
1028 // to do a slow check.
1029 Label fixed_array;
1030 __ mov(a2, v0);
1031 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
1032 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1033 __ Branch(&fixed_array, ne, a1, Operand(at));
1034
1035 // We got a map in register v0. Get the enumeration cache from it.
1036 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001037 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001038 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
1039 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1040
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001041 // Set up the four remaining stack slots.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001042 __ push(v0); // Map.
1043 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
1044 __ li(a0, Operand(Smi::FromInt(0)));
1045 // Push enumeration cache, enumeration cache length (as smi) and zero.
1046 __ Push(a2, a1, a0);
1047 __ jmp(&loop);
1048
1049 // We got a fixed array in register v0. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001050 Label non_proxy;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001051 __ bind(&fixed_array);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001052 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1053 __ lw(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1054 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1055 __ GetObjectType(a2, a3, a3);
1056 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1057 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1058 __ bind(&non_proxy);
1059 __ Push(a1, v0); // Smi and array
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001060 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1061 __ li(a0, Operand(Smi::FromInt(0)));
1062 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1063
1064 // Generate code for doing the condition check.
1065 __ bind(&loop);
1066 // Load the current count to a0, load the length to a1.
1067 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1068 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001069 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001070
1071 // Get the current entry of the array into register a3.
1072 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1073 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1074 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1075 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1076 __ lw(a3, MemOperand(t0)); // Current entry.
1077
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001078 // Get the expected map from the stack or a smi in the
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001079 // permanent slow case into register a2.
1080 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1081
1082 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001083 // If not, we may have to filter the key.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001084 Label update_each;
1085 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1086 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1087 __ Branch(&update_each, eq, t0, Operand(a2));
1088
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001089 // For proxies, no filtering is done.
1090 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1091 ASSERT_EQ(Smi::FromInt(0), 0);
1092 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1093
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001094 // Convert the entry to a string or (smi) 0 if it isn't a property
1095 // any more. If the property has been removed while iterating, we
1096 // just skip it.
1097 __ push(a1); // Enumerable.
1098 __ push(a3); // Current entry.
1099 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1100 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001101 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001102
1103 // Update the 'each' property or variable from the possibly filtered
1104 // entry in register a3.
1105 __ bind(&update_each);
1106 __ mov(result_register(), a3);
1107 // Perform the assignment as if via '='.
1108 { EffectContext context(this);
1109 EmitAssignment(stmt->each(), stmt->AssignmentId());
1110 }
1111
1112 // Generate code for the body of the loop.
1113 Visit(stmt->body());
1114
1115 // Generate code for the going to the next element by incrementing
1116 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001117 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001118 __ pop(a0);
1119 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1120 __ push(a0);
1121
1122 EmitStackCheck(stmt);
1123 __ Branch(&loop);
1124
1125 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001126 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001127 __ Drop(5);
1128
1129 // Exit and decrement the loop depth.
1130 __ bind(&exit);
1131 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001132}
1133
1134
1135void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1136 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001137 // Use the fast case closure allocation code that allocates in new
1138 // space for nested functions that don't need literals cloning. If
1139 // we're running with the --always-opt or the --prepare-always-opt
1140 // flag, we need to use the runtime function so that the new function
1141 // we are creating here gets a chance to have its code optimized and
1142 // doesn't just get a copy of the existing unoptimized code.
1143 if (!FLAG_always_opt &&
1144 !FLAG_prepare_always_opt &&
1145 !pretenure &&
1146 scope()->is_function_scope() &&
1147 info->num_literals() == 0) {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001148 FastNewClosureStub stub(info->language_mode());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001149 __ li(a0, Operand(info));
1150 __ push(a0);
1151 __ CallStub(&stub);
1152 } else {
1153 __ li(a0, Operand(info));
1154 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1155 : Heap::kFalseValueRootIndex);
1156 __ Push(cp, a0, a1);
1157 __ CallRuntime(Runtime::kNewClosure, 3);
1158 }
1159 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001160}
1161
1162
1163void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001164 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001165 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001166}
1167
1168
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001169void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1170 TypeofState typeof_state,
1171 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001172 Register current = cp;
1173 Register next = a1;
1174 Register temp = a2;
1175
1176 Scope* s = scope();
1177 while (s != NULL) {
1178 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001179 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001180 // Check that extension is NULL.
1181 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1182 __ Branch(slow, ne, temp, Operand(zero_reg));
1183 }
1184 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001185 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001186 // Walk the rest of the chain without clobbering cp.
1187 current = next;
1188 }
1189 // If no outer scope calls eval, we do not need to check more
1190 // context extensions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001191 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001192 s = s->outer_scope();
1193 }
1194
1195 if (s->is_eval_scope()) {
1196 Label loop, fast;
1197 if (!current.is(next)) {
1198 __ Move(next, current);
1199 }
1200 __ bind(&loop);
1201 // Terminate at global context.
1202 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1203 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1204 __ Branch(&fast, eq, temp, Operand(t0));
1205 // Check that extension is NULL.
1206 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1207 __ Branch(slow, ne, temp, Operand(zero_reg));
1208 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001209 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001210 __ Branch(&loop);
1211 __ bind(&fast);
1212 }
1213
1214 __ lw(a0, GlobalObjectOperand());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001215 __ li(a2, Operand(var->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001216 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1217 ? RelocInfo::CODE_TARGET
1218 : RelocInfo::CODE_TARGET_CONTEXT;
1219 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001220 __ Call(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001221}
1222
1223
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001224MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1225 Label* slow) {
1226 ASSERT(var->IsContextSlot());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001227 Register context = cp;
1228 Register next = a3;
1229 Register temp = t0;
1230
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001231 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001232 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001233 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001234 // Check that extension is NULL.
1235 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1236 __ Branch(slow, ne, temp, Operand(zero_reg));
1237 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001238 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001239 // Walk the rest of the chain without clobbering cp.
1240 context = next;
1241 }
1242 }
1243 // Check that last extension is NULL.
1244 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1245 __ Branch(slow, ne, temp, Operand(zero_reg));
1246
1247 // This function is used only for loads, not stores, so it's safe to
1248 // return an cp-based operand (the write barrier cannot be allowed to
1249 // destroy the cp register).
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001250 return ContextOperand(context, var->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001251}
1252
1253
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001254void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1255 TypeofState typeof_state,
1256 Label* slow,
1257 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001258 // Generate fast-case code for variables that might be shadowed by
1259 // eval-introduced variables. Eval is used a lot without
1260 // introducing variables. In those cases, we do not want to
1261 // perform a runtime call for all variables in the scope
1262 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001263 if (var->mode() == DYNAMIC_GLOBAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001264 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001265 __ Branch(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001266 } else if (var->mode() == DYNAMIC_LOCAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001267 Variable* local = var->local_if_not_shadowed();
1268 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001269 if (local->mode() == CONST ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001270 local->mode() == CONST_HARMONY ||
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001271 local->mode() == LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001272 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1273 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001274 if (local->mode() == CONST) {
1275 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1276 __ movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001277 } else { // LET || CONST_HARMONY
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001278 __ Branch(done, ne, at, Operand(zero_reg));
1279 __ li(a0, Operand(var->name()));
1280 __ push(a0);
1281 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1282 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001283 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001284 __ Branch(done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001285 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001286}
1287
1288
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001289void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1290 // Record position before possible IC call.
1291 SetSourcePosition(proxy->position());
1292 Variable* var = proxy->var();
1293
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001294 // Three cases: global variables, lookup variables, and all other types of
1295 // variables.
1296 switch (var->location()) {
1297 case Variable::UNALLOCATED: {
1298 Comment cmnt(masm_, "Global variable");
1299 // Use inline caching. Variable name is passed in a2 and the global
1300 // object (receiver) in a0.
1301 __ lw(a0, GlobalObjectOperand());
1302 __ li(a2, Operand(var->name()));
1303 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1304 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
1305 context()->Plug(v0);
1306 break;
1307 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001308
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001309 case Variable::PARAMETER:
1310 case Variable::LOCAL:
1311 case Variable::CONTEXT: {
1312 Comment cmnt(masm_, var->IsContextSlot()
1313 ? "Context variable"
1314 : "Stack variable");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001315 if (var->binding_needs_init()) {
1316 // var->scope() may be NULL when the proxy is located in eval code and
1317 // refers to a potential outside binding. Currently those bindings are
1318 // always looked up dynamically, i.e. in that case
1319 // var->location() == LOOKUP.
1320 // always holds.
1321 ASSERT(var->scope() != NULL);
1322
1323 // Check if the binding really needs an initialization check. The check
1324 // can be skipped in the following situation: we have a LET or CONST
1325 // binding in harmony mode, both the Variable and the VariableProxy have
1326 // the same declaration scope (i.e. they are both in global code, in the
1327 // same function or in the same eval code) and the VariableProxy is in
1328 // the source physically located after the initializer of the variable.
1329 //
1330 // We cannot skip any initialization checks for CONST in non-harmony
1331 // mode because const variables may be declared but never initialized:
1332 // if (false) { const x; }; var y = x;
1333 //
1334 // The condition on the declaration scopes is a conservative check for
1335 // nested functions that access a binding and are called before the
1336 // binding is initialized:
1337 // function() { f(); let x = 1; function f() { x = 2; } }
1338 //
1339 bool skip_init_check;
1340 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1341 skip_init_check = false;
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001342 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001343 // Check that we always have valid source position.
1344 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1345 ASSERT(proxy->position() != RelocInfo::kNoPosition);
1346 skip_init_check = var->mode() != CONST &&
1347 var->initializer_position() < proxy->position();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001348 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001349
1350 if (!skip_init_check) {
1351 // Let and const need a read barrier.
1352 GetVar(v0, var);
1353 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1354 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1355 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1356 // Throw a reference error when using an uninitialized let/const
1357 // binding in harmony mode.
1358 Label done;
1359 __ Branch(&done, ne, at, Operand(zero_reg));
1360 __ li(a0, Operand(var->name()));
1361 __ push(a0);
1362 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1363 __ bind(&done);
1364 } else {
1365 // Uninitalized const bindings outside of harmony mode are unholed.
1366 ASSERT(var->mode() == CONST);
1367 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1368 __ movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1369 }
1370 context()->Plug(v0);
1371 break;
1372 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001373 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001374 context()->Plug(var);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001375 break;
1376 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001377
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001378 case Variable::LOOKUP: {
1379 Label done, slow;
1380 // Generate code for loading from variables potentially shadowed
1381 // by eval-introduced variables.
1382 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1383 __ bind(&slow);
1384 Comment cmnt(masm_, "Lookup variable");
1385 __ li(a1, Operand(var->name()));
1386 __ Push(cp, a1); // Context and name.
1387 __ CallRuntime(Runtime::kLoadContextSlot, 2);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001388 __ bind(&done);
1389 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001390 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001391 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001392}
1393
1394
1395void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001396 Comment cmnt(masm_, "[ RegExpLiteral");
1397 Label materialized;
1398 // Registers will be used as follows:
1399 // t1 = materialized value (RegExp literal)
1400 // t0 = JS function, literals array
1401 // a3 = literal index
1402 // a2 = RegExp pattern
1403 // a1 = RegExp flags
1404 // a0 = RegExp literal clone
1405 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1406 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1407 int literal_offset =
1408 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1409 __ lw(t1, FieldMemOperand(t0, literal_offset));
1410 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1411 __ Branch(&materialized, ne, t1, Operand(at));
1412
1413 // Create regexp literal using runtime function.
1414 // Result will be in v0.
1415 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1416 __ li(a2, Operand(expr->pattern()));
1417 __ li(a1, Operand(expr->flags()));
1418 __ Push(t0, a3, a2, a1);
1419 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1420 __ mov(t1, v0);
1421
1422 __ bind(&materialized);
1423 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1424 Label allocated, runtime_allocate;
1425 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1426 __ jmp(&allocated);
1427
1428 __ bind(&runtime_allocate);
1429 __ push(t1);
1430 __ li(a0, Operand(Smi::FromInt(size)));
1431 __ push(a0);
1432 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1433 __ pop(t1);
1434
1435 __ bind(&allocated);
1436
1437 // After this, registers are used as follows:
1438 // v0: Newly allocated regexp.
1439 // t1: Materialized regexp.
1440 // a2: temp.
1441 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1442 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001443}
1444
1445
1446void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001447 Comment cmnt(masm_, "[ ObjectLiteral");
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001448 Handle<FixedArray> constant_properties = expr->constant_properties();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001449 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1450 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1451 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001452 __ li(a1, Operand(constant_properties));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001453 int flags = expr->fast_elements()
1454 ? ObjectLiteral::kFastElements
1455 : ObjectLiteral::kNoFlags;
1456 flags |= expr->has_function()
1457 ? ObjectLiteral::kHasFunction
1458 : ObjectLiteral::kNoFlags;
1459 __ li(a0, Operand(Smi::FromInt(flags)));
1460 __ Push(a3, a2, a1, a0);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001461 int properties_count = constant_properties->length() / 2;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001462 if (expr->depth() > 1) {
1463 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001464 } else if (flags != ObjectLiteral::kFastElements ||
1465 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001466 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001467 } else {
1468 FastCloneShallowObjectStub stub(properties_count);
1469 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001470 }
1471
1472 // If result_saved is true the result is on top of the stack. If
1473 // result_saved is false the result is in v0.
1474 bool result_saved = false;
1475
1476 // Mark all computed expressions that are bound to a key that
1477 // is shadowed by a later occurrence of the same key. For the
1478 // marked expressions, no store code is emitted.
1479 expr->CalculateEmitStore();
1480
1481 for (int i = 0; i < expr->properties()->length(); i++) {
1482 ObjectLiteral::Property* property = expr->properties()->at(i);
1483 if (property->IsCompileTimeValue()) continue;
1484
1485 Literal* key = property->key();
1486 Expression* value = property->value();
1487 if (!result_saved) {
1488 __ push(v0); // Save result on stack.
1489 result_saved = true;
1490 }
1491 switch (property->kind()) {
1492 case ObjectLiteral::Property::CONSTANT:
1493 UNREACHABLE();
1494 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1495 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1496 // Fall through.
1497 case ObjectLiteral::Property::COMPUTED:
1498 if (key->handle()->IsSymbol()) {
1499 if (property->emit_store()) {
1500 VisitForAccumulatorValue(value);
1501 __ mov(a0, result_register());
1502 __ li(a2, Operand(key->handle()));
1503 __ lw(a1, MemOperand(sp));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001504 Handle<Code> ic = is_classic_mode()
1505 ? isolate()->builtins()->StoreIC_Initialize()
1506 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001507 __ Call(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001508 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1509 } else {
1510 VisitForEffect(value);
1511 }
1512 break;
1513 }
1514 // Fall through.
1515 case ObjectLiteral::Property::PROTOTYPE:
1516 // Duplicate receiver on stack.
1517 __ lw(a0, MemOperand(sp));
1518 __ push(a0);
1519 VisitForStackValue(key);
1520 VisitForStackValue(value);
1521 if (property->emit_store()) {
1522 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1523 __ push(a0);
1524 __ CallRuntime(Runtime::kSetProperty, 4);
1525 } else {
1526 __ Drop(3);
1527 }
1528 break;
1529 case ObjectLiteral::Property::GETTER:
1530 case ObjectLiteral::Property::SETTER:
1531 // Duplicate receiver on stack.
1532 __ lw(a0, MemOperand(sp));
1533 __ push(a0);
1534 VisitForStackValue(key);
1535 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1536 Smi::FromInt(1) :
1537 Smi::FromInt(0)));
1538 __ push(a1);
1539 VisitForStackValue(value);
1540 __ CallRuntime(Runtime::kDefineAccessor, 4);
1541 break;
1542 }
1543 }
1544
1545 if (expr->has_function()) {
1546 ASSERT(result_saved);
1547 __ lw(a0, MemOperand(sp));
1548 __ push(a0);
1549 __ CallRuntime(Runtime::kToFastProperties, 1);
1550 }
1551
1552 if (result_saved) {
1553 context()->PlugTOS();
1554 } else {
1555 context()->Plug(v0);
1556 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001557}
1558
1559
1560void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001561 Comment cmnt(masm_, "[ ArrayLiteral");
1562
1563 ZoneList<Expression*>* subexprs = expr->values();
1564 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001565
1566 Handle<FixedArray> constant_elements = expr->constant_elements();
1567 ASSERT_EQ(2, constant_elements->length());
1568 ElementsKind constant_elements_kind =
1569 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001570 bool has_fast_elements = constant_elements_kind == FAST_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001571 Handle<FixedArrayBase> constant_elements_values(
1572 FixedArrayBase::cast(constant_elements->get(1)));
1573
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001574 __ mov(a0, result_register());
1575 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1576 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1577 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001578 __ li(a1, Operand(constant_elements));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001579 __ Push(a3, a2, a1);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001580 if (has_fast_elements && constant_elements_values->map() ==
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001581 isolate()->heap()->fixed_cow_array_map()) {
1582 FastCloneShallowArrayStub stub(
1583 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1584 __ CallStub(&stub);
1585 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1586 1, a1, a2);
1587 } else if (expr->depth() > 1) {
1588 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1589 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1590 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1591 } else {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001592 ASSERT(constant_elements_kind == FAST_ELEMENTS ||
1593 constant_elements_kind == FAST_SMI_ONLY_ELEMENTS ||
1594 FLAG_smi_only_arrays);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001595 FastCloneShallowArrayStub::Mode mode = has_fast_elements
1596 ? FastCloneShallowArrayStub::CLONE_ELEMENTS
1597 : FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001598 FastCloneShallowArrayStub stub(mode, length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001599 __ CallStub(&stub);
1600 }
1601
1602 bool result_saved = false; // Is the result saved to the stack?
1603
1604 // Emit code to evaluate all the non-constant subexpressions and to store
1605 // them into the newly cloned array.
1606 for (int i = 0; i < length; i++) {
1607 Expression* subexpr = subexprs->at(i);
1608 // If the subexpression is a literal or a simple materialized literal it
1609 // is already set in the cloned array.
1610 if (subexpr->AsLiteral() != NULL ||
1611 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1612 continue;
1613 }
1614
1615 if (!result_saved) {
1616 __ push(v0);
1617 result_saved = true;
1618 }
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001619
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001620 VisitForAccumulatorValue(subexpr);
1621
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001622 if (constant_elements_kind == FAST_ELEMENTS) {
1623 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1624 __ lw(t2, MemOperand(sp)); // Copy of array literal.
1625 __ lw(a1, FieldMemOperand(t2, JSObject::kElementsOffset));
1626 __ sw(result_register(), FieldMemOperand(a1, offset));
1627 // Update the write barrier for the array store.
1628 __ RecordWriteField(a1, offset, result_register(), a2,
1629 kRAHasBeenSaved, kDontSaveFPRegs,
1630 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1631 } else {
1632 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1633 __ lw(a2, FieldMemOperand(a1, JSObject::kMapOffset));
1634 __ li(a3, Operand(Smi::FromInt(i)));
1635 __ li(t0, Operand(Smi::FromInt(expr->literal_index())));
1636 __ mov(a0, result_register());
1637 StoreArrayLiteralElementStub stub;
1638 __ CallStub(&stub);
1639 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001640
1641 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1642 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001643 if (result_saved) {
1644 context()->PlugTOS();
1645 } else {
1646 context()->Plug(v0);
1647 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001648}
1649
1650
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001651void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001652 Comment cmnt(masm_, "[ Assignment");
1653 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1654 // on the left-hand side.
1655 if (!expr->target()->IsValidLeftHandSide()) {
1656 VisitForEffect(expr->target());
1657 return;
1658 }
1659
1660 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001661 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001662 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1663 LhsKind assign_type = VARIABLE;
1664 Property* property = expr->target()->AsProperty();
1665 if (property != NULL) {
1666 assign_type = (property->key()->IsPropertyName())
1667 ? NAMED_PROPERTY
1668 : KEYED_PROPERTY;
1669 }
1670
1671 // Evaluate LHS expression.
1672 switch (assign_type) {
1673 case VARIABLE:
1674 // Nothing to do here.
1675 break;
1676 case NAMED_PROPERTY:
1677 if (expr->is_compound()) {
1678 // We need the receiver both on the stack and in the accumulator.
1679 VisitForAccumulatorValue(property->obj());
1680 __ push(result_register());
1681 } else {
1682 VisitForStackValue(property->obj());
1683 }
1684 break;
1685 case KEYED_PROPERTY:
1686 // We need the key and receiver on both the stack and in v0 and a1.
1687 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001688 VisitForStackValue(property->obj());
1689 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001690 __ lw(a1, MemOperand(sp, 0));
1691 __ push(v0);
1692 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001693 VisitForStackValue(property->obj());
1694 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001695 }
1696 break;
1697 }
1698
1699 // For compound assignments we need another deoptimization point after the
1700 // variable/property load.
1701 if (expr->is_compound()) {
1702 { AccumulatorValueContext context(this);
1703 switch (assign_type) {
1704 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001705 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001706 PrepareForBailout(expr->target(), TOS_REG);
1707 break;
1708 case NAMED_PROPERTY:
1709 EmitNamedPropertyLoad(property);
1710 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1711 break;
1712 case KEYED_PROPERTY:
1713 EmitKeyedPropertyLoad(property);
1714 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1715 break;
1716 }
1717 }
1718
1719 Token::Value op = expr->binary_op();
1720 __ push(v0); // Left operand goes on the stack.
1721 VisitForAccumulatorValue(expr->value());
1722
1723 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1724 ? OVERWRITE_RIGHT
1725 : NO_OVERWRITE;
1726 SetSourcePosition(expr->position() + 1);
1727 AccumulatorValueContext context(this);
1728 if (ShouldInlineSmiCase(op)) {
1729 EmitInlineSmiBinaryOp(expr->binary_operation(),
1730 op,
1731 mode,
1732 expr->target(),
1733 expr->value());
1734 } else {
1735 EmitBinaryOp(expr->binary_operation(), op, mode);
1736 }
1737
1738 // Deoptimization point in case the binary operation may have side effects.
1739 PrepareForBailout(expr->binary_operation(), TOS_REG);
1740 } else {
1741 VisitForAccumulatorValue(expr->value());
1742 }
1743
1744 // Record source position before possible IC call.
1745 SetSourcePosition(expr->position());
1746
1747 // Store the value.
1748 switch (assign_type) {
1749 case VARIABLE:
1750 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1751 expr->op());
1752 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1753 context()->Plug(v0);
1754 break;
1755 case NAMED_PROPERTY:
1756 EmitNamedPropertyAssignment(expr);
1757 break;
1758 case KEYED_PROPERTY:
1759 EmitKeyedPropertyAssignment(expr);
1760 break;
1761 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001762}
1763
1764
ager@chromium.org5c838252010-02-19 08:53:10 +00001765void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001766 SetSourcePosition(prop->position());
1767 Literal* key = prop->key()->AsLiteral();
1768 __ mov(a0, result_register());
1769 __ li(a2, Operand(key->handle()));
1770 // Call load IC. It has arguments receiver and property name a0 and a2.
1771 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001772 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001773}
1774
1775
1776void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001777 SetSourcePosition(prop->position());
1778 __ mov(a0, result_register());
1779 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1780 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001781 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001782}
1783
1784
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001785void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001786 Token::Value op,
1787 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001788 Expression* left_expr,
1789 Expression* right_expr) {
1790 Label done, smi_case, stub_call;
1791
1792 Register scratch1 = a2;
1793 Register scratch2 = a3;
1794
1795 // Get the arguments.
1796 Register left = a1;
1797 Register right = a0;
1798 __ pop(left);
1799 __ mov(a0, result_register());
1800
1801 // Perform combined smi check on both operands.
1802 __ Or(scratch1, left, Operand(right));
1803 STATIC_ASSERT(kSmiTag == 0);
1804 JumpPatchSite patch_site(masm_);
1805 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1806
1807 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001808 BinaryOpStub stub(op, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001809 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001810 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001811 __ jmp(&done);
1812
1813 __ bind(&smi_case);
1814 // Smi case. This code works the same way as the smi-smi case in the type
1815 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001816 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001817 switch (op) {
1818 case Token::SAR:
1819 __ Branch(&stub_call);
1820 __ GetLeastBitsFromSmi(scratch1, right, 5);
1821 __ srav(right, left, scratch1);
1822 __ And(v0, right, Operand(~kSmiTagMask));
1823 break;
1824 case Token::SHL: {
1825 __ Branch(&stub_call);
1826 __ SmiUntag(scratch1, left);
1827 __ GetLeastBitsFromSmi(scratch2, right, 5);
1828 __ sllv(scratch1, scratch1, scratch2);
1829 __ Addu(scratch2, scratch1, Operand(0x40000000));
1830 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1831 __ SmiTag(v0, scratch1);
1832 break;
1833 }
1834 case Token::SHR: {
1835 __ Branch(&stub_call);
1836 __ SmiUntag(scratch1, left);
1837 __ GetLeastBitsFromSmi(scratch2, right, 5);
1838 __ srlv(scratch1, scratch1, scratch2);
1839 __ And(scratch2, scratch1, 0xc0000000);
1840 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1841 __ SmiTag(v0, scratch1);
1842 break;
1843 }
1844 case Token::ADD:
1845 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1846 __ BranchOnOverflow(&stub_call, scratch1);
1847 break;
1848 case Token::SUB:
1849 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1850 __ BranchOnOverflow(&stub_call, scratch1);
1851 break;
1852 case Token::MUL: {
1853 __ SmiUntag(scratch1, right);
1854 __ Mult(left, scratch1);
1855 __ mflo(scratch1);
1856 __ mfhi(scratch2);
1857 __ sra(scratch1, scratch1, 31);
1858 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1859 __ mflo(v0);
1860 __ Branch(&done, ne, v0, Operand(zero_reg));
1861 __ Addu(scratch2, right, left);
1862 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1863 ASSERT(Smi::FromInt(0) == 0);
1864 __ mov(v0, zero_reg);
1865 break;
1866 }
1867 case Token::BIT_OR:
1868 __ Or(v0, left, Operand(right));
1869 break;
1870 case Token::BIT_AND:
1871 __ And(v0, left, Operand(right));
1872 break;
1873 case Token::BIT_XOR:
1874 __ Xor(v0, left, Operand(right));
1875 break;
1876 default:
1877 UNREACHABLE();
1878 }
1879
1880 __ bind(&done);
1881 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001882}
1883
1884
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001885void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1886 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001887 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001888 __ mov(a0, result_register());
1889 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001890 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001891 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001892 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001893 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001894 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001895}
1896
1897
1898void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001899 // Invalid left-hand sides are rewritten to have a 'throw
1900 // ReferenceError' on the left-hand side.
1901 if (!expr->IsValidLeftHandSide()) {
1902 VisitForEffect(expr);
1903 return;
1904 }
1905
1906 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001907 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001908 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1909 LhsKind assign_type = VARIABLE;
1910 Property* prop = expr->AsProperty();
1911 if (prop != NULL) {
1912 assign_type = (prop->key()->IsPropertyName())
1913 ? NAMED_PROPERTY
1914 : KEYED_PROPERTY;
1915 }
1916
1917 switch (assign_type) {
1918 case VARIABLE: {
1919 Variable* var = expr->AsVariableProxy()->var();
1920 EffectContext context(this);
1921 EmitVariableAssignment(var, Token::ASSIGN);
1922 break;
1923 }
1924 case NAMED_PROPERTY: {
1925 __ push(result_register()); // Preserve value.
1926 VisitForAccumulatorValue(prop->obj());
1927 __ mov(a1, result_register());
1928 __ pop(a0); // Restore value.
1929 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001930 Handle<Code> ic = is_classic_mode()
1931 ? isolate()->builtins()->StoreIC_Initialize()
1932 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001933 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001934 break;
1935 }
1936 case KEYED_PROPERTY: {
1937 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001938 VisitForStackValue(prop->obj());
1939 VisitForAccumulatorValue(prop->key());
1940 __ mov(a1, result_register());
1941 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001942 __ pop(a0); // Restore value.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001943 Handle<Code> ic = is_classic_mode()
1944 ? isolate()->builtins()->KeyedStoreIC_Initialize()
1945 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001946 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001947 break;
1948 }
1949 }
1950 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1951 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001952}
1953
1954
1955void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001956 Token::Value op) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001957 if (var->IsUnallocated()) {
1958 // Global var, const, or let.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001959 __ mov(a0, result_register());
1960 __ li(a2, Operand(var->name()));
1961 __ lw(a1, GlobalObjectOperand());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001962 Handle<Code> ic = is_classic_mode()
1963 ? isolate()->builtins()->StoreIC_Initialize()
1964 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001965 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001966
1967 } else if (op == Token::INIT_CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001968 // Const initializers need a write barrier.
1969 ASSERT(!var->IsParameter()); // No const parameters.
1970 if (var->IsStackLocal()) {
1971 Label skip;
1972 __ lw(a1, StackOperand(var));
1973 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1974 __ Branch(&skip, ne, a1, Operand(t0));
1975 __ sw(result_register(), StackOperand(var));
1976 __ bind(&skip);
1977 } else {
1978 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
1979 // Like var declarations, const declarations are hoisted to function
1980 // scope. However, unlike var initializers, const initializers are
1981 // able to drill a hole to that function context, even from inside a
1982 // 'with' context. We thus bypass the normal static scope lookup for
1983 // var->IsContextSlot().
1984 __ push(v0);
1985 __ li(a0, Operand(var->name()));
1986 __ Push(cp, a0); // Context and name.
1987 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001988 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001989
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001990 } else if (var->mode() == LET && op != Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001991 // Non-initializing assignment to let variable needs a write barrier.
1992 if (var->IsLookupSlot()) {
1993 __ push(v0); // Value.
1994 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001995 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001996 __ Push(cp, a1, a0); // Context, name, strict mode.
1997 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1998 } else {
1999 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
2000 Label assign;
2001 MemOperand location = VarOperand(var, a1);
2002 __ lw(a3, location);
2003 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2004 __ Branch(&assign, ne, a3, Operand(t0));
2005 __ li(a3, Operand(var->name()));
2006 __ push(a3);
2007 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2008 // Perform the assignment.
2009 __ bind(&assign);
2010 __ sw(result_register(), location);
2011 if (var->IsContextSlot()) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002012 // RecordWrite may destroy all its register arguments.
2013 __ mov(a3, result_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002014 int offset = Context::SlotOffset(var->index());
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002015 __ RecordWriteContextSlot(
2016 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002017 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002018 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002019
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002020 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2021 // Assignment to var or initializing assignment to let/const
2022 // in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002023 if (var->IsStackAllocated() || var->IsContextSlot()) {
2024 MemOperand location = VarOperand(var, a1);
2025 if (FLAG_debug_code && op == Token::INIT_LET) {
2026 // Check for an uninitialized let binding.
2027 __ lw(a2, location);
2028 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2029 __ Check(eq, "Let binding re-initialization.", a2, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002030 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002031 // Perform the assignment.
2032 __ sw(v0, location);
2033 if (var->IsContextSlot()) {
2034 __ mov(a3, v0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002035 int offset = Context::SlotOffset(var->index());
2036 __ RecordWriteContextSlot(
2037 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002038 }
2039 } else {
2040 ASSERT(var->IsLookupSlot());
2041 __ push(v0); // Value.
2042 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002043 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002044 __ Push(cp, a1, a0); // Context, name, strict mode.
2045 __ CallRuntime(Runtime::kStoreContextSlot, 4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002046 }
2047 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002048 // Non-initializing assignments to consts are ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +00002049}
2050
2051
2052void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002053 // Assignment to a property, using a named store IC.
2054 Property* prop = expr->target()->AsProperty();
2055 ASSERT(prop != NULL);
2056 ASSERT(prop->key()->AsLiteral() != NULL);
2057
2058 // If the assignment starts a block of assignments to the same object,
2059 // change to slow case to avoid the quadratic behavior of repeatedly
2060 // adding fast properties.
2061 if (expr->starts_initialization_block()) {
2062 __ push(result_register());
2063 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
2064 __ push(t0);
2065 __ CallRuntime(Runtime::kToSlowProperties, 1);
2066 __ pop(result_register());
2067 }
2068
2069 // Record source code position before IC call.
2070 SetSourcePosition(expr->position());
2071 __ mov(a0, result_register()); // Load the value.
2072 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
2073 // Load receiver to a1. Leave a copy in the stack if needed for turning the
2074 // receiver into fast case.
2075 if (expr->ends_initialization_block()) {
2076 __ lw(a1, MemOperand(sp));
2077 } else {
2078 __ pop(a1);
2079 }
2080
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002081 Handle<Code> ic = is_classic_mode()
2082 ? isolate()->builtins()->StoreIC_Initialize()
2083 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002084 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002085
2086 // If the assignment ends an initialization block, revert to fast case.
2087 if (expr->ends_initialization_block()) {
2088 __ push(v0); // Result of assignment, saved even if not needed.
2089 // Receiver is under the result value.
2090 __ lw(t0, MemOperand(sp, kPointerSize));
2091 __ push(t0);
2092 __ CallRuntime(Runtime::kToFastProperties, 1);
2093 __ pop(v0);
2094 __ Drop(1);
2095 }
2096 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2097 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002098}
2099
2100
2101void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002102 // Assignment to a property, using a keyed store IC.
2103
2104 // If the assignment starts a block of assignments to the same object,
2105 // change to slow case to avoid the quadratic behavior of repeatedly
2106 // adding fast properties.
2107 if (expr->starts_initialization_block()) {
2108 __ push(result_register());
2109 // Receiver is now under the key and value.
2110 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2111 __ push(t0);
2112 __ CallRuntime(Runtime::kToSlowProperties, 1);
2113 __ pop(result_register());
2114 }
2115
2116 // Record source code position before IC call.
2117 SetSourcePosition(expr->position());
2118 // Call keyed store IC.
2119 // The arguments are:
2120 // - a0 is the value,
2121 // - a1 is the key,
2122 // - a2 is the receiver.
2123 __ mov(a0, result_register());
2124 __ pop(a1); // Key.
2125 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2126 // receiver into fast case.
2127 if (expr->ends_initialization_block()) {
2128 __ lw(a2, MemOperand(sp));
2129 } else {
2130 __ pop(a2);
2131 }
2132
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002133 Handle<Code> ic = is_classic_mode()
2134 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2135 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002136 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002137
2138 // If the assignment ends an initialization block, revert to fast case.
2139 if (expr->ends_initialization_block()) {
2140 __ push(v0); // Result of assignment, saved even if not needed.
2141 // Receiver is under the result value.
2142 __ lw(t0, MemOperand(sp, kPointerSize));
2143 __ push(t0);
2144 __ CallRuntime(Runtime::kToFastProperties, 1);
2145 __ pop(v0);
2146 __ Drop(1);
2147 }
2148 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2149 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002150}
2151
2152
2153void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002154 Comment cmnt(masm_, "[ Property");
2155 Expression* key = expr->key();
2156
2157 if (key->IsPropertyName()) {
2158 VisitForAccumulatorValue(expr->obj());
2159 EmitNamedPropertyLoad(expr);
2160 context()->Plug(v0);
2161 } else {
2162 VisitForStackValue(expr->obj());
2163 VisitForAccumulatorValue(expr->key());
2164 __ pop(a1);
2165 EmitKeyedPropertyLoad(expr);
2166 context()->Plug(v0);
2167 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002168}
2169
lrn@chromium.org7516f052011-03-30 08:52:27 +00002170
ager@chromium.org5c838252010-02-19 08:53:10 +00002171void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002172 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002173 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002174 // Code common for calls using the IC.
2175 ZoneList<Expression*>* args = expr->arguments();
2176 int arg_count = args->length();
2177 { PreservePositionScope scope(masm()->positions_recorder());
2178 for (int i = 0; i < arg_count; i++) {
2179 VisitForStackValue(args->at(i));
2180 }
2181 __ li(a2, Operand(name));
2182 }
2183 // Record source position for debugger.
2184 SetSourcePosition(expr->position());
2185 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002186 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002187 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002188 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002189 RecordJSReturnSite(expr);
2190 // Restore context register.
2191 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2192 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002193}
2194
2195
lrn@chromium.org7516f052011-03-30 08:52:27 +00002196void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002197 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002198 // Load the key.
2199 VisitForAccumulatorValue(key);
2200
2201 // Swap the name of the function and the receiver on the stack to follow
2202 // the calling convention for call ICs.
2203 __ pop(a1);
2204 __ push(v0);
2205 __ push(a1);
2206
2207 // Code common for calls using the IC.
2208 ZoneList<Expression*>* args = expr->arguments();
2209 int arg_count = args->length();
2210 { PreservePositionScope scope(masm()->positions_recorder());
2211 for (int i = 0; i < arg_count; i++) {
2212 VisitForStackValue(args->at(i));
2213 }
2214 }
2215 // Record source position for debugger.
2216 SetSourcePosition(expr->position());
2217 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002218 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002219 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002220 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002221 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002222 RecordJSReturnSite(expr);
2223 // Restore context register.
2224 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2225 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002226}
2227
2228
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002229void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002230 // Code common for calls using the call stub.
2231 ZoneList<Expression*>* args = expr->arguments();
2232 int arg_count = args->length();
2233 { PreservePositionScope scope(masm()->positions_recorder());
2234 for (int i = 0; i < arg_count; i++) {
2235 VisitForStackValue(args->at(i));
2236 }
2237 }
2238 // Record source position for debugger.
2239 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002240 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002241 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002242 __ CallStub(&stub);
2243 RecordJSReturnSite(expr);
2244 // Restore context register.
2245 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2246 context()->DropAndPlug(1, v0);
2247}
2248
2249
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002250void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002251 // Push copy of the first argument or undefined if it doesn't exist.
2252 if (arg_count > 0) {
2253 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2254 } else {
2255 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2256 }
2257 __ push(a1);
2258
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002259 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002260 int receiver_offset = 2 + info_->scope()->num_parameters();
2261 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002262 __ push(a1);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002263 // Push the language mode.
2264 __ li(a1, Operand(Smi::FromInt(language_mode())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002265 __ push(a1);
2266
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002267 // Push the start position of the scope the calls resides in.
2268 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
2269 __ push(a1);
2270
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002271 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002272 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org5c838252010-02-19 08:53:10 +00002273}
2274
2275
2276void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002277#ifdef DEBUG
2278 // We want to verify that RecordJSReturnSite gets called on all paths
2279 // through this function. Avoid early returns.
2280 expr->return_is_recorded_ = false;
2281#endif
2282
2283 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002284 Expression* callee = expr->expression();
2285 VariableProxy* proxy = callee->AsVariableProxy();
2286 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002287
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002288 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002289 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2290 // resolve the function we need to call and the receiver of the
2291 // call. Then we call the resolved function using the given
2292 // arguments.
2293 ZoneList<Expression*>* args = expr->arguments();
2294 int arg_count = args->length();
2295
2296 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002297 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002298 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2299 __ push(a2); // Reserved receiver slot.
2300
2301 // Push the arguments.
2302 for (int i = 0; i < arg_count; i++) {
2303 VisitForStackValue(args->at(i));
2304 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002305
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002306 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002307 // resolve eval.
2308 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2309 __ push(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002310 EmitResolvePossiblyDirectEval(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002311
2312 // The runtime call returns a pair of values in v0 (function) and
2313 // v1 (receiver). Touch up the stack with the right values.
2314 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2315 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2316 }
2317 // Record source position for debugger.
2318 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002319 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002320 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002321 __ CallStub(&stub);
2322 RecordJSReturnSite(expr);
2323 // Restore context register.
2324 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2325 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002326 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002327 // Push global object as receiver for the call IC.
2328 __ lw(a0, GlobalObjectOperand());
2329 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002330 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2331 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002332 // Call to a lookup slot (dynamically introduced variable).
2333 Label slow, done;
2334
2335 { PreservePositionScope scope(masm()->positions_recorder());
2336 // Generate code for loading from variables potentially shadowed
2337 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002338 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002339 }
2340
2341 __ bind(&slow);
2342 // Call the runtime to find the function to call (returned in v0)
2343 // and the object holding it (returned in v1).
2344 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002345 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002346 __ push(a2);
2347 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2348 __ Push(v0, v1); // Function, receiver.
2349
2350 // If fast case code has been generated, emit code to push the
2351 // function and receiver and have the slow path jump around this
2352 // code.
2353 if (done.is_linked()) {
2354 Label call;
2355 __ Branch(&call);
2356 __ bind(&done);
2357 // Push function.
2358 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002359 // The receiver is implicitly the global receiver. Indicate this
2360 // by passing the hole to the call function stub.
2361 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002362 __ push(a1);
2363 __ bind(&call);
2364 }
2365
danno@chromium.org40cb8782011-05-25 07:58:50 +00002366 // The receiver is either the global receiver or an object found
2367 // by LoadContextSlot. That object could be the hole if the
2368 // receiver is implicitly the global object.
2369 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002370 } else if (property != NULL) {
2371 { PreservePositionScope scope(masm()->positions_recorder());
2372 VisitForStackValue(property->obj());
2373 }
2374 if (property->key()->IsPropertyName()) {
2375 EmitCallWithIC(expr,
2376 property->key()->AsLiteral()->handle(),
2377 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002378 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002379 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002380 }
2381 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002382 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002383 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002384 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002385 }
2386 // Load global receiver object.
2387 __ lw(a1, GlobalObjectOperand());
2388 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2389 __ push(a1);
2390 // Emit function call.
2391 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2392 }
2393
2394#ifdef DEBUG
2395 // RecordJSReturnSite should have been called.
2396 ASSERT(expr->return_is_recorded_);
2397#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002398}
2399
2400
2401void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002402 Comment cmnt(masm_, "[ CallNew");
2403 // According to ECMA-262, section 11.2.2, page 44, the function
2404 // expression in new calls must be evaluated before the
2405 // arguments.
2406
2407 // Push constructor on the stack. If it's not a function it's used as
2408 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2409 // ignored.
2410 VisitForStackValue(expr->expression());
2411
2412 // Push the arguments ("left-to-right") on the stack.
2413 ZoneList<Expression*>* args = expr->arguments();
2414 int arg_count = args->length();
2415 for (int i = 0; i < arg_count; i++) {
2416 VisitForStackValue(args->at(i));
2417 }
2418
2419 // Call the construct call builtin that handles allocation and
2420 // constructor invocation.
2421 SetSourcePosition(expr->position());
2422
2423 // Load function and argument count into a1 and a0.
2424 __ li(a0, Operand(arg_count));
2425 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2426
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002427 // Record call targets in unoptimized code, but not in the snapshot.
2428 CallFunctionFlags flags;
2429 if (!Serializer::enabled()) {
2430 flags = RECORD_CALL_TARGET;
2431 Handle<Object> uninitialized =
2432 TypeFeedbackCells::UninitializedSentinel(isolate());
2433 Handle<JSGlobalPropertyCell> cell =
2434 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
2435 RecordTypeFeedbackCell(expr->id(), cell);
2436 __ li(a2, Operand(cell));
2437 } else {
2438 flags = NO_CALL_FUNCTION_FLAGS;
2439 }
2440
2441 CallConstructStub stub(flags);
2442 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002443 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002444}
2445
2446
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002447void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2448 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002449 ASSERT(args->length() == 1);
2450
2451 VisitForAccumulatorValue(args->at(0));
2452
2453 Label materialize_true, materialize_false;
2454 Label* if_true = NULL;
2455 Label* if_false = NULL;
2456 Label* fall_through = NULL;
2457 context()->PrepareTest(&materialize_true, &materialize_false,
2458 &if_true, &if_false, &fall_through);
2459
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002460 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002461 __ And(t0, v0, Operand(kSmiTagMask));
2462 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2463
2464 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002465}
2466
2467
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002468void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2469 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002470 ASSERT(args->length() == 1);
2471
2472 VisitForAccumulatorValue(args->at(0));
2473
2474 Label materialize_true, materialize_false;
2475 Label* if_true = NULL;
2476 Label* if_false = NULL;
2477 Label* fall_through = NULL;
2478 context()->PrepareTest(&materialize_true, &materialize_false,
2479 &if_true, &if_false, &fall_through);
2480
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002481 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002482 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2483 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2484
2485 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002486}
2487
2488
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002489void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2490 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002491 ASSERT(args->length() == 1);
2492
2493 VisitForAccumulatorValue(args->at(0));
2494
2495 Label materialize_true, materialize_false;
2496 Label* if_true = NULL;
2497 Label* if_false = NULL;
2498 Label* fall_through = NULL;
2499 context()->PrepareTest(&materialize_true, &materialize_false,
2500 &if_true, &if_false, &fall_through);
2501
2502 __ JumpIfSmi(v0, if_false);
2503 __ LoadRoot(at, Heap::kNullValueRootIndex);
2504 __ Branch(if_true, eq, v0, Operand(at));
2505 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2506 // Undetectable objects behave like undefined when tested with typeof.
2507 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2508 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2509 __ Branch(if_false, ne, at, Operand(zero_reg));
2510 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002511 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002512 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002513 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2514 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002515
2516 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002517}
2518
2519
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002520void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2521 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002522 ASSERT(args->length() == 1);
2523
2524 VisitForAccumulatorValue(args->at(0));
2525
2526 Label materialize_true, materialize_false;
2527 Label* if_true = NULL;
2528 Label* if_false = NULL;
2529 Label* fall_through = NULL;
2530 context()->PrepareTest(&materialize_true, &materialize_false,
2531 &if_true, &if_false, &fall_through);
2532
2533 __ JumpIfSmi(v0, if_false);
2534 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002535 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002536 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002537 if_true, if_false, fall_through);
2538
2539 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002540}
2541
2542
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002543void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2544 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002545 ASSERT(args->length() == 1);
2546
2547 VisitForAccumulatorValue(args->at(0));
2548
2549 Label materialize_true, materialize_false;
2550 Label* if_true = NULL;
2551 Label* if_false = NULL;
2552 Label* fall_through = NULL;
2553 context()->PrepareTest(&materialize_true, &materialize_false,
2554 &if_true, &if_false, &fall_through);
2555
2556 __ JumpIfSmi(v0, if_false);
2557 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2558 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2559 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002560 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002561 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2562
2563 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002564}
2565
2566
2567void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002568 CallRuntime* expr) {
2569 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002570 ASSERT(args->length() == 1);
2571
2572 VisitForAccumulatorValue(args->at(0));
2573
2574 Label materialize_true, materialize_false;
2575 Label* if_true = NULL;
2576 Label* if_false = NULL;
2577 Label* fall_through = NULL;
2578 context()->PrepareTest(&materialize_true, &materialize_false,
2579 &if_true, &if_false, &fall_through);
2580
2581 if (FLAG_debug_code) __ AbortIfSmi(v0);
2582
2583 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2584 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2585 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2586 __ Branch(if_true, ne, t0, Operand(zero_reg));
2587
2588 // Check for fast case object. Generate false result for slow case object.
2589 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2590 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2591 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2592 __ Branch(if_false, eq, a2, Operand(t0));
2593
2594 // Look for valueOf symbol in the descriptor array, and indicate false if
2595 // found. The type is not checked, so if it is a transition it is a false
2596 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002597 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002598 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2599 // t0: descriptor array
2600 // a3: length of descriptor array
2601 // Calculate the end of the descriptor array.
2602 STATIC_ASSERT(kSmiTag == 0);
2603 STATIC_ASSERT(kSmiTagSize == 1);
2604 STATIC_ASSERT(kPointerSize == 4);
2605 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2606 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2607 __ Addu(a2, a2, t1);
2608
2609 // Calculate location of the first key name.
2610 __ Addu(t0,
2611 t0,
2612 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2613 DescriptorArray::kFirstIndex * kPointerSize));
2614 // Loop through all the keys in the descriptor array. If one of these is the
2615 // symbol valueOf the result is false.
2616 Label entry, loop;
2617 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2618 // used in the loop below.
2619 __ li(t2, Operand(FACTORY->value_of_symbol()));
2620 __ jmp(&entry);
2621 __ bind(&loop);
2622 __ lw(a3, MemOperand(t0, 0));
2623 __ Branch(if_false, eq, a3, Operand(t2));
2624 __ Addu(t0, t0, Operand(kPointerSize));
2625 __ bind(&entry);
2626 __ Branch(&loop, ne, t0, Operand(a2));
2627
2628 // If a valueOf property is not found on the object check that it's
2629 // prototype is the un-modified String prototype. If not result is false.
2630 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2631 __ JumpIfSmi(a2, if_false);
2632 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2633 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2634 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2635 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2636 __ Branch(if_false, ne, a2, Operand(a3));
2637
2638 // Set the bit in the map to indicate that it has been checked safe for
2639 // default valueOf and set true result.
2640 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2641 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2642 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2643 __ jmp(if_true);
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 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002647}
2648
2649
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002650void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2651 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002652 ASSERT(args->length() == 1);
2653
2654 VisitForAccumulatorValue(args->at(0));
2655
2656 Label materialize_true, materialize_false;
2657 Label* if_true = NULL;
2658 Label* if_false = NULL;
2659 Label* fall_through = NULL;
2660 context()->PrepareTest(&materialize_true, &materialize_false,
2661 &if_true, &if_false, &fall_through);
2662
2663 __ JumpIfSmi(v0, if_false);
2664 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002665 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002666 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2667 __ Branch(if_false);
2668
2669 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002670}
2671
2672
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002673void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2674 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002675 ASSERT(args->length() == 1);
2676
2677 VisitForAccumulatorValue(args->at(0));
2678
2679 Label materialize_true, materialize_false;
2680 Label* if_true = NULL;
2681 Label* if_false = NULL;
2682 Label* fall_through = NULL;
2683 context()->PrepareTest(&materialize_true, &materialize_false,
2684 &if_true, &if_false, &fall_through);
2685
2686 __ JumpIfSmi(v0, if_false);
2687 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002688 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002689 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2690 if_true, if_false, fall_through);
2691
2692 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002693}
2694
2695
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002696void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2697 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002698 ASSERT(args->length() == 1);
2699
2700 VisitForAccumulatorValue(args->at(0));
2701
2702 Label materialize_true, materialize_false;
2703 Label* if_true = NULL;
2704 Label* if_false = NULL;
2705 Label* fall_through = NULL;
2706 context()->PrepareTest(&materialize_true, &materialize_false,
2707 &if_true, &if_false, &fall_through);
2708
2709 __ JumpIfSmi(v0, if_false);
2710 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002711 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002712 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2713
2714 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002715}
2716
2717
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002718void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2719 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002720
2721 Label materialize_true, materialize_false;
2722 Label* if_true = NULL;
2723 Label* if_false = NULL;
2724 Label* fall_through = NULL;
2725 context()->PrepareTest(&materialize_true, &materialize_false,
2726 &if_true, &if_false, &fall_through);
2727
2728 // Get the frame pointer for the calling frame.
2729 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2730
2731 // Skip the arguments adaptor frame if it exists.
2732 Label check_frame_marker;
2733 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2734 __ Branch(&check_frame_marker, ne,
2735 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2736 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2737
2738 // Check the marker in the calling frame.
2739 __ bind(&check_frame_marker);
2740 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002741 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002742 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2743 if_true, if_false, fall_through);
2744
2745 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002746}
2747
2748
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002749void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2750 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002751 ASSERT(args->length() == 2);
2752
2753 // Load the two objects into registers and perform the comparison.
2754 VisitForStackValue(args->at(0));
2755 VisitForAccumulatorValue(args->at(1));
2756
2757 Label materialize_true, materialize_false;
2758 Label* if_true = NULL;
2759 Label* if_false = NULL;
2760 Label* fall_through = NULL;
2761 context()->PrepareTest(&materialize_true, &materialize_false,
2762 &if_true, &if_false, &fall_through);
2763
2764 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002765 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002766 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2767
2768 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002769}
2770
2771
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002772void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2773 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002774 ASSERT(args->length() == 1);
2775
2776 // ArgumentsAccessStub expects the key in a1 and the formal
2777 // parameter count in a0.
2778 VisitForAccumulatorValue(args->at(0));
2779 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002780 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002781 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2782 __ CallStub(&stub);
2783 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002784}
2785
2786
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002787void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2788 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002789 Label exit;
2790 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002791 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002792
2793 // Check if the calling frame is an arguments adaptor frame.
2794 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2795 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2796 __ Branch(&exit, ne, a3,
2797 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2798
2799 // Arguments adaptor case: Read the arguments length from the
2800 // adaptor frame.
2801 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2802
2803 __ bind(&exit);
2804 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002805}
2806
2807
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002808void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2809 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002810 ASSERT(args->length() == 1);
2811 Label done, null, function, non_function_constructor;
2812
2813 VisitForAccumulatorValue(args->at(0));
2814
2815 // If the object is a smi, we return null.
2816 __ JumpIfSmi(v0, &null);
2817
2818 // Check that the object is a JS object but take special care of JS
2819 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002820 // Assume that there are only two callable types, and one of them is at
2821 // either end of the type range for JS object types. Saves extra comparisons.
2822 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002823 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002824 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002825
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002826 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2827 FIRST_SPEC_OBJECT_TYPE + 1);
2828 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002829
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002830 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2831 LAST_SPEC_OBJECT_TYPE - 1);
2832 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2833 // Assume that there is no larger type.
2834 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2835
2836 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002837 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2838 __ GetObjectType(v0, a1, a1);
2839 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2840
2841 // v0 now contains the constructor function. Grab the
2842 // instance class name from there.
2843 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2844 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2845 __ Branch(&done);
2846
2847 // Functions have class 'Function'.
2848 __ bind(&function);
2849 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2850 __ jmp(&done);
2851
2852 // Objects with a non-function constructor have class 'Object'.
2853 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002854 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002855 __ jmp(&done);
2856
2857 // Non-JS objects have class null.
2858 __ bind(&null);
2859 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2860
2861 // All done.
2862 __ bind(&done);
2863
2864 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002865}
2866
2867
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002868void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002869 // Conditionally generate a log call.
2870 // Args:
2871 // 0 (literal string): The type of logging (corresponds to the flags).
2872 // This is used to determine whether or not to generate the log call.
2873 // 1 (string): Format string. Access the string at argument index 2
2874 // with '%2s' (see Logger::LogRuntime for all the formats).
2875 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002876 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002877 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002878 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2879 VisitForStackValue(args->at(1));
2880 VisitForStackValue(args->at(2));
2881 __ CallRuntime(Runtime::kLog, 2);
2882 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002883
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002884 // Finally, we're expected to leave a value on the top of the stack.
2885 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2886 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002887}
2888
2889
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002890void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
2891 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002892 Label slow_allocate_heapnumber;
2893 Label heapnumber_allocated;
2894
2895 // Save the new heap number in callee-saved register s0, since
2896 // we call out to external C code below.
2897 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2898 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2899 __ jmp(&heapnumber_allocated);
2900
2901 __ bind(&slow_allocate_heapnumber);
2902
2903 // Allocate a heap number.
2904 __ CallRuntime(Runtime::kNumberAlloc, 0);
2905 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2906
2907 __ bind(&heapnumber_allocated);
2908
2909 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2910 // by computing:
2911 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2912 if (CpuFeatures::IsSupported(FPU)) {
2913 __ PrepareCallCFunction(1, a0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002914 __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
2915 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002916 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2917
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002918 CpuFeatures::Scope scope(FPU);
2919 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2920 __ li(a1, Operand(0x41300000));
2921 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002922 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002923 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002924 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002925 // Subtract and store the result in the heap number.
2926 __ sub_d(f0, f12, f14);
2927 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2928 __ mov(v0, s0);
2929 } else {
2930 __ PrepareCallCFunction(2, a0);
2931 __ mov(a0, s0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002932 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
2933 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002934 __ CallCFunction(
2935 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2936 }
2937
2938 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002939}
2940
2941
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002942void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002943 // Load the arguments on the stack and call the stub.
2944 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002945 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002946 ASSERT(args->length() == 3);
2947 VisitForStackValue(args->at(0));
2948 VisitForStackValue(args->at(1));
2949 VisitForStackValue(args->at(2));
2950 __ CallStub(&stub);
2951 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002952}
2953
2954
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002955void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002956 // Load the arguments on the stack and call the stub.
2957 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002958 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002959 ASSERT(args->length() == 4);
2960 VisitForStackValue(args->at(0));
2961 VisitForStackValue(args->at(1));
2962 VisitForStackValue(args->at(2));
2963 VisitForStackValue(args->at(3));
2964 __ CallStub(&stub);
2965 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002966}
2967
2968
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002969void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
2970 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002971 ASSERT(args->length() == 1);
2972
2973 VisitForAccumulatorValue(args->at(0)); // Load the object.
2974
2975 Label done;
2976 // If the object is a smi return the object.
2977 __ JumpIfSmi(v0, &done);
2978 // If the object is not a value type, return the object.
2979 __ GetObjectType(v0, a1, a1);
2980 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2981
2982 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2983
2984 __ bind(&done);
2985 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002986}
2987
2988
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002989void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002990 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002991 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002992 ASSERT(args->length() == 2);
2993 VisitForStackValue(args->at(0));
2994 VisitForStackValue(args->at(1));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002995 if (CpuFeatures::IsSupported(FPU)) {
2996 MathPowStub stub(MathPowStub::ON_STACK);
2997 __ CallStub(&stub);
2998 } else {
2999 __ CallRuntime(Runtime::kMath_pow, 2);
3000 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003001 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003002}
3003
3004
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003005void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3006 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003007 ASSERT(args->length() == 2);
3008
3009 VisitForStackValue(args->at(0)); // Load the object.
3010 VisitForAccumulatorValue(args->at(1)); // Load the value.
3011 __ pop(a1); // v0 = value. a1 = object.
3012
3013 Label done;
3014 // If the object is a smi, return the value.
3015 __ JumpIfSmi(a1, &done);
3016
3017 // If the object is not a value type, return the value.
3018 __ GetObjectType(a1, a2, a2);
3019 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
3020
3021 // Store the value.
3022 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
3023 // Update the write barrier. Save the value as it will be
3024 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003025 __ mov(a2, v0);
3026 __ RecordWriteField(
3027 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003028
3029 __ bind(&done);
3030 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003031}
3032
3033
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003034void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3035 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003036 ASSERT_EQ(args->length(), 1);
3037
3038 // Load the argument on the stack and call the stub.
3039 VisitForStackValue(args->at(0));
3040
3041 NumberToStringStub stub;
3042 __ CallStub(&stub);
3043 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003044}
3045
3046
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003047void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3048 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003049 ASSERT(args->length() == 1);
3050
3051 VisitForAccumulatorValue(args->at(0));
3052
3053 Label done;
3054 StringCharFromCodeGenerator generator(v0, a1);
3055 generator.GenerateFast(masm_);
3056 __ jmp(&done);
3057
3058 NopRuntimeCallHelper call_helper;
3059 generator.GenerateSlow(masm_, call_helper);
3060
3061 __ bind(&done);
3062 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003063}
3064
3065
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003066void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3067 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003068 ASSERT(args->length() == 2);
3069
3070 VisitForStackValue(args->at(0));
3071 VisitForAccumulatorValue(args->at(1));
3072 __ mov(a0, result_register());
3073
3074 Register object = a1;
3075 Register index = a0;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003076 Register result = v0;
3077
3078 __ pop(object);
3079
3080 Label need_conversion;
3081 Label index_out_of_range;
3082 Label done;
3083 StringCharCodeAtGenerator generator(object,
3084 index,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003085 result,
3086 &need_conversion,
3087 &need_conversion,
3088 &index_out_of_range,
3089 STRING_INDEX_IS_NUMBER);
3090 generator.GenerateFast(masm_);
3091 __ jmp(&done);
3092
3093 __ bind(&index_out_of_range);
3094 // When the index is out of range, the spec requires us to return
3095 // NaN.
3096 __ LoadRoot(result, Heap::kNanValueRootIndex);
3097 __ jmp(&done);
3098
3099 __ bind(&need_conversion);
3100 // Load the undefined value into the result register, which will
3101 // trigger conversion.
3102 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3103 __ jmp(&done);
3104
3105 NopRuntimeCallHelper call_helper;
3106 generator.GenerateSlow(masm_, call_helper);
3107
3108 __ bind(&done);
3109 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003110}
3111
3112
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003113void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3114 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003115 ASSERT(args->length() == 2);
3116
3117 VisitForStackValue(args->at(0));
3118 VisitForAccumulatorValue(args->at(1));
3119 __ mov(a0, result_register());
3120
3121 Register object = a1;
3122 Register index = a0;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003123 Register scratch = a3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003124 Register result = v0;
3125
3126 __ pop(object);
3127
3128 Label need_conversion;
3129 Label index_out_of_range;
3130 Label done;
3131 StringCharAtGenerator generator(object,
3132 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003133 scratch,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003134 result,
3135 &need_conversion,
3136 &need_conversion,
3137 &index_out_of_range,
3138 STRING_INDEX_IS_NUMBER);
3139 generator.GenerateFast(masm_);
3140 __ jmp(&done);
3141
3142 __ bind(&index_out_of_range);
3143 // When the index is out of range, the spec requires us to return
3144 // the empty string.
3145 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3146 __ jmp(&done);
3147
3148 __ bind(&need_conversion);
3149 // Move smi zero into the result register, which will trigger
3150 // conversion.
3151 __ li(result, Operand(Smi::FromInt(0)));
3152 __ jmp(&done);
3153
3154 NopRuntimeCallHelper call_helper;
3155 generator.GenerateSlow(masm_, call_helper);
3156
3157 __ bind(&done);
3158 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003159}
3160
3161
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003162void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3163 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003164 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003165 VisitForStackValue(args->at(0));
3166 VisitForStackValue(args->at(1));
3167
3168 StringAddStub stub(NO_STRING_ADD_FLAGS);
3169 __ CallStub(&stub);
3170 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003171}
3172
3173
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003174void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3175 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003176 ASSERT_EQ(2, args->length());
3177
3178 VisitForStackValue(args->at(0));
3179 VisitForStackValue(args->at(1));
3180
3181 StringCompareStub stub;
3182 __ CallStub(&stub);
3183 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003184}
3185
3186
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003187void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003188 // Load the argument on the stack and call the stub.
3189 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3190 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003191 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003192 ASSERT(args->length() == 1);
3193 VisitForStackValue(args->at(0));
3194 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3195 __ CallStub(&stub);
3196 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003197}
3198
3199
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003200void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003201 // Load the argument on the stack and call the stub.
3202 TranscendentalCacheStub stub(TranscendentalCache::COS,
3203 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003204 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003205 ASSERT(args->length() == 1);
3206 VisitForStackValue(args->at(0));
3207 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3208 __ CallStub(&stub);
3209 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003210}
3211
3212
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003213void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3214 // Load the argument on the stack and call the stub.
3215 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3216 TranscendentalCacheStub::TAGGED);
3217 ZoneList<Expression*>* args = expr->arguments();
3218 ASSERT(args->length() == 1);
3219 VisitForStackValue(args->at(0));
3220 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3221 __ CallStub(&stub);
3222 context()->Plug(v0);
3223}
3224
3225
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003226void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003227 // Load the argument on the stack and call the stub.
3228 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3229 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003230 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003231 ASSERT(args->length() == 1);
3232 VisitForStackValue(args->at(0));
3233 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3234 __ CallStub(&stub);
3235 context()->Plug(v0);
3236}
3237
3238
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003239void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003240 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003241 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003242 ASSERT(args->length() == 1);
3243 VisitForStackValue(args->at(0));
3244 __ CallRuntime(Runtime::kMath_sqrt, 1);
3245 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003246}
3247
3248
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003249void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3250 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003251 ASSERT(args->length() >= 2);
3252
3253 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3254 for (int i = 0; i < arg_count + 1; i++) {
3255 VisitForStackValue(args->at(i));
3256 }
3257 VisitForAccumulatorValue(args->last()); // Function.
3258
danno@chromium.orgc612e022011-11-10 11:38:15 +00003259 // Check for proxy.
3260 Label proxy, done;
3261 __ GetObjectType(v0, a1, a1);
3262 __ Branch(&proxy, eq, a1, Operand(JS_FUNCTION_PROXY_TYPE));
3263
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003264 // InvokeFunction requires the function in a1. Move it in there.
3265 __ mov(a1, result_register());
3266 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003267 __ InvokeFunction(a1, count, CALL_FUNCTION,
3268 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003269 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003270 __ jmp(&done);
3271
3272 __ bind(&proxy);
3273 __ push(v0);
3274 __ CallRuntime(Runtime::kCall, args->length());
3275 __ bind(&done);
3276
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003277 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003278}
3279
3280
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003281void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003282 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003283 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003284 ASSERT(args->length() == 3);
3285 VisitForStackValue(args->at(0));
3286 VisitForStackValue(args->at(1));
3287 VisitForStackValue(args->at(2));
3288 __ CallStub(&stub);
3289 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003290}
3291
3292
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003293void FullCodeGenerator::EmitSwapElements(CallRuntime* expr) {
3294 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003295 ASSERT(args->length() == 3);
3296 VisitForStackValue(args->at(0));
3297 VisitForStackValue(args->at(1));
3298 VisitForStackValue(args->at(2));
3299 Label done;
3300 Label slow_case;
3301 Register object = a0;
3302 Register index1 = a1;
3303 Register index2 = a2;
3304 Register elements = a3;
3305 Register scratch1 = t0;
3306 Register scratch2 = t1;
3307
3308 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3309 // Fetch the map and check if array is in fast case.
3310 // Check that object doesn't require security checks and
3311 // has no indexed interceptor.
3312 __ GetObjectType(object, scratch1, scratch2);
3313 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3314 // Map is now in scratch1.
3315
3316 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3317 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3318 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3319
3320 // Check the object's elements are in fast case and writable.
3321 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3322 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3323 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3324 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3325
3326 // Check that both indices are smis.
3327 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3328 __ lw(index2, MemOperand(sp, 0));
3329 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3330
3331 // Check that both indices are valid.
3332 Label not_hi;
3333 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3334 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3335 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3336 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3337 __ bind(&not_hi);
3338
3339 // Bring the address of the elements into index1 and index2.
3340 __ Addu(scratch1, elements,
3341 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3342 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3343 __ Addu(index1, scratch1, index1);
3344 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3345 __ Addu(index2, scratch1, index2);
3346
3347 // Swap elements.
3348 __ lw(scratch1, MemOperand(index1, 0));
3349 __ lw(scratch2, MemOperand(index2, 0));
3350 __ sw(scratch1, MemOperand(index2, 0));
3351 __ sw(scratch2, MemOperand(index1, 0));
3352
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003353 Label no_remembered_set;
3354 __ CheckPageFlag(elements,
3355 scratch1,
3356 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3357 ne,
3358 &no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003359 // Possible optimization: do a check that both values are Smis
3360 // (or them and test against Smi mask).
3361
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003362 // We are swapping two objects in an array and the incremental marker never
3363 // pauses in the middle of scanning a single object. Therefore the
3364 // incremental marker is not disturbed, so we don't need to call the
3365 // RecordWrite stub that notifies the incremental marker.
3366 __ RememberedSetHelper(elements,
3367 index1,
3368 scratch2,
3369 kDontSaveFPRegs,
3370 MacroAssembler::kFallThroughAtEnd);
3371 __ RememberedSetHelper(elements,
3372 index2,
3373 scratch2,
3374 kDontSaveFPRegs,
3375 MacroAssembler::kFallThroughAtEnd);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003376
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003377 __ bind(&no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003378 // We are done. Drop elements from the stack, and return undefined.
3379 __ Drop(3);
3380 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3381 __ jmp(&done);
3382
3383 __ bind(&slow_case);
3384 __ CallRuntime(Runtime::kSwapElements, 3);
3385
3386 __ bind(&done);
3387 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003388}
3389
3390
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003391void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3392 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003393 ASSERT_EQ(2, args->length());
3394
3395 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3396 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3397
3398 Handle<FixedArray> jsfunction_result_caches(
3399 isolate()->global_context()->jsfunction_result_caches());
3400 if (jsfunction_result_caches->length() <= cache_id) {
3401 __ Abort("Attempt to use undefined cache.");
3402 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3403 context()->Plug(v0);
3404 return;
3405 }
3406
3407 VisitForAccumulatorValue(args->at(1));
3408
3409 Register key = v0;
3410 Register cache = a1;
3411 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3412 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3413 __ lw(cache,
3414 ContextOperand(
3415 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3416 __ lw(cache,
3417 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3418
3419
3420 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003421 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003422 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3423 // a2 now holds finger offset as a smi.
3424 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3425 // a3 now points to the start of fixed array elements.
3426 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3427 __ addu(a3, a3, at);
3428 // a3 now points to key of indexed element of cache.
3429 __ lw(a2, MemOperand(a3));
3430 __ Branch(&not_found, ne, key, Operand(a2));
3431
3432 __ lw(v0, MemOperand(a3, kPointerSize));
3433 __ Branch(&done);
3434
3435 __ bind(&not_found);
3436 // Call runtime to perform the lookup.
3437 __ Push(cache, key);
3438 __ CallRuntime(Runtime::kGetFromCache, 2);
3439
3440 __ bind(&done);
3441 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003442}
3443
3444
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003445void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3446 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003447 ASSERT_EQ(2, args->length());
3448
3449 Register right = v0;
3450 Register left = a1;
3451 Register tmp = a2;
3452 Register tmp2 = a3;
3453
3454 VisitForStackValue(args->at(0));
3455 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3456 __ pop(left);
3457
3458 Label done, fail, ok;
3459 __ Branch(&ok, eq, left, Operand(right));
3460 // Fail if either is a non-HeapObject.
3461 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003462 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003463 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3464 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3465 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3466 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3467 __ Branch(&fail, ne, tmp, Operand(tmp2));
3468 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3469 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3470 __ Branch(&ok, eq, tmp, Operand(tmp2));
3471 __ bind(&fail);
3472 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3473 __ jmp(&done);
3474 __ bind(&ok);
3475 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3476 __ bind(&done);
3477
3478 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003479}
3480
3481
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003482void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3483 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003484 VisitForAccumulatorValue(args->at(0));
3485
3486 Label materialize_true, materialize_false;
3487 Label* if_true = NULL;
3488 Label* if_false = NULL;
3489 Label* fall_through = NULL;
3490 context()->PrepareTest(&materialize_true, &materialize_false,
3491 &if_true, &if_false, &fall_through);
3492
3493 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3494 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3495
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003496 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003497 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3498
3499 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003500}
3501
3502
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003503void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3504 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003505 ASSERT(args->length() == 1);
3506 VisitForAccumulatorValue(args->at(0));
3507
3508 if (FLAG_debug_code) {
3509 __ AbortIfNotString(v0);
3510 }
3511
3512 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3513 __ IndexFromHash(v0, v0);
3514
3515 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003516}
3517
3518
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003519void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003520 Label bailout, done, one_char_separator, long_separator,
3521 non_trivial_array, not_size_one_array, loop,
3522 empty_separator_loop, one_char_separator_loop,
3523 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003524 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003525 ASSERT(args->length() == 2);
3526 VisitForStackValue(args->at(1));
3527 VisitForAccumulatorValue(args->at(0));
3528
3529 // All aliases of the same register have disjoint lifetimes.
3530 Register array = v0;
3531 Register elements = no_reg; // Will be v0.
3532 Register result = no_reg; // Will be v0.
3533 Register separator = a1;
3534 Register array_length = a2;
3535 Register result_pos = no_reg; // Will be a2.
3536 Register string_length = a3;
3537 Register string = t0;
3538 Register element = t1;
3539 Register elements_end = t2;
3540 Register scratch1 = t3;
3541 Register scratch2 = t5;
3542 Register scratch3 = t4;
3543 Register scratch4 = v1;
3544
3545 // Separator operand is on the stack.
3546 __ pop(separator);
3547
3548 // Check that the array is a JSArray.
3549 __ JumpIfSmi(array, &bailout);
3550 __ GetObjectType(array, scratch1, scratch2);
3551 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3552
3553 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003554 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003555
3556 // If the array has length zero, return the empty string.
3557 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3558 __ SmiUntag(array_length);
3559 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3560 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3561 __ Branch(&done);
3562
3563 __ bind(&non_trivial_array);
3564
3565 // Get the FixedArray containing array's elements.
3566 elements = array;
3567 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3568 array = no_reg; // End of array's live range.
3569
3570 // Check that all array elements are sequential ASCII strings, and
3571 // accumulate the sum of their lengths, as a smi-encoded value.
3572 __ mov(string_length, zero_reg);
3573 __ Addu(element,
3574 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3575 __ sll(elements_end, array_length, kPointerSizeLog2);
3576 __ Addu(elements_end, element, elements_end);
3577 // Loop condition: while (element < elements_end).
3578 // Live values in registers:
3579 // elements: Fixed array of strings.
3580 // array_length: Length of the fixed array of strings (not smi)
3581 // separator: Separator string
3582 // string_length: Accumulated sum of string lengths (smi).
3583 // element: Current array element.
3584 // elements_end: Array end.
3585 if (FLAG_debug_code) {
3586 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3587 array_length, Operand(zero_reg));
3588 }
3589 __ bind(&loop);
3590 __ lw(string, MemOperand(element));
3591 __ Addu(element, element, kPointerSize);
3592 __ JumpIfSmi(string, &bailout);
3593 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3594 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3595 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3596 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3597 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3598 __ BranchOnOverflow(&bailout, scratch3);
3599 __ Branch(&loop, lt, element, Operand(elements_end));
3600
3601 // If array_length is 1, return elements[0], a string.
3602 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3603 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3604 __ Branch(&done);
3605
3606 __ bind(&not_size_one_array);
3607
3608 // Live values in registers:
3609 // separator: Separator string
3610 // array_length: Length of the array.
3611 // string_length: Sum of string lengths (smi).
3612 // elements: FixedArray of strings.
3613
3614 // Check that the separator is a flat ASCII string.
3615 __ JumpIfSmi(separator, &bailout);
3616 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3617 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3618 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3619
3620 // Add (separator length times array_length) - separator length to the
3621 // string_length to get the length of the result string. array_length is not
3622 // smi but the other values are, so the result is a smi.
3623 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3624 __ Subu(string_length, string_length, Operand(scratch1));
3625 __ Mult(array_length, scratch1);
3626 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3627 // zero.
3628 __ mfhi(scratch2);
3629 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3630 __ mflo(scratch2);
3631 __ And(scratch3, scratch2, Operand(0x80000000));
3632 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3633 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3634 __ BranchOnOverflow(&bailout, scratch3);
3635 __ SmiUntag(string_length);
3636
3637 // Get first element in the array to free up the elements register to be used
3638 // for the result.
3639 __ Addu(element,
3640 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3641 result = elements; // End of live range for elements.
3642 elements = no_reg;
3643 // Live values in registers:
3644 // element: First array element
3645 // separator: Separator string
3646 // string_length: Length of result string (not smi)
3647 // array_length: Length of the array.
3648 __ AllocateAsciiString(result,
3649 string_length,
3650 scratch1,
3651 scratch2,
3652 elements_end,
3653 &bailout);
3654 // Prepare for looping. Set up elements_end to end of the array. Set
3655 // result_pos to the position of the result where to write the first
3656 // character.
3657 __ sll(elements_end, array_length, kPointerSizeLog2);
3658 __ Addu(elements_end, element, elements_end);
3659 result_pos = array_length; // End of live range for array_length.
3660 array_length = no_reg;
3661 __ Addu(result_pos,
3662 result,
3663 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3664
3665 // Check the length of the separator.
3666 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3667 __ li(at, Operand(Smi::FromInt(1)));
3668 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3669 __ Branch(&long_separator, gt, scratch1, Operand(at));
3670
3671 // Empty separator case.
3672 __ bind(&empty_separator_loop);
3673 // Live values in registers:
3674 // result_pos: the position to which we are currently copying characters.
3675 // element: Current array element.
3676 // elements_end: Array end.
3677
3678 // Copy next array element to the result.
3679 __ lw(string, MemOperand(element));
3680 __ Addu(element, element, kPointerSize);
3681 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3682 __ SmiUntag(string_length);
3683 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3684 __ CopyBytes(string, result_pos, string_length, scratch1);
3685 // End while (element < elements_end).
3686 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3687 ASSERT(result.is(v0));
3688 __ Branch(&done);
3689
3690 // One-character separator case.
3691 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003692 // Replace separator with its ASCII character value.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003693 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3694 // Jump into the loop after the code that copies the separator, so the first
3695 // element is not preceded by a separator.
3696 __ jmp(&one_char_separator_loop_entry);
3697
3698 __ bind(&one_char_separator_loop);
3699 // Live values in registers:
3700 // result_pos: the position to which we are currently copying characters.
3701 // element: Current array element.
3702 // elements_end: Array end.
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003703 // separator: Single separator ASCII char (in lower byte).
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003704
3705 // Copy the separator character to the result.
3706 __ sb(separator, MemOperand(result_pos));
3707 __ Addu(result_pos, result_pos, 1);
3708
3709 // Copy next array element to the result.
3710 __ bind(&one_char_separator_loop_entry);
3711 __ lw(string, MemOperand(element));
3712 __ Addu(element, element, kPointerSize);
3713 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3714 __ SmiUntag(string_length);
3715 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3716 __ CopyBytes(string, result_pos, string_length, scratch1);
3717 // End while (element < elements_end).
3718 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3719 ASSERT(result.is(v0));
3720 __ Branch(&done);
3721
3722 // Long separator case (separator is more than one character). Entry is at the
3723 // label long_separator below.
3724 __ bind(&long_separator_loop);
3725 // Live values in registers:
3726 // result_pos: the position to which we are currently copying characters.
3727 // element: Current array element.
3728 // elements_end: Array end.
3729 // separator: Separator string.
3730
3731 // Copy the separator to the result.
3732 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3733 __ SmiUntag(string_length);
3734 __ Addu(string,
3735 separator,
3736 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3737 __ CopyBytes(string, result_pos, string_length, scratch1);
3738
3739 __ bind(&long_separator);
3740 __ lw(string, MemOperand(element));
3741 __ Addu(element, element, kPointerSize);
3742 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3743 __ SmiUntag(string_length);
3744 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3745 __ CopyBytes(string, result_pos, string_length, scratch1);
3746 // End while (element < elements_end).
3747 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3748 ASSERT(result.is(v0));
3749 __ Branch(&done);
3750
3751 __ bind(&bailout);
3752 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3753 __ bind(&done);
3754 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003755}
3756
3757
ager@chromium.org5c838252010-02-19 08:53:10 +00003758void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003759 Handle<String> name = expr->name();
3760 if (name->length() > 0 && name->Get(0) == '_') {
3761 Comment cmnt(masm_, "[ InlineRuntimeCall");
3762 EmitInlineRuntimeCall(expr);
3763 return;
3764 }
3765
3766 Comment cmnt(masm_, "[ CallRuntime");
3767 ZoneList<Expression*>* args = expr->arguments();
3768
3769 if (expr->is_jsruntime()) {
3770 // Prepare for calling JS runtime function.
3771 __ lw(a0, GlobalObjectOperand());
3772 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3773 __ push(a0);
3774 }
3775
3776 // Push the arguments ("left-to-right").
3777 int arg_count = args->length();
3778 for (int i = 0; i < arg_count; i++) {
3779 VisitForStackValue(args->at(i));
3780 }
3781
3782 if (expr->is_jsruntime()) {
3783 // Call the JS runtime function.
3784 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003785 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003786 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003787 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003788 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003789 // Restore context register.
3790 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3791 } else {
3792 // Call the C runtime function.
3793 __ CallRuntime(expr->function(), arg_count);
3794 }
3795 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003796}
3797
3798
3799void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003800 switch (expr->op()) {
3801 case Token::DELETE: {
3802 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003803 Property* property = expr->expression()->AsProperty();
3804 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003805
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003806 if (property != NULL) {
3807 VisitForStackValue(property->obj());
3808 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003809 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
3810 ? kNonStrictMode : kStrictMode;
3811 __ li(a1, Operand(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003812 __ push(a1);
3813 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3814 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003815 } else if (proxy != NULL) {
3816 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003817 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003818 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003819 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003820 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003821 __ lw(a2, GlobalObjectOperand());
3822 __ li(a1, Operand(var->name()));
3823 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3824 __ Push(a2, a1, a0);
3825 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3826 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003827 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003828 // Result of deleting non-global, non-dynamic variables is false.
3829 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003830 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003831 } else {
3832 // Non-global variable. Call the runtime to try to delete from the
3833 // context where the variable was introduced.
3834 __ push(context_register());
3835 __ li(a2, Operand(var->name()));
3836 __ push(a2);
3837 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3838 context()->Plug(v0);
3839 }
3840 } else {
3841 // Result of deleting non-property, non-variable reference is true.
3842 // The subexpression may have side effects.
3843 VisitForEffect(expr->expression());
3844 context()->Plug(true);
3845 }
3846 break;
3847 }
3848
3849 case Token::VOID: {
3850 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3851 VisitForEffect(expr->expression());
3852 context()->Plug(Heap::kUndefinedValueRootIndex);
3853 break;
3854 }
3855
3856 case Token::NOT: {
3857 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3858 if (context()->IsEffect()) {
3859 // Unary NOT has no side effects so it's only necessary to visit the
3860 // subexpression. Match the optimizing compiler by not branching.
3861 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003862 } else if (context()->IsTest()) {
3863 const TestContext* test = TestContext::cast(context());
3864 // The labels are swapped for the recursive call.
3865 VisitForControl(expr->expression(),
3866 test->false_label(),
3867 test->true_label(),
3868 test->fall_through());
3869 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003870 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003871 // We handle value contexts explicitly rather than simply visiting
3872 // for control and plugging the control flow into the context,
3873 // because we need to prepare a pair of extra administrative AST ids
3874 // for the optimizing compiler.
3875 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3876 Label materialize_true, materialize_false, done;
3877 VisitForControl(expr->expression(),
3878 &materialize_false,
3879 &materialize_true,
3880 &materialize_true);
3881 __ bind(&materialize_true);
3882 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3883 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3884 if (context()->IsStackValue()) __ push(v0);
3885 __ jmp(&done);
3886 __ bind(&materialize_false);
3887 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3888 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3889 if (context()->IsStackValue()) __ push(v0);
3890 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003891 }
3892 break;
3893 }
3894
3895 case Token::TYPEOF: {
3896 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3897 { StackValueContext context(this);
3898 VisitForTypeofValue(expr->expression());
3899 }
3900 __ CallRuntime(Runtime::kTypeof, 1);
3901 context()->Plug(v0);
3902 break;
3903 }
3904
3905 case Token::ADD: {
3906 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3907 VisitForAccumulatorValue(expr->expression());
3908 Label no_conversion;
3909 __ JumpIfSmi(result_register(), &no_conversion);
3910 __ mov(a0, result_register());
3911 ToNumberStub convert_stub;
3912 __ CallStub(&convert_stub);
3913 __ bind(&no_conversion);
3914 context()->Plug(result_register());
3915 break;
3916 }
3917
3918 case Token::SUB:
3919 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3920 break;
3921
3922 case Token::BIT_NOT:
3923 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3924 break;
3925
3926 default:
3927 UNREACHABLE();
3928 }
3929}
3930
3931
3932void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3933 const char* comment) {
3934 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3935 Comment cmt(masm_, comment);
3936 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3937 UnaryOverwriteMode overwrite =
3938 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003939 UnaryOpStub stub(expr->op(), overwrite);
3940 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003941 VisitForAccumulatorValue(expr->expression());
3942 SetSourcePosition(expr->position());
3943 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003944 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003945 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003946}
3947
3948
3949void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003950 Comment cmnt(masm_, "[ CountOperation");
3951 SetSourcePosition(expr->position());
3952
3953 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3954 // as the left-hand side.
3955 if (!expr->expression()->IsValidLeftHandSide()) {
3956 VisitForEffect(expr->expression());
3957 return;
3958 }
3959
3960 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003961 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003962 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3963 LhsKind assign_type = VARIABLE;
3964 Property* prop = expr->expression()->AsProperty();
3965 // In case of a property we use the uninitialized expression context
3966 // of the key to detect a named property.
3967 if (prop != NULL) {
3968 assign_type =
3969 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3970 }
3971
3972 // Evaluate expression and get value.
3973 if (assign_type == VARIABLE) {
3974 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3975 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003976 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003977 } else {
3978 // Reserve space for result of postfix operation.
3979 if (expr->is_postfix() && !context()->IsEffect()) {
3980 __ li(at, Operand(Smi::FromInt(0)));
3981 __ push(at);
3982 }
3983 if (assign_type == NAMED_PROPERTY) {
3984 // Put the object both on the stack and in the accumulator.
3985 VisitForAccumulatorValue(prop->obj());
3986 __ push(v0);
3987 EmitNamedPropertyLoad(prop);
3988 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003989 VisitForStackValue(prop->obj());
3990 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003991 __ lw(a1, MemOperand(sp, 0));
3992 __ push(v0);
3993 EmitKeyedPropertyLoad(prop);
3994 }
3995 }
3996
3997 // We need a second deoptimization point after loading the value
3998 // in case evaluating the property load my have a side effect.
3999 if (assign_type == VARIABLE) {
4000 PrepareForBailout(expr->expression(), TOS_REG);
4001 } else {
4002 PrepareForBailoutForId(expr->CountId(), TOS_REG);
4003 }
4004
4005 // Call ToNumber only if operand is not a smi.
4006 Label no_conversion;
4007 __ JumpIfSmi(v0, &no_conversion);
4008 __ mov(a0, v0);
4009 ToNumberStub convert_stub;
4010 __ CallStub(&convert_stub);
4011 __ bind(&no_conversion);
4012
4013 // Save result for postfix expressions.
4014 if (expr->is_postfix()) {
4015 if (!context()->IsEffect()) {
4016 // Save the result on the stack. If we have a named or keyed property
4017 // we store the result under the receiver that is currently on top
4018 // of the stack.
4019 switch (assign_type) {
4020 case VARIABLE:
4021 __ push(v0);
4022 break;
4023 case NAMED_PROPERTY:
4024 __ sw(v0, MemOperand(sp, kPointerSize));
4025 break;
4026 case KEYED_PROPERTY:
4027 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4028 break;
4029 }
4030 }
4031 }
4032 __ mov(a0, result_register());
4033
4034 // Inline smi case if we are in a loop.
4035 Label stub_call, done;
4036 JumpPatchSite patch_site(masm_);
4037
4038 int count_value = expr->op() == Token::INC ? 1 : -1;
4039 __ li(a1, Operand(Smi::FromInt(count_value)));
4040
4041 if (ShouldInlineSmiCase(expr->op())) {
4042 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
4043 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
4044
4045 // We could eliminate this smi check if we split the code at
4046 // the first smi check before calling ToNumber.
4047 patch_site.EmitJumpIfSmi(v0, &done);
4048 __ bind(&stub_call);
4049 }
4050
4051 // Record position before stub call.
4052 SetSourcePosition(expr->position());
4053
danno@chromium.org40cb8782011-05-25 07:58:50 +00004054 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004055 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004056 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004057 __ bind(&done);
4058
4059 // Store the value returned in v0.
4060 switch (assign_type) {
4061 case VARIABLE:
4062 if (expr->is_postfix()) {
4063 { EffectContext context(this);
4064 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4065 Token::ASSIGN);
4066 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4067 context.Plug(v0);
4068 }
4069 // For all contexts except EffectConstant we have the result on
4070 // top of the stack.
4071 if (!context()->IsEffect()) {
4072 context()->PlugTOS();
4073 }
4074 } else {
4075 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4076 Token::ASSIGN);
4077 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4078 context()->Plug(v0);
4079 }
4080 break;
4081 case NAMED_PROPERTY: {
4082 __ mov(a0, result_register()); // Value.
4083 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4084 __ pop(a1); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004085 Handle<Code> ic = is_classic_mode()
4086 ? isolate()->builtins()->StoreIC_Initialize()
4087 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004088 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004089 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4090 if (expr->is_postfix()) {
4091 if (!context()->IsEffect()) {
4092 context()->PlugTOS();
4093 }
4094 } else {
4095 context()->Plug(v0);
4096 }
4097 break;
4098 }
4099 case KEYED_PROPERTY: {
4100 __ mov(a0, result_register()); // Value.
4101 __ pop(a1); // Key.
4102 __ pop(a2); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004103 Handle<Code> ic = is_classic_mode()
4104 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4105 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004106 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004107 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4108 if (expr->is_postfix()) {
4109 if (!context()->IsEffect()) {
4110 context()->PlugTOS();
4111 }
4112 } else {
4113 context()->Plug(v0);
4114 }
4115 break;
4116 }
4117 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004118}
4119
4120
lrn@chromium.org7516f052011-03-30 08:52:27 +00004121void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004122 ASSERT(!context()->IsEffect());
4123 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004124 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004125 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004126 Comment cmnt(masm_, "Global variable");
4127 __ lw(a0, GlobalObjectOperand());
4128 __ li(a2, Operand(proxy->name()));
4129 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4130 // Use a regular load, not a contextual load, to avoid a reference
4131 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004132 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004133 PrepareForBailout(expr, TOS_REG);
4134 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004135 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004136 Label done, slow;
4137
4138 // Generate code for loading from variables potentially shadowed
4139 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004140 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004141
4142 __ bind(&slow);
4143 __ li(a0, Operand(proxy->name()));
4144 __ Push(cp, a0);
4145 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4146 PrepareForBailout(expr, TOS_REG);
4147 __ bind(&done);
4148
4149 context()->Plug(v0);
4150 } else {
4151 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004152 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004153 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004154}
4155
ager@chromium.org04921a82011-06-27 13:21:41 +00004156void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004157 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004158 Handle<String> check) {
4159 Label materialize_true, materialize_false;
4160 Label* if_true = NULL;
4161 Label* if_false = NULL;
4162 Label* fall_through = NULL;
4163 context()->PrepareTest(&materialize_true, &materialize_false,
4164 &if_true, &if_false, &fall_through);
4165
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004166 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004167 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004168 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004169 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004170
4171 if (check->Equals(isolate()->heap()->number_symbol())) {
4172 __ JumpIfSmi(v0, if_true);
4173 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4174 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4175 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4176 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4177 __ JumpIfSmi(v0, if_false);
4178 // Check for undetectable objects => false.
4179 __ GetObjectType(v0, v0, a1);
4180 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4181 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4182 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4183 Split(eq, a1, Operand(zero_reg),
4184 if_true, if_false, fall_through);
4185 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4186 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4187 __ Branch(if_true, eq, v0, Operand(at));
4188 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4189 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004190 } else if (FLAG_harmony_typeof &&
4191 check->Equals(isolate()->heap()->null_symbol())) {
4192 __ LoadRoot(at, Heap::kNullValueRootIndex);
4193 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004194 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4195 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4196 __ Branch(if_true, eq, v0, Operand(at));
4197 __ JumpIfSmi(v0, if_false);
4198 // Check for undetectable objects => true.
4199 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4200 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4201 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4202 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4203 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4204 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004205 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4206 __ GetObjectType(v0, v0, a1);
4207 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4208 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4209 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004210 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4211 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004212 if (!FLAG_harmony_typeof) {
4213 __ LoadRoot(at, Heap::kNullValueRootIndex);
4214 __ Branch(if_true, eq, v0, Operand(at));
4215 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004216 // Check for JS objects => true.
4217 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004218 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004219 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004220 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004221 // Check for undetectable objects => false.
4222 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4223 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4224 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4225 } else {
4226 if (if_false != fall_through) __ jmp(if_false);
4227 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004228 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004229}
4230
4231
ager@chromium.org5c838252010-02-19 08:53:10 +00004232void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004233 Comment cmnt(masm_, "[ CompareOperation");
4234 SetSourcePosition(expr->position());
4235
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004236 // First we try a fast inlined version of the compare when one of
4237 // the operands is a literal.
4238 if (TryLiteralCompare(expr)) return;
4239
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004240 // Always perform the comparison for its control flow. Pack the result
4241 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004242 Label materialize_true, materialize_false;
4243 Label* if_true = NULL;
4244 Label* if_false = NULL;
4245 Label* fall_through = NULL;
4246 context()->PrepareTest(&materialize_true, &materialize_false,
4247 &if_true, &if_false, &fall_through);
4248
ager@chromium.org04921a82011-06-27 13:21:41 +00004249 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004250 VisitForStackValue(expr->left());
4251 switch (op) {
4252 case Token::IN:
4253 VisitForStackValue(expr->right());
4254 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004255 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004256 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4257 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4258 break;
4259
4260 case Token::INSTANCEOF: {
4261 VisitForStackValue(expr->right());
4262 InstanceofStub stub(InstanceofStub::kNoFlags);
4263 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004264 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004265 // The stub returns 0 for true.
4266 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4267 break;
4268 }
4269
4270 default: {
4271 VisitForAccumulatorValue(expr->right());
4272 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004273 switch (op) {
4274 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004275 case Token::EQ:
4276 cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004277 break;
4278 case Token::LT:
4279 cc = lt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004280 break;
4281 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004282 cc = gt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004283 break;
4284 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004285 cc = le;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004286 break;
4287 case Token::GTE:
4288 cc = ge;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004289 break;
4290 case Token::IN:
4291 case Token::INSTANCEOF:
4292 default:
4293 UNREACHABLE();
4294 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004295 __ mov(a0, result_register());
4296 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004297
4298 bool inline_smi_code = ShouldInlineSmiCase(op);
4299 JumpPatchSite patch_site(masm_);
4300 if (inline_smi_code) {
4301 Label slow_case;
4302 __ Or(a2, a0, Operand(a1));
4303 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4304 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4305 __ bind(&slow_case);
4306 }
4307 // Record position and call the compare IC.
4308 SetSourcePosition(expr->position());
4309 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004310 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004311 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004312 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004313 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4314 }
4315 }
4316
4317 // Convert the result of the comparison into one expected for this
4318 // expression's context.
4319 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004320}
4321
4322
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004323void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4324 Expression* sub_expr,
4325 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004326 Label materialize_true, materialize_false;
4327 Label* if_true = NULL;
4328 Label* if_false = NULL;
4329 Label* fall_through = NULL;
4330 context()->PrepareTest(&materialize_true, &materialize_false,
4331 &if_true, &if_false, &fall_through);
4332
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004333 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004334 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004335 Heap::RootListIndex nil_value = nil == kNullValue ?
4336 Heap::kNullValueRootIndex :
4337 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004338 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004339 __ LoadRoot(a1, nil_value);
4340 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004341 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4342 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004343 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4344 Heap::kUndefinedValueRootIndex :
4345 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004346 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004347 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004348 __ Branch(if_true, eq, a0, Operand(a1));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004349 __ JumpIfSmi(a0, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004350 // It can be an undetectable object.
4351 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4352 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4353 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4354 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4355 }
4356 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004357}
4358
4359
ager@chromium.org5c838252010-02-19 08:53:10 +00004360void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004361 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4362 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004363}
4364
4365
lrn@chromium.org7516f052011-03-30 08:52:27 +00004366Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004367 return v0;
4368}
ager@chromium.org5c838252010-02-19 08:53:10 +00004369
4370
lrn@chromium.org7516f052011-03-30 08:52:27 +00004371Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004372 return cp;
4373}
4374
4375
ager@chromium.org5c838252010-02-19 08:53:10 +00004376void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004377 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4378 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004379}
4380
4381
4382void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004383 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004384}
4385
4386
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004387void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4388 Scope* declaration_scope = scope()->DeclarationScope();
4389 if (declaration_scope->is_global_scope()) {
4390 // Contexts nested in the global context have a canonical empty function
4391 // as their closure, not the anonymous closure containing the global
4392 // code. Pass a smi sentinel and let the runtime look up the empty
4393 // function.
4394 __ li(at, Operand(Smi::FromInt(0)));
4395 } else if (declaration_scope->is_eval_scope()) {
4396 // Contexts created by a call to eval have the same closure as the
4397 // context calling eval, not the anonymous closure containing the eval
4398 // code. Fetch it from the context.
4399 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4400 } else {
4401 ASSERT(declaration_scope->is_function_scope());
4402 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4403 }
4404 __ push(at);
4405}
4406
4407
ager@chromium.org5c838252010-02-19 08:53:10 +00004408// ----------------------------------------------------------------------------
4409// Non-local control flow support.
4410
4411void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004412 ASSERT(!result_register().is(a1));
4413 // Store result register while executing finally block.
4414 __ push(result_register());
4415 // Cook return address in link register to stack (smi encoded Code* delta).
4416 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4417 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004418 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004419 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4420 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004421}
4422
4423
4424void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004425 ASSERT(!result_register().is(a1));
4426 // Restore result register from stack.
4427 __ pop(a1);
4428 // Uncook return address and return.
4429 __ pop(result_register());
4430 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4431 __ sra(a1, a1, 1); // Un-smi-tag value.
4432 __ Addu(at, a1, Operand(masm_->CodeObject()));
4433 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004434}
4435
4436
4437#undef __
4438
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004439#define __ ACCESS_MASM(masm())
4440
4441FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4442 int* stack_depth,
4443 int* context_length) {
4444 // The macros used here must preserve the result register.
4445
4446 // Because the handler block contains the context of the finally
4447 // code, we can restore it directly from there for the finally code
4448 // rather than iteratively unwinding contexts via their previous
4449 // links.
4450 __ Drop(*stack_depth); // Down to the handler block.
4451 if (*context_length > 0) {
4452 // Restore the context to its dedicated register and the stack.
4453 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4454 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4455 }
4456 __ PopTryHandler();
4457 __ Call(finally_entry_);
4458
4459 *stack_depth = 0;
4460 *context_length = 0;
4461 return previous_;
4462}
4463
4464
4465#undef __
4466
ager@chromium.org5c838252010-02-19 08:53:10 +00004467} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004468
4469#endif // V8_TARGET_ARCH_MIPS