blob: 75253c07a462c60b461e7d0d32498fc667870762 [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
yangguo@chromium.org56454712012-02-16 15:33:53 +0000328void FullCodeGenerator::EmitStackCheck(IterationStatement* stmt,
329 Label* back_edge_target) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000330 Comment cmnt(masm_, "[ Stack check");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000331 Label ok;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000332
333 if (FLAG_count_based_interrupts) {
334 int weight = 1;
335 if (FLAG_weighted_back_edges) {
336 ASSERT(back_edge_target->is_bound());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000337 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
danno@chromium.org88aa0582012-03-23 15:11:57 +0000338 weight = Min(kMaxBackEdgeWeight,
danno@chromium.org129d3982012-07-25 15:01:47 +0000339 Max(1, distance / kBackEdgeDistanceUnit));
yangguo@chromium.org56454712012-02-16 15:33:53 +0000340 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000341 EmitProfilingCounterDecrement(weight);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000342 __ j(positive, &ok, Label::kNear);
343 InterruptStub stub;
344 __ CallStub(&stub);
345 } else {
346 // Count based interrupts happen often enough when they are enabled
347 // that the additional stack checks are not necessary (they would
348 // only check for interrupts).
349 ExternalReference stack_limit =
350 ExternalReference::address_of_stack_limit(isolate());
351 __ cmp(esp, Operand::StaticVariable(stack_limit));
352 __ j(above_equal, &ok, Label::kNear);
353 StackCheckStub stub;
354 __ CallStub(&stub);
355 }
356
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000357 // Record a mapping of this PC offset to the OSR id. This is used to find
358 // the AST id from the unoptimized code in order to use it as a key into
359 // the deoptimization input data found in the optimized code.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000360 RecordStackCheck(stmt->OsrEntryId());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000361
362 // Loop stack checks can be patched to perform on-stack replacement. In
363 // order to decide whether or not to perform OSR we embed the loop depth
364 // in a test instruction after the call so we can extract it from the OSR
365 // builtin.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000366 ASSERT(loop_depth() > 0);
367 __ test(eax, Immediate(Min(loop_depth(), Code::kMaxLoopNestingMarker)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000368
yangguo@chromium.org56454712012-02-16 15:33:53 +0000369 if (FLAG_count_based_interrupts) {
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000370 EmitProfilingCounterReset();
yangguo@chromium.org56454712012-02-16 15:33:53 +0000371 }
372
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000373 __ bind(&ok);
374 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
375 // Record a mapping of the OSR id to this PC. This is used if the OSR
376 // entry becomes the target of a bailout. We don't expect it to be, but
377 // we want it to work if it is.
378 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000379}
380
381
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000382void FullCodeGenerator::EmitReturnSequence() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000383 Comment cmnt(masm_, "[ Return sequence");
384 if (return_label_.is_bound()) {
385 __ jmp(&return_label_);
386 } else {
387 // Common return label
388 __ bind(&return_label_);
389 if (FLAG_trace) {
390 __ push(eax);
391 __ CallRuntime(Runtime::kTraceExit, 1);
392 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000393 if (FLAG_interrupt_at_exit || FLAG_self_optimization) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000394 // Pretend that the exit is a backwards jump to the entry.
395 int weight = 1;
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000396 if (info_->ShouldSelfOptimize()) {
397 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
398 } else if (FLAG_weighted_back_edges) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000399 int distance = masm_->pc_offset();
danno@chromium.org88aa0582012-03-23 15:11:57 +0000400 weight = Min(kMaxBackEdgeWeight,
danno@chromium.org129d3982012-07-25 15:01:47 +0000401 Max(1, distance / kBackEdgeDistanceUnit));
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000402 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000403 EmitProfilingCounterDecrement(weight);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000404 Label ok;
405 __ j(positive, &ok, Label::kNear);
406 __ push(eax);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000407 if (info_->ShouldSelfOptimize() && FLAG_direct_self_opt) {
408 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
409 __ CallRuntime(Runtime::kOptimizeFunctionOnNextCall, 1);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000410 } else {
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000411 InterruptStub stub;
412 __ CallStub(&stub);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000413 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000414 __ pop(eax);
415 EmitProfilingCounterReset();
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000416 __ bind(&ok);
417 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000418#ifdef DEBUG
419 // Add a label for checking the size of the code used for returning.
420 Label check_exit_codesize;
421 masm_->bind(&check_exit_codesize);
422#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000423 SetSourcePosition(function()->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000424 __ RecordJSReturn();
425 // Do not use the leave instruction here because it is too short to
426 // patch with the code required by the debugger.
427 __ mov(esp, ebp);
428 __ pop(ebp);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000429
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000430 int arguments_bytes = (info_->scope()->num_parameters() + 1) * kPointerSize;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000431 __ Ret(arguments_bytes, ecx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000432#ifdef ENABLE_DEBUGGER_SUPPORT
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000433 // Check that the size of the code used for returning is large enough
434 // for the debugger's requirements.
435 ASSERT(Assembler::kJSReturnSequenceLength <=
436 masm_->SizeOfCodeGeneratedSince(&check_exit_codesize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000437#endif
438 }
439}
440
441
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000442void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
443 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000444}
445
446
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000447void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
448 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
449 codegen()->GetVar(result_register(), var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000450}
451
452
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000453void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
454 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
455 MemOperand operand = codegen()->VarOperand(var, result_register());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000456 // Memory operands can be pushed directly.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000457 __ push(operand);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000458}
459
460
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000461void FullCodeGenerator::TestContext::Plug(Variable* var) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000462 // For simplicity we always test the accumulator register.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000463 codegen()->GetVar(result_register(), var);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000464 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000465 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000466}
467
468
469void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
470 UNREACHABLE(); // Not used on IA32.
471}
472
473
474void FullCodeGenerator::AccumulatorValueContext::Plug(
475 Heap::RootListIndex index) const {
476 UNREACHABLE(); // Not used on IA32.
477}
478
479
480void FullCodeGenerator::StackValueContext::Plug(
481 Heap::RootListIndex index) const {
482 UNREACHABLE(); // Not used on IA32.
483}
484
485
486void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
487 UNREACHABLE(); // Not used on IA32.
488}
489
490
491void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
492}
493
494
495void FullCodeGenerator::AccumulatorValueContext::Plug(
496 Handle<Object> lit) const {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000497 if (lit->IsSmi()) {
498 __ SafeSet(result_register(), Immediate(lit));
499 } else {
500 __ Set(result_register(), Immediate(lit));
501 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000502}
503
504
505void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000506 if (lit->IsSmi()) {
507 __ SafePush(Immediate(lit));
508 } else {
509 __ push(Immediate(lit));
510 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000511}
512
513
514void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000515 codegen()->PrepareForBailoutBeforeSplit(condition(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000516 true,
517 true_label_,
518 false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000519 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
520 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000521 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000522 } else if (lit->IsTrue() || lit->IsJSObject()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000523 if (true_label_ != fall_through_) __ jmp(true_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000524 } else if (lit->IsString()) {
525 if (String::cast(*lit)->length() == 0) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000526 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000527 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000528 if (true_label_ != fall_through_) __ jmp(true_label_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000529 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000530 } else if (lit->IsSmi()) {
531 if (Smi::cast(*lit)->value() == 0) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000532 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000533 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000534 if (true_label_ != fall_through_) __ jmp(true_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000535 }
536 } else {
537 // For simplicity we always test the accumulator register.
538 __ mov(result_register(), lit);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000539 codegen()->DoTest(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000540 }
541}
542
543
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000544void FullCodeGenerator::EffectContext::DropAndPlug(int count,
545 Register reg) const {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000546 ASSERT(count > 0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000547 __ Drop(count);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000548}
549
550
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000551void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
552 int count,
553 Register reg) const {
554 ASSERT(count > 0);
555 __ Drop(count);
556 __ Move(result_register(), reg);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000557}
558
559
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000560void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
561 Register reg) const {
562 ASSERT(count > 0);
563 if (count > 1) __ Drop(count - 1);
564 __ mov(Operand(esp, 0), reg);
565}
566
567
568void FullCodeGenerator::TestContext::DropAndPlug(int count,
569 Register reg) const {
570 ASSERT(count > 0);
571 // For simplicity we always test the accumulator register.
572 __ Drop(count);
573 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000574 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000575 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000576}
577
578
579void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
580 Label* materialize_false) const {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000581 ASSERT(materialize_true == materialize_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000582 __ bind(materialize_true);
583}
584
585
586void FullCodeGenerator::AccumulatorValueContext::Plug(
587 Label* materialize_true,
588 Label* materialize_false) const {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000589 Label done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000590 __ bind(materialize_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000591 __ mov(result_register(), isolate()->factory()->true_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000592 __ jmp(&done, Label::kNear);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000593 __ bind(materialize_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000594 __ mov(result_register(), isolate()->factory()->false_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000595 __ bind(&done);
596}
597
598
599void FullCodeGenerator::StackValueContext::Plug(
600 Label* materialize_true,
601 Label* materialize_false) const {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000602 Label done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000603 __ bind(materialize_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000604 __ push(Immediate(isolate()->factory()->true_value()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000605 __ jmp(&done, Label::kNear);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000606 __ bind(materialize_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000607 __ push(Immediate(isolate()->factory()->false_value()));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000608 __ bind(&done);
609}
610
611
612void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
613 Label* materialize_false) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000614 ASSERT(materialize_true == true_label_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000615 ASSERT(materialize_false == false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000616}
617
618
619void FullCodeGenerator::EffectContext::Plug(bool flag) const {
620}
621
622
623void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000624 Handle<Object> value = flag
625 ? isolate()->factory()->true_value()
626 : isolate()->factory()->false_value();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000627 __ mov(result_register(), value);
628}
629
630
631void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000632 Handle<Object> value = flag
633 ? isolate()->factory()->true_value()
634 : isolate()->factory()->false_value();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000635 __ push(Immediate(value));
636}
637
638
639void FullCodeGenerator::TestContext::Plug(bool flag) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000640 codegen()->PrepareForBailoutBeforeSplit(condition(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000641 true,
642 true_label_,
643 false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000644 if (flag) {
645 if (true_label_ != fall_through_) __ jmp(true_label_);
646 } else {
647 if (false_label_ != fall_through_) __ jmp(false_label_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000648 }
649}
650
651
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000652void FullCodeGenerator::DoTest(Expression* condition,
653 Label* if_true,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000654 Label* if_false,
655 Label* fall_through) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000656 ToBooleanStub stub(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000657 __ push(result_register());
ricow@chromium.org2c99e282011-07-28 09:15:17 +0000658 __ CallStub(&stub, condition->test_id());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000659 __ test(result_register(), result_register());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000660 // The stub returns nonzero for true.
661 Split(not_zero, if_true, if_false, fall_through);
662}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000663
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000664
ricow@chromium.org65fae842010-08-25 15:26:24 +0000665void FullCodeGenerator::Split(Condition cc,
666 Label* if_true,
667 Label* if_false,
668 Label* fall_through) {
669 if (if_false == fall_through) {
670 __ j(cc, if_true);
671 } else if (if_true == fall_through) {
672 __ j(NegateCondition(cc), if_false);
673 } else {
674 __ j(cc, if_true);
675 __ jmp(if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000676 }
677}
678
679
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000680MemOperand FullCodeGenerator::StackOperand(Variable* var) {
681 ASSERT(var->IsStackAllocated());
682 // Offset is negative because higher indexes are at lower addresses.
683 int offset = -var->index() * kPointerSize;
684 // Adjust by a (parameter or local) base offset.
685 if (var->IsParameter()) {
686 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
687 } else {
688 offset += JavaScriptFrameConstants::kLocal0Offset;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000689 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000690 return Operand(ebp, offset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000691}
692
693
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000694MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
695 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
696 if (var->IsContextSlot()) {
697 int context_chain_length = scope()->ContextChainLength(var->scope());
698 __ LoadContext(scratch, context_chain_length);
699 return ContextOperand(scratch, var->index());
700 } else {
701 return StackOperand(var);
702 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000703}
704
705
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000706void FullCodeGenerator::GetVar(Register dest, Variable* var) {
707 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
708 MemOperand location = VarOperand(var, dest);
709 __ mov(dest, location);
710}
711
712
713void FullCodeGenerator::SetVar(Variable* var,
714 Register src,
715 Register scratch0,
716 Register scratch1) {
717 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
718 ASSERT(!scratch0.is(src));
719 ASSERT(!scratch0.is(scratch1));
720 ASSERT(!scratch1.is(src));
721 MemOperand location = VarOperand(var, scratch0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000722 __ mov(location, src);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000723
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000724 // Emit the write barrier code if the location is in the heap.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000725 if (var->IsContextSlot()) {
726 int offset = Context::SlotOffset(var->index());
727 ASSERT(!scratch0.is(esi) && !src.is(esi) && !scratch1.is(esi));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000728 __ RecordWriteContextSlot(scratch0, offset, src, scratch1, kDontSaveFPRegs);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000729 }
730}
731
732
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000733void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000734 bool should_normalize,
735 Label* if_true,
736 Label* if_false) {
737 // Only prepare for bailouts before splits if we're in a test
738 // context. Otherwise, we let the Visit function deal with the
739 // preparation to avoid preparing with the same AST id twice.
740 if (!context()->IsTest() || !info_->IsOptimizable()) return;
741
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000742 Label skip;
743 if (should_normalize) __ jmp(&skip, Label::kNear);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000744 PrepareForBailout(expr, TOS_REG);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000745 if (should_normalize) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000746 __ cmp(eax, isolate()->factory()->true_value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000747 Split(equal, if_true, if_false, NULL);
748 __ bind(&skip);
749 }
750}
751
752
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000753void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
754 // The variable in the declaration always resides in the current function
755 // context.
756 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
757 if (FLAG_debug_code) {
758 // Check that we're not inside a with or catch context.
759 __ mov(ebx, FieldOperand(esi, HeapObject::kMapOffset));
760 __ cmp(ebx, isolate()->factory()->with_context_map());
761 __ Check(not_equal, "Declaration in with context.");
762 __ cmp(ebx, isolate()->factory()->catch_context_map());
763 __ Check(not_equal, "Declaration in catch context.");
764 }
765}
766
767
768void FullCodeGenerator::VisitVariableDeclaration(
769 VariableDeclaration* declaration) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000770 // If it was not possible to allocate the variable at compile time, we
771 // need to "declare" it at runtime to make sure it actually exists in the
772 // local context.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000773 VariableProxy* proxy = declaration->proxy();
774 VariableMode mode = declaration->mode();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000775 Variable* variable = proxy->var();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000776 bool hole_init = mode == CONST || mode == CONST_HARMONY || mode == LET;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000777 switch (variable->location()) {
778 case Variable::UNALLOCATED:
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000779 globals_->Add(variable->name(), zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000780 globals_->Add(variable->binding_needs_init()
781 ? isolate()->factory()->the_hole_value()
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000782 : isolate()->factory()->undefined_value(), zone());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000783 break;
784
785 case Variable::PARAMETER:
786 case Variable::LOCAL:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000787 if (hole_init) {
788 Comment cmnt(masm_, "[ VariableDeclaration");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000789 __ mov(StackOperand(variable),
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000790 Immediate(isolate()->factory()->the_hole_value()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000791 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000792 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000793
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000794 case Variable::CONTEXT:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000795 if (hole_init) {
796 Comment cmnt(masm_, "[ VariableDeclaration");
797 EmitDebugCheckDeclarationContext(variable);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000798 __ mov(ContextOperand(esi, variable->index()),
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000799 Immediate(isolate()->factory()->the_hole_value()));
800 // No write barrier since the hole value is in old space.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000801 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000802 }
803 break;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000804
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000805 case Variable::LOOKUP: {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000806 Comment cmnt(masm_, "[ VariableDeclaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000807 __ push(esi);
808 __ push(Immediate(variable->name()));
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000809 // VariableDeclaration nodes are always introduced in one of four modes.
810 ASSERT(mode == VAR || mode == LET ||
811 mode == CONST || mode == CONST_HARMONY);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000812 PropertyAttributes attr = (mode == CONST || mode == CONST_HARMONY)
813 ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000814 __ push(Immediate(Smi::FromInt(attr)));
815 // Push initial value, if any.
816 // Note: For variables we must not push an initial value (such as
817 // 'undefined') because we may have a (legal) redeclaration and we
818 // must not destroy the current value.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000819 if (hole_init) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000820 __ push(Immediate(isolate()->factory()->the_hole_value()));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000821 } else {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000822 __ push(Immediate(Smi::FromInt(0))); // Indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000823 }
824 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000825 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000826 }
827 }
828}
829
830
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000831void FullCodeGenerator::VisitFunctionDeclaration(
832 FunctionDeclaration* declaration) {
833 VariableProxy* proxy = declaration->proxy();
834 Variable* variable = proxy->var();
835 switch (variable->location()) {
836 case Variable::UNALLOCATED: {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000837 globals_->Add(variable->name(), zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000838 Handle<SharedFunctionInfo> function =
839 Compiler::BuildFunctionInfo(declaration->fun(), script());
840 // Check for stack-overflow exception.
841 if (function.is_null()) return SetStackOverflow();
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000842 globals_->Add(function, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000843 break;
844 }
845
846 case Variable::PARAMETER:
847 case Variable::LOCAL: {
848 Comment cmnt(masm_, "[ FunctionDeclaration");
849 VisitForAccumulatorValue(declaration->fun());
850 __ mov(StackOperand(variable), result_register());
851 break;
852 }
853
854 case Variable::CONTEXT: {
855 Comment cmnt(masm_, "[ FunctionDeclaration");
856 EmitDebugCheckDeclarationContext(variable);
857 VisitForAccumulatorValue(declaration->fun());
858 __ mov(ContextOperand(esi, variable->index()), result_register());
859 // We know that we have written a function, which is not a smi.
860 __ RecordWriteContextSlot(esi,
861 Context::SlotOffset(variable->index()),
862 result_register(),
863 ecx,
864 kDontSaveFPRegs,
865 EMIT_REMEMBERED_SET,
866 OMIT_SMI_CHECK);
867 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
868 break;
869 }
870
871 case Variable::LOOKUP: {
872 Comment cmnt(masm_, "[ FunctionDeclaration");
873 __ push(esi);
874 __ push(Immediate(variable->name()));
875 __ push(Immediate(Smi::FromInt(NONE)));
876 VisitForStackValue(declaration->fun());
877 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
878 break;
879 }
880 }
881}
882
883
884void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* declaration) {
885 VariableProxy* proxy = declaration->proxy();
886 Variable* variable = proxy->var();
887 Handle<JSModule> instance = declaration->module()->interface()->Instance();
888 ASSERT(!instance.is_null());
889
890 switch (variable->location()) {
891 case Variable::UNALLOCATED: {
892 Comment cmnt(masm_, "[ ModuleDeclaration");
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000893 globals_->Add(variable->name(), zone());
894 globals_->Add(instance, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000895 Visit(declaration->module());
896 break;
897 }
898
899 case Variable::CONTEXT: {
900 Comment cmnt(masm_, "[ ModuleDeclaration");
901 EmitDebugCheckDeclarationContext(variable);
902 __ mov(ContextOperand(esi, variable->index()), Immediate(instance));
903 Visit(declaration->module());
904 break;
905 }
906
907 case Variable::PARAMETER:
908 case Variable::LOCAL:
909 case Variable::LOOKUP:
910 UNREACHABLE();
911 }
912}
913
914
915void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* declaration) {
916 VariableProxy* proxy = declaration->proxy();
917 Variable* variable = proxy->var();
918 switch (variable->location()) {
919 case Variable::UNALLOCATED:
920 // TODO(rossberg)
921 break;
922
923 case Variable::CONTEXT: {
924 Comment cmnt(masm_, "[ ImportDeclaration");
925 EmitDebugCheckDeclarationContext(variable);
926 // TODO(rossberg)
927 break;
928 }
929
930 case Variable::PARAMETER:
931 case Variable::LOCAL:
932 case Variable::LOOKUP:
933 UNREACHABLE();
934 }
935}
936
937
938void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* declaration) {
939 // TODO(rossberg)
940}
941
942
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000943void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
944 // Call the runtime to declare the globals.
945 __ push(esi); // The context is the first argument.
946 __ push(Immediate(pairs));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000947 __ push(Immediate(Smi::FromInt(DeclareGlobalsFlags())));
948 __ CallRuntime(Runtime::kDeclareGlobals, 3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000949 // Return value is ignored.
950}
951
952
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000953void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
954 Comment cmnt(masm_, "[ SwitchStatement");
955 Breakable nested_statement(this, stmt);
956 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000957
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000958 // Keep the switch value on the stack until a case matches.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000959 VisitForStackValue(stmt->tag());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000960 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000961
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000962 ZoneList<CaseClause*>* clauses = stmt->cases();
963 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000964
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000965 Label next_test; // Recycled for each test.
966 // Compile all the tests with branches to their bodies.
967 for (int i = 0; i < clauses->length(); i++) {
968 CaseClause* clause = clauses->at(i);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000969 clause->body_target()->Unuse();
fschneider@chromium.orgd2187832011-01-26 15:44:20 +0000970
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000971 // The default is not a test, but remember it as final fall through.
972 if (clause->is_default()) {
973 default_clause = clause;
974 continue;
975 }
976
977 Comment cmnt(masm_, "[ Case comparison");
978 __ bind(&next_test);
979 next_test.Unuse();
980
981 // Compile the label expression.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000982 VisitForAccumulatorValue(clause->label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000983
ricow@chromium.org65fae842010-08-25 15:26:24 +0000984 // Perform the comparison as if via '==='.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000985 __ mov(edx, Operand(esp, 0)); // Switch value.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000986 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000987 JumpPatchSite patch_site(masm_);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000988 if (inline_smi_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000989 Label slow_case;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000990 __ mov(ecx, edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000991 __ or_(ecx, eax);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000992 patch_site.EmitJumpIfNotSmi(ecx, &slow_case, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000993
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000994 __ cmp(edx, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000995 __ j(not_equal, &next_test);
996 __ Drop(1); // Switch value is no longer needed.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000997 __ jmp(clause->body_target());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000998 __ bind(&slow_case);
999 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001000
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001001 // Record position before stub call for type feedback.
1002 SetSourcePosition(clause->position());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001003 Handle<Code> ic = CompareIC::GetUninitialized(Token::EQ_STRICT);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001004 CallIC(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001005 patch_site.EmitPatchInfo();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001006 __ test(eax, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001007 __ j(not_equal, &next_test);
1008 __ Drop(1); // Switch value is no longer needed.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001009 __ jmp(clause->body_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001010 }
1011
1012 // Discard the test value and jump to the default if present, otherwise to
1013 // the end of the statement.
1014 __ bind(&next_test);
1015 __ Drop(1); // Switch value is no longer needed.
1016 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001017 __ jmp(nested_statement.break_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001018 } else {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001019 __ jmp(default_clause->body_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001020 }
1021
1022 // Compile all the case bodies.
1023 for (int i = 0; i < clauses->length(); i++) {
1024 Comment cmnt(masm_, "[ Case body");
1025 CaseClause* clause = clauses->at(i);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001026 __ bind(clause->body_target());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001027 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001028 VisitStatements(clause->statements());
1029 }
1030
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001031 __ bind(nested_statement.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001032 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001033}
1034
1035
1036void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1037 Comment cmnt(masm_, "[ ForInStatement");
1038 SetStatementPosition(stmt);
1039
1040 Label loop, exit;
1041 ForIn loop_statement(this, stmt);
1042 increment_loop_depth();
1043
1044 // Get the object to enumerate over. Both SpiderMonkey and JSC
1045 // ignore null and undefined in contrast to the specification; see
1046 // ECMA-262 section 12.6.4.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001047 VisitForAccumulatorValue(stmt->enumerable());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001048 __ cmp(eax, isolate()->factory()->undefined_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001049 __ j(equal, &exit);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001050 __ cmp(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001051 __ j(equal, &exit);
1052
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001053 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1054
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001055 // Convert the object to a JS object.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001056 Label convert, done_convert;
whesse@chromium.org7b260152011-06-20 15:33:18 +00001057 __ JumpIfSmi(eax, &convert, Label::kNear);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001058 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001059 __ j(above_equal, &done_convert, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001060 __ bind(&convert);
1061 __ push(eax);
1062 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1063 __ bind(&done_convert);
1064 __ push(eax);
1065
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001066 // Check for proxies.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001067 Label call_runtime, use_cache, fixed_array;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001068 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1069 __ CmpObjectType(eax, LAST_JS_PROXY_TYPE, ecx);
1070 __ j(below_equal, &call_runtime);
1071
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001072 // Check cache validity in generated code. This is a fast case for
1073 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1074 // guarantee cache validity, call the runtime system to check cache
1075 // validity or get the property names in a fixed array.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001076 __ CheckEnumCache(&call_runtime);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001077
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001078 __ mov(eax, FieldOperand(eax, HeapObject::kMapOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001079 __ jmp(&use_cache, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001080
1081 // Get the set of properties to enumerate.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001082 __ bind(&call_runtime);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001083 __ push(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001084 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001085 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
1086 isolate()->factory()->meta_map());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001087 __ j(not_equal, &fixed_array);
1088
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001089
1090 // We got a map in register eax. Get the enumeration cache from it.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001091 __ bind(&use_cache);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001092 __ LoadInstanceDescriptors(eax, ecx);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001093 __ mov(ecx, FieldOperand(ecx, DescriptorArray::kEnumCacheOffset));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001094 __ mov(edx, FieldOperand(ecx, DescriptorArray::kEnumCacheBridgeCacheOffset));
1095
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001096 // Set up the four remaining stack slots.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001097 __ push(eax); // Map.
1098 __ push(edx); // Enumeration cache.
1099 __ mov(eax, FieldOperand(edx, FixedArray::kLengthOffset));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001100 __ push(eax); // Enumeration cache length (as smi).
1101 __ push(Immediate(Smi::FromInt(0))); // Initial index.
1102 __ jmp(&loop);
1103
1104 // We got a fixed array in register eax. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001105 Label non_proxy;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001106 __ bind(&fixed_array);
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001107
1108 Handle<JSGlobalPropertyCell> cell =
1109 isolate()->factory()->NewJSGlobalPropertyCell(
1110 Handle<Object>(
1111 Smi::FromInt(TypeFeedbackCells::kForInFastCaseMarker)));
1112 RecordTypeFeedbackCell(stmt->PrepareId(), cell);
1113 __ LoadHeapObject(ebx, cell);
1114 __ mov(FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset),
1115 Immediate(Smi::FromInt(TypeFeedbackCells::kForInSlowCaseMarker)));
1116
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001117 __ mov(ebx, Immediate(Smi::FromInt(1))); // Smi indicates slow check
1118 __ mov(ecx, Operand(esp, 0 * kPointerSize)); // Get enumerated object
1119 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1120 __ CmpObjectType(ecx, LAST_JS_PROXY_TYPE, ecx);
1121 __ j(above, &non_proxy);
1122 __ mov(ebx, Immediate(Smi::FromInt(0))); // Zero indicates proxy
1123 __ bind(&non_proxy);
1124 __ push(ebx); // Smi
1125 __ push(eax); // Array
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001126 __ mov(eax, FieldOperand(eax, FixedArray::kLengthOffset));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001127 __ push(eax); // Fixed array length (as smi).
1128 __ push(Immediate(Smi::FromInt(0))); // Initial index.
1129
1130 // Generate code for doing the condition check.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001131 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001132 __ bind(&loop);
1133 __ mov(eax, Operand(esp, 0 * kPointerSize)); // Get the current index.
1134 __ cmp(eax, Operand(esp, 1 * kPointerSize)); // Compare to the array length.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001135 __ j(above_equal, loop_statement.break_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001136
1137 // Get the current entry of the array into register ebx.
1138 __ mov(ebx, Operand(esp, 2 * kPointerSize));
1139 __ mov(ebx, FieldOperand(ebx, eax, times_2, FixedArray::kHeaderSize));
1140
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001141 // Get the expected map from the stack or a smi in the
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001142 // permanent slow case into register edx.
1143 __ mov(edx, Operand(esp, 3 * kPointerSize));
1144
1145 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001146 // If not, we may have to filter the key.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001147 Label update_each;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001148 __ mov(ecx, Operand(esp, 4 * kPointerSize));
1149 __ cmp(edx, FieldOperand(ecx, HeapObject::kMapOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001150 __ j(equal, &update_each, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001151
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001152 // For proxies, no filtering is done.
1153 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1154 ASSERT(Smi::FromInt(0) == 0);
1155 __ test(edx, edx);
1156 __ j(zero, &update_each);
1157
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001158 // Convert the entry to a string or null if it isn't a property
1159 // anymore. If the property has been removed while iterating, we
1160 // just skip it.
1161 __ push(ecx); // Enumerable.
1162 __ push(ebx); // Current entry.
1163 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001164 __ test(eax, eax);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001165 __ j(equal, loop_statement.continue_label());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001166 __ mov(ebx, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001167
1168 // Update the 'each' property or variable from the possibly filtered
1169 // entry in register ebx.
1170 __ bind(&update_each);
1171 __ mov(result_register(), ebx);
1172 // Perform the assignment as if via '='.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001173 { EffectContext context(this);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001174 EmitAssignment(stmt->each());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001175 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001176
1177 // Generate code for the body of the loop.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001178 Visit(stmt->body());
1179
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001180 // Generate code for going to the next element by incrementing the
1181 // index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001182 __ bind(loop_statement.continue_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001183 __ add(Operand(esp, 0 * kPointerSize), Immediate(Smi::FromInt(1)));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001184
yangguo@chromium.org56454712012-02-16 15:33:53 +00001185 EmitStackCheck(stmt, &loop);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001186 __ jmp(&loop);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001187
1188 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001189 __ bind(loop_statement.break_label());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001190 __ add(esp, Immediate(5 * kPointerSize));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001191
1192 // Exit and decrement the loop depth.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001193 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001194 __ bind(&exit);
1195 decrement_loop_depth();
1196}
1197
1198
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001199void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1200 bool pretenure) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001201 // Use the fast case closure allocation code that allocates in new
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001202 // space for nested functions that don't need literals cloning. If
1203 // we're running with the --always-opt or the --prepare-always-opt
1204 // flag, we need to use the runtime function so that the new function
1205 // we are creating here gets a chance to have its code optimized and
1206 // doesn't just get a copy of the existing unoptimized code.
1207 if (!FLAG_always_opt &&
1208 !FLAG_prepare_always_opt &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001209 !pretenure &&
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001210 scope()->is_function_scope() &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001211 info->num_literals() == 0) {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001212 FastNewClosureStub stub(info->language_mode());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001213 __ push(Immediate(info));
1214 __ CallStub(&stub);
1215 } else {
1216 __ push(esi);
1217 __ push(Immediate(info));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001218 __ push(Immediate(pretenure
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001219 ? isolate()->factory()->true_value()
1220 : isolate()->factory()->false_value()));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001221 __ CallRuntime(Runtime::kNewClosure, 3);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001222 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001223 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001224}
1225
1226
1227void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
1228 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001229 EmitVariableLoad(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001230}
1231
1232
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001233void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1234 TypeofState typeof_state,
1235 Label* slow) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001236 Register context = esi;
1237 Register temp = edx;
1238
1239 Scope* s = scope();
1240 while (s != NULL) {
1241 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001242 if (s->calls_non_strict_eval()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001243 // Check that extension is NULL.
1244 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX),
1245 Immediate(0));
1246 __ j(not_equal, slow);
1247 }
1248 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001249 __ mov(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001250 // Walk the rest of the chain without clobbering esi.
1251 context = temp;
1252 }
1253 // If no outer scope calls eval, we do not need to check more
1254 // context extensions. If we have reached an eval scope, we check
1255 // all extensions from this point.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001256 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001257 s = s->outer_scope();
1258 }
1259
1260 if (s != NULL && s->is_eval_scope()) {
1261 // Loop up the context chain. There is no frame effect so it is
1262 // safe to use raw labels here.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001263 Label next, fast;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001264 if (!context.is(temp)) {
1265 __ mov(temp, context);
1266 }
1267 __ bind(&next);
1268 // Terminate at global context.
1269 __ cmp(FieldOperand(temp, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001270 Immediate(isolate()->factory()->global_context_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001271 __ j(equal, &fast, Label::kNear);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001272 // Check that extension is NULL.
1273 __ cmp(ContextOperand(temp, Context::EXTENSION_INDEX), Immediate(0));
1274 __ j(not_equal, slow);
1275 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001276 __ mov(temp, ContextOperand(temp, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001277 __ jmp(&next);
1278 __ bind(&fast);
1279 }
1280
1281 // All extension objects were empty and it is safe to use a global
1282 // load IC call.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001283 __ mov(edx, GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001284 __ mov(ecx, var->name());
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001285 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001286 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1287 ? RelocInfo::CODE_TARGET
1288 : RelocInfo::CODE_TARGET_CONTEXT;
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001289 CallIC(ic, mode);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001290}
1291
1292
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001293MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1294 Label* slow) {
1295 ASSERT(var->IsContextSlot());
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001296 Register context = esi;
1297 Register temp = ebx;
1298
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001299 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001300 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001301 if (s->calls_non_strict_eval()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001302 // Check that extension is NULL.
1303 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX),
1304 Immediate(0));
1305 __ j(not_equal, slow);
1306 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001307 __ mov(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001308 // Walk the rest of the chain without clobbering esi.
1309 context = temp;
1310 }
1311 }
1312 // Check that last extension is NULL.
1313 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX), Immediate(0));
1314 __ j(not_equal, slow);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001315
1316 // This function is used only for loads, not stores, so it's safe to
1317 // return an esi-based operand (the write barrier cannot be allowed to
1318 // destroy the esi register).
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001319 return ContextOperand(context, var->index());
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001320}
1321
1322
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001323void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1324 TypeofState typeof_state,
1325 Label* slow,
1326 Label* done) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001327 // Generate fast-case code for variables that might be shadowed by
1328 // eval-introduced variables. Eval is used a lot without
1329 // introducing variables. In those cases, we do not want to
1330 // perform a runtime call for all variables in the scope
1331 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001332 if (var->mode() == DYNAMIC_GLOBAL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001333 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001334 __ jmp(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001335 } else if (var->mode() == DYNAMIC_LOCAL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001336 Variable* local = var->local_if_not_shadowed();
1337 __ mov(eax, ContextSlotOperandCheckExtensions(local, slow));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001338 if (local->mode() == CONST ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001339 local->mode() == CONST_HARMONY ||
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001340 local->mode() == LET) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001341 __ cmp(eax, isolate()->factory()->the_hole_value());
1342 __ j(not_equal, done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001343 if (local->mode() == CONST) {
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001344 __ mov(eax, isolate()->factory()->undefined_value());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001345 } else { // LET || CONST_HARMONY
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001346 __ push(Immediate(var->name()));
1347 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1348 }
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001349 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001350 __ jmp(done);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001351 }
1352}
1353
1354
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001355void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1356 // Record position before possible IC call.
1357 SetSourcePosition(proxy->position());
1358 Variable* var = proxy->var();
1359
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001360 // Three cases: global variables, lookup variables, and all other types of
1361 // variables.
1362 switch (var->location()) {
1363 case Variable::UNALLOCATED: {
1364 Comment cmnt(masm_, "Global variable");
1365 // Use inline caching. Variable name is passed in ecx and the global
1366 // object in eax.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001367 __ mov(edx, GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001368 __ mov(ecx, var->name());
1369 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001370 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001371 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001372 break;
1373 }
1374
1375 case Variable::PARAMETER:
1376 case Variable::LOCAL:
1377 case Variable::CONTEXT: {
1378 Comment cmnt(masm_, var->IsContextSlot()
1379 ? "Context variable"
1380 : "Stack variable");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001381 if (var->binding_needs_init()) {
1382 // var->scope() may be NULL when the proxy is located in eval code and
1383 // refers to a potential outside binding. Currently those bindings are
1384 // always looked up dynamically, i.e. in that case
1385 // var->location() == LOOKUP.
1386 // always holds.
1387 ASSERT(var->scope() != NULL);
1388
1389 // Check if the binding really needs an initialization check. The check
1390 // can be skipped in the following situation: we have a LET or CONST
1391 // binding in harmony mode, both the Variable and the VariableProxy have
1392 // the same declaration scope (i.e. they are both in global code, in the
1393 // same function or in the same eval code) and the VariableProxy is in
1394 // the source physically located after the initializer of the variable.
1395 //
1396 // We cannot skip any initialization checks for CONST in non-harmony
1397 // mode because const variables may be declared but never initialized:
1398 // if (false) { const x; }; var y = x;
1399 //
1400 // The condition on the declaration scopes is a conservative check for
1401 // nested functions that access a binding and are called before the
1402 // binding is initialized:
1403 // function() { f(); let x = 1; function f() { x = 2; } }
1404 //
1405 bool skip_init_check;
1406 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1407 skip_init_check = false;
jkummerow@chromium.orgac45fed2011-11-07 13:11:02 +00001408 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001409 // Check that we always have valid source position.
1410 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1411 ASSERT(proxy->position() != RelocInfo::kNoPosition);
1412 skip_init_check = var->mode() != CONST &&
1413 var->initializer_position() < proxy->position();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001414 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001415
1416 if (!skip_init_check) {
1417 // Let and const need a read barrier.
1418 Label done;
1419 GetVar(eax, var);
1420 __ cmp(eax, isolate()->factory()->the_hole_value());
1421 __ j(not_equal, &done, Label::kNear);
1422 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1423 // Throw a reference error when using an uninitialized let/const
1424 // binding in harmony mode.
1425 __ push(Immediate(var->name()));
1426 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1427 } else {
1428 // Uninitalized const bindings outside of harmony mode are unholed.
1429 ASSERT(var->mode() == CONST);
1430 __ mov(eax, isolate()->factory()->undefined_value());
1431 }
1432 __ bind(&done);
1433 context()->Plug(eax);
1434 break;
1435 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001436 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001437 context()->Plug(var);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001438 break;
1439 }
1440
1441 case Variable::LOOKUP: {
1442 Label done, slow;
1443 // Generate code for loading from variables potentially shadowed
1444 // by eval-introduced variables.
1445 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1446 __ bind(&slow);
1447 Comment cmnt(masm_, "Lookup variable");
1448 __ push(esi); // Context.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001449 __ push(Immediate(var->name()));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001450 __ CallRuntime(Runtime::kLoadContextSlot, 2);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001451 __ bind(&done);
1452 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001453 break;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001454 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001455 }
1456}
1457
1458
1459void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1460 Comment cmnt(masm_, "[ RegExpLiteral");
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001461 Label materialized;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001462 // Registers will be used as follows:
1463 // edi = JS function.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001464 // ecx = literals array.
1465 // ebx = regexp literal.
1466 // eax = regexp literal clone.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001467 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001468 __ mov(ecx, FieldOperand(edi, JSFunction::kLiteralsOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001469 int literal_offset =
ricow@chromium.org65fae842010-08-25 15:26:24 +00001470 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001471 __ mov(ebx, FieldOperand(ecx, literal_offset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001472 __ cmp(ebx, isolate()->factory()->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001473 __ j(not_equal, &materialized, Label::kNear);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001474
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001475 // Create regexp literal using runtime function
1476 // Result will be in eax.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001477 __ push(ecx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001478 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1479 __ push(Immediate(expr->pattern()));
1480 __ push(Immediate(expr->flags()));
1481 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001482 __ mov(ebx, eax);
1483
1484 __ bind(&materialized);
1485 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1486 Label allocated, runtime_allocate;
1487 __ AllocateInNewSpace(size, eax, ecx, edx, &runtime_allocate, TAG_OBJECT);
1488 __ jmp(&allocated);
1489
1490 __ bind(&runtime_allocate);
1491 __ push(ebx);
1492 __ push(Immediate(Smi::FromInt(size)));
1493 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1494 __ pop(ebx);
1495
1496 __ bind(&allocated);
1497 // Copy the content into the newly allocated memory.
1498 // (Unroll copy loop once for better throughput).
1499 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
1500 __ mov(edx, FieldOperand(ebx, i));
1501 __ mov(ecx, FieldOperand(ebx, i + kPointerSize));
1502 __ mov(FieldOperand(eax, i), edx);
1503 __ mov(FieldOperand(eax, i + kPointerSize), ecx);
1504 }
1505 if ((size % (2 * kPointerSize)) != 0) {
1506 __ mov(edx, FieldOperand(ebx, size - kPointerSize));
1507 __ mov(FieldOperand(eax, size - kPointerSize), edx);
1508 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001509 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001510}
1511
1512
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001513void FullCodeGenerator::EmitAccessor(Expression* expression) {
1514 if (expression == NULL) {
1515 __ push(Immediate(isolate()->factory()->null_value()));
1516 } else {
1517 VisitForStackValue(expression);
1518 }
1519}
1520
1521
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001522void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1523 Comment cmnt(masm_, "[ ObjectLiteral");
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001524 Handle<FixedArray> constant_properties = expr->constant_properties();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001525 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1526 __ push(FieldOperand(edi, JSFunction::kLiteralsOffset));
1527 __ push(Immediate(Smi::FromInt(expr->literal_index())));
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001528 __ push(Immediate(constant_properties));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001529 int flags = expr->fast_elements()
1530 ? ObjectLiteral::kFastElements
1531 : ObjectLiteral::kNoFlags;
1532 flags |= expr->has_function()
1533 ? ObjectLiteral::kHasFunction
1534 : ObjectLiteral::kNoFlags;
1535 __ push(Immediate(Smi::FromInt(flags)));
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001536 int properties_count = constant_properties->length() / 2;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001537 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001538 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001539 } else if (flags != ObjectLiteral::kFastElements ||
1540 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001541 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001542 } else {
1543 FastCloneShallowObjectStub stub(properties_count);
1544 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001545 }
1546
1547 // If result_saved is true the result is on top of the stack. If
1548 // result_saved is false the result is in eax.
1549 bool result_saved = false;
1550
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001551 // Mark all computed expressions that are bound to a key that
1552 // is shadowed by a later occurrence of the same key. For the
1553 // marked expressions, no store code is emitted.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001554 expr->CalculateEmitStore(zone());
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001555
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001556 AccessorTable accessor_table(zone());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001557 for (int i = 0; i < expr->properties()->length(); i++) {
1558 ObjectLiteral::Property* property = expr->properties()->at(i);
1559 if (property->IsCompileTimeValue()) continue;
1560
1561 Literal* key = property->key();
1562 Expression* value = property->value();
1563 if (!result_saved) {
1564 __ push(eax); // Save result on the stack
1565 result_saved = true;
1566 }
1567 switch (property->kind()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001568 case ObjectLiteral::Property::CONSTANT:
1569 UNREACHABLE();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001570 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1571 ASSERT(!CompileTimeValue::IsCompileTimeValue(value));
1572 // Fall through.
1573 case ObjectLiteral::Property::COMPUTED:
1574 if (key->handle()->IsSymbol()) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001575 if (property->emit_store()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001576 VisitForAccumulatorValue(value);
1577 __ mov(ecx, Immediate(key->handle()));
1578 __ mov(edx, Operand(esp, 0));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001579 Handle<Code> ic = is_classic_mode()
1580 ? isolate()->builtins()->StoreIC_Initialize()
1581 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001582 CallIC(ic, RelocInfo::CODE_TARGET, key->id());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001583 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1584 } else {
1585 VisitForEffect(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001586 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001587 break;
1588 }
1589 // Fall through.
1590 case ObjectLiteral::Property::PROTOTYPE:
1591 __ push(Operand(esp, 0)); // Duplicate receiver.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001592 VisitForStackValue(key);
1593 VisitForStackValue(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001594 if (property->emit_store()) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001595 __ push(Immediate(Smi::FromInt(NONE))); // PropertyAttributes
1596 __ CallRuntime(Runtime::kSetProperty, 4);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001597 } else {
1598 __ Drop(3);
1599 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001600 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001601 case ObjectLiteral::Property::GETTER:
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001602 accessor_table.lookup(key)->second->getter = value;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001603 break;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001604 case ObjectLiteral::Property::SETTER:
1605 accessor_table.lookup(key)->second->setter = value;
1606 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001607 }
1608 }
1609
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001610 // Emit code to define accessors, using only a single call to the runtime for
1611 // each pair of corresponding getters and setters.
1612 for (AccessorTable::Iterator it = accessor_table.begin();
1613 it != accessor_table.end();
1614 ++it) {
1615 __ push(Operand(esp, 0)); // Duplicate receiver.
1616 VisitForStackValue(it->first);
1617 EmitAccessor(it->second->getter);
1618 EmitAccessor(it->second->setter);
1619 __ push(Immediate(Smi::FromInt(NONE)));
1620 __ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
1621 }
1622
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001623 if (expr->has_function()) {
1624 ASSERT(result_saved);
1625 __ push(Operand(esp, 0));
1626 __ CallRuntime(Runtime::kToFastProperties, 1);
1627 }
1628
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001629 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001630 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001631 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001632 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001633 }
1634}
1635
1636
1637void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1638 Comment cmnt(masm_, "[ ArrayLiteral");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001639
1640 ZoneList<Expression*>* subexprs = expr->values();
1641 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001642 Handle<FixedArray> constant_elements = expr->constant_elements();
1643 ASSERT_EQ(2, constant_elements->length());
1644 ElementsKind constant_elements_kind =
1645 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001646 bool has_constant_fast_elements =
1647 IsFastObjectElementsKind(constant_elements_kind);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001648 Handle<FixedArrayBase> constant_elements_values(
1649 FixedArrayBase::cast(constant_elements->get(1)));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001650
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001651 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1652 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
1653 __ push(Immediate(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001654 __ push(Immediate(constant_elements));
erikcorry0ad885c2011-11-21 13:51:57 +00001655 Heap* heap = isolate()->heap();
1656 if (has_constant_fast_elements &&
1657 constant_elements_values->map() == heap->fixed_cow_array_map()) {
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001658 // If the elements are already FAST_*_ELEMENTS, the boilerplate cannot
erikcorry0ad885c2011-11-21 13:51:57 +00001659 // change, so it's possible to specialize the stub in advance.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001660 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(), 1);
erikcorry0ad885c2011-11-21 13:51:57 +00001661 FastCloneShallowArrayStub stub(
1662 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS,
1663 length);
1664 __ CallStub(&stub);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001665 } else if (expr->depth() > 1) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001666 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001667 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001668 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001669 } else {
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001670 ASSERT(IsFastSmiOrObjectElementsKind(constant_elements_kind) ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001671 FLAG_smi_only_arrays);
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001672 // If the elements are already FAST_*_ELEMENTS, the boilerplate cannot
erikcorry0ad885c2011-11-21 13:51:57 +00001673 // change, so it's possible to specialize the stub in advance.
1674 FastCloneShallowArrayStub::Mode mode = has_constant_fast_elements
1675 ? FastCloneShallowArrayStub::CLONE_ELEMENTS
1676 : FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001677 FastCloneShallowArrayStub stub(mode, length);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001678 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001679 }
1680
1681 bool result_saved = false; // Is the result saved to the stack?
1682
1683 // Emit code to evaluate all the non-constant subexpressions and to store
1684 // them into the newly cloned array.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001685 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001686 Expression* subexpr = subexprs->at(i);
1687 // If the subexpression is a literal or a simple materialized literal it
1688 // is already set in the cloned array.
1689 if (subexpr->AsLiteral() != NULL ||
1690 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1691 continue;
1692 }
1693
1694 if (!result_saved) {
1695 __ push(eax);
1696 result_saved = true;
1697 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001698 VisitForAccumulatorValue(subexpr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001699
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001700 if (IsFastObjectElementsKind(constant_elements_kind)) {
1701 // Fast-case array literal with ElementsKind of FAST_*_ELEMENTS, they
1702 // cannot transition and don't need to call the runtime stub.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001703 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1704 __ mov(ebx, Operand(esp, 0)); // Copy of array literal.
1705 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
1706 // Store the subexpression value in the array's elements.
1707 __ mov(FieldOperand(ebx, offset), result_register());
1708 // Update the write barrier for the array store.
1709 __ RecordWriteField(ebx, offset, result_register(), ecx,
1710 kDontSaveFPRegs,
1711 EMIT_REMEMBERED_SET,
1712 INLINE_SMI_CHECK);
1713 } else {
1714 // Store the subexpression value in the array's elements.
1715 __ mov(ebx, Operand(esp, 0)); // Copy of array literal.
1716 __ mov(edi, FieldOperand(ebx, JSObject::kMapOffset));
1717 __ mov(ecx, Immediate(Smi::FromInt(i)));
1718 __ mov(edx, Immediate(Smi::FromInt(expr->literal_index())));
1719 StoreArrayLiteralElementStub stub;
1720 __ CallStub(&stub);
1721 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001722
1723 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001724 }
1725
1726 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001727 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001728 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001729 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001730 }
1731}
1732
1733
ager@chromium.org5c838252010-02-19 08:53:10 +00001734void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1735 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001736 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1737 // on the left-hand side.
1738 if (!expr->target()->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001739 VisitForEffect(expr->target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001740 return;
1741 }
1742
ager@chromium.org5c838252010-02-19 08:53:10 +00001743 // Left-hand side can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00001744 // slot.
ager@chromium.org5c838252010-02-19 08:53:10 +00001745 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1746 LhsKind assign_type = VARIABLE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001747 Property* property = expr->target()->AsProperty();
1748 if (property != NULL) {
1749 assign_type = (property->key()->IsPropertyName())
1750 ? NAMED_PROPERTY
1751 : KEYED_PROPERTY;
ager@chromium.org5c838252010-02-19 08:53:10 +00001752 }
1753
1754 // Evaluate LHS expression.
1755 switch (assign_type) {
1756 case VARIABLE:
1757 // Nothing to do here.
1758 break;
1759 case NAMED_PROPERTY:
1760 if (expr->is_compound()) {
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001761 // We need the receiver both on the stack and in edx.
1762 VisitForStackValue(property->obj());
1763 __ mov(edx, Operand(esp, 0));
ager@chromium.org5c838252010-02-19 08:53:10 +00001764 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001765 VisitForStackValue(property->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00001766 }
1767 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001768 case KEYED_PROPERTY: {
ager@chromium.org5c838252010-02-19 08:53:10 +00001769 if (expr->is_compound()) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00001770 VisitForStackValue(property->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001771 VisitForStackValue(property->key());
1772 __ mov(edx, Operand(esp, kPointerSize)); // Object.
1773 __ mov(ecx, Operand(esp, 0)); // Key.
ager@chromium.org5c838252010-02-19 08:53:10 +00001774 } else {
whesse@chromium.org7b260152011-06-20 15:33:18 +00001775 VisitForStackValue(property->obj());
1776 VisitForStackValue(property->key());
ager@chromium.org5c838252010-02-19 08:53:10 +00001777 }
1778 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001779 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001780 }
1781
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001782 // For compound assignments we need another deoptimization point after the
1783 // variable/property load.
ager@chromium.org5c838252010-02-19 08:53:10 +00001784 if (expr->is_compound()) {
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001785 AccumulatorValueContext result_context(this);
1786 { AccumulatorValueContext left_operand_context(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001787 switch (assign_type) {
1788 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001789 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001790 PrepareForBailout(expr->target(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001791 break;
1792 case NAMED_PROPERTY:
1793 EmitNamedPropertyLoad(property);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001794 PrepareForBailoutForId(expr->CompoundLoadId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001795 break;
1796 case KEYED_PROPERTY:
1797 EmitKeyedPropertyLoad(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 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001801 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001802
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001803 Token::Value op = expr->binary_op();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001804 __ push(eax); // Left operand goes on the stack.
1805 VisitForAccumulatorValue(expr->value());
ager@chromium.org5c838252010-02-19 08:53:10 +00001806
ricow@chromium.org65fae842010-08-25 15:26:24 +00001807 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1808 ? OVERWRITE_RIGHT
1809 : NO_OVERWRITE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001810 SetSourcePosition(expr->position() + 1);
1811 if (ShouldInlineSmiCase(op)) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001812 EmitInlineSmiBinaryOp(expr->binary_operation(),
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001813 op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001814 mode,
1815 expr->target(),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001816 expr->value());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001817 } else {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001818 EmitBinaryOp(expr->binary_operation(), op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001819 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001820
1821 // Deoptimization point in case the binary operation may have side effects.
1822 PrepareForBailout(expr->binary_operation(), TOS_REG);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001823 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001824 VisitForAccumulatorValue(expr->value());
ager@chromium.org5c838252010-02-19 08:53:10 +00001825 }
1826
1827 // Record source position before possible IC call.
1828 SetSourcePosition(expr->position());
1829
1830 // Store the value.
1831 switch (assign_type) {
1832 case VARIABLE:
1833 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001834 expr->op());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001835 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1836 context()->Plug(eax);
ager@chromium.org5c838252010-02-19 08:53:10 +00001837 break;
1838 case NAMED_PROPERTY:
1839 EmitNamedPropertyAssignment(expr);
1840 break;
1841 case KEYED_PROPERTY:
1842 EmitKeyedPropertyAssignment(expr);
1843 break;
1844 }
1845}
1846
1847
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001848void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1849 SetSourcePosition(prop->position());
1850 Literal* key = prop->key()->AsLiteral();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001851 ASSERT(!key->handle()->IsSmi());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001852 __ mov(ecx, Immediate(key->handle()));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001853 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001854 CallIC(ic, RelocInfo::CODE_TARGET, prop->id());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001855}
1856
1857
1858void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1859 SetSourcePosition(prop->position());
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001860 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001861 CallIC(ic, RelocInfo::CODE_TARGET, prop->id());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001862}
1863
1864
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001865void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001866 Token::Value op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001867 OverwriteMode mode,
1868 Expression* left,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001869 Expression* right) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001870 // Do combined smi check of the operands. Left operand is on the
1871 // stack. Right operand is in eax.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001872 Label smi_case, done, stub_call;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001873 __ pop(edx);
1874 __ mov(ecx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001875 __ or_(eax, edx);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001876 JumpPatchSite patch_site(masm_);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001877 patch_site.EmitJumpIfSmi(eax, &smi_case, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001878
1879 __ bind(&stub_call);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001880 __ mov(eax, ecx);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001881 BinaryOpStub stub(op, mode);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001882 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001883 patch_site.EmitPatchInfo();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001884 __ jmp(&done, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001885
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001886 // Smi case.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001887 __ bind(&smi_case);
1888 __ mov(eax, edx); // Copy left operand in case of a stub call.
1889
1890 switch (op) {
1891 case Token::SAR:
1892 __ SmiUntag(eax);
1893 __ SmiUntag(ecx);
1894 __ sar_cl(eax); // No checks of result necessary
1895 __ SmiTag(eax);
1896 break;
1897 case Token::SHL: {
1898 Label result_ok;
1899 __ SmiUntag(eax);
1900 __ SmiUntag(ecx);
1901 __ shl_cl(eax);
1902 // Check that the *signed* result fits in a smi.
1903 __ cmp(eax, 0xc0000000);
1904 __ j(positive, &result_ok);
1905 __ SmiTag(ecx);
1906 __ jmp(&stub_call);
1907 __ bind(&result_ok);
1908 __ SmiTag(eax);
1909 break;
1910 }
1911 case Token::SHR: {
1912 Label result_ok;
1913 __ SmiUntag(eax);
1914 __ SmiUntag(ecx);
1915 __ shr_cl(eax);
1916 __ test(eax, Immediate(0xc0000000));
1917 __ j(zero, &result_ok);
1918 __ SmiTag(ecx);
1919 __ jmp(&stub_call);
1920 __ bind(&result_ok);
1921 __ SmiTag(eax);
1922 break;
1923 }
1924 case Token::ADD:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001925 __ add(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001926 __ j(overflow, &stub_call);
1927 break;
1928 case Token::SUB:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001929 __ sub(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001930 __ j(overflow, &stub_call);
1931 break;
1932 case Token::MUL: {
1933 __ SmiUntag(eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001934 __ imul(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001935 __ j(overflow, &stub_call);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001936 __ test(eax, eax);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001937 __ j(not_zero, &done, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001938 __ mov(ebx, edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001939 __ or_(ebx, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001940 __ j(negative, &stub_call);
1941 break;
1942 }
1943 case Token::BIT_OR:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001944 __ or_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001945 break;
1946 case Token::BIT_AND:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001947 __ and_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001948 break;
1949 case Token::BIT_XOR:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001950 __ xor_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001951 break;
1952 default:
1953 UNREACHABLE();
1954 }
1955
1956 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001957 context()->Plug(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001958}
1959
1960
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001961void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1962 Token::Value op,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001963 OverwriteMode mode) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001964 __ pop(edx);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001965 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001966 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001967 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001968 patch_site.EmitPatchInfo();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001969 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001970}
1971
1972
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001973void FullCodeGenerator::EmitAssignment(Expression* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001974 // Invalid left-hand sides are rewritten to have a 'throw
1975 // ReferenceError' on the left-hand side.
1976 if (!expr->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001977 VisitForEffect(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001978 return;
1979 }
1980
1981 // Left-hand side can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00001982 // slot.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001983 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1984 LhsKind assign_type = VARIABLE;
1985 Property* prop = expr->AsProperty();
1986 if (prop != NULL) {
1987 assign_type = (prop->key()->IsPropertyName())
1988 ? NAMED_PROPERTY
1989 : KEYED_PROPERTY;
1990 }
1991
1992 switch (assign_type) {
1993 case VARIABLE: {
1994 Variable* var = expr->AsVariableProxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001995 EffectContext context(this);
1996 EmitVariableAssignment(var, Token::ASSIGN);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001997 break;
1998 }
1999 case NAMED_PROPERTY: {
2000 __ push(eax); // Preserve value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002001 VisitForAccumulatorValue(prop->obj());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002002 __ mov(edx, eax);
2003 __ pop(eax); // Restore value.
2004 __ mov(ecx, prop->key()->AsLiteral()->handle());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002005 Handle<Code> ic = is_classic_mode()
2006 ? isolate()->builtins()->StoreIC_Initialize()
2007 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002008 CallIC(ic);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002009 break;
2010 }
2011 case KEYED_PROPERTY: {
2012 __ push(eax); // Preserve value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002013 VisitForStackValue(prop->obj());
2014 VisitForAccumulatorValue(prop->key());
2015 __ mov(ecx, eax);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002016 __ pop(edx); // Receiver.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002017 __ pop(eax); // Restore value.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002018 Handle<Code> ic = is_classic_mode()
2019 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2020 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002021 CallIC(ic);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002022 break;
2023 }
2024 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002025 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002026}
2027
2028
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002029void FullCodeGenerator::EmitVariableAssignment(Variable* var,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002030 Token::Value op) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002031 if (var->IsUnallocated()) {
2032 // Global var, const, or let.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002033 __ mov(ecx, var->name());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002034 __ mov(edx, GlobalObjectOperand());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002035 Handle<Code> ic = is_classic_mode()
2036 ? isolate()->builtins()->StoreIC_Initialize()
2037 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002038 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002039
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002040 } else if (op == Token::INIT_CONST) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002041 // Const initializers need a write barrier.
2042 ASSERT(!var->IsParameter()); // No const parameters.
2043 if (var->IsStackLocal()) {
2044 Label skip;
2045 __ mov(edx, StackOperand(var));
2046 __ cmp(edx, isolate()->factory()->the_hole_value());
2047 __ j(not_equal, &skip);
2048 __ mov(StackOperand(var), eax);
2049 __ bind(&skip);
2050 } else {
2051 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
2052 // Like var declarations, const declarations are hoisted to function
2053 // scope. However, unlike var initializers, const initializers are
2054 // able to drill a hole to that function context, even from inside a
2055 // 'with' context. We thus bypass the normal static scope lookup for
2056 // var->IsContextSlot().
2057 __ push(eax);
2058 __ push(esi);
2059 __ push(Immediate(var->name()));
2060 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002061 }
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002062
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002063 } else if (var->mode() == LET && op != Token::INIT_LET) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002064 // Non-initializing assignment to let variable needs a write barrier.
2065 if (var->IsLookupSlot()) {
2066 __ push(eax); // Value.
2067 __ push(esi); // Context.
2068 __ push(Immediate(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002069 __ push(Immediate(Smi::FromInt(language_mode())));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002070 __ CallRuntime(Runtime::kStoreContextSlot, 4);
2071 } else {
2072 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
2073 Label assign;
2074 MemOperand location = VarOperand(var, ecx);
2075 __ mov(edx, location);
2076 __ cmp(edx, isolate()->factory()->the_hole_value());
2077 __ j(not_equal, &assign, Label::kNear);
2078 __ push(Immediate(var->name()));
2079 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2080 __ bind(&assign);
2081 __ mov(location, eax);
2082 if (var->IsContextSlot()) {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00002083 __ mov(edx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002084 int offset = Context::SlotOffset(var->index());
2085 __ RecordWriteContextSlot(ecx, offset, edx, ebx, kDontSaveFPRegs);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00002086 }
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00002087 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002088
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002089 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2090 // Assignment to var or initializing assignment to let/const
2091 // in harmony mode.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002092 if (var->IsStackAllocated() || var->IsContextSlot()) {
2093 MemOperand location = VarOperand(var, ecx);
2094 if (FLAG_debug_code && op == Token::INIT_LET) {
2095 // Check for an uninitialized let binding.
2096 __ mov(edx, location);
2097 __ cmp(edx, isolate()->factory()->the_hole_value());
2098 __ Check(equal, "Let binding re-initialization.");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002099 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002100 // Perform the assignment.
2101 __ mov(location, eax);
2102 if (var->IsContextSlot()) {
2103 __ mov(edx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002104 int offset = Context::SlotOffset(var->index());
2105 __ RecordWriteContextSlot(ecx, offset, edx, ebx, kDontSaveFPRegs);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002106 }
2107 } else {
2108 ASSERT(var->IsLookupSlot());
2109 __ push(eax); // Value.
2110 __ push(esi); // Context.
2111 __ push(Immediate(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002112 __ push(Immediate(Smi::FromInt(language_mode())));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002113 __ CallRuntime(Runtime::kStoreContextSlot, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002114 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002115 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002116 // Non-initializing assignments to consts are ignored.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002117}
2118
2119
2120void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2121 // Assignment to a property, using a named store IC.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002122 // eax : value
2123 // esp[0] : receiver
2124
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002125 Property* prop = expr->target()->AsProperty();
2126 ASSERT(prop != NULL);
2127 ASSERT(prop->key()->AsLiteral() != NULL);
2128
2129 // If the assignment starts a block of assignments to the same object,
2130 // change to slow case to avoid the quadratic behavior of repeatedly
2131 // adding fast properties.
2132 if (expr->starts_initialization_block()) {
2133 __ push(result_register());
2134 __ push(Operand(esp, kPointerSize)); // Receiver is now under value.
2135 __ CallRuntime(Runtime::kToSlowProperties, 1);
2136 __ pop(result_register());
2137 }
2138
2139 // Record source code position before IC call.
2140 SetSourcePosition(expr->position());
2141 __ mov(ecx, prop->key()->AsLiteral()->handle());
2142 if (expr->ends_initialization_block()) {
2143 __ mov(edx, Operand(esp, 0));
2144 } else {
2145 __ pop(edx);
2146 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002147 Handle<Code> ic = is_classic_mode()
2148 ? isolate()->builtins()->StoreIC_Initialize()
2149 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002150 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002151
2152 // If the assignment ends an initialization block, revert to fast case.
2153 if (expr->ends_initialization_block()) {
2154 __ push(eax); // Result of assignment, saved even if not needed.
2155 __ push(Operand(esp, kPointerSize)); // Receiver is under value.
2156 __ CallRuntime(Runtime::kToFastProperties, 1);
2157 __ pop(eax);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002158 __ Drop(1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002159 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002160 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2161 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002162}
2163
2164
2165void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2166 // Assignment to a property, using a keyed store IC.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002167 // eax : value
2168 // esp[0] : key
2169 // esp[kPointerSize] : receiver
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002170
2171 // If the assignment starts a block of assignments to the same object,
2172 // change to slow case to avoid the quadratic behavior of repeatedly
2173 // adding fast properties.
2174 if (expr->starts_initialization_block()) {
2175 __ push(result_register());
2176 // Receiver is now under the key and value.
2177 __ push(Operand(esp, 2 * kPointerSize));
2178 __ CallRuntime(Runtime::kToSlowProperties, 1);
2179 __ pop(result_register());
2180 }
2181
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002182 __ pop(ecx); // Key.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002183 if (expr->ends_initialization_block()) {
2184 __ mov(edx, Operand(esp, 0)); // Leave receiver on the stack for later.
2185 } else {
2186 __ pop(edx);
2187 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002188 // Record source code position before IC call.
2189 SetSourcePosition(expr->position());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002190 Handle<Code> ic = is_classic_mode()
2191 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2192 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002193 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002194
2195 // If the assignment ends an initialization block, revert to fast case.
2196 if (expr->ends_initialization_block()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002197 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002198 __ push(eax); // Result of assignment, saved even if not needed.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002199 __ push(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002200 __ CallRuntime(Runtime::kToFastProperties, 1);
2201 __ pop(eax);
2202 }
2203
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002204 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002205 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002206}
2207
2208
2209void FullCodeGenerator::VisitProperty(Property* expr) {
2210 Comment cmnt(masm_, "[ Property");
2211 Expression* key = expr->key();
2212
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002213 if (key->IsPropertyName()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002214 VisitForAccumulatorValue(expr->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002215 __ mov(edx, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002216 EmitNamedPropertyLoad(expr);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002217 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002218 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002219 VisitForStackValue(expr->obj());
2220 VisitForAccumulatorValue(expr->key());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002221 __ pop(edx); // Object.
2222 __ mov(ecx, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002223 EmitKeyedPropertyLoad(expr);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002224 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002225 }
2226}
2227
2228
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002229void FullCodeGenerator::CallIC(Handle<Code> code,
2230 RelocInfo::Mode rmode,
2231 unsigned ast_id) {
2232 ic_total_count_++;
2233 __ call(code, rmode, ast_id);
2234}
2235
2236
2237
2238
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002239void FullCodeGenerator::EmitCallWithIC(Call* expr,
2240 Handle<Object> name,
2241 RelocInfo::Mode mode) {
2242 // Code common for calls using the IC.
2243 ZoneList<Expression*>* args = expr->arguments();
2244 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002245 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002246 for (int i = 0; i < arg_count; i++) {
2247 VisitForStackValue(args->at(i));
2248 }
2249 __ Set(ecx, Immediate(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002250 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002251 // Record source position of the IC call.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002252 SetSourcePosition(expr->position());
danno@chromium.org40cb8782011-05-25 07:58:50 +00002253 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002254 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002255 CallIC(ic, mode, expr->id());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002256 RecordJSReturnSite(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002257 // Restore context register.
2258 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002259 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002260}
2261
2262
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002263void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002264 Expression* key) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002265 // Load the key.
2266 VisitForAccumulatorValue(key);
2267
2268 // Swap the name of the function and the receiver on the stack to follow
2269 // the calling convention for call ICs.
2270 __ pop(ecx);
2271 __ push(eax);
2272 __ push(ecx);
2273
2274 // Load the arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002275 ZoneList<Expression*>* args = expr->arguments();
2276 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002277 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002278 for (int i = 0; i < arg_count; i++) {
2279 VisitForStackValue(args->at(i));
2280 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002281 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002282 // Record source position of the IC call.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002283 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002284 Handle<Code> ic =
2285 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002286 __ mov(ecx, Operand(esp, (arg_count + 1) * kPointerSize)); // Key.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002287 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002288 RecordJSReturnSite(expr);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002289 // Restore context register.
2290 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002291 context()->DropAndPlug(1, eax); // Drop the key still on the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002292}
2293
2294
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002295void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002296 // Code common for calls using the call stub.
2297 ZoneList<Expression*>* args = expr->arguments();
2298 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002299 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002300 for (int i = 0; i < arg_count; i++) {
2301 VisitForStackValue(args->at(i));
2302 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002303 }
2304 // Record source position for debugger.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002305 SetSourcePosition(expr->position());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002306
2307 // Record call targets in unoptimized code, but not in the snapshot.
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002308 if (!Serializer::enabled()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002309 flags = static_cast<CallFunctionFlags>(flags | RECORD_CALL_TARGET);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002310 Handle<Object> uninitialized =
2311 TypeFeedbackCells::UninitializedSentinel(isolate());
2312 Handle<JSGlobalPropertyCell> cell =
2313 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
2314 RecordTypeFeedbackCell(expr->id(), cell);
2315 __ mov(ebx, cell);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002316 }
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002317
lrn@chromium.org34e60782011-09-15 07:25:40 +00002318 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002319 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002320 __ CallStub(&stub, expr->id());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002321
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002322 RecordJSReturnSite(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002323 // Restore context register.
2324 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002325 context()->DropAndPlug(1, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002326}
2327
2328
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002329void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002330 // Push copy of the first argument or undefined if it doesn't exist.
2331 if (arg_count > 0) {
2332 __ push(Operand(esp, arg_count * kPointerSize));
2333 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002334 __ push(Immediate(isolate()->factory()->undefined_value()));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002335 }
2336
2337 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002338 __ push(Operand(ebp, (2 + info_->scope()->num_parameters()) * kPointerSize));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002339 // Push the language mode.
2340 __ push(Immediate(Smi::FromInt(language_mode())));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002341
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002342 // Push the start position of the scope the calls resides in.
2343 __ push(Immediate(Smi::FromInt(scope()->start_position())));
2344
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002345 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002346 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002347}
2348
2349
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002350void FullCodeGenerator::VisitCall(Call* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002351#ifdef DEBUG
2352 // We want to verify that RecordJSReturnSite gets called on all paths
2353 // through this function. Avoid early returns.
2354 expr->return_is_recorded_ = false;
2355#endif
2356
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002357 Comment cmnt(masm_, "[ Call");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002358 Expression* callee = expr->expression();
2359 VariableProxy* proxy = callee->AsVariableProxy();
2360 Property* property = callee->AsProperty();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002361
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002362 if (proxy != NULL && proxy->var()->is_possibly_eval()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002363 // In a call to eval, we first call %ResolvePossiblyDirectEval to
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002364 // resolve the function we need to call and the receiver of the call.
2365 // Then we call the resolved function using the given arguments.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002366 ZoneList<Expression*>* args = expr->arguments();
2367 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002368 { PreservePositionScope pos_scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002369 VisitForStackValue(callee);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002370 // Reserved receiver slot.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002371 __ push(Immediate(isolate()->factory()->undefined_value()));
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002372 // Push the arguments.
2373 for (int i = 0; i < arg_count; i++) {
2374 VisitForStackValue(args->at(i));
2375 }
2376
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002377 // Push a copy of the function (found below the arguments) and
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002378 // resolve eval.
2379 __ push(Operand(esp, (arg_count + 1) * kPointerSize));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002380 EmitResolvePossiblyDirectEval(arg_count);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002381
2382 // The runtime call returns a pair of values in eax (function) and
2383 // edx (receiver). Touch up the stack with the right values.
2384 __ mov(Operand(esp, (arg_count + 0) * kPointerSize), edx);
2385 __ mov(Operand(esp, (arg_count + 1) * kPointerSize), eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002386 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002387 // Record source position for debugger.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002388 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002389 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002390 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002391 __ CallStub(&stub);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002392 RecordJSReturnSite(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002393 // Restore context register.
2394 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002395 context()->DropAndPlug(1, eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002396
2397 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002398 // Push global object as receiver for the call IC.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002399 __ push(GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002400 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2401
2402 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002403 // Call to a lookup slot (dynamically introduced variable).
2404 Label slow, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002405 { PreservePositionScope scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002406 // Generate code for loading from variables potentially shadowed by
2407 // eval-introduced variables.
2408 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002409 }
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002410 __ bind(&slow);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002411 // Call the runtime to find the function to call (returned in eax) and
2412 // the object holding it (returned in edx).
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002413 __ push(context_register());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002414 __ push(Immediate(proxy->name()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002415 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2416 __ push(eax); // Function.
2417 __ push(edx); // Receiver.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002418
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002419 // If fast case code has been generated, emit code to push the function
2420 // and receiver and have the slow path jump around this code.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002421 if (done.is_linked()) {
2422 Label call;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002423 __ jmp(&call, Label::kNear);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002424 __ bind(&done);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002425 // Push function.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002426 __ push(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002427 // The receiver is implicitly the global receiver. Indicate this by
2428 // passing the hole to the call function stub.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002429 __ push(Immediate(isolate()->factory()->the_hole_value()));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002430 __ bind(&call);
2431 }
2432
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002433 // The receiver is either the global receiver or an object found by
2434 // LoadContextSlot. That object could be the hole if the receiver is
2435 // implicitly the global object.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002436 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002437
2438 } else if (property != NULL) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002439 { PreservePositionScope scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002440 VisitForStackValue(property->obj());
2441 }
2442 if (property->key()->IsPropertyName()) {
2443 EmitCallWithIC(expr,
2444 property->key()->AsLiteral()->handle(),
2445 RelocInfo::CODE_TARGET);
2446 } else {
2447 EmitKeyedCallWithIC(expr, property->key());
2448 }
2449
2450 } else {
2451 // Call to an arbitrary expression not handled specially above.
2452 { PreservePositionScope scope(masm()->positions_recorder());
2453 VisitForStackValue(callee);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002454 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002455 // Load global receiver object.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002456 __ mov(ebx, GlobalObjectOperand());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002457 __ push(FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
2458 // Emit function call.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002459 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002460 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002461
2462#ifdef DEBUG
2463 // RecordJSReturnSite should have been called.
2464 ASSERT(expr->return_is_recorded_);
2465#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002466}
2467
2468
2469void FullCodeGenerator::VisitCallNew(CallNew* expr) {
2470 Comment cmnt(masm_, "[ CallNew");
2471 // According to ECMA-262, section 11.2.2, page 44, the function
2472 // expression in new calls must be evaluated before the
2473 // arguments.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002474
ricow@chromium.org65fae842010-08-25 15:26:24 +00002475 // Push constructor on the stack. If it's not a function it's used as
2476 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2477 // ignored.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002478 VisitForStackValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002479
2480 // Push the arguments ("left-to-right") on the stack.
2481 ZoneList<Expression*>* args = expr->arguments();
2482 int arg_count = args->length();
2483 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002484 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002485 }
2486
2487 // Call the construct call builtin that handles allocation and
2488 // constructor invocation.
2489 SetSourcePosition(expr->position());
2490
ricow@chromium.org65fae842010-08-25 15:26:24 +00002491 // Load function and argument count into edi and eax.
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002492 __ Set(eax, Immediate(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002493 __ mov(edi, Operand(esp, arg_count * kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002494
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002495 // Record call targets in unoptimized code, but not in the snapshot.
2496 CallFunctionFlags flags;
2497 if (!Serializer::enabled()) {
2498 flags = RECORD_CALL_TARGET;
2499 Handle<Object> uninitialized =
2500 TypeFeedbackCells::UninitializedSentinel(isolate());
2501 Handle<JSGlobalPropertyCell> cell =
2502 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
2503 RecordTypeFeedbackCell(expr->id(), cell);
2504 __ mov(ebx, cell);
2505 } else {
2506 flags = NO_CALL_FUNCTION_FLAGS;
2507 }
2508
2509 CallConstructStub stub(flags);
2510 __ call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
ulan@chromium.org967e2702012-02-28 09:49:15 +00002511 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002512 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002513}
2514
2515
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002516void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2517 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002518 ASSERT(args->length() == 1);
2519
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002520 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002521
2522 Label materialize_true, materialize_false;
2523 Label* if_true = NULL;
2524 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002525 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002526 context()->PrepareTest(&materialize_true, &materialize_false,
2527 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002528
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002529 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002530 __ test(eax, Immediate(kSmiTagMask));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002531 Split(zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002532
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002533 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002534}
2535
2536
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002537void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2538 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002539 ASSERT(args->length() == 1);
2540
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002541 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002542
2543 Label materialize_true, materialize_false;
2544 Label* if_true = NULL;
2545 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002546 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002547 context()->PrepareTest(&materialize_true, &materialize_false,
2548 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002549
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002550 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002551 __ test(eax, Immediate(kSmiTagMask | 0x80000000));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002552 Split(zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002553
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002554 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002555}
2556
2557
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002558void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2559 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002560 ASSERT(args->length() == 1);
2561
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002562 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002563
2564 Label materialize_true, materialize_false;
2565 Label* if_true = NULL;
2566 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002567 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002568 context()->PrepareTest(&materialize_true, &materialize_false,
2569 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002570
whesse@chromium.org7b260152011-06-20 15:33:18 +00002571 __ JumpIfSmi(eax, if_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002572 __ cmp(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002573 __ j(equal, if_true);
2574 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2575 // Undetectable objects behave like undefined when tested with typeof.
2576 __ movzx_b(ecx, FieldOperand(ebx, Map::kBitFieldOffset));
2577 __ test(ecx, Immediate(1 << Map::kIsUndetectable));
2578 __ j(not_zero, if_false);
2579 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceTypeOffset));
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002580 __ cmp(ecx, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002581 __ j(below, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002582 __ cmp(ecx, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002583 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002584 Split(below_equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002585
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002586 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002587}
2588
2589
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002590void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2591 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002592 ASSERT(args->length() == 1);
2593
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002594 VisitForAccumulatorValue(args->at(0));
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002595
2596 Label materialize_true, materialize_false;
2597 Label* if_true = NULL;
2598 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002599 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002600 context()->PrepareTest(&materialize_true, &materialize_false,
2601 &if_true, &if_false, &fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002602
whesse@chromium.org7b260152011-06-20 15:33:18 +00002603 __ JumpIfSmi(eax, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002604 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002605 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002606 Split(above_equal, if_true, if_false, fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002607
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002608 context()->Plug(if_true, if_false);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002609}
2610
2611
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002612void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2613 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002614 ASSERT(args->length() == 1);
2615
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002616 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002617
2618 Label materialize_true, materialize_false;
2619 Label* if_true = NULL;
2620 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002621 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002622 context()->PrepareTest(&materialize_true, &materialize_false,
2623 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002624
whesse@chromium.org7b260152011-06-20 15:33:18 +00002625 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002626 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2627 __ movzx_b(ebx, FieldOperand(ebx, Map::kBitFieldOffset));
2628 __ test(ebx, Immediate(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002629 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002630 Split(not_zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002631
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002632 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002633}
2634
2635
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002636void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002637 CallRuntime* expr) {
2638 ZoneList<Expression*>* args = expr->arguments();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002639 ASSERT(args->length() == 1);
2640
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002641 VisitForAccumulatorValue(args->at(0));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002642
2643 Label materialize_true, materialize_false;
2644 Label* if_true = NULL;
2645 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002646 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002647 context()->PrepareTest(&materialize_true, &materialize_false,
2648 &if_true, &if_false, &fall_through);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002649
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002650 if (FLAG_debug_code) __ AbortIfSmi(eax);
2651
2652 // Check whether this map has already been checked to be safe for default
2653 // valueOf.
2654 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2655 __ test_b(FieldOperand(ebx, Map::kBitField2Offset),
2656 1 << Map::kStringWrapperSafeForDefaultValueOf);
2657 __ j(not_zero, if_true);
2658
2659 // Check for fast case object. Return false for slow case objects.
2660 __ mov(ecx, FieldOperand(eax, JSObject::kPropertiesOffset));
2661 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
2662 __ cmp(ecx, FACTORY->hash_table_map());
2663 __ j(equal, if_false);
2664
2665 // Look for valueOf symbol in the descriptor array, and indicate false if
2666 // found. The type is not checked, so if it is a transition it is a false
2667 // negative.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002668 __ LoadInstanceDescriptors(ebx, ebx);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002669 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
2670 // ebx: descriptor array
2671 // ecx: length of descriptor array
2672 // Calculate the end of the descriptor array.
2673 STATIC_ASSERT(kSmiTag == 0);
2674 STATIC_ASSERT(kSmiTagSize == 1);
2675 STATIC_ASSERT(kPointerSize == 4);
2676 __ lea(ecx, Operand(ebx, ecx, times_2, FixedArray::kHeaderSize));
2677 // Calculate location of the first key name.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002678 __ add(ebx,
2679 Immediate(FixedArray::kHeaderSize +
2680 DescriptorArray::kFirstIndex * kPointerSize));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002681 // Loop through all the keys in the descriptor array. If one of these is the
2682 // symbol valueOf the result is false.
2683 Label entry, loop;
2684 __ jmp(&entry);
2685 __ bind(&loop);
2686 __ mov(edx, FieldOperand(ebx, 0));
2687 __ cmp(edx, FACTORY->value_of_symbol());
2688 __ j(equal, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002689 __ add(ebx, Immediate(kPointerSize));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002690 __ bind(&entry);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002691 __ cmp(ebx, ecx);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002692 __ j(not_equal, &loop);
2693
2694 // Reload map as register ebx was used as temporary above.
2695 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2696
2697 // If a valueOf property is not found on the object check that it's
2698 // prototype is the un-modified String prototype. If not result is false.
2699 __ mov(ecx, FieldOperand(ebx, Map::kPrototypeOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00002700 __ JumpIfSmi(ecx, if_false);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002701 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
2702 __ mov(edx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2703 __ mov(edx,
2704 FieldOperand(edx, GlobalObject::kGlobalContextOffset));
2705 __ cmp(ecx,
2706 ContextOperand(edx,
2707 Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2708 __ j(not_equal, if_false);
2709 // Set the bit in the map to indicate that it has been checked safe for
2710 // default valueOf and set true result.
2711 __ or_(FieldOperand(ebx, Map::kBitField2Offset),
2712 Immediate(1 << Map::kStringWrapperSafeForDefaultValueOf));
2713 __ jmp(if_true);
2714
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002715 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002716 context()->Plug(if_true, if_false);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002717}
2718
2719
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002720void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2721 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002722 ASSERT(args->length() == 1);
2723
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002724 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002725
2726 Label materialize_true, materialize_false;
2727 Label* if_true = NULL;
2728 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002729 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002730 context()->PrepareTest(&materialize_true, &materialize_false,
2731 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002732
whesse@chromium.org7b260152011-06-20 15:33:18 +00002733 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002734 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002735 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002736 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002737
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002738 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002739}
2740
2741
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002742void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2743 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002744 ASSERT(args->length() == 1);
2745
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002746 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002747
2748 Label materialize_true, materialize_false;
2749 Label* if_true = NULL;
2750 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002751 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002752 context()->PrepareTest(&materialize_true, &materialize_false,
2753 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002754
whesse@chromium.org7b260152011-06-20 15:33:18 +00002755 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002756 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002757 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002758 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002759
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002760 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002761}
2762
2763
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002764void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2765 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002766 ASSERT(args->length() == 1);
2767
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002768 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002769
2770 Label materialize_true, materialize_false;
2771 Label* if_true = NULL;
2772 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002773 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002774 context()->PrepareTest(&materialize_true, &materialize_false,
2775 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002776
whesse@chromium.org7b260152011-06-20 15:33:18 +00002777 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002778 __ CmpObjectType(eax, JS_REGEXP_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002779 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002780 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002781
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002782 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002783}
2784
2785
2786
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002787void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2788 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002789
2790 Label materialize_true, materialize_false;
2791 Label* if_true = NULL;
2792 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002793 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002794 context()->PrepareTest(&materialize_true, &materialize_false,
2795 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002796
2797 // Get the frame pointer for the calling frame.
2798 __ mov(eax, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2799
2800 // Skip the arguments adaptor frame if it exists.
2801 Label check_frame_marker;
2802 __ cmp(Operand(eax, StandardFrameConstants::kContextOffset),
2803 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2804 __ j(not_equal, &check_frame_marker);
2805 __ mov(eax, Operand(eax, StandardFrameConstants::kCallerFPOffset));
2806
2807 // Check the marker in the calling frame.
2808 __ bind(&check_frame_marker);
2809 __ cmp(Operand(eax, StandardFrameConstants::kMarkerOffset),
2810 Immediate(Smi::FromInt(StackFrame::CONSTRUCT)));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002811 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002812 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002813
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002814 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002815}
2816
2817
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002818void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2819 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002820 ASSERT(args->length() == 2);
2821
2822 // Load the two objects into registers and perform the comparison.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002823 VisitForStackValue(args->at(0));
2824 VisitForAccumulatorValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002825
2826 Label materialize_true, materialize_false;
2827 Label* if_true = NULL;
2828 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002829 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002830 context()->PrepareTest(&materialize_true, &materialize_false,
2831 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002832
2833 __ pop(ebx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002834 __ cmp(eax, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002835 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002836 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002837
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002838 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002839}
2840
2841
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002842void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2843 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002844 ASSERT(args->length() == 1);
2845
2846 // ArgumentsAccessStub expects the key in edx and the formal
2847 // parameter count in eax.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002848 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002849 __ mov(edx, eax);
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002850 __ Set(eax, Immediate(Smi::FromInt(info_->scope()->num_parameters())));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002851 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2852 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002853 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002854}
2855
2856
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002857void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2858 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002859
2860 Label exit;
2861 // Get the number of formal parameters.
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002862 __ Set(eax, Immediate(Smi::FromInt(info_->scope()->num_parameters())));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002863
2864 // Check if the calling frame is an arguments adaptor frame.
2865 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2866 __ cmp(Operand(ebx, StandardFrameConstants::kContextOffset),
2867 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2868 __ j(not_equal, &exit);
2869
2870 // Arguments adaptor case: Read the arguments length from the
2871 // adaptor frame.
2872 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2873
2874 __ bind(&exit);
2875 if (FLAG_debug_code) __ AbortIfNotSmi(eax);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002876 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002877}
2878
2879
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002880void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2881 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002882 ASSERT(args->length() == 1);
2883 Label done, null, function, non_function_constructor;
2884
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002885 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002886
2887 // If the object is a smi, we return null.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002888 __ JumpIfSmi(eax, &null);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002889
2890 // Check that the object is a JS object but take special care of JS
2891 // functions to make sure they have 'Function' as their class.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002892 // Assume that there are only two callable types, and one of them is at
2893 // either end of the type range for JS object types. Saves extra comparisons.
2894 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002895 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, eax);
2896 // Map is now in eax.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002897 __ j(below, &null);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002898 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2899 FIRST_SPEC_OBJECT_TYPE + 1);
2900 __ j(equal, &function);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002901
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002902 __ CmpInstanceType(eax, LAST_SPEC_OBJECT_TYPE);
2903 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2904 LAST_SPEC_OBJECT_TYPE - 1);
2905 __ j(equal, &function);
2906 // Assume that there is no larger type.
2907 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002908
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002909 // Check if the constructor in the map is a JS function.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002910 __ mov(eax, FieldOperand(eax, Map::kConstructorOffset));
2911 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
2912 __ j(not_equal, &non_function_constructor);
2913
2914 // eax now contains the constructor function. Grab the
2915 // instance class name from there.
2916 __ mov(eax, FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset));
2917 __ mov(eax, FieldOperand(eax, SharedFunctionInfo::kInstanceClassNameOffset));
2918 __ jmp(&done);
2919
2920 // Functions have class 'Function'.
2921 __ bind(&function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002922 __ mov(eax, isolate()->factory()->function_class_symbol());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002923 __ jmp(&done);
2924
2925 // Objects with a non-function constructor have class 'Object'.
2926 __ bind(&non_function_constructor);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002927 __ mov(eax, isolate()->factory()->Object_symbol());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002928 __ jmp(&done);
2929
2930 // Non-JS objects have class null.
2931 __ bind(&null);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002932 __ mov(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002933
2934 // All done.
2935 __ bind(&done);
2936
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002937 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002938}
2939
2940
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002941void FullCodeGenerator::EmitLog(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002942 // Conditionally generate a log call.
2943 // Args:
2944 // 0 (literal string): The type of logging (corresponds to the flags).
2945 // This is used to determine whether or not to generate the log call.
2946 // 1 (string): Format string. Access the string at argument index 2
2947 // with '%2s' (see Logger::LogRuntime for all the formats).
2948 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002949 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002950 ASSERT_EQ(args->length(), 3);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002951 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002952 VisitForStackValue(args->at(1));
2953 VisitForStackValue(args->at(2));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002954 __ CallRuntime(Runtime::kLog, 2);
2955 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002956 // Finally, we're expected to leave a value on the top of the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002957 __ mov(eax, isolate()->factory()->undefined_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002958 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002959}
2960
2961
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002962void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
2963 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002964
2965 Label slow_allocate_heapnumber;
2966 Label heapnumber_allocated;
2967
2968 __ AllocateHeapNumber(edi, ebx, ecx, &slow_allocate_heapnumber);
2969 __ jmp(&heapnumber_allocated);
2970
2971 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002972 // Allocate a heap number.
2973 __ CallRuntime(Runtime::kNumberAlloc, 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002974 __ mov(edi, eax);
2975
2976 __ bind(&heapnumber_allocated);
2977
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002978 __ PrepareCallCFunction(1, ebx);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002979 __ mov(eax, ContextOperand(context_register(), Context::GLOBAL_INDEX));
2980 __ mov(eax, FieldOperand(eax, GlobalObject::kGlobalContextOffset));
2981 __ mov(Operand(esp, 0), eax);
2982 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002983
2984 // Convert 32 random bits in eax to 0.(32 random bits) in a double
2985 // by computing:
2986 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2987 // This is implemented on both SSE2 and FPU.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002988 if (CpuFeatures::IsSupported(SSE2)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002989 CpuFeatures::Scope fscope(SSE2);
2990 __ mov(ebx, Immediate(0x49800000)); // 1.0 x 2^20 as single.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002991 __ movd(xmm1, ebx);
2992 __ movd(xmm0, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002993 __ cvtss2sd(xmm1, xmm1);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002994 __ xorps(xmm0, xmm1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002995 __ subsd(xmm0, xmm1);
2996 __ movdbl(FieldOperand(edi, HeapNumber::kValueOffset), xmm0);
2997 } else {
2998 // 0x4130000000000000 is 1.0 x 2^20 as a double.
2999 __ mov(FieldOperand(edi, HeapNumber::kExponentOffset),
3000 Immediate(0x41300000));
3001 __ mov(FieldOperand(edi, HeapNumber::kMantissaOffset), eax);
3002 __ fld_d(FieldOperand(edi, HeapNumber::kValueOffset));
3003 __ mov(FieldOperand(edi, HeapNumber::kMantissaOffset), Immediate(0));
3004 __ fld_d(FieldOperand(edi, HeapNumber::kValueOffset));
3005 __ fsubp(1);
3006 __ fstp_d(FieldOperand(edi, HeapNumber::kValueOffset));
3007 }
3008 __ mov(eax, edi);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003009 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003010}
3011
3012
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003013void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003014 // Load the arguments on the stack and call the stub.
3015 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003016 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003017 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003018 VisitForStackValue(args->at(0));
3019 VisitForStackValue(args->at(1));
3020 VisitForStackValue(args->at(2));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003021 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003022 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003023}
3024
3025
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003026void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003027 // Load the arguments on the stack and call the stub.
3028 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003029 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003030 ASSERT(args->length() == 4);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003031 VisitForStackValue(args->at(0));
3032 VisitForStackValue(args->at(1));
3033 VisitForStackValue(args->at(2));
3034 VisitForStackValue(args->at(3));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003035 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003036 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003037}
3038
3039
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003040void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3041 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003042 ASSERT(args->length() == 1);
3043
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003044 VisitForAccumulatorValue(args->at(0)); // Load the object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003045
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003046 Label done;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003047 // If the object is a smi return the object.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003048 __ JumpIfSmi(eax, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003049 // If the object is not a value type, return the object.
3050 __ CmpObjectType(eax, JS_VALUE_TYPE, ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003051 __ j(not_equal, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003052 __ mov(eax, FieldOperand(eax, JSValue::kValueOffset));
3053
3054 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003055 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003056}
3057
3058
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003059void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3060 ZoneList<Expression*>* args = expr->arguments();
3061 ASSERT(args->length() == 2);
3062 ASSERT_NE(NULL, args->at(1)->AsLiteral());
3063 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->handle()));
3064
3065 VisitForAccumulatorValue(args->at(0)); // Load the object.
3066
3067 Label runtime, done;
3068 Register object = eax;
3069 Register result = eax;
3070 Register scratch = ecx;
3071
3072#ifdef DEBUG
3073 __ AbortIfSmi(object);
3074 __ CmpObjectType(object, JS_DATE_TYPE, scratch);
3075 __ Assert(equal, "Trying to get date field from non-date.");
3076#endif
3077
3078 if (index->value() == 0) {
3079 __ mov(result, FieldOperand(object, JSDate::kValueOffset));
3080 } else {
3081 if (index->value() < JSDate::kFirstUncachedField) {
3082 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3083 __ mov(scratch, Operand::StaticVariable(stamp));
3084 __ cmp(scratch, FieldOperand(object, JSDate::kCacheStampOffset));
3085 __ j(not_equal, &runtime, Label::kNear);
3086 __ mov(result, FieldOperand(object, JSDate::kValueOffset +
3087 kPointerSize * index->value()));
3088 __ jmp(&done);
3089 }
3090 __ bind(&runtime);
3091 __ PrepareCallCFunction(2, scratch);
3092 __ mov(Operand(esp, 0), object);
3093 __ mov(Operand(esp, 1 * kPointerSize), Immediate(index));
3094 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
3095 __ bind(&done);
3096 }
3097 context()->Plug(result);
3098}
3099
3100
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003101void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003102 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003103 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003104 ASSERT(args->length() == 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003105 VisitForStackValue(args->at(0));
3106 VisitForStackValue(args->at(1));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003107
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003108 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003109 MathPowStub stub(MathPowStub::ON_STACK);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003110 __ CallStub(&stub);
3111 } else {
3112 __ CallRuntime(Runtime::kMath_pow, 2);
3113 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003114 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003115}
3116
3117
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003118void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3119 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003120 ASSERT(args->length() == 2);
3121
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003122 VisitForStackValue(args->at(0)); // Load the object.
3123 VisitForAccumulatorValue(args->at(1)); // Load the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003124 __ pop(ebx); // eax = value. ebx = object.
3125
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003126 Label done;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003127 // If the object is a smi, return the value.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003128 __ JumpIfSmi(ebx, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003129
3130 // If the object is not a value type, return the value.
3131 __ CmpObjectType(ebx, JS_VALUE_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003132 __ j(not_equal, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003133
3134 // Store the value.
3135 __ mov(FieldOperand(ebx, JSValue::kValueOffset), eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003136
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003137 // Update the write barrier. Save the value as it will be
3138 // overwritten by the write barrier code and is needed afterward.
3139 __ mov(edx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003140 __ RecordWriteField(ebx, JSValue::kValueOffset, edx, ecx, kDontSaveFPRegs);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003141
3142 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003143 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003144}
3145
3146
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003147void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3148 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003149 ASSERT_EQ(args->length(), 1);
3150
3151 // Load the argument on the stack and call the stub.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003152 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003153
3154 NumberToStringStub stub;
3155 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003156 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003157}
3158
3159
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003160void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3161 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003162 ASSERT(args->length() == 1);
3163
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003164 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003165
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003166 Label done;
3167 StringCharFromCodeGenerator generator(eax, ebx);
3168 generator.GenerateFast(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003169 __ jmp(&done);
3170
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003171 NopRuntimeCallHelper call_helper;
3172 generator.GenerateSlow(masm_, call_helper);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003173
3174 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003175 context()->Plug(ebx);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003176}
3177
3178
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003179void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3180 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003181 ASSERT(args->length() == 2);
3182
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003183 VisitForStackValue(args->at(0));
3184 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003185
3186 Register object = ebx;
3187 Register index = eax;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003188 Register result = edx;
3189
3190 __ pop(object);
3191
3192 Label need_conversion;
3193 Label index_out_of_range;
3194 Label done;
3195 StringCharCodeAtGenerator generator(object,
3196 index,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003197 result,
3198 &need_conversion,
3199 &need_conversion,
3200 &index_out_of_range,
3201 STRING_INDEX_IS_NUMBER);
3202 generator.GenerateFast(masm_);
3203 __ jmp(&done);
3204
3205 __ bind(&index_out_of_range);
3206 // When the index is out of range, the spec requires us to return
3207 // NaN.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003208 __ Set(result, Immediate(isolate()->factory()->nan_value()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003209 __ jmp(&done);
3210
3211 __ bind(&need_conversion);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003212 // Move the undefined value into the result register, which will
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003213 // trigger conversion.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003214 __ Set(result, Immediate(isolate()->factory()->undefined_value()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003215 __ jmp(&done);
3216
3217 NopRuntimeCallHelper call_helper;
3218 generator.GenerateSlow(masm_, call_helper);
3219
3220 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003221 context()->Plug(result);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003222}
3223
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003224
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003225void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3226 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003227 ASSERT(args->length() == 2);
3228
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003229 VisitForStackValue(args->at(0));
3230 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003231
3232 Register object = ebx;
3233 Register index = eax;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003234 Register scratch = edx;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003235 Register result = eax;
3236
3237 __ pop(object);
3238
3239 Label need_conversion;
3240 Label index_out_of_range;
3241 Label done;
3242 StringCharAtGenerator generator(object,
3243 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003244 scratch,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003245 result,
3246 &need_conversion,
3247 &need_conversion,
3248 &index_out_of_range,
3249 STRING_INDEX_IS_NUMBER);
3250 generator.GenerateFast(masm_);
3251 __ jmp(&done);
3252
3253 __ bind(&index_out_of_range);
3254 // When the index is out of range, the spec requires us to return
3255 // the empty string.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003256 __ Set(result, Immediate(isolate()->factory()->empty_string()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003257 __ jmp(&done);
3258
3259 __ bind(&need_conversion);
3260 // Move smi zero into the result register, which will trigger
3261 // conversion.
3262 __ Set(result, Immediate(Smi::FromInt(0)));
3263 __ jmp(&done);
3264
3265 NopRuntimeCallHelper call_helper;
3266 generator.GenerateSlow(masm_, call_helper);
3267
3268 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003269 context()->Plug(result);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003270}
3271
3272
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003273void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3274 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003275 ASSERT_EQ(2, args->length());
3276
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003277 VisitForStackValue(args->at(0));
3278 VisitForStackValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003279
3280 StringAddStub stub(NO_STRING_ADD_FLAGS);
3281 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003282 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003283}
3284
3285
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003286void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3287 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003288 ASSERT_EQ(2, args->length());
3289
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003290 VisitForStackValue(args->at(0));
3291 VisitForStackValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003292
3293 StringCompareStub stub;
3294 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003295 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003296}
3297
3298
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003299void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003300 // Load the argument on the stack and call the stub.
whesse@chromium.org023421e2010-12-21 12:19:12 +00003301 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3302 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003303 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003304 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003305 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003306 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003307 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003308}
3309
3310
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003311void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003312 // Load the argument on the stack and call the stub.
whesse@chromium.org023421e2010-12-21 12:19:12 +00003313 TranscendentalCacheStub stub(TranscendentalCache::COS,
3314 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003315 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003316 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003317 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003318 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003319 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003320}
3321
3322
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003323void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3324 // Load the argument on the stack and call the stub.
3325 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3326 TranscendentalCacheStub::TAGGED);
3327 ZoneList<Expression*>* args = expr->arguments();
3328 ASSERT(args->length() == 1);
3329 VisitForStackValue(args->at(0));
3330 __ CallStub(&stub);
3331 context()->Plug(eax);
3332}
3333
3334
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003335void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003336 // Load the argument on the stack and call the stub.
whesse@chromium.org023421e2010-12-21 12:19:12 +00003337 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3338 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003339 ZoneList<Expression*>* args = expr->arguments();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003340 ASSERT(args->length() == 1);
3341 VisitForStackValue(args->at(0));
3342 __ CallStub(&stub);
3343 context()->Plug(eax);
3344}
3345
3346
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003347void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003348 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003349 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003350 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003351 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003352 __ CallRuntime(Runtime::kMath_sqrt, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003353 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003354}
3355
3356
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003357void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3358 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003359 ASSERT(args->length() >= 2);
3360
danno@chromium.org160a7b02011-04-18 15:51:38 +00003361 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3362 for (int i = 0; i < arg_count + 1; ++i) {
3363 VisitForStackValue(args->at(i));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003364 }
danno@chromium.org160a7b02011-04-18 15:51:38 +00003365 VisitForAccumulatorValue(args->last()); // Function.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003366
danno@chromium.orgc612e022011-11-10 11:38:15 +00003367 // Check for proxy.
3368 Label proxy, done;
3369 __ CmpObjectType(eax, JS_FUNCTION_PROXY_TYPE, ebx);
3370 __ j(equal, &proxy);
3371
danno@chromium.org160a7b02011-04-18 15:51:38 +00003372 // InvokeFunction requires the function in edi. Move it in there.
3373 __ mov(edi, result_register());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003374 ParameterCount count(arg_count);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003375 __ InvokeFunction(edi, count, CALL_FUNCTION,
3376 NullCallWrapper(), CALL_AS_METHOD);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003377 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003378 __ jmp(&done);
3379
3380 __ bind(&proxy);
3381 __ push(eax);
3382 __ CallRuntime(Runtime::kCall, args->length());
3383 __ bind(&done);
3384
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003385 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003386}
3387
3388
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003389void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003390 // Load the arguments on the stack and call the stub.
3391 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003392 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003393 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003394 VisitForStackValue(args->at(0));
3395 VisitForStackValue(args->at(1));
3396 VisitForStackValue(args->at(2));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003397 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003398 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003399}
3400
3401
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003402void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3403 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003404 ASSERT_EQ(2, args->length());
3405
3406 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3407 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3408
3409 Handle<FixedArray> jsfunction_result_caches(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003410 isolate()->global_context()->jsfunction_result_caches());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003411 if (jsfunction_result_caches->length() <= cache_id) {
3412 __ Abort("Attempt to use undefined cache.");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003413 __ mov(eax, isolate()->factory()->undefined_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003414 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003415 return;
3416 }
3417
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003418 VisitForAccumulatorValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003419
3420 Register key = eax;
3421 Register cache = ebx;
3422 Register tmp = ecx;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003423 __ mov(cache, ContextOperand(esi, Context::GLOBAL_INDEX));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003424 __ mov(cache,
3425 FieldOperand(cache, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003426 __ mov(cache, ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003427 __ mov(cache,
3428 FieldOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3429
3430 Label done, not_found;
3431 // tmp now holds finger offset as a smi.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003432 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003433 __ mov(tmp, FieldOperand(cache, JSFunctionResultCache::kFingerOffset));
3434 __ cmp(key, CodeGenerator::FixedArrayElementOperand(cache, tmp));
3435 __ j(not_equal, &not_found);
3436
3437 __ mov(eax, CodeGenerator::FixedArrayElementOperand(cache, tmp, 1));
3438 __ jmp(&done);
3439
3440 __ bind(&not_found);
3441 // Call runtime to perform the lookup.
3442 __ push(cache);
3443 __ push(key);
3444 __ CallRuntime(Runtime::kGetFromCache, 2);
3445
3446 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003447 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003448}
3449
3450
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003451void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3452 ZoneList<Expression*>* args = expr->arguments();
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003453 ASSERT_EQ(2, args->length());
3454
3455 Register right = eax;
3456 Register left = ebx;
3457 Register tmp = ecx;
3458
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003459 VisitForStackValue(args->at(0));
3460 VisitForAccumulatorValue(args->at(1));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003461 __ pop(left);
3462
3463 Label done, fail, ok;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003464 __ cmp(left, right);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003465 __ j(equal, &ok);
3466 // Fail if either is a non-HeapObject.
3467 __ mov(tmp, left);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003468 __ and_(tmp, right);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003469 __ JumpIfSmi(tmp, &fail);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003470 __ mov(tmp, FieldOperand(left, HeapObject::kMapOffset));
3471 __ CmpInstanceType(tmp, JS_REGEXP_TYPE);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003472 __ j(not_equal, &fail);
3473 __ cmp(tmp, FieldOperand(right, HeapObject::kMapOffset));
3474 __ j(not_equal, &fail);
3475 __ mov(tmp, FieldOperand(left, JSRegExp::kDataOffset));
3476 __ cmp(tmp, FieldOperand(right, JSRegExp::kDataOffset));
3477 __ j(equal, &ok);
3478 __ bind(&fail);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003479 __ mov(eax, Immediate(isolate()->factory()->false_value()));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003480 __ jmp(&done);
3481 __ bind(&ok);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003482 __ mov(eax, Immediate(isolate()->factory()->true_value()));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003483 __ bind(&done);
3484
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003485 context()->Plug(eax);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003486}
3487
3488
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003489void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3490 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003491 ASSERT(args->length() == 1);
3492
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003493 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003494
3495 if (FLAG_debug_code) {
3496 __ AbortIfNotString(eax);
3497 }
3498
3499 Label materialize_true, materialize_false;
3500 Label* if_true = NULL;
3501 Label* if_false = NULL;
3502 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003503 context()->PrepareTest(&materialize_true, &materialize_false,
3504 &if_true, &if_false, &fall_through);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003505
3506 __ test(FieldOperand(eax, String::kHashFieldOffset),
3507 Immediate(String::kContainsCachedArrayIndexMask));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003508 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003509 Split(zero, if_true, if_false, fall_through);
3510
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003511 context()->Plug(if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003512}
3513
3514
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003515void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3516 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003517 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003518 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003519
3520 if (FLAG_debug_code) {
3521 __ AbortIfNotString(eax);
3522 }
3523
3524 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
3525 __ IndexFromHash(eax, eax);
3526
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003527 context()->Plug(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003528}
3529
3530
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003531void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003532 Label bailout, done, one_char_separator, long_separator,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003533 non_trivial_array, not_size_one_array, loop,
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003534 loop_1, loop_1_condition, loop_2, loop_2_entry, loop_3, loop_3_entry;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003535
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003536 ZoneList<Expression*>* args = expr->arguments();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003537 ASSERT(args->length() == 2);
3538 // We will leave the separator on the stack until the end of the function.
3539 VisitForStackValue(args->at(1));
3540 // Load this to eax (= array)
3541 VisitForAccumulatorValue(args->at(0));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003542 // All aliases of the same register have disjoint lifetimes.
3543 Register array = eax;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003544 Register elements = no_reg; // Will be eax.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003545
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003546 Register index = edx;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003547
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003548 Register string_length = ecx;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003549
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003550 Register string = esi;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003551
3552 Register scratch = ebx;
3553
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003554 Register array_length = edi;
3555 Register result_pos = no_reg; // Will be edi.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003556
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003557 // Separator operand is already pushed.
3558 Operand separator_operand = Operand(esp, 2 * kPointerSize);
3559 Operand result_operand = Operand(esp, 1 * kPointerSize);
3560 Operand array_length_operand = Operand(esp, 0);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003561 __ sub(esp, Immediate(2 * kPointerSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003562 __ cld();
3563 // Check that the array is a JSArray
whesse@chromium.org7b260152011-06-20 15:33:18 +00003564 __ JumpIfSmi(array, &bailout);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003565 __ CmpObjectType(array, JS_ARRAY_TYPE, scratch);
3566 __ j(not_equal, &bailout);
3567
3568 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003569 __ CheckFastElements(scratch, &bailout);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003570
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003571 // If the array has length zero, return the empty string.
3572 __ mov(array_length, FieldOperand(array, JSArray::kLengthOffset));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003573 __ SmiUntag(array_length);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003574 __ j(not_zero, &non_trivial_array);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003575 __ mov(result_operand, isolate()->factory()->empty_string());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003576 __ jmp(&done);
3577
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003578 // Save the array length.
3579 __ bind(&non_trivial_array);
3580 __ mov(array_length_operand, array_length);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003581
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003582 // Save the FixedArray containing array's elements.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003583 // End of array's live range.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003584 elements = array;
3585 __ mov(elements, FieldOperand(array, JSArray::kElementsOffset));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003586 array = no_reg;
3587
3588
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003589 // Check that all array elements are sequential ASCII strings, and
3590 // accumulate the sum of their lengths, as a smi-encoded value.
3591 __ Set(index, Immediate(0));
3592 __ Set(string_length, Immediate(0));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003593 // Loop condition: while (index < length).
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003594 // Live loop registers: index, array_length, string,
3595 // scratch, string_length, elements.
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003596 if (FLAG_debug_code) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003597 __ cmp(index, array_length);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003598 __ Assert(less, "No empty arrays here in EmitFastAsciiArrayJoin");
3599 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003600 __ bind(&loop);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003601 __ mov(string, FieldOperand(elements,
3602 index,
3603 times_pointer_size,
3604 FixedArray::kHeaderSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003605 __ JumpIfSmi(string, &bailout);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003606 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
3607 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
3608 __ and_(scratch, Immediate(
3609 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
3610 __ cmp(scratch, kStringTag | kAsciiStringTag | kSeqStringTag);
3611 __ j(not_equal, &bailout);
3612 __ add(string_length,
3613 FieldOperand(string, SeqAsciiString::kLengthOffset));
3614 __ j(overflow, &bailout);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003615 __ add(index, Immediate(1));
3616 __ cmp(index, array_length);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003617 __ j(less, &loop);
3618
3619 // If array_length is 1, return elements[0], a string.
3620 __ cmp(array_length, 1);
3621 __ j(not_equal, &not_size_one_array);
3622 __ mov(scratch, FieldOperand(elements, FixedArray::kHeaderSize));
3623 __ mov(result_operand, scratch);
3624 __ jmp(&done);
3625
3626 __ bind(&not_size_one_array);
3627
3628 // End of array_length live range.
3629 result_pos = array_length;
3630 array_length = no_reg;
3631
3632 // Live registers:
3633 // string_length: Sum of string lengths, as a smi.
3634 // elements: FixedArray of strings.
3635
3636 // Check that the separator is a flat ASCII string.
3637 __ mov(string, separator_operand);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003638 __ JumpIfSmi(string, &bailout);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003639 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
3640 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003641 __ and_(scratch, Immediate(
3642 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003643 __ cmp(scratch, ASCII_STRING_TYPE);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003644 __ j(not_equal, &bailout);
3645
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003646 // Add (separator length times array_length) - separator length
3647 // to string_length.
3648 __ mov(scratch, separator_operand);
3649 __ mov(scratch, FieldOperand(scratch, SeqAsciiString::kLengthOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003650 __ sub(string_length, scratch); // May be negative, temporarily.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003651 __ imul(scratch, array_length_operand);
3652 __ j(overflow, &bailout);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003653 __ add(string_length, scratch);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003654 __ j(overflow, &bailout);
3655
3656 __ shr(string_length, 1);
3657 // Live registers and stack values:
3658 // string_length
3659 // elements
3660 __ AllocateAsciiString(result_pos, string_length, scratch,
3661 index, string, &bailout);
3662 __ mov(result_operand, result_pos);
3663 __ lea(result_pos, FieldOperand(result_pos, SeqAsciiString::kHeaderSize));
3664
3665
3666 __ mov(string, separator_operand);
3667 __ cmp(FieldOperand(string, SeqAsciiString::kLengthOffset),
3668 Immediate(Smi::FromInt(1)));
3669 __ j(equal, &one_char_separator);
3670 __ j(greater, &long_separator);
3671
3672
3673 // Empty separator case
3674 __ mov(index, Immediate(0));
3675 __ jmp(&loop_1_condition);
3676 // Loop condition: while (index < length).
3677 __ bind(&loop_1);
3678 // Each iteration of the loop concatenates one string to the result.
3679 // Live values in registers:
3680 // index: which element of the elements array we are adding to the result.
3681 // result_pos: the position to which we are currently copying characters.
3682 // elements: the FixedArray of strings we are joining.
3683
3684 // Get string = array[index].
3685 __ mov(string, FieldOperand(elements, index,
3686 times_pointer_size,
3687 FixedArray::kHeaderSize));
3688 __ mov(string_length,
3689 FieldOperand(string, String::kLengthOffset));
3690 __ shr(string_length, 1);
3691 __ lea(string,
3692 FieldOperand(string, SeqAsciiString::kHeaderSize));
3693 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003694 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003695 __ bind(&loop_1_condition);
3696 __ cmp(index, array_length_operand);
3697 __ j(less, &loop_1); // End while (index < length).
3698 __ jmp(&done);
3699
3700
3701
3702 // One-character separator case
3703 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003704 // Replace separator with its ASCII character value.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003705 __ mov_b(scratch, FieldOperand(string, SeqAsciiString::kHeaderSize));
3706 __ mov_b(separator_operand, scratch);
3707
3708 __ Set(index, Immediate(0));
3709 // Jump into the loop after the code that copies the separator, so the first
3710 // element is not preceded by a separator
3711 __ jmp(&loop_2_entry);
3712 // Loop condition: while (index < length).
3713 __ bind(&loop_2);
3714 // Each iteration of the loop concatenates one string to the result.
3715 // Live values in registers:
3716 // index: which element of the elements array we are adding to the result.
3717 // result_pos: the position to which we are currently copying characters.
3718
3719 // Copy the separator character to the result.
3720 __ mov_b(scratch, separator_operand);
3721 __ mov_b(Operand(result_pos, 0), scratch);
3722 __ inc(result_pos);
3723
3724 __ bind(&loop_2_entry);
3725 // Get string = array[index].
3726 __ mov(string, FieldOperand(elements, index,
3727 times_pointer_size,
3728 FixedArray::kHeaderSize));
3729 __ mov(string_length,
3730 FieldOperand(string, String::kLengthOffset));
3731 __ shr(string_length, 1);
3732 __ lea(string,
3733 FieldOperand(string, SeqAsciiString::kHeaderSize));
3734 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003735 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003736
3737 __ cmp(index, array_length_operand);
3738 __ j(less, &loop_2); // End while (index < length).
3739 __ jmp(&done);
3740
3741
3742 // Long separator case (separator is more than one character).
3743 __ bind(&long_separator);
3744
3745 __ Set(index, Immediate(0));
3746 // Jump into the loop after the code that copies the separator, so the first
3747 // element is not preceded by a separator
3748 __ jmp(&loop_3_entry);
3749 // Loop condition: while (index < length).
3750 __ bind(&loop_3);
3751 // Each iteration of the loop concatenates one string to the result.
3752 // Live values in registers:
3753 // index: which element of the elements array we are adding to the result.
3754 // result_pos: the position to which we are currently copying characters.
3755
3756 // Copy the separator to the result.
3757 __ mov(string, separator_operand);
3758 __ mov(string_length,
3759 FieldOperand(string, String::kLengthOffset));
3760 __ shr(string_length, 1);
3761 __ lea(string,
3762 FieldOperand(string, SeqAsciiString::kHeaderSize));
3763 __ CopyBytes(string, result_pos, string_length, scratch);
3764
3765 __ bind(&loop_3_entry);
3766 // Get string = array[index].
3767 __ mov(string, FieldOperand(elements, index,
3768 times_pointer_size,
3769 FixedArray::kHeaderSize));
3770 __ mov(string_length,
3771 FieldOperand(string, String::kLengthOffset));
3772 __ shr(string_length, 1);
3773 __ lea(string,
3774 FieldOperand(string, SeqAsciiString::kHeaderSize));
3775 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003776 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003777
3778 __ cmp(index, array_length_operand);
3779 __ j(less, &loop_3); // End while (index < length).
3780 __ jmp(&done);
3781
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003782
3783 __ bind(&bailout);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003784 __ mov(result_operand, isolate()->factory()->undefined_value());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003785 __ bind(&done);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003786 __ mov(eax, result_operand);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003787 // Drop temp values from the stack, and restore context register.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003788 __ add(esp, Immediate(3 * kPointerSize));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003789
3790 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
3791 context()->Plug(eax);
3792}
3793
3794
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003795void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003796 Handle<String> name = expr->name();
3797 if (name->length() > 0 && name->Get(0) == '_') {
3798 Comment cmnt(masm_, "[ InlineRuntimeCall");
3799 EmitInlineRuntimeCall(expr);
3800 return;
3801 }
3802
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003803 Comment cmnt(masm_, "[ CallRuntime");
3804 ZoneList<Expression*>* args = expr->arguments();
3805
3806 if (expr->is_jsruntime()) {
3807 // Prepare for calling JS runtime function.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003808 __ mov(eax, GlobalObjectOperand());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003809 __ push(FieldOperand(eax, GlobalObject::kBuiltinsOffset));
3810 }
3811
3812 // Push the arguments ("left-to-right").
3813 int arg_count = args->length();
3814 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003815 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003816 }
3817
3818 if (expr->is_jsruntime()) {
3819 // Call the JS runtime function via a call IC.
3820 __ Set(ecx, Immediate(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003821 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
lrn@chromium.org34e60782011-09-15 07:25:40 +00003822 Handle<Code> ic =
3823 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00003824 CallIC(ic, mode, expr->id());
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003825 // Restore context register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003826 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
3827 } else {
3828 // Call the C runtime function.
3829 __ CallRuntime(expr->function(), arg_count);
3830 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003831 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003832}
3833
3834
3835void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
3836 switch (expr->op()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003837 case Token::DELETE: {
3838 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003839 Property* property = expr->expression()->AsProperty();
3840 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003841
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003842 if (property != NULL) {
3843 VisitForStackValue(property->obj());
3844 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003845 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
3846 ? kNonStrictMode : kStrictMode;
3847 __ push(Immediate(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003848 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003849 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003850 } else if (proxy != NULL) {
3851 Variable* var = proxy->var();
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003852 // Delete of an unqualified identifier is disallowed in strict mode
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003853 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003854 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003855 if (var->IsUnallocated()) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003856 __ push(GlobalObjectOperand());
3857 __ push(Immediate(var->name()));
3858 __ push(Immediate(Smi::FromInt(kNonStrictMode)));
3859 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3860 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003861 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
3862 // Result of deleting non-global variables is false. 'this' is
3863 // not really a variable, though we implement it as one. The
3864 // subexpression does not have side effects.
3865 context()->Plug(var->is_this());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003866 } else {
3867 // Non-global variable. Call the runtime to try to delete from the
3868 // context where the variable was introduced.
3869 __ push(context_register());
3870 __ push(Immediate(var->name()));
3871 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3872 context()->Plug(eax);
3873 }
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00003874 } else {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003875 // Result of deleting non-property, non-variable reference is true.
3876 // The subexpression may have side effects.
3877 VisitForEffect(expr->expression());
3878 context()->Plug(true);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003879 }
3880 break;
3881 }
3882
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003883 case Token::VOID: {
3884 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3885 VisitForEffect(expr->expression());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003886 context()->Plug(isolate()->factory()->undefined_value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003887 break;
3888 }
3889
3890 case Token::NOT: {
3891 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003892 if (context()->IsEffect()) {
3893 // Unary NOT has no side effects so it's only necessary to visit the
3894 // subexpression. Match the optimizing compiler by not branching.
3895 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003896 } else if (context()->IsTest()) {
3897 const TestContext* test = TestContext::cast(context());
3898 // The labels are swapped for the recursive call.
3899 VisitForControl(expr->expression(),
3900 test->false_label(),
3901 test->true_label(),
3902 test->fall_through());
3903 context()->Plug(test->true_label(), test->false_label());
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003904 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003905 // We handle value contexts explicitly rather than simply visiting
3906 // for control and plugging the control flow into the context,
3907 // because we need to prepare a pair of extra administrative AST ids
3908 // for the optimizing compiler.
3909 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3910 Label materialize_true, materialize_false, done;
3911 VisitForControl(expr->expression(),
3912 &materialize_false,
3913 &materialize_true,
3914 &materialize_true);
3915 __ bind(&materialize_true);
3916 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3917 if (context()->IsAccumulatorValue()) {
3918 __ mov(eax, isolate()->factory()->true_value());
3919 } else {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003920 __ Push(isolate()->factory()->true_value());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003921 }
3922 __ jmp(&done, Label::kNear);
3923 __ bind(&materialize_false);
3924 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3925 if (context()->IsAccumulatorValue()) {
3926 __ mov(eax, isolate()->factory()->false_value());
3927 } else {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003928 __ Push(isolate()->factory()->false_value());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003929 }
3930 __ bind(&done);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003931 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003932 break;
3933 }
3934
3935 case Token::TYPEOF: {
3936 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003937 { StackValueContext context(this);
3938 VisitForTypeofValue(expr->expression());
3939 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003940 __ CallRuntime(Runtime::kTypeof, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003941 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003942 break;
3943 }
3944
3945 case Token::ADD: {
3946 Comment cmt(masm_, "[ UnaryOperation (ADD)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003947 VisitForAccumulatorValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003948 Label no_conversion;
whesse@chromium.org7b260152011-06-20 15:33:18 +00003949 __ JumpIfSmi(result_register(), &no_conversion);
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003950 ToNumberStub convert_stub;
3951 __ CallStub(&convert_stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003952 __ bind(&no_conversion);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003953 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003954 break;
3955 }
3956
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003957 case Token::SUB:
3958 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003959 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003960
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003961 case Token::BIT_NOT:
3962 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003963 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003964
3965 default:
3966 UNREACHABLE();
3967 }
3968}
3969
3970
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003971void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
3972 const char* comment) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003973 Comment cmt(masm_, comment);
3974 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
3975 UnaryOverwriteMode overwrite =
3976 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00003977 UnaryOpStub stub(expr->op(), overwrite);
3978 // UnaryOpStub expects the argument to be in the
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003979 // accumulator register eax.
3980 VisitForAccumulatorValue(expr->expression());
3981 SetSourcePosition(expr->position());
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00003982 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->id());
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003983 context()->Plug(eax);
3984}
3985
3986
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003987void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
3988 Comment cmnt(masm_, "[ CountOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00003989 SetSourcePosition(expr->position());
3990
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003991 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
3992 // as the left-hand side.
3993 if (!expr->expression()->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003994 VisitForEffect(expr->expression());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003995 return;
3996 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003997
3998 // Expression can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00003999 // slot.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004000 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
4001 LhsKind assign_type = VARIABLE;
4002 Property* prop = expr->expression()->AsProperty();
4003 // In case of a property we use the uninitialized expression context
4004 // of the key to detect a named property.
4005 if (prop != NULL) {
4006 assign_type =
4007 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
4008 }
4009
4010 // Evaluate expression and get value.
4011 if (assign_type == VARIABLE) {
4012 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004013 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00004014 EmitVariableLoad(expr->expression()->AsVariableProxy());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004015 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004016 // Reserve space for result of postfix operation.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004017 if (expr->is_postfix() && !context()->IsEffect()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004018 __ push(Immediate(Smi::FromInt(0)));
4019 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004020 if (assign_type == NAMED_PROPERTY) {
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004021 // Put the object both on the stack and in edx.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004022 VisitForAccumulatorValue(prop->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00004023 __ push(eax);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004024 __ mov(edx, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004025 EmitNamedPropertyLoad(prop);
4026 } else {
whesse@chromium.org7b260152011-06-20 15:33:18 +00004027 VisitForStackValue(prop->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004028 VisitForStackValue(prop->key());
4029 __ mov(edx, Operand(esp, kPointerSize)); // Object.
4030 __ mov(ecx, Operand(esp, 0)); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004031 EmitKeyedPropertyLoad(prop);
4032 }
4033 }
4034
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004035 // We need a second deoptimization point after loading the value
4036 // in case evaluating the property load my have a side effect.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004037 if (assign_type == VARIABLE) {
4038 PrepareForBailout(expr->expression(), TOS_REG);
4039 } else {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00004040 PrepareForBailoutForId(expr->CountId(), TOS_REG);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004041 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004042
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004043 // Call ToNumber only if operand is not a smi.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004044 Label no_conversion;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004045 if (ShouldInlineSmiCase(expr->op())) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00004046 __ JumpIfSmi(eax, &no_conversion, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004047 }
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004048 ToNumberStub convert_stub;
4049 __ CallStub(&convert_stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004050 __ bind(&no_conversion);
4051
4052 // Save result for postfix expressions.
4053 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004054 if (!context()->IsEffect()) {
4055 // Save the result on the stack. If we have a named or keyed property
4056 // we store the result under the receiver that is currently on top
4057 // of the stack.
4058 switch (assign_type) {
4059 case VARIABLE:
4060 __ push(eax);
4061 break;
4062 case NAMED_PROPERTY:
4063 __ mov(Operand(esp, kPointerSize), eax);
4064 break;
4065 case KEYED_PROPERTY:
4066 __ mov(Operand(esp, 2 * kPointerSize), eax);
4067 break;
4068 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004069 }
4070 }
4071
4072 // Inline smi case if we are in a loop.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004073 Label done, stub_call;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004074 JumpPatchSite patch_site(masm_);
4075
ricow@chromium.org65fae842010-08-25 15:26:24 +00004076 if (ShouldInlineSmiCase(expr->op())) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004077 if (expr->op() == Token::INC) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004078 __ add(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004079 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004080 __ sub(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004081 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004082 __ j(overflow, &stub_call, Label::kNear);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004083 // We could eliminate this smi check if we split the code at
4084 // the first smi check before calling ToNumber.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004085 patch_site.EmitJumpIfSmi(eax, &done, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004086
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004087 __ bind(&stub_call);
4088 // Call stub. Undo operation first.
4089 if (expr->op() == Token::INC) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004090 __ sub(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004091 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004092 __ add(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004093 }
4094 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004095
4096 // Record position before stub call.
4097 SetSourcePosition(expr->position());
4098
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004099 // Call stub for +1/-1.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004100 __ mov(edx, eax);
4101 __ mov(eax, Immediate(Smi::FromInt(1)));
danno@chromium.org40cb8782011-05-25 07:58:50 +00004102 BinaryOpStub stub(expr->binary_op(), NO_OVERWRITE);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004103 CallIC(stub.GetCode(), RelocInfo::CODE_TARGET, expr->CountId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004104 patch_site.EmitPatchInfo();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004105 __ bind(&done);
4106
4107 // Store the value returned in eax.
4108 switch (assign_type) {
4109 case VARIABLE:
4110 if (expr->is_postfix()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004111 // Perform the assignment as if via '='.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004112 { EffectContext context(this);
4113 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4114 Token::ASSIGN);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004115 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4116 context.Plug(eax);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004117 }
4118 // For all contexts except EffectContext We have the result on
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004119 // top of the stack.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004120 if (!context()->IsEffect()) {
4121 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004122 }
4123 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004124 // Perform the assignment as if via '='.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004125 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004126 Token::ASSIGN);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004127 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4128 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004129 }
4130 break;
4131 case NAMED_PROPERTY: {
4132 __ mov(ecx, prop->key()->AsLiteral()->handle());
4133 __ pop(edx);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004134 Handle<Code> ic = is_classic_mode()
4135 ? isolate()->builtins()->StoreIC_Initialize()
4136 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004137 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004138 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004139 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004140 if (!context()->IsEffect()) {
4141 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004142 }
4143 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004144 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004145 }
4146 break;
4147 }
4148 case KEYED_PROPERTY: {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004149 __ pop(ecx);
4150 __ pop(edx);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004151 Handle<Code> ic = is_classic_mode()
4152 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4153 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004154 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004155 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004156 if (expr->is_postfix()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004157 // Result is on the stack
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004158 if (!context()->IsEffect()) {
4159 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004160 }
4161 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004162 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004163 }
4164 break;
4165 }
4166 }
4167}
4168
4169
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004170void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004171 VariableProxy* proxy = expr->AsVariableProxy();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004172 ASSERT(!context()->IsEffect());
4173 ASSERT(!context()->IsTest());
4174
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004175 if (proxy != NULL && proxy->var()->IsUnallocated()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004176 Comment cmnt(masm_, "Global variable");
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004177 __ mov(edx, GlobalObjectOperand());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004178 __ mov(ecx, Immediate(proxy->name()));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004179 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004180 // Use a regular load, not a contextual load, to avoid a reference
4181 // error.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004182 CallIC(ic);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004183 PrepareForBailout(expr, TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004184 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004185 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004186 Label done, slow;
4187
4188 // Generate code for loading from variables potentially shadowed
4189 // by eval-introduced variables.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004190 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004191
4192 __ bind(&slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004193 __ push(esi);
4194 __ push(Immediate(proxy->name()));
4195 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004196 PrepareForBailout(expr, TOS_REG);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004197 __ bind(&done);
4198
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004199 context()->Plug(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004200 } else {
4201 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004202 VisitInDuplicateContext(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004203 }
4204}
4205
4206
ager@chromium.org04921a82011-06-27 13:21:41 +00004207void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004208 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004209 Handle<String> check) {
4210 Label materialize_true, materialize_false;
4211 Label* if_true = NULL;
4212 Label* if_false = NULL;
4213 Label* fall_through = NULL;
4214 context()->PrepareTest(&materialize_true, &materialize_false,
4215 &if_true, &if_false, &fall_through);
4216
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004217 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004218 VisitForTypeofValue(sub_expr);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004219 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004220 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004221
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004222 if (check->Equals(isolate()->heap()->number_symbol())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004223 __ JumpIfSmi(eax, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004224 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004225 isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004226 Split(equal, if_true, if_false, fall_through);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004227 } else if (check->Equals(isolate()->heap()->string_symbol())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004228 __ JumpIfSmi(eax, if_false);
4229 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edx);
4230 __ j(above_equal, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004231 // Check for undetectable objects => false.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004232 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
4233 1 << Map::kIsUndetectable);
4234 Split(zero, if_true, if_false, fall_through);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004235 } else if (check->Equals(isolate()->heap()->boolean_symbol())) {
4236 __ cmp(eax, isolate()->factory()->true_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004237 __ j(equal, if_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004238 __ cmp(eax, isolate()->factory()->false_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004239 Split(equal, if_true, if_false, fall_through);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004240 } else if (FLAG_harmony_typeof &&
4241 check->Equals(isolate()->heap()->null_symbol())) {
4242 __ cmp(eax, isolate()->factory()->null_value());
4243 Split(equal, if_true, if_false, fall_through);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004244 } else if (check->Equals(isolate()->heap()->undefined_symbol())) {
4245 __ cmp(eax, isolate()->factory()->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004246 __ j(equal, if_true);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004247 __ JumpIfSmi(eax, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004248 // Check for undetectable objects => true.
4249 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
4250 __ movzx_b(ecx, FieldOperand(edx, Map::kBitFieldOffset));
4251 __ test(ecx, Immediate(1 << Map::kIsUndetectable));
4252 Split(not_zero, if_true, if_false, fall_through);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004253 } else if (check->Equals(isolate()->heap()->function_symbol())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004254 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004255 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4256 __ CmpObjectType(eax, JS_FUNCTION_TYPE, edx);
4257 __ j(equal, if_true);
4258 __ CmpInstanceType(edx, JS_FUNCTION_PROXY_TYPE);
4259 Split(equal, if_true, if_false, fall_through);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004260 } else if (check->Equals(isolate()->heap()->object_symbol())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004261 __ JumpIfSmi(eax, if_false);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004262 if (!FLAG_harmony_typeof) {
4263 __ cmp(eax, isolate()->factory()->null_value());
4264 __ j(equal, if_true);
4265 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004266 __ CmpObjectType(eax, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE, edx);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004267 __ j(below, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004268 __ CmpInstanceType(edx, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
4269 __ j(above, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004270 // Check for undetectable objects => false.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004271 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
4272 1 << Map::kIsUndetectable);
4273 Split(zero, if_true, if_false, fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004274 } else {
4275 if (if_false != fall_through) __ jmp(if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004276 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004277 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004278}
4279
4280
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004281void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
4282 Comment cmnt(masm_, "[ CompareOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00004283 SetSourcePosition(expr->position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004284
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004285 // First we try a fast inlined version of the compare when one of
4286 // the operands is a literal.
4287 if (TryLiteralCompare(expr)) return;
4288
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004289 // Always perform the comparison for its control flow. Pack the result
4290 // into the expression's context after the comparison is performed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004291 Label materialize_true, materialize_false;
4292 Label* if_true = NULL;
4293 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004294 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004295 context()->PrepareTest(&materialize_true, &materialize_false,
4296 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004297
ager@chromium.org04921a82011-06-27 13:21:41 +00004298 Token::Value op = expr->op();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004299 VisitForStackValue(expr->left());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004300 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004301 case Token::IN:
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004302 VisitForStackValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004303 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004304 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004305 __ cmp(eax, isolate()->factory()->true_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004306 Split(equal, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004307 break;
4308
4309 case Token::INSTANCEOF: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004310 VisitForStackValue(expr->right());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004311 InstanceofStub stub(InstanceofStub::kNoFlags);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004312 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004313 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004314 __ test(eax, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004315 // The stub returns 0 for true.
4316 Split(zero, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004317 break;
4318 }
4319
4320 default: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004321 VisitForAccumulatorValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004322 Condition cc = no_condition;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004323 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004324 case Token::EQ_STRICT:
ricow@chromium.org65fae842010-08-25 15:26:24 +00004325 case Token::EQ:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004326 cc = equal;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004327 break;
4328 case Token::LT:
4329 cc = less;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004330 break;
4331 case Token::GT:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004332 cc = greater;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004333 break;
4334 case Token::LTE:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004335 cc = less_equal;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004336 break;
4337 case Token::GTE:
4338 cc = greater_equal;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004339 break;
4340 case Token::IN:
4341 case Token::INSTANCEOF:
4342 default:
4343 UNREACHABLE();
4344 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004345 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004346
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004347 bool inline_smi_code = ShouldInlineSmiCase(op);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004348 JumpPatchSite patch_site(masm_);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004349 if (inline_smi_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004350 Label slow_case;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004351 __ mov(ecx, edx);
4352 __ or_(ecx, eax);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004353 patch_site.EmitJumpIfNotSmi(ecx, &slow_case, Label::kNear);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004354 __ cmp(edx, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004355 Split(cc, if_true, if_false, NULL);
4356 __ bind(&slow_case);
4357 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004358
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004359 // Record position and call the compare IC.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004360 SetSourcePosition(expr->position());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004361 Handle<Code> ic = CompareIC::GetUninitialized(op);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004362 CallIC(ic, RelocInfo::CODE_TARGET, expr->id());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004363 patch_site.EmitPatchInfo();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004364
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004365 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004366 __ test(eax, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004367 Split(cc, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004368 }
4369 }
4370
4371 // Convert the result of the comparison into one expected for this
4372 // expression's context.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004373 context()->Plug(if_true, if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004374}
4375
4376
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004377void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4378 Expression* sub_expr,
4379 NilValue nil) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004380 Label materialize_true, materialize_false;
4381 Label* if_true = NULL;
4382 Label* if_false = NULL;
4383 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004384 context()->PrepareTest(&materialize_true, &materialize_false,
4385 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004386
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004387 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004388 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004389 Handle<Object> nil_value = nil == kNullValue ?
4390 isolate()->factory()->null_value() :
4391 isolate()->factory()->undefined_value();
4392 __ cmp(eax, nil_value);
4393 if (expr->op() == Token::EQ_STRICT) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004394 Split(equal, if_true, if_false, fall_through);
4395 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004396 Handle<Object> other_nil_value = nil == kNullValue ?
4397 isolate()->factory()->undefined_value() :
4398 isolate()->factory()->null_value();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004399 __ j(equal, if_true);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004400 __ cmp(eax, other_nil_value);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004401 __ j(equal, if_true);
whesse@chromium.org7b260152011-06-20 15:33:18 +00004402 __ JumpIfSmi(eax, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004403 // It can be an undetectable object.
4404 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
4405 __ movzx_b(edx, FieldOperand(edx, Map::kBitFieldOffset));
4406 __ test(edx, Immediate(1 << Map::kIsUndetectable));
4407 Split(not_zero, if_true, if_false, fall_through);
4408 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004409 context()->Plug(if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004410}
4411
4412
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004413void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
4414 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004415 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004416}
4417
4418
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00004419Register FullCodeGenerator::result_register() {
4420 return eax;
4421}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004422
4423
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00004424Register FullCodeGenerator::context_register() {
4425 return esi;
4426}
4427
4428
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004429void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
4430 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4431 __ mov(Operand(ebp, frame_offset), value);
4432}
4433
4434
4435void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004436 __ mov(dst, ContextOperand(esi, context_index));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004437}
4438
4439
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004440void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004441 Scope* declaration_scope = scope()->DeclarationScope();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00004442 if (declaration_scope->is_global_scope() ||
4443 declaration_scope->is_module_scope()) {
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004444 // Contexts nested in the global context have a canonical empty function
4445 // as their closure, not the anonymous closure containing the global
4446 // code. Pass a smi sentinel and let the runtime look up the empty
4447 // function.
4448 __ push(Immediate(Smi::FromInt(0)));
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004449 } else if (declaration_scope->is_eval_scope()) {
4450 // Contexts nested inside eval code have the same closure as the context
4451 // calling eval, not the anonymous closure containing the eval code.
4452 // Fetch it from the context.
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004453 __ push(ContextOperand(esi, Context::CLOSURE_INDEX));
4454 } else {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004455 ASSERT(declaration_scope->is_function_scope());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004456 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
4457 }
4458}
4459
4460
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004461// ----------------------------------------------------------------------------
4462// Non-local control flow support.
4463
4464void FullCodeGenerator::EnterFinallyBlock() {
4465 // Cook return address on top of stack (smi encoded Code* delta)
4466 ASSERT(!result_register().is(edx));
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004467 __ pop(edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004468 __ sub(edx, Immediate(masm_->CodeObject()));
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00004469 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4470 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004471 __ SmiTag(edx);
4472 __ push(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004473
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004474 // Store result register while executing finally block.
4475 __ push(result_register());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004476
4477 // Store pending message while executing finally block.
4478 ExternalReference pending_message_obj =
4479 ExternalReference::address_of_pending_message_obj(isolate());
4480 __ mov(edx, Operand::StaticVariable(pending_message_obj));
4481 __ push(edx);
4482
4483 ExternalReference has_pending_message =
4484 ExternalReference::address_of_has_pending_message(isolate());
4485 __ mov(edx, Operand::StaticVariable(has_pending_message));
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004486 __ SmiTag(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004487 __ push(edx);
4488
4489 ExternalReference pending_message_script =
4490 ExternalReference::address_of_pending_message_script(isolate());
4491 __ mov(edx, Operand::StaticVariable(pending_message_script));
4492 __ push(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004493}
4494
4495
4496void FullCodeGenerator::ExitFinallyBlock() {
4497 ASSERT(!result_register().is(edx));
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004498 // Restore pending message from stack.
4499 __ pop(edx);
4500 ExternalReference pending_message_script =
4501 ExternalReference::address_of_pending_message_script(isolate());
4502 __ mov(Operand::StaticVariable(pending_message_script), edx);
4503
4504 __ pop(edx);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004505 __ SmiUntag(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004506 ExternalReference has_pending_message =
4507 ExternalReference::address_of_has_pending_message(isolate());
4508 __ mov(Operand::StaticVariable(has_pending_message), edx);
4509
4510 __ pop(edx);
4511 ExternalReference pending_message_obj =
4512 ExternalReference::address_of_pending_message_obj(isolate());
4513 __ mov(Operand::StaticVariable(pending_message_obj), edx);
4514
4515 // Restore result register from stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004516 __ pop(result_register());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004517
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004518 // Uncook return address.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004519 __ pop(edx);
4520 __ SmiUntag(edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004521 __ add(edx, Immediate(masm_->CodeObject()));
4522 __ jmp(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004523}
4524
4525
4526#undef __
4527
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004528#define __ ACCESS_MASM(masm())
4529
4530FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4531 int* stack_depth,
4532 int* context_length) {
4533 // The macros used here must preserve the result register.
4534
4535 // Because the handler block contains the context of the finally
4536 // code, we can restore it directly from there for the finally code
4537 // rather than iteratively unwinding contexts via their previous
4538 // links.
4539 __ Drop(*stack_depth); // Down to the handler block.
4540 if (*context_length > 0) {
4541 // Restore the context to its dedicated register and the stack.
4542 __ mov(esi, Operand(esp, StackHandlerConstants::kContextOffset));
4543 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
4544 }
4545 __ PopTryHandler();
4546 __ call(finally_entry_);
4547
4548 *stack_depth = 0;
4549 *context_length = 0;
4550 return previous_;
4551}
4552
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004553#undef __
4554
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004555} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004556
4557#endif // V8_TARGET_ARCH_IA32