blob: 1428dfe03af6bf7435e96e91dfa5cd94779559bb [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.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000164
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000165 { Comment cmnt(masm_, "[ Allocate locals");
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000166 int locals_count = info->scope()->num_stack_slots();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000167 if (locals_count == 1) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000168 __ push(Immediate(isolate()->factory()->undefined_value()));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000169 } else if (locals_count > 1) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000170 __ mov(eax, Immediate(isolate()->factory()->undefined_value()));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000171 for (int i = 0; i < locals_count; i++) {
172 __ push(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000173 }
174 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000175 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000176
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000177 bool function_in_register = true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000178
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000179 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000180 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000181 if (heap_slots > 0) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000182 Comment cmnt(masm_, "[ Allocate context");
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000183 // Argument to NewContext is the function, which is still in edi.
184 __ push(edi);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000185 if (FLAG_harmony_scoping && info->scope()->is_global_scope()) {
186 __ Push(info->scope()->GetScopeInfo());
187 __ CallRuntime(Runtime::kNewGlobalContext, 2);
188 } else if (heap_slots <= FastNewContextStub::kMaximumSlots) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000189 FastNewContextStub stub(heap_slots);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000190 __ CallStub(&stub);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000191 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000192 __ CallRuntime(Runtime::kNewFunctionContext, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000193 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000194 function_in_register = false;
195 // Context is returned in both eax and esi. It replaces the context
196 // passed to us. It's saved in the stack and kept live in esi.
197 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
198
199 // Copy parameters into context if necessary.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000200 int num_parameters = info->scope()->num_parameters();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000201 for (int i = 0; i < num_parameters; i++) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000202 Variable* var = scope()->parameter(i);
203 if (var->IsContextSlot()) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000204 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
205 (num_parameters - 1 - i) * kPointerSize;
206 // Load parameter from stack.
207 __ mov(eax, Operand(ebp, parameter_offset));
208 // Store it in the context.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000209 int context_offset = Context::SlotOffset(var->index());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000210 __ mov(Operand(esi, context_offset), eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000211 // Update the write barrier. This clobbers eax and ebx.
212 __ RecordWriteContextSlot(esi,
213 context_offset,
214 eax,
215 ebx,
216 kDontSaveFPRegs);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000217 }
218 }
219 }
220
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000221 Variable* arguments = scope()->arguments();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000222 if (arguments != NULL) {
223 // Function uses arguments object.
224 Comment cmnt(masm_, "[ Allocate arguments object");
225 if (function_in_register) {
226 __ push(edi);
227 } else {
228 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
229 }
230 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000231 int num_parameters = info->scope()->num_parameters();
232 int offset = num_parameters * kPointerSize;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000233 __ lea(edx,
234 Operand(ebp, StandardFrameConstants::kCallerSPOffset + offset));
235 __ push(edx);
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000236 __ push(Immediate(Smi::FromInt(num_parameters)));
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000237 // Arguments to ArgumentsAccessStub:
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000238 // function, receiver address, parameter count.
239 // The stub will rewrite receiver and parameter count if the previous
240 // stack frame was an arguments adapter frame.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000241 ArgumentsAccessStub::Type type;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000242 if (!is_classic_mode()) {
whesse@chromium.org7b260152011-06-20 15:33:18 +0000243 type = ArgumentsAccessStub::NEW_STRICT;
244 } else if (function()->has_duplicate_parameters()) {
245 type = ArgumentsAccessStub::NEW_NON_STRICT_SLOW;
246 } else {
247 type = ArgumentsAccessStub::NEW_NON_STRICT_FAST;
248 }
249 ArgumentsAccessStub stub(type);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000250 __ CallStub(&stub);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000251
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000252 SetVar(arguments, eax, ebx, edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000253 }
254
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000255 if (FLAG_trace) {
256 __ CallRuntime(Runtime::kTraceEnter, 0);
257 }
258
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000259 // Visit the declarations and body unless there is an illegal
260 // redeclaration.
261 if (scope()->HasIllegalRedeclaration()) {
262 Comment cmnt(masm_, "[ Declarations");
263 scope()->VisitIllegalRedeclaration(this);
264
265 } else {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000266 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000267 { Comment cmnt(masm_, "[ Declarations");
268 // For named function expressions, declare the function name as a
269 // constant.
270 if (scope()->is_function_scope() && scope()->function() != NULL) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000271 VariableDeclaration* function = scope()->function();
272 ASSERT(function->proxy()->var()->mode() == CONST ||
273 function->proxy()->var()->mode() == CONST_HARMONY);
274 ASSERT(function->proxy()->var()->location() != Variable::UNALLOCATED);
275 VisitVariableDeclaration(function);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000276 }
277 VisitDeclarations(scope()->declarations());
278 }
279
280 { Comment cmnt(masm_, "[ Stack check");
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000281 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000282 Label ok;
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000283 ExternalReference stack_limit =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000284 ExternalReference::address_of_stack_limit(isolate());
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000285 __ cmp(esp, Operand::StaticVariable(stack_limit));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000286 __ j(above_equal, &ok, Label::kNear);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000287 StackCheckStub stub;
288 __ CallStub(&stub);
289 __ bind(&ok);
290 }
291
292 { Comment cmnt(masm_, "[ Body");
293 ASSERT(loop_depth() == 0);
294 VisitStatements(function()->body());
295 ASSERT(loop_depth() == 0);
296 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000297 }
298
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000299 // Always emit a 'return undefined' in case control fell off the end of
300 // the body.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000301 { Comment cmnt(masm_, "[ return <undefined>;");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000302 __ mov(eax, isolate()->factory()->undefined_value());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000303 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000304 }
305}
306
307
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000308void FullCodeGenerator::ClearAccumulator() {
309 __ Set(eax, Immediate(Smi::FromInt(0)));
310}
311
312
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000313void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
314 __ mov(ebx, Immediate(profiling_counter_));
315 __ sub(FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset),
316 Immediate(Smi::FromInt(delta)));
317}
318
319
320void FullCodeGenerator::EmitProfilingCounterReset() {
321 int reset_value = FLAG_interrupt_budget;
322 if (info_->ShouldSelfOptimize() && !FLAG_retry_self_opt) {
323 // Self-optimization is a one-off thing: if it fails, don't try again.
324 reset_value = Smi::kMaxValue;
325 }
326 __ mov(ebx, Immediate(profiling_counter_));
327 __ mov(FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset),
328 Immediate(Smi::FromInt(reset_value)));
329}
330
331
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000332void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
333 Label* back_edge_target) {
334 Comment cmnt(masm_, "[ Back edge bookkeeping");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000335 Label ok;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000336
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000337 int weight = 1;
338 if (FLAG_weighted_back_edges) {
339 ASSERT(back_edge_target->is_bound());
340 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
341 weight = Min(kMaxBackEdgeWeight,
342 Max(1, distance / kBackEdgeDistanceUnit));
yangguo@chromium.org56454712012-02-16 15:33:53 +0000343 }
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000344 EmitProfilingCounterDecrement(weight);
345 __ j(positive, &ok, Label::kNear);
346 InterruptStub stub;
347 __ CallStub(&stub);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000348
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000349 // Record a mapping of this PC offset to the OSR id. This is used to find
350 // the AST id from the unoptimized code in order to use it as a key into
351 // the deoptimization input data found in the optimized code.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000352 RecordBackEdge(stmt->OsrEntryId());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000353
354 // Loop stack checks can be patched to perform on-stack replacement. In
355 // order to decide whether or not to perform OSR we embed the loop depth
356 // in a test instruction after the call so we can extract it from the OSR
357 // builtin.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000358 ASSERT(loop_depth() > 0);
359 __ test(eax, Immediate(Min(loop_depth(), Code::kMaxLoopNestingMarker)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000360
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000361 EmitProfilingCounterReset();
yangguo@chromium.org56454712012-02-16 15:33:53 +0000362
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000363 __ bind(&ok);
364 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
365 // Record a mapping of the OSR id to this PC. This is used if the OSR
366 // entry becomes the target of a bailout. We don't expect it to be, but
367 // we want it to work if it is.
368 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000369}
370
371
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000372void FullCodeGenerator::EmitReturnSequence() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000373 Comment cmnt(masm_, "[ Return sequence");
374 if (return_label_.is_bound()) {
375 __ jmp(&return_label_);
376 } else {
377 // Common return label
378 __ bind(&return_label_);
379 if (FLAG_trace) {
380 __ push(eax);
381 __ CallRuntime(Runtime::kTraceExit, 1);
382 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000383 if (FLAG_interrupt_at_exit || FLAG_self_optimization) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000384 // Pretend that the exit is a backwards jump to the entry.
385 int weight = 1;
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000386 if (info_->ShouldSelfOptimize()) {
387 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
388 } else if (FLAG_weighted_back_edges) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000389 int distance = masm_->pc_offset();
danno@chromium.org88aa0582012-03-23 15:11:57 +0000390 weight = Min(kMaxBackEdgeWeight,
danno@chromium.org129d3982012-07-25 15:01:47 +0000391 Max(1, distance / kBackEdgeDistanceUnit));
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000392 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000393 EmitProfilingCounterDecrement(weight);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000394 Label ok;
395 __ j(positive, &ok, Label::kNear);
396 __ push(eax);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000397 if (info_->ShouldSelfOptimize() && FLAG_direct_self_opt) {
398 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
399 __ CallRuntime(Runtime::kOptimizeFunctionOnNextCall, 1);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000400 } else {
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000401 InterruptStub stub;
402 __ CallStub(&stub);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000403 }
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000404 __ pop(eax);
405 EmitProfilingCounterReset();
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000406 __ bind(&ok);
407 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000408#ifdef DEBUG
409 // Add a label for checking the size of the code used for returning.
410 Label check_exit_codesize;
411 masm_->bind(&check_exit_codesize);
412#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000413 SetSourcePosition(function()->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000414 __ RecordJSReturn();
415 // Do not use the leave instruction here because it is too short to
416 // patch with the code required by the debugger.
417 __ mov(esp, ebp);
418 __ pop(ebp);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000419
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000420 int arguments_bytes = (info_->scope()->num_parameters() + 1) * kPointerSize;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000421 __ Ret(arguments_bytes, ecx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000422#ifdef ENABLE_DEBUGGER_SUPPORT
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000423 // Check that the size of the code used for returning is large enough
424 // for the debugger's requirements.
425 ASSERT(Assembler::kJSReturnSequenceLength <=
426 masm_->SizeOfCodeGeneratedSince(&check_exit_codesize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000427#endif
428 }
429}
430
431
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000432void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
433 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000434}
435
436
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000437void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
438 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
439 codegen()->GetVar(result_register(), var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000440}
441
442
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000443void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
444 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
445 MemOperand operand = codegen()->VarOperand(var, result_register());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000446 // Memory operands can be pushed directly.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000447 __ push(operand);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000448}
449
450
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000451void FullCodeGenerator::TestContext::Plug(Variable* var) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000452 // For simplicity we always test the accumulator register.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000453 codegen()->GetVar(result_register(), var);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000454 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000455 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000456}
457
458
459void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
460 UNREACHABLE(); // Not used on IA32.
461}
462
463
464void FullCodeGenerator::AccumulatorValueContext::Plug(
465 Heap::RootListIndex index) const {
466 UNREACHABLE(); // Not used on IA32.
467}
468
469
470void FullCodeGenerator::StackValueContext::Plug(
471 Heap::RootListIndex index) const {
472 UNREACHABLE(); // Not used on IA32.
473}
474
475
476void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
477 UNREACHABLE(); // Not used on IA32.
478}
479
480
481void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
482}
483
484
485void FullCodeGenerator::AccumulatorValueContext::Plug(
486 Handle<Object> lit) const {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000487 if (lit->IsSmi()) {
488 __ SafeSet(result_register(), Immediate(lit));
489 } else {
490 __ Set(result_register(), Immediate(lit));
491 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000492}
493
494
495void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000496 if (lit->IsSmi()) {
497 __ SafePush(Immediate(lit));
498 } else {
499 __ push(Immediate(lit));
500 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000501}
502
503
504void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000505 codegen()->PrepareForBailoutBeforeSplit(condition(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000506 true,
507 true_label_,
508 false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000509 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
510 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000511 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000512 } else if (lit->IsTrue() || lit->IsJSObject()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000513 if (true_label_ != fall_through_) __ jmp(true_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000514 } else if (lit->IsString()) {
515 if (String::cast(*lit)->length() == 0) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000516 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000517 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000518 if (true_label_ != fall_through_) __ jmp(true_label_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000519 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000520 } else if (lit->IsSmi()) {
521 if (Smi::cast(*lit)->value() == 0) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000522 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000523 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000524 if (true_label_ != fall_through_) __ jmp(true_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000525 }
526 } else {
527 // For simplicity we always test the accumulator register.
528 __ mov(result_register(), lit);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000529 codegen()->DoTest(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000530 }
531}
532
533
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000534void FullCodeGenerator::EffectContext::DropAndPlug(int count,
535 Register reg) const {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000536 ASSERT(count > 0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000537 __ Drop(count);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000538}
539
540
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000541void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
542 int count,
543 Register reg) const {
544 ASSERT(count > 0);
545 __ Drop(count);
546 __ Move(result_register(), reg);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000547}
548
549
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000550void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
551 Register reg) const {
552 ASSERT(count > 0);
553 if (count > 1) __ Drop(count - 1);
554 __ mov(Operand(esp, 0), reg);
555}
556
557
558void FullCodeGenerator::TestContext::DropAndPlug(int count,
559 Register reg) const {
560 ASSERT(count > 0);
561 // For simplicity we always test the accumulator register.
562 __ Drop(count);
563 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000564 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000565 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000566}
567
568
569void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
570 Label* materialize_false) const {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000571 ASSERT(materialize_true == materialize_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000572 __ bind(materialize_true);
573}
574
575
576void FullCodeGenerator::AccumulatorValueContext::Plug(
577 Label* materialize_true,
578 Label* materialize_false) const {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000579 Label done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000580 __ bind(materialize_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000581 __ mov(result_register(), isolate()->factory()->true_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000582 __ jmp(&done, Label::kNear);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000583 __ bind(materialize_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000584 __ mov(result_register(), isolate()->factory()->false_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000585 __ bind(&done);
586}
587
588
589void FullCodeGenerator::StackValueContext::Plug(
590 Label* materialize_true,
591 Label* materialize_false) const {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000592 Label done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000593 __ bind(materialize_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000594 __ push(Immediate(isolate()->factory()->true_value()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000595 __ jmp(&done, Label::kNear);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000596 __ bind(materialize_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000597 __ push(Immediate(isolate()->factory()->false_value()));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000598 __ bind(&done);
599}
600
601
602void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
603 Label* materialize_false) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000604 ASSERT(materialize_true == true_label_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000605 ASSERT(materialize_false == false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000606}
607
608
609void FullCodeGenerator::EffectContext::Plug(bool flag) const {
610}
611
612
613void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000614 Handle<Object> value = flag
615 ? isolate()->factory()->true_value()
616 : isolate()->factory()->false_value();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000617 __ mov(result_register(), value);
618}
619
620
621void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000622 Handle<Object> value = flag
623 ? isolate()->factory()->true_value()
624 : isolate()->factory()->false_value();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000625 __ push(Immediate(value));
626}
627
628
629void FullCodeGenerator::TestContext::Plug(bool flag) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000630 codegen()->PrepareForBailoutBeforeSplit(condition(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000631 true,
632 true_label_,
633 false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000634 if (flag) {
635 if (true_label_ != fall_through_) __ jmp(true_label_);
636 } else {
637 if (false_label_ != fall_through_) __ jmp(false_label_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000638 }
639}
640
641
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000642void FullCodeGenerator::DoTest(Expression* condition,
643 Label* if_true,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000644 Label* if_false,
645 Label* fall_through) {
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000646 ToBooleanStub stub(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000647 __ push(result_register());
ricow@chromium.org2c99e282011-07-28 09:15:17 +0000648 __ CallStub(&stub, condition->test_id());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000649 __ test(result_register(), result_register());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000650 // The stub returns nonzero for true.
651 Split(not_zero, if_true, if_false, fall_through);
652}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000653
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000654
ricow@chromium.org65fae842010-08-25 15:26:24 +0000655void FullCodeGenerator::Split(Condition cc,
656 Label* if_true,
657 Label* if_false,
658 Label* fall_through) {
659 if (if_false == fall_through) {
660 __ j(cc, if_true);
661 } else if (if_true == fall_through) {
662 __ j(NegateCondition(cc), if_false);
663 } else {
664 __ j(cc, if_true);
665 __ jmp(if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000666 }
667}
668
669
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000670MemOperand FullCodeGenerator::StackOperand(Variable* var) {
671 ASSERT(var->IsStackAllocated());
672 // Offset is negative because higher indexes are at lower addresses.
673 int offset = -var->index() * kPointerSize;
674 // Adjust by a (parameter or local) base offset.
675 if (var->IsParameter()) {
676 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
677 } else {
678 offset += JavaScriptFrameConstants::kLocal0Offset;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000679 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000680 return Operand(ebp, offset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000681}
682
683
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000684MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
685 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
686 if (var->IsContextSlot()) {
687 int context_chain_length = scope()->ContextChainLength(var->scope());
688 __ LoadContext(scratch, context_chain_length);
689 return ContextOperand(scratch, var->index());
690 } else {
691 return StackOperand(var);
692 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000693}
694
695
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000696void FullCodeGenerator::GetVar(Register dest, Variable* var) {
697 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
698 MemOperand location = VarOperand(var, dest);
699 __ mov(dest, location);
700}
701
702
703void FullCodeGenerator::SetVar(Variable* var,
704 Register src,
705 Register scratch0,
706 Register scratch1) {
707 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
708 ASSERT(!scratch0.is(src));
709 ASSERT(!scratch0.is(scratch1));
710 ASSERT(!scratch1.is(src));
711 MemOperand location = VarOperand(var, scratch0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000712 __ mov(location, src);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000713
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000714 // Emit the write barrier code if the location is in the heap.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000715 if (var->IsContextSlot()) {
716 int offset = Context::SlotOffset(var->index());
717 ASSERT(!scratch0.is(esi) && !src.is(esi) && !scratch1.is(esi));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000718 __ RecordWriteContextSlot(scratch0, offset, src, scratch1, kDontSaveFPRegs);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000719 }
720}
721
722
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000723void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000724 bool should_normalize,
725 Label* if_true,
726 Label* if_false) {
727 // Only prepare for bailouts before splits if we're in a test
728 // context. Otherwise, we let the Visit function deal with the
729 // preparation to avoid preparing with the same AST id twice.
730 if (!context()->IsTest() || !info_->IsOptimizable()) return;
731
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000732 Label skip;
733 if (should_normalize) __ jmp(&skip, Label::kNear);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000734 PrepareForBailout(expr, TOS_REG);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000735 if (should_normalize) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000736 __ cmp(eax, isolate()->factory()->true_value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000737 Split(equal, if_true, if_false, NULL);
738 __ bind(&skip);
739 }
740}
741
742
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000743void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000744 // The variable in the declaration always resides in the current context.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000745 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000746 if (generate_debug_code_) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000747 // Check that we're not inside a with or catch context.
748 __ mov(ebx, FieldOperand(esi, HeapObject::kMapOffset));
749 __ cmp(ebx, isolate()->factory()->with_context_map());
750 __ Check(not_equal, "Declaration in with context.");
751 __ cmp(ebx, isolate()->factory()->catch_context_map());
752 __ Check(not_equal, "Declaration in catch context.");
753 }
754}
755
756
757void FullCodeGenerator::VisitVariableDeclaration(
758 VariableDeclaration* declaration) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000759 // If it was not possible to allocate the variable at compile time, we
760 // need to "declare" it at runtime to make sure it actually exists in the
761 // local context.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000762 VariableProxy* proxy = declaration->proxy();
763 VariableMode mode = declaration->mode();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000764 Variable* variable = proxy->var();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000765 bool hole_init = mode == CONST || mode == CONST_HARMONY || mode == LET;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000766 switch (variable->location()) {
767 case Variable::UNALLOCATED:
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000768 globals_->Add(variable->name(), zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000769 globals_->Add(variable->binding_needs_init()
770 ? isolate()->factory()->the_hole_value()
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000771 : isolate()->factory()->undefined_value(), zone());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000772 break;
773
774 case Variable::PARAMETER:
775 case Variable::LOCAL:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000776 if (hole_init) {
777 Comment cmnt(masm_, "[ VariableDeclaration");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000778 __ mov(StackOperand(variable),
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000779 Immediate(isolate()->factory()->the_hole_value()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000780 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000781 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000782
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000783 case Variable::CONTEXT:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000784 if (hole_init) {
785 Comment cmnt(masm_, "[ VariableDeclaration");
786 EmitDebugCheckDeclarationContext(variable);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000787 __ mov(ContextOperand(esi, variable->index()),
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000788 Immediate(isolate()->factory()->the_hole_value()));
789 // No write barrier since the hole value is in old space.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000790 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000791 }
792 break;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000793
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000794 case Variable::LOOKUP: {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000795 Comment cmnt(masm_, "[ VariableDeclaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000796 __ push(esi);
797 __ push(Immediate(variable->name()));
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000798 // VariableDeclaration nodes are always introduced in one of four modes.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000799 ASSERT(IsDeclaredVariableMode(mode));
800 PropertyAttributes attr =
801 IsImmutableVariableMode(mode) ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000802 __ push(Immediate(Smi::FromInt(attr)));
803 // Push initial value, if any.
804 // Note: For variables we must not push an initial value (such as
805 // 'undefined') because we may have a (legal) redeclaration and we
806 // must not destroy the current value.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000807 if (hole_init) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000808 __ push(Immediate(isolate()->factory()->the_hole_value()));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000809 } else {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000810 __ push(Immediate(Smi::FromInt(0))); // Indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000811 }
812 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000813 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000814 }
815 }
816}
817
818
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000819void FullCodeGenerator::VisitFunctionDeclaration(
820 FunctionDeclaration* declaration) {
821 VariableProxy* proxy = declaration->proxy();
822 Variable* variable = proxy->var();
823 switch (variable->location()) {
824 case Variable::UNALLOCATED: {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000825 globals_->Add(variable->name(), zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000826 Handle<SharedFunctionInfo> function =
827 Compiler::BuildFunctionInfo(declaration->fun(), script());
828 // Check for stack-overflow exception.
829 if (function.is_null()) return SetStackOverflow();
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000830 globals_->Add(function, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000831 break;
832 }
833
834 case Variable::PARAMETER:
835 case Variable::LOCAL: {
836 Comment cmnt(masm_, "[ FunctionDeclaration");
837 VisitForAccumulatorValue(declaration->fun());
838 __ mov(StackOperand(variable), result_register());
839 break;
840 }
841
842 case Variable::CONTEXT: {
843 Comment cmnt(masm_, "[ FunctionDeclaration");
844 EmitDebugCheckDeclarationContext(variable);
845 VisitForAccumulatorValue(declaration->fun());
846 __ mov(ContextOperand(esi, variable->index()), result_register());
847 // We know that we have written a function, which is not a smi.
848 __ RecordWriteContextSlot(esi,
849 Context::SlotOffset(variable->index()),
850 result_register(),
851 ecx,
852 kDontSaveFPRegs,
853 EMIT_REMEMBERED_SET,
854 OMIT_SMI_CHECK);
855 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
856 break;
857 }
858
859 case Variable::LOOKUP: {
860 Comment cmnt(masm_, "[ FunctionDeclaration");
861 __ push(esi);
862 __ push(Immediate(variable->name()));
863 __ push(Immediate(Smi::FromInt(NONE)));
864 VisitForStackValue(declaration->fun());
865 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
866 break;
867 }
868 }
869}
870
871
872void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* declaration) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000873 Variable* variable = declaration->proxy()->var();
874 ASSERT(variable->location() == Variable::CONTEXT);
875 ASSERT(variable->interface()->IsFrozen());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000876
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000877 Comment cmnt(masm_, "[ ModuleDeclaration");
878 EmitDebugCheckDeclarationContext(variable);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000879
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000880 // Load instance object.
881 __ LoadContext(eax, scope_->ContextChainLength(scope_->GlobalScope()));
882 __ mov(eax, ContextOperand(eax, variable->interface()->Index()));
883 __ mov(eax, ContextOperand(eax, Context::EXTENSION_INDEX));
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000884
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000885 // Assign it.
886 __ mov(ContextOperand(esi, variable->index()), eax);
887 // We know that we have written a module, which is not a smi.
888 __ RecordWriteContextSlot(esi,
889 Context::SlotOffset(variable->index()),
890 eax,
891 ecx,
892 kDontSaveFPRegs,
893 EMIT_REMEMBERED_SET,
894 OMIT_SMI_CHECK);
895 PrepareForBailoutForId(declaration->proxy()->id(), NO_REGISTERS);
896
897 // Traverse into body.
898 Visit(declaration->module());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000899}
900
901
902void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* declaration) {
903 VariableProxy* proxy = declaration->proxy();
904 Variable* variable = proxy->var();
905 switch (variable->location()) {
906 case Variable::UNALLOCATED:
907 // TODO(rossberg)
908 break;
909
910 case Variable::CONTEXT: {
911 Comment cmnt(masm_, "[ ImportDeclaration");
912 EmitDebugCheckDeclarationContext(variable);
913 // TODO(rossberg)
914 break;
915 }
916
917 case Variable::PARAMETER:
918 case Variable::LOCAL:
919 case Variable::LOOKUP:
920 UNREACHABLE();
921 }
922}
923
924
925void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* declaration) {
926 // TODO(rossberg)
927}
928
929
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000930void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
931 // Call the runtime to declare the globals.
932 __ push(esi); // The context is the first argument.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000933 __ Push(pairs);
934 __ Push(Smi::FromInt(DeclareGlobalsFlags()));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000935 __ CallRuntime(Runtime::kDeclareGlobals, 3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000936 // Return value is ignored.
937}
938
939
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000940void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
941 // Call the runtime to declare the modules.
942 __ Push(descriptions);
943 __ CallRuntime(Runtime::kDeclareModules, 1);
944 // Return value is ignored.
945}
946
947
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000948void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
949 Comment cmnt(masm_, "[ SwitchStatement");
950 Breakable nested_statement(this, stmt);
951 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000952
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000953 // Keep the switch value on the stack until a case matches.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000954 VisitForStackValue(stmt->tag());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000955 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000956
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000957 ZoneList<CaseClause*>* clauses = stmt->cases();
958 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000959
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000960 Label next_test; // Recycled for each test.
961 // Compile all the tests with branches to their bodies.
962 for (int i = 0; i < clauses->length(); i++) {
963 CaseClause* clause = clauses->at(i);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000964 clause->body_target()->Unuse();
fschneider@chromium.orgd2187832011-01-26 15:44:20 +0000965
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000966 // The default is not a test, but remember it as final fall through.
967 if (clause->is_default()) {
968 default_clause = clause;
969 continue;
970 }
971
972 Comment cmnt(masm_, "[ Case comparison");
973 __ bind(&next_test);
974 next_test.Unuse();
975
976 // Compile the label expression.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000977 VisitForAccumulatorValue(clause->label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000978
ricow@chromium.org65fae842010-08-25 15:26:24 +0000979 // Perform the comparison as if via '==='.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000980 __ mov(edx, Operand(esp, 0)); // Switch value.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000981 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000982 JumpPatchSite patch_site(masm_);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000983 if (inline_smi_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000984 Label slow_case;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000985 __ mov(ecx, edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000986 __ or_(ecx, eax);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000987 patch_site.EmitJumpIfNotSmi(ecx, &slow_case, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000988
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000989 __ cmp(edx, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000990 __ j(not_equal, &next_test);
991 __ Drop(1); // Switch value is no longer needed.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000992 __ jmp(clause->body_target());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000993 __ bind(&slow_case);
994 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000995
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000996 // Record position before stub call for type feedback.
997 SetSourcePosition(clause->position());
hpayer@chromium.org8432c912013-02-28 15:55:26 +0000998 Handle<Code> ic = CompareIC::GetUninitialized(isolate(), Token::EQ_STRICT);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000999 CallIC(ic, RelocInfo::CODE_TARGET, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001000 patch_site.EmitPatchInfo();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001001 __ test(eax, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001002 __ j(not_equal, &next_test);
1003 __ Drop(1); // Switch value is no longer needed.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001004 __ jmp(clause->body_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001005 }
1006
1007 // Discard the test value and jump to the default if present, otherwise to
1008 // the end of the statement.
1009 __ bind(&next_test);
1010 __ Drop(1); // Switch value is no longer needed.
1011 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001012 __ jmp(nested_statement.break_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001013 } else {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001014 __ jmp(default_clause->body_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001015 }
1016
1017 // Compile all the case bodies.
1018 for (int i = 0; i < clauses->length(); i++) {
1019 Comment cmnt(masm_, "[ Case body");
1020 CaseClause* clause = clauses->at(i);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001021 __ bind(clause->body_target());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001022 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001023 VisitStatements(clause->statements());
1024 }
1025
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001026 __ bind(nested_statement.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001027 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001028}
1029
1030
1031void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1032 Comment cmnt(masm_, "[ ForInStatement");
1033 SetStatementPosition(stmt);
1034
1035 Label loop, exit;
1036 ForIn loop_statement(this, stmt);
1037 increment_loop_depth();
1038
1039 // Get the object to enumerate over. Both SpiderMonkey and JSC
1040 // ignore null and undefined in contrast to the specification; see
1041 // ECMA-262 section 12.6.4.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001042 VisitForAccumulatorValue(stmt->enumerable());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001043 __ cmp(eax, isolate()->factory()->undefined_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001044 __ j(equal, &exit);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001045 __ cmp(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001046 __ j(equal, &exit);
1047
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001048 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1049
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001050 // Convert the object to a JS object.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001051 Label convert, done_convert;
whesse@chromium.org7b260152011-06-20 15:33:18 +00001052 __ JumpIfSmi(eax, &convert, Label::kNear);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001053 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001054 __ j(above_equal, &done_convert, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001055 __ bind(&convert);
1056 __ push(eax);
1057 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1058 __ bind(&done_convert);
1059 __ push(eax);
1060
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001061 // Check for proxies.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001062 Label call_runtime, use_cache, fixed_array;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001063 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1064 __ CmpObjectType(eax, LAST_JS_PROXY_TYPE, ecx);
1065 __ j(below_equal, &call_runtime);
1066
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001067 // Check cache validity in generated code. This is a fast case for
1068 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1069 // guarantee cache validity, call the runtime system to check cache
1070 // validity or get the property names in a fixed array.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001071 __ CheckEnumCache(&call_runtime);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001072
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001073 __ mov(eax, FieldOperand(eax, HeapObject::kMapOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001074 __ jmp(&use_cache, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001075
1076 // Get the set of properties to enumerate.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001077 __ bind(&call_runtime);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001078 __ push(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001079 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001080 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
1081 isolate()->factory()->meta_map());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001082 __ j(not_equal, &fixed_array);
1083
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001084
1085 // We got a map in register eax. Get the enumeration cache from it.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001086 Label no_descriptors;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001087 __ bind(&use_cache);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001088
1089 __ EnumLength(edx, eax);
1090 __ cmp(edx, Immediate(Smi::FromInt(0)));
1091 __ j(equal, &no_descriptors);
1092
danno@chromium.org40cb8782011-05-25 07:58:50 +00001093 __ LoadInstanceDescriptors(eax, ecx);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001094 __ mov(ecx, FieldOperand(ecx, DescriptorArray::kEnumCacheOffset));
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001095 __ mov(ecx, FieldOperand(ecx, DescriptorArray::kEnumCacheBridgeCacheOffset));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001096
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001097 // Set up the four remaining stack slots.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001098 __ push(eax); // Map.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001099 __ push(ecx); // Enumeration cache.
1100 __ push(edx); // Number of valid entries for the map in the enum cache.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001101 __ push(Immediate(Smi::FromInt(0))); // Initial index.
1102 __ jmp(&loop);
1103
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001104 __ bind(&no_descriptors);
1105 __ add(esp, Immediate(kPointerSize));
1106 __ jmp(&exit);
1107
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001108 // We got a fixed array in register eax. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001109 Label non_proxy;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001110 __ bind(&fixed_array);
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001111
1112 Handle<JSGlobalPropertyCell> cell =
1113 isolate()->factory()->NewJSGlobalPropertyCell(
1114 Handle<Object>(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001115 Smi::FromInt(TypeFeedbackCells::kForInFastCaseMarker),
1116 isolate()));
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001117 RecordTypeFeedbackCell(stmt->ForInFeedbackId(), cell);
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001118 __ LoadHeapObject(ebx, cell);
1119 __ mov(FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset),
1120 Immediate(Smi::FromInt(TypeFeedbackCells::kForInSlowCaseMarker)));
1121
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001122 __ mov(ebx, Immediate(Smi::FromInt(1))); // Smi indicates slow check
1123 __ mov(ecx, Operand(esp, 0 * kPointerSize)); // Get enumerated object
1124 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1125 __ CmpObjectType(ecx, LAST_JS_PROXY_TYPE, ecx);
1126 __ j(above, &non_proxy);
1127 __ mov(ebx, Immediate(Smi::FromInt(0))); // Zero indicates proxy
1128 __ bind(&non_proxy);
1129 __ push(ebx); // Smi
1130 __ push(eax); // Array
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001131 __ mov(eax, FieldOperand(eax, FixedArray::kLengthOffset));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001132 __ push(eax); // Fixed array length (as smi).
1133 __ push(Immediate(Smi::FromInt(0))); // Initial index.
1134
1135 // Generate code for doing the condition check.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001136 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001137 __ bind(&loop);
1138 __ mov(eax, Operand(esp, 0 * kPointerSize)); // Get the current index.
1139 __ cmp(eax, Operand(esp, 1 * kPointerSize)); // Compare to the array length.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001140 __ j(above_equal, loop_statement.break_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001141
1142 // Get the current entry of the array into register ebx.
1143 __ mov(ebx, Operand(esp, 2 * kPointerSize));
1144 __ mov(ebx, FieldOperand(ebx, eax, times_2, FixedArray::kHeaderSize));
1145
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001146 // Get the expected map from the stack or a smi in the
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001147 // permanent slow case into register edx.
1148 __ mov(edx, Operand(esp, 3 * kPointerSize));
1149
1150 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001151 // If not, we may have to filter the key.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001152 Label update_each;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001153 __ mov(ecx, Operand(esp, 4 * kPointerSize));
1154 __ cmp(edx, FieldOperand(ecx, HeapObject::kMapOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001155 __ j(equal, &update_each, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001156
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001157 // For proxies, no filtering is done.
1158 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1159 ASSERT(Smi::FromInt(0) == 0);
1160 __ test(edx, edx);
1161 __ j(zero, &update_each);
1162
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001163 // Convert the entry to a string or null if it isn't a property
1164 // anymore. If the property has been removed while iterating, we
1165 // just skip it.
1166 __ push(ecx); // Enumerable.
1167 __ push(ebx); // Current entry.
1168 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001169 __ test(eax, eax);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001170 __ j(equal, loop_statement.continue_label());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001171 __ mov(ebx, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001172
1173 // Update the 'each' property or variable from the possibly filtered
1174 // entry in register ebx.
1175 __ bind(&update_each);
1176 __ mov(result_register(), ebx);
1177 // Perform the assignment as if via '='.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001178 { EffectContext context(this);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001179 EmitAssignment(stmt->each());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001180 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001181
1182 // Generate code for the body of the loop.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001183 Visit(stmt->body());
1184
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001185 // Generate code for going to the next element by incrementing the
1186 // index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001187 __ bind(loop_statement.continue_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001188 __ add(Operand(esp, 0 * kPointerSize), Immediate(Smi::FromInt(1)));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001189
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001190 EmitBackEdgeBookkeeping(stmt, &loop);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001191 __ jmp(&loop);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001192
1193 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001194 __ bind(loop_statement.break_label());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001195 __ add(esp, Immediate(5 * kPointerSize));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001196
1197 // Exit and decrement the loop depth.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001198 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001199 __ bind(&exit);
1200 decrement_loop_depth();
1201}
1202
1203
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001204void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1205 bool pretenure) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001206 // Use the fast case closure allocation code that allocates in new
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001207 // space for nested functions that don't need literals cloning. If
1208 // we're running with the --always-opt or the --prepare-always-opt
1209 // flag, we need to use the runtime function so that the new function
1210 // we are creating here gets a chance to have its code optimized and
1211 // doesn't just get a copy of the existing unoptimized code.
1212 if (!FLAG_always_opt &&
1213 !FLAG_prepare_always_opt &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001214 !pretenure &&
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001215 scope()->is_function_scope() &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001216 info->num_literals() == 0) {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001217 FastNewClosureStub stub(info->language_mode());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001218 __ push(Immediate(info));
1219 __ CallStub(&stub);
1220 } else {
1221 __ push(esi);
1222 __ push(Immediate(info));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001223 __ push(Immediate(pretenure
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001224 ? isolate()->factory()->true_value()
1225 : isolate()->factory()->false_value()));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001226 __ CallRuntime(Runtime::kNewClosure, 3);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001227 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001228 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001229}
1230
1231
1232void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
1233 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001234 EmitVariableLoad(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001235}
1236
1237
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001238void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1239 TypeofState typeof_state,
1240 Label* slow) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001241 Register context = esi;
1242 Register temp = edx;
1243
1244 Scope* s = scope();
1245 while (s != NULL) {
1246 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001247 if (s->calls_non_strict_eval()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001248 // Check that extension is NULL.
1249 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX),
1250 Immediate(0));
1251 __ j(not_equal, slow);
1252 }
1253 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001254 __ mov(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001255 // Walk the rest of the chain without clobbering esi.
1256 context = temp;
1257 }
1258 // If no outer scope calls eval, we do not need to check more
1259 // context extensions. If we have reached an eval scope, we check
1260 // all extensions from this point.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001261 if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001262 s = s->outer_scope();
1263 }
1264
1265 if (s != NULL && s->is_eval_scope()) {
1266 // Loop up the context chain. There is no frame effect so it is
1267 // safe to use raw labels here.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001268 Label next, fast;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001269 if (!context.is(temp)) {
1270 __ mov(temp, context);
1271 }
1272 __ bind(&next);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001273 // Terminate at native context.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001274 __ cmp(FieldOperand(temp, HeapObject::kMapOffset),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001275 Immediate(isolate()->factory()->native_context_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001276 __ j(equal, &fast, Label::kNear);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001277 // Check that extension is NULL.
1278 __ cmp(ContextOperand(temp, Context::EXTENSION_INDEX), Immediate(0));
1279 __ j(not_equal, slow);
1280 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001281 __ mov(temp, ContextOperand(temp, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001282 __ jmp(&next);
1283 __ bind(&fast);
1284 }
1285
1286 // All extension objects were empty and it is safe to use a global
1287 // load IC call.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001288 __ mov(edx, GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001289 __ mov(ecx, var->name());
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001290 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001291 RelocInfo::Mode mode = (typeof_state == INSIDE_TYPEOF)
1292 ? RelocInfo::CODE_TARGET
1293 : RelocInfo::CODE_TARGET_CONTEXT;
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001294 CallIC(ic, mode);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001295}
1296
1297
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001298MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1299 Label* slow) {
1300 ASSERT(var->IsContextSlot());
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001301 Register context = esi;
1302 Register temp = ebx;
1303
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001304 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001305 if (s->num_heap_slots() > 0) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001306 if (s->calls_non_strict_eval()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001307 // Check that extension is NULL.
1308 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX),
1309 Immediate(0));
1310 __ j(not_equal, slow);
1311 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001312 __ mov(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001313 // Walk the rest of the chain without clobbering esi.
1314 context = temp;
1315 }
1316 }
1317 // Check that last extension is NULL.
1318 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX), Immediate(0));
1319 __ j(not_equal, slow);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001320
1321 // This function is used only for loads, not stores, so it's safe to
1322 // return an esi-based operand (the write barrier cannot be allowed to
1323 // destroy the esi register).
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001324 return ContextOperand(context, var->index());
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001325}
1326
1327
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001328void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1329 TypeofState typeof_state,
1330 Label* slow,
1331 Label* done) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001332 // Generate fast-case code for variables that might be shadowed by
1333 // eval-introduced variables. Eval is used a lot without
1334 // introducing variables. In those cases, we do not want to
1335 // perform a runtime call for all variables in the scope
1336 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001337 if (var->mode() == DYNAMIC_GLOBAL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001338 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001339 __ jmp(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001340 } else if (var->mode() == DYNAMIC_LOCAL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001341 Variable* local = var->local_if_not_shadowed();
1342 __ mov(eax, ContextSlotOperandCheckExtensions(local, slow));
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001343 if (local->mode() == LET ||
1344 local->mode() == CONST ||
1345 local->mode() == CONST_HARMONY) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001346 __ cmp(eax, isolate()->factory()->the_hole_value());
1347 __ j(not_equal, done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001348 if (local->mode() == CONST) {
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001349 __ mov(eax, isolate()->factory()->undefined_value());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001350 } else { // LET || CONST_HARMONY
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001351 __ push(Immediate(var->name()));
1352 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1353 }
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001354 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001355 __ jmp(done);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001356 }
1357}
1358
1359
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001360void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1361 // Record position before possible IC call.
1362 SetSourcePosition(proxy->position());
1363 Variable* var = proxy->var();
1364
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001365 // Three cases: global variables, lookup variables, and all other types of
1366 // variables.
1367 switch (var->location()) {
1368 case Variable::UNALLOCATED: {
1369 Comment cmnt(masm_, "Global variable");
1370 // Use inline caching. Variable name is passed in ecx and the global
1371 // object in eax.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001372 __ mov(edx, GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001373 __ mov(ecx, var->name());
1374 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001375 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001376 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001377 break;
1378 }
1379
1380 case Variable::PARAMETER:
1381 case Variable::LOCAL:
1382 case Variable::CONTEXT: {
1383 Comment cmnt(masm_, var->IsContextSlot()
1384 ? "Context variable"
1385 : "Stack variable");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001386 if (var->binding_needs_init()) {
1387 // var->scope() may be NULL when the proxy is located in eval code and
1388 // refers to a potential outside binding. Currently those bindings are
1389 // always looked up dynamically, i.e. in that case
1390 // var->location() == LOOKUP.
1391 // always holds.
1392 ASSERT(var->scope() != NULL);
1393
1394 // Check if the binding really needs an initialization check. The check
1395 // can be skipped in the following situation: we have a LET or CONST
1396 // binding in harmony mode, both the Variable and the VariableProxy have
1397 // the same declaration scope (i.e. they are both in global code, in the
1398 // same function or in the same eval code) and the VariableProxy is in
1399 // the source physically located after the initializer of the variable.
1400 //
1401 // We cannot skip any initialization checks for CONST in non-harmony
1402 // mode because const variables may be declared but never initialized:
1403 // if (false) { const x; }; var y = x;
1404 //
1405 // The condition on the declaration scopes is a conservative check for
1406 // nested functions that access a binding and are called before the
1407 // binding is initialized:
1408 // function() { f(); let x = 1; function f() { x = 2; } }
1409 //
1410 bool skip_init_check;
1411 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1412 skip_init_check = false;
jkummerow@chromium.orgac45fed2011-11-07 13:11:02 +00001413 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001414 // Check that we always have valid source position.
1415 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1416 ASSERT(proxy->position() != RelocInfo::kNoPosition);
1417 skip_init_check = var->mode() != CONST &&
1418 var->initializer_position() < proxy->position();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001419 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001420
1421 if (!skip_init_check) {
1422 // Let and const need a read barrier.
1423 Label done;
1424 GetVar(eax, var);
1425 __ cmp(eax, isolate()->factory()->the_hole_value());
1426 __ j(not_equal, &done, Label::kNear);
1427 if (var->mode() == LET || var->mode() == CONST_HARMONY) {
1428 // Throw a reference error when using an uninitialized let/const
1429 // binding in harmony mode.
1430 __ push(Immediate(var->name()));
1431 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1432 } else {
1433 // Uninitalized const bindings outside of harmony mode are unholed.
1434 ASSERT(var->mode() == CONST);
1435 __ mov(eax, isolate()->factory()->undefined_value());
1436 }
1437 __ bind(&done);
1438 context()->Plug(eax);
1439 break;
1440 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001441 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001442 context()->Plug(var);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001443 break;
1444 }
1445
1446 case Variable::LOOKUP: {
1447 Label done, slow;
1448 // Generate code for loading from variables potentially shadowed
1449 // by eval-introduced variables.
1450 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1451 __ bind(&slow);
1452 Comment cmnt(masm_, "Lookup variable");
1453 __ push(esi); // Context.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001454 __ push(Immediate(var->name()));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001455 __ CallRuntime(Runtime::kLoadContextSlot, 2);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001456 __ bind(&done);
1457 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001458 break;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001459 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001460 }
1461}
1462
1463
1464void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1465 Comment cmnt(masm_, "[ RegExpLiteral");
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001466 Label materialized;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001467 // Registers will be used as follows:
1468 // edi = JS function.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001469 // ecx = literals array.
1470 // ebx = regexp literal.
1471 // eax = regexp literal clone.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001472 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001473 __ mov(ecx, FieldOperand(edi, JSFunction::kLiteralsOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001474 int literal_offset =
ricow@chromium.org65fae842010-08-25 15:26:24 +00001475 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001476 __ mov(ebx, FieldOperand(ecx, literal_offset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001477 __ cmp(ebx, isolate()->factory()->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001478 __ j(not_equal, &materialized, Label::kNear);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001479
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001480 // Create regexp literal using runtime function
1481 // Result will be in eax.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001482 __ push(ecx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001483 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1484 __ push(Immediate(expr->pattern()));
1485 __ push(Immediate(expr->flags()));
1486 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001487 __ mov(ebx, eax);
1488
1489 __ bind(&materialized);
1490 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1491 Label allocated, runtime_allocate;
1492 __ AllocateInNewSpace(size, eax, ecx, edx, &runtime_allocate, TAG_OBJECT);
1493 __ jmp(&allocated);
1494
1495 __ bind(&runtime_allocate);
1496 __ push(ebx);
1497 __ push(Immediate(Smi::FromInt(size)));
1498 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1499 __ pop(ebx);
1500
1501 __ bind(&allocated);
1502 // Copy the content into the newly allocated memory.
1503 // (Unroll copy loop once for better throughput).
1504 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
1505 __ mov(edx, FieldOperand(ebx, i));
1506 __ mov(ecx, FieldOperand(ebx, i + kPointerSize));
1507 __ mov(FieldOperand(eax, i), edx);
1508 __ mov(FieldOperand(eax, i + kPointerSize), ecx);
1509 }
1510 if ((size % (2 * kPointerSize)) != 0) {
1511 __ mov(edx, FieldOperand(ebx, size - kPointerSize));
1512 __ mov(FieldOperand(eax, size - kPointerSize), edx);
1513 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001514 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001515}
1516
1517
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001518void FullCodeGenerator::EmitAccessor(Expression* expression) {
1519 if (expression == NULL) {
1520 __ push(Immediate(isolate()->factory()->null_value()));
1521 } else {
1522 VisitForStackValue(expression);
1523 }
1524}
1525
1526
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001527void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1528 Comment cmnt(masm_, "[ ObjectLiteral");
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001529 Handle<FixedArray> constant_properties = expr->constant_properties();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001530 int flags = expr->fast_elements()
1531 ? ObjectLiteral::kFastElements
1532 : ObjectLiteral::kNoFlags;
1533 flags |= expr->has_function()
1534 ? ObjectLiteral::kHasFunction
1535 : ObjectLiteral::kNoFlags;
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001536 int properties_count = constant_properties->length() / 2;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001537 if (expr->depth() > 1) {
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001538 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1539 __ push(FieldOperand(edi, JSFunction::kLiteralsOffset));
1540 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1541 __ push(Immediate(constant_properties));
1542 __ push(Immediate(Smi::FromInt(flags)));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001543 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001544 } else if (Serializer::enabled() || flags != ObjectLiteral::kFastElements ||
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001545 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001546 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1547 __ push(FieldOperand(edi, JSFunction::kLiteralsOffset));
1548 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1549 __ push(Immediate(constant_properties));
1550 __ push(Immediate(Smi::FromInt(flags)));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001551 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001552 } else {
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001553 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1554 __ mov(eax, FieldOperand(edi, JSFunction::kLiteralsOffset));
1555 __ mov(ebx, Immediate(Smi::FromInt(expr->literal_index())));
1556 __ mov(ecx, Immediate(constant_properties));
1557 __ mov(edx, Immediate(Smi::FromInt(flags)));
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001558 FastCloneShallowObjectStub stub(properties_count);
1559 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001560 }
1561
1562 // If result_saved is true the result is on top of the stack. If
1563 // result_saved is false the result is in eax.
1564 bool result_saved = false;
1565
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001566 // Mark all computed expressions that are bound to a key that
1567 // is shadowed by a later occurrence of the same key. For the
1568 // marked expressions, no store code is emitted.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001569 expr->CalculateEmitStore(zone());
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001570
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001571 AccessorTable accessor_table(zone());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001572 for (int i = 0; i < expr->properties()->length(); i++) {
1573 ObjectLiteral::Property* property = expr->properties()->at(i);
1574 if (property->IsCompileTimeValue()) continue;
1575
1576 Literal* key = property->key();
1577 Expression* value = property->value();
1578 if (!result_saved) {
1579 __ push(eax); // Save result on the stack
1580 result_saved = true;
1581 }
1582 switch (property->kind()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001583 case ObjectLiteral::Property::CONSTANT:
1584 UNREACHABLE();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001585 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1586 ASSERT(!CompileTimeValue::IsCompileTimeValue(value));
1587 // Fall through.
1588 case ObjectLiteral::Property::COMPUTED:
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001589 if (key->handle()->IsInternalizedString()) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001590 if (property->emit_store()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001591 VisitForAccumulatorValue(value);
1592 __ mov(ecx, Immediate(key->handle()));
1593 __ mov(edx, Operand(esp, 0));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001594 Handle<Code> ic = is_classic_mode()
1595 ? isolate()->builtins()->StoreIC_Initialize()
1596 : isolate()->builtins()->StoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001597 CallIC(ic, RelocInfo::CODE_TARGET, key->LiteralFeedbackId());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001598 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1599 } else {
1600 VisitForEffect(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001601 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001602 break;
1603 }
1604 // Fall through.
1605 case ObjectLiteral::Property::PROTOTYPE:
1606 __ push(Operand(esp, 0)); // Duplicate receiver.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001607 VisitForStackValue(key);
1608 VisitForStackValue(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001609 if (property->emit_store()) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001610 __ push(Immediate(Smi::FromInt(NONE))); // PropertyAttributes
1611 __ CallRuntime(Runtime::kSetProperty, 4);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001612 } else {
1613 __ Drop(3);
1614 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001615 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001616 case ObjectLiteral::Property::GETTER:
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001617 accessor_table.lookup(key)->second->getter = value;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001618 break;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001619 case ObjectLiteral::Property::SETTER:
1620 accessor_table.lookup(key)->second->setter = value;
1621 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001622 }
1623 }
1624
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001625 // Emit code to define accessors, using only a single call to the runtime for
1626 // each pair of corresponding getters and setters.
1627 for (AccessorTable::Iterator it = accessor_table.begin();
1628 it != accessor_table.end();
1629 ++it) {
1630 __ push(Operand(esp, 0)); // Duplicate receiver.
1631 VisitForStackValue(it->first);
1632 EmitAccessor(it->second->getter);
1633 EmitAccessor(it->second->setter);
1634 __ push(Immediate(Smi::FromInt(NONE)));
1635 __ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
1636 }
1637
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001638 if (expr->has_function()) {
1639 ASSERT(result_saved);
1640 __ push(Operand(esp, 0));
1641 __ CallRuntime(Runtime::kToFastProperties, 1);
1642 }
1643
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001644 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001645 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001646 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001647 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001648 }
1649}
1650
1651
1652void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1653 Comment cmnt(masm_, "[ ArrayLiteral");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001654
1655 ZoneList<Expression*>* subexprs = expr->values();
1656 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001657 Handle<FixedArray> constant_elements = expr->constant_elements();
1658 ASSERT_EQ(2, constant_elements->length());
1659 ElementsKind constant_elements_kind =
1660 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001661 bool has_constant_fast_elements =
1662 IsFastObjectElementsKind(constant_elements_kind);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001663 Handle<FixedArrayBase> constant_elements_values(
1664 FixedArrayBase::cast(constant_elements->get(1)));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001665
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001666 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1667 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
1668 __ push(Immediate(Smi::FromInt(expr->literal_index())));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001669 __ push(Immediate(constant_elements));
erikcorry0ad885c2011-11-21 13:51:57 +00001670 Heap* heap = isolate()->heap();
1671 if (has_constant_fast_elements &&
1672 constant_elements_values->map() == heap->fixed_cow_array_map()) {
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001673 // If the elements are already FAST_*_ELEMENTS, the boilerplate cannot
erikcorry0ad885c2011-11-21 13:51:57 +00001674 // change, so it's possible to specialize the stub in advance.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001675 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(), 1);
erikcorry0ad885c2011-11-21 13:51:57 +00001676 FastCloneShallowArrayStub stub(
1677 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001678 DONT_TRACK_ALLOCATION_SITE,
erikcorry0ad885c2011-11-21 13:51:57 +00001679 length);
1680 __ CallStub(&stub);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001681 } else if (expr->depth() > 1) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001682 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001683 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001684 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001685 } else {
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001686 ASSERT(IsFastSmiOrObjectElementsKind(constant_elements_kind) ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001687 FLAG_smi_only_arrays);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001688 FastCloneShallowArrayStub::Mode mode =
1689 FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS;
1690 AllocationSiteMode allocation_site_mode = FLAG_track_allocation_sites
1691 ? TRACK_ALLOCATION_SITE : DONT_TRACK_ALLOCATION_SITE;
1692
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001693 // If the elements are already FAST_*_ELEMENTS, the boilerplate cannot
erikcorry0ad885c2011-11-21 13:51:57 +00001694 // change, so it's possible to specialize the stub in advance.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001695 if (has_constant_fast_elements) {
1696 mode = FastCloneShallowArrayStub::CLONE_ELEMENTS;
1697 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001698 }
1699
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001700 FastCloneShallowArrayStub stub(mode, allocation_site_mode, length);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001701 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001702 }
1703
1704 bool result_saved = false; // Is the result saved to the stack?
1705
1706 // Emit code to evaluate all the non-constant subexpressions and to store
1707 // them into the newly cloned array.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001708 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001709 Expression* subexpr = subexprs->at(i);
1710 // If the subexpression is a literal or a simple materialized literal it
1711 // is already set in the cloned array.
1712 if (subexpr->AsLiteral() != NULL ||
1713 CompileTimeValue::IsCompileTimeValue(subexpr)) {
1714 continue;
1715 }
1716
1717 if (!result_saved) {
1718 __ push(eax);
1719 result_saved = true;
1720 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001721 VisitForAccumulatorValue(subexpr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001722
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001723 if (IsFastObjectElementsKind(constant_elements_kind)) {
1724 // Fast-case array literal with ElementsKind of FAST_*_ELEMENTS, they
1725 // cannot transition and don't need to call the runtime stub.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001726 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1727 __ mov(ebx, Operand(esp, 0)); // Copy of array literal.
1728 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
1729 // Store the subexpression value in the array's elements.
1730 __ mov(FieldOperand(ebx, offset), result_register());
1731 // Update the write barrier for the array store.
1732 __ RecordWriteField(ebx, offset, result_register(), ecx,
1733 kDontSaveFPRegs,
1734 EMIT_REMEMBERED_SET,
1735 INLINE_SMI_CHECK);
1736 } else {
1737 // Store the subexpression value in the array's elements.
1738 __ mov(ebx, Operand(esp, 0)); // Copy of array literal.
1739 __ mov(edi, FieldOperand(ebx, JSObject::kMapOffset));
1740 __ mov(ecx, Immediate(Smi::FromInt(i)));
1741 __ mov(edx, Immediate(Smi::FromInt(expr->literal_index())));
1742 StoreArrayLiteralElementStub stub;
1743 __ CallStub(&stub);
1744 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001745
1746 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001747 }
1748
1749 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001750 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001751 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001752 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001753 }
1754}
1755
1756
ager@chromium.org5c838252010-02-19 08:53:10 +00001757void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1758 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001759 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1760 // on the left-hand side.
1761 if (!expr->target()->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001762 VisitForEffect(expr->target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001763 return;
1764 }
1765
ager@chromium.org5c838252010-02-19 08:53:10 +00001766 // Left-hand side can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00001767 // slot.
ager@chromium.org5c838252010-02-19 08:53:10 +00001768 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1769 LhsKind assign_type = VARIABLE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001770 Property* property = expr->target()->AsProperty();
1771 if (property != NULL) {
1772 assign_type = (property->key()->IsPropertyName())
1773 ? NAMED_PROPERTY
1774 : KEYED_PROPERTY;
ager@chromium.org5c838252010-02-19 08:53:10 +00001775 }
1776
1777 // Evaluate LHS expression.
1778 switch (assign_type) {
1779 case VARIABLE:
1780 // Nothing to do here.
1781 break;
1782 case NAMED_PROPERTY:
1783 if (expr->is_compound()) {
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001784 // We need the receiver both on the stack and in edx.
1785 VisitForStackValue(property->obj());
1786 __ mov(edx, Operand(esp, 0));
ager@chromium.org5c838252010-02-19 08:53:10 +00001787 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001788 VisitForStackValue(property->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00001789 }
1790 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001791 case KEYED_PROPERTY: {
ager@chromium.org5c838252010-02-19 08:53:10 +00001792 if (expr->is_compound()) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00001793 VisitForStackValue(property->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001794 VisitForStackValue(property->key());
1795 __ mov(edx, Operand(esp, kPointerSize)); // Object.
1796 __ mov(ecx, Operand(esp, 0)); // Key.
ager@chromium.org5c838252010-02-19 08:53:10 +00001797 } else {
whesse@chromium.org7b260152011-06-20 15:33:18 +00001798 VisitForStackValue(property->obj());
1799 VisitForStackValue(property->key());
ager@chromium.org5c838252010-02-19 08:53:10 +00001800 }
1801 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001802 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001803 }
1804
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001805 // For compound assignments we need another deoptimization point after the
1806 // variable/property load.
ager@chromium.org5c838252010-02-19 08:53:10 +00001807 if (expr->is_compound()) {
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001808 AccumulatorValueContext result_context(this);
1809 { AccumulatorValueContext left_operand_context(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001810 switch (assign_type) {
1811 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001812 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001813 PrepareForBailout(expr->target(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001814 break;
1815 case NAMED_PROPERTY:
1816 EmitNamedPropertyLoad(property);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001817 PrepareForBailoutForId(property->LoadId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001818 break;
1819 case KEYED_PROPERTY:
1820 EmitKeyedPropertyLoad(property);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001821 PrepareForBailoutForId(property->LoadId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001822 break;
1823 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001824 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001825
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001826 Token::Value op = expr->binary_op();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001827 __ push(eax); // Left operand goes on the stack.
1828 VisitForAccumulatorValue(expr->value());
ager@chromium.org5c838252010-02-19 08:53:10 +00001829
ricow@chromium.org65fae842010-08-25 15:26:24 +00001830 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1831 ? OVERWRITE_RIGHT
1832 : NO_OVERWRITE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001833 SetSourcePosition(expr->position() + 1);
1834 if (ShouldInlineSmiCase(op)) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001835 EmitInlineSmiBinaryOp(expr->binary_operation(),
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001836 op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001837 mode,
1838 expr->target(),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001839 expr->value());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001840 } else {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001841 EmitBinaryOp(expr->binary_operation(), op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001842 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001843
1844 // Deoptimization point in case the binary operation may have side effects.
1845 PrepareForBailout(expr->binary_operation(), TOS_REG);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001846 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001847 VisitForAccumulatorValue(expr->value());
ager@chromium.org5c838252010-02-19 08:53:10 +00001848 }
1849
1850 // Record source position before possible IC call.
1851 SetSourcePosition(expr->position());
1852
1853 // Store the value.
1854 switch (assign_type) {
1855 case VARIABLE:
1856 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001857 expr->op());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001858 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1859 context()->Plug(eax);
ager@chromium.org5c838252010-02-19 08:53:10 +00001860 break;
1861 case NAMED_PROPERTY:
1862 EmitNamedPropertyAssignment(expr);
1863 break;
1864 case KEYED_PROPERTY:
1865 EmitKeyedPropertyAssignment(expr);
1866 break;
1867 }
1868}
1869
1870
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001871void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1872 SetSourcePosition(prop->position());
1873 Literal* key = prop->key()->AsLiteral();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001874 ASSERT(!key->handle()->IsSmi());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001875 __ mov(ecx, Immediate(key->handle()));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001876 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001877 CallIC(ic, RelocInfo::CODE_TARGET, prop->PropertyFeedbackId());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001878}
1879
1880
1881void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1882 SetSourcePosition(prop->position());
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001883 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001884 CallIC(ic, RelocInfo::CODE_TARGET, prop->PropertyFeedbackId());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001885}
1886
1887
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001888void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001889 Token::Value op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001890 OverwriteMode mode,
1891 Expression* left,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001892 Expression* right) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001893 // Do combined smi check of the operands. Left operand is on the
1894 // stack. Right operand is in eax.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001895 Label smi_case, done, stub_call;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001896 __ pop(edx);
1897 __ mov(ecx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001898 __ or_(eax, edx);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001899 JumpPatchSite patch_site(masm_);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001900 patch_site.EmitJumpIfSmi(eax, &smi_case, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001901
1902 __ bind(&stub_call);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001903 __ mov(eax, ecx);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001904 BinaryOpStub stub(op, mode);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001905 CallIC(stub.GetCode(isolate()), RelocInfo::CODE_TARGET,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001906 expr->BinaryOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001907 patch_site.EmitPatchInfo();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001908 __ jmp(&done, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001909
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001910 // Smi case.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001911 __ bind(&smi_case);
1912 __ mov(eax, edx); // Copy left operand in case of a stub call.
1913
1914 switch (op) {
1915 case Token::SAR:
1916 __ SmiUntag(eax);
1917 __ SmiUntag(ecx);
1918 __ sar_cl(eax); // No checks of result necessary
1919 __ SmiTag(eax);
1920 break;
1921 case Token::SHL: {
1922 Label result_ok;
1923 __ SmiUntag(eax);
1924 __ SmiUntag(ecx);
1925 __ shl_cl(eax);
1926 // Check that the *signed* result fits in a smi.
1927 __ cmp(eax, 0xc0000000);
1928 __ j(positive, &result_ok);
1929 __ SmiTag(ecx);
1930 __ jmp(&stub_call);
1931 __ bind(&result_ok);
1932 __ SmiTag(eax);
1933 break;
1934 }
1935 case Token::SHR: {
1936 Label result_ok;
1937 __ SmiUntag(eax);
1938 __ SmiUntag(ecx);
1939 __ shr_cl(eax);
1940 __ test(eax, Immediate(0xc0000000));
1941 __ j(zero, &result_ok);
1942 __ SmiTag(ecx);
1943 __ jmp(&stub_call);
1944 __ bind(&result_ok);
1945 __ SmiTag(eax);
1946 break;
1947 }
1948 case Token::ADD:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001949 __ add(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001950 __ j(overflow, &stub_call);
1951 break;
1952 case Token::SUB:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001953 __ sub(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001954 __ j(overflow, &stub_call);
1955 break;
1956 case Token::MUL: {
1957 __ SmiUntag(eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001958 __ imul(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001959 __ j(overflow, &stub_call);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001960 __ test(eax, eax);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001961 __ j(not_zero, &done, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001962 __ mov(ebx, edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001963 __ or_(ebx, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001964 __ j(negative, &stub_call);
1965 break;
1966 }
1967 case Token::BIT_OR:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001968 __ or_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001969 break;
1970 case Token::BIT_AND:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001971 __ and_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001972 break;
1973 case Token::BIT_XOR:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001974 __ xor_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001975 break;
1976 default:
1977 UNREACHABLE();
1978 }
1979
1980 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001981 context()->Plug(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001982}
1983
1984
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001985void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
1986 Token::Value op,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001987 OverwriteMode mode) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001988 __ pop(edx);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001989 BinaryOpStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001990 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001991 CallIC(stub.GetCode(isolate()), RelocInfo::CODE_TARGET,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001992 expr->BinaryOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001993 patch_site.EmitPatchInfo();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001994 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001995}
1996
1997
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001998void FullCodeGenerator::EmitAssignment(Expression* expr) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001999 // Invalid left-hand sides are rewritten by the parser to have a 'throw
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002000 // ReferenceError' on the left-hand side.
2001 if (!expr->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002002 VisitForEffect(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002003 return;
2004 }
2005
2006 // Left-hand side can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00002007 // slot.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002008 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2009 LhsKind assign_type = VARIABLE;
2010 Property* prop = expr->AsProperty();
2011 if (prop != NULL) {
2012 assign_type = (prop->key()->IsPropertyName())
2013 ? NAMED_PROPERTY
2014 : KEYED_PROPERTY;
2015 }
2016
2017 switch (assign_type) {
2018 case VARIABLE: {
2019 Variable* var = expr->AsVariableProxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002020 EffectContext context(this);
2021 EmitVariableAssignment(var, Token::ASSIGN);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002022 break;
2023 }
2024 case NAMED_PROPERTY: {
2025 __ push(eax); // Preserve value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002026 VisitForAccumulatorValue(prop->obj());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002027 __ mov(edx, eax);
2028 __ pop(eax); // Restore value.
2029 __ mov(ecx, prop->key()->AsLiteral()->handle());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002030 Handle<Code> ic = is_classic_mode()
2031 ? isolate()->builtins()->StoreIC_Initialize()
2032 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002033 CallIC(ic);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002034 break;
2035 }
2036 case KEYED_PROPERTY: {
2037 __ push(eax); // Preserve value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002038 VisitForStackValue(prop->obj());
2039 VisitForAccumulatorValue(prop->key());
2040 __ mov(ecx, eax);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002041 __ pop(edx); // Receiver.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002042 __ pop(eax); // Restore value.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002043 Handle<Code> ic = is_classic_mode()
2044 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2045 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002046 CallIC(ic);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002047 break;
2048 }
2049 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002050 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002051}
2052
2053
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002054void FullCodeGenerator::EmitVariableAssignment(Variable* var,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002055 Token::Value op) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002056 if (var->IsUnallocated()) {
2057 // Global var, const, or let.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002058 __ mov(ecx, var->name());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002059 __ mov(edx, GlobalObjectOperand());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002060 Handle<Code> ic = is_classic_mode()
2061 ? isolate()->builtins()->StoreIC_Initialize()
2062 : isolate()->builtins()->StoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002063 CallIC(ic, RelocInfo::CODE_TARGET_CONTEXT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002064
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002065 } else if (op == Token::INIT_CONST) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002066 // Const initializers need a write barrier.
2067 ASSERT(!var->IsParameter()); // No const parameters.
2068 if (var->IsStackLocal()) {
2069 Label skip;
2070 __ mov(edx, StackOperand(var));
2071 __ cmp(edx, isolate()->factory()->the_hole_value());
2072 __ j(not_equal, &skip);
2073 __ mov(StackOperand(var), eax);
2074 __ bind(&skip);
2075 } else {
2076 ASSERT(var->IsContextSlot() || var->IsLookupSlot());
2077 // Like var declarations, const declarations are hoisted to function
2078 // scope. However, unlike var initializers, const initializers are
2079 // able to drill a hole to that function context, even from inside a
2080 // 'with' context. We thus bypass the normal static scope lookup for
2081 // var->IsContextSlot().
2082 __ push(eax);
2083 __ push(esi);
2084 __ push(Immediate(var->name()));
2085 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002086 }
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002087
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002088 } else if (var->mode() == LET && op != Token::INIT_LET) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002089 // Non-initializing assignment to let variable needs a write barrier.
2090 if (var->IsLookupSlot()) {
2091 __ push(eax); // Value.
2092 __ push(esi); // Context.
2093 __ push(Immediate(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002094 __ push(Immediate(Smi::FromInt(language_mode())));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002095 __ CallRuntime(Runtime::kStoreContextSlot, 4);
2096 } else {
2097 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
2098 Label assign;
2099 MemOperand location = VarOperand(var, ecx);
2100 __ mov(edx, location);
2101 __ cmp(edx, isolate()->factory()->the_hole_value());
2102 __ j(not_equal, &assign, Label::kNear);
2103 __ push(Immediate(var->name()));
2104 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2105 __ bind(&assign);
2106 __ mov(location, eax);
2107 if (var->IsContextSlot()) {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00002108 __ mov(edx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002109 int offset = Context::SlotOffset(var->index());
2110 __ RecordWriteContextSlot(ecx, offset, edx, ebx, kDontSaveFPRegs);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00002111 }
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00002112 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002113
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002114 } else if (!var->is_const_mode() || op == Token::INIT_CONST_HARMONY) {
2115 // Assignment to var or initializing assignment to let/const
2116 // in harmony mode.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002117 if (var->IsStackAllocated() || var->IsContextSlot()) {
2118 MemOperand location = VarOperand(var, ecx);
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002119 if (generate_debug_code_ && op == Token::INIT_LET) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002120 // Check for an uninitialized let binding.
2121 __ mov(edx, location);
2122 __ cmp(edx, isolate()->factory()->the_hole_value());
2123 __ Check(equal, "Let binding re-initialization.");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002124 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002125 // Perform the assignment.
2126 __ mov(location, eax);
2127 if (var->IsContextSlot()) {
2128 __ mov(edx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002129 int offset = Context::SlotOffset(var->index());
2130 __ RecordWriteContextSlot(ecx, offset, edx, ebx, kDontSaveFPRegs);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002131 }
2132 } else {
2133 ASSERT(var->IsLookupSlot());
2134 __ push(eax); // Value.
2135 __ push(esi); // Context.
2136 __ push(Immediate(var->name()));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002137 __ push(Immediate(Smi::FromInt(language_mode())));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002138 __ CallRuntime(Runtime::kStoreContextSlot, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002139 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002140 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002141 // Non-initializing assignments to consts are ignored.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002142}
2143
2144
2145void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2146 // Assignment to a property, using a named store IC.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002147 // eax : value
2148 // esp[0] : receiver
2149
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002150 Property* prop = expr->target()->AsProperty();
2151 ASSERT(prop != NULL);
2152 ASSERT(prop->key()->AsLiteral() != NULL);
2153
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002154 // Record source code position before IC call.
2155 SetSourcePosition(expr->position());
2156 __ mov(ecx, prop->key()->AsLiteral()->handle());
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002157 __ pop(edx);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002158 Handle<Code> ic = is_classic_mode()
2159 ? isolate()->builtins()->StoreIC_Initialize()
2160 : isolate()->builtins()->StoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002161 CallIC(ic, RelocInfo::CODE_TARGET, expr->AssignmentFeedbackId());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002162
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002163 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2164 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002165}
2166
2167
2168void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2169 // Assignment to a property, using a keyed store IC.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002170 // eax : value
2171 // esp[0] : key
2172 // esp[kPointerSize] : receiver
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002173
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002174 __ pop(ecx); // Key.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002175 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002176 // Record source code position before IC call.
2177 SetSourcePosition(expr->position());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002178 Handle<Code> ic = is_classic_mode()
2179 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2180 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002181 CallIC(ic, RelocInfo::CODE_TARGET, expr->AssignmentFeedbackId());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002182
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002183 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002184 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002185}
2186
2187
2188void FullCodeGenerator::VisitProperty(Property* expr) {
2189 Comment cmnt(masm_, "[ Property");
2190 Expression* key = expr->key();
2191
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002192 if (key->IsPropertyName()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002193 VisitForAccumulatorValue(expr->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002194 __ mov(edx, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002195 EmitNamedPropertyLoad(expr);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002196 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002197 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002198 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002199 VisitForStackValue(expr->obj());
2200 VisitForAccumulatorValue(expr->key());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002201 __ pop(edx); // Object.
2202 __ mov(ecx, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002203 EmitKeyedPropertyLoad(expr);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002204 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002205 }
2206}
2207
2208
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002209void FullCodeGenerator::CallIC(Handle<Code> code,
2210 RelocInfo::Mode rmode,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002211 TypeFeedbackId ast_id) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002212 ic_total_count_++;
2213 __ call(code, rmode, ast_id);
2214}
2215
2216
2217
2218
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002219void FullCodeGenerator::EmitCallWithIC(Call* expr,
2220 Handle<Object> name,
2221 RelocInfo::Mode mode) {
2222 // Code common for calls using the IC.
2223 ZoneList<Expression*>* args = expr->arguments();
2224 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002225 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002226 for (int i = 0; i < arg_count; i++) {
2227 VisitForStackValue(args->at(i));
2228 }
2229 __ Set(ecx, Immediate(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002230 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002231 // Record source position of the IC call.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002232 SetSourcePosition(expr->position());
danno@chromium.org40cb8782011-05-25 07:58:50 +00002233 Handle<Code> ic =
lrn@chromium.org34e60782011-09-15 07:25:40 +00002234 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002235 CallIC(ic, mode, expr->CallFeedbackId());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002236 RecordJSReturnSite(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002237 // Restore context register.
2238 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002239 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002240}
2241
2242
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002243void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002244 Expression* key) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002245 // Load the key.
2246 VisitForAccumulatorValue(key);
2247
2248 // Swap the name of the function and the receiver on the stack to follow
2249 // the calling convention for call ICs.
2250 __ pop(ecx);
2251 __ push(eax);
2252 __ push(ecx);
2253
2254 // Load the arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002255 ZoneList<Expression*>* args = expr->arguments();
2256 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002257 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002258 for (int i = 0; i < arg_count; i++) {
2259 VisitForStackValue(args->at(i));
2260 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002261 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002262 // Record source position of the IC call.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002263 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002264 Handle<Code> ic =
2265 isolate()->stub_cache()->ComputeKeyedCallInitialize(arg_count);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002266 __ mov(ecx, Operand(esp, (arg_count + 1) * kPointerSize)); // Key.
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002267 CallIC(ic, RelocInfo::CODE_TARGET, expr->CallFeedbackId());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002268 RecordJSReturnSite(expr);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002269 // Restore context register.
2270 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002271 context()->DropAndPlug(1, eax); // Drop the key still on the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002272}
2273
2274
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002275void FullCodeGenerator::EmitCallWithStub(Call* expr, CallFunctionFlags flags) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002276 // Code common for calls using the call stub.
2277 ZoneList<Expression*>* args = expr->arguments();
2278 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002279 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002280 for (int i = 0; i < arg_count; i++) {
2281 VisitForStackValue(args->at(i));
2282 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002283 }
2284 // Record source position for debugger.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002285 SetSourcePosition(expr->position());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002286
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002287 // Record call targets in unoptimized code.
2288 flags = static_cast<CallFunctionFlags>(flags | RECORD_CALL_TARGET);
2289 Handle<Object> uninitialized =
2290 TypeFeedbackCells::UninitializedSentinel(isolate());
2291 Handle<JSGlobalPropertyCell> cell =
2292 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002293 RecordTypeFeedbackCell(expr->CallFeedbackId(), cell);
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002294 __ mov(ebx, cell);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002295
lrn@chromium.org34e60782011-09-15 07:25:40 +00002296 CallFunctionStub stub(arg_count, flags);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002297 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002298 __ CallStub(&stub, expr->CallFeedbackId());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002299
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002300 RecordJSReturnSite(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002301 // Restore context register.
2302 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002303 context()->DropAndPlug(1, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002304}
2305
2306
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002307void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002308 // Push copy of the first argument or undefined if it doesn't exist.
2309 if (arg_count > 0) {
2310 __ push(Operand(esp, arg_count * kPointerSize));
2311 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002312 __ push(Immediate(isolate()->factory()->undefined_value()));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002313 }
2314
2315 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002316 __ push(Operand(ebp, (2 + info_->scope()->num_parameters()) * kPointerSize));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002317 // Push the language mode.
2318 __ push(Immediate(Smi::FromInt(language_mode())));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002319
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002320 // Push the start position of the scope the calls resides in.
2321 __ push(Immediate(Smi::FromInt(scope()->start_position())));
2322
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002323 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002324 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002325}
2326
2327
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002328void FullCodeGenerator::VisitCall(Call* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002329#ifdef DEBUG
2330 // We want to verify that RecordJSReturnSite gets called on all paths
2331 // through this function. Avoid early returns.
2332 expr->return_is_recorded_ = false;
2333#endif
2334
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002335 Comment cmnt(masm_, "[ Call");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002336 Expression* callee = expr->expression();
2337 VariableProxy* proxy = callee->AsVariableProxy();
2338 Property* property = callee->AsProperty();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002339
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002340 if (proxy != NULL && proxy->var()->is_possibly_eval(isolate())) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002341 // In a call to eval, we first call %ResolvePossiblyDirectEval to
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002342 // resolve the function we need to call and the receiver of the call.
2343 // Then we call the resolved function using the given arguments.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002344 ZoneList<Expression*>* args = expr->arguments();
2345 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002346 { PreservePositionScope pos_scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002347 VisitForStackValue(callee);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002348 // Reserved receiver slot.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002349 __ push(Immediate(isolate()->factory()->undefined_value()));
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002350 // Push the arguments.
2351 for (int i = 0; i < arg_count; i++) {
2352 VisitForStackValue(args->at(i));
2353 }
2354
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002355 // Push a copy of the function (found below the arguments) and
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002356 // resolve eval.
2357 __ push(Operand(esp, (arg_count + 1) * kPointerSize));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002358 EmitResolvePossiblyDirectEval(arg_count);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002359
2360 // The runtime call returns a pair of values in eax (function) and
2361 // edx (receiver). Touch up the stack with the right values.
2362 __ mov(Operand(esp, (arg_count + 0) * kPointerSize), edx);
2363 __ mov(Operand(esp, (arg_count + 1) * kPointerSize), eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002364 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002365 // Record source position for debugger.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002366 SetSourcePosition(expr->position());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002367 CallFunctionStub stub(arg_count, RECEIVER_MIGHT_BE_IMPLICIT);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002368 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002369 __ CallStub(&stub);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002370 RecordJSReturnSite(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002371 // Restore context register.
2372 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002373 context()->DropAndPlug(1, eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002374
2375 } else if (proxy != NULL && proxy->var()->IsUnallocated()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002376 // Push global object as receiver for the call IC.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002377 __ push(GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002378 EmitCallWithIC(expr, proxy->name(), RelocInfo::CODE_TARGET_CONTEXT);
2379
2380 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002381 // Call to a lookup slot (dynamically introduced variable).
2382 Label slow, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002383 { PreservePositionScope scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002384 // Generate code for loading from variables potentially shadowed by
2385 // eval-introduced variables.
2386 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002387 }
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002388 __ bind(&slow);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002389 // Call the runtime to find the function to call (returned in eax) and
2390 // the object holding it (returned in edx).
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002391 __ push(context_register());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002392 __ push(Immediate(proxy->name()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002393 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2394 __ push(eax); // Function.
2395 __ push(edx); // Receiver.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002396
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002397 // If fast case code has been generated, emit code to push the function
2398 // and receiver and have the slow path jump around this code.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002399 if (done.is_linked()) {
2400 Label call;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002401 __ jmp(&call, Label::kNear);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002402 __ bind(&done);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002403 // Push function.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002404 __ push(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002405 // The receiver is implicitly the global receiver. Indicate this by
2406 // passing the hole to the call function stub.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002407 __ push(Immediate(isolate()->factory()->the_hole_value()));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002408 __ bind(&call);
2409 }
2410
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002411 // The receiver is either the global receiver or an object found by
2412 // LoadContextSlot. That object could be the hole if the receiver is
2413 // implicitly the global object.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002414 EmitCallWithStub(expr, RECEIVER_MIGHT_BE_IMPLICIT);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002415
2416 } else if (property != NULL) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002417 { PreservePositionScope scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002418 VisitForStackValue(property->obj());
2419 }
2420 if (property->key()->IsPropertyName()) {
2421 EmitCallWithIC(expr,
2422 property->key()->AsLiteral()->handle(),
2423 RelocInfo::CODE_TARGET);
2424 } else {
2425 EmitKeyedCallWithIC(expr, property->key());
2426 }
2427
2428 } else {
2429 // Call to an arbitrary expression not handled specially above.
2430 { PreservePositionScope scope(masm()->positions_recorder());
2431 VisitForStackValue(callee);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002432 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002433 // Load global receiver object.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002434 __ mov(ebx, GlobalObjectOperand());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002435 __ push(FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
2436 // Emit function call.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002437 EmitCallWithStub(expr, NO_CALL_FUNCTION_FLAGS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002438 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002439
2440#ifdef DEBUG
2441 // RecordJSReturnSite should have been called.
2442 ASSERT(expr->return_is_recorded_);
2443#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002444}
2445
2446
2447void FullCodeGenerator::VisitCallNew(CallNew* expr) {
2448 Comment cmnt(masm_, "[ CallNew");
2449 // According to ECMA-262, section 11.2.2, page 44, the function
2450 // expression in new calls must be evaluated before the
2451 // arguments.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002452
ricow@chromium.org65fae842010-08-25 15:26:24 +00002453 // Push constructor on the stack. If it's not a function it's used as
2454 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2455 // ignored.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002456 VisitForStackValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002457
2458 // Push the arguments ("left-to-right") on the stack.
2459 ZoneList<Expression*>* args = expr->arguments();
2460 int arg_count = args->length();
2461 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002462 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002463 }
2464
2465 // Call the construct call builtin that handles allocation and
2466 // constructor invocation.
2467 SetSourcePosition(expr->position());
2468
ricow@chromium.org65fae842010-08-25 15:26:24 +00002469 // Load function and argument count into edi and eax.
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002470 __ Set(eax, Immediate(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002471 __ mov(edi, Operand(esp, arg_count * kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002472
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002473 // Record call targets in unoptimized code.
2474 Handle<Object> uninitialized =
2475 TypeFeedbackCells::UninitializedSentinel(isolate());
2476 Handle<JSGlobalPropertyCell> cell =
2477 isolate()->factory()->NewJSGlobalPropertyCell(uninitialized);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002478 RecordTypeFeedbackCell(expr->CallNewFeedbackId(), cell);
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002479 __ mov(ebx, cell);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002480
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002481 CallConstructStub stub(RECORD_CALL_TARGET);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002482 __ call(stub.GetCode(isolate()), RelocInfo::CONSTRUCT_CALL);
ulan@chromium.org967e2702012-02-28 09:49:15 +00002483 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002484 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002485}
2486
2487
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002488void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2489 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002490 ASSERT(args->length() == 1);
2491
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002492 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002493
2494 Label materialize_true, materialize_false;
2495 Label* if_true = NULL;
2496 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002497 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002498 context()->PrepareTest(&materialize_true, &materialize_false,
2499 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002500
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002501 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002502 __ test(eax, Immediate(kSmiTagMask));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002503 Split(zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002504
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002505 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002506}
2507
2508
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002509void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2510 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002511 ASSERT(args->length() == 1);
2512
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002513 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002514
2515 Label materialize_true, materialize_false;
2516 Label* if_true = NULL;
2517 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002518 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002519 context()->PrepareTest(&materialize_true, &materialize_false,
2520 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002521
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002522 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002523 __ test(eax, Immediate(kSmiTagMask | 0x80000000));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002524 Split(zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002525
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002526 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002527}
2528
2529
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002530void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2531 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002532 ASSERT(args->length() == 1);
2533
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002534 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002535
2536 Label materialize_true, materialize_false;
2537 Label* if_true = NULL;
2538 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002539 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002540 context()->PrepareTest(&materialize_true, &materialize_false,
2541 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002542
whesse@chromium.org7b260152011-06-20 15:33:18 +00002543 __ JumpIfSmi(eax, if_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002544 __ cmp(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002545 __ j(equal, if_true);
2546 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2547 // Undetectable objects behave like undefined when tested with typeof.
2548 __ movzx_b(ecx, FieldOperand(ebx, Map::kBitFieldOffset));
2549 __ test(ecx, Immediate(1 << Map::kIsUndetectable));
2550 __ j(not_zero, if_false);
2551 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceTypeOffset));
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002552 __ cmp(ecx, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002553 __ j(below, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002554 __ cmp(ecx, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002555 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002556 Split(below_equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002557
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002558 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002559}
2560
2561
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002562void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002563 // TODO(rossberg): incorporate symbols.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002564 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002565 ASSERT(args->length() == 1);
2566
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002567 VisitForAccumulatorValue(args->at(0));
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002568
2569 Label materialize_true, materialize_false;
2570 Label* if_true = NULL;
2571 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002572 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002573 context()->PrepareTest(&materialize_true, &materialize_false,
2574 &if_true, &if_false, &fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002575
whesse@chromium.org7b260152011-06-20 15:33:18 +00002576 __ JumpIfSmi(eax, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002577 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002578 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002579 Split(above_equal, if_true, if_false, fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002580
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002581 context()->Plug(if_true, if_false);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002582}
2583
2584
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002585void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2586 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002587 ASSERT(args->length() == 1);
2588
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002589 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002590
2591 Label materialize_true, materialize_false;
2592 Label* if_true = NULL;
2593 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002594 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002595 context()->PrepareTest(&materialize_true, &materialize_false,
2596 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002597
whesse@chromium.org7b260152011-06-20 15:33:18 +00002598 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002599 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2600 __ movzx_b(ebx, FieldOperand(ebx, Map::kBitFieldOffset));
2601 __ test(ebx, Immediate(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002602 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002603 Split(not_zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002604
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002605 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002606}
2607
2608
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002609void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002610 CallRuntime* expr) {
2611 ZoneList<Expression*>* args = expr->arguments();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002612 ASSERT(args->length() == 1);
2613
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002614 VisitForAccumulatorValue(args->at(0));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002615
2616 Label materialize_true, materialize_false;
2617 Label* if_true = NULL;
2618 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002619 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002620 context()->PrepareTest(&materialize_true, &materialize_false,
2621 &if_true, &if_false, &fall_through);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002622
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002623 __ AssertNotSmi(eax);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002624
2625 // Check whether this map has already been checked to be safe for default
2626 // valueOf.
2627 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2628 __ test_b(FieldOperand(ebx, Map::kBitField2Offset),
2629 1 << Map::kStringWrapperSafeForDefaultValueOf);
2630 __ j(not_zero, if_true);
2631
2632 // Check for fast case object. Return false for slow case objects.
2633 __ mov(ecx, FieldOperand(eax, JSObject::kPropertiesOffset));
2634 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
2635 __ cmp(ecx, FACTORY->hash_table_map());
2636 __ j(equal, if_false);
2637
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002638 // Look for valueOf string in the descriptor array, and indicate false if
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002639 // found. Since we omit an enumeration index check, if it is added via a
2640 // transition that shares its descriptor array, this is a false positive.
2641 Label entry, loop, done;
2642
2643 // Skip loop if no descriptors are valid.
2644 __ NumberOfOwnDescriptors(ecx, ebx);
2645 __ cmp(ecx, 0);
2646 __ j(equal, &done);
2647
danno@chromium.org40cb8782011-05-25 07:58:50 +00002648 __ LoadInstanceDescriptors(ebx, ebx);
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002649 // ebx: descriptor array.
2650 // ecx: valid entries in the descriptor array.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002651 // Calculate the end of the descriptor array.
2652 STATIC_ASSERT(kSmiTag == 0);
2653 STATIC_ASSERT(kSmiTagSize == 1);
2654 STATIC_ASSERT(kPointerSize == 4);
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002655 __ imul(ecx, ecx, DescriptorArray::kDescriptorSize);
2656 __ lea(ecx, Operand(ebx, ecx, times_2, DescriptorArray::kFirstOffset));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002657 // Calculate location of the first key name.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002658 __ add(ebx, Immediate(DescriptorArray::kFirstOffset));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002659 // Loop through all the keys in the descriptor array. If one of these is the
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002660 // internalized string "valueOf" the result is false.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002661 __ jmp(&entry);
2662 __ bind(&loop);
2663 __ mov(edx, FieldOperand(ebx, 0));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002664 __ cmp(edx, FACTORY->value_of_string());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002665 __ j(equal, if_false);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002666 __ add(ebx, Immediate(DescriptorArray::kDescriptorSize * kPointerSize));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002667 __ bind(&entry);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002668 __ cmp(ebx, ecx);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002669 __ j(not_equal, &loop);
2670
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002671 __ bind(&done);
2672
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002673 // Reload map as register ebx was used as temporary above.
2674 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2675
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002676 // If a valueOf property is not found on the object check that its
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002677 // prototype is the un-modified String prototype. If not result is false.
2678 __ mov(ecx, FieldOperand(ebx, Map::kPrototypeOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00002679 __ JumpIfSmi(ecx, if_false);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002680 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002681 __ mov(edx, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002682 __ mov(edx,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002683 FieldOperand(edx, GlobalObject::kNativeContextOffset));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002684 __ cmp(ecx,
2685 ContextOperand(edx,
2686 Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
2687 __ j(not_equal, if_false);
2688 // Set the bit in the map to indicate that it has been checked safe for
2689 // default valueOf and set true result.
2690 __ or_(FieldOperand(ebx, Map::kBitField2Offset),
2691 Immediate(1 << Map::kStringWrapperSafeForDefaultValueOf));
2692 __ jmp(if_true);
2693
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002694 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002695 context()->Plug(if_true, if_false);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002696}
2697
2698
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002699void FullCodeGenerator::EmitIsSymbol(CallRuntime* expr) {
2700 ZoneList<Expression*>* args = expr->arguments();
2701 ASSERT(args->length() == 1);
2702
2703 VisitForAccumulatorValue(args->at(0));
2704
2705 Label materialize_true, materialize_false;
2706 Label* if_true = NULL;
2707 Label* if_false = NULL;
2708 Label* fall_through = NULL;
2709 context()->PrepareTest(&materialize_true, &materialize_false,
2710 &if_true, &if_false, &fall_through);
2711
2712 __ JumpIfSmi(eax, if_false);
2713 __ CmpObjectType(eax, SYMBOL_TYPE, ebx);
2714 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
2715 Split(equal, if_true, if_false, fall_through);
2716
2717 context()->Plug(if_true, if_false);
2718}
2719
2720
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002721void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
2722 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002723 ASSERT(args->length() == 1);
2724
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002725 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002726
2727 Label materialize_true, materialize_false;
2728 Label* if_true = NULL;
2729 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002730 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002731 context()->PrepareTest(&materialize_true, &materialize_false,
2732 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002733
whesse@chromium.org7b260152011-06-20 15:33:18 +00002734 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002735 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002736 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002737 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002738
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002739 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002740}
2741
2742
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002743void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2744 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002745 ASSERT(args->length() == 1);
2746
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002747 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002748
2749 Label materialize_true, materialize_false;
2750 Label* if_true = NULL;
2751 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002752 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002753 context()->PrepareTest(&materialize_true, &materialize_false,
2754 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002755
whesse@chromium.org7b260152011-06-20 15:33:18 +00002756 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002757 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002758 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002759 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002760
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002761 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002762}
2763
2764
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002765void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2766 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002767 ASSERT(args->length() == 1);
2768
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002769 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002770
2771 Label materialize_true, materialize_false;
2772 Label* if_true = NULL;
2773 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002774 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002775 context()->PrepareTest(&materialize_true, &materialize_false,
2776 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002777
whesse@chromium.org7b260152011-06-20 15:33:18 +00002778 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002779 __ CmpObjectType(eax, JS_REGEXP_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002780 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002781 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002782
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002783 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002784}
2785
2786
2787
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002788void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
2789 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002790
2791 Label materialize_true, materialize_false;
2792 Label* if_true = NULL;
2793 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002794 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002795 context()->PrepareTest(&materialize_true, &materialize_false,
2796 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002797
2798 // Get the frame pointer for the calling frame.
2799 __ mov(eax, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2800
2801 // Skip the arguments adaptor frame if it exists.
2802 Label check_frame_marker;
2803 __ cmp(Operand(eax, StandardFrameConstants::kContextOffset),
2804 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2805 __ j(not_equal, &check_frame_marker);
2806 __ mov(eax, Operand(eax, StandardFrameConstants::kCallerFPOffset));
2807
2808 // Check the marker in the calling frame.
2809 __ bind(&check_frame_marker);
2810 __ cmp(Operand(eax, StandardFrameConstants::kMarkerOffset),
2811 Immediate(Smi::FromInt(StackFrame::CONSTRUCT)));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002812 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002813 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002814
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002815 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002816}
2817
2818
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002819void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
2820 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002821 ASSERT(args->length() == 2);
2822
2823 // Load the two objects into registers and perform the comparison.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002824 VisitForStackValue(args->at(0));
2825 VisitForAccumulatorValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002826
2827 Label materialize_true, materialize_false;
2828 Label* if_true = NULL;
2829 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002830 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002831 context()->PrepareTest(&materialize_true, &materialize_false,
2832 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002833
2834 __ pop(ebx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002835 __ cmp(eax, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002836 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002837 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002838
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002839 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002840}
2841
2842
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002843void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
2844 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002845 ASSERT(args->length() == 1);
2846
2847 // ArgumentsAccessStub expects the key in edx and the formal
2848 // parameter count in eax.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002849 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002850 __ mov(edx, eax);
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002851 __ Set(eax, Immediate(Smi::FromInt(info_->scope()->num_parameters())));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002852 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
2853 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002854 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002855}
2856
2857
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002858void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
2859 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002860
2861 Label exit;
2862 // Get the number of formal parameters.
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002863 __ Set(eax, Immediate(Smi::FromInt(info_->scope()->num_parameters())));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002864
2865 // Check if the calling frame is an arguments adaptor frame.
2866 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2867 __ cmp(Operand(ebx, StandardFrameConstants::kContextOffset),
2868 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2869 __ j(not_equal, &exit);
2870
2871 // Arguments adaptor case: Read the arguments length from the
2872 // adaptor frame.
2873 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2874
2875 __ bind(&exit);
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002876 __ AssertSmi(eax);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002877 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002878}
2879
2880
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002881void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2882 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002883 ASSERT(args->length() == 1);
2884 Label done, null, function, non_function_constructor;
2885
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002886 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002887
2888 // If the object is a smi, we return null.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002889 __ JumpIfSmi(eax, &null);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002890
2891 // Check that the object is a JS object but take special care of JS
2892 // functions to make sure they have 'Function' as their class.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002893 // Assume that there are only two callable types, and one of them is at
2894 // either end of the type range for JS object types. Saves extra comparisons.
2895 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002896 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, eax);
2897 // Map is now in eax.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002898 __ j(below, &null);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002899 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2900 FIRST_SPEC_OBJECT_TYPE + 1);
2901 __ j(equal, &function);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002902
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002903 __ CmpInstanceType(eax, LAST_SPEC_OBJECT_TYPE);
2904 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2905 LAST_SPEC_OBJECT_TYPE - 1);
2906 __ j(equal, &function);
2907 // Assume that there is no larger type.
2908 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002909
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002910 // Check if the constructor in the map is a JS function.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002911 __ mov(eax, FieldOperand(eax, Map::kConstructorOffset));
2912 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
2913 __ j(not_equal, &non_function_constructor);
2914
2915 // eax now contains the constructor function. Grab the
2916 // instance class name from there.
2917 __ mov(eax, FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset));
2918 __ mov(eax, FieldOperand(eax, SharedFunctionInfo::kInstanceClassNameOffset));
2919 __ jmp(&done);
2920
2921 // Functions have class 'Function'.
2922 __ bind(&function);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002923 __ mov(eax, isolate()->factory()->function_class_string());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002924 __ jmp(&done);
2925
2926 // Objects with a non-function constructor have class 'Object'.
2927 __ bind(&non_function_constructor);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002928 __ mov(eax, isolate()->factory()->Object_string());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002929 __ jmp(&done);
2930
2931 // Non-JS objects have class null.
2932 __ bind(&null);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002933 __ mov(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002934
2935 // All done.
2936 __ bind(&done);
2937
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002938 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002939}
2940
2941
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002942void FullCodeGenerator::EmitLog(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002943 // Conditionally generate a log call.
2944 // Args:
2945 // 0 (literal string): The type of logging (corresponds to the flags).
2946 // This is used to determine whether or not to generate the log call.
2947 // 1 (string): Format string. Access the string at argument index 2
2948 // with '%2s' (see Logger::LogRuntime for all the formats).
2949 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002950 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002951 ASSERT_EQ(args->length(), 3);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002952 if (CodeGenerator::ShouldGenerateLog(args->at(0))) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002953 VisitForStackValue(args->at(1));
2954 VisitForStackValue(args->at(2));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002955 __ CallRuntime(Runtime::kLog, 2);
2956 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002957 // Finally, we're expected to leave a value on the top of the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002958 __ mov(eax, isolate()->factory()->undefined_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002959 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002960}
2961
2962
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002963void FullCodeGenerator::EmitRandomHeapNumber(CallRuntime* expr) {
2964 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002965
2966 Label slow_allocate_heapnumber;
2967 Label heapnumber_allocated;
2968
2969 __ AllocateHeapNumber(edi, ebx, ecx, &slow_allocate_heapnumber);
2970 __ jmp(&heapnumber_allocated);
2971
2972 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002973 // Allocate a heap number.
2974 __ CallRuntime(Runtime::kNumberAlloc, 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002975 __ mov(edi, eax);
2976
2977 __ bind(&heapnumber_allocated);
2978
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002979 __ PrepareCallCFunction(1, ebx);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002980 __ mov(eax, ContextOperand(context_register(), Context::GLOBAL_OBJECT_INDEX));
2981 __ mov(eax, FieldOperand(eax, GlobalObject::kNativeContextOffset));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002982 __ mov(Operand(esp, 0), eax);
2983 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002984
2985 // Convert 32 random bits in eax to 0.(32 random bits) in a double
2986 // by computing:
2987 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
2988 // This is implemented on both SSE2 and FPU.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002989 if (CpuFeatures::IsSupported(SSE2)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002990 CpuFeatures::Scope fscope(SSE2);
2991 __ mov(ebx, Immediate(0x49800000)); // 1.0 x 2^20 as single.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002992 __ movd(xmm1, ebx);
2993 __ movd(xmm0, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002994 __ cvtss2sd(xmm1, xmm1);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002995 __ xorps(xmm0, xmm1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002996 __ subsd(xmm0, xmm1);
2997 __ movdbl(FieldOperand(edi, HeapNumber::kValueOffset), xmm0);
2998 } else {
2999 // 0x4130000000000000 is 1.0 x 2^20 as a double.
3000 __ mov(FieldOperand(edi, HeapNumber::kExponentOffset),
3001 Immediate(0x41300000));
3002 __ mov(FieldOperand(edi, HeapNumber::kMantissaOffset), eax);
3003 __ fld_d(FieldOperand(edi, HeapNumber::kValueOffset));
3004 __ mov(FieldOperand(edi, HeapNumber::kMantissaOffset), Immediate(0));
3005 __ fld_d(FieldOperand(edi, HeapNumber::kValueOffset));
3006 __ fsubp(1);
3007 __ fstp_d(FieldOperand(edi, HeapNumber::kValueOffset));
3008 }
3009 __ mov(eax, edi);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003010 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003011}
3012
3013
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003014void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003015 // Load the arguments on the stack and call the stub.
3016 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003017 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003018 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003019 VisitForStackValue(args->at(0));
3020 VisitForStackValue(args->at(1));
3021 VisitForStackValue(args->at(2));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003022 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003023 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003024}
3025
3026
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003027void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003028 // Load the arguments on the stack and call the stub.
3029 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003030 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003031 ASSERT(args->length() == 4);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003032 VisitForStackValue(args->at(0));
3033 VisitForStackValue(args->at(1));
3034 VisitForStackValue(args->at(2));
3035 VisitForStackValue(args->at(3));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003036 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003037 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003038}
3039
3040
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003041void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3042 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003043 ASSERT(args->length() == 1);
3044
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003045 VisitForAccumulatorValue(args->at(0)); // Load the object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003046
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003047 Label done;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003048 // If the object is a smi return the object.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003049 __ JumpIfSmi(eax, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003050 // If the object is not a value type, return the object.
3051 __ CmpObjectType(eax, JS_VALUE_TYPE, ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003052 __ j(not_equal, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003053 __ mov(eax, FieldOperand(eax, JSValue::kValueOffset));
3054
3055 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003056 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003057}
3058
3059
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003060void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3061 ZoneList<Expression*>* args = expr->arguments();
3062 ASSERT(args->length() == 2);
3063 ASSERT_NE(NULL, args->at(1)->AsLiteral());
3064 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->handle()));
3065
3066 VisitForAccumulatorValue(args->at(0)); // Load the object.
3067
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003068 Label runtime, done, not_date_object;
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003069 Register object = eax;
3070 Register result = eax;
3071 Register scratch = ecx;
3072
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003073 __ JumpIfSmi(object, &not_date_object);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003074 __ CmpObjectType(object, JS_DATE_TYPE, scratch);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003075 __ j(not_equal, &not_date_object);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003076
3077 if (index->value() == 0) {
3078 __ mov(result, FieldOperand(object, JSDate::kValueOffset));
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003079 __ jmp(&done);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003080 } else {
3081 if (index->value() < JSDate::kFirstUncachedField) {
3082 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3083 __ mov(scratch, Operand::StaticVariable(stamp));
3084 __ cmp(scratch, FieldOperand(object, JSDate::kCacheStampOffset));
3085 __ j(not_equal, &runtime, Label::kNear);
3086 __ mov(result, FieldOperand(object, JSDate::kValueOffset +
3087 kPointerSize * index->value()));
3088 __ jmp(&done);
3089 }
3090 __ bind(&runtime);
3091 __ PrepareCallCFunction(2, scratch);
3092 __ mov(Operand(esp, 0), object);
3093 __ mov(Operand(esp, 1 * kPointerSize), Immediate(index));
3094 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003095 __ jmp(&done);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003096 }
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003097
3098 __ bind(&not_date_object);
3099 __ CallRuntime(Runtime::kThrowNotDateError, 0);
3100 __ bind(&done);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003101 context()->Plug(result);
3102}
3103
3104
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003105void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3106 ZoneList<Expression*>* args = expr->arguments();
3107 ASSERT_EQ(3, args->length());
3108
3109 VisitForStackValue(args->at(1)); // index
3110 VisitForStackValue(args->at(2)); // value
3111 __ pop(ecx);
3112 __ pop(ebx);
3113 VisitForAccumulatorValue(args->at(0)); // string
3114
3115 static const String::Encoding encoding = String::ONE_BYTE_ENCODING;
3116 SeqStringSetCharGenerator::Generate(masm_, encoding, eax, ebx, ecx);
3117 context()->Plug(eax);
3118}
3119
3120
3121void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3122 ZoneList<Expression*>* args = expr->arguments();
3123 ASSERT_EQ(3, args->length());
3124
3125 VisitForStackValue(args->at(1)); // index
3126 VisitForStackValue(args->at(2)); // value
3127 __ pop(ecx);
3128 __ pop(ebx);
3129 VisitForAccumulatorValue(args->at(0)); // string
3130
3131 static const String::Encoding encoding = String::TWO_BYTE_ENCODING;
3132 SeqStringSetCharGenerator::Generate(masm_, encoding, eax, ebx, ecx);
3133 context()->Plug(eax);
3134}
3135
3136
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003137void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003138 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003139 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003140 ASSERT(args->length() == 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003141 VisitForStackValue(args->at(0));
3142 VisitForStackValue(args->at(1));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003143
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003144 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003145 MathPowStub stub(MathPowStub::ON_STACK);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003146 __ CallStub(&stub);
3147 } else {
3148 __ CallRuntime(Runtime::kMath_pow, 2);
3149 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003150 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003151}
3152
3153
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003154void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3155 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003156 ASSERT(args->length() == 2);
3157
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003158 VisitForStackValue(args->at(0)); // Load the object.
3159 VisitForAccumulatorValue(args->at(1)); // Load the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003160 __ pop(ebx); // eax = value. ebx = object.
3161
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003162 Label done;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003163 // If the object is a smi, return the value.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003164 __ JumpIfSmi(ebx, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003165
3166 // If the object is not a value type, return the value.
3167 __ CmpObjectType(ebx, JS_VALUE_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003168 __ j(not_equal, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003169
3170 // Store the value.
3171 __ mov(FieldOperand(ebx, JSValue::kValueOffset), eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003172
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003173 // Update the write barrier. Save the value as it will be
3174 // overwritten by the write barrier code and is needed afterward.
3175 __ mov(edx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003176 __ RecordWriteField(ebx, JSValue::kValueOffset, edx, ecx, kDontSaveFPRegs);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003177
3178 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003179 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003180}
3181
3182
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003183void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3184 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003185 ASSERT_EQ(args->length(), 1);
3186
3187 // Load the argument on the stack and call the stub.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003188 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003189
3190 NumberToStringStub stub;
3191 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003192 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003193}
3194
3195
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003196void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3197 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003198 ASSERT(args->length() == 1);
3199
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003200 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003201
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003202 Label done;
3203 StringCharFromCodeGenerator generator(eax, ebx);
3204 generator.GenerateFast(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003205 __ jmp(&done);
3206
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003207 NopRuntimeCallHelper call_helper;
3208 generator.GenerateSlow(masm_, call_helper);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003209
3210 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003211 context()->Plug(ebx);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003212}
3213
3214
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003215void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3216 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003217 ASSERT(args->length() == 2);
3218
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003219 VisitForStackValue(args->at(0));
3220 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003221
3222 Register object = ebx;
3223 Register index = eax;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003224 Register result = edx;
3225
3226 __ pop(object);
3227
3228 Label need_conversion;
3229 Label index_out_of_range;
3230 Label done;
3231 StringCharCodeAtGenerator generator(object,
3232 index,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003233 result,
3234 &need_conversion,
3235 &need_conversion,
3236 &index_out_of_range,
3237 STRING_INDEX_IS_NUMBER);
3238 generator.GenerateFast(masm_);
3239 __ jmp(&done);
3240
3241 __ bind(&index_out_of_range);
3242 // When the index is out of range, the spec requires us to return
3243 // NaN.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003244 __ Set(result, Immediate(isolate()->factory()->nan_value()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003245 __ jmp(&done);
3246
3247 __ bind(&need_conversion);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003248 // Move the undefined value into the result register, which will
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003249 // trigger conversion.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003250 __ Set(result, Immediate(isolate()->factory()->undefined_value()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003251 __ jmp(&done);
3252
3253 NopRuntimeCallHelper call_helper;
3254 generator.GenerateSlow(masm_, call_helper);
3255
3256 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003257 context()->Plug(result);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003258}
3259
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003260
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003261void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3262 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003263 ASSERT(args->length() == 2);
3264
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003265 VisitForStackValue(args->at(0));
3266 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003267
3268 Register object = ebx;
3269 Register index = eax;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003270 Register scratch = edx;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003271 Register result = eax;
3272
3273 __ pop(object);
3274
3275 Label need_conversion;
3276 Label index_out_of_range;
3277 Label done;
3278 StringCharAtGenerator generator(object,
3279 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003280 scratch,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003281 result,
3282 &need_conversion,
3283 &need_conversion,
3284 &index_out_of_range,
3285 STRING_INDEX_IS_NUMBER);
3286 generator.GenerateFast(masm_);
3287 __ jmp(&done);
3288
3289 __ bind(&index_out_of_range);
3290 // When the index is out of range, the spec requires us to return
3291 // the empty string.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003292 __ Set(result, Immediate(isolate()->factory()->empty_string()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003293 __ jmp(&done);
3294
3295 __ bind(&need_conversion);
3296 // Move smi zero into the result register, which will trigger
3297 // conversion.
3298 __ Set(result, Immediate(Smi::FromInt(0)));
3299 __ jmp(&done);
3300
3301 NopRuntimeCallHelper call_helper;
3302 generator.GenerateSlow(masm_, call_helper);
3303
3304 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003305 context()->Plug(result);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003306}
3307
3308
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003309void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3310 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003311 ASSERT_EQ(2, args->length());
3312
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003313 VisitForStackValue(args->at(0));
3314 VisitForStackValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003315
3316 StringAddStub stub(NO_STRING_ADD_FLAGS);
3317 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003318 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003319}
3320
3321
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003322void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3323 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003324 ASSERT_EQ(2, args->length());
3325
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003326 VisitForStackValue(args->at(0));
3327 VisitForStackValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003328
3329 StringCompareStub stub;
3330 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003331 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003332}
3333
3334
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003335void FullCodeGenerator::EmitMathSin(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003336 // Load the argument on the stack and call the stub.
whesse@chromium.org023421e2010-12-21 12:19:12 +00003337 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3338 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003339 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003340 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003341 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003342 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003343 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003344}
3345
3346
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003347void FullCodeGenerator::EmitMathCos(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003348 // Load the argument on the stack and call the stub.
whesse@chromium.org023421e2010-12-21 12:19:12 +00003349 TranscendentalCacheStub stub(TranscendentalCache::COS,
3350 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003351 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003352 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003353 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003354 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003355 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003356}
3357
3358
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003359void FullCodeGenerator::EmitMathTan(CallRuntime* expr) {
3360 // Load the argument on the stack and call the stub.
3361 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3362 TranscendentalCacheStub::TAGGED);
3363 ZoneList<Expression*>* args = expr->arguments();
3364 ASSERT(args->length() == 1);
3365 VisitForStackValue(args->at(0));
3366 __ CallStub(&stub);
3367 context()->Plug(eax);
3368}
3369
3370
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003371void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003372 // Load the argument on the stack and call the stub.
whesse@chromium.org023421e2010-12-21 12:19:12 +00003373 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3374 TranscendentalCacheStub::TAGGED);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003375 ZoneList<Expression*>* args = expr->arguments();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003376 ASSERT(args->length() == 1);
3377 VisitForStackValue(args->at(0));
3378 __ CallStub(&stub);
3379 context()->Plug(eax);
3380}
3381
3382
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003383void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003384 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003385 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003386 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003387 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003388 __ CallRuntime(Runtime::kMath_sqrt, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003389 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003390}
3391
3392
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003393void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3394 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003395 ASSERT(args->length() >= 2);
3396
danno@chromium.org160a7b02011-04-18 15:51:38 +00003397 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3398 for (int i = 0; i < arg_count + 1; ++i) {
3399 VisitForStackValue(args->at(i));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003400 }
danno@chromium.org160a7b02011-04-18 15:51:38 +00003401 VisitForAccumulatorValue(args->last()); // Function.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003402
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00003403 Label runtime, done;
3404 // Check for non-function argument (including proxy).
3405 __ JumpIfSmi(eax, &runtime);
3406 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
3407 __ j(not_equal, &runtime);
danno@chromium.orgc612e022011-11-10 11:38:15 +00003408
danno@chromium.org160a7b02011-04-18 15:51:38 +00003409 // InvokeFunction requires the function in edi. Move it in there.
3410 __ mov(edi, result_register());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003411 ParameterCount count(arg_count);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003412 __ InvokeFunction(edi, count, CALL_FUNCTION,
3413 NullCallWrapper(), CALL_AS_METHOD);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003414 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003415 __ jmp(&done);
3416
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00003417 __ bind(&runtime);
danno@chromium.orgc612e022011-11-10 11:38:15 +00003418 __ push(eax);
3419 __ CallRuntime(Runtime::kCall, args->length());
3420 __ bind(&done);
3421
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003422 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003423}
3424
3425
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003426void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003427 // Load the arguments on the stack and call the stub.
3428 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003429 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003430 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003431 VisitForStackValue(args->at(0));
3432 VisitForStackValue(args->at(1));
3433 VisitForStackValue(args->at(2));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003434 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003435 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003436}
3437
3438
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003439void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3440 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003441 ASSERT_EQ(2, args->length());
3442
3443 ASSERT_NE(NULL, args->at(0)->AsLiteral());
3444 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
3445
3446 Handle<FixedArray> jsfunction_result_caches(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003447 isolate()->native_context()->jsfunction_result_caches());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003448 if (jsfunction_result_caches->length() <= cache_id) {
3449 __ Abort("Attempt to use undefined cache.");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003450 __ mov(eax, isolate()->factory()->undefined_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003451 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003452 return;
3453 }
3454
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003455 VisitForAccumulatorValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003456
3457 Register key = eax;
3458 Register cache = ebx;
3459 Register tmp = ecx;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003460 __ mov(cache, ContextOperand(esi, Context::GLOBAL_OBJECT_INDEX));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003461 __ mov(cache,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003462 FieldOperand(cache, GlobalObject::kNativeContextOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003463 __ mov(cache, ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003464 __ mov(cache,
3465 FieldOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3466
3467 Label done, not_found;
3468 // tmp now holds finger offset as a smi.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003469 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003470 __ mov(tmp, FieldOperand(cache, JSFunctionResultCache::kFingerOffset));
3471 __ cmp(key, CodeGenerator::FixedArrayElementOperand(cache, tmp));
3472 __ j(not_equal, &not_found);
3473
3474 __ mov(eax, CodeGenerator::FixedArrayElementOperand(cache, tmp, 1));
3475 __ jmp(&done);
3476
3477 __ bind(&not_found);
3478 // Call runtime to perform the lookup.
3479 __ push(cache);
3480 __ push(key);
3481 __ CallRuntime(Runtime::kGetFromCache, 2);
3482
3483 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003484 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003485}
3486
3487
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003488void FullCodeGenerator::EmitIsRegExpEquivalent(CallRuntime* expr) {
3489 ZoneList<Expression*>* args = expr->arguments();
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003490 ASSERT_EQ(2, args->length());
3491
3492 Register right = eax;
3493 Register left = ebx;
3494 Register tmp = ecx;
3495
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003496 VisitForStackValue(args->at(0));
3497 VisitForAccumulatorValue(args->at(1));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003498 __ pop(left);
3499
3500 Label done, fail, ok;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003501 __ cmp(left, right);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003502 __ j(equal, &ok);
3503 // Fail if either is a non-HeapObject.
3504 __ mov(tmp, left);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003505 __ and_(tmp, right);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003506 __ JumpIfSmi(tmp, &fail);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003507 __ mov(tmp, FieldOperand(left, HeapObject::kMapOffset));
3508 __ CmpInstanceType(tmp, JS_REGEXP_TYPE);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003509 __ j(not_equal, &fail);
3510 __ cmp(tmp, FieldOperand(right, HeapObject::kMapOffset));
3511 __ j(not_equal, &fail);
3512 __ mov(tmp, FieldOperand(left, JSRegExp::kDataOffset));
3513 __ cmp(tmp, FieldOperand(right, JSRegExp::kDataOffset));
3514 __ j(equal, &ok);
3515 __ bind(&fail);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003516 __ mov(eax, Immediate(isolate()->factory()->false_value()));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003517 __ jmp(&done);
3518 __ bind(&ok);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003519 __ mov(eax, Immediate(isolate()->factory()->true_value()));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003520 __ bind(&done);
3521
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003522 context()->Plug(eax);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003523}
3524
3525
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003526void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3527 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003528 ASSERT(args->length() == 1);
3529
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003530 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003531
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003532 __ AssertString(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003533
3534 Label materialize_true, materialize_false;
3535 Label* if_true = NULL;
3536 Label* if_false = NULL;
3537 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003538 context()->PrepareTest(&materialize_true, &materialize_false,
3539 &if_true, &if_false, &fall_through);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003540
3541 __ test(FieldOperand(eax, String::kHashFieldOffset),
3542 Immediate(String::kContainsCachedArrayIndexMask));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003543 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003544 Split(zero, if_true, if_false, fall_through);
3545
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003546 context()->Plug(if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003547}
3548
3549
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003550void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3551 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003552 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003553 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003554
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003555 __ AssertString(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003556
3557 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
3558 __ IndexFromHash(eax, eax);
3559
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003560 context()->Plug(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003561}
3562
3563
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003564void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003565 Label bailout, done, one_char_separator, long_separator,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003566 non_trivial_array, not_size_one_array, loop,
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003567 loop_1, loop_1_condition, loop_2, loop_2_entry, loop_3, loop_3_entry;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003568
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003569 ZoneList<Expression*>* args = expr->arguments();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003570 ASSERT(args->length() == 2);
3571 // We will leave the separator on the stack until the end of the function.
3572 VisitForStackValue(args->at(1));
3573 // Load this to eax (= array)
3574 VisitForAccumulatorValue(args->at(0));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003575 // All aliases of the same register have disjoint lifetimes.
3576 Register array = eax;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003577 Register elements = no_reg; // Will be eax.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003578
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003579 Register index = edx;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003580
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003581 Register string_length = ecx;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003582
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003583 Register string = esi;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003584
3585 Register scratch = ebx;
3586
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003587 Register array_length = edi;
3588 Register result_pos = no_reg; // Will be edi.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003589
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003590 // Separator operand is already pushed.
3591 Operand separator_operand = Operand(esp, 2 * kPointerSize);
3592 Operand result_operand = Operand(esp, 1 * kPointerSize);
3593 Operand array_length_operand = Operand(esp, 0);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003594 __ sub(esp, Immediate(2 * kPointerSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003595 __ cld();
3596 // Check that the array is a JSArray
whesse@chromium.org7b260152011-06-20 15:33:18 +00003597 __ JumpIfSmi(array, &bailout);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003598 __ CmpObjectType(array, JS_ARRAY_TYPE, scratch);
3599 __ j(not_equal, &bailout);
3600
3601 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003602 __ CheckFastElements(scratch, &bailout);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003603
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003604 // If the array has length zero, return the empty string.
3605 __ mov(array_length, FieldOperand(array, JSArray::kLengthOffset));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003606 __ SmiUntag(array_length);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003607 __ j(not_zero, &non_trivial_array);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003608 __ mov(result_operand, isolate()->factory()->empty_string());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003609 __ jmp(&done);
3610
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003611 // Save the array length.
3612 __ bind(&non_trivial_array);
3613 __ mov(array_length_operand, array_length);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003614
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003615 // Save the FixedArray containing array's elements.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003616 // End of array's live range.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003617 elements = array;
3618 __ mov(elements, FieldOperand(array, JSArray::kElementsOffset));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003619 array = no_reg;
3620
3621
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003622 // Check that all array elements are sequential ASCII strings, and
3623 // accumulate the sum of their lengths, as a smi-encoded value.
3624 __ Set(index, Immediate(0));
3625 __ Set(string_length, Immediate(0));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003626 // Loop condition: while (index < length).
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003627 // Live loop registers: index, array_length, string,
3628 // scratch, string_length, elements.
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00003629 if (generate_debug_code_) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003630 __ cmp(index, array_length);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003631 __ Assert(less, "No empty arrays here in EmitFastAsciiArrayJoin");
3632 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003633 __ bind(&loop);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003634 __ mov(string, FieldOperand(elements,
3635 index,
3636 times_pointer_size,
3637 FixedArray::kHeaderSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003638 __ JumpIfSmi(string, &bailout);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003639 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
3640 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
3641 __ and_(scratch, Immediate(
3642 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003643 __ cmp(scratch, kStringTag | kOneByteStringTag | kSeqStringTag);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003644 __ j(not_equal, &bailout);
3645 __ add(string_length,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003646 FieldOperand(string, SeqOneByteString::kLengthOffset));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003647 __ j(overflow, &bailout);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003648 __ add(index, Immediate(1));
3649 __ cmp(index, array_length);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003650 __ j(less, &loop);
3651
3652 // If array_length is 1, return elements[0], a string.
3653 __ cmp(array_length, 1);
3654 __ j(not_equal, &not_size_one_array);
3655 __ mov(scratch, FieldOperand(elements, FixedArray::kHeaderSize));
3656 __ mov(result_operand, scratch);
3657 __ jmp(&done);
3658
3659 __ bind(&not_size_one_array);
3660
3661 // End of array_length live range.
3662 result_pos = array_length;
3663 array_length = no_reg;
3664
3665 // Live registers:
3666 // string_length: Sum of string lengths, as a smi.
3667 // elements: FixedArray of strings.
3668
3669 // Check that the separator is a flat ASCII string.
3670 __ mov(string, separator_operand);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003671 __ JumpIfSmi(string, &bailout);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003672 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
3673 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003674 __ and_(scratch, Immediate(
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003675 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003676 __ cmp(scratch, ASCII_STRING_TYPE);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003677 __ j(not_equal, &bailout);
3678
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003679 // Add (separator length times array_length) - separator length
3680 // to string_length.
3681 __ mov(scratch, separator_operand);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003682 __ mov(scratch, FieldOperand(scratch, SeqOneByteString::kLengthOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003683 __ sub(string_length, scratch); // May be negative, temporarily.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003684 __ imul(scratch, array_length_operand);
3685 __ j(overflow, &bailout);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003686 __ add(string_length, scratch);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003687 __ j(overflow, &bailout);
3688
3689 __ shr(string_length, 1);
3690 // Live registers and stack values:
3691 // string_length
3692 // elements
3693 __ AllocateAsciiString(result_pos, string_length, scratch,
3694 index, string, &bailout);
3695 __ mov(result_operand, result_pos);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003696 __ lea(result_pos, FieldOperand(result_pos, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003697
3698
3699 __ mov(string, separator_operand);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003700 __ cmp(FieldOperand(string, SeqOneByteString::kLengthOffset),
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003701 Immediate(Smi::FromInt(1)));
3702 __ j(equal, &one_char_separator);
3703 __ j(greater, &long_separator);
3704
3705
3706 // Empty separator case
3707 __ mov(index, Immediate(0));
3708 __ jmp(&loop_1_condition);
3709 // Loop condition: while (index < length).
3710 __ bind(&loop_1);
3711 // Each iteration of the loop concatenates one string to the result.
3712 // Live values in registers:
3713 // index: which element of the elements array we are adding to the result.
3714 // result_pos: the position to which we are currently copying characters.
3715 // elements: the FixedArray of strings we are joining.
3716
3717 // Get string = array[index].
3718 __ mov(string, FieldOperand(elements, index,
3719 times_pointer_size,
3720 FixedArray::kHeaderSize));
3721 __ mov(string_length,
3722 FieldOperand(string, String::kLengthOffset));
3723 __ shr(string_length, 1);
3724 __ lea(string,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003725 FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003726 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003727 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003728 __ bind(&loop_1_condition);
3729 __ cmp(index, array_length_operand);
3730 __ j(less, &loop_1); // End while (index < length).
3731 __ jmp(&done);
3732
3733
3734
3735 // One-character separator case
3736 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003737 // Replace separator with its ASCII character value.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003738 __ mov_b(scratch, FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003739 __ mov_b(separator_operand, scratch);
3740
3741 __ Set(index, Immediate(0));
3742 // Jump into the loop after the code that copies the separator, so the first
3743 // element is not preceded by a separator
3744 __ jmp(&loop_2_entry);
3745 // Loop condition: while (index < length).
3746 __ bind(&loop_2);
3747 // Each iteration of the loop concatenates one string to the result.
3748 // Live values in registers:
3749 // index: which element of the elements array we are adding to the result.
3750 // result_pos: the position to which we are currently copying characters.
3751
3752 // Copy the separator character to the result.
3753 __ mov_b(scratch, separator_operand);
3754 __ mov_b(Operand(result_pos, 0), scratch);
3755 __ inc(result_pos);
3756
3757 __ bind(&loop_2_entry);
3758 // Get string = array[index].
3759 __ mov(string, FieldOperand(elements, index,
3760 times_pointer_size,
3761 FixedArray::kHeaderSize));
3762 __ mov(string_length,
3763 FieldOperand(string, String::kLengthOffset));
3764 __ shr(string_length, 1);
3765 __ lea(string,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003766 FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003767 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003768 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003769
3770 __ cmp(index, array_length_operand);
3771 __ j(less, &loop_2); // End while (index < length).
3772 __ jmp(&done);
3773
3774
3775 // Long separator case (separator is more than one character).
3776 __ bind(&long_separator);
3777
3778 __ Set(index, Immediate(0));
3779 // Jump into the loop after the code that copies the separator, so the first
3780 // element is not preceded by a separator
3781 __ jmp(&loop_3_entry);
3782 // Loop condition: while (index < length).
3783 __ bind(&loop_3);
3784 // Each iteration of the loop concatenates one string to the result.
3785 // Live values in registers:
3786 // index: which element of the elements array we are adding to the result.
3787 // result_pos: the position to which we are currently copying characters.
3788
3789 // Copy the separator to the result.
3790 __ mov(string, separator_operand);
3791 __ mov(string_length,
3792 FieldOperand(string, String::kLengthOffset));
3793 __ shr(string_length, 1);
3794 __ lea(string,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003795 FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003796 __ CopyBytes(string, result_pos, string_length, scratch);
3797
3798 __ bind(&loop_3_entry);
3799 // Get string = array[index].
3800 __ mov(string, FieldOperand(elements, index,
3801 times_pointer_size,
3802 FixedArray::kHeaderSize));
3803 __ mov(string_length,
3804 FieldOperand(string, String::kLengthOffset));
3805 __ shr(string_length, 1);
3806 __ lea(string,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003807 FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003808 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003809 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003810
3811 __ cmp(index, array_length_operand);
3812 __ j(less, &loop_3); // End while (index < length).
3813 __ jmp(&done);
3814
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003815
3816 __ bind(&bailout);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003817 __ mov(result_operand, isolate()->factory()->undefined_value());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003818 __ bind(&done);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003819 __ mov(eax, result_operand);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003820 // Drop temp values from the stack, and restore context register.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003821 __ add(esp, Immediate(3 * kPointerSize));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003822
3823 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
3824 context()->Plug(eax);
3825}
3826
3827
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003828void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003829 Handle<String> name = expr->name();
3830 if (name->length() > 0 && name->Get(0) == '_') {
3831 Comment cmnt(masm_, "[ InlineRuntimeCall");
3832 EmitInlineRuntimeCall(expr);
3833 return;
3834 }
3835
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003836 Comment cmnt(masm_, "[ CallRuntime");
3837 ZoneList<Expression*>* args = expr->arguments();
3838
3839 if (expr->is_jsruntime()) {
3840 // Prepare for calling JS runtime function.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003841 __ mov(eax, GlobalObjectOperand());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003842 __ push(FieldOperand(eax, GlobalObject::kBuiltinsOffset));
3843 }
3844
3845 // Push the arguments ("left-to-right").
3846 int arg_count = args->length();
3847 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003848 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003849 }
3850
3851 if (expr->is_jsruntime()) {
3852 // Call the JS runtime function via a call IC.
3853 __ Set(ecx, Immediate(expr->name()));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003854 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
lrn@chromium.org34e60782011-09-15 07:25:40 +00003855 Handle<Code> ic =
3856 isolate()->stub_cache()->ComputeCallInitialize(arg_count, mode);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00003857 CallIC(ic, mode, expr->CallRuntimeFeedbackId());
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003858 // Restore context register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003859 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
3860 } else {
3861 // Call the C runtime function.
3862 __ CallRuntime(expr->function(), arg_count);
3863 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003864 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003865}
3866
3867
3868void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
3869 switch (expr->op()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003870 case Token::DELETE: {
3871 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003872 Property* property = expr->expression()->AsProperty();
3873 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003874
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003875 if (property != NULL) {
3876 VisitForStackValue(property->obj());
3877 VisitForStackValue(property->key());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003878 StrictModeFlag strict_mode_flag = (language_mode() == CLASSIC_MODE)
3879 ? kNonStrictMode : kStrictMode;
3880 __ push(Immediate(Smi::FromInt(strict_mode_flag)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003881 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003882 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003883 } else if (proxy != NULL) {
3884 Variable* var = proxy->var();
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003885 // Delete of an unqualified identifier is disallowed in strict mode
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003886 // but "delete this" is allowed.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003887 ASSERT(language_mode() == CLASSIC_MODE || var->is_this());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003888 if (var->IsUnallocated()) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003889 __ push(GlobalObjectOperand());
3890 __ push(Immediate(var->name()));
3891 __ push(Immediate(Smi::FromInt(kNonStrictMode)));
3892 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
3893 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00003894 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
3895 // Result of deleting non-global variables is false. 'this' is
3896 // not really a variable, though we implement it as one. The
3897 // subexpression does not have side effects.
3898 context()->Plug(var->is_this());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003899 } else {
3900 // Non-global variable. Call the runtime to try to delete from the
3901 // context where the variable was introduced.
3902 __ push(context_register());
3903 __ push(Immediate(var->name()));
3904 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
3905 context()->Plug(eax);
3906 }
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00003907 } else {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003908 // Result of deleting non-property, non-variable reference is true.
3909 // The subexpression may have side effects.
3910 VisitForEffect(expr->expression());
3911 context()->Plug(true);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003912 }
3913 break;
3914 }
3915
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003916 case Token::VOID: {
3917 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3918 VisitForEffect(expr->expression());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003919 context()->Plug(isolate()->factory()->undefined_value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003920 break;
3921 }
3922
3923 case Token::NOT: {
3924 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003925 if (context()->IsEffect()) {
3926 // Unary NOT has no side effects so it's only necessary to visit the
3927 // subexpression. Match the optimizing compiler by not branching.
3928 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003929 } else if (context()->IsTest()) {
3930 const TestContext* test = TestContext::cast(context());
3931 // The labels are swapped for the recursive call.
3932 VisitForControl(expr->expression(),
3933 test->false_label(),
3934 test->true_label(),
3935 test->fall_through());
3936 context()->Plug(test->true_label(), test->false_label());
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003937 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003938 // We handle value contexts explicitly rather than simply visiting
3939 // for control and plugging the control flow into the context,
3940 // because we need to prepare a pair of extra administrative AST ids
3941 // for the optimizing compiler.
3942 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
3943 Label materialize_true, materialize_false, done;
3944 VisitForControl(expr->expression(),
3945 &materialize_false,
3946 &materialize_true,
3947 &materialize_true);
3948 __ bind(&materialize_true);
3949 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3950 if (context()->IsAccumulatorValue()) {
3951 __ mov(eax, isolate()->factory()->true_value());
3952 } else {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003953 __ Push(isolate()->factory()->true_value());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003954 }
3955 __ jmp(&done, Label::kNear);
3956 __ bind(&materialize_false);
3957 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3958 if (context()->IsAccumulatorValue()) {
3959 __ mov(eax, isolate()->factory()->false_value());
3960 } else {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003961 __ Push(isolate()->factory()->false_value());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003962 }
3963 __ bind(&done);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003964 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003965 break;
3966 }
3967
3968 case Token::TYPEOF: {
3969 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003970 { StackValueContext context(this);
3971 VisitForTypeofValue(expr->expression());
3972 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003973 __ CallRuntime(Runtime::kTypeof, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003974 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003975 break;
3976 }
3977
3978 case Token::ADD: {
3979 Comment cmt(masm_, "[ UnaryOperation (ADD)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003980 VisitForAccumulatorValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003981 Label no_conversion;
whesse@chromium.org7b260152011-06-20 15:33:18 +00003982 __ JumpIfSmi(result_register(), &no_conversion);
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003983 ToNumberStub convert_stub;
3984 __ CallStub(&convert_stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003985 __ bind(&no_conversion);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003986 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003987 break;
3988 }
3989
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003990 case Token::SUB:
3991 EmitUnaryOperation(expr, "[ UnaryOperation (SUB)");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003992 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003993
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003994 case Token::BIT_NOT:
3995 EmitUnaryOperation(expr, "[ UnaryOperation (BIT_NOT)");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003996 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003997
3998 default:
3999 UNREACHABLE();
4000 }
4001}
4002
4003
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004004void FullCodeGenerator::EmitUnaryOperation(UnaryOperation* expr,
4005 const char* comment) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004006 Comment cmt(masm_, comment);
4007 bool can_overwrite = expr->expression()->ResultOverwriteAllowed();
4008 UnaryOverwriteMode overwrite =
4009 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004010 UnaryOpStub stub(expr->op(), overwrite);
4011 // UnaryOpStub expects the argument to be in the
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004012 // accumulator register eax.
4013 VisitForAccumulatorValue(expr->expression());
4014 SetSourcePosition(expr->position());
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004015 CallIC(stub.GetCode(isolate()), RelocInfo::CODE_TARGET,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004016 expr->UnaryOperationFeedbackId());
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004017 context()->Plug(eax);
4018}
4019
4020
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004021void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
4022 Comment cmnt(masm_, "[ CountOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00004023 SetSourcePosition(expr->position());
4024
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004025 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
4026 // as the left-hand side.
4027 if (!expr->expression()->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004028 VisitForEffect(expr->expression());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004029 return;
4030 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004031
4032 // Expression can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00004033 // slot.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004034 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
4035 LhsKind assign_type = VARIABLE;
4036 Property* prop = expr->expression()->AsProperty();
4037 // In case of a property we use the uninitialized expression context
4038 // of the key to detect a named property.
4039 if (prop != NULL) {
4040 assign_type =
4041 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
4042 }
4043
4044 // Evaluate expression and get value.
4045 if (assign_type == VARIABLE) {
4046 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004047 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00004048 EmitVariableLoad(expr->expression()->AsVariableProxy());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004049 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004050 // Reserve space for result of postfix operation.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004051 if (expr->is_postfix() && !context()->IsEffect()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004052 __ push(Immediate(Smi::FromInt(0)));
4053 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004054 if (assign_type == NAMED_PROPERTY) {
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004055 // Put the object both on the stack and in edx.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004056 VisitForAccumulatorValue(prop->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00004057 __ push(eax);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004058 __ mov(edx, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004059 EmitNamedPropertyLoad(prop);
4060 } else {
whesse@chromium.org7b260152011-06-20 15:33:18 +00004061 VisitForStackValue(prop->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004062 VisitForStackValue(prop->key());
4063 __ mov(edx, Operand(esp, kPointerSize)); // Object.
4064 __ mov(ecx, Operand(esp, 0)); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004065 EmitKeyedPropertyLoad(prop);
4066 }
4067 }
4068
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004069 // We need a second deoptimization point after loading the value
4070 // in case evaluating the property load my have a side effect.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004071 if (assign_type == VARIABLE) {
4072 PrepareForBailout(expr->expression(), TOS_REG);
4073 } else {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004074 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004075 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004076
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004077 // Call ToNumber only if operand is not a smi.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004078 Label no_conversion;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004079 if (ShouldInlineSmiCase(expr->op())) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00004080 __ JumpIfSmi(eax, &no_conversion, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004081 }
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004082 ToNumberStub convert_stub;
4083 __ CallStub(&convert_stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004084 __ bind(&no_conversion);
4085
4086 // Save result for postfix expressions.
4087 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004088 if (!context()->IsEffect()) {
4089 // Save the result on the stack. If we have a named or keyed property
4090 // we store the result under the receiver that is currently on top
4091 // of the stack.
4092 switch (assign_type) {
4093 case VARIABLE:
4094 __ push(eax);
4095 break;
4096 case NAMED_PROPERTY:
4097 __ mov(Operand(esp, kPointerSize), eax);
4098 break;
4099 case KEYED_PROPERTY:
4100 __ mov(Operand(esp, 2 * kPointerSize), eax);
4101 break;
4102 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004103 }
4104 }
4105
4106 // Inline smi case if we are in a loop.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004107 Label done, stub_call;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004108 JumpPatchSite patch_site(masm_);
4109
ricow@chromium.org65fae842010-08-25 15:26:24 +00004110 if (ShouldInlineSmiCase(expr->op())) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004111 if (expr->op() == Token::INC) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004112 __ add(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004113 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004114 __ sub(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004115 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004116 __ j(overflow, &stub_call, Label::kNear);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004117 // We could eliminate this smi check if we split the code at
4118 // the first smi check before calling ToNumber.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004119 patch_site.EmitJumpIfSmi(eax, &done, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004120
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004121 __ bind(&stub_call);
4122 // Call stub. Undo operation first.
4123 if (expr->op() == Token::INC) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004124 __ sub(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004125 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004126 __ add(eax, Immediate(Smi::FromInt(1)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004127 }
4128 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004129
4130 // Record position before stub call.
4131 SetSourcePosition(expr->position());
4132
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004133 // Call stub for +1/-1.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004134 __ mov(edx, eax);
4135 __ mov(eax, Immediate(Smi::FromInt(1)));
danno@chromium.org40cb8782011-05-25 07:58:50 +00004136 BinaryOpStub stub(expr->binary_op(), NO_OVERWRITE);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004137 CallIC(stub.GetCode(isolate()),
4138 RelocInfo::CODE_TARGET,
4139 expr->CountBinOpFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004140 patch_site.EmitPatchInfo();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004141 __ bind(&done);
4142
4143 // Store the value returned in eax.
4144 switch (assign_type) {
4145 case VARIABLE:
4146 if (expr->is_postfix()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004147 // Perform the assignment as if via '='.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004148 { EffectContext context(this);
4149 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4150 Token::ASSIGN);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004151 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4152 context.Plug(eax);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004153 }
4154 // For all contexts except EffectContext We have the result on
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004155 // top of the stack.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004156 if (!context()->IsEffect()) {
4157 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004158 }
4159 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004160 // Perform the assignment as if via '='.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004161 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004162 Token::ASSIGN);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004163 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4164 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004165 }
4166 break;
4167 case NAMED_PROPERTY: {
4168 __ mov(ecx, prop->key()->AsLiteral()->handle());
4169 __ pop(edx);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004170 Handle<Code> ic = is_classic_mode()
4171 ? isolate()->builtins()->StoreIC_Initialize()
4172 : isolate()->builtins()->StoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004173 CallIC(ic, RelocInfo::CODE_TARGET, expr->CountStoreFeedbackId());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004174 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004175 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004176 if (!context()->IsEffect()) {
4177 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004178 }
4179 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004180 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004181 }
4182 break;
4183 }
4184 case KEYED_PROPERTY: {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004185 __ pop(ecx);
4186 __ pop(edx);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004187 Handle<Code> ic = is_classic_mode()
4188 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4189 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004190 CallIC(ic, RelocInfo::CODE_TARGET, expr->CountStoreFeedbackId());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004191 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004192 if (expr->is_postfix()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004193 // Result is on the stack
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004194 if (!context()->IsEffect()) {
4195 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004196 }
4197 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004198 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004199 }
4200 break;
4201 }
4202 }
4203}
4204
4205
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004206void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004207 VariableProxy* proxy = expr->AsVariableProxy();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004208 ASSERT(!context()->IsEffect());
4209 ASSERT(!context()->IsTest());
4210
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004211 if (proxy != NULL && proxy->var()->IsUnallocated()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004212 Comment cmnt(masm_, "Global variable");
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004213 __ mov(edx, GlobalObjectOperand());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004214 __ mov(ecx, Immediate(proxy->name()));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004215 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004216 // Use a regular load, not a contextual load, to avoid a reference
4217 // error.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004218 CallIC(ic);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004219 PrepareForBailout(expr, TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004220 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004221 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004222 Label done, slow;
4223
4224 // Generate code for loading from variables potentially shadowed
4225 // by eval-introduced variables.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004226 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004227
4228 __ bind(&slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004229 __ push(esi);
4230 __ push(Immediate(proxy->name()));
4231 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004232 PrepareForBailout(expr, TOS_REG);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004233 __ bind(&done);
4234
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004235 context()->Plug(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004236 } else {
4237 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004238 VisitInDuplicateContext(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004239 }
4240}
4241
4242
ager@chromium.org04921a82011-06-27 13:21:41 +00004243void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004244 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004245 Handle<String> check) {
4246 Label materialize_true, materialize_false;
4247 Label* if_true = NULL;
4248 Label* if_false = NULL;
4249 Label* fall_through = NULL;
4250 context()->PrepareTest(&materialize_true, &materialize_false,
4251 &if_true, &if_false, &fall_through);
4252
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004253 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004254 VisitForTypeofValue(sub_expr);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004255 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004256 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004257
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004258 if (check->Equals(isolate()->heap()->number_string())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004259 __ JumpIfSmi(eax, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004260 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004261 isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004262 Split(equal, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004263 } else if (check->Equals(isolate()->heap()->string_string())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004264 __ JumpIfSmi(eax, if_false);
4265 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edx);
4266 __ j(above_equal, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004267 // Check for undetectable objects => false.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004268 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
4269 1 << Map::kIsUndetectable);
4270 Split(zero, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004271 } else if (check->Equals(isolate()->heap()->boolean_string())) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004272 __ cmp(eax, isolate()->factory()->true_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004273 __ j(equal, if_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004274 __ cmp(eax, isolate()->factory()->false_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004275 Split(equal, if_true, if_false, fall_through);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004276 } else if (FLAG_harmony_typeof &&
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004277 check->Equals(isolate()->heap()->null_string())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004278 __ cmp(eax, isolate()->factory()->null_value());
4279 Split(equal, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004280 } else if (check->Equals(isolate()->heap()->undefined_string())) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004281 __ cmp(eax, isolate()->factory()->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004282 __ j(equal, if_true);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004283 __ JumpIfSmi(eax, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004284 // Check for undetectable objects => true.
4285 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
4286 __ movzx_b(ecx, FieldOperand(edx, Map::kBitFieldOffset));
4287 __ test(ecx, Immediate(1 << Map::kIsUndetectable));
4288 Split(not_zero, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004289 } else if (check->Equals(isolate()->heap()->function_string())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004290 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004291 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4292 __ CmpObjectType(eax, JS_FUNCTION_TYPE, edx);
4293 __ j(equal, if_true);
4294 __ CmpInstanceType(edx, JS_FUNCTION_PROXY_TYPE);
4295 Split(equal, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004296 } else if (check->Equals(isolate()->heap()->object_string())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004297 __ JumpIfSmi(eax, if_false);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004298 if (!FLAG_harmony_typeof) {
4299 __ cmp(eax, isolate()->factory()->null_value());
4300 __ j(equal, if_true);
4301 }
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004302 if (FLAG_harmony_symbols) {
4303 __ CmpObjectType(eax, SYMBOL_TYPE, edx);
4304 __ j(equal, if_true);
4305 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004306 __ CmpObjectType(eax, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE, edx);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004307 __ j(below, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004308 __ CmpInstanceType(edx, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
4309 __ j(above, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004310 // Check for undetectable objects => false.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004311 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
4312 1 << Map::kIsUndetectable);
4313 Split(zero, if_true, if_false, fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004314 } else {
4315 if (if_false != fall_through) __ jmp(if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004316 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004317 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004318}
4319
4320
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004321void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
4322 Comment cmnt(masm_, "[ CompareOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00004323 SetSourcePosition(expr->position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004324
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004325 // First we try a fast inlined version of the compare when one of
4326 // the operands is a literal.
4327 if (TryLiteralCompare(expr)) return;
4328
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004329 // Always perform the comparison for its control flow. Pack the result
4330 // into the expression's context after the comparison is performed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004331 Label materialize_true, materialize_false;
4332 Label* if_true = NULL;
4333 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004334 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004335 context()->PrepareTest(&materialize_true, &materialize_false,
4336 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004337
ager@chromium.org04921a82011-06-27 13:21:41 +00004338 Token::Value op = expr->op();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004339 VisitForStackValue(expr->left());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004340 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004341 case Token::IN:
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004342 VisitForStackValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004343 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004344 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004345 __ cmp(eax, isolate()->factory()->true_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004346 Split(equal, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004347 break;
4348
4349 case Token::INSTANCEOF: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004350 VisitForStackValue(expr->right());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004351 InstanceofStub stub(InstanceofStub::kNoFlags);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004352 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004353 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004354 __ test(eax, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004355 // The stub returns 0 for true.
4356 Split(zero, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004357 break;
4358 }
4359
4360 default: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004361 VisitForAccumulatorValue(expr->right());
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004362 Condition cc = CompareIC::ComputeCondition(op);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004363 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004364
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004365 bool inline_smi_code = ShouldInlineSmiCase(op);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004366 JumpPatchSite patch_site(masm_);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004367 if (inline_smi_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004368 Label slow_case;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004369 __ mov(ecx, edx);
4370 __ or_(ecx, eax);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004371 patch_site.EmitJumpIfNotSmi(ecx, &slow_case, Label::kNear);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004372 __ cmp(edx, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004373 Split(cc, if_true, if_false, NULL);
4374 __ bind(&slow_case);
4375 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004376
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004377 // Record position and call the compare IC.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004378 SetSourcePosition(expr->position());
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004379 Handle<Code> ic = CompareIC::GetUninitialized(isolate(), op);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004380 CallIC(ic, RelocInfo::CODE_TARGET, expr->CompareOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004381 patch_site.EmitPatchInfo();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004382
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004383 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004384 __ test(eax, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004385 Split(cc, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004386 }
4387 }
4388
4389 // Convert the result of the comparison into one expected for this
4390 // expression's context.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004391 context()->Plug(if_true, if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004392}
4393
4394
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004395void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4396 Expression* sub_expr,
4397 NilValue nil) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004398 Label materialize_true, materialize_false;
4399 Label* if_true = NULL;
4400 Label* if_false = NULL;
4401 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004402 context()->PrepareTest(&materialize_true, &materialize_false,
4403 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004404
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004405 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004406 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004407 Handle<Object> nil_value = nil == kNullValue ?
4408 isolate()->factory()->null_value() :
4409 isolate()->factory()->undefined_value();
4410 __ cmp(eax, nil_value);
4411 if (expr->op() == Token::EQ_STRICT) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004412 Split(equal, if_true, if_false, fall_through);
4413 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004414 Handle<Object> other_nil_value = nil == kNullValue ?
4415 isolate()->factory()->undefined_value() :
4416 isolate()->factory()->null_value();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004417 __ j(equal, if_true);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004418 __ cmp(eax, other_nil_value);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004419 __ j(equal, if_true);
whesse@chromium.org7b260152011-06-20 15:33:18 +00004420 __ JumpIfSmi(eax, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004421 // It can be an undetectable object.
4422 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
4423 __ movzx_b(edx, FieldOperand(edx, Map::kBitFieldOffset));
4424 __ test(edx, Immediate(1 << Map::kIsUndetectable));
4425 Split(not_zero, if_true, if_false, fall_through);
4426 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004427 context()->Plug(if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004428}
4429
4430
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004431void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
4432 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004433 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004434}
4435
4436
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00004437Register FullCodeGenerator::result_register() {
4438 return eax;
4439}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004440
4441
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00004442Register FullCodeGenerator::context_register() {
4443 return esi;
4444}
4445
4446
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004447void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
4448 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4449 __ mov(Operand(ebp, frame_offset), value);
4450}
4451
4452
4453void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004454 __ mov(dst, ContextOperand(esi, context_index));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004455}
4456
4457
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004458void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004459 Scope* declaration_scope = scope()->DeclarationScope();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00004460 if (declaration_scope->is_global_scope() ||
4461 declaration_scope->is_module_scope()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00004462 // Contexts nested in the native context have a canonical empty function
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004463 // as their closure, not the anonymous closure containing the global
4464 // code. Pass a smi sentinel and let the runtime look up the empty
4465 // function.
4466 __ push(Immediate(Smi::FromInt(0)));
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004467 } else if (declaration_scope->is_eval_scope()) {
4468 // Contexts nested inside eval code have the same closure as the context
4469 // calling eval, not the anonymous closure containing the eval code.
4470 // Fetch it from the context.
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004471 __ push(ContextOperand(esi, Context::CLOSURE_INDEX));
4472 } else {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004473 ASSERT(declaration_scope->is_function_scope());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004474 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
4475 }
4476}
4477
4478
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004479// ----------------------------------------------------------------------------
4480// Non-local control flow support.
4481
4482void FullCodeGenerator::EnterFinallyBlock() {
4483 // Cook return address on top of stack (smi encoded Code* delta)
4484 ASSERT(!result_register().is(edx));
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004485 __ pop(edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004486 __ sub(edx, Immediate(masm_->CodeObject()));
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00004487 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4488 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004489 __ SmiTag(edx);
4490 __ push(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004491
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004492 // Store result register while executing finally block.
4493 __ push(result_register());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004494
4495 // Store pending message while executing finally block.
4496 ExternalReference pending_message_obj =
4497 ExternalReference::address_of_pending_message_obj(isolate());
4498 __ mov(edx, Operand::StaticVariable(pending_message_obj));
4499 __ push(edx);
4500
4501 ExternalReference has_pending_message =
4502 ExternalReference::address_of_has_pending_message(isolate());
4503 __ mov(edx, Operand::StaticVariable(has_pending_message));
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004504 __ SmiTag(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004505 __ push(edx);
4506
4507 ExternalReference pending_message_script =
4508 ExternalReference::address_of_pending_message_script(isolate());
4509 __ mov(edx, Operand::StaticVariable(pending_message_script));
4510 __ push(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004511}
4512
4513
4514void FullCodeGenerator::ExitFinallyBlock() {
4515 ASSERT(!result_register().is(edx));
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004516 // Restore pending message from stack.
4517 __ pop(edx);
4518 ExternalReference pending_message_script =
4519 ExternalReference::address_of_pending_message_script(isolate());
4520 __ mov(Operand::StaticVariable(pending_message_script), edx);
4521
4522 __ pop(edx);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004523 __ SmiUntag(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004524 ExternalReference has_pending_message =
4525 ExternalReference::address_of_has_pending_message(isolate());
4526 __ mov(Operand::StaticVariable(has_pending_message), edx);
4527
4528 __ pop(edx);
4529 ExternalReference pending_message_obj =
4530 ExternalReference::address_of_pending_message_obj(isolate());
4531 __ mov(Operand::StaticVariable(pending_message_obj), edx);
4532
4533 // Restore result register from stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004534 __ pop(result_register());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004535
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004536 // Uncook return address.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004537 __ pop(edx);
4538 __ SmiUntag(edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004539 __ add(edx, Immediate(masm_->CodeObject()));
4540 __ jmp(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004541}
4542
4543
4544#undef __
4545
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004546#define __ ACCESS_MASM(masm())
4547
4548FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4549 int* stack_depth,
4550 int* context_length) {
4551 // The macros used here must preserve the result register.
4552
4553 // Because the handler block contains the context of the finally
4554 // code, we can restore it directly from there for the finally code
4555 // rather than iteratively unwinding contexts via their previous
4556 // links.
4557 __ Drop(*stack_depth); // Down to the handler block.
4558 if (*context_length > 0) {
4559 // Restore the context to its dedicated register and the stack.
4560 __ mov(esi, Operand(esp, StackHandlerConstants::kContextOffset));
4561 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
4562 }
4563 __ PopTryHandler();
4564 __ call(finally_entry_);
4565
4566 *stack_depth = 0;
4567 *context_length = 0;
4568 return previous_;
4569}
4570
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004571#undef __
4572
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004573} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004574
4575#endif // V8_TARGET_ARCH_IA32