blob: e259fc460033025342f66e848d4643112b78837c [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));
ulan@chromium.org812308e2012-02-29 15:58:45 +0000955 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
956 __ mov(a0, v0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000957 // Convert the object to a JS object.
958 Label convert, done_convert;
959 __ JumpIfSmi(a0, &convert);
960 __ GetObjectType(a0, a1, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000961 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000962 __ bind(&convert);
963 __ push(a0);
964 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
965 __ mov(a0, v0);
966 __ bind(&done_convert);
967 __ push(a0);
968
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000969 // Check for proxies.
970 Label call_runtime;
971 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
972 __ GetObjectType(a0, a1, a1);
973 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
974
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000975 // Check cache validity in generated code. This is a fast case for
976 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
977 // guarantee cache validity, call the runtime system to check cache
978 // validity or get the property names in a fixed array.
ulan@chromium.org812308e2012-02-29 15:58:45 +0000979 __ CheckEnumCache(null_value, &call_runtime);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000980
981 // The enum cache is valid. Load the map of the object being
982 // iterated over and use the cache for the iteration.
983 Label use_cache;
984 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
985 __ Branch(&use_cache);
986
987 // Get the set of properties to enumerate.
988 __ bind(&call_runtime);
989 __ push(a0); // Duplicate the enumerable object on the stack.
990 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
991
992 // If we got a map from the runtime call, we can do a fast
993 // modification check. Otherwise, we got a fixed array, and we have
994 // to do a slow check.
995 Label fixed_array;
996 __ mov(a2, v0);
997 __ lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
998 __ LoadRoot(at, Heap::kMetaMapRootIndex);
999 __ Branch(&fixed_array, ne, a1, Operand(at));
1000
1001 // We got a map in register v0. Get the enumeration cache from it.
1002 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001003 __ LoadInstanceDescriptors(v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001004 __ lw(a1, FieldMemOperand(a1, DescriptorArray::kEnumerationIndexOffset));
1005 __ lw(a2, FieldMemOperand(a1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1006
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001007 // Set up the four remaining stack slots.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001008 __ push(v0); // Map.
1009 __ lw(a1, FieldMemOperand(a2, FixedArray::kLengthOffset));
1010 __ li(a0, Operand(Smi::FromInt(0)));
1011 // Push enumeration cache, enumeration cache length (as smi) and zero.
1012 __ Push(a2, a1, a0);
1013 __ jmp(&loop);
1014
1015 // We got a fixed array in register v0. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001016 Label non_proxy;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001017 __ bind(&fixed_array);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001018 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1019 __ lw(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1020 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1021 __ GetObjectType(a2, a3, a3);
1022 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1023 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1024 __ bind(&non_proxy);
1025 __ Push(a1, v0); // Smi and array
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001026 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1027 __ li(a0, Operand(Smi::FromInt(0)));
1028 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1029
1030 // Generate code for doing the condition check.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001031 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001032 __ bind(&loop);
1033 // Load the current count to a0, load the length to a1.
1034 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1035 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001036 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001037
1038 // Get the current entry of the array into register a3.
1039 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1040 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1041 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1042 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1043 __ lw(a3, MemOperand(t0)); // Current entry.
1044
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001045 // Get the expected map from the stack or a smi in the
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001046 // permanent slow case into register a2.
1047 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1048
1049 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001050 // If not, we may have to filter the key.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001051 Label update_each;
1052 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1053 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1054 __ Branch(&update_each, eq, t0, Operand(a2));
1055
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001056 // For proxies, no filtering is done.
1057 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1058 ASSERT_EQ(Smi::FromInt(0), 0);
1059 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1060
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001061 // Convert the entry to a string or (smi) 0 if it isn't a property
1062 // any more. If the property has been removed while iterating, we
1063 // just skip it.
1064 __ push(a1); // Enumerable.
1065 __ push(a3); // Current entry.
1066 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1067 __ mov(a3, result_register());
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001068 __ Branch(loop_statement.continue_label(), eq, a3, Operand(zero_reg));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001069
1070 // Update the 'each' property or variable from the possibly filtered
1071 // entry in register a3.
1072 __ bind(&update_each);
1073 __ mov(result_register(), a3);
1074 // Perform the assignment as if via '='.
1075 { EffectContext context(this);
ulan@chromium.org812308e2012-02-29 15:58:45 +00001076 EmitAssignment(stmt->each());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001077 }
1078
1079 // Generate code for the body of the loop.
1080 Visit(stmt->body());
1081
1082 // Generate code for the going to the next element by incrementing
1083 // the index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001084 __ bind(loop_statement.continue_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001085 __ pop(a0);
1086 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1087 __ push(a0);
1088
yangguo@chromium.org56454712012-02-16 15:33:53 +00001089 EmitStackCheck(stmt, &loop);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001090 __ Branch(&loop);
1091
1092 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001093 __ bind(loop_statement.break_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001094 __ Drop(5);
1095
1096 // Exit and decrement the loop depth.
ulan@chromium.org812308e2012-02-29 15:58:45 +00001097 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001098 __ bind(&exit);
1099 decrement_loop_depth();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001100}
1101
1102
1103void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1104 bool pretenure) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001105 // Use the fast case closure allocation code that allocates in new
1106 // space for nested functions that don't need literals cloning. If
1107 // we're running with the --always-opt or the --prepare-always-opt
1108 // flag, we need to use the runtime function so that the new function
1109 // we are creating here gets a chance to have its code optimized and
1110 // doesn't just get a copy of the existing unoptimized code.
1111 if (!FLAG_always_opt &&
1112 !FLAG_prepare_always_opt &&
1113 !pretenure &&
1114 scope()->is_function_scope() &&
1115 info->num_literals() == 0) {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001116 FastNewClosureStub stub(info->language_mode());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001117 __ li(a0, Operand(info));
1118 __ push(a0);
1119 __ CallStub(&stub);
1120 } else {
1121 __ li(a0, Operand(info));
1122 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1123 : Heap::kFalseValueRootIndex);
1124 __ Push(cp, a0, a1);
1125 __ CallRuntime(Runtime::kNewClosure, 3);
1126 }
1127 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001128}
1129
1130
1131void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001132 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001133 EmitVariableLoad(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001134}
1135
1136
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001137void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1138 TypeofState typeof_state,
1139 Label* slow) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001140 Register current = cp;
1141 Register next = a1;
1142 Register temp = a2;
1143
1144 Scope* s = scope();
1145 while (s != NULL) {
1146 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001147 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001148 // Check that extension is NULL.
1149 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1150 __ Branch(slow, ne, temp, Operand(zero_reg));
1151 }
1152 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001153 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001154 // Walk the rest of the chain without clobbering cp.
1155 current = next;
1156 }
1157 // If no outer scope calls eval, we do not need to check more
1158 // context extensions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001159 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001160 s = s->outer_scope();
1161 }
1162
1163 if (s->is_eval_scope()) {
1164 Label loop, fast;
1165 if (!current.is(next)) {
1166 __ Move(next, current);
1167 }
1168 __ bind(&loop);
1169 // Terminate at global context.
1170 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1171 __ LoadRoot(t0, Heap::kGlobalContextMapRootIndex);
1172 __ Branch(&fast, eq, temp, Operand(t0));
1173 // Check that extension is NULL.
1174 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1175 __ Branch(slow, ne, temp, Operand(zero_reg));
1176 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001177 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001178 __ Branch(&loop);
1179 __ bind(&fast);
1180 }
1181
1182 __ lw(a0, GlobalObjectOperand());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001183 __ li(a2, Operand(var->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001184 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1185 ? RelocInfo::CODE_TARGET
1186 : RelocInfo::CODE_TARGET_CONTEXT;
1187 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001188 __ Call(ic, mode);
ager@chromium.org5c838252010-02-19 08:53:10 +00001189}
1190
1191
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001192MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1193 Label* slow) {
1194 ASSERT(var->IsContextSlot());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001195 Register context = cp;
1196 Register next = a3;
1197 Register temp = t0;
1198
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001199 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001200 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001201 if (s->calls_non_strict_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001202 // Check that extension is NULL.
1203 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1204 __ Branch(slow, ne, temp, Operand(zero_reg));
1205 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001206 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001207 // Walk the rest of the chain without clobbering cp.
1208 context = next;
1209 }
1210 }
1211 // Check that last extension is NULL.
1212 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1213 __ Branch(slow, ne, temp, Operand(zero_reg));
1214
1215 // This function is used only for loads, not stores, so it's safe to
1216 // return an cp-based operand (the write barrier cannot be allowed to
1217 // destroy the cp register).
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001218 return ContextOperand(context, var->index());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001219}
1220
1221
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001222void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1223 TypeofState typeof_state,
1224 Label* slow,
1225 Label* done) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001226 // Generate fast-case code for variables that might be shadowed by
1227 // eval-introduced variables. Eval is used a lot without
1228 // introducing variables. In those cases, we do not want to
1229 // perform a runtime call for all variables in the scope
1230 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001231 if (var->mode() == DYNAMIC_GLOBAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001232 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001233 __ Branch(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001234 } else if (var->mode() == DYNAMIC_LOCAL) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001235 Variable* local = var->local_if_not_shadowed();
1236 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001237 if (local->mode() == CONST ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001238 local->mode() == CONST_HARMONY ||
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001239 local->mode() == LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001240 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1241 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001242 if (local->mode() == CONST) {
1243 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1244 __ movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001245 } else { // LET || CONST_HARMONY
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001246 __ Branch(done, ne, at, Operand(zero_reg));
1247 __ li(a0, Operand(var->name()));
1248 __ push(a0);
1249 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1250 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001251 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001252 __ Branch(done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001253 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001254}
1255
1256
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001257void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1258 // Record position before possible IC call.
1259 SetSourcePosition(proxy->position());
1260 Variable* var = proxy->var();
1261
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001262 // Three cases: global variables, lookup variables, and all other types of
1263 // variables.
1264 switch (var->location()) {
1265 case Variable::UNALLOCATED: {
1266 Comment cmnt(masm_, "Global variable");
1267 // Use inline caching. Variable name is passed in a2 and the global
1268 // object (receiver) in a0.
1269 __ lw(a0, GlobalObjectOperand());
1270 __ li(a2, Operand(var->name()));
1271 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
1272 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
1273 context()->Plug(v0);
1274 break;
1275 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001276
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001277 case Variable::PARAMETER:
1278 case Variable::LOCAL:
1279 case Variable::CONTEXT: {
1280 Comment cmnt(masm_, var->IsContextSlot()
1281 ? "Context variable"
1282 : "Stack variable");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001283 if (var->binding_needs_init()) {
1284 // var->scope() may be NULL when the proxy is located in eval code and
1285 // refers to a potential outside binding. Currently those bindings are
1286 // always looked up dynamically, i.e. in that case
1287 // var->location() == LOOKUP.
1288 // always holds.
1289 ASSERT(var->scope() != NULL);
1290
1291 // Check if the binding really needs an initialization check. The check
1292 // can be skipped in the following situation: we have a LET or CONST
1293 // binding in harmony mode, both the Variable and the VariableProxy have
1294 // the same declaration scope (i.e. they are both in global code, in the
1295 // same function or in the same eval code) and the VariableProxy is in
1296 // the source physically located after the initializer of the variable.
1297 //
1298 // We cannot skip any initialization checks for CONST in non-harmony
1299 // mode because const variables may be declared but never initialized:
1300 // if (false) { const x; }; var y = x;
1301 //
1302 // The condition on the declaration scopes is a conservative check for
1303 // nested functions that access a binding and are called before the
1304 // binding is initialized:
1305 // function() { f(); let x = 1; function f() { x = 2; } }
1306 //
1307 bool skip_init_check;
1308 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1309 skip_init_check = false;
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001310 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001311 // Check that we always have valid source position.
1312 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1313 ASSERT(proxy->position() != RelocInfo::kNoPosition);
1314 skip_init_check = var->mode() != CONST &&
1315 var->initializer_position() < proxy->position();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001316 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001317
1318 if (!skip_init_check) {
1319 // Let and const need a read barrier.
1320 GetVar(v0, var);
1321 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1322 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1323 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1324 // Throw a reference error when using an uninitialized let/const
1325 // binding in harmony mode.
1326 Label done;
1327 __ Branch(&done, ne, at, Operand(zero_reg));
1328 __ li(a0, Operand(var->name()));
1329 __ push(a0);
1330 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1331 __ bind(&done);
1332 } else {
1333 // Uninitalized const bindings outside of harmony mode are unholed.
1334 ASSERT(var->mode() == CONST);
1335 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1336 __ movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1337 }
1338 context()->Plug(v0);
1339 break;
1340 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001341 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001342 context()->Plug(var);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001343 break;
1344 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001345
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001346 case Variable::LOOKUP: {
1347 Label done, slow;
1348 // Generate code for loading from variables potentially shadowed
1349 // by eval-introduced variables.
1350 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1351 __ bind(&slow);
1352 Comment cmnt(masm_, "Lookup variable");
1353 __ li(a1, Operand(var->name()));
1354 __ Push(cp, a1); // Context and name.
1355 __ CallRuntime(Runtime::kLoadContextSlot, 2);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001356 __ bind(&done);
1357 context()->Plug(v0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001358 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001359 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001360}
1361
1362
1363void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001364 Comment cmnt(masm_, "[ RegExpLiteral");
1365 Label materialized;
1366 // Registers will be used as follows:
1367 // t1 = materialized value (RegExp literal)
1368 // t0 = JS function, literals array
1369 // a3 = literal index
1370 // a2 = RegExp pattern
1371 // a1 = RegExp flags
1372 // a0 = RegExp literal clone
1373 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1374 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1375 int literal_offset =
1376 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1377 __ lw(t1, FieldMemOperand(t0, literal_offset));
1378 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1379 __ Branch(&materialized, ne, t1, Operand(at));
1380
1381 // Create regexp literal using runtime function.
1382 // Result will be in v0.
1383 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1384 __ li(a2, Operand(expr->pattern()));
1385 __ li(a1, Operand(expr->flags()));
1386 __ Push(t0, a3, a2, a1);
1387 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1388 __ mov(t1, v0);
1389
1390 __ bind(&materialized);
1391 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1392 Label allocated, runtime_allocate;
1393 __ AllocateInNewSpace(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1394 __ jmp(&allocated);
1395
1396 __ bind(&runtime_allocate);
1397 __ push(t1);
1398 __ li(a0, Operand(Smi::FromInt(size)));
1399 __ push(a0);
1400 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1401 __ pop(t1);
1402
1403 __ bind(&allocated);
1404
1405 // After this, registers are used as follows:
1406 // v0: Newly allocated regexp.
1407 // t1: Materialized regexp.
1408 // a2: temp.
1409 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1410 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001411}
1412
1413
1414void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001415 Comment cmnt(masm_, "[ ObjectLiteral");
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001416 Handle<FixedArray> constant_properties = expr->constant_properties();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001417 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1418 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1419 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001420 __ li(a1, Operand(constant_properties));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001421 int flags = expr->fast_elements()
1422 ? ObjectLiteral::kFastElements
1423 : ObjectLiteral::kNoFlags;
1424 flags |= expr->has_function()
1425 ? ObjectLiteral::kHasFunction
1426 : ObjectLiteral::kNoFlags;
1427 __ li(a0, Operand(Smi::FromInt(flags)));
1428 __ Push(a3, a2, a1, a0);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001429 int properties_count = constant_properties->length() / 2;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001430 if (expr->depth() > 1) {
1431 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001432 } else if (flags != ObjectLiteral::kFastElements ||
1433 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001434 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001435 } else {
1436 FastCloneShallowObjectStub stub(properties_count);
1437 __ CallStub(&stub);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001438 }
1439
1440 // If result_saved is true the result is on top of the stack. If
1441 // result_saved is false the result is in v0.
1442 bool result_saved = false;
1443
1444 // Mark all computed expressions that are bound to a key that
1445 // is shadowed by a later occurrence of the same key. For the
1446 // marked expressions, no store code is emitted.
1447 expr->CalculateEmitStore();
1448
1449 for (int i = 0; i < expr->properties()->length(); i++) {
1450 ObjectLiteral::Property* property = expr->properties()->at(i);
1451 if (property->IsCompileTimeValue()) continue;
1452
1453 Literal* key = property->key();
1454 Expression* value = property->value();
1455 if (!result_saved) {
1456 __ push(v0); // Save result on stack.
1457 result_saved = true;
1458 }
1459 switch (property->kind()) {
1460 case ObjectLiteral::Property::CONSTANT:
1461 UNREACHABLE();
1462 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1463 ASSERT(!CompileTimeValue::IsCompileTimeValue(property->value()));
1464 // Fall through.
1465 case ObjectLiteral::Property::COMPUTED:
1466 if (key->handle()->IsSymbol()) {
1467 if (property->emit_store()) {
1468 VisitForAccumulatorValue(value);
1469 __ mov(a0, result_register());
1470 __ li(a2, Operand(key->handle()));
1471 __ lw(a1, MemOperand(sp));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001472 Handle<Code> ic = is_classic_mode()
1473 ? isolate()->builtins()->StoreIC_Initialize()
1474 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001475 __ Call(ic, RelocInfo::CODE_TARGET, key->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001476 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1477 } else {
1478 VisitForEffect(value);
1479 }
1480 break;
1481 }
1482 // Fall through.
1483 case ObjectLiteral::Property::PROTOTYPE:
1484 // Duplicate receiver on stack.
1485 __ lw(a0, MemOperand(sp));
1486 __ push(a0);
1487 VisitForStackValue(key);
1488 VisitForStackValue(value);
1489 if (property->emit_store()) {
1490 __ li(a0, Operand(Smi::FromInt(NONE))); // PropertyAttributes.
1491 __ push(a0);
1492 __ CallRuntime(Runtime::kSetProperty, 4);
1493 } else {
1494 __ Drop(3);
1495 }
1496 break;
1497 case ObjectLiteral::Property::GETTER:
1498 case ObjectLiteral::Property::SETTER:
1499 // Duplicate receiver on stack.
1500 __ lw(a0, MemOperand(sp));
1501 __ push(a0);
1502 VisitForStackValue(key);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001503 if (property->kind() == ObjectLiteral::Property::GETTER) {
1504 VisitForStackValue(value);
1505 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1506 __ push(a1);
1507 } else {
1508 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1509 __ push(a1);
1510 VisitForStackValue(value);
1511 }
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001512 __ li(a0, Operand(Smi::FromInt(NONE)));
1513 __ push(a0);
1514 __ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001515 break;
1516 }
1517 }
1518
1519 if (expr->has_function()) {
1520 ASSERT(result_saved);
1521 __ lw(a0, MemOperand(sp));
1522 __ push(a0);
1523 __ CallRuntime(Runtime::kToFastProperties, 1);
1524 }
1525
1526 if (result_saved) {
1527 context()->PlugTOS();
1528 } else {
1529 context()->Plug(v0);
1530 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001531}
1532
1533
1534void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001535 Comment cmnt(masm_, "[ ArrayLiteral");
1536
1537 ZoneList<Expression*>* subexprs = expr->values();
1538 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001539
1540 Handle<FixedArray> constant_elements = expr->constant_elements();
1541 ASSERT_EQ(2, constant_elements->length());
1542 ElementsKind constant_elements_kind =
1543 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001544 bool has_fast_elements = constant_elements_kind == FAST_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001545 Handle<FixedArrayBase> constant_elements_values(
1546 FixedArrayBase::cast(constant_elements->get(1)));
1547
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001548 __ mov(a0, result_register());
1549 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1550 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1551 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001552 __ li(a1, Operand(constant_elements));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001553 __ Push(a3, a2, a1);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001554 if (has_fast_elements && constant_elements_values->map() ==
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001555 isolate()->heap()->fixed_cow_array_map()) {
1556 FastCloneShallowArrayStub stub(
1557 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
1558 __ CallStub(&stub);
1559 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(),
1560 1, a1, a2);
1561 } else if (expr->depth() > 1) {
1562 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
1563 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1564 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
1565 } else {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001566 ASSERT(constant_elements_kind == FAST_ELEMENTS ||
1567 constant_elements_kind == FAST_SMI_ONLY_ELEMENTS ||
1568 FLAG_smi_only_arrays);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001569 FastCloneShallowArrayStub::Mode mode = has_fast_elements
1570 ? FastCloneShallowArrayStub::CLONE_ELEMENTS
1571 : FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001572 FastCloneShallowArrayStub stub(mode, length);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001573 __ CallStub(&stub);
1574 }
1575
1576 bool result_saved = false; // Is the result saved to the stack?
1577
1578 // Emit code to evaluate all the non-constant subexpressions and to store
1579 // them into the newly cloned array.
1580 for (int i = 0; i < length; i++) {
1581 Expression* subexpr = subexprs->at(i);
1582 // If the subexpression is a literal or a simple materialized literal it
1583 // is already set in the cloned array.
1584 if (subexpr->AsLiteral() != NULL ||
1585 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1586 continue;
1587 }
1588
1589 if (!result_saved) {
1590 __ push(v0);
1591 result_saved = true;
1592 }
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001593
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001594 VisitForAccumulatorValue(subexpr);
1595
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001596 if (constant_elements_kind == FAST_ELEMENTS) {
1597 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1598 __ lw(t2, MemOperand(sp)); // Copy of array literal.
1599 __ lw(a1, FieldMemOperand(t2, JSObject::kElementsOffset));
1600 __ sw(result_register(), FieldMemOperand(a1, offset));
1601 // Update the write barrier for the array store.
1602 __ RecordWriteField(a1, offset, result_register(), a2,
1603 kRAHasBeenSaved, kDontSaveFPRegs,
1604 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1605 } else {
1606 __ lw(a1, MemOperand(sp)); // Copy of array literal.
1607 __ lw(a2, FieldMemOperand(a1, JSObject::kMapOffset));
1608 __ li(a3, Operand(Smi::FromInt(i)));
1609 __ li(t0, Operand(Smi::FromInt(expr->literal_index())));
1610 __ mov(a0, result_register());
1611 StoreArrayLiteralElementStub stub;
1612 __ CallStub(&stub);
1613 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001614
1615 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1616 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001617 if (result_saved) {
1618 context()->PlugTOS();
1619 } else {
1620 context()->Plug(v0);
1621 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001622}
1623
1624
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001625void FullCodeGenerator::VisitAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001626 Comment cmnt(masm_, "[ Assignment");
1627 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1628 // on the left-hand side.
1629 if (!expr->target()->IsValidLeftHandSide()) {
1630 VisitForEffect(expr->target());
1631 return;
1632 }
1633
1634 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001635 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001636 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1637 LhsKind assign_type = VARIABLE;
1638 Property* property = expr->target()->AsProperty();
1639 if (property != NULL) {
1640 assign_type = (property->key()->IsPropertyName())
1641 ? NAMED_PROPERTY
1642 : KEYED_PROPERTY;
1643 }
1644
1645 // Evaluate LHS expression.
1646 switch (assign_type) {
1647 case VARIABLE:
1648 // Nothing to do here.
1649 break;
1650 case NAMED_PROPERTY:
1651 if (expr->is_compound()) {
1652 // We need the receiver both on the stack and in the accumulator.
1653 VisitForAccumulatorValue(property->obj());
1654 __ push(result_register());
1655 } else {
1656 VisitForStackValue(property->obj());
1657 }
1658 break;
1659 case KEYED_PROPERTY:
1660 // We need the key and receiver on both the stack and in v0 and a1.
1661 if (expr->is_compound()) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001662 VisitForStackValue(property->obj());
1663 VisitForAccumulatorValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001664 __ lw(a1, MemOperand(sp, 0));
1665 __ push(v0);
1666 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001667 VisitForStackValue(property->obj());
1668 VisitForStackValue(property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001669 }
1670 break;
1671 }
1672
1673 // For compound assignments we need another deoptimization point after the
1674 // variable/property load.
1675 if (expr->is_compound()) {
1676 { AccumulatorValueContext context(this);
1677 switch (assign_type) {
1678 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001679 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001680 PrepareForBailout(expr->target(), TOS_REG);
1681 break;
1682 case NAMED_PROPERTY:
1683 EmitNamedPropertyLoad(property);
1684 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1685 break;
1686 case KEYED_PROPERTY:
1687 EmitKeyedPropertyLoad(property);
1688 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
1689 break;
1690 }
1691 }
1692
1693 Token::Value op = expr->binary_op();
1694 __ push(v0); // Left operand goes on the stack.
1695 VisitForAccumulatorValue(expr->value());
1696
1697 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1698 ? OVERWRITE_RIGHT
1699 : NO_OVERWRITE;
1700 SetSourcePosition(expr->position() + 1);
1701 AccumulatorValueContext context(this);
1702 if (ShouldInlineSmiCase(op)) {
1703 EmitInlineSmiBinaryOp(expr->binary_operation(),
1704 op,
1705 mode,
1706 expr->target(),
1707 expr->value());
1708 } else {
1709 EmitBinaryOp(expr->binary_operation(), op, mode);
1710 }
1711
1712 // Deoptimization point in case the binary operation may have side effects.
1713 PrepareForBailout(expr->binary_operation(), TOS_REG);
1714 } else {
1715 VisitForAccumulatorValue(expr->value());
1716 }
1717
1718 // Record source position before possible IC call.
1719 SetSourcePosition(expr->position());
1720
1721 // Store the value.
1722 switch (assign_type) {
1723 case VARIABLE:
1724 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1725 expr->op());
1726 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1727 context()->Plug(v0);
1728 break;
1729 case NAMED_PROPERTY:
1730 EmitNamedPropertyAssignment(expr);
1731 break;
1732 case KEYED_PROPERTY:
1733 EmitKeyedPropertyAssignment(expr);
1734 break;
1735 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001736}
1737
1738
ager@chromium.org5c838252010-02-19 08:53:10 +00001739void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001740 SetSourcePosition(prop->position());
1741 Literal* key = prop->key()->AsLiteral();
1742 __ mov(a0, result_register());
1743 __ li(a2, Operand(key->handle()));
1744 // Call load IC. It has arguments receiver and property name a0 and a2.
1745 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001746 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001747}
1748
1749
1750void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001751 SetSourcePosition(prop->position());
1752 __ mov(a0, result_register());
1753 // Call keyed load IC. It has arguments key and receiver in a0 and a1.
1754 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001755 __ Call(ic, RelocInfo::CODE_TARGET, prop->id());
ager@chromium.org5c838252010-02-19 08:53:10 +00001756}
1757
1758
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001759void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001760 Token::Value op,
1761 OverwriteMode mode,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001762 Expression* left_expr,
1763 Expression* right_expr) {
1764 Label done, smi_case, stub_call;
1765
1766 Register scratch1 = a2;
1767 Register scratch2 = a3;
1768
1769 // Get the arguments.
1770 Register left = a1;
1771 Register right = a0;
1772 __ pop(left);
1773 __ mov(a0, result_register());
1774
1775 // Perform combined smi check on both operands.
1776 __ Or(scratch1, left, Operand(right));
1777 STATIC_ASSERT(kSmiTag == 0);
1778 JumpPatchSite patch_site(masm_);
1779 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
1780
1781 __ bind(&stub_call);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001782 BinaryOpStub stub(op, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001783 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001784 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001785 __ jmp(&done);
1786
1787 __ bind(&smi_case);
1788 // Smi case. This code works the same way as the smi-smi case in the type
1789 // recording binary operation stub, see
danno@chromium.org40cb8782011-05-25 07:58:50 +00001790 // BinaryOpStub::GenerateSmiSmiOperation for comments.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001791 switch (op) {
1792 case Token::SAR:
1793 __ Branch(&stub_call);
1794 __ GetLeastBitsFromSmi(scratch1, right, 5);
1795 __ srav(right, left, scratch1);
1796 __ And(v0, right, Operand(~kSmiTagMask));
1797 break;
1798 case Token::SHL: {
1799 __ Branch(&stub_call);
1800 __ SmiUntag(scratch1, left);
1801 __ GetLeastBitsFromSmi(scratch2, right, 5);
1802 __ sllv(scratch1, scratch1, scratch2);
1803 __ Addu(scratch2, scratch1, Operand(0x40000000));
1804 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1805 __ SmiTag(v0, scratch1);
1806 break;
1807 }
1808 case Token::SHR: {
1809 __ Branch(&stub_call);
1810 __ SmiUntag(scratch1, left);
1811 __ GetLeastBitsFromSmi(scratch2, right, 5);
1812 __ srlv(scratch1, scratch1, scratch2);
1813 __ And(scratch2, scratch1, 0xc0000000);
1814 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
1815 __ SmiTag(v0, scratch1);
1816 break;
1817 }
1818 case Token::ADD:
1819 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
1820 __ BranchOnOverflow(&stub_call, scratch1);
1821 break;
1822 case Token::SUB:
1823 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
1824 __ BranchOnOverflow(&stub_call, scratch1);
1825 break;
1826 case Token::MUL: {
1827 __ SmiUntag(scratch1, right);
1828 __ Mult(left, scratch1);
1829 __ mflo(scratch1);
1830 __ mfhi(scratch2);
1831 __ sra(scratch1, scratch1, 31);
1832 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
1833 __ mflo(v0);
1834 __ Branch(&done, ne, v0, Operand(zero_reg));
1835 __ Addu(scratch2, right, left);
1836 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
1837 ASSERT(Smi::FromInt(0) == 0);
1838 __ mov(v0, zero_reg);
1839 break;
1840 }
1841 case Token::BIT_OR:
1842 __ Or(v0, left, Operand(right));
1843 break;
1844 case Token::BIT_AND:
1845 __ And(v0, left, Operand(right));
1846 break;
1847 case Token::BIT_XOR:
1848 __ Xor(v0, left, Operand(right));
1849 break;
1850 default:
1851 UNREACHABLE();
1852 }
1853
1854 __ bind(&done);
1855 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001856}
1857
1858
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001859void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1860 Token::Value op,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001861 OverwriteMode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001862 __ mov(a0, result_register());
1863 __ pop(a1);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001864 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001865 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001866 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001867 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001868 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001869}
1870
1871
ulan@chromium.org812308e2012-02-29 15:58:45 +00001872void FullCodeGenerator::EmitAssignment(Expression* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001873 // Invalid left-hand sides are rewritten to have a 'throw
1874 // ReferenceError' on the left-hand side.
1875 if (!expr->IsValidLeftHandSide()) {
1876 VisitForEffect(expr);
1877 return;
1878 }
1879
1880 // Left-hand side can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001881 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001882 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1883 LhsKind assign_type = VARIABLE;
1884 Property* prop = expr->AsProperty();
1885 if (prop != NULL) {
1886 assign_type = (prop->key()->IsPropertyName())
1887 ? NAMED_PROPERTY
1888 : KEYED_PROPERTY;
1889 }
1890
1891 switch (assign_type) {
1892 case VARIABLE: {
1893 Variable* var = expr->AsVariableProxy()->var();
1894 EffectContext context(this);
1895 EmitVariableAssignment(var, Token::ASSIGN);
1896 break;
1897 }
1898 case NAMED_PROPERTY: {
1899 __ push(result_register()); // Preserve value.
1900 VisitForAccumulatorValue(prop->obj());
1901 __ mov(a1, result_register());
1902 __ pop(a0); // Restore value.
1903 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001904 Handle<Code> ic = is_classic_mode()
1905 ? isolate()->builtins()->StoreIC_Initialize()
1906 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001907 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001908 break;
1909 }
1910 case KEYED_PROPERTY: {
1911 __ push(result_register()); // Preserve value.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001912 VisitForStackValue(prop->obj());
1913 VisitForAccumulatorValue(prop->key());
1914 __ mov(a1, result_register());
1915 __ pop(a2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001916 __ pop(a0); // Restore value.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001917 Handle<Code> ic = is_classic_mode()
1918 ? isolate()->builtins()->KeyedStoreIC_Initialize()
1919 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001920 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001921 break;
1922 }
1923 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001924 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001925}
1926
1927
1928void FullCodeGenerator::EmitVariableAssignment(Variable* var,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001929 Token::Value op) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001930 if (var->IsUnallocated()) {
1931 // Global var, const, or let.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001932 __ mov(a0, result_register());
1933 __ li(a2, Operand(var->name()));
1934 __ lw(a1, GlobalObjectOperand());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001935 Handle<Code> ic = is_classic_mode()
1936 ? isolate()->builtins()->StoreIC_Initialize()
1937 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001938 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001939
1940 } else if (op == Token::INIT_CONST) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001941 // Const initializers need a write barrier.
1942 ASSERT(!var->IsParameter()); // No const parameters.
1943 if (var->IsStackLocal()) {
1944 Label skip;
1945 __ lw(a1, StackOperand(var));
1946 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1947 __ Branch(&skip, ne, a1, Operand(t0));
1948 __ sw(result_register(), StackOperand(var));
1949 __ bind(&skip);
1950 } else {
1951 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
1952 // Like var declarations, const declarations are hoisted to function
1953 // scope. However, unlike var initializers, const initializers are
1954 // able to drill a hole to that function context, even from inside a
1955 // 'with' context. We thus bypass the normal static scope lookup for
1956 // var->IsContextSlot().
1957 __ push(v0);
1958 __ li(a0, Operand(var->name()));
1959 __ Push(cp, a0); // Context and name.
1960 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001961 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001962
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001963 } else if (var->mode() == LET && op != Token::INIT_LET) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001964 // Non-initializing assignment to let variable needs a write barrier.
1965 if (var->IsLookupSlot()) {
1966 __ push(v0); // Value.
1967 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001968 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001969 __ Push(cp, a1, a0); // Context, name, strict mode.
1970 __ CallRuntime(Runtime::kStoreContextSlot, 4);
1971 } else {
1972 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
1973 Label assign;
1974 MemOperand location = VarOperand(var, a1);
1975 __ lw(a3, location);
1976 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1977 __ Branch(&assign, ne, a3, Operand(t0));
1978 __ li(a3, Operand(var->name()));
1979 __ push(a3);
1980 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1981 // Perform the assignment.
1982 __ bind(&assign);
1983 __ sw(result_register(), location);
1984 if (var->IsContextSlot()) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001985 // RecordWrite may destroy all its register arguments.
1986 __ mov(a3, result_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001987 int offset = Context::SlotOffset(var->index());
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001988 __ RecordWriteContextSlot(
1989 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001990 }
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001991 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001992
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001993 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
1994 // Assignment to var or initializing assignment to let/const
1995 // in harmony mode.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001996 if (var->IsStackAllocated() || var->IsContextSlot()) {
1997 MemOperand location = VarOperand(var, a1);
1998 if (FLAG_debug_code && op == Token::INIT_LET) {
1999 // Check for an uninitialized let binding.
2000 __ lw(a2, location);
2001 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2002 __ Check(eq, "Let binding re-initialization.", a2, Operand(t0));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002003 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002004 // Perform the assignment.
2005 __ sw(v0, location);
2006 if (var->IsContextSlot()) {
2007 __ mov(a3, v0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002008 int offset = Context::SlotOffset(var->index());
2009 __ RecordWriteContextSlot(
2010 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002011 }
2012 } else {
2013 ASSERT(var->IsLookupSlot());
2014 __ push(v0); // Value.
2015 __ li(a1, Operand(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002016 __ li(a0, Operand(Smi::FromInt(language_mode())));
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002017 __ Push(cp, a1, a0); // Context, name, strict mode.
2018 __ CallRuntime(Runtime::kStoreContextSlot, 4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002019 }
2020 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002021 // Non-initializing assignments to consts are ignored.
ager@chromium.org5c838252010-02-19 08:53:10 +00002022}
2023
2024
2025void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002026 // Assignment to a property, using a named store IC.
2027 Property* prop = expr->target()->AsProperty();
2028 ASSERT(prop != NULL);
2029 ASSERT(prop->key()->AsLiteral() != NULL);
2030
2031 // If the assignment starts a block of assignments to the same object,
2032 // change to slow case to avoid the quadratic behavior of repeatedly
2033 // adding fast properties.
2034 if (expr->starts_initialization_block()) {
2035 __ push(result_register());
2036 __ lw(t0, MemOperand(sp, kPointerSize)); // Receiver is now under value.
2037 __ push(t0);
2038 __ CallRuntime(Runtime::kToSlowProperties, 1);
2039 __ pop(result_register());
2040 }
2041
2042 // Record source code position before IC call.
2043 SetSourcePosition(expr->position());
2044 __ mov(a0, result_register()); // Load the value.
2045 __ li(a2, Operand(prop->key()->AsLiteral()->handle()));
2046 // Load receiver to a1. Leave a copy in the stack if needed for turning the
2047 // receiver into fast case.
2048 if (expr->ends_initialization_block()) {
2049 __ lw(a1, MemOperand(sp));
2050 } else {
2051 __ pop(a1);
2052 }
2053
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002054 Handle<Code> ic = is_classic_mode()
2055 ? isolate()->builtins()->StoreIC_Initialize()
2056 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002057 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002058
2059 // If the assignment ends an initialization block, revert to fast case.
2060 if (expr->ends_initialization_block()) {
2061 __ push(v0); // Result of assignment, saved even if not needed.
2062 // Receiver is under the result value.
2063 __ lw(t0, MemOperand(sp, kPointerSize));
2064 __ push(t0);
2065 __ CallRuntime(Runtime::kToFastProperties, 1);
2066 __ pop(v0);
2067 __ Drop(1);
2068 }
2069 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2070 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002071}
2072
2073
2074void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002075 // Assignment to a property, using a keyed store IC.
2076
2077 // If the assignment starts a block of assignments to the same object,
2078 // change to slow case to avoid the quadratic behavior of repeatedly
2079 // adding fast properties.
2080 if (expr->starts_initialization_block()) {
2081 __ push(result_register());
2082 // Receiver is now under the key and value.
2083 __ lw(t0, MemOperand(sp, 2 * kPointerSize));
2084 __ push(t0);
2085 __ CallRuntime(Runtime::kToSlowProperties, 1);
2086 __ pop(result_register());
2087 }
2088
2089 // Record source code position before IC call.
2090 SetSourcePosition(expr->position());
2091 // Call keyed store IC.
2092 // The arguments are:
2093 // - a0 is the value,
2094 // - a1 is the key,
2095 // - a2 is the receiver.
2096 __ mov(a0, result_register());
2097 __ pop(a1); // Key.
2098 // Load receiver to a2. Leave a copy in the stack if needed for turning the
2099 // receiver into fast case.
2100 if (expr->ends_initialization_block()) {
2101 __ lw(a2, MemOperand(sp));
2102 } else {
2103 __ pop(a2);
2104 }
2105
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002106 Handle<Code> ic = is_classic_mode()
2107 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2108 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002109 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002110
2111 // If the assignment ends an initialization block, revert to fast case.
2112 if (expr->ends_initialization_block()) {
2113 __ push(v0); // Result of assignment, saved even if not needed.
2114 // Receiver is under the result value.
2115 __ lw(t0, MemOperand(sp, kPointerSize));
2116 __ push(t0);
2117 __ CallRuntime(Runtime::kToFastProperties, 1);
2118 __ pop(v0);
2119 __ Drop(1);
2120 }
2121 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2122 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002123}
2124
2125
2126void FullCodeGenerator::VisitProperty(Property* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002127 Comment cmnt(masm_, "[ Property");
2128 Expression* key = expr->key();
2129
2130 if (key->IsPropertyName()) {
2131 VisitForAccumulatorValue(expr->obj());
2132 EmitNamedPropertyLoad(expr);
2133 context()->Plug(v0);
2134 } else {
2135 VisitForStackValue(expr->obj());
2136 VisitForAccumulatorValue(expr->key());
2137 __ pop(a1);
2138 EmitKeyedPropertyLoad(expr);
2139 context()->Plug(v0);
2140 }
ager@chromium.org5c838252010-02-19 08:53:10 +00002141}
2142
lrn@chromium.org7516f052011-03-30 08:52:27 +00002143
ager@chromium.org5c838252010-02-19 08:53:10 +00002144void FullCodeGenerator::EmitCallWithIC(Call* expr,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002145 Handle<Object> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00002146 RelocInfo::Mode mode) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002147 // Code common for calls using the IC.
2148 ZoneList<Expression*>* args = expr->arguments();
2149 int arg_count = args->length();
2150 { PreservePositionScope scope(masm()->positions_recorder());
2151 for (int i = 0; i < arg_count; i++) {
2152 VisitForStackValue(args->at(i));
2153 }
2154 __ li(a2, Operand(name));
2155 }
2156 // Record source position for debugger.
2157 SetSourcePosition(expr->position());
2158 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002159 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002160 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002161 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002162 RecordJSReturnSite(expr);
2163 // Restore context register.
2164 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2165 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002166}
2167
2168
lrn@chromium.org7516f052011-03-30 08:52:27 +00002169void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002170 Expression* key) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002171 // Load the key.
2172 VisitForAccumulatorValue(key);
2173
2174 // Swap the name of the function and the receiver on the stack to follow
2175 // the calling convention for call ICs.
2176 __ pop(a1);
2177 __ push(v0);
2178 __ push(a1);
2179
2180 // Code common for calls using the IC.
2181 ZoneList<Expression*>* args = expr->arguments();
2182 int arg_count = args->length();
2183 { PreservePositionScope scope(masm()->positions_recorder());
2184 for (int i = 0; i < arg_count; i++) {
2185 VisitForStackValue(args->at(i));
2186 }
2187 }
2188 // Record source position for debugger.
2189 SetSourcePosition(expr->position());
2190 // Call the IC initialization code.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002191 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002192 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002193 __ lw(a2, MemOperand(sp, (arg_count + 1) * kPointerSize)); // Key.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002194 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002195 RecordJSReturnSite(expr);
2196 // Restore context register.
2197 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2198 context()->DropAndPlug(1, v0); // Drop the key still on the stack.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002199}
2200
2201
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002202void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002203 // Code common for calls using the call stub.
2204 ZoneList<Expression*>* args = expr->arguments();
2205 int arg_count = args->length();
2206 { PreservePositionScope scope(masm()->positions_recorder());
2207 for (int i = 0; i < arg_count; i++) {
2208 VisitForStackValue(args->at(i));
2209 }
2210 }
2211 // Record source position for debugger.
2212 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002213 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002214 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002215 __ CallStub(&stub);
2216 RecordJSReturnSite(expr);
2217 // Restore context register.
2218 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2219 context()->DropAndPlug(1, v0);
2220}
2221
2222
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002223void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002224 // Push copy of the first argument or undefined if it doesn't exist.
2225 if (arg_count > 0) {
2226 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2227 } else {
2228 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2229 }
2230 __ push(a1);
2231
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002232 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002233 int receiver_offset = 2 + info_->scope()->num_parameters();
2234 __ lw(a1, MemOperand(fp, receiver_offset * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002235 __ push(a1);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002236 // Push the language mode.
2237 __ li(a1, Operand(Smi::FromInt(language_mode())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002238 __ push(a1);
2239
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002240 // Push the start position of the scope the calls resides in.
2241 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
2242 __ push(a1);
2243
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002244 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002245 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org5c838252010-02-19 08:53:10 +00002246}
2247
2248
2249void FullCodeGenerator::VisitCall(Call* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002250#ifdef DEBUG
2251 // We want to verify that RecordJSReturnSite gets called on all paths
2252 // through this function. Avoid early returns.
2253 expr->return_is_recorded_ = false;
2254#endif
2255
2256 Comment cmnt(masm_, "[ Call");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002257 Expression* callee = expr->expression();
2258 VariableProxy* proxy = callee->AsVariableProxy();
2259 Property* property = callee->AsProperty();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002260
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002261 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002262 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2263 // resolve the function we need to call and the receiver of the
2264 // call. Then we call the resolved function using the given
2265 // arguments.
2266 ZoneList<Expression*>* args = expr->arguments();
2267 int arg_count = args->length();
2268
2269 { PreservePositionScope pos_scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002270 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002271 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2272 __ push(a2); // Reserved receiver slot.
2273
2274 // Push the arguments.
2275 for (int i = 0; i < arg_count; i++) {
2276 VisitForStackValue(args->at(i));
2277 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002278
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002279 // Push a copy of the function (found below the arguments) and
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002280 // resolve eval.
2281 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2282 __ push(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002283 EmitResolvePossiblyDirectEval(arg_count);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002284
2285 // The runtime call returns a pair of values in v0 (function) and
2286 // v1 (receiver). Touch up the stack with the right values.
2287 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2288 __ sw(v1, MemOperand(sp, arg_count * kPointerSize));
2289 }
2290 // Record source position for debugger.
2291 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002292 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002293 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002294 __ CallStub(&stub);
2295 RecordJSReturnSite(expr);
2296 // Restore context register.
2297 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2298 context()->DropAndPlug(1, v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002299 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002300 // Push global object as receiver for the call IC.
2301 __ lw(a0, GlobalObjectOperand());
2302 __ push(a0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002303 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2304 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002305 // Call to a lookup slot (dynamically introduced variable).
2306 Label slow, done;
2307
2308 { PreservePositionScope scope(masm()->positions_recorder());
2309 // Generate code for loading from variables potentially shadowed
2310 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002311 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002312 }
2313
2314 __ bind(&slow);
2315 // Call the runtime to find the function to call (returned in v0)
2316 // and the object holding it (returned in v1).
2317 __ push(context_register());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002318 __ li(a2, Operand(proxy->name()));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002319 __ push(a2);
2320 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2321 __ Push(v0, v1); // Function, receiver.
2322
2323 // If fast case code has been generated, emit code to push the
2324 // function and receiver and have the slow path jump around this
2325 // code.
2326 if (done.is_linked()) {
2327 Label call;
2328 __ Branch(&call);
2329 __ bind(&done);
2330 // Push function.
2331 __ push(v0);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002332 // The receiver is implicitly the global receiver. Indicate this
2333 // by passing the hole to the call function stub.
2334 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002335 __ push(a1);
2336 __ bind(&call);
2337 }
2338
danno@chromium.org40cb8782011-05-25 07:58:50 +00002339 // The receiver is either the global receiver or an object found
2340 // by LoadContextSlot. That object could be the hole if the
2341 // receiver is implicitly the global object.
2342 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002343 } else if (property != NULL) {
2344 { PreservePositionScope scope(masm()->positions_recorder());
2345 VisitForStackValue(property->obj());
2346 }
2347 if (property->key()->IsPropertyName()) {
2348 EmitCallWithIC(expr,
2349 property->key()->AsLiteral()->handle(),
2350 RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002351 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002352 EmitKeyedCallWithIC(expr, property->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002353 }
2354 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002355 // Call to an arbitrary expression not handled specially above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002356 { PreservePositionScope scope(masm()->positions_recorder());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00002357 VisitForStackValue(callee);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002358 }
2359 // Load global receiver object.
2360 __ lw(a1, GlobalObjectOperand());
2361 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2362 __ push(a1);
2363 // Emit function call.
2364 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
2365 }
2366
2367#ifdef DEBUG
2368 // RecordJSReturnSite should have been called.
2369 ASSERT(expr->return_is_recorded_);
2370#endif
ager@chromium.org5c838252010-02-19 08:53:10 +00002371}
2372
2373
2374void FullCodeGenerator::VisitCallNew(CallNew* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002375 Comment cmnt(masm_, "[ CallNew");
2376 // According to ECMA-262, section 11.2.2, page 44, the function
2377 // expression in new calls must be evaluated before the
2378 // arguments.
2379
2380 // Push constructor on the stack. If it's not a function it's used as
2381 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2382 // ignored.
2383 VisitForStackValue(expr->expression());
2384
2385 // Push the arguments ("left-to-right") on the stack.
2386 ZoneList<Expression*>* args = expr->arguments();
2387 int arg_count = args->length();
2388 for (int i = 0; i < arg_count; i++) {
2389 VisitForStackValue(args->at(i));
2390 }
2391
2392 // Call the construct call builtin that handles allocation and
2393 // constructor invocation.
2394 SetSourcePosition(expr->position());
2395
2396 // Load function and argument count into a1 and a0.
2397 __ li(a0, Operand(arg_count));
2398 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
2399
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002400 // Record call targets in unoptimized code, but not in the snapshot.
2401 CallFunctionFlags flags;
2402 if (!Serializer::enabled()) {
2403 flags = RECORD_CALL_TARGET;
2404 Handle<Object> uninitialized =
2405 TypeFeedbackCells::UninitializedSentinel(isolate());
2406 Handle<JSGlobalPropertyCell> cell =
2407 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
2408 RecordTypeFeedbackCell(expr->id(), cell);
2409 __ li(a2, Operand(cell));
2410 } else {
2411 flags = NO_CALL_FUNCTION_FLAGS;
2412 }
2413
2414 CallConstructStub stub(flags);
2415 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
ulan@chromium.org967e2702012-02-28 09:49:15 +00002416 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002417 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00002418}
2419
2420
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002421void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2422 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002423 ASSERT(args->length() == 1);
2424
2425 VisitForAccumulatorValue(args->at(0));
2426
2427 Label materialize_true, materialize_false;
2428 Label* if_true = NULL;
2429 Label* if_false = NULL;
2430 Label* fall_through = NULL;
2431 context()->PrepareTest(&materialize_true, &materialize_false,
2432 &if_true, &if_false, &fall_through);
2433
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002434 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002435 __ And(t0, v0, Operand(kSmiTagMask));
2436 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
2437
2438 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002439}
2440
2441
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002442void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2443 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002444 ASSERT(args->length() == 1);
2445
2446 VisitForAccumulatorValue(args->at(0));
2447
2448 Label materialize_true, materialize_false;
2449 Label* if_true = NULL;
2450 Label* if_false = NULL;
2451 Label* fall_through = NULL;
2452 context()->PrepareTest(&materialize_true, &materialize_false,
2453 &if_true, &if_false, &fall_through);
2454
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002455 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002456 __ And(at, v0, Operand(kSmiTagMask | 0x80000000));
2457 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
2458
2459 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002460}
2461
2462
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002463void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2464 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002465 ASSERT(args->length() == 1);
2466
2467 VisitForAccumulatorValue(args->at(0));
2468
2469 Label materialize_true, materialize_false;
2470 Label* if_true = NULL;
2471 Label* if_false = NULL;
2472 Label* fall_through = NULL;
2473 context()->PrepareTest(&materialize_true, &materialize_false,
2474 &if_true, &if_false, &fall_through);
2475
2476 __ JumpIfSmi(v0, if_false);
2477 __ LoadRoot(at, Heap::kNullValueRootIndex);
2478 __ Branch(if_true, eq, v0, Operand(at));
2479 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
2480 // Undetectable objects behave like undefined when tested with typeof.
2481 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
2482 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
2483 __ Branch(if_false, ne, at, Operand(zero_reg));
2484 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002485 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002486 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002487 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
2488 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002489
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::EmitIsSpecObject(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 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002509 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002510 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002511 if_true, if_false, fall_through);
2512
2513 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002514}
2515
2516
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002517void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2518 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002519 ASSERT(args->length() == 1);
2520
2521 VisitForAccumulatorValue(args->at(0));
2522
2523 Label materialize_true, materialize_false;
2524 Label* if_true = NULL;
2525 Label* if_false = NULL;
2526 Label* fall_through = NULL;
2527 context()->PrepareTest(&materialize_true, &materialize_false,
2528 &if_true, &if_false, &fall_through);
2529
2530 __ JumpIfSmi(v0, if_false);
2531 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2532 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
2533 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002534 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002535 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
2536
2537 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002538}
2539
2540
2541void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002542 CallRuntime* expr) {
2543 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002544 ASSERT(args->length() == 1);
2545
2546 VisitForAccumulatorValue(args->at(0));
2547
2548 Label materialize_true, materialize_false;
2549 Label* if_true = NULL;
2550 Label* if_false = NULL;
2551 Label* fall_through = NULL;
2552 context()->PrepareTest(&materialize_true, &materialize_false,
2553 &if_true, &if_false, &fall_through);
2554
2555 if (FLAG_debug_code) __ AbortIfSmi(v0);
2556
2557 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2558 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
2559 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
2560 __ Branch(if_true, ne, t0, Operand(zero_reg));
2561
2562 // Check for fast case object. Generate false result for slow case object.
2563 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2564 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2565 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
2566 __ Branch(if_false, eq, a2, Operand(t0));
2567
2568 // Look for valueOf symbol in the descriptor array, and indicate false if
2569 // found. The type is not checked, so if it is a transition it is a false
2570 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002571 __ LoadInstanceDescriptors(a1, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002572 __ lw(a3, FieldMemOperand(t0, FixedArray::kLengthOffset));
2573 // t0: descriptor array
2574 // a3: length of descriptor array
2575 // Calculate the end of the descriptor array.
2576 STATIC_ASSERT(kSmiTag == 0);
2577 STATIC_ASSERT(kSmiTagSize == 1);
2578 STATIC_ASSERT(kPointerSize == 4);
2579 __ Addu(a2, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2580 __ sll(t1, a3, kPointerSizeLog2 - kSmiTagSize);
2581 __ Addu(a2, a2, t1);
2582
2583 // Calculate location of the first key name.
2584 __ Addu(t0,
2585 t0,
2586 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
2587 DescriptorArray::kFirstIndex * kPointerSize));
2588 // Loop through all the keys in the descriptor array. If one of these is the
2589 // symbol valueOf the result is false.
2590 Label entry, loop;
2591 // The use of t2 to store the valueOf symbol asumes that it is not otherwise
2592 // used in the loop below.
2593 __ li(t2, Operand(FACTORY->value_of_symbol()));
2594 __ jmp(&entry);
2595 __ bind(&loop);
2596 __ lw(a3, MemOperand(t0, 0));
2597 __ Branch(if_false, eq, a3, Operand(t2));
2598 __ Addu(t0, t0, Operand(kPointerSize));
2599 __ bind(&entry);
2600 __ Branch(&loop, ne, t0, Operand(a2));
2601
2602 // If a valueOf property is not found on the object check that it's
2603 // prototype is the un-modified String prototype. If not result is false.
2604 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
2605 __ JumpIfSmi(a2, if_false);
2606 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
2607 __ lw(a3, ContextOperand(cp, Context::GLOBAL_INDEX));
2608 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
2609 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2610 __ Branch(if_false, ne, a2, Operand(a3));
2611
2612 // Set the bit in the map to indicate that it has been checked safe for
2613 // default valueOf and set true result.
2614 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2615 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
2616 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
2617 __ jmp(if_true);
2618
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002619 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002620 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002621}
2622
2623
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002624void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2625 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002626 ASSERT(args->length() == 1);
2627
2628 VisitForAccumulatorValue(args->at(0));
2629
2630 Label materialize_true, materialize_false;
2631 Label* if_true = NULL;
2632 Label* if_false = NULL;
2633 Label* fall_through = NULL;
2634 context()->PrepareTest(&materialize_true, &materialize_false,
2635 &if_true, &if_false, &fall_through);
2636
2637 __ JumpIfSmi(v0, if_false);
2638 __ GetObjectType(v0, a1, a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002639 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002640 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
2641 __ Branch(if_false);
2642
2643 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002644}
2645
2646
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002647void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2648 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002649 ASSERT(args->length() == 1);
2650
2651 VisitForAccumulatorValue(args->at(0));
2652
2653 Label materialize_true, materialize_false;
2654 Label* if_true = NULL;
2655 Label* if_false = NULL;
2656 Label* fall_through = NULL;
2657 context()->PrepareTest(&materialize_true, &materialize_false,
2658 &if_true, &if_false, &fall_through);
2659
2660 __ JumpIfSmi(v0, if_false);
2661 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002662 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002663 Split(eq, a1, Operand(JS_ARRAY_TYPE),
2664 if_true, if_false, fall_through);
2665
2666 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002667}
2668
2669
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002670void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2671 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002672 ASSERT(args->length() == 1);
2673
2674 VisitForAccumulatorValue(args->at(0));
2675
2676 Label materialize_true, materialize_false;
2677 Label* if_true = NULL;
2678 Label* if_false = NULL;
2679 Label* fall_through = NULL;
2680 context()->PrepareTest(&materialize_true, &materialize_false,
2681 &if_true, &if_false, &fall_through);
2682
2683 __ JumpIfSmi(v0, if_false);
2684 __ GetObjectType(v0, a1, a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002685 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002686 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
2687
2688 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002689}
2690
2691
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002692void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2693 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002694
2695 Label materialize_true, materialize_false;
2696 Label* if_true = NULL;
2697 Label* if_false = NULL;
2698 Label* fall_through = NULL;
2699 context()->PrepareTest(&materialize_true, &materialize_false,
2700 &if_true, &if_false, &fall_through);
2701
2702 // Get the frame pointer for the calling frame.
2703 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2704
2705 // Skip the arguments adaptor frame if it exists.
2706 Label check_frame_marker;
2707 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
2708 __ Branch(&check_frame_marker, ne,
2709 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2710 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
2711
2712 // Check the marker in the calling frame.
2713 __ bind(&check_frame_marker);
2714 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002715 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002716 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
2717 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::EmitObjectEquals(CallRuntime* expr) {
2724 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002725 ASSERT(args->length() == 2);
2726
2727 // Load the two objects into registers and perform the comparison.
2728 VisitForStackValue(args->at(0));
2729 VisitForAccumulatorValue(args->at(1));
2730
2731 Label materialize_true, materialize_false;
2732 Label* if_true = NULL;
2733 Label* if_false = NULL;
2734 Label* fall_through = NULL;
2735 context()->PrepareTest(&materialize_true, &materialize_false,
2736 &if_true, &if_false, &fall_through);
2737
2738 __ pop(a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002739 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002740 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
2741
2742 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002743}
2744
2745
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002746void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2747 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002748 ASSERT(args->length() == 1);
2749
2750 // ArgumentsAccessStub expects the key in a1 and the formal
2751 // parameter count in a0.
2752 VisitForAccumulatorValue(args->at(0));
2753 __ mov(a1, v0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002754 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002755 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2756 __ CallStub(&stub);
2757 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002758}
2759
2760
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002761void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2762 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002763 Label exit;
2764 // Get the number of formal parameters.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002765 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002766
2767 // Check if the calling frame is an arguments adaptor frame.
2768 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2769 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
2770 __ Branch(&exit, ne, a3,
2771 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2772
2773 // Arguments adaptor case: Read the arguments length from the
2774 // adaptor frame.
2775 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2776
2777 __ bind(&exit);
2778 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002779}
2780
2781
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002782void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2783 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002784 ASSERT(args->length() == 1);
2785 Label done, null, function, non_function_constructor;
2786
2787 VisitForAccumulatorValue(args->at(0));
2788
2789 // If the object is a smi, we return null.
2790 __ JumpIfSmi(v0, &null);
2791
2792 // Check that the object is a JS object but take special care of JS
2793 // functions to make sure they have 'Function' as their class.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002794 // Assume that there are only two callable types, and one of them is at
2795 // either end of the type range for JS object types. Saves extra comparisons.
2796 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002797 __ GetObjectType(v0, v0, a1); // Map is now in v0.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002798 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002799
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002800 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2801 FIRST_SPEC_OBJECT_TYPE + 1);
2802 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002803
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002804 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2805 LAST_SPEC_OBJECT_TYPE - 1);
2806 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
2807 // Assume that there is no larger type.
2808 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
2809
2810 // Check if the constructor in the map is a JS function.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002811 __ lw(v0, FieldMemOperand(v0, Map::kConstructorOffset));
2812 __ GetObjectType(v0, a1, a1);
2813 __ Branch(&non_function_constructor, ne, a1, Operand(JS_FUNCTION_TYPE));
2814
2815 // v0 now contains the constructor function. Grab the
2816 // instance class name from there.
2817 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
2818 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
2819 __ Branch(&done);
2820
2821 // Functions have class 'Function'.
2822 __ bind(&function);
2823 __ LoadRoot(v0, Heap::kfunction_class_symbolRootIndex);
2824 __ jmp(&done);
2825
2826 // Objects with a non-function constructor have class 'Object'.
2827 __ bind(&non_function_constructor);
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +00002828 __ LoadRoot(v0, Heap::kObject_symbolRootIndex);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002829 __ jmp(&done);
2830
2831 // Non-JS objects have class null.
2832 __ bind(&null);
2833 __ LoadRoot(v0, Heap::kNullValueRootIndex);
2834
2835 // All done.
2836 __ bind(&done);
2837
2838 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002839}
2840
2841
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002842void FullCodeGenerator::EmitLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002843 // Conditionally generate a log call.
2844 // Args:
2845 // 0 (literal string): The type of logging (corresponds to the flags).
2846 // This is used to determine whether or not to generate the log call.
2847 // 1 (string): Format string. Access the string at argument index 2
2848 // with '%2s' (see Logger::LogRuntime for all the formats).
2849 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002850 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002851 ASSERT_EQ(args->length(), 3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002852 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
2853 VisitForStackValue(args->at(1));
2854 VisitForStackValue(args->at(2));
2855 __ CallRuntime(Runtime::kLog, 2);
2856 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002857
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002858 // Finally, we're expected to leave a value on the top of the stack.
2859 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2860 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002861}
2862
2863
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002864void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
2865 ASSERT(expr->arguments()->length() == 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002866 Label slow_allocate_heapnumber;
2867 Label heapnumber_allocated;
2868
2869 // Save the new heap number in callee-saved register s0, since
2870 // we call out to external C code below.
2871 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2872 __ AllocateHeapNumber(s0, a1, a2, t6, &slow_allocate_heapnumber);
2873 __ jmp(&heapnumber_allocated);
2874
2875 __ bind(&slow_allocate_heapnumber);
2876
2877 // Allocate a heap number.
2878 __ CallRuntime(Runtime::kNumberAlloc, 0);
2879 __ mov(s0, v0); // Save result in s0, so it is saved thru CFunc call.
2880
2881 __ bind(&heapnumber_allocated);
2882
2883 // Convert 32 random bits in v0 to 0.(32 random bits) in a double
2884 // by computing:
2885 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2886 if (CpuFeatures::IsSupported(FPU)) {
2887 __ PrepareCallCFunction(1, a0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002888 __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
2889 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002890 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
2891
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002892 CpuFeatures::Scope scope(FPU);
2893 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
2894 __ li(a1, Operand(0x41300000));
2895 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002896 __ Move(f12, v0, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002897 // Move 0x4130000000000000 to FPU.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002898 __ Move(f14, zero_reg, a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002899 // Subtract and store the result in the heap number.
2900 __ sub_d(f0, f12, f14);
2901 __ sdc1(f0, MemOperand(s0, HeapNumber::kValueOffset - kHeapObjectTag));
2902 __ mov(v0, s0);
2903 } else {
2904 __ PrepareCallCFunction(2, a0);
2905 __ mov(a0, s0);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002906 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
2907 __ lw(a1, FieldMemOperand(a1, GlobalObject::kGlobalContextOffset));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002908 __ CallCFunction(
2909 ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
2910 }
2911
2912 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002913}
2914
2915
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002916void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002917 // Load the arguments on the stack and call the stub.
2918 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002919 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002920 ASSERT(args->length() == 3);
2921 VisitForStackValue(args->at(0));
2922 VisitForStackValue(args->at(1));
2923 VisitForStackValue(args->at(2));
2924 __ CallStub(&stub);
2925 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002926}
2927
2928
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002929void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002930 // Load the arguments on the stack and call the stub.
2931 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002932 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002933 ASSERT(args->length() == 4);
2934 VisitForStackValue(args->at(0));
2935 VisitForStackValue(args->at(1));
2936 VisitForStackValue(args->at(2));
2937 VisitForStackValue(args->at(3));
2938 __ CallStub(&stub);
2939 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002940}
2941
2942
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002943void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
2944 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002945 ASSERT(args->length() == 1);
2946
2947 VisitForAccumulatorValue(args->at(0)); // Load the object.
2948
2949 Label done;
2950 // If the object is a smi return the object.
2951 __ JumpIfSmi(v0, &done);
2952 // If the object is not a value type, return the object.
2953 __ GetObjectType(v0, a1, a1);
2954 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
2955
2956 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
2957
2958 __ bind(&done);
2959 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002960}
2961
2962
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002963void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002964 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002965 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002966 ASSERT(args->length() == 2);
2967 VisitForStackValue(args->at(0));
2968 VisitForStackValue(args->at(1));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002969 if (CpuFeatures::IsSupported(FPU)) {
2970 MathPowStub stub(MathPowStub::ON_STACK);
2971 __ CallStub(&stub);
2972 } else {
2973 __ CallRuntime(Runtime::kMath_pow, 2);
2974 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002975 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002976}
2977
2978
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002979void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
2980 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002981 ASSERT(args->length() == 2);
2982
2983 VisitForStackValue(args->at(0)); // Load the object.
2984 VisitForAccumulatorValue(args->at(1)); // Load the value.
2985 __ pop(a1); // v0 = value. a1 = object.
2986
2987 Label done;
2988 // If the object is a smi, return the value.
2989 __ JumpIfSmi(a1, &done);
2990
2991 // If the object is not a value type, return the value.
2992 __ GetObjectType(a1, a2, a2);
2993 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
2994
2995 // Store the value.
2996 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
2997 // Update the write barrier. Save the value as it will be
2998 // overwritten by the write barrier code and is needed afterward.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002999 __ mov(a2, v0);
3000 __ RecordWriteField(
3001 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003002
3003 __ bind(&done);
3004 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003005}
3006
3007
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003008void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3009 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003010 ASSERT_EQ(args->length(), 1);
3011
3012 // Load the argument on the stack and call the stub.
3013 VisitForStackValue(args->at(0));
3014
3015 NumberToStringStub stub;
3016 __ CallStub(&stub);
3017 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003018}
3019
3020
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003021void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3022 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003023 ASSERT(args->length() == 1);
3024
3025 VisitForAccumulatorValue(args->at(0));
3026
3027 Label done;
3028 StringCharFromCodeGenerator generator(v0, a1);
3029 generator.GenerateFast(masm_);
3030 __ jmp(&done);
3031
3032 NopRuntimeCallHelper call_helper;
3033 generator.GenerateSlow(masm_, call_helper);
3034
3035 __ bind(&done);
3036 context()->Plug(a1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003037}
3038
3039
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003040void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3041 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003042 ASSERT(args->length() == 2);
3043
3044 VisitForStackValue(args->at(0));
3045 VisitForAccumulatorValue(args->at(1));
3046 __ mov(a0, result_register());
3047
3048 Register object = a1;
3049 Register index = a0;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003050 Register result = v0;
3051
3052 __ pop(object);
3053
3054 Label need_conversion;
3055 Label index_out_of_range;
3056 Label done;
3057 StringCharCodeAtGenerator generator(object,
3058 index,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003059 result,
3060 &need_conversion,
3061 &need_conversion,
3062 &index_out_of_range,
3063 STRING_INDEX_IS_NUMBER);
3064 generator.GenerateFast(masm_);
3065 __ jmp(&done);
3066
3067 __ bind(&index_out_of_range);
3068 // When the index is out of range, the spec requires us to return
3069 // NaN.
3070 __ LoadRoot(result, Heap::kNanValueRootIndex);
3071 __ jmp(&done);
3072
3073 __ bind(&need_conversion);
3074 // Load the undefined value into the result register, which will
3075 // trigger conversion.
3076 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3077 __ jmp(&done);
3078
3079 NopRuntimeCallHelper call_helper;
3080 generator.GenerateSlow(masm_, call_helper);
3081
3082 __ bind(&done);
3083 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003084}
3085
3086
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003087void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3088 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003089 ASSERT(args->length() == 2);
3090
3091 VisitForStackValue(args->at(0));
3092 VisitForAccumulatorValue(args->at(1));
3093 __ mov(a0, result_register());
3094
3095 Register object = a1;
3096 Register index = a0;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003097 Register scratch = a3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003098 Register result = v0;
3099
3100 __ pop(object);
3101
3102 Label need_conversion;
3103 Label index_out_of_range;
3104 Label done;
3105 StringCharAtGenerator generator(object,
3106 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003107 scratch,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003108 result,
3109 &need_conversion,
3110 &need_conversion,
3111 &index_out_of_range,
3112 STRING_INDEX_IS_NUMBER);
3113 generator.GenerateFast(masm_);
3114 __ jmp(&done);
3115
3116 __ bind(&index_out_of_range);
3117 // When the index is out of range, the spec requires us to return
3118 // the empty string.
3119 __ LoadRoot(result, Heap::kEmptyStringRootIndex);
3120 __ jmp(&done);
3121
3122 __ bind(&need_conversion);
3123 // Move smi zero into the result register, which will trigger
3124 // conversion.
3125 __ li(result, Operand(Smi::FromInt(0)));
3126 __ jmp(&done);
3127
3128 NopRuntimeCallHelper call_helper;
3129 generator.GenerateSlow(masm_, call_helper);
3130
3131 __ bind(&done);
3132 context()->Plug(result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003133}
3134
3135
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003136void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3137 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003138 ASSERT_EQ(2, args->length());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003139 VisitForStackValue(args->at(0));
3140 VisitForStackValue(args->at(1));
3141
3142 StringAddStub stub(NO_STRING_ADD_FLAGS);
3143 __ CallStub(&stub);
3144 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003145}
3146
3147
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003148void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3149 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003150 ASSERT_EQ(2, args->length());
3151
3152 VisitForStackValue(args->at(0));
3153 VisitForStackValue(args->at(1));
3154
3155 StringCompareStub stub;
3156 __ CallStub(&stub);
3157 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003158}
3159
3160
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003161void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003162 // Load the argument on the stack and call the stub.
3163 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3164 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003165 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003166 ASSERT(args->length() == 1);
3167 VisitForStackValue(args->at(0));
3168 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3169 __ CallStub(&stub);
3170 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003171}
3172
3173
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003174void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003175 // Load the argument on the stack and call the stub.
3176 TranscendentalCacheStub stub(TranscendentalCache::COS,
3177 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003178 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003179 ASSERT(args->length() == 1);
3180 VisitForStackValue(args->at(0));
3181 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3182 __ CallStub(&stub);
3183 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003184}
3185
3186
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003187void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3188 // Load the argument on the stack and call the stub.
3189 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3190 TranscendentalCacheStub::TAGGED);
3191 ZoneList<Expression*>* args = expr->arguments();
3192 ASSERT(args->length() == 1);
3193 VisitForStackValue(args->at(0));
3194 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3195 __ CallStub(&stub);
3196 context()->Plug(v0);
3197}
3198
3199
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003200void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003201 // Load the argument on the stack and call the stub.
3202 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3203 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003204 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003205 ASSERT(args->length() == 1);
3206 VisitForStackValue(args->at(0));
3207 __ mov(a0, result_register()); // Stub requires parameter in a0 and on tos.
3208 __ CallStub(&stub);
3209 context()->Plug(v0);
3210}
3211
3212
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003213void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003214 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003215 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003216 ASSERT(args->length() == 1);
3217 VisitForStackValue(args->at(0));
3218 __ CallRuntime(Runtime::kMath_sqrt, 1);
3219 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003220}
3221
3222
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003223void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3224 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003225 ASSERT(args->length() >= 2);
3226
3227 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3228 for (int i = 0; i < arg_count + 1; i++) {
3229 VisitForStackValue(args->at(i));
3230 }
3231 VisitForAccumulatorValue(args->last()); // Function.
3232
danno@chromium.orgc612e022011-11-10 11:38:15 +00003233 // Check for proxy.
3234 Label proxy, done;
3235 __ GetObjectType(v0, a1, a1);
3236 __ Branch(&proxy, eq, a1, Operand(JS_FUNCTION_PROXY_TYPE));
3237
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003238 // InvokeFunction requires the function in a1. Move it in there.
3239 __ mov(a1, result_register());
3240 ParameterCount count(arg_count);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003241 __ InvokeFunction(a1, count, CALL_FUNCTION,
3242 NullCallWrapper(), CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003243 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003244 __ jmp(&done);
3245
3246 __ bind(&proxy);
3247 __ push(v0);
3248 __ CallRuntime(Runtime::kCall, args->length());
3249 __ bind(&done);
3250
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003251 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003252}
3253
3254
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003255void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003256 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003257 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003258 ASSERT(args->length() == 3);
3259 VisitForStackValue(args->at(0));
3260 VisitForStackValue(args->at(1));
3261 VisitForStackValue(args->at(2));
3262 __ CallStub(&stub);
3263 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003264}
3265
3266
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003267void FullCodeGenerator::EmitSwapElements(CallRuntime* expr) {
3268 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003269 ASSERT(args->length() == 3);
3270 VisitForStackValue(args->at(0));
3271 VisitForStackValue(args->at(1));
3272 VisitForStackValue(args->at(2));
3273 Label done;
3274 Label slow_case;
3275 Register object = a0;
3276 Register index1 = a1;
3277 Register index2 = a2;
3278 Register elements = a3;
3279 Register scratch1 = t0;
3280 Register scratch2 = t1;
3281
3282 __ lw(object, MemOperand(sp, 2 * kPointerSize));
3283 // Fetch the map and check if array is in fast case.
3284 // Check that object doesn't require security checks and
3285 // has no indexed interceptor.
3286 __ GetObjectType(object, scratch1, scratch2);
3287 __ Branch(&slow_case, ne, scratch2, Operand(JS_ARRAY_TYPE));
3288 // Map is now in scratch1.
3289
3290 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitFieldOffset));
3291 __ And(scratch2, scratch2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
3292 __ Branch(&slow_case, ne, scratch2, Operand(zero_reg));
3293
3294 // Check the object's elements are in fast case and writable.
3295 __ lw(elements, FieldMemOperand(object, JSObject::kElementsOffset));
3296 __ lw(scratch1, FieldMemOperand(elements, HeapObject::kMapOffset));
3297 __ LoadRoot(scratch2, Heap::kFixedArrayMapRootIndex);
3298 __ Branch(&slow_case, ne, scratch1, Operand(scratch2));
3299
3300 // Check that both indices are smis.
3301 __ lw(index1, MemOperand(sp, 1 * kPointerSize));
3302 __ lw(index2, MemOperand(sp, 0));
3303 __ JumpIfNotBothSmi(index1, index2, &slow_case);
3304
3305 // Check that both indices are valid.
3306 Label not_hi;
3307 __ lw(scratch1, FieldMemOperand(object, JSArray::kLengthOffset));
3308 __ Branch(&slow_case, ls, scratch1, Operand(index1));
3309 __ Branch(&not_hi, NegateCondition(hi), scratch1, Operand(index1));
3310 __ Branch(&slow_case, ls, scratch1, Operand(index2));
3311 __ bind(&not_hi);
3312
3313 // Bring the address of the elements into index1 and index2.
3314 __ Addu(scratch1, elements,
3315 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3316 __ sll(index1, index1, kPointerSizeLog2 - kSmiTagSize);
3317 __ Addu(index1, scratch1, index1);
3318 __ sll(index2, index2, kPointerSizeLog2 - kSmiTagSize);
3319 __ Addu(index2, scratch1, index2);
3320
3321 // Swap elements.
3322 __ lw(scratch1, MemOperand(index1, 0));
3323 __ lw(scratch2, MemOperand(index2, 0));
3324 __ sw(scratch1, MemOperand(index2, 0));
3325 __ sw(scratch2, MemOperand(index1, 0));
3326
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003327 Label no_remembered_set;
3328 __ CheckPageFlag(elements,
3329 scratch1,
3330 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3331 ne,
3332 &no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003333 // Possible optimization: do a check that both values are Smis
3334 // (or them and test against Smi mask).
3335
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003336 // We are swapping two objects in an array and the incremental marker never
3337 // pauses in the middle of scanning a single object. Therefore the
3338 // incremental marker is not disturbed, so we don't need to call the
3339 // RecordWrite stub that notifies the incremental marker.
3340 __ RememberedSetHelper(elements,
3341 index1,
3342 scratch2,
3343 kDontSaveFPRegs,
3344 MacroAssembler::kFallThroughAtEnd);
3345 __ RememberedSetHelper(elements,
3346 index2,
3347 scratch2,
3348 kDontSaveFPRegs,
3349 MacroAssembler::kFallThroughAtEnd);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003350
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003351 __ bind(&no_remembered_set);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003352 // We are done. Drop elements from the stack, and return undefined.
3353 __ Drop(3);
3354 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3355 __ jmp(&done);
3356
3357 __ bind(&slow_case);
3358 __ CallRuntime(Runtime::kSwapElements, 3);
3359
3360 __ bind(&done);
3361 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003362}
3363
3364
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003365void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3366 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003367 ASSERT_EQ(2, args->length());
3368
3369 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3370 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3371
3372 Handle<FixedArray> jsfunction_result_caches(
3373 isolate()->global_context()->jsfunction_result_caches());
3374 if (jsfunction_result_caches->length() <= cache_id) {
3375 __ Abort("Attempt to use undefined cache.");
3376 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3377 context()->Plug(v0);
3378 return;
3379 }
3380
3381 VisitForAccumulatorValue(args->at(1));
3382
3383 Register key = v0;
3384 Register cache = a1;
3385 __ lw(cache, ContextOperand(cp, Context::GLOBAL_INDEX));
3386 __ lw(cache, FieldMemOperand(cache, GlobalObject::kGlobalContextOffset));
3387 __ lw(cache,
3388 ContextOperand(
3389 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
3390 __ lw(cache,
3391 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3392
3393
3394 Label done, not_found;
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003395 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003396 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
3397 // a2 now holds finger offset as a smi.
3398 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3399 // a3 now points to the start of fixed array elements.
3400 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
3401 __ addu(a3, a3, at);
3402 // a3 now points to key of indexed element of cache.
3403 __ lw(a2, MemOperand(a3));
3404 __ Branch(&not_found, ne, key, Operand(a2));
3405
3406 __ lw(v0, MemOperand(a3, kPointerSize));
3407 __ Branch(&done);
3408
3409 __ bind(&not_found);
3410 // Call runtime to perform the lookup.
3411 __ Push(cache, key);
3412 __ CallRuntime(Runtime::kGetFromCache, 2);
3413
3414 __ bind(&done);
3415 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003416}
3417
3418
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003419void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3420 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003421 ASSERT_EQ(2, args->length());
3422
3423 Register right = v0;
3424 Register left = a1;
3425 Register tmp = a2;
3426 Register tmp2 = a3;
3427
3428 VisitForStackValue(args->at(0));
3429 VisitForAccumulatorValue(args->at(1)); // Result (right) in v0.
3430 __ pop(left);
3431
3432 Label done, fail, ok;
3433 __ Branch(&ok, eq, left, Operand(right));
3434 // Fail if either is a non-HeapObject.
3435 __ And(tmp, left, Operand(right));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003436 __ JumpIfSmi(tmp, &fail);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003437 __ lw(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
3438 __ lbu(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
3439 __ Branch(&fail, ne, tmp2, Operand(JS_REGEXP_TYPE));
3440 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3441 __ Branch(&fail, ne, tmp, Operand(tmp2));
3442 __ lw(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
3443 __ lw(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
3444 __ Branch(&ok, eq, tmp, Operand(tmp2));
3445 __ bind(&fail);
3446 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3447 __ jmp(&done);
3448 __ bind(&ok);
3449 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3450 __ bind(&done);
3451
3452 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003453}
3454
3455
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003456void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3457 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003458 VisitForAccumulatorValue(args->at(0));
3459
3460 Label materialize_true, materialize_false;
3461 Label* if_true = NULL;
3462 Label* if_false = NULL;
3463 Label* fall_through = NULL;
3464 context()->PrepareTest(&materialize_true, &materialize_false,
3465 &if_true, &if_false, &fall_through);
3466
3467 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3468 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3469
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003470 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003471 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3472
3473 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003474}
3475
3476
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003477void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3478 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003479 ASSERT(args->length() == 1);
3480 VisitForAccumulatorValue(args->at(0));
3481
3482 if (FLAG_debug_code) {
3483 __ AbortIfNotString(v0);
3484 }
3485
3486 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3487 __ IndexFromHash(v0, v0);
3488
3489 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003490}
3491
3492
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003493void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003494 Label bailout, done, one_char_separator, long_separator,
3495 non_trivial_array, not_size_one_array, loop,
3496 empty_separator_loop, one_char_separator_loop,
3497 one_char_separator_loop_entry, long_separator_loop;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003498 ZoneList<Expression*>* args = expr->arguments();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003499 ASSERT(args->length() == 2);
3500 VisitForStackValue(args->at(1));
3501 VisitForAccumulatorValue(args->at(0));
3502
3503 // All aliases of the same register have disjoint lifetimes.
3504 Register array = v0;
3505 Register elements = no_reg; // Will be v0.
3506 Register result = no_reg; // Will be v0.
3507 Register separator = a1;
3508 Register array_length = a2;
3509 Register result_pos = no_reg; // Will be a2.
3510 Register string_length = a3;
3511 Register string = t0;
3512 Register element = t1;
3513 Register elements_end = t2;
3514 Register scratch1 = t3;
3515 Register scratch2 = t5;
3516 Register scratch3 = t4;
3517 Register scratch4 = v1;
3518
3519 // Separator operand is on the stack.
3520 __ pop(separator);
3521
3522 // Check that the array is a JSArray.
3523 __ JumpIfSmi(array, &bailout);
3524 __ GetObjectType(array, scratch1, scratch2);
3525 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3526
3527 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003528 __ CheckFastElements(scratch1, scratch2, &bailout);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003529
3530 // If the array has length zero, return the empty string.
3531 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3532 __ SmiUntag(array_length);
3533 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3534 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
3535 __ Branch(&done);
3536
3537 __ bind(&non_trivial_array);
3538
3539 // Get the FixedArray containing array's elements.
3540 elements = array;
3541 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3542 array = no_reg; // End of array's live range.
3543
3544 // Check that all array elements are sequential ASCII strings, and
3545 // accumulate the sum of their lengths, as a smi-encoded value.
3546 __ mov(string_length, zero_reg);
3547 __ Addu(element,
3548 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3549 __ sll(elements_end, array_length, kPointerSizeLog2);
3550 __ Addu(elements_end, element, elements_end);
3551 // Loop condition: while (element < elements_end).
3552 // Live values in registers:
3553 // elements: Fixed array of strings.
3554 // array_length: Length of the fixed array of strings (not smi)
3555 // separator: Separator string
3556 // string_length: Accumulated sum of string lengths (smi).
3557 // element: Current array element.
3558 // elements_end: Array end.
3559 if (FLAG_debug_code) {
3560 __ Assert(gt, "No empty arrays here in EmitFastAsciiArrayJoin",
3561 array_length, Operand(zero_reg));
3562 }
3563 __ bind(&loop);
3564 __ lw(string, MemOperand(element));
3565 __ Addu(element, element, kPointerSize);
3566 __ JumpIfSmi(string, &bailout);
3567 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3568 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3569 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3570 __ lw(scratch1, FieldMemOperand(string, SeqAsciiString::kLengthOffset));
3571 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
3572 __ BranchOnOverflow(&bailout, scratch3);
3573 __ Branch(&loop, lt, element, Operand(elements_end));
3574
3575 // If array_length is 1, return elements[0], a string.
3576 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3577 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3578 __ Branch(&done);
3579
3580 __ bind(&not_size_one_array);
3581
3582 // Live values in registers:
3583 // separator: Separator string
3584 // array_length: Length of the array.
3585 // string_length: Sum of string lengths (smi).
3586 // elements: FixedArray of strings.
3587
3588 // Check that the separator is a flat ASCII string.
3589 __ JumpIfSmi(separator, &bailout);
3590 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3591 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3592 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch1, scratch2, &bailout);
3593
3594 // Add (separator length times array_length) - separator length to the
3595 // string_length to get the length of the result string. array_length is not
3596 // smi but the other values are, so the result is a smi.
3597 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3598 __ Subu(string_length, string_length, Operand(scratch1));
3599 __ Mult(array_length, scratch1);
3600 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3601 // zero.
3602 __ mfhi(scratch2);
3603 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
3604 __ mflo(scratch2);
3605 __ And(scratch3, scratch2, Operand(0x80000000));
3606 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3607 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
3608 __ BranchOnOverflow(&bailout, scratch3);
3609 __ SmiUntag(string_length);
3610
3611 // Get first element in the array to free up the elements register to be used
3612 // for the result.
3613 __ Addu(element,
3614 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3615 result = elements; // End of live range for elements.
3616 elements = no_reg;
3617 // Live values in registers:
3618 // element: First array element
3619 // separator: Separator string
3620 // string_length: Length of result string (not smi)
3621 // array_length: Length of the array.
3622 __ AllocateAsciiString(result,
3623 string_length,
3624 scratch1,
3625 scratch2,
3626 elements_end,
3627 &bailout);
3628 // Prepare for looping. Set up elements_end to end of the array. Set
3629 // result_pos to the position of the result where to write the first
3630 // character.
3631 __ sll(elements_end, array_length, kPointerSizeLog2);
3632 __ Addu(elements_end, element, elements_end);
3633 result_pos = array_length; // End of live range for array_length.
3634 array_length = no_reg;
3635 __ Addu(result_pos,
3636 result,
3637 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3638
3639 // Check the length of the separator.
3640 __ lw(scratch1, FieldMemOperand(separator, SeqAsciiString::kLengthOffset));
3641 __ li(at, Operand(Smi::FromInt(1)));
3642 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3643 __ Branch(&long_separator, gt, scratch1, Operand(at));
3644
3645 // Empty separator case.
3646 __ bind(&empty_separator_loop);
3647 // Live values in registers:
3648 // result_pos: the position to which we are currently copying characters.
3649 // element: Current array element.
3650 // elements_end: Array end.
3651
3652 // Copy next array element to the result.
3653 __ lw(string, MemOperand(element));
3654 __ Addu(element, element, kPointerSize);
3655 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3656 __ SmiUntag(string_length);
3657 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3658 __ CopyBytes(string, result_pos, string_length, scratch1);
3659 // End while (element < elements_end).
3660 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3661 ASSERT(result.is(v0));
3662 __ Branch(&done);
3663
3664 // One-character separator case.
3665 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003666 // Replace separator with its ASCII character value.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003667 __ lbu(separator, FieldMemOperand(separator, SeqAsciiString::kHeaderSize));
3668 // Jump into the loop after the code that copies the separator, so the first
3669 // element is not preceded by a separator.
3670 __ jmp(&one_char_separator_loop_entry);
3671
3672 __ bind(&one_char_separator_loop);
3673 // Live values in registers:
3674 // result_pos: the position to which we are currently copying characters.
3675 // element: Current array element.
3676 // elements_end: Array end.
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003677 // separator: Single separator ASCII char (in lower byte).
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003678
3679 // Copy the separator character to the result.
3680 __ sb(separator, MemOperand(result_pos));
3681 __ Addu(result_pos, result_pos, 1);
3682
3683 // Copy next array element to the result.
3684 __ bind(&one_char_separator_loop_entry);
3685 __ lw(string, MemOperand(element));
3686 __ Addu(element, element, kPointerSize);
3687 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3688 __ SmiUntag(string_length);
3689 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3690 __ CopyBytes(string, result_pos, string_length, scratch1);
3691 // End while (element < elements_end).
3692 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3693 ASSERT(result.is(v0));
3694 __ Branch(&done);
3695
3696 // Long separator case (separator is more than one character). Entry is at the
3697 // label long_separator below.
3698 __ bind(&long_separator_loop);
3699 // Live values in registers:
3700 // result_pos: the position to which we are currently copying characters.
3701 // element: Current array element.
3702 // elements_end: Array end.
3703 // separator: Separator string.
3704
3705 // Copy the separator to the result.
3706 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3707 __ SmiUntag(string_length);
3708 __ Addu(string,
3709 separator,
3710 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3711 __ CopyBytes(string, result_pos, string_length, scratch1);
3712
3713 __ bind(&long_separator);
3714 __ lw(string, MemOperand(element));
3715 __ Addu(element, element, kPointerSize);
3716 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3717 __ SmiUntag(string_length);
3718 __ Addu(string, string, SeqAsciiString::kHeaderSize - kHeapObjectTag);
3719 __ CopyBytes(string, result_pos, string_length, scratch1);
3720 // End while (element < elements_end).
3721 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3722 ASSERT(result.is(v0));
3723 __ Branch(&done);
3724
3725 __ bind(&bailout);
3726 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3727 __ bind(&done);
3728 context()->Plug(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003729}
3730
3731
ager@chromium.org5c838252010-02-19 08:53:10 +00003732void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003733 Handle<String> name = expr->name();
3734 if (name->length() > 0 && name->Get(0) == '_') {
3735 Comment cmnt(masm_, "[ InlineRuntimeCall");
3736 EmitInlineRuntimeCall(expr);
3737 return;
3738 }
3739
3740 Comment cmnt(masm_, "[ CallRuntime");
3741 ZoneList<Expression*>* args = expr->arguments();
3742
3743 if (expr->is_jsruntime()) {
3744 // Prepare for calling JS runtime function.
3745 __ lw(a0, GlobalObjectOperand());
3746 __ lw(a0, FieldMemOperand(a0, GlobalObject::kBuiltinsOffset));
3747 __ push(a0);
3748 }
3749
3750 // Push the arguments ("left-to-right").
3751 int arg_count = args->length();
3752 for (int i = 0; i < arg_count; i++) {
3753 VisitForStackValue(args->at(i));
3754 }
3755
3756 if (expr->is_jsruntime()) {
3757 // Call the JS runtime function.
3758 __ li(a2, Operand(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003759 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003760 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00003761 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003762 __ Call(ic, mode, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003763 // Restore context register.
3764 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3765 } else {
3766 // Call the C runtime function.
3767 __ CallRuntime(expr->function(), arg_count);
3768 }
3769 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003770}
3771
3772
3773void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003774 switch (expr->op()) {
3775 case Token::DELETE: {
3776 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003777 Property* property = expr->expression()->AsProperty();
3778 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003779
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003780 if (property != NULL) {
3781 VisitForStackValue(property->obj());
3782 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003783 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
3784 ? kNonStrictMode : kStrictMode;
3785 __ li(a1, Operand(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003786 __ push(a1);
3787 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3788 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003789 } else if (proxy != NULL) {
3790 Variable* var = proxy->var();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003791 // Delete of an unqualified identifier is disallowed in strict mode
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003792 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003793 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003794 if (var->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003795 __ lw(a2, GlobalObjectOperand());
3796 __ li(a1, Operand(var->name()));
3797 __ li(a0, Operand(Smi::FromInt(kNonStrictMode)));
3798 __ Push(a2, a1, a0);
3799 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3800 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003801 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003802 // Result of deleting non-global, non-dynamic variables is false.
3803 // The subexpression does not have side effects.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00003804 context()->Plug(var->is_this());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003805 } else {
3806 // Non-global variable. Call the runtime to try to delete from the
3807 // context where the variable was introduced.
3808 __ push(context_register());
3809 __ li(a2, Operand(var->name()));
3810 __ push(a2);
3811 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3812 context()->Plug(v0);
3813 }
3814 } else {
3815 // Result of deleting non-property, non-variable reference is true.
3816 // The subexpression may have side effects.
3817 VisitForEffect(expr->expression());
3818 context()->Plug(true);
3819 }
3820 break;
3821 }
3822
3823 case Token::VOID: {
3824 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3825 VisitForEffect(expr->expression());
3826 context()->Plug(Heap::kUndefinedValueRootIndex);
3827 break;
3828 }
3829
3830 case Token::NOT: {
3831 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3832 if (context()->IsEffect()) {
3833 // Unary NOT has no side effects so it's only necessary to visit the
3834 // subexpression. Match the optimizing compiler by not branching.
3835 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003836 } else if (context()->IsTest()) {
3837 const TestContext* test = TestContext::cast(context());
3838 // The labels are swapped for the recursive call.
3839 VisitForControl(expr->expression(),
3840 test->false_label(),
3841 test->true_label(),
3842 test->fall_through());
3843 context()->Plug(test->true_label(), test->false_label());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003844 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003845 // We handle value contexts explicitly rather than simply visiting
3846 // for control and plugging the control flow into the context,
3847 // because we need to prepare a pair of extra administrative AST ids
3848 // for the optimizing compiler.
3849 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3850 Label materialize_true, materialize_false, done;
3851 VisitForControl(expr->expression(),
3852 &materialize_false,
3853 &materialize_true,
3854 &materialize_true);
3855 __ bind(&materialize_true);
3856 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3857 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
3858 if (context()->IsStackValue()) __ push(v0);
3859 __ jmp(&done);
3860 __ bind(&materialize_false);
3861 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3862 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
3863 if (context()->IsStackValue()) __ push(v0);
3864 __ bind(&done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003865 }
3866 break;
3867 }
3868
3869 case Token::TYPEOF: {
3870 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3871 { StackValueContext context(this);
3872 VisitForTypeofValue(expr->expression());
3873 }
3874 __ CallRuntime(Runtime::kTypeof, 1);
3875 context()->Plug(v0);
3876 break;
3877 }
3878
3879 case Token::ADD: {
3880 Comment cmt(masm_, "[ UnaryOperation (ADD)");
3881 VisitForAccumulatorValue(expr->expression());
3882 Label no_conversion;
3883 __ JumpIfSmi(result_register(), &no_conversion);
3884 __ mov(a0, result_register());
3885 ToNumberStub convert_stub;
3886 __ CallStub(&convert_stub);
3887 __ bind(&no_conversion);
3888 context()->Plug(result_register());
3889 break;
3890 }
3891
3892 case Token::SUB:
3893 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
3894 break;
3895
3896 case Token::BIT_NOT:
3897 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
3898 break;
3899
3900 default:
3901 UNREACHABLE();
3902 }
3903}
3904
3905
3906void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3907 const char* comment) {
3908 // TODO(svenpanne): Allowing format strings in Comment would be nice here...
3909 Comment cmt(masm_, comment);
3910 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3911 UnaryOverwriteMode overwrite =
3912 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003913 UnaryOpStub stub(expr->op(), overwrite);
3914 // GenericUnaryOpStub expects the argument to be in a0.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003915 VisitForAccumulatorValue(expr->expression());
3916 SetSourcePosition(expr->position());
3917 __ mov(a0, result_register());
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003918 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003919 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00003920}
3921
3922
3923void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003924 Comment cmnt(masm_, "[ CountOperation");
3925 SetSourcePosition(expr->position());
3926
3927 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3928 // as the left-hand side.
3929 if (!expr->expression()->IsValidLeftHandSide()) {
3930 VisitForEffect(expr->expression());
3931 return;
3932 }
3933
3934 // Expression can only be a property, a global or a (parameter or local)
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003935 // slot.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003936 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
3937 LhsKind assign_type = VARIABLE;
3938 Property* prop = expr->expression()->AsProperty();
3939 // In case of a property we use the uninitialized expression context
3940 // of the key to detect a named property.
3941 if (prop != NULL) {
3942 assign_type =
3943 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
3944 }
3945
3946 // Evaluate expression and get value.
3947 if (assign_type == VARIABLE) {
3948 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
3949 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003950 EmitVariableLoad(expr->expression()->AsVariableProxy());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003951 } else {
3952 // Reserve space for result of postfix operation.
3953 if (expr->is_postfix() && !context()->IsEffect()) {
3954 __ li(at, Operand(Smi::FromInt(0)));
3955 __ push(at);
3956 }
3957 if (assign_type == NAMED_PROPERTY) {
3958 // Put the object both on the stack and in the accumulator.
3959 VisitForAccumulatorValue(prop->obj());
3960 __ push(v0);
3961 EmitNamedPropertyLoad(prop);
3962 } else {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003963 VisitForStackValue(prop->obj());
3964 VisitForAccumulatorValue(prop->key());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003965 __ lw(a1, MemOperand(sp, 0));
3966 __ push(v0);
3967 EmitKeyedPropertyLoad(prop);
3968 }
3969 }
3970
3971 // We need a second deoptimization point after loading the value
3972 // in case evaluating the property load my have a side effect.
3973 if (assign_type == VARIABLE) {
3974 PrepareForBailout(expr->expression(), TOS_REG);
3975 } else {
3976 PrepareForBailoutForId(expr->CountId(), TOS_REG);
3977 }
3978
3979 // Call ToNumber only if operand is not a smi.
3980 Label no_conversion;
3981 __ JumpIfSmi(v0, &no_conversion);
3982 __ mov(a0, v0);
3983 ToNumberStub convert_stub;
3984 __ CallStub(&convert_stub);
3985 __ bind(&no_conversion);
3986
3987 // Save result for postfix expressions.
3988 if (expr->is_postfix()) {
3989 if (!context()->IsEffect()) {
3990 // Save the result on the stack. If we have a named or keyed property
3991 // we store the result under the receiver that is currently on top
3992 // of the stack.
3993 switch (assign_type) {
3994 case VARIABLE:
3995 __ push(v0);
3996 break;
3997 case NAMED_PROPERTY:
3998 __ sw(v0, MemOperand(sp, kPointerSize));
3999 break;
4000 case KEYED_PROPERTY:
4001 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4002 break;
4003 }
4004 }
4005 }
4006 __ mov(a0, result_register());
4007
4008 // Inline smi case if we are in a loop.
4009 Label stub_call, done;
4010 JumpPatchSite patch_site(masm_);
4011
4012 int count_value = expr->op() == Token::INC ? 1 : -1;
4013 __ li(a1, Operand(Smi::FromInt(count_value)));
4014
4015 if (ShouldInlineSmiCase(expr->op())) {
4016 __ AdduAndCheckForOverflow(v0, a0, a1, t0);
4017 __ BranchOnOverflow(&stub_call, t0); // Do stub on overflow.
4018
4019 // We could eliminate this smi check if we split the code at
4020 // the first smi check before calling ToNumber.
4021 patch_site.EmitJumpIfSmi(v0, &done);
4022 __ bind(&stub_call);
4023 }
4024
4025 // Record position before stub call.
4026 SetSourcePosition(expr->position());
4027
danno@chromium.org40cb8782011-05-25 07:58:50 +00004028 BinaryOpStub stub(Token::ADD, NO_OVERWRITE);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004029 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004030 patch_site.EmitPatchInfo();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004031 __ bind(&done);
4032
4033 // Store the value returned in v0.
4034 switch (assign_type) {
4035 case VARIABLE:
4036 if (expr->is_postfix()) {
4037 { EffectContext context(this);
4038 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4039 Token::ASSIGN);
4040 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4041 context.Plug(v0);
4042 }
4043 // For all contexts except EffectConstant we have the result on
4044 // top of the stack.
4045 if (!context()->IsEffect()) {
4046 context()->PlugTOS();
4047 }
4048 } else {
4049 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4050 Token::ASSIGN);
4051 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4052 context()->Plug(v0);
4053 }
4054 break;
4055 case NAMED_PROPERTY: {
4056 __ mov(a0, result_register()); // Value.
4057 __ li(a2, Operand(prop->key()->AsLiteral()->handle())); // Name.
4058 __ pop(a1); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004059 Handle<Code> ic = is_classic_mode()
4060 ? isolate()->builtins()->StoreIC_Initialize()
4061 : isolate()->builtins()->StoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004062 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004063 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4064 if (expr->is_postfix()) {
4065 if (!context()->IsEffect()) {
4066 context()->PlugTOS();
4067 }
4068 } else {
4069 context()->Plug(v0);
4070 }
4071 break;
4072 }
4073 case KEYED_PROPERTY: {
4074 __ mov(a0, result_register()); // Value.
4075 __ pop(a1); // Key.
4076 __ pop(a2); // Receiver.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004077 Handle<Code> ic = is_classic_mode()
4078 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4079 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004080 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004081 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4082 if (expr->is_postfix()) {
4083 if (!context()->IsEffect()) {
4084 context()->PlugTOS();
4085 }
4086 } else {
4087 context()->Plug(v0);
4088 }
4089 break;
4090 }
4091 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004092}
4093
4094
lrn@chromium.org7516f052011-03-30 08:52:27 +00004095void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004096 ASSERT(!context()->IsEffect());
4097 ASSERT(!context()->IsTest());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004098 VariableProxy* proxy = expr->AsVariableProxy();
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004099 if (proxy != NULL && proxy->var()->IsUnallocated()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004100 Comment cmnt(masm_, "Global variable");
4101 __ lw(a0, GlobalObjectOperand());
4102 __ li(a2, Operand(proxy->name()));
4103 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
4104 // Use a regular load, not a contextual load, to avoid a reference
4105 // error.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004106 __ Call(ic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004107 PrepareForBailout(expr, TOS_REG);
4108 context()->Plug(v0);
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004109 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004110 Label done, slow;
4111
4112 // Generate code for loading from variables potentially shadowed
4113 // by eval-introduced variables.
ricow@chromium.org55ee8072011-09-08 16:33:10 +00004114 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004115
4116 __ bind(&slow);
4117 __ li(a0, Operand(proxy->name()));
4118 __ Push(cp, a0);
4119 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
4120 PrepareForBailout(expr, TOS_REG);
4121 __ bind(&done);
4122
4123 context()->Plug(v0);
4124 } else {
4125 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004126 VisitInDuplicateContext(expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004127 }
ager@chromium.org5c838252010-02-19 08:53:10 +00004128}
4129
ager@chromium.org04921a82011-06-27 13:21:41 +00004130void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004131 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004132 Handle<String> check) {
4133 Label materialize_true, materialize_false;
4134 Label* if_true = NULL;
4135 Label* if_false = NULL;
4136 Label* fall_through = NULL;
4137 context()->PrepareTest(&materialize_true, &materialize_false,
4138 &if_true, &if_false, &fall_through);
4139
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004140 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004141 VisitForTypeofValue(sub_expr);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004142 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004143 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004144
4145 if (check->Equals(isolate()->heap()->number_symbol())) {
4146 __ JumpIfSmi(v0, if_true);
4147 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4148 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4149 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4150 } else if (check->Equals(isolate()->heap()->string_symbol())) {
4151 __ JumpIfSmi(v0, if_false);
4152 // Check for undetectable objects => false.
4153 __ GetObjectType(v0, v0, a1);
4154 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
4155 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4156 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4157 Split(eq, a1, Operand(zero_reg),
4158 if_true, if_false, fall_through);
4159 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4160 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4161 __ Branch(if_true, eq, v0, Operand(at));
4162 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4163 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004164 } else if (FLAG_harmony_typeof &&
4165 check->Equals(isolate()->heap()->null_symbol())) {
4166 __ LoadRoot(at, Heap::kNullValueRootIndex);
4167 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004168 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4169 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4170 __ Branch(if_true, eq, v0, Operand(at));
4171 __ JumpIfSmi(v0, if_false);
4172 // Check for undetectable objects => true.
4173 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4174 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4175 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4176 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4177 } else if (check->Equals(isolate()->heap()->function_symbol())) {
4178 __ JumpIfSmi(v0, if_false);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004179 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4180 __ GetObjectType(v0, v0, a1);
4181 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
4182 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
4183 if_true, if_false, fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004184 } else if (check->Equals(isolate()->heap()->object_symbol())) {
4185 __ JumpIfSmi(v0, if_false);
danno@chromium.orgb6451162011-08-17 14:33:23 +00004186 if (!FLAG_harmony_typeof) {
4187 __ LoadRoot(at, Heap::kNullValueRootIndex);
4188 __ Branch(if_true, eq, v0, Operand(at));
4189 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004190 // Check for JS objects => true.
4191 __ GetObjectType(v0, v0, a1);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004192 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004193 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004194 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004195 // Check for undetectable objects => false.
4196 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4197 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4198 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4199 } else {
4200 if (if_false != fall_through) __ jmp(if_false);
4201 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004202 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004203}
4204
4205
ager@chromium.org5c838252010-02-19 08:53:10 +00004206void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004207 Comment cmnt(masm_, "[ CompareOperation");
4208 SetSourcePosition(expr->position());
4209
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004210 // First we try a fast inlined version of the compare when one of
4211 // the operands is a literal.
4212 if (TryLiteralCompare(expr)) return;
4213
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004214 // Always perform the comparison for its control flow. Pack the result
4215 // into the expression's context after the comparison is performed.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004216 Label materialize_true, materialize_false;
4217 Label* if_true = NULL;
4218 Label* if_false = NULL;
4219 Label* fall_through = NULL;
4220 context()->PrepareTest(&materialize_true, &materialize_false,
4221 &if_true, &if_false, &fall_through);
4222
ager@chromium.org04921a82011-06-27 13:21:41 +00004223 Token::Value op = expr->op();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004224 VisitForStackValue(expr->left());
4225 switch (op) {
4226 case Token::IN:
4227 VisitForStackValue(expr->right());
4228 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004229 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004230 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4231 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4232 break;
4233
4234 case Token::INSTANCEOF: {
4235 VisitForStackValue(expr->right());
4236 InstanceofStub stub(InstanceofStub::kNoFlags);
4237 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004238 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004239 // The stub returns 0 for true.
4240 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
4241 break;
4242 }
4243
4244 default: {
4245 VisitForAccumulatorValue(expr->right());
4246 Condition cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004247 switch (op) {
4248 case Token::EQ_STRICT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004249 case Token::EQ:
4250 cc = eq;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004251 break;
4252 case Token::LT:
4253 cc = lt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004254 break;
4255 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004256 cc = gt;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004257 break;
4258 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004259 cc = le;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004260 break;
4261 case Token::GTE:
4262 cc = ge;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004263 break;
4264 case Token::IN:
4265 case Token::INSTANCEOF:
4266 default:
4267 UNREACHABLE();
4268 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004269 __ mov(a0, result_register());
4270 __ pop(a1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004271
4272 bool inline_smi_code = ShouldInlineSmiCase(op);
4273 JumpPatchSite patch_site(masm_);
4274 if (inline_smi_code) {
4275 Label slow_case;
4276 __ Or(a2, a0, Operand(a1));
4277 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4278 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4279 __ bind(&slow_case);
4280 }
4281 // Record position and call the compare IC.
4282 SetSourcePosition(expr->position());
4283 Handle<Code> ic = CompareIC::GetUninitialized(op);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004284 __ Call(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004285 patch_site.EmitPatchInfo();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004286 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004287 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4288 }
4289 }
4290
4291 // Convert the result of the comparison into one expected for this
4292 // expression's context.
4293 context()->Plug(if_true, if_false);
ager@chromium.org5c838252010-02-19 08:53:10 +00004294}
4295
4296
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004297void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4298 Expression* sub_expr,
4299 NilValue nil) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004300 Label materialize_true, materialize_false;
4301 Label* if_true = NULL;
4302 Label* if_false = NULL;
4303 Label* fall_through = NULL;
4304 context()->PrepareTest(&materialize_true, &materialize_false,
4305 &if_true, &if_false, &fall_through);
4306
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004307 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004308 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004309 Heap::RootListIndex nil_value = nil == kNullValue ?
4310 Heap::kNullValueRootIndex :
4311 Heap::kUndefinedValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004312 __ mov(a0, result_register());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004313 __ LoadRoot(a1, nil_value);
4314 if (expr->op() == Token::EQ_STRICT) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004315 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4316 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004317 Heap::RootListIndex other_nil_value = nil == kNullValue ?
4318 Heap::kUndefinedValueRootIndex :
4319 Heap::kNullValueRootIndex;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004320 __ Branch(if_true, eq, a0, Operand(a1));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004321 __ LoadRoot(a1, other_nil_value);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004322 __ Branch(if_true, eq, a0, Operand(a1));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004323 __ JumpIfSmi(a0, if_false);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004324 // It can be an undetectable object.
4325 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
4326 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
4327 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4328 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4329 }
4330 context()->Plug(if_true, if_false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004331}
4332
4333
ager@chromium.org5c838252010-02-19 08:53:10 +00004334void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004335 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4336 context()->Plug(v0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004337}
4338
4339
lrn@chromium.org7516f052011-03-30 08:52:27 +00004340Register FullCodeGenerator::result_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004341 return v0;
4342}
ager@chromium.org5c838252010-02-19 08:53:10 +00004343
4344
lrn@chromium.org7516f052011-03-30 08:52:27 +00004345Register FullCodeGenerator::context_register() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00004346 return cp;
4347}
4348
4349
ager@chromium.org5c838252010-02-19 08:53:10 +00004350void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004351 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4352 __ sw(value, MemOperand(fp, frame_offset));
ager@chromium.org5c838252010-02-19 08:53:10 +00004353}
4354
4355
4356void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004357 __ lw(dst, ContextOperand(cp, context_index));
ager@chromium.org5c838252010-02-19 08:53:10 +00004358}
4359
4360
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004361void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4362 Scope* declaration_scope = scope()->DeclarationScope();
4363 if (declaration_scope->is_global_scope()) {
4364 // Contexts nested in the global context have a canonical empty function
4365 // as their closure, not the anonymous closure containing the global
4366 // code. Pass a smi sentinel and let the runtime look up the empty
4367 // function.
4368 __ li(at, Operand(Smi::FromInt(0)));
4369 } else if (declaration_scope->is_eval_scope()) {
4370 // Contexts created by a call to eval have the same closure as the
4371 // context calling eval, not the anonymous closure containing the eval
4372 // code. Fetch it from the context.
4373 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
4374 } else {
4375 ASSERT(declaration_scope->is_function_scope());
4376 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4377 }
4378 __ push(at);
4379}
4380
4381
ager@chromium.org5c838252010-02-19 08:53:10 +00004382// ----------------------------------------------------------------------------
4383// Non-local control flow support.
4384
4385void FullCodeGenerator::EnterFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004386 ASSERT(!result_register().is(a1));
4387 // Store result register while executing finally block.
4388 __ push(result_register());
4389 // Cook return address in link register to stack (smi encoded Code* delta).
4390 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4391 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004392 STATIC_ASSERT(0 == kSmiTag);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004393 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4394 __ push(a1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004395}
4396
4397
4398void FullCodeGenerator::ExitFinallyBlock() {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004399 ASSERT(!result_register().is(a1));
4400 // Restore result register from stack.
4401 __ pop(a1);
4402 // Uncook return address and return.
4403 __ pop(result_register());
4404 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
4405 __ sra(a1, a1, 1); // Un-smi-tag value.
4406 __ Addu(at, a1, Operand(masm_->CodeObject()));
4407 __ Jump(at);
ager@chromium.org5c838252010-02-19 08:53:10 +00004408}
4409
4410
4411#undef __
4412
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004413#define __ ACCESS_MASM(masm())
4414
4415FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4416 int* stack_depth,
4417 int* context_length) {
4418 // The macros used here must preserve the result register.
4419
4420 // Because the handler block contains the context of the finally
4421 // code, we can restore it directly from there for the finally code
4422 // rather than iteratively unwinding contexts via their previous
4423 // links.
4424 __ Drop(*stack_depth); // Down to the handler block.
4425 if (*context_length > 0) {
4426 // Restore the context to its dedicated register and the stack.
4427 __ lw(cp, MemOperand(sp, StackHandlerConstants::kContextOffset));
4428 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4429 }
4430 __ PopTryHandler();
4431 __ Call(finally_entry_);
4432
4433 *stack_depth = 0;
4434 *context_length = 0;
4435 return previous_;
4436}
4437
4438
4439#undef __
4440
ager@chromium.org5c838252010-02-19 08:53:10 +00004441} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004442
4443#endif // V8_TARGET_ARCH_MIPS