blob: 9c08ef3033de13d7a74c3964aea2f50877220b2e [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(
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000122 Handle<Smi>(Smi::FromInt(FLAG_interrupt_budget), isolate()));
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 &&
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000130 info->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000131 __ 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
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +0000159 info->set_prologue_offset(masm_->pc_offset());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000160 __ push(ebp); // Caller's frame pointer.
161 __ mov(ebp, esp);
162 __ push(esi); // Callee's context.
163 __ push(edi); // Callee's JS Function.
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000164 info->AddNoFrameRange(0, masm_->pc_offset());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000165
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000166 { Comment cmnt(masm_, "[ Allocate locals");
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000167 int locals_count = info->scope()->num_stack_slots();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000168 // Generators allocate locals, if any, in context slots.
169 ASSERT(!info->function()->is_generator() || locals_count == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000170 if (locals_count == 1) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000171 __ push(Immediate(isolate()->factory()->undefined_value()));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000172 } else if (locals_count > 1) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000173 __ mov(eax, Immediate(isolate()->factory()->undefined_value()));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000174 for (int i = 0; i < locals_count; i++) {
175 __ push(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000176 }
177 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000178 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000179
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000180 bool function_in_register = true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000181
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000182 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000183 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000184 if (heap_slots > 0) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000185 Comment cmnt(masm_, "[ Allocate context");
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000186 // Argument to NewContext is the function, which is still in edi.
187 __ push(edi);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000188 if (FLAG_harmony_scoping && info->scope()->is_global_scope()) {
189 __ Push(info->scope()->GetScopeInfo());
190 __ CallRuntime(Runtime::kNewGlobalContext, 2);
191 } else if (heap_slots <= FastNewContextStub::kMaximumSlots) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000192 FastNewContextStub stub(heap_slots);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000193 __ CallStub(&stub);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000194 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000195 __ CallRuntime(Runtime::kNewFunctionContext, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000196 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000197 function_in_register = false;
198 // Context is returned in both eax and esi. It replaces the context
199 // passed to us. It's saved in the stack and kept live in esi.
200 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
201
202 // Copy parameters into context if necessary.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000203 int num_parameters = info->scope()->num_parameters();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000204 for (int i = 0; i < num_parameters; i++) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000205 Variable* var = scope()->parameter(i);
206 if (var->IsContextSlot()) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000207 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
208 (num_parameters - 1 - i) * kPointerSize;
209 // Load parameter from stack.
210 __ mov(eax, Operand(ebp, parameter_offset));
211 // Store it in the context.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000212 int context_offset = Context::SlotOffset(var->index());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000213 __ mov(Operand(esi, context_offset), eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000214 // Update the write barrier. This clobbers eax and ebx.
215 __ RecordWriteContextSlot(esi,
216 context_offset,
217 eax,
218 ebx,
219 kDontSaveFPRegs);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000220 }
221 }
222 }
223
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000224 Variable* arguments = scope()->arguments();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000225 if (arguments != NULL) {
226 // Function uses arguments object.
227 Comment cmnt(masm_, "[ Allocate arguments object");
228 if (function_in_register) {
229 __ push(edi);
230 } else {
231 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
232 }
233 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000234 int num_parameters = info->scope()->num_parameters();
235 int offset = num_parameters * kPointerSize;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000236 __ lea(edx,
237 Operand(ebp, StandardFrameConstants::kCallerSPOffset + offset));
238 __ push(edx);
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000239 __ push(Immediate(Smi::FromInt(num_parameters)));
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000240 // Arguments to ArgumentsAccessStub:
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000241 // function, receiver address, parameter count.
242 // The stub will rewrite receiver and parameter count if the previous
243 // stack frame was an arguments adapter frame.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000244 ArgumentsAccessStub::Type type;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000245 if (!is_classic_mode()) {
whesse@chromium.org7b260152011-06-20 15:33:18 +0000246 type = ArgumentsAccessStub::NEW_STRICT;
247 } else if (function()->has_duplicate_parameters()) {
248 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
249 } else {
250 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
251 }
252 ArgumentsAccessStub stub(type);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000253 __ CallStub(&stub);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000254
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000255 SetVar(arguments, eax, ebx, edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000256 }
257
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000258 if (FLAG_trace) {
259 __ CallRuntime(Runtime::kTraceEnter, 0);
260 }
261
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000262 // Visit the declarations and body unless there is an illegal
263 // redeclaration.
264 if (scope()->HasIllegalRedeclaration()) {
265 Comment cmnt(masm_, "[ Declarations");
266 scope()->VisitIllegalRedeclaration(this);
267
268 } else {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000269 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000270 { Comment cmnt(masm_, "[ Declarations");
271 // For named function expressions, declare the function name as a
272 // constant.
273 if (scope()->is_function_scope() && scope()->function() != NULL) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000274 VariableDeclaration* function = scope()->function();
275 ASSERT(function->proxy()->var()->mode() == CONST ||
276 function->proxy()->var()->mode() == CONST_HARMONY);
277 ASSERT(function->proxy()->var()->location() != Variable::UNALLOCATED);
278 VisitVariableDeclaration(function);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000279 }
280 VisitDeclarations(scope()->declarations());
281 }
282
283 { Comment cmnt(masm_, "[ Stack check");
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000284 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000285 Label ok;
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000286 ExternalReference stack_limit =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000287 ExternalReference::address_of_stack_limit(isolate());
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000288 __ cmp(esp, Operand::StaticVariable(stack_limit));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000289 __ j(above_equal, &ok, Label::kNear);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000290 StackCheckStub stub;
291 __ CallStub(&stub);
292 __ bind(&ok);
293 }
294
295 { Comment cmnt(masm_, "[ Body");
296 ASSERT(loop_depth() == 0);
297 VisitStatements(function()->body());
298 ASSERT(loop_depth() == 0);
299 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000300 }
301
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000302 // Always emit a 'return undefined' in case control fell off the end of
303 // the body.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000304 { Comment cmnt(masm_, "[ return <undefined>;");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000305 __ mov(eax, isolate()->factory()->undefined_value());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000306 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000307 }
308}
309
310
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000311void FullCodeGenerator::ClearAccumulator() {
312 __ Set(eax, Immediate(Smi::FromInt(0)));
313}
314
315
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000316void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
317 __ mov(ebx, Immediate(profiling_counter_));
318 __ sub(FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset),
319 Immediate(Smi::FromInt(delta)));
320}
321
322
323void FullCodeGenerator::EmitProfilingCounterReset() {
324 int reset_value = FLAG_interrupt_budget;
325 if (info_->ShouldSelfOptimize() && !FLAG_retry_self_opt) {
326 // Self-optimization is a one-off thing: if it fails, don't try again.
327 reset_value = Smi::kMaxValue;
328 }
329 __ mov(ebx, Immediate(profiling_counter_));
330 __ mov(FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset),
331 Immediate(Smi::FromInt(reset_value)));
332}
333
334
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000335void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
336 Label* back_edge_target) {
337 Comment cmnt(masm_, "[ Back edge bookkeeping");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000338 Label ok;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000339
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000340 int weight = 1;
341 if (FLAG_weighted_back_edges) {
342 ASSERT(back_edge_target->is_bound());
343 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
344 weight = Min(kMaxBackEdgeWeight,
345 Max(1, distance / kBackEdgeDistanceUnit));
yangguo@chromium.org56454712012-02-16 15:33:53 +0000346 }
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000347 EmitProfilingCounterDecrement(weight);
348 __ j(positive, &ok, Label::kNear);
349 InterruptStub stub;
350 __ CallStub(&stub);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000351
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000352 // Record a mapping of this PC offset to the OSR id. This is used to find
353 // the AST id from the unoptimized code in order to use it as a key into
354 // the deoptimization input data found in the optimized code.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000355 RecordBackEdge(stmt->OsrEntryId());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000356
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000357 EmitProfilingCounterReset();
yangguo@chromium.org56454712012-02-16 15:33:53 +0000358
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000359 __ bind(&ok);
360 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
361 // Record a mapping of the OSR id to this PC. This is used if the OSR
362 // entry becomes the target of a bailout. We don't expect it to be, but
363 // we want it to work if it is.
364 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000365}
366
367
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000368void FullCodeGenerator::EmitReturnSequence() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000369 Comment cmnt(masm_, "[ Return sequence");
370 if (return_label_.is_bound()) {
371 __ jmp(&return_label_);
372 } else {
373 // Common return label
374 __ bind(&return_label_);
375 if (FLAG_trace) {
376 __ push(eax);
377 __ CallRuntime(Runtime::kTraceExit, 1);
378 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000379 if (FLAG_interrupt_at_exit || FLAG_self_optimization) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000380 // Pretend that the exit is a backwards jump to the entry.
381 int weight = 1;
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000382 if (info_->ShouldSelfOptimize()) {
383 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
384 } else if (FLAG_weighted_back_edges) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000385 int distance = masm_->pc_offset();
danno@chromium.org88aa0582012-03-23 15:11:57 +0000386 weight = Min(kMaxBackEdgeWeight,
danno@chromium.org129d3982012-07-25 15:01:47 +0000387 Max(1, distance / kBackEdgeDistanceUnit));
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000388 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000389 EmitProfilingCounterDecrement(weight);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000390 Label ok;
391 __ j(positive, &ok, Label::kNear);
392 __ push(eax);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000393 if (info_->ShouldSelfOptimize() && FLAG_direct_self_opt) {
394 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
395 __ CallRuntime(Runtime::kOptimizeFunctionOnNextCall, 1);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000396 } else {
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000397 InterruptStub stub;
398 __ CallStub(&stub);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000399 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000400 __ pop(eax);
401 EmitProfilingCounterReset();
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000402 __ bind(&ok);
403 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000404#ifdef DEBUG
405 // Add a label for checking the size of the code used for returning.
406 Label check_exit_codesize;
407 masm_->bind(&check_exit_codesize);
408#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000409 SetSourcePosition(function()->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000410 __ RecordJSReturn();
411 // Do not use the leave instruction here because it is too short to
412 // patch with the code required by the debugger.
413 __ mov(esp, ebp);
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000414 int no_frame_start = masm_->pc_offset();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000415 __ pop(ebp);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000416
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000417 int arguments_bytes = (info_->scope()->num_parameters() + 1) * kPointerSize;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000418 __ Ret(arguments_bytes, ecx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000419#ifdef ENABLE_DEBUGGER_SUPPORT
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000420 // Check that the size of the code used for returning is large enough
421 // for the debugger's requirements.
422 ASSERT(Assembler::kJSReturnSequenceLength <=
423 masm_->SizeOfCodeGeneratedSince(&check_exit_codesize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000424#endif
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000425 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000426 }
427}
428
429
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000430void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
431 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000432}
433
434
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000435void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
436 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
437 codegen()->GetVar(result_register(), var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000438}
439
440
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000441void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
442 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
443 MemOperand operand = codegen()->VarOperand(var, result_register());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000444 // Memory operands can be pushed directly.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000445 __ push(operand);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000446}
447
448
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000449void FullCodeGenerator::TestContext::Plug(Variable* var) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000450 // For simplicity we always test the accumulator register.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000451 codegen()->GetVar(result_register(), var);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000452 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000453 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000454}
455
456
457void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
458 UNREACHABLE(); // Not used on IA32.
459}
460
461
462void FullCodeGenerator::AccumulatorValueContext::Plug(
463 Heap::RootListIndex index) const {
464 UNREACHABLE(); // Not used on IA32.
465}
466
467
468void FullCodeGenerator::StackValueContext::Plug(
469 Heap::RootListIndex index) const {
470 UNREACHABLE(); // Not used on IA32.
471}
472
473
474void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
475 UNREACHABLE(); // Not used on IA32.
476}
477
478
479void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
480}
481
482
483void FullCodeGenerator::AccumulatorValueContext::Plug(
484 Handle<Object> lit) const {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000485 if (lit->IsSmi()) {
486 __ SafeSet(result_register(), Immediate(lit));
487 } else {
488 __ Set(result_register(), Immediate(lit));
489 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000490}
491
492
493void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000494 if (lit->IsSmi()) {
495 __ SafePush(Immediate(lit));
496 } else {
497 __ push(Immediate(lit));
498 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000499}
500
501
502void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000503 codegen()->PrepareForBailoutBeforeSplit(condition(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000504 true,
505 true_label_,
506 false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000507 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
508 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000509 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000510 } else if (lit->IsTrue() || lit->IsJSObject()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000511 if (true_label_ != fall_through_) __ jmp(true_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000512 } else if (lit->IsString()) {
513 if (String::cast(*lit)->length() == 0) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000514 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000515 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000516 if (true_label_ != fall_through_) __ jmp(true_label_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000517 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000518 } else if (lit->IsSmi()) {
519 if (Smi::cast(*lit)->value() == 0) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000520 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000521 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000522 if (true_label_ != fall_through_) __ jmp(true_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000523 }
524 } else {
525 // For simplicity we always test the accumulator register.
526 __ mov(result_register(), lit);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000527 codegen()->DoTest(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000528 }
529}
530
531
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000532void FullCodeGenerator::EffectContext::DropAndPlug(int count,
533 Register reg) const {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000534 ASSERT(count > 0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000535 __ Drop(count);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000536}
537
538
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000539void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
540 int count,
541 Register reg) const {
542 ASSERT(count > 0);
543 __ Drop(count);
544 __ Move(result_register(), reg);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000545}
546
547
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000548void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
549 Register reg) const {
550 ASSERT(count > 0);
551 if (count > 1) __ Drop(count - 1);
552 __ mov(Operand(esp, 0), reg);
553}
554
555
556void FullCodeGenerator::TestContext::DropAndPlug(int count,
557 Register reg) const {
558 ASSERT(count > 0);
559 // For simplicity we always test the accumulator register.
560 __ Drop(count);
561 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000562 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000563 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000564}
565
566
567void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
568 Label* materialize_false) const {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000569 ASSERT(materialize_true == materialize_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000570 __ bind(materialize_true);
571}
572
573
574void FullCodeGenerator::AccumulatorValueContext::Plug(
575 Label* materialize_true,
576 Label* materialize_false) const {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000577 Label done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000578 __ bind(materialize_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000579 __ mov(result_register(), isolate()->factory()->true_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000580 __ jmp(&done, Label::kNear);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000581 __ bind(materialize_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000582 __ mov(result_register(), isolate()->factory()->false_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000583 __ bind(&done);
584}
585
586
587void FullCodeGenerator::StackValueContext::Plug(
588 Label* materialize_true,
589 Label* materialize_false) const {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000590 Label done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000591 __ bind(materialize_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000592 __ push(Immediate(isolate()->factory()->true_value()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000593 __ jmp(&done, Label::kNear);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000594 __ bind(materialize_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000595 __ push(Immediate(isolate()->factory()->false_value()));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000596 __ bind(&done);
597}
598
599
600void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
601 Label* materialize_false) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000602 ASSERT(materialize_true == true_label_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000603 ASSERT(materialize_false == false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000604}
605
606
607void FullCodeGenerator::EffectContext::Plug(bool flag) const {
608}
609
610
611void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000612 Handle<Object> value = flag
613 ? isolate()->factory()->true_value()
614 : isolate()->factory()->false_value();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000615 __ mov(result_register(), value);
616}
617
618
619void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000620 Handle<Object> value = flag
621 ? isolate()->factory()->true_value()
622 : isolate()->factory()->false_value();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000623 __ push(Immediate(value));
624}
625
626
627void FullCodeGenerator::TestContext::Plug(bool flag) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000628 codegen()->PrepareForBailoutBeforeSplit(condition(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000629 true,
630 true_label_,
631 false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000632 if (flag) {
633 if (true_label_ != fall_through_) __ jmp(true_label_);
634 } else {
635 if (false_label_ != fall_through_) __ jmp(false_label_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000636 }
637}
638
639
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000640void FullCodeGenerator::DoTest(Expression* condition,
641 Label* if_true,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000642 Label* if_false,
643 Label* fall_through) {
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000644 Handle<Code> ic = ToBooleanStub::GetUninitialized(isolate());
645 CallIC(ic, RelocInfo::CODE_TARGET, condition->test_id());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000646 __ test(result_register(), result_register());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000647 // The stub returns nonzero for true.
648 Split(not_zero, if_true, if_false, fall_through);
649}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000650
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000651
ricow@chromium.org65fae842010-08-25 15:26:24 +0000652void FullCodeGenerator::Split(Condition cc,
653 Label* if_true,
654 Label* if_false,
655 Label* fall_through) {
656 if (if_false == fall_through) {
657 __ j(cc, if_true);
658 } else if (if_true == fall_through) {
659 __ j(NegateCondition(cc), if_false);
660 } else {
661 __ j(cc, if_true);
662 __ jmp(if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000663 }
664}
665
666
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000667MemOperand FullCodeGenerator::StackOperand(Variable* var) {
668 ASSERT(var->IsStackAllocated());
669 // Offset is negative because higher indexes are at lower addresses.
670 int offset = -var->index() * kPointerSize;
671 // Adjust by a (parameter or local) base offset.
672 if (var->IsParameter()) {
673 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
674 } else {
675 offset += JavaScriptFrameConstants::kLocal0Offset;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000676 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000677 return Operand(ebp, offset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000678}
679
680
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000681MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
682 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
683 if (var->IsContextSlot()) {
684 int context_chain_length = scope()->ContextChainLength(var->scope());
685 __ LoadContext(scratch, context_chain_length);
686 return ContextOperand(scratch, var->index());
687 } else {
688 return StackOperand(var);
689 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000690}
691
692
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000693void FullCodeGenerator::GetVar(Register dest, Variable* var) {
694 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
695 MemOperand location = VarOperand(var, dest);
696 __ mov(dest, location);
697}
698
699
700void FullCodeGenerator::SetVar(Variable* var,
701 Register src,
702 Register scratch0,
703 Register scratch1) {
704 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
705 ASSERT(!scratch0.is(src));
706 ASSERT(!scratch0.is(scratch1));
707 ASSERT(!scratch1.is(src));
708 MemOperand location = VarOperand(var, scratch0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000709 __ mov(location, src);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000710
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000711 // Emit the write barrier code if the location is in the heap.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000712 if (var->IsContextSlot()) {
713 int offset = Context::SlotOffset(var->index());
714 ASSERT(!scratch0.is(esi) && !src.is(esi) && !scratch1.is(esi));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000715 __ RecordWriteContextSlot(scratch0, offset, src, scratch1, kDontSaveFPRegs);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000716 }
717}
718
719
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000720void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000721 bool should_normalize,
722 Label* if_true,
723 Label* if_false) {
724 // Only prepare for bailouts before splits if we're in a test
725 // context. Otherwise, we let the Visit function deal with the
726 // preparation to avoid preparing with the same AST id twice.
727 if (!context()->IsTest() || !info_->IsOptimizable()) return;
728
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000729 Label skip;
730 if (should_normalize) __ jmp(&skip, Label::kNear);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000731 PrepareForBailout(expr, TOS_REG);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000732 if (should_normalize) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000733 __ cmp(eax, isolate()->factory()->true_value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000734 Split(equal, if_true, if_false, NULL);
735 __ bind(&skip);
736 }
737}
738
739
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000740void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000741 // The variable in the declaration always resides in the current context.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000742 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000743 if (generate_debug_code_) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000744 // Check that we're not inside a with or catch context.
745 __ mov(ebx, FieldOperand(esi, HeapObject::kMapOffset));
746 __ cmp(ebx, isolate()->factory()->with_context_map());
747 __ Check(not_equal, "Declaration in with context.");
748 __ cmp(ebx, isolate()->factory()->catch_context_map());
749 __ Check(not_equal, "Declaration in catch context.");
750 }
751}
752
753
754void FullCodeGenerator::VisitVariableDeclaration(
755 VariableDeclaration* declaration) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000756 // If it was not possible to allocate the variable at compile time, we
757 // need to "declare" it at runtime to make sure it actually exists in the
758 // local context.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000759 VariableProxy* proxy = declaration->proxy();
760 VariableMode mode = declaration->mode();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000761 Variable* variable = proxy->var();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000762 bool hole_init = mode == CONST || mode == CONST_HARMONY || mode == LET;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000763 switch (variable->location()) {
764 case Variable::UNALLOCATED:
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000765 globals_->Add(variable->name(), zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000766 globals_->Add(variable->binding_needs_init()
767 ? isolate()->factory()->the_hole_value()
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000768 : isolate()->factory()->undefined_value(), zone());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000769 break;
770
771 case Variable::PARAMETER:
772 case Variable::LOCAL:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000773 if (hole_init) {
774 Comment cmnt(masm_, "[ VariableDeclaration");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000775 __ mov(StackOperand(variable),
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000776 Immediate(isolate()->factory()->the_hole_value()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000777 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000778 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000779
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000780 case Variable::CONTEXT:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000781 if (hole_init) {
782 Comment cmnt(masm_, "[ VariableDeclaration");
783 EmitDebugCheckDeclarationContext(variable);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000784 __ mov(ContextOperand(esi, variable->index()),
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000785 Immediate(isolate()->factory()->the_hole_value()));
786 // No write barrier since the hole value is in old space.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000787 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000788 }
789 break;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000790
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000791 case Variable::LOOKUP: {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000792 Comment cmnt(masm_, "[ VariableDeclaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000793 __ push(esi);
794 __ push(Immediate(variable->name()));
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000795 // VariableDeclaration nodes are always introduced in one of four modes.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000796 ASSERT(IsDeclaredVariableMode(mode));
797 PropertyAttributes attr =
798 IsImmutableVariableMode(mode) ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000799 __ push(Immediate(Smi::FromInt(attr)));
800 // Push initial value, if any.
801 // Note: For variables we must not push an initial value (such as
802 // 'undefined') because we may have a (legal) redeclaration and we
803 // must not destroy the current value.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000804 if (hole_init) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000805 __ push(Immediate(isolate()->factory()->the_hole_value()));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000806 } else {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000807 __ push(Immediate(Smi::FromInt(0))); // Indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000808 }
809 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000810 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000811 }
812 }
813}
814
815
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000816void FullCodeGenerator::VisitFunctionDeclaration(
817 FunctionDeclaration* declaration) {
818 VariableProxy* proxy = declaration->proxy();
819 Variable* variable = proxy->var();
820 switch (variable->location()) {
821 case Variable::UNALLOCATED: {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000822 globals_->Add(variable->name(), zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000823 Handle<SharedFunctionInfo> function =
824 Compiler::BuildFunctionInfo(declaration->fun(), script());
825 // Check for stack-overflow exception.
826 if (function.is_null()) return SetStackOverflow();
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000827 globals_->Add(function, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000828 break;
829 }
830
831 case Variable::PARAMETER:
832 case Variable::LOCAL: {
833 Comment cmnt(masm_, "[ FunctionDeclaration");
834 VisitForAccumulatorValue(declaration->fun());
835 __ mov(StackOperand(variable), result_register());
836 break;
837 }
838
839 case Variable::CONTEXT: {
840 Comment cmnt(masm_, "[ FunctionDeclaration");
841 EmitDebugCheckDeclarationContext(variable);
842 VisitForAccumulatorValue(declaration->fun());
843 __ mov(ContextOperand(esi, variable->index()), result_register());
844 // We know that we have written a function, which is not a smi.
845 __ RecordWriteContextSlot(esi,
846 Context::SlotOffset(variable->index()),
847 result_register(),
848 ecx,
849 kDontSaveFPRegs,
850 EMIT_REMEMBERED_SET,
851 OMIT_SMI_CHECK);
852 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
853 break;
854 }
855
856 case Variable::LOOKUP: {
857 Comment cmnt(masm_, "[ FunctionDeclaration");
858 __ push(esi);
859 __ push(Immediate(variable->name()));
860 __ push(Immediate(Smi::FromInt(NONE)));
861 VisitForStackValue(declaration->fun());
862 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
863 break;
864 }
865 }
866}
867
868
869void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* declaration) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000870 Variable* variable = declaration->proxy()->var();
871 ASSERT(variable->location() == Variable::CONTEXT);
872 ASSERT(variable->interface()->IsFrozen());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000873
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000874 Comment cmnt(masm_, "[ ModuleDeclaration");
875 EmitDebugCheckDeclarationContext(variable);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000876
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000877 // Load instance object.
878 __ LoadContext(eax, scope_->ContextChainLength(scope_->GlobalScope()));
879 __ mov(eax, ContextOperand(eax, variable->interface()->Index()));
880 __ mov(eax, ContextOperand(eax, Context::EXTENSION_INDEX));
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000881
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000882 // Assign it.
883 __ mov(ContextOperand(esi, variable->index()), eax);
884 // We know that we have written a module, which is not a smi.
885 __ RecordWriteContextSlot(esi,
886 Context::SlotOffset(variable->index()),
887 eax,
888 ecx,
889 kDontSaveFPRegs,
890 EMIT_REMEMBERED_SET,
891 OMIT_SMI_CHECK);
892 PrepareForBailoutForId(declaration->proxy()->id(), NO_REGISTERS);
893
894 // Traverse into body.
895 Visit(declaration->module());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000896}
897
898
899void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* declaration) {
900 VariableProxy* proxy = declaration->proxy();
901 Variable* variable = proxy->var();
902 switch (variable->location()) {
903 case Variable::UNALLOCATED:
904 // TODO(rossberg)
905 break;
906
907 case Variable::CONTEXT: {
908 Comment cmnt(masm_, "[ ImportDeclaration");
909 EmitDebugCheckDeclarationContext(variable);
910 // TODO(rossberg)
911 break;
912 }
913
914 case Variable::PARAMETER:
915 case Variable::LOCAL:
916 case Variable::LOOKUP:
917 UNREACHABLE();
918 }
919}
920
921
922void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* declaration) {
923 // TODO(rossberg)
924}
925
926
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000927void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
928 // Call the runtime to declare the globals.
929 __ push(esi); // The context is the first argument.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000930 __ Push(pairs);
931 __ Push(Smi::FromInt(DeclareGlobalsFlags()));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000932 __ CallRuntime(Runtime::kDeclareGlobals, 3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000933 // Return value is ignored.
934}
935
936
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000937void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
938 // Call the runtime to declare the modules.
939 __ Push(descriptions);
940 __ CallRuntime(Runtime::kDeclareModules, 1);
941 // Return value is ignored.
942}
943
944
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000945void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
946 Comment cmnt(masm_, "[ SwitchStatement");
947 Breakable nested_statement(this, stmt);
948 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000949
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000950 // Keep the switch value on the stack until a case matches.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000951 VisitForStackValue(stmt->tag());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000952 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000953
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000954 ZoneList<CaseClause*>* clauses = stmt->cases();
955 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000956
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000957 Label next_test; // Recycled for each test.
958 // Compile all the tests with branches to their bodies.
959 for (int i = 0; i < clauses->length(); i++) {
960 CaseClause* clause = clauses->at(i);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000961 clause->body_target()->Unuse();
fschneider@chromium.orgd2187832011-01-26 15:44:20 +0000962
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000963 // The default is not a test, but remember it as final fall through.
964 if (clause->is_default()) {
965 default_clause = clause;
966 continue;
967 }
968
969 Comment cmnt(masm_, "[ Case comparison");
970 __ bind(&next_test);
971 next_test.Unuse();
972
973 // Compile the label expression.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000974 VisitForAccumulatorValue(clause->label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000975
ricow@chromium.org65fae842010-08-25 15:26:24 +0000976 // Perform the comparison as if via '==='.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000977 __ mov(edx, Operand(esp, 0)); // Switch value.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000978 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000979 JumpPatchSite patch_site(masm_);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000980 if (inline_smi_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000981 Label slow_case;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000982 __ mov(ecx, edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000983 __ or_(ecx, eax);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000984 patch_site.EmitJumpIfNotSmi(ecx, &slow_case, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000985
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000986 __ cmp(edx, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000987 __ j(not_equal, &next_test);
988 __ Drop(1); // Switch value is no longer needed.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000989 __ jmp(clause->body_target());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000990 __ bind(&slow_case);
991 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000992
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000993 // Record position before stub call for type feedback.
994 SetSourcePosition(clause->position());
hpayer@chromium.org8432c912013-02-28 15:55:26 +0000995 Handle<Code> ic = CompareIC::GetUninitialized(isolate(), Token::EQ_STRICT);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000996 CallIC(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000997 patch_site.EmitPatchInfo();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000998 __ test(eax, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000999 __ j(not_equal, &next_test);
1000 __ Drop(1); // Switch value is no longer needed.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001001 __ jmp(clause->body_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001002 }
1003
1004 // Discard the test value and jump to the default if present, otherwise to
1005 // the end of the statement.
1006 __ bind(&next_test);
1007 __ Drop(1); // Switch value is no longer needed.
1008 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001009 __ jmp(nested_statement.break_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001010 } else {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001011 __ jmp(default_clause->body_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001012 }
1013
1014 // Compile all the case bodies.
1015 for (int i = 0; i < clauses->length(); i++) {
1016 Comment cmnt(masm_, "[ Case body");
1017 CaseClause* clause = clauses->at(i);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001018 __ bind(clause->body_target());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001019 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001020 VisitStatements(clause->statements());
1021 }
1022
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001023 __ bind(nested_statement.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001024 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001025}
1026
1027
1028void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1029 Comment cmnt(masm_, "[ ForInStatement");
1030 SetStatementPosition(stmt);
1031
1032 Label loop, exit;
1033 ForIn loop_statement(this, stmt);
1034 increment_loop_depth();
1035
1036 // Get the object to enumerate over. Both SpiderMonkey and JSC
1037 // ignore null and undefined in contrast to the specification; see
1038 // ECMA-262 section 12.6.4.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001039 VisitForAccumulatorValue(stmt->enumerable());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001040 __ cmp(eax, isolate()->factory()->undefined_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001041 __ j(equal, &exit);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001042 __ cmp(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001043 __ j(equal, &exit);
1044
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001045 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1046
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001047 // Convert the object to a JS object.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001048 Label convert, done_convert;
whesse@chromium.org7b260152011-06-20 15:33:18 +00001049 __ JumpIfSmi(eax, &convert, Label::kNear);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001050 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001051 __ j(above_equal, &done_convert, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001052 __ bind(&convert);
1053 __ push(eax);
1054 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1055 __ bind(&done_convert);
1056 __ push(eax);
1057
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001058 // Check for proxies.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001059 Label call_runtime, use_cache, fixed_array;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001060 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1061 __ CmpObjectType(eax, LAST_JS_PROXY_TYPE, ecx);
1062 __ j(below_equal, &call_runtime);
1063
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001064 // Check cache validity in generated code. This is a fast case for
1065 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1066 // guarantee cache validity, call the runtime system to check cache
1067 // validity or get the property names in a fixed array.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001068 __ CheckEnumCache(&call_runtime);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001069
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001070 __ mov(eax, FieldOperand(eax, HeapObject::kMapOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001071 __ jmp(&use_cache, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001072
1073 // Get the set of properties to enumerate.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001074 __ bind(&call_runtime);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001075 __ push(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001076 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001077 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
1078 isolate()->factory()->meta_map());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001079 __ j(not_equal, &fixed_array);
1080
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001081
1082 // We got a map in register eax. Get the enumeration cache from it.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001083 Label no_descriptors;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001084 __ bind(&use_cache);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001085
1086 __ EnumLength(edx, eax);
1087 __ cmp(edx, Immediate(Smi::FromInt(0)));
1088 __ j(equal, &no_descriptors);
1089
danno@chromium.org40cb8782011-05-25 07:58:50 +00001090 __ LoadInstanceDescriptors(eax, ecx);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001091 __ mov(ecx, FieldOperand(ecx, DescriptorArray::kEnumCacheOffset));
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001092 __ mov(ecx, FieldOperand(ecx, DescriptorArray::kEnumCacheBridgeCacheOffset));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001093
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001094 // Set up the four remaining stack slots.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001095 __ push(eax); // Map.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001096 __ push(ecx); // Enumeration cache.
1097 __ push(edx); // Number of valid entries for the map in the enum cache.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001098 __ push(Immediate(Smi::FromInt(0))); // Initial index.
1099 __ jmp(&loop);
1100
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001101 __ bind(&no_descriptors);
1102 __ add(esp, Immediate(kPointerSize));
1103 __ jmp(&exit);
1104
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001105 // We got a fixed array in register eax. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001106 Label non_proxy;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001107 __ bind(&fixed_array);
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001108
1109 Handle<JSGlobalPropertyCell> cell =
1110 isolate()->factory()->NewJSGlobalPropertyCell(
1111 Handle<Object>(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001112 Smi::FromInt(TypeFeedbackCells::kForInFastCaseMarker),
1113 isolate()));
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001114 RecordTypeFeedbackCell(stmt->ForInFeedbackId(), cell);
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001115 __ LoadHeapObject(ebx, cell);
1116 __ mov(FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset),
1117 Immediate(Smi::FromInt(TypeFeedbackCells::kForInSlowCaseMarker)));
1118
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001119 __ mov(ebx, Immediate(Smi::FromInt(1))); // Smi indicates slow check
1120 __ mov(ecx, Operand(esp, 0 * kPointerSize)); // Get enumerated object
1121 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1122 __ CmpObjectType(ecx, LAST_JS_PROXY_TYPE, ecx);
1123 __ j(above, &non_proxy);
1124 __ mov(ebx, Immediate(Smi::FromInt(0))); // Zero indicates proxy
1125 __ bind(&non_proxy);
1126 __ push(ebx); // Smi
1127 __ push(eax); // Array
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001128 __ mov(eax, FieldOperand(eax, FixedArray::kLengthOffset));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001129 __ push(eax); // Fixed array length (as smi).
1130 __ push(Immediate(Smi::FromInt(0))); // Initial index.
1131
1132 // Generate code for doing the condition check.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001133 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001134 __ bind(&loop);
1135 __ mov(eax, Operand(esp, 0 * kPointerSize)); // Get the current index.
1136 __ cmp(eax, Operand(esp, 1 * kPointerSize)); // Compare to the array length.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001137 __ j(above_equal, loop_statement.break_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001138
1139 // Get the current entry of the array into register ebx.
1140 __ mov(ebx, Operand(esp, 2 * kPointerSize));
1141 __ mov(ebx, FieldOperand(ebx, eax, times_2, FixedArray::kHeaderSize));
1142
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001143 // Get the expected map from the stack or a smi in the
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001144 // permanent slow case into register edx.
1145 __ mov(edx, Operand(esp, 3 * kPointerSize));
1146
1147 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001148 // If not, we may have to filter the key.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001149 Label update_each;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001150 __ mov(ecx, Operand(esp, 4 * kPointerSize));
1151 __ cmp(edx, FieldOperand(ecx, HeapObject::kMapOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001152 __ j(equal, &update_each, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001153
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001154 // For proxies, no filtering is done.
1155 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1156 ASSERT(Smi::FromInt(0) == 0);
1157 __ test(edx, edx);
1158 __ j(zero, &update_each);
1159
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001160 // Convert the entry to a string or null if it isn't a property
1161 // anymore. If the property has been removed while iterating, we
1162 // just skip it.
1163 __ push(ecx); // Enumerable.
1164 __ push(ebx); // Current entry.
1165 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001166 __ test(eax, eax);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001167 __ j(equal, loop_statement.continue_label());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001168 __ mov(ebx, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001169
1170 // Update the 'each' property or variable from the possibly filtered
1171 // entry in register ebx.
1172 __ bind(&update_each);
1173 __ mov(result_register(), ebx);
1174 // Perform the assignment as if via '='.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001175 { EffectContext context(this);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001176 EmitAssignment(stmt->each());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001177 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001178
1179 // Generate code for the body of the loop.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001180 Visit(stmt->body());
1181
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001182 // Generate code for going to the next element by incrementing the
1183 // index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001184 __ bind(loop_statement.continue_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001185 __ add(Operand(esp, 0 * kPointerSize), Immediate(Smi::FromInt(1)));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001186
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001187 EmitBackEdgeBookkeeping(stmt, &loop);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001188 __ jmp(&loop);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001189
1190 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001191 __ bind(loop_statement.break_label());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001192 __ add(esp, Immediate(5 * kPointerSize));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001193
1194 // Exit and decrement the loop depth.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001195 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001196 __ bind(&exit);
1197 decrement_loop_depth();
1198}
1199
1200
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001201void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1202 bool pretenure) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001203 // Use the fast case closure allocation code that allocates in new
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001204 // space for nested functions that don't need literals cloning. If
1205 // we're running with the --always-opt or the --prepare-always-opt
1206 // flag, we need to use the runtime function so that the new function
1207 // we are creating here gets a chance to have its code optimized and
1208 // doesn't just get a copy of the existing unoptimized code.
1209 if (!FLAG_always_opt &&
1210 !FLAG_prepare_always_opt &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001211 !pretenure &&
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001212 scope()->is_function_scope() &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001213 info->num_literals() == 0) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001214 FastNewClosureStub stub(info->language_mode(), info->is_generator());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001215 __ push(Immediate(info));
1216 __ CallStub(&stub);
1217 } else {
1218 __ push(esi);
1219 __ push(Immediate(info));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001220 __ push(Immediate(pretenure
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001221 ? isolate()->factory()->true_value()
1222 : isolate()->factory()->false_value()));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001223 __ CallRuntime(Runtime::kNewClosure, 3);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001224 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001225 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001226}
1227
1228
1229void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
1230 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001231 EmitVariableLoad(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001232}
1233
1234
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001235void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1236 TypeofState typeof_state,
1237 Label* slow) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001238 Register context = esi;
1239 Register temp = edx;
1240
1241 Scope* s = scope();
1242 while (s != NULL) {
1243 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001244 if (s->calls_non_strict_eval()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001245 // Check that extension is NULL.
1246 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX),
1247 Immediate(0));
1248 __ j(not_equal, slow);
1249 }
1250 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001251 __ mov(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001252 // Walk the rest of the chain without clobbering esi.
1253 context = temp;
1254 }
1255 // If no outer scope calls eval, we do not need to check more
1256 // context extensions. If we have reached an eval scope, we check
1257 // all extensions from this point.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001258 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001259 s = s->outer_scope();
1260 }
1261
1262 if (s != NULL && s->is_eval_scope()) {
1263 // Loop up the context chain. There is no frame effect so it is
1264 // safe to use raw labels here.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001265 Label next, fast;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001266 if (!context.is(temp)) {
1267 __ mov(temp, context);
1268 }
1269 __ bind(&next);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001270 // Terminate at native context.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001271 __ cmp(FieldOperand(temp, HeapObject::kMapOffset),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001272 Immediate(isolate()->factory()->native_context_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001273 __ j(equal, &fast, Label::kNear);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001274 // Check that extension is NULL.
1275 __ cmp(ContextOperand(temp, Context::EXTENSION_INDEX), Immediate(0));
1276 __ j(not_equal, slow);
1277 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001278 __ mov(temp, ContextOperand(temp, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001279 __ jmp(&next);
1280 __ bind(&fast);
1281 }
1282
1283 // All extension objects were empty and it is safe to use a global
1284 // load IC call.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001285 __ mov(edx, GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001286 __ mov(ecx, var->name());
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001287 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001288 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1289 ? RelocInfo::CODE_TARGET
1290 : RelocInfo::CODE_TARGET_CONTEXT;
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001291 CallIC(ic, mode);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001292}
1293
1294
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001295MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1296 Label* slow) {
1297 ASSERT(var->IsContextSlot());
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001298 Register context = esi;
1299 Register temp = ebx;
1300
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001301 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001302 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001303 if (s->calls_non_strict_eval()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001304 // Check that extension is NULL.
1305 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX),
1306 Immediate(0));
1307 __ j(not_equal, slow);
1308 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001309 __ mov(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001310 // Walk the rest of the chain without clobbering esi.
1311 context = temp;
1312 }
1313 }
1314 // Check that last extension is NULL.
1315 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX), Immediate(0));
1316 __ j(not_equal, slow);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001317
1318 // This function is used only for loads, not stores, so it's safe to
1319 // return an esi-based operand (the write barrier cannot be allowed to
1320 // destroy the esi register).
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001321 return ContextOperand(context, var->index());
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001322}
1323
1324
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001325void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1326 TypeofState typeof_state,
1327 Label* slow,
1328 Label* done) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001329 // Generate fast-case code for variables that might be shadowed by
1330 // eval-introduced variables. Eval is used a lot without
1331 // introducing variables. In those cases, we do not want to
1332 // perform a runtime call for all variables in the scope
1333 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001334 if (var->mode() == DYNAMIC_GLOBAL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001335 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001336 __ jmp(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001337 } else if (var->mode() == DYNAMIC_LOCAL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001338 Variable* local = var->local_if_not_shadowed();
1339 __ mov(eax, ContextSlotOperandCheckExtensions(local, slow));
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001340 if (local->mode() == LET ||
1341 local->mode() == CONST ||
1342 local->mode() == CONST_HARMONY) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001343 __ cmp(eax, isolate()->factory()->the_hole_value());
1344 __ j(not_equal, done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001345 if (local->mode() == CONST) {
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001346 __ mov(eax, isolate()->factory()->undefined_value());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001347 } else { // LET || CONST_HARMONY
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001348 __ push(Immediate(var->name()));
1349 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1350 }
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001351 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001352 __ jmp(done);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001353 }
1354}
1355
1356
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001357void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1358 // Record position before possible IC call.
1359 SetSourcePosition(proxy->position());
1360 Variable* var = proxy->var();
1361
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001362 // Three cases: global variables, lookup variables, and all other types of
1363 // variables.
1364 switch (var->location()) {
1365 case Variable::UNALLOCATED: {
1366 Comment cmnt(masm_, "Global variable");
1367 // Use inline caching. Variable name is passed in ecx and the global
1368 // object in eax.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001369 __ mov(edx, GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001370 __ mov(ecx, var->name());
1371 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001372 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001373 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001374 break;
1375 }
1376
1377 case Variable::PARAMETER:
1378 case Variable::LOCAL:
1379 case Variable::CONTEXT: {
1380 Comment cmnt(masm_, var->IsContextSlot()
1381 ? "Context variable"
1382 : "Stack variable");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001383 if (var->binding_needs_init()) {
1384 // var->scope() may be NULL when the proxy is located in eval code and
1385 // refers to a potential outside binding. Currently those bindings are
1386 // always looked up dynamically, i.e. in that case
1387 // var->location() == LOOKUP.
1388 // always holds.
1389 ASSERT(var->scope() != NULL);
1390
1391 // Check if the binding really needs an initialization check. The check
1392 // can be skipped in the following situation: we have a LET or CONST
1393 // binding in harmony mode, both the Variable and the VariableProxy have
1394 // the same declaration scope (i.e. they are both in global code, in the
1395 // same function or in the same eval code) and the VariableProxy is in
1396 // the source physically located after the initializer of the variable.
1397 //
1398 // We cannot skip any initialization checks for CONST in non-harmony
1399 // mode because const variables may be declared but never initialized:
1400 // if (false) { const x; }; var y = x;
1401 //
1402 // The condition on the declaration scopes is a conservative check for
1403 // nested functions that access a binding and are called before the
1404 // binding is initialized:
1405 // function() { f(); let x = 1; function f() { x = 2; } }
1406 //
1407 bool skip_init_check;
1408 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1409 skip_init_check = false;
jkummerow@chromium.orgac45fed2011-11-07 13:11:02 +00001410 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001411 // Check that we always have valid source position.
1412 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1413 ASSERT(proxy->position() != RelocInfo::kNoPosition);
1414 skip_init_check = var->mode() != CONST &&
1415 var->initializer_position() < proxy->position();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001416 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001417
1418 if (!skip_init_check) {
1419 // Let and const need a read barrier.
1420 Label done;
1421 GetVar(eax, var);
1422 __ cmp(eax, isolate()->factory()->the_hole_value());
1423 __ j(not_equal, &done, Label::kNear);
1424 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1425 // Throw a reference error when using an uninitialized let/const
1426 // binding in harmony mode.
1427 __ push(Immediate(var->name()));
1428 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1429 } else {
1430 // Uninitalized const bindings outside of harmony mode are unholed.
1431 ASSERT(var->mode() == CONST);
1432 __ mov(eax, isolate()->factory()->undefined_value());
1433 }
1434 __ bind(&done);
1435 context()->Plug(eax);
1436 break;
1437 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001438 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001439 context()->Plug(var);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001440 break;
1441 }
1442
1443 case Variable::LOOKUP: {
1444 Label done, slow;
1445 // Generate code for loading from variables potentially shadowed
1446 // by eval-introduced variables.
1447 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1448 __ bind(&slow);
1449 Comment cmnt(masm_, "Lookup variable");
1450 __ push(esi); // Context.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001451 __ push(Immediate(var->name()));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001452 __ CallRuntime(Runtime::kLoadContextSlot, 2);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001453 __ bind(&done);
1454 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001455 break;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001456 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001457 }
1458}
1459
1460
1461void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1462 Comment cmnt(masm_, "[ RegExpLiteral");
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001463 Label materialized;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001464 // Registers will be used as follows:
1465 // edi = JS function.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001466 // ecx = literals array.
1467 // ebx = regexp literal.
1468 // eax = regexp literal clone.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001469 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001470 __ mov(ecx, FieldOperand(edi, JSFunction::kLiteralsOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001471 int literal_offset =
ricow@chromium.org65fae842010-08-25 15:26:24 +00001472 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001473 __ mov(ebx, FieldOperand(ecx, literal_offset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001474 __ cmp(ebx, isolate()->factory()->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001475 __ j(not_equal, &materialized, Label::kNear);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001476
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001477 // Create regexp literal using runtime function
1478 // Result will be in eax.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001479 __ push(ecx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001480 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1481 __ push(Immediate(expr->pattern()));
1482 __ push(Immediate(expr->flags()));
1483 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001484 __ mov(ebx, eax);
1485
1486 __ bind(&materialized);
1487 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1488 Label allocated, runtime_allocate;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001489 __ Allocate(size, eax, ecx, edx, &runtime_allocate, TAG_OBJECT);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001490 __ jmp(&allocated);
1491
1492 __ bind(&runtime_allocate);
1493 __ push(ebx);
1494 __ push(Immediate(Smi::FromInt(size)));
1495 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1496 __ pop(ebx);
1497
1498 __ bind(&allocated);
1499 // Copy the content into the newly allocated memory.
1500 // (Unroll copy loop once for better throughput).
1501 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
1502 __ mov(edx, FieldOperand(ebx, i));
1503 __ mov(ecx, FieldOperand(ebx, i + kPointerSize));
1504 __ mov(FieldOperand(eax, i), edx);
1505 __ mov(FieldOperand(eax, i + kPointerSize), ecx);
1506 }
1507 if ((size % (2 * kPointerSize)) != 0) {
1508 __ mov(edx, FieldOperand(ebx, size - kPointerSize));
1509 __ mov(FieldOperand(eax, size - kPointerSize), edx);
1510 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001511 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001512}
1513
1514
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001515void FullCodeGenerator::EmitAccessor(Expression* expression) {
1516 if (expression == NULL) {
1517 __ push(Immediate(isolate()->factory()->null_value()));
1518 } else {
1519 VisitForStackValue(expression);
1520 }
1521}
1522
1523
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001524void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1525 Comment cmnt(masm_, "[ ObjectLiteral");
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001526 Handle<FixedArray> constant_properties = expr->constant_properties();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001527 int flags = expr->fast_elements()
1528 ? ObjectLiteral::kFastElements
1529 : ObjectLiteral::kNoFlags;
1530 flags |= expr->has_function()
1531 ? ObjectLiteral::kHasFunction
1532 : ObjectLiteral::kNoFlags;
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001533 int properties_count = constant_properties->length() / 2;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001534 if ((FLAG_track_double_fields && expr->may_store_doubles()) ||
1535 expr->depth() > 1) {
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001536 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1537 __ push(FieldOperand(edi, JSFunction::kLiteralsOffset));
1538 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1539 __ push(Immediate(constant_properties));
1540 __ push(Immediate(Smi::FromInt(flags)));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001541 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001542 } else if (Serializer::enabled() || flags != ObjectLiteral::kFastElements ||
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001543 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001544 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1545 __ push(FieldOperand(edi, JSFunction::kLiteralsOffset));
1546 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1547 __ push(Immediate(constant_properties));
1548 __ push(Immediate(Smi::FromInt(flags)));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001549 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001550 } else {
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001551 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1552 __ mov(eax, FieldOperand(edi, JSFunction::kLiteralsOffset));
1553 __ mov(ebx, Immediate(Smi::FromInt(expr->literal_index())));
1554 __ mov(ecx, Immediate(constant_properties));
1555 __ mov(edx, Immediate(Smi::FromInt(flags)));
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001556 FastCloneShallowObjectStub stub(properties_count);
1557 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001558 }
1559
1560 // If result_saved is true the result is on top of the stack. If
1561 // result_saved is false the result is in eax.
1562 bool result_saved = false;
1563
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001564 // Mark all computed expressions that are bound to a key that
1565 // is shadowed by a later occurrence of the same key. For the
1566 // marked expressions, no store code is emitted.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001567 expr->CalculateEmitStore(zone());
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001568
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001569 AccessorTable accessor_table(zone());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001570 for (int i = 0; i < expr->properties()->length(); i++) {
1571 ObjectLiteral::Property* property = expr->properties()->at(i);
1572 if (property->IsCompileTimeValue()) continue;
1573
1574 Literal* key = property->key();
1575 Expression* value = property->value();
1576 if (!result_saved) {
1577 __ push(eax); // Save result on the stack
1578 result_saved = true;
1579 }
1580 switch (property->kind()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001581 case ObjectLiteral::Property::CONSTANT:
1582 UNREACHABLE();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001583 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1584 ASSERT(!CompileTimeValue::IsCompileTimeValue(value));
1585 // Fall through.
1586 case ObjectLiteral::Property::COMPUTED:
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001587 if (key->handle()->IsInternalizedString()) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001588 if (property->emit_store()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001589 VisitForAccumulatorValue(value);
1590 __ mov(ecx, Immediate(key->handle()));
1591 __ mov(edx, Operand(esp, 0));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001592 Handle<Code> ic = is_classic_mode()
1593 ? isolate()->builtins()->StoreIC_Initialize()
1594 : isolate()->builtins()->StoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001595 CallIC(ic, RelocInfo::CODE_TARGET, key->LiteralFeedbackId());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001596 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1597 } else {
1598 VisitForEffect(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001599 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001600 break;
1601 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001602 __ push(Operand(esp, 0)); // Duplicate receiver.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001603 VisitForStackValue(key);
1604 VisitForStackValue(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001605 if (property->emit_store()) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001606 __ push(Immediate(Smi::FromInt(NONE))); // PropertyAttributes
1607 __ CallRuntime(Runtime::kSetProperty, 4);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001608 } else {
1609 __ Drop(3);
1610 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001611 break;
ulan@chromium.org750145a2013-03-07 15:14:13 +00001612 case ObjectLiteral::Property::PROTOTYPE:
1613 __ push(Operand(esp, 0)); // Duplicate receiver.
1614 VisitForStackValue(value);
1615 if (property->emit_store()) {
1616 __ CallRuntime(Runtime::kSetPrototype, 2);
1617 } else {
1618 __ Drop(2);
1619 }
1620 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001621 case ObjectLiteral::Property::GETTER:
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001622 accessor_table.lookup(key)->second->getter = value;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001623 break;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001624 case ObjectLiteral::Property::SETTER:
1625 accessor_table.lookup(key)->second->setter = value;
1626 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001627 }
1628 }
1629
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001630 // Emit code to define accessors, using only a single call to the runtime for
1631 // each pair of corresponding getters and setters.
1632 for (AccessorTable::Iterator it = accessor_table.begin();
1633 it != accessor_table.end();
1634 ++it) {
1635 __ push(Operand(esp, 0)); // Duplicate receiver.
1636 VisitForStackValue(it->first);
1637 EmitAccessor(it->second->getter);
1638 EmitAccessor(it->second->setter);
1639 __ push(Immediate(Smi::FromInt(NONE)));
1640 __ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
1641 }
1642
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001643 if (expr->has_function()) {
1644 ASSERT(result_saved);
1645 __ push(Operand(esp, 0));
1646 __ CallRuntime(Runtime::kToFastProperties, 1);
1647 }
1648
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001649 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001650 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001651 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001652 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001653 }
1654}
1655
1656
1657void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1658 Comment cmnt(masm_, "[ ArrayLiteral");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001659
1660 ZoneList<Expression*>* subexprs = expr->values();
1661 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001662 Handle<FixedArray> constant_elements = expr->constant_elements();
1663 ASSERT_EQ(2, constant_elements->length());
1664 ElementsKind constant_elements_kind =
1665 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001666 bool has_constant_fast_elements =
1667 IsFastObjectElementsKind(constant_elements_kind);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001668 Handle<FixedArrayBase> constant_elements_values(
1669 FixedArrayBase::cast(constant_elements->get(1)));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001670
erikcorry0ad885c2011-11-21 13:51:57 +00001671 Heap* heap = isolate()->heap();
1672 if (has_constant_fast_elements &&
1673 constant_elements_values->map() == heap->fixed_cow_array_map()) {
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001674 // If the elements are already FAST_*_ELEMENTS, the boilerplate cannot
erikcorry0ad885c2011-11-21 13:51:57 +00001675 // change, so it's possible to specialize the stub in advance.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001676 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(), 1);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001677 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1678 __ mov(eax, FieldOperand(ebx, JSFunction::kLiteralsOffset));
1679 __ mov(ebx, Immediate(Smi::FromInt(expr->literal_index())));
1680 __ mov(ecx, Immediate(constant_elements));
erikcorry0ad885c2011-11-21 13:51:57 +00001681 FastCloneShallowArrayStub stub(
1682 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001683 DONT_TRACK_ALLOCATION_SITE,
erikcorry0ad885c2011-11-21 13:51:57 +00001684 length);
1685 __ CallStub(&stub);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001686 } else if (expr->depth() > 1) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001687 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1688 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
1689 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1690 __ push(Immediate(constant_elements));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001691 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001692 } else if (Serializer::enabled() ||
1693 length > FastCloneShallowArrayStub::kMaximumClonedLength) {
1694 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1695 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
1696 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1697 __ push(Immediate(constant_elements));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001698 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001699 } else {
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001700 ASSERT(IsFastSmiOrObjectElementsKind(constant_elements_kind) ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001701 FLAG_smi_only_arrays);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001702 FastCloneShallowArrayStub::Mode mode =
1703 FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS;
1704 AllocationSiteMode allocation_site_mode = FLAG_track_allocation_sites
1705 ? TRACK_ALLOCATION_SITE : DONT_TRACK_ALLOCATION_SITE;
1706
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001707 // If the elements are already FAST_*_ELEMENTS, the boilerplate cannot
erikcorry0ad885c2011-11-21 13:51:57 +00001708 // change, so it's possible to specialize the stub in advance.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001709 if (has_constant_fast_elements) {
1710 mode = FastCloneShallowArrayStub::CLONE_ELEMENTS;
1711 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001712 }
1713
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001714 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1715 __ mov(eax, FieldOperand(ebx, JSFunction::kLiteralsOffset));
1716 __ mov(ebx, Immediate(Smi::FromInt(expr->literal_index())));
1717 __ mov(ecx, Immediate(constant_elements));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001718 FastCloneShallowArrayStub stub(mode, allocation_site_mode, length);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001719 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001720 }
1721
1722 bool result_saved = false; // Is the result saved to the stack?
1723
1724 // Emit code to evaluate all the non-constant subexpressions and to store
1725 // them into the newly cloned array.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001726 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001727 Expression* subexpr = subexprs->at(i);
1728 // If the subexpression is a literal or a simple materialized literal it
1729 // is already set in the cloned array.
1730 if (subexpr->AsLiteral() != NULL ||
1731 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1732 continue;
1733 }
1734
1735 if (!result_saved) {
1736 __ push(eax);
1737 result_saved = true;
1738 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001739 VisitForAccumulatorValue(subexpr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001740
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001741 if (IsFastObjectElementsKind(constant_elements_kind)) {
1742 // Fast-case array literal with ElementsKind of FAST_*_ELEMENTS, they
1743 // cannot transition and don't need to call the runtime stub.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001744 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1745 __ mov(ebx, Operand(esp, 0)); // Copy of array literal.
1746 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
1747 // Store the subexpression value in the array's elements.
1748 __ mov(FieldOperand(ebx, offset), result_register());
1749 // Update the write barrier for the array store.
1750 __ RecordWriteField(ebx, offset, result_register(), ecx,
1751 kDontSaveFPRegs,
1752 EMIT_REMEMBERED_SET,
1753 INLINE_SMI_CHECK);
1754 } else {
1755 // Store the subexpression value in the array's elements.
1756 __ mov(ebx, Operand(esp, 0)); // Copy of array literal.
1757 __ mov(edi, FieldOperand(ebx, JSObject::kMapOffset));
1758 __ mov(ecx, Immediate(Smi::FromInt(i)));
1759 __ mov(edx, Immediate(Smi::FromInt(expr->literal_index())));
1760 StoreArrayLiteralElementStub stub;
1761 __ CallStub(&stub);
1762 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001763
1764 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001765 }
1766
1767 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001768 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001769 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001770 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001771 }
1772}
1773
1774
ager@chromium.org5c838252010-02-19 08:53:10 +00001775void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1776 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001777 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1778 // on the left-hand side.
1779 if (!expr->target()->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001780 VisitForEffect(expr->target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001781 return;
1782 }
1783
ager@chromium.org5c838252010-02-19 08:53:10 +00001784 // Left-hand side can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00001785 // slot.
ager@chromium.org5c838252010-02-19 08:53:10 +00001786 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1787 LhsKind assign_type = VARIABLE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001788 Property* property = expr->target()->AsProperty();
1789 if (property != NULL) {
1790 assign_type = (property->key()->IsPropertyName())
1791 ? NAMED_PROPERTY
1792 : KEYED_PROPERTY;
ager@chromium.org5c838252010-02-19 08:53:10 +00001793 }
1794
1795 // Evaluate LHS expression.
1796 switch (assign_type) {
1797 case VARIABLE:
1798 // Nothing to do here.
1799 break;
1800 case NAMED_PROPERTY:
1801 if (expr->is_compound()) {
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001802 // We need the receiver both on the stack and in edx.
1803 VisitForStackValue(property->obj());
1804 __ mov(edx, Operand(esp, 0));
ager@chromium.org5c838252010-02-19 08:53:10 +00001805 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001806 VisitForStackValue(property->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00001807 }
1808 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001809 case KEYED_PROPERTY: {
ager@chromium.org5c838252010-02-19 08:53:10 +00001810 if (expr->is_compound()) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00001811 VisitForStackValue(property->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001812 VisitForStackValue(property->key());
1813 __ mov(edx, Operand(esp, kPointerSize)); // Object.
1814 __ mov(ecx, Operand(esp, 0)); // Key.
ager@chromium.org5c838252010-02-19 08:53:10 +00001815 } else {
whesse@chromium.org7b260152011-06-20 15:33:18 +00001816 VisitForStackValue(property->obj());
1817 VisitForStackValue(property->key());
ager@chromium.org5c838252010-02-19 08:53:10 +00001818 }
1819 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001820 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001821 }
1822
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001823 // For compound assignments we need another deoptimization point after the
1824 // variable/property load.
ager@chromium.org5c838252010-02-19 08:53:10 +00001825 if (expr->is_compound()) {
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001826 AccumulatorValueContext result_context(this);
1827 { AccumulatorValueContext left_operand_context(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001828 switch (assign_type) {
1829 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001830 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001831 PrepareForBailout(expr->target(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001832 break;
1833 case NAMED_PROPERTY:
1834 EmitNamedPropertyLoad(property);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001835 PrepareForBailoutForId(property->LoadId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001836 break;
1837 case KEYED_PROPERTY:
1838 EmitKeyedPropertyLoad(property);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001839 PrepareForBailoutForId(property->LoadId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001840 break;
1841 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001842 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001843
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001844 Token::Value op = expr->binary_op();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001845 __ push(eax); // Left operand goes on the stack.
1846 VisitForAccumulatorValue(expr->value());
ager@chromium.org5c838252010-02-19 08:53:10 +00001847
ricow@chromium.org65fae842010-08-25 15:26:24 +00001848 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1849 ? OVERWRITE_RIGHT
1850 : NO_OVERWRITE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001851 SetSourcePosition(expr->position() + 1);
1852 if (ShouldInlineSmiCase(op)) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001853 EmitInlineSmiBinaryOp(expr->binary_operation(),
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001854 op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001855 mode,
1856 expr->target(),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001857 expr->value());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001858 } else {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001859 EmitBinaryOp(expr->binary_operation(), op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001860 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001861
1862 // Deoptimization point in case the binary operation may have side effects.
1863 PrepareForBailout(expr->binary_operation(), TOS_REG);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001864 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001865 VisitForAccumulatorValue(expr->value());
ager@chromium.org5c838252010-02-19 08:53:10 +00001866 }
1867
1868 // Record source position before possible IC call.
1869 SetSourcePosition(expr->position());
1870
1871 // Store the value.
1872 switch (assign_type) {
1873 case VARIABLE:
1874 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001875 expr->op());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001876 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1877 context()->Plug(eax);
ager@chromium.org5c838252010-02-19 08:53:10 +00001878 break;
1879 case NAMED_PROPERTY:
1880 EmitNamedPropertyAssignment(expr);
1881 break;
1882 case KEYED_PROPERTY:
1883 EmitKeyedPropertyAssignment(expr);
1884 break;
1885 }
1886}
1887
1888
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001889void FullCodeGenerator::VisitYield(Yield* expr) {
1890 Comment cmnt(masm_, "[ Yield");
1891 // Evaluate yielded value first; the initial iterator definition depends on
1892 // this. It stays on the stack while we update the iterator.
1893 VisitForStackValue(expr->expression());
1894
1895 switch (expr->yield_kind()) {
1896 case Yield::INITIAL:
1897 case Yield::SUSPEND: {
1898 VisitForStackValue(expr->generator_object());
1899 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
1900 __ mov(context_register(),
1901 Operand(ebp, StandardFrameConstants::kContextOffset));
1902
1903 Label resume;
1904 __ CompareRoot(result_register(), Heap::kTheHoleValueRootIndex);
1905 __ j(not_equal, &resume);
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001906 if (expr->yield_kind() == Yield::SUSPEND) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001907 EmitReturnIteratorResult(false);
1908 } else {
1909 __ pop(result_register());
1910 EmitReturnSequence();
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001911 }
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001912
1913 __ bind(&resume);
1914 context()->Plug(result_register());
1915 break;
1916 }
1917
1918 case Yield::FINAL: {
1919 VisitForAccumulatorValue(expr->generator_object());
1920 __ mov(FieldOperand(result_register(),
1921 JSGeneratorObject::kContinuationOffset),
1922 Immediate(Smi::FromInt(JSGeneratorObject::kGeneratorClosed)));
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001923 EmitReturnIteratorResult(true);
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001924 break;
1925 }
1926
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001927 case Yield::DELEGATING: {
1928 VisitForStackValue(expr->generator_object());
1929
1930 // Initial stack layout is as follows:
1931 // [sp + 1 * kPointerSize] iter
1932 // [sp + 0 * kPointerSize] g
1933
1934 Label l_catch, l_try, l_resume, l_send, l_call, l_loop;
1935 // Initial send value is undefined.
1936 __ mov(eax, isolate()->factory()->undefined_value());
1937 __ jmp(&l_send);
1938
1939 // catch (e) { receiver = iter; f = iter.throw; arg = e; goto l_call; }
1940 __ bind(&l_catch);
1941 handler_table()->set(expr->index(), Smi::FromInt(l_catch.pos()));
1942 __ mov(edx, Operand(esp, 1 * kPointerSize)); // iter
1943 __ push(edx); // iter
1944 __ push(eax); // exception
1945 __ mov(ecx, isolate()->factory()->throw_string()); // "throw"
1946 Handle<Code> throw_ic = isolate()->builtins()->LoadIC_Initialize();
1947 CallIC(throw_ic); // iter.throw in eax
1948 __ jmp(&l_call);
1949
1950 // try { received = yield result.value }
1951 __ bind(&l_try);
1952 __ pop(eax); // result.value
1953 __ PushTryHandler(StackHandler::CATCH, expr->index());
1954 const int handler_size = StackHandlerConstants::kSize;
1955 __ push(eax); // result.value
1956 __ push(Operand(esp, (0 + 1) * kPointerSize + handler_size)); // g
1957 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
1958 __ mov(context_register(),
1959 Operand(ebp, StandardFrameConstants::kContextOffset));
1960 __ CompareRoot(eax, Heap::kTheHoleValueRootIndex);
1961 __ j(not_equal, &l_resume);
1962 EmitReturnIteratorResult(false);
1963 __ bind(&l_resume); // received in eax
1964 __ PopTryHandler();
1965
1966 // receiver = iter; f = iter.send; arg = received;
1967 __ bind(&l_send);
1968 __ mov(edx, Operand(esp, 1 * kPointerSize)); // iter
1969 __ push(edx); // iter
1970 __ push(eax); // received
1971 __ mov(ecx, isolate()->factory()->send_string()); // "send"
1972 Handle<Code> send_ic = isolate()->builtins()->LoadIC_Initialize();
1973 CallIC(send_ic); // iter.send in eax
1974
1975 // result = f.call(receiver, arg);
1976 __ bind(&l_call);
1977 Label l_call_runtime;
1978 __ JumpIfSmi(eax, &l_call_runtime);
1979 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
1980 __ j(not_equal, &l_call_runtime);
1981 __ mov(edi, eax);
1982 ParameterCount count(1);
1983 __ InvokeFunction(edi, count, CALL_FUNCTION,
1984 NullCallWrapper(), CALL_AS_METHOD);
1985 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
1986 __ jmp(&l_loop);
1987 __ bind(&l_call_runtime);
1988 __ push(eax);
1989 __ CallRuntime(Runtime::kCall, 3);
1990
1991 // val = result.value; if (!result.done) goto l_try;
1992 __ bind(&l_loop);
1993 // result.value
1994 __ push(eax); // save result
1995 __ mov(edx, eax); // result
1996 __ mov(ecx, isolate()->factory()->value_string()); // "value"
1997 Handle<Code> value_ic = isolate()->builtins()->LoadIC_Initialize();
1998 CallIC(value_ic); // result.value in eax
1999 __ pop(ebx); // result
2000 __ push(eax); // result.value
2001 __ mov(edx, ebx); // result
2002 __ mov(ecx, isolate()->factory()->done_string()); // "done"
2003 Handle<Code> done_ic = isolate()->builtins()->LoadIC_Initialize();
2004 CallIC(done_ic); // result.done in eax
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00002005 Handle<Code> bool_ic = ToBooleanStub::GetUninitialized(isolate());
2006 CallIC(bool_ic);
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002007 __ test(eax, eax);
2008 __ j(zero, &l_try);
2009
2010 // result.value
2011 __ pop(eax); // result.value
2012 context()->DropAndPlug(2, eax); // drop iter and g
2013 break;
2014 }
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00002015 }
2016}
2017
2018
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002019void FullCodeGenerator::EmitGeneratorResume(Expression *generator,
2020 Expression *value,
2021 JSGeneratorObject::ResumeMode resume_mode) {
2022 // The value stays in eax, and is ultimately read by the resumed generator, as
2023 // if the CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. ebx
2024 // will hold the generator object until the activation has been resumed.
2025 VisitForStackValue(generator);
2026 VisitForAccumulatorValue(value);
2027 __ pop(ebx);
2028
2029 // Check generator state.
2030 Label wrong_state, done;
2031 STATIC_ASSERT(JSGeneratorObject::kGeneratorExecuting <= 0);
2032 STATIC_ASSERT(JSGeneratorObject::kGeneratorClosed <= 0);
2033 __ cmp(FieldOperand(ebx, JSGeneratorObject::kContinuationOffset),
2034 Immediate(Smi::FromInt(0)));
2035 __ j(less_equal, &wrong_state);
2036
2037 // Load suspended function and context.
2038 __ mov(esi, FieldOperand(ebx, JSGeneratorObject::kContextOffset));
2039 __ mov(edi, FieldOperand(ebx, JSGeneratorObject::kFunctionOffset));
2040
2041 // Push receiver.
2042 __ push(FieldOperand(ebx, JSGeneratorObject::kReceiverOffset));
2043
2044 // Push holes for arguments to generator function.
2045 __ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2046 __ mov(edx,
2047 FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
2048 __ mov(ecx, isolate()->factory()->the_hole_value());
2049 Label push_argument_holes, push_frame;
2050 __ bind(&push_argument_holes);
2051 __ sub(edx, Immediate(1));
2052 __ j(carry, &push_frame);
2053 __ push(ecx);
2054 __ jmp(&push_argument_holes);
2055
2056 // Enter a new JavaScript frame, and initialize its slots as they were when
2057 // the generator was suspended.
2058 Label resume_frame;
2059 __ bind(&push_frame);
2060 __ call(&resume_frame);
2061 __ jmp(&done);
2062 __ bind(&resume_frame);
2063 __ push(ebp); // Caller's frame pointer.
2064 __ mov(ebp, esp);
2065 __ push(esi); // Callee's context.
2066 __ push(edi); // Callee's JS Function.
2067
2068 // Load the operand stack size.
2069 __ mov(edx, FieldOperand(ebx, JSGeneratorObject::kOperandStackOffset));
2070 __ mov(edx, FieldOperand(edx, FixedArray::kLengthOffset));
2071 __ SmiUntag(edx);
2072
2073 // If we are sending a value and there is no operand stack, we can jump back
2074 // in directly.
2075 if (resume_mode == JSGeneratorObject::SEND) {
2076 Label slow_resume;
2077 __ cmp(edx, Immediate(0));
2078 __ j(not_zero, &slow_resume);
2079 __ mov(edx, FieldOperand(edi, JSFunction::kCodeEntryOffset));
2080 __ mov(ecx, FieldOperand(ebx, JSGeneratorObject::kContinuationOffset));
2081 __ SmiUntag(ecx);
2082 __ add(edx, ecx);
2083 __ mov(FieldOperand(ebx, JSGeneratorObject::kContinuationOffset),
2084 Immediate(Smi::FromInt(JSGeneratorObject::kGeneratorExecuting)));
2085 __ jmp(edx);
2086 __ bind(&slow_resume);
2087 }
2088
2089 // Otherwise, we push holes for the operand stack and call the runtime to fix
2090 // up the stack and the handlers.
2091 Label push_operand_holes, call_resume;
2092 __ bind(&push_operand_holes);
2093 __ sub(edx, Immediate(1));
2094 __ j(carry, &call_resume);
2095 __ push(ecx);
2096 __ jmp(&push_operand_holes);
2097 __ bind(&call_resume);
2098 __ push(ebx);
2099 __ push(result_register());
2100 __ Push(Smi::FromInt(resume_mode));
2101 __ CallRuntime(Runtime::kResumeJSGeneratorObject, 3);
2102 // Not reached: the runtime call returns elsewhere.
2103 __ Abort("Generator failed to resume.");
2104
2105 // Throw error if we attempt to operate on a running generator.
2106 __ bind(&wrong_state);
2107 __ push(ebx);
2108 __ CallRuntime(Runtime::kThrowGeneratorStateError, 1);
2109
2110 __ bind(&done);
2111 context()->Plug(result_register());
2112}
2113
2114
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002115void FullCodeGenerator::EmitReturnIteratorResult(bool done) {
2116 Label gc_required;
2117 Label allocated;
2118
2119 Handle<Map> map(isolate()->native_context()->generator_result_map());
2120
2121 __ Allocate(map->instance_size(), eax, ecx, edx, &gc_required, TAG_OBJECT);
2122
2123 __ bind(&allocated);
2124 __ mov(ebx, map);
2125 __ pop(ecx);
2126 __ mov(edx, isolate()->factory()->ToBoolean(done));
2127 ASSERT_EQ(map->instance_size(), 5 * kPointerSize);
2128 __ mov(FieldOperand(eax, HeapObject::kMapOffset), ebx);
2129 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
2130 isolate()->factory()->empty_fixed_array());
2131 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
2132 isolate()->factory()->empty_fixed_array());
2133 __ mov(FieldOperand(eax, JSGeneratorObject::kResultValuePropertyOffset), ecx);
2134 __ mov(FieldOperand(eax, JSGeneratorObject::kResultDonePropertyOffset), edx);
2135
2136 // Only the value field needs a write barrier, as the other values are in the
2137 // root set.
2138 __ RecordWriteField(eax, JSGeneratorObject::kResultValuePropertyOffset,
2139 ecx, edx, kDontSaveFPRegs);
2140
2141 if (done) {
2142 // Exit all nested statements.
2143 NestedStatement* current = nesting_stack_;
2144 int stack_depth = 0;
2145 int context_length = 0;
2146 while (current != NULL) {
2147 current = current->Exit(&stack_depth, &context_length);
2148 }
2149 __ Drop(stack_depth);
2150 }
2151
2152 EmitReturnSequence();
2153
2154 __ bind(&gc_required);
2155 __ Push(Smi::FromInt(map->instance_size()));
2156 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
2157 __ mov(context_register(),
2158 Operand(ebp, StandardFrameConstants::kContextOffset));
2159 __ jmp(&allocated);
2160}
2161
2162
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002163void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
2164 SetSourcePosition(prop->position());
2165 Literal* key = prop->key()->AsLiteral();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002166 ASSERT(!key->handle()->IsSmi());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002167 __ mov(ecx, Immediate(key->handle()));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002168 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002169 CallIC(ic, RelocInfo::CODE_TARGET, prop->PropertyFeedbackId());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002170}
2171
2172
2173void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
2174 SetSourcePosition(prop->position());
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002175 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002176 CallIC(ic, RelocInfo::CODE_TARGET, prop->PropertyFeedbackId());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002177}
2178
2179
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002180void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002181 Token::Value op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002182 OverwriteMode mode,
2183 Expression* left,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002184 Expression* right) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002185 // Do combined smi check of the operands. Left operand is on the
2186 // stack. Right operand is in eax.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002187 Label smi_case, done, stub_call;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002188 __ pop(edx);
2189 __ mov(ecx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002190 __ or_(eax, edx);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002191 JumpPatchSite patch_site(masm_);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002192 patch_site.EmitJumpIfSmi(eax, &smi_case, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002193
2194 __ bind(&stub_call);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002195 __ mov(eax, ecx);
danno@chromium.org40cb8782011-05-25 07:58:50 +00002196 BinaryOpStub stub(op, mode);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002197 CallIC(stub.GetCode(isolate()), RelocInfo::CODE_TARGET,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002198 expr->BinaryOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002199 patch_site.EmitPatchInfo();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002200 __ jmp(&done, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002201
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002202 // Smi case.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002203 __ bind(&smi_case);
2204 __ mov(eax, edx); // Copy left operand in case of a stub call.
2205
2206 switch (op) {
2207 case Token::SAR:
2208 __ SmiUntag(eax);
2209 __ SmiUntag(ecx);
2210 __ sar_cl(eax); // No checks of result necessary
2211 __ SmiTag(eax);
2212 break;
2213 case Token::SHL: {
2214 Label result_ok;
2215 __ SmiUntag(eax);
2216 __ SmiUntag(ecx);
2217 __ shl_cl(eax);
2218 // Check that the *signed* result fits in a smi.
2219 __ cmp(eax, 0xc0000000);
2220 __ j(positive, &result_ok);
2221 __ SmiTag(ecx);
2222 __ jmp(&stub_call);
2223 __ bind(&result_ok);
2224 __ SmiTag(eax);
2225 break;
2226 }
2227 case Token::SHR: {
2228 Label result_ok;
2229 __ SmiUntag(eax);
2230 __ SmiUntag(ecx);
2231 __ shr_cl(eax);
2232 __ test(eax, Immediate(0xc0000000));
2233 __ j(zero, &result_ok);
2234 __ SmiTag(ecx);
2235 __ jmp(&stub_call);
2236 __ bind(&result_ok);
2237 __ SmiTag(eax);
2238 break;
2239 }
2240 case Token::ADD:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002241 __ add(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002242 __ j(overflow, &stub_call);
2243 break;
2244 case Token::SUB:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002245 __ sub(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002246 __ j(overflow, &stub_call);
2247 break;
2248 case Token::MUL: {
2249 __ SmiUntag(eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002250 __ imul(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002251 __ j(overflow, &stub_call);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002252 __ test(eax, eax);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002253 __ j(not_zero, &done, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002254 __ mov(ebx, edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002255 __ or_(ebx, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002256 __ j(negative, &stub_call);
2257 break;
2258 }
2259 case Token::BIT_OR:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002260 __ or_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002261 break;
2262 case Token::BIT_AND:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002263 __ and_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002264 break;
2265 case Token::BIT_XOR:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002266 __ xor_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002267 break;
2268 default:
2269 UNREACHABLE();
2270 }
2271
2272 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002273 context()->Plug(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002274}
2275
2276
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002277void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
2278 Token::Value op,
ricow@chromium.org65fae842010-08-25 15:26:24 +00002279 OverwriteMode mode) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002280 __ pop(edx);
danno@chromium.org40cb8782011-05-25 07:58:50 +00002281 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002282 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002283 CallIC(stub.GetCode(isolate()), RelocInfo::CODE_TARGET,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002284 expr->BinaryOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002285 patch_site.EmitPatchInfo();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002286 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002287}
2288
2289
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00002290void FullCodeGenerator::EmitAssignment(Expression* expr) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002291 // Invalid left-hand sides are rewritten by the parser to have a 'throw
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002292 // ReferenceError' on the left-hand side.
2293 if (!expr->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002294 VisitForEffect(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002295 return;
2296 }
2297
2298 // Left-hand side can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00002299 // slot.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002300 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2301 LhsKind assign_type = VARIABLE;
2302 Property* prop = expr->AsProperty();
2303 if (prop != NULL) {
2304 assign_type = (prop->key()->IsPropertyName())
2305 ? NAMED_PROPERTY
2306 : KEYED_PROPERTY;
2307 }
2308
2309 switch (assign_type) {
2310 case VARIABLE: {
2311 Variable* var = expr->AsVariableProxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002312 EffectContext context(this);
2313 EmitVariableAssignment(var, Token::ASSIGN);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002314 break;
2315 }
2316 case NAMED_PROPERTY: {
2317 __ push(eax); // Preserve value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002318 VisitForAccumulatorValue(prop->obj());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002319 __ mov(edx, eax);
2320 __ pop(eax); // Restore value.
2321 __ mov(ecx, prop->key()->AsLiteral()->handle());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002322 Handle<Code> ic = is_classic_mode()
2323 ? isolate()->builtins()->StoreIC_Initialize()
2324 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002325 CallIC(ic);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002326 break;
2327 }
2328 case KEYED_PROPERTY: {
2329 __ push(eax); // Preserve value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002330 VisitForStackValue(prop->obj());
2331 VisitForAccumulatorValue(prop->key());
2332 __ mov(ecx, eax);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002333 __ pop(edx); // Receiver.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002334 __ pop(eax); // Restore value.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002335 Handle<Code> ic = is_classic_mode()
2336 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2337 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002338 CallIC(ic);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002339 break;
2340 }
2341 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002342 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002343}
2344
2345
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002346void FullCodeGenerator::EmitVariableAssignment(Variable* var,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002347 Token::Value op) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002348 if (var->IsUnallocated()) {
2349 // Global var, const, or let.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002350 __ mov(ecx, var->name());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002351 __ mov(edx, GlobalObjectOperand());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002352 Handle<Code> ic = is_classic_mode()
2353 ? isolate()->builtins()->StoreIC_Initialize()
2354 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002355 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002356
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002357 } else if (op == Token::INIT_CONST) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002358 // Const initializers need a write barrier.
2359 ASSERT(!var->IsParameter()); // No const parameters.
2360 if (var->IsStackLocal()) {
2361 Label skip;
2362 __ mov(edx, StackOperand(var));
2363 __ cmp(edx, isolate()->factory()->the_hole_value());
2364 __ j(not_equal, &skip);
2365 __ mov(StackOperand(var), eax);
2366 __ bind(&skip);
2367 } else {
2368 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
2369 // Like var declarations, const declarations are hoisted to function
2370 // scope. However, unlike var initializers, const initializers are
2371 // able to drill a hole to that function context, even from inside a
2372 // 'with' context. We thus bypass the normal static scope lookup for
2373 // var->IsContextSlot().
2374 __ push(eax);
2375 __ push(esi);
2376 __ push(Immediate(var->name()));
2377 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002378 }
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002379
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002380 } else if (var->mode() == LET && op != Token::INIT_LET) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002381 // Non-initializing assignment to let variable needs a write barrier.
2382 if (var->IsLookupSlot()) {
2383 __ push(eax); // Value.
2384 __ push(esi); // Context.
2385 __ push(Immediate(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002386 __ push(Immediate(Smi::FromInt(language_mode())));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002387 __ CallRuntime(Runtime::kStoreContextSlot, 4);
2388 } else {
2389 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
2390 Label assign;
2391 MemOperand location = VarOperand(var, ecx);
2392 __ mov(edx, location);
2393 __ cmp(edx, isolate()->factory()->the_hole_value());
2394 __ j(not_equal, &assign, Label::kNear);
2395 __ push(Immediate(var->name()));
2396 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2397 __ bind(&assign);
2398 __ mov(location, eax);
2399 if (var->IsContextSlot()) {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00002400 __ mov(edx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002401 int offset = Context::SlotOffset(var->index());
2402 __ RecordWriteContextSlot(ecx, offset, edx, ebx, kDontSaveFPRegs);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00002403 }
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00002404 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002405
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002406 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2407 // Assignment to var or initializing assignment to let/const
2408 // in harmony mode.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002409 if (var->IsStackAllocated() || var->IsContextSlot()) {
2410 MemOperand location = VarOperand(var, ecx);
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002411 if (generate_debug_code_ && op == Token::INIT_LET) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002412 // Check for an uninitialized let binding.
2413 __ mov(edx, location);
2414 __ cmp(edx, isolate()->factory()->the_hole_value());
2415 __ Check(equal, "Let binding re-initialization.");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002416 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002417 // Perform the assignment.
2418 __ mov(location, eax);
2419 if (var->IsContextSlot()) {
2420 __ mov(edx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002421 int offset = Context::SlotOffset(var->index());
2422 __ RecordWriteContextSlot(ecx, offset, edx, ebx, kDontSaveFPRegs);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002423 }
2424 } else {
2425 ASSERT(var->IsLookupSlot());
2426 __ push(eax); // Value.
2427 __ push(esi); // Context.
2428 __ push(Immediate(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002429 __ push(Immediate(Smi::FromInt(language_mode())));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002430 __ CallRuntime(Runtime::kStoreContextSlot, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002431 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002432 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002433 // Non-initializing assignments to consts are ignored.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002434}
2435
2436
2437void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2438 // Assignment to a property, using a named store IC.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002439 // eax : value
2440 // esp[0] : receiver
2441
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002442 Property* prop = expr->target()->AsProperty();
2443 ASSERT(prop != NULL);
2444 ASSERT(prop->key()->AsLiteral() != NULL);
2445
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002446 // Record source code position before IC call.
2447 SetSourcePosition(expr->position());
2448 __ mov(ecx, prop->key()->AsLiteral()->handle());
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002449 __ pop(edx);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002450 Handle<Code> ic = is_classic_mode()
2451 ? isolate()->builtins()->StoreIC_Initialize()
2452 : isolate()->builtins()->StoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002453 CallIC(ic, RelocInfo::CODE_TARGET, expr->AssignmentFeedbackId());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002454
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002455 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2456 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002457}
2458
2459
2460void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2461 // Assignment to a property, using a keyed store IC.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002462 // eax : value
2463 // esp[0] : key
2464 // esp[kPointerSize] : receiver
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002465
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002466 __ pop(ecx); // Key.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002467 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002468 // Record source code position before IC call.
2469 SetSourcePosition(expr->position());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002470 Handle<Code> ic = is_classic_mode()
2471 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2472 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002473 CallIC(ic, RelocInfo::CODE_TARGET, expr->AssignmentFeedbackId());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002474
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002475 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002476 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002477}
2478
2479
2480void FullCodeGenerator::VisitProperty(Property* expr) {
2481 Comment cmnt(masm_, "[ Property");
2482 Expression* key = expr->key();
2483
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002484 if (key->IsPropertyName()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002485 VisitForAccumulatorValue(expr->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002486 __ mov(edx, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002487 EmitNamedPropertyLoad(expr);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002488 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002489 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002490 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002491 VisitForStackValue(expr->obj());
2492 VisitForAccumulatorValue(expr->key());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002493 __ pop(edx); // Object.
2494 __ mov(ecx, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002495 EmitKeyedPropertyLoad(expr);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002496 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002497 }
2498}
2499
2500
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002501void FullCodeGenerator::CallIC(Handle<Code> code,
2502 RelocInfo::Mode rmode,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002503 TypeFeedbackId ast_id) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002504 ic_total_count_++;
2505 __ call(code, rmode, ast_id);
2506}
2507
2508
2509
2510
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002511void FullCodeGenerator::EmitCallWithIC(Call* expr,
2512 Handle<Object> name,
2513 RelocInfo::Mode mode) {
2514 // Code common for calls using the IC.
2515 ZoneList<Expression*>* args = expr->arguments();
2516 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002517 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002518 for (int i = 0; i < arg_count; i++) {
2519 VisitForStackValue(args->at(i));
2520 }
2521 __ Set(ecx, Immediate(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002522 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002523 // Record source position of the IC call.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002524 SetSourcePosition(expr->position());
danno@chromium.org40cb8782011-05-25 07:58:50 +00002525 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002526 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002527 CallIC(ic, mode, expr->CallFeedbackId());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002528 RecordJSReturnSite(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002529 // Restore context register.
2530 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002531 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002532}
2533
2534
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002535void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002536 Expression* key) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002537 // Load the key.
2538 VisitForAccumulatorValue(key);
2539
2540 // Swap the name of the function and the receiver on the stack to follow
2541 // the calling convention for call ICs.
2542 __ pop(ecx);
2543 __ push(eax);
2544 __ push(ecx);
2545
2546 // Load the arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002547 ZoneList<Expression*>* args = expr->arguments();
2548 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002549 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002550 for (int i = 0; i < arg_count; i++) {
2551 VisitForStackValue(args->at(i));
2552 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002553 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002554 // Record source position of the IC call.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002555 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002556 Handle<Code> ic =
2557 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002558 __ mov(ecx, Operand(esp, (arg_count + 1) * kPointerSize)); // Key.
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002559 CallIC(ic, RelocInfo::CODE_TARGET, expr->CallFeedbackId());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002560 RecordJSReturnSite(expr);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002561 // Restore context register.
2562 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002563 context()->DropAndPlug(1, eax); // Drop the key still on the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002564}
2565
2566
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002567void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002568 // Code common for calls using the call stub.
2569 ZoneList<Expression*>* args = expr->arguments();
2570 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002571 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002572 for (int i = 0; i < arg_count; i++) {
2573 VisitForStackValue(args->at(i));
2574 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002575 }
2576 // Record source position for debugger.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002577 SetSourcePosition(expr->position());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002578
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002579 // Record call targets in unoptimized code.
2580 flags = static_cast<CallFunctionFlags>(flags | RECORD_CALL_TARGET);
2581 Handle<Object> uninitialized =
2582 TypeFeedbackCells::UninitializedSentinel(isolate());
2583 Handle<JSGlobalPropertyCell> cell =
2584 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002585 RecordTypeFeedbackCell(expr->CallFeedbackId(), cell);
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002586 __ mov(ebx, cell);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002587
lrn@chromium.org34e60782011-09-15 07:25:40 +00002588 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002589 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002590 __ CallStub(&stub, expr->CallFeedbackId());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002591
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002592 RecordJSReturnSite(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002593 // Restore context register.
2594 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002595 context()->DropAndPlug(1, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002596}
2597
2598
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002599void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002600 // Push copy of the first argument or undefined if it doesn't exist.
2601 if (arg_count > 0) {
2602 __ push(Operand(esp, arg_count * kPointerSize));
2603 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002604 __ push(Immediate(isolate()->factory()->undefined_value()));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002605 }
2606
2607 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002608 __ push(Operand(ebp, (2 + info_->scope()->num_parameters()) * kPointerSize));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002609 // Push the language mode.
2610 __ push(Immediate(Smi::FromInt(language_mode())));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002611
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002612 // Push the start position of the scope the calls resides in.
2613 __ push(Immediate(Smi::FromInt(scope()->start_position())));
2614
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002615 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002616 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002617}
2618
2619
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002620void FullCodeGenerator::VisitCall(Call* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002621#ifdef DEBUG
2622 // We want to verify that RecordJSReturnSite gets called on all paths
2623 // through this function. Avoid early returns.
2624 expr->return_is_recorded_ = false;
2625#endif
2626
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002627 Comment cmnt(masm_, "[ Call");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002628 Expression* callee = expr->expression();
2629 VariableProxy* proxy = callee->AsVariableProxy();
2630 Property* property = callee->AsProperty();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002631
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002632 if (proxy != NULL && proxy->var()->is_possibly_eval(isolate())) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002633 // In a call to eval, we first call %ResolvePossiblyDirectEval to
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002634 // resolve the function we need to call and the receiver of the call.
2635 // Then we call the resolved function using the given arguments.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002636 ZoneList<Expression*>* args = expr->arguments();
2637 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002638 { PreservePositionScope pos_scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002639 VisitForStackValue(callee);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002640 // Reserved receiver slot.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002641 __ push(Immediate(isolate()->factory()->undefined_value()));
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002642 // Push the arguments.
2643 for (int i = 0; i < arg_count; i++) {
2644 VisitForStackValue(args->at(i));
2645 }
2646
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002647 // Push a copy of the function (found below the arguments) and
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002648 // resolve eval.
2649 __ push(Operand(esp, (arg_count + 1) * kPointerSize));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002650 EmitResolvePossiblyDirectEval(arg_count);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002651
2652 // The runtime call returns a pair of values in eax (function) and
2653 // edx (receiver). Touch up the stack with the right values.
2654 __ mov(Operand(esp, (arg_count + 0) * kPointerSize), edx);
2655 __ mov(Operand(esp, (arg_count + 1) * kPointerSize), eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002656 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002657 // Record source position for debugger.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002658 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002659 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002660 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002661 __ CallStub(&stub);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002662 RecordJSReturnSite(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002663 // Restore context register.
2664 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002665 context()->DropAndPlug(1, eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002666
2667 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002668 // Push global object as receiver for the call IC.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002669 __ push(GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002670 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2671
2672 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002673 // Call to a lookup slot (dynamically introduced variable).
2674 Label slow, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002675 { PreservePositionScope scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002676 // Generate code for loading from variables potentially shadowed by
2677 // eval-introduced variables.
2678 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002679 }
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002680 __ bind(&slow);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002681 // Call the runtime to find the function to call (returned in eax) and
2682 // the object holding it (returned in edx).
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002683 __ push(context_register());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002684 __ push(Immediate(proxy->name()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002685 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2686 __ push(eax); // Function.
2687 __ push(edx); // Receiver.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002688
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002689 // If fast case code has been generated, emit code to push the function
2690 // and receiver and have the slow path jump around this code.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002691 if (done.is_linked()) {
2692 Label call;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002693 __ jmp(&call, Label::kNear);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002694 __ bind(&done);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002695 // Push function.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002696 __ push(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002697 // The receiver is implicitly the global receiver. Indicate this by
2698 // passing the hole to the call function stub.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002699 __ push(Immediate(isolate()->factory()->the_hole_value()));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002700 __ bind(&call);
2701 }
2702
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002703 // The receiver is either the global receiver or an object found by
2704 // LoadContextSlot. That object could be the hole if the receiver is
2705 // implicitly the global object.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002706 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002707
2708 } else if (property != NULL) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002709 { PreservePositionScope scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002710 VisitForStackValue(property->obj());
2711 }
2712 if (property->key()->IsPropertyName()) {
2713 EmitCallWithIC(expr,
2714 property->key()->AsLiteral()->handle(),
2715 RelocInfo::CODE_TARGET);
2716 } else {
2717 EmitKeyedCallWithIC(expr, property->key());
2718 }
2719
2720 } else {
2721 // Call to an arbitrary expression not handled specially above.
2722 { PreservePositionScope scope(masm()->positions_recorder());
2723 VisitForStackValue(callee);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002724 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002725 // Load global receiver object.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002726 __ mov(ebx, GlobalObjectOperand());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002727 __ push(FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
2728 // Emit function call.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002729 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002730 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002731
2732#ifdef DEBUG
2733 // RecordJSReturnSite should have been called.
2734 ASSERT(expr->return_is_recorded_);
2735#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002736}
2737
2738
2739void FullCodeGenerator::VisitCallNew(CallNew* expr) {
2740 Comment cmnt(masm_, "[ CallNew");
2741 // According to ECMA-262, section 11.2.2, page 44, the function
2742 // expression in new calls must be evaluated before the
2743 // arguments.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002744
ricow@chromium.org65fae842010-08-25 15:26:24 +00002745 // Push constructor on the stack. If it's not a function it's used as
2746 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2747 // ignored.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002748 VisitForStackValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002749
2750 // Push the arguments ("left-to-right") on the stack.
2751 ZoneList<Expression*>* args = expr->arguments();
2752 int arg_count = args->length();
2753 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002754 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002755 }
2756
2757 // Call the construct call builtin that handles allocation and
2758 // constructor invocation.
2759 SetSourcePosition(expr->position());
2760
ricow@chromium.org65fae842010-08-25 15:26:24 +00002761 // Load function and argument count into edi and eax.
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002762 __ Set(eax, Immediate(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002763 __ mov(edi, Operand(esp, arg_count * kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002764
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002765 // Record call targets in unoptimized code.
2766 Handle<Object> uninitialized =
2767 TypeFeedbackCells::UninitializedSentinel(isolate());
2768 Handle<JSGlobalPropertyCell> cell =
2769 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002770 RecordTypeFeedbackCell(expr->CallNewFeedbackId(), cell);
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002771 __ mov(ebx, cell);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002772
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002773 CallConstructStub stub(RECORD_CALL_TARGET);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002774 __ call(stub.GetCode(isolate()), RelocInfo::CONSTRUCT_CALL);
ulan@chromium.org967e2702012-02-28 09:49:15 +00002775 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002776 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002777}
2778
2779
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002780void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2781 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002782 ASSERT(args->length() == 1);
2783
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002784 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002785
2786 Label materialize_true, materialize_false;
2787 Label* if_true = NULL;
2788 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002789 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002790 context()->PrepareTest(&materialize_true, &materialize_false,
2791 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002792
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002793 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002794 __ test(eax, Immediate(kSmiTagMask));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002795 Split(zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002796
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002797 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002798}
2799
2800
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002801void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2802 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002803 ASSERT(args->length() == 1);
2804
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002805 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002806
2807 Label materialize_true, materialize_false;
2808 Label* if_true = NULL;
2809 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002810 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002811 context()->PrepareTest(&materialize_true, &materialize_false,
2812 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002813
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002814 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002815 __ test(eax, Immediate(kSmiTagMask | 0x80000000));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002816 Split(zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002817
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002818 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002819}
2820
2821
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002822void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2823 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002824 ASSERT(args->length() == 1);
2825
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002826 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002827
2828 Label materialize_true, materialize_false;
2829 Label* if_true = NULL;
2830 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002831 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002832 context()->PrepareTest(&materialize_true, &materialize_false,
2833 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002834
whesse@chromium.org7b260152011-06-20 15:33:18 +00002835 __ JumpIfSmi(eax, if_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002836 __ cmp(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002837 __ j(equal, if_true);
2838 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2839 // Undetectable objects behave like undefined when tested with typeof.
2840 __ movzx_b(ecx, FieldOperand(ebx, Map::kBitFieldOffset));
2841 __ test(ecx, Immediate(1 << Map::kIsUndetectable));
2842 __ j(not_zero, if_false);
2843 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceTypeOffset));
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002844 __ cmp(ecx, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002845 __ j(below, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002846 __ cmp(ecx, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002847 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002848 Split(below_equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002849
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002850 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002851}
2852
2853
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002854void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2855 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002856 ASSERT(args->length() == 1);
2857
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002858 VisitForAccumulatorValue(args->at(0));
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002859
2860 Label materialize_true, materialize_false;
2861 Label* if_true = NULL;
2862 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002863 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002864 context()->PrepareTest(&materialize_true, &materialize_false,
2865 &if_true, &if_false, &fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002866
whesse@chromium.org7b260152011-06-20 15:33:18 +00002867 __ JumpIfSmi(eax, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002868 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002869 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002870 Split(above_equal, if_true, if_false, fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002871
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002872 context()->Plug(if_true, if_false);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002873}
2874
2875
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002876void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2877 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002878 ASSERT(args->length() == 1);
2879
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002880 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002881
2882 Label materialize_true, materialize_false;
2883 Label* if_true = NULL;
2884 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002885 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002886 context()->PrepareTest(&materialize_true, &materialize_false,
2887 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002888
whesse@chromium.org7b260152011-06-20 15:33:18 +00002889 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002890 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2891 __ movzx_b(ebx, FieldOperand(ebx, Map::kBitFieldOffset));
2892 __ test(ebx, Immediate(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002893 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002894 Split(not_zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002895
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002896 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002897}
2898
2899
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002900void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002901 CallRuntime* expr) {
2902 ZoneList<Expression*>* args = expr->arguments();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002903 ASSERT(args->length() == 1);
2904
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002905 VisitForAccumulatorValue(args->at(0));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002906
2907 Label materialize_true, materialize_false;
2908 Label* if_true = NULL;
2909 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002910 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002911 context()->PrepareTest(&materialize_true, &materialize_false,
2912 &if_true, &if_false, &fall_through);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002913
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002914 __ AssertNotSmi(eax);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002915
2916 // Check whether this map has already been checked to be safe for default
2917 // valueOf.
2918 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2919 __ test_b(FieldOperand(ebx, Map::kBitField2Offset),
2920 1 << Map::kStringWrapperSafeForDefaultValueOf);
2921 __ j(not_zero, if_true);
2922
2923 // Check for fast case object. Return false for slow case objects.
2924 __ mov(ecx, FieldOperand(eax, JSObject::kPropertiesOffset));
2925 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
2926 __ cmp(ecx, FACTORY->hash_table_map());
2927 __ j(equal, if_false);
2928
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002929 // Look for valueOf string in the descriptor array, and indicate false if
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002930 // found. Since we omit an enumeration index check, if it is added via a
2931 // transition that shares its descriptor array, this is a false positive.
2932 Label entry, loop, done;
2933
2934 // Skip loop if no descriptors are valid.
2935 __ NumberOfOwnDescriptors(ecx, ebx);
2936 __ cmp(ecx, 0);
2937 __ j(equal, &done);
2938
danno@chromium.org40cb8782011-05-25 07:58:50 +00002939 __ LoadInstanceDescriptors(ebx, ebx);
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002940 // ebx: descriptor array.
2941 // ecx: valid entries in the descriptor array.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002942 // Calculate the end of the descriptor array.
2943 STATIC_ASSERT(kSmiTag == 0);
2944 STATIC_ASSERT(kSmiTagSize == 1);
2945 STATIC_ASSERT(kPointerSize == 4);
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002946 __ imul(ecx, ecx, DescriptorArray::kDescriptorSize);
2947 __ lea(ecx, Operand(ebx, ecx, times_2, DescriptorArray::kFirstOffset));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002948 // Calculate location of the first key name.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002949 __ add(ebx, Immediate(DescriptorArray::kFirstOffset));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002950 // Loop through all the keys in the descriptor array. If one of these is the
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002951 // internalized string "valueOf" the result is false.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002952 __ jmp(&entry);
2953 __ bind(&loop);
2954 __ mov(edx, FieldOperand(ebx, 0));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002955 __ cmp(edx, FACTORY->value_of_string());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002956 __ j(equal, if_false);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002957 __ add(ebx, Immediate(DescriptorArray::kDescriptorSize * kPointerSize));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002958 __ bind(&entry);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002959 __ cmp(ebx, ecx);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002960 __ j(not_equal, &loop);
2961
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002962 __ bind(&done);
2963
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002964 // Reload map as register ebx was used as temporary above.
2965 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2966
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002967 // If a valueOf property is not found on the object check that its
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002968 // prototype is the un-modified String prototype. If not result is false.
2969 __ mov(ecx, FieldOperand(ebx, Map::kPrototypeOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00002970 __ JumpIfSmi(ecx, if_false);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002971 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002972 __ mov(edx, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002973 __ mov(edx,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002974 FieldOperand(edx, GlobalObject::kNativeContextOffset));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002975 __ cmp(ecx,
2976 ContextOperand(edx,
2977 Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2978 __ j(not_equal, if_false);
2979 // Set the bit in the map to indicate that it has been checked safe for
2980 // default valueOf and set true result.
2981 __ or_(FieldOperand(ebx, Map::kBitField2Offset),
2982 Immediate(1 << Map::kStringWrapperSafeForDefaultValueOf));
2983 __ jmp(if_true);
2984
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002985 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002986 context()->Plug(if_true, if_false);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002987}
2988
2989
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002990void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2991 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002992 ASSERT(args->length() == 1);
2993
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002994 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002995
2996 Label materialize_true, materialize_false;
2997 Label* if_true = NULL;
2998 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002999 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003000 context()->PrepareTest(&materialize_true, &materialize_false,
3001 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003002
whesse@chromium.org7b260152011-06-20 15:33:18 +00003003 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003004 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003005 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003006 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003007
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003008 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003009}
3010
3011
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003012void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
3013 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003014 ASSERT(args->length() == 1);
3015
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003016 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003017
3018 Label materialize_true, materialize_false;
3019 Label* if_true = NULL;
3020 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003021 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003022 context()->PrepareTest(&materialize_true, &materialize_false,
3023 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003024
whesse@chromium.org7b260152011-06-20 15:33:18 +00003025 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003026 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003027 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003028 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003029
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003030 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003031}
3032
3033
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003034void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
3035 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003036 ASSERT(args->length() == 1);
3037
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003038 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003039
3040 Label materialize_true, materialize_false;
3041 Label* if_true = NULL;
3042 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003043 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003044 context()->PrepareTest(&materialize_true, &materialize_false,
3045 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003046
whesse@chromium.org7b260152011-06-20 15:33:18 +00003047 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003048 __ CmpObjectType(eax, JS_REGEXP_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003049 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003050 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003051
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003052 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003053}
3054
3055
3056
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003057void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
3058 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003059
3060 Label materialize_true, materialize_false;
3061 Label* if_true = NULL;
3062 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003063 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003064 context()->PrepareTest(&materialize_true, &materialize_false,
3065 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003066
3067 // Get the frame pointer for the calling frame.
3068 __ mov(eax, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3069
3070 // Skip the arguments adaptor frame if it exists.
3071 Label check_frame_marker;
3072 __ cmp(Operand(eax, StandardFrameConstants::kContextOffset),
3073 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3074 __ j(not_equal, &check_frame_marker);
3075 __ mov(eax, Operand(eax, StandardFrameConstants::kCallerFPOffset));
3076
3077 // Check the marker in the calling frame.
3078 __ bind(&check_frame_marker);
3079 __ cmp(Operand(eax, StandardFrameConstants::kMarkerOffset),
3080 Immediate(Smi::FromInt(StackFrame::CONSTRUCT)));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003081 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003082 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003083
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003084 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003085}
3086
3087
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003088void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
3089 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003090 ASSERT(args->length() == 2);
3091
3092 // Load the two objects into registers and perform the comparison.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003093 VisitForStackValue(args->at(0));
3094 VisitForAccumulatorValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003095
3096 Label materialize_true, materialize_false;
3097 Label* if_true = NULL;
3098 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003099 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003100 context()->PrepareTest(&materialize_true, &materialize_false,
3101 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003102
3103 __ pop(ebx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003104 __ cmp(eax, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003105 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003106 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003107
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003108 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003109}
3110
3111
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003112void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
3113 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003114 ASSERT(args->length() == 1);
3115
3116 // ArgumentsAccessStub expects the key in edx and the formal
3117 // parameter count in eax.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003118 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003119 __ mov(edx, eax);
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00003120 __ Set(eax, Immediate(Smi::FromInt(info_->scope()->num_parameters())));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003121 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
3122 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003123 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003124}
3125
3126
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003127void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
3128 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003129
3130 Label exit;
3131 // Get the number of formal parameters.
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00003132 __ Set(eax, Immediate(Smi::FromInt(info_->scope()->num_parameters())));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003133
3134 // Check if the calling frame is an arguments adaptor frame.
3135 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3136 __ cmp(Operand(ebx, StandardFrameConstants::kContextOffset),
3137 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3138 __ j(not_equal, &exit);
3139
3140 // Arguments adaptor case: Read the arguments length from the
3141 // adaptor frame.
3142 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3143
3144 __ bind(&exit);
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003145 __ AssertSmi(eax);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003146 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003147}
3148
3149
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003150void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
3151 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003152 ASSERT(args->length() == 1);
3153 Label done, null, function, non_function_constructor;
3154
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003155 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003156
3157 // If the object is a smi, we return null.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003158 __ JumpIfSmi(eax, &null);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003159
3160 // Check that the object is a JS object but take special care of JS
3161 // functions to make sure they have 'Function' as their class.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003162 // Assume that there are only two callable types, and one of them is at
3163 // either end of the type range for JS object types. Saves extra comparisons.
3164 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003165 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, eax);
3166 // Map is now in eax.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003167 __ j(below, &null);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003168 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3169 FIRST_SPEC_OBJECT_TYPE + 1);
3170 __ j(equal, &function);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003171
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003172 __ CmpInstanceType(eax, LAST_SPEC_OBJECT_TYPE);
3173 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3174 LAST_SPEC_OBJECT_TYPE - 1);
3175 __ j(equal, &function);
3176 // Assume that there is no larger type.
3177 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003178
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003179 // Check if the constructor in the map is a JS function.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003180 __ mov(eax, FieldOperand(eax, Map::kConstructorOffset));
3181 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
3182 __ j(not_equal, &non_function_constructor);
3183
3184 // eax now contains the constructor function. Grab the
3185 // instance class name from there.
3186 __ mov(eax, FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset));
3187 __ mov(eax, FieldOperand(eax, SharedFunctionInfo::kInstanceClassNameOffset));
3188 __ jmp(&done);
3189
3190 // Functions have class 'Function'.
3191 __ bind(&function);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00003192 __ mov(eax, isolate()->factory()->function_class_string());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003193 __ jmp(&done);
3194
3195 // Objects with a non-function constructor have class 'Object'.
3196 __ bind(&non_function_constructor);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00003197 __ mov(eax, isolate()->factory()->Object_string());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003198 __ jmp(&done);
3199
3200 // Non-JS objects have class null.
3201 __ bind(&null);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003202 __ mov(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003203
3204 // All done.
3205 __ bind(&done);
3206
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003207 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003208}
3209
3210
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003211void FullCodeGenerator::EmitLog(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003212 // Conditionally generate a log call.
3213 // Args:
3214 // 0 (literal string): The type of logging (corresponds to the flags).
3215 // This is used to determine whether or not to generate the log call.
3216 // 1 (string): Format string. Access the string at argument index 2
3217 // with '%2s' (see Logger::LogRuntime for all the formats).
3218 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003219 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003220 ASSERT_EQ(args->length(), 3);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003221 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003222 VisitForStackValue(args->at(1));
3223 VisitForStackValue(args->at(2));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003224 __ CallRuntime(Runtime::kLog, 2);
3225 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003226 // Finally, we're expected to leave a value on the top of the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003227 __ mov(eax, isolate()->factory()->undefined_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003228 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003229}
3230
3231
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003232void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
3233 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003234
3235 Label slow_allocate_heapnumber;
3236 Label heapnumber_allocated;
3237
3238 __ AllocateHeapNumber(edi, ebx, ecx, &slow_allocate_heapnumber);
3239 __ jmp(&heapnumber_allocated);
3240
3241 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003242 // Allocate a heap number.
3243 __ CallRuntime(Runtime::kNumberAlloc, 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003244 __ mov(edi, eax);
3245
3246 __ bind(&heapnumber_allocated);
3247
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003248 __ PrepareCallCFunction(1, ebx);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003249 __ mov(eax, ContextOperand(context_register(), Context::GLOBAL_OBJECT_INDEX));
3250 __ mov(eax, FieldOperand(eax, GlobalObject::kNativeContextOffset));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003251 __ mov(Operand(esp, 0), eax);
3252 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003253
3254 // Convert 32 random bits in eax to 0.(32 random bits) in a double
3255 // by computing:
3256 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
3257 // This is implemented on both SSE2 and FPU.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003258 if (CpuFeatures::IsSupported(SSE2)) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00003259 CpuFeatureScope fscope(masm(), SSE2);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003260 __ mov(ebx, Immediate(0x49800000)); // 1.0 x 2^20 as single.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003261 __ movd(xmm1, ebx);
3262 __ movd(xmm0, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003263 __ cvtss2sd(xmm1, xmm1);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003264 __ xorps(xmm0, xmm1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003265 __ subsd(xmm0, xmm1);
3266 __ movdbl(FieldOperand(edi, HeapNumber::kValueOffset), xmm0);
3267 } else {
3268 // 0x4130000000000000 is 1.0 x 2^20 as a double.
3269 __ mov(FieldOperand(edi, HeapNumber::kExponentOffset),
3270 Immediate(0x41300000));
3271 __ mov(FieldOperand(edi, HeapNumber::kMantissaOffset), eax);
3272 __ fld_d(FieldOperand(edi, HeapNumber::kValueOffset));
3273 __ mov(FieldOperand(edi, HeapNumber::kMantissaOffset), Immediate(0));
3274 __ fld_d(FieldOperand(edi, HeapNumber::kValueOffset));
3275 __ fsubp(1);
3276 __ fstp_d(FieldOperand(edi, HeapNumber::kValueOffset));
3277 }
3278 __ mov(eax, edi);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003279 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003280}
3281
3282
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003283void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003284 // Load the arguments on the stack and call the stub.
3285 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003286 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003287 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003288 VisitForStackValue(args->at(0));
3289 VisitForStackValue(args->at(1));
3290 VisitForStackValue(args->at(2));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003291 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003292 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003293}
3294
3295
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003296void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003297 // Load the arguments on the stack and call the stub.
3298 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003299 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003300 ASSERT(args->length() == 4);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003301 VisitForStackValue(args->at(0));
3302 VisitForStackValue(args->at(1));
3303 VisitForStackValue(args->at(2));
3304 VisitForStackValue(args->at(3));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003305 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003306 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003307}
3308
3309
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003310void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3311 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003312 ASSERT(args->length() == 1);
3313
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003314 VisitForAccumulatorValue(args->at(0)); // Load the object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003315
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003316 Label done;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003317 // If the object is a smi return the object.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003318 __ JumpIfSmi(eax, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003319 // If the object is not a value type, return the object.
3320 __ CmpObjectType(eax, JS_VALUE_TYPE, ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003321 __ j(not_equal, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003322 __ mov(eax, FieldOperand(eax, JSValue::kValueOffset));
3323
3324 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003325 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003326}
3327
3328
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003329void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3330 ZoneList<Expression*>* args = expr->arguments();
3331 ASSERT(args->length() == 2);
3332 ASSERT_NE(NULL, args->at(1)->AsLiteral());
3333 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->handle()));
3334
3335 VisitForAccumulatorValue(args->at(0)); // Load the object.
3336
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003337 Label runtime, done, not_date_object;
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003338 Register object = eax;
3339 Register result = eax;
3340 Register scratch = ecx;
3341
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003342 __ JumpIfSmi(object, &not_date_object);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003343 __ CmpObjectType(object, JS_DATE_TYPE, scratch);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003344 __ j(not_equal, &not_date_object);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003345
3346 if (index->value() == 0) {
3347 __ mov(result, FieldOperand(object, JSDate::kValueOffset));
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003348 __ jmp(&done);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003349 } else {
3350 if (index->value() < JSDate::kFirstUncachedField) {
3351 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3352 __ mov(scratch, Operand::StaticVariable(stamp));
3353 __ cmp(scratch, FieldOperand(object, JSDate::kCacheStampOffset));
3354 __ j(not_equal, &runtime, Label::kNear);
3355 __ mov(result, FieldOperand(object, JSDate::kValueOffset +
3356 kPointerSize * index->value()));
3357 __ jmp(&done);
3358 }
3359 __ bind(&runtime);
3360 __ PrepareCallCFunction(2, scratch);
3361 __ mov(Operand(esp, 0), object);
3362 __ mov(Operand(esp, 1 * kPointerSize), Immediate(index));
3363 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003364 __ jmp(&done);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003365 }
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003366
3367 __ bind(&not_date_object);
3368 __ CallRuntime(Runtime::kThrowNotDateError, 0);
3369 __ bind(&done);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003370 context()->Plug(result);
3371}
3372
3373
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003374void FullCodeGenerator::EmitSeqStringSetCharCheck(Register string,
3375 Register index,
3376 Register value,
3377 uint32_t encoding_mask) {
3378 __ test(index, Immediate(kSmiTagMask));
3379 __ Check(zero, "Non-smi index");
3380 __ test(value, Immediate(kSmiTagMask));
3381 __ Check(zero, "Non-smi value");
3382
3383 __ cmp(index, FieldOperand(string, String::kLengthOffset));
3384 __ Check(less, "Index is too large");
3385
3386 __ cmp(index, Immediate(Smi::FromInt(0)));
3387 __ Check(greater_equal, "Index is negative");
3388
3389 __ push(value);
3390 __ mov(value, FieldOperand(string, HeapObject::kMapOffset));
3391 __ movzx_b(value, FieldOperand(value, Map::kInstanceTypeOffset));
3392
3393 __ and_(value, Immediate(kStringRepresentationMask | kStringEncodingMask));
3394 __ cmp(value, Immediate(encoding_mask));
3395 __ Check(equal, "Unexpected string type");
3396 __ pop(value);
3397}
3398
3399
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003400void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3401 ZoneList<Expression*>* args = expr->arguments();
3402 ASSERT_EQ(3, args->length());
3403
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003404 Register string = eax;
3405 Register index = ebx;
3406 Register value = ecx;
3407
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003408 VisitForStackValue(args->at(1)); // index
3409 VisitForStackValue(args->at(2)); // value
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003410 __ pop(value);
3411 __ pop(index);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003412 VisitForAccumulatorValue(args->at(0)); // string
3413
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003414
3415 if (FLAG_debug_code) {
3416 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
3417 EmitSeqStringSetCharCheck(string, index, value, one_byte_seq_type);
3418 }
3419
3420 __ SmiUntag(value);
3421 __ SmiUntag(index);
3422 __ mov_b(FieldOperand(string, index, times_1, SeqOneByteString::kHeaderSize),
3423 value);
3424 context()->Plug(string);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003425}
3426
3427
3428void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3429 ZoneList<Expression*>* args = expr->arguments();
3430 ASSERT_EQ(3, args->length());
3431
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003432 Register string = eax;
3433 Register index = ebx;
3434 Register value = ecx;
3435
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003436 VisitForStackValue(args->at(1)); // index
3437 VisitForStackValue(args->at(2)); // value
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003438 __ pop(value);
3439 __ pop(index);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003440 VisitForAccumulatorValue(args->at(0)); // string
3441
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003442 if (FLAG_debug_code) {
3443 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
3444 EmitSeqStringSetCharCheck(string, index, value, two_byte_seq_type);
3445 }
3446
3447 __ SmiUntag(value);
3448 // No need to untag a smi for two-byte addressing.
3449 __ mov_w(FieldOperand(string, index, times_1, SeqTwoByteString::kHeaderSize),
3450 value);
3451 context()->Plug(string);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003452}
3453
3454
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003455void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003456 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003457 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003458 ASSERT(args->length() == 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003459 VisitForStackValue(args->at(0));
3460 VisitForStackValue(args->at(1));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003461
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003462 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003463 MathPowStub stub(MathPowStub::ON_STACK);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003464 __ CallStub(&stub);
3465 } else {
3466 __ CallRuntime(Runtime::kMath_pow, 2);
3467 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003468 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003469}
3470
3471
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003472void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3473 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003474 ASSERT(args->length() == 2);
3475
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003476 VisitForStackValue(args->at(0)); // Load the object.
3477 VisitForAccumulatorValue(args->at(1)); // Load the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003478 __ pop(ebx); // eax = value. ebx = object.
3479
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003480 Label done;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003481 // If the object is a smi, return the value.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003482 __ JumpIfSmi(ebx, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003483
3484 // If the object is not a value type, return the value.
3485 __ CmpObjectType(ebx, JS_VALUE_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003486 __ j(not_equal, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003487
3488 // Store the value.
3489 __ mov(FieldOperand(ebx, JSValue::kValueOffset), eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003490
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003491 // Update the write barrier. Save the value as it will be
3492 // overwritten by the write barrier code and is needed afterward.
3493 __ mov(edx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003494 __ RecordWriteField(ebx, JSValue::kValueOffset, edx, ecx, kDontSaveFPRegs);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003495
3496 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003497 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003498}
3499
3500
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003501void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3502 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003503 ASSERT_EQ(args->length(), 1);
3504
3505 // Load the argument on the stack and call the stub.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003506 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003507
3508 NumberToStringStub stub;
3509 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003510 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003511}
3512
3513
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003514void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3515 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003516 ASSERT(args->length() == 1);
3517
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003518 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003519
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003520 Label done;
3521 StringCharFromCodeGenerator generator(eax, ebx);
3522 generator.GenerateFast(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003523 __ jmp(&done);
3524
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003525 NopRuntimeCallHelper call_helper;
3526 generator.GenerateSlow(masm_, call_helper);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003527
3528 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003529 context()->Plug(ebx);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003530}
3531
3532
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003533void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3534 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003535 ASSERT(args->length() == 2);
3536
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003537 VisitForStackValue(args->at(0));
3538 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003539
3540 Register object = ebx;
3541 Register index = eax;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003542 Register result = edx;
3543
3544 __ pop(object);
3545
3546 Label need_conversion;
3547 Label index_out_of_range;
3548 Label done;
3549 StringCharCodeAtGenerator generator(object,
3550 index,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003551 result,
3552 &need_conversion,
3553 &need_conversion,
3554 &index_out_of_range,
3555 STRING_INDEX_IS_NUMBER);
3556 generator.GenerateFast(masm_);
3557 __ jmp(&done);
3558
3559 __ bind(&index_out_of_range);
3560 // When the index is out of range, the spec requires us to return
3561 // NaN.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003562 __ Set(result, Immediate(isolate()->factory()->nan_value()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003563 __ jmp(&done);
3564
3565 __ bind(&need_conversion);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003566 // Move the undefined value into the result register, which will
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003567 // trigger conversion.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003568 __ Set(result, Immediate(isolate()->factory()->undefined_value()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003569 __ jmp(&done);
3570
3571 NopRuntimeCallHelper call_helper;
3572 generator.GenerateSlow(masm_, call_helper);
3573
3574 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003575 context()->Plug(result);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003576}
3577
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003578
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003579void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3580 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003581 ASSERT(args->length() == 2);
3582
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003583 VisitForStackValue(args->at(0));
3584 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003585
3586 Register object = ebx;
3587 Register index = eax;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003588 Register scratch = edx;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003589 Register result = eax;
3590
3591 __ pop(object);
3592
3593 Label need_conversion;
3594 Label index_out_of_range;
3595 Label done;
3596 StringCharAtGenerator generator(object,
3597 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003598 scratch,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003599 result,
3600 &need_conversion,
3601 &need_conversion,
3602 &index_out_of_range,
3603 STRING_INDEX_IS_NUMBER);
3604 generator.GenerateFast(masm_);
3605 __ jmp(&done);
3606
3607 __ bind(&index_out_of_range);
3608 // When the index is out of range, the spec requires us to return
3609 // the empty string.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003610 __ Set(result, Immediate(isolate()->factory()->empty_string()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003611 __ jmp(&done);
3612
3613 __ bind(&need_conversion);
3614 // Move smi zero into the result register, which will trigger
3615 // conversion.
3616 __ Set(result, Immediate(Smi::FromInt(0)));
3617 __ jmp(&done);
3618
3619 NopRuntimeCallHelper call_helper;
3620 generator.GenerateSlow(masm_, call_helper);
3621
3622 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003623 context()->Plug(result);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003624}
3625
3626
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003627void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3628 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003629 ASSERT_EQ(2, args->length());
3630
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003631 VisitForStackValue(args->at(0));
3632 VisitForStackValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003633
3634 StringAddStub stub(NO_STRING_ADD_FLAGS);
3635 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003636 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003637}
3638
3639
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003640void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3641 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003642 ASSERT_EQ(2, args->length());
3643
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003644 VisitForStackValue(args->at(0));
3645 VisitForStackValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003646
3647 StringCompareStub stub;
3648 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003649 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003650}
3651
3652
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003653void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003654 // Load the argument on the stack and call the stub.
whesse@chromium.org023421e2010-12-21 12:19:12 +00003655 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3656 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003657 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003658 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003659 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003660 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003661 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003662}
3663
3664
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003665void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003666 // Load the argument on the stack and call the stub.
whesse@chromium.org023421e2010-12-21 12:19:12 +00003667 TranscendentalCacheStub stub(TranscendentalCache::COS,
3668 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003669 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003670 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003671 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003672 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003673 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003674}
3675
3676
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003677void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3678 // Load the argument on the stack and call the stub.
3679 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3680 TranscendentalCacheStub::TAGGED);
3681 ZoneList<Expression*>* args = expr->arguments();
3682 ASSERT(args->length() == 1);
3683 VisitForStackValue(args->at(0));
3684 __ CallStub(&stub);
3685 context()->Plug(eax);
3686}
3687
3688
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003689void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003690 // Load the argument on the stack and call the stub.
whesse@chromium.org023421e2010-12-21 12:19:12 +00003691 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3692 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003693 ZoneList<Expression*>* args = expr->arguments();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003694 ASSERT(args->length() == 1);
3695 VisitForStackValue(args->at(0));
3696 __ CallStub(&stub);
3697 context()->Plug(eax);
3698}
3699
3700
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003701void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003702 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003703 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003704 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003705 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003706 __ CallRuntime(Runtime::kMath_sqrt, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003707 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003708}
3709
3710
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003711void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3712 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003713 ASSERT(args->length() >= 2);
3714
danno@chromium.org160a7b02011-04-18 15:51:38 +00003715 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3716 for (int i = 0; i < arg_count + 1; ++i) {
3717 VisitForStackValue(args->at(i));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003718 }
danno@chromium.org160a7b02011-04-18 15:51:38 +00003719 VisitForAccumulatorValue(args->last()); // Function.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003720
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00003721 Label runtime, done;
3722 // Check for non-function argument (including proxy).
3723 __ JumpIfSmi(eax, &runtime);
3724 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
3725 __ j(not_equal, &runtime);
danno@chromium.orgc612e022011-11-10 11:38:15 +00003726
danno@chromium.org160a7b02011-04-18 15:51:38 +00003727 // InvokeFunction requires the function in edi. Move it in there.
3728 __ mov(edi, result_register());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003729 ParameterCount count(arg_count);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003730 __ InvokeFunction(edi, count, CALL_FUNCTION,
3731 NullCallWrapper(), CALL_AS_METHOD);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003732 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003733 __ jmp(&done);
3734
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00003735 __ bind(&runtime);
danno@chromium.orgc612e022011-11-10 11:38:15 +00003736 __ push(eax);
3737 __ CallRuntime(Runtime::kCall, args->length());
3738 __ bind(&done);
3739
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003740 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003741}
3742
3743
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003744void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003745 // Load the arguments on the stack and call the stub.
3746 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003747 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003748 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003749 VisitForStackValue(args->at(0));
3750 VisitForStackValue(args->at(1));
3751 VisitForStackValue(args->at(2));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003752 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003753 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003754}
3755
3756
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003757void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3758 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003759 ASSERT_EQ(2, args->length());
3760
3761 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3762 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3763
3764 Handle<FixedArray> jsfunction_result_caches(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003765 isolate()->native_context()->jsfunction_result_caches());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003766 if (jsfunction_result_caches->length() <= cache_id) {
3767 __ Abort("Attempt to use undefined cache.");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003768 __ mov(eax, isolate()->factory()->undefined_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003769 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003770 return;
3771 }
3772
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003773 VisitForAccumulatorValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003774
3775 Register key = eax;
3776 Register cache = ebx;
3777 Register tmp = ecx;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003778 __ mov(cache, ContextOperand(esi, Context::GLOBAL_OBJECT_INDEX));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003779 __ mov(cache,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003780 FieldOperand(cache, GlobalObject::kNativeContextOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003781 __ mov(cache, ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003782 __ mov(cache,
3783 FieldOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3784
3785 Label done, not_found;
3786 // tmp now holds finger offset as a smi.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003787 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003788 __ mov(tmp, FieldOperand(cache, JSFunctionResultCache::kFingerOffset));
3789 __ cmp(key, CodeGenerator::FixedArrayElementOperand(cache, tmp));
3790 __ j(not_equal, &not_found);
3791
3792 __ mov(eax, CodeGenerator::FixedArrayElementOperand(cache, tmp, 1));
3793 __ jmp(&done);
3794
3795 __ bind(&not_found);
3796 // Call runtime to perform the lookup.
3797 __ push(cache);
3798 __ push(key);
3799 __ CallRuntime(Runtime::kGetFromCache, 2);
3800
3801 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003802 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003803}
3804
3805
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003806void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3807 ZoneList<Expression*>* args = expr->arguments();
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003808 ASSERT_EQ(2, args->length());
3809
3810 Register right = eax;
3811 Register left = ebx;
3812 Register tmp = ecx;
3813
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003814 VisitForStackValue(args->at(0));
3815 VisitForAccumulatorValue(args->at(1));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003816 __ pop(left);
3817
3818 Label done, fail, ok;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003819 __ cmp(left, right);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003820 __ j(equal, &ok);
3821 // Fail if either is a non-HeapObject.
3822 __ mov(tmp, left);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003823 __ and_(tmp, right);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003824 __ JumpIfSmi(tmp, &fail);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003825 __ mov(tmp, FieldOperand(left, HeapObject::kMapOffset));
3826 __ CmpInstanceType(tmp, JS_REGEXP_TYPE);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003827 __ j(not_equal, &fail);
3828 __ cmp(tmp, FieldOperand(right, HeapObject::kMapOffset));
3829 __ j(not_equal, &fail);
3830 __ mov(tmp, FieldOperand(left, JSRegExp::kDataOffset));
3831 __ cmp(tmp, FieldOperand(right, JSRegExp::kDataOffset));
3832 __ j(equal, &ok);
3833 __ bind(&fail);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003834 __ mov(eax, Immediate(isolate()->factory()->false_value()));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003835 __ jmp(&done);
3836 __ bind(&ok);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003837 __ mov(eax, Immediate(isolate()->factory()->true_value()));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003838 __ bind(&done);
3839
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003840 context()->Plug(eax);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003841}
3842
3843
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003844void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3845 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003846 ASSERT(args->length() == 1);
3847
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003848 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003849
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003850 __ AssertString(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003851
3852 Label materialize_true, materialize_false;
3853 Label* if_true = NULL;
3854 Label* if_false = NULL;
3855 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003856 context()->PrepareTest(&materialize_true, &materialize_false,
3857 &if_true, &if_false, &fall_through);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003858
3859 __ test(FieldOperand(eax, String::kHashFieldOffset),
3860 Immediate(String::kContainsCachedArrayIndexMask));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003861 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003862 Split(zero, if_true, if_false, fall_through);
3863
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003864 context()->Plug(if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003865}
3866
3867
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003868void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3869 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003870 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003871 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003872
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003873 __ AssertString(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003874
3875 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
3876 __ IndexFromHash(eax, eax);
3877
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003878 context()->Plug(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003879}
3880
3881
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003882void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003883 Label bailout, done, one_char_separator, long_separator,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003884 non_trivial_array, not_size_one_array, loop,
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003885 loop_1, loop_1_condition, loop_2, loop_2_entry, loop_3, loop_3_entry;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003886
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003887 ZoneList<Expression*>* args = expr->arguments();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003888 ASSERT(args->length() == 2);
3889 // We will leave the separator on the stack until the end of the function.
3890 VisitForStackValue(args->at(1));
3891 // Load this to eax (= array)
3892 VisitForAccumulatorValue(args->at(0));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003893 // All aliases of the same register have disjoint lifetimes.
3894 Register array = eax;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003895 Register elements = no_reg; // Will be eax.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003896
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003897 Register index = edx;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003898
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003899 Register string_length = ecx;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003900
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003901 Register string = esi;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003902
3903 Register scratch = ebx;
3904
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003905 Register array_length = edi;
3906 Register result_pos = no_reg; // Will be edi.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003907
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003908 // Separator operand is already pushed.
3909 Operand separator_operand = Operand(esp, 2 * kPointerSize);
3910 Operand result_operand = Operand(esp, 1 * kPointerSize);
3911 Operand array_length_operand = Operand(esp, 0);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003912 __ sub(esp, Immediate(2 * kPointerSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003913 __ cld();
3914 // Check that the array is a JSArray
whesse@chromium.org7b260152011-06-20 15:33:18 +00003915 __ JumpIfSmi(array, &bailout);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003916 __ CmpObjectType(array, JS_ARRAY_TYPE, scratch);
3917 __ j(not_equal, &bailout);
3918
3919 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003920 __ CheckFastElements(scratch, &bailout);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003921
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003922 // If the array has length zero, return the empty string.
3923 __ mov(array_length, FieldOperand(array, JSArray::kLengthOffset));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003924 __ SmiUntag(array_length);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003925 __ j(not_zero, &non_trivial_array);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003926 __ mov(result_operand, isolate()->factory()->empty_string());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003927 __ jmp(&done);
3928
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003929 // Save the array length.
3930 __ bind(&non_trivial_array);
3931 __ mov(array_length_operand, array_length);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003932
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003933 // Save the FixedArray containing array's elements.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003934 // End of array's live range.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003935 elements = array;
3936 __ mov(elements, FieldOperand(array, JSArray::kElementsOffset));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003937 array = no_reg;
3938
3939
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003940 // Check that all array elements are sequential ASCII strings, and
3941 // accumulate the sum of their lengths, as a smi-encoded value.
3942 __ Set(index, Immediate(0));
3943 __ Set(string_length, Immediate(0));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003944 // Loop condition: while (index < length).
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003945 // Live loop registers: index, array_length, string,
3946 // scratch, string_length, elements.
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00003947 if (generate_debug_code_) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003948 __ cmp(index, array_length);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003949 __ Assert(less, "No empty arrays here in EmitFastAsciiArrayJoin");
3950 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003951 __ bind(&loop);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003952 __ mov(string, FieldOperand(elements,
3953 index,
3954 times_pointer_size,
3955 FixedArray::kHeaderSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003956 __ JumpIfSmi(string, &bailout);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003957 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
3958 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
3959 __ and_(scratch, Immediate(
3960 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003961 __ cmp(scratch, kStringTag | kOneByteStringTag | kSeqStringTag);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003962 __ j(not_equal, &bailout);
3963 __ add(string_length,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003964 FieldOperand(string, SeqOneByteString::kLengthOffset));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003965 __ j(overflow, &bailout);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003966 __ add(index, Immediate(1));
3967 __ cmp(index, array_length);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003968 __ j(less, &loop);
3969
3970 // If array_length is 1, return elements[0], a string.
3971 __ cmp(array_length, 1);
3972 __ j(not_equal, &not_size_one_array);
3973 __ mov(scratch, FieldOperand(elements, FixedArray::kHeaderSize));
3974 __ mov(result_operand, scratch);
3975 __ jmp(&done);
3976
3977 __ bind(&not_size_one_array);
3978
3979 // End of array_length live range.
3980 result_pos = array_length;
3981 array_length = no_reg;
3982
3983 // Live registers:
3984 // string_length: Sum of string lengths, as a smi.
3985 // elements: FixedArray of strings.
3986
3987 // Check that the separator is a flat ASCII string.
3988 __ mov(string, separator_operand);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003989 __ JumpIfSmi(string, &bailout);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003990 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
3991 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003992 __ and_(scratch, Immediate(
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003993 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003994 __ cmp(scratch, ASCII_STRING_TYPE);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003995 __ j(not_equal, &bailout);
3996
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003997 // Add (separator length times array_length) - separator length
3998 // to string_length.
3999 __ mov(scratch, separator_operand);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004000 __ mov(scratch, FieldOperand(scratch, SeqOneByteString::kLengthOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004001 __ sub(string_length, scratch); // May be negative, temporarily.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004002 __ imul(scratch, array_length_operand);
4003 __ j(overflow, &bailout);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004004 __ add(string_length, scratch);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004005 __ j(overflow, &bailout);
4006
4007 __ shr(string_length, 1);
4008 // Live registers and stack values:
4009 // string_length
4010 // elements
4011 __ AllocateAsciiString(result_pos, string_length, scratch,
4012 index, string, &bailout);
4013 __ mov(result_operand, result_pos);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004014 __ lea(result_pos, FieldOperand(result_pos, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004015
4016
4017 __ mov(string, separator_operand);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004018 __ cmp(FieldOperand(string, SeqOneByteString::kLengthOffset),
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004019 Immediate(Smi::FromInt(1)));
4020 __ j(equal, &one_char_separator);
4021 __ j(greater, &long_separator);
4022
4023
4024 // Empty separator case
4025 __ mov(index, Immediate(0));
4026 __ jmp(&loop_1_condition);
4027 // Loop condition: while (index < length).
4028 __ bind(&loop_1);
4029 // Each iteration of the loop concatenates one string to the result.
4030 // Live values in registers:
4031 // index: which element of the elements array we are adding to the result.
4032 // result_pos: the position to which we are currently copying characters.
4033 // elements: the FixedArray of strings we are joining.
4034
4035 // Get string = array[index].
4036 __ mov(string, FieldOperand(elements, index,
4037 times_pointer_size,
4038 FixedArray::kHeaderSize));
4039 __ mov(string_length,
4040 FieldOperand(string, String::kLengthOffset));
4041 __ shr(string_length, 1);
4042 __ lea(string,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004043 FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004044 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004045 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004046 __ bind(&loop_1_condition);
4047 __ cmp(index, array_length_operand);
4048 __ j(less, &loop_1); // End while (index < length).
4049 __ jmp(&done);
4050
4051
4052
4053 // One-character separator case
4054 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00004055 // Replace separator with its ASCII character value.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004056 __ mov_b(scratch, FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004057 __ mov_b(separator_operand, scratch);
4058
4059 __ Set(index, Immediate(0));
4060 // Jump into the loop after the code that copies the separator, so the first
4061 // element is not preceded by a separator
4062 __ jmp(&loop_2_entry);
4063 // Loop condition: while (index < length).
4064 __ bind(&loop_2);
4065 // Each iteration of the loop concatenates one string to the result.
4066 // Live values in registers:
4067 // index: which element of the elements array we are adding to the result.
4068 // result_pos: the position to which we are currently copying characters.
4069
4070 // Copy the separator character to the result.
4071 __ mov_b(scratch, separator_operand);
4072 __ mov_b(Operand(result_pos, 0), scratch);
4073 __ inc(result_pos);
4074
4075 __ bind(&loop_2_entry);
4076 // Get string = array[index].
4077 __ mov(string, FieldOperand(elements, index,
4078 times_pointer_size,
4079 FixedArray::kHeaderSize));
4080 __ mov(string_length,
4081 FieldOperand(string, String::kLengthOffset));
4082 __ shr(string_length, 1);
4083 __ lea(string,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004084 FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004085 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004086 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004087
4088 __ cmp(index, array_length_operand);
4089 __ j(less, &loop_2); // End while (index < length).
4090 __ jmp(&done);
4091
4092
4093 // Long separator case (separator is more than one character).
4094 __ bind(&long_separator);
4095
4096 __ Set(index, Immediate(0));
4097 // Jump into the loop after the code that copies the separator, so the first
4098 // element is not preceded by a separator
4099 __ jmp(&loop_3_entry);
4100 // Loop condition: while (index < length).
4101 __ bind(&loop_3);
4102 // Each iteration of the loop concatenates one string to the result.
4103 // Live values in registers:
4104 // index: which element of the elements array we are adding to the result.
4105 // result_pos: the position to which we are currently copying characters.
4106
4107 // Copy the separator to the result.
4108 __ mov(string, separator_operand);
4109 __ mov(string_length,
4110 FieldOperand(string, String::kLengthOffset));
4111 __ shr(string_length, 1);
4112 __ lea(string,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004113 FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004114 __ CopyBytes(string, result_pos, string_length, scratch);
4115
4116 __ bind(&loop_3_entry);
4117 // Get string = array[index].
4118 __ mov(string, FieldOperand(elements, index,
4119 times_pointer_size,
4120 FixedArray::kHeaderSize));
4121 __ mov(string_length,
4122 FieldOperand(string, String::kLengthOffset));
4123 __ shr(string_length, 1);
4124 __ lea(string,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004125 FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004126 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004127 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004128
4129 __ cmp(index, array_length_operand);
4130 __ j(less, &loop_3); // End while (index < length).
4131 __ jmp(&done);
4132
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00004133
4134 __ bind(&bailout);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004135 __ mov(result_operand, isolate()->factory()->undefined_value());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00004136 __ bind(&done);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004137 __ mov(eax, result_operand);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00004138 // Drop temp values from the stack, and restore context register.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004139 __ add(esp, Immediate(3 * kPointerSize));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00004140
4141 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
4142 context()->Plug(eax);
4143}
4144
4145
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004146void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004147 Handle<String> name = expr->name();
4148 if (name->length() > 0 && name->Get(0) == '_') {
4149 Comment cmnt(masm_, "[ InlineRuntimeCall");
4150 EmitInlineRuntimeCall(expr);
4151 return;
4152 }
4153
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004154 Comment cmnt(masm_, "[ CallRuntime");
4155 ZoneList<Expression*>* args = expr->arguments();
4156
4157 if (expr->is_jsruntime()) {
4158 // Prepare for calling JS runtime function.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004159 __ mov(eax, GlobalObjectOperand());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004160 __ push(FieldOperand(eax, GlobalObject::kBuiltinsOffset));
4161 }
4162
4163 // Push the arguments ("left-to-right").
4164 int arg_count = args->length();
4165 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004166 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004167 }
4168
4169 if (expr->is_jsruntime()) {
4170 // Call the JS runtime function via a call IC.
4171 __ Set(ecx, Immediate(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00004172 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
lrn@chromium.org34e60782011-09-15 07:25:40 +00004173 Handle<Code> ic =
4174 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004175 CallIC(ic, mode, expr->CallRuntimeFeedbackId());
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004176 // Restore context register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004177 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
4178 } else {
4179 // Call the C runtime function.
4180 __ CallRuntime(expr->function(), arg_count);
4181 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004182 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004183}
4184
4185
4186void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
4187 switch (expr->op()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004188 case Token::DELETE: {
4189 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004190 Property* property = expr->expression()->AsProperty();
4191 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004192
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004193 if (property != NULL) {
4194 VisitForStackValue(property->obj());
4195 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004196 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
4197 ? kNonStrictMode : kStrictMode;
4198 __ push(Immediate(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004199 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004200 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004201 } else if (proxy != NULL) {
4202 Variable* var = proxy->var();
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004203 // Delete of an unqualified identifier is disallowed in strict mode
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004204 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004205 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004206 if (var->IsUnallocated()) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004207 __ push(GlobalObjectOperand());
4208 __ push(Immediate(var->name()));
4209 __ push(Immediate(Smi::FromInt(kNonStrictMode)));
4210 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4211 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004212 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
4213 // Result of deleting non-global variables is false. 'this' is
4214 // not really a variable, though we implement it as one. The
4215 // subexpression does not have side effects.
4216 context()->Plug(var->is_this());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004217 } else {
4218 // Non-global variable. Call the runtime to try to delete from the
4219 // context where the variable was introduced.
4220 __ push(context_register());
4221 __ push(Immediate(var->name()));
4222 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
4223 context()->Plug(eax);
4224 }
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00004225 } else {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004226 // Result of deleting non-property, non-variable reference is true.
4227 // The subexpression may have side effects.
4228 VisitForEffect(expr->expression());
4229 context()->Plug(true);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004230 }
4231 break;
4232 }
4233
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004234 case Token::VOID: {
4235 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4236 VisitForEffect(expr->expression());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004237 context()->Plug(isolate()->factory()->undefined_value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004238 break;
4239 }
4240
4241 case Token::NOT: {
4242 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00004243 if (context()->IsEffect()) {
4244 // Unary NOT has no side effects so it's only necessary to visit the
4245 // subexpression. Match the optimizing compiler by not branching.
4246 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004247 } else if (context()->IsTest()) {
4248 const TestContext* test = TestContext::cast(context());
4249 // The labels are swapped for the recursive call.
4250 VisitForControl(expr->expression(),
4251 test->false_label(),
4252 test->true_label(),
4253 test->fall_through());
4254 context()->Plug(test->true_label(), test->false_label());
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00004255 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004256 // We handle value contexts explicitly rather than simply visiting
4257 // for control and plugging the control flow into the context,
4258 // because we need to prepare a pair of extra administrative AST ids
4259 // for the optimizing compiler.
4260 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
4261 Label materialize_true, materialize_false, done;
4262 VisitForControl(expr->expression(),
4263 &materialize_false,
4264 &materialize_true,
4265 &materialize_true);
4266 __ bind(&materialize_true);
4267 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4268 if (context()->IsAccumulatorValue()) {
4269 __ mov(eax, isolate()->factory()->true_value());
4270 } else {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00004271 __ Push(isolate()->factory()->true_value());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004272 }
4273 __ jmp(&done, Label::kNear);
4274 __ bind(&materialize_false);
4275 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4276 if (context()->IsAccumulatorValue()) {
4277 __ mov(eax, isolate()->factory()->false_value());
4278 } else {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00004279 __ Push(isolate()->factory()->false_value());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004280 }
4281 __ bind(&done);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00004282 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004283 break;
4284 }
4285
4286 case Token::TYPEOF: {
4287 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004288 { StackValueContext context(this);
4289 VisitForTypeofValue(expr->expression());
4290 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004291 __ CallRuntime(Runtime::kTypeof, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004292 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004293 break;
4294 }
4295
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004296 case Token::SUB:
4297 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004298 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004299
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004300 case Token::BIT_NOT:
4301 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004302 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004303
4304 default:
4305 UNREACHABLE();
4306 }
4307}
4308
4309
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004310void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
4311 const char* comment) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004312 Comment cmt(masm_, comment);
4313 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
4314 UnaryOverwriteMode overwrite =
4315 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004316 UnaryOpStub stub(expr->op(), overwrite);
4317 // UnaryOpStub expects the argument to be in the
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004318 // accumulator register eax.
4319 VisitForAccumulatorValue(expr->expression());
4320 SetSourcePosition(expr->position());
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004321 CallIC(stub.GetCode(isolate()), RelocInfo::CODE_TARGET,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004322 expr->UnaryOperationFeedbackId());
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004323 context()->Plug(eax);
4324}
4325
4326
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004327void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
4328 Comment cmnt(masm_, "[ CountOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00004329 SetSourcePosition(expr->position());
4330
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004331 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
4332 // as the left-hand side.
4333 if (!expr->expression()->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004334 VisitForEffect(expr->expression());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004335 return;
4336 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004337
4338 // Expression can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00004339 // slot.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004340 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
4341 LhsKind assign_type = VARIABLE;
4342 Property* prop = expr->expression()->AsProperty();
4343 // In case of a property we use the uninitialized expression context
4344 // of the key to detect a named property.
4345 if (prop != NULL) {
4346 assign_type =
4347 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
4348 }
4349
4350 // Evaluate expression and get value.
4351 if (assign_type == VARIABLE) {
4352 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004353 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00004354 EmitVariableLoad(expr->expression()->AsVariableProxy());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004355 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004356 // Reserve space for result of postfix operation.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004357 if (expr->is_postfix() && !context()->IsEffect()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004358 __ push(Immediate(Smi::FromInt(0)));
4359 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004360 if (assign_type == NAMED_PROPERTY) {
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004361 // Put the object both on the stack and in edx.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004362 VisitForAccumulatorValue(prop->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00004363 __ push(eax);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004364 __ mov(edx, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004365 EmitNamedPropertyLoad(prop);
4366 } else {
whesse@chromium.org7b260152011-06-20 15:33:18 +00004367 VisitForStackValue(prop->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004368 VisitForStackValue(prop->key());
4369 __ mov(edx, Operand(esp, kPointerSize)); // Object.
4370 __ mov(ecx, Operand(esp, 0)); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004371 EmitKeyedPropertyLoad(prop);
4372 }
4373 }
4374
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004375 // We need a second deoptimization point after loading the value
4376 // in case evaluating the property load my have a side effect.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004377 if (assign_type == VARIABLE) {
4378 PrepareForBailout(expr->expression(), TOS_REG);
4379 } else {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004380 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004381 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004382
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004383 // Call ToNumber only if operand is not a smi.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004384 Label no_conversion;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004385 if (ShouldInlineSmiCase(expr->op())) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00004386 __ JumpIfSmi(eax, &no_conversion, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004387 }
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004388 ToNumberStub convert_stub;
4389 __ CallStub(&convert_stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004390 __ bind(&no_conversion);
4391
4392 // Save result for postfix expressions.
4393 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004394 if (!context()->IsEffect()) {
4395 // Save the result on the stack. If we have a named or keyed property
4396 // we store the result under the receiver that is currently on top
4397 // of the stack.
4398 switch (assign_type) {
4399 case VARIABLE:
4400 __ push(eax);
4401 break;
4402 case NAMED_PROPERTY:
4403 __ mov(Operand(esp, kPointerSize), eax);
4404 break;
4405 case KEYED_PROPERTY:
4406 __ mov(Operand(esp, 2 * kPointerSize), eax);
4407 break;
4408 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004409 }
4410 }
4411
4412 // Inline smi case if we are in a loop.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004413 Label done, stub_call;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004414 JumpPatchSite patch_site(masm_);
4415
ricow@chromium.org65fae842010-08-25 15:26:24 +00004416 if (ShouldInlineSmiCase(expr->op())) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004417 if (expr->op() == Token::INC) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004418 __ add(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004419 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004420 __ sub(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004421 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004422 __ j(overflow, &stub_call, Label::kNear);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004423 // We could eliminate this smi check if we split the code at
4424 // the first smi check before calling ToNumber.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004425 patch_site.EmitJumpIfSmi(eax, &done, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004426
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004427 __ bind(&stub_call);
4428 // Call stub. Undo operation first.
4429 if (expr->op() == Token::INC) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004430 __ sub(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004431 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004432 __ add(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004433 }
4434 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004435
4436 // Record position before stub call.
4437 SetSourcePosition(expr->position());
4438
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004439 // Call stub for +1/-1.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004440 __ mov(edx, eax);
4441 __ mov(eax, Immediate(Smi::FromInt(1)));
danno@chromium.org40cb8782011-05-25 07:58:50 +00004442 BinaryOpStub stub(expr->binary_op(), NO_OVERWRITE);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004443 CallIC(stub.GetCode(isolate()),
4444 RelocInfo::CODE_TARGET,
4445 expr->CountBinOpFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004446 patch_site.EmitPatchInfo();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004447 __ bind(&done);
4448
4449 // Store the value returned in eax.
4450 switch (assign_type) {
4451 case VARIABLE:
4452 if (expr->is_postfix()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004453 // Perform the assignment as if via '='.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004454 { EffectContext context(this);
4455 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4456 Token::ASSIGN);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004457 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4458 context.Plug(eax);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004459 }
4460 // For all contexts except EffectContext We have the result on
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004461 // top of the stack.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004462 if (!context()->IsEffect()) {
4463 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004464 }
4465 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004466 // Perform the assignment as if via '='.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004467 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004468 Token::ASSIGN);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004469 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4470 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004471 }
4472 break;
4473 case NAMED_PROPERTY: {
4474 __ mov(ecx, prop->key()->AsLiteral()->handle());
4475 __ pop(edx);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004476 Handle<Code> ic = is_classic_mode()
4477 ? isolate()->builtins()->StoreIC_Initialize()
4478 : isolate()->builtins()->StoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004479 CallIC(ic, RelocInfo::CODE_TARGET, expr->CountStoreFeedbackId());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004480 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004481 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004482 if (!context()->IsEffect()) {
4483 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004484 }
4485 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004486 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004487 }
4488 break;
4489 }
4490 case KEYED_PROPERTY: {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004491 __ pop(ecx);
4492 __ pop(edx);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004493 Handle<Code> ic = is_classic_mode()
4494 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4495 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004496 CallIC(ic, RelocInfo::CODE_TARGET, expr->CountStoreFeedbackId());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004497 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004498 if (expr->is_postfix()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004499 // Result is on the stack
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004500 if (!context()->IsEffect()) {
4501 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004502 }
4503 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004504 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004505 }
4506 break;
4507 }
4508 }
4509}
4510
4511
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004512void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004513 VariableProxy* proxy = expr->AsVariableProxy();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004514 ASSERT(!context()->IsEffect());
4515 ASSERT(!context()->IsTest());
4516
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004517 if (proxy != NULL && proxy->var()->IsUnallocated()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004518 Comment cmnt(masm_, "Global variable");
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004519 __ mov(edx, GlobalObjectOperand());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004520 __ mov(ecx, Immediate(proxy->name()));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004521 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004522 // Use a regular load, not a contextual load, to avoid a reference
4523 // error.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004524 CallIC(ic);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004525 PrepareForBailout(expr, TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004526 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004527 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004528 Label done, slow;
4529
4530 // Generate code for loading from variables potentially shadowed
4531 // by eval-introduced variables.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004532 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004533
4534 __ bind(&slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004535 __ push(esi);
4536 __ push(Immediate(proxy->name()));
4537 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004538 PrepareForBailout(expr, TOS_REG);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004539 __ bind(&done);
4540
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004541 context()->Plug(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004542 } else {
4543 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004544 VisitInDuplicateContext(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004545 }
4546}
4547
4548
ager@chromium.org04921a82011-06-27 13:21:41 +00004549void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004550 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004551 Handle<String> check) {
4552 Label materialize_true, materialize_false;
4553 Label* if_true = NULL;
4554 Label* if_false = NULL;
4555 Label* fall_through = NULL;
4556 context()->PrepareTest(&materialize_true, &materialize_false,
4557 &if_true, &if_false, &fall_through);
4558
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004559 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004560 VisitForTypeofValue(sub_expr);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004561 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004562 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004563
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004564 if (check->Equals(isolate()->heap()->number_string())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004565 __ JumpIfSmi(eax, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004566 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004567 isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004568 Split(equal, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004569 } else if (check->Equals(isolate()->heap()->string_string())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004570 __ JumpIfSmi(eax, if_false);
4571 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edx);
4572 __ j(above_equal, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004573 // Check for undetectable objects => false.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004574 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
4575 1 << Map::kIsUndetectable);
4576 Split(zero, if_true, if_false, fall_through);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00004577 } else if (check->Equals(isolate()->heap()->symbol_string())) {
4578 __ JumpIfSmi(eax, if_false);
4579 __ CmpObjectType(eax, SYMBOL_TYPE, edx);
4580 Split(equal, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004581 } else if (check->Equals(isolate()->heap()->boolean_string())) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004582 __ cmp(eax, isolate()->factory()->true_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004583 __ j(equal, if_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004584 __ cmp(eax, isolate()->factory()->false_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004585 Split(equal, if_true, if_false, fall_through);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004586 } else if (FLAG_harmony_typeof &&
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004587 check->Equals(isolate()->heap()->null_string())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004588 __ cmp(eax, isolate()->factory()->null_value());
4589 Split(equal, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004590 } else if (check->Equals(isolate()->heap()->undefined_string())) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004591 __ cmp(eax, isolate()->factory()->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004592 __ j(equal, if_true);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004593 __ JumpIfSmi(eax, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004594 // Check for undetectable objects => true.
4595 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
4596 __ movzx_b(ecx, FieldOperand(edx, Map::kBitFieldOffset));
4597 __ test(ecx, Immediate(1 << Map::kIsUndetectable));
4598 Split(not_zero, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004599 } else if (check->Equals(isolate()->heap()->function_string())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004600 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004601 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4602 __ CmpObjectType(eax, JS_FUNCTION_TYPE, edx);
4603 __ j(equal, if_true);
4604 __ CmpInstanceType(edx, JS_FUNCTION_PROXY_TYPE);
4605 Split(equal, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004606 } else if (check->Equals(isolate()->heap()->object_string())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004607 __ JumpIfSmi(eax, if_false);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004608 if (!FLAG_harmony_typeof) {
4609 __ cmp(eax, isolate()->factory()->null_value());
4610 __ j(equal, if_true);
4611 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004612 __ CmpObjectType(eax, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE, edx);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004613 __ j(below, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004614 __ CmpInstanceType(edx, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
4615 __ j(above, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004616 // Check for undetectable objects => false.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004617 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
4618 1 << Map::kIsUndetectable);
4619 Split(zero, if_true, if_false, fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004620 } else {
4621 if (if_false != fall_through) __ jmp(if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004622 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004623 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004624}
4625
4626
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004627void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
4628 Comment cmnt(masm_, "[ CompareOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00004629 SetSourcePosition(expr->position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004630
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004631 // First we try a fast inlined version of the compare when one of
4632 // the operands is a literal.
4633 if (TryLiteralCompare(expr)) return;
4634
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004635 // Always perform the comparison for its control flow. Pack the result
4636 // into the expression's context after the comparison is performed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004637 Label materialize_true, materialize_false;
4638 Label* if_true = NULL;
4639 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004640 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004641 context()->PrepareTest(&materialize_true, &materialize_false,
4642 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004643
ager@chromium.org04921a82011-06-27 13:21:41 +00004644 Token::Value op = expr->op();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004645 VisitForStackValue(expr->left());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004646 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004647 case Token::IN:
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004648 VisitForStackValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004649 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004650 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004651 __ cmp(eax, isolate()->factory()->true_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004652 Split(equal, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004653 break;
4654
4655 case Token::INSTANCEOF: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004656 VisitForStackValue(expr->right());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004657 InstanceofStub stub(InstanceofStub::kNoFlags);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004658 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004659 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004660 __ test(eax, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004661 // The stub returns 0 for true.
4662 Split(zero, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004663 break;
4664 }
4665
4666 default: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004667 VisitForAccumulatorValue(expr->right());
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004668 Condition cc = CompareIC::ComputeCondition(op);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004669 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004670
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004671 bool inline_smi_code = ShouldInlineSmiCase(op);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004672 JumpPatchSite patch_site(masm_);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004673 if (inline_smi_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004674 Label slow_case;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004675 __ mov(ecx, edx);
4676 __ or_(ecx, eax);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004677 patch_site.EmitJumpIfNotSmi(ecx, &slow_case, Label::kNear);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004678 __ cmp(edx, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004679 Split(cc, if_true, if_false, NULL);
4680 __ bind(&slow_case);
4681 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004682
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004683 // Record position and call the compare IC.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004684 SetSourcePosition(expr->position());
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004685 Handle<Code> ic = CompareIC::GetUninitialized(isolate(), op);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004686 CallIC(ic, RelocInfo::CODE_TARGET, expr->CompareOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004687 patch_site.EmitPatchInfo();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004688
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004689 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004690 __ test(eax, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004691 Split(cc, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004692 }
4693 }
4694
4695 // Convert the result of the comparison into one expected for this
4696 // expression's context.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004697 context()->Plug(if_true, if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004698}
4699
4700
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004701void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4702 Expression* sub_expr,
4703 NilValue nil) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004704 Label materialize_true, materialize_false;
4705 Label* if_true = NULL;
4706 Label* if_false = NULL;
4707 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004708 context()->PrepareTest(&materialize_true, &materialize_false,
4709 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004710
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004711 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004712 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004713
4714 EqualityKind kind = expr->op() == Token::EQ_STRICT
4715 ? kStrictEquality : kNonStrictEquality;
4716 Handle<Object> nil_value = nil == kNullValue
4717 ? isolate()->factory()->null_value()
4718 : isolate()->factory()->undefined_value();
4719 if (kind == kStrictEquality) {
4720 __ cmp(eax, nil_value);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004721 Split(equal, if_true, if_false, fall_through);
4722 } else {
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004723 Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(),
4724 kNonStrictEquality,
4725 nil);
4726 CallIC(ic, RelocInfo::CODE_TARGET, expr->CompareOperationFeedbackId());
4727 __ test(eax, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004728 Split(not_zero, if_true, if_false, fall_through);
4729 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004730 context()->Plug(if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004731}
4732
4733
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004734void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
4735 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004736 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004737}
4738
4739
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00004740Register FullCodeGenerator::result_register() {
4741 return eax;
4742}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004743
4744
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00004745Register FullCodeGenerator::context_register() {
4746 return esi;
4747}
4748
4749
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004750void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
4751 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4752 __ mov(Operand(ebp, frame_offset), value);
4753}
4754
4755
4756void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004757 __ mov(dst, ContextOperand(esi, context_index));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004758}
4759
4760
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004761void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004762 Scope* declaration_scope = scope()->DeclarationScope();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00004763 if (declaration_scope->is_global_scope() ||
4764 declaration_scope->is_module_scope()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00004765 // Contexts nested in the native context have a canonical empty function
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004766 // as their closure, not the anonymous closure containing the global
4767 // code. Pass a smi sentinel and let the runtime look up the empty
4768 // function.
4769 __ push(Immediate(Smi::FromInt(0)));
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004770 } else if (declaration_scope->is_eval_scope()) {
4771 // Contexts nested inside eval code have the same closure as the context
4772 // calling eval, not the anonymous closure containing the eval code.
4773 // Fetch it from the context.
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004774 __ push(ContextOperand(esi, Context::CLOSURE_INDEX));
4775 } else {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004776 ASSERT(declaration_scope->is_function_scope());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004777 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
4778 }
4779}
4780
4781
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004782// ----------------------------------------------------------------------------
4783// Non-local control flow support.
4784
4785void FullCodeGenerator::EnterFinallyBlock() {
4786 // Cook return address on top of stack (smi encoded Code* delta)
4787 ASSERT(!result_register().is(edx));
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004788 __ pop(edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004789 __ sub(edx, Immediate(masm_->CodeObject()));
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00004790 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4791 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004792 __ SmiTag(edx);
4793 __ push(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004794
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004795 // Store result register while executing finally block.
4796 __ push(result_register());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004797
4798 // Store pending message while executing finally block.
4799 ExternalReference pending_message_obj =
4800 ExternalReference::address_of_pending_message_obj(isolate());
4801 __ mov(edx, Operand::StaticVariable(pending_message_obj));
4802 __ push(edx);
4803
4804 ExternalReference has_pending_message =
4805 ExternalReference::address_of_has_pending_message(isolate());
4806 __ mov(edx, Operand::StaticVariable(has_pending_message));
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004807 __ SmiTag(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004808 __ push(edx);
4809
4810 ExternalReference pending_message_script =
4811 ExternalReference::address_of_pending_message_script(isolate());
4812 __ mov(edx, Operand::StaticVariable(pending_message_script));
4813 __ push(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004814}
4815
4816
4817void FullCodeGenerator::ExitFinallyBlock() {
4818 ASSERT(!result_register().is(edx));
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004819 // Restore pending message from stack.
4820 __ pop(edx);
4821 ExternalReference pending_message_script =
4822 ExternalReference::address_of_pending_message_script(isolate());
4823 __ mov(Operand::StaticVariable(pending_message_script), edx);
4824
4825 __ pop(edx);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004826 __ SmiUntag(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004827 ExternalReference has_pending_message =
4828 ExternalReference::address_of_has_pending_message(isolate());
4829 __ mov(Operand::StaticVariable(has_pending_message), edx);
4830
4831 __ pop(edx);
4832 ExternalReference pending_message_obj =
4833 ExternalReference::address_of_pending_message_obj(isolate());
4834 __ mov(Operand::StaticVariable(pending_message_obj), edx);
4835
4836 // Restore result register from stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004837 __ pop(result_register());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004838
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004839 // Uncook return address.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004840 __ pop(edx);
4841 __ SmiUntag(edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004842 __ add(edx, Immediate(masm_->CodeObject()));
4843 __ jmp(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004844}
4845
4846
4847#undef __
4848
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004849#define __ ACCESS_MASM(masm())
4850
4851FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4852 int* stack_depth,
4853 int* context_length) {
4854 // The macros used here must preserve the result register.
4855
4856 // Because the handler block contains the context of the finally
4857 // code, we can restore it directly from there for the finally code
4858 // rather than iteratively unwinding contexts via their previous
4859 // links.
4860 __ Drop(*stack_depth); // Down to the handler block.
4861 if (*context_length > 0) {
4862 // Restore the context to its dedicated register and the stack.
4863 __ mov(esi, Operand(esp, StackHandlerConstants::kContextOffset));
4864 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
4865 }
4866 __ PopTryHandler();
4867 __ call(finally_entry_);
4868
4869 *stack_depth = 0;
4870 *context_length = 0;
4871 return previous_;
4872}
4873
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004874#undef __
4875
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004876} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004877
4878#endif // V8_TARGET_ARCH_IA32