blob: d6e7a440e6a93d1fb4d707bef09b08a47057da24 [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
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000122int FullCodeGenerator::self_optimization_header_size() {
ulan@chromium.org967e2702012-02-28 09:49:15 +0000123 return 11 * Instruction::kInstrSize;
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000124}
125
126
lrn@chromium.org7516f052011-03-30 08:52:27 +0000127// Generate code for a JS function. On entry to the function the receiver
128// and arguments have been pushed on the stack left to right. The actual
129// argument count matches the formal parameter count expected by the
130// function.
131//
132// The live registers are:
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000133// o a1: the JS function object being called (i.e. ourselves)
lrn@chromium.org7516f052011-03-30 08:52:27 +0000134// o cp: our context
135// o fp: our caller's frame pointer
136// o sp: stack pointer
137// o ra: return address
138//
139// The function builds a JS frame. Please see JavaScriptFrameConstants in
140// frames-mips.h for its layout.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000141void FullCodeGenerator::Generate() {
142 CompilationInfo* info = info_;
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +0000143 handler_table_ =
144 isolate()->factory()->NewFixedArray(function()->handler_count(), TENURED);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000145 SetFunctionPosition(function());
146 Comment cmnt(masm_, "[ function compiled by full code generator");
147
ulan@chromium.org65a89c22012-02-14 11:46:07 +0000148 // We can optionally optimize based on counters rather than statistical
149 // sampling.
150 if (info->ShouldSelfOptimize()) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000151 if (FLAG_trace_opt_verbose) {
ulan@chromium.org65a89c22012-02-14 11:46:07 +0000152 PrintF("[adding self-optimization header to %s]\n",
153 *info->function()->debug_name()->ToCString());
154 }
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000155 has_self_optimization_header_ = true;
ulan@chromium.org65a89c22012-02-14 11:46:07 +0000156 MaybeObject* maybe_cell = isolate()->heap()->AllocateJSGlobalPropertyCell(
157 Smi::FromInt(Compiler::kCallsUntilPrimitiveOpt));
158 JSGlobalPropertyCell* cell;
159 if (maybe_cell->To(&cell)) {
160 __ li(a2, Handle<JSGlobalPropertyCell>(cell));
161 __ lw(a3, FieldMemOperand(a2, JSGlobalPropertyCell::kValueOffset));
162 __ Subu(a3, a3, Operand(Smi::FromInt(1)));
163 __ sw(a3, FieldMemOperand(a2, JSGlobalPropertyCell::kValueOffset));
164 Handle<Code> compile_stub(
165 isolate()->builtins()->builtin(Builtins::kLazyRecompile));
166 __ Jump(compile_stub, RelocInfo::CODE_TARGET, eq, a3, Operand(zero_reg));
ulan@chromium.org967e2702012-02-28 09:49:15 +0000167 ASSERT_EQ(masm_->pc_offset(), self_optimization_header_size());
ulan@chromium.org65a89c22012-02-14 11:46:07 +0000168 }
169 }
170
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000171#ifdef DEBUG
172 if (strlen(FLAG_stop_at) > 0 &&
173 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
174 __ stop("stop-at");
175 }
176#endif
177
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000178 // Strict mode functions and builtins need to replace the receiver
179 // with undefined when called as functions (without an explicit
180 // receiver object). t1 is zero for method calls and non-zero for
181 // function calls.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000182 if (!info->is_classic_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000183 Label ok;
184 __ Branch(&ok, eq, t1, Operand(zero_reg));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000185 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000186 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
187 __ sw(a2, MemOperand(sp, receiver_offset));
188 __ bind(&ok);
189 }
190
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000191 // Open a frame scope to indicate that there is a frame on the stack. The
192 // MANUAL indicates that the scope shouldn't actually generate code to set up
193 // the frame (that is done below).
194 FrameScope frame_scope(masm_, StackFrame::MANUAL);
195
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000196 int locals_count = info->scope()->num_stack_slots();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000197
198 __ Push(ra, fp, cp, a1);
199 if (locals_count > 0) {
200 // Load undefined value here, so the value is ready for the loop
201 // below.
202 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
203 }
204 // Adjust fp to point to caller's fp.
205 __ Addu(fp, sp, Operand(2 * kPointerSize));
206
207 { Comment cmnt(masm_, "[ Allocate locals");
208 for (int i = 0; i < locals_count; i++) {
209 __ push(at);
210 }
211 }
212
213 bool function_in_register = true;
214
215 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000216 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000217 if (heap_slots > 0) {
218 Comment cmnt(masm_, "[ Allocate local context");
219 // Argument to NewContext is the function, which is in a1.
220 __ push(a1);
221 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
222 FastNewContextStub stub(heap_slots);
223 __ CallStub(&stub);
224 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000225 __ CallRuntime(Runtime::kNewFunctionContext, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000226 }
227 function_in_register = false;
228 // Context is returned in both v0 and cp. It replaces the context
229 // passed to us. It's saved in the stack and kept live in cp.
230 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
231 // Copy any necessary parameters into the context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000232 int num_parameters = info->scope()->num_parameters();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000233 for (int i = 0; i < num_parameters; i++) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000234 Variable* var = scope()->parameter(i);
235 if (var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000236 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
237 (num_parameters - 1 - i) * kPointerSize;
238 // Load parameter from stack.
239 __ lw(a0, MemOperand(fp, parameter_offset));
240 // Store it in the context.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000241 MemOperand target = ContextOperand(cp, var->index());
242 __ sw(a0, target);
243
244 // Update the write barrier.
245 __ RecordWriteContextSlot(
246 cp, target.offset(), a0, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000247 }
248 }
249 }
250
251 Variable* arguments = scope()->arguments();
252 if (arguments != NULL) {
253 // Function uses arguments object.
254 Comment cmnt(masm_, "[ Allocate arguments object");
255 if (!function_in_register) {
256 // Load this again, if it's used by the local context below.
257 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
258 } else {
259 __ mov(a3, a1);
260 }
261 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000262 int num_parameters = info->scope()->num_parameters();
263 int offset = num_parameters * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000264 __ Addu(a2, fp,
265 Operand(StandardFrameConstants::kCallerSPOffset + offset));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000266 __ li(a1, Operand(Smi::FromInt(num_parameters)));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000267 __ Push(a3, a2, a1);
268
269 // Arguments to ArgumentsAccessStub:
270 // function, receiver address, parameter count.
271 // The stub will rewrite receiever and parameter count if the previous
272 // stack frame was an arguments adapter frame.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000273 ArgumentsAccessStub::Type type;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000274 if (!is_classic_mode()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000275 type = ArgumentsAccessStub::NEW_STRICT;
276 } else if (function()->has_duplicate_parameters()) {
277 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
278 } else {
279 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
280 }
281 ArgumentsAccessStub stub(type);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000282 __ CallStub(&stub);
283
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000284 SetVar(arguments, v0, a1, a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000285 }
286
287 if (FLAG_trace) {
288 __ CallRuntime(Runtime::kTraceEnter, 0);
289 }
290
291 // Visit the declarations and body unless there is an illegal
292 // redeclaration.
293 if (scope()->HasIllegalRedeclaration()) {
294 Comment cmnt(masm_, "[ Declarations");
295 scope()->VisitIllegalRedeclaration(this);
296
297 } else {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000298 PrepareForBailoutForId(AstNode::kFunctionEntryId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000299 { Comment cmnt(masm_, "[ Declarations");
300 // For named function expressions, declare the function name as a
301 // constant.
302 if (scope()->is_function_scope() && scope()->function() != NULL) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000303 VariableProxy* proxy = scope()->function();
304 ASSERT(proxy->var()->mode() == CONST ||
305 proxy->var()->mode() == CONST_HARMONY);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000306 ASSERT(proxy->var()->location() != Variable::UNALLOCATED);
307 EmitDeclaration(proxy, proxy->var()->mode(), NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000308 }
309 VisitDeclarations(scope()->declarations());
310 }
311
312 { Comment cmnt(masm_, "[ Stack check");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000313 PrepareForBailoutForId(AstNode::kDeclarationsId, NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000314 Label ok;
315 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
316 __ Branch(&ok, hs, sp, Operand(t0));
317 StackCheckStub stub;
318 __ CallStub(&stub);
319 __ bind(&ok);
320 }
321
322 { Comment cmnt(masm_, "[ Body");
323 ASSERT(loop_depth() == 0);
324 VisitStatements(function()->body());
325 ASSERT(loop_depth() == 0);
326 }
327 }
328
329 // Always emit a 'return undefined' in case control fell off the end of
330 // the body.
331 { Comment cmnt(masm_, "[ return <undefined>;");
332 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
333 }
334 EmitReturnSequence();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000335}
336
337
338void FullCodeGenerator::ClearAccumulator() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000339 ASSERT(Smi::FromInt(0) == 0);
340 __ mov(v0, zero_reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000341}
342
343
yangguo@chromium.org56454712012-02-16 15:33:53 +0000344void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt,
345 Label* back_edge_target) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000346 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
347 // to make sure it is constant. Branch may emit a skip-or-jump sequence
348 // instead of the normal Branch. It seems that the "skip" part of that
349 // sequence is about as long as this Branch would be so it is safe to ignore
350 // that.
351 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000352 Comment cmnt(masm_, "[ Stack check");
353 Label ok;
354 __ LoadRoot(t0, Heap::kStackLimitRootIndex);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000355 __ sltu(at, sp, t0);
356 __ beq(at, zero_reg, &ok);
357 // CallStub will emit a li t9, ... first, so it is safe to use the delay slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000358 StackCheckStub stub;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000359 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000360 // Record a mapping of this PC offset to the OSR id. This is used to find
361 // the AST id from the unoptimized code in order to use it as a key into
362 // the deoptimization input data found in the optimized code.
363 RecordStackCheck(stmt->OsrEntryId());
364
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000365 __ bind(&ok);
366 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
367 // Record a mapping of the OSR id to this PC. This is used if the OSR
368 // entry becomes the target of a bailout. We don't expect it to be, but
369 // we want it to work if it is.
370 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
ager@chromium.org5c838252010-02-19 08:53:10 +0000371}
372
373
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000374void FullCodeGenerator::EmitReturnSequence() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000375 Comment cmnt(masm_, "[ Return sequence");
376 if (return_label_.is_bound()) {
377 __ Branch(&return_label_);
378 } else {
379 __ bind(&return_label_);
380 if (FLAG_trace) {
381 // Push the return value on the stack as the parameter.
382 // Runtime::TraceExit returns its parameter in v0.
383 __ push(v0);
384 __ CallRuntime(Runtime::kTraceExit, 1);
385 }
386
387#ifdef DEBUG
388 // Add a label for checking the size of the code used for returning.
389 Label check_exit_codesize;
390 masm_->bind(&check_exit_codesize);
391#endif
392 // Make sure that the constant pool is not emitted inside of the return
393 // sequence.
394 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
395 // Here we use masm_-> instead of the __ macro to avoid the code coverage
396 // tool from instrumenting as we rely on the code size here.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000397 int32_t sp_delta = (info_->scope()->num_parameters() + 1) * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000398 CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
399 __ RecordJSReturn();
400 masm_->mov(sp, fp);
401 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
402 masm_->Addu(sp, sp, Operand(sp_delta));
403 masm_->Jump(ra);
404 }
405
406#ifdef DEBUG
407 // Check that the size of the code used for returning is large enough
408 // for the debugger's requirements.
409 ASSERT(Assembler::kJSReturnSequenceInstructions <=
410 masm_->InstructionsGeneratedSince(&check_exit_codesize));
411#endif
412 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000413}
414
415
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000416void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
417 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
ager@chromium.org5c838252010-02-19 08:53:10 +0000418}
419
420
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000421void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
422 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
423 codegen()->GetVar(result_register(), var);
ager@chromium.org5c838252010-02-19 08:53:10 +0000424}
425
426
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000427void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
428 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
429 codegen()->GetVar(result_register(), var);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000430 __ push(result_register());
ager@chromium.org5c838252010-02-19 08:53:10 +0000431}
432
433
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000434void FullCodeGenerator::TestContext::Plug(Variable* var) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000435 // For simplicity we always test the accumulator register.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000436 codegen()->GetVar(result_register(), var);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000437 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000438 codegen()->DoTest(this);
ager@chromium.org5c838252010-02-19 08:53:10 +0000439}
440
441
lrn@chromium.org7516f052011-03-30 08:52:27 +0000442void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
ager@chromium.org5c838252010-02-19 08:53:10 +0000443}
444
445
lrn@chromium.org7516f052011-03-30 08:52:27 +0000446void FullCodeGenerator::AccumulatorValueContext::Plug(
447 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000448 __ LoadRoot(result_register(), index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000449}
450
451
452void FullCodeGenerator::StackValueContext::Plug(
453 Heap::RootListIndex index) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000454 __ LoadRoot(result_register(), index);
455 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000456}
457
458
459void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000460 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000461 true,
462 true_label_,
463 false_label_);
464 if (index == Heap::kUndefinedValueRootIndex ||
465 index == Heap::kNullValueRootIndex ||
466 index == Heap::kFalseValueRootIndex) {
467 if (false_label_ != fall_through_) __ Branch(false_label_);
468 } else if (index == Heap::kTrueValueRootIndex) {
469 if (true_label_ != fall_through_) __ Branch(true_label_);
470 } else {
471 __ LoadRoot(result_register(), index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000472 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000473 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000474}
475
476
477void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000478}
479
480
481void FullCodeGenerator::AccumulatorValueContext::Plug(
482 Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000483 __ li(result_register(), Operand(lit));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000484}
485
486
487void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000488 // Immediates cannot be pushed directly.
489 __ li(result_register(), Operand(lit));
490 __ push(result_register());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000491}
492
493
494void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000495 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000496 true,
497 true_label_,
498 false_label_);
499 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
500 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
501 if (false_label_ != fall_through_) __ Branch(false_label_);
502 } else if (lit->IsTrue() || lit->IsJSObject()) {
503 if (true_label_ != fall_through_) __ Branch(true_label_);
504 } else if (lit->IsString()) {
505 if (String::cast(*lit)->length() == 0) {
506 if (false_label_ != fall_through_) __ Branch(false_label_);
507 } else {
508 if (true_label_ != fall_through_) __ Branch(true_label_);
509 }
510 } else if (lit->IsSmi()) {
511 if (Smi::cast(*lit)->value() == 0) {
512 if (false_label_ != fall_through_) __ Branch(false_label_);
513 } else {
514 if (true_label_ != fall_through_) __ Branch(true_label_);
515 }
516 } else {
517 // For simplicity we always test the accumulator register.
518 __ li(result_register(), Operand(lit));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000519 codegen()->DoTest(this);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000520 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000521}
522
523
524void FullCodeGenerator::EffectContext::DropAndPlug(int count,
525 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000526 ASSERT(count > 0);
527 __ Drop(count);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000528}
529
530
531void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
532 int count,
533 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000534 ASSERT(count > 0);
535 __ Drop(count);
536 __ Move(result_register(), reg);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000537}
538
539
540void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
541 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000542 ASSERT(count > 0);
543 if (count > 1) __ Drop(count - 1);
544 __ sw(reg, MemOperand(sp, 0));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000545}
546
547
548void FullCodeGenerator::TestContext::DropAndPlug(int count,
549 Register reg) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000550 ASSERT(count > 0);
551 // For simplicity we always test the accumulator register.
552 __ Drop(count);
553 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000554 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000555 codegen()->DoTest(this);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000556}
557
558
559void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
560 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000561 ASSERT(materialize_true == materialize_false);
562 __ bind(materialize_true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000563}
564
565
566void FullCodeGenerator::AccumulatorValueContext::Plug(
567 Label* materialize_true,
568 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000569 Label done;
570 __ bind(materialize_true);
571 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
572 __ Branch(&done);
573 __ bind(materialize_false);
574 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
575 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000576}
577
578
579void FullCodeGenerator::StackValueContext::Plug(
580 Label* materialize_true,
581 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000582 Label done;
583 __ bind(materialize_true);
584 __ LoadRoot(at, Heap::kTrueValueRootIndex);
585 __ push(at);
586 __ Branch(&done);
587 __ bind(materialize_false);
588 __ LoadRoot(at, Heap::kFalseValueRootIndex);
589 __ push(at);
590 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000591}
592
593
594void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
595 Label* materialize_false) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000596 ASSERT(materialize_true == true_label_);
597 ASSERT(materialize_false == false_label_);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000598}
599
600
601void FullCodeGenerator::EffectContext::Plug(bool flag) const {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000602}
603
604
605void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000606 Heap::RootListIndex value_root_index =
607 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
608 __ LoadRoot(result_register(), value_root_index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000609}
610
611
612void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000613 Heap::RootListIndex value_root_index =
614 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
615 __ LoadRoot(at, value_root_index);
616 __ push(at);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000617}
618
619
620void FullCodeGenerator::TestContext::Plug(bool flag) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000621 codegen()->PrepareForBailoutBeforeSplit(condition(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000622 true,
623 true_label_,
624 false_label_);
625 if (flag) {
626 if (true_label_ != fall_through_) __ Branch(true_label_);
627 } else {
628 if (false_label_ != fall_through_) __ Branch(false_label_);
629 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000630}
631
632
whesse@chromium.org7b260152011-06-20 15:33:18 +0000633void FullCodeGenerator::DoTest(Expression* condition,
634 Label* if_true,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000635 Label* if_false,
636 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000637 if (CpuFeatures::IsSupported(FPU)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000638 ToBooleanStub stub(result_register());
639 __ CallStub(&stub);
640 __ mov(at, zero_reg);
641 } else {
642 // Call the runtime to find the boolean value of the source and then
643 // translate it into control flow to the pair of labels.
644 __ push(result_register());
645 __ CallRuntime(Runtime::kToBool, 1);
646 __ LoadRoot(at, Heap::kFalseValueRootIndex);
647 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000648 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000649}
650
651
lrn@chromium.org7516f052011-03-30 08:52:27 +0000652void FullCodeGenerator::Split(Condition cc,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000653 Register lhs,
654 const Operand& rhs,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000655 Label* if_true,
656 Label* if_false,
657 Label* fall_through) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000658 if (if_false == fall_through) {
659 __ Branch(if_true, cc, lhs, rhs);
660 } else if (if_true == fall_through) {
661 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
662 } else {
663 __ Branch(if_true, cc, lhs, rhs);
664 __ Branch(if_false);
665 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000666}
667
668
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000669MemOperand FullCodeGenerator::StackOperand(Variable* var) {
670 ASSERT(var->IsStackAllocated());
671 // Offset is negative because higher indexes are at lower addresses.
672 int offset = -var->index() * kPointerSize;
673 // Adjust by a (parameter or local) base offset.
674 if (var->IsParameter()) {
675 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
676 } else {
677 offset += JavaScriptFrameConstants::kLocal0Offset;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000678 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000679 return MemOperand(fp, offset);
ager@chromium.org5c838252010-02-19 08:53:10 +0000680}
681
682
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000683MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
684 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
685 if (var->IsContextSlot()) {
686 int context_chain_length = scope()->ContextChainLength(var->scope());
687 __ LoadContext(scratch, context_chain_length);
688 return ContextOperand(scratch, var->index());
689 } else {
690 return StackOperand(var);
691 }
692}
693
694
695void FullCodeGenerator::GetVar(Register dest, Variable* var) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000696 // Use destination as scratch.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000697 MemOperand location = VarOperand(var, dest);
698 __ lw(dest, location);
699}
700
701
702void FullCodeGenerator::SetVar(Variable* var,
703 Register src,
704 Register scratch0,
705 Register scratch1) {
706 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
707 ASSERT(!scratch0.is(src));
708 ASSERT(!scratch0.is(scratch1));
709 ASSERT(!scratch1.is(src));
710 MemOperand location = VarOperand(var, scratch0);
711 __ sw(src, location);
712 // Emit the write barrier code if the location is in the heap.
713 if (var->IsContextSlot()) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000714 __ RecordWriteContextSlot(scratch0,
715 location.offset(),
716 src,
717 scratch1,
718 kRAHasBeenSaved,
719 kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000720 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000721}
722
723
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000724void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000725 bool should_normalize,
726 Label* if_true,
727 Label* if_false) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000728 // Only prepare for bailouts before splits if we're in a test
729 // context. Otherwise, we let the Visit function deal with the
730 // preparation to avoid preparing with the same AST id twice.
731 if (!context()->IsTest() || !info_->IsOptimizable()) return;
732
733 Label skip;
734 if (should_normalize) __ Branch(&skip);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000735 PrepareForBailout(expr, TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000736 if (should_normalize) {
737 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
738 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
739 __ bind(&skip);
740 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000741}
742
743
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000744void FullCodeGenerator::EmitDeclaration(VariableProxy* proxy,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000745 VariableMode mode,
yangguo@chromium.org56454712012-02-16 15:33:53 +0000746 FunctionLiteral* function) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000747 // If it was not possible to allocate the variable at compile time, we
748 // need to "declare" it at runtime to make sure it actually exists in the
749 // local context.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000750 Variable* variable = proxy->var();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000751 bool binding_needs_init = (function == NULL) &&
752 (mode == CONST || mode == CONST_HARMONY || mode == LET);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000753 switch (variable->location()) {
754 case Variable::UNALLOCATED:
yangguo@chromium.org56454712012-02-16 15:33:53 +0000755 ++global_count_;
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000756 break;
757
758 case Variable::PARAMETER:
759 case Variable::LOCAL:
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000760 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000761 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000762 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000763 __ sw(result_register(), StackOperand(variable));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000764 } else if (binding_needs_init) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000765 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000766 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000767 __ sw(t0, StackOperand(variable));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000768 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000769 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000770
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000771 case Variable::CONTEXT:
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000772 // The variable in the decl always resides in the current function
773 // context.
774 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
775 if (FLAG_debug_code) {
776 // Check that we're not inside a with or catch context.
777 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
778 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
779 __ Check(ne, "Declaration in with context.",
780 a1, Operand(t0));
781 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
782 __ Check(ne, "Declaration in catch context.",
783 a1, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000784 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000785 if (function != NULL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000786 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000787 VisitForAccumulatorValue(function);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000788 __ sw(result_register(), ContextOperand(cp, variable->index()));
789 int offset = Context::SlotOffset(variable->index());
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000790 // We know that we have written a function, which is not a smi.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000791 __ RecordWriteContextSlot(cp,
792 offset,
793 result_register(),
794 a2,
795 kRAHasBeenSaved,
796 kDontSaveFPRegs,
797 EMIT_REMEMBERED_SET,
798 OMIT_SMI_CHECK);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000799 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000800 } else if (binding_needs_init) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000801 Comment cmnt(masm_, "[ Declaration");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000802 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000803 __ sw(at, ContextOperand(cp, variable->index()));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000804 // No write barrier since the_hole_value is in old space.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000805 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000806 }
807 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000808
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000809 case Variable::LOOKUP: {
810 Comment cmnt(masm_, "[ Declaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000811 __ li(a2, Operand(variable->name()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000812 // Declaration nodes are always introduced in one of four modes.
813 ASSERT(mode == VAR ||
814 mode == CONST ||
815 mode == CONST_HARMONY ||
816 mode == LET);
817 PropertyAttributes attr = (mode == CONST || mode == CONST_HARMONY)
818 ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000819 __ li(a1, Operand(Smi::FromInt(attr)));
820 // Push initial value, if any.
821 // Note: For variables we must not push an initial value (such as
822 // 'undefined') because we may have a (legal) redeclaration and we
823 // must not destroy the current value.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000824 if (function != NULL) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000825 __ Push(cp, a2, a1);
826 // Push initial value for function declaration.
827 VisitForStackValue(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000828 } else if (binding_needs_init) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000829 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
830 __ Push(cp, a2, a1, a0);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000831 } else {
832 ASSERT(Smi::FromInt(0) == 0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000833 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000834 __ Push(cp, a2, a1, a0);
835 }
836 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
837 break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000838 }
839 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000840}
841
842
ager@chromium.org5c838252010-02-19 08:53:10 +0000843void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000844 // Call the runtime to declare the globals.
845 // The context is the first argument.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000846 __ li(a1, Operand(pairs));
847 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
848 __ Push(cp, a1, a0);
849 __ CallRuntime(Runtime::kDeclareGlobals, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000850 // Return value is ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +0000851}
852
853
lrn@chromium.org7516f052011-03-30 08:52:27 +0000854void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000855 Comment cmnt(masm_, "[ SwitchStatement");
856 Breakable nested_statement(this, stmt);
857 SetStatementPosition(stmt);
858
859 // Keep the switch value on the stack until a case matches.
860 VisitForStackValue(stmt->tag());
861 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
862
863 ZoneList<CaseClause*>* clauses = stmt->cases();
864 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
865
866 Label next_test; // Recycled for each test.
867 // Compile all the tests with branches to their bodies.
868 for (int i = 0; i < clauses->length(); i++) {
869 CaseClause* clause = clauses->at(i);
870 clause->body_target()->Unuse();
871
872 // The default is not a test, but remember it as final fall through.
873 if (clause->is_default()) {
874 default_clause = clause;
875 continue;
876 }
877
878 Comment cmnt(masm_, "[ Case comparison");
879 __ bind(&next_test);
880 next_test.Unuse();
881
882 // Compile the label expression.
883 VisitForAccumulatorValue(clause->label());
884 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
885
886 // Perform the comparison as if via '==='.
887 __ lw(a1, MemOperand(sp, 0)); // Switch value.
888 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
889 JumpPatchSite patch_site(masm_);
890 if (inline_smi_code) {
891 Label slow_case;
892 __ or_(a2, a1, a0);
893 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
894
895 __ Branch(&next_test, ne, a1, Operand(a0));
896 __ Drop(1); // Switch value is no longer needed.
897 __ Branch(clause->body_target());
898
899 __ bind(&slow_case);
900 }
901
902 // Record position before stub call for type feedback.
903 SetSourcePosition(clause->position());
904 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000905 __ Call(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000906 patch_site.EmitPatchInfo();
danno@chromium.org40cb8782011-05-25 07:58:50 +0000907
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000908 __ Branch(&next_test, ne, v0, Operand(zero_reg));
909 __ Drop(1); // Switch value is no longer needed.
910 __ Branch(clause->body_target());
911 }
912
913 // Discard the test value and jump to the default if present, otherwise to
914 // the end of the statement.
915 __ bind(&next_test);
916 __ Drop(1); // Switch value is no longer needed.
917 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000918 __ Branch(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000919 } else {
920 __ Branch(default_clause->body_target());
921 }
922
923 // Compile all the case bodies.
924 for (int i = 0; i < clauses->length(); i++) {
925 Comment cmnt(masm_, "[ Case body");
926 CaseClause* clause = clauses->at(i);
927 __ bind(clause->body_target());
928 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
929 VisitStatements(clause->statements());
930 }
931
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000932 __ bind(nested_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000933 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000934}
935
936
937void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000938 Comment cmnt(masm_, "[ ForInStatement");
939 SetStatementPosition(stmt);
940
941 Label loop, exit;
942 ForIn loop_statement(this, stmt);
943 increment_loop_depth();
944
945 // Get the object to enumerate over. Both SpiderMonkey and JSC
946 // ignore null and undefined in contrast to the specification; see
947 // ECMA-262 section 12.6.4.
948 VisitForAccumulatorValue(stmt->enumerable());
949 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
950 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
951 __ Branch(&exit, eq, a0, Operand(at));
952 Register null_value = t1;
953 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
954 __ Branch(&exit, eq, a0, Operand(null_value));
955
956 // Convert the object to a JS object.
957 Label convert, done_convert;
958 __ JumpIfSmi(a0, &convert);
959 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000960 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000961 __ bind(&convert);
962 __ push(a0);
963 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
964 __ mov(a0, v0);
965 __ bind(&done_convert);
966 __ push(a0);
967
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000968 // Check for proxies.
969 Label call_runtime;
970 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
971 __ GetObjectType(a0, a1, a1);
972 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
973
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000974 // Check cache validity in generated code. This is a fast case for
975 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
976 // guarantee cache validity, call the runtime system to check cache
977 // validity or get the property names in a fixed array.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000978 Label next;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000979 // Preload a couple of values used in the loop.
980 Register empty_fixed_array_value = t2;
981 __ LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
982 Register empty_descriptor_array_value = t3;
983 __ LoadRoot(empty_descriptor_array_value,
984 Heap::kEmptyDescriptorArrayRootIndex);
985 __ mov(a1, a0);
986 __ bind(&next);
987
988 // Check that there are no elements. Register a1 contains the
989 // current JS object we've reached through the prototype chain.
990 __ lw(a2, FieldMemOperand(a1, JSObject::kElementsOffset));
991 __ Branch(&call_runtime, ne, a2, Operand(empty_fixed_array_value));
992
993 // Check that instance descriptors are not empty so that we can
994 // check for an enum cache. Leave the map in a2 for the subsequent
995 // prototype load.
996 __ lw(a2, FieldMemOperand(a1, HeapObject::kMapOffset));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000997 __ lw(a3, FieldMemOperand(a2, Map::kInstanceDescriptorsOrBitField3Offset));
998 __ JumpIfSmi(a3, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000999
1000 // Check that there is an enum cache in the non-empty instance
1001 // descriptors (a3). This is the case if the next enumeration
1002 // index field does not contain a smi.
1003 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumerationIndexOffset));
1004 __ JumpIfSmi(a3, &call_runtime);
1005
1006 // For all objects but the receiver, check that the cache is empty.
1007 Label check_prototype;
1008 __ Branch(&check_prototype, eq, a1, Operand(a0));
1009 __ lw(a3, FieldMemOperand(a3, DescriptorArray::kEnumCacheBridgeCacheOffset));
1010 __ Branch(&call_runtime, ne, a3, Operand(empty_fixed_array_value));
1011
1012 // Load the prototype from the map and loop if non-null.
1013 __ bind(&check_prototype);
1014 __ lw(a1, FieldMemOperand(a2, Map::kPrototypeOffset));
1015 __ Branch(&next, ne, a1, Operand(null_value));
1016
1017 // The enum cache is valid. Load the map of the object being
1018 // iterated over and use the cache for the iteration.
1019 Label use_cache;
1020 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
1021 __ Branch(&use_cache);
1022
1023 // Get the set of properties to enumerate.
1024 __ bind(&call_runtime);
1025 __ push(a0); // Duplicate the enumerable object on the stack.
1026 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1027
1028 // If we got a map from the runtime call, we can do a fast
1029 // modification check. Otherwise, we got a fixed array, and we have
1030 // to do a slow check.
1031 Label fixed_array;
1032 __ mov(a2, v0);
1033 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
1034 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1035 __ Branch(&fixed_array, ne, a1, Operand(at));
1036
1037 // We got a map in register v0. Get the enumeration cache from it.
1038 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001039 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001040 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
1041 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1042
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001043 // Set up the four remaining stack slots.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001044 __ push(v0); // Map.
1045 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
1046 __ li(a0, Operand(Smi::FromInt(0)));
1047 // Push enumeration cache, enumeration cache length (as smi) and zero.
1048 __ Push(a2, a1, a0);
1049 __ jmp(&loop);
1050
1051 // We got a fixed array in register v0. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001052 Label non_proxy;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001053 __ bind(&fixed_array);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001054 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1055 __ lw(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1056 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1057 __ GetObjectType(a2, a3, a3);
1058 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1059 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1060 __ bind(&non_proxy);
1061 __ Push(a1, v0); // Smi and array
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001062 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1063 __ li(a0, Operand(Smi::FromInt(0)));
1064 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1065
1066 // Generate code for doing the condition check.
1067 __ bind(&loop);
1068 // Load the current count to a0, load the length to a1.
1069 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1070 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001071 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001072
1073 // Get the current entry of the array into register a3.
1074 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1075 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1076 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1077 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1078 __ lw(a3, MemOperand(t0)); // Current entry.
1079
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001080 // Get the expected map from the stack or a smi in the
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001081 // permanent slow case into register a2.
1082 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1083
1084 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001085 // If not, we may have to filter the key.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001086 Label update_each;
1087 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1088 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1089 __ Branch(&update_each, eq, t0, Operand(a2));
1090
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001091 // For proxies, no filtering is done.
1092 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1093 ASSERT_EQ(Smi::FromInt(0), 0);
1094 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1095
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001096 // Convert the entry to a string or (smi) 0 if it isn't a property
1097 // any more. If the property has been removed while iterating, we
1098 // just skip it.
1099 __ push(a1); // Enumerable.
1100 __ push(a3); // Current entry.
1101 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1102 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001103 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001104
1105 // Update the 'each' property or variable from the possibly filtered
1106 // entry in register a3.
1107 __ bind(&update_each);
1108 __ mov(result_register(), a3);
1109 // Perform the assignment as if via '='.
1110 { EffectContext context(this);
1111 EmitAssignment(stmt->each(), stmt->AssignmentId());
1112 }
1113
1114 // Generate code for the body of the loop.
1115 Visit(stmt->body());
1116
1117 // Generate code for the going to the next element by incrementing
1118 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001119 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001120 __ pop(a0);
1121 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1122 __ push(a0);
1123
yangguo@chromium.org56454712012-02-16 15:33:53 +00001124 EmitStackCheck(stmt, &loop);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001125 __ Branch(&loop);
1126
1127 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001128 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001129 __ Drop(5);
1130
1131 // Exit and decrement the loop depth.
1132 __ bind(&exit);
1133 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001134}
1135
1136
1137void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1138 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001139 // Use the fast case closure allocation code that allocates in new
1140 // space for nested functions that don't need literals cloning. If
1141 // we're running with the --always-opt or the --prepare-always-opt
1142 // flag, we need to use the runtime function so that the new function
1143 // we are creating here gets a chance to have its code optimized and
1144 // doesn't just get a copy of the existing unoptimized code.
1145 if (!FLAG_always_opt &&
1146 !FLAG_prepare_always_opt &&
1147 !pretenure &&
1148 scope()->is_function_scope() &&
1149 info->num_literals() == 0) {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001150 FastNewClosureStub stub(info->language_mode());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001151 __ li(a0, Operand(info));
1152 __ push(a0);
1153 __ CallStub(&stub);
1154 } else {
1155 __ li(a0, Operand(info));
1156 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1157 : Heap::kFalseValueRootIndex);
1158 __ Push(cp, a0, a1);
1159 __ CallRuntime(Runtime::kNewClosure, 3);
1160 }
1161 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001162}
1163
1164
1165void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001166 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001167 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001168}
1169
1170
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001171void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1172 TypeofState typeof_state,
1173 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001174 Register current = cp;
1175 Register next = a1;
1176 Register temp = a2;
1177
1178 Scope* s = scope();
1179 while (s != NULL) {
1180 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001181 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001182 // Check that extension is NULL.
1183 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1184 __ Branch(slow, ne, temp, Operand(zero_reg));
1185 }
1186 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001187 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001188 // Walk the rest of the chain without clobbering cp.
1189 current = next;
1190 }
1191 // If no outer scope calls eval, we do not need to check more
1192 // context extensions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001193 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001194 s = s->outer_scope();
1195 }
1196
1197 if (s->is_eval_scope()) {
1198 Label loop, fast;
1199 if (!current.is(next)) {
1200 __ Move(next, current);
1201 }
1202 __ bind(&loop);
1203 // Terminate at global context.
1204 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1205 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1206 __ Branch(&fast, eq, temp, Operand(t0));
1207 // Check that extension is NULL.
1208 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1209 __ Branch(slow, ne, temp, Operand(zero_reg));
1210 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001211 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001212 __ Branch(&loop);
1213 __ bind(&fast);
1214 }
1215
1216 __ lw(a0, GlobalObjectOperand());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001217 __ li(a2, Operand(var->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001218 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1219 ? RelocInfo::CODE_TARGET
1220 : RelocInfo::CODE_TARGET_CONTEXT;
1221 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001222 __ Call(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001223}
1224
1225
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001226MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1227 Label* slow) {
1228 ASSERT(var->IsContextSlot());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001229 Register context = cp;
1230 Register next = a3;
1231 Register temp = t0;
1232
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001233 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001234 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001235 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001236 // Check that extension is NULL.
1237 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1238 __ Branch(slow, ne, temp, Operand(zero_reg));
1239 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001240 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001241 // Walk the rest of the chain without clobbering cp.
1242 context = next;
1243 }
1244 }
1245 // Check that last extension is NULL.
1246 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1247 __ Branch(slow, ne, temp, Operand(zero_reg));
1248
1249 // This function is used only for loads, not stores, so it's safe to
1250 // return an cp-based operand (the write barrier cannot be allowed to
1251 // destroy the cp register).
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001252 return ContextOperand(context, var->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001253}
1254
1255
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001256void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1257 TypeofState typeof_state,
1258 Label* slow,
1259 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001260 // Generate fast-case code for variables that might be shadowed by
1261 // eval-introduced variables. Eval is used a lot without
1262 // introducing variables. In those cases, we do not want to
1263 // perform a runtime call for all variables in the scope
1264 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001265 if (var->mode() == DYNAMIC_GLOBAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001266 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001267 __ Branch(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001268 } else if (var->mode() == DYNAMIC_LOCAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001269 Variable* local = var->local_if_not_shadowed();
1270 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001271 if (local->mode() == CONST ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001272 local->mode() == CONST_HARMONY ||
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001273 local->mode() == LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001274 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1275 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001276 if (local->mode() == CONST) {
1277 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1278 __ movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001279 } else { // LET || CONST_HARMONY
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001280 __ Branch(done, ne, at, Operand(zero_reg));
1281 __ li(a0, Operand(var->name()));
1282 __ push(a0);
1283 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1284 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001285 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001286 __ Branch(done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001287 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001288}
1289
1290
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001291void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1292 // Record position before possible IC call.
1293 SetSourcePosition(proxy->position());
1294 Variable* var = proxy->var();
1295
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001296 // Three cases: global variables, lookup variables, and all other types of
1297 // variables.
1298 switch (var->location()) {
1299 case Variable::UNALLOCATED: {
1300 Comment cmnt(masm_, "Global variable");
1301 // Use inline caching. Variable name is passed in a2 and the global
1302 // object (receiver) in a0.
1303 __ lw(a0, GlobalObjectOperand());
1304 __ li(a2, Operand(var->name()));
1305 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1306 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
1307 context()->Plug(v0);
1308 break;
1309 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001310
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001311 case Variable::PARAMETER:
1312 case Variable::LOCAL:
1313 case Variable::CONTEXT: {
1314 Comment cmnt(masm_, var->IsContextSlot()
1315 ? "Context variable"
1316 : "Stack variable");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001317 if (var->binding_needs_init()) {
1318 // var->scope() may be NULL when the proxy is located in eval code and
1319 // refers to a potential outside binding. Currently those bindings are
1320 // always looked up dynamically, i.e. in that case
1321 // var->location() == LOOKUP.
1322 // always holds.
1323 ASSERT(var->scope() != NULL);
1324
1325 // Check if the binding really needs an initialization check. The check
1326 // can be skipped in the following situation: we have a LET or CONST
1327 // binding in harmony mode, both the Variable and the VariableProxy have
1328 // the same declaration scope (i.e. they are both in global code, in the
1329 // same function or in the same eval code) and the VariableProxy is in
1330 // the source physically located after the initializer of the variable.
1331 //
1332 // We cannot skip any initialization checks for CONST in non-harmony
1333 // mode because const variables may be declared but never initialized:
1334 // if (false) { const x; }; var y = x;
1335 //
1336 // The condition on the declaration scopes is a conservative check for
1337 // nested functions that access a binding and are called before the
1338 // binding is initialized:
1339 // function() { f(); let x = 1; function f() { x = 2; } }
1340 //
1341 bool skip_init_check;
1342 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1343 skip_init_check = false;
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001344 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001345 // Check that we always have valid source position.
1346 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1347 ASSERT(proxy->position() != RelocInfo::kNoPosition);
1348 skip_init_check = var->mode() != CONST &&
1349 var->initializer_position() < proxy->position();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001350 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001351
1352 if (!skip_init_check) {
1353 // Let and const need a read barrier.
1354 GetVar(v0, var);
1355 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1356 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1357 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1358 // Throw a reference error when using an uninitialized let/const
1359 // binding in harmony mode.
1360 Label done;
1361 __ Branch(&done, ne, at, Operand(zero_reg));
1362 __ li(a0, Operand(var->name()));
1363 __ push(a0);
1364 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1365 __ bind(&done);
1366 } else {
1367 // Uninitalized const bindings outside of harmony mode are unholed.
1368 ASSERT(var->mode() == CONST);
1369 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1370 __ movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1371 }
1372 context()->Plug(v0);
1373 break;
1374 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001375 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001376 context()->Plug(var);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001377 break;
1378 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001379
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001380 case Variable::LOOKUP: {
1381 Label done, slow;
1382 // Generate code for loading from variables potentially shadowed
1383 // by eval-introduced variables.
1384 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1385 __ bind(&slow);
1386 Comment cmnt(masm_, "Lookup variable");
1387 __ li(a1, Operand(var->name()));
1388 __ Push(cp, a1); // Context and name.
1389 __ CallRuntime(Runtime::kLoadContextSlot, 2);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001390 __ bind(&done);
1391 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001392 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001393 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001394}
1395
1396
1397void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001398 Comment cmnt(masm_, "[ RegExpLiteral");
1399 Label materialized;
1400 // Registers will be used as follows:
1401 // t1 = materialized value (RegExp literal)
1402 // t0 = JS function, literals array
1403 // a3 = literal index
1404 // a2 = RegExp pattern
1405 // a1 = RegExp flags
1406 // a0 = RegExp literal clone
1407 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1408 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1409 int literal_offset =
1410 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1411 __ lw(t1, FieldMemOperand(t0, literal_offset));
1412 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1413 __ Branch(&materialized, ne, t1, Operand(at));
1414
1415 // Create regexp literal using runtime function.
1416 // Result will be in v0.
1417 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1418 __ li(a2, Operand(expr->pattern()));
1419 __ li(a1, Operand(expr->flags()));
1420 __ Push(t0, a3, a2, a1);
1421 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1422 __ mov(t1, v0);
1423
1424 __ bind(&materialized);
1425 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1426 Label allocated, runtime_allocate;
1427 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1428 __ jmp(&allocated);
1429
1430 __ bind(&runtime_allocate);
1431 __ push(t1);
1432 __ li(a0, Operand(Smi::FromInt(size)));
1433 __ push(a0);
1434 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1435 __ pop(t1);
1436
1437 __ bind(&allocated);
1438
1439 // After this, registers are used as follows:
1440 // v0: Newly allocated regexp.
1441 // t1: Materialized regexp.
1442 // a2: temp.
1443 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1444 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001445}
1446
1447
1448void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001449 Comment cmnt(masm_, "[ ObjectLiteral");
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001450 Handle<FixedArray> constant_properties = expr->constant_properties();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001451 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1452 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1453 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001454 __ li(a1, Operand(constant_properties));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001455 int flags = expr->fast_elements()
1456 ? ObjectLiteral::kFastElements
1457 : ObjectLiteral::kNoFlags;
1458 flags |= expr->has_function()
1459 ? ObjectLiteral::kHasFunction
1460 : ObjectLiteral::kNoFlags;
1461 __ li(a0, Operand(Smi::FromInt(flags)));
1462 __ Push(a3, a2, a1, a0);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001463 int properties_count = constant_properties->length() / 2;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001464 if (expr->depth() > 1) {
1465 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001466 } else if (flags != ObjectLiteral::kFastElements ||
1467 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001468 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001469 } else {
1470 FastCloneShallowObjectStub stub(properties_count);
1471 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001472 }
1473
1474 // If result_saved is true the result is on top of the stack. If
1475 // result_saved is false the result is in v0.
1476 bool result_saved = false;
1477
1478 // Mark all computed expressions that are bound to a key that
1479 // is shadowed by a later occurrence of the same key. For the
1480 // marked expressions, no store code is emitted.
1481 expr->CalculateEmitStore();
1482
1483 for (int i = 0; i < expr->properties()->length(); i++) {
1484 ObjectLiteral::Property* property = expr->properties()->at(i);
1485 if (property->IsCompileTimeValue()) continue;
1486
1487 Literal* key = property->key();
1488 Expression* value = property->value();
1489 if (!result_saved) {
1490 __ push(v0); // Save result on stack.
1491 result_saved = true;
1492 }
1493 switch (property->kind()) {
1494 case ObjectLiteral::Property::CONSTANT:
1495 UNREACHABLE();
1496 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1497 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1498 // Fall through.
1499 case ObjectLiteral::Property::COMPUTED:
1500 if (key->handle()->IsSymbol()) {
1501 if (property->emit_store()) {
1502 VisitForAccumulatorValue(value);
1503 __ mov(a0, result_register());
1504 __ li(a2, Operand(key->handle()));
1505 __ lw(a1, MemOperand(sp));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001506 Handle<Code> ic = is_classic_mode()
1507 ? isolate()->builtins()->StoreIC_Initialize()
1508 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001509 __ Call(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001510 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1511 } else {
1512 VisitForEffect(value);
1513 }
1514 break;
1515 }
1516 // Fall through.
1517 case ObjectLiteral::Property::PROTOTYPE:
1518 // Duplicate receiver on stack.
1519 __ lw(a0, MemOperand(sp));
1520 __ push(a0);
1521 VisitForStackValue(key);
1522 VisitForStackValue(value);
1523 if (property->emit_store()) {
1524 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1525 __ push(a0);
1526 __ CallRuntime(Runtime::kSetProperty, 4);
1527 } else {
1528 __ Drop(3);
1529 }
1530 break;
1531 case ObjectLiteral::Property::GETTER:
1532 case ObjectLiteral::Property::SETTER:
1533 // Duplicate receiver on stack.
1534 __ lw(a0, MemOperand(sp));
1535 __ push(a0);
1536 VisitForStackValue(key);
1537 __ li(a1, Operand(property->kind() == ObjectLiteral::Property::SETTER ?
1538 Smi::FromInt(1) :
1539 Smi::FromInt(0)));
1540 __ push(a1);
1541 VisitForStackValue(value);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001542 __ li(a0, Operand(Smi::FromInt(NONE)));
1543 __ push(a0);
1544 __ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001545 break;
1546 }
1547 }
1548
1549 if (expr->has_function()) {
1550 ASSERT(result_saved);
1551 __ lw(a0, MemOperand(sp));
1552 __ push(a0);
1553 __ CallRuntime(Runtime::kToFastProperties, 1);
1554 }
1555
1556 if (result_saved) {
1557 context()->PlugTOS();
1558 } else {
1559 context()->Plug(v0);
1560 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001561}
1562
1563
1564void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001565 Comment cmnt(masm_, "[ ArrayLiteral");
1566
1567 ZoneList<Expression*>* subexprs = expr->values();
1568 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001569
1570 Handle<FixedArray> constant_elements = expr->constant_elements();
1571 ASSERT_EQ(2, constant_elements->length());
1572 ElementsKind constant_elements_kind =
1573 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001574 bool has_fast_elements = constant_elements_kind == FAST_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001575 Handle<FixedArrayBase> constant_elements_values(
1576 FixedArrayBase::cast(constant_elements->get(1)));
1577
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001578 __ mov(a0, result_register());
1579 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1580 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1581 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001582 __ li(a1, Operand(constant_elements));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001583 __ Push(a3, a2, a1);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001584 if (has_fast_elements && constant_elements_values->map() ==
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001585 isolate()->heap()->fixed_cow_array_map()) {
1586 FastCloneShallowArrayStub stub(
1587 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1588 __ CallStub(&stub);
1589 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1590 1, a1, a2);
1591 } else if (expr->depth() > 1) {
1592 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1593 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1594 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1595 } else {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001596 ASSERT(constant_elements_kind == FAST_ELEMENTS ||
1597 constant_elements_kind == FAST_SMI_ONLY_ELEMENTS ||
1598 FLAG_smi_only_arrays);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001599 FastCloneShallowArrayStub::Mode mode = has_fast_elements
1600 ? FastCloneShallowArrayStub::CLONE_ELEMENTS
1601 : FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001602 FastCloneShallowArrayStub stub(mode, length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001603 __ CallStub(&stub);
1604 }
1605
1606 bool result_saved = false; // Is the result saved to the stack?
1607
1608 // Emit code to evaluate all the non-constant subexpressions and to store
1609 // them into the newly cloned array.
1610 for (int i = 0; i < length; i++) {
1611 Expression* subexpr = subexprs->at(i);
1612 // If the subexpression is a literal or a simple materialized literal it
1613 // is already set in the cloned array.
1614 if (subexpr->AsLiteral() != NULL ||
1615 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1616 continue;
1617 }
1618
1619 if (!result_saved) {
1620 __ push(v0);
1621 result_saved = true;
1622 }
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001623
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001624 VisitForAccumulatorValue(subexpr);
1625
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001626 if (constant_elements_kind == FAST_ELEMENTS) {
1627 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1628 __ lw(t2, MemOperand(sp)); // Copy of array literal.
1629 __ lw(a1, FieldMemOperand(t2, JSObject::kElementsOffset));
1630 __ sw(result_register(), FieldMemOperand(a1, offset));
1631 // Update the write barrier for the array store.
1632 __ RecordWriteField(a1, offset, result_register(), a2,
1633 kRAHasBeenSaved, kDontSaveFPRegs,
1634 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1635 } else {
1636 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1637 __ lw(a2, FieldMemOperand(a1, JSObject::kMapOffset));
1638 __ li(a3, Operand(Smi::FromInt(i)));
1639 __ li(t0, Operand(Smi::FromInt(expr->literal_index())));
1640 __ mov(a0, result_register());
1641 StoreArrayLiteralElementStub stub;
1642 __ CallStub(&stub);
1643 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001644
1645 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1646 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001647 if (result_saved) {
1648 context()->PlugTOS();
1649 } else {
1650 context()->Plug(v0);
1651 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001652}
1653
1654
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001655void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001656 Comment cmnt(masm_, "[ Assignment");
1657 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1658 // on the left-hand side.
1659 if (!expr->target()->IsValidLeftHandSide()) {
1660 VisitForEffect(expr->target());
1661 return;
1662 }
1663
1664 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001665 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001666 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1667 LhsKind assign_type = VARIABLE;
1668 Property* property = expr->target()->AsProperty();
1669 if (property != NULL) {
1670 assign_type = (property->key()->IsPropertyName())
1671 ? NAMED_PROPERTY
1672 : KEYED_PROPERTY;
1673 }
1674
1675 // Evaluate LHS expression.
1676 switch (assign_type) {
1677 case VARIABLE:
1678 // Nothing to do here.
1679 break;
1680 case NAMED_PROPERTY:
1681 if (expr->is_compound()) {
1682 // We need the receiver both on the stack and in the accumulator.
1683 VisitForAccumulatorValue(property->obj());
1684 __ push(result_register());
1685 } else {
1686 VisitForStackValue(property->obj());
1687 }
1688 break;
1689 case KEYED_PROPERTY:
1690 // We need the key and receiver on both the stack and in v0 and a1.
1691 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001692 VisitForStackValue(property->obj());
1693 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001694 __ lw(a1, MemOperand(sp, 0));
1695 __ push(v0);
1696 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001697 VisitForStackValue(property->obj());
1698 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001699 }
1700 break;
1701 }
1702
1703 // For compound assignments we need another deoptimization point after the
1704 // variable/property load.
1705 if (expr->is_compound()) {
1706 { AccumulatorValueContext context(this);
1707 switch (assign_type) {
1708 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001709 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001710 PrepareForBailout(expr->target(), TOS_REG);
1711 break;
1712 case NAMED_PROPERTY:
1713 EmitNamedPropertyLoad(property);
1714 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1715 break;
1716 case KEYED_PROPERTY:
1717 EmitKeyedPropertyLoad(property);
1718 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1719 break;
1720 }
1721 }
1722
1723 Token::Value op = expr->binary_op();
1724 __ push(v0); // Left operand goes on the stack.
1725 VisitForAccumulatorValue(expr->value());
1726
1727 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1728 ? OVERWRITE_RIGHT
1729 : NO_OVERWRITE;
1730 SetSourcePosition(expr->position() + 1);
1731 AccumulatorValueContext context(this);
1732 if (ShouldInlineSmiCase(op)) {
1733 EmitInlineSmiBinaryOp(expr->binary_operation(),
1734 op,
1735 mode,
1736 expr->target(),
1737 expr->value());
1738 } else {
1739 EmitBinaryOp(expr->binary_operation(), op, mode);
1740 }
1741
1742 // Deoptimization point in case the binary operation may have side effects.
1743 PrepareForBailout(expr->binary_operation(), TOS_REG);
1744 } else {
1745 VisitForAccumulatorValue(expr->value());
1746 }
1747
1748 // Record source position before possible IC call.
1749 SetSourcePosition(expr->position());
1750
1751 // Store the value.
1752 switch (assign_type) {
1753 case VARIABLE:
1754 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1755 expr->op());
1756 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1757 context()->Plug(v0);
1758 break;
1759 case NAMED_PROPERTY:
1760 EmitNamedPropertyAssignment(expr);
1761 break;
1762 case KEYED_PROPERTY:
1763 EmitKeyedPropertyAssignment(expr);
1764 break;
1765 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001766}
1767
1768
ager@chromium.org5c838252010-02-19 08:53:10 +00001769void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001770 SetSourcePosition(prop->position());
1771 Literal* key = prop->key()->AsLiteral();
1772 __ mov(a0, result_register());
1773 __ li(a2, Operand(key->handle()));
1774 // Call load IC. It has arguments receiver and property name a0 and a2.
1775 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001776 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001777}
1778
1779
1780void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001781 SetSourcePosition(prop->position());
1782 __ mov(a0, result_register());
1783 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1784 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001785 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001786}
1787
1788
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001789void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001790 Token::Value op,
1791 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001792 Expression* left_expr,
1793 Expression* right_expr) {
1794 Label done, smi_case, stub_call;
1795
1796 Register scratch1 = a2;
1797 Register scratch2 = a3;
1798
1799 // Get the arguments.
1800 Register left = a1;
1801 Register right = a0;
1802 __ pop(left);
1803 __ mov(a0, result_register());
1804
1805 // Perform combined smi check on both operands.
1806 __ Or(scratch1, left, Operand(right));
1807 STATIC_ASSERT(kSmiTag == 0);
1808 JumpPatchSite patch_site(masm_);
1809 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1810
1811 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001812 BinaryOpStub stub(op, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001813 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001814 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001815 __ jmp(&done);
1816
1817 __ bind(&smi_case);
1818 // Smi case. This code works the same way as the smi-smi case in the type
1819 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001820 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001821 switch (op) {
1822 case Token::SAR:
1823 __ Branch(&stub_call);
1824 __ GetLeastBitsFromSmi(scratch1, right, 5);
1825 __ srav(right, left, scratch1);
1826 __ And(v0, right, Operand(~kSmiTagMask));
1827 break;
1828 case Token::SHL: {
1829 __ Branch(&stub_call);
1830 __ SmiUntag(scratch1, left);
1831 __ GetLeastBitsFromSmi(scratch2, right, 5);
1832 __ sllv(scratch1, scratch1, scratch2);
1833 __ Addu(scratch2, scratch1, Operand(0x40000000));
1834 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1835 __ SmiTag(v0, scratch1);
1836 break;
1837 }
1838 case Token::SHR: {
1839 __ Branch(&stub_call);
1840 __ SmiUntag(scratch1, left);
1841 __ GetLeastBitsFromSmi(scratch2, right, 5);
1842 __ srlv(scratch1, scratch1, scratch2);
1843 __ And(scratch2, scratch1, 0xc0000000);
1844 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1845 __ SmiTag(v0, scratch1);
1846 break;
1847 }
1848 case Token::ADD:
1849 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1850 __ BranchOnOverflow(&stub_call, scratch1);
1851 break;
1852 case Token::SUB:
1853 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1854 __ BranchOnOverflow(&stub_call, scratch1);
1855 break;
1856 case Token::MUL: {
1857 __ SmiUntag(scratch1, right);
1858 __ Mult(left, scratch1);
1859 __ mflo(scratch1);
1860 __ mfhi(scratch2);
1861 __ sra(scratch1, scratch1, 31);
1862 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1863 __ mflo(v0);
1864 __ Branch(&done, ne, v0, Operand(zero_reg));
1865 __ Addu(scratch2, right, left);
1866 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1867 ASSERT(Smi::FromInt(0) == 0);
1868 __ mov(v0, zero_reg);
1869 break;
1870 }
1871 case Token::BIT_OR:
1872 __ Or(v0, left, Operand(right));
1873 break;
1874 case Token::BIT_AND:
1875 __ And(v0, left, Operand(right));
1876 break;
1877 case Token::BIT_XOR:
1878 __ Xor(v0, left, Operand(right));
1879 break;
1880 default:
1881 UNREACHABLE();
1882 }
1883
1884 __ bind(&done);
1885 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001886}
1887
1888
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001889void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1890 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001891 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001892 __ mov(a0, result_register());
1893 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001894 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001895 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001896 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001897 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001898 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001899}
1900
1901
1902void FullCodeGenerator::EmitAssignment(Expression* expr, int bailout_ast_id) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001903 // Invalid left-hand sides are rewritten to have a 'throw
1904 // ReferenceError' on the left-hand side.
1905 if (!expr->IsValidLeftHandSide()) {
1906 VisitForEffect(expr);
1907 return;
1908 }
1909
1910 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001911 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001912 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1913 LhsKind assign_type = VARIABLE;
1914 Property* prop = expr->AsProperty();
1915 if (prop != NULL) {
1916 assign_type = (prop->key()->IsPropertyName())
1917 ? NAMED_PROPERTY
1918 : KEYED_PROPERTY;
1919 }
1920
1921 switch (assign_type) {
1922 case VARIABLE: {
1923 Variable* var = expr->AsVariableProxy()->var();
1924 EffectContext context(this);
1925 EmitVariableAssignment(var, Token::ASSIGN);
1926 break;
1927 }
1928 case NAMED_PROPERTY: {
1929 __ push(result_register()); // Preserve value.
1930 VisitForAccumulatorValue(prop->obj());
1931 __ mov(a1, result_register());
1932 __ pop(a0); // Restore value.
1933 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001934 Handle<Code> ic = is_classic_mode()
1935 ? isolate()->builtins()->StoreIC_Initialize()
1936 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001937 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001938 break;
1939 }
1940 case KEYED_PROPERTY: {
1941 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001942 VisitForStackValue(prop->obj());
1943 VisitForAccumulatorValue(prop->key());
1944 __ mov(a1, result_register());
1945 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001946 __ pop(a0); // Restore value.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001947 Handle<Code> ic = is_classic_mode()
1948 ? isolate()->builtins()->KeyedStoreIC_Initialize()
1949 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001950 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001951 break;
1952 }
1953 }
1954 PrepareForBailoutForId(bailout_ast_id, TOS_REG);
1955 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001956}
1957
1958
1959void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001960 Token::Value op) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001961 if (var->IsUnallocated()) {
1962 // Global var, const, or let.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001963 __ mov(a0, result_register());
1964 __ li(a2, Operand(var->name()));
1965 __ lw(a1, GlobalObjectOperand());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001966 Handle<Code> ic = is_classic_mode()
1967 ? isolate()->builtins()->StoreIC_Initialize()
1968 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001969 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001970
1971 } else if (op == Token::INIT_CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001972 // Const initializers need a write barrier.
1973 ASSERT(!var->IsParameter()); // No const parameters.
1974 if (var->IsStackLocal()) {
1975 Label skip;
1976 __ lw(a1, StackOperand(var));
1977 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1978 __ Branch(&skip, ne, a1, Operand(t0));
1979 __ sw(result_register(), StackOperand(var));
1980 __ bind(&skip);
1981 } else {
1982 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
1983 // Like var declarations, const declarations are hoisted to function
1984 // scope. However, unlike var initializers, const initializers are
1985 // able to drill a hole to that function context, even from inside a
1986 // 'with' context. We thus bypass the normal static scope lookup for
1987 // var->IsContextSlot().
1988 __ push(v0);
1989 __ li(a0, Operand(var->name()));
1990 __ Push(cp, a0); // Context and name.
1991 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001992 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001993
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001994 } else if (var->mode() == LET && op != Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001995 // Non-initializing assignment to let variable needs a write barrier.
1996 if (var->IsLookupSlot()) {
1997 __ push(v0); // Value.
1998 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001999 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002000 __ Push(cp, a1, a0); // Context, name, strict mode.
2001 __ CallRuntime(Runtime::kStoreContextSlot, 4);
2002 } else {
2003 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
2004 Label assign;
2005 MemOperand location = VarOperand(var, a1);
2006 __ lw(a3, location);
2007 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2008 __ Branch(&assign, ne, a3, Operand(t0));
2009 __ li(a3, Operand(var->name()));
2010 __ push(a3);
2011 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2012 // Perform the assignment.
2013 __ bind(&assign);
2014 __ sw(result_register(), location);
2015 if (var->IsContextSlot()) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002016 // RecordWrite may destroy all its register arguments.
2017 __ mov(a3, result_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002018 int offset = Context::SlotOffset(var->index());
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002019 __ RecordWriteContextSlot(
2020 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002021 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002022 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002023
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002024 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2025 // Assignment to var or initializing assignment to let/const
2026 // in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002027 if (var->IsStackAllocated() || var->IsContextSlot()) {
2028 MemOperand location = VarOperand(var, a1);
2029 if (FLAG_debug_code && op == Token::INIT_LET) {
2030 // Check for an uninitialized let binding.
2031 __ lw(a2, location);
2032 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2033 __ Check(eq, "Let binding re-initialization.", a2, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002034 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002035 // Perform the assignment.
2036 __ sw(v0, location);
2037 if (var->IsContextSlot()) {
2038 __ mov(a3, v0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002039 int offset = Context::SlotOffset(var->index());
2040 __ RecordWriteContextSlot(
2041 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002042 }
2043 } else {
2044 ASSERT(var->IsLookupSlot());
2045 __ push(v0); // Value.
2046 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002047 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002048 __ Push(cp, a1, a0); // Context, name, strict mode.
2049 __ CallRuntime(Runtime::kStoreContextSlot, 4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002050 }
2051 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002052 // Non-initializing assignments to consts are ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +00002053}
2054
2055
2056void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002057 // Assignment to a property, using a named store IC.
2058 Property* prop = expr->target()->AsProperty();
2059 ASSERT(prop != NULL);
2060 ASSERT(prop->key()->AsLiteral() != NULL);
2061
2062 // If the assignment starts a block of assignments to the same object,
2063 // change to slow case to avoid the quadratic behavior of repeatedly
2064 // adding fast properties.
2065 if (expr->starts_initialization_block()) {
2066 __ push(result_register());
2067 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
2068 __ push(t0);
2069 __ CallRuntime(Runtime::kToSlowProperties, 1);
2070 __ pop(result_register());
2071 }
2072
2073 // Record source code position before IC call.
2074 SetSourcePosition(expr->position());
2075 __ mov(a0, result_register()); // Load the value.
2076 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
2077 // Load receiver to a1. Leave a copy in the stack if needed for turning the
2078 // receiver into fast case.
2079 if (expr->ends_initialization_block()) {
2080 __ lw(a1, MemOperand(sp));
2081 } else {
2082 __ pop(a1);
2083 }
2084
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002085 Handle<Code> ic = is_classic_mode()
2086 ? isolate()->builtins()->StoreIC_Initialize()
2087 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002088 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002089
2090 // If the assignment ends an initialization block, revert to fast case.
2091 if (expr->ends_initialization_block()) {
2092 __ push(v0); // Result of assignment, saved even if not needed.
2093 // Receiver is under the result value.
2094 __ lw(t0, MemOperand(sp, kPointerSize));
2095 __ push(t0);
2096 __ CallRuntime(Runtime::kToFastProperties, 1);
2097 __ pop(v0);
2098 __ Drop(1);
2099 }
2100 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2101 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002102}
2103
2104
2105void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002106 // Assignment to a property, using a keyed store IC.
2107
2108 // If the assignment starts a block of assignments to the same object,
2109 // change to slow case to avoid the quadratic behavior of repeatedly
2110 // adding fast properties.
2111 if (expr->starts_initialization_block()) {
2112 __ push(result_register());
2113 // Receiver is now under the key and value.
2114 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2115 __ push(t0);
2116 __ CallRuntime(Runtime::kToSlowProperties, 1);
2117 __ pop(result_register());
2118 }
2119
2120 // Record source code position before IC call.
2121 SetSourcePosition(expr->position());
2122 // Call keyed store IC.
2123 // The arguments are:
2124 // - a0 is the value,
2125 // - a1 is the key,
2126 // - a2 is the receiver.
2127 __ mov(a0, result_register());
2128 __ pop(a1); // Key.
2129 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2130 // receiver into fast case.
2131 if (expr->ends_initialization_block()) {
2132 __ lw(a2, MemOperand(sp));
2133 } else {
2134 __ pop(a2);
2135 }
2136
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002137 Handle<Code> ic = is_classic_mode()
2138 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2139 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002140 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002141
2142 // If the assignment ends an initialization block, revert to fast case.
2143 if (expr->ends_initialization_block()) {
2144 __ push(v0); // Result of assignment, saved even if not needed.
2145 // Receiver is under the result value.
2146 __ lw(t0, MemOperand(sp, kPointerSize));
2147 __ push(t0);
2148 __ CallRuntime(Runtime::kToFastProperties, 1);
2149 __ pop(v0);
2150 __ Drop(1);
2151 }
2152 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2153 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002154}
2155
2156
2157void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002158 Comment cmnt(masm_, "[ Property");
2159 Expression* key = expr->key();
2160
2161 if (key->IsPropertyName()) {
2162 VisitForAccumulatorValue(expr->obj());
2163 EmitNamedPropertyLoad(expr);
2164 context()->Plug(v0);
2165 } else {
2166 VisitForStackValue(expr->obj());
2167 VisitForAccumulatorValue(expr->key());
2168 __ pop(a1);
2169 EmitKeyedPropertyLoad(expr);
2170 context()->Plug(v0);
2171 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002172}
2173
lrn@chromium.org7516f052011-03-30 08:52:27 +00002174
ager@chromium.org5c838252010-02-19 08:53:10 +00002175void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002176 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002177 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002178 // Code common for calls using the IC.
2179 ZoneList<Expression*>* args = expr->arguments();
2180 int arg_count = args->length();
2181 { PreservePositionScope scope(masm()->positions_recorder());
2182 for (int i = 0; i < arg_count; i++) {
2183 VisitForStackValue(args->at(i));
2184 }
2185 __ li(a2, Operand(name));
2186 }
2187 // Record source position for debugger.
2188 SetSourcePosition(expr->position());
2189 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002190 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002191 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002192 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002193 RecordJSReturnSite(expr);
2194 // Restore context register.
2195 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2196 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002197}
2198
2199
lrn@chromium.org7516f052011-03-30 08:52:27 +00002200void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002201 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002202 // Load the key.
2203 VisitForAccumulatorValue(key);
2204
2205 // Swap the name of the function and the receiver on the stack to follow
2206 // the calling convention for call ICs.
2207 __ pop(a1);
2208 __ push(v0);
2209 __ push(a1);
2210
2211 // Code common for calls using the IC.
2212 ZoneList<Expression*>* args = expr->arguments();
2213 int arg_count = args->length();
2214 { PreservePositionScope scope(masm()->positions_recorder());
2215 for (int i = 0; i < arg_count; i++) {
2216 VisitForStackValue(args->at(i));
2217 }
2218 }
2219 // Record source position for debugger.
2220 SetSourcePosition(expr->position());
2221 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002222 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002223 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002224 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002225 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002226 RecordJSReturnSite(expr);
2227 // Restore context register.
2228 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2229 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002230}
2231
2232
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002233void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002234 // Code common for calls using the call stub.
2235 ZoneList<Expression*>* args = expr->arguments();
2236 int arg_count = args->length();
2237 { PreservePositionScope scope(masm()->positions_recorder());
2238 for (int i = 0; i < arg_count; i++) {
2239 VisitForStackValue(args->at(i));
2240 }
2241 }
2242 // Record source position for debugger.
2243 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002244 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002245 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002246 __ CallStub(&stub);
2247 RecordJSReturnSite(expr);
2248 // Restore context register.
2249 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2250 context()->DropAndPlug(1, v0);
2251}
2252
2253
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002254void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002255 // Push copy of the first argument or undefined if it doesn't exist.
2256 if (arg_count > 0) {
2257 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2258 } else {
2259 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2260 }
2261 __ push(a1);
2262
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002263 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002264 int receiver_offset = 2 + info_->scope()->num_parameters();
2265 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002266 __ push(a1);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002267 // Push the language mode.
2268 __ li(a1, Operand(Smi::FromInt(language_mode())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002269 __ push(a1);
2270
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002271 // Push the start position of the scope the calls resides in.
2272 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
2273 __ push(a1);
2274
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002275 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002276 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org5c838252010-02-19 08:53:10 +00002277}
2278
2279
2280void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002281#ifdef DEBUG
2282 // We want to verify that RecordJSReturnSite gets called on all paths
2283 // through this function. Avoid early returns.
2284 expr->return_is_recorded_ = false;
2285#endif
2286
2287 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002288 Expression* callee = expr->expression();
2289 VariableProxy* proxy = callee->AsVariableProxy();
2290 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002291
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002292 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002293 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2294 // resolve the function we need to call and the receiver of the
2295 // call. Then we call the resolved function using the given
2296 // arguments.
2297 ZoneList<Expression*>* args = expr->arguments();
2298 int arg_count = args->length();
2299
2300 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002301 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002302 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2303 __ push(a2); // Reserved receiver slot.
2304
2305 // Push the arguments.
2306 for (int i = 0; i < arg_count; i++) {
2307 VisitForStackValue(args->at(i));
2308 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002309
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002310 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002311 // resolve eval.
2312 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2313 __ push(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002314 EmitResolvePossiblyDirectEval(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002315
2316 // The runtime call returns a pair of values in v0 (function) and
2317 // v1 (receiver). Touch up the stack with the right values.
2318 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2319 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2320 }
2321 // Record source position for debugger.
2322 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002323 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002324 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002325 __ CallStub(&stub);
2326 RecordJSReturnSite(expr);
2327 // Restore context register.
2328 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2329 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002330 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002331 // Push global object as receiver for the call IC.
2332 __ lw(a0, GlobalObjectOperand());
2333 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002334 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2335 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002336 // Call to a lookup slot (dynamically introduced variable).
2337 Label slow, done;
2338
2339 { PreservePositionScope scope(masm()->positions_recorder());
2340 // Generate code for loading from variables potentially shadowed
2341 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002342 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002343 }
2344
2345 __ bind(&slow);
2346 // Call the runtime to find the function to call (returned in v0)
2347 // and the object holding it (returned in v1).
2348 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002349 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002350 __ push(a2);
2351 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2352 __ Push(v0, v1); // Function, receiver.
2353
2354 // If fast case code has been generated, emit code to push the
2355 // function and receiver and have the slow path jump around this
2356 // code.
2357 if (done.is_linked()) {
2358 Label call;
2359 __ Branch(&call);
2360 __ bind(&done);
2361 // Push function.
2362 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002363 // The receiver is implicitly the global receiver. Indicate this
2364 // by passing the hole to the call function stub.
2365 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002366 __ push(a1);
2367 __ bind(&call);
2368 }
2369
danno@chromium.org40cb8782011-05-25 07:58:50 +00002370 // The receiver is either the global receiver or an object found
2371 // by LoadContextSlot. That object could be the hole if the
2372 // receiver is implicitly the global object.
2373 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002374 } else if (property != NULL) {
2375 { PreservePositionScope scope(masm()->positions_recorder());
2376 VisitForStackValue(property->obj());
2377 }
2378 if (property->key()->IsPropertyName()) {
2379 EmitCallWithIC(expr,
2380 property->key()->AsLiteral()->handle(),
2381 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002382 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002383 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002384 }
2385 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002386 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002387 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002388 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002389 }
2390 // Load global receiver object.
2391 __ lw(a1, GlobalObjectOperand());
2392 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2393 __ push(a1);
2394 // Emit function call.
2395 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2396 }
2397
2398#ifdef DEBUG
2399 // RecordJSReturnSite should have been called.
2400 ASSERT(expr->return_is_recorded_);
2401#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002402}
2403
2404
2405void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002406 Comment cmnt(masm_, "[ CallNew");
2407 // According to ECMA-262, section 11.2.2, page 44, the function
2408 // expression in new calls must be evaluated before the
2409 // arguments.
2410
2411 // Push constructor on the stack. If it's not a function it's used as
2412 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2413 // ignored.
2414 VisitForStackValue(expr->expression());
2415
2416 // Push the arguments ("left-to-right") on the stack.
2417 ZoneList<Expression*>* args = expr->arguments();
2418 int arg_count = args->length();
2419 for (int i = 0; i < arg_count; i++) {
2420 VisitForStackValue(args->at(i));
2421 }
2422
2423 // Call the construct call builtin that handles allocation and
2424 // constructor invocation.
2425 SetSourcePosition(expr->position());
2426
2427 // Load function and argument count into a1 and a0.
2428 __ li(a0, Operand(arg_count));
2429 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2430
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002431 // Record call targets in unoptimized code, but not in the snapshot.
2432 CallFunctionFlags flags;
2433 if (!Serializer::enabled()) {
2434 flags = RECORD_CALL_TARGET;
2435 Handle<Object> uninitialized =
2436 TypeFeedbackCells::UninitializedSentinel(isolate());
2437 Handle<JSGlobalPropertyCell> cell =
2438 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
2439 RecordTypeFeedbackCell(expr->id(), cell);
2440 __ li(a2, Operand(cell));
2441 } else {
2442 flags = NO_CALL_FUNCTION_FLAGS;
2443 }
2444
2445 CallConstructStub stub(flags);
2446 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
ulan@chromium.org967e2702012-02-28 09:49:15 +00002447 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002448 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002449}
2450
2451
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002452void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2453 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002454 ASSERT(args->length() == 1);
2455
2456 VisitForAccumulatorValue(args->at(0));
2457
2458 Label materialize_true, materialize_false;
2459 Label* if_true = NULL;
2460 Label* if_false = NULL;
2461 Label* fall_through = NULL;
2462 context()->PrepareTest(&materialize_true, &materialize_false,
2463 &if_true, &if_false, &fall_through);
2464
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002465 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002466 __ And(t0, v0, Operand(kSmiTagMask));
2467 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2468
2469 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002470}
2471
2472
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002473void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2474 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002475 ASSERT(args->length() == 1);
2476
2477 VisitForAccumulatorValue(args->at(0));
2478
2479 Label materialize_true, materialize_false;
2480 Label* if_true = NULL;
2481 Label* if_false = NULL;
2482 Label* fall_through = NULL;
2483 context()->PrepareTest(&materialize_true, &materialize_false,
2484 &if_true, &if_false, &fall_through);
2485
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002486 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002487 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2488 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2489
2490 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002491}
2492
2493
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002494void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2495 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002496 ASSERT(args->length() == 1);
2497
2498 VisitForAccumulatorValue(args->at(0));
2499
2500 Label materialize_true, materialize_false;
2501 Label* if_true = NULL;
2502 Label* if_false = NULL;
2503 Label* fall_through = NULL;
2504 context()->PrepareTest(&materialize_true, &materialize_false,
2505 &if_true, &if_false, &fall_through);
2506
2507 __ JumpIfSmi(v0, if_false);
2508 __ LoadRoot(at, Heap::kNullValueRootIndex);
2509 __ Branch(if_true, eq, v0, Operand(at));
2510 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2511 // Undetectable objects behave like undefined when tested with typeof.
2512 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2513 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2514 __ Branch(if_false, ne, at, Operand(zero_reg));
2515 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002516 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002517 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002518 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2519 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002520
2521 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002522}
2523
2524
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002525void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2526 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002527 ASSERT(args->length() == 1);
2528
2529 VisitForAccumulatorValue(args->at(0));
2530
2531 Label materialize_true, materialize_false;
2532 Label* if_true = NULL;
2533 Label* if_false = NULL;
2534 Label* fall_through = NULL;
2535 context()->PrepareTest(&materialize_true, &materialize_false,
2536 &if_true, &if_false, &fall_through);
2537
2538 __ JumpIfSmi(v0, if_false);
2539 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002540 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002541 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002542 if_true, if_false, fall_through);
2543
2544 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002545}
2546
2547
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002548void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2549 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002550 ASSERT(args->length() == 1);
2551
2552 VisitForAccumulatorValue(args->at(0));
2553
2554 Label materialize_true, materialize_false;
2555 Label* if_true = NULL;
2556 Label* if_false = NULL;
2557 Label* fall_through = NULL;
2558 context()->PrepareTest(&materialize_true, &materialize_false,
2559 &if_true, &if_false, &fall_through);
2560
2561 __ JumpIfSmi(v0, if_false);
2562 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2563 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2564 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002565 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002566 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2567
2568 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002569}
2570
2571
2572void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002573 CallRuntime* expr) {
2574 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002575 ASSERT(args->length() == 1);
2576
2577 VisitForAccumulatorValue(args->at(0));
2578
2579 Label materialize_true, materialize_false;
2580 Label* if_true = NULL;
2581 Label* if_false = NULL;
2582 Label* fall_through = NULL;
2583 context()->PrepareTest(&materialize_true, &materialize_false,
2584 &if_true, &if_false, &fall_through);
2585
2586 if (FLAG_debug_code) __ AbortIfSmi(v0);
2587
2588 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2589 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2590 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2591 __ Branch(if_true, ne, t0, Operand(zero_reg));
2592
2593 // Check for fast case object. Generate false result for slow case object.
2594 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2595 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2596 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2597 __ Branch(if_false, eq, a2, Operand(t0));
2598
2599 // Look for valueOf symbol in the descriptor array, and indicate false if
2600 // found. The type is not checked, so if it is a transition it is a false
2601 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002602 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002603 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2604 // t0: descriptor array
2605 // a3: length of descriptor array
2606 // Calculate the end of the descriptor array.
2607 STATIC_ASSERT(kSmiTag == 0);
2608 STATIC_ASSERT(kSmiTagSize == 1);
2609 STATIC_ASSERT(kPointerSize == 4);
2610 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2611 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2612 __ Addu(a2, a2, t1);
2613
2614 // Calculate location of the first key name.
2615 __ Addu(t0,
2616 t0,
2617 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2618 DescriptorArray::kFirstIndex * kPointerSize));
2619 // Loop through all the keys in the descriptor array. If one of these is the
2620 // symbol valueOf the result is false.
2621 Label entry, loop;
2622 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2623 // used in the loop below.
2624 __ li(t2, Operand(FACTORY->value_of_symbol()));
2625 __ jmp(&entry);
2626 __ bind(&loop);
2627 __ lw(a3, MemOperand(t0, 0));
2628 __ Branch(if_false, eq, a3, Operand(t2));
2629 __ Addu(t0, t0, Operand(kPointerSize));
2630 __ bind(&entry);
2631 __ Branch(&loop, ne, t0, Operand(a2));
2632
2633 // If a valueOf property is not found on the object check that it's
2634 // prototype is the un-modified String prototype. If not result is false.
2635 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2636 __ JumpIfSmi(a2, if_false);
2637 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2638 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2639 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2640 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2641 __ Branch(if_false, ne, a2, Operand(a3));
2642
2643 // Set the bit in the map to indicate that it has been checked safe for
2644 // default valueOf and set true result.
2645 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2646 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2647 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2648 __ jmp(if_true);
2649
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002650 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002651 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002652}
2653
2654
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002655void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2656 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002657 ASSERT(args->length() == 1);
2658
2659 VisitForAccumulatorValue(args->at(0));
2660
2661 Label materialize_true, materialize_false;
2662 Label* if_true = NULL;
2663 Label* if_false = NULL;
2664 Label* fall_through = NULL;
2665 context()->PrepareTest(&materialize_true, &materialize_false,
2666 &if_true, &if_false, &fall_through);
2667
2668 __ JumpIfSmi(v0, if_false);
2669 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002670 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002671 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2672 __ Branch(if_false);
2673
2674 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002675}
2676
2677
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002678void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2679 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002680 ASSERT(args->length() == 1);
2681
2682 VisitForAccumulatorValue(args->at(0));
2683
2684 Label materialize_true, materialize_false;
2685 Label* if_true = NULL;
2686 Label* if_false = NULL;
2687 Label* fall_through = NULL;
2688 context()->PrepareTest(&materialize_true, &materialize_false,
2689 &if_true, &if_false, &fall_through);
2690
2691 __ JumpIfSmi(v0, if_false);
2692 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002693 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002694 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2695 if_true, if_false, fall_through);
2696
2697 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002698}
2699
2700
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002701void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2702 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002703 ASSERT(args->length() == 1);
2704
2705 VisitForAccumulatorValue(args->at(0));
2706
2707 Label materialize_true, materialize_false;
2708 Label* if_true = NULL;
2709 Label* if_false = NULL;
2710 Label* fall_through = NULL;
2711 context()->PrepareTest(&materialize_true, &materialize_false,
2712 &if_true, &if_false, &fall_through);
2713
2714 __ JumpIfSmi(v0, if_false);
2715 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002716 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002717 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2718
2719 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002720}
2721
2722
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002723void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2724 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002725
2726 Label materialize_true, materialize_false;
2727 Label* if_true = NULL;
2728 Label* if_false = NULL;
2729 Label* fall_through = NULL;
2730 context()->PrepareTest(&materialize_true, &materialize_false,
2731 &if_true, &if_false, &fall_through);
2732
2733 // Get the frame pointer for the calling frame.
2734 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2735
2736 // Skip the arguments adaptor frame if it exists.
2737 Label check_frame_marker;
2738 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2739 __ Branch(&check_frame_marker, ne,
2740 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2741 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2742
2743 // Check the marker in the calling frame.
2744 __ bind(&check_frame_marker);
2745 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002746 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002747 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2748 if_true, if_false, fall_through);
2749
2750 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002751}
2752
2753
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002754void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2755 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002756 ASSERT(args->length() == 2);
2757
2758 // Load the two objects into registers and perform the comparison.
2759 VisitForStackValue(args->at(0));
2760 VisitForAccumulatorValue(args->at(1));
2761
2762 Label materialize_true, materialize_false;
2763 Label* if_true = NULL;
2764 Label* if_false = NULL;
2765 Label* fall_through = NULL;
2766 context()->PrepareTest(&materialize_true, &materialize_false,
2767 &if_true, &if_false, &fall_through);
2768
2769 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002770 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002771 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2772
2773 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002774}
2775
2776
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002777void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2778 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002779 ASSERT(args->length() == 1);
2780
2781 // ArgumentsAccessStub expects the key in a1 and the formal
2782 // parameter count in a0.
2783 VisitForAccumulatorValue(args->at(0));
2784 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002785 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002786 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2787 __ CallStub(&stub);
2788 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002789}
2790
2791
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002792void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2793 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002794 Label exit;
2795 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002796 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002797
2798 // Check if the calling frame is an arguments adaptor frame.
2799 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2800 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2801 __ Branch(&exit, ne, a3,
2802 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2803
2804 // Arguments adaptor case: Read the arguments length from the
2805 // adaptor frame.
2806 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2807
2808 __ bind(&exit);
2809 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002810}
2811
2812
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002813void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2814 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002815 ASSERT(args->length() == 1);
2816 Label done, null, function, non_function_constructor;
2817
2818 VisitForAccumulatorValue(args->at(0));
2819
2820 // If the object is a smi, we return null.
2821 __ JumpIfSmi(v0, &null);
2822
2823 // Check that the object is a JS object but take special care of JS
2824 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002825 // Assume that there are only two callable types, and one of them is at
2826 // either end of the type range for JS object types. Saves extra comparisons.
2827 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002828 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002829 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002830
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002831 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2832 FIRST_SPEC_OBJECT_TYPE + 1);
2833 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002834
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002835 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2836 LAST_SPEC_OBJECT_TYPE - 1);
2837 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2838 // Assume that there is no larger type.
2839 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2840
2841 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002842 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2843 __ GetObjectType(v0, a1, a1);
2844 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2845
2846 // v0 now contains the constructor function. Grab the
2847 // instance class name from there.
2848 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2849 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2850 __ Branch(&done);
2851
2852 // Functions have class 'Function'.
2853 __ bind(&function);
2854 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2855 __ jmp(&done);
2856
2857 // Objects with a non-function constructor have class 'Object'.
2858 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002859 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002860 __ jmp(&done);
2861
2862 // Non-JS objects have class null.
2863 __ bind(&null);
2864 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2865
2866 // All done.
2867 __ bind(&done);
2868
2869 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002870}
2871
2872
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002873void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002874 // Conditionally generate a log call.
2875 // Args:
2876 // 0 (literal string): The type of logging (corresponds to the flags).
2877 // This is used to determine whether or not to generate the log call.
2878 // 1 (string): Format string. Access the string at argument index 2
2879 // with '%2s' (see Logger::LogRuntime for all the formats).
2880 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002881 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002882 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002883 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2884 VisitForStackValue(args->at(1));
2885 VisitForStackValue(args->at(2));
2886 __ CallRuntime(Runtime::kLog, 2);
2887 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002888
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002889 // Finally, we're expected to leave a value on the top of the stack.
2890 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2891 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002892}
2893
2894
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002895void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
2896 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002897 Label slow_allocate_heapnumber;
2898 Label heapnumber_allocated;
2899
2900 // Save the new heap number in callee-saved register s0, since
2901 // we call out to external C code below.
2902 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2903 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2904 __ jmp(&heapnumber_allocated);
2905
2906 __ bind(&slow_allocate_heapnumber);
2907
2908 // Allocate a heap number.
2909 __ CallRuntime(Runtime::kNumberAlloc, 0);
2910 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2911
2912 __ bind(&heapnumber_allocated);
2913
2914 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2915 // by computing:
2916 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2917 if (CpuFeatures::IsSupported(FPU)) {
2918 __ PrepareCallCFunction(1, a0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002919 __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
2920 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002921 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2922
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002923 CpuFeatures::Scope scope(FPU);
2924 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2925 __ li(a1, Operand(0x41300000));
2926 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002927 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002928 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002929 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002930 // Subtract and store the result in the heap number.
2931 __ sub_d(f0, f12, f14);
2932 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2933 __ mov(v0, s0);
2934 } else {
2935 __ PrepareCallCFunction(2, a0);
2936 __ mov(a0, s0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002937 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
2938 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002939 __ CallCFunction(
2940 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2941 }
2942
2943 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002944}
2945
2946
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002947void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002948 // Load the arguments on the stack and call the stub.
2949 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002950 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002951 ASSERT(args->length() == 3);
2952 VisitForStackValue(args->at(0));
2953 VisitForStackValue(args->at(1));
2954 VisitForStackValue(args->at(2));
2955 __ CallStub(&stub);
2956 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002957}
2958
2959
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002960void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002961 // Load the arguments on the stack and call the stub.
2962 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002963 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002964 ASSERT(args->length() == 4);
2965 VisitForStackValue(args->at(0));
2966 VisitForStackValue(args->at(1));
2967 VisitForStackValue(args->at(2));
2968 VisitForStackValue(args->at(3));
2969 __ CallStub(&stub);
2970 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002971}
2972
2973
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002974void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
2975 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002976 ASSERT(args->length() == 1);
2977
2978 VisitForAccumulatorValue(args->at(0)); // Load the object.
2979
2980 Label done;
2981 // If the object is a smi return the object.
2982 __ JumpIfSmi(v0, &done);
2983 // If the object is not a value type, return the object.
2984 __ GetObjectType(v0, a1, a1);
2985 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2986
2987 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2988
2989 __ bind(&done);
2990 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002991}
2992
2993
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002994void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002995 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002996 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002997 ASSERT(args->length() == 2);
2998 VisitForStackValue(args->at(0));
2999 VisitForStackValue(args->at(1));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003000 if (CpuFeatures::IsSupported(FPU)) {
3001 MathPowStub stub(MathPowStub::ON_STACK);
3002 __ CallStub(&stub);
3003 } else {
3004 __ CallRuntime(Runtime::kMath_pow, 2);
3005 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003006 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003007}
3008
3009
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003010void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3011 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003012 ASSERT(args->length() == 2);
3013
3014 VisitForStackValue(args->at(0)); // Load the object.
3015 VisitForAccumulatorValue(args->at(1)); // Load the value.
3016 __ pop(a1); // v0 = value. a1 = object.
3017
3018 Label done;
3019 // If the object is a smi, return the value.
3020 __ JumpIfSmi(a1, &done);
3021
3022 // If the object is not a value type, return the value.
3023 __ GetObjectType(a1, a2, a2);
3024 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
3025
3026 // Store the value.
3027 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
3028 // Update the write barrier. Save the value as it will be
3029 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003030 __ mov(a2, v0);
3031 __ RecordWriteField(
3032 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003033
3034 __ bind(&done);
3035 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003036}
3037
3038
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003039void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3040 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003041 ASSERT_EQ(args->length(), 1);
3042
3043 // Load the argument on the stack and call the stub.
3044 VisitForStackValue(args->at(0));
3045
3046 NumberToStringStub stub;
3047 __ CallStub(&stub);
3048 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003049}
3050
3051
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003052void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3053 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003054 ASSERT(args->length() == 1);
3055
3056 VisitForAccumulatorValue(args->at(0));
3057
3058 Label done;
3059 StringCharFromCodeGenerator generator(v0, a1);
3060 generator.GenerateFast(masm_);
3061 __ jmp(&done);
3062
3063 NopRuntimeCallHelper call_helper;
3064 generator.GenerateSlow(masm_, call_helper);
3065
3066 __ bind(&done);
3067 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003068}
3069
3070
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003071void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3072 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003073 ASSERT(args->length() == 2);
3074
3075 VisitForStackValue(args->at(0));
3076 VisitForAccumulatorValue(args->at(1));
3077 __ mov(a0, result_register());
3078
3079 Register object = a1;
3080 Register index = a0;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003081 Register result = v0;
3082
3083 __ pop(object);
3084
3085 Label need_conversion;
3086 Label index_out_of_range;
3087 Label done;
3088 StringCharCodeAtGenerator generator(object,
3089 index,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003090 result,
3091 &need_conversion,
3092 &need_conversion,
3093 &index_out_of_range,
3094 STRING_INDEX_IS_NUMBER);
3095 generator.GenerateFast(masm_);
3096 __ jmp(&done);
3097
3098 __ bind(&index_out_of_range);
3099 // When the index is out of range, the spec requires us to return
3100 // NaN.
3101 __ LoadRoot(result, Heap::kNanValueRootIndex);
3102 __ jmp(&done);
3103
3104 __ bind(&need_conversion);
3105 // Load the undefined value into the result register, which will
3106 // trigger conversion.
3107 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3108 __ jmp(&done);
3109
3110 NopRuntimeCallHelper call_helper;
3111 generator.GenerateSlow(masm_, call_helper);
3112
3113 __ bind(&done);
3114 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003115}
3116
3117
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003118void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3119 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003120 ASSERT(args->length() == 2);
3121
3122 VisitForStackValue(args->at(0));
3123 VisitForAccumulatorValue(args->at(1));
3124 __ mov(a0, result_register());
3125
3126 Register object = a1;
3127 Register index = a0;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003128 Register scratch = a3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003129 Register result = v0;
3130
3131 __ pop(object);
3132
3133 Label need_conversion;
3134 Label index_out_of_range;
3135 Label done;
3136 StringCharAtGenerator generator(object,
3137 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003138 scratch,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003139 result,
3140 &need_conversion,
3141 &need_conversion,
3142 &index_out_of_range,
3143 STRING_INDEX_IS_NUMBER);
3144 generator.GenerateFast(masm_);
3145 __ jmp(&done);
3146
3147 __ bind(&index_out_of_range);
3148 // When the index is out of range, the spec requires us to return
3149 // the empty string.
3150 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3151 __ jmp(&done);
3152
3153 __ bind(&need_conversion);
3154 // Move smi zero into the result register, which will trigger
3155 // conversion.
3156 __ li(result, Operand(Smi::FromInt(0)));
3157 __ jmp(&done);
3158
3159 NopRuntimeCallHelper call_helper;
3160 generator.GenerateSlow(masm_, call_helper);
3161
3162 __ bind(&done);
3163 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003164}
3165
3166
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003167void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3168 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003169 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003170 VisitForStackValue(args->at(0));
3171 VisitForStackValue(args->at(1));
3172
3173 StringAddStub stub(NO_STRING_ADD_FLAGS);
3174 __ CallStub(&stub);
3175 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003176}
3177
3178
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003179void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3180 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003181 ASSERT_EQ(2, args->length());
3182
3183 VisitForStackValue(args->at(0));
3184 VisitForStackValue(args->at(1));
3185
3186 StringCompareStub stub;
3187 __ CallStub(&stub);
3188 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003189}
3190
3191
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003192void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003193 // Load the argument on the stack and call the stub.
3194 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3195 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003196 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003197 ASSERT(args->length() == 1);
3198 VisitForStackValue(args->at(0));
3199 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3200 __ CallStub(&stub);
3201 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003202}
3203
3204
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003205void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003206 // Load the argument on the stack and call the stub.
3207 TranscendentalCacheStub stub(TranscendentalCache::COS,
3208 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003209 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003210 ASSERT(args->length() == 1);
3211 VisitForStackValue(args->at(0));
3212 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3213 __ CallStub(&stub);
3214 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003215}
3216
3217
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003218void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3219 // Load the argument on the stack and call the stub.
3220 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3221 TranscendentalCacheStub::TAGGED);
3222 ZoneList<Expression*>* args = expr->arguments();
3223 ASSERT(args->length() == 1);
3224 VisitForStackValue(args->at(0));
3225 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3226 __ CallStub(&stub);
3227 context()->Plug(v0);
3228}
3229
3230
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003231void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003232 // Load the argument on the stack and call the stub.
3233 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3234 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003235 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003236 ASSERT(args->length() == 1);
3237 VisitForStackValue(args->at(0));
3238 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3239 __ CallStub(&stub);
3240 context()->Plug(v0);
3241}
3242
3243
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003244void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003245 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003246 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003247 ASSERT(args->length() == 1);
3248 VisitForStackValue(args->at(0));
3249 __ CallRuntime(Runtime::kMath_sqrt, 1);
3250 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003251}
3252
3253
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003254void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3255 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003256 ASSERT(args->length() >= 2);
3257
3258 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3259 for (int i = 0; i < arg_count + 1; i++) {
3260 VisitForStackValue(args->at(i));
3261 }
3262 VisitForAccumulatorValue(args->last()); // Function.
3263
danno@chromium.orgc612e022011-11-10 11:38:15 +00003264 // Check for proxy.
3265 Label proxy, done;
3266 __ GetObjectType(v0, a1, a1);
3267 __ Branch(&proxy, eq, a1, Operand(JS_FUNCTION_PROXY_TYPE));
3268
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003269 // InvokeFunction requires the function in a1. Move it in there.
3270 __ mov(a1, result_register());
3271 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003272 __ InvokeFunction(a1, count, CALL_FUNCTION,
3273 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003274 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003275 __ jmp(&done);
3276
3277 __ bind(&proxy);
3278 __ push(v0);
3279 __ CallRuntime(Runtime::kCall, args->length());
3280 __ bind(&done);
3281
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003282 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003283}
3284
3285
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003286void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003287 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003288 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003289 ASSERT(args->length() == 3);
3290 VisitForStackValue(args->at(0));
3291 VisitForStackValue(args->at(1));
3292 VisitForStackValue(args->at(2));
3293 __ CallStub(&stub);
3294 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003295}
3296
3297
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003298void FullCodeGenerator::EmitSwapElements(CallRuntime* expr) {
3299 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003300 ASSERT(args->length() == 3);
3301 VisitForStackValue(args->at(0));
3302 VisitForStackValue(args->at(1));
3303 VisitForStackValue(args->at(2));
3304 Label done;
3305 Label slow_case;
3306 Register object = a0;
3307 Register index1 = a1;
3308 Register index2 = a2;
3309 Register elements = a3;
3310 Register scratch1 = t0;
3311 Register scratch2 = t1;
3312
3313 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3314 // Fetch the map and check if array is in fast case.
3315 // Check that object doesn't require security checks and
3316 // has no indexed interceptor.
3317 __ GetObjectType(object, scratch1, scratch2);
3318 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3319 // Map is now in scratch1.
3320
3321 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3322 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3323 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3324
3325 // Check the object's elements are in fast case and writable.
3326 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3327 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3328 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3329 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3330
3331 // Check that both indices are smis.
3332 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3333 __ lw(index2, MemOperand(sp, 0));
3334 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3335
3336 // Check that both indices are valid.
3337 Label not_hi;
3338 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3339 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3340 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3341 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3342 __ bind(&not_hi);
3343
3344 // Bring the address of the elements into index1 and index2.
3345 __ Addu(scratch1, elements,
3346 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3347 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3348 __ Addu(index1, scratch1, index1);
3349 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3350 __ Addu(index2, scratch1, index2);
3351
3352 // Swap elements.
3353 __ lw(scratch1, MemOperand(index1, 0));
3354 __ lw(scratch2, MemOperand(index2, 0));
3355 __ sw(scratch1, MemOperand(index2, 0));
3356 __ sw(scratch2, MemOperand(index1, 0));
3357
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003358 Label no_remembered_set;
3359 __ CheckPageFlag(elements,
3360 scratch1,
3361 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3362 ne,
3363 &no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003364 // Possible optimization: do a check that both values are Smis
3365 // (or them and test against Smi mask).
3366
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003367 // We are swapping two objects in an array and the incremental marker never
3368 // pauses in the middle of scanning a single object. Therefore the
3369 // incremental marker is not disturbed, so we don't need to call the
3370 // RecordWrite stub that notifies the incremental marker.
3371 __ RememberedSetHelper(elements,
3372 index1,
3373 scratch2,
3374 kDontSaveFPRegs,
3375 MacroAssembler::kFallThroughAtEnd);
3376 __ RememberedSetHelper(elements,
3377 index2,
3378 scratch2,
3379 kDontSaveFPRegs,
3380 MacroAssembler::kFallThroughAtEnd);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003381
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003382 __ bind(&no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003383 // We are done. Drop elements from the stack, and return undefined.
3384 __ Drop(3);
3385 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3386 __ jmp(&done);
3387
3388 __ bind(&slow_case);
3389 __ CallRuntime(Runtime::kSwapElements, 3);
3390
3391 __ bind(&done);
3392 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003393}
3394
3395
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003396void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3397 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003398 ASSERT_EQ(2, args->length());
3399
3400 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3401 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3402
3403 Handle<FixedArray> jsfunction_result_caches(
3404 isolate()->global_context()->jsfunction_result_caches());
3405 if (jsfunction_result_caches->length() <= cache_id) {
3406 __ Abort("Attempt to use undefined cache.");
3407 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3408 context()->Plug(v0);
3409 return;
3410 }
3411
3412 VisitForAccumulatorValue(args->at(1));
3413
3414 Register key = v0;
3415 Register cache = a1;
3416 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3417 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3418 __ lw(cache,
3419 ContextOperand(
3420 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3421 __ lw(cache,
3422 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3423
3424
3425 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003426 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003427 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3428 // a2 now holds finger offset as a smi.
3429 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3430 // a3 now points to the start of fixed array elements.
3431 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3432 __ addu(a3, a3, at);
3433 // a3 now points to key of indexed element of cache.
3434 __ lw(a2, MemOperand(a3));
3435 __ Branch(&not_found, ne, key, Operand(a2));
3436
3437 __ lw(v0, MemOperand(a3, kPointerSize));
3438 __ Branch(&done);
3439
3440 __ bind(&not_found);
3441 // Call runtime to perform the lookup.
3442 __ Push(cache, key);
3443 __ CallRuntime(Runtime::kGetFromCache, 2);
3444
3445 __ bind(&done);
3446 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003447}
3448
3449
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003450void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3451 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003452 ASSERT_EQ(2, args->length());
3453
3454 Register right = v0;
3455 Register left = a1;
3456 Register tmp = a2;
3457 Register tmp2 = a3;
3458
3459 VisitForStackValue(args->at(0));
3460 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3461 __ pop(left);
3462
3463 Label done, fail, ok;
3464 __ Branch(&ok, eq, left, Operand(right));
3465 // Fail if either is a non-HeapObject.
3466 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003467 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003468 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3469 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3470 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3471 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3472 __ Branch(&fail, ne, tmp, Operand(tmp2));
3473 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3474 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3475 __ Branch(&ok, eq, tmp, Operand(tmp2));
3476 __ bind(&fail);
3477 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3478 __ jmp(&done);
3479 __ bind(&ok);
3480 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3481 __ bind(&done);
3482
3483 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003484}
3485
3486
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003487void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3488 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003489 VisitForAccumulatorValue(args->at(0));
3490
3491 Label materialize_true, materialize_false;
3492 Label* if_true = NULL;
3493 Label* if_false = NULL;
3494 Label* fall_through = NULL;
3495 context()->PrepareTest(&materialize_true, &materialize_false,
3496 &if_true, &if_false, &fall_through);
3497
3498 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3499 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3500
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003501 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003502 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3503
3504 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003505}
3506
3507
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003508void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3509 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003510 ASSERT(args->length() == 1);
3511 VisitForAccumulatorValue(args->at(0));
3512
3513 if (FLAG_debug_code) {
3514 __ AbortIfNotString(v0);
3515 }
3516
3517 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3518 __ IndexFromHash(v0, v0);
3519
3520 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003521}
3522
3523
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003524void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003525 Label bailout, done, one_char_separator, long_separator,
3526 non_trivial_array, not_size_one_array, loop,
3527 empty_separator_loop, one_char_separator_loop,
3528 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003529 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003530 ASSERT(args->length() == 2);
3531 VisitForStackValue(args->at(1));
3532 VisitForAccumulatorValue(args->at(0));
3533
3534 // All aliases of the same register have disjoint lifetimes.
3535 Register array = v0;
3536 Register elements = no_reg; // Will be v0.
3537 Register result = no_reg; // Will be v0.
3538 Register separator = a1;
3539 Register array_length = a2;
3540 Register result_pos = no_reg; // Will be a2.
3541 Register string_length = a3;
3542 Register string = t0;
3543 Register element = t1;
3544 Register elements_end = t2;
3545 Register scratch1 = t3;
3546 Register scratch2 = t5;
3547 Register scratch3 = t4;
3548 Register scratch4 = v1;
3549
3550 // Separator operand is on the stack.
3551 __ pop(separator);
3552
3553 // Check that the array is a JSArray.
3554 __ JumpIfSmi(array, &bailout);
3555 __ GetObjectType(array, scratch1, scratch2);
3556 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3557
3558 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003559 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003560
3561 // If the array has length zero, return the empty string.
3562 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3563 __ SmiUntag(array_length);
3564 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3565 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3566 __ Branch(&done);
3567
3568 __ bind(&non_trivial_array);
3569
3570 // Get the FixedArray containing array's elements.
3571 elements = array;
3572 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3573 array = no_reg; // End of array's live range.
3574
3575 // Check that all array elements are sequential ASCII strings, and
3576 // accumulate the sum of their lengths, as a smi-encoded value.
3577 __ mov(string_length, zero_reg);
3578 __ Addu(element,
3579 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3580 __ sll(elements_end, array_length, kPointerSizeLog2);
3581 __ Addu(elements_end, element, elements_end);
3582 // Loop condition: while (element < elements_end).
3583 // Live values in registers:
3584 // elements: Fixed array of strings.
3585 // array_length: Length of the fixed array of strings (not smi)
3586 // separator: Separator string
3587 // string_length: Accumulated sum of string lengths (smi).
3588 // element: Current array element.
3589 // elements_end: Array end.
3590 if (FLAG_debug_code) {
3591 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3592 array_length, Operand(zero_reg));
3593 }
3594 __ bind(&loop);
3595 __ lw(string, MemOperand(element));
3596 __ Addu(element, element, kPointerSize);
3597 __ JumpIfSmi(string, &bailout);
3598 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3599 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3600 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3601 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3602 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3603 __ BranchOnOverflow(&bailout, scratch3);
3604 __ Branch(&loop, lt, element, Operand(elements_end));
3605
3606 // If array_length is 1, return elements[0], a string.
3607 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3608 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3609 __ Branch(&done);
3610
3611 __ bind(&not_size_one_array);
3612
3613 // Live values in registers:
3614 // separator: Separator string
3615 // array_length: Length of the array.
3616 // string_length: Sum of string lengths (smi).
3617 // elements: FixedArray of strings.
3618
3619 // Check that the separator is a flat ASCII string.
3620 __ JumpIfSmi(separator, &bailout);
3621 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3622 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3623 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3624
3625 // Add (separator length times array_length) - separator length to the
3626 // string_length to get the length of the result string. array_length is not
3627 // smi but the other values are, so the result is a smi.
3628 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3629 __ Subu(string_length, string_length, Operand(scratch1));
3630 __ Mult(array_length, scratch1);
3631 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3632 // zero.
3633 __ mfhi(scratch2);
3634 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3635 __ mflo(scratch2);
3636 __ And(scratch3, scratch2, Operand(0x80000000));
3637 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3638 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3639 __ BranchOnOverflow(&bailout, scratch3);
3640 __ SmiUntag(string_length);
3641
3642 // Get first element in the array to free up the elements register to be used
3643 // for the result.
3644 __ Addu(element,
3645 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3646 result = elements; // End of live range for elements.
3647 elements = no_reg;
3648 // Live values in registers:
3649 // element: First array element
3650 // separator: Separator string
3651 // string_length: Length of result string (not smi)
3652 // array_length: Length of the array.
3653 __ AllocateAsciiString(result,
3654 string_length,
3655 scratch1,
3656 scratch2,
3657 elements_end,
3658 &bailout);
3659 // Prepare for looping. Set up elements_end to end of the array. Set
3660 // result_pos to the position of the result where to write the first
3661 // character.
3662 __ sll(elements_end, array_length, kPointerSizeLog2);
3663 __ Addu(elements_end, element, elements_end);
3664 result_pos = array_length; // End of live range for array_length.
3665 array_length = no_reg;
3666 __ Addu(result_pos,
3667 result,
3668 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3669
3670 // Check the length of the separator.
3671 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3672 __ li(at, Operand(Smi::FromInt(1)));
3673 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3674 __ Branch(&long_separator, gt, scratch1, Operand(at));
3675
3676 // Empty separator case.
3677 __ bind(&empty_separator_loop);
3678 // Live values in registers:
3679 // result_pos: the position to which we are currently copying characters.
3680 // element: Current array element.
3681 // elements_end: Array end.
3682
3683 // Copy next array element to the result.
3684 __ lw(string, MemOperand(element));
3685 __ Addu(element, element, kPointerSize);
3686 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3687 __ SmiUntag(string_length);
3688 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3689 __ CopyBytes(string, result_pos, string_length, scratch1);
3690 // End while (element < elements_end).
3691 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3692 ASSERT(result.is(v0));
3693 __ Branch(&done);
3694
3695 // One-character separator case.
3696 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003697 // Replace separator with its ASCII character value.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003698 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3699 // Jump into the loop after the code that copies the separator, so the first
3700 // element is not preceded by a separator.
3701 __ jmp(&one_char_separator_loop_entry);
3702
3703 __ bind(&one_char_separator_loop);
3704 // Live values in registers:
3705 // result_pos: the position to which we are currently copying characters.
3706 // element: Current array element.
3707 // elements_end: Array end.
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003708 // separator: Single separator ASCII char (in lower byte).
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003709
3710 // Copy the separator character to the result.
3711 __ sb(separator, MemOperand(result_pos));
3712 __ Addu(result_pos, result_pos, 1);
3713
3714 // Copy next array element to the result.
3715 __ bind(&one_char_separator_loop_entry);
3716 __ lw(string, MemOperand(element));
3717 __ Addu(element, element, kPointerSize);
3718 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3719 __ SmiUntag(string_length);
3720 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3721 __ CopyBytes(string, result_pos, string_length, scratch1);
3722 // End while (element < elements_end).
3723 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3724 ASSERT(result.is(v0));
3725 __ Branch(&done);
3726
3727 // Long separator case (separator is more than one character). Entry is at the
3728 // label long_separator below.
3729 __ bind(&long_separator_loop);
3730 // Live values in registers:
3731 // result_pos: the position to which we are currently copying characters.
3732 // element: Current array element.
3733 // elements_end: Array end.
3734 // separator: Separator string.
3735
3736 // Copy the separator to the result.
3737 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3738 __ SmiUntag(string_length);
3739 __ Addu(string,
3740 separator,
3741 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3742 __ CopyBytes(string, result_pos, string_length, scratch1);
3743
3744 __ bind(&long_separator);
3745 __ lw(string, MemOperand(element));
3746 __ Addu(element, element, kPointerSize);
3747 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3748 __ SmiUntag(string_length);
3749 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3750 __ CopyBytes(string, result_pos, string_length, scratch1);
3751 // End while (element < elements_end).
3752 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3753 ASSERT(result.is(v0));
3754 __ Branch(&done);
3755
3756 __ bind(&bailout);
3757 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3758 __ bind(&done);
3759 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003760}
3761
3762
ager@chromium.org5c838252010-02-19 08:53:10 +00003763void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003764 Handle<String> name = expr->name();
3765 if (name->length() > 0 && name->Get(0) == '_') {
3766 Comment cmnt(masm_, "[ InlineRuntimeCall");
3767 EmitInlineRuntimeCall(expr);
3768 return;
3769 }
3770
3771 Comment cmnt(masm_, "[ CallRuntime");
3772 ZoneList<Expression*>* args = expr->arguments();
3773
3774 if (expr->is_jsruntime()) {
3775 // Prepare for calling JS runtime function.
3776 __ lw(a0, GlobalObjectOperand());
3777 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3778 __ push(a0);
3779 }
3780
3781 // Push the arguments ("left-to-right").
3782 int arg_count = args->length();
3783 for (int i = 0; i < arg_count; i++) {
3784 VisitForStackValue(args->at(i));
3785 }
3786
3787 if (expr->is_jsruntime()) {
3788 // Call the JS runtime function.
3789 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003790 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003791 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003792 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003793 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003794 // Restore context register.
3795 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3796 } else {
3797 // Call the C runtime function.
3798 __ CallRuntime(expr->function(), arg_count);
3799 }
3800 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003801}
3802
3803
3804void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003805 switch (expr->op()) {
3806 case Token::DELETE: {
3807 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003808 Property* property = expr->expression()->AsProperty();
3809 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003810
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003811 if (property != NULL) {
3812 VisitForStackValue(property->obj());
3813 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003814 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
3815 ? kNonStrictMode : kStrictMode;
3816 __ li(a1, Operand(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003817 __ push(a1);
3818 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3819 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003820 } else if (proxy != NULL) {
3821 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003822 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003823 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003824 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003825 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003826 __ lw(a2, GlobalObjectOperand());
3827 __ li(a1, Operand(var->name()));
3828 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3829 __ Push(a2, a1, a0);
3830 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3831 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003832 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003833 // Result of deleting non-global, non-dynamic variables is false.
3834 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003835 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003836 } else {
3837 // Non-global variable. Call the runtime to try to delete from the
3838 // context where the variable was introduced.
3839 __ push(context_register());
3840 __ li(a2, Operand(var->name()));
3841 __ push(a2);
3842 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3843 context()->Plug(v0);
3844 }
3845 } else {
3846 // Result of deleting non-property, non-variable reference is true.
3847 // The subexpression may have side effects.
3848 VisitForEffect(expr->expression());
3849 context()->Plug(true);
3850 }
3851 break;
3852 }
3853
3854 case Token::VOID: {
3855 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3856 VisitForEffect(expr->expression());
3857 context()->Plug(Heap::kUndefinedValueRootIndex);
3858 break;
3859 }
3860
3861 case Token::NOT: {
3862 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3863 if (context()->IsEffect()) {
3864 // Unary NOT has no side effects so it's only necessary to visit the
3865 // subexpression. Match the optimizing compiler by not branching.
3866 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003867 } else if (context()->IsTest()) {
3868 const TestContext* test = TestContext::cast(context());
3869 // The labels are swapped for the recursive call.
3870 VisitForControl(expr->expression(),
3871 test->false_label(),
3872 test->true_label(),
3873 test->fall_through());
3874 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003875 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003876 // We handle value contexts explicitly rather than simply visiting
3877 // for control and plugging the control flow into the context,
3878 // because we need to prepare a pair of extra administrative AST ids
3879 // for the optimizing compiler.
3880 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3881 Label materialize_true, materialize_false, done;
3882 VisitForControl(expr->expression(),
3883 &materialize_false,
3884 &materialize_true,
3885 &materialize_true);
3886 __ bind(&materialize_true);
3887 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3888 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3889 if (context()->IsStackValue()) __ push(v0);
3890 __ jmp(&done);
3891 __ bind(&materialize_false);
3892 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3893 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3894 if (context()->IsStackValue()) __ push(v0);
3895 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003896 }
3897 break;
3898 }
3899
3900 case Token::TYPEOF: {
3901 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3902 { StackValueContext context(this);
3903 VisitForTypeofValue(expr->expression());
3904 }
3905 __ CallRuntime(Runtime::kTypeof, 1);
3906 context()->Plug(v0);
3907 break;
3908 }
3909
3910 case Token::ADD: {
3911 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3912 VisitForAccumulatorValue(expr->expression());
3913 Label no_conversion;
3914 __ JumpIfSmi(result_register(), &no_conversion);
3915 __ mov(a0, result_register());
3916 ToNumberStub convert_stub;
3917 __ CallStub(&convert_stub);
3918 __ bind(&no_conversion);
3919 context()->Plug(result_register());
3920 break;
3921 }
3922
3923 case Token::SUB:
3924 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3925 break;
3926
3927 case Token::BIT_NOT:
3928 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3929 break;
3930
3931 default:
3932 UNREACHABLE();
3933 }
3934}
3935
3936
3937void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3938 const char* comment) {
3939 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3940 Comment cmt(masm_, comment);
3941 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3942 UnaryOverwriteMode overwrite =
3943 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003944 UnaryOpStub stub(expr->op(), overwrite);
3945 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003946 VisitForAccumulatorValue(expr->expression());
3947 SetSourcePosition(expr->position());
3948 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003949 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003950 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003951}
3952
3953
3954void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003955 Comment cmnt(masm_, "[ CountOperation");
3956 SetSourcePosition(expr->position());
3957
3958 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3959 // as the left-hand side.
3960 if (!expr->expression()->IsValidLeftHandSide()) {
3961 VisitForEffect(expr->expression());
3962 return;
3963 }
3964
3965 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003966 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003967 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3968 LhsKind assign_type = VARIABLE;
3969 Property* prop = expr->expression()->AsProperty();
3970 // In case of a property we use the uninitialized expression context
3971 // of the key to detect a named property.
3972 if (prop != NULL) {
3973 assign_type =
3974 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3975 }
3976
3977 // Evaluate expression and get value.
3978 if (assign_type == VARIABLE) {
3979 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3980 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003981 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003982 } else {
3983 // Reserve space for result of postfix operation.
3984 if (expr->is_postfix() && !context()->IsEffect()) {
3985 __ li(at, Operand(Smi::FromInt(0)));
3986 __ push(at);
3987 }
3988 if (assign_type == NAMED_PROPERTY) {
3989 // Put the object both on the stack and in the accumulator.
3990 VisitForAccumulatorValue(prop->obj());
3991 __ push(v0);
3992 EmitNamedPropertyLoad(prop);
3993 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003994 VisitForStackValue(prop->obj());
3995 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003996 __ lw(a1, MemOperand(sp, 0));
3997 __ push(v0);
3998 EmitKeyedPropertyLoad(prop);
3999 }
4000 }
4001
4002 // We need a second deoptimization point after loading the value
4003 // in case evaluating the property load my have a side effect.
4004 if (assign_type == VARIABLE) {
4005 PrepareForBailout(expr->expression(), TOS_REG);
4006 } else {
4007 PrepareForBailoutForId(expr->CountId(), TOS_REG);
4008 }
4009
4010 // Call ToNumber only if operand is not a smi.
4011 Label no_conversion;
4012 __ JumpIfSmi(v0, &no_conversion);
4013 __ mov(a0, v0);
4014 ToNumberStub convert_stub;
4015 __ CallStub(&convert_stub);
4016 __ bind(&no_conversion);
4017
4018 // Save result for postfix expressions.
4019 if (expr->is_postfix()) {
4020 if (!context()->IsEffect()) {
4021 // Save the result on the stack. If we have a named or keyed property
4022 // we store the result under the receiver that is currently on top
4023 // of the stack.
4024 switch (assign_type) {
4025 case VARIABLE:
4026 __ push(v0);
4027 break;
4028 case NAMED_PROPERTY:
4029 __ sw(v0, MemOperand(sp, kPointerSize));
4030 break;
4031 case KEYED_PROPERTY:
4032 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4033 break;
4034 }
4035 }
4036 }
4037 __ mov(a0, result_register());
4038
4039 // Inline smi case if we are in a loop.
4040 Label stub_call, done;
4041 JumpPatchSite patch_site(masm_);
4042
4043 int count_value = expr->op() == Token::INC ? 1 : -1;
4044 __ li(a1, Operand(Smi::FromInt(count_value)));
4045
4046 if (ShouldInlineSmiCase(expr->op())) {
4047 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
4048 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
4049
4050 // We could eliminate this smi check if we split the code at
4051 // the first smi check before calling ToNumber.
4052 patch_site.EmitJumpIfSmi(v0, &done);
4053 __ bind(&stub_call);
4054 }
4055
4056 // Record position before stub call.
4057 SetSourcePosition(expr->position());
4058
danno@chromium.org40cb8782011-05-25 07:58:50 +00004059 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004060 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004061 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004062 __ bind(&done);
4063
4064 // Store the value returned in v0.
4065 switch (assign_type) {
4066 case VARIABLE:
4067 if (expr->is_postfix()) {
4068 { EffectContext context(this);
4069 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4070 Token::ASSIGN);
4071 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4072 context.Plug(v0);
4073 }
4074 // For all contexts except EffectConstant we have the result on
4075 // top of the stack.
4076 if (!context()->IsEffect()) {
4077 context()->PlugTOS();
4078 }
4079 } else {
4080 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4081 Token::ASSIGN);
4082 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4083 context()->Plug(v0);
4084 }
4085 break;
4086 case NAMED_PROPERTY: {
4087 __ mov(a0, result_register()); // Value.
4088 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4089 __ pop(a1); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004090 Handle<Code> ic = is_classic_mode()
4091 ? isolate()->builtins()->StoreIC_Initialize()
4092 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004093 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004094 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4095 if (expr->is_postfix()) {
4096 if (!context()->IsEffect()) {
4097 context()->PlugTOS();
4098 }
4099 } else {
4100 context()->Plug(v0);
4101 }
4102 break;
4103 }
4104 case KEYED_PROPERTY: {
4105 __ mov(a0, result_register()); // Value.
4106 __ pop(a1); // Key.
4107 __ pop(a2); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004108 Handle<Code> ic = is_classic_mode()
4109 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4110 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004111 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004112 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4113 if (expr->is_postfix()) {
4114 if (!context()->IsEffect()) {
4115 context()->PlugTOS();
4116 }
4117 } else {
4118 context()->Plug(v0);
4119 }
4120 break;
4121 }
4122 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004123}
4124
4125
lrn@chromium.org7516f052011-03-30 08:52:27 +00004126void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004127 ASSERT(!context()->IsEffect());
4128 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004129 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004130 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004131 Comment cmnt(masm_, "Global variable");
4132 __ lw(a0, GlobalObjectOperand());
4133 __ li(a2, Operand(proxy->name()));
4134 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4135 // Use a regular load, not a contextual load, to avoid a reference
4136 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004137 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004138 PrepareForBailout(expr, TOS_REG);
4139 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004140 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004141 Label done, slow;
4142
4143 // Generate code for loading from variables potentially shadowed
4144 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004145 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004146
4147 __ bind(&slow);
4148 __ li(a0, Operand(proxy->name()));
4149 __ Push(cp, a0);
4150 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4151 PrepareForBailout(expr, TOS_REG);
4152 __ bind(&done);
4153
4154 context()->Plug(v0);
4155 } else {
4156 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004157 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004158 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004159}
4160
ager@chromium.org04921a82011-06-27 13:21:41 +00004161void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004162 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004163 Handle<String> check) {
4164 Label materialize_true, materialize_false;
4165 Label* if_true = NULL;
4166 Label* if_false = NULL;
4167 Label* fall_through = NULL;
4168 context()->PrepareTest(&materialize_true, &materialize_false,
4169 &if_true, &if_false, &fall_through);
4170
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004171 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004172 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004173 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004174 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004175
4176 if (check->Equals(isolate()->heap()->number_symbol())) {
4177 __ JumpIfSmi(v0, if_true);
4178 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4179 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4180 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4181 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4182 __ JumpIfSmi(v0, if_false);
4183 // Check for undetectable objects => false.
4184 __ GetObjectType(v0, v0, a1);
4185 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4186 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4187 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4188 Split(eq, a1, Operand(zero_reg),
4189 if_true, if_false, fall_through);
4190 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4191 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4192 __ Branch(if_true, eq, v0, Operand(at));
4193 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4194 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004195 } else if (FLAG_harmony_typeof &&
4196 check->Equals(isolate()->heap()->null_symbol())) {
4197 __ LoadRoot(at, Heap::kNullValueRootIndex);
4198 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004199 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4200 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4201 __ Branch(if_true, eq, v0, Operand(at));
4202 __ JumpIfSmi(v0, if_false);
4203 // Check for undetectable objects => true.
4204 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4205 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4206 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4207 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4208 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4209 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004210 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4211 __ GetObjectType(v0, v0, a1);
4212 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4213 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4214 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004215 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4216 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004217 if (!FLAG_harmony_typeof) {
4218 __ LoadRoot(at, Heap::kNullValueRootIndex);
4219 __ Branch(if_true, eq, v0, Operand(at));
4220 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004221 // Check for JS objects => true.
4222 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004223 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004224 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004225 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004226 // Check for undetectable objects => false.
4227 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4228 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4229 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4230 } else {
4231 if (if_false != fall_through) __ jmp(if_false);
4232 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004233 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004234}
4235
4236
ager@chromium.org5c838252010-02-19 08:53:10 +00004237void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004238 Comment cmnt(masm_, "[ CompareOperation");
4239 SetSourcePosition(expr->position());
4240
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004241 // First we try a fast inlined version of the compare when one of
4242 // the operands is a literal.
4243 if (TryLiteralCompare(expr)) return;
4244
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004245 // Always perform the comparison for its control flow. Pack the result
4246 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004247 Label materialize_true, materialize_false;
4248 Label* if_true = NULL;
4249 Label* if_false = NULL;
4250 Label* fall_through = NULL;
4251 context()->PrepareTest(&materialize_true, &materialize_false,
4252 &if_true, &if_false, &fall_through);
4253
ager@chromium.org04921a82011-06-27 13:21:41 +00004254 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004255 VisitForStackValue(expr->left());
4256 switch (op) {
4257 case Token::IN:
4258 VisitForStackValue(expr->right());
4259 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004260 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004261 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4262 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4263 break;
4264
4265 case Token::INSTANCEOF: {
4266 VisitForStackValue(expr->right());
4267 InstanceofStub stub(InstanceofStub::kNoFlags);
4268 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004269 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004270 // The stub returns 0 for true.
4271 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4272 break;
4273 }
4274
4275 default: {
4276 VisitForAccumulatorValue(expr->right());
4277 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004278 switch (op) {
4279 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004280 case Token::EQ:
4281 cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004282 break;
4283 case Token::LT:
4284 cc = lt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004285 break;
4286 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004287 cc = gt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004288 break;
4289 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004290 cc = le;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004291 break;
4292 case Token::GTE:
4293 cc = ge;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004294 break;
4295 case Token::IN:
4296 case Token::INSTANCEOF:
4297 default:
4298 UNREACHABLE();
4299 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004300 __ mov(a0, result_register());
4301 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004302
4303 bool inline_smi_code = ShouldInlineSmiCase(op);
4304 JumpPatchSite patch_site(masm_);
4305 if (inline_smi_code) {
4306 Label slow_case;
4307 __ Or(a2, a0, Operand(a1));
4308 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4309 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4310 __ bind(&slow_case);
4311 }
4312 // Record position and call the compare IC.
4313 SetSourcePosition(expr->position());
4314 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004315 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004316 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004317 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004318 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4319 }
4320 }
4321
4322 // Convert the result of the comparison into one expected for this
4323 // expression's context.
4324 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004325}
4326
4327
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004328void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4329 Expression* sub_expr,
4330 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004331 Label materialize_true, materialize_false;
4332 Label* if_true = NULL;
4333 Label* if_false = NULL;
4334 Label* fall_through = NULL;
4335 context()->PrepareTest(&materialize_true, &materialize_false,
4336 &if_true, &if_false, &fall_through);
4337
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004338 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004339 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004340 Heap::RootListIndex nil_value = nil == kNullValue ?
4341 Heap::kNullValueRootIndex :
4342 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004343 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004344 __ LoadRoot(a1, nil_value);
4345 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004346 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4347 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004348 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4349 Heap::kUndefinedValueRootIndex :
4350 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004351 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004352 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004353 __ Branch(if_true, eq, a0, Operand(a1));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004354 __ JumpIfSmi(a0, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004355 // It can be an undetectable object.
4356 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4357 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4358 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4359 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4360 }
4361 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004362}
4363
4364
ager@chromium.org5c838252010-02-19 08:53:10 +00004365void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004366 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4367 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004368}
4369
4370
lrn@chromium.org7516f052011-03-30 08:52:27 +00004371Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004372 return v0;
4373}
ager@chromium.org5c838252010-02-19 08:53:10 +00004374
4375
lrn@chromium.org7516f052011-03-30 08:52:27 +00004376Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004377 return cp;
4378}
4379
4380
ager@chromium.org5c838252010-02-19 08:53:10 +00004381void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004382 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4383 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004384}
4385
4386
4387void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004388 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004389}
4390
4391
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004392void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4393 Scope* declaration_scope = scope()->DeclarationScope();
4394 if (declaration_scope->is_global_scope()) {
4395 // Contexts nested in the global context have a canonical empty function
4396 // as their closure, not the anonymous closure containing the global
4397 // code. Pass a smi sentinel and let the runtime look up the empty
4398 // function.
4399 __ li(at, Operand(Smi::FromInt(0)));
4400 } else if (declaration_scope->is_eval_scope()) {
4401 // Contexts created by a call to eval have the same closure as the
4402 // context calling eval, not the anonymous closure containing the eval
4403 // code. Fetch it from the context.
4404 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4405 } else {
4406 ASSERT(declaration_scope->is_function_scope());
4407 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4408 }
4409 __ push(at);
4410}
4411
4412
ager@chromium.org5c838252010-02-19 08:53:10 +00004413// ----------------------------------------------------------------------------
4414// Non-local control flow support.
4415
4416void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004417 ASSERT(!result_register().is(a1));
4418 // Store result register while executing finally block.
4419 __ push(result_register());
4420 // Cook return address in link register to stack (smi encoded Code* delta).
4421 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4422 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004423 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004424 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4425 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004426}
4427
4428
4429void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004430 ASSERT(!result_register().is(a1));
4431 // Restore result register from stack.
4432 __ pop(a1);
4433 // Uncook return address and return.
4434 __ pop(result_register());
4435 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4436 __ sra(a1, a1, 1); // Un-smi-tag value.
4437 __ Addu(at, a1, Operand(masm_->CodeObject()));
4438 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004439}
4440
4441
4442#undef __
4443
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004444#define __ ACCESS_MASM(masm())
4445
4446FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4447 int* stack_depth,
4448 int* context_length) {
4449 // The macros used here must preserve the result register.
4450
4451 // Because the handler block contains the context of the finally
4452 // code, we can restore it directly from there for the finally code
4453 // rather than iteratively unwinding contexts via their previous
4454 // links.
4455 __ Drop(*stack_depth); // Down to the handler block.
4456 if (*context_length > 0) {
4457 // Restore the context to its dedicated register and the stack.
4458 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4459 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4460 }
4461 __ PopTryHandler();
4462 __ Call(finally_entry_);
4463
4464 *stack_depth = 0;
4465 *context_length = 0;
4466 return previous_;
4467}
4468
4469
4470#undef __
4471
ager@chromium.org5c838252010-02-19 08:53:10 +00004472} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004473
4474#endif // V8_TARGET_ARCH_MIPS