blob: 096e2edb119000da4ae6e1013aefaa8e9f219c0d [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
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +000030#if V8_TARGET_ARCH_IA32
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000031
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);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000121
machenbach@chromium.org8545d492014-03-17 09:28:03 +0000122 InitializeFeedbackVector();
123
danno@chromium.org41728482013-06-12 22:31:22 +0000124 profiling_counter_ = isolate()->factory()->NewCell(
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000125 Handle<Smi>(Smi::FromInt(FLAG_interrupt_budget), isolate()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000126 SetFunctionPosition(function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000127 Comment cmnt(masm_, "[ function compiled by full code generator");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000128
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000129 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
130
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000131#ifdef DEBUG
132 if (strlen(FLAG_stop_at) > 0 &&
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000133 info->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000134 __ int3();
135 }
136#endif
137
dslomov@chromium.org486536d2014-03-12 13:09:18 +0000138 // Sloppy mode functions and builtins need to replace the receiver with the
machenbach@chromium.orge31286d2014-01-15 10:29:52 +0000139 // global proxy when called as functions (without an explicit receiver
140 // object).
dslomov@chromium.org486536d2014-03-12 13:09:18 +0000141 if (info->strict_mode() == SLOPPY && !info->is_native()) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000142 Label ok;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000143 // +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));
machenbach@chromium.orge31286d2014-01-15 10:29:52 +0000146
147 __ cmp(ecx, isolate()->factory()->undefined_value());
danno@chromium.orgc612e022011-11-10 11:38:15 +0000148 __ j(not_equal, &ok, Label::kNear);
machenbach@chromium.orge31286d2014-01-15 10:29:52 +0000149
150 __ mov(ecx, GlobalObjectOperand());
151 __ mov(ecx, FieldOperand(ecx, GlobalObject::kGlobalReceiverOffset));
152
153 __ mov(Operand(esp, receiver_offset), ecx);
154
danno@chromium.org40cb8782011-05-25 07:58:50 +0000155 __ bind(&ok);
156 }
157
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000158 // Open a frame scope to indicate that there is a frame on the stack. The
159 // MANUAL indicates that the scope shouldn't actually generate code to set up
160 // the frame (that is done below).
161 FrameScope frame_scope(masm_, StackFrame::MANUAL);
162
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +0000163 info->set_prologue_offset(masm_->pc_offset());
bmeurer@chromium.orgc9913f02013-10-24 06:31:36 +0000164 __ Prologue(BUILD_FUNCTION_FRAME);
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000165 info->AddNoFrameRange(0, masm_->pc_offset());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000166
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000167 { Comment cmnt(masm_, "[ Allocate locals");
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000168 int locals_count = info->scope()->num_stack_slots();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000169 // Generators allocate locals, if any, in context slots.
170 ASSERT(!info->function()->is_generator() || locals_count == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000171 if (locals_count == 1) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000172 __ push(Immediate(isolate()->factory()->undefined_value()));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000173 } else if (locals_count > 1) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000174 __ mov(eax, Immediate(isolate()->factory()->undefined_value()));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000175 for (int i = 0; i < locals_count; i++) {
176 __ push(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000177 }
178 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000179 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000180
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000181 bool function_in_register = true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000182
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000183 // Possibly allocate a local context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000184 int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000185 if (heap_slots > 0) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000186 Comment cmnt(masm_, "[ Allocate context");
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000187 // Argument to NewContext is the function, which is still in edi.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000188 if (FLAG_harmony_scoping && info->scope()->is_global_scope()) {
machenbach@chromium.orgbbbda922014-01-23 09:38:20 +0000189 __ push(edi);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000190 __ Push(info->scope()->GetScopeInfo());
191 __ CallRuntime(Runtime::kNewGlobalContext, 2);
192 } else if (heap_slots <= FastNewContextStub::kMaximumSlots) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000193 FastNewContextStub stub(heap_slots);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000194 __ CallStub(&stub);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000195 } else {
machenbach@chromium.orgbbbda922014-01-23 09:38:20 +0000196 __ push(edi);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000197 __ CallRuntime(Runtime::kNewFunctionContext, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000198 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000199 function_in_register = false;
machenbach@chromium.orgbbbda922014-01-23 09:38:20 +0000200 // Context is returned in eax. It replaces the context passed to us.
201 // It's saved in the stack and kept live in esi.
202 __ mov(esi, eax);
203 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), eax);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000204
205 // Copy parameters into context if necessary.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000206 int num_parameters = info->scope()->num_parameters();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000207 for (int i = 0; i < num_parameters; i++) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000208 Variable* var = scope()->parameter(i);
209 if (var->IsContextSlot()) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000210 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
211 (num_parameters - 1 - i) * kPointerSize;
212 // Load parameter from stack.
213 __ mov(eax, Operand(ebp, parameter_offset));
214 // Store it in the context.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000215 int context_offset = Context::SlotOffset(var->index());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000216 __ mov(Operand(esi, context_offset), eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000217 // Update the write barrier. This clobbers eax and ebx.
218 __ RecordWriteContextSlot(esi,
219 context_offset,
220 eax,
221 ebx,
222 kDontSaveFPRegs);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000223 }
224 }
225 }
226
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000227 Variable* arguments = scope()->arguments();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000228 if (arguments != NULL) {
229 // Function uses arguments object.
230 Comment cmnt(masm_, "[ Allocate arguments object");
231 if (function_in_register) {
232 __ push(edi);
233 } else {
234 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
235 }
236 // Receiver is just before the parameters on the caller's stack.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000237 int num_parameters = info->scope()->num_parameters();
238 int offset = num_parameters * kPointerSize;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000239 __ lea(edx,
240 Operand(ebp, StandardFrameConstants::kCallerSPOffset + offset));
241 __ push(edx);
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000242 __ push(Immediate(Smi::FromInt(num_parameters)));
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000243 // Arguments to ArgumentsAccessStub:
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000244 // function, receiver address, parameter count.
245 // The stub will rewrite receiver and parameter count if the previous
246 // stack frame was an arguments adapter frame.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000247 ArgumentsAccessStub::Type type;
dslomov@chromium.org486536d2014-03-12 13:09:18 +0000248 if (strict_mode() == STRICT) {
whesse@chromium.org7b260152011-06-20 15:33:18 +0000249 type = ArgumentsAccessStub::NEW_STRICT;
250 } else if (function()->has_duplicate_parameters()) {
dslomov@chromium.org486536d2014-03-12 13:09:18 +0000251 type = ArgumentsAccessStub::NEW_SLOPPY_SLOW;
whesse@chromium.org7b260152011-06-20 15:33:18 +0000252 } else {
dslomov@chromium.org486536d2014-03-12 13:09:18 +0000253 type = ArgumentsAccessStub::NEW_SLOPPY_FAST;
whesse@chromium.org7b260152011-06-20 15:33:18 +0000254 }
255 ArgumentsAccessStub stub(type);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000256 __ CallStub(&stub);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000257
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000258 SetVar(arguments, eax, ebx, edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000259 }
260
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000261 if (FLAG_trace) {
262 __ CallRuntime(Runtime::kTraceEnter, 0);
263 }
264
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000265 // Visit the declarations and body unless there is an illegal
266 // redeclaration.
267 if (scope()->HasIllegalRedeclaration()) {
268 Comment cmnt(masm_, "[ Declarations");
269 scope()->VisitIllegalRedeclaration(this);
270
271 } else {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000272 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000273 { Comment cmnt(masm_, "[ Declarations");
274 // For named function expressions, declare the function name as a
275 // constant.
276 if (scope()->is_function_scope() && scope()->function() != NULL) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000277 VariableDeclaration* function = scope()->function();
278 ASSERT(function->proxy()->var()->mode() == CONST ||
dslomov@chromium.org486536d2014-03-12 13:09:18 +0000279 function->proxy()->var()->mode() == CONST_LEGACY);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000280 ASSERT(function->proxy()->var()->location() != Variable::UNALLOCATED);
281 VisitVariableDeclaration(function);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000282 }
283 VisitDeclarations(scope()->declarations());
284 }
285
286 { Comment cmnt(masm_, "[ Stack check");
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000287 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000288 Label ok;
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000289 ExternalReference stack_limit =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000290 ExternalReference::address_of_stack_limit(isolate());
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000291 __ cmp(esp, Operand::StaticVariable(stack_limit));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000292 __ j(above_equal, &ok, Label::kNear);
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000293 __ call(isolate()->builtins()->StackCheck(), RelocInfo::CODE_TARGET);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000294 __ bind(&ok);
295 }
296
297 { Comment cmnt(masm_, "[ Body");
298 ASSERT(loop_depth() == 0);
299 VisitStatements(function()->body());
300 ASSERT(loop_depth() == 0);
301 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000302 }
303
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000304 // Always emit a 'return undefined' in case control fell off the end of
305 // the body.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000306 { Comment cmnt(masm_, "[ return <undefined>;");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000307 __ mov(eax, isolate()->factory()->undefined_value());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000308 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000309 }
310}
311
312
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000313void FullCodeGenerator::ClearAccumulator() {
314 __ Set(eax, Immediate(Smi::FromInt(0)));
315}
316
317
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000318void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
319 __ mov(ebx, Immediate(profiling_counter_));
danno@chromium.org41728482013-06-12 22:31:22 +0000320 __ sub(FieldOperand(ebx, Cell::kValueOffset),
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000321 Immediate(Smi::FromInt(delta)));
322}
323
324
325void FullCodeGenerator::EmitProfilingCounterReset() {
326 int reset_value = FLAG_interrupt_budget;
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000327 __ mov(ebx, Immediate(profiling_counter_));
danno@chromium.org41728482013-06-12 22:31:22 +0000328 __ mov(FieldOperand(ebx, Cell::kValueOffset),
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000329 Immediate(Smi::FromInt(reset_value)));
330}
331
332
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000333void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
334 Label* back_edge_target) {
335 Comment cmnt(masm_, "[ Back edge bookkeeping");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000336 Label ok;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000337
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +0000338 ASSERT(back_edge_target->is_bound());
339 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
340 int weight = Min(kMaxBackEdgeWeight,
341 Max(1, distance / kCodeSizeMultiplier));
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000342 EmitProfilingCounterDecrement(weight);
343 __ j(positive, &ok, Label::kNear);
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000344 __ call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000345
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000346 // Record a mapping of this PC offset to the OSR id. This is used to find
347 // the AST id from the unoptimized code in order to use it as a key into
348 // the deoptimization input data found in the optimized code.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000349 RecordBackEdge(stmt->OsrEntryId());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000350
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000351 EmitProfilingCounterReset();
yangguo@chromium.org56454712012-02-16 15:33:53 +0000352
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000353 __ bind(&ok);
354 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
355 // Record a mapping of the OSR id to this PC. This is used if the OSR
356 // entry becomes the target of a bailout. We don't expect it to be, but
357 // we want it to work if it is.
358 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000359}
360
361
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000362void FullCodeGenerator::EmitReturnSequence() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000363 Comment cmnt(masm_, "[ Return sequence");
364 if (return_label_.is_bound()) {
365 __ jmp(&return_label_);
366 } else {
367 // Common return label
368 __ bind(&return_label_);
369 if (FLAG_trace) {
370 __ push(eax);
371 __ CallRuntime(Runtime::kTraceExit, 1);
372 }
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +0000373 // Pretend that the exit is a backwards jump to the entry.
374 int weight = 1;
375 if (info_->ShouldSelfOptimize()) {
376 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
377 } else {
378 int distance = masm_->pc_offset();
379 weight = Min(kMaxBackEdgeWeight,
380 Max(1, distance / kCodeSizeMultiplier));
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000381 }
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +0000382 EmitProfilingCounterDecrement(weight);
383 Label ok;
384 __ j(positive, &ok, Label::kNear);
385 __ push(eax);
386 __ call(isolate()->builtins()->InterruptCheck(),
387 RelocInfo::CODE_TARGET);
388 __ pop(eax);
389 EmitProfilingCounterReset();
390 __ bind(&ok);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000391#ifdef DEBUG
392 // Add a label for checking the size of the code used for returning.
393 Label check_exit_codesize;
394 masm_->bind(&check_exit_codesize);
395#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000396 SetSourcePosition(function()->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000397 __ RecordJSReturn();
398 // Do not use the leave instruction here because it is too short to
399 // patch with the code required by the debugger.
400 __ mov(esp, ebp);
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000401 int no_frame_start = masm_->pc_offset();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000402 __ pop(ebp);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000403
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000404 int arguments_bytes = (info_->scope()->num_parameters() + 1) * kPointerSize;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000405 __ Ret(arguments_bytes, ecx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000406#ifdef ENABLE_DEBUGGER_SUPPORT
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000407 // Check that the size of the code used for returning is large enough
408 // for the debugger's requirements.
409 ASSERT(Assembler::kJSReturnSequenceLength <=
410 masm_->SizeOfCodeGeneratedSince(&check_exit_codesize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000411#endif
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000412 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000413 }
414}
415
416
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000417void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
418 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000419}
420
421
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000422void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
423 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
424 codegen()->GetVar(result_register(), var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000425}
426
427
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000428void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
429 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
430 MemOperand operand = codegen()->VarOperand(var, result_register());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000431 // Memory operands can be pushed directly.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000432 __ push(operand);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000433}
434
435
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000436void FullCodeGenerator::TestContext::Plug(Variable* var) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000437 // For simplicity we always test the accumulator register.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000438 codegen()->GetVar(result_register(), var);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000439 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000440 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000441}
442
443
444void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
445 UNREACHABLE(); // Not used on IA32.
446}
447
448
449void FullCodeGenerator::AccumulatorValueContext::Plug(
450 Heap::RootListIndex index) const {
451 UNREACHABLE(); // Not used on IA32.
452}
453
454
455void FullCodeGenerator::StackValueContext::Plug(
456 Heap::RootListIndex index) const {
457 UNREACHABLE(); // Not used on IA32.
458}
459
460
461void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
462 UNREACHABLE(); // Not used on IA32.
463}
464
465
466void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
467}
468
469
470void FullCodeGenerator::AccumulatorValueContext::Plug(
471 Handle<Object> lit) const {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000472 if (lit->IsSmi()) {
473 __ SafeSet(result_register(), Immediate(lit));
474 } else {
475 __ Set(result_register(), Immediate(lit));
476 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000477}
478
479
480void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000481 if (lit->IsSmi()) {
482 __ SafePush(Immediate(lit));
483 } else {
484 __ push(Immediate(lit));
485 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000486}
487
488
489void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000490 codegen()->PrepareForBailoutBeforeSplit(condition(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000491 true,
492 true_label_,
493 false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000494 ASSERT(!lit->IsUndetectableObject()); // There are no undetectable literals.
495 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000496 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000497 } else if (lit->IsTrue() || lit->IsJSObject()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000498 if (true_label_ != fall_through_) __ jmp(true_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000499 } else if (lit->IsString()) {
500 if (String::cast(*lit)->length() == 0) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000501 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000502 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000503 if (true_label_ != fall_through_) __ jmp(true_label_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000504 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000505 } else if (lit->IsSmi()) {
506 if (Smi::cast(*lit)->value() == 0) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000507 if (false_label_ != fall_through_) __ jmp(false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000508 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000509 if (true_label_ != fall_through_) __ jmp(true_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000510 }
511 } else {
512 // For simplicity we always test the accumulator register.
513 __ mov(result_register(), lit);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000514 codegen()->DoTest(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000515 }
516}
517
518
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000519void FullCodeGenerator::EffectContext::DropAndPlug(int count,
520 Register reg) const {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000521 ASSERT(count > 0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000522 __ Drop(count);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000523}
524
525
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000526void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
527 int count,
528 Register reg) const {
529 ASSERT(count > 0);
530 __ Drop(count);
531 __ Move(result_register(), reg);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000532}
533
534
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000535void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
536 Register reg) const {
537 ASSERT(count > 0);
538 if (count > 1) __ Drop(count - 1);
539 __ mov(Operand(esp, 0), reg);
540}
541
542
543void FullCodeGenerator::TestContext::DropAndPlug(int count,
544 Register reg) const {
545 ASSERT(count > 0);
546 // For simplicity we always test the accumulator register.
547 __ Drop(count);
548 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000549 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000550 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000551}
552
553
554void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
555 Label* materialize_false) const {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000556 ASSERT(materialize_true == materialize_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000557 __ bind(materialize_true);
558}
559
560
561void FullCodeGenerator::AccumulatorValueContext::Plug(
562 Label* materialize_true,
563 Label* materialize_false) const {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000564 Label done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000565 __ bind(materialize_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000566 __ mov(result_register(), isolate()->factory()->true_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000567 __ jmp(&done, Label::kNear);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000568 __ bind(materialize_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000569 __ mov(result_register(), isolate()->factory()->false_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000570 __ bind(&done);
571}
572
573
574void FullCodeGenerator::StackValueContext::Plug(
575 Label* materialize_true,
576 Label* materialize_false) const {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000577 Label done;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000578 __ bind(materialize_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000579 __ push(Immediate(isolate()->factory()->true_value()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000580 __ jmp(&done, Label::kNear);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000581 __ bind(materialize_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000582 __ push(Immediate(isolate()->factory()->false_value()));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000583 __ bind(&done);
584}
585
586
587void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
588 Label* materialize_false) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000589 ASSERT(materialize_true == true_label_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000590 ASSERT(materialize_false == false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000591}
592
593
594void FullCodeGenerator::EffectContext::Plug(bool flag) const {
595}
596
597
598void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000599 Handle<Object> value = flag
600 ? isolate()->factory()->true_value()
601 : isolate()->factory()->false_value();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000602 __ mov(result_register(), value);
603}
604
605
606void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000607 Handle<Object> value = flag
608 ? isolate()->factory()->true_value()
609 : isolate()->factory()->false_value();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000610 __ push(Immediate(value));
611}
612
613
614void FullCodeGenerator::TestContext::Plug(bool flag) const {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000615 codegen()->PrepareForBailoutBeforeSplit(condition(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000616 true,
617 true_label_,
618 false_label_);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000619 if (flag) {
620 if (true_label_ != fall_through_) __ jmp(true_label_);
621 } else {
622 if (false_label_ != fall_through_) __ jmp(false_label_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000623 }
624}
625
626
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000627void FullCodeGenerator::DoTest(Expression* condition,
628 Label* if_true,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000629 Label* if_false,
630 Label* fall_through) {
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000631 Handle<Code> ic = ToBooleanStub::GetUninitialized(isolate());
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000632 CallIC(ic, condition->test_id());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000633 __ test(result_register(), result_register());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000634 // The stub returns nonzero for true.
635 Split(not_zero, if_true, if_false, fall_through);
636}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000637
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000638
ricow@chromium.org65fae842010-08-25 15:26:24 +0000639void FullCodeGenerator::Split(Condition cc,
640 Label* if_true,
641 Label* if_false,
642 Label* fall_through) {
643 if (if_false == fall_through) {
644 __ j(cc, if_true);
645 } else if (if_true == fall_through) {
646 __ j(NegateCondition(cc), if_false);
647 } else {
648 __ j(cc, if_true);
649 __ jmp(if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000650 }
651}
652
653
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000654MemOperand FullCodeGenerator::StackOperand(Variable* var) {
655 ASSERT(var->IsStackAllocated());
656 // Offset is negative because higher indexes are at lower addresses.
657 int offset = -var->index() * kPointerSize;
658 // Adjust by a (parameter or local) base offset.
659 if (var->IsParameter()) {
660 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
661 } else {
662 offset += JavaScriptFrameConstants::kLocal0Offset;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000663 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000664 return Operand(ebp, offset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000665}
666
667
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000668MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
669 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
670 if (var->IsContextSlot()) {
671 int context_chain_length = scope()->ContextChainLength(var->scope());
672 __ LoadContext(scratch, context_chain_length);
673 return ContextOperand(scratch, var->index());
674 } else {
675 return StackOperand(var);
676 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000677}
678
679
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000680void FullCodeGenerator::GetVar(Register dest, Variable* var) {
681 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
682 MemOperand location = VarOperand(var, dest);
683 __ mov(dest, location);
684}
685
686
687void FullCodeGenerator::SetVar(Variable* var,
688 Register src,
689 Register scratch0,
690 Register scratch1) {
691 ASSERT(var->IsContextSlot() || var->IsStackAllocated());
692 ASSERT(!scratch0.is(src));
693 ASSERT(!scratch0.is(scratch1));
694 ASSERT(!scratch1.is(src));
695 MemOperand location = VarOperand(var, scratch0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000696 __ mov(location, src);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000697
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000698 // Emit the write barrier code if the location is in the heap.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000699 if (var->IsContextSlot()) {
700 int offset = Context::SlotOffset(var->index());
701 ASSERT(!scratch0.is(esi) && !src.is(esi) && !scratch1.is(esi));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000702 __ RecordWriteContextSlot(scratch0, offset, src, scratch1, kDontSaveFPRegs);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000703 }
704}
705
706
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000707void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000708 bool should_normalize,
709 Label* if_true,
710 Label* if_false) {
711 // Only prepare for bailouts before splits if we're in a test
712 // context. Otherwise, we let the Visit function deal with the
713 // preparation to avoid preparing with the same AST id twice.
714 if (!context()->IsTest() || !info_->IsOptimizable()) return;
715
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000716 Label skip;
717 if (should_normalize) __ jmp(&skip, Label::kNear);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000718 PrepareForBailout(expr, TOS_REG);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000719 if (should_normalize) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000720 __ cmp(eax, isolate()->factory()->true_value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000721 Split(equal, if_true, if_false, NULL);
722 __ bind(&skip);
723 }
724}
725
726
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000727void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000728 // The variable in the declaration always resides in the current context.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000729 ASSERT_EQ(0, scope()->ContextChainLength(variable->scope()));
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000730 if (generate_debug_code_) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000731 // Check that we're not inside a with or catch context.
732 __ mov(ebx, FieldOperand(esi, HeapObject::kMapOffset));
733 __ cmp(ebx, isolate()->factory()->with_context_map());
danno@chromium.org59400602013-08-13 17:09:37 +0000734 __ Check(not_equal, kDeclarationInWithContext);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000735 __ cmp(ebx, isolate()->factory()->catch_context_map());
danno@chromium.org59400602013-08-13 17:09:37 +0000736 __ Check(not_equal, kDeclarationInCatchContext);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000737 }
738}
739
740
741void FullCodeGenerator::VisitVariableDeclaration(
742 VariableDeclaration* declaration) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000743 // If it was not possible to allocate the variable at compile time, we
744 // need to "declare" it at runtime to make sure it actually exists in the
745 // local context.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000746 VariableProxy* proxy = declaration->proxy();
747 VariableMode mode = declaration->mode();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000748 Variable* variable = proxy->var();
dslomov@chromium.org486536d2014-03-12 13:09:18 +0000749 bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000750 switch (variable->location()) {
751 case Variable::UNALLOCATED:
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000752 globals_->Add(variable->name(), zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000753 globals_->Add(variable->binding_needs_init()
754 ? isolate()->factory()->the_hole_value()
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000755 : isolate()->factory()->undefined_value(), zone());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000756 break;
757
758 case Variable::PARAMETER:
759 case Variable::LOCAL:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000760 if (hole_init) {
761 Comment cmnt(masm_, "[ VariableDeclaration");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000762 __ mov(StackOperand(variable),
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000763 Immediate(isolate()->factory()->the_hole_value()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000764 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000765 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000766
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000767 case Variable::CONTEXT:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000768 if (hole_init) {
769 Comment cmnt(masm_, "[ VariableDeclaration");
770 EmitDebugCheckDeclarationContext(variable);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000771 __ mov(ContextOperand(esi, variable->index()),
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000772 Immediate(isolate()->factory()->the_hole_value()));
773 // No write barrier since the hole value is in old space.
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000774 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000775 }
776 break;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000777
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000778 case Variable::LOOKUP: {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000779 Comment cmnt(masm_, "[ VariableDeclaration");
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000780 __ push(esi);
781 __ push(Immediate(variable->name()));
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000782 // VariableDeclaration nodes are always introduced in one of four modes.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000783 ASSERT(IsDeclaredVariableMode(mode));
784 PropertyAttributes attr =
785 IsImmutableVariableMode(mode) ? READ_ONLY : NONE;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000786 __ push(Immediate(Smi::FromInt(attr)));
787 // Push initial value, if any.
788 // Note: For variables we must not push an initial value (such as
789 // 'undefined') because we may have a (legal) redeclaration and we
790 // must not destroy the current value.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000791 if (hole_init) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000792 __ push(Immediate(isolate()->factory()->the_hole_value()));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000793 } else {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000794 __ push(Immediate(Smi::FromInt(0))); // Indicates no initial value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000795 }
796 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000797 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000798 }
799 }
800}
801
802
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000803void FullCodeGenerator::VisitFunctionDeclaration(
804 FunctionDeclaration* declaration) {
805 VariableProxy* proxy = declaration->proxy();
806 Variable* variable = proxy->var();
807 switch (variable->location()) {
808 case Variable::UNALLOCATED: {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000809 globals_->Add(variable->name(), zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000810 Handle<SharedFunctionInfo> function =
811 Compiler::BuildFunctionInfo(declaration->fun(), script());
812 // Check for stack-overflow exception.
813 if (function.is_null()) return SetStackOverflow();
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000814 globals_->Add(function, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000815 break;
816 }
817
818 case Variable::PARAMETER:
819 case Variable::LOCAL: {
820 Comment cmnt(masm_, "[ FunctionDeclaration");
821 VisitForAccumulatorValue(declaration->fun());
822 __ mov(StackOperand(variable), result_register());
823 break;
824 }
825
826 case Variable::CONTEXT: {
827 Comment cmnt(masm_, "[ FunctionDeclaration");
828 EmitDebugCheckDeclarationContext(variable);
829 VisitForAccumulatorValue(declaration->fun());
830 __ mov(ContextOperand(esi, variable->index()), result_register());
831 // We know that we have written a function, which is not a smi.
832 __ RecordWriteContextSlot(esi,
833 Context::SlotOffset(variable->index()),
834 result_register(),
835 ecx,
836 kDontSaveFPRegs,
837 EMIT_REMEMBERED_SET,
838 OMIT_SMI_CHECK);
839 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
840 break;
841 }
842
843 case Variable::LOOKUP: {
844 Comment cmnt(masm_, "[ FunctionDeclaration");
845 __ push(esi);
846 __ push(Immediate(variable->name()));
847 __ push(Immediate(Smi::FromInt(NONE)));
848 VisitForStackValue(declaration->fun());
849 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
850 break;
851 }
852 }
853}
854
855
856void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* declaration) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000857 Variable* variable = declaration->proxy()->var();
858 ASSERT(variable->location() == Variable::CONTEXT);
859 ASSERT(variable->interface()->IsFrozen());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000860
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000861 Comment cmnt(masm_, "[ ModuleDeclaration");
862 EmitDebugCheckDeclarationContext(variable);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000863
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000864 // Load instance object.
865 __ LoadContext(eax, scope_->ContextChainLength(scope_->GlobalScope()));
866 __ mov(eax, ContextOperand(eax, variable->interface()->Index()));
867 __ mov(eax, ContextOperand(eax, Context::EXTENSION_INDEX));
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000868
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000869 // Assign it.
870 __ mov(ContextOperand(esi, variable->index()), eax);
871 // We know that we have written a module, which is not a smi.
872 __ RecordWriteContextSlot(esi,
873 Context::SlotOffset(variable->index()),
874 eax,
875 ecx,
876 kDontSaveFPRegs,
877 EMIT_REMEMBERED_SET,
878 OMIT_SMI_CHECK);
879 PrepareForBailoutForId(declaration->proxy()->id(), NO_REGISTERS);
880
881 // Traverse into body.
882 Visit(declaration->module());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000883}
884
885
886void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* declaration) {
887 VariableProxy* proxy = declaration->proxy();
888 Variable* variable = proxy->var();
889 switch (variable->location()) {
890 case Variable::UNALLOCATED:
891 // TODO(rossberg)
892 break;
893
894 case Variable::CONTEXT: {
895 Comment cmnt(masm_, "[ ImportDeclaration");
896 EmitDebugCheckDeclarationContext(variable);
897 // TODO(rossberg)
898 break;
899 }
900
901 case Variable::PARAMETER:
902 case Variable::LOCAL:
903 case Variable::LOOKUP:
904 UNREACHABLE();
905 }
906}
907
908
909void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* declaration) {
910 // TODO(rossberg)
911}
912
913
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000914void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
915 // Call the runtime to declare the globals.
916 __ push(esi); // The context is the first argument.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000917 __ Push(pairs);
918 __ Push(Smi::FromInt(DeclareGlobalsFlags()));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000919 __ CallRuntime(Runtime::kDeclareGlobals, 3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000920 // Return value is ignored.
921}
922
923
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000924void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
925 // Call the runtime to declare the modules.
926 __ Push(descriptions);
927 __ CallRuntime(Runtime::kDeclareModules, 1);
928 // Return value is ignored.
929}
930
931
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000932void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
933 Comment cmnt(masm_, "[ SwitchStatement");
934 Breakable nested_statement(this, stmt);
935 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000936
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000937 // Keep the switch value on the stack until a case matches.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000938 VisitForStackValue(stmt->tag());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000939 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000940
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000941 ZoneList<CaseClause*>* clauses = stmt->cases();
942 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000943
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000944 Label next_test; // Recycled for each test.
945 // Compile all the tests with branches to their bodies.
946 for (int i = 0; i < clauses->length(); i++) {
947 CaseClause* clause = clauses->at(i);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000948 clause->body_target()->Unuse();
fschneider@chromium.orgd2187832011-01-26 15:44:20 +0000949
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000950 // The default is not a test, but remember it as final fall through.
951 if (clause->is_default()) {
952 default_clause = clause;
953 continue;
954 }
955
956 Comment cmnt(masm_, "[ Case comparison");
957 __ bind(&next_test);
958 next_test.Unuse();
959
960 // Compile the label expression.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000961 VisitForAccumulatorValue(clause->label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000962
ricow@chromium.org65fae842010-08-25 15:26:24 +0000963 // Perform the comparison as if via '==='.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000964 __ mov(edx, Operand(esp, 0)); // Switch value.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000965 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000966 JumpPatchSite patch_site(masm_);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000967 if (inline_smi_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000968 Label slow_case;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000969 __ mov(ecx, edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000970 __ or_(ecx, eax);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000971 patch_site.EmitJumpIfNotSmi(ecx, &slow_case, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000972
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000973 __ cmp(edx, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000974 __ j(not_equal, &next_test);
975 __ Drop(1); // Switch value is no longer needed.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000976 __ jmp(clause->body_target());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000977 __ bind(&slow_case);
978 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000979
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000980 // Record position before stub call for type feedback.
981 SetSourcePosition(clause->position());
hpayer@chromium.org8432c912013-02-28 15:55:26 +0000982 Handle<Code> ic = CompareIC::GetUninitialized(isolate(), Token::EQ_STRICT);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000983 CallIC(ic, clause->CompareId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000984 patch_site.EmitPatchInfo();
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000985
986 Label skip;
987 __ jmp(&skip, Label::kNear);
988 PrepareForBailout(clause, TOS_REG);
989 __ cmp(eax, isolate()->factory()->true_value());
990 __ j(not_equal, &next_test);
991 __ Drop(1);
992 __ jmp(clause->body_target());
993 __ bind(&skip);
994
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000995 __ test(eax, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000996 __ j(not_equal, &next_test);
997 __ Drop(1); // Switch value is no longer needed.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000998 __ jmp(clause->body_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000999 }
1000
1001 // Discard the test value and jump to the default if present, otherwise to
1002 // the end of the statement.
1003 __ bind(&next_test);
1004 __ Drop(1); // Switch value is no longer needed.
1005 if (default_clause == NULL) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001006 __ jmp(nested_statement.break_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001007 } else {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001008 __ jmp(default_clause->body_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001009 }
1010
1011 // Compile all the case bodies.
1012 for (int i = 0; i < clauses->length(); i++) {
1013 Comment cmnt(masm_, "[ Case body");
1014 CaseClause* clause = clauses->at(i);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001015 __ bind(clause->body_target());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001016 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001017 VisitStatements(clause->statements());
1018 }
1019
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001020 __ bind(nested_statement.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001021 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001022}
1023
1024
1025void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1026 Comment cmnt(masm_, "[ ForInStatement");
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00001027 int slot = stmt->ForInFeedbackSlot();
1028
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001029 SetStatementPosition(stmt);
1030
1031 Label loop, exit;
1032 ForIn loop_statement(this, stmt);
1033 increment_loop_depth();
1034
danno@chromium.org1fd77d52013-06-07 16:01:45 +00001035 // Get the object to enumerate over. If the object is null or undefined, skip
1036 // over the loop. See ECMA-262 version 5, section 12.6.4.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001037 VisitForAccumulatorValue(stmt->enumerable());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001038 __ cmp(eax, isolate()->factory()->undefined_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001039 __ j(equal, &exit);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001040 __ cmp(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001041 __ j(equal, &exit);
1042
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001043 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1044
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001045 // Convert the object to a JS object.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001046 Label convert, done_convert;
whesse@chromium.org7b260152011-06-20 15:33:18 +00001047 __ JumpIfSmi(eax, &convert, Label::kNear);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001048 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001049 __ j(above_equal, &done_convert, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001050 __ bind(&convert);
1051 __ push(eax);
1052 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1053 __ bind(&done_convert);
1054 __ push(eax);
1055
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001056 // Check for proxies.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001057 Label call_runtime, use_cache, fixed_array;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001058 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1059 __ CmpObjectType(eax, LAST_JS_PROXY_TYPE, ecx);
1060 __ j(below_equal, &call_runtime);
1061
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001062 // Check cache validity in generated code. This is a fast case for
1063 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1064 // guarantee cache validity, call the runtime system to check cache
1065 // validity or get the property names in a fixed array.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001066 __ CheckEnumCache(&call_runtime);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001067
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001068 __ mov(eax, FieldOperand(eax, HeapObject::kMapOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001069 __ jmp(&use_cache, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001070
1071 // Get the set of properties to enumerate.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001072 __ bind(&call_runtime);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001073 __ push(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001074 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001075 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
1076 isolate()->factory()->meta_map());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001077 __ j(not_equal, &fixed_array);
1078
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001079
1080 // We got a map in register eax. Get the enumeration cache from it.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001081 Label no_descriptors;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001082 __ bind(&use_cache);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001083
1084 __ EnumLength(edx, eax);
1085 __ cmp(edx, Immediate(Smi::FromInt(0)));
1086 __ j(equal, &no_descriptors);
1087
danno@chromium.org40cb8782011-05-25 07:58:50 +00001088 __ LoadInstanceDescriptors(eax, ecx);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001089 __ mov(ecx, FieldOperand(ecx, DescriptorArray::kEnumCacheOffset));
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001090 __ mov(ecx, FieldOperand(ecx, DescriptorArray::kEnumCacheBridgeCacheOffset));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001091
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001092 // Set up the four remaining stack slots.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001093 __ push(eax); // Map.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001094 __ push(ecx); // Enumeration cache.
1095 __ push(edx); // Number of valid entries for the map in the enum cache.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001096 __ push(Immediate(Smi::FromInt(0))); // Initial index.
1097 __ jmp(&loop);
1098
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001099 __ bind(&no_descriptors);
1100 __ add(esp, Immediate(kPointerSize));
1101 __ jmp(&exit);
1102
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001103 // We got a fixed array in register eax. Iterate through that.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001104 Label non_proxy;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001105 __ bind(&fixed_array);
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001106
machenbach@chromium.org8545d492014-03-17 09:28:03 +00001107 Handle<Object> feedback = Handle<Object>(
1108 Smi::FromInt(TypeFeedbackInfo::kForInFastCaseMarker),
1109 isolate());
1110 StoreFeedbackVectorSlot(slot, feedback);
1111
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00001112 // No need for a write barrier, we are storing a Smi in the feedback vector.
1113 __ LoadHeapObject(ebx, FeedbackVector());
1114 __ mov(FieldOperand(ebx, FixedArray::OffsetOfElementAt(slot)),
machenbach@chromium.org8545d492014-03-17 09:28:03 +00001115 Immediate(Smi::FromInt(TypeFeedbackInfo::kForInSlowCaseMarker)));
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001116
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001117 __ mov(ebx, Immediate(Smi::FromInt(1))); // Smi indicates slow check
1118 __ mov(ecx, Operand(esp, 0 * kPointerSize)); // Get enumerated object
1119 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1120 __ CmpObjectType(ecx, LAST_JS_PROXY_TYPE, ecx);
1121 __ j(above, &non_proxy);
machenbach@chromium.orgaf9cfcb2013-11-19 11:05:18 +00001122 __ Set(ebx, Immediate(Smi::FromInt(0))); // Zero indicates proxy
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001123 __ bind(&non_proxy);
1124 __ push(ebx); // Smi
1125 __ push(eax); // Array
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001126 __ mov(eax, FieldOperand(eax, FixedArray::kLengthOffset));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001127 __ push(eax); // Fixed array length (as smi).
1128 __ push(Immediate(Smi::FromInt(0))); // Initial index.
1129
1130 // Generate code for doing the condition check.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001131 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001132 __ bind(&loop);
1133 __ mov(eax, Operand(esp, 0 * kPointerSize)); // Get the current index.
1134 __ cmp(eax, Operand(esp, 1 * kPointerSize)); // Compare to the array length.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001135 __ j(above_equal, loop_statement.break_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001136
1137 // Get the current entry of the array into register ebx.
1138 __ mov(ebx, Operand(esp, 2 * kPointerSize));
1139 __ mov(ebx, FieldOperand(ebx, eax, times_2, FixedArray::kHeaderSize));
1140
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001141 // Get the expected map from the stack or a smi in the
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001142 // permanent slow case into register edx.
1143 __ mov(edx, Operand(esp, 3 * kPointerSize));
1144
1145 // Check if the expected map still matches that of the enumerable.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001146 // If not, we may have to filter the key.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001147 Label update_each;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001148 __ mov(ecx, Operand(esp, 4 * kPointerSize));
1149 __ cmp(edx, FieldOperand(ecx, HeapObject::kMapOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001150 __ j(equal, &update_each, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001151
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001152 // For proxies, no filtering is done.
1153 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1154 ASSERT(Smi::FromInt(0) == 0);
1155 __ test(edx, edx);
1156 __ j(zero, &update_each);
1157
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001158 // Convert the entry to a string or null if it isn't a property
1159 // anymore. If the property has been removed while iterating, we
1160 // just skip it.
1161 __ push(ecx); // Enumerable.
1162 __ push(ebx); // Current entry.
1163 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001164 __ test(eax, eax);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001165 __ j(equal, loop_statement.continue_label());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001166 __ mov(ebx, eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001167
1168 // Update the 'each' property or variable from the possibly filtered
1169 // entry in register ebx.
1170 __ bind(&update_each);
1171 __ mov(result_register(), ebx);
1172 // Perform the assignment as if via '='.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001173 { EffectContext context(this);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001174 EmitAssignment(stmt->each());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001175 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001176
1177 // Generate code for the body of the loop.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001178 Visit(stmt->body());
1179
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001180 // Generate code for going to the next element by incrementing the
1181 // index (smi) stored on top of the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001182 __ bind(loop_statement.continue_label());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001183 __ add(Operand(esp, 0 * kPointerSize), Immediate(Smi::FromInt(1)));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001184
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001185 EmitBackEdgeBookkeeping(stmt, &loop);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001186 __ jmp(&loop);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001187
1188 // Remove the pointers stored on the stack.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001189 __ bind(loop_statement.break_label());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001190 __ add(esp, Immediate(5 * kPointerSize));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001191
1192 // Exit and decrement the loop depth.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001193 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001194 __ bind(&exit);
1195 decrement_loop_depth();
1196}
1197
1198
danno@chromium.org1fd77d52013-06-07 16:01:45 +00001199void FullCodeGenerator::VisitForOfStatement(ForOfStatement* stmt) {
1200 Comment cmnt(masm_, "[ ForOfStatement");
1201 SetStatementPosition(stmt);
1202
1203 Iteration loop_statement(this, stmt);
1204 increment_loop_depth();
1205
1206 // var iterator = iterable[@@iterator]()
1207 VisitForAccumulatorValue(stmt->assign_iterator());
1208
1209 // As with for-in, skip the loop if the iterator is null or undefined.
1210 __ CompareRoot(eax, Heap::kUndefinedValueRootIndex);
1211 __ j(equal, loop_statement.break_label());
1212 __ CompareRoot(eax, Heap::kNullValueRootIndex);
1213 __ j(equal, loop_statement.break_label());
1214
1215 // Convert the iterator to a JS object.
1216 Label convert, done_convert;
1217 __ JumpIfSmi(eax, &convert);
1218 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
1219 __ j(above_equal, &done_convert);
1220 __ bind(&convert);
1221 __ push(eax);
1222 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1223 __ bind(&done_convert);
1224
1225 // Loop entry.
1226 __ bind(loop_statement.continue_label());
1227
1228 // result = iterator.next()
1229 VisitForEffect(stmt->next_result());
1230
1231 // if (result.done) break;
1232 Label result_not_done;
1233 VisitForControl(stmt->result_done(),
1234 loop_statement.break_label(),
1235 &result_not_done,
1236 &result_not_done);
1237 __ bind(&result_not_done);
1238
1239 // each = result.value
1240 VisitForEffect(stmt->assign_each());
1241
1242 // Generate code for the body of the loop.
1243 Visit(stmt->body());
1244
1245 // Check stack before looping.
1246 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
1247 EmitBackEdgeBookkeeping(stmt, loop_statement.continue_label());
1248 __ jmp(loop_statement.continue_label());
1249
1250 // Exit and decrement the loop depth.
1251 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1252 __ bind(loop_statement.break_label());
1253 decrement_loop_depth();
1254}
1255
1256
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001257void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1258 bool pretenure) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001259 // Use the fast case closure allocation code that allocates in new
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001260 // space for nested functions that don't need literals cloning. If
1261 // we're running with the --always-opt or the --prepare-always-opt
1262 // flag, we need to use the runtime function so that the new function
1263 // we are creating here gets a chance to have its code optimized and
1264 // doesn't just get a copy of the existing unoptimized code.
1265 if (!FLAG_always_opt &&
1266 !FLAG_prepare_always_opt &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001267 !pretenure &&
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001268 scope()->is_function_scope() &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001269 info->num_literals() == 0) {
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001270 FastNewClosureStub stub(info->strict_mode(), info->is_generator());
verwaest@chromium.org662436e2013-08-28 08:41:27 +00001271 __ mov(ebx, Immediate(info));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001272 __ CallStub(&stub);
1273 } else {
1274 __ push(esi);
1275 __ push(Immediate(info));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001276 __ push(Immediate(pretenure
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001277 ? isolate()->factory()->true_value()
1278 : isolate()->factory()->false_value()));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001279 __ CallRuntime(Runtime::kNewClosure, 3);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001280 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001281 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001282}
1283
1284
1285void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
1286 Comment cmnt(masm_, "[ VariableProxy");
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001287 EmitVariableLoad(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001288}
1289
1290
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001291void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
1292 TypeofState typeof_state,
1293 Label* slow) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001294 Register context = esi;
1295 Register temp = edx;
1296
1297 Scope* s = scope();
1298 while (s != NULL) {
1299 if (s->num_heap_slots() > 0) {
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001300 if (s->calls_sloppy_eval()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001301 // Check that extension is NULL.
1302 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX),
1303 Immediate(0));
1304 __ j(not_equal, slow);
1305 }
1306 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001307 __ mov(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001308 // Walk the rest of the chain without clobbering esi.
1309 context = temp;
1310 }
1311 // If no outer scope calls eval, we do not need to check more
1312 // context extensions. If we have reached an eval scope, we check
1313 // all extensions from this point.
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001314 if (!s->outer_scope_calls_sloppy_eval() || s->is_eval_scope()) break;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001315 s = s->outer_scope();
1316 }
1317
1318 if (s != NULL && s->is_eval_scope()) {
1319 // Loop up the context chain. There is no frame effect so it is
1320 // safe to use raw labels here.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001321 Label next, fast;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001322 if (!context.is(temp)) {
1323 __ mov(temp, context);
1324 }
1325 __ bind(&next);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001326 // Terminate at native context.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001327 __ cmp(FieldOperand(temp, HeapObject::kMapOffset),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001328 Immediate(isolate()->factory()->native_context_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001329 __ j(equal, &fast, Label::kNear);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001330 // Check that extension is NULL.
1331 __ cmp(ContextOperand(temp, Context::EXTENSION_INDEX), Immediate(0));
1332 __ j(not_equal, slow);
1333 // Load next context in chain.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001334 __ mov(temp, ContextOperand(temp, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001335 __ jmp(&next);
1336 __ bind(&fast);
1337 }
1338
1339 // All extension objects were empty and it is safe to use a global
1340 // load IC call.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001341 __ mov(edx, GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001342 __ mov(ecx, var->name());
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +00001343 ContextualMode mode = (typeof_state == INSIDE_TYPEOF)
1344 ? NOT_CONTEXTUAL
1345 : CONTEXTUAL;
1346
1347 CallLoadIC(mode);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001348}
1349
1350
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001351MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1352 Label* slow) {
1353 ASSERT(var->IsContextSlot());
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001354 Register context = esi;
1355 Register temp = ebx;
1356
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001357 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001358 if (s->num_heap_slots() > 0) {
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001359 if (s->calls_sloppy_eval()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001360 // Check that extension is NULL.
1361 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX),
1362 Immediate(0));
1363 __ j(not_equal, slow);
1364 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001365 __ mov(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001366 // Walk the rest of the chain without clobbering esi.
1367 context = temp;
1368 }
1369 }
1370 // Check that last extension is NULL.
1371 __ cmp(ContextOperand(context, Context::EXTENSION_INDEX), Immediate(0));
1372 __ j(not_equal, slow);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001373
1374 // This function is used only for loads, not stores, so it's safe to
1375 // return an esi-based operand (the write barrier cannot be allowed to
1376 // destroy the esi register).
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001377 return ContextOperand(context, var->index());
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001378}
1379
1380
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001381void FullCodeGenerator::EmitDynamicLookupFastCase(Variable* var,
1382 TypeofState typeof_state,
1383 Label* slow,
1384 Label* done) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001385 // Generate fast-case code for variables that might be shadowed by
1386 // eval-introduced variables. Eval is used a lot without
1387 // introducing variables. In those cases, we do not want to
1388 // perform a runtime call for all variables in the scope
1389 // containing the eval.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001390 if (var->mode() == DYNAMIC_GLOBAL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001391 EmitLoadGlobalCheckExtensions(var, typeof_state, slow);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001392 __ jmp(done);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001393 } else if (var->mode() == DYNAMIC_LOCAL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001394 Variable* local = var->local_if_not_shadowed();
1395 __ mov(eax, ContextSlotOperandCheckExtensions(local, slow));
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001396 if (local->mode() == LET || local->mode() == CONST ||
1397 local->mode() == CONST_LEGACY) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001398 __ cmp(eax, isolate()->factory()->the_hole_value());
1399 __ j(not_equal, done);
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001400 if (local->mode() == CONST_LEGACY) {
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001401 __ mov(eax, isolate()->factory()->undefined_value());
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001402 } else { // LET || CONST
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001403 __ push(Immediate(var->name()));
1404 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1405 }
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001406 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001407 __ jmp(done);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001408 }
1409}
1410
1411
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001412void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1413 // Record position before possible IC call.
1414 SetSourcePosition(proxy->position());
1415 Variable* var = proxy->var();
1416
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001417 // Three cases: global variables, lookup variables, and all other types of
1418 // variables.
1419 switch (var->location()) {
1420 case Variable::UNALLOCATED: {
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00001421 Comment cmnt(masm_, "[ Global variable");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001422 // Use inline caching. Variable name is passed in ecx and the global
1423 // object in eax.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001424 __ mov(edx, GlobalObjectOperand());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001425 __ mov(ecx, var->name());
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +00001426 CallLoadIC(CONTEXTUAL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001427 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001428 break;
1429 }
1430
1431 case Variable::PARAMETER:
1432 case Variable::LOCAL:
1433 case Variable::CONTEXT: {
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00001434 Comment cmnt(masm_, var->IsContextSlot() ? "[ Context variable"
1435 : "[ Stack variable");
danno@chromium.orgc612e022011-11-10 11:38:15 +00001436 if (var->binding_needs_init()) {
1437 // var->scope() may be NULL when the proxy is located in eval code and
1438 // refers to a potential outside binding. Currently those bindings are
1439 // always looked up dynamically, i.e. in that case
1440 // var->location() == LOOKUP.
1441 // always holds.
1442 ASSERT(var->scope() != NULL);
1443
1444 // Check if the binding really needs an initialization check. The check
1445 // can be skipped in the following situation: we have a LET or CONST
1446 // binding in harmony mode, both the Variable and the VariableProxy have
1447 // the same declaration scope (i.e. they are both in global code, in the
1448 // same function or in the same eval code) and the VariableProxy is in
1449 // the source physically located after the initializer of the variable.
1450 //
1451 // We cannot skip any initialization checks for CONST in non-harmony
1452 // mode because const variables may be declared but never initialized:
1453 // if (false) { const x; }; var y = x;
1454 //
1455 // The condition on the declaration scopes is a conservative check for
1456 // nested functions that access a binding and are called before the
1457 // binding is initialized:
1458 // function() { f(); let x = 1; function f() { x = 2; } }
1459 //
1460 bool skip_init_check;
1461 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1462 skip_init_check = false;
jkummerow@chromium.orgac45fed2011-11-07 13:11:02 +00001463 } else {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001464 // Check that we always have valid source position.
1465 ASSERT(var->initializer_position() != RelocInfo::kNoPosition);
1466 ASSERT(proxy->position() != RelocInfo::kNoPosition);
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001467 skip_init_check = var->mode() != CONST_LEGACY &&
danno@chromium.orgc612e022011-11-10 11:38:15 +00001468 var->initializer_position() < proxy->position();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001469 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001470
1471 if (!skip_init_check) {
1472 // Let and const need a read barrier.
1473 Label done;
1474 GetVar(eax, var);
1475 __ cmp(eax, isolate()->factory()->the_hole_value());
1476 __ j(not_equal, &done, Label::kNear);
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001477 if (var->mode() == LET || var->mode() == CONST) {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001478 // Throw a reference error when using an uninitialized let/const
1479 // binding in harmony mode.
1480 __ push(Immediate(var->name()));
1481 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1482 } else {
1483 // Uninitalized const bindings outside of harmony mode are unholed.
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001484 ASSERT(var->mode() == CONST_LEGACY);
danno@chromium.orgc612e022011-11-10 11:38:15 +00001485 __ mov(eax, isolate()->factory()->undefined_value());
1486 }
1487 __ bind(&done);
1488 context()->Plug(eax);
1489 break;
1490 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001491 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001492 context()->Plug(var);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001493 break;
1494 }
1495
1496 case Variable::LOOKUP: {
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00001497 Comment cmnt(masm_, "[ Lookup variable");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001498 Label done, slow;
1499 // Generate code for loading from variables potentially shadowed
1500 // by eval-introduced variables.
1501 EmitDynamicLookupFastCase(var, NOT_INSIDE_TYPEOF, &slow, &done);
1502 __ bind(&slow);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001503 __ push(esi); // Context.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001504 __ push(Immediate(var->name()));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001505 __ CallRuntime(Runtime::kLoadContextSlot, 2);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001506 __ bind(&done);
1507 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001508 break;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001509 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001510 }
1511}
1512
1513
1514void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1515 Comment cmnt(masm_, "[ RegExpLiteral");
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001516 Label materialized;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001517 // Registers will be used as follows:
1518 // edi = JS function.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001519 // ecx = literals array.
1520 // ebx = regexp literal.
1521 // eax = regexp literal clone.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001522 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001523 __ mov(ecx, FieldOperand(edi, JSFunction::kLiteralsOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001524 int literal_offset =
ricow@chromium.org65fae842010-08-25 15:26:24 +00001525 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001526 __ mov(ebx, FieldOperand(ecx, literal_offset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001527 __ cmp(ebx, isolate()->factory()->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001528 __ j(not_equal, &materialized, Label::kNear);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001529
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001530 // Create regexp literal using runtime function
1531 // Result will be in eax.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001532 __ push(ecx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001533 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1534 __ push(Immediate(expr->pattern()));
1535 __ push(Immediate(expr->flags()));
1536 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001537 __ mov(ebx, eax);
1538
1539 __ bind(&materialized);
1540 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1541 Label allocated, runtime_allocate;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001542 __ Allocate(size, eax, ecx, edx, &runtime_allocate, TAG_OBJECT);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001543 __ jmp(&allocated);
1544
1545 __ bind(&runtime_allocate);
1546 __ push(ebx);
1547 __ push(Immediate(Smi::FromInt(size)));
1548 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1549 __ pop(ebx);
1550
1551 __ bind(&allocated);
1552 // Copy the content into the newly allocated memory.
1553 // (Unroll copy loop once for better throughput).
1554 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
1555 __ mov(edx, FieldOperand(ebx, i));
1556 __ mov(ecx, FieldOperand(ebx, i + kPointerSize));
1557 __ mov(FieldOperand(eax, i), edx);
1558 __ mov(FieldOperand(eax, i + kPointerSize), ecx);
1559 }
1560 if ((size % (2 * kPointerSize)) != 0) {
1561 __ mov(edx, FieldOperand(ebx, size - kPointerSize));
1562 __ mov(FieldOperand(eax, size - kPointerSize), edx);
1563 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001564 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001565}
1566
1567
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001568void FullCodeGenerator::EmitAccessor(Expression* expression) {
1569 if (expression == NULL) {
1570 __ push(Immediate(isolate()->factory()->null_value()));
1571 } else {
1572 VisitForStackValue(expression);
1573 }
1574}
1575
1576
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001577void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1578 Comment cmnt(masm_, "[ ObjectLiteral");
machenbach@chromium.orge8412be2013-11-08 10:23:52 +00001579
machenbach@chromium.org7ff76072013-11-21 09:47:43 +00001580 expr->BuildConstantProperties(isolate());
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001581 Handle<FixedArray> constant_properties = expr->constant_properties();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001582 int flags = expr->fast_elements()
1583 ? ObjectLiteral::kFastElements
1584 : ObjectLiteral::kNoFlags;
1585 flags |= expr->has_function()
1586 ? ObjectLiteral::kHasFunction
1587 : ObjectLiteral::kNoFlags;
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001588 int properties_count = constant_properties->length() / 2;
titzer@chromium.orgbc176052014-03-05 15:10:53 +00001589 if (expr->may_store_doubles() || expr->depth() > 1 || Serializer::enabled() ||
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001590 flags != ObjectLiteral::kFastElements ||
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001591 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001592 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1593 __ push(FieldOperand(edi, JSFunction::kLiteralsOffset));
1594 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1595 __ push(Immediate(constant_properties));
1596 __ push(Immediate(Smi::FromInt(flags)));
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001597 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001598 } else {
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001599 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1600 __ mov(eax, FieldOperand(edi, JSFunction::kLiteralsOffset));
1601 __ mov(ebx, Immediate(Smi::FromInt(expr->literal_index())));
1602 __ mov(ecx, Immediate(constant_properties));
1603 __ mov(edx, Immediate(Smi::FromInt(flags)));
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001604 FastCloneShallowObjectStub stub(properties_count);
1605 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001606 }
1607
1608 // If result_saved is true the result is on top of the stack. If
1609 // result_saved is false the result is in eax.
1610 bool result_saved = false;
1611
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001612 // Mark all computed expressions that are bound to a key that
1613 // is shadowed by a later occurrence of the same key. For the
1614 // marked expressions, no store code is emitted.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001615 expr->CalculateEmitStore(zone());
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001616
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001617 AccessorTable accessor_table(zone());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001618 for (int i = 0; i < expr->properties()->length(); i++) {
1619 ObjectLiteral::Property* property = expr->properties()->at(i);
1620 if (property->IsCompileTimeValue()) continue;
1621
1622 Literal* key = property->key();
1623 Expression* value = property->value();
1624 if (!result_saved) {
1625 __ push(eax); // Save result on the stack
1626 result_saved = true;
1627 }
1628 switch (property->kind()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001629 case ObjectLiteral::Property::CONSTANT:
1630 UNREACHABLE();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001631 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1632 ASSERT(!CompileTimeValue::IsCompileTimeValue(value));
1633 // Fall through.
1634 case ObjectLiteral::Property::COMPUTED:
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001635 if (key->value()->IsInternalizedString()) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001636 if (property->emit_store()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001637 VisitForAccumulatorValue(value);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001638 __ mov(ecx, Immediate(key->value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001639 __ mov(edx, Operand(esp, 0));
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00001640 CallStoreIC(key->LiteralFeedbackId());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001641 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1642 } else {
1643 VisitForEffect(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001644 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001645 break;
1646 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001647 __ push(Operand(esp, 0)); // Duplicate receiver.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001648 VisitForStackValue(key);
1649 VisitForStackValue(value);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001650 if (property->emit_store()) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001651 __ push(Immediate(Smi::FromInt(NONE))); // PropertyAttributes
1652 __ CallRuntime(Runtime::kSetProperty, 4);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001653 } else {
1654 __ Drop(3);
1655 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001656 break;
ulan@chromium.org750145a2013-03-07 15:14:13 +00001657 case ObjectLiteral::Property::PROTOTYPE:
1658 __ push(Operand(esp, 0)); // Duplicate receiver.
1659 VisitForStackValue(value);
1660 if (property->emit_store()) {
1661 __ CallRuntime(Runtime::kSetPrototype, 2);
1662 } else {
1663 __ Drop(2);
1664 }
1665 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001666 case ObjectLiteral::Property::GETTER:
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001667 accessor_table.lookup(key)->second->getter = value;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001668 break;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001669 case ObjectLiteral::Property::SETTER:
1670 accessor_table.lookup(key)->second->setter = value;
1671 break;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001672 }
1673 }
1674
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001675 // Emit code to define accessors, using only a single call to the runtime for
1676 // each pair of corresponding getters and setters.
1677 for (AccessorTable::Iterator it = accessor_table.begin();
1678 it != accessor_table.end();
1679 ++it) {
1680 __ push(Operand(esp, 0)); // Duplicate receiver.
1681 VisitForStackValue(it->first);
1682 EmitAccessor(it->second->getter);
1683 EmitAccessor(it->second->setter);
1684 __ push(Immediate(Smi::FromInt(NONE)));
1685 __ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
1686 }
1687
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001688 if (expr->has_function()) {
1689 ASSERT(result_saved);
1690 __ push(Operand(esp, 0));
1691 __ CallRuntime(Runtime::kToFastProperties, 1);
1692 }
1693
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001694 if (result_saved) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001695 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001696 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001697 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001698 }
1699}
1700
1701
1702void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1703 Comment cmnt(masm_, "[ ArrayLiteral");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001704
machenbach@chromium.org7ff76072013-11-21 09:47:43 +00001705 expr->BuildConstantElements(isolate());
machenbach@chromium.org37be4082013-11-26 13:50:38 +00001706 int flags = expr->depth() == 1
1707 ? ArrayLiteral::kShallowElements
1708 : ArrayLiteral::kNoFlags;
1709
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001710 ZoneList<Expression*>* subexprs = expr->values();
1711 int length = subexprs->length();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001712 Handle<FixedArray> constant_elements = expr->constant_elements();
1713 ASSERT_EQ(2, constant_elements->length());
1714 ElementsKind constant_elements_kind =
1715 static_cast<ElementsKind>(Smi::cast(constant_elements->get(0))->value());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001716 bool has_constant_fast_elements =
1717 IsFastObjectElementsKind(constant_elements_kind);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001718 Handle<FixedArrayBase> constant_elements_values(
1719 FixedArrayBase::cast(constant_elements->get(1)));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001720
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +00001721 AllocationSiteMode allocation_site_mode = TRACK_ALLOCATION_SITE;
machenbach@chromium.orgc86e8c22013-11-27 15:11:04 +00001722 if (has_constant_fast_elements && !FLAG_allocation_site_pretenuring) {
1723 // If the only customer of allocation sites is transitioning, then
1724 // we can turn it off if we don't have anywhere else to transition to.
1725 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
1726 }
1727
erikcorry0ad885c2011-11-21 13:51:57 +00001728 Heap* heap = isolate()->heap();
1729 if (has_constant_fast_elements &&
1730 constant_elements_values->map() == heap->fixed_cow_array_map()) {
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001731 // If the elements are already FAST_*_ELEMENTS, the boilerplate cannot
erikcorry0ad885c2011-11-21 13:51:57 +00001732 // change, so it's possible to specialize the stub in advance.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001733 __ IncrementCounter(isolate()->counters()->cow_arrays_created_stub(), 1);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001734 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1735 __ mov(eax, FieldOperand(ebx, JSFunction::kLiteralsOffset));
1736 __ mov(ebx, Immediate(Smi::FromInt(expr->literal_index())));
1737 __ mov(ecx, Immediate(constant_elements));
erikcorry0ad885c2011-11-21 13:51:57 +00001738 FastCloneShallowArrayStub stub(
1739 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS,
machenbach@chromium.orgc86e8c22013-11-27 15:11:04 +00001740 allocation_site_mode,
erikcorry0ad885c2011-11-21 13:51:57 +00001741 length);
1742 __ CallStub(&stub);
machenbach@chromium.org7ff76072013-11-21 09:47:43 +00001743 } else if (expr->depth() > 1 || Serializer::enabled() ||
verwaest@chromium.org057bd502013-11-06 12:03:29 +00001744 length > FastCloneShallowArrayStub::kMaximumClonedLength) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001745 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1746 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
1747 __ push(Immediate(Smi::FromInt(expr->literal_index())));
1748 __ push(Immediate(constant_elements));
machenbach@chromium.org37be4082013-11-26 13:50:38 +00001749 __ push(Immediate(Smi::FromInt(flags)));
1750 __ CallRuntime(Runtime::kCreateArrayLiteral, 4);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001751 } else {
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001752 ASSERT(IsFastSmiOrObjectElementsKind(constant_elements_kind) ||
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001753 FLAG_smi_only_arrays);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001754 FastCloneShallowArrayStub::Mode mode =
1755 FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001756
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001757 // If the elements are already FAST_*_ELEMENTS, the boilerplate cannot
erikcorry0ad885c2011-11-21 13:51:57 +00001758 // change, so it's possible to specialize the stub in advance.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001759 if (has_constant_fast_elements) {
1760 mode = FastCloneShallowArrayStub::CLONE_ELEMENTS;
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001761 }
1762
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001763 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1764 __ mov(eax, FieldOperand(ebx, JSFunction::kLiteralsOffset));
1765 __ mov(ebx, Immediate(Smi::FromInt(expr->literal_index())));
1766 __ mov(ecx, Immediate(constant_elements));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001767 FastCloneShallowArrayStub stub(mode, allocation_site_mode, length);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001768 __ CallStub(&stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001769 }
1770
1771 bool result_saved = false; // Is the result saved to the stack?
1772
1773 // Emit code to evaluate all the non-constant subexpressions and to store
1774 // them into the newly cloned array.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001775 for (int i = 0; i < length; i++) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001776 Expression* subexpr = subexprs->at(i);
1777 // If the subexpression is a literal or a simple materialized literal it
1778 // is already set in the cloned array.
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001779 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001780
1781 if (!result_saved) {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001782 __ push(eax); // array literal.
1783 __ push(Immediate(Smi::FromInt(expr->literal_index())));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001784 result_saved = true;
1785 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001786 VisitForAccumulatorValue(subexpr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001787
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001788 if (IsFastObjectElementsKind(constant_elements_kind)) {
1789 // Fast-case array literal with ElementsKind of FAST_*_ELEMENTS, they
1790 // cannot transition and don't need to call the runtime stub.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001791 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001792 __ mov(ebx, Operand(esp, kPointerSize)); // Copy of array literal.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001793 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
1794 // Store the subexpression value in the array's elements.
1795 __ mov(FieldOperand(ebx, offset), result_register());
1796 // Update the write barrier for the array store.
1797 __ RecordWriteField(ebx, offset, result_register(), ecx,
1798 kDontSaveFPRegs,
1799 EMIT_REMEMBERED_SET,
1800 INLINE_SMI_CHECK);
1801 } else {
1802 // Store the subexpression value in the array's elements.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001803 __ mov(ecx, Immediate(Smi::FromInt(i)));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001804 StoreArrayLiteralElementStub stub;
1805 __ CallStub(&stub);
1806 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001807
1808 PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001809 }
1810
1811 if (result_saved) {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001812 __ add(esp, Immediate(kPointerSize)); // literal index
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001813 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001814 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001815 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001816 }
1817}
1818
1819
ager@chromium.org5c838252010-02-19 08:53:10 +00001820void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1821 Comment cmnt(masm_, "[ Assignment");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001822 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
1823 // on the left-hand side.
1824 if (!expr->target()->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001825 VisitForEffect(expr->target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001826 return;
1827 }
1828
ager@chromium.org5c838252010-02-19 08:53:10 +00001829 // Left-hand side can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00001830 // slot.
ager@chromium.org5c838252010-02-19 08:53:10 +00001831 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1832 LhsKind assign_type = VARIABLE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001833 Property* property = expr->target()->AsProperty();
1834 if (property != NULL) {
1835 assign_type = (property->key()->IsPropertyName())
1836 ? NAMED_PROPERTY
1837 : KEYED_PROPERTY;
ager@chromium.org5c838252010-02-19 08:53:10 +00001838 }
1839
1840 // Evaluate LHS expression.
1841 switch (assign_type) {
1842 case VARIABLE:
1843 // Nothing to do here.
1844 break;
1845 case NAMED_PROPERTY:
1846 if (expr->is_compound()) {
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001847 // We need the receiver both on the stack and in edx.
1848 VisitForStackValue(property->obj());
1849 __ mov(edx, Operand(esp, 0));
ager@chromium.org5c838252010-02-19 08:53:10 +00001850 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001851 VisitForStackValue(property->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00001852 }
1853 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001854 case KEYED_PROPERTY: {
ager@chromium.org5c838252010-02-19 08:53:10 +00001855 if (expr->is_compound()) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00001856 VisitForStackValue(property->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001857 VisitForStackValue(property->key());
1858 __ mov(edx, Operand(esp, kPointerSize)); // Object.
1859 __ mov(ecx, Operand(esp, 0)); // Key.
ager@chromium.org5c838252010-02-19 08:53:10 +00001860 } else {
whesse@chromium.org7b260152011-06-20 15:33:18 +00001861 VisitForStackValue(property->obj());
1862 VisitForStackValue(property->key());
ager@chromium.org5c838252010-02-19 08:53:10 +00001863 }
1864 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001865 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001866 }
1867
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001868 // For compound assignments we need another deoptimization point after the
1869 // variable/property load.
ager@chromium.org5c838252010-02-19 08:53:10 +00001870 if (expr->is_compound()) {
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001871 AccumulatorValueContext result_context(this);
1872 { AccumulatorValueContext left_operand_context(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001873 switch (assign_type) {
1874 case VARIABLE:
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001875 EmitVariableLoad(expr->target()->AsVariableProxy());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001876 PrepareForBailout(expr->target(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001877 break;
1878 case NAMED_PROPERTY:
1879 EmitNamedPropertyLoad(property);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001880 PrepareForBailoutForId(property->LoadId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001881 break;
1882 case KEYED_PROPERTY:
1883 EmitKeyedPropertyLoad(property);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001884 PrepareForBailoutForId(property->LoadId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001885 break;
1886 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001887 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001888
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001889 Token::Value op = expr->binary_op();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001890 __ push(eax); // Left operand goes on the stack.
1891 VisitForAccumulatorValue(expr->value());
ager@chromium.org5c838252010-02-19 08:53:10 +00001892
ricow@chromium.org65fae842010-08-25 15:26:24 +00001893 OverwriteMode mode = expr->value()->ResultOverwriteAllowed()
1894 ? OVERWRITE_RIGHT
1895 : NO_OVERWRITE;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001896 SetSourcePosition(expr->position() + 1);
1897 if (ShouldInlineSmiCase(op)) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001898 EmitInlineSmiBinaryOp(expr->binary_operation(),
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001899 op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001900 mode,
1901 expr->target(),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001902 expr->value());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001903 } else {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001904 EmitBinaryOp(expr->binary_operation(), op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001905 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001906
1907 // Deoptimization point in case the binary operation may have side effects.
1908 PrepareForBailout(expr->binary_operation(), TOS_REG);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001909 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001910 VisitForAccumulatorValue(expr->value());
ager@chromium.org5c838252010-02-19 08:53:10 +00001911 }
1912
1913 // Record source position before possible IC call.
1914 SetSourcePosition(expr->position());
1915
1916 // Store the value.
1917 switch (assign_type) {
1918 case VARIABLE:
1919 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001920 expr->op());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001921 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1922 context()->Plug(eax);
ager@chromium.org5c838252010-02-19 08:53:10 +00001923 break;
1924 case NAMED_PROPERTY:
1925 EmitNamedPropertyAssignment(expr);
1926 break;
1927 case KEYED_PROPERTY:
1928 EmitKeyedPropertyAssignment(expr);
1929 break;
1930 }
1931}
1932
1933
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001934void FullCodeGenerator::VisitYield(Yield* expr) {
1935 Comment cmnt(masm_, "[ Yield");
1936 // Evaluate yielded value first; the initial iterator definition depends on
1937 // this. It stays on the stack while we update the iterator.
1938 VisitForStackValue(expr->expression());
1939
1940 switch (expr->yield_kind()) {
danno@chromium.org41728482013-06-12 22:31:22 +00001941 case Yield::SUSPEND:
1942 // Pop value from top-of-stack slot; box result into result register.
1943 EmitCreateIteratorResult(false);
1944 __ push(result_register());
1945 // Fall through.
1946 case Yield::INITIAL: {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001947 Label suspend, continuation, post_runtime, resume;
1948
1949 __ jmp(&suspend);
1950
1951 __ bind(&continuation);
1952 __ jmp(&resume);
1953
1954 __ bind(&suspend);
1955 VisitForAccumulatorValue(expr->generator_object());
1956 ASSERT(continuation.pos() > 0 && Smi::IsValid(continuation.pos()));
1957 __ mov(FieldOperand(eax, JSGeneratorObject::kContinuationOffset),
1958 Immediate(Smi::FromInt(continuation.pos())));
1959 __ mov(FieldOperand(eax, JSGeneratorObject::kContextOffset), esi);
1960 __ mov(ecx, esi);
1961 __ RecordWriteField(eax, JSGeneratorObject::kContextOffset, ecx, edx,
1962 kDontSaveFPRegs);
1963 __ lea(ebx, Operand(ebp, StandardFrameConstants::kExpressionsOffset));
1964 __ cmp(esp, ebx);
1965 __ j(equal, &post_runtime);
1966 __ push(eax); // generator object
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001967 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
1968 __ mov(context_register(),
1969 Operand(ebp, StandardFrameConstants::kContextOffset));
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001970 __ bind(&post_runtime);
danno@chromium.org41728482013-06-12 22:31:22 +00001971 __ pop(result_register());
1972 EmitReturnSequence();
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001973
1974 __ bind(&resume);
1975 context()->Plug(result_register());
1976 break;
1977 }
1978
1979 case Yield::FINAL: {
1980 VisitForAccumulatorValue(expr->generator_object());
1981 __ mov(FieldOperand(result_register(),
1982 JSGeneratorObject::kContinuationOffset),
1983 Immediate(Smi::FromInt(JSGeneratorObject::kGeneratorClosed)));
danno@chromium.org41728482013-06-12 22:31:22 +00001984 // Pop value from top-of-stack slot, box result into result register.
1985 EmitCreateIteratorResult(true);
1986 EmitUnwindBeforeReturn();
1987 EmitReturnSequence();
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001988 break;
1989 }
1990
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001991 case Yield::DELEGATING: {
1992 VisitForStackValue(expr->generator_object());
1993
1994 // Initial stack layout is as follows:
1995 // [sp + 1 * kPointerSize] iter
1996 // [sp + 0 * kPointerSize] g
1997
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001998 Label l_catch, l_try, l_suspend, l_continuation, l_resume;
1999 Label l_next, l_call, l_loop;
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002000 // Initial send value is undefined.
2001 __ mov(eax, isolate()->factory()->undefined_value());
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002002 __ jmp(&l_next);
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002003
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00002004 // catch (e) { receiver = iter; f = 'throw'; arg = e; goto l_call; }
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002005 __ bind(&l_catch);
2006 handler_table()->set(expr->index(), Smi::FromInt(l_catch.pos()));
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002007 __ mov(ecx, isolate()->factory()->throw_string()); // "throw"
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00002008 __ push(ecx); // "throw"
2009 __ push(Operand(esp, 2 * kPointerSize)); // iter
2010 __ push(eax); // exception
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002011 __ jmp(&l_call);
2012
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00002013 // try { received = %yield result }
2014 // Shuffle the received result above a try handler and yield it without
2015 // re-boxing.
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002016 __ bind(&l_try);
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00002017 __ pop(eax); // result
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002018 __ PushTryHandler(StackHandler::CATCH, expr->index());
2019 const int handler_size = StackHandlerConstants::kSize;
danno@chromium.org41728482013-06-12 22:31:22 +00002020 __ push(eax); // result
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002021 __ jmp(&l_suspend);
2022 __ bind(&l_continuation);
2023 __ jmp(&l_resume);
2024 __ bind(&l_suspend);
2025 const int generator_object_depth = kPointerSize + handler_size;
2026 __ mov(eax, Operand(esp, generator_object_depth));
2027 __ push(eax); // g
2028 ASSERT(l_continuation.pos() > 0 && Smi::IsValid(l_continuation.pos()));
2029 __ mov(FieldOperand(eax, JSGeneratorObject::kContinuationOffset),
2030 Immediate(Smi::FromInt(l_continuation.pos())));
2031 __ mov(FieldOperand(eax, JSGeneratorObject::kContextOffset), esi);
2032 __ mov(ecx, esi);
2033 __ RecordWriteField(eax, JSGeneratorObject::kContextOffset, ecx, edx,
2034 kDontSaveFPRegs);
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002035 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
2036 __ mov(context_register(),
2037 Operand(ebp, StandardFrameConstants::kContextOffset));
danno@chromium.org41728482013-06-12 22:31:22 +00002038 __ pop(eax); // result
2039 EmitReturnSequence();
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002040 __ bind(&l_resume); // received in eax
2041 __ PopTryHandler();
2042
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002043 // receiver = iter; f = iter.next; arg = received;
2044 __ bind(&l_next);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002045 __ mov(ecx, isolate()->factory()->next_string()); // "next"
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00002046 __ push(ecx);
2047 __ push(Operand(esp, 2 * kPointerSize)); // iter
2048 __ push(eax); // received
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002049
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00002050 // result = receiver[f](arg);
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002051 __ bind(&l_call);
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002052 __ mov(edx, Operand(esp, kPointerSize));
2053 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002054 CallIC(ic, TypeFeedbackId::None());
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002055 __ mov(edi, eax);
2056 __ mov(Operand(esp, 2 * kPointerSize), edi);
2057 CallFunctionStub stub(1, CALL_AS_METHOD);
2058 __ CallStub(&stub);
2059
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002060 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002061 __ Drop(1); // The function is still on the stack; drop it.
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002062
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00002063 // if (!result.done) goto l_try;
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002064 __ bind(&l_loop);
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002065 __ push(eax); // save result
2066 __ mov(edx, eax); // result
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002067 __ mov(ecx, isolate()->factory()->done_string()); // "done"
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +00002068 CallLoadIC(NOT_CONTEXTUAL); // result.done in eax
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00002069 Handle<Code> bool_ic = ToBooleanStub::GetUninitialized(isolate());
2070 CallIC(bool_ic);
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002071 __ test(eax, eax);
2072 __ j(zero, &l_try);
2073
2074 // result.value
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +00002075 __ pop(edx); // result
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00002076 __ mov(ecx, isolate()->factory()->value_string()); // "value"
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +00002077 CallLoadIC(NOT_CONTEXTUAL); // result.value in eax
2078 context()->DropAndPlug(2, eax); // drop iter and g
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00002079 break;
2080 }
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00002081 }
2082}
2083
2084
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002085void FullCodeGenerator::EmitGeneratorResume(Expression *generator,
2086 Expression *value,
2087 JSGeneratorObject::ResumeMode resume_mode) {
2088 // The value stays in eax, and is ultimately read by the resumed generator, as
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002089 // if the CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
2090 // is read to throw the value when the resumed generator is already closed.
2091 // ebx will hold the generator object until the activation has been resumed.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002092 VisitForStackValue(generator);
2093 VisitForAccumulatorValue(value);
2094 __ pop(ebx);
2095
2096 // Check generator state.
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002097 Label wrong_state, closed_state, done;
2098 STATIC_ASSERT(JSGeneratorObject::kGeneratorExecuting < 0);
2099 STATIC_ASSERT(JSGeneratorObject::kGeneratorClosed == 0);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002100 __ cmp(FieldOperand(ebx, JSGeneratorObject::kContinuationOffset),
2101 Immediate(Smi::FromInt(0)));
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002102 __ j(equal, &closed_state);
2103 __ j(less, &wrong_state);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002104
2105 // Load suspended function and context.
2106 __ mov(esi, FieldOperand(ebx, JSGeneratorObject::kContextOffset));
2107 __ mov(edi, FieldOperand(ebx, JSGeneratorObject::kFunctionOffset));
2108
2109 // Push receiver.
2110 __ push(FieldOperand(ebx, JSGeneratorObject::kReceiverOffset));
2111
2112 // Push holes for arguments to generator function.
2113 __ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2114 __ mov(edx,
2115 FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
2116 __ mov(ecx, isolate()->factory()->the_hole_value());
2117 Label push_argument_holes, push_frame;
2118 __ bind(&push_argument_holes);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002119 __ sub(edx, Immediate(Smi::FromInt(1)));
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002120 __ j(carry, &push_frame);
2121 __ push(ecx);
2122 __ jmp(&push_argument_holes);
2123
2124 // Enter a new JavaScript frame, and initialize its slots as they were when
2125 // the generator was suspended.
2126 Label resume_frame;
2127 __ bind(&push_frame);
2128 __ call(&resume_frame);
2129 __ jmp(&done);
2130 __ bind(&resume_frame);
2131 __ push(ebp); // Caller's frame pointer.
2132 __ mov(ebp, esp);
2133 __ push(esi); // Callee's context.
2134 __ push(edi); // Callee's JS Function.
2135
2136 // Load the operand stack size.
2137 __ mov(edx, FieldOperand(ebx, JSGeneratorObject::kOperandStackOffset));
2138 __ mov(edx, FieldOperand(edx, FixedArray::kLengthOffset));
2139 __ SmiUntag(edx);
2140
2141 // If we are sending a value and there is no operand stack, we can jump back
2142 // in directly.
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002143 if (resume_mode == JSGeneratorObject::NEXT) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002144 Label slow_resume;
2145 __ cmp(edx, Immediate(0));
2146 __ j(not_zero, &slow_resume);
2147 __ mov(edx, FieldOperand(edi, JSFunction::kCodeEntryOffset));
2148 __ mov(ecx, FieldOperand(ebx, JSGeneratorObject::kContinuationOffset));
2149 __ SmiUntag(ecx);
2150 __ add(edx, ecx);
2151 __ mov(FieldOperand(ebx, JSGeneratorObject::kContinuationOffset),
2152 Immediate(Smi::FromInt(JSGeneratorObject::kGeneratorExecuting)));
2153 __ jmp(edx);
2154 __ bind(&slow_resume);
2155 }
2156
2157 // Otherwise, we push holes for the operand stack and call the runtime to fix
2158 // up the stack and the handlers.
2159 Label push_operand_holes, call_resume;
2160 __ bind(&push_operand_holes);
2161 __ sub(edx, Immediate(1));
2162 __ j(carry, &call_resume);
2163 __ push(ecx);
2164 __ jmp(&push_operand_holes);
2165 __ bind(&call_resume);
2166 __ push(ebx);
2167 __ push(result_register());
2168 __ Push(Smi::FromInt(resume_mode));
2169 __ CallRuntime(Runtime::kResumeJSGeneratorObject, 3);
2170 // Not reached: the runtime call returns elsewhere.
danno@chromium.org59400602013-08-13 17:09:37 +00002171 __ Abort(kGeneratorFailedToResume);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002172
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002173 // Reach here when generator is closed.
2174 __ bind(&closed_state);
2175 if (resume_mode == JSGeneratorObject::NEXT) {
2176 // Return completed iterator result when generator is closed.
2177 __ push(Immediate(isolate()->factory()->undefined_value()));
2178 // Pop value from top-of-stack slot; box result into result register.
2179 EmitCreateIteratorResult(true);
2180 } else {
2181 // Throw the provided value.
2182 __ push(eax);
2183 __ CallRuntime(Runtime::kThrow, 1);
2184 }
2185 __ jmp(&done);
2186
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002187 // Throw error if we attempt to operate on a running generator.
2188 __ bind(&wrong_state);
2189 __ push(ebx);
2190 __ CallRuntime(Runtime::kThrowGeneratorStateError, 1);
2191
2192 __ bind(&done);
2193 context()->Plug(result_register());
2194}
2195
2196
danno@chromium.org41728482013-06-12 22:31:22 +00002197void FullCodeGenerator::EmitCreateIteratorResult(bool done) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002198 Label gc_required;
2199 Label allocated;
2200
2201 Handle<Map> map(isolate()->native_context()->generator_result_map());
2202
2203 __ Allocate(map->instance_size(), eax, ecx, edx, &gc_required, TAG_OBJECT);
danno@chromium.org41728482013-06-12 22:31:22 +00002204 __ jmp(&allocated);
2205
2206 __ bind(&gc_required);
2207 __ Push(Smi::FromInt(map->instance_size()));
2208 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
2209 __ mov(context_register(),
2210 Operand(ebp, StandardFrameConstants::kContextOffset));
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002211
2212 __ bind(&allocated);
2213 __ mov(ebx, map);
2214 __ pop(ecx);
2215 __ mov(edx, isolate()->factory()->ToBoolean(done));
2216 ASSERT_EQ(map->instance_size(), 5 * kPointerSize);
2217 __ mov(FieldOperand(eax, HeapObject::kMapOffset), ebx);
2218 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
2219 isolate()->factory()->empty_fixed_array());
2220 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
2221 isolate()->factory()->empty_fixed_array());
2222 __ mov(FieldOperand(eax, JSGeneratorObject::kResultValuePropertyOffset), ecx);
2223 __ mov(FieldOperand(eax, JSGeneratorObject::kResultDonePropertyOffset), edx);
2224
2225 // Only the value field needs a write barrier, as the other values are in the
2226 // root set.
2227 __ RecordWriteField(eax, JSGeneratorObject::kResultValuePropertyOffset,
2228 ecx, edx, kDontSaveFPRegs);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002229}
2230
2231
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002232void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
2233 SetSourcePosition(prop->position());
2234 Literal* key = prop->key()->AsLiteral();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002235 ASSERT(!key->value()->IsSmi());
2236 __ mov(ecx, Immediate(key->value()));
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +00002237 CallLoadIC(NOT_CONTEXTUAL, prop->PropertyFeedbackId());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002238}
2239
2240
2241void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
2242 SetSourcePosition(prop->position());
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002243 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002244 CallIC(ic, prop->PropertyFeedbackId());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002245}
2246
2247
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002248void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002249 Token::Value op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002250 OverwriteMode mode,
2251 Expression* left,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002252 Expression* right) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002253 // Do combined smi check of the operands. Left operand is on the
2254 // stack. Right operand is in eax.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002255 Label smi_case, done, stub_call;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002256 __ pop(edx);
2257 __ mov(ecx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002258 __ or_(eax, edx);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002259 JumpPatchSite patch_site(masm_);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002260 patch_site.EmitJumpIfSmi(eax, &smi_case, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002261
2262 __ bind(&stub_call);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002263 __ mov(eax, ecx);
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +00002264 BinaryOpICStub stub(op, mode);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002265 CallIC(stub.GetCode(isolate()), expr->BinaryOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002266 patch_site.EmitPatchInfo();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002267 __ jmp(&done, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002268
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002269 // Smi case.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002270 __ bind(&smi_case);
2271 __ mov(eax, edx); // Copy left operand in case of a stub call.
2272
2273 switch (op) {
2274 case Token::SAR:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002275 __ SmiUntag(ecx);
2276 __ sar_cl(eax); // No checks of result necessary
machenbach@chromium.orgca2f2042014-03-10 10:03:12 +00002277 __ and_(eax, Immediate(~kSmiTagMask));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002278 break;
2279 case Token::SHL: {
2280 Label result_ok;
2281 __ SmiUntag(eax);
2282 __ SmiUntag(ecx);
2283 __ shl_cl(eax);
2284 // Check that the *signed* result fits in a smi.
2285 __ cmp(eax, 0xc0000000);
2286 __ j(positive, &result_ok);
2287 __ SmiTag(ecx);
2288 __ jmp(&stub_call);
2289 __ bind(&result_ok);
2290 __ SmiTag(eax);
2291 break;
2292 }
2293 case Token::SHR: {
2294 Label result_ok;
2295 __ SmiUntag(eax);
2296 __ SmiUntag(ecx);
2297 __ shr_cl(eax);
2298 __ test(eax, Immediate(0xc0000000));
2299 __ j(zero, &result_ok);
2300 __ SmiTag(ecx);
2301 __ jmp(&stub_call);
2302 __ bind(&result_ok);
2303 __ SmiTag(eax);
2304 break;
2305 }
2306 case Token::ADD:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002307 __ add(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002308 __ j(overflow, &stub_call);
2309 break;
2310 case Token::SUB:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002311 __ sub(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002312 __ j(overflow, &stub_call);
2313 break;
2314 case Token::MUL: {
2315 __ SmiUntag(eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002316 __ imul(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002317 __ j(overflow, &stub_call);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002318 __ test(eax, eax);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002319 __ j(not_zero, &done, Label::kNear);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002320 __ mov(ebx, edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002321 __ or_(ebx, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002322 __ j(negative, &stub_call);
2323 break;
2324 }
2325 case Token::BIT_OR:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002326 __ or_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002327 break;
2328 case Token::BIT_AND:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002329 __ and_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002330 break;
2331 case Token::BIT_XOR:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002332 __ xor_(eax, ecx);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002333 break;
2334 default:
2335 UNREACHABLE();
2336 }
2337
2338 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002339 context()->Plug(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002340}
2341
2342
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002343void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr,
2344 Token::Value op,
ricow@chromium.org65fae842010-08-25 15:26:24 +00002345 OverwriteMode mode) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002346 __ pop(edx);
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +00002347 BinaryOpICStub stub(op, mode);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002348 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002349 CallIC(stub.GetCode(isolate()), expr->BinaryOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002350 patch_site.EmitPatchInfo();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002351 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002352}
2353
2354
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00002355void FullCodeGenerator::EmitAssignment(Expression* expr) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002356 // Invalid left-hand sides are rewritten by the parser to have a 'throw
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002357 // ReferenceError' on the left-hand side.
2358 if (!expr->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002359 VisitForEffect(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002360 return;
2361 }
2362
2363 // Left-hand side can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00002364 // slot.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002365 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
2366 LhsKind assign_type = VARIABLE;
2367 Property* prop = expr->AsProperty();
2368 if (prop != NULL) {
2369 assign_type = (prop->key()->IsPropertyName())
2370 ? NAMED_PROPERTY
2371 : KEYED_PROPERTY;
2372 }
2373
2374 switch (assign_type) {
2375 case VARIABLE: {
2376 Variable* var = expr->AsVariableProxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002377 EffectContext context(this);
2378 EmitVariableAssignment(var, Token::ASSIGN);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002379 break;
2380 }
2381 case NAMED_PROPERTY: {
2382 __ push(eax); // Preserve value.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002383 VisitForAccumulatorValue(prop->obj());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002384 __ mov(edx, eax);
2385 __ pop(eax); // Restore value.
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002386 __ mov(ecx, prop->key()->AsLiteral()->value());
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002387 CallStoreIC();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002388 break;
2389 }
2390 case KEYED_PROPERTY: {
2391 __ push(eax); // Preserve value.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002392 VisitForStackValue(prop->obj());
2393 VisitForAccumulatorValue(prop->key());
2394 __ mov(ecx, eax);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002395 __ pop(edx); // Receiver.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002396 __ pop(eax); // Restore value.
dslomov@chromium.org486536d2014-03-12 13:09:18 +00002397 Handle<Code> ic = strict_mode() == SLOPPY
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002398 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2399 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002400 CallIC(ic);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002401 break;
2402 }
2403 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002404 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002405}
2406
2407
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002408void FullCodeGenerator::EmitStoreToStackLocalOrContextSlot(
2409 Variable* var, MemOperand location) {
2410 __ mov(location, eax);
2411 if (var->IsContextSlot()) {
2412 __ mov(edx, eax);
2413 int offset = Context::SlotOffset(var->index());
2414 __ RecordWriteContextSlot(ecx, offset, edx, ebx, kDontSaveFPRegs);
2415 }
2416}
2417
2418
2419void FullCodeGenerator::EmitCallStoreContextSlot(
dslomov@chromium.org486536d2014-03-12 13:09:18 +00002420 Handle<String> name, StrictMode strict_mode) {
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002421 __ push(eax); // Value.
2422 __ push(esi); // Context.
2423 __ push(Immediate(name));
dslomov@chromium.org486536d2014-03-12 13:09:18 +00002424 __ push(Immediate(Smi::FromInt(strict_mode)));
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002425 __ CallRuntime(Runtime::kStoreContextSlot, 4);
2426}
2427
2428
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002429void FullCodeGenerator::EmitVariableAssignment(Variable* var,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002430 Token::Value op) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002431 if (var->IsUnallocated()) {
2432 // Global var, const, or let.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002433 __ mov(ecx, var->name());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002434 __ mov(edx, GlobalObjectOperand());
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002435 CallStoreIC();
2436
dslomov@chromium.org486536d2014-03-12 13:09:18 +00002437 } else if (op == Token::INIT_CONST_LEGACY) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002438 // Const initializers need a write barrier.
2439 ASSERT(!var->IsParameter()); // No const parameters.
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002440 if (var->IsLookupSlot()) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002441 __ push(eax);
2442 __ push(esi);
2443 __ push(Immediate(var->name()));
2444 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002445 } else {
2446 ASSERT(var->IsStackLocal() || var->IsContextSlot());
2447 Label skip;
2448 MemOperand location = VarOperand(var, ecx);
2449 __ mov(edx, location);
2450 __ cmp(edx, isolate()->factory()->the_hole_value());
2451 __ j(not_equal, &skip, Label::kNear);
2452 EmitStoreToStackLocalOrContextSlot(var, location);
2453 __ bind(&skip);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002454 }
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002455
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002456 } else if (var->mode() == LET && op != Token::INIT_LET) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002457 // Non-initializing assignment to let variable needs a write barrier.
2458 if (var->IsLookupSlot()) {
dslomov@chromium.org486536d2014-03-12 13:09:18 +00002459 EmitCallStoreContextSlot(var->name(), strict_mode());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002460 } else {
2461 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
2462 Label assign;
2463 MemOperand location = VarOperand(var, ecx);
2464 __ mov(edx, location);
2465 __ cmp(edx, isolate()->factory()->the_hole_value());
2466 __ j(not_equal, &assign, Label::kNear);
2467 __ push(Immediate(var->name()));
2468 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2469 __ bind(&assign);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002470 EmitStoreToStackLocalOrContextSlot(var, location);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00002471 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002472
dslomov@chromium.org486536d2014-03-12 13:09:18 +00002473 } else if (!var->is_const_mode() || op == Token::INIT_CONST) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002474 // Assignment to var or initializing assignment to let/const
2475 // in harmony mode.
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002476 if (var->IsLookupSlot()) {
dslomov@chromium.org486536d2014-03-12 13:09:18 +00002477 EmitCallStoreContextSlot(var->name(), strict_mode());
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002478 } else {
2479 ASSERT(var->IsStackAllocated() || var->IsContextSlot());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002480 MemOperand location = VarOperand(var, ecx);
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002481 if (generate_debug_code_ && op == Token::INIT_LET) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002482 // Check for an uninitialized let binding.
2483 __ mov(edx, location);
2484 __ cmp(edx, isolate()->factory()->the_hole_value());
danno@chromium.org59400602013-08-13 17:09:37 +00002485 __ Check(equal, kLetBindingReInitialization);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002486 }
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002487 EmitStoreToStackLocalOrContextSlot(var, location);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002488 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002489 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002490 // Non-initializing assignments to consts are ignored.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002491}
2492
2493
2494void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2495 // Assignment to a property, using a named store IC.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002496 // eax : value
2497 // esp[0] : receiver
2498
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002499 Property* prop = expr->target()->AsProperty();
2500 ASSERT(prop != NULL);
2501 ASSERT(prop->key()->AsLiteral() != NULL);
2502
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002503 // Record source code position before IC call.
2504 SetSourcePosition(expr->position());
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002505 __ mov(ecx, prop->key()->AsLiteral()->value());
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002506 __ pop(edx);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002507 CallStoreIC(expr->AssignmentFeedbackId());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002508 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2509 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002510}
2511
2512
2513void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2514 // Assignment to a property, using a keyed store IC.
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002515 // eax : value
2516 // esp[0] : key
2517 // esp[kPointerSize] : receiver
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002518
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002519 __ pop(ecx); // Key.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00002520 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002521 // Record source code position before IC call.
2522 SetSourcePosition(expr->position());
dslomov@chromium.org486536d2014-03-12 13:09:18 +00002523 Handle<Code> ic = strict_mode() == SLOPPY
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002524 ? isolate()->builtins()->KeyedStoreIC_Initialize()
2525 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002526 CallIC(ic, expr->AssignmentFeedbackId());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002527
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002528 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002529 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002530}
2531
2532
2533void FullCodeGenerator::VisitProperty(Property* expr) {
2534 Comment cmnt(masm_, "[ Property");
2535 Expression* key = expr->key();
2536
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002537 if (key->IsPropertyName()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002538 VisitForAccumulatorValue(expr->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002539 __ mov(edx, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002540 EmitNamedPropertyLoad(expr);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002541 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002542 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002543 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002544 VisitForStackValue(expr->obj());
2545 VisitForAccumulatorValue(expr->key());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002546 __ pop(edx); // Object.
2547 __ mov(ecx, result_register()); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002548 EmitKeyedPropertyLoad(expr);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002549 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002550 }
2551}
2552
2553
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002554void FullCodeGenerator::CallIC(Handle<Code> code,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002555 TypeFeedbackId ast_id) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002556 ic_total_count_++;
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +00002557 __ call(code, RelocInfo::CODE_TARGET, ast_id);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002558}
2559
2560
2561
2562
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002563// Code common for calls using the IC.
2564void FullCodeGenerator::EmitCallWithIC(Call* expr) {
2565 Expression* callee = expr->expression();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002566 ZoneList<Expression*>* args = expr->arguments();
2567 int arg_count = args->length();
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002568
2569 CallFunctionFlags flags;
2570 // Get the target function.
2571 if (callee->IsVariableProxy()) {
2572 { StackValueContext context(this);
2573 EmitVariableLoad(callee->AsVariableProxy());
2574 PrepareForBailout(callee, NO_REGISTERS);
2575 }
2576 // Push undefined as receiver. This is patched in the method prologue if it
dslomov@chromium.org486536d2014-03-12 13:09:18 +00002577 // is a sloppy mode method.
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002578 __ push(Immediate(isolate()->factory()->undefined_value()));
2579 flags = NO_CALL_FUNCTION_FLAGS;
2580 } else {
2581 // Load the function from the receiver.
2582 ASSERT(callee->IsProperty());
2583 __ mov(edx, Operand(esp, 0));
2584 EmitNamedPropertyLoad(callee->AsProperty());
2585 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2586 // Push the target function under the receiver.
2587 __ push(Operand(esp, 0));
2588 __ mov(Operand(esp, kPointerSize), eax);
2589 flags = CALL_AS_METHOD;
2590 }
2591
2592 // Load the arguments.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002593 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002594 for (int i = 0; i < arg_count; i++) {
2595 VisitForStackValue(args->at(i));
2596 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002597 }
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002598
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002599 // Record source position of the IC call.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002600 SetSourcePosition(expr->position());
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002601 CallFunctionStub stub(arg_count, flags);
2602 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
2603 __ CallStub(&stub);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002604 RecordJSReturnSite(expr);
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002605
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002606 // Restore context register.
2607 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002608
2609 context()->DropAndPlug(1, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002610}
2611
2612
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002613// Code common for calls using the IC.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002614void FullCodeGenerator::EmitKeyedCallWithIC(Call* expr,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002615 Expression* key) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002616 // Load the key.
2617 VisitForAccumulatorValue(key);
2618
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002619 Expression* callee = expr->expression();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002620 ZoneList<Expression*>* args = expr->arguments();
2621 int arg_count = args->length();
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002622
2623 // Load the function from the receiver.
2624 ASSERT(callee->IsProperty());
2625 __ mov(edx, Operand(esp, 0));
2626 // Move the key into the right register for the keyed load IC.
2627 __ mov(ecx, eax);
2628 EmitKeyedPropertyLoad(callee->AsProperty());
2629 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2630
2631 // Push the target function under the receiver.
2632 __ push(Operand(esp, 0));
2633 __ mov(Operand(esp, kPointerSize), eax);
2634
2635 // Load the arguments.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002636 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002637 for (int i = 0; i < arg_count; i++) {
2638 VisitForStackValue(args->at(i));
2639 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002640 }
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002641
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002642 // Record source position of the IC call.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002643 SetSourcePosition(expr->position());
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002644 CallFunctionStub stub(arg_count, CALL_AS_METHOD);
2645 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
2646 __ CallStub(&stub);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002647 RecordJSReturnSite(expr);
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002648
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002649 // Restore context register.
2650 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002651
2652 context()->DropAndPlug(1, eax);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002653}
2654
2655
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002656void FullCodeGenerator::EmitCallWithStub(Call* expr) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002657 // Code common for calls using the call stub.
2658 ZoneList<Expression*>* args = expr->arguments();
2659 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002660 { PreservePositionScope scope(masm()->positions_recorder());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002661 for (int i = 0; i < arg_count; i++) {
2662 VisitForStackValue(args->at(i));
2663 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002664 }
2665 // Record source position for debugger.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002666 SetSourcePosition(expr->position());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002667
machenbach@chromium.org8545d492014-03-17 09:28:03 +00002668 Handle<Object> uninitialized =
2669 TypeFeedbackInfo::UninitializedSentinel(isolate());
2670 StoreFeedbackVectorSlot(expr->CallFeedbackSlot(), uninitialized);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002671 __ LoadHeapObject(ebx, FeedbackVector());
2672 __ mov(edx, Immediate(Smi::FromInt(expr->CallFeedbackSlot())));
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002673
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002674 // Record call targets in unoptimized code.
2675 CallFunctionStub stub(arg_count, RECORD_CALL_TARGET);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002676 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002677 __ CallStub(&stub);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002678
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002679 RecordJSReturnSite(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002680 // Restore context register.
2681 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002682 context()->DropAndPlug(1, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002683}
2684
2685
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002686void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002687 // Push copy of the first argument or undefined if it doesn't exist.
2688 if (arg_count > 0) {
2689 __ push(Operand(esp, arg_count * kPointerSize));
2690 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002691 __ push(Immediate(isolate()->factory()->undefined_value()));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002692 }
2693
2694 // Push the receiver of the enclosing function.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002695 __ push(Operand(ebp, (2 + info_->scope()->num_parameters()) * kPointerSize));
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002696 // Push the language mode.
dslomov@chromium.org486536d2014-03-12 13:09:18 +00002697 __ push(Immediate(Smi::FromInt(strict_mode())));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002698
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002699 // Push the start position of the scope the calls resides in.
2700 __ push(Immediate(Smi::FromInt(scope()->start_position())));
2701
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002702 // Do the runtime call.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002703 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002704}
2705
2706
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002707void FullCodeGenerator::VisitCall(Call* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002708#ifdef DEBUG
2709 // We want to verify that RecordJSReturnSite gets called on all paths
2710 // through this function. Avoid early returns.
2711 expr->return_is_recorded_ = false;
2712#endif
2713
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002714 Comment cmnt(masm_, "[ Call");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002715 Expression* callee = expr->expression();
machenbach@chromium.org43c51e52014-01-20 07:57:28 +00002716 Call::CallType call_type = expr->GetCallType(isolate());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002717
machenbach@chromium.org43c51e52014-01-20 07:57:28 +00002718 if (call_type == Call::POSSIBLY_EVAL_CALL) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002719 // In a call to eval, we first call %ResolvePossiblyDirectEval to
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002720 // resolve the function we need to call and the receiver of the call.
2721 // Then we call the resolved function using the given arguments.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002722 ZoneList<Expression*>* args = expr->arguments();
2723 int arg_count = args->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002724 { PreservePositionScope pos_scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002725 VisitForStackValue(callee);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002726 // Reserved receiver slot.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002727 __ push(Immediate(isolate()->factory()->undefined_value()));
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002728 // Push the arguments.
2729 for (int i = 0; i < arg_count; i++) {
2730 VisitForStackValue(args->at(i));
2731 }
2732
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002733 // Push a copy of the function (found below the arguments) and
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002734 // resolve eval.
2735 __ push(Operand(esp, (arg_count + 1) * kPointerSize));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002736 EmitResolvePossiblyDirectEval(arg_count);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002737
2738 // The runtime call returns a pair of values in eax (function) and
2739 // edx (receiver). Touch up the stack with the right values.
2740 __ mov(Operand(esp, (arg_count + 0) * kPointerSize), edx);
2741 __ mov(Operand(esp, (arg_count + 1) * kPointerSize), eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002742 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002743 // Record source position for debugger.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002744 SetSourcePosition(expr->position());
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002745 CallFunctionStub stub(arg_count, NO_CALL_FUNCTION_FLAGS);
danno@chromium.orgc612e022011-11-10 11:38:15 +00002746 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002747 __ CallStub(&stub);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002748 RecordJSReturnSite(expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002749 // Restore context register.
2750 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002751 context()->DropAndPlug(1, eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002752
machenbach@chromium.org43c51e52014-01-20 07:57:28 +00002753 } else if (call_type == Call::GLOBAL_CALL) {
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002754 EmitCallWithIC(expr);
2755
machenbach@chromium.org43c51e52014-01-20 07:57:28 +00002756 } else if (call_type == Call::LOOKUP_SLOT_CALL) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002757 // Call to a lookup slot (dynamically introduced variable).
machenbach@chromium.org43c51e52014-01-20 07:57:28 +00002758 VariableProxy* proxy = callee->AsVariableProxy();
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002759 Label slow, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002760 { PreservePositionScope scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002761 // Generate code for loading from variables potentially shadowed by
2762 // eval-introduced variables.
2763 EmitDynamicLookupFastCase(proxy->var(), NOT_INSIDE_TYPEOF, &slow, &done);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002764 }
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002765 __ bind(&slow);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002766 // Call the runtime to find the function to call (returned in eax) and
2767 // the object holding it (returned in edx).
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002768 __ push(context_register());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002769 __ push(Immediate(proxy->name()));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002770 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2771 __ push(eax); // Function.
2772 __ push(edx); // Receiver.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002773
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002774 // If fast case code has been generated, emit code to push the function
2775 // and receiver and have the slow path jump around this code.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002776 if (done.is_linked()) {
2777 Label call;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002778 __ jmp(&call, Label::kNear);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002779 __ bind(&done);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002780 // Push function.
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002781 __ push(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002782 // The receiver is implicitly the global receiver. Indicate this by
2783 // passing the hole to the call function stub.
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002784 __ push(Immediate(isolate()->factory()->undefined_value()));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002785 __ bind(&call);
2786 }
2787
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002788 // The receiver is either the global receiver or an object found by
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002789 // LoadContextSlot.
2790 EmitCallWithStub(expr);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002791
machenbach@chromium.org43c51e52014-01-20 07:57:28 +00002792 } else if (call_type == Call::PROPERTY_CALL) {
2793 Property* property = callee->AsProperty();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002794 { PreservePositionScope scope(masm()->positions_recorder());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002795 VisitForStackValue(property->obj());
2796 }
2797 if (property->key()->IsPropertyName()) {
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00002798 EmitCallWithIC(expr);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002799 } else {
2800 EmitKeyedCallWithIC(expr, property->key());
2801 }
2802
2803 } else {
machenbach@chromium.org43c51e52014-01-20 07:57:28 +00002804 ASSERT(call_type == Call::OTHER_CALL);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002805 // Call to an arbitrary expression not handled specially above.
2806 { PreservePositionScope scope(masm()->positions_recorder());
2807 VisitForStackValue(callee);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002808 }
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002809 __ push(Immediate(isolate()->factory()->undefined_value()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002810 // Emit function call.
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002811 EmitCallWithStub(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002812 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002813
2814#ifdef DEBUG
2815 // RecordJSReturnSite should have been called.
2816 ASSERT(expr->return_is_recorded_);
2817#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002818}
2819
2820
2821void FullCodeGenerator::VisitCallNew(CallNew* expr) {
2822 Comment cmnt(masm_, "[ CallNew");
2823 // According to ECMA-262, section 11.2.2, page 44, the function
2824 // expression in new calls must be evaluated before the
2825 // arguments.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002826
ricow@chromium.org65fae842010-08-25 15:26:24 +00002827 // Push constructor on the stack. If it's not a function it's used as
2828 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2829 // ignored.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002830 VisitForStackValue(expr->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002831
2832 // Push the arguments ("left-to-right") on the stack.
2833 ZoneList<Expression*>* args = expr->arguments();
2834 int arg_count = args->length();
2835 for (int i = 0; i < arg_count; i++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002836 VisitForStackValue(args->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002837 }
2838
2839 // Call the construct call builtin that handles allocation and
2840 // constructor invocation.
2841 SetSourcePosition(expr->position());
2842
ricow@chromium.org65fae842010-08-25 15:26:24 +00002843 // Load function and argument count into edi and eax.
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002844 __ Set(eax, Immediate(arg_count));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002845 __ mov(edi, Operand(esp, arg_count * kPointerSize));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002846
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002847 // Record call targets in unoptimized code.
machenbach@chromium.org8545d492014-03-17 09:28:03 +00002848 Handle<Object> uninitialized =
2849 TypeFeedbackInfo::UninitializedSentinel(isolate());
2850 StoreFeedbackVectorSlot(expr->CallNewFeedbackSlot(), uninitialized);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00002851 __ LoadHeapObject(ebx, FeedbackVector());
2852 __ mov(edx, Immediate(Smi::FromInt(expr->CallNewFeedbackSlot())));
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002853
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002854 CallConstructStub stub(RECORD_CALL_TARGET);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002855 __ call(stub.GetCode(isolate()), RelocInfo::CONSTRUCT_CALL);
ulan@chromium.org967e2702012-02-28 09:49:15 +00002856 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002857 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002858}
2859
2860
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002861void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2862 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002863 ASSERT(args->length() == 1);
2864
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002865 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002866
2867 Label materialize_true, materialize_false;
2868 Label* if_true = NULL;
2869 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002870 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002871 context()->PrepareTest(&materialize_true, &materialize_false,
2872 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002873
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002874 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002875 __ test(eax, Immediate(kSmiTagMask));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002876 Split(zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002877
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002878 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002879}
2880
2881
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002882void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
2883 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002884 ASSERT(args->length() == 1);
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 Label materialize_true, materialize_false;
2889 Label* if_true = NULL;
2890 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002891 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002892 context()->PrepareTest(&materialize_true, &materialize_false,
2893 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002894
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002895 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002896 __ test(eax, Immediate(kSmiTagMask | 0x80000000));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002897 Split(zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002898
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002899 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002900}
2901
2902
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002903void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
2904 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002905 ASSERT(args->length() == 1);
2906
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002907 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002908
2909 Label materialize_true, materialize_false;
2910 Label* if_true = NULL;
2911 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002912 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002913 context()->PrepareTest(&materialize_true, &materialize_false,
2914 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002915
whesse@chromium.org7b260152011-06-20 15:33:18 +00002916 __ JumpIfSmi(eax, if_false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002917 __ cmp(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002918 __ j(equal, if_true);
2919 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2920 // Undetectable objects behave like undefined when tested with typeof.
2921 __ movzx_b(ecx, FieldOperand(ebx, Map::kBitFieldOffset));
2922 __ test(ecx, Immediate(1 << Map::kIsUndetectable));
2923 __ j(not_zero, if_false);
2924 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceTypeOffset));
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002925 __ cmp(ecx, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002926 __ j(below, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002927 __ cmp(ecx, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002928 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002929 Split(below_equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002930
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002931 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002932}
2933
2934
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002935void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
2936 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002937 ASSERT(args->length() == 1);
2938
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002939 VisitForAccumulatorValue(args->at(0));
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002940
2941 Label materialize_true, materialize_false;
2942 Label* if_true = NULL;
2943 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002944 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002945 context()->PrepareTest(&materialize_true, &materialize_false,
2946 &if_true, &if_false, &fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002947
whesse@chromium.org7b260152011-06-20 15:33:18 +00002948 __ JumpIfSmi(eax, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002949 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002950 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002951 Split(above_equal, if_true, if_false, fall_through);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002952
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002953 context()->Plug(if_true, if_false);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002954}
2955
2956
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002957void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
2958 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002959 ASSERT(args->length() == 1);
2960
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002961 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002962
2963 Label materialize_true, materialize_false;
2964 Label* if_true = NULL;
2965 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002966 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002967 context()->PrepareTest(&materialize_true, &materialize_false,
2968 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002969
whesse@chromium.org7b260152011-06-20 15:33:18 +00002970 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002971 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2972 __ movzx_b(ebx, FieldOperand(ebx, Map::kBitFieldOffset));
2973 __ test(ebx, Immediate(1 << Map::kIsUndetectable));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002974 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002975 Split(not_zero, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002976
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002977 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002978}
2979
2980
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002981void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002982 CallRuntime* expr) {
2983 ZoneList<Expression*>* args = expr->arguments();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002984 ASSERT(args->length() == 1);
2985
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002986 VisitForAccumulatorValue(args->at(0));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002987
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00002988 Label materialize_true, materialize_false, skip_lookup;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002989 Label* if_true = NULL;
2990 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002991 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002992 context()->PrepareTest(&materialize_true, &materialize_false,
2993 &if_true, &if_false, &fall_through);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002994
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002995 __ AssertNotSmi(eax);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002996
2997 // Check whether this map has already been checked to be safe for default
2998 // valueOf.
2999 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3000 __ test_b(FieldOperand(ebx, Map::kBitField2Offset),
3001 1 << Map::kStringWrapperSafeForDefaultValueOf);
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00003002 __ j(not_zero, &skip_lookup);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003003
3004 // Check for fast case object. Return false for slow case objects.
3005 __ mov(ecx, FieldOperand(eax, JSObject::kPropertiesOffset));
3006 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00003007 __ cmp(ecx, isolate()->factory()->hash_table_map());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003008 __ j(equal, if_false);
3009
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00003010 // Look for valueOf string in the descriptor array, and indicate false if
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00003011 // found. Since we omit an enumeration index check, if it is added via a
3012 // transition that shares its descriptor array, this is a false positive.
3013 Label entry, loop, done;
3014
3015 // Skip loop if no descriptors are valid.
3016 __ NumberOfOwnDescriptors(ecx, ebx);
3017 __ cmp(ecx, 0);
3018 __ j(equal, &done);
3019
danno@chromium.org40cb8782011-05-25 07:58:50 +00003020 __ LoadInstanceDescriptors(ebx, ebx);
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00003021 // ebx: descriptor array.
3022 // ecx: valid entries in the descriptor array.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003023 // Calculate the end of the descriptor array.
3024 STATIC_ASSERT(kSmiTag == 0);
3025 STATIC_ASSERT(kSmiTagSize == 1);
3026 STATIC_ASSERT(kPointerSize == 4);
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00003027 __ imul(ecx, ecx, DescriptorArray::kDescriptorSize);
3028 __ lea(ecx, Operand(ebx, ecx, times_2, DescriptorArray::kFirstOffset));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003029 // Calculate location of the first key name.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003030 __ add(ebx, Immediate(DescriptorArray::kFirstOffset));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003031 // Loop through all the keys in the descriptor array. If one of these is the
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00003032 // internalized string "valueOf" the result is false.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003033 __ jmp(&entry);
3034 __ bind(&loop);
3035 __ mov(edx, FieldOperand(ebx, 0));
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00003036 __ cmp(edx, isolate()->factory()->value_of_string());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003037 __ j(equal, if_false);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003038 __ add(ebx, Immediate(DescriptorArray::kDescriptorSize * kPointerSize));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003039 __ bind(&entry);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003040 __ cmp(ebx, ecx);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003041 __ j(not_equal, &loop);
3042
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00003043 __ bind(&done);
3044
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003045 // Reload map as register ebx was used as temporary above.
3046 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3047
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00003048 // Set the bit in the map to indicate that there is no local valueOf field.
3049 __ or_(FieldOperand(ebx, Map::kBitField2Offset),
3050 Immediate(1 << Map::kStringWrapperSafeForDefaultValueOf));
3051
3052 __ bind(&skip_lookup);
3053
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00003054 // If a valueOf property is not found on the object check that its
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003055 // prototype is the un-modified String prototype. If not result is false.
3056 __ mov(ecx, FieldOperand(ebx, Map::kPrototypeOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003057 __ JumpIfSmi(ecx, if_false);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003058 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003059 __ mov(edx, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003060 __ mov(edx,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003061 FieldOperand(edx, GlobalObject::kNativeContextOffset));
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003062 __ cmp(ecx,
3063 ContextOperand(edx,
3064 Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003065 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00003066 Split(equal, if_true, if_false, fall_through);
3067
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003068 context()->Plug(if_true, if_false);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003069}
3070
3071
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003072void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
3073 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003074 ASSERT(args->length() == 1);
3075
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003076 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003077
3078 Label materialize_true, materialize_false;
3079 Label* if_true = NULL;
3080 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003081 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003082 context()->PrepareTest(&materialize_true, &materialize_false,
3083 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003084
whesse@chromium.org7b260152011-06-20 15:33:18 +00003085 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003086 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003087 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003088 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003089
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003090 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003091}
3092
3093
machenbach@chromium.org0cc09502013-11-13 12:20:55 +00003094void FullCodeGenerator::EmitIsMinusZero(CallRuntime* expr) {
3095 ZoneList<Expression*>* args = expr->arguments();
3096 ASSERT(args->length() == 1);
3097
3098 VisitForAccumulatorValue(args->at(0));
3099
3100 Label materialize_true, materialize_false;
3101 Label* if_true = NULL;
3102 Label* if_false = NULL;
3103 Label* fall_through = NULL;
3104 context()->PrepareTest(&materialize_true, &materialize_false,
3105 &if_true, &if_false, &fall_through);
3106
3107 Handle<Map> map = masm()->isolate()->factory()->heap_number_map();
3108 __ CheckMap(eax, map, if_false, DO_SMI_CHECK);
3109 __ cmp(FieldOperand(eax, HeapNumber::kExponentOffset), Immediate(0x80000000));
3110 __ j(not_equal, if_false);
3111 __ cmp(FieldOperand(eax, HeapNumber::kMantissaOffset), Immediate(0x00000000));
3112 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3113 Split(equal, if_true, if_false, fall_through);
3114
3115 context()->Plug(if_true, if_false);
3116}
3117
3118
3119
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003120void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
3121 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003122 ASSERT(args->length() == 1);
3123
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003124 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003125
3126 Label materialize_true, materialize_false;
3127 Label* if_true = NULL;
3128 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003129 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003130 context()->PrepareTest(&materialize_true, &materialize_false,
3131 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003132
whesse@chromium.org7b260152011-06-20 15:33:18 +00003133 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003134 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003135 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003136 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003137
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003138 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003139}
3140
3141
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003142void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
3143 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003144 ASSERT(args->length() == 1);
3145
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003146 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003147
3148 Label materialize_true, materialize_false;
3149 Label* if_true = NULL;
3150 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003151 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003152 context()->PrepareTest(&materialize_true, &materialize_false,
3153 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003154
whesse@chromium.org7b260152011-06-20 15:33:18 +00003155 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003156 __ CmpObjectType(eax, JS_REGEXP_TYPE, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003157 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003158 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003159
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003160 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003161}
3162
3163
3164
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003165void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
3166 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003167
3168 Label materialize_true, materialize_false;
3169 Label* if_true = NULL;
3170 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003171 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003172 context()->PrepareTest(&materialize_true, &materialize_false,
3173 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003174
3175 // Get the frame pointer for the calling frame.
3176 __ mov(eax, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3177
3178 // Skip the arguments adaptor frame if it exists.
3179 Label check_frame_marker;
3180 __ cmp(Operand(eax, StandardFrameConstants::kContextOffset),
3181 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3182 __ j(not_equal, &check_frame_marker);
3183 __ mov(eax, Operand(eax, StandardFrameConstants::kCallerFPOffset));
3184
3185 // Check the marker in the calling frame.
3186 __ bind(&check_frame_marker);
3187 __ cmp(Operand(eax, StandardFrameConstants::kMarkerOffset),
3188 Immediate(Smi::FromInt(StackFrame::CONSTRUCT)));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003189 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003190 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003191
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003192 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003193}
3194
3195
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003196void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
3197 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003198 ASSERT(args->length() == 2);
3199
3200 // Load the two objects into registers and perform the comparison.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003201 VisitForStackValue(args->at(0));
3202 VisitForAccumulatorValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003203
3204 Label materialize_true, materialize_false;
3205 Label* if_true = NULL;
3206 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003207 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003208 context()->PrepareTest(&materialize_true, &materialize_false,
3209 &if_true, &if_false, &fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003210
3211 __ pop(ebx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003212 __ cmp(eax, ebx);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003213 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003214 Split(equal, if_true, if_false, fall_through);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003215
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003216 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003217}
3218
3219
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003220void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
3221 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003222 ASSERT(args->length() == 1);
3223
3224 // ArgumentsAccessStub expects the key in edx and the formal
3225 // parameter count in eax.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003226 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003227 __ mov(edx, eax);
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00003228 __ Set(eax, Immediate(Smi::FromInt(info_->scope()->num_parameters())));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003229 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
3230 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003231 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003232}
3233
3234
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003235void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
3236 ASSERT(expr->arguments()->length() == 0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003237
3238 Label exit;
3239 // Get the number of formal parameters.
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00003240 __ Set(eax, Immediate(Smi::FromInt(info_->scope()->num_parameters())));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003241
3242 // Check if the calling frame is an arguments adaptor frame.
3243 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3244 __ cmp(Operand(ebx, StandardFrameConstants::kContextOffset),
3245 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3246 __ j(not_equal, &exit);
3247
3248 // Arguments adaptor case: Read the arguments length from the
3249 // adaptor frame.
3250 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3251
3252 __ bind(&exit);
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003253 __ AssertSmi(eax);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003254 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003255}
3256
3257
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003258void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
3259 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003260 ASSERT(args->length() == 1);
3261 Label done, null, function, non_function_constructor;
3262
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003263 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003264
3265 // If the object is a smi, we return null.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003266 __ JumpIfSmi(eax, &null);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003267
3268 // Check that the object is a JS object but take special care of JS
3269 // functions to make sure they have 'Function' as their class.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003270 // Assume that there are only two callable types, and one of them is at
3271 // either end of the type range for JS object types. Saves extra comparisons.
3272 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003273 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, eax);
3274 // Map is now in eax.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003275 __ j(below, &null);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003276 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3277 FIRST_SPEC_OBJECT_TYPE + 1);
3278 __ j(equal, &function);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003279
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003280 __ CmpInstanceType(eax, LAST_SPEC_OBJECT_TYPE);
3281 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3282 LAST_SPEC_OBJECT_TYPE - 1);
3283 __ j(equal, &function);
3284 // Assume that there is no larger type.
3285 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003286
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003287 // Check if the constructor in the map is a JS function.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003288 __ mov(eax, FieldOperand(eax, Map::kConstructorOffset));
3289 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
3290 __ j(not_equal, &non_function_constructor);
3291
3292 // eax now contains the constructor function. Grab the
3293 // instance class name from there.
3294 __ mov(eax, FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset));
3295 __ mov(eax, FieldOperand(eax, SharedFunctionInfo::kInstanceClassNameOffset));
3296 __ jmp(&done);
3297
3298 // Functions have class 'Function'.
3299 __ bind(&function);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00003300 __ mov(eax, isolate()->factory()->function_class_string());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003301 __ jmp(&done);
3302
3303 // Objects with a non-function constructor have class 'Object'.
3304 __ bind(&non_function_constructor);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00003305 __ mov(eax, isolate()->factory()->Object_string());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003306 __ jmp(&done);
3307
3308 // Non-JS objects have class null.
3309 __ bind(&null);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003310 __ mov(eax, isolate()->factory()->null_value());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003311
3312 // All done.
3313 __ bind(&done);
3314
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003315 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003316}
3317
3318
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003319void FullCodeGenerator::EmitLog(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003320 // Conditionally generate a log call.
3321 // Args:
3322 // 0 (literal string): The type of logging (corresponds to the flags).
3323 // This is used to determine whether or not to generate the log call.
3324 // 1 (string): Format string. Access the string at argument index 2
3325 // with '%2s' (see Logger::LogRuntime for all the formats).
3326 // 2 (array): Arguments to the format string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003327 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003328 ASSERT_EQ(args->length(), 3);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003329 if (CodeGenerator::ShouldGenerateLog(isolate(), args->at(0))) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003330 VisitForStackValue(args->at(1));
3331 VisitForStackValue(args->at(2));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003332 __ CallRuntime(Runtime::kLog, 2);
3333 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003334 // Finally, we're expected to leave a value on the top of the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003335 __ mov(eax, isolate()->factory()->undefined_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003336 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003337}
3338
3339
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003340void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003341 // Load the arguments on the stack and call the stub.
3342 SubStringStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003343 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003344 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003345 VisitForStackValue(args->at(0));
3346 VisitForStackValue(args->at(1));
3347 VisitForStackValue(args->at(2));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003348 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003349 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003350}
3351
3352
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003353void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003354 // Load the arguments on the stack and call the stub.
3355 RegExpExecStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003356 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003357 ASSERT(args->length() == 4);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003358 VisitForStackValue(args->at(0));
3359 VisitForStackValue(args->at(1));
3360 VisitForStackValue(args->at(2));
3361 VisitForStackValue(args->at(3));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003362 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003363 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003364}
3365
3366
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003367void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3368 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003369 ASSERT(args->length() == 1);
3370
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003371 VisitForAccumulatorValue(args->at(0)); // Load the object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003372
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003373 Label done;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003374 // If the object is a smi return the object.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003375 __ JumpIfSmi(eax, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003376 // If the object is not a value type, return the object.
3377 __ CmpObjectType(eax, JS_VALUE_TYPE, ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003378 __ j(not_equal, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003379 __ mov(eax, FieldOperand(eax, JSValue::kValueOffset));
3380
3381 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003382 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003383}
3384
3385
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003386void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3387 ZoneList<Expression*>* args = expr->arguments();
3388 ASSERT(args->length() == 2);
3389 ASSERT_NE(NULL, args->at(1)->AsLiteral());
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00003390 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->value()));
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003391
3392 VisitForAccumulatorValue(args->at(0)); // Load the object.
3393
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003394 Label runtime, done, not_date_object;
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003395 Register object = eax;
3396 Register result = eax;
3397 Register scratch = ecx;
3398
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003399 __ JumpIfSmi(object, &not_date_object);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003400 __ CmpObjectType(object, JS_DATE_TYPE, scratch);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003401 __ j(not_equal, &not_date_object);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003402
3403 if (index->value() == 0) {
3404 __ mov(result, FieldOperand(object, JSDate::kValueOffset));
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003405 __ jmp(&done);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003406 } else {
3407 if (index->value() < JSDate::kFirstUncachedField) {
3408 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3409 __ mov(scratch, Operand::StaticVariable(stamp));
3410 __ cmp(scratch, FieldOperand(object, JSDate::kCacheStampOffset));
3411 __ j(not_equal, &runtime, Label::kNear);
3412 __ mov(result, FieldOperand(object, JSDate::kValueOffset +
3413 kPointerSize * index->value()));
3414 __ jmp(&done);
3415 }
3416 __ bind(&runtime);
3417 __ PrepareCallCFunction(2, scratch);
3418 __ mov(Operand(esp, 0), object);
3419 __ mov(Operand(esp, 1 * kPointerSize), Immediate(index));
3420 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003421 __ jmp(&done);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003422 }
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00003423
3424 __ bind(&not_date_object);
3425 __ CallRuntime(Runtime::kThrowNotDateError, 0);
3426 __ bind(&done);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003427 context()->Plug(result);
3428}
3429
3430
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003431void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3432 ZoneList<Expression*>* args = expr->arguments();
3433 ASSERT_EQ(3, args->length());
3434
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003435 Register string = eax;
3436 Register index = ebx;
3437 Register value = ecx;
3438
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003439 VisitForStackValue(args->at(1)); // index
3440 VisitForStackValue(args->at(2)); // value
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003441 VisitForAccumulatorValue(args->at(0)); // string
3442
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00003443 __ pop(value);
3444 __ pop(index);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003445
3446 if (FLAG_debug_code) {
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00003447 __ test(value, Immediate(kSmiTagMask));
machenbach@chromium.org05150ab2014-01-29 08:13:29 +00003448 __ Check(zero, kNonSmiValue);
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00003449 __ test(index, Immediate(kSmiTagMask));
machenbach@chromium.org05150ab2014-01-29 08:13:29 +00003450 __ Check(zero, kNonSmiValue);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003451 }
3452
3453 __ SmiUntag(value);
3454 __ SmiUntag(index);
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00003455
3456 if (FLAG_debug_code) {
3457 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
3458 __ EmitSeqStringSetCharCheck(string, index, value, one_byte_seq_type);
3459 }
3460
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003461 __ mov_b(FieldOperand(string, index, times_1, SeqOneByteString::kHeaderSize),
3462 value);
3463 context()->Plug(string);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003464}
3465
3466
3467void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3468 ZoneList<Expression*>* args = expr->arguments();
3469 ASSERT_EQ(3, args->length());
3470
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003471 Register string = eax;
3472 Register index = ebx;
3473 Register value = ecx;
3474
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003475 VisitForStackValue(args->at(1)); // index
3476 VisitForStackValue(args->at(2)); // value
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00003477 VisitForAccumulatorValue(args->at(0)); // string
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003478 __ pop(value);
3479 __ pop(index);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003480
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003481 if (FLAG_debug_code) {
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00003482 __ test(value, Immediate(kSmiTagMask));
machenbach@chromium.org05150ab2014-01-29 08:13:29 +00003483 __ Check(zero, kNonSmiValue);
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00003484 __ test(index, Immediate(kSmiTagMask));
machenbach@chromium.org05150ab2014-01-29 08:13:29 +00003485 __ Check(zero, kNonSmiValue);
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00003486 __ SmiUntag(index);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003487 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00003488 __ EmitSeqStringSetCharCheck(string, index, value, two_byte_seq_type);
3489 __ SmiTag(index);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00003490 }
3491
3492 __ SmiUntag(value);
3493 // No need to untag a smi for two-byte addressing.
3494 __ mov_w(FieldOperand(string, index, times_1, SeqTwoByteString::kHeaderSize),
3495 value);
3496 context()->Plug(string);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003497}
3498
3499
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003500void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003501 // Load the arguments on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003502 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003503 ASSERT(args->length() == 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003504 VisitForStackValue(args->at(0));
3505 VisitForStackValue(args->at(1));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003506
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003507 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003508 MathPowStub stub(MathPowStub::ON_STACK);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003509 __ CallStub(&stub);
3510 } else {
3511 __ CallRuntime(Runtime::kMath_pow, 2);
3512 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003513 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003514}
3515
3516
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003517void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3518 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003519 ASSERT(args->length() == 2);
3520
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003521 VisitForStackValue(args->at(0)); // Load the object.
3522 VisitForAccumulatorValue(args->at(1)); // Load the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003523 __ pop(ebx); // eax = value. ebx = object.
3524
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003525 Label done;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003526 // If the object is a smi, return the value.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003527 __ JumpIfSmi(ebx, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003528
3529 // If the object is not a value type, return the value.
3530 __ CmpObjectType(ebx, JS_VALUE_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003531 __ j(not_equal, &done, Label::kNear);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003532
3533 // Store the value.
3534 __ mov(FieldOperand(ebx, JSValue::kValueOffset), eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003535
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003536 // Update the write barrier. Save the value as it will be
3537 // overwritten by the write barrier code and is needed afterward.
3538 __ mov(edx, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003539 __ RecordWriteField(ebx, JSValue::kValueOffset, edx, ecx, kDontSaveFPRegs);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003540
3541 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003542 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003543}
3544
3545
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003546void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3547 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003548 ASSERT_EQ(args->length(), 1);
3549
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +00003550 // Load the argument into eax and call the stub.
3551 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003552
3553 NumberToStringStub stub;
3554 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003555 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003556}
3557
3558
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003559void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3560 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003561 ASSERT(args->length() == 1);
3562
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003563 VisitForAccumulatorValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003564
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003565 Label done;
3566 StringCharFromCodeGenerator generator(eax, ebx);
3567 generator.GenerateFast(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003568 __ jmp(&done);
3569
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003570 NopRuntimeCallHelper call_helper;
3571 generator.GenerateSlow(masm_, call_helper);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003572
3573 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003574 context()->Plug(ebx);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003575}
3576
3577
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003578void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3579 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003580 ASSERT(args->length() == 2);
3581
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003582 VisitForStackValue(args->at(0));
3583 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003584
3585 Register object = ebx;
3586 Register index = eax;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003587 Register result = edx;
3588
3589 __ pop(object);
3590
3591 Label need_conversion;
3592 Label index_out_of_range;
3593 Label done;
3594 StringCharCodeAtGenerator generator(object,
3595 index,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003596 result,
3597 &need_conversion,
3598 &need_conversion,
3599 &index_out_of_range,
3600 STRING_INDEX_IS_NUMBER);
3601 generator.GenerateFast(masm_);
3602 __ jmp(&done);
3603
3604 __ bind(&index_out_of_range);
3605 // When the index is out of range, the spec requires us to return
3606 // NaN.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003607 __ Set(result, Immediate(isolate()->factory()->nan_value()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003608 __ jmp(&done);
3609
3610 __ bind(&need_conversion);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003611 // Move the undefined value into the result register, which will
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003612 // trigger conversion.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003613 __ Set(result, Immediate(isolate()->factory()->undefined_value()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003614 __ jmp(&done);
3615
3616 NopRuntimeCallHelper call_helper;
3617 generator.GenerateSlow(masm_, call_helper);
3618
3619 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003620 context()->Plug(result);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003621}
3622
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003623
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003624void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3625 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003626 ASSERT(args->length() == 2);
3627
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003628 VisitForStackValue(args->at(0));
3629 VisitForAccumulatorValue(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003630
3631 Register object = ebx;
3632 Register index = eax;
danno@chromium.orgc612e022011-11-10 11:38:15 +00003633 Register scratch = edx;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003634 Register result = eax;
3635
3636 __ pop(object);
3637
3638 Label need_conversion;
3639 Label index_out_of_range;
3640 Label done;
3641 StringCharAtGenerator generator(object,
3642 index,
danno@chromium.orgc612e022011-11-10 11:38:15 +00003643 scratch,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003644 result,
3645 &need_conversion,
3646 &need_conversion,
3647 &index_out_of_range,
3648 STRING_INDEX_IS_NUMBER);
3649 generator.GenerateFast(masm_);
3650 __ jmp(&done);
3651
3652 __ bind(&index_out_of_range);
3653 // When the index is out of range, the spec requires us to return
3654 // the empty string.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003655 __ Set(result, Immediate(isolate()->factory()->empty_string()));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003656 __ jmp(&done);
3657
3658 __ bind(&need_conversion);
3659 // Move smi zero into the result register, which will trigger
3660 // conversion.
3661 __ Set(result, Immediate(Smi::FromInt(0)));
3662 __ jmp(&done);
3663
3664 NopRuntimeCallHelper call_helper;
3665 generator.GenerateSlow(masm_, call_helper);
3666
3667 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003668 context()->Plug(result);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003669}
3670
3671
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003672void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3673 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003674 ASSERT_EQ(2, args->length());
machenbach@chromium.orgbbbda922014-01-23 09:38:20 +00003675 VisitForStackValue(args->at(0));
3676 VisitForAccumulatorValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003677
machenbach@chromium.orgbbbda922014-01-23 09:38:20 +00003678 __ pop(edx);
3679 StringAddStub stub(STRING_ADD_CHECK_BOTH, NOT_TENURED);
3680 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003681 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003682}
3683
3684
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003685void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3686 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003687 ASSERT_EQ(2, args->length());
3688
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003689 VisitForStackValue(args->at(0));
3690 VisitForStackValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003691
3692 StringCompareStub stub;
3693 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003694 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003695}
3696
3697
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003698void FullCodeGenerator::EmitMathLog(CallRuntime* expr) {
hpayer@chromium.org4f99be92013-12-18 16:23:55 +00003699 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003700 ZoneList<Expression*>* args = expr->arguments();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003701 ASSERT(args->length() == 1);
3702 VisitForStackValue(args->at(0));
hpayer@chromium.org4f99be92013-12-18 16:23:55 +00003703 __ CallRuntime(Runtime::kMath_log, 1);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003704 context()->Plug(eax);
3705}
3706
3707
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003708void FullCodeGenerator::EmitMathSqrt(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003709 // Load the argument on the stack and call the runtime function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003710 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003711 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003712 VisitForStackValue(args->at(0));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003713 __ CallRuntime(Runtime::kMath_sqrt, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003714 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003715}
3716
3717
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003718void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3719 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003720 ASSERT(args->length() >= 2);
3721
danno@chromium.org160a7b02011-04-18 15:51:38 +00003722 int arg_count = args->length() - 2; // 2 ~ receiver and function.
3723 for (int i = 0; i < arg_count + 1; ++i) {
3724 VisitForStackValue(args->at(i));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003725 }
danno@chromium.org160a7b02011-04-18 15:51:38 +00003726 VisitForAccumulatorValue(args->last()); // Function.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003727
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00003728 Label runtime, done;
3729 // Check for non-function argument (including proxy).
3730 __ JumpIfSmi(eax, &runtime);
3731 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
3732 __ j(not_equal, &runtime);
danno@chromium.orgc612e022011-11-10 11:38:15 +00003733
danno@chromium.org160a7b02011-04-18 15:51:38 +00003734 // InvokeFunction requires the function in edi. Move it in there.
3735 __ mov(edi, result_register());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003736 ParameterCount count(arg_count);
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00003737 __ InvokeFunction(edi, count, CALL_FUNCTION, NullCallWrapper());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003738 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00003739 __ jmp(&done);
3740
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00003741 __ bind(&runtime);
danno@chromium.orgc612e022011-11-10 11:38:15 +00003742 __ push(eax);
3743 __ CallRuntime(Runtime::kCall, args->length());
3744 __ bind(&done);
3745
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003746 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003747}
3748
3749
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003750void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003751 // Load the arguments on the stack and call the stub.
3752 RegExpConstructResultStub stub;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003753 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003754 ASSERT(args->length() == 3);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003755 VisitForStackValue(args->at(0));
3756 VisitForStackValue(args->at(1));
machenbach@chromium.org09cae8d2014-01-30 01:05:27 +00003757 VisitForAccumulatorValue(args->at(2));
3758 __ pop(ebx);
3759 __ pop(ecx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003760 __ CallStub(&stub);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003761 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003762}
3763
3764
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003765void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3766 ZoneList<Expression*>* args = expr->arguments();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003767 ASSERT_EQ(2, args->length());
3768
3769 ASSERT_NE(NULL, args->at(0)->AsLiteral());
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00003770 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->value()))->value();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003771
3772 Handle<FixedArray> jsfunction_result_caches(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003773 isolate()->native_context()->jsfunction_result_caches());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003774 if (jsfunction_result_caches->length() <= cache_id) {
danno@chromium.org59400602013-08-13 17:09:37 +00003775 __ Abort(kAttemptToUseUndefinedCache);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003776 __ mov(eax, isolate()->factory()->undefined_value());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003777 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003778 return;
3779 }
3780
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003781 VisitForAccumulatorValue(args->at(1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003782
3783 Register key = eax;
3784 Register cache = ebx;
3785 Register tmp = ecx;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003786 __ mov(cache, ContextOperand(esi, Context::GLOBAL_OBJECT_INDEX));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003787 __ mov(cache,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003788 FieldOperand(cache, GlobalObject::kNativeContextOffset));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00003789 __ mov(cache, ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003790 __ mov(cache,
3791 FieldOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
3792
3793 Label done, not_found;
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003794 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003795 __ mov(tmp, FieldOperand(cache, JSFunctionResultCache::kFingerOffset));
machenbach@chromium.org0a730362014-02-05 03:04:56 +00003796 // tmp now holds finger offset as a smi.
3797 __ cmp(key, FixedArrayElementOperand(cache, tmp));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003798 __ j(not_equal, &not_found);
3799
machenbach@chromium.org0a730362014-02-05 03:04:56 +00003800 __ mov(eax, FixedArrayElementOperand(cache, tmp, 1));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003801 __ jmp(&done);
3802
3803 __ bind(&not_found);
3804 // Call runtime to perform the lookup.
3805 __ push(cache);
3806 __ push(key);
3807 __ CallRuntime(Runtime::kGetFromCache, 2);
3808
3809 __ bind(&done);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003810 context()->Plug(eax);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003811}
3812
3813
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003814void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3815 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003816 ASSERT(args->length() == 1);
3817
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003818 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003819
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003820 __ AssertString(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003821
3822 Label materialize_true, materialize_false;
3823 Label* if_true = NULL;
3824 Label* if_false = NULL;
3825 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003826 context()->PrepareTest(&materialize_true, &materialize_false,
3827 &if_true, &if_false, &fall_through);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003828
3829 __ test(FieldOperand(eax, String::kHashFieldOffset),
3830 Immediate(String::kContainsCachedArrayIndexMask));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003831 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003832 Split(zero, if_true, if_false, fall_through);
3833
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003834 context()->Plug(if_true, if_false);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003835}
3836
3837
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003838void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3839 ZoneList<Expression*>* args = expr->arguments();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003840 ASSERT(args->length() == 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003841 VisitForAccumulatorValue(args->at(0));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003842
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003843 __ AssertString(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003844
3845 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
3846 __ IndexFromHash(eax, eax);
3847
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003848 context()->Plug(eax);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003849}
3850
3851
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003852void FullCodeGenerator::EmitFastAsciiArrayJoin(CallRuntime* expr) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003853 Label bailout, done, one_char_separator, long_separator,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003854 non_trivial_array, not_size_one_array, loop,
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003855 loop_1, loop_1_condition, loop_2, loop_2_entry, loop_3, loop_3_entry;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003856
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003857 ZoneList<Expression*>* args = expr->arguments();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003858 ASSERT(args->length() == 2);
3859 // We will leave the separator on the stack until the end of the function.
3860 VisitForStackValue(args->at(1));
3861 // Load this to eax (= array)
3862 VisitForAccumulatorValue(args->at(0));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003863 // All aliases of the same register have disjoint lifetimes.
3864 Register array = eax;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003865 Register elements = no_reg; // Will be eax.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003866
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003867 Register index = edx;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003868
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003869 Register string_length = ecx;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003870
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003871 Register string = esi;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003872
3873 Register scratch = ebx;
3874
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003875 Register array_length = edi;
3876 Register result_pos = no_reg; // Will be edi.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003877
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003878 // Separator operand is already pushed.
3879 Operand separator_operand = Operand(esp, 2 * kPointerSize);
3880 Operand result_operand = Operand(esp, 1 * kPointerSize);
3881 Operand array_length_operand = Operand(esp, 0);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003882 __ sub(esp, Immediate(2 * kPointerSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003883 __ cld();
3884 // Check that the array is a JSArray
whesse@chromium.org7b260152011-06-20 15:33:18 +00003885 __ JumpIfSmi(array, &bailout);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003886 __ CmpObjectType(array, JS_ARRAY_TYPE, scratch);
3887 __ j(not_equal, &bailout);
3888
3889 // Check that the array has fast elements.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003890 __ CheckFastElements(scratch, &bailout);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003891
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003892 // If the array has length zero, return the empty string.
3893 __ mov(array_length, FieldOperand(array, JSArray::kLengthOffset));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003894 __ SmiUntag(array_length);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003895 __ j(not_zero, &non_trivial_array);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003896 __ mov(result_operand, isolate()->factory()->empty_string());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003897 __ jmp(&done);
3898
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003899 // Save the array length.
3900 __ bind(&non_trivial_array);
3901 __ mov(array_length_operand, array_length);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003902
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003903 // Save the FixedArray containing array's elements.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003904 // End of array's live range.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003905 elements = array;
3906 __ mov(elements, FieldOperand(array, JSArray::kElementsOffset));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003907 array = no_reg;
3908
3909
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003910 // Check that all array elements are sequential ASCII strings, and
3911 // accumulate the sum of their lengths, as a smi-encoded value.
3912 __ Set(index, Immediate(0));
3913 __ Set(string_length, Immediate(0));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003914 // Loop condition: while (index < length).
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003915 // Live loop registers: index, array_length, string,
3916 // scratch, string_length, elements.
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00003917 if (generate_debug_code_) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003918 __ cmp(index, array_length);
danno@chromium.org59400602013-08-13 17:09:37 +00003919 __ Assert(less, kNoEmptyArraysHereInEmitFastAsciiArrayJoin);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003920 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003921 __ bind(&loop);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003922 __ mov(string, FieldOperand(elements,
3923 index,
3924 times_pointer_size,
3925 FixedArray::kHeaderSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003926 __ JumpIfSmi(string, &bailout);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003927 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
3928 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
3929 __ and_(scratch, Immediate(
3930 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003931 __ cmp(scratch, kStringTag | kOneByteStringTag | kSeqStringTag);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003932 __ j(not_equal, &bailout);
3933 __ add(string_length,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003934 FieldOperand(string, SeqOneByteString::kLengthOffset));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003935 __ j(overflow, &bailout);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003936 __ add(index, Immediate(1));
3937 __ cmp(index, array_length);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003938 __ j(less, &loop);
3939
3940 // If array_length is 1, return elements[0], a string.
3941 __ cmp(array_length, 1);
3942 __ j(not_equal, &not_size_one_array);
3943 __ mov(scratch, FieldOperand(elements, FixedArray::kHeaderSize));
3944 __ mov(result_operand, scratch);
3945 __ jmp(&done);
3946
3947 __ bind(&not_size_one_array);
3948
3949 // End of array_length live range.
3950 result_pos = array_length;
3951 array_length = no_reg;
3952
3953 // Live registers:
3954 // string_length: Sum of string lengths, as a smi.
3955 // elements: FixedArray of strings.
3956
3957 // Check that the separator is a flat ASCII string.
3958 __ mov(string, separator_operand);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003959 __ JumpIfSmi(string, &bailout);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003960 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
3961 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003962 __ and_(scratch, Immediate(
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003963 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00003964 __ cmp(scratch, kStringTag | kOneByteStringTag | kSeqStringTag);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003965 __ j(not_equal, &bailout);
3966
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003967 // Add (separator length times array_length) - separator length
3968 // to string_length.
3969 __ mov(scratch, separator_operand);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003970 __ mov(scratch, FieldOperand(scratch, SeqOneByteString::kLengthOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003971 __ sub(string_length, scratch); // May be negative, temporarily.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003972 __ imul(scratch, array_length_operand);
3973 __ j(overflow, &bailout);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003974 __ add(string_length, scratch);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003975 __ j(overflow, &bailout);
3976
3977 __ shr(string_length, 1);
3978 // Live registers and stack values:
3979 // string_length
3980 // elements
3981 __ AllocateAsciiString(result_pos, string_length, scratch,
3982 index, string, &bailout);
3983 __ mov(result_operand, result_pos);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003984 __ lea(result_pos, FieldOperand(result_pos, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003985
3986
3987 __ mov(string, separator_operand);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003988 __ cmp(FieldOperand(string, SeqOneByteString::kLengthOffset),
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003989 Immediate(Smi::FromInt(1)));
3990 __ j(equal, &one_char_separator);
3991 __ j(greater, &long_separator);
3992
3993
3994 // Empty separator case
3995 __ mov(index, Immediate(0));
3996 __ jmp(&loop_1_condition);
3997 // Loop condition: while (index < length).
3998 __ bind(&loop_1);
3999 // Each iteration of the loop concatenates one string to the result.
4000 // Live values in registers:
4001 // index: which element of the elements array we are adding to the result.
4002 // result_pos: the position to which we are currently copying characters.
4003 // elements: the FixedArray of strings we are joining.
4004
4005 // Get string = array[index].
4006 __ mov(string, FieldOperand(elements, index,
4007 times_pointer_size,
4008 FixedArray::kHeaderSize));
4009 __ mov(string_length,
4010 FieldOperand(string, String::kLengthOffset));
4011 __ shr(string_length, 1);
4012 __ lea(string,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004013 FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004014 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004015 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004016 __ bind(&loop_1_condition);
4017 __ cmp(index, array_length_operand);
4018 __ j(less, &loop_1); // End while (index < length).
4019 __ jmp(&done);
4020
4021
4022
4023 // One-character separator case
4024 __ bind(&one_char_separator);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00004025 // Replace separator with its ASCII character value.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004026 __ mov_b(scratch, FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004027 __ mov_b(separator_operand, scratch);
4028
4029 __ Set(index, Immediate(0));
4030 // Jump into the loop after the code that copies the separator, so the first
4031 // element is not preceded by a separator
4032 __ jmp(&loop_2_entry);
4033 // Loop condition: while (index < length).
4034 __ bind(&loop_2);
4035 // Each iteration of the loop concatenates one string to the result.
4036 // Live values in registers:
4037 // index: which element of the elements array we are adding to the result.
4038 // result_pos: the position to which we are currently copying characters.
4039
4040 // Copy the separator character to the result.
4041 __ mov_b(scratch, separator_operand);
4042 __ mov_b(Operand(result_pos, 0), scratch);
4043 __ inc(result_pos);
4044
4045 __ bind(&loop_2_entry);
4046 // Get string = array[index].
4047 __ mov(string, FieldOperand(elements, index,
4048 times_pointer_size,
4049 FixedArray::kHeaderSize));
4050 __ mov(string_length,
4051 FieldOperand(string, String::kLengthOffset));
4052 __ shr(string_length, 1);
4053 __ lea(string,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004054 FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004055 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004056 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004057
4058 __ cmp(index, array_length_operand);
4059 __ j(less, &loop_2); // End while (index < length).
4060 __ jmp(&done);
4061
4062
4063 // Long separator case (separator is more than one character).
4064 __ bind(&long_separator);
4065
4066 __ Set(index, Immediate(0));
4067 // Jump into the loop after the code that copies the separator, so the first
4068 // element is not preceded by a separator
4069 __ jmp(&loop_3_entry);
4070 // Loop condition: while (index < length).
4071 __ bind(&loop_3);
4072 // Each iteration of the loop concatenates one string to the result.
4073 // Live values in registers:
4074 // index: which element of the elements array we are adding to the result.
4075 // result_pos: the position to which we are currently copying characters.
4076
4077 // Copy the separator to the result.
4078 __ mov(string, separator_operand);
4079 __ mov(string_length,
4080 FieldOperand(string, String::kLengthOffset));
4081 __ shr(string_length, 1);
4082 __ lea(string,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004083 FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004084 __ CopyBytes(string, result_pos, string_length, scratch);
4085
4086 __ bind(&loop_3_entry);
4087 // Get string = array[index].
4088 __ mov(string, FieldOperand(elements, index,
4089 times_pointer_size,
4090 FixedArray::kHeaderSize));
4091 __ mov(string_length,
4092 FieldOperand(string, String::kLengthOffset));
4093 __ shr(string_length, 1);
4094 __ lea(string,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004095 FieldOperand(string, SeqOneByteString::kHeaderSize));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004096 __ CopyBytes(string, result_pos, string_length, scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004097 __ add(index, Immediate(1));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004098
4099 __ cmp(index, array_length_operand);
4100 __ j(less, &loop_3); // End while (index < length).
4101 __ jmp(&done);
4102
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00004103
4104 __ bind(&bailout);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004105 __ mov(result_operand, isolate()->factory()->undefined_value());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00004106 __ bind(&done);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004107 __ mov(eax, result_operand);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00004108 // Drop temp values from the stack, and restore context register.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004109 __ add(esp, Immediate(3 * kPointerSize));
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00004110
4111 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
4112 context()->Plug(eax);
4113}
4114
4115
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004116void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004117 Handle<String> name = expr->name();
4118 if (name->length() > 0 && name->Get(0) == '_') {
4119 Comment cmnt(masm_, "[ InlineRuntimeCall");
4120 EmitInlineRuntimeCall(expr);
4121 return;
4122 }
4123
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004124 Comment cmnt(masm_, "[ CallRuntime");
4125 ZoneList<Expression*>* args = expr->arguments();
4126
4127 if (expr->is_jsruntime()) {
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00004128 // Push the builtins object as receiver.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004129 __ mov(eax, GlobalObjectOperand());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004130 __ push(FieldOperand(eax, GlobalObject::kBuiltinsOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004131
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00004132 // Load the function from the receiver.
4133 __ mov(edx, Operand(esp, 0));
4134 __ mov(ecx, Immediate(expr->name()));
4135 CallLoadIC(NOT_CONTEXTUAL, expr->CallRuntimeFeedbackId());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004136
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00004137 // Push the target function under the receiver.
4138 __ push(Operand(esp, 0));
4139 __ mov(Operand(esp, kPointerSize), eax);
4140
4141 // Code common for calls using the IC.
4142 ZoneList<Expression*>* args = expr->arguments();
4143 int arg_count = args->length();
4144 for (int i = 0; i < arg_count; i++) {
4145 VisitForStackValue(args->at(i));
4146 }
4147
4148 // Record source position of the IC call.
4149 SetSourcePosition(expr->position());
4150 CallFunctionStub stub(arg_count, NO_CALL_FUNCTION_FLAGS);
4151 __ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
4152 __ CallStub(&stub);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004153 // Restore context register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004154 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00004155 context()->DropAndPlug(1, eax);
4156
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004157 } else {
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00004158 // Push the arguments ("left-to-right").
4159 int arg_count = args->length();
4160 for (int i = 0; i < arg_count; i++) {
4161 VisitForStackValue(args->at(i));
4162 }
4163
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004164 // Call the C runtime function.
4165 __ CallRuntime(expr->function(), arg_count);
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00004166
4167 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004168 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004169}
4170
4171
4172void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
4173 switch (expr->op()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004174 case Token::DELETE: {
4175 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004176 Property* property = expr->expression()->AsProperty();
4177 VariableProxy* proxy = expr->expression()->AsVariableProxy();
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004178
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004179 if (property != NULL) {
4180 VisitForStackValue(property->obj());
4181 VisitForStackValue(property->key());
dslomov@chromium.org486536d2014-03-12 13:09:18 +00004182 __ push(Immediate(Smi::FromInt(strict_mode())));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004183 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004184 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004185 } else if (proxy != NULL) {
4186 Variable* var = proxy->var();
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004187 // Delete of an unqualified identifier is disallowed in strict mode
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004188 // but "delete this" is allowed.
dslomov@chromium.org486536d2014-03-12 13:09:18 +00004189 ASSERT(strict_mode() == SLOPPY || var->is_this());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004190 if (var->IsUnallocated()) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004191 __ push(GlobalObjectOperand());
4192 __ push(Immediate(var->name()));
dslomov@chromium.org486536d2014-03-12 13:09:18 +00004193 __ push(Immediate(Smi::FromInt(SLOPPY)));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004194 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4195 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004196 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
4197 // Result of deleting non-global variables is false. 'this' is
4198 // not really a variable, though we implement it as one. The
4199 // subexpression does not have side effects.
4200 context()->Plug(var->is_this());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004201 } else {
4202 // Non-global variable. Call the runtime to try to delete from the
4203 // context where the variable was introduced.
4204 __ push(context_register());
4205 __ push(Immediate(var->name()));
4206 __ CallRuntime(Runtime::kDeleteContextSlot, 2);
4207 context()->Plug(eax);
4208 }
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00004209 } else {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004210 // Result of deleting non-property, non-variable reference is true.
4211 // The subexpression may have side effects.
4212 VisitForEffect(expr->expression());
4213 context()->Plug(true);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004214 }
4215 break;
4216 }
4217
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004218 case Token::VOID: {
4219 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4220 VisitForEffect(expr->expression());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004221 context()->Plug(isolate()->factory()->undefined_value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004222 break;
4223 }
4224
4225 case Token::NOT: {
4226 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00004227 if (context()->IsEffect()) {
4228 // Unary NOT has no side effects so it's only necessary to visit the
4229 // subexpression. Match the optimizing compiler by not branching.
4230 VisitForEffect(expr->expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004231 } else if (context()->IsTest()) {
4232 const TestContext* test = TestContext::cast(context());
4233 // The labels are swapped for the recursive call.
4234 VisitForControl(expr->expression(),
4235 test->false_label(),
4236 test->true_label(),
4237 test->fall_through());
4238 context()->Plug(test->true_label(), test->false_label());
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00004239 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004240 // We handle value contexts explicitly rather than simply visiting
4241 // for control and plugging the control flow into the context,
4242 // because we need to prepare a pair of extra administrative AST ids
4243 // for the optimizing compiler.
4244 ASSERT(context()->IsAccumulatorValue() || context()->IsStackValue());
4245 Label materialize_true, materialize_false, done;
4246 VisitForControl(expr->expression(),
4247 &materialize_false,
4248 &materialize_true,
4249 &materialize_true);
4250 __ bind(&materialize_true);
4251 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4252 if (context()->IsAccumulatorValue()) {
4253 __ mov(eax, isolate()->factory()->true_value());
4254 } else {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00004255 __ Push(isolate()->factory()->true_value());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004256 }
4257 __ jmp(&done, Label::kNear);
4258 __ bind(&materialize_false);
4259 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4260 if (context()->IsAccumulatorValue()) {
4261 __ mov(eax, isolate()->factory()->false_value());
4262 } else {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00004263 __ Push(isolate()->factory()->false_value());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004264 }
4265 __ bind(&done);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00004266 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004267 break;
4268 }
4269
4270 case Token::TYPEOF: {
4271 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004272 { StackValueContext context(this);
4273 VisitForTypeofValue(expr->expression());
4274 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004275 __ CallRuntime(Runtime::kTypeof, 1);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004276 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004277 break;
4278 }
4279
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004280 default:
4281 UNREACHABLE();
4282 }
4283}
4284
4285
4286void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
4287 Comment cmnt(masm_, "[ CountOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00004288 SetSourcePosition(expr->position());
4289
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004290 // Invalid left-hand sides are rewritten to have a 'throw ReferenceError'
4291 // as the left-hand side.
4292 if (!expr->expression()->IsValidLeftHandSide()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004293 VisitForEffect(expr->expression());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004294 return;
4295 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004296
4297 // Expression can only be a property, a global or a (parameter or local)
whesse@chromium.org7b260152011-06-20 15:33:18 +00004298 // slot.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004299 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
4300 LhsKind assign_type = VARIABLE;
4301 Property* prop = expr->expression()->AsProperty();
4302 // In case of a property we use the uninitialized expression context
4303 // of the key to detect a named property.
4304 if (prop != NULL) {
4305 assign_type =
4306 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
4307 }
4308
4309 // Evaluate expression and get value.
4310 if (assign_type == VARIABLE) {
4311 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004312 AccumulatorValueContext context(this);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00004313 EmitVariableLoad(expr->expression()->AsVariableProxy());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004314 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004315 // Reserve space for result of postfix operation.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004316 if (expr->is_postfix() && !context()->IsEffect()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004317 __ push(Immediate(Smi::FromInt(0)));
4318 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004319 if (assign_type == NAMED_PROPERTY) {
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004320 // Put the object both on the stack and in edx.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004321 VisitForAccumulatorValue(prop->obj());
ager@chromium.org5c838252010-02-19 08:53:10 +00004322 __ push(eax);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004323 __ mov(edx, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004324 EmitNamedPropertyLoad(prop);
4325 } else {
whesse@chromium.org7b260152011-06-20 15:33:18 +00004326 VisitForStackValue(prop->obj());
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004327 VisitForStackValue(prop->key());
4328 __ mov(edx, Operand(esp, kPointerSize)); // Object.
4329 __ mov(ecx, Operand(esp, 0)); // Key.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004330 EmitKeyedPropertyLoad(prop);
4331 }
4332 }
4333
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004334 // We need a second deoptimization point after loading the value
4335 // in case evaluating the property load my have a side effect.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004336 if (assign_type == VARIABLE) {
4337 PrepareForBailout(expr->expression(), TOS_REG);
4338 } else {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00004339 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004340 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004341
machenbach@chromium.orge8412be2013-11-08 10:23:52 +00004342 // Inline smi case if we are in a loop.
4343 Label done, stub_call;
4344 JumpPatchSite patch_site(masm_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004345 if (ShouldInlineSmiCase(expr->op())) {
machenbach@chromium.orge8412be2013-11-08 10:23:52 +00004346 Label slow;
4347 patch_site.EmitJumpIfNotSmi(eax, &slow, Label::kNear);
4348
4349 // Save result for postfix expressions.
4350 if (expr->is_postfix()) {
4351 if (!context()->IsEffect()) {
4352 // Save the result on the stack. If we have a named or keyed property
4353 // we store the result under the receiver that is currently on top
4354 // of the stack.
4355 switch (assign_type) {
4356 case VARIABLE:
4357 __ push(eax);
4358 break;
4359 case NAMED_PROPERTY:
4360 __ mov(Operand(esp, kPointerSize), eax);
4361 break;
4362 case KEYED_PROPERTY:
4363 __ mov(Operand(esp, 2 * kPointerSize), eax);
4364 break;
4365 }
4366 }
4367 }
4368
4369 if (expr->op() == Token::INC) {
4370 __ add(eax, Immediate(Smi::FromInt(1)));
4371 } else {
4372 __ sub(eax, Immediate(Smi::FromInt(1)));
4373 }
4374 __ j(no_overflow, &done, Label::kNear);
4375 // Call stub. Undo operation first.
4376 if (expr->op() == Token::INC) {
4377 __ sub(eax, Immediate(Smi::FromInt(1)));
4378 } else {
4379 __ add(eax, Immediate(Smi::FromInt(1)));
4380 }
4381 __ jmp(&stub_call, Label::kNear);
4382 __ bind(&slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004383 }
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004384 ToNumberStub convert_stub;
4385 __ CallStub(&convert_stub);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004386
4387 // Save result for postfix expressions.
4388 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004389 if (!context()->IsEffect()) {
4390 // Save the result on the stack. If we have a named or keyed property
4391 // we store the result under the receiver that is currently on top
4392 // of the stack.
4393 switch (assign_type) {
4394 case VARIABLE:
4395 __ push(eax);
4396 break;
4397 case NAMED_PROPERTY:
4398 __ mov(Operand(esp, kPointerSize), eax);
4399 break;
4400 case KEYED_PROPERTY:
4401 __ mov(Operand(esp, 2 * kPointerSize), eax);
4402 break;
4403 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004404 }
4405 }
4406
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004407 // Record position before stub call.
4408 SetSourcePosition(expr->position());
4409
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004410 // Call stub for +1/-1.
machenbach@chromium.orge8412be2013-11-08 10:23:52 +00004411 __ bind(&stub_call);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004412 __ mov(edx, eax);
4413 __ mov(eax, Immediate(Smi::FromInt(1)));
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +00004414 BinaryOpICStub stub(expr->binary_op(), NO_OVERWRITE);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004415 CallIC(stub.GetCode(isolate()), expr->CountBinOpFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004416 patch_site.EmitPatchInfo();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004417 __ bind(&done);
4418
4419 // Store the value returned in eax.
4420 switch (assign_type) {
4421 case VARIABLE:
4422 if (expr->is_postfix()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004423 // Perform the assignment as if via '='.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004424 { EffectContext context(this);
4425 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4426 Token::ASSIGN);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004427 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4428 context.Plug(eax);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004429 }
4430 // For all contexts except EffectContext We have the result on
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004431 // top of the stack.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004432 if (!context()->IsEffect()) {
4433 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004434 }
4435 } else {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004436 // Perform the assignment as if via '='.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004437 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004438 Token::ASSIGN);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004439 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4440 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004441 }
4442 break;
4443 case NAMED_PROPERTY: {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00004444 __ mov(ecx, prop->key()->AsLiteral()->value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004445 __ pop(edx);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004446 CallStoreIC(expr->CountStoreFeedbackId());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004447 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004448 if (expr->is_postfix()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004449 if (!context()->IsEffect()) {
4450 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004451 }
4452 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004453 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004454 }
4455 break;
4456 }
4457 case KEYED_PROPERTY: {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004458 __ pop(ecx);
4459 __ pop(edx);
dslomov@chromium.org486536d2014-03-12 13:09:18 +00004460 Handle<Code> ic = strict_mode() == SLOPPY
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004461 ? isolate()->builtins()->KeyedStoreIC_Initialize()
4462 : isolate()->builtins()->KeyedStoreIC_Initialize_Strict();
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004463 CallIC(ic, expr->CountStoreFeedbackId());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004464 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004465 if (expr->is_postfix()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004466 // Result is on the stack
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004467 if (!context()->IsEffect()) {
4468 context()->PlugTOS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004469 }
4470 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004471 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004472 }
4473 break;
4474 }
4475 }
4476}
4477
4478
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004479void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004480 VariableProxy* proxy = expr->AsVariableProxy();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004481 ASSERT(!context()->IsEffect());
4482 ASSERT(!context()->IsTest());
4483
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004484 if (proxy != NULL && proxy->var()->IsUnallocated()) {
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004485 Comment cmnt(masm_, "[ Global variable");
danno@chromium.org1044a4d2012-04-30 12:34:39 +00004486 __ mov(edx, GlobalObjectOperand());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004487 __ mov(ecx, Immediate(proxy->name()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004488 // Use a regular load, not a contextual load, to avoid a reference
4489 // error.
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +00004490 CallLoadIC(NOT_CONTEXTUAL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004491 PrepareForBailout(expr, TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004492 context()->Plug(eax);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004493 } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004494 Comment cmnt(masm_, "[ Lookup slot");
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004495 Label done, slow;
4496
4497 // Generate code for loading from variables potentially shadowed
4498 // by eval-introduced variables.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004499 EmitDynamicLookupFastCase(proxy->var(), INSIDE_TYPEOF, &slow, &done);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004500
4501 __ bind(&slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004502 __ push(esi);
4503 __ push(Immediate(proxy->name()));
4504 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004505 PrepareForBailout(expr, TOS_REG);
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004506 __ bind(&done);
4507
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004508 context()->Plug(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004509 } else {
4510 // This expression cannot throw a reference error at the top level.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004511 VisitInDuplicateContext(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004512 }
4513}
4514
4515
ager@chromium.org04921a82011-06-27 13:21:41 +00004516void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004517 Expression* sub_expr,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004518 Handle<String> check) {
4519 Label materialize_true, materialize_false;
4520 Label* if_true = NULL;
4521 Label* if_false = NULL;
4522 Label* fall_through = NULL;
4523 context()->PrepareTest(&materialize_true, &materialize_false,
4524 &if_true, &if_false, &fall_through);
4525
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004526 { AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004527 VisitForTypeofValue(sub_expr);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004528 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004529 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004530
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004531 if (check->Equals(isolate()->heap()->number_string())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004532 __ JumpIfSmi(eax, if_true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004533 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004534 isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004535 Split(equal, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004536 } else if (check->Equals(isolate()->heap()->string_string())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004537 __ JumpIfSmi(eax, if_false);
4538 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edx);
4539 __ j(above_equal, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004540 // Check for undetectable objects => false.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004541 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
4542 1 << Map::kIsUndetectable);
4543 Split(zero, if_true, if_false, fall_through);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00004544 } else if (check->Equals(isolate()->heap()->symbol_string())) {
4545 __ JumpIfSmi(eax, if_false);
4546 __ CmpObjectType(eax, SYMBOL_TYPE, edx);
4547 Split(equal, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004548 } else if (check->Equals(isolate()->heap()->boolean_string())) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004549 __ cmp(eax, isolate()->factory()->true_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004550 __ j(equal, if_true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004551 __ cmp(eax, isolate()->factory()->false_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004552 Split(equal, if_true, if_false, fall_through);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004553 } else if (FLAG_harmony_typeof &&
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004554 check->Equals(isolate()->heap()->null_string())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004555 __ cmp(eax, isolate()->factory()->null_value());
4556 Split(equal, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004557 } else if (check->Equals(isolate()->heap()->undefined_string())) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004558 __ cmp(eax, isolate()->factory()->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004559 __ j(equal, if_true);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004560 __ JumpIfSmi(eax, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004561 // Check for undetectable objects => true.
4562 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
4563 __ movzx_b(ecx, FieldOperand(edx, Map::kBitFieldOffset));
4564 __ test(ecx, Immediate(1 << Map::kIsUndetectable));
4565 Split(not_zero, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004566 } else if (check->Equals(isolate()->heap()->function_string())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004567 __ JumpIfSmi(eax, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004568 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4569 __ CmpObjectType(eax, JS_FUNCTION_TYPE, edx);
4570 __ j(equal, if_true);
4571 __ CmpInstanceType(edx, JS_FUNCTION_PROXY_TYPE);
4572 Split(equal, if_true, if_false, fall_through);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004573 } else if (check->Equals(isolate()->heap()->object_string())) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004574 __ JumpIfSmi(eax, if_false);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004575 if (!FLAG_harmony_typeof) {
4576 __ cmp(eax, isolate()->factory()->null_value());
4577 __ j(equal, if_true);
4578 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004579 __ CmpObjectType(eax, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE, edx);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004580 __ j(below, if_false);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004581 __ CmpInstanceType(edx, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
4582 __ j(above, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004583 // Check for undetectable objects => false.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00004584 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
4585 1 << Map::kIsUndetectable);
4586 Split(zero, if_true, if_false, fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004587 } else {
4588 if (if_false != fall_through) __ jmp(if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004589 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004590 context()->Plug(if_true, if_false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004591}
4592
4593
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004594void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
4595 Comment cmnt(masm_, "[ CompareOperation");
ricow@chromium.org65fae842010-08-25 15:26:24 +00004596 SetSourcePosition(expr->position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004597
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004598 // First we try a fast inlined version of the compare when one of
4599 // the operands is a literal.
4600 if (TryLiteralCompare(expr)) return;
4601
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004602 // Always perform the comparison for its control flow. Pack the result
4603 // into the expression's context after the comparison is performed.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004604 Label materialize_true, materialize_false;
4605 Label* if_true = NULL;
4606 Label* if_false = NULL;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004607 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004608 context()->PrepareTest(&materialize_true, &materialize_false,
4609 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004610
ager@chromium.org04921a82011-06-27 13:21:41 +00004611 Token::Value op = expr->op();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004612 VisitForStackValue(expr->left());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004613 switch (op) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004614 case Token::IN:
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004615 VisitForStackValue(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004616 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004617 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004618 __ cmp(eax, isolate()->factory()->true_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004619 Split(equal, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004620 break;
4621
4622 case Token::INSTANCEOF: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004623 VisitForStackValue(expr->right());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004624 InstanceofStub stub(InstanceofStub::kNoFlags);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004625 __ CallStub(&stub);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004626 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004627 __ test(eax, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004628 // The stub returns 0 for true.
4629 Split(zero, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004630 break;
4631 }
4632
4633 default: {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004634 VisitForAccumulatorValue(expr->right());
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004635 Condition cc = CompareIC::ComputeCondition(op);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004636 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004637
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004638 bool inline_smi_code = ShouldInlineSmiCase(op);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004639 JumpPatchSite patch_site(masm_);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004640 if (inline_smi_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004641 Label slow_case;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004642 __ mov(ecx, edx);
4643 __ or_(ecx, eax);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004644 patch_site.EmitJumpIfNotSmi(ecx, &slow_case, Label::kNear);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004645 __ cmp(edx, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004646 Split(cc, if_true, if_false, NULL);
4647 __ bind(&slow_case);
4648 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004649
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004650 // Record position and call the compare IC.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004651 SetSourcePosition(expr->position());
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004652 Handle<Code> ic = CompareIC::GetUninitialized(isolate(), op);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004653 CallIC(ic, expr->CompareOperationFeedbackId());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004654 patch_site.EmitPatchInfo();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004655
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004656 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004657 __ test(eax, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004658 Split(cc, if_true, if_false, fall_through);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004659 }
4660 }
4661
4662 // Convert the result of the comparison into one expected for this
4663 // expression's context.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004664 context()->Plug(if_true, if_false);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004665}
4666
4667
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004668void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4669 Expression* sub_expr,
4670 NilValue nil) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004671 Label materialize_true, materialize_false;
4672 Label* if_true = NULL;
4673 Label* if_false = NULL;
4674 Label* fall_through = NULL;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004675 context()->PrepareTest(&materialize_true, &materialize_false,
4676 &if_true, &if_false, &fall_through);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004677
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004678 VisitForAccumulatorValue(sub_expr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004679 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004680
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004681 Handle<Object> nil_value = nil == kNullValue
4682 ? isolate()->factory()->null_value()
4683 : isolate()->factory()->undefined_value();
ulan@chromium.org837a67e2013-06-11 15:39:48 +00004684 if (expr->op() == Token::EQ_STRICT) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004685 __ cmp(eax, nil_value);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004686 Split(equal, if_true, if_false, fall_through);
4687 } else {
ulan@chromium.org837a67e2013-06-11 15:39:48 +00004688 Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(), nil);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004689 CallIC(ic, expr->CompareOperationFeedbackId());
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004690 __ test(eax, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004691 Split(not_zero, if_true, if_false, fall_through);
4692 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004693 context()->Plug(if_true, if_false);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004694}
4695
4696
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004697void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
4698 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004699 context()->Plug(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004700}
4701
4702
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00004703Register FullCodeGenerator::result_register() {
4704 return eax;
4705}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004706
4707
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00004708Register FullCodeGenerator::context_register() {
4709 return esi;
4710}
4711
4712
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004713void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
4714 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4715 __ mov(Operand(ebp, frame_offset), value);
4716}
4717
4718
4719void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00004720 __ mov(dst, ContextOperand(esi, context_index));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004721}
4722
4723
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004724void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004725 Scope* declaration_scope = scope()->DeclarationScope();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00004726 if (declaration_scope->is_global_scope() ||
4727 declaration_scope->is_module_scope()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00004728 // Contexts nested in the native context have a canonical empty function
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004729 // as their closure, not the anonymous closure containing the global
4730 // code. Pass a smi sentinel and let the runtime look up the empty
4731 // function.
4732 __ push(Immediate(Smi::FromInt(0)));
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004733 } else if (declaration_scope->is_eval_scope()) {
4734 // Contexts nested inside eval code have the same closure as the context
4735 // calling eval, not the anonymous closure containing the eval code.
4736 // Fetch it from the context.
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004737 __ push(ContextOperand(esi, Context::CLOSURE_INDEX));
4738 } else {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004739 ASSERT(declaration_scope->is_function_scope());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00004740 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
4741 }
4742}
4743
4744
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004745// ----------------------------------------------------------------------------
4746// Non-local control flow support.
4747
4748void FullCodeGenerator::EnterFinallyBlock() {
4749 // Cook return address on top of stack (smi encoded Code* delta)
4750 ASSERT(!result_register().is(edx));
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004751 __ pop(edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004752 __ sub(edx, Immediate(masm_->CodeObject()));
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00004753 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4754 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004755 __ SmiTag(edx);
4756 __ push(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004757
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004758 // Store result register while executing finally block.
4759 __ push(result_register());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004760
4761 // Store pending message while executing finally block.
4762 ExternalReference pending_message_obj =
4763 ExternalReference::address_of_pending_message_obj(isolate());
4764 __ mov(edx, Operand::StaticVariable(pending_message_obj));
4765 __ push(edx);
4766
4767 ExternalReference has_pending_message =
4768 ExternalReference::address_of_has_pending_message(isolate());
4769 __ mov(edx, Operand::StaticVariable(has_pending_message));
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004770 __ SmiTag(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004771 __ push(edx);
4772
4773 ExternalReference pending_message_script =
4774 ExternalReference::address_of_pending_message_script(isolate());
4775 __ mov(edx, Operand::StaticVariable(pending_message_script));
4776 __ push(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004777}
4778
4779
4780void FullCodeGenerator::ExitFinallyBlock() {
4781 ASSERT(!result_register().is(edx));
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004782 // Restore pending message from stack.
4783 __ pop(edx);
4784 ExternalReference pending_message_script =
4785 ExternalReference::address_of_pending_message_script(isolate());
4786 __ mov(Operand::StaticVariable(pending_message_script), edx);
4787
4788 __ pop(edx);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004789 __ SmiUntag(edx);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004790 ExternalReference has_pending_message =
4791 ExternalReference::address_of_has_pending_message(isolate());
4792 __ mov(Operand::StaticVariable(has_pending_message), edx);
4793
4794 __ pop(edx);
4795 ExternalReference pending_message_obj =
4796 ExternalReference::address_of_pending_message_obj(isolate());
4797 __ mov(Operand::StaticVariable(pending_message_obj), edx);
4798
4799 // Restore result register from stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004800 __ pop(result_register());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00004801
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004802 // Uncook return address.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004803 __ pop(edx);
4804 __ SmiUntag(edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004805 __ add(edx, Immediate(masm_->CodeObject()));
4806 __ jmp(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004807}
4808
4809
4810#undef __
4811
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004812#define __ ACCESS_MASM(masm())
4813
4814FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
4815 int* stack_depth,
4816 int* context_length) {
4817 // The macros used here must preserve the result register.
4818
4819 // Because the handler block contains the context of the finally
4820 // code, we can restore it directly from there for the finally code
4821 // rather than iteratively unwinding contexts via their previous
4822 // links.
4823 __ Drop(*stack_depth); // Down to the handler block.
4824 if (*context_length > 0) {
4825 // Restore the context to its dedicated register and the stack.
4826 __ mov(esi, Operand(esp, StackHandlerConstants::kContextOffset));
4827 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
4828 }
4829 __ PopTryHandler();
4830 __ call(finally_entry_);
4831
4832 *stack_depth = 0;
4833 *context_length = 0;
4834 return previous_;
4835}
4836
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004837#undef __
4838
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004839
4840static const byte kJnsInstruction = 0x79;
4841static const byte kJnsOffset = 0x11;
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004842static const byte kNopByteOne = 0x66;
4843static const byte kNopByteTwo = 0x90;
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +00004844#ifdef DEBUG
4845static const byte kCallInstruction = 0xe8;
4846#endif
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004847
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004848
4849void BackEdgeTable::PatchAt(Code* unoptimized_code,
4850 Address pc,
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00004851 BackEdgeState target_state,
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004852 Code* replacement_code) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004853 Address call_target_address = pc - kIntSize;
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00004854 Address jns_instr_address = call_target_address - 3;
4855 Address jns_offset_address = call_target_address - 2;
4856
4857 switch (target_state) {
4858 case INTERRUPT:
4859 // sub <profiling_counter>, <delta> ;; Not changed
4860 // jns ok
4861 // call <interrupt stub>
4862 // ok:
4863 *jns_instr_address = kJnsInstruction;
4864 *jns_offset_address = kJnsOffset;
4865 break;
4866 case ON_STACK_REPLACEMENT:
4867 case OSR_AFTER_STACK_CHECK:
4868 // sub <profiling_counter>, <delta> ;; Not changed
4869 // nop
4870 // nop
4871 // call <on-stack replacment>
4872 // ok:
4873 *jns_instr_address = kNopByteOne;
4874 *jns_offset_address = kNopByteTwo;
4875 break;
4876 }
4877
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004878 Assembler::set_target_address_at(call_target_address,
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00004879 unoptimized_code,
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004880 replacement_code->entry());
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004881 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
4882 unoptimized_code, call_target_address, replacement_code);
4883}
4884
4885
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004886BackEdgeTable::BackEdgeState BackEdgeTable::GetBackEdgeState(
4887 Isolate* isolate,
4888 Code* unoptimized_code,
4889 Address pc) {
4890 Address call_target_address = pc - kIntSize;
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00004891 Address jns_instr_address = call_target_address - 3;
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004892 ASSERT_EQ(kCallInstruction, *(call_target_address - 1));
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00004893
4894 if (*jns_instr_address == kJnsInstruction) {
4895 ASSERT_EQ(kJnsOffset, *(call_target_address - 2));
4896 ASSERT_EQ(isolate->builtins()->InterruptCheck()->entry(),
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00004897 Assembler::target_address_at(call_target_address,
4898 unoptimized_code));
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004899 return INTERRUPT;
4900 }
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00004901
4902 ASSERT_EQ(kNopByteOne, *jns_instr_address);
4903 ASSERT_EQ(kNopByteTwo, *(call_target_address - 2));
4904
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00004905 if (Assembler::target_address_at(call_target_address, unoptimized_code) ==
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00004906 isolate->builtins()->OnStackReplacement()->entry()) {
4907 return ON_STACK_REPLACEMENT;
4908 }
4909
4910 ASSERT_EQ(isolate->builtins()->OsrAfterStackCheck()->entry(),
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00004911 Assembler::target_address_at(call_target_address,
4912 unoptimized_code));
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00004913 return OSR_AFTER_STACK_CHECK;
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004914}
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004915
4916
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004917} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004918
4919#endif // V8_TARGET_ARCH_IA32