blob: fc49cebf731f01e6428a04965162081a03c8f4b5 [file] [log] [blame]
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001// Copyright 2012 the V8 project authors. All rights reserved.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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_IA32)
31
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000032#include "code-stubs.h"
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000033#include "codegen.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000034#include "compiler.h"
35#include "debug.h"
36#include "full-codegen.h"
danno@chromium.org88aa0582012-03-23 15:11:57 +000037#include "isolate-inl.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000038#include "parser.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000039#include "scopes.h"
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000040#include "stub-cache.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000041
42namespace v8 {
43namespace internal {
44
45#define __ ACCESS_MASM(masm_)
46
danno@chromium.org40cb8782011-05-25 07:58:50 +000047
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000048class JumpPatchSite BASE_EMBEDDED {
49 public:
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +000050 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000051#ifdef DEBUG
52 info_emitted_ = false;
53#endif
54 }
55
56 ~JumpPatchSite() {
57 ASSERT(patch_site_.is_bound() == info_emitted_);
58 }
59
karlklose@chromium.org83a47282011-05-11 11:54:09 +000060 void EmitJumpIfNotSmi(Register reg,
61 Label* target,
62 Label::Distance distance = Label::kFar) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000063 __ test(reg, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +000064 EmitJump(not_carry, target, distance); // Always taken before patched.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000065 }
66
karlklose@chromium.org83a47282011-05-11 11:54:09 +000067 void EmitJumpIfSmi(Register reg,
68 Label* target,
69 Label::Distance distance = Label::kFar) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000070 __ test(reg, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +000071 EmitJump(carry, target, distance); // Never taken before patched.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000072 }
73
74 void EmitPatchInfo() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +000075 if (patch_site_.is_bound()) {
76 int delta_to_patch_site = masm_->SizeOfCodeGeneratedSince(&patch_site_);
77 ASSERT(is_int8(delta_to_patch_site));
78 __ test(eax, Immediate(delta_to_patch_site));
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000079#ifdef DEBUG
ricow@chromium.org4f693d62011-07-04 14:01:31 +000080 info_emitted_ = true;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000081#endif
ricow@chromium.org4f693d62011-07-04 14:01:31 +000082 } else {
83 __ nop(); // Signals no inlined code.
84 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000085 }
86
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000087 private:
88 // jc will be patched with jz, jnc will become jnz.
karlklose@chromium.org83a47282011-05-11 11:54:09 +000089 void EmitJump(Condition cc, Label* target, Label::Distance distance) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000090 ASSERT(!patch_site_.is_bound() && !info_emitted_);
91 ASSERT(cc == carry || cc == not_carry);
92 __ bind(&patch_site_);
karlklose@chromium.org83a47282011-05-11 11:54:09 +000093 __ j(cc, target, distance);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000094 }
95
96 MacroAssembler* masm_;
97 Label patch_site_;
98#ifdef DEBUG
99 bool info_emitted_;
100#endif
101};
102
103
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000104// Generate code for a JS function. On entry to the function the receiver
105// and arguments have been pushed on the stack left to right, with the
106// return address on top of them. The actual argument count matches the
107// formal parameter count expected by the function.
108//
109// The live registers are:
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000110// o edi: the JS function object being called (i.e. ourselves)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000111// o esi: our context
112// o ebp: our caller's frame pointer
113// o esp: stack pointer (pointing to return address)
114//
115// The function builds a JS frame. Please see JavaScriptFrameConstants in
116// frames-ia32.h for its layout.
yangguo@chromium.org56454712012-02-16 15:33:53 +0000117void FullCodeGenerator::Generate() {
118 CompilationInfo* info = info_;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000119 handler_table_ =
120 isolate()->factory()->NewFixedArray(function()->handler_count(), TENURED);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000121 profiling_counter_ = isolate()->factory()->NewJSGlobalPropertyCell(
122 Handle<Smi>(Smi::FromInt(FLAG_interrupt_budget)));
ager@chromium.org5c838252010-02-19 08:53:10 +0000123 SetFunctionPosition(function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000124 Comment cmnt(masm_, "[ function compiled by full code generator");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000125
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000126 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
127
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000128#ifdef DEBUG
129 if (strlen(FLAG_stop_at) > 0 &&
130 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
131 __ int3();
132 }
133#endif
134
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000135 // Strict mode functions and builtins need to replace the receiver
136 // with undefined when called as functions (without an explicit
137 // receiver object). ecx is zero for method calls and non-zero for
138 // function calls.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000139 if (!info->is_classic_mode() || info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000140 Label ok;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000141 __ test(ecx, ecx);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000142 __ j(zero, &ok, Label::kNear);
143 // +1 for return address.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000144 int receiver_offset = (info->scope()->num_parameters() + 1) * kPointerSize;
danno@chromium.orgc612e022011-11-10 11:38:15 +0000145 __ mov(ecx, Operand(esp, receiver_offset));
146 __ JumpIfSmi(ecx, &ok);
147 __ CmpObjectType(ecx, JS_GLOBAL_PROXY_TYPE, ecx);
148 __ j(not_equal, &ok, Label::kNear);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000149 __ mov(Operand(esp, receiver_offset),
150 Immediate(isolate()->factory()->undefined_value()));
151 __ bind(&ok);
152 }
153
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000154 // Open a frame scope to indicate that there is a frame on the stack. The
155 // MANUAL indicates that the scope shouldn't actually generate code to set up
156 // the frame (that is done below).
157 FrameScope frame_scope(masm_, StackFrame::MANUAL);
158
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000159 __ push(ebp); // Caller's frame pointer.
160 __ mov(ebp, esp);
161 __ push(esi); // Callee's context.
162 __ push(edi); // Callee's JS Function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000163
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000164 { Comment cmnt(masm_, "[ Allocate locals");
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000165 int locals_count = info->scope()->num_stack_slots();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000166 if (locals_count == 1) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000167 __ push(Immediate(isolate()->factory()->undefined_value()));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000168 } else if (locals_count > 1) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000169 __ mov(eax, Immediate(isolate()->factory()->undefined_value()));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000170 for (int i = 0; i < locals_count; i++) {
171 __ push(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000172 }
173 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000174 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000175
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000176 bool function_in_register = true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000177
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000178 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000179 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000180 if (heap_slots > 0) {
181 Comment cmnt(masm_, "[ Allocate local context");
182 // Argument to NewContext is the function, which is still in edi.
183 __ push(edi);
184 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
185 FastNewContextStub stub(heap_slots);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000186 __ CallStub(&stub);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000187 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000188 __ CallRuntime(Runtime::kNewFunctionContext, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000189 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000190 function_in_register = false;
191 // Context is returned in both eax and esi. It replaces the context
192 // passed to us. It's saved in the stack and kept live in esi.
193 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
194
195 // Copy parameters into context if necessary.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000196 int num_parameters = info->scope()->num_parameters();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000197 for (int i = 0; i < num_parameters; i++) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000198 Variable* var = scope()->parameter(i);
199 if (var->IsContextSlot()) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000200 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
201 (num_parameters - 1 - i) * kPointerSize;
202 // Load parameter from stack.
203 __ mov(eax, Operand(ebp, parameter_offset));
204 // Store it in the context.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000205 int context_offset = Context::SlotOffset(var->index());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000206 __ mov(Operand(esi, context_offset), eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000207 // Update the write barrier. This clobbers eax and ebx.
208 __ RecordWriteContextSlot(esi,
209 context_offset,
210 eax,
211 ebx,
212 kDontSaveFPRegs);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000213 }
214 }
215 }
216
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000217 Variable* arguments = scope()->arguments();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000218 if (arguments != NULL) {
219 // Function uses arguments object.
220 Comment cmnt(masm_, "[ Allocate arguments object");
221 if (function_in_register) {
222 __ push(edi);
223 } else {
224 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
225 }
226 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000227 int num_parameters = info->scope()->num_parameters();
228 int offset = num_parameters * kPointerSize;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000229 __ lea(edx,
230 Operand(ebp, StandardFrameConstants::kCallerSPOffset + offset));
231 __ push(edx);
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000232 __ push(Immediate(Smi::FromInt(num_parameters)));
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000233 // Arguments to ArgumentsAccessStub:
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000234 // function, receiver address, parameter count.
235 // The stub will rewrite receiver and parameter count if the previous
236 // stack frame was an arguments adapter frame.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000237 ArgumentsAccessStub::Type type;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000238 if (!is_classic_mode()) {
whesse@chromium.org7b260152011-06-20 15:33:18 +0000239 type = ArgumentsAccessStub::NEW_STRICT;
240 } else if (function()->has_duplicate_parameters()) {
241 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
242 } else {
243 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
244 }
245 ArgumentsAccessStub stub(type);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000246 __ CallStub(&stub);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000247
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000248 SetVar(arguments, eax, ebx, edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000249 }
250
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000251 if (FLAG_trace) {
252 __ CallRuntime(Runtime::kTraceEnter, 0);
253 }
254
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000255 // Visit the declarations and body unless there is an illegal
256 // redeclaration.
257 if (scope()->HasIllegalRedeclaration()) {
258 Comment cmnt(masm_, "[ Declarations");
259 scope()->VisitIllegalRedeclaration(this);
260
261 } else {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000262 PrepareForBailoutForId(AstNode::kFunctionEntryId, NO_REGISTERS);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000263 { Comment cmnt(masm_, "[ Declarations");
264 // For named function expressions, declare the function name as a
265 // constant.
266 if (scope()->is_function_scope() && scope()->function() != NULL) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000267 VariableDeclaration* function = scope()->function();
268 ASSERT(function->proxy()->var()->mode() == CONST ||
269 function->proxy()->var()->mode() == CONST_HARMONY);
270 ASSERT(function->proxy()->var()->location() != Variable::UNALLOCATED);
271 VisitVariableDeclaration(function);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000272 }
273 VisitDeclarations(scope()->declarations());
274 }
275
276 { Comment cmnt(masm_, "[ Stack check");
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000277 PrepareForBailoutForId(AstNode::kDeclarationsId, NO_REGISTERS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000278 Label ok;
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000279 ExternalReference stack_limit =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000280 ExternalReference::address_of_stack_limit(isolate());
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000281 __ cmp(esp, Operand::StaticVariable(stack_limit));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000282 __ j(above_equal, &ok, Label::kNear);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000283 StackCheckStub stub;
284 __ CallStub(&stub);
285 __ bind(&ok);
286 }
287
288 { Comment cmnt(masm_, "[ Body");
289 ASSERT(loop_depth() == 0);
290 VisitStatements(function()->body());
291 ASSERT(loop_depth() == 0);
292 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000293 }
294
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000295 // Always emit a 'return undefined' in case control fell off the end of
296 // the body.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000297 { Comment cmnt(masm_, "[ return <undefined>;");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000298 __ mov(eax, isolate()->factory()->undefined_value());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000299 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000300 }
301}
302
303
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000304void FullCodeGenerator::ClearAccumulator() {
305 __ Set(eax, Immediate(Smi::FromInt(0)));
306}
307
308
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000309void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
310 __ mov(ebx, Immediate(profiling_counter_));
311 __ sub(FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset),
312 Immediate(Smi::FromInt(delta)));
313}
314
315
316void FullCodeGenerator::EmitProfilingCounterReset() {
317 int reset_value = FLAG_interrupt_budget;
318 if (info_->ShouldSelfOptimize() && !FLAG_retry_self_opt) {
319 // Self-optimization is a one-off thing: if it fails, don't try again.
320 reset_value = Smi::kMaxValue;
321 }
322 __ mov(ebx, Immediate(profiling_counter_));
323 __ mov(FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset),
324 Immediate(Smi::FromInt(reset_value)));
325}
326
327
danno@chromium.org88aa0582012-03-23 15:11:57 +0000328static const int kMaxBackEdgeWeight = 127;
329static const int kBackEdgeDistanceDivisor = 100;
330
331
yangguo@chromium.org56454712012-02-16 15:33:53 +0000332void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt,
333 Label* back_edge_target) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000334 Comment cmnt(masm_, "[ Stack check");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000335 Label ok;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000336
337 if (FLAG_count_based_interrupts) {
338 int weight = 1;
339 if (FLAG_weighted_back_edges) {
340 ASSERT(back_edge_target->is_bound());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000341 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
danno@chromium.org88aa0582012-03-23 15:11:57 +0000342 weight = Min(kMaxBackEdgeWeight,
343 Max(1, distance / kBackEdgeDistanceDivisor));
yangguo@chromium.org56454712012-02-16 15:33:53 +0000344 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000345 EmitProfilingCounterDecrement(weight);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000346 __ j(positive, &ok, Label::kNear);
347 InterruptStub stub;
348 __ CallStub(&stub);
349 } else {
350 // Count based interrupts happen often enough when they are enabled
351 // that the additional stack checks are not necessary (they would
352 // only check for interrupts).
353 ExternalReference stack_limit =
354 ExternalReference::address_of_stack_limit(isolate());
355 __ cmp(esp, Operand::StaticVariable(stack_limit));
356 __ j(above_equal, &ok, Label::kNear);
357 StackCheckStub stub;
358 __ CallStub(&stub);
359 }
360
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000361 // Record a mapping of this PC offset to the OSR id. This is used to find
362 // the AST id from the unoptimized code in order to use it as a key into
363 // the deoptimization input data found in the optimized code.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000364 RecordStackCheck(stmt->OsrEntryId());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000365
366 // Loop stack checks can be patched to perform on-stack replacement. In
367 // order to decide whether or not to perform OSR we embed the loop depth
368 // in a test instruction after the call so we can extract it from the OSR
369 // builtin.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000370 ASSERT(loop_depth() > 0);
371 __ test(eax, Immediate(Min(loop_depth(), Code::kMaxLoopNestingMarker)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000372
yangguo@chromium.org56454712012-02-16 15:33:53 +0000373 if (FLAG_count_based_interrupts) {
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000374 EmitProfilingCounterReset();
yangguo@chromium.org56454712012-02-16 15:33:53 +0000375 }
376
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000377 __ bind(&ok);
378 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
379 // Record a mapping of the OSR id to this PC. This is used if the OSR
380 // entry becomes the target of a bailout. We don't expect it to be, but
381 // we want it to work if it is.
382 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000383}
384
385
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000386void FullCodeGenerator::EmitReturnSequence() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000387 Comment cmnt(masm_, "[ Return sequence");
388 if (return_label_.is_bound()) {
389 __ jmp(&return_label_);
390 } else {
391 // Common return label
392 __ bind(&return_label_);
393 if (FLAG_trace) {
394 __ push(eax);
395 __ CallRuntime(Runtime::kTraceExit, 1);
396 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000397 if (FLAG_interrupt_at_exit || FLAG_self_optimization) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000398 // Pretend that the exit is a backwards jump to the entry.
399 int weight = 1;
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000400 if (info_->ShouldSelfOptimize()) {
401 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
402 } else if (FLAG_weighted_back_edges) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000403 int distance = masm_->pc_offset();
danno@chromium.org88aa0582012-03-23 15:11:57 +0000404 weight = Min(kMaxBackEdgeWeight,
405 Max(1, distance / kBackEdgeDistanceDivisor));
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000406 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000407 EmitProfilingCounterDecrement(weight);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000408 Label ok;
409 __ j(positive, &ok, Label::kNear);
410 __ push(eax);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000411 if (info_->ShouldSelfOptimize() && FLAG_direct_self_opt) {
412 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
413 __ CallRuntime(Runtime::kOptimizeFunctionOnNextCall, 1);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000414 } else {
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000415 InterruptStub stub;
416 __ CallStub(&stub);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000417 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000418 __ pop(eax);
419 EmitProfilingCounterReset();
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000420 __ bind(&ok);
421 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000422#ifdef DEBUG
423 // Add a label for checking the size of the code used for returning.
424 Label check_exit_codesize;
425 masm_->bind(&check_exit_codesize);
426#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000427 SetSourcePosition(function()->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000428 __ RecordJSReturn();
429 // Do not use the leave instruction here because it is too short to
430 // patch with the code required by the debugger.
431 __ mov(esp, ebp);
432 __ pop(ebp);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000433
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000434 int arguments_bytes = (info_->scope()->num_parameters() + 1) * kPointerSize;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000435 __ Ret(arguments_bytes, ecx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000436#ifdef ENABLE_DEBUGGER_SUPPORT
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000437 // Check that the size of the code used for returning is large enough
438 // for the debugger's requirements.
439 ASSERT(Assembler::kJSReturnSequenceLength <=
440 masm_->SizeOfCodeGeneratedSince(&check_exit_codesize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000441#endif
442 }
443}
444
445
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000446void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
447 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000448}
449
450
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000451void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
452 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
453 codegen()->GetVar(result_register(), var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000454}
455
456
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000457void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
458 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
459 MemOperand operand = codegen()->VarOperand(var, result_register());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000460 // Memory operands can be pushed directly.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000461 __ push(operand);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000462}
463
464
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000465void FullCodeGenerator::TestContext::Plug(Variable* var) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000466 // For simplicity we always test the accumulator register.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000467 codegen()->GetVar(result_register(), var);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000468 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000469 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000470}
471
472
473void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
474 UNREACHABLE(); // Not used on IA32.
475}
476
477
478void FullCodeGenerator::AccumulatorValueContext::Plug(
479 Heap::RootListIndex index) const {
480 UNREACHABLE(); // Not used on IA32.
481}
482
483
484void FullCodeGenerator::StackValueContext::Plug(
485 Heap::RootListIndex index) const {
486 UNREACHABLE(); // Not used on IA32.
487}
488
489
490void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
491 UNREACHABLE(); // Not used on IA32.
492}
493
494
495void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
496}
497
498
499void FullCodeGenerator::AccumulatorValueContext::Plug(
500 Handle<Object> lit) const {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000501 if (lit->IsSmi()) {
502 __ SafeSet(result_register(), Immediate(lit));
503 } else {
504 __ Set(result_register(), Immediate(lit));
505 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000506}
507
508
509void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000510 if (lit->IsSmi()) {
511 __ SafePush(Immediate(lit));
512 } else {
513 __ push(Immediate(lit));
514 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000515}
516
517
518void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000519 codegen()->PrepareForBailoutBeforeSplit(condition(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000520 true,
521 true_label_,
522 false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000523 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
524 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000525 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000526 } else if (lit->IsTrue() || lit->IsJSObject()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000527 if (true_label_ != fall_through_) __ jmp(true_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000528 } else if (lit->IsString()) {
529 if (String::cast(*lit)->length() == 0) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000530 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000531 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000532 if (true_label_ != fall_through_) __ jmp(true_label_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000533 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000534 } else if (lit->IsSmi()) {
535 if (Smi::cast(*lit)->value() == 0) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000536 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000537 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000538 if (true_label_ != fall_through_) __ jmp(true_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000539 }
540 } else {
541 // For simplicity we always test the accumulator register.
542 __ mov(result_register(), lit);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000543 codegen()->DoTest(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000544 }
545}
546
547
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000548void FullCodeGenerator::EffectContext::DropAndPlug(int count,
549 Register reg) const {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000550 ASSERT(count > 0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000551 __ Drop(count);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000552}
553
554
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000555void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
556 int count,
557 Register reg) const {
558 ASSERT(count > 0);
559 __ Drop(count);
560 __ Move(result_register(), reg);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000561}
562
563
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000564void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
565 Register reg) const {
566 ASSERT(count > 0);
567 if (count > 1) __ Drop(count - 1);
568 __ mov(Operand(esp, 0), reg);
569}
570
571
572void FullCodeGenerator::TestContext::DropAndPlug(int count,
573 Register reg) const {
574 ASSERT(count > 0);
575 // For simplicity we always test the accumulator register.
576 __ Drop(count);
577 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000578 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000579 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000580}
581
582
583void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
584 Label* materialize_false) const {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000585 ASSERT(materialize_true == materialize_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000586 __ bind(materialize_true);
587}
588
589
590void FullCodeGenerator::AccumulatorValueContext::Plug(
591 Label* materialize_true,
592 Label* materialize_false) const {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000593 Label done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000594 __ bind(materialize_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000595 __ mov(result_register(), isolate()->factory()->true_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000596 __ jmp(&done, Label::kNear);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000597 __ bind(materialize_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000598 __ mov(result_register(), isolate()->factory()->false_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000599 __ bind(&done);
600}
601
602
603void FullCodeGenerator::StackValueContext::Plug(
604 Label* materialize_true,
605 Label* materialize_false) const {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000606 Label done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000607 __ bind(materialize_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000608 __ push(Immediate(isolate()->factory()->true_value()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000609 __ jmp(&done, Label::kNear);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000610 __ bind(materialize_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000611 __ push(Immediate(isolate()->factory()->false_value()));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000612 __ bind(&done);
613}
614
615
616void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
617 Label* materialize_false) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000618 ASSERT(materialize_true == true_label_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000619 ASSERT(materialize_false == false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000620}
621
622
623void FullCodeGenerator::EffectContext::Plug(bool flag) const {
624}
625
626
627void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000628 Handle<Object> value = flag
629 ? isolate()->factory()->true_value()
630 : isolate()->factory()->false_value();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000631 __ mov(result_register(), value);
632}
633
634
635void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000636 Handle<Object> value = flag
637 ? isolate()->factory()->true_value()
638 : isolate()->factory()->false_value();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000639 __ push(Immediate(value));
640}
641
642
643void FullCodeGenerator::TestContext::Plug(bool flag) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000644 codegen()->PrepareForBailoutBeforeSplit(condition(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000645 true,
646 true_label_,
647 false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000648 if (flag) {
649 if (true_label_ != fall_through_) __ jmp(true_label_);
650 } else {
651 if (false_label_ != fall_through_) __ jmp(false_label_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000652 }
653}
654
655
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000656void FullCodeGenerator::DoTest(Expression* condition,
657 Label* if_true,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000658 Label* if_false,
659 Label* fall_through) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000660 ToBooleanStub stub(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000661 __ push(result_register());
ricow@chromium.org2c99e282011-07-28 09:15:17 +0000662 __ CallStub(&stub, condition->test_id());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000663 __ test(result_register(), result_register());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000664 // The stub returns nonzero for true.
665 Split(not_zero, if_true, if_false, fall_through);
666}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000667
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000668
ricow@chromium.org65fae842010-08-25 15:26:24 +0000669void FullCodeGenerator::Split(Condition cc,
670 Label* if_true,
671 Label* if_false,
672 Label* fall_through) {
673 if (if_false == fall_through) {
674 __ j(cc, if_true);
675 } else if (if_true == fall_through) {
676 __ j(NegateCondition(cc), if_false);
677 } else {
678 __ j(cc, if_true);
679 __ jmp(if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000680 }
681}
682
683
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000684MemOperand FullCodeGenerator::StackOperand(Variable* var) {
685 ASSERT(var->IsStackAllocated());
686 // Offset is negative because higher indexes are at lower addresses.
687 int offset = -var->index() * kPointerSize;
688 // Adjust by a (parameter or local) base offset.
689 if (var->IsParameter()) {
690 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
691 } else {
692 offset += JavaScriptFrameConstants::kLocal0Offset;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000693 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000694 return Operand(ebp, offset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000695}
696
697
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000698MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
699 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
700 if (var->IsContextSlot()) {
701 int context_chain_length = scope()->ContextChainLength(var->scope());
702 __ LoadContext(scratch, context_chain_length);
703 return ContextOperand(scratch, var->index());
704 } else {
705 return StackOperand(var);
706 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000707}
708
709
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000710void FullCodeGenerator::GetVar(Register dest, Variable* var) {
711 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
712 MemOperand location = VarOperand(var, dest);
713 __ mov(dest, location);
714}
715
716
717void FullCodeGenerator::SetVar(Variable* var,
718 Register src,
719 Register scratch0,
720 Register scratch1) {
721 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
722 ASSERT(!scratch0.is(src));
723 ASSERT(!scratch0.is(scratch1));
724 ASSERT(!scratch1.is(src));
725 MemOperand location = VarOperand(var, scratch0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000726 __ mov(location, src);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000727
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000728 // Emit the write barrier code if the location is in the heap.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000729 if (var->IsContextSlot()) {
730 int offset = Context::SlotOffset(var->index());
731 ASSERT(!scratch0.is(esi) && !src.is(esi) && !scratch1.is(esi));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000732 __ RecordWriteContextSlot(scratch0, offset, src, scratch1, kDontSaveFPRegs);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000733 }
734}
735
736
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000737void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000738 bool should_normalize,
739 Label* if_true,
740 Label* if_false) {
741 // Only prepare for bailouts before splits if we're in a test
742 // context. Otherwise, we let the Visit function deal with the
743 // preparation to avoid preparing with the same AST id twice.
744 if (!context()->IsTest() || !info_->IsOptimizable()) return;
745
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000746 Label skip;
747 if (should_normalize) __ jmp(&skip, Label::kNear);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000748 PrepareForBailout(expr, TOS_REG);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000749 if (should_normalize) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000750 __ cmp(eax, isolate()->factory()->true_value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000751 Split(equal, if_true, if_false, NULL);
752 __ bind(&skip);
753 }
754}
755
756
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000757void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
758 // The variable in the declaration always resides in the current function
759 // context.
760 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
761 if (FLAG_debug_code) {
762 // Check that we're not inside a with or catch context.
763 __ mov(ebx, FieldOperand(esi, HeapObject::kMapOffset));
764 __ cmp(ebx, isolate()->factory()->with_context_map());
765 __ Check(not_equal, "Declaration in with context.");
766 __ cmp(ebx, isolate()->factory()->catch_context_map());
767 __ Check(not_equal, "Declaration in catch context.");
768 }
769}
770
771
772void FullCodeGenerator::VisitVariableDeclaration(
773 VariableDeclaration* declaration) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000774 // If it was not possible to allocate the variable at compile time, we
775 // need to "declare" it at runtime to make sure it actually exists in the
776 // local context.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000777 VariableProxy* proxy = declaration->proxy();
778 VariableMode mode = declaration->mode();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000779 Variable* variable = proxy->var();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000780 bool hole_init = mode == CONST || mode == CONST_HARMONY || mode == LET;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000781 switch (variable->location()) {
782 case Variable::UNALLOCATED:
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000783 globals_->Add(variable->name(), zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000784 globals_->Add(variable->binding_needs_init()
785 ? isolate()->factory()->the_hole_value()
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000786 : isolate()->factory()->undefined_value(), zone());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000787 break;
788
789 case Variable::PARAMETER:
790 case Variable::LOCAL:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000791 if (hole_init) {
792 Comment cmnt(masm_, "[ VariableDeclaration");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000793 __ mov(StackOperand(variable),
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000794 Immediate(isolate()->factory()->the_hole_value()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000795 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000796 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000797
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000798 case Variable::CONTEXT:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000799 if (hole_init) {
800 Comment cmnt(masm_, "[ VariableDeclaration");
801 EmitDebugCheckDeclarationContext(variable);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000802 __ mov(ContextOperand(esi, variable->index()),
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000803 Immediate(isolate()->factory()->the_hole_value()));
804 // No write barrier since the hole value is in old space.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000805 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000806 }
807 break;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000808
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000809 case Variable::LOOKUP: {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000810 Comment cmnt(masm_, "[ VariableDeclaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000811 __ push(esi);
812 __ push(Immediate(variable->name()));
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000813 // VariableDeclaration nodes are always introduced in one of four modes.
814 ASSERT(mode == VAR || mode == LET ||
815 mode == CONST || mode == CONST_HARMONY);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000816 PropertyAttributes attr = (mode == CONST || mode == CONST_HARMONY)
817 ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000818 __ push(Immediate(Smi::FromInt(attr)));
819 // Push initial value, if any.
820 // Note: For variables we must not push an initial value (such as
821 // 'undefined') because we may have a (legal) redeclaration and we
822 // must not destroy the current value.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000823 if (hole_init) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000824 __ push(Immediate(isolate()->factory()->the_hole_value()));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000825 } else {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000826 __ push(Immediate(Smi::FromInt(0))); // Indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000827 }
828 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000829 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000830 }
831 }
832}
833
834
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000835void FullCodeGenerator::VisitFunctionDeclaration(
836 FunctionDeclaration* declaration) {
837 VariableProxy* proxy = declaration->proxy();
838 Variable* variable = proxy->var();
839 switch (variable->location()) {
840 case Variable::UNALLOCATED: {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000841 globals_->Add(variable->name(), zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000842 Handle<SharedFunctionInfo> function =
843 Compiler::BuildFunctionInfo(declaration->fun(), script());
844 // Check for stack-overflow exception.
845 if (function.is_null()) return SetStackOverflow();
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000846 globals_->Add(function, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000847 break;
848 }
849
850 case Variable::PARAMETER:
851 case Variable::LOCAL: {
852 Comment cmnt(masm_, "[ FunctionDeclaration");
853 VisitForAccumulatorValue(declaration->fun());
854 __ mov(StackOperand(variable), result_register());
855 break;
856 }
857
858 case Variable::CONTEXT: {
859 Comment cmnt(masm_, "[ FunctionDeclaration");
860 EmitDebugCheckDeclarationContext(variable);
861 VisitForAccumulatorValue(declaration->fun());
862 __ mov(ContextOperand(esi, variable->index()), result_register());
863 // We know that we have written a function, which is not a smi.
864 __ RecordWriteContextSlot(esi,
865 Context::SlotOffset(variable->index()),
866 result_register(),
867 ecx,
868 kDontSaveFPRegs,
869 EMIT_REMEMBERED_SET,
870 OMIT_SMI_CHECK);
871 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
872 break;
873 }
874
875 case Variable::LOOKUP: {
876 Comment cmnt(masm_, "[ FunctionDeclaration");
877 __ push(esi);
878 __ push(Immediate(variable->name()));
879 __ push(Immediate(Smi::FromInt(NONE)));
880 VisitForStackValue(declaration->fun());
881 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
882 break;
883 }
884 }
885}
886
887
888void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* declaration) {
889 VariableProxy* proxy = declaration->proxy();
890 Variable* variable = proxy->var();
891 Handle<JSModule> instance = declaration->module()->interface()->Instance();
892 ASSERT(!instance.is_null());
893
894 switch (variable->location()) {
895 case Variable::UNALLOCATED: {
896 Comment cmnt(masm_, "[ ModuleDeclaration");
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000897 globals_->Add(variable->name(), zone());
898 globals_->Add(instance, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000899 Visit(declaration->module());
900 break;
901 }
902
903 case Variable::CONTEXT: {
904 Comment cmnt(masm_, "[ ModuleDeclaration");
905 EmitDebugCheckDeclarationContext(variable);
906 __ mov(ContextOperand(esi, variable->index()), Immediate(instance));
907 Visit(declaration->module());
908 break;
909 }
910
911 case Variable::PARAMETER:
912 case Variable::LOCAL:
913 case Variable::LOOKUP:
914 UNREACHABLE();
915 }
916}
917
918
919void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* declaration) {
920 VariableProxy* proxy = declaration->proxy();
921 Variable* variable = proxy->var();
922 switch (variable->location()) {
923 case Variable::UNALLOCATED:
924 // TODO(rossberg)
925 break;
926
927 case Variable::CONTEXT: {
928 Comment cmnt(masm_, "[ ImportDeclaration");
929 EmitDebugCheckDeclarationContext(variable);
930 // TODO(rossberg)
931 break;
932 }
933
934 case Variable::PARAMETER:
935 case Variable::LOCAL:
936 case Variable::LOOKUP:
937 UNREACHABLE();
938 }
939}
940
941
942void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* declaration) {
943 // TODO(rossberg)
944}
945
946
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000947void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
948 // Call the runtime to declare the globals.
949 __ push(esi); // The context is the first argument.
950 __ push(Immediate(pairs));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000951 __ push(Immediate(Smi::FromInt(DeclareGlobalsFlags())));
952 __ CallRuntime(Runtime::kDeclareGlobals, 3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000953 // Return value is ignored.
954}
955
956
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000957void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
958 Comment cmnt(masm_, "[ SwitchStatement");
959 Breakable nested_statement(this, stmt);
960 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000961
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000962 // Keep the switch value on the stack until a case matches.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000963 VisitForStackValue(stmt->tag());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000964 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000965
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000966 ZoneList<CaseClause*>* clauses = stmt->cases();
967 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000968
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000969 Label next_test; // Recycled for each test.
970 // Compile all the tests with branches to their bodies.
971 for (int i = 0; i < clauses->length(); i++) {
972 CaseClause* clause = clauses->at(i);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000973 clause->body_target()->Unuse();
fschneider@chromium.orgd2187832011-01-26 15:44:20 +0000974
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000975 // The default is not a test, but remember it as final fall through.
976 if (clause->is_default()) {
977 default_clause = clause;
978 continue;
979 }
980
981 Comment cmnt(masm_, "[ Case comparison");
982 __ bind(&next_test);
983 next_test.Unuse();
984
985 // Compile the label expression.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000986 VisitForAccumulatorValue(clause->label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000987
ricow@chromium.org65fae842010-08-25 15:26:24 +0000988 // Perform the comparison as if via '==='.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000989 __ mov(edx, Operand(esp, 0)); // Switch value.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000990 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000991 JumpPatchSite patch_site(masm_);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000992 if (inline_smi_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000993 Label slow_case;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000994 __ mov(ecx, edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000995 __ or_(ecx, eax);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000996 patch_site.EmitJumpIfNotSmi(ecx, &slow_case, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000997
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000998 __ cmp(edx, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000999 __ j(not_equal, &next_test);
1000 __ Drop(1); // Switch value is no longer needed.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001001 __ jmp(clause->body_target());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001002 __ bind(&slow_case);
1003 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001004
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001005 // Record position before stub call for type feedback.
1006 SetSourcePosition(clause->position());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001007 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001008 CallIC(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001009 patch_site.EmitPatchInfo();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001010 __ test(eax, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001011 __ j(not_equal, &next_test);
1012 __ Drop(1); // Switch value is no longer needed.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001013 __ jmp(clause->body_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001014 }
1015
1016 // Discard the test value and jump to the default if present, otherwise to
1017 // the end of the statement.
1018 __ bind(&next_test);
1019 __ Drop(1); // Switch value is no longer needed.
1020 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001021 __ jmp(nested_statement.break_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001022 } else {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001023 __ jmp(default_clause->body_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001024 }
1025
1026 // Compile all the case bodies.
1027 for (int i = 0; i < clauses->length(); i++) {
1028 Comment cmnt(masm_, "[ Case body");
1029 CaseClause* clause = clauses->at(i);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001030 __ bind(clause->body_target());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001031 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001032 VisitStatements(clause->statements());
1033 }
1034
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001035 __ bind(nested_statement.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001036 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001037}
1038
1039
1040void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1041 Comment cmnt(masm_, "[ ForInStatement");
1042 SetStatementPosition(stmt);
1043
1044 Label loop, exit;
1045 ForIn loop_statement(this, stmt);
1046 increment_loop_depth();
1047
1048 // Get the object to enumerate over. Both SpiderMonkey and JSC
1049 // ignore null and undefined in contrast to the specification; see
1050 // ECMA-262 section 12.6.4.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001051 VisitForAccumulatorValue(stmt->enumerable());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001052 __ cmp(eax, isolate()->factory()->undefined_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001053 __ j(equal, &exit);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001054 __ cmp(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001055 __ j(equal, &exit);
1056
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001057 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1058
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001059 // Convert the object to a JS object.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001060 Label convert, done_convert;
whesse@chromium.org7b260152011-06-20 15:33:18 +00001061 __ JumpIfSmi(eax, &convert, Label::kNear);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001062 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001063 __ j(above_equal, &done_convert, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001064 __ bind(&convert);
1065 __ push(eax);
1066 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1067 __ bind(&done_convert);
1068 __ push(eax);
1069
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001070 // Check for proxies.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001071 Label call_runtime, use_cache, fixed_array;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001072 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1073 __ CmpObjectType(eax, LAST_JS_PROXY_TYPE, ecx);
1074 __ j(below_equal, &call_runtime);
1075
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001076 // Check cache validity in generated code. This is a fast case for
1077 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1078 // guarantee cache validity, call the runtime system to check cache
1079 // validity or get the property names in a fixed array.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001080 __ CheckEnumCache(&call_runtime);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001081
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001082 __ mov(eax, FieldOperand(eax, HeapObject::kMapOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001083 __ jmp(&use_cache, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001084
1085 // Get the set of properties to enumerate.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001086 __ bind(&call_runtime);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001087 __ push(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001088 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001089 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
1090 isolate()->factory()->meta_map());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001091 __ j(not_equal, &fixed_array);
1092
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001093
1094 // We got a map in register eax. Get the enumeration cache from it.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001095 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001096 __ LoadInstanceDescriptors(eax, ecx);
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00001097 __ mov(ecx, FieldOperand(ecx, DescriptorArray::kLastAddedOffset));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001098 __ mov(edx, FieldOperand(ecx, DescriptorArray::kEnumCacheBridgeCacheOffset));
1099
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001100 // Set up the four remaining stack slots.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001101 __ push(eax); // Map.
1102 __ push(edx); // Enumeration cache.
1103 __ mov(eax, FieldOperand(edx, FixedArray::kLengthOffset));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001104 __ push(eax); // Enumeration cache length (as smi).
1105 __ push(Immediate(Smi::FromInt(0))); // Initial index.
1106 __ jmp(&loop);
1107
1108 // We got a fixed array in register eax. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001109 Label non_proxy;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001110 __ bind(&fixed_array);
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001111
1112 Handle<JSGlobalPropertyCell> cell =
1113 isolate()->factory()->NewJSGlobalPropertyCell(
1114 Handle<Object>(
1115 Smi::FromInt(TypeFeedbackCells::kForInFastCaseMarker)));
1116 RecordTypeFeedbackCell(stmt->PrepareId(), cell);
1117 __ LoadHeapObject(ebx, cell);
1118 __ mov(FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset),
1119 Immediate(Smi::FromInt(TypeFeedbackCells::kForInSlowCaseMarker)));
1120
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001121 __ mov(ebx, Immediate(Smi::FromInt(1))); // Smi indicates slow check
1122 __ mov(ecx, Operand(esp, 0 * kPointerSize)); // Get enumerated object
1123 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1124 __ CmpObjectType(ecx, LAST_JS_PROXY_TYPE, ecx);
1125 __ j(above, &non_proxy);
1126 __ mov(ebx, Immediate(Smi::FromInt(0))); // Zero indicates proxy
1127 __ bind(&non_proxy);
1128 __ push(ebx); // Smi
1129 __ push(eax); // Array
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001130 __ mov(eax, FieldOperand(eax, FixedArray::kLengthOffset));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001131 __ push(eax); // Fixed array length (as smi).
1132 __ push(Immediate(Smi::FromInt(0))); // Initial index.
1133
1134 // Generate code for doing the condition check.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001135 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001136 __ bind(&loop);
1137 __ mov(eax, Operand(esp, 0 * kPointerSize)); // Get the current index.
1138 __ cmp(eax, Operand(esp, 1 * kPointerSize)); // Compare to the array length.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001139 __ j(above_equal, loop_statement.break_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001140
1141 // Get the current entry of the array into register ebx.
1142 __ mov(ebx, Operand(esp, 2 * kPointerSize));
1143 __ mov(ebx, FieldOperand(ebx, eax, times_2, FixedArray::kHeaderSize));
1144
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001145 // Get the expected map from the stack or a smi in the
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001146 // permanent slow case into register edx.
1147 __ mov(edx, Operand(esp, 3 * kPointerSize));
1148
1149 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001150 // If not, we may have to filter the key.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001151 Label update_each;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001152 __ mov(ecx, Operand(esp, 4 * kPointerSize));
1153 __ cmp(edx, FieldOperand(ecx, HeapObject::kMapOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001154 __ j(equal, &update_each, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001155
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001156 // For proxies, no filtering is done.
1157 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1158 ASSERT(Smi::FromInt(0) == 0);
1159 __ test(edx, edx);
1160 __ j(zero, &update_each);
1161
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001162 // Convert the entry to a string or null if it isn't a property
1163 // anymore. If the property has been removed while iterating, we
1164 // just skip it.
1165 __ push(ecx); // Enumerable.
1166 __ push(ebx); // Current entry.
1167 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001168 __ test(eax, eax);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001169 __ j(equal, loop_statement.continue_label());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001170 __ mov(ebx, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001171
1172 // Update the 'each' property or variable from the possibly filtered
1173 // entry in register ebx.
1174 __ bind(&update_each);
1175 __ mov(result_register(), ebx);
1176 // Perform the assignment as if via '='.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001177 { EffectContext context(this);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001178 EmitAssignment(stmt->each());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001179 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001180
1181 // Generate code for the body of the loop.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001182 Visit(stmt->body());
1183
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001184 // Generate code for going to the next element by incrementing the
1185 // index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001186 __ bind(loop_statement.continue_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001187 __ add(Operand(esp, 0 * kPointerSize), Immediate(Smi::FromInt(1)));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001188
yangguo@chromium.org56454712012-02-16 15:33:53 +00001189 EmitStackCheck(stmt, &loop);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001190 __ jmp(&loop);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001191
1192 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001193 __ bind(loop_statement.break_label());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001194 __ add(esp, Immediate(5 * kPointerSize));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001195
1196 // Exit and decrement the loop depth.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001197 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001198 __ bind(&exit);
1199 decrement_loop_depth();
1200}
1201
1202
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001203void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1204 bool pretenure) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001205 // Use the fast case closure allocation code that allocates in new
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001206 // space for nested functions that don't need literals cloning. If
1207 // we're running with the --always-opt or the --prepare-always-opt
1208 // flag, we need to use the runtime function so that the new function
1209 // we are creating here gets a chance to have its code optimized and
1210 // doesn't just get a copy of the existing unoptimized code.
1211 if (!FLAG_always_opt &&
1212 !FLAG_prepare_always_opt &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001213 !pretenure &&
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001214 scope()->is_function_scope() &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001215 info->num_literals() == 0) {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001216 FastNewClosureStub stub(info->language_mode());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001217 __ push(Immediate(info));
1218 __ CallStub(&stub);
1219 } else {
1220 __ push(esi);
1221 __ push(Immediate(info));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001222 __ push(Immediate(pretenure
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001223 ? isolate()->factory()->true_value()
1224 : isolate()->factory()->false_value()));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001225 __ CallRuntime(Runtime::kNewClosure, 3);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001226 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001227 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001228}
1229
1230
1231void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
1232 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001233 EmitVariableLoad(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001234}
1235
1236
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001237void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1238 TypeofState typeof_state,
1239 Label* slow) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001240 Register context = esi;
1241 Register temp = edx;
1242
1243 Scope* s = scope();
1244 while (s != NULL) {
1245 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001246 if (s->calls_non_strict_eval()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001247 // Check that extension is NULL.
1248 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX),
1249 Immediate(0));
1250 __ j(not_equal, slow);
1251 }
1252 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001253 __ mov(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001254 // Walk the rest of the chain without clobbering esi.
1255 context = temp;
1256 }
1257 // If no outer scope calls eval, we do not need to check more
1258 // context extensions. If we have reached an eval scope, we check
1259 // all extensions from this point.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001260 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001261 s = s->outer_scope();
1262 }
1263
1264 if (s != NULL && s->is_eval_scope()) {
1265 // Loop up the context chain. There is no frame effect so it is
1266 // safe to use raw labels here.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001267 Label next, fast;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001268 if (!context.is(temp)) {
1269 __ mov(temp, context);
1270 }
1271 __ bind(&next);
1272 // Terminate at global context.
1273 __ cmp(FieldOperand(temp, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001274 Immediate(isolate()->factory()->global_context_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001275 __ j(equal, &fast, Label::kNear);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001276 // Check that extension is NULL.
1277 __ cmp(ContextOperand(temp, Context::EXTENSION_INDEX), Immediate(0));
1278 __ j(not_equal, slow);
1279 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001280 __ mov(temp, ContextOperand(temp, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001281 __ jmp(&next);
1282 __ bind(&fast);
1283 }
1284
1285 // All extension objects were empty and it is safe to use a global
1286 // load IC call.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001287 __ mov(edx, GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001288 __ mov(ecx, var->name());
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001289 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001290 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1291 ? RelocInfo::CODE_TARGET
1292 : RelocInfo::CODE_TARGET_CONTEXT;
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001293 CallIC(ic, mode);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001294}
1295
1296
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001297MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1298 Label* slow) {
1299 ASSERT(var->IsContextSlot());
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001300 Register context = esi;
1301 Register temp = ebx;
1302
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001303 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001304 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001305 if (s->calls_non_strict_eval()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001306 // Check that extension is NULL.
1307 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX),
1308 Immediate(0));
1309 __ j(not_equal, slow);
1310 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001311 __ mov(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001312 // Walk the rest of the chain without clobbering esi.
1313 context = temp;
1314 }
1315 }
1316 // Check that last extension is NULL.
1317 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX), Immediate(0));
1318 __ j(not_equal, slow);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001319
1320 // This function is used only for loads, not stores, so it's safe to
1321 // return an esi-based operand (the write barrier cannot be allowed to
1322 // destroy the esi register).
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001323 return ContextOperand(context, var->index());
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001324}
1325
1326
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001327void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1328 TypeofState typeof_state,
1329 Label* slow,
1330 Label* done) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001331 // Generate fast-case code for variables that might be shadowed by
1332 // eval-introduced variables. Eval is used a lot without
1333 // introducing variables. In those cases, we do not want to
1334 // perform a runtime call for all variables in the scope
1335 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001336 if (var->mode() == DYNAMIC_GLOBAL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001337 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001338 __ jmp(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001339 } else if (var->mode() == DYNAMIC_LOCAL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001340 Variable* local = var->local_if_not_shadowed();
1341 __ mov(eax, ContextSlotOperandCheckExtensions(local, slow));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001342 if (local->mode() == CONST ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001343 local->mode() == CONST_HARMONY ||
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001344 local->mode() == LET) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001345 __ cmp(eax, isolate()->factory()->the_hole_value());
1346 __ j(not_equal, done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001347 if (local->mode() == CONST) {
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001348 __ mov(eax, isolate()->factory()->undefined_value());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001349 } else { // LET || CONST_HARMONY
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001350 __ push(Immediate(var->name()));
1351 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1352 }
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001353 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001354 __ jmp(done);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001355 }
1356}
1357
1358
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001359void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1360 // Record position before possible IC call.
1361 SetSourcePosition(proxy->position());
1362 Variable* var = proxy->var();
1363
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001364 // Three cases: global variables, lookup variables, and all other types of
1365 // variables.
1366 switch (var->location()) {
1367 case Variable::UNALLOCATED: {
1368 Comment cmnt(masm_, "Global variable");
1369 // Use inline caching. Variable name is passed in ecx and the global
1370 // object in eax.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001371 __ mov(edx, GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001372 __ mov(ecx, var->name());
1373 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001374 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001375 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001376 break;
1377 }
1378
1379 case Variable::PARAMETER:
1380 case Variable::LOCAL:
1381 case Variable::CONTEXT: {
1382 Comment cmnt(masm_, var->IsContextSlot()
1383 ? "Context variable"
1384 : "Stack variable");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001385 if (var->binding_needs_init()) {
1386 // var->scope() may be NULL when the proxy is located in eval code and
1387 // refers to a potential outside binding. Currently those bindings are
1388 // always looked up dynamically, i.e. in that case
1389 // var->location() == LOOKUP.
1390 // always holds.
1391 ASSERT(var->scope() != NULL);
1392
1393 // Check if the binding really needs an initialization check. The check
1394 // can be skipped in the following situation: we have a LET or CONST
1395 // binding in harmony mode, both the Variable and the VariableProxy have
1396 // the same declaration scope (i.e. they are both in global code, in the
1397 // same function or in the same eval code) and the VariableProxy is in
1398 // the source physically located after the initializer of the variable.
1399 //
1400 // We cannot skip any initialization checks for CONST in non-harmony
1401 // mode because const variables may be declared but never initialized:
1402 // if (false) { const x; }; var y = x;
1403 //
1404 // The condition on the declaration scopes is a conservative check for
1405 // nested functions that access a binding and are called before the
1406 // binding is initialized:
1407 // function() { f(); let x = 1; function f() { x = 2; } }
1408 //
1409 bool skip_init_check;
1410 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1411 skip_init_check = false;
jkummerow@chromium.orgac45fed2011-11-07 13:11:02 +00001412 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001413 // Check that we always have valid source position.
1414 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1415 ASSERT(proxy->position() != RelocInfo::kNoPosition);
1416 skip_init_check = var->mode() != CONST &&
1417 var->initializer_position() < proxy->position();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001418 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001419
1420 if (!skip_init_check) {
1421 // Let and const need a read barrier.
1422 Label done;
1423 GetVar(eax, var);
1424 __ cmp(eax, isolate()->factory()->the_hole_value());
1425 __ j(not_equal, &done, Label::kNear);
1426 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1427 // Throw a reference error when using an uninitialized let/const
1428 // binding in harmony mode.
1429 __ push(Immediate(var->name()));
1430 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1431 } else {
1432 // Uninitalized const bindings outside of harmony mode are unholed.
1433 ASSERT(var->mode() == CONST);
1434 __ mov(eax, isolate()->factory()->undefined_value());
1435 }
1436 __ bind(&done);
1437 context()->Plug(eax);
1438 break;
1439 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001440 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001441 context()->Plug(var);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001442 break;
1443 }
1444
1445 case Variable::LOOKUP: {
1446 Label done, slow;
1447 // Generate code for loading from variables potentially shadowed
1448 // by eval-introduced variables.
1449 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1450 __ bind(&slow);
1451 Comment cmnt(masm_, "Lookup variable");
1452 __ push(esi); // Context.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001453 __ push(Immediate(var->name()));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001454 __ CallRuntime(Runtime::kLoadContextSlot, 2);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001455 __ bind(&done);
1456 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001457 break;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001458 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001459 }
1460}
1461
1462
1463void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1464 Comment cmnt(masm_, "[ RegExpLiteral");
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001465 Label materialized;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001466 // Registers will be used as follows:
1467 // edi = JS function.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001468 // ecx = literals array.
1469 // ebx = regexp literal.
1470 // eax = regexp literal clone.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001471 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001472 __ mov(ecx, FieldOperand(edi, JSFunction::kLiteralsOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001473 int literal_offset =
ricow@chromium.org65fae842010-08-25 15:26:24 +00001474 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001475 __ mov(ebx, FieldOperand(ecx, literal_offset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001476 __ cmp(ebx, isolate()->factory()->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001477 __ j(not_equal, &materialized, Label::kNear);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001478
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001479 // Create regexp literal using runtime function
1480 // Result will be in eax.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001481 __ push(ecx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001482 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1483 __ push(Immediate(expr->pattern()));
1484 __ push(Immediate(expr->flags()));
1485 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001486 __ mov(ebx, eax);
1487
1488 __ bind(&materialized);
1489 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1490 Label allocated, runtime_allocate;
1491 __ AllocateInNewSpace(size, eax, ecx, edx, &runtime_allocate, TAG_OBJECT);
1492 __ jmp(&allocated);
1493
1494 __ bind(&runtime_allocate);
1495 __ push(ebx);
1496 __ push(Immediate(Smi::FromInt(size)));
1497 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1498 __ pop(ebx);
1499
1500 __ bind(&allocated);
1501 // Copy the content into the newly allocated memory.
1502 // (Unroll copy loop once for better throughput).
1503 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
1504 __ mov(edx, FieldOperand(ebx, i));
1505 __ mov(ecx, FieldOperand(ebx, i + kPointerSize));
1506 __ mov(FieldOperand(eax, i), edx);
1507 __ mov(FieldOperand(eax, i + kPointerSize), ecx);
1508 }
1509 if ((size % (2 * kPointerSize)) != 0) {
1510 __ mov(edx, FieldOperand(ebx, size - kPointerSize));
1511 __ mov(FieldOperand(eax, size - kPointerSize), edx);
1512 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001513 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001514}
1515
1516
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001517void FullCodeGenerator::EmitAccessor(Expression* expression) {
1518 if (expression == NULL) {
1519 __ push(Immediate(isolate()->factory()->null_value()));
1520 } else {
1521 VisitForStackValue(expression);
1522 }
1523}
1524
1525
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001526void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1527 Comment cmnt(masm_, "[ ObjectLiteral");
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001528 Handle<FixedArray> constant_properties = expr->constant_properties();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001529 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1530 __ push(FieldOperand(edi, JSFunction::kLiteralsOffset));
1531 __ push(Immediate(Smi::FromInt(expr->literal_index())));
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001532 __ push(Immediate(constant_properties));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001533 int flags = expr->fast_elements()
1534 ? ObjectLiteral::kFastElements
1535 : ObjectLiteral::kNoFlags;
1536 flags |= expr->has_function()
1537 ? ObjectLiteral::kHasFunction
1538 : ObjectLiteral::kNoFlags;
1539 __ push(Immediate(Smi::FromInt(flags)));
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001540 int properties_count = constant_properties->length() / 2;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001541 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001542 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001543 } else if (flags != ObjectLiteral::kFastElements ||
1544 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001545 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001546 } else {
1547 FastCloneShallowObjectStub stub(properties_count);
1548 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001549 }
1550
1551 // If result_saved is true the result is on top of the stack. If
1552 // result_saved is false the result is in eax.
1553 bool result_saved = false;
1554
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001555 // Mark all computed expressions that are bound to a key that
1556 // is shadowed by a later occurrence of the same key. For the
1557 // marked expressions, no store code is emitted.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001558 expr->CalculateEmitStore(zone());
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001559
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001560 AccessorTable accessor_table(zone());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001561 for (int i = 0; i < expr->properties()->length(); i++) {
1562 ObjectLiteral::Property* property = expr->properties()->at(i);
1563 if (property->IsCompileTimeValue()) continue;
1564
1565 Literal* key = property->key();
1566 Expression* value = property->value();
1567 if (!result_saved) {
1568 __ push(eax); // Save result on the stack
1569 result_saved = true;
1570 }
1571 switch (property->kind()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001572 case ObjectLiteral::Property::CONSTANT:
1573 UNREACHABLE();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001574 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1575 ASSERT(!CompileTimeValue::IsCompileTimeValue(value));
1576 // Fall through.
1577 case ObjectLiteral::Property::COMPUTED:
1578 if (key->handle()->IsSymbol()) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001579 if (property->emit_store()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001580 VisitForAccumulatorValue(value);
1581 __ mov(ecx, Immediate(key->handle()));
1582 __ mov(edx, Operand(esp, 0));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001583 Handle<Code> ic = is_classic_mode()
1584 ? isolate()->builtins()->StoreIC_Initialize()
1585 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001586 CallIC(ic, RelocInfo::CODE_TARGET, key->id());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001587 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1588 } else {
1589 VisitForEffect(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001590 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001591 break;
1592 }
1593 // Fall through.
1594 case ObjectLiteral::Property::PROTOTYPE:
1595 __ push(Operand(esp, 0)); // Duplicate receiver.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001596 VisitForStackValue(key);
1597 VisitForStackValue(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001598 if (property->emit_store()) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001599 __ push(Immediate(Smi::FromInt(NONE))); // PropertyAttributes
1600 __ CallRuntime(Runtime::kSetProperty, 4);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001601 } else {
1602 __ Drop(3);
1603 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001604 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001605 case ObjectLiteral::Property::GETTER:
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001606 accessor_table.lookup(key)->second->getter = value;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001607 break;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001608 case ObjectLiteral::Property::SETTER:
1609 accessor_table.lookup(key)->second->setter = value;
1610 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001611 }
1612 }
1613
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001614 // Emit code to define accessors, using only a single call to the runtime for
1615 // each pair of corresponding getters and setters.
1616 for (AccessorTable::Iterator it = accessor_table.begin();
1617 it != accessor_table.end();
1618 ++it) {
1619 __ push(Operand(esp, 0)); // Duplicate receiver.
1620 VisitForStackValue(it->first);
1621 EmitAccessor(it->second->getter);
1622 EmitAccessor(it->second->setter);
1623 __ push(Immediate(Smi::FromInt(NONE)));
1624 __ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
1625 }
1626
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001627 if (expr->has_function()) {
1628 ASSERT(result_saved);
1629 __ push(Operand(esp, 0));
1630 __ CallRuntime(Runtime::kToFastProperties, 1);
1631 }
1632
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001633 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001634 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001635 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001636 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001637 }
1638}
1639
1640
1641void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1642 Comment cmnt(masm_, "[ ArrayLiteral");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001643
1644 ZoneList<Expression*>* subexprs = expr->values();
1645 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001646 Handle<FixedArray> constant_elements = expr->constant_elements();
1647 ASSERT_EQ(2, constant_elements->length());
1648 ElementsKind constant_elements_kind =
1649 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001650 bool has_constant_fast_elements =
1651 IsFastObjectElementsKind(constant_elements_kind);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001652 Handle<FixedArrayBase> constant_elements_values(
1653 FixedArrayBase::cast(constant_elements->get(1)));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001654
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001655 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1656 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
1657 __ push(Immediate(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001658 __ push(Immediate(constant_elements));
erikcorry0ad885c2011-11-21 13:51:57 +00001659 Heap* heap = isolate()->heap();
1660 if (has_constant_fast_elements &&
1661 constant_elements_values->map() == heap->fixed_cow_array_map()) {
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001662 // If the elements are already FAST_*_ELEMENTS, the boilerplate cannot
erikcorry0ad885c2011-11-21 13:51:57 +00001663 // change, so it's possible to specialize the stub in advance.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001664 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(), 1);
erikcorry0ad885c2011-11-21 13:51:57 +00001665 FastCloneShallowArrayStub stub(
1666 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS,
1667 length);
1668 __ CallStub(&stub);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001669 } else if (expr->depth() > 1) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001670 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001671 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001672 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001673 } else {
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001674 ASSERT(IsFastSmiOrObjectElementsKind(constant_elements_kind) ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001675 FLAG_smi_only_arrays);
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001676 // If the elements are already FAST_*_ELEMENTS, the boilerplate cannot
erikcorry0ad885c2011-11-21 13:51:57 +00001677 // change, so it's possible to specialize the stub in advance.
1678 FastCloneShallowArrayStub::Mode mode = has_constant_fast_elements
1679 ? FastCloneShallowArrayStub::CLONE_ELEMENTS
1680 : FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001681 FastCloneShallowArrayStub stub(mode, length);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001682 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001683 }
1684
1685 bool result_saved = false; // Is the result saved to the stack?
1686
1687 // Emit code to evaluate all the non-constant subexpressions and to store
1688 // them into the newly cloned array.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001689 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001690 Expression* subexpr = subexprs->at(i);
1691 // If the subexpression is a literal or a simple materialized literal it
1692 // is already set in the cloned array.
1693 if (subexpr->AsLiteral() != NULL ||
1694 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1695 continue;
1696 }
1697
1698 if (!result_saved) {
1699 __ push(eax);
1700 result_saved = true;
1701 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001702 VisitForAccumulatorValue(subexpr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001703
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001704 if (IsFastObjectElementsKind(constant_elements_kind)) {
1705 // Fast-case array literal with ElementsKind of FAST_*_ELEMENTS, they
1706 // cannot transition and don't need to call the runtime stub.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001707 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1708 __ mov(ebx, Operand(esp, 0)); // Copy of array literal.
1709 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
1710 // Store the subexpression value in the array's elements.
1711 __ mov(FieldOperand(ebx, offset), result_register());
1712 // Update the write barrier for the array store.
1713 __ RecordWriteField(ebx, offset, result_register(), ecx,
1714 kDontSaveFPRegs,
1715 EMIT_REMEMBERED_SET,
1716 INLINE_SMI_CHECK);
1717 } else {
1718 // Store the subexpression value in the array's elements.
1719 __ mov(ebx, Operand(esp, 0)); // Copy of array literal.
1720 __ mov(edi, FieldOperand(ebx, JSObject::kMapOffset));
1721 __ mov(ecx, Immediate(Smi::FromInt(i)));
1722 __ mov(edx, Immediate(Smi::FromInt(expr->literal_index())));
1723 StoreArrayLiteralElementStub stub;
1724 __ CallStub(&stub);
1725 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001726
1727 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001728 }
1729
1730 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001731 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001732 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001733 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001734 }
1735}
1736
1737
ager@chromium.org5c838252010-02-19 08:53:10 +00001738void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1739 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001740 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1741 // on the left-hand side.
1742 if (!expr->target()->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001743 VisitForEffect(expr->target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001744 return;
1745 }
1746
ager@chromium.org5c838252010-02-19 08:53:10 +00001747 // Left-hand side can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00001748 // slot.
ager@chromium.org5c838252010-02-19 08:53:10 +00001749 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1750 LhsKind assign_type = VARIABLE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001751 Property* property = expr->target()->AsProperty();
1752 if (property != NULL) {
1753 assign_type = (property->key()->IsPropertyName())
1754 ? NAMED_PROPERTY
1755 : KEYED_PROPERTY;
ager@chromium.org5c838252010-02-19 08:53:10 +00001756 }
1757
1758 // Evaluate LHS expression.
1759 switch (assign_type) {
1760 case VARIABLE:
1761 // Nothing to do here.
1762 break;
1763 case NAMED_PROPERTY:
1764 if (expr->is_compound()) {
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001765 // We need the receiver both on the stack and in edx.
1766 VisitForStackValue(property->obj());
1767 __ mov(edx, Operand(esp, 0));
ager@chromium.org5c838252010-02-19 08:53:10 +00001768 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001769 VisitForStackValue(property->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00001770 }
1771 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001772 case KEYED_PROPERTY: {
ager@chromium.org5c838252010-02-19 08:53:10 +00001773 if (expr->is_compound()) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00001774 VisitForStackValue(property->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001775 VisitForStackValue(property->key());
1776 __ mov(edx, Operand(esp, kPointerSize)); // Object.
1777 __ mov(ecx, Operand(esp, 0)); // Key.
ager@chromium.org5c838252010-02-19 08:53:10 +00001778 } else {
whesse@chromium.org7b260152011-06-20 15:33:18 +00001779 VisitForStackValue(property->obj());
1780 VisitForStackValue(property->key());
ager@chromium.org5c838252010-02-19 08:53:10 +00001781 }
1782 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001783 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001784 }
1785
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001786 // For compound assignments we need another deoptimization point after the
1787 // variable/property load.
ager@chromium.org5c838252010-02-19 08:53:10 +00001788 if (expr->is_compound()) {
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001789 AccumulatorValueContext result_context(this);
1790 { AccumulatorValueContext left_operand_context(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001791 switch (assign_type) {
1792 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001793 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001794 PrepareForBailout(expr->target(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001795 break;
1796 case NAMED_PROPERTY:
1797 EmitNamedPropertyLoad(property);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001798 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001799 break;
1800 case KEYED_PROPERTY:
1801 EmitKeyedPropertyLoad(property);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001802 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001803 break;
1804 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001805 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001806
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001807 Token::Value op = expr->binary_op();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001808 __ push(eax); // Left operand goes on the stack.
1809 VisitForAccumulatorValue(expr->value());
ager@chromium.org5c838252010-02-19 08:53:10 +00001810
ricow@chromium.org65fae842010-08-25 15:26:24 +00001811 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1812 ? OVERWRITE_RIGHT
1813 : NO_OVERWRITE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001814 SetSourcePosition(expr->position() + 1);
1815 if (ShouldInlineSmiCase(op)) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001816 EmitInlineSmiBinaryOp(expr->binary_operation(),
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001817 op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001818 mode,
1819 expr->target(),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001820 expr->value());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001821 } else {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001822 EmitBinaryOp(expr->binary_operation(), op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001823 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001824
1825 // Deoptimization point in case the binary operation may have side effects.
1826 PrepareForBailout(expr->binary_operation(), TOS_REG);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001827 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001828 VisitForAccumulatorValue(expr->value());
ager@chromium.org5c838252010-02-19 08:53:10 +00001829 }
1830
1831 // Record source position before possible IC call.
1832 SetSourcePosition(expr->position());
1833
1834 // Store the value.
1835 switch (assign_type) {
1836 case VARIABLE:
1837 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001838 expr->op());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001839 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1840 context()->Plug(eax);
ager@chromium.org5c838252010-02-19 08:53:10 +00001841 break;
1842 case NAMED_PROPERTY:
1843 EmitNamedPropertyAssignment(expr);
1844 break;
1845 case KEYED_PROPERTY:
1846 EmitKeyedPropertyAssignment(expr);
1847 break;
1848 }
1849}
1850
1851
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001852void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1853 SetSourcePosition(prop->position());
1854 Literal* key = prop->key()->AsLiteral();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001855 ASSERT(!key->handle()->IsSmi());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001856 __ mov(ecx, Immediate(key->handle()));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001857 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001858 CallIC(ic, RelocInfo::CODE_TARGET, prop->id());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001859}
1860
1861
1862void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1863 SetSourcePosition(prop->position());
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001864 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001865 CallIC(ic, RelocInfo::CODE_TARGET, prop->id());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001866}
1867
1868
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001869void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001870 Token::Value op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001871 OverwriteMode mode,
1872 Expression* left,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001873 Expression* right) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001874 // Do combined smi check of the operands. Left operand is on the
1875 // stack. Right operand is in eax.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001876 Label smi_case, done, stub_call;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001877 __ pop(edx);
1878 __ mov(ecx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001879 __ or_(eax, edx);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001880 JumpPatchSite patch_site(masm_);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001881 patch_site.EmitJumpIfSmi(eax, &smi_case, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001882
1883 __ bind(&stub_call);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001884 __ mov(eax, ecx);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001885 BinaryOpStub stub(op, mode);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001886 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001887 patch_site.EmitPatchInfo();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001888 __ jmp(&done, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001889
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001890 // Smi case.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001891 __ bind(&smi_case);
1892 __ mov(eax, edx); // Copy left operand in case of a stub call.
1893
1894 switch (op) {
1895 case Token::SAR:
1896 __ SmiUntag(eax);
1897 __ SmiUntag(ecx);
1898 __ sar_cl(eax); // No checks of result necessary
1899 __ SmiTag(eax);
1900 break;
1901 case Token::SHL: {
1902 Label result_ok;
1903 __ SmiUntag(eax);
1904 __ SmiUntag(ecx);
1905 __ shl_cl(eax);
1906 // Check that the *signed* result fits in a smi.
1907 __ cmp(eax, 0xc0000000);
1908 __ j(positive, &result_ok);
1909 __ SmiTag(ecx);
1910 __ jmp(&stub_call);
1911 __ bind(&result_ok);
1912 __ SmiTag(eax);
1913 break;
1914 }
1915 case Token::SHR: {
1916 Label result_ok;
1917 __ SmiUntag(eax);
1918 __ SmiUntag(ecx);
1919 __ shr_cl(eax);
1920 __ test(eax, Immediate(0xc0000000));
1921 __ j(zero, &result_ok);
1922 __ SmiTag(ecx);
1923 __ jmp(&stub_call);
1924 __ bind(&result_ok);
1925 __ SmiTag(eax);
1926 break;
1927 }
1928 case Token::ADD:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001929 __ add(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001930 __ j(overflow, &stub_call);
1931 break;
1932 case Token::SUB:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001933 __ sub(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001934 __ j(overflow, &stub_call);
1935 break;
1936 case Token::MUL: {
1937 __ SmiUntag(eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001938 __ imul(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001939 __ j(overflow, &stub_call);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001940 __ test(eax, eax);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001941 __ j(not_zero, &done, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001942 __ mov(ebx, edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001943 __ or_(ebx, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001944 __ j(negative, &stub_call);
1945 break;
1946 }
1947 case Token::BIT_OR:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001948 __ or_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001949 break;
1950 case Token::BIT_AND:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001951 __ and_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001952 break;
1953 case Token::BIT_XOR:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001954 __ xor_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001955 break;
1956 default:
1957 UNREACHABLE();
1958 }
1959
1960 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001961 context()->Plug(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001962}
1963
1964
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001965void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1966 Token::Value op,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001967 OverwriteMode mode) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001968 __ pop(edx);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001969 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001970 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001971 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001972 patch_site.EmitPatchInfo();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001973 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001974}
1975
1976
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001977void FullCodeGenerator::EmitAssignment(Expression* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001978 // Invalid left-hand sides are rewritten to have a 'throw
1979 // ReferenceError' on the left-hand side.
1980 if (!expr->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001981 VisitForEffect(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001982 return;
1983 }
1984
1985 // Left-hand side can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00001986 // slot.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001987 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1988 LhsKind assign_type = VARIABLE;
1989 Property* prop = expr->AsProperty();
1990 if (prop != NULL) {
1991 assign_type = (prop->key()->IsPropertyName())
1992 ? NAMED_PROPERTY
1993 : KEYED_PROPERTY;
1994 }
1995
1996 switch (assign_type) {
1997 case VARIABLE: {
1998 Variable* var = expr->AsVariableProxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001999 EffectContext context(this);
2000 EmitVariableAssignment(var, Token::ASSIGN);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002001 break;
2002 }
2003 case NAMED_PROPERTY: {
2004 __ push(eax); // Preserve value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002005 VisitForAccumulatorValue(prop->obj());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002006 __ mov(edx, eax);
2007 __ pop(eax); // Restore value.
2008 __ mov(ecx, prop->key()->AsLiteral()->handle());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002009 Handle<Code> ic = is_classic_mode()
2010 ? isolate()->builtins()->StoreIC_Initialize()
2011 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002012 CallIC(ic);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002013 break;
2014 }
2015 case KEYED_PROPERTY: {
2016 __ push(eax); // Preserve value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002017 VisitForStackValue(prop->obj());
2018 VisitForAccumulatorValue(prop->key());
2019 __ mov(ecx, eax);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002020 __ pop(edx); // Receiver.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002021 __ pop(eax); // Restore value.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002022 Handle<Code> ic = is_classic_mode()
2023 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2024 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002025 CallIC(ic);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002026 break;
2027 }
2028 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002029 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002030}
2031
2032
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002033void FullCodeGenerator::EmitVariableAssignment(Variable* var,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002034 Token::Value op) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002035 if (var->IsUnallocated()) {
2036 // Global var, const, or let.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002037 __ mov(ecx, var->name());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002038 __ mov(edx, GlobalObjectOperand());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002039 Handle<Code> ic = is_classic_mode()
2040 ? isolate()->builtins()->StoreIC_Initialize()
2041 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002042 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002043
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002044 } else if (op == Token::INIT_CONST) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002045 // Const initializers need a write barrier.
2046 ASSERT(!var->IsParameter()); // No const parameters.
2047 if (var->IsStackLocal()) {
2048 Label skip;
2049 __ mov(edx, StackOperand(var));
2050 __ cmp(edx, isolate()->factory()->the_hole_value());
2051 __ j(not_equal, &skip);
2052 __ mov(StackOperand(var), eax);
2053 __ bind(&skip);
2054 } else {
2055 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
2056 // Like var declarations, const declarations are hoisted to function
2057 // scope. However, unlike var initializers, const initializers are
2058 // able to drill a hole to that function context, even from inside a
2059 // 'with' context. We thus bypass the normal static scope lookup for
2060 // var->IsContextSlot().
2061 __ push(eax);
2062 __ push(esi);
2063 __ push(Immediate(var->name()));
2064 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002065 }
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002066
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002067 } else if (var->mode() == LET && op != Token::INIT_LET) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002068 // Non-initializing assignment to let variable needs a write barrier.
2069 if (var->IsLookupSlot()) {
2070 __ push(eax); // Value.
2071 __ push(esi); // Context.
2072 __ push(Immediate(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002073 __ push(Immediate(Smi::FromInt(language_mode())));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002074 __ CallRuntime(Runtime::kStoreContextSlot, 4);
2075 } else {
2076 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
2077 Label assign;
2078 MemOperand location = VarOperand(var, ecx);
2079 __ mov(edx, location);
2080 __ cmp(edx, isolate()->factory()->the_hole_value());
2081 __ j(not_equal, &assign, Label::kNear);
2082 __ push(Immediate(var->name()));
2083 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2084 __ bind(&assign);
2085 __ mov(location, eax);
2086 if (var->IsContextSlot()) {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00002087 __ mov(edx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002088 int offset = Context::SlotOffset(var->index());
2089 __ RecordWriteContextSlot(ecx, offset, edx, ebx, kDontSaveFPRegs);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00002090 }
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00002091 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002092
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002093 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2094 // Assignment to var or initializing assignment to let/const
2095 // in harmony mode.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002096 if (var->IsStackAllocated() || var->IsContextSlot()) {
2097 MemOperand location = VarOperand(var, ecx);
2098 if (FLAG_debug_code && op == Token::INIT_LET) {
2099 // Check for an uninitialized let binding.
2100 __ mov(edx, location);
2101 __ cmp(edx, isolate()->factory()->the_hole_value());
2102 __ Check(equal, "Let binding re-initialization.");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002103 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002104 // Perform the assignment.
2105 __ mov(location, eax);
2106 if (var->IsContextSlot()) {
2107 __ mov(edx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002108 int offset = Context::SlotOffset(var->index());
2109 __ RecordWriteContextSlot(ecx, offset, edx, ebx, kDontSaveFPRegs);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002110 }
2111 } else {
2112 ASSERT(var->IsLookupSlot());
2113 __ push(eax); // Value.
2114 __ push(esi); // Context.
2115 __ push(Immediate(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002116 __ push(Immediate(Smi::FromInt(language_mode())));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002117 __ CallRuntime(Runtime::kStoreContextSlot, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002118 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002119 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002120 // Non-initializing assignments to consts are ignored.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002121}
2122
2123
2124void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2125 // Assignment to a property, using a named store IC.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002126 // eax : value
2127 // esp[0] : receiver
2128
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002129 Property* prop = expr->target()->AsProperty();
2130 ASSERT(prop != NULL);
2131 ASSERT(prop->key()->AsLiteral() != NULL);
2132
2133 // If the assignment starts a block of assignments to the same object,
2134 // change to slow case to avoid the quadratic behavior of repeatedly
2135 // adding fast properties.
2136 if (expr->starts_initialization_block()) {
2137 __ push(result_register());
2138 __ push(Operand(esp, kPointerSize)); // Receiver is now under value.
2139 __ CallRuntime(Runtime::kToSlowProperties, 1);
2140 __ pop(result_register());
2141 }
2142
2143 // Record source code position before IC call.
2144 SetSourcePosition(expr->position());
2145 __ mov(ecx, prop->key()->AsLiteral()->handle());
2146 if (expr->ends_initialization_block()) {
2147 __ mov(edx, Operand(esp, 0));
2148 } else {
2149 __ pop(edx);
2150 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002151 Handle<Code> ic = is_classic_mode()
2152 ? isolate()->builtins()->StoreIC_Initialize()
2153 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002154 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002155
2156 // If the assignment ends an initialization block, revert to fast case.
2157 if (expr->ends_initialization_block()) {
2158 __ push(eax); // Result of assignment, saved even if not needed.
2159 __ push(Operand(esp, kPointerSize)); // Receiver is under value.
2160 __ CallRuntime(Runtime::kToFastProperties, 1);
2161 __ pop(eax);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002162 __ Drop(1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002163 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002164 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2165 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002166}
2167
2168
2169void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2170 // Assignment to a property, using a keyed store IC.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002171 // eax : value
2172 // esp[0] : key
2173 // esp[kPointerSize] : receiver
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002174
2175 // If the assignment starts a block of assignments to the same object,
2176 // change to slow case to avoid the quadratic behavior of repeatedly
2177 // adding fast properties.
2178 if (expr->starts_initialization_block()) {
2179 __ push(result_register());
2180 // Receiver is now under the key and value.
2181 __ push(Operand(esp, 2 * kPointerSize));
2182 __ CallRuntime(Runtime::kToSlowProperties, 1);
2183 __ pop(result_register());
2184 }
2185
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002186 __ pop(ecx); // Key.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002187 if (expr->ends_initialization_block()) {
2188 __ mov(edx, Operand(esp, 0)); // Leave receiver on the stack for later.
2189 } else {
2190 __ pop(edx);
2191 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002192 // Record source code position before IC call.
2193 SetSourcePosition(expr->position());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002194 Handle<Code> ic = is_classic_mode()
2195 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2196 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002197 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002198
2199 // If the assignment ends an initialization block, revert to fast case.
2200 if (expr->ends_initialization_block()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002201 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002202 __ push(eax); // Result of assignment, saved even if not needed.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002203 __ push(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002204 __ CallRuntime(Runtime::kToFastProperties, 1);
2205 __ pop(eax);
2206 }
2207
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002208 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002209 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002210}
2211
2212
2213void FullCodeGenerator::VisitProperty(Property* expr) {
2214 Comment cmnt(masm_, "[ Property");
2215 Expression* key = expr->key();
2216
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002217 if (key->IsPropertyName()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002218 VisitForAccumulatorValue(expr->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002219 __ mov(edx, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002220 EmitNamedPropertyLoad(expr);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002221 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002222 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002223 VisitForStackValue(expr->obj());
2224 VisitForAccumulatorValue(expr->key());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002225 __ pop(edx); // Object.
2226 __ mov(ecx, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002227 EmitKeyedPropertyLoad(expr);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002228 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002229 }
2230}
2231
2232
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002233void FullCodeGenerator::CallIC(Handle<Code> code,
2234 RelocInfo::Mode rmode,
2235 unsigned ast_id) {
2236 ic_total_count_++;
2237 __ call(code, rmode, ast_id);
2238}
2239
2240
2241
2242
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002243void FullCodeGenerator::EmitCallWithIC(Call* expr,
2244 Handle<Object> name,
2245 RelocInfo::Mode mode) {
2246 // Code common for calls using the IC.
2247 ZoneList<Expression*>* args = expr->arguments();
2248 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002249 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002250 for (int i = 0; i < arg_count; i++) {
2251 VisitForStackValue(args->at(i));
2252 }
2253 __ Set(ecx, Immediate(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002254 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002255 // Record source position of the IC call.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002256 SetSourcePosition(expr->position());
danno@chromium.org40cb8782011-05-25 07:58:50 +00002257 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002258 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002259 CallIC(ic, mode, expr->id());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002260 RecordJSReturnSite(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002261 // Restore context register.
2262 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002263 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002264}
2265
2266
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002267void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002268 Expression* key) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002269 // Load the key.
2270 VisitForAccumulatorValue(key);
2271
2272 // Swap the name of the function and the receiver on the stack to follow
2273 // the calling convention for call ICs.
2274 __ pop(ecx);
2275 __ push(eax);
2276 __ push(ecx);
2277
2278 // Load the arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002279 ZoneList<Expression*>* args = expr->arguments();
2280 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002281 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002282 for (int i = 0; i < arg_count; i++) {
2283 VisitForStackValue(args->at(i));
2284 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002285 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002286 // Record source position of the IC call.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002287 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002288 Handle<Code> ic =
2289 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002290 __ mov(ecx, Operand(esp, (arg_count + 1) * kPointerSize)); // Key.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002291 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002292 RecordJSReturnSite(expr);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002293 // Restore context register.
2294 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002295 context()->DropAndPlug(1, eax); // Drop the key still on the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002296}
2297
2298
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002299void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002300 // Code common for calls using the call stub.
2301 ZoneList<Expression*>* args = expr->arguments();
2302 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002303 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002304 for (int i = 0; i < arg_count; i++) {
2305 VisitForStackValue(args->at(i));
2306 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002307 }
2308 // Record source position for debugger.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002309 SetSourcePosition(expr->position());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002310
2311 // Record call targets in unoptimized code, but not in the snapshot.
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002312 if (!Serializer::enabled()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002313 flags = static_cast<CallFunctionFlags>(flags | RECORD_CALL_TARGET);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002314 Handle<Object> uninitialized =
2315 TypeFeedbackCells::UninitializedSentinel(isolate());
2316 Handle<JSGlobalPropertyCell> cell =
2317 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
2318 RecordTypeFeedbackCell(expr->id(), cell);
2319 __ mov(ebx, cell);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002320 }
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002321
lrn@chromium.org34e60782011-09-15 07:25:40 +00002322 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002323 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002324 __ CallStub(&stub, expr->id());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002325
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002326 RecordJSReturnSite(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002327 // Restore context register.
2328 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002329 context()->DropAndPlug(1, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002330}
2331
2332
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002333void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002334 // Push copy of the first argument or undefined if it doesn't exist.
2335 if (arg_count > 0) {
2336 __ push(Operand(esp, arg_count * kPointerSize));
2337 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002338 __ push(Immediate(isolate()->factory()->undefined_value()));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002339 }
2340
2341 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002342 __ push(Operand(ebp, (2 + info_->scope()->num_parameters()) * kPointerSize));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002343 // Push the language mode.
2344 __ push(Immediate(Smi::FromInt(language_mode())));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002345
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002346 // Push the start position of the scope the calls resides in.
2347 __ push(Immediate(Smi::FromInt(scope()->start_position())));
2348
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002349 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002350 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002351}
2352
2353
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002354void FullCodeGenerator::VisitCall(Call* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002355#ifdef DEBUG
2356 // We want to verify that RecordJSReturnSite gets called on all paths
2357 // through this function. Avoid early returns.
2358 expr->return_is_recorded_ = false;
2359#endif
2360
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002361 Comment cmnt(masm_, "[ Call");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002362 Expression* callee = expr->expression();
2363 VariableProxy* proxy = callee->AsVariableProxy();
2364 Property* property = callee->AsProperty();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002365
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002366 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002367 // In a call to eval, we first call %ResolvePossiblyDirectEval to
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002368 // resolve the function we need to call and the receiver of the call.
2369 // Then we call the resolved function using the given arguments.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002370 ZoneList<Expression*>* args = expr->arguments();
2371 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002372 { PreservePositionScope pos_scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002373 VisitForStackValue(callee);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002374 // Reserved receiver slot.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002375 __ push(Immediate(isolate()->factory()->undefined_value()));
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002376 // Push the arguments.
2377 for (int i = 0; i < arg_count; i++) {
2378 VisitForStackValue(args->at(i));
2379 }
2380
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002381 // Push a copy of the function (found below the arguments) and
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002382 // resolve eval.
2383 __ push(Operand(esp, (arg_count + 1) * kPointerSize));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002384 EmitResolvePossiblyDirectEval(arg_count);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002385
2386 // The runtime call returns a pair of values in eax (function) and
2387 // edx (receiver). Touch up the stack with the right values.
2388 __ mov(Operand(esp, (arg_count + 0) * kPointerSize), edx);
2389 __ mov(Operand(esp, (arg_count + 1) * kPointerSize), eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002390 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002391 // Record source position for debugger.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002392 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002393 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002394 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002395 __ CallStub(&stub);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002396 RecordJSReturnSite(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002397 // Restore context register.
2398 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002399 context()->DropAndPlug(1, eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002400
2401 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002402 // Push global object as receiver for the call IC.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002403 __ push(GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002404 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2405
2406 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002407 // Call to a lookup slot (dynamically introduced variable).
2408 Label slow, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002409 { PreservePositionScope scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002410 // Generate code for loading from variables potentially shadowed by
2411 // eval-introduced variables.
2412 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002413 }
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002414 __ bind(&slow);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002415 // Call the runtime to find the function to call (returned in eax) and
2416 // the object holding it (returned in edx).
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002417 __ push(context_register());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002418 __ push(Immediate(proxy->name()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002419 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2420 __ push(eax); // Function.
2421 __ push(edx); // Receiver.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002422
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002423 // If fast case code has been generated, emit code to push the function
2424 // and receiver and have the slow path jump around this code.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002425 if (done.is_linked()) {
2426 Label call;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002427 __ jmp(&call, Label::kNear);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002428 __ bind(&done);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002429 // Push function.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002430 __ push(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002431 // The receiver is implicitly the global receiver. Indicate this by
2432 // passing the hole to the call function stub.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002433 __ push(Immediate(isolate()->factory()->the_hole_value()));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002434 __ bind(&call);
2435 }
2436
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002437 // The receiver is either the global receiver or an object found by
2438 // LoadContextSlot. That object could be the hole if the receiver is
2439 // implicitly the global object.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002440 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002441
2442 } else if (property != NULL) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002443 { PreservePositionScope scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002444 VisitForStackValue(property->obj());
2445 }
2446 if (property->key()->IsPropertyName()) {
2447 EmitCallWithIC(expr,
2448 property->key()->AsLiteral()->handle(),
2449 RelocInfo::CODE_TARGET);
2450 } else {
2451 EmitKeyedCallWithIC(expr, property->key());
2452 }
2453
2454 } else {
2455 // Call to an arbitrary expression not handled specially above.
2456 { PreservePositionScope scope(masm()->positions_recorder());
2457 VisitForStackValue(callee);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002458 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002459 // Load global receiver object.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002460 __ mov(ebx, GlobalObjectOperand());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002461 __ push(FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
2462 // Emit function call.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002463 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002464 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002465
2466#ifdef DEBUG
2467 // RecordJSReturnSite should have been called.
2468 ASSERT(expr->return_is_recorded_);
2469#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002470}
2471
2472
2473void FullCodeGenerator::VisitCallNew(CallNew* expr) {
2474 Comment cmnt(masm_, "[ CallNew");
2475 // According to ECMA-262, section 11.2.2, page 44, the function
2476 // expression in new calls must be evaluated before the
2477 // arguments.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002478
ricow@chromium.org65fae842010-08-25 15:26:24 +00002479 // Push constructor on the stack. If it's not a function it's used as
2480 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2481 // ignored.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002482 VisitForStackValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002483
2484 // Push the arguments ("left-to-right") on the stack.
2485 ZoneList<Expression*>* args = expr->arguments();
2486 int arg_count = args->length();
2487 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002488 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002489 }
2490
2491 // Call the construct call builtin that handles allocation and
2492 // constructor invocation.
2493 SetSourcePosition(expr->position());
2494
ricow@chromium.org65fae842010-08-25 15:26:24 +00002495 // Load function and argument count into edi and eax.
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002496 __ Set(eax, Immediate(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002497 __ mov(edi, Operand(esp, arg_count * kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002498
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002499 // Record call targets in unoptimized code, but not in the snapshot.
2500 CallFunctionFlags flags;
2501 if (!Serializer::enabled()) {
2502 flags = RECORD_CALL_TARGET;
2503 Handle<Object> uninitialized =
2504 TypeFeedbackCells::UninitializedSentinel(isolate());
2505 Handle<JSGlobalPropertyCell> cell =
2506 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
2507 RecordTypeFeedbackCell(expr->id(), cell);
2508 __ mov(ebx, cell);
2509 } else {
2510 flags = NO_CALL_FUNCTION_FLAGS;
2511 }
2512
2513 CallConstructStub stub(flags);
2514 __ call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
ulan@chromium.org967e2702012-02-28 09:49:15 +00002515 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002516 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002517}
2518
2519
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002520void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2521 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002522 ASSERT(args->length() == 1);
2523
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002524 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002525
2526 Label materialize_true, materialize_false;
2527 Label* if_true = NULL;
2528 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002529 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002530 context()->PrepareTest(&materialize_true, &materialize_false,
2531 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002532
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002533 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002534 __ test(eax, Immediate(kSmiTagMask));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002535 Split(zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002536
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002537 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002538}
2539
2540
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002541void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2542 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002543 ASSERT(args->length() == 1);
2544
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002545 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002546
2547 Label materialize_true, materialize_false;
2548 Label* if_true = NULL;
2549 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002550 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002551 context()->PrepareTest(&materialize_true, &materialize_false,
2552 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002553
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002554 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002555 __ test(eax, Immediate(kSmiTagMask | 0x80000000));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002556 Split(zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002557
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002558 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002559}
2560
2561
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002562void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2563 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002564 ASSERT(args->length() == 1);
2565
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002566 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002567
2568 Label materialize_true, materialize_false;
2569 Label* if_true = NULL;
2570 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002571 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002572 context()->PrepareTest(&materialize_true, &materialize_false,
2573 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002574
whesse@chromium.org7b260152011-06-20 15:33:18 +00002575 __ JumpIfSmi(eax, if_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002576 __ cmp(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002577 __ j(equal, if_true);
2578 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2579 // Undetectable objects behave like undefined when tested with typeof.
2580 __ movzx_b(ecx, FieldOperand(ebx, Map::kBitFieldOffset));
2581 __ test(ecx, Immediate(1 << Map::kIsUndetectable));
2582 __ j(not_zero, if_false);
2583 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceTypeOffset));
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002584 __ cmp(ecx, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002585 __ j(below, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002586 __ cmp(ecx, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002587 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002588 Split(below_equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002589
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002590 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002591}
2592
2593
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002594void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2595 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002596 ASSERT(args->length() == 1);
2597
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002598 VisitForAccumulatorValue(args->at(0));
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002599
2600 Label materialize_true, materialize_false;
2601 Label* if_true = NULL;
2602 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002603 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002604 context()->PrepareTest(&materialize_true, &materialize_false,
2605 &if_true, &if_false, &fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002606
whesse@chromium.org7b260152011-06-20 15:33:18 +00002607 __ JumpIfSmi(eax, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002608 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002609 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002610 Split(above_equal, if_true, if_false, fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002611
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002612 context()->Plug(if_true, if_false);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002613}
2614
2615
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002616void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2617 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002618 ASSERT(args->length() == 1);
2619
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002620 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002621
2622 Label materialize_true, materialize_false;
2623 Label* if_true = NULL;
2624 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002625 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002626 context()->PrepareTest(&materialize_true, &materialize_false,
2627 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002628
whesse@chromium.org7b260152011-06-20 15:33:18 +00002629 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002630 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2631 __ movzx_b(ebx, FieldOperand(ebx, Map::kBitFieldOffset));
2632 __ test(ebx, Immediate(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002633 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002634 Split(not_zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002635
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002636 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002637}
2638
2639
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002640void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002641 CallRuntime* expr) {
2642 ZoneList<Expression*>* args = expr->arguments();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002643 ASSERT(args->length() == 1);
2644
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002645 VisitForAccumulatorValue(args->at(0));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002646
2647 Label materialize_true, materialize_false;
2648 Label* if_true = NULL;
2649 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002650 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002651 context()->PrepareTest(&materialize_true, &materialize_false,
2652 &if_true, &if_false, &fall_through);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002653
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002654 if (FLAG_debug_code) __ AbortIfSmi(eax);
2655
2656 // Check whether this map has already been checked to be safe for default
2657 // valueOf.
2658 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2659 __ test_b(FieldOperand(ebx, Map::kBitField2Offset),
2660 1 << Map::kStringWrapperSafeForDefaultValueOf);
2661 __ j(not_zero, if_true);
2662
2663 // Check for fast case object. Return false for slow case objects.
2664 __ mov(ecx, FieldOperand(eax, JSObject::kPropertiesOffset));
2665 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
2666 __ cmp(ecx, FACTORY->hash_table_map());
2667 __ j(equal, if_false);
2668
2669 // Look for valueOf symbol in the descriptor array, and indicate false if
2670 // found. The type is not checked, so if it is a transition it is a false
2671 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002672 __ LoadInstanceDescriptors(ebx, ebx);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002673 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
2674 // ebx: descriptor array
2675 // ecx: length of descriptor array
2676 // Calculate the end of the descriptor array.
2677 STATIC_ASSERT(kSmiTag == 0);
2678 STATIC_ASSERT(kSmiTagSize == 1);
2679 STATIC_ASSERT(kPointerSize == 4);
2680 __ lea(ecx, Operand(ebx, ecx, times_2, FixedArray::kHeaderSize));
2681 // Calculate location of the first key name.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002682 __ add(ebx,
2683 Immediate(FixedArray::kHeaderSize +
2684 DescriptorArray::kFirstIndex * kPointerSize));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002685 // Loop through all the keys in the descriptor array. If one of these is the
2686 // symbol valueOf the result is false.
2687 Label entry, loop;
2688 __ jmp(&entry);
2689 __ bind(&loop);
2690 __ mov(edx, FieldOperand(ebx, 0));
2691 __ cmp(edx, FACTORY->value_of_symbol());
2692 __ j(equal, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002693 __ add(ebx, Immediate(kPointerSize));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002694 __ bind(&entry);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002695 __ cmp(ebx, ecx);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002696 __ j(not_equal, &loop);
2697
2698 // Reload map as register ebx was used as temporary above.
2699 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2700
2701 // If a valueOf property is not found on the object check that it's
2702 // prototype is the un-modified String prototype. If not result is false.
2703 __ mov(ecx, FieldOperand(ebx, Map::kPrototypeOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00002704 __ JumpIfSmi(ecx, if_false);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002705 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
2706 __ mov(edx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2707 __ mov(edx,
2708 FieldOperand(edx, GlobalObject::kGlobalContextOffset));
2709 __ cmp(ecx,
2710 ContextOperand(edx,
2711 Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2712 __ j(not_equal, if_false);
2713 // Set the bit in the map to indicate that it has been checked safe for
2714 // default valueOf and set true result.
2715 __ or_(FieldOperand(ebx, Map::kBitField2Offset),
2716 Immediate(1 << Map::kStringWrapperSafeForDefaultValueOf));
2717 __ jmp(if_true);
2718
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002719 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002720 context()->Plug(if_true, if_false);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002721}
2722
2723
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002724void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2725 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002726 ASSERT(args->length() == 1);
2727
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002728 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002729
2730 Label materialize_true, materialize_false;
2731 Label* if_true = NULL;
2732 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002733 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002734 context()->PrepareTest(&materialize_true, &materialize_false,
2735 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002736
whesse@chromium.org7b260152011-06-20 15:33:18 +00002737 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002738 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002739 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002740 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002741
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002742 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002743}
2744
2745
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002746void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2747 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002748 ASSERT(args->length() == 1);
2749
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002750 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002751
2752 Label materialize_true, materialize_false;
2753 Label* if_true = NULL;
2754 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002755 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002756 context()->PrepareTest(&materialize_true, &materialize_false,
2757 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002758
whesse@chromium.org7b260152011-06-20 15:33:18 +00002759 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002760 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002761 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002762 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002763
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002764 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002765}
2766
2767
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002768void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2769 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002770 ASSERT(args->length() == 1);
2771
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002772 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002773
2774 Label materialize_true, materialize_false;
2775 Label* if_true = NULL;
2776 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002777 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002778 context()->PrepareTest(&materialize_true, &materialize_false,
2779 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002780
whesse@chromium.org7b260152011-06-20 15:33:18 +00002781 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002782 __ CmpObjectType(eax, JS_REGEXP_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002783 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002784 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002785
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002786 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002787}
2788
2789
2790
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002791void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2792 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002793
2794 Label materialize_true, materialize_false;
2795 Label* if_true = NULL;
2796 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002797 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002798 context()->PrepareTest(&materialize_true, &materialize_false,
2799 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002800
2801 // Get the frame pointer for the calling frame.
2802 __ mov(eax, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2803
2804 // Skip the arguments adaptor frame if it exists.
2805 Label check_frame_marker;
2806 __ cmp(Operand(eax, StandardFrameConstants::kContextOffset),
2807 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2808 __ j(not_equal, &check_frame_marker);
2809 __ mov(eax, Operand(eax, StandardFrameConstants::kCallerFPOffset));
2810
2811 // Check the marker in the calling frame.
2812 __ bind(&check_frame_marker);
2813 __ cmp(Operand(eax, StandardFrameConstants::kMarkerOffset),
2814 Immediate(Smi::FromInt(StackFrame::CONSTRUCT)));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002815 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002816 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002817
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002818 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002819}
2820
2821
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002822void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2823 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002824 ASSERT(args->length() == 2);
2825
2826 // Load the two objects into registers and perform the comparison.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002827 VisitForStackValue(args->at(0));
2828 VisitForAccumulatorValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002829
2830 Label materialize_true, materialize_false;
2831 Label* if_true = NULL;
2832 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002833 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002834 context()->PrepareTest(&materialize_true, &materialize_false,
2835 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002836
2837 __ pop(ebx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002838 __ cmp(eax, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002839 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002840 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002841
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002842 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002843}
2844
2845
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002846void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2847 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002848 ASSERT(args->length() == 1);
2849
2850 // ArgumentsAccessStub expects the key in edx and the formal
2851 // parameter count in eax.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002852 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002853 __ mov(edx, eax);
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002854 __ Set(eax, Immediate(Smi::FromInt(info_->scope()->num_parameters())));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002855 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2856 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002857 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002858}
2859
2860
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002861void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2862 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002863
2864 Label exit;
2865 // Get the number of formal parameters.
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002866 __ Set(eax, Immediate(Smi::FromInt(info_->scope()->num_parameters())));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002867
2868 // Check if the calling frame is an arguments adaptor frame.
2869 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2870 __ cmp(Operand(ebx, StandardFrameConstants::kContextOffset),
2871 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2872 __ j(not_equal, &exit);
2873
2874 // Arguments adaptor case: Read the arguments length from the
2875 // adaptor frame.
2876 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2877
2878 __ bind(&exit);
2879 if (FLAG_debug_code) __ AbortIfNotSmi(eax);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002880 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002881}
2882
2883
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002884void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2885 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002886 ASSERT(args->length() == 1);
2887 Label done, null, function, non_function_constructor;
2888
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002889 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002890
2891 // If the object is a smi, we return null.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002892 __ JumpIfSmi(eax, &null);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002893
2894 // Check that the object is a JS object but take special care of JS
2895 // functions to make sure they have 'Function' as their class.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002896 // Assume that there are only two callable types, and one of them is at
2897 // either end of the type range for JS object types. Saves extra comparisons.
2898 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002899 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, eax);
2900 // Map is now in eax.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002901 __ j(below, &null);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002902 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2903 FIRST_SPEC_OBJECT_TYPE + 1);
2904 __ j(equal, &function);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002905
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002906 __ CmpInstanceType(eax, LAST_SPEC_OBJECT_TYPE);
2907 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2908 LAST_SPEC_OBJECT_TYPE - 1);
2909 __ j(equal, &function);
2910 // Assume that there is no larger type.
2911 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002912
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002913 // Check if the constructor in the map is a JS function.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002914 __ mov(eax, FieldOperand(eax, Map::kConstructorOffset));
2915 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
2916 __ j(not_equal, &non_function_constructor);
2917
2918 // eax now contains the constructor function. Grab the
2919 // instance class name from there.
2920 __ mov(eax, FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset));
2921 __ mov(eax, FieldOperand(eax, SharedFunctionInfo::kInstanceClassNameOffset));
2922 __ jmp(&done);
2923
2924 // Functions have class 'Function'.
2925 __ bind(&function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002926 __ mov(eax, isolate()->factory()->function_class_symbol());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002927 __ jmp(&done);
2928
2929 // Objects with a non-function constructor have class 'Object'.
2930 __ bind(&non_function_constructor);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002931 __ mov(eax, isolate()->factory()->Object_symbol());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002932 __ jmp(&done);
2933
2934 // Non-JS objects have class null.
2935 __ bind(&null);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002936 __ mov(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002937
2938 // All done.
2939 __ bind(&done);
2940
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002941 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002942}
2943
2944
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002945void FullCodeGenerator::EmitLog(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002946 // Conditionally generate a log call.
2947 // Args:
2948 // 0 (literal string): The type of logging (corresponds to the flags).
2949 // This is used to determine whether or not to generate the log call.
2950 // 1 (string): Format string. Access the string at argument index 2
2951 // with '%2s' (see Logger::LogRuntime for all the formats).
2952 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002953 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002954 ASSERT_EQ(args->length(), 3);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002955 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002956 VisitForStackValue(args->at(1));
2957 VisitForStackValue(args->at(2));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002958 __ CallRuntime(Runtime::kLog, 2);
2959 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002960 // Finally, we're expected to leave a value on the top of the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002961 __ mov(eax, isolate()->factory()->undefined_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002962 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002963}
2964
2965
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002966void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
2967 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002968
2969 Label slow_allocate_heapnumber;
2970 Label heapnumber_allocated;
2971
2972 __ AllocateHeapNumber(edi, ebx, ecx, &slow_allocate_heapnumber);
2973 __ jmp(&heapnumber_allocated);
2974
2975 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002976 // Allocate a heap number.
2977 __ CallRuntime(Runtime::kNumberAlloc, 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002978 __ mov(edi, eax);
2979
2980 __ bind(&heapnumber_allocated);
2981
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002982 __ PrepareCallCFunction(1, ebx);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002983 __ mov(eax, ContextOperand(context_register(), Context::GLOBAL_INDEX));
2984 __ mov(eax, FieldOperand(eax, GlobalObject::kGlobalContextOffset));
2985 __ mov(Operand(esp, 0), eax);
2986 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002987
2988 // Convert 32 random bits in eax to 0.(32 random bits) in a double
2989 // by computing:
2990 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2991 // This is implemented on both SSE2 and FPU.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002992 if (CpuFeatures::IsSupported(SSE2)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002993 CpuFeatures::Scope fscope(SSE2);
2994 __ mov(ebx, Immediate(0x49800000)); // 1.0 x 2^20 as single.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002995 __ movd(xmm1, ebx);
2996 __ movd(xmm0, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002997 __ cvtss2sd(xmm1, xmm1);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002998 __ xorps(xmm0, xmm1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002999 __ subsd(xmm0, xmm1);
3000 __ movdbl(FieldOperand(edi, HeapNumber::kValueOffset), xmm0);
3001 } else {
3002 // 0x4130000000000000 is 1.0 x 2^20 as a double.
3003 __ mov(FieldOperand(edi, HeapNumber::kExponentOffset),
3004 Immediate(0x41300000));
3005 __ mov(FieldOperand(edi, HeapNumber::kMantissaOffset), eax);
3006 __ fld_d(FieldOperand(edi, HeapNumber::kValueOffset));
3007 __ mov(FieldOperand(edi, HeapNumber::kMantissaOffset), Immediate(0));
3008 __ fld_d(FieldOperand(edi, HeapNumber::kValueOffset));
3009 __ fsubp(1);
3010 __ fstp_d(FieldOperand(edi, HeapNumber::kValueOffset));
3011 }
3012 __ mov(eax, edi);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003013 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003014}
3015
3016
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003017void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003018 // Load the arguments on the stack and call the stub.
3019 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003020 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003021 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003022 VisitForStackValue(args->at(0));
3023 VisitForStackValue(args->at(1));
3024 VisitForStackValue(args->at(2));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003025 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003026 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003027}
3028
3029
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003030void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003031 // Load the arguments on the stack and call the stub.
3032 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003033 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003034 ASSERT(args->length() == 4);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003035 VisitForStackValue(args->at(0));
3036 VisitForStackValue(args->at(1));
3037 VisitForStackValue(args->at(2));
3038 VisitForStackValue(args->at(3));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003039 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003040 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003041}
3042
3043
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003044void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3045 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003046 ASSERT(args->length() == 1);
3047
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003048 VisitForAccumulatorValue(args->at(0)); // Load the object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003049
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003050 Label done;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003051 // If the object is a smi return the object.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003052 __ JumpIfSmi(eax, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003053 // If the object is not a value type, return the object.
3054 __ CmpObjectType(eax, JS_VALUE_TYPE, ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003055 __ j(not_equal, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003056 __ mov(eax, FieldOperand(eax, JSValue::kValueOffset));
3057
3058 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003059 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003060}
3061
3062
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003063void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3064 ZoneList<Expression*>* args = expr->arguments();
3065 ASSERT(args->length() == 2);
3066 ASSERT_NE(NULL, args->at(1)->AsLiteral());
3067 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->handle()));
3068
3069 VisitForAccumulatorValue(args->at(0)); // Load the object.
3070
3071 Label runtime, done;
3072 Register object = eax;
3073 Register result = eax;
3074 Register scratch = ecx;
3075
3076#ifdef DEBUG
3077 __ AbortIfSmi(object);
3078 __ CmpObjectType(object, JS_DATE_TYPE, scratch);
3079 __ Assert(equal, "Trying to get date field from non-date.");
3080#endif
3081
3082 if (index->value() == 0) {
3083 __ mov(result, FieldOperand(object, JSDate::kValueOffset));
3084 } else {
3085 if (index->value() < JSDate::kFirstUncachedField) {
3086 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3087 __ mov(scratch, Operand::StaticVariable(stamp));
3088 __ cmp(scratch, FieldOperand(object, JSDate::kCacheStampOffset));
3089 __ j(not_equal, &runtime, Label::kNear);
3090 __ mov(result, FieldOperand(object, JSDate::kValueOffset +
3091 kPointerSize * index->value()));
3092 __ jmp(&done);
3093 }
3094 __ bind(&runtime);
3095 __ PrepareCallCFunction(2, scratch);
3096 __ mov(Operand(esp, 0), object);
3097 __ mov(Operand(esp, 1 * kPointerSize), Immediate(index));
3098 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
3099 __ bind(&done);
3100 }
3101 context()->Plug(result);
3102}
3103
3104
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003105void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003106 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003107 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003108 ASSERT(args->length() == 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003109 VisitForStackValue(args->at(0));
3110 VisitForStackValue(args->at(1));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003111
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003112 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003113 MathPowStub stub(MathPowStub::ON_STACK);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003114 __ CallStub(&stub);
3115 } else {
3116 __ CallRuntime(Runtime::kMath_pow, 2);
3117 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003118 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003119}
3120
3121
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003122void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3123 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003124 ASSERT(args->length() == 2);
3125
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003126 VisitForStackValue(args->at(0)); // Load the object.
3127 VisitForAccumulatorValue(args->at(1)); // Load the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003128 __ pop(ebx); // eax = value. ebx = object.
3129
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003130 Label done;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003131 // If the object is a smi, return the value.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003132 __ JumpIfSmi(ebx, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003133
3134 // If the object is not a value type, return the value.
3135 __ CmpObjectType(ebx, JS_VALUE_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003136 __ j(not_equal, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003137
3138 // Store the value.
3139 __ mov(FieldOperand(ebx, JSValue::kValueOffset), eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003140
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003141 // Update the write barrier. Save the value as it will be
3142 // overwritten by the write barrier code and is needed afterward.
3143 __ mov(edx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003144 __ RecordWriteField(ebx, JSValue::kValueOffset, edx, ecx, kDontSaveFPRegs);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003145
3146 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003147 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003148}
3149
3150
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003151void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3152 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003153 ASSERT_EQ(args->length(), 1);
3154
3155 // Load the argument on the stack and call the stub.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003156 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003157
3158 NumberToStringStub stub;
3159 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003160 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003161}
3162
3163
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003164void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3165 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003166 ASSERT(args->length() == 1);
3167
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003168 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003169
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003170 Label done;
3171 StringCharFromCodeGenerator generator(eax, ebx);
3172 generator.GenerateFast(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003173 __ jmp(&done);
3174
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003175 NopRuntimeCallHelper call_helper;
3176 generator.GenerateSlow(masm_, call_helper);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003177
3178 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003179 context()->Plug(ebx);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003180}
3181
3182
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003183void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3184 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003185 ASSERT(args->length() == 2);
3186
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003187 VisitForStackValue(args->at(0));
3188 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003189
3190 Register object = ebx;
3191 Register index = eax;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003192 Register result = edx;
3193
3194 __ pop(object);
3195
3196 Label need_conversion;
3197 Label index_out_of_range;
3198 Label done;
3199 StringCharCodeAtGenerator generator(object,
3200 index,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003201 result,
3202 &need_conversion,
3203 &need_conversion,
3204 &index_out_of_range,
3205 STRING_INDEX_IS_NUMBER);
3206 generator.GenerateFast(masm_);
3207 __ jmp(&done);
3208
3209 __ bind(&index_out_of_range);
3210 // When the index is out of range, the spec requires us to return
3211 // NaN.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003212 __ Set(result, Immediate(isolate()->factory()->nan_value()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003213 __ jmp(&done);
3214
3215 __ bind(&need_conversion);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003216 // Move the undefined value into the result register, which will
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003217 // trigger conversion.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003218 __ Set(result, Immediate(isolate()->factory()->undefined_value()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003219 __ jmp(&done);
3220
3221 NopRuntimeCallHelper call_helper;
3222 generator.GenerateSlow(masm_, call_helper);
3223
3224 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003225 context()->Plug(result);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003226}
3227
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003228
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003229void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3230 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003231 ASSERT(args->length() == 2);
3232
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003233 VisitForStackValue(args->at(0));
3234 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003235
3236 Register object = ebx;
3237 Register index = eax;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003238 Register scratch = edx;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003239 Register result = eax;
3240
3241 __ pop(object);
3242
3243 Label need_conversion;
3244 Label index_out_of_range;
3245 Label done;
3246 StringCharAtGenerator generator(object,
3247 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003248 scratch,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003249 result,
3250 &need_conversion,
3251 &need_conversion,
3252 &index_out_of_range,
3253 STRING_INDEX_IS_NUMBER);
3254 generator.GenerateFast(masm_);
3255 __ jmp(&done);
3256
3257 __ bind(&index_out_of_range);
3258 // When the index is out of range, the spec requires us to return
3259 // the empty string.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003260 __ Set(result, Immediate(isolate()->factory()->empty_string()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003261 __ jmp(&done);
3262
3263 __ bind(&need_conversion);
3264 // Move smi zero into the result register, which will trigger
3265 // conversion.
3266 __ Set(result, Immediate(Smi::FromInt(0)));
3267 __ jmp(&done);
3268
3269 NopRuntimeCallHelper call_helper;
3270 generator.GenerateSlow(masm_, call_helper);
3271
3272 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003273 context()->Plug(result);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003274}
3275
3276
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003277void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3278 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003279 ASSERT_EQ(2, args->length());
3280
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003281 VisitForStackValue(args->at(0));
3282 VisitForStackValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003283
3284 StringAddStub stub(NO_STRING_ADD_FLAGS);
3285 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003286 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003287}
3288
3289
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003290void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3291 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003292 ASSERT_EQ(2, args->length());
3293
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003294 VisitForStackValue(args->at(0));
3295 VisitForStackValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003296
3297 StringCompareStub stub;
3298 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003299 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003300}
3301
3302
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003303void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003304 // Load the argument on the stack and call the stub.
whesse@chromium.org023421e2010-12-21 12:19:12 +00003305 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3306 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003307 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003308 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003309 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003310 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003311 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003312}
3313
3314
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003315void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003316 // Load the argument on the stack and call the stub.
whesse@chromium.org023421e2010-12-21 12:19:12 +00003317 TranscendentalCacheStub stub(TranscendentalCache::COS,
3318 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003319 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003320 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003321 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003322 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003323 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003324}
3325
3326
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003327void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3328 // Load the argument on the stack and call the stub.
3329 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3330 TranscendentalCacheStub::TAGGED);
3331 ZoneList<Expression*>* args = expr->arguments();
3332 ASSERT(args->length() == 1);
3333 VisitForStackValue(args->at(0));
3334 __ CallStub(&stub);
3335 context()->Plug(eax);
3336}
3337
3338
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003339void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003340 // Load the argument on the stack and call the stub.
whesse@chromium.org023421e2010-12-21 12:19:12 +00003341 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3342 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003343 ZoneList<Expression*>* args = expr->arguments();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003344 ASSERT(args->length() == 1);
3345 VisitForStackValue(args->at(0));
3346 __ CallStub(&stub);
3347 context()->Plug(eax);
3348}
3349
3350
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003351void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003352 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003353 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003354 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003355 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003356 __ CallRuntime(Runtime::kMath_sqrt, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003357 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003358}
3359
3360
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003361void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3362 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003363 ASSERT(args->length() >= 2);
3364
danno@chromium.org160a7b02011-04-18 15:51:38 +00003365 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3366 for (int i = 0; i < arg_count + 1; ++i) {
3367 VisitForStackValue(args->at(i));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003368 }
danno@chromium.org160a7b02011-04-18 15:51:38 +00003369 VisitForAccumulatorValue(args->last()); // Function.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003370
danno@chromium.orgc612e022011-11-10 11:38:15 +00003371 // Check for proxy.
3372 Label proxy, done;
3373 __ CmpObjectType(eax, JS_FUNCTION_PROXY_TYPE, ebx);
3374 __ j(equal, &proxy);
3375
danno@chromium.org160a7b02011-04-18 15:51:38 +00003376 // InvokeFunction requires the function in edi. Move it in there.
3377 __ mov(edi, result_register());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003378 ParameterCount count(arg_count);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003379 __ InvokeFunction(edi, count, CALL_FUNCTION,
3380 NullCallWrapper(), CALL_AS_METHOD);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003381 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003382 __ jmp(&done);
3383
3384 __ bind(&proxy);
3385 __ push(eax);
3386 __ CallRuntime(Runtime::kCall, args->length());
3387 __ bind(&done);
3388
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003389 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003390}
3391
3392
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003393void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003394 // Load the arguments on the stack and call the stub.
3395 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003396 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003397 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003398 VisitForStackValue(args->at(0));
3399 VisitForStackValue(args->at(1));
3400 VisitForStackValue(args->at(2));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003401 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003402 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003403}
3404
3405
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003406void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3407 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003408 ASSERT_EQ(2, args->length());
3409
3410 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3411 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3412
3413 Handle<FixedArray> jsfunction_result_caches(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003414 isolate()->global_context()->jsfunction_result_caches());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003415 if (jsfunction_result_caches->length() <= cache_id) {
3416 __ Abort("Attempt to use undefined cache.");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003417 __ mov(eax, isolate()->factory()->undefined_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003418 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003419 return;
3420 }
3421
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003422 VisitForAccumulatorValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003423
3424 Register key = eax;
3425 Register cache = ebx;
3426 Register tmp = ecx;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003427 __ mov(cache, ContextOperand(esi, Context::GLOBAL_INDEX));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003428 __ mov(cache,
3429 FieldOperand(cache, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003430 __ mov(cache, ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003431 __ mov(cache,
3432 FieldOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3433
3434 Label done, not_found;
3435 // tmp now holds finger offset as a smi.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003436 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003437 __ mov(tmp, FieldOperand(cache, JSFunctionResultCache::kFingerOffset));
3438 __ cmp(key, CodeGenerator::FixedArrayElementOperand(cache, tmp));
3439 __ j(not_equal, &not_found);
3440
3441 __ mov(eax, CodeGenerator::FixedArrayElementOperand(cache, tmp, 1));
3442 __ jmp(&done);
3443
3444 __ bind(&not_found);
3445 // Call runtime to perform the lookup.
3446 __ push(cache);
3447 __ push(key);
3448 __ CallRuntime(Runtime::kGetFromCache, 2);
3449
3450 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003451 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003452}
3453
3454
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003455void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3456 ZoneList<Expression*>* args = expr->arguments();
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003457 ASSERT_EQ(2, args->length());
3458
3459 Register right = eax;
3460 Register left = ebx;
3461 Register tmp = ecx;
3462
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003463 VisitForStackValue(args->at(0));
3464 VisitForAccumulatorValue(args->at(1));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003465 __ pop(left);
3466
3467 Label done, fail, ok;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003468 __ cmp(left, right);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003469 __ j(equal, &ok);
3470 // Fail if either is a non-HeapObject.
3471 __ mov(tmp, left);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003472 __ and_(tmp, right);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003473 __ JumpIfSmi(tmp, &fail);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003474 __ mov(tmp, FieldOperand(left, HeapObject::kMapOffset));
3475 __ CmpInstanceType(tmp, JS_REGEXP_TYPE);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003476 __ j(not_equal, &fail);
3477 __ cmp(tmp, FieldOperand(right, HeapObject::kMapOffset));
3478 __ j(not_equal, &fail);
3479 __ mov(tmp, FieldOperand(left, JSRegExp::kDataOffset));
3480 __ cmp(tmp, FieldOperand(right, JSRegExp::kDataOffset));
3481 __ j(equal, &ok);
3482 __ bind(&fail);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003483 __ mov(eax, Immediate(isolate()->factory()->false_value()));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003484 __ jmp(&done);
3485 __ bind(&ok);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003486 __ mov(eax, Immediate(isolate()->factory()->true_value()));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003487 __ bind(&done);
3488
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003489 context()->Plug(eax);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003490}
3491
3492
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003493void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3494 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003495 ASSERT(args->length() == 1);
3496
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003497 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003498
3499 if (FLAG_debug_code) {
3500 __ AbortIfNotString(eax);
3501 }
3502
3503 Label materialize_true, materialize_false;
3504 Label* if_true = NULL;
3505 Label* if_false = NULL;
3506 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003507 context()->PrepareTest(&materialize_true, &materialize_false,
3508 &if_true, &if_false, &fall_through);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003509
3510 __ test(FieldOperand(eax, String::kHashFieldOffset),
3511 Immediate(String::kContainsCachedArrayIndexMask));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003512 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003513 Split(zero, if_true, if_false, fall_through);
3514
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003515 context()->Plug(if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003516}
3517
3518
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003519void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3520 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003521 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003522 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003523
3524 if (FLAG_debug_code) {
3525 __ AbortIfNotString(eax);
3526 }
3527
3528 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
3529 __ IndexFromHash(eax, eax);
3530
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003531 context()->Plug(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003532}
3533
3534
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003535void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003536 Label bailout, done, one_char_separator, long_separator,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003537 non_trivial_array, not_size_one_array, loop,
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003538 loop_1, loop_1_condition, loop_2, loop_2_entry, loop_3, loop_3_entry;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003539
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003540 ZoneList<Expression*>* args = expr->arguments();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003541 ASSERT(args->length() == 2);
3542 // We will leave the separator on the stack until the end of the function.
3543 VisitForStackValue(args->at(1));
3544 // Load this to eax (= array)
3545 VisitForAccumulatorValue(args->at(0));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003546 // All aliases of the same register have disjoint lifetimes.
3547 Register array = eax;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003548 Register elements = no_reg; // Will be eax.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003549
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003550 Register index = edx;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003551
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003552 Register string_length = ecx;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003553
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003554 Register string = esi;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003555
3556 Register scratch = ebx;
3557
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003558 Register array_length = edi;
3559 Register result_pos = no_reg; // Will be edi.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003560
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003561 // Separator operand is already pushed.
3562 Operand separator_operand = Operand(esp, 2 * kPointerSize);
3563 Operand result_operand = Operand(esp, 1 * kPointerSize);
3564 Operand array_length_operand = Operand(esp, 0);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003565 __ sub(esp, Immediate(2 * kPointerSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003566 __ cld();
3567 // Check that the array is a JSArray
whesse@chromium.org7b260152011-06-20 15:33:18 +00003568 __ JumpIfSmi(array, &bailout);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003569 __ CmpObjectType(array, JS_ARRAY_TYPE, scratch);
3570 __ j(not_equal, &bailout);
3571
3572 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003573 __ CheckFastElements(scratch, &bailout);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003574
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003575 // If the array has length zero, return the empty string.
3576 __ mov(array_length, FieldOperand(array, JSArray::kLengthOffset));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003577 __ SmiUntag(array_length);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003578 __ j(not_zero, &non_trivial_array);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003579 __ mov(result_operand, isolate()->factory()->empty_string());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003580 __ jmp(&done);
3581
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003582 // Save the array length.
3583 __ bind(&non_trivial_array);
3584 __ mov(array_length_operand, array_length);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003585
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003586 // Save the FixedArray containing array's elements.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003587 // End of array's live range.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003588 elements = array;
3589 __ mov(elements, FieldOperand(array, JSArray::kElementsOffset));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003590 array = no_reg;
3591
3592
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003593 // Check that all array elements are sequential ASCII strings, and
3594 // accumulate the sum of their lengths, as a smi-encoded value.
3595 __ Set(index, Immediate(0));
3596 __ Set(string_length, Immediate(0));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003597 // Loop condition: while (index < length).
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003598 // Live loop registers: index, array_length, string,
3599 // scratch, string_length, elements.
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003600 if (FLAG_debug_code) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003601 __ cmp(index, array_length);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003602 __ Assert(less, "No empty arrays here in EmitFastAsciiArrayJoin");
3603 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003604 __ bind(&loop);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003605 __ mov(string, FieldOperand(elements,
3606 index,
3607 times_pointer_size,
3608 FixedArray::kHeaderSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003609 __ JumpIfSmi(string, &bailout);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003610 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
3611 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
3612 __ and_(scratch, Immediate(
3613 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
3614 __ cmp(scratch, kStringTag | kAsciiStringTag | kSeqStringTag);
3615 __ j(not_equal, &bailout);
3616 __ add(string_length,
3617 FieldOperand(string, SeqAsciiString::kLengthOffset));
3618 __ j(overflow, &bailout);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003619 __ add(index, Immediate(1));
3620 __ cmp(index, array_length);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003621 __ j(less, &loop);
3622
3623 // If array_length is 1, return elements[0], a string.
3624 __ cmp(array_length, 1);
3625 __ j(not_equal, &not_size_one_array);
3626 __ mov(scratch, FieldOperand(elements, FixedArray::kHeaderSize));
3627 __ mov(result_operand, scratch);
3628 __ jmp(&done);
3629
3630 __ bind(&not_size_one_array);
3631
3632 // End of array_length live range.
3633 result_pos = array_length;
3634 array_length = no_reg;
3635
3636 // Live registers:
3637 // string_length: Sum of string lengths, as a smi.
3638 // elements: FixedArray of strings.
3639
3640 // Check that the separator is a flat ASCII string.
3641 __ mov(string, separator_operand);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003642 __ JumpIfSmi(string, &bailout);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003643 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
3644 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003645 __ and_(scratch, Immediate(
3646 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003647 __ cmp(scratch, ASCII_STRING_TYPE);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003648 __ j(not_equal, &bailout);
3649
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003650 // Add (separator length times array_length) - separator length
3651 // to string_length.
3652 __ mov(scratch, separator_operand);
3653 __ mov(scratch, FieldOperand(scratch, SeqAsciiString::kLengthOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003654 __ sub(string_length, scratch); // May be negative, temporarily.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003655 __ imul(scratch, array_length_operand);
3656 __ j(overflow, &bailout);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003657 __ add(string_length, scratch);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003658 __ j(overflow, &bailout);
3659
3660 __ shr(string_length, 1);
3661 // Live registers and stack values:
3662 // string_length
3663 // elements
3664 __ AllocateAsciiString(result_pos, string_length, scratch,
3665 index, string, &bailout);
3666 __ mov(result_operand, result_pos);
3667 __ lea(result_pos, FieldOperand(result_pos, SeqAsciiString::kHeaderSize));
3668
3669
3670 __ mov(string, separator_operand);
3671 __ cmp(FieldOperand(string, SeqAsciiString::kLengthOffset),
3672 Immediate(Smi::FromInt(1)));
3673 __ j(equal, &one_char_separator);
3674 __ j(greater, &long_separator);
3675
3676
3677 // Empty separator case
3678 __ mov(index, Immediate(0));
3679 __ jmp(&loop_1_condition);
3680 // Loop condition: while (index < length).
3681 __ bind(&loop_1);
3682 // Each iteration of the loop concatenates one string to the result.
3683 // Live values in registers:
3684 // index: which element of the elements array we are adding to the result.
3685 // result_pos: the position to which we are currently copying characters.
3686 // elements: the FixedArray of strings we are joining.
3687
3688 // Get string = array[index].
3689 __ mov(string, FieldOperand(elements, index,
3690 times_pointer_size,
3691 FixedArray::kHeaderSize));
3692 __ mov(string_length,
3693 FieldOperand(string, String::kLengthOffset));
3694 __ shr(string_length, 1);
3695 __ lea(string,
3696 FieldOperand(string, SeqAsciiString::kHeaderSize));
3697 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003698 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003699 __ bind(&loop_1_condition);
3700 __ cmp(index, array_length_operand);
3701 __ j(less, &loop_1); // End while (index < length).
3702 __ jmp(&done);
3703
3704
3705
3706 // One-character separator case
3707 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003708 // Replace separator with its ASCII character value.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003709 __ mov_b(scratch, FieldOperand(string, SeqAsciiString::kHeaderSize));
3710 __ mov_b(separator_operand, scratch);
3711
3712 __ Set(index, Immediate(0));
3713 // Jump into the loop after the code that copies the separator, so the first
3714 // element is not preceded by a separator
3715 __ jmp(&loop_2_entry);
3716 // Loop condition: while (index < length).
3717 __ bind(&loop_2);
3718 // Each iteration of the loop concatenates one string to the result.
3719 // Live values in registers:
3720 // index: which element of the elements array we are adding to the result.
3721 // result_pos: the position to which we are currently copying characters.
3722
3723 // Copy the separator character to the result.
3724 __ mov_b(scratch, separator_operand);
3725 __ mov_b(Operand(result_pos, 0), scratch);
3726 __ inc(result_pos);
3727
3728 __ bind(&loop_2_entry);
3729 // Get string = array[index].
3730 __ mov(string, FieldOperand(elements, index,
3731 times_pointer_size,
3732 FixedArray::kHeaderSize));
3733 __ mov(string_length,
3734 FieldOperand(string, String::kLengthOffset));
3735 __ shr(string_length, 1);
3736 __ lea(string,
3737 FieldOperand(string, SeqAsciiString::kHeaderSize));
3738 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003739 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003740
3741 __ cmp(index, array_length_operand);
3742 __ j(less, &loop_2); // End while (index < length).
3743 __ jmp(&done);
3744
3745
3746 // Long separator case (separator is more than one character).
3747 __ bind(&long_separator);
3748
3749 __ Set(index, Immediate(0));
3750 // Jump into the loop after the code that copies the separator, so the first
3751 // element is not preceded by a separator
3752 __ jmp(&loop_3_entry);
3753 // Loop condition: while (index < length).
3754 __ bind(&loop_3);
3755 // Each iteration of the loop concatenates one string to the result.
3756 // Live values in registers:
3757 // index: which element of the elements array we are adding to the result.
3758 // result_pos: the position to which we are currently copying characters.
3759
3760 // Copy the separator to the result.
3761 __ mov(string, separator_operand);
3762 __ mov(string_length,
3763 FieldOperand(string, String::kLengthOffset));
3764 __ shr(string_length, 1);
3765 __ lea(string,
3766 FieldOperand(string, SeqAsciiString::kHeaderSize));
3767 __ CopyBytes(string, result_pos, string_length, scratch);
3768
3769 __ bind(&loop_3_entry);
3770 // Get string = array[index].
3771 __ mov(string, FieldOperand(elements, index,
3772 times_pointer_size,
3773 FixedArray::kHeaderSize));
3774 __ mov(string_length,
3775 FieldOperand(string, String::kLengthOffset));
3776 __ shr(string_length, 1);
3777 __ lea(string,
3778 FieldOperand(string, SeqAsciiString::kHeaderSize));
3779 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003780 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003781
3782 __ cmp(index, array_length_operand);
3783 __ j(less, &loop_3); // End while (index < length).
3784 __ jmp(&done);
3785
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003786
3787 __ bind(&bailout);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003788 __ mov(result_operand, isolate()->factory()->undefined_value());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003789 __ bind(&done);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003790 __ mov(eax, result_operand);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003791 // Drop temp values from the stack, and restore context register.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003792 __ add(esp, Immediate(3 * kPointerSize));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003793
3794 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
3795 context()->Plug(eax);
3796}
3797
3798
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003799void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003800 Handle<String> name = expr->name();
3801 if (name->length() > 0 && name->Get(0) == '_') {
3802 Comment cmnt(masm_, "[ InlineRuntimeCall");
3803 EmitInlineRuntimeCall(expr);
3804 return;
3805 }
3806
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003807 Comment cmnt(masm_, "[ CallRuntime");
3808 ZoneList<Expression*>* args = expr->arguments();
3809
3810 if (expr->is_jsruntime()) {
3811 // Prepare for calling JS runtime function.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003812 __ mov(eax, GlobalObjectOperand());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003813 __ push(FieldOperand(eax, GlobalObject::kBuiltinsOffset));
3814 }
3815
3816 // Push the arguments ("left-to-right").
3817 int arg_count = args->length();
3818 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003819 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003820 }
3821
3822 if (expr->is_jsruntime()) {
3823 // Call the JS runtime function via a call IC.
3824 __ Set(ecx, Immediate(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003825 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
lrn@chromium.org34e60782011-09-15 07:25:40 +00003826 Handle<Code> ic =
3827 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00003828 CallIC(ic, mode, expr->id());
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003829 // Restore context register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003830 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
3831 } else {
3832 // Call the C runtime function.
3833 __ CallRuntime(expr->function(), arg_count);
3834 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003835 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003836}
3837
3838
3839void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
3840 switch (expr->op()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003841 case Token::DELETE: {
3842 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003843 Property* property = expr->expression()->AsProperty();
3844 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003845
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003846 if (property != NULL) {
3847 VisitForStackValue(property->obj());
3848 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003849 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
3850 ? kNonStrictMode : kStrictMode;
3851 __ push(Immediate(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003852 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003853 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003854 } else if (proxy != NULL) {
3855 Variable* var = proxy->var();
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003856 // Delete of an unqualified identifier is disallowed in strict mode
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003857 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003858 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003859 if (var->IsUnallocated()) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003860 __ push(GlobalObjectOperand());
3861 __ push(Immediate(var->name()));
3862 __ push(Immediate(Smi::FromInt(kNonStrictMode)));
3863 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3864 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003865 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
3866 // Result of deleting non-global variables is false. 'this' is
3867 // not really a variable, though we implement it as one. The
3868 // subexpression does not have side effects.
3869 context()->Plug(var->is_this());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003870 } else {
3871 // Non-global variable. Call the runtime to try to delete from the
3872 // context where the variable was introduced.
3873 __ push(context_register());
3874 __ push(Immediate(var->name()));
3875 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3876 context()->Plug(eax);
3877 }
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00003878 } else {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003879 // Result of deleting non-property, non-variable reference is true.
3880 // The subexpression may have side effects.
3881 VisitForEffect(expr->expression());
3882 context()->Plug(true);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003883 }
3884 break;
3885 }
3886
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003887 case Token::VOID: {
3888 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3889 VisitForEffect(expr->expression());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003890 context()->Plug(isolate()->factory()->undefined_value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003891 break;
3892 }
3893
3894 case Token::NOT: {
3895 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003896 if (context()->IsEffect()) {
3897 // Unary NOT has no side effects so it's only necessary to visit the
3898 // subexpression. Match the optimizing compiler by not branching.
3899 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003900 } else if (context()->IsTest()) {
3901 const TestContext* test = TestContext::cast(context());
3902 // The labels are swapped for the recursive call.
3903 VisitForControl(expr->expression(),
3904 test->false_label(),
3905 test->true_label(),
3906 test->fall_through());
3907 context()->Plug(test->true_label(), test->false_label());
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003908 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003909 // We handle value contexts explicitly rather than simply visiting
3910 // for control and plugging the control flow into the context,
3911 // because we need to prepare a pair of extra administrative AST ids
3912 // for the optimizing compiler.
3913 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3914 Label materialize_true, materialize_false, done;
3915 VisitForControl(expr->expression(),
3916 &materialize_false,
3917 &materialize_true,
3918 &materialize_true);
3919 __ bind(&materialize_true);
3920 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3921 if (context()->IsAccumulatorValue()) {
3922 __ mov(eax, isolate()->factory()->true_value());
3923 } else {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003924 __ Push(isolate()->factory()->true_value());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003925 }
3926 __ jmp(&done, Label::kNear);
3927 __ bind(&materialize_false);
3928 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3929 if (context()->IsAccumulatorValue()) {
3930 __ mov(eax, isolate()->factory()->false_value());
3931 } else {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003932 __ Push(isolate()->factory()->false_value());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003933 }
3934 __ bind(&done);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003935 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003936 break;
3937 }
3938
3939 case Token::TYPEOF: {
3940 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003941 { StackValueContext context(this);
3942 VisitForTypeofValue(expr->expression());
3943 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003944 __ CallRuntime(Runtime::kTypeof, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003945 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003946 break;
3947 }
3948
3949 case Token::ADD: {
3950 Comment cmt(masm_, "[ UnaryOperation (ADD)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003951 VisitForAccumulatorValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003952 Label no_conversion;
whesse@chromium.org7b260152011-06-20 15:33:18 +00003953 __ JumpIfSmi(result_register(), &no_conversion);
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003954 ToNumberStub convert_stub;
3955 __ CallStub(&convert_stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003956 __ bind(&no_conversion);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003957 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003958 break;
3959 }
3960
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003961 case Token::SUB:
3962 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003963 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003964
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003965 case Token::BIT_NOT:
3966 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003967 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003968
3969 default:
3970 UNREACHABLE();
3971 }
3972}
3973
3974
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003975void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3976 const char* comment) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003977 Comment cmt(masm_, comment);
3978 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3979 UnaryOverwriteMode overwrite =
3980 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003981 UnaryOpStub stub(expr->op(), overwrite);
3982 // UnaryOpStub expects the argument to be in the
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003983 // accumulator register eax.
3984 VisitForAccumulatorValue(expr->expression());
3985 SetSourcePosition(expr->position());
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00003986 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003987 context()->Plug(eax);
3988}
3989
3990
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003991void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
3992 Comment cmnt(masm_, "[ CountOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00003993 SetSourcePosition(expr->position());
3994
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003995 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3996 // as the left-hand side.
3997 if (!expr->expression()->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003998 VisitForEffect(expr->expression());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003999 return;
4000 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004001
4002 // Expression can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00004003 // slot.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004004 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
4005 LhsKind assign_type = VARIABLE;
4006 Property* prop = expr->expression()->AsProperty();
4007 // In case of a property we use the uninitialized expression context
4008 // of the key to detect a named property.
4009 if (prop != NULL) {
4010 assign_type =
4011 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
4012 }
4013
4014 // Evaluate expression and get value.
4015 if (assign_type == VARIABLE) {
4016 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004017 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00004018 EmitVariableLoad(expr->expression()->AsVariableProxy());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004019 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004020 // Reserve space for result of postfix operation.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004021 if (expr->is_postfix() && !context()->IsEffect()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004022 __ push(Immediate(Smi::FromInt(0)));
4023 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004024 if (assign_type == NAMED_PROPERTY) {
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004025 // Put the object both on the stack and in edx.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004026 VisitForAccumulatorValue(prop->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00004027 __ push(eax);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004028 __ mov(edx, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004029 EmitNamedPropertyLoad(prop);
4030 } else {
whesse@chromium.org7b260152011-06-20 15:33:18 +00004031 VisitForStackValue(prop->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004032 VisitForStackValue(prop->key());
4033 __ mov(edx, Operand(esp, kPointerSize)); // Object.
4034 __ mov(ecx, Operand(esp, 0)); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004035 EmitKeyedPropertyLoad(prop);
4036 }
4037 }
4038
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004039 // We need a second deoptimization point after loading the value
4040 // in case evaluating the property load my have a side effect.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004041 if (assign_type == VARIABLE) {
4042 PrepareForBailout(expr->expression(), TOS_REG);
4043 } else {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00004044 PrepareForBailoutForId(expr->CountId(), TOS_REG);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004045 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004046
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004047 // Call ToNumber only if operand is not a smi.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004048 Label no_conversion;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004049 if (ShouldInlineSmiCase(expr->op())) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00004050 __ JumpIfSmi(eax, &no_conversion, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004051 }
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004052 ToNumberStub convert_stub;
4053 __ CallStub(&convert_stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004054 __ bind(&no_conversion);
4055
4056 // Save result for postfix expressions.
4057 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004058 if (!context()->IsEffect()) {
4059 // Save the result on the stack. If we have a named or keyed property
4060 // we store the result under the receiver that is currently on top
4061 // of the stack.
4062 switch (assign_type) {
4063 case VARIABLE:
4064 __ push(eax);
4065 break;
4066 case NAMED_PROPERTY:
4067 __ mov(Operand(esp, kPointerSize), eax);
4068 break;
4069 case KEYED_PROPERTY:
4070 __ mov(Operand(esp, 2 * kPointerSize), eax);
4071 break;
4072 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004073 }
4074 }
4075
4076 // Inline smi case if we are in a loop.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004077 Label done, stub_call;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004078 JumpPatchSite patch_site(masm_);
4079
ricow@chromium.org65fae842010-08-25 15:26:24 +00004080 if (ShouldInlineSmiCase(expr->op())) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004081 if (expr->op() == Token::INC) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004082 __ add(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004083 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004084 __ sub(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004085 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004086 __ j(overflow, &stub_call, Label::kNear);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004087 // We could eliminate this smi check if we split the code at
4088 // the first smi check before calling ToNumber.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004089 patch_site.EmitJumpIfSmi(eax, &done, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004090
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004091 __ bind(&stub_call);
4092 // Call stub. Undo operation first.
4093 if (expr->op() == Token::INC) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004094 __ sub(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004095 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004096 __ add(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004097 }
4098 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004099
4100 // Record position before stub call.
4101 SetSourcePosition(expr->position());
4102
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004103 // Call stub for +1/-1.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004104 __ mov(edx, eax);
4105 __ mov(eax, Immediate(Smi::FromInt(1)));
danno@chromium.org40cb8782011-05-25 07:58:50 +00004106 BinaryOpStub stub(expr->binary_op(), NO_OVERWRITE);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004107 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004108 patch_site.EmitPatchInfo();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004109 __ bind(&done);
4110
4111 // Store the value returned in eax.
4112 switch (assign_type) {
4113 case VARIABLE:
4114 if (expr->is_postfix()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004115 // Perform the assignment as if via '='.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004116 { EffectContext context(this);
4117 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4118 Token::ASSIGN);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004119 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4120 context.Plug(eax);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004121 }
4122 // For all contexts except EffectContext We have the result on
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004123 // top of the stack.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004124 if (!context()->IsEffect()) {
4125 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004126 }
4127 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004128 // Perform the assignment as if via '='.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004129 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004130 Token::ASSIGN);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004131 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4132 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004133 }
4134 break;
4135 case NAMED_PROPERTY: {
4136 __ mov(ecx, prop->key()->AsLiteral()->handle());
4137 __ pop(edx);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004138 Handle<Code> ic = is_classic_mode()
4139 ? isolate()->builtins()->StoreIC_Initialize()
4140 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004141 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004142 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004143 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004144 if (!context()->IsEffect()) {
4145 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004146 }
4147 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004148 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004149 }
4150 break;
4151 }
4152 case KEYED_PROPERTY: {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004153 __ pop(ecx);
4154 __ pop(edx);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004155 Handle<Code> ic = is_classic_mode()
4156 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4157 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004158 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004159 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004160 if (expr->is_postfix()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004161 // Result is on the stack
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004162 if (!context()->IsEffect()) {
4163 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004164 }
4165 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004166 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004167 }
4168 break;
4169 }
4170 }
4171}
4172
4173
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004174void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004175 VariableProxy* proxy = expr->AsVariableProxy();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004176 ASSERT(!context()->IsEffect());
4177 ASSERT(!context()->IsTest());
4178
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004179 if (proxy != NULL && proxy->var()->IsUnallocated()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004180 Comment cmnt(masm_, "Global variable");
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004181 __ mov(edx, GlobalObjectOperand());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004182 __ mov(ecx, Immediate(proxy->name()));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004183 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004184 // Use a regular load, not a contextual load, to avoid a reference
4185 // error.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004186 CallIC(ic);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004187 PrepareForBailout(expr, TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004188 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004189 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004190 Label done, slow;
4191
4192 // Generate code for loading from variables potentially shadowed
4193 // by eval-introduced variables.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004194 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004195
4196 __ bind(&slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004197 __ push(esi);
4198 __ push(Immediate(proxy->name()));
4199 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004200 PrepareForBailout(expr, TOS_REG);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004201 __ bind(&done);
4202
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004203 context()->Plug(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004204 } else {
4205 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004206 VisitInDuplicateContext(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004207 }
4208}
4209
4210
ager@chromium.org04921a82011-06-27 13:21:41 +00004211void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004212 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004213 Handle<String> check) {
4214 Label materialize_true, materialize_false;
4215 Label* if_true = NULL;
4216 Label* if_false = NULL;
4217 Label* fall_through = NULL;
4218 context()->PrepareTest(&materialize_true, &materialize_false,
4219 &if_true, &if_false, &fall_through);
4220
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004221 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004222 VisitForTypeofValue(sub_expr);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004223 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004224 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004225
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004226 if (check->Equals(isolate()->heap()->number_symbol())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004227 __ JumpIfSmi(eax, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004228 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004229 isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004230 Split(equal, if_true, if_false, fall_through);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004231 } else if (check->Equals(isolate()->heap()->string_symbol())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004232 __ JumpIfSmi(eax, if_false);
4233 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edx);
4234 __ j(above_equal, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004235 // Check for undetectable objects => false.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004236 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
4237 1 << Map::kIsUndetectable);
4238 Split(zero, if_true, if_false, fall_through);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004239 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4240 __ cmp(eax, isolate()->factory()->true_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004241 __ j(equal, if_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004242 __ cmp(eax, isolate()->factory()->false_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004243 Split(equal, if_true, if_false, fall_through);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004244 } else if (FLAG_harmony_typeof &&
4245 check->Equals(isolate()->heap()->null_symbol())) {
4246 __ cmp(eax, isolate()->factory()->null_value());
4247 Split(equal, if_true, if_false, fall_through);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004248 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4249 __ cmp(eax, isolate()->factory()->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004250 __ j(equal, if_true);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004251 __ JumpIfSmi(eax, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004252 // Check for undetectable objects => true.
4253 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
4254 __ movzx_b(ecx, FieldOperand(edx, Map::kBitFieldOffset));
4255 __ test(ecx, Immediate(1 << Map::kIsUndetectable));
4256 Split(not_zero, if_true, if_false, fall_through);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004257 } else if (check->Equals(isolate()->heap()->function_symbol())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004258 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004259 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4260 __ CmpObjectType(eax, JS_FUNCTION_TYPE, edx);
4261 __ j(equal, if_true);
4262 __ CmpInstanceType(edx, JS_FUNCTION_PROXY_TYPE);
4263 Split(equal, if_true, if_false, fall_through);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004264 } else if (check->Equals(isolate()->heap()->object_symbol())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004265 __ JumpIfSmi(eax, if_false);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004266 if (!FLAG_harmony_typeof) {
4267 __ cmp(eax, isolate()->factory()->null_value());
4268 __ j(equal, if_true);
4269 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004270 __ CmpObjectType(eax, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE, edx);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004271 __ j(below, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004272 __ CmpInstanceType(edx, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
4273 __ j(above, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004274 // Check for undetectable objects => false.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004275 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
4276 1 << Map::kIsUndetectable);
4277 Split(zero, if_true, if_false, fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004278 } else {
4279 if (if_false != fall_through) __ jmp(if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004280 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004281 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004282}
4283
4284
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004285void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
4286 Comment cmnt(masm_, "[ CompareOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00004287 SetSourcePosition(expr->position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004288
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004289 // First we try a fast inlined version of the compare when one of
4290 // the operands is a literal.
4291 if (TryLiteralCompare(expr)) return;
4292
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004293 // Always perform the comparison for its control flow. Pack the result
4294 // into the expression's context after the comparison is performed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004295 Label materialize_true, materialize_false;
4296 Label* if_true = NULL;
4297 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004298 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004299 context()->PrepareTest(&materialize_true, &materialize_false,
4300 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004301
ager@chromium.org04921a82011-06-27 13:21:41 +00004302 Token::Value op = expr->op();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004303 VisitForStackValue(expr->left());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004304 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004305 case Token::IN:
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004306 VisitForStackValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004307 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004308 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004309 __ cmp(eax, isolate()->factory()->true_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004310 Split(equal, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004311 break;
4312
4313 case Token::INSTANCEOF: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004314 VisitForStackValue(expr->right());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004315 InstanceofStub stub(InstanceofStub::kNoFlags);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004316 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004317 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004318 __ test(eax, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004319 // The stub returns 0 for true.
4320 Split(zero, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004321 break;
4322 }
4323
4324 default: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004325 VisitForAccumulatorValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004326 Condition cc = no_condition;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004327 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004328 case Token::EQ_STRICT:
ricow@chromium.org65fae842010-08-25 15:26:24 +00004329 case Token::EQ:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004330 cc = equal;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004331 break;
4332 case Token::LT:
4333 cc = less;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004334 break;
4335 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004336 cc = greater;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004337 break;
4338 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004339 cc = less_equal;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004340 break;
4341 case Token::GTE:
4342 cc = greater_equal;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004343 break;
4344 case Token::IN:
4345 case Token::INSTANCEOF:
4346 default:
4347 UNREACHABLE();
4348 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004349 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004350
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004351 bool inline_smi_code = ShouldInlineSmiCase(op);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004352 JumpPatchSite patch_site(masm_);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004353 if (inline_smi_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004354 Label slow_case;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004355 __ mov(ecx, edx);
4356 __ or_(ecx, eax);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004357 patch_site.EmitJumpIfNotSmi(ecx, &slow_case, Label::kNear);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004358 __ cmp(edx, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004359 Split(cc, if_true, if_false, NULL);
4360 __ bind(&slow_case);
4361 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004362
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004363 // Record position and call the compare IC.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004364 SetSourcePosition(expr->position());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004365 Handle<Code> ic = CompareIC::GetUninitialized(op);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004366 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004367 patch_site.EmitPatchInfo();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004368
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004369 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004370 __ test(eax, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004371 Split(cc, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004372 }
4373 }
4374
4375 // Convert the result of the comparison into one expected for this
4376 // expression's context.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004377 context()->Plug(if_true, if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004378}
4379
4380
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004381void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4382 Expression* sub_expr,
4383 NilValue nil) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004384 Label materialize_true, materialize_false;
4385 Label* if_true = NULL;
4386 Label* if_false = NULL;
4387 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004388 context()->PrepareTest(&materialize_true, &materialize_false,
4389 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004390
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004391 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004392 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004393 Handle<Object> nil_value = nil == kNullValue ?
4394 isolate()->factory()->null_value() :
4395 isolate()->factory()->undefined_value();
4396 __ cmp(eax, nil_value);
4397 if (expr->op() == Token::EQ_STRICT) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004398 Split(equal, if_true, if_false, fall_through);
4399 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004400 Handle<Object> other_nil_value = nil == kNullValue ?
4401 isolate()->factory()->undefined_value() :
4402 isolate()->factory()->null_value();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004403 __ j(equal, if_true);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004404 __ cmp(eax, other_nil_value);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004405 __ j(equal, if_true);
whesse@chromium.org7b260152011-06-20 15:33:18 +00004406 __ JumpIfSmi(eax, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004407 // It can be an undetectable object.
4408 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
4409 __ movzx_b(edx, FieldOperand(edx, Map::kBitFieldOffset));
4410 __ test(edx, Immediate(1 << Map::kIsUndetectable));
4411 Split(not_zero, if_true, if_false, fall_through);
4412 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004413 context()->Plug(if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004414}
4415
4416
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004417void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
4418 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004419 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004420}
4421
4422
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00004423Register FullCodeGenerator::result_register() {
4424 return eax;
4425}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004426
4427
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00004428Register FullCodeGenerator::context_register() {
4429 return esi;
4430}
4431
4432
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004433void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
4434 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4435 __ mov(Operand(ebp, frame_offset), value);
4436}
4437
4438
4439void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004440 __ mov(dst, ContextOperand(esi, context_index));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004441}
4442
4443
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004444void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004445 Scope* declaration_scope = scope()->DeclarationScope();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00004446 if (declaration_scope->is_global_scope() ||
4447 declaration_scope->is_module_scope()) {
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004448 // Contexts nested in the global context have a canonical empty function
4449 // as their closure, not the anonymous closure containing the global
4450 // code. Pass a smi sentinel and let the runtime look up the empty
4451 // function.
4452 __ push(Immediate(Smi::FromInt(0)));
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004453 } else if (declaration_scope->is_eval_scope()) {
4454 // Contexts nested inside eval code have the same closure as the context
4455 // calling eval, not the anonymous closure containing the eval code.
4456 // Fetch it from the context.
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004457 __ push(ContextOperand(esi, Context::CLOSURE_INDEX));
4458 } else {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004459 ASSERT(declaration_scope->is_function_scope());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004460 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
4461 }
4462}
4463
4464
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004465// ----------------------------------------------------------------------------
4466// Non-local control flow support.
4467
4468void FullCodeGenerator::EnterFinallyBlock() {
4469 // Cook return address on top of stack (smi encoded Code* delta)
4470 ASSERT(!result_register().is(edx));
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004471 __ pop(edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004472 __ sub(edx, Immediate(masm_->CodeObject()));
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00004473 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4474 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004475 __ SmiTag(edx);
4476 __ push(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004477
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004478 // Store result register while executing finally block.
4479 __ push(result_register());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004480
4481 // Store pending message while executing finally block.
4482 ExternalReference pending_message_obj =
4483 ExternalReference::address_of_pending_message_obj(isolate());
4484 __ mov(edx, Operand::StaticVariable(pending_message_obj));
4485 __ push(edx);
4486
4487 ExternalReference has_pending_message =
4488 ExternalReference::address_of_has_pending_message(isolate());
4489 __ mov(edx, Operand::StaticVariable(has_pending_message));
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004490 __ SmiTag(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004491 __ push(edx);
4492
4493 ExternalReference pending_message_script =
4494 ExternalReference::address_of_pending_message_script(isolate());
4495 __ mov(edx, Operand::StaticVariable(pending_message_script));
4496 __ push(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004497}
4498
4499
4500void FullCodeGenerator::ExitFinallyBlock() {
4501 ASSERT(!result_register().is(edx));
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004502 // Restore pending message from stack.
4503 __ pop(edx);
4504 ExternalReference pending_message_script =
4505 ExternalReference::address_of_pending_message_script(isolate());
4506 __ mov(Operand::StaticVariable(pending_message_script), edx);
4507
4508 __ pop(edx);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004509 __ SmiUntag(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004510 ExternalReference has_pending_message =
4511 ExternalReference::address_of_has_pending_message(isolate());
4512 __ mov(Operand::StaticVariable(has_pending_message), edx);
4513
4514 __ pop(edx);
4515 ExternalReference pending_message_obj =
4516 ExternalReference::address_of_pending_message_obj(isolate());
4517 __ mov(Operand::StaticVariable(pending_message_obj), edx);
4518
4519 // Restore result register from stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004520 __ pop(result_register());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004521
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004522 // Uncook return address.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004523 __ pop(edx);
4524 __ SmiUntag(edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004525 __ add(edx, Immediate(masm_->CodeObject()));
4526 __ jmp(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004527}
4528
4529
4530#undef __
4531
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004532#define __ ACCESS_MASM(masm())
4533
4534FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4535 int* stack_depth,
4536 int* context_length) {
4537 // The macros used here must preserve the result register.
4538
4539 // Because the handler block contains the context of the finally
4540 // code, we can restore it directly from there for the finally code
4541 // rather than iteratively unwinding contexts via their previous
4542 // links.
4543 __ Drop(*stack_depth); // Down to the handler block.
4544 if (*context_length > 0) {
4545 // Restore the context to its dedicated register and the stack.
4546 __ mov(esi, Operand(esp, StackHandlerConstants::kContextOffset));
4547 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
4548 }
4549 __ PopTryHandler();
4550 __ call(finally_entry_);
4551
4552 *stack_depth = 0;
4553 *context_length = 0;
4554 return previous_;
4555}
4556
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004557#undef __
4558
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004559} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004560
4561#endif // V8_TARGET_ARCH_IA32